diff options
Diffstat (limited to 'src/effects/rotating_cube_effect.cc')
| -rw-r--r-- | src/effects/rotating_cube_effect.cc | 68 |
1 files changed, 45 insertions, 23 deletions
diff --git a/src/effects/rotating_cube_effect.cc b/src/effects/rotating_cube_effect.cc index 57f0a83..6c350a7 100644 --- a/src/effects/rotating_cube_effect.cc +++ b/src/effects/rotating_cube_effect.cc @@ -1,16 +1,17 @@ // This file is part of the 64k demo project. -// It implements RotatingCubeEffect (simplified v2 port). +// It implements RotatingCube. -#include "util/fatal_error.h" #include "effects/rotating_cube_effect.h" +#include "effects/shaders.h" #include "gpu/bind_group_builder.h" #include "gpu/gpu.h" -#include "gpu/shaders.h" +#include "util/fatal_error.h" -RotatingCubeEffect::RotatingCubeEffect(const GpuContext& ctx, - const std::vector<std::string>& inputs, - const std::vector<std::string>& outputs) - : Effect(ctx, inputs, outputs), depth_node_(outputs[0] + "_depth") { +RotatingCube::RotatingCube(const GpuContext& ctx, + const std::vector<std::string>& inputs, + const std::vector<std::string>& outputs, float start_time, + float end_time) + : Effect(ctx, inputs, outputs, start_time, end_time), depth_node_(outputs[0] + "_depth") { // Headless mode: skip GPU resource creation (compiled out in STRIP_ALL) HEADLESS_RETURN_IF_NULL(ctx_.device); @@ -39,10 +40,10 @@ RotatingCubeEffect::RotatingCubeEffect(const GpuContext& ctx, WGPUPipelineLayout pipeline_layout = wgpuDeviceCreatePipelineLayout(ctx_.device, &pl_desc); - // Load shader (TODO: create rotating_cube_v2.wgsl) + // Load shader WGPUShaderSourceWGSL wgsl_src = {}; wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(rotating_cube_v2_wgsl); + wgsl_src.code = str_view(rotating_cube_wgsl); WGPUShaderModuleDescriptor shader_desc = {}; shader_desc.nextInChain = &wgsl_src.chain; @@ -100,21 +101,21 @@ RotatingCubeEffect::RotatingCubeEffect(const GpuContext& ctx, wgpuBindGroupLayoutRelease(bgl); } -RotatingCubeEffect::~RotatingCubeEffect() { +RotatingCube::~RotatingCube() { if (bind_group_) wgpuBindGroupRelease(bind_group_); if (pipeline_) wgpuRenderPipelineRelease(pipeline_); } -void RotatingCubeEffect::declare_nodes(NodeRegistry& registry) { +void RotatingCube::declare_nodes(NodeRegistry& registry) { // Declare depth buffer node registry.declare_node(depth_node_, NodeType::DEPTH24, -1, -1); } -void RotatingCubeEffect::render(WGPUCommandEncoder encoder, - const UniformsSequenceParams& params, - NodeRegistry& nodes) { +void RotatingCube::render(WGPUCommandEncoder encoder, + const UniformsSequenceParams& params, + NodeRegistry& nodes) { rotation_ += 0.016f * 1.5f; // Camera setup @@ -138,7 +139,8 @@ void RotatingCubeEffect::render(WGPUCommandEncoder encoder, const Uniforms uniforms = { .view_proj = view_proj, .inv_view_proj = view_proj.inverse(), - .camera_pos_time = vec4(camera_pos.x, camera_pos.y, camera_pos.z, params.time), + .camera_pos_time = + vec4(camera_pos.x, camera_pos.y, camera_pos.z, params.time), .params = vec4(1.0f, 0.0f, 0.0f, 0.0f), .resolution = params.resolution, .aspect_ratio = params.aspect_ratio, @@ -156,6 +158,22 @@ void RotatingCubeEffect::render(WGPUCommandEncoder encoder, wgpuQueueWriteBuffer(ctx_.queue, object_buffer_.buffer, 0, &obj_data, sizeof(ObjectData)); + // Blit input to output if compositing (not reading from source) + if (!input_nodes_.empty() && input_nodes_[0] != "source") { + WGPUTexture input_tex = nodes.get_texture(input_nodes_[0]); + WGPUTexture output_tex = nodes.get_texture(output_nodes_[0]); + if (input_tex && output_tex) { + GpuTextureCopyInfo src = { + .texture = input_tex, .mipLevel = 0, .aspect = WGPUTextureAspect_All}; + GpuTextureCopyInfo dst = {.texture = output_tex, + .mipLevel = 0, + .aspect = WGPUTextureAspect_All}; + WGPUExtent3D copy_size = {(uint32_t)params.resolution.x, + (uint32_t)params.resolution.y, 1}; + wgpuCommandEncoderCopyTextureToTexture(encoder, &src, &dst, ©_size); + } + } + // Get output views WGPUTextureView color_view = nodes.get_view(output_nodes_[0]); WGPUTextureView depth_view = nodes.get_view(depth_node_); @@ -163,10 +181,13 @@ void RotatingCubeEffect::render(WGPUCommandEncoder encoder, // Render pass with depth WGPURenderPassColorAttachment color_attachment = { .view = color_view, + #if !defined(DEMO_CROSS_COMPILE_WIN32) .depthSlice = WGPU_DEPTH_SLICE_UNDEFINED, - .loadOp = WGPULoadOp_Clear, +#endif + // .loadOp = WGPULoadOp_Clear, + .loadOp = WGPULoadOp_Load, .storeOp = WGPUStoreOp_Store, - .clearValue = {0.0, 0.0, 0.0, 1.0}}; + .clearValue = {0.0, 0.0, 0.0, 0.0}}; WGPURenderPassDepthStencilAttachment depth_attachment = { .view = depth_view, @@ -174,15 +195,16 @@ void RotatingCubeEffect::render(WGPUCommandEncoder encoder, .depthStoreOp = WGPUStoreOp_Discard, .depthClearValue = 1.0f}; - WGPURenderPassDescriptor pass_desc = { - .colorAttachmentCount = 1, - .colorAttachments = &color_attachment, - .depthStencilAttachment = &depth_attachment}; + WGPURenderPassDescriptor pass_desc = {.colorAttachmentCount = 1, + .colorAttachments = &color_attachment, + .depthStencilAttachment = + &depth_attachment}; - WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); + WGPURenderPassEncoder pass = + wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); wgpuRenderPassEncoderSetPipeline(pass, pipeline_); wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr); - wgpuRenderPassEncoderDraw(pass, 36, 1, 0, 0); // 36 vertices for cube + wgpuRenderPassEncoderDraw(pass, 36, 1, 0, 0); // 36 vertices for cube wgpuRenderPassEncoderEnd(pass); wgpuRenderPassEncoderRelease(pass); } |
