summaryrefslogtreecommitdiff
path: root/src/gpu/gpu.h
blob: 0bc20b7151db730ff9ea2851d66181dd0f586551 (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
// This file is part of the 64k demo project.
// It defines the public interface for the GPU rendering system.
// Coordinates WebGPU lifecycle and draw calls.

#pragma once

#include <webgpu.h>

#include <cstring> // For strlen



#if defined(DEMO_CROSS_COMPILE_WIN32)

// Windows (MinGW) using wgpu-native v0.19.4.1

#include <webgpu/webgpu.h>

#include <webgpu/wgpu.h>



static inline const char *str_view(const char *str) { return str; }



static inline const char *label_view(const char *str) { return str; }







#define WGPUSType_ShaderSourceWGSL WGPUSType_ShaderModuleWGSLDescriptor



using WGPUShaderSourceWGSL = WGPUShaderModuleWGSLDescriptor;



#define WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal WGPUSurfaceGetCurrentTextureStatus_Success



#define WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal WGPUSurfaceGetCurrentTextureStatus_Success



#define WGPUCallbackMode_WaitAnyOnly 0





#else

// Native (macOS/Linux) using newer wgpu-native



#include <webgpu.h>

#include <wgpu.h>



static inline WGPUStringView str_view(const char *str) {

  if (!str) return {nullptr, 0};

  return {str, strlen(str)};

}



static inline WGPUStringView label_view(const char *str) {

#ifndef STRIP_ALL

  if (!str) return {nullptr, 0};

  return {str, strlen(str)};

#else

  (void)str;

  return {nullptr, 0};

#endif

}



#endif



struct GLFWwindow;

// Basic wrapper for WebGPU buffers
struct GpuBuffer {
  WGPUBuffer buffer;
  size_t size;
};

// Encapsulates a compute operation
struct ComputePass {
  WGPUComputePipeline pipeline;
  WGPUBindGroup bind_group;
  uint32_t workgroup_size_x;
  uint32_t workgroup_size_y;
  uint32_t workgroup_size_z;
};

// Encapsulates a render operation
struct RenderPass {
  WGPURenderPipeline pipeline;
  WGPUBindGroup bind_group;
  uint32_t vertex_count;
  uint32_t instance_count;
};

void gpu_init(GLFWwindow *window, int width, int height);
void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat);
#ifndef STRIP_ALL
void gpu_simulate_until(float time);
#endif
void gpu_shutdown();

// Helper functions (exposed for internal/future use)
struct ResourceBinding {
  GpuBuffer buffer;
  WGPUBufferBindingType type; // e.g., WGPUBufferBindingType_Uniform,
                              // WGPUBufferBindingType_Storage
};

GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage,
                            const void *data = nullptr);
ComputePass gpu_create_compute_pass(WGPUDevice device, const char *shader_code,
                                    ResourceBinding *bindings,
                                    int num_bindings);
RenderPass
gpu_create_render_pass(WGPUDevice device,
                       WGPUTextureFormat format, // Needed for render pipeline
                       const char *shader_code, ResourceBinding *bindings,
                       int num_bindings);