1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef SHADERS_COMMON_3D_DEFAULT_MATERIAL_STRUCTURES_COMMON_H
17 #define SHADERS_COMMON_3D_DEFAULT_MATERIAL_STRUCTURES_COMMON_H
18
19 #include "render/shaders/common/render_compatibility_common.h"
20 #include "render/shaders/common/render_packing_common.h"
21
22 #ifdef VULKAN
23
24 // aligned to 256 with indcies uvec4
25 #define CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT 15
26 #define CORE_MATERIAL_PACKED_UNIFORM_UVEC4_COUNT 15
27
28 #define CORE_MATERIAL_DEFAULT_REFLECTANCE 0.04
29
30 #define CORE3D_HDR_FLOAT_CLAMP_MAX_VALUE 64512.0
31
32 // base color is separated
33 #define CORE_MATERIAL_TEX_NORMAL_IDX 0
34 #define CORE_MATERIAL_TEX_MATERIAL_IDX 1
35 #define CORE_MATERIAL_TEX_EMISSIVE_IDX 2
36 #define CORE_MATERIAL_TEX_AO_IDX 3
37 #define CORE_MATERIAL_TEX_CLEARCOAT_IDX 4
38 #define CORE_MATERIAL_TEX_CLEARCOAT_ROUGHNESS_IDX 5
39 #define CORE_MATERIAL_TEX_CLEARCOAT_NORMAL_IDX 6
40 #define CORE_MATERIAL_TEX_SHEEN_IDX 7
41 #define CORE_MATERIAL_TEX_TRANSMISSION_IDX 8
42 #define CORE_MATERIAL_TEX_SPECULAR_IDX 9
43 #define CORE_MATERIAL_SAMPTEX_COUNT 10
44
45 #define CORE_MATERIAL_TEXCOORD_INFO_SHIFT 16
46
47 #define CORE_MATERIAL_TEXCOORD_INFO_BASE_BIT (1 << 0)
48 #define CORE_MATERIAL_TEXCOORD_INFO_NORMAL_BIT (1 << 1)
49 #define CORE_MATERIAL_TEXCOORD_INFO_MATERIAL_BIT (1 << 2)
50 #define CORE_MATERIAL_TEXCOORD_INFO_EMISSIVE_BIT (1 << 3)
51 #define CORE_MATERIAL_TEXCOORD_INFO_AO_BIT (1 << 4)
52 #define CORE_MATERIAL_TEXCOORD_INFO_CLEARCOAT_BIT (1 << 5)
53 #define CORE_MATERIAL_TEXCOORD_INFO_CLEARCOAT_ROUGHNESS_BIT (1 << 6)
54 #define CORE_MATERIAL_TEXCOORD_INFO_CLEARCOAT_NORMAL_BIT (1 << 7)
55 #define CORE_MATERIAL_TEXCOORD_INFO_SHEEN_BIT (1 << 8)
56 #define CORE_MATERIAL_TEXCOORD_INFO_TRANSMISSION_BIT (1 << 9)
57 #define CORE_MATERIAL_TEXCOORD_INFO_SPECULAR_BIT (1 << 10)
58
59 // factor indices
60 #define CORE_MATERIAL_FACTOR_BASE_IDX 0
61 #define CORE_MATERIAL_FACTOR_NORMAL_IDX 1
62 #define CORE_MATERIAL_FACTOR_MATERIAL_IDX 2
63 #define CORE_MATERIAL_FACTOR_EMISSIVE_IDX 3
64 #define CORE_MATERIAL_FACTOR_AO_IDX 4
65 #define CORE_MATERIAL_FACTOR_CLEARCOAT_IDX 5
66 #define CORE_MATERIAL_FACTOR_CLEARCOAT_ROUGHNESS_IDX 6
67 #define CORE_MATERIAL_FACTOR_CLEARCOAT_NORMAL_IDX 7
68 #define CORE_MATERIAL_FACTOR_SHEEN_IDX 8
69 #define CORE_MATERIAL_FACTOR_TRANSMISSION_IDX 9
70 #define CORE_MATERIAL_FACTOR_SPECULAR_IDX 10
71 #define CORE_MATERIAL_FACTOR_ADDITIONAL_IDX 11
72
73 // packed texture transforms
74 #define CORE_MATERIAL_PACK_TEX_BASE_COLOR_UV_IDX 0
75 #define CORE_MATERIAL_PACK_TEX_NORMAL_UV_IDX 1
76 #define CORE_MATERIAL_PACK_TEX_MATERIAL_UV_IDX 2
77 #define CORE_MATERIAL_PACK_TEX_EMISSIVE_UV_IDX 3
78 #define CORE_MATERIAL_PACK_TEX_AO_UV_IDX 4
79 #define CORE_MATERIAL_PACK_TEX_CLEARCOAT_UV_IDX 5
80 #define CORE_MATERIAL_PACK_TEX_CLEARCOAT_ROUGHNESS_UV_IDX 6
81 #define CORE_MATERIAL_PACK_TEX_CLEARCOAT_NORMAL_UV_IDX 7
82 #define CORE_MATERIAL_PACK_TEX_SHEEN_UV_IDX 8
83 #define CORE_MATERIAL_PACK_TEX_TRANSMISSION_UV_IDX 9
84 #define CORE_MATERIAL_PACK_TEX_SPECULAR_UV_IDX 10
85 #define CORE_MATERIAL_PACK_ADDITIONAL_IDX 11
86
87 // needs to match api/core/material/material_desc.h MaterialAlphaMode
88 #define CORE_MATERIAL_METALLIC_ROUGHNESS 0
89 #define CORE_MATERIAL_SPECULAR_GLOSSINESS 1
90 #define CORE_MATERIAL_UNLIT 2
91 #define CORE_MATERIAL_UNLIT_SHADOW_ALPHA 3
92 #define CORE_MATERIAL_CUSTOM 4
93 #define CORE_MATERIAL_CUSTOM_COMPLEX 5
94
95 // needs to match LumeEngine/3D/api/3d/render/render_data_defines_3d.h RenderMaterialFlagBits
96 #define CORE_MATERIAL_SHADOW_RECEIVER_BIT (1 << 0)
97 #define CORE_MATERIAL_SHADOW_CASTER_BIT (1 << 1)
98 #define CORE_MATERIAL_NORMAL_MAP_BIT (1 << 2)
99 #define CORE_MATERIAL_TEXTURE_TRANSFORM_BIT (1 << 3)
100 #define CORE_MATERIAL_CLEARCOAT_BIT (1 << 4)
101 #define CORE_MATERIAL_TRANSMISSION_BIT (1 << 5)
102 #define CORE_MATERIAL_SHEEN_BIT (1 << 6)
103 #define CORE_MATERIAL_ADDITIONAL_SHADER_DISCARD_BIT (1 << 7)
104 #define CORE_MATERIAL_OPAQUE_BIT (1 << 8)
105 #define CORE_MATERIAL_SPECULAR_BIT (1 << 9)
106 #define CORE_MATERIAL_PUNCTUAL_LIGHT_RECEIVER_BIT (1 << 10)
107 #define CORE_MATERIAL_INDIRECT_LIGHT_RECEIVER_BIT (1 << 11)
108 // basic (1 << 12)
109 // complex (1 << 13)
110 #define CORE_MATERIAL_GPU_INSTANCING_BIT (1 << 14)
111 // use instancing for materials in fragment shader
112 #define CORE_MATERIAL_GPU_INSTANCING_MATERIAL_BIT (1 << 15)
113
114 // needs to match api/core/render/intf_render_data_store_default_light LightingFlagBits
115 #define CORE_LIGHTING_SHADOW_TYPE_VSM_BIT (1 << 0)
116 #define CORE_LIGHTING_POINT_ENABLED_BIT (1 << 1)
117 #define CORE_LIGHTING_SPOT_ENABLED_BIT (1 << 2)
118
119 // needs to match LumeEngine/3D/api/3d/render/render_data_defines_3d.h RenderSubmeshFlagBits
120 #define CORE_SUBMESH_TANGENTS_BIT (1 << 0)
121 #define CORE_SUBMESH_VERTEX_COLORS_BIT (1 << 1)
122 #define CORE_SUBMESH_SKIN_BIT (1 << 2)
123 #define CORE_SUBMESH_SECOND_TEXCOORD_BIT (1 << 3)
124 #define CORE_SUBMESH_INVERSE_WINDING_BIT (1 << 4) // usually not controlled in shader
125 #define CORE_SUBMESH_VELOCITY_BIT (1 << 5) // defines that one should calculate correct velocity
126
127 // needs to match render_data_defines.h LightUsageFlagBits
128 #define CORE_LIGHT_USAGE_DIRECTIONAL_LIGHT_BIT (1 << 0)
129 #define CORE_LIGHT_USAGE_POINT_LIGHT_BIT (1 << 1)
130 #define CORE_LIGHT_USAGE_SPOT_LIGHT_BIT (1 << 2)
131 #define CORE_LIGHT_USAGE_SHADOW_LIGHT_BIT (1 << 3)
132
133 // CORE_CAMERA_FLAGS related to render_data_defines_3d.h CameraShaderFlags
134 #define CORE_CAMERA_FOG_BIT (1 << 0)
135
136 // needs to match api/core/render/render_data_defines.h BackgroundType
137 #define CORE_BACKGROUND_TYPE_IMAGE 1
138 #define CORE_BACKGROUND_TYPE_CUBEMAP 2
139 #define CORE_BACKGROUND_TYPE_EQUIRECTANGULAR 3
140
141 #define CORE_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT 256
142 // 16kB min limit
143 #define CORE_MAX_MESH_MATRIX_UBO_ELEMENT_COUNT 64
144 #define CORE_MAX_MESH_MATRIX_UBO_USER_DATA_COUNT 2
145 #define CORE_MAX_MATERIAL_UBO_ELEMENT_COUNT 64
146
147 // 16 * sizeof(uvec4) -> 256 (CORE_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT)
148 #define CORE_MAX_MATERIAL_UBO_USER_DATA_COUNT 16u
149
150 #define CORE_DEFAULT_MATERIAL_MAX_JOINT_COUNT 256u
151 #define CORE_DEFAULT_MATERIAL_PREV_JOINT_OFFSET 128u
152
153 #define CORE_DEFAULT_MATERIAL_MAX_LIGHT_COUNT 64
154 #define CORE_DEFAULT_MATERIAL_MAX_CLUSTER_LIGHT_COUNT 15
155 #define CORE_DEFAULT_MATERIAL_MAX_CAMERA_COUNT 16
156 #define CORE_DEFAULT_MATERIAL_MAX_ENVIRONMENT_COUNT 8
157
158 #define CORE_DEFAULT_MATERIAL_MAX_SH_VEC3_VALUE_COUNT 9
159
160 #define CORE_DEFAULT_MATERIAL_TEXTURE_TRANSFORM_COUNT 5
161 #define CORE_DEFAULT_MATERIAL_USER_DATA_VEC4_COUNT 8
162
163 #define CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_LEFT 0
164 #define CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_RIGHT 1
165 #define CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_BOTTOM 2
166 #define CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_TOP 3
167 #define CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_NEAR 4
168 #define CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_FAR 5
169 #define CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_COUNT 6
170
171 #define LIGHT_CLUSTERS_X 16
172 #define LIGHT_CLUSTERS_Y 9
173 #define LIGHT_CLUSTERS_Z 24
174 #define CORE_DEFAULT_MATERIAL_MAX_CLUSTERS_COUNT LIGHT_CLUSTERS_X* LIGHT_CLUSTERS_Y* LIGHT_CLUSTERS_Z
175 #define LIGHT_CLUSTER_TGS 64
176 #define CORE_DEFAULT_ENABLE_LIGHT_CLUSTERING 0
177
178 #define CORE_MULTI_VIEW_VIEW_INDEX_SHIFT 16U
179 #define CORE_MULTI_VIEW_VIEW_INDEX_MASK 0xffffU
180 #define CORE_MULTI_VIEW_VIEW_INDEX_MODULO 4U
181
182 #define CORE_DM_CONSTANT_ID_MATERIAL_TYPE 0
183 #define CORE_DM_CONSTANT_ID_MATERIAL_FLAGS 1
184 #define CORE_DM_CONSTANT_ID_LIGHTING_FLAGS 2
185 #define CORE_DM_CONSTANT_ID_POST_PROCESS_FLAGS 3
186 #define CORE_DM_CONSTANT_ID_CAMERA_FLAGS 4
187 #define CORE_DM_CONSTANT_ID_ENV_TYPE 5
188 #define CORE_DM_CONSTANT_ID_SUBMESH_FLAGS 6
189
190 #else
191
192 // aligned for 256 with indices uvec4 bytes
193 constexpr uint32_t CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT { 15u };
194 constexpr uint32_t CORE_MATERIAL_PACKED_UNIFORM_UVEC4_COUNT { 15u };
195
196 // factor indices
197 constexpr uint32_t CORE_MATERIAL_FACTOR_BASE_IDX { 0u };
198 constexpr uint32_t CORE_MATERIAL_FACTOR_NORMAL_IDX { 1 };
199 constexpr uint32_t CORE_MATERIAL_FACTOR_MATERIAL_IDX { 2 };
200 constexpr uint32_t CORE_MATERIAL_FACTOR_EMISSIVE_IDX { 3 };
201 constexpr uint32_t CORE_MATERIAL_FACTOR_AO_IDX { 4 };
202 constexpr uint32_t CORE_MATERIAL_FACTOR_CLEARCOAT_IDX { 5 };
203 constexpr uint32_t CORE_MATERIAL_FACTOR_CLEARCOAT_ROUGHNESS_IDX { 6 };
204 constexpr uint32_t CORE_MATERIAL_FACTOR_CLEARCOAT_NORMAL_IDX { 7 };
205 constexpr uint32_t CORE_MATERIAL_FACTOR_SHEEN_IDX { 8 };
206 constexpr uint32_t CORE_MATERIAL_FACTOR_TRANSMISSION_IDX { 9 };
207 constexpr uint32_t CORE_MATERIAL_FACTOR_SPECULAR_IDX { 10 };
208
209 // packed texture transforms
210 constexpr uint32_t CORE_MATERIAL_PACK_TEX_BASE_COLOR_UV_IDX { 0u };
211 constexpr uint32_t CORE_MATERIAL_PACK_TEX_NORMAL_UV_IDX { 1u };
212 constexpr uint32_t CORE_MATERIAL_PACK_TEX_MATERIAL_UV_IDX { 2u };
213 constexpr uint32_t CORE_MATERIAL_PACK_TEX_EMISSIVE_UV_IDX { 3u };
214 constexpr uint32_t CORE_MATERIAL_PACK_TEX_AO_UV_IDX { 4u };
215 constexpr uint32_t CORE_MATERIAL_PACK_TEX_CLEARCOAT_UV_IDX { 5u };
216 constexpr uint32_t CORE_MATERIAL_PACK_TEX_CLEARCOAT_ROUGHNESS_UV_IDX { 6u };
217 constexpr uint32_t CORE_MATERIAL_PACK_TEX_CLEARCOAT_NORMAL_UV_IDX { 7u };
218 constexpr uint32_t CORE_MATERIAL_PACK_TEX_SHEEN_UV_IDX { 8u };
219 constexpr uint32_t CORE_MATERIAL_PACK_TEX_TRANSMISSION_UV_IDX { 9u };
220 constexpr uint32_t CORE_MATERIAL_PACK_TEX_SPECULAR_UV_IDX { 10u };
221 constexpr uint32_t CORE_MATERIAL_PACK_ADDITIONAL_IDX { 11u };
222
223 // matches PipelineLayoutConstants::MIN_UBO_BIND_OFFSET_ALIGNMENT_BYTE_SIZE
224 constexpr uint32_t CORE_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT { 256u };
225 constexpr uint32_t CORE_UNIFORM_BUFFER_MAX_BIND_SIZE { 1024u * 16u };
226 // 16kB min limit
227 constexpr uint32_t CORE_MAX_MESH_MATRIX_UBO_ELEMENT_COUNT { 64u };
228 constexpr uint32_t CORE_MAX_MESH_MATRIX_UBO_USER_DATA_COUNT { 2u };
229 constexpr uint32_t CORE_MAX_MATERIAL_UBO_ELEMENT_COUNT { 64u };
230
231 // 16 * sizeof(uvec4) -> 256 (CORE_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT)
232 constexpr uint32_t CORE_MAX_MATERIAL_UBO_USER_DATA_COUNT { 16u };
233
234 constexpr uint32_t CORE_DEFAULT_MATERIAL_MAX_JOINT_COUNT { 256u };
235 constexpr uint32_t CORE_DEFAULT_MATERIAL_PREV_JOINT_OFFSET { 128u };
236
237 constexpr uint32_t CORE_DEFAULT_MATERIAL_MAX_LIGHT_COUNT { 64u };
238 constexpr uint32_t CORE_DEFAULT_MATERIAL_MAX_CLUSTER_LIGHT_COUNT { 15u };
239 constexpr uint32_t CORE_DEFAULT_MATERIAL_MAX_CAMERA_COUNT { 16u };
240 constexpr uint32_t CORE_DEFAULT_MATERIAL_MAX_ENVIRONMENT_COUNT { 8u };
241
242 constexpr uint32_t CORE_DEFAULT_MATERIAL_MAX_SH_VEC3_VALUE_COUNT { 9u };
243
244 constexpr uint32_t CORE_DEFAULT_MATERIAL_TEXTURE_TRANSFORM_COUNT { 5u };
245 constexpr uint32_t CORE_DEFAULT_MATERIAL_USER_DATA_VEC4_COUNT { 8u };
246
247 constexpr uint32_t CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_LEFT { 0u };
248 constexpr uint32_t CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_RIGHT { 1 };
249 constexpr uint32_t CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_BOTTOM { 2 };
250 constexpr uint32_t CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_TOP { 3 };
251 constexpr uint32_t CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_NEAR { 4 };
252 constexpr uint32_t CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_FAR { 5 };
253 constexpr uint32_t CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_COUNT { 6 };
254
255 constexpr uint32_t LIGHT_CLUSTERS_X { 16u };
256 constexpr uint32_t LIGHT_CLUSTERS_Y { 9u };
257 constexpr uint32_t LIGHT_CLUSTERS_Z { 24u };
258 constexpr uint32_t CORE_DEFAULT_MATERIAL_MAX_CLUSTERS_COUNT { LIGHT_CLUSTERS_X * LIGHT_CLUSTERS_Y * LIGHT_CLUSTERS_Z };
259 constexpr uint32_t LIGHT_CLUSTER_TGS { 64u };
260
261 constexpr uint32_t CORE_MULTI_VIEW_VIEW_INDEX_SHIFT { 16U };
262 constexpr uint32_t CORE_MULTI_VIEW_VIEW_INDEX_MASK { 0xffffU };
263 constexpr uint32_t CORE_MULTI_VIEW_VIEW_INDEX_MODULO { 4U };
264
265 constexpr uint32_t CORE_DM_CONSTANT_ID_MATERIAL_TYPE { 0 };
266 constexpr uint32_t CORE_DM_CONSTANT_ID_MATERIAL_FLAGS { 1 };
267 constexpr uint32_t CORE_DM_CONSTANT_ID_LIGHTING_FLAGS { 2 };
268 constexpr uint32_t CORE_DM_CONSTANT_ID_POST_PROCESS_FLAGS { 3 };
269 constexpr uint32_t CORE_DM_CONSTANT_ID_CAMERA_FLAGS { 4 };
270 constexpr uint32_t CORE_DM_CONSTANT_ID_ENV_TYPE { 5 };
271 constexpr uint32_t CORE_DM_CONSTANT_ID_SUBMESH_FLAGS { 6 };
272
273 #endif
274
275 struct DefaultMaterialSingleMeshStruct {
276 // currently using offsets in binding CORE_MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT
277
278 mat4 world;
279 mat4 normalWorld; // NOTE: not implemented yet
280 mat4 prevWorld;
281
282 // .xy render mesh id, .zw mesh id (64 bit id, with 32 bit info usually meaningfull)
283 uvec4 indices;
284 // layers, .xy layer mask
285 uvec4 layers;
286 // custom data
287 uvec4 customData[CORE_MAX_MESH_MATRIX_UBO_USER_DATA_COUNT];
288 };
289 // by default GPU instancing enablers are active
290 struct DefaultMaterialMeshStruct {
291 DefaultMaterialSingleMeshStruct mesh[CORE_MAX_MESH_MATRIX_UBO_ELEMENT_COUNT];
292 };
293
294 struct DefaultMaterialSingleMaterialStruct {
295 vec4 factors[CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT];
296 uvec4 indices;
297 };
298 // by default GPU instancing enablers are active
299 struct DefaultMaterialMaterialStruct {
300 DefaultMaterialSingleMaterialStruct material[CORE_MAX_MATERIAL_UBO_ELEMENT_COUNT];
301 };
302
303 struct DefaultMaterialTransformSingleMaterialStruct {
304 uvec4 packed[CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT];
305 uvec4 indices;
306 };
307 // by default GPU instancing enablers are active
308 struct DefaultMaterialTransformMaterialStruct {
309 DefaultMaterialTransformSingleMaterialStruct material[CORE_MAX_MATERIAL_UBO_ELEMENT_COUNT];
310 };
311
312 struct DefaultMaterialSingleUserMaterialStruct {
313 uvec4 userData[CORE_MAX_MATERIAL_UBO_USER_DATA_COUNT];
314 };
315 // by default GPU instancing enablers are active
316 struct DefaultMaterialUserMaterialStruct {
317 DefaultMaterialSingleUserMaterialStruct material[CORE_MAX_MATERIAL_UBO_ELEMENT_COUNT];
318 };
319
320 struct DefaultMaterialSingleLightStruct {
321 vec4 pos;
322 // .w is range
323 vec4 dir;
324 // intensity baked into rgb, .w is intensity (if needs to be inverse multiplied)
325 vec4 color;
326 // .x is inner cone angle, .y is outer cone angle, .z is light angle scale, .w is light angle offset
327 vec4 spotLightParams;
328 // .x = shadow strength, .y = depth bias, .z = normal bias, .w = shadow uv step size
329 vec4 shadowFactors;
330 // additional factors for customization
331 vec4 additionalFactor;
332
333 // .x is light flags, .y is shadow camera idx, .z is shadow idx in atlas, .w is shadow count
334 uvec4 flags;
335 // .xy = unique id (64-bit), .zw = layer mask (64 bit)
336 uvec4 indices;
337 };
338
339 struct DefaultMaterialLightStruct {
340 uint directionalLightBeginIndex;
341 uint directionalLightCount;
342
343 uint pointLightBeginIndex;
344 uint pointLightCount;
345
346 uint spotLightBeginIndex;
347 uint spotLightCount;
348
349 uint pad0;
350 uint pad1;
351
352 uvec4 clusterSizes;
353 vec4 clusterFactors;
354
355 vec4 atlasSizeInvSize;
356 vec4 additionalFactors;
357
358 DefaultMaterialSingleLightStruct lights[CORE_DEFAULT_MATERIAL_MAX_LIGHT_COUNT];
359 };
360
361 // contains the per-cluster data: the number of lights within this cluster
362 // and the indices to the lights in this cluster.
363 struct DefaultMaterialLightClusterData {
364 uint count;
365 uint lightIndices[CORE_DEFAULT_MATERIAL_MAX_CLUSTER_LIGHT_COUNT];
366 };
367
368 struct DefaultMaterialSkinStruct {
369 // previous frame matrices in offset CORE_DEFAULT_MATERIAL_PREV_JOINT_OFFSET
370 mat4 jointMatrices[CORE_DEFAULT_MATERIAL_MAX_JOINT_COUNT];
371 };
372
373 // can change per camera
374 struct DefaultMaterialGeneralDataStruct {
375 // .x is render camera index
376 uvec4 indices;
377 // .xy is viewport size 0 - size, .zw is inv viewport size
378 vec4 viewportSizeInvViewportSize;
379 // .x = scene delta time (ms), .y = tick delta time (ms), .z = tick total time (s), .w = frame index (asuint)
380 vec4 sceneTimingData;
381 };
382
383 // can change per camera
384 struct DefaultMaterialEnvironmentStruct {
385 // .rgb is tint * intensity (intensity baked into rgb), .a is intensity
386 vec4 indirectSpecularColorFactor;
387 // .rgb = tint * intensity (intensity baked into rgb), .a is intensity
388 vec4 indirectDiffuseColorFactor;
389 // .rgb = tint * intensity (intensity baked into rgb), .a is intensity
390 vec4 envMapColorFactor;
391 // .r = radiance cubemap mip count, .g = specular cubemap lod level
392 vec4 values;
393 // blender factor for multi-env case
394 vec4 blendFactor;
395
396 // Environment rotation matrix.
397 mat4 envRotation;
398
399 // .xy = unique id (64-bit), .zw = layer mask (64 bit)
400 uvec4 indices;
401
402 // spherical harmonics for indirect diffuse environment lighting (3 bands)
403 vec4 shIndirectCoefficients[CORE_DEFAULT_MATERIAL_MAX_SH_VEC3_VALUE_COUNT];
404
405 // .x = count, .yzw = multi env indices
406 uvec4 multiEnvIndices;
407 };
408
409 struct DefaultMaterialFogStruct {
410 // .xy = unique id (64 bit), .zw = layer mask (64 bit)
411 uvec4 indices;
412
413 // .x = density, .y = heightFalloff, .z = heightFogOffset
414 vec4 firstLayer;
415 // .x = density, .y = heightFalloff, .z = heightFogOffset
416 vec4 secondLayer;
417
418 // .x = startDistance, .y = cutoffDistance, .z = maxOpacity
419 vec4 baseFactors;
420 // primary color for the fog (.rgb = tint, .a = intensity)
421 vec4 inscatteringColor;
422 // env map color factor (.rgb = tint, .a = intensity)
423 vec4 envMapFactor;
424
425 // Additional custom factor
426 vec4 additionalFactor;
427 };
428
429 // camera data
430 struct DefaultCameraMatrixStruct {
431 mat4 view;
432 mat4 proj;
433 mat4 viewProj;
434
435 mat4 viewInv;
436 mat4 projInv;
437 mat4 viewProjInv;
438
439 mat4 viewPrevFrame;
440 mat4 projPrevFrame;
441 mat4 viewProjPrevFrame;
442
443 mat4 shadowViewProj;
444 mat4 shadowViewProjInv;
445
446 // .xy = jitter offset, .zw = jitter offset with baked screen size
447 vec4 jitter;
448 vec4 jitterPrevFrame;
449
450 // .xy = unique id (64-bit), .zw = layer mask (64 bit)
451 uvec4 indices;
452 // .x multi-view camera additional layer count, .yzw 3 multi-view camera indices
453 // yzw are packed, use unpack functions
454 uvec4 multiViewIndices;
455
456 vec4 frustumPlanes[CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_COUNT];
457
458 // .x environment count
459 uvec4 counts;
460 // padding to 256
461 uvec4 pad0;
462 mat4 matPad0;
463 mat4 matPad1;
464 };
465
466 #ifdef VULKAN
467
468 // General data unpacking
469
470 struct DefaultMaterialUnpackedSceneTimingStruct {
471 // scene delta time (ms)
472 float sceneDeltaTime;
473 // tick delta time (ms)
474 float tickDeltaTime;
475 // tick total time (s)
476 float tickTotalTime;
477 // frame index of the current ECS scene render system
478 uint frameIndex;
479 };
480
481 struct DefaultMaterialUnpackedPostProcessStruct {
482 float tonemapExposure;
483 float vignetteCoeff;
484 float vignettePower;
485 };
486
GetPackFlatIndices(const uint cameraIdx,const uint instanceIdx)487 uint GetPackFlatIndices(const uint cameraIdx, const uint instanceIdx)
488 {
489 return ((instanceIdx << 16) | (cameraIdx & 0xffff)); // 16 : left shift
490 }
491
GetUnpackFlatIndices(in uint indices,out uint cameraIdx,out uint instanceIdx)492 void GetUnpackFlatIndices(in uint indices, out uint cameraIdx, out uint instanceIdx)
493 {
494 cameraIdx = indices & 0xffff;
495 instanceIdx = indices >> 16; // 16 : right shift
496 }
497
GetUnpackFlatIndicesInstanceIdx(in uint indices)498 uint GetUnpackFlatIndicesInstanceIdx(in uint indices)
499 {
500 return (indices >> 16); // 16 : right shift
501 }
502
GetUnpackFlatIndicesCameraIdx(in uint indices)503 uint GetUnpackFlatIndicesCameraIdx(in uint indices)
504 {
505 return (indices & 0xffff);
506 }
507
GetUnpackCameraIndex(const DefaultMaterialGeneralDataStruct dmgds)508 uint GetUnpackCameraIndex(const DefaultMaterialGeneralDataStruct dmgds)
509 {
510 return dmgds.indices.x;
511 }
512
GetMaterialInstanceIndex(const uint materialFLags,const uint indices)513 uint GetMaterialInstanceIndex(const uint materialFLags, const uint indices)
514 {
515 uint instanceIdx = 0U;
516 if (((materialFLags & CORE_MATERIAL_GPU_INSTANCING_BIT) == CORE_MATERIAL_GPU_INSTANCING_BIT) &&
517 ((materialFLags & CORE_MATERIAL_GPU_INSTANCING_MATERIAL_BIT) == CORE_MATERIAL_GPU_INSTANCING_MATERIAL_BIT)) {
518 instanceIdx = GetUnpackFlatIndicesInstanceIdx(indices);
519 }
520 return instanceIdx;
521 }
522
GetUnpackSceneTiming(const DefaultMaterialGeneralDataStruct dmgds)523 DefaultMaterialUnpackedSceneTimingStruct GetUnpackSceneTiming(const DefaultMaterialGeneralDataStruct dmgds)
524 {
525 DefaultMaterialUnpackedSceneTimingStruct dm;
526 dm.sceneDeltaTime = dmgds.sceneTimingData.x;
527 dm.tickDeltaTime = dmgds.sceneTimingData.y;
528 dm.tickTotalTime = dmgds.sceneTimingData.z;
529 dm.frameIndex = floatBitsToUint(dmgds.sceneTimingData.w);
530 return dm;
531 }
532
GetUnpackViewport(const DefaultMaterialGeneralDataStruct dmgds)533 vec4 GetUnpackViewport(const DefaultMaterialGeneralDataStruct dmgds)
534 {
535 return dmgds.viewportSizeInvViewportSize;
536 }
537
538 // Default material unpacking
539
540 struct DefaultMaterialUnpackedTexTransformStruct {
541 vec4 rotateScale;
542 vec2 translate;
543 };
544
GetUnpackTextureTransform(const uvec4 packedTexTransform)545 DefaultMaterialUnpackedTexTransformStruct GetUnpackTextureTransform(const uvec4 packedTexTransform)
546 {
547 DefaultMaterialUnpackedTexTransformStruct dm;
548 dm.rotateScale = UnpackVec4Half2x16(packedTexTransform.xy);
549 dm.translate = UnpackVec4Half2x16(packedTexTransform.zw).xy;
550 return dm;
551 }
552
GetUnpackCameraMultiViewIndex(const uint inputCameraIndex,const uint glViewIndex,const uint viewCount,const uvec4 multiViewIndices)553 uint GetUnpackCameraMultiViewIndex(
554 const uint inputCameraIndex, const uint glViewIndex, const uint viewCount, const uvec4 multiViewIndices)
555 {
556 uint cameraIdx = inputCameraIndex;
557 // NOTE: when gl_ViewIndex is 0 the "main" camera is used and no multi-view indexing
558 if ((viewCount > 0u) && (glViewIndex <= viewCount) && (glViewIndex > 0u)) {
559 // additional index packed to uints
560 const uint viewIndexShift = (glViewIndex >= 4U) ? CORE_MULTI_VIEW_VIEW_INDEX_SHIFT : 0U; // 4 : idx
561 const uint finalViewIndex = glViewIndex % 4U; // 4 idx
562 cameraIdx = multiViewIndices[finalViewIndex];
563 cameraIdx = (cameraIdx >> viewIndexShift) & CORE_MULTI_VIEW_VIEW_INDEX_MASK;
564 }
565 return cameraIdx;
566 }
567
568 // DEPRECATED base methods with struct inputs
569 #ifdef CORE3D_USE_DEPRECATED_UNPACK_METHODS
570
GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms)571 vec4 GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms)
572 {
573 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_BASE_IDX];
574 }
GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)575 vec4 GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
576 {
577 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_BASE_IDX];
578 }
579
GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms)580 vec4 GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms)
581 {
582 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_MATERIAL_IDX];
583 }
GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)584 vec4 GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
585 {
586 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_MATERIAL_IDX];
587 }
588
GetUnpackAO(const DefaultMaterialMaterialStruct dmms)589 float GetUnpackAO(const DefaultMaterialMaterialStruct dmms)
590 {
591 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_AO_IDX].x;
592 }
GetUnpackAO(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)593 float GetUnpackAO(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
594 {
595 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_AO_IDX].x;
596 }
597
GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms)598 float GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms)
599 {
600 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_IDX].x;
601 }
GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)602 float GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
603 {
604 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_IDX].x;
605 }
606
607 // NOTE: sampling from .y
GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms)608 float GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms)
609 {
610 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_ROUGHNESS_IDX].y;
611 }
GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)612 float GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
613 {
614 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_ROUGHNESS_IDX].y;
615 }
616
617 // .xyz = sheen factor, .w = sheen roughness
GetUnpackSheen(const DefaultMaterialMaterialStruct dmms)618 vec4 GetUnpackSheen(const DefaultMaterialMaterialStruct dmms)
619 {
620 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_SHEEN_IDX];
621 }
GetUnpackSheen(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)622 vec4 GetUnpackSheen(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
623 {
624 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_SHEEN_IDX];
625 }
626
GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms)627 float GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms)
628 {
629 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_TRANSMISSION_IDX].x;
630 }
GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)631 float GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
632 {
633 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_TRANSMISSION_IDX].x;
634 }
635
636 // .xyz = specular color, .w = specular strength
GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms)637 vec4 GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms)
638 {
639 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_SPECULAR_IDX];
640 }
GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)641 vec4 GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
642 {
643 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_SPECULAR_IDX];
644 }
645
GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms)646 vec3 GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms)
647 {
648 const vec4 emissive = dmms.material[0].factors[CORE_MATERIAL_FACTOR_EMISSIVE_IDX];
649 return emissive.rgb * emissive.a;
650 }
GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)651 vec3 GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
652 {
653 const vec4 emissive = dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_EMISSIVE_IDX];
654 return emissive.rgb * emissive.a;
655 }
656
GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms)657 float GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms)
658 {
659 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_NORMAL_IDX].x;
660 }
GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)661 float GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
662 {
663 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_NORMAL_IDX].x;
664 }
665
GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms)666 float GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms)
667 {
668 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_ADDITIONAL_IDX].x;
669 }
GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)670 float GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
671 {
672 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_ADDITIONAL_IDX].x;
673 }
674
GetUnpackMaterialTextureInfoSlotFactor(const DefaultMaterialMaterialStruct dmms,const uint materialIndexSlot)675 vec4 GetUnpackMaterialTextureInfoSlotFactor(const DefaultMaterialMaterialStruct dmms, const uint materialIndexSlot)
676 {
677 const uint maxIndex = min(materialIndexSlot, CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT - 1);
678 return dmms.material[0].factors[maxIndex].xyzw;
679 }
GetUnpackMaterialTextureInfoSlotFactor(const DefaultMaterialMaterialStruct dmms,const uint materialIndexSlot,const uint instanceIdx)680 vec4 GetUnpackMaterialTextureInfoSlotFactor(
681 const DefaultMaterialMaterialStruct dmms, const uint materialIndexSlot, const uint instanceIdx)
682 {
683 const uint maxIndex = min(materialIndexSlot, CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT - 1);
684 return dmms.material[instanceIdx].factors[maxIndex].xyzw;
685 }
686
687 // transform = high bits (16)
688 // uv set bit = low bits (0)
GetUnpackTexCoordInfo(const DefaultMaterialMaterialStruct dmms)689 uint GetUnpackTexCoordInfo(const DefaultMaterialMaterialStruct dmms)
690 {
691 return floatBitsToUint(dmms.material[0].factors[CORE_MATERIAL_FACTOR_ADDITIONAL_IDX].y);
692 }
693
694 // transform = high bits (16)
695 // uv set bit = low bits (0)
GetUnpackTexCoordInfo(const DefaultMaterialTransformMaterialStruct dmms)696 uint GetUnpackTexCoordInfo(const DefaultMaterialTransformMaterialStruct dmms)
697 {
698 return dmms.material[0].packed[CORE_MATERIAL_PACK_ADDITIONAL_IDX].y;
699 }
700
701 #endif // CORE3D_USE_DEPRECATED_UNPACK_METHODS
702
703 #endif
704
705 #endif // SHADERS_COMMON_3D_DEFAULT_MATERIAL_STRUCTURES_COMMON_H
706