Skip to content

Commit 0a3cc70

Browse files
jdolanCopilot
andcommitted
Give passes a weak back-reference to their CommandBuffer
CopyPass, RenderPass, and ComputePass each now hold a non-retaining CommandBuffer *cmd. CopyPass::uploadData uses self->cmd->device->device for the SDL device instead of a directly threaded SDL_GPUDevice *. - Eliminates the SDL_GPUDevice * field on CopyPass - All three init signatures: init(self, pass, cmd) - CommandBuffer::begin*Pass passes (CommandBuffer *)self at construction - Consistent hierarchy: Pass → CommandBuffer → RenderDevice → SDL_GPUDevice Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 49eecab commit 0a3cc70

7 files changed

Lines changed: 49 additions & 24 deletions

File tree

Sources/ObjectivelyGPU/CommandBuffer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static ComputePass *beginComputePass(const CommandBuffer *self, const SDL_GPUSto
5757
SDL_GPUComputePass *pass = SDL_BeginGPUComputePass(self->cmd, storageTextures, numStorageTextures, storageBuffers, numStorageBuffers);
5858
GPU_Assert(pass, "SDL_BeginGPUComputePass");
5959

60-
return $(alloc(ComputePass), init, pass);
60+
return $(alloc(ComputePass), init, pass, (CommandBuffer *) self);
6161
}
6262

