summaryrefslogtreecommitdiff
path: root/src/gpu/gpu.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/gpu/gpu.cc')
-rw-r--r--src/gpu/gpu.cc71
1 files changed, 30 insertions, 41 deletions
diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc
index 0be5afe..f5f1515 100644
--- a/src/gpu/gpu.cc
+++ b/src/gpu/gpu.cc
@@ -5,6 +5,7 @@
#include "gpu.h"
#include "effects/shaders.h"
#include "generated/timeline.h"
+#include "gpu/sampler_cache.h"
#include "gpu/shader_composer.h"
#include "platform/platform.h"
@@ -124,26 +125,28 @@ WGPUTextureView gpu_create_texture_view_2d(WGPUTexture texture,
return wgpuTextureCreateView(texture, &view_desc);
}
+WGPUShaderModule gpu_create_shader_module(WGPUDevice device,
+ const char* wgsl_code,
+ const char* label) {
+ WGPUShaderSourceWGSL wgsl_src = {};
+ wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL;
+ wgsl_src.code = str_view(wgsl_code);
+ WGPUShaderModuleDescriptor desc = {};
+ desc.label = label_view(label);
+ desc.nextInChain = &wgsl_src.chain;
+ return wgpuDeviceCreateShaderModule(device, &desc);
+}
+
WGPUSampler gpu_create_linear_sampler(WGPUDevice device) {
- WGPUSamplerDescriptor desc = {};
- desc.addressModeU = WGPUAddressMode_ClampToEdge;
- desc.addressModeV = WGPUAddressMode_ClampToEdge;
- desc.addressModeW = WGPUAddressMode_ClampToEdge;
- desc.magFilter = WGPUFilterMode_Linear;
- desc.minFilter = WGPUFilterMode_Linear;
- desc.mipmapFilter = WGPUMipmapFilterMode_Nearest;
- desc.maxAnisotropy = 1;
- return wgpuDeviceCreateSampler(device, &desc);
+ WGPUSampler s = SamplerCache::Get().get_or_create(device, SamplerCache::clamp());
+ if (s) wgpuSamplerAddRef(s); // Caller owns a reference (for RAII wrappers)
+ return s;
}
WGPUSampler gpu_create_nearest_sampler(WGPUDevice device) {
- WGPUSamplerDescriptor desc = {};
- desc.addressModeU = WGPUAddressMode_ClampToEdge;
- desc.addressModeV = WGPUAddressMode_ClampToEdge;
- desc.magFilter = WGPUFilterMode_Nearest;
- desc.minFilter = WGPUFilterMode_Nearest;
- desc.maxAnisotropy = 1;
- return wgpuDeviceCreateSampler(device, &desc);
+ WGPUSampler s = SamplerCache::Get().get_or_create(device, SamplerCache::nearest());
+ if (s) wgpuSamplerAddRef(s); // Caller owns a reference (for RAII wrappers)
+ return s;
}
TextureWithView gpu_create_dummy_scene_texture(WGPUDevice device) {
@@ -167,15 +170,8 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format,
// Compose shader to resolve #include directives
std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code);
- // Create Shader Module
- WGPUShaderSourceWGSL wgsl_src = {};
- wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL;
- wgsl_src.code = str_view(composed_shader.c_str());
- WGPUShaderModuleDescriptor shader_desc = {};
- shader_desc.label = label_view("render_shader");
- shader_desc.nextInChain = &wgsl_src.chain;
WGPUShaderModule shader_module =
- wgpuDeviceCreateShaderModule(device, &shader_desc);
+ gpu_create_shader_module(device, composed_shader.c_str(), "render_shader");
// Create Bind Group Layout & Bind Group
std::vector<WGPUBindGroupLayoutEntry> bgl_entries;
@@ -257,6 +253,10 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format,
pass.pipeline = wgpuDeviceCreateRenderPipeline(device, &pipeline_desc);
+ wgpuShaderModuleRelease(shader_module);
+ wgpuPipelineLayoutRelease(pipeline_layout);
+ wgpuBindGroupLayoutRelease(bind_group_layout);
+
return pass;
}
@@ -268,14 +268,8 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code,
// Compose shader to resolve #include directives
std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code);
- WGPUShaderSourceWGSL wgsl_src = {};
- wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL;
- wgsl_src.code = str_view(composed_shader.c_str());
- WGPUShaderModuleDescriptor shader_desc = {};
- shader_desc.label = label_view("compute_shader");
- shader_desc.nextInChain = &wgsl_src.chain;
WGPUShaderModule shader_module =
- wgpuDeviceCreateShaderModule(device, &shader_desc);
+ gpu_create_shader_module(device, composed_shader.c_str(), "compute_shader");
std::vector<WGPUBindGroupLayoutEntry> bgl_entries;
std::vector<WGPUBindGroupEntry> bg_entries;
@@ -319,6 +313,11 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code,
pipeline_desc.compute.entryPoint = str_view("main");
pass.pipeline = wgpuDeviceCreateComputePipeline(device, &pipeline_desc);
+
+ wgpuShaderModuleRelease(shader_module);
+ wgpuPipelineLayoutRelease(pipeline_layout);
+ wgpuBindGroupLayoutRelease(bind_group_layout);
+
return pass;
}
@@ -439,16 +438,6 @@ void gpu_init(PlatformState* platform_state) {
}
-void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat_time,
- float beat_phase) {
- // Rendering is driven externally via the sequence pipeline
- (void)audio_peak;
- (void)aspect_ratio;
- (void)time;
- (void)beat_time;
- (void)beat_phase;
-}
-
void gpu_resize(int width, int height) {
if (width <= 0 || height <= 0)
return;