Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_RENDERER_BACKEND_IDEVICE 19 : #define INCLUDED_RENDERER_BACKEND_IDEVICE 20 : 21 : #include "graphics/Color.h" 22 : #include "renderer/backend/IBuffer.h" 23 : #include "renderer/backend/IDevice.h" 24 : #include "renderer/backend/ITexture.h" 25 : 26 : #include <cstdint> 27 : #include <js/TypeDecls.h> 28 : #include <memory> 29 : #include <span> 30 : #include <string> 31 : #include <variant> 32 : #include <vector> 33 : 34 : class CShaderDefines; 35 : class CStr; 36 : namespace Renderer::Backend { class IComputePipelineState; } 37 : namespace Renderer::Backend { class IDeviceCommandContext; } 38 : namespace Renderer::Backend { class IFramebuffer; } 39 : namespace Renderer::Backend { class IGraphicsPipelineState; } 40 : namespace Renderer::Backend { class IShaderProgram; } 41 : namespace Renderer::Backend { class ISwapChain; } 42 : namespace Renderer::Backend { class IVertexInputLayout; } 43 : namespace Renderer::Backend { enum class AttachmentLoadOp; } 44 : namespace Renderer::Backend { enum class AttachmentStoreOp; } 45 : namespace Renderer::Backend { enum class Backend; } 46 : namespace Renderer::Backend { enum class Format; } 47 : namespace Renderer::Backend { struct SColorAttachment; } 48 : namespace Renderer::Backend { struct SComputePipelineStateDesc; } 49 : namespace Renderer::Backend { struct SDepthStencilAttachment; } 50 : namespace Renderer::Backend { struct SGraphicsPipelineStateDesc; } 51 : namespace Renderer::Backend { struct SVertexAttributeFormat; } 52 : namespace Renderer::Backend::Sampler { struct Desc; } 53 : namespace Script { class Request; } 54 : 55 : typedef struct SDL_Window SDL_Window; 56 : 57 : namespace Renderer 58 : { 59 : 60 : namespace Backend 61 : { 62 : 63 12 : class IDevice 64 : { 65 : public: 66 : struct Capabilities 67 : { 68 : bool S3TC; 69 : bool computeShaders; 70 : bool debugLabels; 71 : bool debugScopedLabels; 72 : bool multisampling; 73 : bool anisotropicFiltering; 74 : uint32_t maxSampleCount; 75 : float maxAnisotropy; 76 : uint32_t maxTextureSize; 77 : bool instancing; 78 : bool storage; 79 : bool timestamps; 80 : double timestampMultiplier; 81 : }; 82 : 83 : /** 84 : * It's a responsibility of a device owner to make sure (via WaitUntilIdle) 85 : * that the device is available to be destroyed. 86 : */ 87 12 : virtual ~IDevice() {} 88 : 89 : virtual Backend GetBackend() const = 0; 90 : 91 : virtual const std::string& GetName() const = 0; 92 : virtual const std::string& GetVersion() const = 0; 93 : virtual const std::string& GetDriverInformation() const = 0; 94 : virtual const std::vector<std::string>& GetExtensions() const = 0; 95 : 96 : virtual void Report(const Script::Request& rq, JS::HandleValue settings) = 0; 97 : 98 : virtual std::unique_ptr<IDeviceCommandContext> CreateCommandContext() = 0; 99 : 100 : /** 101 : * To be able to present something on a window it needs to create swapchain 102 : * which provides framebuffer to output rendering result. Generally it's 103 : * not allowed to have multiple swapchains for the same window. 104 : * 105 : * We use the provided surface size when the window is nullptr. It's used 106 : * in Atlas when we don't have an SDL_Window. 107 : * 108 : * @return A valid swapchain if it was created successfully else nullptr. 109 : */ 110 : virtual std::unique_ptr<ISwapChain> CreateSwapChain( 111 : const char* name, SDL_Window* window, 112 : int surfaceDrawableWidth, int surfaceDrawableHeight, const bool vsync, 113 : std::unique_ptr<ISwapChain> oldSwapChain) = 0; 114 : 115 : /** 116 : * Waits until all submitted work (via DeviceCommandContext::Flush) to 117 : * backend is completed and it's safe to release a resource like SwapChain. 118 : */ 119 : virtual void WaitUntilIdle() = 0; 120 : 121 : /** 122 : * Creates a graphics pipeline state. It's a caller responsibility to 123 : * guarantee a lifespan of IShaderProgram stored in the description. 124 : */ 125 : virtual std::unique_ptr<IGraphicsPipelineState> CreateGraphicsPipelineState( 126 : const SGraphicsPipelineStateDesc& pipelineStateDesc) = 0; 127 : 128 : /** 129 : * Creates a compute pipeline state. It's a caller responsibility to 130 : * guarantee a lifespan of IShaderProgram stored in the description. 131 : */ 132 : virtual std::unique_ptr<IComputePipelineState> CreateComputePipelineState( 133 : const SComputePipelineStateDesc& pipelineStateDesc) = 0; 134 : 135 : /** 136 : * Creates a vertex input layout. It's recommended to use as few different 137 : * layouts as posible. 138 : */ 139 : virtual std::unique_ptr<IVertexInputLayout> CreateVertexInputLayout( 140 : const std::span<const SVertexAttributeFormat> attributes) = 0; 141 : 142 : virtual std::unique_ptr<ITexture> CreateTexture( 143 : const char* name, const ITexture::Type type, const uint32_t usage, 144 : const Format format, const uint32_t width, const uint32_t height, 145 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount) = 0; 146 : 147 : virtual std::unique_ptr<ITexture> CreateTexture2D( 148 : const char* name, const uint32_t usage, 149 : const Format format, const uint32_t width, const uint32_t height, 150 : const Sampler::Desc& defaultSamplerDesc, const uint32_t MIPLevelCount = 1, const uint32_t sampleCount = 1) = 0; 151 : 152 : /** 153 : * @see IFramebuffer 154 : * 155 : * The color attachment and the depth-stencil attachment should not be 156 : * nullptr at the same time. There should not be many different clear 157 : * colors along all color attachments for all framebuffers created for 158 : * the device. 159 : * 160 : * @return A valid framebuffer if it was created successfully else nullptr. 161 : */ 162 : virtual std::unique_ptr<IFramebuffer> CreateFramebuffer( 163 : const char* name, SColorAttachment* colorAttachment, 164 : SDepthStencilAttachment* depthStencilAttachment) = 0; 165 : 166 : virtual std::unique_ptr<IBuffer> CreateBuffer( 167 : const char* name, const IBuffer::Type type, const uint32_t size, const uint32_t usage) = 0; 168 : 169 : virtual std::unique_ptr<IShaderProgram> CreateShaderProgram( 170 : const CStr& name, const CShaderDefines& defines) = 0; 171 : 172 : virtual bool IsTextureFormatSupported(const Format format) const = 0; 173 : 174 : virtual bool IsFramebufferFormatSupported(const Format format) const = 0; 175 : 176 : /** 177 : * Returns the most suitable format for the usage. Returns 178 : * Format::UNDEFINED if there is no such format. 179 : */ 180 : virtual Format GetPreferredDepthStencilFormat( 181 : const uint32_t usage, const bool depth, const bool stencil) const = 0; 182 : 183 : virtual uint32_t AllocateQuery() = 0; 184 : 185 : virtual void FreeQuery(const uint32_t handle) = 0; 186 : 187 : /** 188 : * @see GetQueryResult 189 : * 190 : * It must be called only if the query was submitted via 191 : * IDeviceCommandContext::Flush. 192 : * 193 : * @param handle Must be a valid handle of a query. 194 : * 195 : * @return True if a result for the query is available. 196 : */ 197 : virtual bool IsQueryResultAvailable(const uint32_t handle) const = 0; 198 : 199 : /** 200 : * After a call of the function the query result becomes invalid. 201 : * 202 : * @param handle Must be a valid handle of a query. 203 : * 204 : * @return A result for the query. The result is undefined if the query isn't 205 : * ready. 206 : */ 207 : virtual uint64_t GetQueryResult(const uint32_t handle) = 0; 208 : 209 : virtual const Capabilities& GetCapabilities() const = 0; 210 : 211 : /** 212 : * Collects backend-specific statistics. 213 : */ 214 : struct StatisticsItem 215 : { 216 : std::string_view name; 217 : std::string_view unit; 218 : std::variant<float, uint32_t> value; 219 : 220 : // clang can't do emplace_back yet because of the aggregate type. 221 0 : StatisticsItem( 222 : std::string_view name, std::string_view unit, 223 : std::variant<float, uint32_t> value) 224 0 : : name(name), unit(unit), value(value) 225 : { 226 : } 227 : }; 228 : using StatisticsVector = std::vector<StatisticsItem>; 229 : virtual void CollectStatistics(StatisticsVector& statistics) const = 0; 230 : }; 231 : 232 : } // namespace Backend 233 : 234 : } // namespace Renderer 235 : 236 : #endif // INCLUDED_RENDERER_BACKEND_IDEVICE