summaryrefslogtreecommitdiff
path: root/src/gpu
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-06 02:14:30 +0100
committerskal <pascal.massimino@gmail.com>2026-02-06 02:14:30 +0100
commit75bdd2f8c17169422d175ed2d9dfceb9acb0fe77 (patch)
tree9a08a9cc1527c36d10cad71b34ab8ffa64c9cef0 /src/gpu
parentae4b03ef6f5ef07dcc80affd6877d17fceee7d29 (diff)
refactor(gpu): Implement compile-time BVH toggle via shader composition
Completed Task #18-B optimization and refactoring. - Replaced runtime branching in shader with compile-time snippet substitution in ShaderComposer. - Added 'scene_query_bvh.wgsl' and 'scene_query_linear.wgsl' as distinct snippets. - Refactored Renderer3D to manage two separate pipelines (with and without BVH). - Updated ShaderComposer to support snippet substitution during composition. - Verified both paths with test_3d_render (default and --no-bvh). - Removed temporary shader hacks and cleaned up renderer_3d.wgsl.
Diffstat (limited to 'src/gpu')
-rw-r--r--src/gpu/effects/shader_composer.cc33
-rw-r--r--src/gpu/effects/shader_composer.h9
-rw-r--r--src/gpu/effects/shaders.cc6
3 files changed, 35 insertions, 13 deletions
diff --git a/src/gpu/effects/shader_composer.cc b/src/gpu/effects/shader_composer.cc
index 8d66ad7..055b996 100644
--- a/src/gpu/effects/shader_composer.cc
+++ b/src/gpu/effects/shader_composer.cc
@@ -17,7 +17,8 @@ void ShaderComposer::RegisterSnippet(const std::string& name,
void ShaderComposer::ResolveRecursive(const std::string& source,
std::stringstream& ss,
- std::set<std::string>& included) {
+ std::set<std::string>& included,
+ const CompositionMap& substitutions) {
std::istringstream stream(source);
std::string line;
while (std::getline(stream, line)) {
@@ -27,12 +28,19 @@ void ShaderComposer::ResolveRecursive(const std::string& source,
size_t end = line.find('"', start + 1);
if (start != std::string::npos && end != std::string::npos) {
std::string name = line.substr(start + 1, end - start - 1);
+
+ // Apply substitution if available
+ auto sub_it = substitutions.find(name);
+ if (sub_it != substitutions.end()) {
+ name = sub_it->second;
+ }
+
if (included.find(name) == included.end()) {
included.insert(name);
auto it = snippets_.find(name);
if (it != snippets_.end()) {
ss << "// --- Included: " << name << " ---\n";
- ResolveRecursive(it->second, ss, included);
+ ResolveRecursive(it->second, ss, included, substitutions);
ss << "// --- End Include: " << name << " ---\n";
} else {
ss << "// ERROR: Snippet not found: " << name << "\n";
@@ -47,7 +55,8 @@ void ShaderComposer::ResolveRecursive(const std::string& source,
std::string
ShaderComposer::Compose(const std::vector<std::string>& dependencies,
- const std::string& main_code) {
+ const std::string& main_code,
+ const CompositionMap& substitutions) {
std::stringstream ss;
ss << "// Generated by ShaderComposer\n\n";
@@ -55,19 +64,25 @@ ShaderComposer::Compose(const std::vector<std::string>& dependencies,
// Process explicit dependencies first
for (const auto& dep : dependencies) {
- if (included.find(dep) == included.end()) {
- included.insert(dep);
- auto it = snippets_.find(dep);
+ std::string name = dep;
+ auto sub_it = substitutions.find(name);
+ if (sub_it != substitutions.end()) {
+ name = sub_it->second;
+ }
+
+ if (included.find(name) == included.end()) {
+ included.insert(name);
+ auto it = snippets_.find(name);
if (it != snippets_.end()) {
- ss << "// --- Dependency: " << dep << " ---\n";
- ResolveRecursive(it->second, ss, included);
+ ss << "// --- Dependency: " << name << " ---\n";
+ ResolveRecursive(it->second, ss, included, substitutions);
ss << "\n";
}
}
}
ss << "// --- Main Code ---\n";
- ResolveRecursive(main_code, ss, included);
+ ResolveRecursive(main_code, ss, included, substitutions);
return ss.str();
}
diff --git a/src/gpu/effects/shader_composer.h b/src/gpu/effects/shader_composer.h
index a63a6a4..9eb43f4 100644
--- a/src/gpu/effects/shader_composer.h
+++ b/src/gpu/effects/shader_composer.h
@@ -15,16 +15,21 @@ class ShaderComposer {
// Register a snippet (e.g. "common_math", "sdf_primitives")
void RegisterSnippet(const std::string& name, const std::string& code);
+ using CompositionMap = std::map<std::string, std::string>;
+
// Assemble a final shader string by prepending required snippets
// and recursively resolving #include "snippet_name" directives.
+ // Optional substitutions: map "placeholder_name" -> "actual_snippet_name"
std::string Compose(const std::vector<std::string>& dependencies,
- const std::string& main_code);
+ const std::string& main_code,
+ const CompositionMap& substitutions = {});
private:
ShaderComposer() = default;
void ResolveRecursive(const std::string& source, std::stringstream& ss,
- std::set<std::string>& included);
+ std::set<std::string>& included,
+ const CompositionMap& substitutions);
std::map<std::string, std::string> snippets_;
};
diff --git a/src/gpu/effects/shaders.cc b/src/gpu/effects/shaders.cc
index 7b543e1..4fc8108 100644
--- a/src/gpu/effects/shaders.cc
+++ b/src/gpu/effects/shaders.cc
@@ -34,8 +34,10 @@ void InitShaderComposer() {
register_if_exists("math/sdf_shapes", AssetId::ASSET_SHADER_MATH_SDF_SHAPES);
register_if_exists("math/sdf_utils", AssetId::ASSET_SHADER_MATH_SDF_UTILS);
register_if_exists("render/shadows", AssetId::ASSET_SHADER_RENDER_SHADOWS);
- register_if_exists("render/scene_query",
- AssetId::ASSET_SHADER_RENDER_SCENE_QUERY);
+ register_if_exists("render/scene_query_bvh",
+ AssetId::ASSET_SHADER_RENDER_SCENE_QUERY_BVH);
+ register_if_exists("render/scene_query_linear",
+ AssetId::ASSET_SHADER_RENDER_SCENE_QUERY_LINEAR);
register_if_exists("render/lighting_utils",
AssetId::ASSET_SHADER_RENDER_LIGHTING_UTILS);