From cd14b69ce59ac615bdb9325873a8773f0d609540 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Tue, 11 Nov 2025 22:56:09 +0800 Subject: [PATCH 01/58] feat: add class PileLineWrapper --- sdl_wrapper/CMakeLists.txt | 1 + sdl_wrapper/sdl_wrapper.ixx | 1 + sdl_wrapper/sdl_wrapper.pipeline.ixx | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 sdl_wrapper/sdl_wrapper.pipeline.ixx diff --git a/sdl_wrapper/CMakeLists.txt b/sdl_wrapper/CMakeLists.txt index 796b21f..e8c0453 100644 --- a/sdl_wrapper/CMakeLists.txt +++ b/sdl_wrapper/CMakeLists.txt @@ -10,6 +10,7 @@ target_sources(sdl_wrapper sdl_wrapper.ixx sdl_wrapper.buffer.ixx sdl_wrapper.app.ixx + sdl_wrapper.pipeline.ixx PRIVATE sdl_callback_implement.cpp ) diff --git a/sdl_wrapper/sdl_wrapper.ixx b/sdl_wrapper/sdl_wrapper.ixx index 27f47c6..77a244d 100644 --- a/sdl_wrapper/sdl_wrapper.ixx +++ b/sdl_wrapper/sdl_wrapper.ixx @@ -5,3 +5,4 @@ export module sdl_wrapper; export import :buffer; export import :app; +export import :pipeline; diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx new file mode 100644 index 0000000..27c9291 --- /dev/null +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -0,0 +1,19 @@ +// +// Created by sophomore on 11/11/25. +// +module; +#include "SDL3/SDL_gpu.h" +export module sdl_wrapper:pipeline; + +namespace sopho { + class PileLineWrapper { + SDL_GPUGraphicsPipeline *m_graphics_pipeline{}; + + public: + PileLineWrapper() = default; + + auto data() { + return m_graphics_pipeline; + } + }; +} From 4c8fae5fdbf3a18a6fd72d21cf9ca70e2314f855 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Wed, 12 Nov 2025 11:21:13 +0800 Subject: [PATCH 02/58] feat: add pipe line wrapper to User App --- main.cpp | 1 + sdl_wrapper/sdl_wrapper.pipeline.ixx | 35 ++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index 15323bb..93da734 100644 --- a/main.cpp +++ b/main.cpp @@ -23,6 +23,7 @@ struct Vertex class UserApp : public sopho::App { std::optional vertexBuffer; + std::optional pipe_line_wrapper; SDL_Window* window{}; SDL_GPUDevice* device{}; diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index 27c9291..6d546d9 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -5,15 +5,36 @@ module; #include "SDL3/SDL_gpu.h" export module sdl_wrapper:pipeline; -namespace sopho { - class PileLineWrapper { - SDL_GPUGraphicsPipeline *m_graphics_pipeline{}; +namespace sopho +{ + export class PipeLineWrapper + { + SDL_GPUGraphicsPipeline* m_graphics_pipeline{}; + SDL_GPUDevice* m_device{}; + SDL_GPUGraphicsPipelineCreateInfo m_pipeline_info{}; + bool modified = false; public: - PileLineWrapper() = default; + PipeLineWrapper() = default; + ~PipeLineWrapper() + { + if (m_graphics_pipeline) + { + SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); + m_graphics_pipeline = nullptr; + } + } + + auto data() { return m_graphics_pipeline; } - auto data() { - return m_graphics_pipeline; + auto submit() + { + if (modified) + { + modified = false; + SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); + m_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(m_device, &m_pipeline_info); + } } }; -} +} // namespace sopho From 51b78f3b8dbcbfd5d99f5478855942738ebe2235 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Wed, 12 Nov 2025 11:23:29 +0800 Subject: [PATCH 03/58] refactor: rename class --- main.cpp | 2 +- sdl_wrapper/sdl_wrapper.pipeline.ixx | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 93da734..c51a053 100644 --- a/main.cpp +++ b/main.cpp @@ -23,7 +23,7 @@ struct Vertex class UserApp : public sopho::App { std::optional vertexBuffer; - std::optional pipe_line_wrapper; + std::optional pipeline_wrapper; SDL_Window* window{}; SDL_GPUDevice* device{}; diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index 6d546d9..46a44d5 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -7,7 +7,7 @@ export module sdl_wrapper:pipeline; namespace sopho { - export class PipeLineWrapper + export class PipelineWrapper { SDL_GPUGraphicsPipeline* m_graphics_pipeline{}; SDL_GPUDevice* m_device{}; @@ -15,8 +15,8 @@ namespace sopho bool modified = false; public: - PipeLineWrapper() = default; - ~PipeLineWrapper() + PipelineWrapper() = default; + ~PipelineWrapper() { if (m_graphics_pipeline) { From 353ac03b1e152d33fa7e2c081da7c45176f32a47 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Wed, 12 Nov 2025 11:35:16 +0800 Subject: [PATCH 04/58] feat:code rabbit enable auto_apply_labels --- .coderabbit.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.coderabbit.yaml b/.coderabbit.yaml index 3424534..82f4ff3 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -22,7 +22,7 @@ reviews: related_prs: true suggested_labels: true labeling_instructions: [] - auto_apply_labels: false + auto_apply_labels: true suggested_reviewers: true auto_assign_reviewers: false in_progress_fortune: true From 788e2ceb5ab11af876552891856afe384af4eed5 Mon Sep 17 00:00:00 2001 From: WSQS <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 19:32:34 +0800 Subject: [PATCH 05/58] fix: add check before release pipeline Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- sdl_wrapper/sdl_wrapper.pipeline.ixx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index 46a44d5..2b8c775 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -32,7 +32,10 @@ namespace sopho if (modified) { modified = false; - SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); + if (m_graphics_pipeline) + { + SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); + } m_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(m_device, &m_pipeline_info); } } From c15f74dfe303b98060a877e90c0c06164b031114 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 19:42:39 +0800 Subject: [PATCH 06/58] fix:handle SDL_CreateGPUGraphicsPipeline error --- sdl_wrapper/sdl_wrapper.pipeline.ixx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index 2b8c775..e7c0c10 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -3,6 +3,7 @@ // module; #include "SDL3/SDL_gpu.h" +#include "SDL3/SDL_log.h" export module sdl_wrapper:pipeline; namespace sopho @@ -32,11 +33,15 @@ namespace sopho if (modified) { modified = false; - if (m_graphics_pipeline) - { - SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); + auto new_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(m_device, &m_pipeline_info); + if (new_graphics_pipeline) { + if (m_graphics_pipeline) { + SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); + } + m_graphics_pipeline = new_graphics_pipeline; + } else { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s get error:%s",__FUNCTION__, SDL_GetError()); } - m_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(m_device, &m_pipeline_info); } } }; From 46e77a768fe5cbc7758e9434f7dfd48628fca229 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 20:20:21 +0800 Subject: [PATCH 07/58] fix:call sdl init before app init --- sdl_wrapper/sdl_callback_implement.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/sdl_wrapper/sdl_callback_implement.cpp b/sdl_wrapper/sdl_callback_implement.cpp index 17e6843..62bf87b 100644 --- a/sdl_wrapper/sdl_callback_implement.cpp +++ b/sdl_wrapper/sdl_callback_implement.cpp @@ -9,6 +9,7 @@ import sdl_wrapper; extern sopho::App *create_app(); SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { + SDL_Init(SDL_INIT_VIDEO); auto app = create_app(); if (!app) return SDL_APP_FAILURE; From 9a086de57151079f7aed29c46c0e4deecae2debf Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 20:21:21 +0800 Subject: [PATCH 08/58] feat: add class gpu wrapper --- main.cpp | 39 ++++++++++++++------------------- sdl_wrapper/CMakeLists.txt | 1 + sdl_wrapper/sdl_wrapper.gpu.ixx | 28 +++++++++++++++++++++++ sdl_wrapper/sdl_wrapper.ixx | 1 + 4 files changed, 47 insertions(+), 22 deletions(-) create mode 100644 sdl_wrapper/sdl_wrapper.gpu.ixx diff --git a/main.cpp b/main.cpp index c51a053..75916c2 100644 --- a/main.cpp +++ b/main.cpp @@ -24,9 +24,9 @@ class UserApp : public sopho::App { std::optional vertexBuffer; std::optional pipeline_wrapper; + std::shared_ptr gpu_wrapper{std::make_shared()}; SDL_Window* window{}; - SDL_GPUDevice* device{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; // a list of vertices @@ -75,9 +75,7 @@ void main() // create a window window = SDL_CreateWindow("Hello, Triangle!", 960, 540, SDL_WINDOW_RESIZABLE); - // create the device - device = SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL); - SDL_ClaimWindowForGPUDevice(device, window); + SDL_ClaimWindowForGPUDevice(gpu_wrapper->data(), window); options.SetTargetEnvironment(shaderc_target_env_vulkan, 0); auto result = compiler.CompileGlslToSpv(vertex_source, shaderc_glsl_vertex_shader, "test.glsl", options); @@ -102,7 +100,7 @@ void main() vertexInfo.num_storage_textures = 0; vertexInfo.num_uniform_buffers = 0; - vertexShader = SDL_CreateGPUShader(device, &vertexInfo); + vertexShader = SDL_CreateGPUShader(gpu_wrapper->data(), &vertexInfo); result = compiler.CompileGlslToSpv(fragment_source, shaderc_glsl_fragment_shader, "test.frag", options); @@ -126,7 +124,7 @@ void main() fragmentInfo.num_storage_textures = 0; fragmentInfo.num_uniform_buffers = 0; - fragmentShader = SDL_CreateGPUShader(device, &fragmentInfo); + fragmentShader = SDL_CreateGPUShader(gpu_wrapper->data(), &fragmentInfo); // create the graphics pipeline pipelineInfo.vertex_shader = vertexShader; @@ -167,19 +165,19 @@ void main() colorTargetDescriptions[0].blend_state.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; colorTargetDescriptions[0].blend_state.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA; colorTargetDescriptions[0].blend_state.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; - colorTargetDescriptions[0].format = SDL_GetGPUSwapchainTextureFormat(device, window); + colorTargetDescriptions[0].format = SDL_GetGPUSwapchainTextureFormat(gpu_wrapper->data(), window); pipelineInfo.target_info.num_color_targets = 1; pipelineInfo.target_info.color_target_descriptions = colorTargetDescriptions; // create the pipeline - graphicsPipeline = SDL_CreateGPUGraphicsPipeline(device, &pipelineInfo); + graphicsPipeline = SDL_CreateGPUGraphicsPipeline(gpu_wrapper->data(), &pipelineInfo); // create the vertex buffer SDL_GPUBufferCreateInfo bufferInfo{}; bufferInfo.size = sizeof(vertices); bufferInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX; - vertexBuffer.emplace(device, &bufferInfo); + vertexBuffer.emplace(gpu_wrapper->data(), &bufferInfo); vertexBuffer->upload(&vertices, sizeof(vertices), 0); @@ -208,8 +206,8 @@ void main() // Setup Platform/Renderer backends ImGui_ImplSDL3_InitForSDLGPU(window); ImGui_ImplSDLGPU3_InitInfo init_info = {}; - init_info.Device = device; - init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(device, window); + init_info.Device = gpu_wrapper->data(); + init_info.ColorTargetFormat = SDL_GetGPUSwapchainTextureFormat(gpu_wrapper->data(), window); init_info.MSAASamples = SDL_GPU_SAMPLECOUNT_1; // Only used in multi-viewports mode. init_info.SwapchainComposition = SDL_GPU_SWAPCHAINCOMPOSITION_SDR; // Only used in multi-viewports mode. init_info.PresentMode = SDL_GPU_PRESENTMODE_VSYNC; @@ -271,12 +269,12 @@ void main() vertexInfo.num_uniform_buffers = 0; - SDL_ReleaseGPUShader(device, vertexShader); - vertexShader = SDL_CreateGPUShader(device, &vertexInfo); + SDL_ReleaseGPUShader(gpu_wrapper->data(), vertexShader); + vertexShader = SDL_CreateGPUShader(gpu_wrapper->data(), &vertexInfo); pipelineInfo.vertex_shader = vertexShader; - SDL_ReleaseGPUGraphicsPipeline(device, graphicsPipeline); - graphicsPipeline = SDL_CreateGPUGraphicsPipeline(device, &pipelineInfo); + SDL_ReleaseGPUGraphicsPipeline(gpu_wrapper->data(), graphicsPipeline); + graphicsPipeline = SDL_CreateGPUGraphicsPipeline(gpu_wrapper->data(), &pipelineInfo); } } @@ -287,7 +285,7 @@ void main() ImDrawData* draw_data = ImGui::GetDrawData(); // acquire the command buffer - SDL_GPUCommandBuffer* commandBuffer = SDL_AcquireGPUCommandBuffer(device); + SDL_GPUCommandBuffer* commandBuffer = SDL_AcquireGPUCommandBuffer(gpu_wrapper->data()); // get the swapchain texture SDL_GPUTexture* swapchainTexture; @@ -357,14 +355,11 @@ void main() vertexBuffer = std::nullopt; // Release the shader - SDL_ReleaseGPUShader(device, vertexShader); - SDL_ReleaseGPUShader(device, fragmentShader); + SDL_ReleaseGPUShader(gpu_wrapper->data(), vertexShader); + SDL_ReleaseGPUShader(gpu_wrapper->data(), fragmentShader); // release the pipeline - SDL_ReleaseGPUGraphicsPipeline(device, graphicsPipeline); - - // destroy the GPU device - SDL_DestroyGPUDevice(device); + SDL_ReleaseGPUGraphicsPipeline(gpu_wrapper->data(), graphicsPipeline); // destroy the window SDL_DestroyWindow(window); diff --git a/sdl_wrapper/CMakeLists.txt b/sdl_wrapper/CMakeLists.txt index e8c0453..9d04301 100644 --- a/sdl_wrapper/CMakeLists.txt +++ b/sdl_wrapper/CMakeLists.txt @@ -11,6 +11,7 @@ target_sources(sdl_wrapper sdl_wrapper.buffer.ixx sdl_wrapper.app.ixx sdl_wrapper.pipeline.ixx + sdl_wrapper.gpu.ixx PRIVATE sdl_callback_implement.cpp ) diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx new file mode 100644 index 0000000..75cd2bb --- /dev/null +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -0,0 +1,28 @@ +// +// Created by sophomore on 11/12/25. +// +module; +#include "SDL3/SDL_gpu.h" +#include "SDL3/SDL_log.h" +export module sdl_wrapper:gpu; + +namespace sopho { + export class GpuWrapper { + SDL_GPUDevice *m_device{}; + + public: + GpuWrapper() : m_device(SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL)) { + if (m_device == nullptr) { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s:%d %s", __FILE__, __LINE__, SDL_GetError()); + } + } + + ~GpuWrapper() { + SDL_DestroyGPUDevice(m_device); + } + + auto data() { + return m_device; + } + }; +} diff --git a/sdl_wrapper/sdl_wrapper.ixx b/sdl_wrapper/sdl_wrapper.ixx index 77a244d..fec38fd 100644 --- a/sdl_wrapper/sdl_wrapper.ixx +++ b/sdl_wrapper/sdl_wrapper.ixx @@ -6,3 +6,4 @@ export module sdl_wrapper; export import :buffer; export import :app; export import :pipeline; +export import :gpu; From 55d0db8e401c900b7356cae04d75aff176b9c66b Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 12 Nov 2025 12:26:35 +0000 Subject: [PATCH 09/58] style: format code with ClangFormat This commit fixes the style issues introduced in 46e77a7 according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- sdl_wrapper/sdl_callback_implement.cpp | 40 ++++++++++++++------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/sdl_wrapper/sdl_callback_implement.cpp b/sdl_wrapper/sdl_callback_implement.cpp index 62bf87b..4786de6 100644 --- a/sdl_wrapper/sdl_callback_implement.cpp +++ b/sdl_wrapper/sdl_callback_implement.cpp @@ -6,29 +6,33 @@ #include import sdl_wrapper; -extern sopho::App *create_app(); +extern sopho::App* create_app(); -SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv) { - SDL_Init(SDL_INIT_VIDEO); - auto app = create_app(); - if (!app) - return SDL_APP_FAILURE; - *appstate = app; - return app->init(argc, argv); +SDL_AppResult SDL_AppInit(void** appstate, int argc, char** argv) +{ + SDL_Init(SDL_INIT_VIDEO); + auto app = create_app(); + if (!app) + return SDL_APP_FAILURE; + *appstate = app; + return app->init(argc, argv); } -SDL_AppResult SDL_AppIterate(void *appstate) { - auto *app = static_cast(appstate); - return app->iterate(); +SDL_AppResult SDL_AppIterate(void* appstate) +{ + auto* app = static_cast(appstate); + return app->iterate(); } -SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { - auto *app = static_cast(appstate); - return app->event(event); +SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) +{ + auto* app = static_cast(appstate); + return app->event(event); } -void SDL_AppQuit(void *appstate, SDL_AppResult result) { - auto *app = static_cast(appstate); - app->quit(result); - delete app; +void SDL_AppQuit(void* appstate, SDL_AppResult result) +{ + auto* app = static_cast(appstate); + app->quit(result); + delete app; } From 0541f8e7f3ae010cf5d20e8724abc2a3ac441b43 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Wed, 12 Nov 2025 20:47:53 +0800 Subject: [PATCH 10/58] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`p?= =?UTF-8?q?ipiline`=20(#8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📝 Add docstrings to `pipiline` Docstrings generation was requested by @WSQS. * https://github.com/WSQS/sdl_test/pull/7#issuecomment-3521731024 The following files were modified: * `main.cpp` * `sdl_wrapper/sdl_callback_implement.cpp` * style: format code with ClangFormat This commit fixes the style issues introduced in 49e577b according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/8 --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- main.cpp | 27 ++++++++++++++++++++++ sdl_wrapper/sdl_callback_implement.cpp | 31 ++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/main.cpp b/main.cpp index 75916c2..45d4c22 100644 --- a/main.cpp +++ b/main.cpp @@ -70,6 +70,14 @@ void main() SDL_GPUVertexAttribute vertexAttributes[2]{}; SDL_GPUVertexBufferDescription vertexBufferDesctiptions[1]{}; + /** + * @brief Initialize the application: create window, compile shaders, create GPU pipeline and resources, and + * initialize ImGui. + * + * @param argc Number of command-line arguments provided to the application. + * @param argv Command-line argument vector provided to the application. + * @return SDL_AppResult SDL_APP_CONTINUE to proceed with the main loop, SDL_APP_SUCCESS to request termination. + */ virtual SDL_AppResult init(int argc, char** argv) override { // create a window @@ -216,6 +224,17 @@ void main() return SDL_APP_CONTINUE; } + /** + * @brief Advance the application by one frame: update ImGui, handle UI for vertex editing and live shader + * recompilation, record GPU commands to render the triangle and ImGui, and submit the GPU command buffer. + * + * Performs per-frame UI updates (including draggable vertex positions and a multiline shader editor), uploads + * vertex data when modified, recompiles and replaces the vertex shader and graphics pipeline on shader edits, + * acquires a GPU command buffer and the swapchain texture, executes a render pass that draws the triangle and ImGui + * draw lists, and submits the command buffer. + * + * @return SDL_AppResult SDL_APP_CONTINUE to continue the main loop. + */ virtual SDL_AppResult iterate() override { ImGui_ImplSDLGPU3_NewFrame(); @@ -346,6 +365,14 @@ void main() return SDL_APP_CONTINUE; } + /** + * @brief Cleanly shuts down the application and releases GPU and UI resources. + * + * Performs final cleanup: shuts down ImGui SDL/SDLGPU backends, destroys the ImGui context, + * releases the vertex buffer, shaders, and graphics pipeline, and destroys the SDL window. + * + * @param result The application's exit result code provided by the SDL app framework. + */ virtual void quit(SDL_AppResult result) override { ImGui_ImplSDL3_Shutdown(); diff --git a/sdl_wrapper/sdl_callback_implement.cpp b/sdl_wrapper/sdl_callback_implement.cpp index 4786de6..4407bef 100644 --- a/sdl_wrapper/sdl_callback_implement.cpp +++ b/sdl_wrapper/sdl_callback_implement.cpp @@ -8,6 +8,14 @@ import sdl_wrapper; extern sopho::App* create_app(); +/** + * @brief Initializes the SDL video subsystem, constructs the application, and invokes its initialization. + * + * @param appstate Pointer to storage that will receive the created sopho::App* on success. + * @param argc Program argument count forwarded to the application's init. + * @param argv Program argument vector forwarded to the application's init. + * @return SDL_AppResult Result returned by the application's init, or SDL_APP_FAILURE if application creation failed. + */ SDL_AppResult SDL_AppInit(void** appstate, int argc, char** argv) { SDL_Init(SDL_INIT_VIDEO); @@ -18,18 +26,41 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char** argv) return app->init(argc, argv); } +/** + * @brief Invoke the application's per-frame iterate handler. + * + * @param appstate Pointer to the sopho::App instance previously stored in SDL_AppInit. + * @return SDL_AppResult The result of the application's iterate call indicating the application's requested next + * action. + */ SDL_AppResult SDL_AppIterate(void* appstate) { auto* app = static_cast(appstate); return app->iterate(); } +/** + * @brief Dispatches an SDL event to the stored application instance. + * + * @param appstate Opaque pointer previously set by SDL_AppInit; must point to a `sopho::App` instance. + * @param event Pointer to the SDL event to deliver to the application. + * @return SDL_AppResult Result returned by the application's event handler. + */ SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) { auto* app = static_cast(appstate); return app->event(event); } +/** + * @brief Shuts down the application and releases its instance. + * + * Invokes the application's quit handler with the provided result and destroys + * the stored application instance. + * + * @param appstate Pointer to the sopho::App instance previously stored by SDL_AppInit. + * @param result Result code describing why the application is quitting. + */ void SDL_AppQuit(void* appstate, SDL_AppResult result) { auto* app = static_cast(appstate); From 6e670ef3620c8f3d4ee3131c9445679f8e05dc2f Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 20:30:14 +0800 Subject: [PATCH 11/58] fix:change log --- main.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 45d4c22..e837a63 100644 --- a/main.cpp +++ b/main.cpp @@ -90,7 +90,8 @@ void main() if (result.GetCompilationStatus() != shaderc_compilation_status_success) { - std::cerr << "[shaderc] compile error in " << "test.glsl" << ":\n" << result.GetErrorMessage() << std::endl; + SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error in test.glsl: %s", + result.GetErrorMessage().data()); } // load the vertex shader code @@ -114,7 +115,8 @@ void main() if (result.GetCompilationStatus() != shaderc_compilation_status_success) { - std::cerr << "[shaderc] compile error in " << "test.frag" << ":\n" << result.GetErrorMessage() << std::endl; + SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error in est.frag: %s", + result.GetErrorMessage().data()); } // load the fragment shader code From cf79f663dd49115262d8d200d6f76a687cfde4ec Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 20:35:54 +0800 Subject: [PATCH 12/58] fix:cadd check for gpu wrapper --- sdl_wrapper/sdl_wrapper.gpu.ixx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx index 75cd2bb..8999615 100644 --- a/sdl_wrapper/sdl_wrapper.gpu.ixx +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -18,7 +18,11 @@ namespace sopho { } ~GpuWrapper() { - SDL_DestroyGPUDevice(m_device); + if (m_device) + { + SDL_DestroyGPUDevice(m_device); + } + m_device = nullptr; } auto data() { From 06cf31eea73228a365d4eeeb3b4edde698bb51ff Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 21:02:08 +0800 Subject: [PATCH 13/58] fix:using gpu wrapper in buffer wrapper --- main.cpp | 4 +--- sdl_wrapper/sdl_wrapper.buffer.ixx | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index e837a63..988c06a 100644 --- a/main.cpp +++ b/main.cpp @@ -187,7 +187,7 @@ void main() SDL_GPUBufferCreateInfo bufferInfo{}; bufferInfo.size = sizeof(vertices); bufferInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX; - vertexBuffer.emplace(gpu_wrapper->data(), &bufferInfo); + vertexBuffer.emplace(gpu_wrapper, &bufferInfo); vertexBuffer->upload(&vertices, sizeof(vertices), 0); @@ -380,8 +380,6 @@ void main() ImGui_ImplSDL3_Shutdown(); ImGui_ImplSDLGPU3_Shutdown(); ImGui::DestroyContext(); - // release buffers - vertexBuffer = std::nullopt; // Release the shader SDL_ReleaseGPUShader(gpu_wrapper->data(), vertexShader); diff --git a/sdl_wrapper/sdl_wrapper.buffer.ixx b/sdl_wrapper/sdl_wrapper.buffer.ixx index 845436f..54083b4 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.ixx +++ b/sdl_wrapper/sdl_wrapper.buffer.ixx @@ -2,12 +2,15 @@ // Created by sophomore on 11/8/25. // module; +#include #include "SDL3/SDL_gpu.h" + export module sdl_wrapper:buffer; +import :gpu; namespace sopho { export class BufferWrapper { - SDL_GPUDevice *m_gpu{}; + std::shared_ptr m_gpu{}; SDL_GPUBuffer *m_vertex_buffer{}; SDL_GPUTransferBuffer *m_transfer_buffer{}; uint32_t m_transfer_buffer_size{}; @@ -15,26 +18,27 @@ namespace sopho { 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)) { + BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo* p_create_info) : + m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(m_gpu->data(), p_create_info)) + { } 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_ReleaseGPUTransferBuffer(m_gpu->data(), 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 = SDL_CreateGPUTransferBuffer(m_gpu->data(), &transfer_info); m_transfer_buffer_size = transfer_info.size; } - auto data = SDL_MapGPUTransferBuffer(m_gpu, m_transfer_buffer, false); + auto data = SDL_MapGPUTransferBuffer(m_gpu->data(), m_transfer_buffer, false); SDL_memcpy(data, p_data, p_size); - SDL_UnmapGPUTransferBuffer(m_gpu, m_transfer_buffer); + SDL_UnmapGPUTransferBuffer(m_gpu->data(), m_transfer_buffer); // TODO: Delay submit command in collect - auto command_buffer = SDL_AcquireGPUCommandBuffer(m_gpu); + auto command_buffer = SDL_AcquireGPUCommandBuffer(m_gpu->data()); auto copy_pass = SDL_BeginGPUCopyPass(command_buffer); SDL_GPUTransferBufferLocation location{}; @@ -58,10 +62,10 @@ namespace sopho { ~BufferWrapper() { // TODO: It's too late to release gpu buffer, gpu was released - SDL_ReleaseGPUBuffer(m_gpu, m_vertex_buffer); + SDL_ReleaseGPUBuffer(m_gpu->data(), m_vertex_buffer); m_vertex_buffer = nullptr; if (m_transfer_buffer) { - SDL_ReleaseGPUTransferBuffer(m_gpu, m_transfer_buffer); + SDL_ReleaseGPUTransferBuffer(m_gpu->data(), m_transfer_buffer); m_transfer_buffer = nullptr; m_transfer_buffer_size = 0; } From 99a824f45bb9625084129789ab9d03e264c40253 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 21:04:58 +0800 Subject: [PATCH 14/58] fix:rename variable --- main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 988c06a..e98198b 100644 --- a/main.cpp +++ b/main.cpp @@ -22,7 +22,7 @@ struct Vertex class UserApp : public sopho::App { - std::optional vertexBuffer; + std::optional vertex_buffer; std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; @@ -187,9 +187,9 @@ void main() SDL_GPUBufferCreateInfo bufferInfo{}; bufferInfo.size = sizeof(vertices); bufferInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX; - vertexBuffer.emplace(gpu_wrapper, &bufferInfo); + vertex_buffer.emplace(gpu_wrapper, &bufferInfo); - vertexBuffer->upload(&vertices, sizeof(vertices), 0); + vertex_buffer->upload(&vertices, sizeof(vertices), 0); // Setup Dear ImGui context IMGUI_CHECKVERSION(); @@ -252,7 +252,7 @@ void main() change = ImGui::DragFloat3("node3", vertices[2].position(), 0.01f, -1.f, 1.f) || change; if (change) { - vertexBuffer->upload(&vertices, sizeof(vertices), 0); + vertex_buffer->upload(&vertices, sizeof(vertices), 0); } ImGui::End(); } @@ -338,7 +338,7 @@ void main() // bind the vertex buffer SDL_GPUBufferBinding bufferBindings[1]; - bufferBindings[0].buffer = vertexBuffer->data(); // index 0 is slot 0 in this example + bufferBindings[0].buffer = vertex_buffer->data(); // index 0 is slot 0 in this example bufferBindings[0].offset = 0; // start from the first byte SDL_BindGPUVertexBuffers(renderPass, 0, bufferBindings, 1); // bind one buffer starting from slot 0 From 9cd5876ea5d4ec4d42d629811b77c64e8dfe86c0 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 21:10:33 +0800 Subject: [PATCH 15/58] fix:change vertex buffer to value semantics --- main.cpp | 14 ++++---------- sdl_wrapper/sdl_wrapper.buffer.ixx | 4 ++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/main.cpp b/main.cpp index e98198b..3051b79 100644 --- a/main.cpp +++ b/main.cpp @@ -22,9 +22,9 @@ struct Vertex class UserApp : public sopho::App { - std::optional vertex_buffer; std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; + sopho::BufferWrapper vertex_buffer{gpu_wrapper,{SDL_GPU_BUFFERUSAGE_VERTEX,sizeof(vertices)}}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; @@ -183,13 +183,7 @@ void main() // create the pipeline graphicsPipeline = SDL_CreateGPUGraphicsPipeline(gpu_wrapper->data(), &pipelineInfo); - // create the vertex buffer - SDL_GPUBufferCreateInfo bufferInfo{}; - bufferInfo.size = sizeof(vertices); - bufferInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX; - vertex_buffer.emplace(gpu_wrapper, &bufferInfo); - - vertex_buffer->upload(&vertices, sizeof(vertices), 0); + vertex_buffer.upload(&vertices, sizeof(vertices), 0); // Setup Dear ImGui context IMGUI_CHECKVERSION(); @@ -252,7 +246,7 @@ void main() change = ImGui::DragFloat3("node3", vertices[2].position(), 0.01f, -1.f, 1.f) || change; if (change) { - vertex_buffer->upload(&vertices, sizeof(vertices), 0); + vertex_buffer.upload(&vertices, sizeof(vertices), 0); } ImGui::End(); } @@ -338,7 +332,7 @@ void main() // bind the vertex buffer SDL_GPUBufferBinding bufferBindings[1]; - bufferBindings[0].buffer = vertex_buffer->data(); // index 0 is slot 0 in this example + bufferBindings[0].buffer = vertex_buffer.data(); // index 0 is slot 0 in this example bufferBindings[0].offset = 0; // start from the first byte SDL_BindGPUVertexBuffers(renderPass, 0, bufferBindings, 1); // bind one buffer starting from slot 0 diff --git a/sdl_wrapper/sdl_wrapper.buffer.ixx b/sdl_wrapper/sdl_wrapper.buffer.ixx index 54083b4..4aa7e4e 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.ixx +++ b/sdl_wrapper/sdl_wrapper.buffer.ixx @@ -18,8 +18,8 @@ namespace sopho { public: BufferWrapper() = default; - BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo* p_create_info) : - m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(m_gpu->data(), p_create_info)) + BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo& p_create_info) : + m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(m_gpu->data(), &p_create_info)) { } From acd6c4c5e57695687a607cb49c799cf7b2bc171b Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 12 Nov 2025 13:11:07 +0000 Subject: [PATCH 16/58] style: format code with ClangFormat This commit fixes the style issues introduced in 9cd5876 according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 3051b79..079d948 100644 --- a/main.cpp +++ b/main.cpp @@ -24,7 +24,7 @@ class UserApp : public sopho::App { std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; - sopho::BufferWrapper vertex_buffer{gpu_wrapper,{SDL_GPU_BUFFERUSAGE_VERTEX,sizeof(vertices)}}; + sopho::BufferWrapper vertex_buffer{gpu_wrapper, {SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices)}}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; From 6bbdfd02160e164b28d6b7de721ac30c25c1f7d0 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 21:12:45 +0800 Subject: [PATCH 17/58] fix: remote typo --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 079d948..48bfece 100644 --- a/main.cpp +++ b/main.cpp @@ -115,7 +115,7 @@ void main() if (result.GetCompilationStatus() != shaderc_compilation_status_success) { - SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error in est.frag: %s", + SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error in test.frag: %s", result.GetErrorMessage().data()); } From 97964265e18d63c77d438d3ef7e363cc69a1d564 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 22:25:04 +0800 Subject: [PATCH 18/58] break: split implement unit of module --- sdl_wrapper/CMakeLists.txt | 1 + sdl_wrapper/sdl_wrapper.buffer.cpp | 65 +++++++++++++++++++++++++++ sdl_wrapper/sdl_wrapper.buffer.ixx | 71 ++++++------------------------ sdl_wrapper/sdl_wrapper.gpu.ixx | 24 +++++----- sdl_wrapper/sdl_wrapper.ixx | 4 +- 5 files changed, 95 insertions(+), 70 deletions(-) create mode 100644 sdl_wrapper/sdl_wrapper.buffer.cpp diff --git a/sdl_wrapper/CMakeLists.txt b/sdl_wrapper/CMakeLists.txt index 9d04301..20d2fef 100644 --- a/sdl_wrapper/CMakeLists.txt +++ b/sdl_wrapper/CMakeLists.txt @@ -13,6 +13,7 @@ target_sources(sdl_wrapper sdl_wrapper.pipeline.ixx sdl_wrapper.gpu.ixx PRIVATE + sdl_wrapper.buffer.cpp sdl_callback_implement.cpp ) diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp new file mode 100644 index 0000000..a96b2fb --- /dev/null +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -0,0 +1,65 @@ +// +// Created by sophomore on 11/12/25. +// +module; +#include "SDL3/SDL_gpu.h" +#include +module sdl_wrapper; +import :buffer; +import :gpu; + +namespace sopho +{ + BufferWrapper::BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo& p_create_info) : + m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(m_gpu->data(), &p_create_info)) + { + } + BufferWrapper::~BufferWrapper() + { + // TODO: It's too late to release gpu buffer, gpu was released + SDL_ReleaseGPUBuffer(m_gpu->data(), m_vertex_buffer); + m_vertex_buffer = nullptr; + if (m_transfer_buffer) + { + SDL_ReleaseGPUTransferBuffer(m_gpu->data(), m_transfer_buffer); + m_transfer_buffer = nullptr; + m_transfer_buffer_size = 0; + } + } + + void BufferWrapper::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->data(), m_transfer_buffer); + } + SDL_GPUTransferBufferCreateInfo transfer_info{SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, p_size, 0}; + m_transfer_buffer = SDL_CreateGPUTransferBuffer(m_gpu->data(), &transfer_info); + m_transfer_buffer_size = transfer_info.size; + } + + auto data = SDL_MapGPUTransferBuffer(m_gpu->data(), m_transfer_buffer, false); + SDL_memcpy(data, p_data, p_size); + SDL_UnmapGPUTransferBuffer(m_gpu->data(), m_transfer_buffer); + + // TODO: Delay submit command in collect + auto command_buffer = SDL_AcquireGPUCommandBuffer(m_gpu->data()); + 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); + } +} // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.buffer.ixx b/sdl_wrapper/sdl_wrapper.buffer.ixx index 4aa7e4e..ad95cd8 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.ixx +++ b/sdl_wrapper/sdl_wrapper.buffer.ixx @@ -4,71 +4,26 @@ module; #include #include "SDL3/SDL_gpu.h" - export module sdl_wrapper:buffer; -import :gpu; -namespace sopho { - export class BufferWrapper { +namespace sopho +{ + class GpuWrapper; + export class BufferWrapper + { std::shared_ptr m_gpu{}; - SDL_GPUBuffer *m_vertex_buffer{}; - SDL_GPUTransferBuffer *m_transfer_buffer{}; + SDL_GPUBuffer* m_vertex_buffer{}; + SDL_GPUTransferBuffer* m_transfer_buffer{}; uint32_t m_transfer_buffer_size{}; public: - BufferWrapper() = default; - - BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo& p_create_info) : - m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(m_gpu->data(), &p_create_info)) - { - } - - 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->data(), m_transfer_buffer); - } - SDL_GPUTransferBufferCreateInfo transfer_info{SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, p_size, 0}; - m_transfer_buffer = SDL_CreateGPUTransferBuffer(m_gpu->data(), &transfer_info); - m_transfer_buffer_size = transfer_info.size; - } - - auto data = SDL_MapGPUTransferBuffer(m_gpu->data(), m_transfer_buffer, false); - SDL_memcpy(data, p_data, p_size); - SDL_UnmapGPUTransferBuffer(m_gpu->data(), m_transfer_buffer); - - // TODO: Delay submit command in collect - auto command_buffer = SDL_AcquireGPUCommandBuffer(m_gpu->data()); - 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); + BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo& p_create_info); - SDL_EndGPUCopyPass(copy_pass); - SDL_SubmitGPUCommandBuffer(command_buffer); - } + void upload(void* p_data, uint32_t p_size, uint32_t p_offset); - auto data() { - return m_vertex_buffer; - } + auto data() { return m_vertex_buffer; } - ~BufferWrapper() { - // TODO: It's too late to release gpu buffer, gpu was released - SDL_ReleaseGPUBuffer(m_gpu->data(), m_vertex_buffer); - m_vertex_buffer = nullptr; - if (m_transfer_buffer) { - SDL_ReleaseGPUTransferBuffer(m_gpu->data(), m_transfer_buffer); - m_transfer_buffer = nullptr; - m_transfer_buffer_size = 0; - } - } + ~BufferWrapper(); + friend GpuWrapper; }; -} +} // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx index 8999615..102bfa4 100644 --- a/sdl_wrapper/sdl_wrapper.gpu.ixx +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -2,22 +2,28 @@ // Created by sophomore on 11/12/25. // module; +#include #include "SDL3/SDL_gpu.h" #include "SDL3/SDL_log.h" export module sdl_wrapper:gpu; -namespace sopho { - export class GpuWrapper { - SDL_GPUDevice *m_device{}; +namespace sopho +{ + export class GpuWrapper + { + SDL_GPUDevice* m_device{}; public: - GpuWrapper() : m_device(SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL)) { - if (m_device == nullptr) { + GpuWrapper() : m_device(SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, nullptr)) + { + if (m_device == nullptr) + { SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s:%d %s", __FILE__, __LINE__, SDL_GetError()); } } - ~GpuWrapper() { + ~GpuWrapper() + { if (m_device) { SDL_DestroyGPUDevice(m_device); @@ -25,8 +31,6 @@ namespace sopho { m_device = nullptr; } - auto data() { - return m_device; - } + auto data() { return m_device; } }; -} +} // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.ixx b/sdl_wrapper/sdl_wrapper.ixx index fec38fd..53fdd1e 100644 --- a/sdl_wrapper/sdl_wrapper.ixx +++ b/sdl_wrapper/sdl_wrapper.ixx @@ -3,7 +3,7 @@ // export module sdl_wrapper; -export import :buffer; export import :app; -export import :pipeline; export import :gpu; +export import :buffer; +export import :pipeline; From 9097331972df70dde50556bb527e595a55b5a299 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 12 Nov 2025 14:25:38 +0000 Subject: [PATCH 19/58] style: format code with ClangFormat This commit fixes the style issues introduced in 9796426 according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- sdl_wrapper/sdl_wrapper.buffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp index a96b2fb..88b3aec 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.cpp +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -2,8 +2,8 @@ // Created by sophomore on 11/12/25. // module; -#include "SDL3/SDL_gpu.h" #include +#include "SDL3/SDL_gpu.h" module sdl_wrapper; import :buffer; import :gpu; From 3505ce745d762b682f1b8680889dfb62b2f6a7ec Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 23:31:37 +0800 Subject: [PATCH 20/58] fix: add log for windows ci --- .github/workflows/cmake-windows.yml | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/.github/workflows/cmake-windows.yml b/.github/workflows/cmake-windows.yml index 970e4ab..01d7851 100644 --- a/.github/workflows/cmake-windows.yml +++ b/.github/workflows/cmake-windows.yml @@ -61,7 +61,20 @@ jobs: - name: Build # Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator). - run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} + run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }} 1> build.log 2>&1 + shell: pwsh + + - name: Print log tail on failure + if: failure() + run: Get-Content build.log -Tail 200 + shell: pwsh + + - name: Upload full build log + if: always() + uses: actions/upload-artifact@v4 + with: + name: build-log-${{ matrix.os }}-${{ matrix.c_compiler }}-${{ matrix.build_type }} + path: build.log - name: Test working-directory: ${{ steps.strings.outputs.build-output-dir }} From f388c65359e8f3c2b15dcb57e549e6d5a1e58c5f Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 23:37:59 +0800 Subject: [PATCH 21/58] fix: rename log name --- .github/workflows/cmake-windows.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cmake-windows.yml b/.github/workflows/cmake-windows.yml index 01d7851..acb14af 100644 --- a/.github/workflows/cmake-windows.yml +++ b/.github/workflows/cmake-windows.yml @@ -73,7 +73,7 @@ jobs: if: always() uses: actions/upload-artifact@v4 with: - name: build-log-${{ matrix.os }}-${{ matrix.c_compiler }}-${{ matrix.build_type }} + name: ${{ matrix.os }}-${{ matrix.c_compiler }}-${{ matrix.build_type }}-build-log-${{ github.sha }} path: build.log - name: Test From f75fa223df1e9b3e882db98ac59cb591dde6ee2f Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 23:46:49 +0800 Subject: [PATCH 22/58] fix: Add type --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 48bfece..ae048bf 100644 --- a/main.cpp +++ b/main.cpp @@ -24,7 +24,7 @@ class UserApp : public sopho::App { std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; - sopho::BufferWrapper vertex_buffer{gpu_wrapper, {SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices)}}; + sopho::BufferWrapper vertex_buffer{gpu_wrapper, SDL_GPUBufferCreateInfo{SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices)}}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; From c8b39e97861c7ac5c743bc3165dbd70bec987d34 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Wed, 12 Nov 2025 15:47:05 +0000 Subject: [PATCH 23/58] style: format code with ClangFormat This commit fixes the style issues introduced in f75fa22 according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- main.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index ae048bf..800a1b4 100644 --- a/main.cpp +++ b/main.cpp @@ -24,7 +24,8 @@ class UserApp : public sopho::App { std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; - sopho::BufferWrapper vertex_buffer{gpu_wrapper, SDL_GPUBufferCreateInfo{SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices)}}; + sopho::BufferWrapper vertex_buffer{gpu_wrapper, + SDL_GPUBufferCreateInfo{SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices)}}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; From b6a0af4f0934fd452bdd9c6f044f75bec0a237c2 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Wed, 12 Nov 2025 23:59:24 +0800 Subject: [PATCH 24/58] fix: Add module unit sdl_wrapper:decl --- sdl_wrapper/CMakeLists.txt | 1 + sdl_wrapper/sdl_wrapper.app.ixx | 12 +++++++----- sdl_wrapper/sdl_wrapper.buffer.ixx | 6 +++--- sdl_wrapper/sdl_wrapper.decl.ixx | 13 +++++++++++++ sdl_wrapper/sdl_wrapper.ixx | 1 + sdl_wrapper/sdl_wrapper.pipeline.ixx | 4 ++-- 6 files changed, 27 insertions(+), 10 deletions(-) create mode 100644 sdl_wrapper/sdl_wrapper.decl.ixx diff --git a/sdl_wrapper/CMakeLists.txt b/sdl_wrapper/CMakeLists.txt index 20d2fef..06e6353 100644 --- a/sdl_wrapper/CMakeLists.txt +++ b/sdl_wrapper/CMakeLists.txt @@ -12,6 +12,7 @@ target_sources(sdl_wrapper sdl_wrapper.app.ixx sdl_wrapper.pipeline.ixx sdl_wrapper.gpu.ixx + sdl_wrapper.decl.ixx PRIVATE sdl_wrapper.buffer.cpp sdl_callback_implement.cpp diff --git a/sdl_wrapper/sdl_wrapper.app.ixx b/sdl_wrapper/sdl_wrapper.app.ixx index 5ac5bc3..253d070 100644 --- a/sdl_wrapper/sdl_wrapper.app.ixx +++ b/sdl_wrapper/sdl_wrapper.app.ixx @@ -4,17 +4,19 @@ module; #include "SDL3/SDL_init.h" export module sdl_wrapper:app; -namespace sopho { - export class App { +export namespace sopho +{ + class App + { public: virtual ~App() = default; - virtual SDL_AppResult init(int argc, char **argv) = 0; + virtual SDL_AppResult init(int argc, char** argv) = 0; virtual SDL_AppResult iterate() = 0; - virtual SDL_AppResult event(SDL_Event *event) = 0; + virtual SDL_AppResult event(SDL_Event* event) = 0; virtual void quit(SDL_AppResult result) = 0; }; -} +} // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.buffer.ixx b/sdl_wrapper/sdl_wrapper.buffer.ixx index ad95cd8..971153a 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.ixx +++ b/sdl_wrapper/sdl_wrapper.buffer.ixx @@ -5,11 +5,11 @@ module; #include #include "SDL3/SDL_gpu.h" export module sdl_wrapper:buffer; +import :decl; -namespace sopho +export namespace sopho { - class GpuWrapper; - export class BufferWrapper + class BufferWrapper { std::shared_ptr m_gpu{}; SDL_GPUBuffer* m_vertex_buffer{}; diff --git a/sdl_wrapper/sdl_wrapper.decl.ixx b/sdl_wrapper/sdl_wrapper.decl.ixx new file mode 100644 index 0000000..0ac8bce --- /dev/null +++ b/sdl_wrapper/sdl_wrapper.decl.ixx @@ -0,0 +1,13 @@ +// +// Created by sophomore on 11/12/25. +// + +export module sdl_wrapper:decl; + +export namespace sopho +{ + class App; + class GpuWrapper; + class BufferWrapper; + class PipelineWrapper; +} diff --git a/sdl_wrapper/sdl_wrapper.ixx b/sdl_wrapper/sdl_wrapper.ixx index 53fdd1e..9e7a9fd 100644 --- a/sdl_wrapper/sdl_wrapper.ixx +++ b/sdl_wrapper/sdl_wrapper.ixx @@ -3,6 +3,7 @@ // export module sdl_wrapper; +export import :decl; export import :app; export import :gpu; export import :buffer; diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index e7c0c10..44f1c31 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -6,9 +6,9 @@ module; #include "SDL3/SDL_log.h" export module sdl_wrapper:pipeline; -namespace sopho +export namespace sopho { - export class PipelineWrapper + class PipelineWrapper { SDL_GPUGraphicsPipeline* m_graphics_pipeline{}; SDL_GPUDevice* m_device{}; From f6535467464062163bb66e1d5c060a0ae331bbf8 Mon Sep 17 00:00:00 2001 From: WSQS <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 09:29:40 +0800 Subject: [PATCH 25/58] Fix resource leak (#10) * feat:release gpu before windows * feat:release window from gpu devices --------- Co-authored-by: Sophomore --- main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.cpp b/main.cpp index 800a1b4..bccaffb 100644 --- a/main.cpp +++ b/main.cpp @@ -383,6 +383,8 @@ void main() // release the pipeline SDL_ReleaseGPUGraphicsPipeline(gpu_wrapper->data(), graphicsPipeline); + SDL_ReleaseWindowFromGPUDevice(gpu_wrapper->data(), window); + // destroy the window SDL_DestroyWindow(window); } From a20179b7962b2302fd0246ff53308b5170bd0fb9 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 09:52:43 +0800 Subject: [PATCH 26/58] fix: remove todo, the destroy order is defined by shared_ptr --- sdl_wrapper/sdl_wrapper.buffer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp index 88b3aec..b2ae7bc 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.cpp +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -16,7 +16,6 @@ namespace sopho } BufferWrapper::~BufferWrapper() { - // TODO: It's too late to release gpu buffer, gpu was released SDL_ReleaseGPUBuffer(m_gpu->data(), m_vertex_buffer); m_vertex_buffer = nullptr; if (m_transfer_buffer) From 60d5ebde205f4b212f11f16b73e6ebed41ae0157 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 10:20:27 +0800 Subject: [PATCH 27/58] feat: add function create_buffer to GpuWrapper --- main.cpp | 3 +-- sdl_wrapper/sdl_wrapper.buffer.cpp | 5 +---- sdl_wrapper/sdl_wrapper.buffer.ixx | 9 ++++++--- sdl_wrapper/sdl_wrapper.gpu.ixx | 12 ++++++++++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index bccaffb..58a8dab 100644 --- a/main.cpp +++ b/main.cpp @@ -24,8 +24,7 @@ class UserApp : public sopho::App { std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; - sopho::BufferWrapper vertex_buffer{gpu_wrapper, - SDL_GPUBufferCreateInfo{SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices)}}; + sopho::BufferWrapper vertex_buffer{gpu_wrapper->create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX,sizeof(vertices))}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp index b2ae7bc..f476a12 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.cpp +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -10,10 +10,7 @@ import :gpu; namespace sopho { - BufferWrapper::BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo& p_create_info) : - m_gpu(p_gpu), m_vertex_buffer(SDL_CreateGPUBuffer(m_gpu->data(), &p_create_info)) - { - } + BufferWrapper::~BufferWrapper() { SDL_ReleaseGPUBuffer(m_gpu->data(), m_vertex_buffer); diff --git a/sdl_wrapper/sdl_wrapper.buffer.ixx b/sdl_wrapper/sdl_wrapper.buffer.ixx index 971153a..a813db7 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.ixx +++ b/sdl_wrapper/sdl_wrapper.buffer.ixx @@ -9,16 +9,19 @@ import :decl; export namespace sopho { - class BufferWrapper + class BufferWrapper { std::shared_ptr m_gpu{}; SDL_GPUBuffer* m_vertex_buffer{}; SDL_GPUTransferBuffer* m_transfer_buffer{}; uint32_t m_transfer_buffer_size{}; - public: - BufferWrapper(std::shared_ptr p_gpu, const SDL_GPUBufferCreateInfo& p_create_info); + BufferWrapper(std::shared_ptr p_gpu, SDL_GPUBuffer* p_buffer) : + m_gpu(p_gpu), m_vertex_buffer(p_buffer) + { + } + public: void upload(void* p_data, uint32_t p_size, uint32_t p_offset); auto data() { return m_vertex_buffer; } diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx index 102bfa4..8a411b8 100644 --- a/sdl_wrapper/sdl_wrapper.gpu.ixx +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -6,10 +6,10 @@ module; #include "SDL3/SDL_gpu.h" #include "SDL3/SDL_log.h" export module sdl_wrapper:gpu; - +import :buffer; namespace sopho { - export class GpuWrapper + export class GpuWrapper : public std::enable_shared_from_this { SDL_GPUDevice* m_device{}; @@ -32,5 +32,13 @@ namespace sopho } auto data() { return m_device; } + + auto create_buffer(SDL_GPUBufferUsageFlags flag, uint32_t size) + { + SDL_GPUBufferCreateInfo create_info{flag, size}; + auto buffer = SDL_CreateGPUBuffer(m_device, &create_info); + BufferWrapper result(shared_from_this(), buffer); + return result; + } }; } // namespace sopho From 7f6eaefd97a09176ef2a99641549c67860bc4d1e Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 02:22:48 +0000 Subject: [PATCH 28/58] style: format code with ClangFormat This commit fixes the style issues introduced in 60d5ebd according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 58a8dab..50012ac 100644 --- a/main.cpp +++ b/main.cpp @@ -24,7 +24,7 @@ class UserApp : public sopho::App { std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; - sopho::BufferWrapper vertex_buffer{gpu_wrapper->create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX,sizeof(vertices))}; + sopho::BufferWrapper vertex_buffer{gpu_wrapper->create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices))}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; From 3deb0231d429bd57f615e4ada42ea543e87698b3 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 11:08:16 +0800 Subject: [PATCH 29/58] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`p?= =?UTF-8?q?ipiline`=20(#9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📝 Add docstrings to `pipiline` Docstrings generation was requested by @WSQS. * https://github.com/WSQS/sdl_test/pull/7#issuecomment-3519726972 The following files were modified: * `main.cpp` * `sdl_wrapper/sdl_callback_implement.cpp` * `sdl_wrapper/sdl_wrapper.buffer.cpp` * style: format code with ClangFormat This commit fixes the style issues introduced in 32e7ade according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/9 --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> Co-authored-by: WSQS <17792626+WSQS@users.noreply.github.com> --- main.cpp | 40 ++++++++++++++++---------- sdl_wrapper/sdl_callback_implement.cpp | 7 ++--- sdl_wrapper/sdl_wrapper.buffer.cpp | 21 ++++++++++++++ 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/main.cpp b/main.cpp index 50012ac..8d1114e 100644 --- a/main.cpp +++ b/main.cpp @@ -71,12 +71,16 @@ void main() SDL_GPUVertexBufferDescription vertexBufferDesctiptions[1]{}; /** - * @brief Initialize the application: create window, compile shaders, create GPU pipeline and resources, and - * initialize ImGui. + * @brief Initialize the application by creating the window, compiling shaders, creating the GPU pipeline and + * resources, and initializing ImGui. * - * @param argc Number of command-line arguments provided to the application. - * @param argv Command-line argument vector provided to the application. - * @return SDL_AppResult SDL_APP_CONTINUE to proceed with the main loop, SDL_APP_SUCCESS to request termination. + * Performs window creation and GPU device claim, compiles vertex and fragment GLSL to SPIR-V and creates GPU shader + * objects, configures and creates the graphics pipeline (vertex input, attributes, and color target/blend state), + * uploads initial vertex data to the vertex buffer, and initializes Dear ImGui (context, style, scaling, and + * SDL3/SDLGPU backends). + * + * @return SDL_AppResult `SDL_APP_CONTINUE` to enter the main loop, `SDL_APP_SUCCESS` to request immediate + * termination. */ virtual SDL_AppResult init(int argc, char** argv) override { @@ -221,13 +225,12 @@ void main() } /** - * @brief Advance the application by one frame: update ImGui, handle UI for vertex editing and live shader - * recompilation, record GPU commands to render the triangle and ImGui, and submit the GPU command buffer. + * @brief Advance the application by one frame: update UI state, handle vertex edits and live shader recompilation, + * and render. * - * Performs per-frame UI updates (including draggable vertex positions and a multiline shader editor), uploads - * vertex data when modified, recompiles and replaces the vertex shader and graphics pipeline on shader edits, - * acquires a GPU command buffer and the swapchain texture, executes a render pass that draws the triangle and ImGui - * draw lists, and submits the command buffer. + * Processes ImGui frames, applies interactive vertex edits (uploading vertex data when changed), recompiles and + * replaces the vertex shader and graphics pipeline on edits, acquires a GPU command buffer and swapchain texture, + * records a render pass that draws the triangle and ImGui draw lists, and submits the command buffer. * * @return SDL_AppResult SDL_APP_CONTINUE to continue the main loop. */ @@ -349,6 +352,12 @@ void main() return SDL_APP_CONTINUE; } + /** + * @brief Handle an SDL event by forwarding it to ImGui and handling window-close requests. + * + * @param event Pointer to the SDL event to process. + * @return SDL_AppResult `SDL_APP_SUCCESS` when a window close was requested, `SDL_APP_CONTINUE` otherwise. + */ virtual SDL_AppResult event(SDL_Event* event) override { ImGui_ImplSDL3_ProcessEvent(event); @@ -362,12 +371,13 @@ void main() } /** - * @brief Cleanly shuts down the application and releases GPU and UI resources. + * @brief Shut down the application and release GPU and UI resources. * - * Performs final cleanup: shuts down ImGui SDL/SDLGPU backends, destroys the ImGui context, - * releases the vertex buffer, shaders, and graphics pipeline, and destroys the SDL window. + * Shuts down the ImGui SDL3 and SDLGPU backends, destroys the ImGui context, + * releases the vertex and fragment GPU shaders and the graphics pipeline, and + * destroys the SDL window. * - * @param result The application's exit result code provided by the SDL app framework. + * @param result Application exit result code provided by the SDL app framework. */ virtual void quit(SDL_AppResult result) override { diff --git a/sdl_wrapper/sdl_callback_implement.cpp b/sdl_wrapper/sdl_callback_implement.cpp index 4407bef..c684dea 100644 --- a/sdl_wrapper/sdl_callback_implement.cpp +++ b/sdl_wrapper/sdl_callback_implement.cpp @@ -27,11 +27,10 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char** argv) } /** - * @brief Invoke the application's per-frame iterate handler. + * @brief Run a single per-frame iteration on the application. * - * @param appstate Pointer to the sopho::App instance previously stored in SDL_AppInit. - * @return SDL_AppResult The result of the application's iterate call indicating the application's requested next - * action. + * @param appstate Pointer to the sopho::App instance stored by SDL_AppInit. + * @return SDL_AppResult The application's requested next action. */ SDL_AppResult SDL_AppIterate(void* appstate) { diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp index f476a12..2ecca51 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.cpp +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -10,6 +10,15 @@ import :gpu; namespace sopho { + /** + * @brief Releases GPU resources owned by this BufferWrapper. + * + * Releases the GPU vertex buffer and, if present, the transfer (staging) buffer, + * then clears the corresponding handles and resets the transfer buffer size. + * + * @note If the associated GPU has already been destroyed, releasing these resources + * may have no effect or may be too late to perform a proper cleanup. + */ BufferWrapper::~BufferWrapper() { @@ -23,6 +32,18 @@ namespace sopho } } + /** + * @brief Uploads a block of data into the wrapped GPU vertex buffer at the specified offset. + * + * If the internal transfer (staging) buffer is smaller than `p_size`, it will be reallocated + * to accommodate the upload. The function copies `p_size` bytes from `p_data` into the transfer + * buffer and enqueues a GPU copy pass that transfers those bytes into the vertex buffer at + * `p_offset`. + * + * @param p_data Pointer to the source data to upload. + * @param p_size Size in bytes of the data to upload. + * @param p_offset Byte offset within the vertex buffer where the data will be written. + */ void BufferWrapper::upload(void* p_data, uint32_t p_size, uint32_t p_offset) { if (p_size > m_transfer_buffer_size) From 28c6119c1490b3f6ec21a795736d63dfda7cdb1c Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 13:42:37 +0800 Subject: [PATCH 30/58] Add workflow clang-tidy-review --- .github/workflows/clang-tidy-review.yml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 .github/workflows/clang-tidy-review.yml diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml new file mode 100644 index 0000000..0e9e604 --- /dev/null +++ b/.github/workflows/clang-tidy-review.yml @@ -0,0 +1,24 @@ +name: clang-tidy-review + +on: + pull_request: + +jobs: + clang-tidy-review: + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Configure (CMake) + run: | + cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + + - name: Run clang-tidy-review + uses: ZedThree/clang-tidy-review@v0.21.0 + id: review + with: + token: ${{ secrets.GITHUB_TOKEN }} + + build_dir: build From ab1c8dc0f915446b1bbe949211df1584e395cbfa Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 13:58:34 +0800 Subject: [PATCH 31/58] fix:workflow set compiler --- .github/workflows/clang-tidy-review.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml index 0e9e604..069d795 100644 --- a/.github/workflows/clang-tidy-review.yml +++ b/.github/workflows/clang-tidy-review.yml @@ -13,12 +13,16 @@ jobs: - name: Configure (CMake) run: | - cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + cmake -S ${{ github.workspace }} + -B build + -DCMAKE_CXX_COMPILER=clang++ + -DCMAKE_C_COMPILER=clang + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - name: Run clang-tidy-review uses: ZedThree/clang-tidy-review@v0.21.0 id: review with: token: ${{ secrets.GITHUB_TOKEN }} - build_dir: build From cec23f39f6d7b371d4039c3546a097e16290dad1 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 14:05:05 +0800 Subject: [PATCH 32/58] fix:install package for ubuntu --- .github/workflows/clang-tidy-review.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml index 069d795..49d7f12 100644 --- a/.github/workflows/clang-tidy-review.yml +++ b/.github/workflows/clang-tidy-review.yml @@ -11,6 +11,16 @@ jobs: - name: Checkout uses: actions/checkout@v4 + - name: Install SDL windowing deps (Linux) + run: | + sudo apt-get update + sudo apt-get install -y \ + build-essential cmake ninja-build pkg-config gcc g++ clang git python3 python3-pip \ + libasound2-dev libjack-jackd2-dev libpulse-dev \ + xorg-dev libx11-dev libxext-dev libxrandr-dev libxcursor-dev libxfixes-dev libxi-dev libxss-dev libxtst-dev \ + libxkbcommon-dev wayland-protocols libwayland-dev \ + libdrm-dev mesa-common-dev mesa-utils + - name: Configure (CMake) run: | cmake -S ${{ github.workspace }} From 00774bca5d4aeb9c3a8862153e1a52abb7207908 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 14:20:27 +0800 Subject: [PATCH 33/58] fix:add parameter --- .github/workflows/clang-tidy-review.yml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml index 49d7f12..93baa6a 100644 --- a/.github/workflows/clang-tidy-review.yml +++ b/.github/workflows/clang-tidy-review.yml @@ -24,11 +24,12 @@ jobs: - name: Configure (CMake) run: | cmake -S ${{ github.workspace }} - -B build - -DCMAKE_CXX_COMPILER=clang++ - -DCMAKE_C_COMPILER=clang - -DCMAKE_BUILD_TYPE=Release - -DCMAKE_EXPORT_COMPILE_COMMANDS=ON + -B build + -G Ninja + -DCMAKE_CXX_COMPILER=clang++ + -DCMAKE_C_COMPILER=clang + -DCMAKE_BUILD_TYPE=Release + -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - name: Run clang-tidy-review uses: ZedThree/clang-tidy-review@v0.21.0 From d8c575cbe852821733d26b1fd190138b3dba4422 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 14:23:16 +0800 Subject: [PATCH 34/58] fix:add parameter --- .github/workflows/clang-tidy-review.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml index 93baa6a..5c267b7 100644 --- a/.github/workflows/clang-tidy-review.yml +++ b/.github/workflows/clang-tidy-review.yml @@ -23,12 +23,12 @@ jobs: - name: Configure (CMake) run: | - cmake -S ${{ github.workspace }} - -B build - -G Ninja - -DCMAKE_CXX_COMPILER=clang++ - -DCMAKE_C_COMPILER=clang - -DCMAKE_BUILD_TYPE=Release + cmake -S ${{ github.workspace }} \ + -B build \ + -G Ninja \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_C_COMPILER=clang \ + -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - name: Run clang-tidy-review From 12597cb799a4a9fcb2ab782e32a3e99a04af21f1 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 14:32:12 +0800 Subject: [PATCH 35/58] fix:add permission --- .github/workflows/clang-tidy-review.yml | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml index 5c267b7..485cd64 100644 --- a/.github/workflows/clang-tidy-review.yml +++ b/.github/workflows/clang-tidy-review.yml @@ -6,6 +6,9 @@ on: jobs: clang-tidy-review: runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write steps: - name: Checkout @@ -21,14 +24,14 @@ jobs: libxkbcommon-dev wayland-protocols libwayland-dev \ libdrm-dev mesa-common-dev mesa-utils - - name: Configure (CMake) - run: | - cmake -S ${{ github.workspace }} \ - -B build \ - -G Ninja \ - -DCMAKE_CXX_COMPILER=clang++ \ - -DCMAKE_C_COMPILER=clang \ - -DCMAKE_BUILD_TYPE=Release \ + - name: Configure CMake + run: > + cmake -S ${{ github.workspace }} + -B build + -G Ninja + -DCMAKE_CXX_COMPILER=clang++ + -DCMAKE_C_COMPILER=clang + -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - name: Run clang-tidy-review From ebc33753b469b775f67fa1920183e24020998eb7 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 14:33:01 +0800 Subject: [PATCH 36/58] fix:remove bom --- .github/workflows/clang-tidy-review.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml index 485cd64..90de7f2 100644 --- a/.github/workflows/clang-tidy-review.yml +++ b/.github/workflows/clang-tidy-review.yml @@ -1,4 +1,4 @@ -name: clang-tidy-review +name: clang-tidy-review on: pull_request: From c06b349945e64b057ffe738ef507dc1359b1073d Mon Sep 17 00:00:00 2001 From: Sophomore Date: Thu, 13 Nov 2025 14:40:56 +0800 Subject: [PATCH 37/58] fix:change command --- .github/workflows/clang-tidy-review.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/clang-tidy-review.yml b/.github/workflows/clang-tidy-review.yml index 90de7f2..9e5e26f 100644 --- a/.github/workflows/clang-tidy-review.yml +++ b/.github/workflows/clang-tidy-review.yml @@ -25,13 +25,13 @@ jobs: libdrm-dev mesa-common-dev mesa-utils - name: Configure CMake - run: > - cmake -S ${{ github.workspace }} - -B build - -G Ninja - -DCMAKE_CXX_COMPILER=clang++ - -DCMAKE_C_COMPILER=clang - -DCMAKE_BUILD_TYPE=Release + run: | + cmake -S ${{ github.workspace }} \ + -B build \ + -G Ninja \ + -DCMAKE_CXX_COMPILER=clang++ \ + -DCMAKE_C_COMPILER=clang \ + -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_EXPORT_COMPILE_COMMANDS=ON - name: Run clang-tidy-review From 68d7f67c9e38d5056e85a9629f4993a700783fb3 Mon Sep 17 00:00:00 2001 From: WSQS <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:15:02 +0800 Subject: [PATCH 38/58] Add workflow Gen pr (#16) * Add workflow * feat: Add permission * feat: change model * feat: add include file * feat: change key word * feat: set workflow in aider * feat: enable verbose * feat: enable verbose * feat: enable verbose * feat: remove bom --------- Co-authored-by: Sophomore --- .github/workflows/gen-pr.yml | 33 +++++++++++++++++++++++++++++++++ gen-pr.config.yml | 12 ++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 .github/workflows/gen-pr.yml create mode 100644 gen-pr.config.yml diff --git a/.github/workflows/gen-pr.yml b/.github/workflows/gen-pr.yml new file mode 100644 index 0000000..468d738 --- /dev/null +++ b/.github/workflows/gen-pr.yml @@ -0,0 +1,33 @@ +name: Generate PR with AI + +on: + workflow_dispatch: + inputs: + issue-number: + description: "Issue number to implement" + required: true + type: number + +jobs: + gen-pr: + runs-on: ubuntu-latest + + permissions: + contents: write + pull-requests: write + issues: read + + env: + GH_TOKEN: ${{ github.token }} + OPENROUTER_API_KEY: ${{ secrets.OPENROUTER_API_KEY }} + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Generate PR from issue using OpenRouter + uses: WillBooster/gen-pr@v4.1.4 + with: + issue-number: ${{ inputs.issue-number }} + aider-extra-args: "--model openrouter/openrouter/polaris-alpha" + verbose: true diff --git a/gen-pr.config.yml b/gen-pr.config.yml new file mode 100644 index 0000000..f6a5694 --- /dev/null +++ b/gen-pr.config.yml @@ -0,0 +1,12 @@ +# gen-pr.config.yml +planning-model: openrouter/openrouter/polaris-alpha +reasoning-effort: high + +repomix-extra-args: "--compress --remove-empty-lines --include '**/*.cpp' --include '**/*.ixx'" + +coding-tool: aider +aider-extra-args: "--model openrouter/openrouter/polaris-alpha" +verbose: true + +# 例如让生成完代码后跑一个测试命令(按你的项目改) +test-command: "cmake -S . -B build" From db03e22787b488683602c632b60c56ff31e9f4b7 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:26:12 +0800 Subject: [PATCH 39/58] feat: let gpu create pipeline --- main.cpp | 2 +- sdl_wrapper/CMakeLists.txt | 1 + sdl_wrapper/sdl_wrapper.gpu.ixx | 6 ++++ sdl_wrapper/sdl_wrapper.pipeline.cpp | 42 ++++++++++++++++++++++++++++ sdl_wrapper/sdl_wrapper.pipeline.ixx | 36 ++++++------------------ 5 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 sdl_wrapper/sdl_wrapper.pipeline.cpp diff --git a/main.cpp b/main.cpp index 8d1114e..2291650 100644 --- a/main.cpp +++ b/main.cpp @@ -22,9 +22,9 @@ struct Vertex class UserApp : public sopho::App { - std::optional pipeline_wrapper; std::shared_ptr gpu_wrapper{std::make_shared()}; sopho::BufferWrapper vertex_buffer{gpu_wrapper->create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices))}; + std::optional pipeline_wrapper{gpu_wrapper->create_pipeline()}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; diff --git a/sdl_wrapper/CMakeLists.txt b/sdl_wrapper/CMakeLists.txt index 06e6353..ad2a370 100644 --- a/sdl_wrapper/CMakeLists.txt +++ b/sdl_wrapper/CMakeLists.txt @@ -15,6 +15,7 @@ target_sources(sdl_wrapper sdl_wrapper.decl.ixx PRIVATE sdl_wrapper.buffer.cpp + sdl_wrapper.pipeline.cpp sdl_callback_implement.cpp ) diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx index 8a411b8..b42dede 100644 --- a/sdl_wrapper/sdl_wrapper.gpu.ixx +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -7,6 +7,7 @@ module; #include "SDL3/SDL_log.h" export module sdl_wrapper:gpu; import :buffer; +import :pipeline; namespace sopho { export class GpuWrapper : public std::enable_shared_from_this @@ -40,5 +41,10 @@ namespace sopho BufferWrapper result(shared_from_this(), buffer); return result; } + + auto create_pipeline() + { + return PipelineWrapper{shared_from_this()}; + } }; } // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp new file mode 100644 index 0000000..41e6863 --- /dev/null +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -0,0 +1,42 @@ +// +// Created by sophomore on 11/13/25. +// +module; +#include "SDL3/SDL_gpu.h" +#include "SDL3/SDL_log.h" +#include +module sdl_wrapper; +import :pipeline; + +namespace sopho +{ + PipelineWrapper::PipelineWrapper(std::shared_ptr p_device) : m_device(p_device) {} + PipelineWrapper::~PipelineWrapper() + { + if (m_graphics_pipeline) + { + SDL_ReleaseGPUGraphicsPipeline(m_device->data(), m_graphics_pipeline); + m_graphics_pipeline = nullptr; + } + } + auto PipelineWrapper::submit() + { + if (modified) + { + modified = false; + auto new_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(m_device->data(), &m_pipeline_info); + if (new_graphics_pipeline) + { + if (m_graphics_pipeline) + { + SDL_ReleaseGPUGraphicsPipeline(m_device->data(), m_graphics_pipeline); + } + m_graphics_pipeline = new_graphics_pipeline; + } + else + { + SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s get error:%s", __FUNCTION__, SDL_GetError()); + } + } + } +} // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index 44f1c31..fed3995 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -2,47 +2,27 @@ // Created by sophomore on 11/11/25. // module; +#include #include "SDL3/SDL_gpu.h" #include "SDL3/SDL_log.h" export module sdl_wrapper:pipeline; - +import :decl; export namespace sopho { - class PipelineWrapper + class PipelineWrapper { SDL_GPUGraphicsPipeline* m_graphics_pipeline{}; - SDL_GPUDevice* m_device{}; + std::shared_ptr m_device{}; SDL_GPUGraphicsPipelineCreateInfo m_pipeline_info{}; bool modified = false; + PipelineWrapper(std::shared_ptr p_device); public: - PipelineWrapper() = default; - ~PipelineWrapper() - { - if (m_graphics_pipeline) - { - SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); - m_graphics_pipeline = nullptr; - } - } + ~PipelineWrapper(); auto data() { return m_graphics_pipeline; } - auto submit() - { - if (modified) - { - modified = false; - auto new_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(m_device, &m_pipeline_info); - if (new_graphics_pipeline) { - if (m_graphics_pipeline) { - SDL_ReleaseGPUGraphicsPipeline(m_device, m_graphics_pipeline); - } - m_graphics_pipeline = new_graphics_pipeline; - } else { - SDL_LogError(SDL_LOG_CATEGORY_GPU, "%s get error:%s",__FUNCTION__, SDL_GetError()); - } - } - } + auto submit(); + friend GpuWrapper; }; } // namespace sopho From 4a3cb8669bd4f4e7af0ae9093d9227dd84e1e213 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 11:26:39 +0000 Subject: [PATCH 40/58] style: format code with ClangFormat This commit fixes the style issues introduced in db03e22 according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- sdl_wrapper/sdl_wrapper.pipeline.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 41e6863..7e7edcf 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -2,9 +2,9 @@ // Created by sophomore on 11/13/25. // module; +#include #include "SDL3/SDL_gpu.h" #include "SDL3/SDL_log.h" -#include module sdl_wrapper; import :pipeline; From 721f0efd4eb72cbb3ad9be30203f690835fef619 Mon Sep 17 00:00:00 2001 From: WSQS <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:31:18 +0800 Subject: [PATCH 41/58] Remove unused headfile Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- sdl_wrapper/sdl_wrapper.pipeline.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 7e7edcf..62e4a66 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -4,7 +4,7 @@ module; #include #include "SDL3/SDL_gpu.h" -#include "SDL3/SDL_log.h" +#include module sdl_wrapper; import :pipeline; From cf8dc7a2f9790b918f27c6d68737b03cd88507e8 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 11:31:28 +0000 Subject: [PATCH 42/58] style: format code with ClangFormat This commit fixes the style issues introduced in 721f0ef according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- sdl_wrapper/sdl_wrapper.pipeline.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 62e4a66..5f981f5 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -4,7 +4,6 @@ module; #include #include "SDL3/SDL_gpu.h" -#include module sdl_wrapper; import :pipeline; From d1faa0a76b38db10ca525721d024034600bde67b Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:28:13 +0800 Subject: [PATCH 43/58] fix: change comment --- gen-pr.config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gen-pr.config.yml b/gen-pr.config.yml index f6a5694..7e0b7dc 100644 --- a/gen-pr.config.yml +++ b/gen-pr.config.yml @@ -8,5 +8,5 @@ coding-tool: aider aider-extra-args: "--model openrouter/openrouter/polaris-alpha" verbose: true -# 例如让生成完代码后跑一个测试命令(按你的项目改) +# Run build tests after code generation test-command: "cmake -S . -B build" From d9851ce5e1178ab6f1d4f06e1cdf0c0fe53ec208 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:38:56 +0800 Subject: [PATCH 44/58] fix: add header file --- main.cpp | 3 ++- sdl_wrapper/sdl_wrapper.buffer.cpp | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 2291650..247edf5 100644 --- a/main.cpp +++ b/main.cpp @@ -9,7 +9,8 @@ #include "misc/cpp/imgui_stdlib.h" #include - +#include "SDL3/SDL_log.h" +#include import sdl_wrapper; // the vertex input layout diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp index 2ecca51..907cfd2 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.cpp +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -4,6 +4,7 @@ module; #include #include "SDL3/SDL_gpu.h" +#include "SDL3/SDL_stdinc.h" module sdl_wrapper; import :buffer; import :gpu; From 415b169245f92c6420a8f036cb06017d32e04c5f Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 11:39:12 +0000 Subject: [PATCH 45/58] style: format code with ClangFormat This commit fixes the style issues introduced in d9851ce according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 247edf5..8034d76 100644 --- a/main.cpp +++ b/main.cpp @@ -9,8 +9,8 @@ #include "misc/cpp/imgui_stdlib.h" #include -#include "SDL3/SDL_log.h" #include +#include "SDL3/SDL_log.h" import sdl_wrapper; // the vertex input layout From f7e16bd6d37681ccba1182703b485d7b27e4ec3e Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:40:50 +0800 Subject: [PATCH 46/58] fix: remove unused file. --- sdl_wrapper/sdl_wrapper.pipeline.ixx | 1 - 1 file changed, 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index fed3995..25064e9 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -4,7 +4,6 @@ module; #include #include "SDL3/SDL_gpu.h" -#include "SDL3/SDL_log.h" export module sdl_wrapper:pipeline; import :decl; export namespace sopho From 4966f3ddbfafabdf92c4e75ea51a2ba83af31032 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:47:04 +0800 Subject: [PATCH 47/58] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`p?= =?UTF-8?q?ipiline`=20(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📝 Add docstrings to `pipiline` Docstrings generation was requested by @WSQS. * https://github.com/WSQS/sdl_test/pull/7#issuecomment-3527425652 The following files were modified: * `main.cpp` * `sdl_wrapper/sdl_wrapper.buffer.cpp` * `sdl_wrapper/sdl_wrapper.pipeline.cpp` * style: format code with ClangFormat This commit fixes the style issues introduced in c7383b5 according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/17 --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- main.cpp | 12 ++++++------ sdl_wrapper/sdl_wrapper.buffer.cpp | 9 ++++----- sdl_wrapper/sdl_wrapper.pipeline.cpp | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 8034d76..4ab014f 100644 --- a/main.cpp +++ b/main.cpp @@ -226,14 +226,14 @@ void main() } /** - * @brief Advance the application by one frame: update UI state, handle vertex edits and live shader recompilation, - * and render. + * @brief Advance the application by one frame: update UI, apply vertex edits and live shader recompilation, render, + * and submit GPU work. * - * Processes ImGui frames, applies interactive vertex edits (uploading vertex data when changed), recompiles and - * replaces the vertex shader and graphics pipeline on edits, acquires a GPU command buffer and swapchain texture, - * records a render pass that draws the triangle and ImGui draw lists, and submits the command buffer. + * Processes ImGui frames, uploads vertex data when edited, recompiles and replaces the vertex shader and graphics + * pipeline on shader edits, records a render pass that draws the triangle and ImGui draw lists, and submits the GPU + * command buffer for presentation. * - * @return SDL_AppResult SDL_APP_CONTINUE to continue the main loop. + * @return `SDL_APP_CONTINUE` to continue the main loop. */ virtual SDL_AppResult iterate() override { diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp index 907cfd2..3249bb7 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.cpp +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -34,12 +34,11 @@ namespace sopho } /** - * @brief Uploads a block of data into the wrapped GPU vertex buffer at the specified offset. + * @brief Uploads a block of data into the wrapped GPU vertex buffer at the specified byte offset. * - * If the internal transfer (staging) buffer is smaller than `p_size`, it will be reallocated - * to accommodate the upload. The function copies `p_size` bytes from `p_data` into the transfer - * buffer and enqueues a GPU copy pass that transfers those bytes into the vertex buffer at - * `p_offset`. + * If the internal staging transfer buffer is smaller than p_size it is reallocated to fit. + * Copies p_size bytes from p_data into the transfer buffer and enqueues a GPU copy pass to transfer them into the + * vertex buffer at p_offset. * * @param p_data Pointer to the source data to upload. * @param p_size Size in bytes of the data to upload. diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 5f981f5..5136955 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -9,7 +9,20 @@ import :pipeline; namespace sopho { + /** + * @brief Initializes the PipelineWrapper with the given GPU device wrapper. + * + * @param p_device Shared pointer to a GpuWrapper representing the target GPU device; the wrapper retains this + * reference for its lifetime. + */ PipelineWrapper::PipelineWrapper(std::shared_ptr p_device) : m_device(p_device) {} + /** + * @brief Releases any GPU graphics pipeline owned by this wrapper. + * + * If a graphics pipeline is currently held, it is released using the associated + * device and the stored pipeline handle is cleared so the wrapper no longer + * references the pipeline. + */ PipelineWrapper::~PipelineWrapper() { if (m_graphics_pipeline) @@ -18,6 +31,14 @@ namespace sopho m_graphics_pipeline = nullptr; } } + /** + * @brief Rebuilds the GPU graphics pipeline when the wrapper is marked modified. + * + * If the wrapper's modified flag is set, this clears the flag, attempts to create a new + * graphics pipeline from the stored pipeline info and device, and on success replaces the + * current pipeline (releasing the previous pipeline first). If creation fails, an error + * is logged. + */ auto PipelineWrapper::submit() { if (modified) From 2859b486b47a9a5c51ec611888e0a6b63cd09139 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Thu, 13 Nov 2025 19:55:49 +0800 Subject: [PATCH 48/58] fix: from optional to value --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 4ab014f..20dac9e 100644 --- a/main.cpp +++ b/main.cpp @@ -25,7 +25,7 @@ class UserApp : public sopho::App { std::shared_ptr gpu_wrapper{std::make_shared()}; sopho::BufferWrapper vertex_buffer{gpu_wrapper->create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices))}; - std::optional pipeline_wrapper{gpu_wrapper->create_pipeline()}; + sopho::PipelineWrapper pipeline_wrapper{gpu_wrapper->create_pipeline()}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; From 09827b9de1914f6286ac87912eb905bec7929452 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Fri, 14 Nov 2025 00:10:56 +0800 Subject: [PATCH 49/58] feat: add compile command --- CMakeLists.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1923319..e1d5ca2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,8 @@ set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) include(FetchContent) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + set(FETCHCONTENT_QUIET OFF CACHE BOOL "" FORCE) set(CMAKE_DOWNLOAD_NO_PROGRESS OFF CACHE BOOL "" FORCE) set(CMAKE_MESSAGE_LOG_LEVEL STATUS CACHE STRING "" FORCE) @@ -95,7 +97,7 @@ target_link_libraries(imgui PUBLIC SDL3::SDL3) add_subdirectory(sdl_wrapper) add_executable(SDL_TEST main.cpp) -target_link_libraries(SDL_TEST PRIVATE shaderc imgui sdl_wrapper) +target_link_libraries(SDL_TEST PRIVATE imgui sdl_wrapper) include(GNUInstallDirs) From b70c22e417c1392f98ef115a599395ffa36d8488 Mon Sep 17 00:00:00 2001 From: Sophomore <17792626+WSQS@users.noreply.github.com> Date: Fri, 14 Nov 2025 00:11:21 +0800 Subject: [PATCH 50/58] feat:implement pipeline_wrapper --- main.cpp | 63 ++++++---------------- sdl_wrapper/CMakeLists.txt | 2 +- sdl_wrapper/sdl_wrapper.gpu.ixx | 37 ++++++++++++- sdl_wrapper/sdl_wrapper.pipeline.cpp | 81 ++++++++++++++++++++++++++-- sdl_wrapper/sdl_wrapper.pipeline.ixx | 22 ++++++-- 5 files changed, 148 insertions(+), 57 deletions(-) diff --git a/main.cpp b/main.cpp index 20dac9e..67e2a29 100644 --- a/main.cpp +++ b/main.cpp @@ -25,7 +25,7 @@ class UserApp : public sopho::App { std::shared_ptr gpu_wrapper{std::make_shared()}; sopho::BufferWrapper vertex_buffer{gpu_wrapper->create_buffer(SDL_GPU_BUFFERUSAGE_VERTEX, sizeof(vertices))}; - sopho::PipelineWrapper pipeline_wrapper{gpu_wrapper->create_pipeline()}; + std::optional pipeline_wrapper{std::nullopt}; SDL_Window* window{}; SDL_GPUGraphicsPipeline* graphicsPipeline{}; @@ -87,8 +87,10 @@ void main() { // create a window window = SDL_CreateWindow("Hello, Triangle!", 960, 540, SDL_WINDOW_RESIZABLE); + gpu_wrapper->set_window(window); SDL_ClaimWindowForGPUDevice(gpu_wrapper->data(), window); + pipeline_wrapper.emplace(gpu_wrapper); options.SetTargetEnvironment(shaderc_target_env_vulkan, 0); auto result = compiler.CompileGlslToSpv(vertex_source, shaderc_glsl_vertex_shader, "test.glsl", options); @@ -148,17 +150,17 @@ void main() // describe the vertex buffers vertexBufferDesctiptions[0].slot = 0; + vertexBufferDesctiptions[0].pitch = sizeof(Vertex); vertexBufferDesctiptions[0].input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX; vertexBufferDesctiptions[0].instance_step_rate = 0; - vertexBufferDesctiptions[0].pitch = sizeof(Vertex); pipelineInfo.vertex_input_state.num_vertex_buffers = 1; pipelineInfo.vertex_input_state.vertex_buffer_descriptions = vertexBufferDesctiptions; // describe the vertex attribute // a_position - vertexAttributes[0].buffer_slot = 0; vertexAttributes[0].location = 0; + vertexAttributes[0].buffer_slot = 0; vertexAttributes[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3; vertexAttributes[0].offset = 0; @@ -173,13 +175,13 @@ void main() // describe the color target colorTargetDescriptions[0] = {}; - colorTargetDescriptions[0].blend_state.enable_blend = true; - colorTargetDescriptions[0].blend_state.color_blend_op = SDL_GPU_BLENDOP_ADD; - colorTargetDescriptions[0].blend_state.alpha_blend_op = SDL_GPU_BLENDOP_ADD; colorTargetDescriptions[0].blend_state.src_color_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA; colorTargetDescriptions[0].blend_state.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; + colorTargetDescriptions[0].blend_state.color_blend_op = SDL_GPU_BLENDOP_ADD; colorTargetDescriptions[0].blend_state.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_SRC_ALPHA; colorTargetDescriptions[0].blend_state.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA; + colorTargetDescriptions[0].blend_state.alpha_blend_op = SDL_GPU_BLENDOP_ADD; + colorTargetDescriptions[0].blend_state.enable_blend = true; colorTargetDescriptions[0].format = SDL_GetGPUSwapchainTextureFormat(gpu_wrapper->data(), window); pipelineInfo.target_info.num_color_targets = 1; @@ -187,9 +189,13 @@ void main() // create the pipeline graphicsPipeline = SDL_CreateGPUGraphicsPipeline(gpu_wrapper->data(), &pipelineInfo); + pipeline_wrapper->set_vertex_shader(vertex_source); + pipeline_wrapper->set_fragment_shader(fragment_source); + pipeline_wrapper->submit(); vertex_buffer.upload(&vertices, sizeof(vertices), 0); + // Setup Dear ImGui context IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -262,39 +268,7 @@ void main() std::min(ImGui::GetTextLineHeight() * (line_count + 3), ImGui::GetContentRegionAvail().y)); if (ImGui::InputTextMultiline("code editor", &vertex_source, size, ImGuiInputTextFlags_AllowTabInput)) { - auto result = - compiler.CompileGlslToSpv(vertex_source, shaderc_glsl_vertex_shader, "test.glsl", options); - - if (result.GetCompilationStatus() != shaderc_compilation_status_success) - { - std::cerr << "[shaderc] compile error in " << "test.glsl" << ":\n" - << result.GetErrorMessage() << std::endl; - } - else - { - // load the vertex shader code - std::vector vertexCode{result.cbegin(), result.cend()}; - - // create the vertex shader - SDL_GPUShaderCreateInfo vertexInfo{}; - vertexInfo.code = (Uint8*)vertexCode.data(); - vertexInfo.code_size = vertexCode.size() * 4; - vertexInfo.entrypoint = "main"; - vertexInfo.format = SDL_GPU_SHADERFORMAT_SPIRV; - vertexInfo.stage = SDL_GPU_SHADERSTAGE_VERTEX; - vertexInfo.num_samplers = 0; - vertexInfo.num_storage_buffers = 0; - vertexInfo.num_storage_textures = 0; - vertexInfo.num_uniform_buffers = 0; - - - SDL_ReleaseGPUShader(gpu_wrapper->data(), vertexShader); - vertexShader = SDL_CreateGPUShader(gpu_wrapper->data(), &vertexInfo); - - pipelineInfo.vertex_shader = vertexShader; - SDL_ReleaseGPUGraphicsPipeline(gpu_wrapper->data(), graphicsPipeline); - graphicsPipeline = SDL_CreateGPUGraphicsPipeline(gpu_wrapper->data(), &pipelineInfo); - } + pipeline_wrapper->set_vertex_shader(vertex_source); } ImGui::End(); @@ -303,6 +277,8 @@ void main() ImGui::Render(); ImDrawData* draw_data = ImGui::GetDrawData(); + pipeline_wrapper->submit(); + // acquire the command buffer SDL_GPUCommandBuffer* commandBuffer = SDL_AcquireGPUCommandBuffer(gpu_wrapper->data()); @@ -332,7 +308,7 @@ void main() SDL_GPURenderPass* renderPass = SDL_BeginGPURenderPass(commandBuffer, &colorTargetInfo, 1, NULL); // draw calls go here - SDL_BindGPUGraphicsPipeline(renderPass, graphicsPipeline); + SDL_BindGPUGraphicsPipeline(renderPass, pipeline_wrapper->data()); // bind the vertex buffer SDL_GPUBufferBinding bufferBindings[1]; @@ -386,13 +362,6 @@ void main() ImGui_ImplSDLGPU3_Shutdown(); ImGui::DestroyContext(); - // Release the shader - SDL_ReleaseGPUShader(gpu_wrapper->data(), vertexShader); - SDL_ReleaseGPUShader(gpu_wrapper->data(), fragmentShader); - - // release the pipeline - SDL_ReleaseGPUGraphicsPipeline(gpu_wrapper->data(), graphicsPipeline); - SDL_ReleaseWindowFromGPUDevice(gpu_wrapper->data(), window); // destroy the window diff --git a/sdl_wrapper/CMakeLists.txt b/sdl_wrapper/CMakeLists.txt index ad2a370..5beae00 100644 --- a/sdl_wrapper/CMakeLists.txt +++ b/sdl_wrapper/CMakeLists.txt @@ -19,4 +19,4 @@ target_sources(sdl_wrapper sdl_callback_implement.cpp ) -target_link_libraries(sdl_wrapper PUBLIC SDL3::SDL3) +target_link_libraries(sdl_wrapper PUBLIC SDL3::SDL3 shaderc) diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx index b42dede..942d51a 100644 --- a/sdl_wrapper/sdl_wrapper.gpu.ixx +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -5,6 +5,7 @@ module; #include #include "SDL3/SDL_gpu.h" #include "SDL3/SDL_log.h" +#include "shaderc/shaderc.hpp" export module sdl_wrapper:gpu; import :buffer; import :pipeline; @@ -14,6 +15,8 @@ namespace sopho { SDL_GPUDevice* m_device{}; + SDL_Window* m_window{}; + public: GpuWrapper() : m_device(SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, nullptr)) { @@ -42,9 +45,39 @@ namespace sopho return result; } - auto create_pipeline() + auto create_pipeline() { return PipelineWrapper{shared_from_this()}; } + + auto create_shader(const std::vector& p_shader, SDL_GPUShaderStage p_stage) + { + SDL_GPUShaderCreateInfo vertexInfo{}; + vertexInfo.code = p_shader.data(); + vertexInfo.code_size = p_shader.size() * 4; + vertexInfo.entrypoint = "main"; + vertexInfo.format = SDL_GPU_SHADERFORMAT_SPIRV; + vertexInfo.stage = p_stage; + vertexInfo.num_samplers = 0; + vertexInfo.num_storage_buffers = 0; + vertexInfo.num_storage_textures = 0; + vertexInfo.num_uniform_buffers = 0; + return SDL_CreateGPUShader(m_device, &vertexInfo); + } + + auto release_shader(SDL_GPUShader* shader) + { + if (shader) + { + SDL_ReleaseGPUShader(m_device, shader); + } + } + + auto get_texture_formate() + { + return SDL_GetGPUSwapchainTextureFormat(m_device, m_window); + } + + auto set_window(SDL_Window* p_window) { - return PipelineWrapper{shared_from_this()}; + m_window = p_window; } }; } // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 5136955..9d71237 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -4,6 +4,8 @@ module; #include #include "SDL3/SDL_gpu.h" +#include "SDL3/SDL_log.h" +#include "shaderc/shaderc.hpp" module sdl_wrapper; import :pipeline; @@ -15,7 +17,38 @@ namespace sopho * @param p_device Shared pointer to a GpuWrapper representing the target GPU device; the wrapper retains this * reference for its lifetime. */ - PipelineWrapper::PipelineWrapper(std::shared_ptr p_device) : m_device(p_device) {} + PipelineWrapper::PipelineWrapper(std::shared_ptr p_device) : m_device(p_device) + { + options.SetTargetEnvironment(shaderc_target_env_vulkan, 0); + + m_vertex_buffer_description.emplace_back(0, 28, SDL_GPU_VERTEXINPUTRATE_VERTEX, 0); + m_pipeline_info.vertex_input_state.vertex_buffer_descriptions = m_vertex_buffer_description.data(); + m_pipeline_info.vertex_input_state.num_vertex_buffers = m_vertex_buffer_description.size(); + + m_vertex_attribute.emplace_back(0, 0, SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, 0); + m_vertex_attribute.emplace_back(1, 0, SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, sizeof(float) * 3); + m_pipeline_info.vertex_input_state.vertex_attributes = m_vertex_attribute.data(); + m_pipeline_info.vertex_input_state.num_vertex_attributes = m_vertex_attribute.size(); + + m_pipeline_info.primitive_type = SDL_GPU_PRIMITIVETYPE_LINELIST; + + m_color_target_description.emplace_back(m_device->get_texture_formate(), + SDL_GPUColorTargetBlendState{SDL_GPU_BLENDFACTOR_SRC_ALPHA, + SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, + SDL_GPU_BLENDOP_ADD, + SDL_GPU_BLENDFACTOR_SRC_ALPHA, + SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, + SDL_GPU_BLENDOP_ADD, + {}, + true}); + + m_pipeline_info.target_info.color_target_descriptions = m_color_target_description.data(); + m_pipeline_info.target_info.num_color_targets = m_color_target_description.size(); + + m_modified = true; + + } + /** * @brief Releases any GPU graphics pipeline owned by this wrapper. * @@ -30,6 +63,8 @@ namespace sopho SDL_ReleaseGPUGraphicsPipeline(m_device->data(), m_graphics_pipeline); m_graphics_pipeline = nullptr; } + m_device->release_shader(m_vertex_shader); + m_device->release_shader(m_fragment_shader); } /** * @brief Rebuilds the GPU graphics pipeline when the wrapper is marked modified. @@ -39,11 +74,14 @@ namespace sopho * current pipeline (releasing the previous pipeline first). If creation fails, an error * is logged. */ - auto PipelineWrapper::submit() + void PipelineWrapper::submit() { - if (modified) + if (m_modified) { - modified = false; + m_modified = false; + m_pipeline_info.vertex_input_state.vertex_buffer_descriptions = m_vertex_buffer_description.data(); + m_pipeline_info.vertex_input_state.vertex_attributes = m_vertex_attribute.data(); + m_pipeline_info.target_info.color_target_descriptions = m_color_target_description.data(); auto new_graphics_pipeline = SDL_CreateGPUGraphicsPipeline(m_device->data(), &m_pipeline_info); if (new_graphics_pipeline) { @@ -59,4 +97,39 @@ namespace sopho } } } + + void PipelineWrapper::set_vertex_shader(const std::string& p_source) + { + auto result = compiler.CompileGlslToSpv(p_source, shaderc_glsl_vertex_shader, "vertex.glsl", options); + if (result.GetCompilationStatus() != shaderc_compilation_status_success) + { + SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error in vertex.glsl: %s", + result.GetErrorMessage().data()); + } + else + { + m_device->release_shader(m_vertex_shader); + std::vector vertex_code{result.cbegin(), result.cend()}; + m_vertex_shader = m_device->create_shader(vertex_code, SDL_GPU_SHADERSTAGE_VERTEX); + m_pipeline_info.vertex_shader = m_vertex_shader; + m_modified = true; + } + } + void PipelineWrapper::set_fragment_shader(const std::string& p_source) + { + auto result = compiler.CompileGlslToSpv(p_source, shaderc_glsl_fragment_shader, "fragment.glsl", options); + if (result.GetCompilationStatus() != shaderc_compilation_status_success) + { + SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error fragment.glsl: %s", + result.GetErrorMessage().data()); + } + else + { + m_device->release_shader(m_fragment_shader); + std::vector vertex_code{result.cbegin(), result.cend()}; + m_fragment_shader = m_device->create_shader(vertex_code, SDL_GPU_SHADERSTAGE_FRAGMENT); + m_pipeline_info.fragment_shader = m_fragment_shader; + m_modified = true; + } + } } // namespace sopho diff --git a/sdl_wrapper/sdl_wrapper.pipeline.ixx b/sdl_wrapper/sdl_wrapper.pipeline.ixx index 25064e9..8b58e31 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.ixx +++ b/sdl_wrapper/sdl_wrapper.pipeline.ixx @@ -4,6 +4,7 @@ module; #include #include "SDL3/SDL_gpu.h" +#include "shaderc/shaderc.hpp" export module sdl_wrapper:pipeline; import :decl; export namespace sopho @@ -12,16 +13,31 @@ export namespace sopho { SDL_GPUGraphicsPipeline* m_graphics_pipeline{}; std::shared_ptr m_device{}; + + SDL_GPUShader* m_vertex_shader{}; + SDL_GPUShader* m_fragment_shader{}; + std::vector m_vertex_buffer_description{}; + std::vector m_vertex_attribute{}; + std::vector m_color_target_description{}; SDL_GPUGraphicsPipelineCreateInfo m_pipeline_info{}; - bool modified = false; - PipelineWrapper(std::shared_ptr p_device); + shaderc::Compiler compiler{}; + shaderc::CompileOptions options{}; + + bool m_modified = false; + public: + PipelineWrapper(std::shared_ptr p_device); + PipelineWrapper(const PipelineWrapper&)=default; + PipelineWrapper(PipelineWrapper&&)=default; ~PipelineWrapper(); auto data() { return m_graphics_pipeline; } - auto submit(); + void submit(); + + void set_vertex_shader(const std::string& p_source); + void set_fragment_shader(const std::string& p_source); friend GpuWrapper; }; } // namespace sopho From f157b68d9f0fa43c600589d14740dbdd1eccd1dd Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 16:11:40 +0000 Subject: [PATCH 51/58] style: format code with ClangFormat This commit fixes the style issues introduced in b70c22e according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/7 --- sdl_wrapper/sdl_wrapper.pipeline.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 9d71237..65f0c65 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -46,7 +46,6 @@ namespace sopho m_pipeline_info.target_info.num_color_targets = m_color_target_description.size(); m_modified = true; - } /** From 9f029eeb2ab4ead30b7f97514d516630ca07e350 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Fri, 14 Nov 2025 08:41:40 +0800 Subject: [PATCH 52/58] fix: shader length error --- sdl_wrapper/sdl_wrapper.gpu.ixx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx index 942d51a..ab0c566 100644 --- a/sdl_wrapper/sdl_wrapper.gpu.ixx +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -51,7 +51,7 @@ namespace sopho { SDL_GPUShaderCreateInfo vertexInfo{}; vertexInfo.code = p_shader.data(); - vertexInfo.code_size = p_shader.size() * 4; + vertexInfo.code_size = p_shader.size(); vertexInfo.entrypoint = "main"; vertexInfo.format = SDL_GPU_SHADERFORMAT_SPIRV; vertexInfo.stage = p_stage; From e6d2b2e87972b422bd62d37606c236206c70454c Mon Sep 17 00:00:00 2001 From: Sophomore Date: Fri, 14 Nov 2025 08:43:26 +0800 Subject: [PATCH 53/58] fix: rename variable --- sdl_wrapper/sdl_wrapper.pipeline.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 65f0c65..e5014f4 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -108,8 +108,8 @@ namespace sopho else { m_device->release_shader(m_vertex_shader); - std::vector vertex_code{result.cbegin(), result.cend()}; - m_vertex_shader = m_device->create_shader(vertex_code, SDL_GPU_SHADERSTAGE_VERTEX); + std::vector code{result.cbegin(), result.cend()}; + m_vertex_shader = m_device->create_shader(code, SDL_GPU_SHADERSTAGE_VERTEX); m_pipeline_info.vertex_shader = m_vertex_shader; m_modified = true; } @@ -125,8 +125,8 @@ namespace sopho else { m_device->release_shader(m_fragment_shader); - std::vector vertex_code{result.cbegin(), result.cend()}; - m_fragment_shader = m_device->create_shader(vertex_code, SDL_GPU_SHADERSTAGE_FRAGMENT); + std::vector code{result.cbegin(), result.cend()}; + m_fragment_shader = m_device->create_shader(code, SDL_GPU_SHADERSTAGE_FRAGMENT); m_pipeline_info.fragment_shader = m_fragment_shader; m_modified = true; } From 1aac3039fe0698722e7e6e6f310dd69666c46d11 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Fri, 14 Nov 2025 08:48:35 +0800 Subject: [PATCH 54/58] fix: rename function --- sdl_wrapper/sdl_wrapper.gpu.ixx | 2 +- sdl_wrapper/sdl_wrapper.pipeline.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sdl_wrapper/sdl_wrapper.gpu.ixx b/sdl_wrapper/sdl_wrapper.gpu.ixx index ab0c566..c6c1694 100644 --- a/sdl_wrapper/sdl_wrapper.gpu.ixx +++ b/sdl_wrapper/sdl_wrapper.gpu.ixx @@ -70,7 +70,7 @@ namespace sopho } } - auto get_texture_formate() + auto get_texture_format() { return SDL_GetGPUSwapchainTextureFormat(m_device, m_window); } diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index e5014f4..154c72b 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -32,7 +32,7 @@ namespace sopho m_pipeline_info.primitive_type = SDL_GPU_PRIMITIVETYPE_LINELIST; - m_color_target_description.emplace_back(m_device->get_texture_formate(), + m_color_target_description.emplace_back(m_device->get_texture_format(), SDL_GPUColorTargetBlendState{SDL_GPU_BLENDFACTOR_SRC_ALPHA, SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA, SDL_GPU_BLENDOP_ADD, From 9a9335a99a98253c2502ffb241cb87d81fc7be52 Mon Sep 17 00:00:00 2001 From: Sophomore Date: Fri, 14 Nov 2025 09:00:31 +0800 Subject: [PATCH 55/58] fix: change shader code --- sdl_wrapper/sdl_wrapper.pipeline.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 154c72b..159a48f 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -108,7 +108,9 @@ namespace sopho else { m_device->release_shader(m_vertex_shader); - std::vector code{result.cbegin(), result.cend()}; + auto code_size = static_cast(result.cend() - result.cbegin()) * sizeof(uint32_t); + auto ptr = reinterpret_cast(result.cbegin()); + std::vector code{ptr, ptr + code_size}; m_vertex_shader = m_device->create_shader(code, SDL_GPU_SHADERSTAGE_VERTEX); m_pipeline_info.vertex_shader = m_vertex_shader; m_modified = true; @@ -125,7 +127,9 @@ namespace sopho else { m_device->release_shader(m_fragment_shader); - std::vector code{result.cbegin(), result.cend()}; + auto code_size = static_cast(result.cend() - result.cbegin()) * sizeof(uint32_t); + auto ptr = reinterpret_cast(result.cbegin()); + std::vector code{ptr, ptr + code_size}; m_fragment_shader = m_device->create_shader(code, SDL_GPU_SHADERSTAGE_FRAGMENT); m_pipeline_info.fragment_shader = m_fragment_shader; m_modified = true; From 7a9e1f7dd642f3e3fa6f090bb7e4fea1b52f8f6f Mon Sep 17 00:00:00 2001 From: Sophomore Date: Fri, 14 Nov 2025 09:15:18 +0800 Subject: [PATCH 56/58] fix: avoid resource leak --- main.cpp | 62 ++------------------------------------------------------ 1 file changed, 2 insertions(+), 60 deletions(-) diff --git a/main.cpp b/main.cpp index 67e2a29..38e1bbc 100644 --- a/main.cpp +++ b/main.cpp @@ -28,7 +28,7 @@ class UserApp : public sopho::App std::optional pipeline_wrapper{std::nullopt}; SDL_Window* window{}; - SDL_GPUGraphicsPipeline* graphicsPipeline{}; + // SDL_GPUGraphicsPipeline* graphicsPipeline{}; // a list of vertices std::array vertices{ @@ -64,8 +64,6 @@ void main() shaderc::CompileOptions options{}; SDL_GPUGraphicsPipelineCreateInfo pipelineInfo{}; - SDL_GPUShader* vertexShader{}; - SDL_GPUShader* fragmentShader{}; SDL_GPUColorTargetDescription colorTargetDescriptions[1]{}; SDL_GPUVertexAttribute vertexAttributes[2]{}; @@ -92,62 +90,6 @@ void main() SDL_ClaimWindowForGPUDevice(gpu_wrapper->data(), window); pipeline_wrapper.emplace(gpu_wrapper); - options.SetTargetEnvironment(shaderc_target_env_vulkan, 0); - auto result = compiler.CompileGlslToSpv(vertex_source, shaderc_glsl_vertex_shader, "test.glsl", options); - - if (result.GetCompilationStatus() != shaderc_compilation_status_success) - { - SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error in test.glsl: %s", - result.GetErrorMessage().data()); - } - - // load the vertex shader code - std::vector vertexCode{result.cbegin(), result.cend()}; - - // create the vertex shader - SDL_GPUShaderCreateInfo vertexInfo{}; - vertexInfo.code = (Uint8*)vertexCode.data(); - vertexInfo.code_size = vertexCode.size() * 4; - vertexInfo.entrypoint = "main"; - vertexInfo.format = SDL_GPU_SHADERFORMAT_SPIRV; - vertexInfo.stage = SDL_GPU_SHADERSTAGE_VERTEX; - vertexInfo.num_samplers = 0; - vertexInfo.num_storage_buffers = 0; - vertexInfo.num_storage_textures = 0; - vertexInfo.num_uniform_buffers = 0; - - vertexShader = SDL_CreateGPUShader(gpu_wrapper->data(), &vertexInfo); - - result = compiler.CompileGlslToSpv(fragment_source, shaderc_glsl_fragment_shader, "test.frag", options); - - if (result.GetCompilationStatus() != shaderc_compilation_status_success) - { - SDL_LogError(SDL_LOG_CATEGORY_RENDER, "[shaderc] compile error in test.frag: %s", - result.GetErrorMessage().data()); - } - - // load the fragment shader code - std::vector fragmentCode{result.cbegin(), result.cend()}; - - // create the fragment shader - SDL_GPUShaderCreateInfo fragmentInfo{}; - fragmentInfo.code = (Uint8*)fragmentCode.data(); - fragmentInfo.code_size = fragmentCode.size() * 4; - fragmentInfo.entrypoint = "main"; - fragmentInfo.format = SDL_GPU_SHADERFORMAT_SPIRV; - fragmentInfo.stage = SDL_GPU_SHADERSTAGE_FRAGMENT; - fragmentInfo.num_samplers = 0; - fragmentInfo.num_storage_buffers = 0; - fragmentInfo.num_storage_textures = 0; - fragmentInfo.num_uniform_buffers = 0; - - fragmentShader = SDL_CreateGPUShader(gpu_wrapper->data(), &fragmentInfo); - - // create the graphics pipeline - pipelineInfo.vertex_shader = vertexShader; - pipelineInfo.fragment_shader = fragmentShader; - pipelineInfo.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; - // describe the vertex buffers vertexBufferDesctiptions[0].slot = 0; vertexBufferDesctiptions[0].pitch = sizeof(Vertex); @@ -188,7 +130,7 @@ void main() pipelineInfo.target_info.color_target_descriptions = colorTargetDescriptions; // create the pipeline - graphicsPipeline = SDL_CreateGPUGraphicsPipeline(gpu_wrapper->data(), &pipelineInfo); + // graphicsPipeline = SDL_CreateGPUGraphicsPipeline(gpu_wrapper->data(), &pipelineInfo); pipeline_wrapper->set_vertex_shader(vertex_source); pipeline_wrapper->set_fragment_shader(fragment_source); pipeline_wrapper->submit(); From 484d9d4ac9cb35e1a1140278b6b6c5bdcd6c945a Mon Sep 17 00:00:00 2001 From: Sophomore Date: Fri, 14 Nov 2025 09:21:02 +0800 Subject: [PATCH 57/58] fix: change primitive type --- sdl_wrapper/sdl_wrapper.pipeline.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 159a48f..5a4e38c 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -30,7 +30,7 @@ namespace sopho m_pipeline_info.vertex_input_state.vertex_attributes = m_vertex_attribute.data(); m_pipeline_info.vertex_input_state.num_vertex_attributes = m_vertex_attribute.size(); - m_pipeline_info.primitive_type = SDL_GPU_PRIMITIVETYPE_LINELIST; + m_pipeline_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; m_color_target_description.emplace_back(m_device->get_texture_format(), SDL_GPUColorTargetBlendState{SDL_GPU_BLENDFACTOR_SRC_ALPHA, From f34ca767abe4f6a441df3805c5486fbf94542dc9 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Fri, 14 Nov 2025 09:30:51 +0800 Subject: [PATCH 58/58] =?UTF-8?q?=F0=9F=93=9D=20Add=20docstrings=20to=20`p?= =?UTF-8?q?ipiline`=20(#19)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 📝 Add docstrings to `pipiline` Docstrings generation was requested by @WSQS. * https://github.com/WSQS/sdl_test/pull/7#issuecomment-3519726972 The following files were modified: * `main.cpp` * `sdl_wrapper/sdl_wrapper.buffer.cpp` * `sdl_wrapper/sdl_wrapper.pipeline.cpp` * style: format code with ClangFormat This commit fixes the style issues introduced in 66165a8 according to the output from ClangFormat. Details: https://github.com/WSQS/sdl_test/pull/19 --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: deepsource-autofix[bot] <62050782+deepsource-autofix[bot]@users.noreply.github.com> --- main.cpp | 19 +++++++++--------- sdl_wrapper/sdl_wrapper.buffer.cpp | 6 +++--- sdl_wrapper/sdl_wrapper.pipeline.cpp | 30 +++++++++++++++++++++++++--- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/main.cpp b/main.cpp index 38e1bbc..b6d51ad 100644 --- a/main.cpp +++ b/main.cpp @@ -70,13 +70,12 @@ void main() SDL_GPUVertexBufferDescription vertexBufferDesctiptions[1]{}; /** - * @brief Initialize the application by creating the window, compiling shaders, creating the GPU pipeline and - * resources, and initializing ImGui. + * @brief Initialize the application: create the window, configure GPU pipeline and resources, upload initial vertex + * data, and initialize ImGui. * - * Performs window creation and GPU device claim, compiles vertex and fragment GLSL to SPIR-V and creates GPU shader - * objects, configures and creates the graphics pipeline (vertex input, attributes, and color target/blend state), - * uploads initial vertex data to the vertex buffer, and initializes Dear ImGui (context, style, scaling, and - * SDL3/SDLGPU backends). + * Performs window creation and GPU device claim, configures vertex input and color target state, sets vertex and + * fragment shaders on the pipeline wrapper and submits pipeline creation, uploads initial vertex data to the vertex + * buffer, and initializes Dear ImGui (context, style scaling, and SDL3/SDLGPU backends). * * @return SDL_AppResult `SDL_APP_CONTINUE` to enter the main loop, `SDL_APP_SUCCESS` to request immediate * termination. @@ -290,11 +289,11 @@ void main() } /** - * @brief Shut down the application and release GPU and UI resources. + * @brief Clean up UI and GPU resources and close the application window. * - * Shuts down the ImGui SDL3 and SDLGPU backends, destroys the ImGui context, - * releases the vertex and fragment GPU shaders and the graphics pipeline, and - * destroys the SDL window. + * Shuts down ImGui SDL3 and SDLGPU backends, destroys the ImGui context, + * releases the application's association with the GPU device for the window, + * and destroys the SDL window. * * @param result Application exit result code provided by the SDL app framework. */ diff --git a/sdl_wrapper/sdl_wrapper.buffer.cpp b/sdl_wrapper/sdl_wrapper.buffer.cpp index 3249bb7..38e3cb1 100644 --- a/sdl_wrapper/sdl_wrapper.buffer.cpp +++ b/sdl_wrapper/sdl_wrapper.buffer.cpp @@ -36,9 +36,9 @@ namespace sopho /** * @brief Uploads a block of data into the wrapped GPU vertex buffer at the specified byte offset. * - * If the internal staging transfer buffer is smaller than p_size it is reallocated to fit. - * Copies p_size bytes from p_data into the transfer buffer and enqueues a GPU copy pass to transfer them into the - * vertex buffer at p_offset. + * Reallocates the internal staging (transfer) buffer if its capacity is less than the requested size, + * copies p_size bytes from p_data into the staging buffer, and enqueues a GPU copy pass that writes the data + * into the vertex buffer at p_offset. The GPU command buffer is submitted immediately. * * @param p_data Pointer to the source data to upload. * @param p_size Size in bytes of the data to upload. diff --git a/sdl_wrapper/sdl_wrapper.pipeline.cpp b/sdl_wrapper/sdl_wrapper.pipeline.cpp index 5a4e38c..3978a43 100644 --- a/sdl_wrapper/sdl_wrapper.pipeline.cpp +++ b/sdl_wrapper/sdl_wrapper.pipeline.cpp @@ -12,10 +12,13 @@ import :pipeline; namespace sopho { /** - * @brief Initializes the PipelineWrapper with the given GPU device wrapper. + * @brief Constructs a PipelineWrapper and configures default pipeline state for the provided GPU device. * - * @param p_device Shared pointer to a GpuWrapper representing the target GPU device; the wrapper retains this - * reference for its lifetime. + * Stores the provided GPU device wrapper for the wrapper's lifetime, configures the shaderc target environment, + * and initializes default vertex input state, primitive type, and color target description used when creating + * graphics pipelines. + * + * @param p_device Shared pointer to the GpuWrapper used to create and release shaders and graphics pipelines. */ PipelineWrapper::PipelineWrapper(std::shared_ptr p_device) : m_device(p_device) { @@ -97,6 +100,17 @@ namespace sopho } } + /** + * @brief Compile and install a new vertex shader from GLSL source. + * + * Compiles the provided GLSL vertex shader source to SPIR‑V, replaces any previously installed + * vertex shader on the device with the newly created shader, updates the pipeline's vertex + * shader reference, and marks the pipeline wrapper as modified so the pipeline will be rebuilt. + * + * @param p_source GLSL source code for the vertex shader. + * + * On compilation failure, a compilation error is logged and the previously installed shader is left unchanged. + */ void PipelineWrapper::set_vertex_shader(const std::string& p_source) { auto result = compiler.CompileGlslToSpv(p_source, shaderc_glsl_vertex_shader, "vertex.glsl", options); @@ -116,6 +130,16 @@ namespace sopho m_modified = true; } } + /** + * @brief Compile and install a fragment shader from GLSL source. + * + * Compiles the provided GLSL fragment shader source to SPIR‑V; on compilation failure logs the error. + * On success, releases the previously installed fragment shader (if any), uploads the new SPIR‑V bytecode + * to the device as a fragment-stage shader, updates the internal pipeline description to reference it, + * and marks the wrapper as modified so the graphics pipeline will be rebuilt. + * + * @param p_source GLSL source code for the fragment shader. + */ void PipelineWrapper::set_fragment_shader(const std::string& p_source) { auto result = compiler.CompileGlslToSpv(p_source, shaderc_glsl_fragment_shader, "fragment.glsl", options);