summaryrefslogtreecommitdiff
path: root/src/tests/test_3d_render.cc
blob: 41bffe6e3bdd3900e7435c5111888ec4bc8bf8cb (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
// This file is part of the 64k demo project.
// Standalone "mini-demo" for testing the 3D renderer.

#include "3d/camera.h"
#include "3d/object.h"
#include "3d/renderer.h"
#include "3d/scene.h"
#include "platform.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>

#if defined(DEMO_CROSS_COMPILE_WIN32)
#include <webgpu/webgpu.h>
#else
#include <webgpu.h>
#endif

// Global State
static Renderer3D g_renderer;
static Scene g_scene;
static Camera g_camera;
static WGPUDevice g_device = nullptr;
static WGPUQueue g_queue = nullptr;
static WGPUSurface g_surface = nullptr;
static WGPUAdapter g_adapter = nullptr;
static WGPUTextureFormat g_format = WGPUTextureFormat_Undefined;
static int g_width = 1280;
static int g_height = 720;

// Reimplementing basic WebGPU init here
void init_wgpu() {
  WGPUInstance instance = wgpuCreateInstance(nullptr);
  if (!instance) {
      std::cerr << "Failed to create WGPU instance." << std::endl;
      exit(1);
  }
  
  g_surface = platform_create_wgpu_surface(instance);
  if (!g_surface) {
      std::cerr << "Failed to create WGPU surface." << std::endl;
      exit(1);
  }

  WGPURequestAdapterOptions adapter_opts = {};
  adapter_opts.compatibleSurface = g_surface;
  adapter_opts.powerPreference = WGPUPowerPreference_HighPerformance;

#if defined(DEMO_CROSS_COMPILE_WIN32)
  auto on_adapter = [](WGPURequestAdapterStatus status, WGPUAdapter adapter,
                       const char* message, void* userdata) {
      if (status == WGPURequestAdapterStatus_Success) {
          *(WGPUAdapter*)userdata = adapter;
      } else {
          std::cerr << "Adapter Error: " << (message ? message : "null") << std::endl;
      }
  };
  wgpuInstanceRequestAdapter(instance, &adapter_opts, on_adapter, &g_adapter);
#else
  auto on_adapter = [](WGPURequestAdapterStatus status, WGPUAdapter adapter,
                       WGPUStringView message, void* userdata, void* user2) {
      if (status == WGPURequestAdapterStatus_Success) {
          *(WGPUAdapter*)userdata = adapter;
      } else {
          std::cerr << "Adapter Error: " << (message.data ? message.data : "null") << std::endl;
      }
  };
  WGPURequestAdapterCallbackInfo adapter_cb = {};
  adapter_cb.mode = WGPUCallbackMode_WaitAnyOnly;
  adapter_cb.callback = on_adapter;
  adapter_cb.userdata1 = &g_adapter;
  wgpuInstanceRequestAdapter(instance, &adapter_opts, adapter_cb);
#endif

  // Spin wait for adapter
#if !defined(DEMO_CROSS_COMPILE_WIN32)
  while (!g_adapter) {
      wgpuInstanceProcessEvents(instance);
  }
#endif
  
  if (!g_adapter) {
      std::cerr << "Failed to get adapter." << std::endl;
      exit(1);
  }

  WGPUDeviceDescriptor device_desc = {};
  
#if defined(DEMO_CROSS_COMPILE_WIN32)
  auto on_device = [](WGPURequestDeviceStatus status, WGPUDevice device,
                      const char* message, void* userdata) {
      if (status == WGPURequestDeviceStatus_Success) {
          *(WGPUDevice*)userdata = device;
      }
  };
  wgpuAdapterRequestDevice(g_adapter, &device_desc, on_device, &g_device);
#else
  auto on_device = [](WGPURequestDeviceStatus status, WGPUDevice device,
                      WGPUStringView message, void* userdata, void* user2) {
      if (status == WGPURequestDeviceStatus_Success) {
          *(WGPUDevice*)userdata = device;
      }
  };
  WGPURequestDeviceCallbackInfo device_cb = {};
  device_cb.mode = WGPUCallbackMode_WaitAnyOnly;
  device_cb.callback = on_device;
  device_cb.userdata1 = &g_device;
  wgpuAdapterRequestDevice(g_adapter, &device_desc, device_cb);
#endif
  
#if !defined(DEMO_CROSS_COMPILE_WIN32)
  // Poll until device is ready (WaitAny is unimplemented in current wgpu-native build)
  while (!g_device) {
      wgpuInstanceProcessEvents(instance);
  }
#endif

  if (!g_device) {
      std::cerr << "Failed to get device." << std::endl;
      exit(1);
  }
  
  g_queue = wgpuDeviceGetQueue(g_device);

  WGPUSurfaceCapabilities caps = {};
  wgpuSurfaceGetCapabilities(g_surface, g_adapter, &caps);
  g_format = caps.formats[0];

  WGPUSurfaceConfiguration config = {};
  config.device = g_device;
  config.format = g_format;
  config.usage = WGPUTextureUsage_RenderAttachment;
  config.width = g_width;
  config.height = g_height;
  config.presentMode = WGPUPresentMode_Fifo;
  config.alphaMode = WGPUCompositeAlphaMode_Opaque;
  wgpuSurfaceConfigure(g_surface, &config);
}

void setup_scene() {
  g_scene.clear();
  // Center Red Cube
  Object3D center;
  center.position = vec3(0, 0, 0);
  center.color = vec4(1, 0, 0, 1);
  g_scene.add_object(center);

  // Orbiting Green Cubes
  for (int i = 0; i < 8; ++i) {
    Object3D obj;
    float angle = (i / 8.0f) * 6.28318f;
    obj.position = vec3(std::cos(angle) * 4.0f, 0, std::sin(angle) * 4.0f);
    obj.scale = vec3(0.5f, 0.5f, 0.5f);
    obj.color = vec4(0, 1, 0, 1);
    g_scene.add_object(obj);
  }
}

int main() {
  platform_init_window(false);
  
  init_wgpu();
  
  g_renderer.init(g_device, g_queue, g_format);
  g_renderer.resize(g_width, g_height);
  
  setup_scene();
  
  g_camera.position = vec3(0, 5, 10);
  g_camera.target = vec3(0, 0, 0);
  
  float time = 0.0f;
  while (!platform_should_close()) {
    platform_poll();
    
    time += 0.016f; // Approx 60fps
    
    // Animate Objects
    for (size_t i = 1; i < g_scene.objects.size(); ++i) {
        g_scene.objects[i].rotation = quat::from_axis(vec3(0, 1, 0), time * 2.0f + i);
        g_scene.objects[i].position.y = std::sin(time * 3.0f + i) * 1.5f;
    }
    
    // Animate Camera Height and Radius
    float cam_radius = 10.0f + std::sin(time * 0.3f) * 4.0f;
    float cam_height = 5.0f + std::cos(time * 0.4f) * 3.0f;
    g_camera.set_look_at(
        vec3(std::sin(time * 0.5f) * cam_radius, cam_height, std::cos(time * 0.5f) * cam_radius),
        vec3(0, 0, 0),
        vec3(0, 1, 0)
    );
    
    // Render Frame
    WGPUSurfaceTexture surface_tex;
    wgpuSurfaceGetCurrentTexture(g_surface, &surface_tex);
    
    if (surface_tex.status == WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal) {
        WGPUTextureViewDescriptor view_desc = {};
        view_desc.format = g_format;
        view_desc.dimension = WGPUTextureViewDimension_2D;
        view_desc.baseMipLevel = 0;
        view_desc.mipLevelCount = 1;
        view_desc.baseArrayLayer = 0;
        view_desc.arrayLayerCount = 1;
        view_desc.aspect = WGPUTextureAspect_All;
        WGPUTextureView view = wgpuTextureCreateView(surface_tex.texture, &view_desc);
        
        g_renderer.render(g_scene, g_camera, time, view);
        
        wgpuTextureViewRelease(view);
        wgpuSurfacePresent(g_surface);
        wgpuTextureRelease(surface_tex.texture);
    }
    
#if !defined(DEMO_CROSS_COMPILE_WIN32)
    // Poll events for wgpu-native to ensure callbacks fire and frame presents?
    // We don't have easy access to instance here unless we store it globally.
    // Let's just assume Present handles enough synchronization for this demo.
#endif
  }
  
  g_renderer.shutdown();
  platform_shutdown();
  return 0;
}