Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
IDevice.h
Go to the documentation of this file.
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"
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
34class CShaderDefines;
35class CStr;
36namespace Renderer::Backend { class IComputePipelineState; }
37namespace Renderer::Backend { class IDeviceCommandContext; }
38namespace Renderer::Backend { class IFramebuffer; }
39namespace Renderer::Backend { class IGraphicsPipelineState; }
40namespace Renderer::Backend { class IShaderProgram; }
41namespace Renderer::Backend { class ISwapChain; }
42namespace Renderer::Backend { class IVertexInputLayout; }
43namespace Renderer::Backend { enum class AttachmentLoadOp; }
44namespace Renderer::Backend { enum class AttachmentStoreOp; }
45namespace Renderer::Backend { enum class Backend; }
46namespace Renderer::Backend { enum class Format; }
47namespace Renderer::Backend { struct SColorAttachment; }
48namespace Renderer::Backend { struct SComputePipelineStateDesc; }
49namespace Renderer::Backend { struct SDepthStencilAttachment; }
50namespace Renderer::Backend { struct SGraphicsPipelineStateDesc; }
51namespace Renderer::Backend { struct SVertexAttributeFormat; }
52namespace Renderer::Backend::Sampler { struct Desc; }
53namespace Script { class Request; }
54
55typedef struct SDL_Window SDL_Window;
56
57namespace Renderer
58{
59
60namespace Backend
61{
62
64{
65public:
67 {
68 bool S3TC;
78 bool storage;
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 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 */
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 */
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.
222 std::string_view name, std::string_view unit,
223 std::variant<float, uint32_t> 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
struct SDL_Window SDL_Window
Definition: VideoMode.h:31
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:153
Type
Definition: IBuffer.h:35
Definition: IDevice.h:64
virtual std::unique_ptr< ISwapChain > CreateSwapChain(const char *name, SDL_Window *window, int surfaceDrawableWidth, int surfaceDrawableHeight, const bool vsync, std::unique_ptr< ISwapChain > oldSwapChain)=0
To be able to present something on a window it needs to create swapchain which provides framebuffer t...
virtual Backend GetBackend() const =0
virtual const std::string & GetName() const =0
virtual const std::vector< std::string > & GetExtensions() const =0
virtual std::unique_ptr< IComputePipelineState > CreateComputePipelineState(const SComputePipelineStateDesc &pipelineStateDesc)=0
Creates a compute pipeline state.
virtual Format GetPreferredDepthStencilFormat(const uint32_t usage, const bool depth, const bool stencil) const =0
Returns the most suitable format for the usage.
virtual void CollectStatistics(StatisticsVector &statistics) const =0
virtual std::unique_ptr< IDeviceCommandContext > CreateCommandContext()=0
virtual uint32_t AllocateQuery()=0
virtual const Capabilities & GetCapabilities() const =0
virtual uint64_t GetQueryResult(const uint32_t handle)=0
After a call of the function the query result becomes invalid.
virtual ~IDevice()
It's a responsibility of a device owner to make sure (via WaitUntilIdle) that the device is available...
Definition: IDevice.h:87
virtual bool IsFramebufferFormatSupported(const Format format) const =0
virtual const std::string & GetVersion() const =0
virtual std::unique_ptr< IFramebuffer > CreateFramebuffer(const char *name, SColorAttachment *colorAttachment, SDepthStencilAttachment *depthStencilAttachment)=0
virtual const std::string & GetDriverInformation() const =0
virtual bool IsTextureFormatSupported(const Format format) const =0
virtual void WaitUntilIdle()=0
Waits until all submitted work (via DeviceCommandContext::Flush) to backend is completed and it's saf...
virtual std::unique_ptr< IVertexInputLayout > CreateVertexInputLayout(const std::span< const SVertexAttributeFormat > attributes)=0
Creates a vertex input layout.
virtual std::unique_ptr< ITexture > CreateTexture2D(const char *name, const uint32_t usage, const Format format, const uint32_t width, const uint32_t height, const Sampler::Desc &defaultSamplerDesc, const uint32_t MIPLevelCount=1, const uint32_t sampleCount=1)=0
virtual std::unique_ptr< ITexture > CreateTexture(const char *name, const ITexture::Type type, const uint32_t usage, const Format format, const uint32_t width, const uint32_t height, const Sampler::Desc &defaultSamplerDesc, const uint32_t MIPLevelCount, const uint32_t sampleCount)=0
std::vector< StatisticsItem > StatisticsVector
Definition: IDevice.h:228
virtual std::unique_ptr< IGraphicsPipelineState > CreateGraphicsPipelineState(const SGraphicsPipelineStateDesc &pipelineStateDesc)=0
Creates a graphics pipeline state.
virtual bool IsQueryResultAvailable(const uint32_t handle) const =0
virtual void FreeQuery(const uint32_t handle)=0
virtual void Report(const Script::Request &rq, JS::HandleValue settings)=0
virtual std::unique_ptr< IShaderProgram > CreateShaderProgram(const CStr &name, const CShaderDefines &defines)=0
virtual std::unique_ptr< IBuffer > CreateBuffer(const char *name, const IBuffer::Type type, const uint32_t size, const uint32_t usage)=0
Type
Definition: ITexture.h:36
Definition: Request.h:51
Definition: Font.h:44
Definition: Canvas2D.h:30
Format
Definition: Format.h:28
AttachmentStoreOp
Store operation is set for each attachment, what should be done with its content on EndFramebufferPas...
Definition: IFramebuffer.h:54
Backend
Definition: Backend.h:28
AttachmentLoadOp
Load operation is set for each attachment, what should be done with its content on BeginFramebufferPa...
Definition: IFramebuffer.h:39
Definition: Canvas2D.h:30
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32
bool anisotropicFiltering
Definition: IDevice.h:73
double timestampMultiplier
Definition: IDevice.h:80
float maxAnisotropy
Definition: IDevice.h:75
bool timestamps
Definition: IDevice.h:79
bool multisampling
Definition: IDevice.h:72
bool storage
Definition: IDevice.h:78
uint32_t maxSampleCount
Definition: IDevice.h:74
bool instancing
Definition: IDevice.h:77
bool S3TC
Definition: IDevice.h:68
bool computeShaders
Definition: IDevice.h:69
bool debugLabels
Definition: IDevice.h:70
bool debugScopedLabels
Definition: IDevice.h:71
uint32_t maxTextureSize
Definition: IDevice.h:76
Collects backend-specific statistics.
Definition: IDevice.h:215
std::string_view unit
Definition: IDevice.h:217
std::string_view name
Definition: IDevice.h:216
std::variant< float, uint32_t > value
Definition: IDevice.h:218
StatisticsItem(std::string_view name, std::string_view unit, std::variant< float, uint32_t > value)
Definition: IDevice.h:221
Definition: IFramebuffer.h:62
Definition: PipelineState.h:177
Definition: IFramebuffer.h:70
Definition: PipelineState.h:167
Definition: Sampler.h:54