VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
programLibrary.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 11.10.2025.
5//
6#pragma once
7
8#include <unordered_map>
9#include <unordered_set>
10#include <string>
11#include <vector>
12
16
17namespace visutwin::canvas
18{
26 {
27 public:
28 ProgramLibrary(const std::shared_ptr<GraphicsDevice>& device, StandardMaterial* standardMaterial);
29
30 void registerProgram(const std::string& name, const std::vector<std::string>& chunkOrder);
31 bool hasProgram(const std::string& name) const;
32
33 std::shared_ptr<Shader> getForwardShader(const Material* material, bool transparentPass,
34 bool dynamicBatch = false);
35 std::shared_ptr<Shader> getShadowShader(bool dynamicBatch = false);
36
37 void bindMaterial(const std::shared_ptr<GraphicsDevice>& device, const Material* material, bool transparentPass,
38 bool dynamicBatch = false);
39
40 // set whether a skybox cubemap is available.
41 // When true, skybox materials compile with VT_FEATURE_SKY_CUBEMAP.
42 void setSkyCubemapAvailable(bool value) { _skyCubemapAvailable = value; }
43
44 // when true, all forward shaders compile with
45 // VT_FEATURE_PLANAR_REFLECTION_DEPTH_PASS, overriding fragment output
46 // to emit distance-from-reflection-plane. Set per-camera by the renderer.
47 void setPlanarReflectionDepthPass(bool value) { _planarReflectionDepthPass = value; }
48
49 // Set when any local light (spot/point) has castShadows enabled.
50 // When true, forward shaders compile with VT_FEATURE_LOCAL_SHADOWS.
51 void setLocalShadowsEnabled(bool value) { _localShadowsEnabled = value; }
52
53 // Set when any omni light has castShadows enabled.
54 // When true, forward shaders compile with VT_FEATURE_OMNI_SHADOWS.
55 void setOmniShadowsEnabled(bool value) { _omniShadowsEnabled = value; }
56
57 // Set when clustered lighting is enabled on the scene.
58 // When true, forward shaders compile with VT_FEATURE_LIGHT_CLUSTERING.
59 void setClusteredLightingEnabled(bool value) { _clusteredLightingEnabled = value; }
60
61 // Set when any area rect light is active in the scene.
62 // When true, forward shaders compile with VT_FEATURE_AREA_LIGHTS.
63 void setAreaLightsEnabled(bool value) { _areaLightsEnabled = value; }
64
65 // Set when SSAO is in per-material lighting mode (SSAOTYPE_LIGHTING).
66 // When true, forward shaders compile with VT_FEATURE_SSAO to sample
67 // the SSAO texture during PBR lighting and modulate ambient occlusion.
68 void setSsaoEnabled(bool value) { _ssaoEnabled = value; }
69
70 // Set when atmosphere scattering is enabled on the scene.
71 // When true, skybox shaders compile with VT_FEATURE_ATMOSPHERE.
72 void setAtmosphereEnabled(bool value) { _atmosphereEnabled = value; }
73
74 private:
75 struct ShaderVariantOptions
76 {
77 bool skybox = false;
78 bool transparentPass = false;
79 bool alphaTest = false;
80 bool doubleSided = false;
81
82 bool baseColorMap = false;
83 bool normalMap = false;
84 bool metallicRoughnessMap = false;
85 bool occlusionMap = false;
86 bool emissiveMap = false;
87 bool envAtlas = true;
88
89 // Feature toggles for future shader chunk ports.
90 bool shadowMapping = false;
91 bool fog = false;
92 bool parallax = false;
93 bool clearcoat = false;
94 bool anisotropy = false;
95 bool sheen = false;
96 bool iridescence = false;
97 bool transmission = false;
98 bool lightClustering = false;
99 bool ssao = false;
100 bool lightProbes = false;
101 bool vertexColors = false;
102 bool skinning = false;
103 bool morphing = false;
104 bool specGloss = false;
105 bool orenNayar = false;
106 bool detailNormals = false;
107 bool displacement = false;
108 bool atmosphere = false;
109 bool pointSpotAttenuation = false;
110 bool multiLight = false;
111 bool shadowCatcher = false;
112 bool skyCubemap = false;
113 bool surfaceLIC = false; // Surface LIC flow visualization (velocity output + LIC compositing)
114 bool instancing = false; // Hardware instancing — per-instance transform + color via [[instance_id]]
115 bool planarReflection = false; // Planar reflection — screen-space UV sampling + Fresnel blend
116 bool planarReflectionDepthPass = false; // Depth pass: output distance-from-plane instead of PBR
117 bool localShadows = false; // Local light (spot/point) shadow mapping — 2D depth textures
118 bool omniShadows = false; // Omni light cubemap shadow mapping — depthcube textures
119 bool dynamicBatch = false; // Dynamic batching — per-vertex bone index + matrix palette
120 bool pointSize = false; // Point primitive rendering — [[point_size]] in vertex output
121 bool areaLights = false; // Area rectangular lights — MRP evaluation in main loop
122 bool unlit = false; // KHR_materials_unlit — skip PBR lighting
123 };
124
125 ShaderVariantOptions buildForwardVariantOptions(const Material* material, bool transparentPass,
126 bool dynamicBatch = false) const;
127 static std::string resolveProgramName(const ShaderVariantOptions& options);
128 uint64_t makeVariantKey(const std::string& programName, const ShaderVariantOptions& options, const Material* material) const;
129 std::shared_ptr<Shader> buildForwardShaderVariant(const std::string& programName, const ShaderVariantOptions& options, uint64_t variantKey);
130
131 std::string composeProgramVariantMetalSource(const std::string& programName, const ShaderVariantOptions& options,
132 const std::string& vertexEntry, const std::string& fragmentEntry);
133
134 std::shared_ptr<GraphicsDevice> _device;
135 std::unordered_map<uint64_t, std::shared_ptr<Shader>> _forwardShaderCache;
136 std::unordered_set<std::string> _warnedFeatureFlags;
137 std::unordered_map<std::string, std::vector<std::string>> _registeredPrograms;
138 bool _skyCubemapAvailable = false;
139 bool _planarReflectionDepthPass = false;
140 bool _localShadowsEnabled = false;
141 bool _omniShadowsEnabled = false;
142 bool _clusteredLightingEnabled = false;
143 bool _areaLightsEnabled = false;
144 bool _ssaoEnabled = false;
145 bool _atmosphereEnabled = false;
146 };
147
148 // Assigns the program library to the device cache.
149 void setProgramLibrary(const std::shared_ptr<GraphicsDevice>& device, const std::shared_ptr<ProgramLibrary>& library);
150 std::shared_ptr<ProgramLibrary> getProgramLibrary(const std::shared_ptr<GraphicsDevice>& device);
151}
Base class for GPU materials — owns uniform data, texture bindings, blend/depth state,...
Definition material.h:143
void setClusteredLightingEnabled(bool value)
bool hasProgram(const std::string &name) const
void setPlanarReflectionDepthPass(bool value)
void registerProgram(const std::string &name, const std::vector< std::string > &chunkOrder)
std::shared_ptr< Shader > getForwardShader(const Material *material, bool transparentPass, bool dynamicBatch=false)
ProgramLibrary(const std::shared_ptr< GraphicsDevice > &device, StandardMaterial *standardMaterial)
void setLocalShadowsEnabled(bool value)
void setSkyCubemapAvailable(bool value)
void bindMaterial(const std::shared_ptr< GraphicsDevice > &device, const Material *material, bool transparentPass, bool dynamicBatch=false)
std::shared_ptr< Shader > getShadowShader(bool dynamicBatch=false)
Full PBR material with metalness/roughness workflow and advanced surface features.
void setProgramLibrary(const std::shared_ptr< GraphicsDevice > &device, const std::shared_ptr< ProgramLibrary > &library)
std::shared_ptr< ProgramLibrary > getProgramLibrary(const std::shared_ptr< GraphicsDevice > &device)