summaryrefslogtreecommitdiff
path: root/src/gpu
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-16 15:20:39 +0100
committerskal <pascal.massimino@gmail.com>2026-02-16 15:20:39 +0100
commit1c9bc4abd5ab90a61e3485fe30ff3c6f9b4b319c (patch)
treeae97a9712411eedc1fee98c134fc54e53de8b047 /src/gpu
parent80d395b8bc0c37778401eb771094c25db7a1b8a4 (diff)
refactor(headless): convert nullptr checks to strippable macros
Added HEADLESS_RETURN_IF_NULL/HEADLESS_RETURN_VAL_IF_NULL macros that compile to no-ops in STRIP_ALL/FINAL_STRIP modes. Files updated: - fatal_error.h: New headless check macros - sequence.cc: NodeRegistry::create_texture - post_process_helper.cc: Pipeline creation functions - sampler_cache.h: SamplerCache::get_or_create - bind_group_builder.h: Layout/group builders - pipeline_builder.h: Shader and pipeline builders - All effect constructors (7 files) Headless tests passing. STRIP_ALL builds will have zero overhead. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/gpu')
-rw-r--r--src/gpu/bind_group_builder.h13
-rw-r--r--src/gpu/pipeline_builder.h28
-rw-r--r--src/gpu/post_process_helper.cc20
-rw-r--r--src/gpu/sampler_cache.h7
-rw-r--r--src/gpu/sequence.cc10
5 files changed, 34 insertions, 44 deletions
diff --git a/src/gpu/bind_group_builder.h b/src/gpu/bind_group_builder.h
index 3b25ba9..49b7ebe 100644
--- a/src/gpu/bind_group_builder.h
+++ b/src/gpu/bind_group_builder.h
@@ -20,6 +20,7 @@ typedef struct WGPUSamplerImpl* WGPUSampler;
typedef uint32_t WGPUShaderStageFlags;
#include "platform/platform.h"
+#include "util/fatal_error.h"
class BindGroupLayoutBuilder {
std::vector<WGPUBindGroupLayoutEntry> entries_;
@@ -93,10 +94,8 @@ class BindGroupLayoutBuilder {
}
WGPUBindGroupLayout build(WGPUDevice device) {
- // Headless mode: skip bind group layout creation
- if (device == nullptr) {
- return nullptr;
- }
+ // Headless mode: skip bind group layout creation (compiled out in STRIP_ALL)
+ HEADLESS_RETURN_VAL_IF_NULL(device, nullptr);
WGPUBindGroupLayoutDescriptor desc{};
desc.entryCount = entries_.size();
desc.entries = entries_.data();
@@ -134,10 +133,8 @@ class BindGroupBuilder {
}
WGPUBindGroup build(WGPUDevice device, WGPUBindGroupLayout layout) {
- // Headless mode: skip bind group creation
- if (device == nullptr) {
- return nullptr;
- }
+ // Headless mode: skip bind group creation (compiled out in STRIP_ALL)
+ HEADLESS_RETURN_VAL_IF_NULL(device, nullptr);
WGPUBindGroupDescriptor desc{};
desc.layout = layout;
desc.entryCount = entries_.size();
diff --git a/src/gpu/pipeline_builder.h b/src/gpu/pipeline_builder.h
index a258b49..937c55f 100644
--- a/src/gpu/pipeline_builder.h
+++ b/src/gpu/pipeline_builder.h
@@ -15,6 +15,7 @@ typedef struct WGPUShaderModuleImpl* WGPUShaderModule;
#include "gpu/shader_composer.h"
#include "platform/platform.h"
+#include "util/fatal_error.h"
class RenderPipelineBuilder {
WGPUDevice device_;
@@ -38,17 +39,18 @@ class RenderPipelineBuilder {
RenderPipelineBuilder& shader(const char* wgsl, bool compose = true) {
shader_text_ = compose ? ShaderComposer::Get().Compose({}, wgsl) : wgsl;
- // Headless mode: skip shader module creation
- if (device_ != nullptr) {
- WGPUShaderSourceWGSL wgsl_src{};
- wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL;
- wgsl_src.code = str_view(shader_text_.c_str());
- WGPUShaderModuleDescriptor shader_desc{};
- shader_desc.nextInChain = &wgsl_src.chain;
- shader_module_ = wgpuDeviceCreateShaderModule(device_, &shader_desc);
- desc_.vertex.module = shader_module_;
- desc_.vertex.entryPoint = str_view("vs_main");
+ // Headless mode: skip shader module creation (compiled out in STRIP_ALL)
+ if (device_ == nullptr) {
+ return *this;
}
+ WGPUShaderSourceWGSL wgsl_src{};
+ wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL;
+ wgsl_src.code = str_view(shader_text_.c_str());
+ WGPUShaderModuleDescriptor shader_desc{};
+ shader_desc.nextInChain = &wgsl_src.chain;
+ shader_module_ = wgpuDeviceCreateShaderModule(device_, &shader_desc);
+ desc_.vertex.module = shader_module_;
+ desc_.vertex.entryPoint = str_view("vs_main");
return *this;
}
@@ -88,10 +90,8 @@ class RenderPipelineBuilder {
}
WGPURenderPipeline build() {
- // Headless mode: skip pipeline creation
- if (device_ == nullptr) {
- return nullptr;
- }
+ // Headless mode: skip pipeline creation (compiled out in STRIP_ALL)
+ HEADLESS_RETURN_VAL_IF_NULL(device_, nullptr);
color_.writeMask = WGPUColorWriteMask_All;
if (has_blend_)
diff --git a/src/gpu/post_process_helper.cc b/src/gpu/post_process_helper.cc
index 6026f6e..281fde6 100644
--- a/src/gpu/post_process_helper.cc
+++ b/src/gpu/post_process_helper.cc
@@ -8,16 +8,15 @@
#include "gpu/pipeline_builder.h"
#include "gpu/sampler_cache.h"
#include "gpu/shader_composer.h"
+#include "util/fatal_error.h"
#include <cstring>
// Helper to create a standard post-processing pipeline
WGPURenderPipeline create_post_process_pipeline(WGPUDevice device,
WGPUTextureFormat format,
const char* shader_code) {
- // Headless mode: skip pipeline creation
- if (device == nullptr) {
- return nullptr;
- }
+ // Headless mode: skip pipeline creation (compiled out in STRIP_ALL)
+ HEADLESS_RETURN_VAL_IF_NULL(device, nullptr);
WGPUBindGroupLayout bgl =
BindGroupLayoutBuilder()
@@ -45,10 +44,8 @@ WGPURenderPipeline create_post_process_pipeline(WGPUDevice device,
WGPURenderPipeline create_post_process_pipeline_simple(WGPUDevice device,
WGPUTextureFormat format,
const char* shader_code) {
- // Headless mode: skip pipeline creation
- if (device == nullptr) {
- return nullptr;
- }
+ // Headless mode: skip pipeline creation (compiled out in STRIP_ALL)
+ HEADLESS_RETURN_VAL_IF_NULL(device, nullptr);
WGPUBindGroupLayout bgl =
BindGroupLayoutBuilder()
@@ -77,10 +74,9 @@ static GpuBuffer g_dummy_buffer = {nullptr, 0};
void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline,
WGPUBindGroup* bind_group, WGPUTextureView input_view,
GpuBuffer uniforms, GpuBuffer effect_params) {
- // Headless mode: skip bind group creation
- if (device == nullptr || pipeline == nullptr) {
- return;
- }
+ // Headless mode: skip bind group creation (compiled out in STRIP_ALL)
+ HEADLESS_RETURN_IF_NULL(device);
+ HEADLESS_RETURN_IF_NULL(pipeline);
if (!g_dummy_buffer.buffer) {
g_dummy_buffer = gpu_create_buffer(device, 32, WGPUBufferUsage_Uniform);
diff --git a/src/gpu/sampler_cache.h b/src/gpu/sampler_cache.h
index b3fdb19..7d88109 100644
--- a/src/gpu/sampler_cache.h
+++ b/src/gpu/sampler_cache.h
@@ -9,6 +9,7 @@ struct WGPUSamplerImpl;
typedef struct WGPUSamplerImpl* WGPUSampler;
#include "platform/platform.h"
+#include "util/fatal_error.h"
struct SamplerSpec {
WGPUAddressMode u, v;
@@ -39,10 +40,8 @@ class SamplerCache {
}
WGPUSampler get_or_create(WGPUDevice device, const SamplerSpec& spec) {
- // Headless mode: skip sampler creation
- if (device == nullptr) {
- return nullptr;
- }
+ // Headless mode: skip sampler creation (compiled out in STRIP_ALL)
+ HEADLESS_RETURN_VAL_IF_NULL(device, nullptr);
auto it = cache_.find(spec);
if (it != cache_.end())
diff --git a/src/gpu/sequence.cc b/src/gpu/sequence.cc
index 578fc01..62c2933 100644
--- a/src/gpu/sequence.cc
+++ b/src/gpu/sequence.cc
@@ -130,12 +130,10 @@ void NodeRegistry::set_external_view(const std::string& name,
}
void NodeRegistry::create_texture(Node& node) {
- // Headless mode: skip texture creation
- if (device_ == nullptr) {
- node.texture = nullptr;
- node.view = nullptr;
- return;
- }
+ // Headless mode: skip texture creation (compiled out in STRIP_ALL)
+ node.texture = nullptr;
+ node.view = nullptr;
+ HEADLESS_RETURN_IF_NULL(device_);
WGPUTextureFormat format;
WGPUTextureUsage usage;