6363
/**
@@ -69,7 +69,7 @@ static CopyPass *beginCopyPass(const CommandBuffer *self) {
6969
SDL_GPUCopyPass *pass = SDL_BeginGPUCopyPass(self->cmd);
7070
GPU_Assert(pass, "SDL_BeginGPUCopyPass");
7171

72-
return $(alloc(CopyPass), init, pass, self->device->device);
72+
return $(alloc(CopyPass), init, pass, (CommandBuffer *) self);
7373
}
7474

7575
/**
@@ -81,7 +81,7 @@ static RenderPass *beginRenderPass(const CommandBuffer *self, const SDL_GPUColor
8181
SDL_GPURenderPass *pass = SDL_BeginGPURenderPass(self->cmd, colorTargets, numColorTargets, depthStencil);
8282
GPU_Assert(pass, "SDL_BeginGPURenderPass");
8383

84-
return $(alloc(RenderPass), init, pass);
84+
return $(alloc(RenderPass), init, pass, (CommandBuffer *) self);
8585
}
8686

8787
/**

Sources/ObjectivelyGPU/ComputePass.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <assert.h>
2525

26+
#include "CommandBuffer.h"
2627
#include "ComputePass.h"
2728

2829
#define _Class _ComputePass
@@ -93,15 +94,17 @@ static void dispatchComputeIndirect(const ComputePass *self, SDL_GPUBuffer *buff
9394
}
9495

9596
/**
96-
* @fn ComputePass *ComputePass::init(ComputePass *self, SDL_GPUComputePass *pass)
97+
* @fn ComputePass *ComputePass::init(ComputePass *self, SDL_GPUComputePass *pass, CommandBuffer *cmd)
9798
* @memberof ComputePass
9899
*/
99-
static ComputePass *init(ComputePass *self, SDL_GPUComputePass *pass) {
100+
static ComputePass *init(ComputePass *self, SDL_GPUComputePass *pass, CommandBuffer *cmd) {
100101

101102
self = (ComputePass *) super(Object, self, init);
102103
if (self) {
103104
self->pass = pass;
104105
assert(self->pass);
106+
self->cmd = cmd;
107+
assert(self->cmd);
105108
}
106109

107110
return self;

Sources/ObjectivelyGPU/ComputePass.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @brief ComputePass wraps an `SDL_GPUComputePass` for recording compute dispatches.
3333
*/
3434

35+
typedef struct CommandBuffer CommandBuffer;
3536
typedef struct ComputePass ComputePass;
3637
typedef struct ComputePassInterface ComputePassInterface;
3738

@@ -64,6 +65,12 @@ struct ComputePass {
6465
*/
6566
ComputePassInterface *interface;
6667

68+
/**
69+
* @brief The CommandBuffer this pass was begun from.
70+
* @private
71+
*/
72+
CommandBuffer *cmd;
73+
6774
/**
6875
* @brief The underlying SDL compute pass.
6976
* @details Set to NULL by `dealloc` after `SDL_EndGPUComputePass` is called.
@@ -129,14 +136,15 @@ struct ComputePassInterface {
129136
void (*dispatchComputeIndirect)(const ComputePass *self, SDL_GPUBuffer *buffer, Uint32 offset);
130137

131138
/**
132-
* @fn ComputePass *ComputePass::init(ComputePass *self, SDL_GPUComputePass *pass)
139+
* @fn ComputePass *ComputePass::init(ComputePass *self, SDL_GPUComputePass *pass, CommandBuffer *cmd)
133140
* @brief Initializes this ComputePass wrapping the given SDL compute pass.
134141
* @param self The ComputePass.
135142
* @param pass The SDL compute pass to wrap. Must not be NULL.
143+
* @param cmd The CommandBuffer this pass was begun from.
136144
* @return The initialized ComputePass, or NULL on failure.
137145
* @memberof ComputePass
138146
*/
139-
ComputePass *(*init)(ComputePass *self, SDL_GPUComputePass *pass);
147+
ComputePass *(*init)(ComputePass *self, SDL_GPUComputePass *pass, CommandBuffer *cmd);
140148
};
141149

142150
/**

Sources/ObjectivelyGPU/CopyPass.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include <assert.h>
2525
#include <string.h>
2626

27+
#include "CommandBuffer.h"
2728
#include "CopyPass.h"
29+
#include "RenderDevice.h"
2830

2931
#define _Class _CopyPass
3032

@@ -78,17 +80,17 @@ static void downloadTexture(const CopyPass *self, const SDL_GPUTextureRegion *sr
7880
}
7981

8082
/**
81-
* @fn CopyPass *CopyPass::init(CopyPass *self, SDL_GPUCopyPass *pass, SDL_GPUDevice *device)
83+
* @fn CopyPass *CopyPass::init(CopyPass *self, SDL_GPUCopyPass *pass, CommandBuffer *cmd)
8284
* @memberof CopyPass
8385
*/
84-
static CopyPass *init(CopyPass *self, SDL_GPUCopyPass *pass, SDL_GPUDevice *device) {
86+
static CopyPass *init(CopyPass *self, SDL_GPUCopyPass *pass, CommandBuffer *cmd) {
8587

8688
self = (CopyPass *) super(Object, self, init);
8789
if (self) {
8890
self->pass = pass;
8991
assert(self->pass);
90-
self->device = device;
91-
assert(self->device);
92+
self->cmd = cmd;
93+
assert(self->cmd);
9294
}
9395

9496
return self;
@@ -112,24 +114,24 @@ static void uploadData(const CopyPass *self, SDL_GPUBuffer *dst, const void *dat
112114
assert(data);
113115
assert(size);
114116

115-
SDL_GPUTransferBuffer *tbuf = SDL_CreateGPUTransferBuffer(self->device, &(SDL_GPUTransferBufferCreateInfo) {
117+
SDL_GPUTransferBuffer *tbuf = SDL_CreateGPUTransferBuffer(self->cmd->device->device, &(SDL_GPUTransferBufferCreateInfo) {
116118
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
117119
.size = size,
118120
});
119121
GPU_Assert(tbuf, "SDL_CreateGPUTransferBuffer");
120122

121-
void *mapped = SDL_MapGPUTransferBuffer(self->device, tbuf, cycle);
123+
void *mapped = SDL_MapGPUTransferBuffer(self->cmd->device->device, tbuf, cycle);
122124
GPU_Assert(mapped, "SDL_MapGPUTransferBuffer");
123125

124126
memcpy(mapped, data, size);
125-
SDL_UnmapGPUTransferBuffer(self->device, tbuf);
127+
SDL_UnmapGPUTransferBuffer(self->cmd->device->device, tbuf);
126128

127129
SDL_UploadToGPUBuffer(self->pass,
128130
&(SDL_GPUTransferBufferLocation) { .transfer_buffer = tbuf },
129131
&(SDL_GPUBufferRegion) { .buffer = dst, .offset = offset, .size = size },
130132
cycle);
131133

132-
SDL_ReleaseGPUTransferBuffer(self->device, tbuf);
134+
SDL_ReleaseGPUTransferBuffer(self->cmd->device->device, tbuf);
133135
}
134136

135137
/**

Sources/ObjectivelyGPU/CopyPass.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @brief CopyPass wraps an `SDL_GPUCopyPass` for uploading and copying GPU resources.
3333
*/
3434

35+
typedef struct CommandBuffer CommandBuffer;
3536
typedef struct CopyPass CopyPass;
3637
typedef struct CopyPassInterface CopyPassInterface;
3738

@@ -71,10 +72,10 @@ struct CopyPass {
7172
SDL_GPUCopyPass *pass;
7273

7374
/**
74-
* @brief The GPU device, used by `uploadData` to manage transfer buffers.
75+
* @brief The CommandBuffer this pass was begun from.
7576
* @private
7677
*/
77-
SDL_GPUDevice *device;
78+
CommandBuffer *cmd;
7879
};
7980

8081
/**
@@ -116,15 +117,15 @@ struct CopyPassInterface {
116117
void (*downloadTexture)(const CopyPass *self, const SDL_GPUTextureRegion *src, const SDL_GPUTextureTransferInfo *dst);
117118

118119
/**
119-
* @fn CopyPass *CopyPass::init(CopyPass *self, SDL_GPUCopyPass *pass, SDL_GPUDevice *device)
120+
* @fn CopyPass *CopyPass::init(CopyPass *self, SDL_GPUCopyPass *pass, CommandBuffer *cmd)
120121
* @brief Initializes this CopyPass wrapping the given SDL copy pass.
121122
* @param self The CopyPass.
122123
* @param pass The SDL copy pass to wrap. Must not be NULL.
123-
* @param device The SDL GPU device, used by `uploadData` to manage transfer buffers.
124+
* @param cmd The CommandBuffer this pass was begun from.
124125
* @return The initialized CopyPass, or NULL on failure.
125126
* @memberof CopyPass
126127
*/
127-
CopyPass *(*init)(CopyPass *self, SDL_GPUCopyPass *pass, SDL_GPUDevice *device);
128+
CopyPass *(*init)(CopyPass *self, SDL_GPUCopyPass *pass, CommandBuffer *cmd);
128129

129130
/**
130131
* @fn void CopyPass::uploadBuffer(const CopyPass *self, const SDL_GPUTransferBufferLocation *src, const SDL_GPUBufferRegion *dst, bool cycle)

Sources/ObjectivelyGPU/RenderPass.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <assert.h>
2525

26+
#include "CommandBuffer.h"
2627
#include "RenderPass.h"
2728

2829
#define _Class _RenderPass
@@ -150,15 +151,17 @@ static void drawPrimitivesIndirect(const RenderPass *self, SDL_GPUBuffer *buffer
150151
}
151152

152153
/**
153-
* @fn RenderPass *RenderPass::init(RenderPass *self, SDL_GPURenderPass *pass)
154+
* @fn RenderPass *RenderPass::init(RenderPass *self, SDL_GPURenderPass *pass, CommandBuffer *cmd)
154155
* @memberof RenderPass
155156
*/
156-
static RenderPass *init(RenderPass *self, SDL_GPURenderPass *pass) {
157+
static RenderPass *init(RenderPass *self, SDL_GPURenderPass *pass, CommandBuffer *cmd) {
157158

158159
self = (RenderPass *) super(Object, self, init);
159160
if (self) {
160161
self->pass = pass;
161162
assert(self->pass);
163+
self->cmd = cmd;
164+
assert(self->cmd);
162165
}
163166

164167
return self;

Sources/ObjectivelyGPU/RenderPass.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* @brief RenderPass wraps an `SDL_GPURenderPass` for recording draw commands.
3333
*/
3434

35+
typedef struct CommandBuffer CommandBuffer;
3536
typedef struct RenderPass RenderPass;
3637
typedef struct RenderPassInterface RenderPassInterface;
3738

@@ -64,6 +65,12 @@ struct RenderPass {
6465
*/
6566
RenderPassInterface *interface;
6667

68+
/**
69+
* @brief The CommandBuffer this pass was begun from.
70+
* @private
71+
*/
72+
CommandBuffer *cmd;
73+
6774
/**
6875
* @brief When true, the render pass clears to @c clear_color before drawing.
6976
* @details Defaults to `true` (clear to opaque black).
@@ -186,14 +193,15 @@ struct RenderPassInterface {
186193
void (*drawPrimitivesIndirect)(const RenderPass *self, SDL_GPUBuffer *buffer, Uint32 offset, Uint32 drawCount);
187194

188195
/**
189-
* @fn RenderPass *RenderPass::init(RenderPass *self, SDL_GPURenderPass *pass)
196+
* @fn RenderPass *RenderPass::init(RenderPass *self, SDL_GPURenderPass *pass, CommandBuffer *cmd)
190197
* @brief Initializes this RenderPass wrapping the given SDL render pass.
191198
* @param self The RenderPass.
192199
* @param pass The SDL render pass to wrap. Must not be NULL.
200+
* @param cmd The CommandBuffer this pass was begun from.
193201
* @return The initialized RenderPass, or NULL on failure.
194202
* @memberof RenderPass
195203
*/
196-
RenderPass *(*init)(RenderPass *self, SDL_GPURenderPass *pass);
204+
RenderPass *(*init)(RenderPass *self, SDL_GPURenderPass *pass, CommandBuffer *cmd);
197205

198206
/**
199207
* @fn void RenderPass::setBlendConstants(const RenderPass *self, SDL_FColor blendConstants)

0 commit comments

Comments
 (0)