Debian Codegraph @

shaders/src/getters.fs

raw
1 #if defined(HAS_ATTRIBUTE_COLOR)
2 /** @public-api */
3 vec4 getColor() {
4 return vertex_color;
5 }
6 #endif
7
8 #if defined(HAS_ATTRIBUTE_UV0)
9 /** @public-api */
10 highp vec2 getUV0() {
11 return vertex_uv01.xy;
12 }
13 #endif
14
15 #if defined(HAS_ATTRIBUTE_UV1)
16 /** @public-api */
17 highp vec2 getUV1() {
18 return vertex_uv01.zw;
19 }
20 #endif
21
22 #if defined(BLEND_MODE_MASKED)
23 /** @public-api */
24 float getMaskThreshold() {
25 return materialParams._maskThreshold;
26 }
27 #endif
28
29 /** @public-api */
30 highp mat3 getWorldTangentFrame() {
31 return shading_tangentToWorld;
32 }
33
34 /** @public-api */
35 highp vec3 getWorldPosition() {
36 return shading_position;
37 }
38
39 /** @public-api */
40 vec3 getWorldViewVector() {
41 return shading_view;
42 }
43
44 /** @public-api */
45 vec3 getWorldNormalVector() {
46 return shading_normal;
47 }
48
49 /** @public-api */
50 vec3 getWorldGeometricNormalVector() {
51 return shading_geometricNormal;
52 }
53
54 /** @public-api */
55 vec3 getWorldReflectedVector() {
56 return shading_reflected;
57 }
58
59 /** @public-api */
60 float getNdotV() {
61 return shading_NoV;
62 }
63
64 /**
65 * Transforms a texture UV to make it suitable for a render target attachment.
66 *
67 * In Vulkan and Metal, texture coords are Y-down but in OpenGL they are Y-up. This wrapper function
68 * accounts for these differences. When sampling from non-render targets (i.e. uploaded textures)
69 * these differences do not matter because OpenGL has a second piece of backwardness, which is that
70 * the first row of texels in glTexImage2D is interpreted as the bottom row.
71 *
72 * To protect users from these differences, we recommend that materials in the SURFACE domain
73 * leverage this wrapper function when sampling from offscreen render targets.
74 *
75 * @public-api
76 */
77 highp vec2 uvToRenderTargetUV(highp vec2 uv) {
78 #if defined(TARGET_METAL_ENVIRONMENT) || defined(TARGET_VULKAN_ENVIRONMENT)
79 uv.y = 1.0 - uv.y;
80 #endif
81 return uv;
82 }
83
84 #if defined(HAS_SHADOWING) && defined(HAS_DIRECTIONAL_LIGHTING)
85 highp vec3 getLightSpacePosition() {
86 #if defined(HAS_VSM)
87 // For VSM, do not project the Z coordinate. It remains as linear Z in light space.
88 // See the computeVsmLightSpaceMatrix comments in ShadowMap.cpp.
89 return vec3(vertex_lightSpacePosition.xy * (1.0 / vertex_lightSpacePosition.w),
90 vertex_lightSpacePosition.z);
91 #else
92 return vertex_lightSpacePosition.xyz * (1.0 / vertex_lightSpacePosition.w);
93 #endif
94 }
95 #endif
96
97 /**
98 * Returns the normalized [0, 1] viewport coordinates with the origin at the viewport's bottom-left.
99 * Z coordinate is in the [0, 1] range as well.
100 *
101 * @public-api
102 */
103 highp vec3 getNormalizedViewportCoord() {
104 // make sure to handle our reversed-z
105 return vec3(shading_normalizedViewportCoord, 1.0 - gl_FragCoord.z);
106 }
107
108 #if defined(HAS_SHADOWING) && defined(HAS_DYNAMIC_LIGHTING)
109 highp vec3 getSpotLightSpacePosition(uint index) {
110 highp vec4 position = vertex_spotLightSpacePosition[index];
111 #if defined(HAS_VSM)
112 // For VSM, do not project the Z coordinate. It remains as linear Z in light space.
113 // See the computeVsmLightSpaceMatrix comments in ShadowMap.cpp.
114 return vec3(position.xy * (1.0 / position.w), position.z);
115 #else
116 return position.xyz * (1.0 / position.w);
117 #endif
118 }
119 #endif
120
121 #if defined(MATERIAL_HAS_DOUBLE_SIDED_CAPABILITY)
122 bool isDoubleSided() {
123 return materialParams._doubleSided;
124 }
125 #endif
126
127 /**
128 * Returns the cascade index for this fragment (between 0 and CONFIG_MAX_SHADOW_CASCADES - 1).
129 */
130 uint getShadowCascade() {
131 vec3 viewPos = mulMat4x4Float3(getViewFromWorldMatrix(), getWorldPosition()).xyz;
132 bvec4 greaterZ = greaterThan(frameUniforms.cascadeSplits, vec4(viewPos.z));
133 uint cascadeCount = frameUniforms.cascades & 0xFu;
134 return clamp(uint(dot(vec4(greaterZ), vec4(1.0))), 0u, cascadeCount - 1u);
135 }
136
137 #if defined(HAS_SHADOWING) && defined(HAS_DIRECTIONAL_LIGHTING)
138
139 highp vec3 getCascadeLightSpacePosition(uint cascade) {
140 // For the first cascade, return the interpolated light space position.
141 // This branch will be coherent (mostly) for neighboring fragments, and it's worth avoiding
142 // the matrix multiply inside computeLightSpacePosition.
143 if (cascade == 0u) {
144 return getLightSpacePosition();
145 }
146
147 highp vec4 pos = computeLightSpacePosition(getWorldPosition(), getWorldNormalVector(),
148 frameUniforms.lightDirection, frameUniforms.shadowBias.y,
149 frameUniforms.lightFromWorldMatrix[cascade]);
150 #if defined(HAS_VSM)
151 // For VSM, do not project the Z coordinate. It remains as linear Z in light space.
152 // See the computeVsmLightSpaceMatrix comments in ShadowMap.cpp.
153 return vec3(pos.xy * (1.0 / pos.w), pos.z);
154 #else
155 return pos.xyz * (1.0 / pos.w);
156 #endif
157 }
158
159 #endif