refactor sdl3gpu_shader: extreu createPostfxVertexShader i createPostfxLikePipeline

This commit is contained in:
2026-05-17 21:34:25 +02:00
parent 058f9d7630
commit 0d876b902d
2 changed files with 52 additions and 69 deletions
@@ -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) auto SDL3GPUShader::createPostfxVertexShader() -> SDL_GPUShader* {
const SDL_GPUTextureFormat SWAPCHAIN_FMT = SDL_GetGPUSwapchainTextureFormat(device_, window_);
// ---- PostFX pipeline (scene/scaled → swapchain) ----
#ifdef __APPLE__ #ifdef __APPLE__
SDL_GPUShader* vert = createShaderMSL(device_, Rendering::Msl::kPostfxVert, "postfx_vs", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); return 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);
#else #else
SDL_GPUShader* vert = createShaderSPIRV(device_, kpostfx_vert_spv, kpostfx_vert_spv_size, "main", SDL_GPU_SHADERSTAGE_VERTEX, 0, 0); return 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);
#endif #endif
}
if ((vert == nullptr) || (frag == nullptr)) { // ---------------------------------------------------------------------------
SDL_Log("SDL3GPUShader: failed to compile PostFX shaders"); // createPostfxLikePipeline — empaqueta vert(postfx) + frag dado + target en un pipeline.
if (vert != nullptr) { SDL_ReleaseGPUShader(device_, vert); } // Pren ownership de `frag` (el libera abans de retornar).
if (frag != nullptr) { SDL_ReleaseGPUShader(device_, frag); } // ---------------------------------------------------------------------------
return false; 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 = {}; SDL_GPUColorTargetBlendState no_blend = {};
@@ -188,30 +193,44 @@ namespace Rendering {
no_blend.enable_color_write_mask = false; no_blend.enable_color_write_mask = false;
SDL_GPUColorTargetDescription color_target = {}; SDL_GPUColorTargetDescription color_target = {};
color_target.format = SWAPCHAIN_FMT; color_target.format = format;
color_target.blend_state = no_blend; color_target.blend_state = no_blend;
SDL_GPUVertexInputState no_input = {}; SDL_GPUVertexInputState no_input = {};
SDL_GPUGraphicsPipelineCreateInfo pipe_info = {}; SDL_GPUGraphicsPipelineCreateInfo info = {};
pipe_info.vertex_shader = vert; info.vertex_shader = vert;
pipe_info.fragment_shader = frag; info.fragment_shader = frag;
pipe_info.vertex_input_state = no_input; info.vertex_input_state = no_input;
pipe_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST; info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
pipe_info.target_info.num_color_targets = 1; info.target_info.num_color_targets = 1;
pipe_info.target_info.color_target_descriptions = &color_target; 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_, vert);
SDL_ReleaseGPUShader(device_, frag); SDL_ReleaseGPUShader(device_, frag);
if (pipeline_ == nullptr) { if (pipeline == nullptr) {
SDL_Log("SDL3GPUShader: PostFX pipeline creation failed: %s", SDL_GetError()); SDL_Log("SDL3GPUShader: %s pipeline creation failed: %s", debug_name, SDL_GetError());
return false; }
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 { auto SDL3GPUShader::createCrtPiPipeline() -> bool {
const SDL_GPUTextureFormat SWAPCHAIN_FMT = SDL_GetGPUSwapchainTextureFormat(device_, window_); const SDL_GPUTextureFormat SWAPCHAIN_FMT = SDL_GetGPUSwapchainTextureFormat(device_, window_);
#ifdef __APPLE__ #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); SDL_GPUShader* frag = createShaderMSL(device_, Rendering::Msl::kCrtpiFrag, "crtpi_fs", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1);
#else #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); SDL_GPUShader* frag = createShaderSPIRV(device_, kcrtpi_frag_spv, kcrtpi_frag_spv_size, "main", SDL_GPU_SHADERSTAGE_FRAGMENT, 1, 1);
#endif #endif
crtpi_pipeline_ = createPostfxLikePipeline(frag, SWAPCHAIN_FMT, "CrtPi");
if ((vert == nullptr) || (frag == nullptr)) { return crtpi_pipeline_ != 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;
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -125,6 +125,8 @@ namespace Rendering {
auto createPipeline() -> bool; auto createPipeline() -> bool;
auto createCrtPiPipeline() -> bool; // Pipeline dedicado para el shader CrtPi 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_ auto reinitTexturesAndBuffer() -> bool; // Recrea scene_texture_ y upload_buffer_
// Devuelve el mejor present mode disponible: IMMEDIATE > MAILBOX > VSYNC // Devuelve el mejor present mode disponible: IMMEDIATE > MAILBOX > VSYNC
[[nodiscard]] auto bestPresentMode(bool vsync) const -> SDL_GPUPresentMode; [[nodiscard]] auto bestPresentMode(bool vsync) const -> SDL_GPUPresentMode;