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_IDEVICECOMMANDCONTEXT
19 : #define INCLUDED_RENDERER_BACKEND_IDEVICECOMMANDCONTEXT
20 :
21 : #include "renderer/backend/IDeviceObject.h"
22 :
23 : #include <cstddef>
24 : #include <cstdint>
25 : #include <functional>
26 : #include <span>
27 :
28 : namespace Renderer::Backend { class IComputePipelineState; }
29 : namespace Renderer::Backend { class IGraphicsPipelineState; }
30 : namespace Renderer::Backend { class ISwapChain; }
31 : namespace Renderer::Backend { class IVertexInputLayout; }
32 : namespace Renderer::Backend { enum class Format; }
33 : namespace Renderer::Backend::Sampler { enum class Filter; }
34 :
35 : namespace Renderer
36 : {
37 :
38 : namespace Backend
39 : {
40 :
41 : class IBuffer;
42 : class IFramebuffer;
43 : class ITexture;
44 :
45 10 : class IDeviceCommandContext : public IDeviceObject<IDeviceCommandContext>
46 : {
47 : public:
48 : /**
49 : * Binds the graphics pipeline state. It should be called only inside a
50 : * framebuffer pass and as rarely as possible.
51 : */
52 : virtual void SetGraphicsPipelineState(IGraphicsPipelineState* pipelineState) = 0;
53 :
54 : /**
55 : * Binds the graphics pipeline state. It should be called only inside a
56 : * framebuffer pass and as rarely as possible.
57 : */
58 : virtual void SetComputePipelineState(IComputePipelineState* pipelineState) = 0;
59 :
60 : // TODO: maybe we should add a more common type, like CRectI.
61 : struct Rect
62 : {
63 : int32_t x, y;
64 : int32_t width, height;
65 : };
66 : /**
67 : * Copies source region into destination region automatically applying
68 : * compatible format conversion and scaling using a provided filter.
69 : * A backbuffer can't be a source.
70 : */
71 : virtual void BlitFramebuffer(
72 : IFramebuffer* sourceFramebuffer, IFramebuffer* destinationFramebuffer,
73 : const Rect& sourceRegion, const Rect& destinationRegion,
74 : const Sampler::Filter filter) = 0;
75 :
76 : /**
77 : * Resolves multisample source framebuffer attachments to destination
78 : * attachments. Source attachments should have a sample count > 1 and
79 : * destination attachments should have a sample count = 1.
80 : * A backbuffer can't be a source.
81 : */
82 : virtual void ResolveFramebuffer(
83 : IFramebuffer* sourceFramebuffer, IFramebuffer* destinationFramebuffer) = 0;
84 :
85 : /**
86 : * Starts a framebuffer pass, performs attachment load operations.
87 : * It should be called as rarely as possible.
88 : *
89 : * @see IFramebuffer
90 : */
91 : virtual void BeginFramebufferPass(IFramebuffer* framebuffer) = 0;
92 :
93 : /**
94 : * Finishes a framebuffer pass, performs attachment store operations.
95 : */
96 : virtual void EndFramebufferPass() = 0;
97 :
98 : /**
99 : * Clears all mentioned attachments. Prefer to use attachment load operations over
100 : * this function. It should be called only inside a framebuffer pass.
101 : */
102 : virtual void ClearFramebuffer(const bool color, const bool depth, const bool stencil) = 0;
103 :
104 : /**
105 : * Readbacks the current backbuffer to data in R8G8B8_UNORM format somewhen
106 : * between the function call and Flush (inclusively). Because of that the
107 : * data pointer should be valid in that time period and have enough space
108 : * to fit the readback result.
109 : * @note this operation is very slow and should not be used regularly.
110 : * TODO: ideally we should do readback on Present or even asynchronously
111 : * but a client doesn't support that yet.
112 : */
113 : virtual void ReadbackFramebufferSync(
114 : ISwapChain& swapChain, const uint32_t x, const uint32_t y,
115 : const uint32_t width, const uint32_t height,
116 : void* data) = 0;
117 :
118 : virtual void UploadTexture(ITexture* texture, const Format dataFormat,
119 : const void* data, const size_t dataSize,
120 : const uint32_t level = 0, const uint32_t layer = 0) = 0;
121 : virtual void UploadTextureRegion(ITexture* texture, const Format dataFormat,
122 : const void* data, const size_t dataSize,
123 : const uint32_t xOffset, const uint32_t yOffset,
124 : const uint32_t width, const uint32_t height,
125 : const uint32_t level = 0, const uint32_t layer = 0) = 0;
126 :
127 : using UploadBufferFunction = std::function<void(std::uint8_t*)>;
128 : virtual void UploadBuffer(IBuffer* buffer, const void* data, const uint32_t dataSize) = 0;
129 : virtual void UploadBuffer(IBuffer* buffer, const UploadBufferFunction& uploadFunction) = 0;
130 : virtual void UploadBufferRegion(
131 : IBuffer* buffer, const void* data, const uint32_t dataOffset, const uint32_t dataSize) = 0;
132 : virtual void UploadBufferRegion(
133 : IBuffer* buffer, const uint32_t dataOffset, const uint32_t dataSize,
134 : const UploadBufferFunction& uploadFunction) = 0;
135 :
136 : virtual void SetScissors(const uint32_t scissorCount, const Rect* scissors) = 0;
137 : virtual void SetViewports(const uint32_t viewportCount, const Rect* viewports) = 0;
138 :
139 : /**
140 : * Binds the vertex input layout. It should be compatible with the shader
141 : * program's one. It should be called only inside a framebuffer pass and as
142 : * rarely as possible.
143 : */
144 : virtual void SetVertexInputLayout(
145 : IVertexInputLayout* vertexInputLayout) = 0;
146 :
147 : virtual void SetVertexBuffer(
148 : const uint32_t bindingSlot, IBuffer* buffer, const uint32_t offset) = 0;
149 : virtual void SetVertexBufferData(
150 : const uint32_t bindingSlot, const void* data, const uint32_t dataSize) = 0;
151 :
152 : virtual void SetIndexBuffer(IBuffer* buffer) = 0;
153 : virtual void SetIndexBufferData(const void* data, const uint32_t dataSize) = 0;
154 :
155 : virtual void BeginPass() = 0;
156 : virtual void EndPass() = 0;
157 :
158 : virtual void Draw(const uint32_t firstVertex, const uint32_t vertexCount) = 0;
159 : virtual void DrawIndexed(
160 : const uint32_t firstIndex, const uint32_t indexCount, const int32_t vertexOffset) = 0;
161 : virtual void DrawInstanced(
162 : const uint32_t firstVertex, const uint32_t vertexCount,
163 : const uint32_t firstInstance, const uint32_t instanceCount) = 0;
164 : virtual void DrawIndexedInstanced(
165 : const uint32_t firstIndex, const uint32_t indexCount,
166 : const uint32_t firstInstance, const uint32_t instanceCount,
167 : const int32_t vertexOffset) = 0;
168 : // TODO: should be removed when performance impact is minimal on slow hardware.
169 : virtual void DrawIndexedInRange(
170 : const uint32_t firstIndex, const uint32_t indexCount,
171 : const uint32_t start, const uint32_t end) = 0;
172 :
173 : /**
174 : * Starts a compute pass, can't be called inside a framebuffer pass.
175 : * It should be called as rarely as possible.
176 : */
177 : virtual void BeginComputePass() = 0;
178 :
179 : /**
180 : * Finishes a compute pass.
181 : */
182 : virtual void EndComputePass() = 0;
183 :
184 : /**
185 : * Dispatches groupCountX * groupCountY * groupCountZ compute groups.
186 : */
187 : virtual void Dispatch(
188 : const uint32_t groupCountX,
189 : const uint32_t groupCountY,
190 : const uint32_t groupCountZ) = 0;
191 :
192 : /**
193 : * Inserts a memory barrier which guarantees that all memory accesses
194 : * matched by `srcAccessMask` in src are completed before all memory accesses
195 : * described by `dstAccessMask` in dst.
196 : */
197 : virtual void InsertMemoryBarrier(
198 : const uint32_t srcStageMask, const uint32_t dstStageMask,
199 : const uint32_t srcAccessMask, const uint32_t dstAccessMask) = 0;
200 :
201 : /**
202 : * Sets a read-only texture to the binding slot.
203 : */
204 : virtual void SetTexture(const int32_t bindingSlot, ITexture* texture) = 0;
205 :
206 : /**
207 : * Sets a read & write resource to the binding slot.
208 : */
209 : virtual void SetStorageTexture(const int32_t bindingSlot, ITexture* texture) = 0;
210 : virtual void SetStorageBuffer(const int32_t bindingSlot, IBuffer* buffer) = 0;
211 :
212 : virtual void SetUniform(
213 : const int32_t bindingSlot,
214 : const float value) = 0;
215 : virtual void SetUniform(
216 : const int32_t bindingSlot,
217 : const float valueX, const float valueY) = 0;
218 : virtual void SetUniform(
219 : const int32_t bindingSlot,
220 : const float valueX, const float valueY,
221 : const float valueZ) = 0;
222 : virtual void SetUniform(
223 : const int32_t bindingSlot,
224 : const float valueX, const float valueY,
225 : const float valueZ, const float valueW) = 0;
226 : virtual void SetUniform(
227 : const int32_t bindingSlot, std::span<const float> values) = 0;
228 :
229 : /**
230 : * Insert a timestamp query which can be later requested via IDevice.
231 : * @see IDevice::IsQueryResultAvailable
232 : * It can be used only outside of a framebuffer pass. The query must
233 : * not be used till Flush.
234 : *
235 : * @param handle Must be a valid handle to a query.
236 : * @param isScopeBegin True if it's a scope start.
237 : */
238 : virtual void InsertTimestampQuery(const uint32_t handle, const bool isScopeBegin) = 0;
239 :
240 : virtual void BeginScopedLabel(const char* name) = 0;
241 : virtual void EndScopedLabel() = 0;
242 :
243 : virtual void Flush() = 0;
244 : };
245 :
246 : } // namespace Backend
247 :
248 : } // namespace Renderer
249 :
250 : #define GPU_SCOPED_LABEL(deviceCommandContext, name) \
251 : GPUScopedLabel scopedLabel((deviceCommandContext), (name));
252 :
253 : class GPUScopedLabel
254 : {
255 : public:
256 0 : GPUScopedLabel(
257 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
258 : const char* name)
259 0 : : m_DeviceCommandContext(deviceCommandContext)
260 : {
261 0 : m_DeviceCommandContext->BeginScopedLabel(name);
262 0 : }
263 :
264 0 : ~GPUScopedLabel()
265 : {
266 0 : m_DeviceCommandContext->EndScopedLabel();
267 0 : }
268 :
269 : private:
270 : Renderer::Backend::IDeviceCommandContext* m_DeviceCommandContext = nullptr;
271 : };
272 :
273 : #endif // INCLUDED_RENDERER_BACKEND_IDEVICECOMMANDCONTEXT
|