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
|
// This file is part of the 64k demo project.
// It defines the Renderer3D class.
// Handles WebGPU pipeline creation and execution for 3D scenes.
#pragma once
#include "3d/camera.h"
#include "3d/scene.h"
#include "gpu/gpu.h"
#include <vector>
#if !defined(STRIP_ALL)
#include "3d/visual_debug.h"
#endif
// Matches the GPU struct layout
struct GlobalUniforms {
mat4 view_proj;
mat4 inv_view_proj; // Added for skybox/raymarching
vec4 camera_pos_time; // xyz = camera_pos, w = time
vec4 params; // x = num_objects, yzw = padding
vec2 resolution;
vec2 padding;
};
// Matches the GPU struct layout
struct ObjectData {
mat4 model;
mat4 inv_model;
vec4 color;
vec4 params; // Type, etc.
};
class Renderer3D {
public:
void init(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
void shutdown();
#if !defined(STRIP_ALL)
static void SetDebugEnabled(bool enabled) {
s_debug_enabled_ = enabled;
}
#endif
// Renders the scene to the given texture view (Convenience: creates a pass)
void render(const Scene& scene, const Camera& camera, float time,
WGPUTextureView target_view,
WGPUTextureView depth_view_opt = nullptr);
// Records draw commands to an existing pass.
// Assumes the pass has a compatible pipeline (or we set it here).
// Note: Caller must ensure depth/color attachments are set up correctly in
// the pass.
void draw(WGPURenderPassEncoder pass, const Scene& scene,
const Camera& camera, float time);
void set_noise_texture(WGPUTextureView noise_view);
void set_sky_texture(WGPUTextureView sky_view);
// Resize handler (if needed for internal buffers)
void resize(int width, int height);
private:
void create_pipeline();
void create_skybox_pipeline();
void create_default_resources();
void update_uniforms(const Scene& scene, const Camera& camera, float time);
WGPUDevice device_ = nullptr;
WGPUQueue queue_ = nullptr;
WGPUTextureFormat format_ = WGPUTextureFormat_Undefined;
WGPURenderPipeline pipeline_ = nullptr;
WGPUBindGroup bind_group_ = nullptr;
WGPURenderPipeline skybox_pipeline_ = nullptr;
WGPUBindGroup skybox_bind_group_ = nullptr;
WGPUBuffer global_uniform_buffer_ = nullptr;
WGPUBuffer object_storage_buffer_ = nullptr;
WGPUTextureView noise_texture_view_ = nullptr;
WGPUTextureView sky_texture_view_ = nullptr;
WGPUSampler default_sampler_ = nullptr;
// Depth buffer management
WGPUTexture depth_texture_ = nullptr;
WGPUTextureView depth_view_ = nullptr;
int width_ = 0;
int height_ = 0;
// Max objects capacity
static const int kMaxObjects = 100;
#if !defined(STRIP_ALL)
VisualDebug visual_debug_;
static bool s_debug_enabled_;
#endif
};
|