From 0d876b902db13c62d4741ab13ece1a45258d8d60 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 17 May 2026 21:34:25 +0200 Subject: [PATCH] refactor sdl3gpu_shader: extreu createPostfxVertexShader i createPostfxLikePipeline --- .../core/rendering/sdl3gpu/sdl3gpu_shader.cpp | 115 ++++++++---------- .../core/rendering/sdl3gpu/sdl3gpu_shader.hpp | 6 +- 2 files changed, 52 insertions(+), 69 deletions(-) diff --git a/source/core/rendering/sdl3gpu/sdl3gpu_shader.cpp b/source/core/rendering/sdl3gpu/sdl3gpu_shader.cpp index a67de39..f247496 100644 --- a/source/core/rendering/sdl3gpu/sdl3gpu_shader.cpp +++ b/source/core/rendering/sdl3gpu/sdl3gpu_shader.cpp @@ -162,25 +162,30 @@ namespace Rendering { } // --------------------------------------------------------------------------- - // createPipeline + // createPostfxVertexShader — fullscreen-triangle vertex compartit per tots els pipelines // --------------------------------------------------------------------------- - auto SDL3GPUShader::createPipeline() -> bool { // NOLINT(readability-function-cognitive-complexity) - const SDL_GPUTextureFormat SWAPCHAIN_FMT = SDL_GetGPUSwapchainTextureFormat(device_, window_); - - // ---- PostFX pipeline (scene/scaled → swapchain) ---- + auto SDL3GPUShader::createPostfxVertexShader() -> SDL_GPUShader* { #ifdef __APPLE__ - SDL_GPUShader* vert = createShaderMSL(device_, Rendering::Msl::kPostfxVert, "postfx_vs", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); - SDL_GPUShader* frag = createShaderMSL(device_, Rendering::Msl::kPostfxFrag, "postfx_fs", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1); + return createShaderMSL(device_, Rendering::Msl::kPostfxVert, "postfx_vs", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); #else - SDL_GPUShader* vert = createShaderSPIRV(device_, kpostfx_vert_spv, kpostfx_vert_spv_size, "main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); - SDL_GPUShader* frag = createShaderSPIRV(device_, kpostfx_frag_spv, kpostfx_frag_spv_size, "main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1); + return createShaderSPIRV(device_, kpostfx_vert_spv, kpostfx_vert_spv_size, "main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); #endif + } - if ((vert == nullptr) || (frag == nullptr)) { - SDL_Log("SDL3GPUShader: failed to compile PostFX shaders"); - if (vert != nullptr) { SDL_ReleaseGPUShader(device_, vert); } - if (frag != nullptr) { SDL_ReleaseGPUShader(device_, frag); } - return false; + // --------------------------------------------------------------------------- + // createPostfxLikePipeline — empaqueta vert(postfx) + frag dado + target en un pipeline. + // Pren ownership de `frag` (el libera abans de retornar). + // --------------------------------------------------------------------------- + auto SDL3GPUShader::createPostfxLikePipeline(SDL_GPUShader* frag, SDL_GPUTextureFormat format, const char* debug_name) -> SDL_GPUGraphicsPipeline* { + if (frag == nullptr) { + SDL_Log("SDL3GPUShader: %s frag shader is null", debug_name); + return nullptr; + } + SDL_GPUShader* vert = createPostfxVertexShader(); + if (vert == nullptr) { + SDL_Log("SDL3GPUShader: %s vert shader creation failed", debug_name); + SDL_ReleaseGPUShader(device_, frag); + return nullptr; } SDL_GPUColorTargetBlendState no_blend = {}; @@ -188,30 +193,44 @@ namespace Rendering { no_blend.enable_color_write_mask = false; SDL_GPUColorTargetDescription color_target = {}; - color_target.format = SWAPCHAIN_FMT; + color_target.format = format; color_target.blend_state = no_blend; SDL_GPUVertexInputState no_input = {}; - SDL_GPUGraphicsPipelineCreateInfo pipe_info = {}; - pipe_info.vertex_shader = vert; - pipe_info.fragment_shader = frag; - pipe_info.vertex_input_state = no_input; - pipe_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; - pipe_info.target_info.num_color_targets = 1; - pipe_info.target_info.color_target_descriptions = &color_target; + SDL_GPUGraphicsPipelineCreateInfo info = {}; + info.vertex_shader = vert; + info.fragment_shader = frag; + info.vertex_input_state = no_input; + info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; + info.target_info.num_color_targets = 1; + info.target_info.color_target_descriptions = &color_target; - pipeline_ = SDL_CreateGPUGraphicsPipeline(device_, &pipe_info); + SDL_GPUGraphicsPipeline* pipeline = SDL_CreateGPUGraphicsPipeline(device_, &info); SDL_ReleaseGPUShader(device_, vert); SDL_ReleaseGPUShader(device_, frag); - if (pipeline_ == nullptr) { - SDL_Log("SDL3GPUShader: PostFX pipeline creation failed: %s", SDL_GetError()); - return false; + if (pipeline == nullptr) { + SDL_Log("SDL3GPUShader: %s pipeline creation failed: %s", debug_name, SDL_GetError()); } + return pipeline; + } - return true; + // --------------------------------------------------------------------------- + // createPipeline — pipeline únic PostFX → swapchain + // --------------------------------------------------------------------------- + auto SDL3GPUShader::createPipeline() -> bool { + const SDL_GPUTextureFormat SWAPCHAIN_FMT = SDL_GetGPUSwapchainTextureFormat(device_, window_); + +#ifdef __APPLE__ + SDL_GPUShader* postfx_frag = createShaderMSL(device_, Rendering::Msl::kPostfxFrag, "postfx_fs", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1); +#else + SDL_GPUShader* postfx_frag = createShaderSPIRV(device_, kpostfx_frag_spv, kpostfx_frag_spv_size, "main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1); +#endif + + pipeline_ = createPostfxLikePipeline(postfx_frag, SWAPCHAIN_FMT, "PostFX"); + return pipeline_ != nullptr; } // --------------------------------------------------------------------------- @@ -222,51 +241,13 @@ namespace Rendering { // --------------------------------------------------------------------------- auto SDL3GPUShader::createCrtPiPipeline() -> bool { const SDL_GPUTextureFormat SWAPCHAIN_FMT = SDL_GetGPUSwapchainTextureFormat(device_, window_); - #ifdef __APPLE__ - SDL_GPUShader* vert = createShaderMSL(device_, Rendering::Msl::kPostfxVert, "postfx_vs", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); SDL_GPUShader* frag = createShaderMSL(device_, Rendering::Msl::kCrtpiFrag, "crtpi_fs", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1); #else - SDL_GPUShader* vert = createShaderSPIRV(device_, kpostfx_vert_spv, kpostfx_vert_spv_size, "main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); SDL_GPUShader* frag = createShaderSPIRV(device_, kcrtpi_frag_spv, kcrtpi_frag_spv_size, "main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1); #endif - - if ((vert == nullptr) || (frag == nullptr)) { - SDL_Log("SDL3GPUShader: failed to compile CrtPi shaders"); - if (vert != nullptr) { SDL_ReleaseGPUShader(device_, vert); } - if (frag != nullptr) { SDL_ReleaseGPUShader(device_, frag); } - return false; - } - - SDL_GPUColorTargetBlendState no_blend = {}; - no_blend.enable_blend = false; - no_blend.enable_color_write_mask = false; - - SDL_GPUColorTargetDescription color_target = {}; - color_target.format = SWAPCHAIN_FMT; - color_target.blend_state = no_blend; - - SDL_GPUVertexInputState no_input = {}; - - SDL_GPUGraphicsPipelineCreateInfo pipe_info = {}; - pipe_info.vertex_shader = vert; - pipe_info.fragment_shader = frag; - pipe_info.vertex_input_state = no_input; - pipe_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; - pipe_info.target_info.num_color_targets = 1; - pipe_info.target_info.color_target_descriptions = &color_target; - - crtpi_pipeline_ = SDL_CreateGPUGraphicsPipeline(device_, &pipe_info); - - SDL_ReleaseGPUShader(device_, vert); - SDL_ReleaseGPUShader(device_, frag); - - if (crtpi_pipeline_ == nullptr) { - SDL_Log("SDL3GPUShader: CrtPi pipeline creation failed: %s", SDL_GetError()); - return false; - } - - return true; + crtpi_pipeline_ = createPostfxLikePipeline(frag, SWAPCHAIN_FMT, "CrtPi"); + return crtpi_pipeline_ != nullptr; } // --------------------------------------------------------------------------- diff --git a/source/core/rendering/sdl3gpu/sdl3gpu_shader.hpp b/source/core/rendering/sdl3gpu/sdl3gpu_shader.hpp index d1fe704..41b539e 100644 --- a/source/core/rendering/sdl3gpu/sdl3gpu_shader.hpp +++ b/source/core/rendering/sdl3gpu/sdl3gpu_shader.hpp @@ -124,8 +124,10 @@ namespace Rendering { Uint32 num_uniform_buffers) -> SDL_GPUShader*; auto createPipeline() -> bool; - auto createCrtPiPipeline() -> bool; // Pipeline dedicado para el shader CrtPi - auto reinitTexturesAndBuffer() -> bool; // Recrea scene_texture_ y upload_buffer_ + auto createCrtPiPipeline() -> bool; // Pipeline dedicado para el shader CrtPi + auto createPostfxVertexShader() -> SDL_GPUShader*; // Vertex shader fullscreen-triangle compartit (MSL/SPIRV) + auto createPostfxLikePipeline(SDL_GPUShader* frag, SDL_GPUTextureFormat format, const char* debug_name) -> SDL_GPUGraphicsPipeline*; // Empaqueta vert + frag + target en un pipeline + auto reinitTexturesAndBuffer() -> bool; // Recrea scene_texture_ y upload_buffer_ // Devuelve el mejor present mode disponible: IMMEDIATE > MAILBOX > VSYNC [[nodiscard]] auto bestPresentMode(bool vsync) const -> SDL_GPUPresentMode;