1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// This file is part of the 64k demo project.
// It implements the core Renderer3D class logic.
#include "3d/renderer.h"
#include "generated/assets.h"
#include "gpu/effects/shader_composer.h"
#include "util/asset_manager.h"
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>
#if !defined(STRIP_ALL)
bool Renderer3D::s_debug_enabled_ = false;
#endif
void Renderer3D::init(WGPUDevice device, WGPUQueue queue,
WGPUTextureFormat format) {
device_ = device;
queue_ = queue;
format_ = format;
WGPUSamplerDescriptor sampler_desc = {};
sampler_desc.addressModeU = WGPUAddressMode_Repeat;
sampler_desc.addressModeV = WGPUAddressMode_Repeat;
sampler_desc.magFilter = WGPUFilterMode_Linear;
sampler_desc.minFilter = WGPUFilterMode_Linear;
sampler_desc.maxAnisotropy = 1;
default_sampler_ = wgpuDeviceCreateSampler(device_, &sampler_desc);
create_default_resources();
create_pipeline();
create_skybox_pipeline();
#if !defined(STRIP_ALL)
visual_debug_.init(device_, format_);
#endif
}
void Renderer3D::shutdown() {
#if !defined(STRIP_ALL)
visual_debug_.shutdown();
#endif
if (default_sampler_)
wgpuSamplerRelease(default_sampler_);
if (pipeline_)
wgpuRenderPipelineRelease(pipeline_);
if (pipeline_no_bvh_)
wgpuRenderPipelineRelease(pipeline_no_bvh_);
if (bind_group_)
wgpuBindGroupRelease(bind_group_);
if (mesh_pipeline_)
wgpuRenderPipelineRelease(mesh_pipeline_);
if (skybox_pipeline_)
wgpuRenderPipelineRelease(skybox_pipeline_);
if (skybox_bind_group_)
wgpuBindGroupRelease(skybox_bind_group_);
if (global_uniform_buffer_)
wgpuBufferRelease(global_uniform_buffer_);
if (object_storage_buffer_)
wgpuBufferRelease(object_storage_buffer_);
if (bvh_storage_buffer_)
wgpuBufferRelease(bvh_storage_buffer_);
if (depth_view_)
wgpuTextureViewRelease(depth_view_);
if (depth_texture_)
wgpuTextureRelease(depth_texture_);
// Clear mesh cache
for (auto& pair : mesh_cache_) {
if (pair.second.vertex_buffer)
wgpuBufferRelease(pair.second.vertex_buffer);
if (pair.second.index_buffer)
wgpuBufferRelease(pair.second.index_buffer);
}
mesh_cache_.clear();
}
void Renderer3D::resize(int width, int height) {
if (width == width_ && height == height_)
return;
width_ = width;
height_ = height;
if (depth_view_)
wgpuTextureViewRelease(depth_view_);
if (depth_texture_)
wgpuTextureRelease(depth_texture_);
WGPUTextureDescriptor desc = {};
desc.usage = WGPUTextureUsage_RenderAttachment;
desc.dimension = WGPUTextureDimension_2D;
desc.size = {(uint32_t)width, (uint32_t)height, 1};
desc.format = WGPUTextureFormat_Depth24Plus;
desc.mipLevelCount = 1;
desc.sampleCount = 1;
depth_texture_ = wgpuDeviceCreateTexture(device_, &desc);
WGPUTextureViewDescriptor view_desc = {};
view_desc.format = WGPUTextureFormat_Depth24Plus;
view_desc.dimension = WGPUTextureViewDimension_2D;
view_desc.aspect = WGPUTextureAspect_DepthOnly;
view_desc.arrayLayerCount = 1;
view_desc.mipLevelCount = 1;
depth_view_ = wgpuTextureCreateView(depth_texture_, &view_desc);
}
void Renderer3D::set_noise_texture(WGPUTextureView noise_view) {
noise_texture_view_ = noise_view;
}
void Renderer3D::set_sky_texture(WGPUTextureView sky_view) {
sky_texture_view_ = sky_view;
}
void Renderer3D::add_debug_aabb(const vec3& min, const vec3& max,
const vec3& color) {
#if !defined(STRIP_ALL)
visual_debug_.add_aabb(min, max, color);
#endif
}
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;
WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device_, nullptr);
// --- Pass 1: Render Skybox (Clears color, clears and stores depth) ---
bool skybox_rendered = false;
if (sky_texture_view_ && skybox_pipeline_) {
WGPURenderPassColorAttachment sky_color_attachment = {};
gpu_init_color_attachment(sky_color_attachment, target_view);
sky_color_attachment.loadOp = WGPULoadOp_Clear; // Clear to black
sky_color_attachment.storeOp = WGPUStoreOp_Store;
sky_color_attachment.clearValue = {0.0f, 0.0f, 0.0f, 1.0f};
WGPURenderPassDepthStencilAttachment sky_depth_attachment = {};
sky_depth_attachment.view = depth_view;
sky_depth_attachment.depthLoadOp = WGPULoadOp_Clear; // Clear depth
sky_depth_attachment.depthStoreOp =
WGPUStoreOp_Store; // Store cleared depth
sky_depth_attachment.depthClearValue = 1.0f; // Farthest possible depth
WGPURenderPassDescriptor sky_pass_desc = {};
sky_pass_desc.colorAttachmentCount = 1;
sky_pass_desc.colorAttachments = &sky_color_attachment;
sky_pass_desc.depthStencilAttachment = &sky_depth_attachment;
WGPURenderPassEncoder sky_pass =
wgpuCommandEncoderBeginRenderPass(encoder, &sky_pass_desc);
wgpuRenderPassEncoderSetViewport(sky_pass, 0.0f, 0.0f, (float)width_,
(float)height_, 0.0f, 1.0f);
if (skybox_bind_group_)
wgpuBindGroupRelease(skybox_bind_group_);
WGPUBindGroupEntry bg_entries[3] = {};
bg_entries[0].binding = 0;
bg_entries[0].textureView = sky_texture_view_;
bg_entries[1].binding = 1;
bg_entries[1].sampler = default_sampler_;
bg_entries[2].binding = 2;
bg_entries[2].buffer = global_uniform_buffer_;
bg_entries[2].size = sizeof(GlobalUniforms);
WGPUBindGroupDescriptor bg_desc = {};
bg_desc.layout = wgpuRenderPipelineGetBindGroupLayout(skybox_pipeline_, 0);
bg_desc.entryCount = 3;
bg_desc.entries = bg_entries;
skybox_bind_group_ = wgpuDeviceCreateBindGroup(device_, &bg_desc);
wgpuBindGroupLayoutRelease(bg_desc.layout);
wgpuRenderPassEncoderSetPipeline(sky_pass, skybox_pipeline_);
wgpuRenderPassEncoderSetBindGroup(sky_pass, 0, skybox_bind_group_, 0,
nullptr);
wgpuRenderPassEncoderDraw(sky_pass, 3, 1, 0, 0); // Draw a full-screen quad
wgpuRenderPassEncoderEnd(sky_pass);
wgpuRenderPassEncoderRelease(sky_pass);
skybox_rendered = true;
}
// --- Pass 2: Render Scene Objects (Loads depth, writes depth) ---
WGPURenderPassColorAttachment obj_color_attachment = {};
gpu_init_color_attachment(obj_color_attachment, target_view);
obj_color_attachment.loadOp =
skybox_rendered ? WGPULoadOp_Load : WGPULoadOp_Clear; // Load or Clear
obj_color_attachment.storeOp = WGPUStoreOp_Store;
obj_color_attachment.clearValue = {0.05f, 0.05f, 0.05f,
1.0f}; // Dark gray if no sky
WGPURenderPassDepthStencilAttachment obj_depth_attachment = {};
obj_depth_attachment.view = depth_view;
obj_depth_attachment.depthLoadOp =
skybox_rendered ? WGPULoadOp_Load : WGPULoadOp_Clear; // Load or Clear
obj_depth_attachment.depthStoreOp = WGPUStoreOp_Store; // Store object depth
obj_depth_attachment.depthClearValue = 1.0f;
WGPURenderPassDescriptor obj_pass_desc = {};
obj_pass_desc.colorAttachmentCount = 1;
obj_pass_desc.colorAttachments = &obj_color_attachment;
obj_pass_desc.depthStencilAttachment = &obj_depth_attachment;
WGPURenderPassEncoder obj_pass =
wgpuCommandEncoderBeginRenderPass(encoder, &obj_pass_desc);
wgpuRenderPassEncoderSetViewport(obj_pass, 0.0f, 0.0f, (float)width_,
(float)height_, 0.0f, 1.0f);
draw(obj_pass, scene, camera, time);
wgpuRenderPassEncoderEnd(obj_pass);
wgpuRenderPassEncoderRelease(obj_pass);
WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr);
wgpuQueueSubmit(queue_, 1, &commands);
wgpuCommandBufferRelease(commands);
wgpuCommandEncoderRelease(encoder);
}
|