-
Notifications
You must be signed in to change notification settings - Fork 0
Buffer wrapper Implement #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
56e8451
ac5bb8f
60c49e5
53bab69
66aa0d3
709e19c
c0857eb
846d7ca
95014bd
57a1330
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,64 @@ export module sdl_wrapper:buffer; | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| namespace sopho { | ||||||||||||||||||||||||||||||||||||||||||||||
| export class BufferWrapper { | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_GPUBuffer *vertexBuffer{}; | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_GPUDevice *m_gpu{}; | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_GPUBuffer *m_vertex_buffer{}; | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_GPUTransferBuffer *m_transfer_buffer{}; | ||||||||||||||||||||||||||||||||||||||||||||||
| uint32_t m_transfer_buffer_size{}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public: | ||||||||||||||||||||||||||||||||||||||||||||||
| BufferWrapper() = default; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| BufferWrapper(SDL_GPUDevice *p_gpu, const SDL_GPUBufferCreateInfo *p_create_info) | ||||||||||||||||||||||||||||||||||||||||||||||
| : m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(p_gpu, p_create_info)) { | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing null check for buffer creation.
Consider validating buffer creation or documenting that callers must check via a separate method: BufferWrapper(SDL_GPUDevice *p_gpu, const SDL_GPUBufferCreateInfo *p_create_info)
: m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(p_gpu, p_create_info)) {
+ if (!m_vertex_buffer) {
+ SDL_Log("Failed to create GPU buffer");
+ // Consider throwing or setting an error state
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| void upload(void *p_data, uint32_t p_size, uint32_t p_offset) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (p_size > m_transfer_buffer_size) { | ||||||||||||||||||||||||||||||||||||||||||||||
| if (m_transfer_buffer != nullptr) { | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_ReleaseGPUTransferBuffer(m_gpu, m_transfer_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_GPUTransferBufferCreateInfo transfer_info{SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, p_size, 0}; | ||||||||||||||||||||||||||||||||||||||||||||||
| m_transfer_buffer = SDL_CreateGPUTransferBuffer(m_gpu, &transfer_info); | ||||||||||||||||||||||||||||||||||||||||||||||
| m_transfer_buffer_size = transfer_info.size; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+22
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing null check for transfer buffer creation.
Apply this diff: SDL_GPUTransferBufferCreateInfo transfer_info{SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, p_size, 0};
m_transfer_buffer = SDL_CreateGPUTransferBuffer(m_gpu, &transfer_info);
+ if (!m_transfer_buffer) {
+ SDL_Log("Failed to create GPU transfer buffer");
+ return; // or throw
+ }
m_transfer_buffer_size = transfer_info.size;📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| auto data = SDL_MapGPUTransferBuffer(m_gpu, m_transfer_buffer, false); | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_memcpy(data, p_data, p_size); | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_UnmapGPUTransferBuffer(m_gpu, m_transfer_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing null check after mapping transfer buffer.
Apply this diff: auto data = SDL_MapGPUTransferBuffer(m_gpu, m_transfer_buffer, false);
+ if (!data) {
+ SDL_Log("Failed to map GPU transfer buffer");
+ return;
+ }
SDL_memcpy(data, p_data, p_size);
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: Delay submit command in collect | ||||||||||||||||||||||||||||||||||||||||||||||
| auto command_buffer = SDL_AcquireGPUCommandBuffer(m_gpu); | ||||||||||||||||||||||||||||||||||||||||||||||
| auto copy_pass = SDL_BeginGPUCopyPass(command_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| SDL_GPUTransferBufferLocation location{}; | ||||||||||||||||||||||||||||||||||||||||||||||
| location.transfer_buffer = m_transfer_buffer; | ||||||||||||||||||||||||||||||||||||||||||||||
| location.offset = 0; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| SDL_GPUBufferRegion region{}; | ||||||||||||||||||||||||||||||||||||||||||||||
| region.buffer = m_vertex_buffer; | ||||||||||||||||||||||||||||||||||||||||||||||
| region.size = p_size; | ||||||||||||||||||||||||||||||||||||||||||||||
| region.offset = p_offset; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| SDL_UploadToGPUBuffer(copy_pass, &location, ®ion, false); | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| SDL_EndGPUCopyPass(copy_pass); | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_SubmitGPUCommandBuffer(command_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| auto data() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return m_vertex_buffer; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| ~BufferWrapper() { | ||||||||||||||||||||||||||||||||||||||||||||||
| // TODO: It's too late to release gpu buffer, gpu was released | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_ReleaseGPUBuffer(m_gpu, m_vertex_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||
| m_vertex_buffer = nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+59
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing null check before releasing vertex buffer.
Apply this diff: ~BufferWrapper() {
// TODO: It's too late to release gpu buffer, gpu was released
- SDL_ReleaseGPUBuffer(m_gpu, m_vertex_buffer);
- m_vertex_buffer = nullptr;
+ if (m_vertex_buffer) {
+ SDL_ReleaseGPUBuffer(m_gpu, m_vertex_buffer);
+ m_vertex_buffer = nullptr;
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||
| if (m_transfer_buffer) { | ||||||||||||||||||||||||||||||||||||||||||||||
| SDL_ReleaseGPUTransferBuffer(m_gpu, m_transfer_buffer); | ||||||||||||||||||||||||||||||||||||||||||||||
| m_transfer_buffer = nullptr; | ||||||||||||||||||||||||||||||||||||||||||||||
| m_transfer_buffer_size = 0; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing optional validation before dereferencing.
vertexBuffer->data()is called without checking if the optional has a value. If initialization failed, this will crash during rendering.Apply this diff:
// bind the vertex buffer SDL_GPUBufferBinding bufferBindings[1]; - bufferBindings[0].buffer = vertexBuffer->data(); // index 0 is slot 0 in this example + bufferBindings[0].buffer = vertexBuffer.has_value() ? vertexBuffer->data() : nullptr; bufferBindings[0].offset = 0; // start from the first byte + + if (!bufferBindings[0].buffer) { + SDL_EndGPURenderPass(renderPass); + SDL_SubmitGPUCommandBuffer(commandBuffer); + return SDL_APP_CONTINUE; + }🤖 Prompt for AI Agents