From bc1beb58ba259263eb98d43d2aa742307764591c Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 28 Feb 2026 09:25:35 +0100 Subject: fix(tools/shadertoy): sync templates and script to current codebase conventions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - template.h/cc: new Effect constructor/render signatures, RAII wrappers, HEADLESS_RETURN_IF_NULL, #pragma once - template.wgsl: sequence_uniforms + render/fullscreen_uv_vs includes, UniformsSequenceParams at binding 2, VertexOutput in fs_main - convert_shadertoy.py: paths src/effects/ + src/shaders/, new Effect pattern (create_post_process_pipeline, pp_update_bind_group), correct field names (beat_time/beat_phase), updated next-steps instructions - README.md: streamlined to quick-ref; accurate GLSL→WGSL table and uniforms Co-Authored-By: Claude Sonnet 4.6 --- tools/shadertoy/template.cc | 144 ++++++++++++-------------------------------- 1 file changed, 37 insertions(+), 107 deletions(-) (limited to 'tools/shadertoy/template.cc') diff --git a/tools/shadertoy/template.cc b/tools/shadertoy/template.cc index fdc4f70..81c39ba 100644 --- a/tools/shadertoy/template.cc +++ b/tools/shadertoy/template.cc @@ -3,118 +3,48 @@ // TODO: Update description, rename class #include "effects/shadertoy_effect.h" -#include "generated/assets.h" -#include "gpu/shader_composer.h" - -// TODO: Rename class and adjust constructor parameters -ShaderToyEffect::ShaderToyEffect(const GpuContext& ctx) : Effect(ctx) { -} - -ShaderToyEffect::~ShaderToyEffect() { - if (sampler_) - wgpuSamplerRelease(sampler_); - if (bind_group_) - wgpuBindGroupRelease(bind_group_); - if (pipeline_) - wgpuRenderPipelineRelease(pipeline_); +#include "effects/shaders.h" +#include "gpu/gpu.h" +#include "gpu/post_process_helper.h" +#include "util/fatal_error.h" + +// TODO: Rename class +ShaderToyEffect::ShaderToyEffect(const GpuContext& ctx, + const std::vector& inputs, + const std::vector& outputs, + float start_time, float end_time) + : Effect(ctx, inputs, outputs, start_time, end_time) { + HEADLESS_RETURN_IF_NULL(ctx_.device); + create_nearest_sampler(); + create_dummy_scene_texture(); + + // TODO: Update shader name to match declaration in shaders.h + pipeline_.set(create_post_process_pipeline( + ctx_.device, WGPUTextureFormat_RGBA8Unorm, shadertoy_shader_wgsl)); } -void ShaderToyEffect::init(MainSequence* demo) { - demo_ = demo; - params_.init(ctx_.device); - - WGPUSamplerDescriptor sampler_desc = {}; - sampler_desc.addressModeU = WGPUAddressMode_ClampToEdge; - sampler_desc.addressModeV = WGPUAddressMode_ClampToEdge; - sampler_desc.magFilter = WGPUFilterMode_Linear; - sampler_desc.minFilter = WGPUFilterMode_Linear; - sampler_desc.mipmapFilter = WGPUMipmapFilterMode_Linear; - sampler_desc.maxAnisotropy = 1; - sampler_ = wgpuDeviceCreateSampler(ctx_.device, &sampler_desc); - - // TODO: Update asset name to match your shader file - size_t shader_size; - const char* shader_code = - (const char*)GetAsset(AssetId::ASSET_SHADERTOY_SHADER, &shader_size); - - std::string composed = ShaderComposer::Get().Compose({}, shader_code); - - WGPUShaderSourceWGSL wgsl = {}; - wgsl.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl.code = str_view(composed.c_str()); +void ShaderToyEffect::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { + WGPUTextureView output_view = nodes.get_view(output_nodes_[0]); - WGPUShaderModuleDescriptor desc = {}; - desc.nextInChain = &wgsl.chain; - WGPUShaderModule module = wgpuDeviceCreateShaderModule(ctx_.device, &desc); - - const WGPUColorTargetState target = { - .format = ctx_.format, - .writeMask = WGPUColorWriteMask_All, - }; - WGPUFragmentState frag = {}; - frag.module = module; - frag.entryPoint = str_view("fs_main"); - frag.targetCount = 1; - frag.targets = ⌖ - - const WGPUDepthStencilState depth_stencil = { - .format = WGPUTextureFormat_Depth24Plus, - .depthWriteEnabled = WGPUOptionalBool_False, - .depthCompare = WGPUCompareFunction_Always, - }; - - WGPURenderPipelineDescriptor pipeline_desc = {}; - pipeline_desc.label = label_view("ShaderToyEffect"); - pipeline_desc.vertex.module = module; - pipeline_desc.vertex.entryPoint = str_view("vs_main"); - pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList; - pipeline_desc.primitive.cullMode = WGPUCullMode_None; - pipeline_desc.depthStencil = &depth_stencil; - pipeline_desc.multisample.count = 1; - pipeline_desc.multisample.mask = 0xFFFFFFFF; - pipeline_desc.fragment = &frag; - - pipeline_ = wgpuDeviceCreateRenderPipeline(ctx_.device, &pipeline_desc); - wgpuShaderModuleRelease(module); - - WGPUTextureView prev_view = demo_->get_prev_texture_view(); - const WGPUBindGroupEntry entries[] = { - {.binding = 0, .sampler = sampler_}, - {.binding = 1, .textureView = prev_view}, - {.binding = 2, - .buffer = uniforms_.get().buffer, - .size = sizeof(UniformsSequenceParams)}, - {.binding = 3, - .buffer = params_.get().buffer, - .size = sizeof(ShaderToyParams)}, - }; - const WGPUBindGroupDescriptor bg_desc = { - .layout = wgpuRenderPipelineGetBindGroupLayout(pipeline_, 0), - .entryCount = 4, - .entries = entries, - }; - bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc); -} + // uniforms_buffer_ is auto-updated by base class dispatch_render() + pp_update_bind_group(ctx_.device, pipeline_.get(), bind_group_.get_address(), + dummy_texture_view_.get(), uniforms_buffer_.get(), + {nullptr, 0}); -void ShaderToyEffect::render(WGPURenderPassEncoder pass, float time, float beat, - float intensity, float aspect_ratio) { - const UniformsSequenceParams uniforms = { - .resolution = {(float)(width_), (float)(height_)}, - .aspect_ratio = aspect_ratio, - .time = time, - .beat = beat, - .audio_intensity = intensity, - }; - uniforms_.update(ctx_.queue, uniforms); + WGPURenderPassColorAttachment color_attachment = {}; + gpu_init_color_attachment(color_attachment, output_view); - // TODO: Update parameters based on your effect - const ShaderToyParams params = { - .param1 = 1.0f, - .param2 = beat, - }; - params_.update(ctx_.queue, params); + WGPURenderPassDescriptor pass_desc = {}; + pass_desc.colorAttachmentCount = 1; + pass_desc.colorAttachments = &color_attachment; - wgpuRenderPassEncoderSetPipeline(pass, pipeline_); - wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); + WGPURenderPassEncoder pass = + wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + wgpuRenderPassEncoderSetPipeline(pass, pipeline_.get()); + wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_.get(), 0, nullptr); wgpuRenderPassEncoderDraw(pass, 3, 1, 0, 0); + wgpuRenderPassEncoderEnd(pass); + wgpuRenderPassEncoderRelease(pass); } -- cgit v1.2.3