summaryrefslogtreecommitdiff
path: root/src/3d/renderer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/3d/renderer.cc')
-rw-r--r--src/3d/renderer.cc101
1 files changed, 87 insertions, 14 deletions
diff --git a/src/3d/renderer.cc b/src/3d/renderer.cc
index 0578271..0d0b2d8 100644
--- a/src/3d/renderer.cc
+++ b/src/3d/renderer.cc
@@ -428,63 +428,136 @@ void Renderer3D::update_uniforms(const Scene& scene, const Camera& camera,
}
}
-void Renderer3D::render(const Scene& scene, const Camera& camera, float time,
- WGPUTextureView target_view,
- WGPUTextureView depth_view_opt) {
+void Renderer3D::draw(WGPURenderPassEncoder pass, const Scene& scene, const Camera& camera, float time) {
+
update_uniforms(scene, camera, time);
- // Lazy Bind Group creation (since noise_texture might change)
- if (bind_group_)
- wgpuBindGroupRelease(bind_group_);
+
+
+ // Lazy Bind Group creation
+
+ if (bind_group_) wgpuBindGroupRelease(bind_group_);
+
+
WGPUBindGroupEntry bg_entries[4] = {};
+
bg_entries[0].binding = 0;
+
bg_entries[0].buffer = global_uniform_buffer_;
+
bg_entries[0].size = sizeof(GlobalUniforms);
+
bg_entries[1].binding = 1;
+
bg_entries[1].buffer = object_storage_buffer_;
+
bg_entries[1].size = sizeof(ObjectData) * kMaxObjects;
+
bg_entries[2].binding = 2;
+
bg_entries[2].textureView = noise_texture_view_;
+
bg_entries[3].binding = 3;
+
bg_entries[3].sampler = default_sampler_;
+
+
WGPUBindGroupDescriptor bg_desc = {};
+
bg_desc.layout = wgpuRenderPipelineGetBindGroupLayout(pipeline_, 0);
+
bg_desc.entryCount = 4;
+
bg_desc.entries = bg_entries;
+
bind_group_ = wgpuDeviceCreateBindGroup(device_, &bg_desc);
+
wgpuBindGroupLayoutRelease(bg_desc.layout);
+
+
+ wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
+
+ wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
+
+
+
+ uint32_t instance_count = (uint32_t)std::min((size_t)kMaxObjects, scene.objects.size());
+
+ if (instance_count > 0) {
+
+ wgpuRenderPassEncoderDraw(pass, 36, instance_count, 0, 0);
+
+ }
+
+}
+
+
+
+void Renderer3D::render(const Scene& scene, const Camera& camera, float time,
+
+ WGPUTextureView target_view, WGPUTextureView depth_view_opt) {
+
WGPUTextureView depth_view = depth_view_opt ? depth_view_opt : depth_view_;
+
+ if (!depth_view) return;
+
+
+
WGPURenderPassColorAttachment color_attachment = {};
+
gpu_init_color_attachment(color_attachment, target_view);
+
color_attachment.clearValue = {0.05, 0.05, 0.1, 1.0};
+
+
WGPURenderPassDepthStencilAttachment depth_attachment = {};
+
depth_attachment.view = depth_view;
+
depth_attachment.depthLoadOp = WGPULoadOp_Clear;
+
depth_attachment.depthStoreOp = WGPUStoreOp_Store;
+
depth_attachment.depthClearValue = 1.0f;
+
+
WGPURenderPassDescriptor pass_desc = {};
+
pass_desc.colorAttachmentCount = 1;
+
pass_desc.colorAttachments = &color_attachment;
+
pass_desc.depthStencilAttachment = &depth_attachment;
+
+
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device_, nullptr);
- WGPURenderPassEncoder pass =
- wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc);
- wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
- wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
- uint32_t instance_count =
- (uint32_t)std::min((size_t)kMaxObjects, scene.objects.size());
- if (instance_count > 0)
- wgpuRenderPassEncoderDraw(pass, 36, instance_count, 0, 0);
+
+ WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc);
+
+
+
+ draw(pass, scene, camera, time);
+
+
+
wgpuRenderPassEncoderEnd(pass);
+
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr);
+
wgpuQueueSubmit(queue_, 1, &commands);
+
+
+
wgpuRenderPassEncoderRelease(pass);
+
wgpuCommandBufferRelease(commands);
+
wgpuCommandEncoderRelease(encoder);
+
} \ No newline at end of file