summaryrefslogtreecommitdiff
path: root/src/3d/renderer.h
blob: e87d47a6f34d6d471379a3c4d86e89f0fd6741ec (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
// 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>

// Matches the GPU struct layout
struct GlobalUniforms {
  mat4 view_proj;
  vec3 camera_pos;
  float time;
};

// Matches the GPU struct layout
struct ObjectData {
  mat4 model;
  vec4 color;
  vec4 params; // Type, etc.
};

class Renderer3D {
 public:
  void init(WGPUDevice device, WGPUQueue queue, WGPUTextureFormat format);
  void shutdown();

    // 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);

  // Resize handler (if needed for internal buffers)
  void resize(int width, int height);

 private:
  void create_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;
  WGPUBuffer global_uniform_buffer_ = nullptr;
  WGPUBuffer object_storage_buffer_ = nullptr;

  WGPUTextureView noise_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;
};