summaryrefslogtreecommitdiff
path: root/src/3d/visual_debug.cc
blob: f8d9bedcacd02651ca5a8a33460640c8675f545c (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// This file is part of the 64k demo project.
// Implementation of visual debugging tools.

#include "3d/visual_debug.h"

#if !defined(STRIP_ALL)

#include "generated/assets.h"
#include "util/asset_manager.h"
#include <cstdio>
#include <cstring>

void VisualDebug::init(WGPUDevice device, WGPUTextureFormat format) {
  device_ = device;
  create_pipeline(format);

  // Initial capacity for vertex buffer (e.g., 1024 lines)
  vertex_buffer_capacity_ =
      1024 * 2 * sizeof(float) * 6; // 2 verts per line, 6 floats per vert

  WGPUBufferDescriptor vb_desc = {};
  vb_desc.usage = WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst;
  vb_desc.size = vertex_buffer_capacity_;
  vertex_buffer_ = wgpuDeviceCreateBuffer(device_, &vb_desc);

  WGPUBufferDescriptor ub_desc = {};
  ub_desc.usage = WGPUBufferUsage_Uniform | WGPUBufferUsage_CopyDst;
  ub_desc.size = sizeof(mat4);
  uniform_buffer_ = wgpuDeviceCreateBuffer(device_, &ub_desc);
}

void VisualDebug::shutdown() {
  if (pipeline_)
    wgpuRenderPipelineRelease(pipeline_);
  if (bind_group_layout_)
    wgpuBindGroupLayoutRelease(bind_group_layout_);
  if (vertex_buffer_)
    wgpuBufferRelease(vertex_buffer_);
  if (uniform_buffer_)
    wgpuBufferRelease(uniform_buffer_);
  if (bind_group_)
    wgpuBindGroupRelease(bind_group_);
}

void VisualDebug::create_pipeline(WGPUTextureFormat format) {
  // Bind Group Layout
  WGPUBindGroupLayoutEntry bgl_entry = {};
  bgl_entry.binding = 0;
  bgl_entry.visibility = WGPUShaderStage_Vertex;
  bgl_entry.buffer.type = WGPUBufferBindingType_Uniform;
  bgl_entry.buffer.minBindingSize = sizeof(mat4);

  WGPUBindGroupLayoutDescriptor bgl_desc = {};
  bgl_desc.entryCount = 1;
  bgl_desc.entries = &bgl_entry;
  bind_group_layout_ = wgpuDeviceCreateBindGroupLayout(device_, &bgl_desc);

  // Pipeline Layout
  WGPUPipelineLayoutDescriptor pl_desc = {};
  pl_desc.bindGroupLayoutCount = 1;
  pl_desc.bindGroupLayouts = &bind_group_layout_;
  WGPUPipelineLayout pipeline_layout =
      wgpuDeviceCreatePipelineLayout(device_, &pl_desc);

  // Shader
  size_t shader_len = 0;
  const char* shader_code =
      (const char*)GetAsset(AssetId::ASSET_SHADER_VISUAL_DEBUG, &shader_len);

#if defined(DEMO_CROSS_COMPILE_WIN32)
  WGPUShaderModuleWGSLDescriptor wgsl_desc = {};
  wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
  wgsl_desc.code = shader_code;
  WGPUShaderModuleDescriptor shader_desc = {};
  shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#else
  WGPUShaderSourceWGSL wgsl_desc = {};
  wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
  wgsl_desc.code = {shader_code, shader_len};
  WGPUShaderModuleDescriptor shader_desc = {};
  shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#endif
  WGPUShaderModule shader_module =
      wgpuDeviceCreateShaderModule(device_, &shader_desc);

  // Vertex State
  WGPUVertexAttribute attributes[2];
  // Position
  attributes[0].format = WGPUVertexFormat_Float32x3;
  attributes[0].offset = 0;
  attributes[0].shaderLocation = 0;
  // Color
  attributes[1].format = WGPUVertexFormat_Float32x3;
  attributes[1].offset = sizeof(float) * 3;
  attributes[1].shaderLocation = 1;

  WGPUVertexBufferLayout vertex_layout = {};
  vertex_layout.arrayStride = sizeof(float) * 6;
  vertex_layout.stepMode = WGPUVertexStepMode_Vertex;
  vertex_layout.attributeCount = 2;
  vertex_layout.attributes = attributes;

  // Pipeline
  WGPURenderPipelineDescriptor pipeline_desc = {};
  pipeline_desc.layout = pipeline_layout;
  pipeline_desc.vertex.module = shader_module;
#if defined(DEMO_CROSS_COMPILE_WIN32)
  pipeline_desc.vertex.entryPoint = "vs_main";
#else
  pipeline_desc.vertex.entryPoint = {"vs_main", 7};
#endif
  pipeline_desc.vertex.bufferCount = 1;
  pipeline_desc.vertex.buffers = &vertex_layout;

  WGPUFragmentState fragment_state = {};
  fragment_state.module = shader_module;
#if defined(DEMO_CROSS_COMPILE_WIN32)
  fragment_state.entryPoint = "fs_main";
#else
  fragment_state.entryPoint = {"fs_main", 7};
#endif
  fragment_state.targetCount = 1;

  WGPUColorTargetState color_target = {};
  color_target.format = format;
  color_target.writeMask = WGPUColorWriteMask_All;
  // Enable simple alpha blending if needed, but opaque lines are fine for now
  fragment_state.targets = &color_target;
  pipeline_desc.fragment = &fragment_state;

  pipeline_desc.primitive.topology = WGPUPrimitiveTopology_LineList;
  pipeline_desc.primitive.cullMode = WGPUCullMode_None;
  pipeline_desc.primitive.frontFace = WGPUFrontFace_CCW;

  WGPUDepthStencilState depth_stencil = {};
  depth_stencil.format = WGPUTextureFormat_Depth24Plus;
  depth_stencil.depthWriteEnabled = WGPUOptionalBool_False; // Don't write depth
  depth_stencil.depthCompare =
      WGPUCompareFunction_Less; // But do test against it
  pipeline_desc.depthStencil = &depth_stencil;

  pipeline_desc.multisample.count = 1;
  pipeline_desc.multisample.mask = 0xFFFFFFFF;

  pipeline_ = wgpuDeviceCreateRenderPipeline(device_, &pipeline_desc);

  wgpuPipelineLayoutRelease(pipeline_layout);
  wgpuShaderModuleRelease(shader_module);
}

void VisualDebug::add_box(const mat4& transform, const vec3& local_extent,
                          const vec3& color) {
  float lx = local_extent.x;
  float ly = local_extent.y;
  float lz = local_extent.z;

  // 8 corners of transformed box
  vec4 p[] = {
      transform * vec4(-lx, -ly, -lz, 1), transform * vec4(lx, -ly, -lz, 1),
      transform * vec4(lx, ly, -lz, 1),   transform * vec4(-lx, ly, -lz, 1),
      transform * vec4(-lx, -ly, lz, 1),  transform * vec4(lx, -ly, lz, 1),
      transform * vec4(lx, ly, lz, 1),    transform * vec4(-lx, ly, lz, 1)};

  // 12 edges (each 2 vertices)
  DebugLine edges[] = {
      {p[0].xyz(), p[1].xyz(), color},
      {p[1].xyz(), p[2].xyz(), color},
      {p[2].xyz(), p[3].xyz(), color},
      {p[3].xyz(), p[0].xyz(), color}, // Front face
      {p[4].xyz(), p[5].xyz(), color},
      {p[5].xyz(), p[6].xyz(), color},
      {p[6].xyz(), p[7].xyz(), color},
      {p[7].xyz(), p[4].xyz(), color}, // Back face
      {p[0].xyz(), p[4].xyz(), color},
      {p[1].xyz(), p[5].xyz(), color},
      {p[2].xyz(), p[6].xyz(), color},
      {p[3].xyz(), p[7].xyz(), color} // Connecting edges
  };

  for (const auto& l : edges) {
    lines_.push_back(l);
  }
}

void VisualDebug::update_buffers(const mat4& view_proj) {
  // Update Uniforms
  wgpuQueueWriteBuffer(wgpuDeviceGetQueue(device_), uniform_buffer_, 0,
                       &view_proj, sizeof(mat4));

  // Update Vertices
  size_t required_size = lines_.size() * 2 * sizeof(float) * 6;
  if (required_size > vertex_buffer_capacity_) {
    // Resize buffer
    wgpuBufferRelease(vertex_buffer_);
    vertex_buffer_capacity_ = required_size * 2; // Double capacity
    WGPUBufferDescriptor vb_desc = {};
    vb_desc.usage = WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst;
    vb_desc.size = vertex_buffer_capacity_;
    vertex_buffer_ = wgpuDeviceCreateBuffer(device_, &vb_desc);
  }

  if (required_size > 0) {
    std::vector<float> vertex_data;
    vertex_data.reserve(lines_.size() * 12); // 2 verts * 6 floats
    for (const auto& line : lines_) {
      vertex_data.push_back(line.start.x);
      vertex_data.push_back(line.start.y);
      vertex_data.push_back(line.start.z);
      vertex_data.push_back(line.color.x);
      vertex_data.push_back(line.color.y);
      vertex_data.push_back(line.color.z);

      vertex_data.push_back(line.end.x);
      vertex_data.push_back(line.end.y);
      vertex_data.push_back(line.end.z);
      vertex_data.push_back(line.color.x);
      vertex_data.push_back(line.color.y);
      vertex_data.push_back(line.color.z);
    }
    wgpuQueueWriteBuffer(wgpuDeviceGetQueue(device_), vertex_buffer_, 0,
                         vertex_data.data(),
                         vertex_data.size() * sizeof(float));
  }

  // Re-create bind group if needed (e.g. if uniform buffer changed, though here
  // it's static)
  if (!bind_group_) {
    WGPUBindGroupEntry bg_entry = {};
    bg_entry.binding = 0;
    bg_entry.buffer = uniform_buffer_;
    bg_entry.size = sizeof(mat4);

    WGPUBindGroupDescriptor bg_desc = {};
    bg_desc.layout = bind_group_layout_;
    bg_desc.entryCount = 1;
    bg_desc.entries = &bg_entry;
    bind_group_ = wgpuDeviceCreateBindGroup(device_, &bg_desc);
  }
}

void VisualDebug::render(WGPURenderPassEncoder pass, const mat4& view_proj) {
  if (lines_.empty())
    return;

  update_buffers(view_proj);

  wgpuRenderPassEncoderSetPipeline(pass, pipeline_);
  wgpuRenderPassEncoderSetBindGroup(pass, 0, bind_group_, 0, nullptr);
  wgpuRenderPassEncoderSetVertexBuffer(pass, 0, vertex_buffer_, 0,
                                       lines_.size() * 2 * sizeof(float) * 6);
  wgpuRenderPassEncoderDraw(pass, (uint32_t)lines_.size() * 2, 1, 0, 0);

  lines_.clear(); // Clear for next frame
}

#endif // !defined(STRIP_ALL)