summaryrefslogtreecommitdiff
path: root/cnn_v3/src/gbuf_deferred_effect.cc
diff options
context:
space:
mode:
Diffstat (limited to 'cnn_v3/src/gbuf_deferred_effect.cc')
-rw-r--r--cnn_v3/src/gbuf_deferred_effect.cc91
1 files changed, 49 insertions, 42 deletions
diff --git a/cnn_v3/src/gbuf_deferred_effect.cc b/cnn_v3/src/gbuf_deferred_effect.cc
index de6bd29..561f660 100644
--- a/cnn_v3/src/gbuf_deferred_effect.cc
+++ b/cnn_v3/src/gbuf_deferred_effect.cc
@@ -1,4 +1,5 @@
-// GBufDeferredEffect — simple deferred render: albedo * shadow from packed G-buffer.
+// GBufDeferredEffect — simple deferred render: albedo * shadow from packed
+// G-buffer.
#include "gbuf_deferred_effect.h"
#include "gpu/gpu.h"
@@ -10,22 +11,24 @@ extern const char* gbuf_deferred_wgsl;
struct GBufDeferredUniforms {
float resolution[2];
};
-static_assert(sizeof(GBufDeferredUniforms) == 8, "GBufDeferredUniforms must be 8 bytes");
+static_assert(sizeof(GBufDeferredUniforms) == 8,
+ "GBufDeferredUniforms must be 8 bytes");
static WGPUBindGroupLayoutEntry bgl_uint_tex(uint32_t binding) {
WGPUBindGroupLayoutEntry e = {};
- e.binding = binding;
- e.visibility = WGPUShaderStage_Fragment;
- e.texture.sampleType = WGPUTextureSampleType_Uint;
+ e.binding = binding;
+ e.visibility = WGPUShaderStage_Fragment;
+ e.texture.sampleType = WGPUTextureSampleType_Uint;
e.texture.viewDimension = WGPUTextureViewDimension_2D;
return e;
}
-static WGPUBindGroupLayoutEntry bgl_uniform(uint32_t binding, uint64_t min_size) {
+static WGPUBindGroupLayoutEntry bgl_uniform(uint32_t binding,
+ uint64_t min_size) {
WGPUBindGroupLayoutEntry e = {};
- e.binding = binding;
- e.visibility = WGPUShaderStage_Fragment;
- e.buffer.type = WGPUBufferBindingType_Uniform;
+ e.binding = binding;
+ e.visibility = WGPUShaderStage_Fragment;
+ e.buffer.type = WGPUBufferBindingType_Uniform;
e.buffer.minBindingSize = min_size;
return e;
}
@@ -44,40 +47,43 @@ GBufDeferredEffect::GBufDeferredEffect(const GpuContext& ctx,
};
WGPUBindGroupLayoutDescriptor bgl_desc = {};
bgl_desc.entryCount = 3;
- bgl_desc.entries = entries;
- WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(ctx_.device, &bgl_desc);
+ bgl_desc.entries = entries;
+ WGPUBindGroupLayout bgl =
+ wgpuDeviceCreateBindGroupLayout(ctx_.device, &bgl_desc);
WGPUPipelineLayoutDescriptor pl_desc = {};
pl_desc.bindGroupLayoutCount = 1;
- pl_desc.bindGroupLayouts = &bgl;
+ pl_desc.bindGroupLayouts = &bgl;
WGPUPipelineLayout pl = wgpuDeviceCreatePipelineLayout(ctx_.device, &pl_desc);
WGPUShaderSourceWGSL wgsl_src = {};
wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL;
- const std::string composed = ShaderComposer::Get().Compose({}, gbuf_deferred_wgsl);
- wgsl_src.code = str_view(composed.c_str());
+ const std::string composed =
+ ShaderComposer::Get().Compose({}, gbuf_deferred_wgsl);
+ wgsl_src.code = str_view(composed.c_str());
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = &wgsl_src.chain;
- WGPUShaderModule shader = wgpuDeviceCreateShaderModule(ctx_.device, &shader_desc);
+ WGPUShaderModule shader =
+ wgpuDeviceCreateShaderModule(ctx_.device, &shader_desc);
WGPUColorTargetState target = {};
- target.format = WGPUTextureFormat_RGBA8Unorm;
+ target.format = WGPUTextureFormat_RGBA8Unorm;
target.writeMask = WGPUColorWriteMask_All;
WGPUFragmentState frag = {};
- frag.module = shader;
- frag.entryPoint = str_view("fs_main");
+ frag.module = shader;
+ frag.entryPoint = str_view("fs_main");
frag.targetCount = 1;
- frag.targets = ⌖
+ frag.targets = ⌖
WGPURenderPipelineDescriptor pipe_desc = {};
- pipe_desc.layout = pl;
- pipe_desc.vertex.module = shader;
- pipe_desc.vertex.entryPoint = str_view("vs_main");
- pipe_desc.fragment = &frag;
- pipe_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
- pipe_desc.multisample.count = 1;
- pipe_desc.multisample.mask = UINT32_MAX;
+ pipe_desc.layout = pl;
+ pipe_desc.vertex.module = shader;
+ pipe_desc.vertex.entryPoint = str_view("vs_main");
+ pipe_desc.fragment = &frag;
+ pipe_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList;
+ pipe_desc.multisample.count = 1;
+ pipe_desc.multisample.mask = UINT32_MAX;
pipeline_.set(wgpuDeviceCreateRenderPipeline(ctx_.device, &pipe_desc));
@@ -89,46 +95,47 @@ GBufDeferredEffect::GBufDeferredEffect(const GpuContext& ctx,
void GBufDeferredEffect::render(WGPUCommandEncoder encoder,
const UniformsSequenceParams& params,
NodeRegistry& nodes) {
- WGPUTextureView feat0_view = nodes.get_view(input_nodes_[0]);
- WGPUTextureView feat1_view = nodes.get_view(input_nodes_[1]);
+ WGPUTextureView feat0_view = nodes.get_view(input_nodes_[0]);
+ WGPUTextureView feat1_view = nodes.get_view(input_nodes_[1]);
WGPUTextureView output_view = nodes.get_view(output_nodes_[0]);
- // Upload resolution uniform into the base class uniforms buffer (first 8 bytes).
+ // Upload resolution uniform into the base class uniforms buffer (first 8
+ // bytes).
GBufDeferredUniforms u;
u.resolution[0] = params.resolution.x;
u.resolution[1] = params.resolution.y;
- wgpuQueueWriteBuffer(ctx_.queue, uniforms_buffer_.get().buffer, 0,
- &u, sizeof(u));
+ wgpuQueueWriteBuffer(ctx_.queue, uniforms_buffer_.get().buffer, 0, &u,
+ sizeof(u));
WGPUBindGroupLayout bgl =
wgpuRenderPipelineGetBindGroupLayout(pipeline_.get(), 0);
WGPUBindGroupEntry bg_entries[3] = {};
- bg_entries[0].binding = 0;
+ bg_entries[0].binding = 0;
bg_entries[0].textureView = feat0_view;
- bg_entries[1].binding = 1;
+ bg_entries[1].binding = 1;
bg_entries[1].textureView = feat1_view;
- bg_entries[2].binding = 2;
- bg_entries[2].buffer = uniforms_buffer_.get().buffer;
- bg_entries[2].size = sizeof(GBufDeferredUniforms);
+ bg_entries[2].binding = 2;
+ bg_entries[2].buffer = uniforms_buffer_.get().buffer;
+ bg_entries[2].size = sizeof(GBufDeferredUniforms);
WGPUBindGroupDescriptor bg_desc = {};
- bg_desc.layout = bgl;
+ bg_desc.layout = bgl;
bg_desc.entryCount = 3;
- bg_desc.entries = bg_entries;
+ bg_desc.entries = bg_entries;
bind_group_.replace(wgpuDeviceCreateBindGroup(ctx_.device, &bg_desc));
wgpuBindGroupLayoutRelease(bgl);
WGPURenderPassColorAttachment color_att = {};
- color_att.view = output_view;
- color_att.loadOp = WGPULoadOp_Clear;
- color_att.storeOp = WGPUStoreOp_Store;
+ color_att.view = output_view;
+ color_att.loadOp = WGPULoadOp_Clear;
+ color_att.storeOp = WGPUStoreOp_Store;
color_att.clearValue = {0.0f, 0.0f, 0.0f, 1.0f};
color_att.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED;
WGPURenderPassDescriptor pass_desc = {};
pass_desc.colorAttachmentCount = 1;
- pass_desc.colorAttachments = &color_att;
+ pass_desc.colorAttachments = &color_att;
WGPURenderPassEncoder pass =
wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc);