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