Skip to content

Commit 121fdbe

Browse files
thatcosmonautTheSpydog
authored andcommitted
Move uniforms to command buffer scope (#55)
Co-authored-by: Caleb Cornett <caleb.cornett@outlook.com>
1 parent 5953516 commit 121fdbe

File tree

8 files changed

+1046
-753
lines changed

8 files changed

+1046
-753
lines changed

include/SDL3/SDL_gpu.h

Lines changed: 101 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,107 @@ extern SDL_DECLSPEC void SDLCALL SDL_GpuReleaseGraphicsPipeline(
11411141
SDL_GpuDevice *device,
11421142
SDL_GpuGraphicsPipeline *graphicsPipeline);
11431143

1144+
/*
1145+
* COMMAND BUFFERS
1146+
*
1147+
* Render state is managed via command buffers.
1148+
* When setting render state, that state is always local to the command buffer.
1149+
*
1150+
* Commands only begin execution on the GPU once Submit is called.
1151+
* Once the command buffer is submitted, it is no longer valid to use it.
1152+
*
1153+
* In multi-threading scenarios, you should acquire and submit a command buffer on the same thread.
1154+
* As long as you satisfy this requirement, all functionality related to command buffers is thread-safe.
1155+
*/
1156+
1157+
/**
1158+
* Acquire a command buffer.
1159+
* This command buffer is managed by the implementation and should not be freed by the user.
1160+
* The command buffer may only be used on the thread it was acquired on.
1161+
* The command buffer should be submitted on the thread it was acquired on.
1162+
*
1163+
* \param device a GPU context
1164+
* \returns a command buffer
1165+
*
1166+
* \since This function is available since SDL 3.x.x
1167+
*
1168+
* \sa SDL_GpuSubmit
1169+
* \sa SDL_GpuSubmitAndAcquireFence
1170+
*/
1171+
extern SDL_DECLSPEC SDL_GpuCommandBuffer *SDLCALL SDL_GpuAcquireCommandBuffer(
1172+
SDL_GpuDevice *device);
1173+
1174+
/*
1175+
* UNIFORM DATA
1176+
*
1177+
* Uniforms are for passing data to shaders.
1178+
* The uniform data will be constant across all executions of the shader.
1179+
*
1180+
* There are 4 available uniform slots per shader stage (vertex, fragment, compute).
1181+
* Uniform data pushed to a slot on a stage keeps its value throughout the command buffer
1182+
* until you call the relevant Push function on that slot again.
1183+
*
1184+
* For example, you could write your vertex shaders to read a camera matrix from uniform binding slot 0,
1185+
* push the camera matrix at the start of the command buffer, and that data will be used for every
1186+
* subsequent draw call.
1187+
*
1188+
* It is valid to push uniform data during a render or compute pass.
1189+
*
1190+
* Uniforms are best for pushing small amounts of data.
1191+
* If you are pushing more than a matrix or two per call you should consider using a storage buffer instead.
1192+
*/
1193+
1194+
/**
1195+
* Pushes data to a vertex uniform slot on the command buffer.
1196+
* Subsequent draw calls will use this uniform data.
1197+
*
1198+
* \param commandBuffer a command buffer
1199+
* \param slotIndex the vertex uniform slot to push data to
1200+
* \param data client data to write
1201+
* \param dataLengthInBytes the length of the data to write
1202+
*
1203+
* \since This function is available since SDL 3.x.x
1204+
*/
1205+
extern SDL_DECLSPEC void SDLCALL SDL_GpuPushVertexUniformData(
1206+
SDL_GpuCommandBuffer *commandBuffer,
1207+
Uint32 slotIndex,
1208+
const void *data,
1209+
Uint32 dataLengthInBytes);
1210+
1211+
/**
1212+
* Pushes data to a fragment uniform slot on the command buffer.
1213+
* Subsequent draw calls will use this uniform data.
1214+
*
1215+
* \param commandBuffer a command buffer
1216+
* \param slotIndex the fragment uniform slot to push data to
1217+
* \param data client data to write
1218+
* \param dataLengthInBytes the length of the data to write
1219+
*
1220+
* \since This function is available since SDL 3.x.x
1221+
*/
1222+
extern SDL_DECLSPEC void SDLCALL SDL_GpuPushFragmentUniformData(
1223+
SDL_GpuCommandBuffer *commandBuffer,
1224+
Uint32 slotIndex,
1225+
const void *data,
1226+
Uint32 dataLengthInBytes);
1227+
1228+
/**
1229+
* Pushes data to a uniform slot on the command buffer.
1230+
* Subsequent draw calls will use this uniform data.
1231+
*
1232+
* \param commandBuffer a command buffer
1233+
* \param slotIndex the uniform slot to push data to
1234+
* \param data client data to write
1235+
* \param dataLengthInBytes the length of the data to write
1236+
*
1237+
* \since This function is available since SDL 3.x.x
1238+
*/
1239+
extern SDL_DECLSPEC void SDLCALL SDL_GpuPushComputeUniformData(
1240+
SDL_GpuCommandBuffer *commandBuffer,
1241+
Uint32 slotIndex,
1242+
const void *data,
1243+
Uint32 dataLengthInBytes);
1244+
11441245
/*
11451246
* A NOTE ON CYCLING
11461247
*
@@ -1375,40 +1476,6 @@ extern SDL_DECLSPEC void SDLCALL SDL_GpuBindFragmentStorageBuffers(
13751476
SDL_GpuBuffer **storageBuffers,
13761477
Uint32 bindingCount);
13771478

1378-
/**
1379-
* Pushes data to a vertex uniform slot on the bound graphics pipeline.
1380-
* Subsequent draw calls will use this uniform data.
1381-
*
1382-
* \param renderPass a render pass handle
1383-
* \param slotIndex the vertex uniform slot to push data to
1384-
* \param data client data to write
1385-
* \param dataLengthInBytes the length of the data to write
1386-
*
1387-
* \since This function is available since SDL 3.x.x
1388-
*/
1389-
extern SDL_DECLSPEC void SDLCALL SDL_GpuPushVertexUniformData(
1390-
SDL_GpuRenderPass *renderPass,
1391-
Uint32 slotIndex,
1392-
const void *data,
1393-
Uint32 dataLengthInBytes);
1394-
1395-
/**
1396-
* Pushes data to a fragment uniform slot on the bound graphics pipeline.
1397-
* Subsequent draw calls will use this uniform data.
1398-
*
1399-
* \param renderPass a render pass handle
1400-
* \param slotIndex the fragment uniform slot to push data to
1401-
* \param data client data to write
1402-
* \param dataLengthInBytes the length of the data to write
1403-
*
1404-
* \since This function is available since SDL 3.x.x
1405-
*/
1406-
extern SDL_DECLSPEC void SDLCALL SDL_GpuPushFragmentUniformData(
1407-
SDL_GpuRenderPass *renderPass,
1408-
Uint32 slotIndex,
1409-
const void *data,
1410-
Uint32 dataLengthInBytes);
1411-
14121479
/* Drawing */
14131480

14141481
/**
@@ -1575,23 +1642,6 @@ extern SDL_DECLSPEC void SDLCALL SDL_GpuBindComputeStorageBuffers(
15751642
SDL_GpuBuffer **storageBuffers,
15761643
Uint32 bindingCount);
15771644

1578-
/**
1579-
* Pushes data to a uniform slot on the bound compute pipeline.
1580-
* Subsequent draw calls will use this uniform data.
1581-
*
1582-
* \param computePass a compute pass handle
1583-
* \param slotIndex the uniform slot to push data to
1584-
* \param data client data to write
1585-
* \param dataLengthInBytes the length of the data to write
1586-
*
1587-
* \since This function is available since SDL 3.x.x
1588-
*/
1589-
extern SDL_DECLSPEC void SDLCALL SDL_GpuPushComputeUniformData(
1590-
SDL_GpuComputePass *computePass,
1591-
Uint32 slotIndex,
1592-
const void *data,
1593-
Uint32 dataLengthInBytes);
1594-
15951645
/**
15961646
* Dispatches compute work.
15971647
* You must not call this function before binding a compute pipeline.
@@ -1963,23 +2013,6 @@ extern SDL_DECLSPEC SDL_GpuTextureFormat SDLCALL SDL_GpuGetSwapchainTextureForma
19632013
SDL_GpuDevice *device,
19642014
SDL_Window *window);
19652015

1966-
/**
1967-
* Acquire a command buffer.
1968-
* This command buffer is managed by the implementation and should not be freed by the user.
1969-
* The command buffer may only be used on the thread it was acquired on.
1970-
* The command buffer should be submitted on the thread it was acquired on.
1971-
*
1972-
* \param device a GPU context
1973-
* \returns a command buffer
1974-
*
1975-
* \since This function is available since SDL 3.x.x
1976-
*
1977-
* \sa SDL_GpuSubmit
1978-
* \sa SDL_GpuSubmitAndAcquireFence
1979-
*/
1980-
extern SDL_DECLSPEC SDL_GpuCommandBuffer *SDLCALL SDL_GpuAcquireCommandBuffer(
1981-
SDL_GpuDevice *device);
1982-
19832016
/**
19842017
* Acquire a texture to use in presentation.
19852018
* When a swapchain texture is acquired on a command buffer,

src/dynapi/SDL_dynapi_procs.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,10 @@ SDL_DYNAPI_PROC(void,SDL_GpuReleaseTransferBuffer,(SDL_GpuDevice *a, SDL_GpuTran
11011101
SDL_DYNAPI_PROC(void,SDL_GpuReleaseComputePipeline,(SDL_GpuDevice *a, SDL_GpuComputePipeline *b),(a,b),)
11021102
SDL_DYNAPI_PROC(void,SDL_GpuReleaseShader,(SDL_GpuDevice *a, SDL_GpuShader *b),(a,b),)
11031103
SDL_DYNAPI_PROC(void,SDL_GpuReleaseGraphicsPipeline,(SDL_GpuDevice *a, SDL_GpuGraphicsPipeline *b),(a,b),)
1104+
SDL_DYNAPI_PROC(SDL_GpuCommandBuffer*,SDL_GpuAcquireCommandBuffer,(SDL_GpuDevice *a),(a),return)
1105+
SDL_DYNAPI_PROC(void,SDL_GpuPushVertexUniformData,(SDL_GpuCommandBuffer *a, Uint32 b, const void *c, Uint32 d),(a,b,c,d),)
1106+
SDL_DYNAPI_PROC(void,SDL_GpuPushFragmentUniformData,(SDL_GpuCommandBuffer *a, Uint32 b, const void *c, Uint32 d),(a,b,c,d),)
1107+
SDL_DYNAPI_PROC(void,SDL_GpuPushComputeUniformData,(SDL_GpuCommandBuffer *a, Uint32 b, const void *c, Uint32 d),(a,b,c,d),)
11041108
SDL_DYNAPI_PROC(SDL_GpuRenderPass*,SDL_GpuBeginRenderPass,(SDL_GpuCommandBuffer *a, SDL_GpuColorAttachmentInfo *b, Uint32 c, SDL_GpuDepthStencilAttachmentInfo *d),(a,b,c,d),return)
11051109
SDL_DYNAPI_PROC(void,SDL_GpuBindGraphicsPipeline,(SDL_GpuRenderPass *a, SDL_GpuGraphicsPipeline *b),(a,b),)
11061110
SDL_DYNAPI_PROC(void,SDL_GpuSetViewport,(SDL_GpuRenderPass *a, SDL_GpuViewport *b),(a,b),)
@@ -1113,8 +1117,6 @@ SDL_DYNAPI_PROC(void,SDL_GpuBindVertexStorageBuffers,(SDL_GpuRenderPass *a, Uint
11131117
SDL_DYNAPI_PROC(void,SDL_GpuBindFragmentSamplers,(SDL_GpuRenderPass *a, Uint32 b, SDL_GpuTextureSamplerBinding *c, Uint32 d),(a,b,c,d),)
11141118
SDL_DYNAPI_PROC(void,SDL_GpuBindFragmentStorageTextures,(SDL_GpuRenderPass *a, Uint32 b, SDL_GpuTextureSlice *c, Uint32 d),(a,b,c,d),)
11151119
SDL_DYNAPI_PROC(void,SDL_GpuBindFragmentStorageBuffers,(SDL_GpuRenderPass *a, Uint32 b, SDL_GpuBuffer **c, Uint32 d),(a,b,c,d),)
1116-
SDL_DYNAPI_PROC(void,SDL_GpuPushVertexUniformData,(SDL_GpuRenderPass *a, Uint32 b, const void *c, Uint32 d),(a,b,c,d),)
1117-
SDL_DYNAPI_PROC(void,SDL_GpuPushFragmentUniformData,(SDL_GpuRenderPass *a, Uint32 b, const void *c, Uint32 d),(a,b,c,d),)
11181120
SDL_DYNAPI_PROC(void,SDL_GpuDrawIndexedPrimitives,(SDL_GpuRenderPass *a, Uint32 b, Uint32 c, Uint32 d, Uint32 e),(a,b,c,d,e),)
11191121
SDL_DYNAPI_PROC(void,SDL_GpuDrawPrimitives,(SDL_GpuRenderPass *a, Uint32 b, Uint32 c),(a,b,c),)
11201122
SDL_DYNAPI_PROC(void,SDL_GpuDrawPrimitivesIndirect,(SDL_GpuRenderPass *a, SDL_GpuBuffer *b, Uint32 c, Uint32 d, Uint32 e),(a,b,c,d,e),)
@@ -1124,7 +1126,6 @@ SDL_DYNAPI_PROC(SDL_GpuComputePass*,SDL_GpuBeginComputePass,(SDL_GpuCommandBuffe
11241126
SDL_DYNAPI_PROC(void,SDL_GpuBindComputePipeline,(SDL_GpuComputePass *a, SDL_GpuComputePipeline *b),(a,b),)
11251127
SDL_DYNAPI_PROC(void,SDL_GpuBindComputeStorageTextures,(SDL_GpuComputePass *a, Uint32 b, SDL_GpuTextureSlice *c, Uint32 d),(a,b,c,d),)
11261128
SDL_DYNAPI_PROC(void,SDL_GpuBindComputeStorageBuffers,(SDL_GpuComputePass *a, Uint32 b, SDL_GpuBuffer **c, Uint32 d),(a,b,c,d),)
1127-
SDL_DYNAPI_PROC(void,SDL_GpuPushComputeUniformData,(SDL_GpuComputePass *a, Uint32 b, const void *c, Uint32 d),(a,b,c,d),)
11281129
SDL_DYNAPI_PROC(void,SDL_GpuDispatchCompute,(SDL_GpuComputePass *a, Uint32 b, Uint32 c, Uint32 d),(a,b,c,d),)
11291130
SDL_DYNAPI_PROC(void,SDL_GpuEndComputePass,(SDL_GpuComputePass *a),(a),)
11301131
SDL_DYNAPI_PROC(void,SDL_GpuMapTransferBuffer,(SDL_GpuDevice *a, SDL_GpuTransferBuffer *b, SDL_bool c, void **d),(a,b,c,d),)
@@ -1147,7 +1148,6 @@ SDL_DYNAPI_PROC(SDL_bool,SDL_GpuClaimWindow,(SDL_GpuDevice *a, SDL_Window *b, SD
11471148
SDL_DYNAPI_PROC(void,SDL_GpuUnclaimWindow,(SDL_GpuDevice *a, SDL_Window *b),(a,b),)
11481149
SDL_DYNAPI_PROC(void,SDL_GpuSetSwapchainParameters,(SDL_GpuDevice *a, SDL_Window *b, SDL_GpuSwapchainComposition c, SDL_GpuPresentMode d),(a,b,c,d),)
11491150
SDL_DYNAPI_PROC(SDL_GpuTextureFormat,SDL_GpuGetSwapchainTextureFormat,(SDL_GpuDevice *a, SDL_Window *b),(a,b),return)
1150-
SDL_DYNAPI_PROC(SDL_GpuCommandBuffer*,SDL_GpuAcquireCommandBuffer,(SDL_GpuDevice *a),(a),return)
11511151
SDL_DYNAPI_PROC(SDL_GpuTexture*,SDL_GpuAcquireSwapchainTexture,(SDL_GpuCommandBuffer *a, SDL_Window *b, Uint32 *c, Uint32 *d),(a,b,c,d),return)
11521152
SDL_DYNAPI_PROC(void,SDL_GpuSubmit,(SDL_GpuCommandBuffer *a),(a),)
11531153
SDL_DYNAPI_PROC(SDL_GpuFence*,SDL_GpuSubmitAndAcquireFence,(SDL_GpuCommandBuffer *a),(a),return)

0 commit comments

Comments
 (0)