VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
uniformBinder.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Base interface for uniform packing, ring-buffer allocation, and per-pass deduplication.
5// Backend implementations (Metal, Vulkan) provide concrete GPU submission logic.
6//
7#pragma once
8
9#include <cstdint>
10
11namespace visutwin::canvas
12{
13 class Material;
14 class Texture;
15
22 {
23 public:
24 virtual ~UniformBinder() = default;
25
26 // ---------------------------------------------------------------
27 // GPU-side uniform structs (shared across all backends)
28 // ---------------------------------------------------------------
29
30 struct alignas(16) GpuLightUniform
31 {
32 float positionRange[4] = {0.0f, 0.0f, 0.0f, 0.0f};
33 float directionCone[4] = {0.0f, -1.0f, 0.0f, 1.0f};
34 float colorIntensity[4] = {1.0f, 1.0f, 1.0f, 0.0f};
35 float coneAngles[4] = {1.0f, 1.0f, 0.0f, 0.0f};
36 // [0]=lightType, [1]=castsShadows, [2]=falloffLinear, [3]=localShadowMapIndex
37 uint32_t typeCastShadows[4] = {0u, 0u, 0u, 0u};
38 };
39
40 struct alignas(16) LightingUniforms
41 {
42 float ambientColor[4] = {0.0f, 0.0f, 0.0f, 0.0f};
43 uint32_t lightCountAndFlags[4] = {0u, 0u, 0u, 0u};
44 uint32_t flagsAndPad[4] = {0u, 0u, 0u, 0u};
45 float cameraPositionSkyboxIntensity[4] = {0.0f, 0.0f, 0.0f, 1.0f};
46 float skyboxMipAndPad[4] = {0.0f, 0.0f, 0.0f, 0.0f};
48 float fogColorDensity[4] = {0.0f, 0.0f, 0.0f, 0.0f};
49 float fogStartEndType[4] = {10.0f, 100.0f, 0.0f, 0.0f};
50 float shadowBiasNormalStrength[4] = {0.001f, 0.0f, 1.0f, 0.0f};
51
52 // CSM: 4 cascade VP matrices (viewport-scaled).
53 float shadowMatrixPalette[64] = {};
54 float shadowCascadeDistances[4] = {0.0f, 0.0f, 0.0f, 0.0f};
55 float shadowCascadeParams[4] = {4.0f, 0.0f, 0.0f, 0.0f};
56
57 float skyDomeCenter[4] = {0.0f, 0.0f, 0.0f, 0.0f};
58 float screenInvResolution[4] = {0.0f, 0.0f, 0.0f, 0.0f};
59
60 float reflectionParams[4] = {1.0f, 0.0f, 1.0f, 0.5f};
61 float reflectionFadeColor[4] = {0.5f, 0.5f, 0.5f, 0.0f};
62 float reflectionDepthParams[4] = {0.0f, 10.0f, 0.0f, 0.0f};
63
64 // Local light shadows (spot/point): up to 2 VP matrices + per-light params.
65 float localShadowMatrix0[16] = {};
66 float localShadowMatrix1[16] = {};
67 float localShadowParams0[4] = {0.0001f, 0.0f, 1.0f, 0.0f};
68 float localShadowParams1[4] = {0.0001f, 0.0f, 1.0f, 0.0f};
69
70 // Omni cubemap shadow params.
71 float omniShadowParams0[4] = {0.01f, 100.0f, 0.0001f, 0.0f};
72 float omniShadowParams0Extra[4] = {1.0f, 0.0f, 0.0f, 0.0f};
73 float omniShadowParams1[4] = {0.01f, 100.0f, 0.0001f, 0.0f};
74 float omniShadowParams1Extra[4] = {1.0f, 0.0f, 0.0f, 0.0f};
75
76 // Clustered lighting grid parameters.
77 float clusterBoundsMin[4] = {};
78 float clusterBoundsRange[4] = {};
80 uint32_t clusterParams[4] = {};
81 uint32_t clusterParams2[4] = {};
82 };
83
84 // ---------------------------------------------------------------
85 // Per-pass lifecycle
86 // ---------------------------------------------------------------
87
88 virtual void resetPassState() = 0;
89
90 // ---------------------------------------------------------------
91 // Queries
92 // ---------------------------------------------------------------
93
94 [[nodiscard]] virtual bool isMaterialChanged(const Material* mat) const = 0;
95
96 [[nodiscard]] virtual Texture* envAtlasTexture() const = 0;
97 [[nodiscard]] virtual Texture* skyboxCubeMapTexture() const = 0;
98 [[nodiscard]] virtual Texture* shadowTexture() const = 0;
99 [[nodiscard]] virtual Texture* localShadowTexture0() const = 0;
100 [[nodiscard]] virtual Texture* localShadowTexture1() const = 0;
101 [[nodiscard]] virtual Texture* omniShadowCube0() const = 0;
102 [[nodiscard]] virtual Texture* omniShadowCube1() const = 0;
103
106 struct alignas(16) AtmosphereUniforms
107 {
108 float planetCenterAndRadius[4] = {0.0f, 0.0f, 0.0f, 6371000.0f};
109 float atmosphereRadiusAndSunIntensity[4] = {6471000.0f, 22.0f, 0.9998f, 0.0f};
110 float rayleighCoeffAndScaleHeight[4] = {5.5e-6f, 13.0e-6f, 22.4e-6f, 8500.0f};
111 float mieCoeffAndScaleHeight[4] = {21.0e-6f, 1200.0f, 0.758f, 0.0f};
112 float sunDirection[4] = {0.0f, 1.0f, 0.0f, 0.0f};
113 float cameraAltitudeAndParams[4] = {0.0f, 32.0f, 8.0f, 0.0f};
114 };
115
117 [[nodiscard]] const LightingUniforms& lightingUniforms() const { return _lightingUniforms; }
118
120 [[nodiscard]] const AtmosphereUniforms& atmosphereUniforms() const { return _atmosphereUniforms; }
121
122 protected:
125 };
126}
Base class for GPU materials — owns uniform data, texture bindings, blend/depth state,...
Definition material.h:143
GPU texture resource supporting 2D, cubemap, volume, and array formats with mipmap management.
Definition texture.h:57
virtual Texture * localShadowTexture1() const =0
virtual Texture * skyboxCubeMapTexture() const =0
virtual Texture * omniShadowCube1() const =0
virtual Texture * envAtlasTexture() const =0
virtual bool isMaterialChanged(const Material *mat) const =0
virtual Texture * omniShadowCube0() const =0
virtual Texture * localShadowTexture0() const =0
virtual Texture * shadowTexture() const =0
virtual ~UniformBinder()=default
const AtmosphereUniforms & atmosphereUniforms() const
Access the packed AtmosphereUniforms struct.
const LightingUniforms & lightingUniforms() const
Access the packed LightingUniforms struct (for backends to submit to GPU).
AtmosphereUniforms _atmosphereUniforms