summaryrefslogtreecommitdiff
path: root/src/3d/visual_debug.h
blob: a58d913a7b1269ccf0a8ff64791c1e4fcfee7503 (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
// This file is part of the 64k demo project.
// It provides visual debugging tools, such as wireframe rendering.
// This entire module is excluded when STRIP_ALL is defined.

#pragma once

#if !defined(STRIP_ALL)

#include "gpu/gpu.h"
#include "util/mini_math.h"
#include <vector>

struct DebugLine {
  vec3 start;
  vec3 end;
  vec3 color;
};

class VisualDebug {
 public:
  void init(WGPUDevice device, WGPUTextureFormat format);
  void shutdown();

  // Queue a wireframe box for rendering this frame
  void add_box(const mat4& transform, const vec3& color);

  // Render all queued primitives and clear the queue
  void render(WGPURenderPassEncoder pass, const mat4& view_proj);

 private:
  WGPUDevice device_ = nullptr;
  WGPURenderPipeline pipeline_ = nullptr;
  WGPUBindGroupLayout bind_group_layout_ = nullptr;

  std::vector<DebugLine> lines_;

  // Uniform buffer for ViewProjection matrix
  WGPUBuffer uniform_buffer_ = nullptr;
  WGPUBindGroup bind_group_ = nullptr;

  // Vertex buffer for line segments
  WGPUBuffer vertex_buffer_ = nullptr;
  size_t vertex_buffer_capacity_ = 0;

  void create_pipeline(WGPUTextureFormat format);
  void update_buffers(const mat4& view_proj);
};

#endif // !defined(STRIP_ALL)