1 /*
2 * Copyright (c) 2022 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 // sky atmosphere
409 // sun position
410 vec4 packedSun;
411 // rain data
412 vec4 packedRain;
413 };
414
415 struct DefaultMaterialFogStruct {
416 // .xy = unique id (64 bit), .zw = layer mask (64 bit)
417 uvec4 indices;
418
419 // .x = density, .y = heightFalloff, .z = heightFogOffset
420 vec4 firstLayer;
421 // .x = density, .y = heightFalloff, .z = heightFogOffset
422 vec4 secondLayer;
423
424 // .x = startDistance, .y = cutoffDistance, .z = maxOpacity
425 vec4 baseFactors;
426 // primary color for the fog (.rgb = tint, .a = intensity)
427 vec4 inscatteringColor;
428 // env map color factor (.rgb = tint, .a = intensity)
429 vec4 envMapFactor;
430
431 // Additional custom factor
432 vec4 additionalFactor;
433 };
434
435 // camera data
436 struct DefaultCameraMatrixStruct {
437 mat4 view;
438 mat4 proj;
439 mat4 viewProj;
440
441 mat4 viewInv;
442 mat4 projInv;
443 mat4 viewProjInv;
444
445 mat4 viewPrevFrame;
446 mat4 projPrevFrame;
447 mat4 viewProjPrevFrame;
448
449 mat4 shadowViewProj;
450 mat4 shadowViewProjInv;
451
452 // .xy = jitter offset, .zw = jitter offset with baked screen size
453 vec4 jitter;
454 vec4 jitterPrevFrame;
455
456 // .xy = unique id (64-bit), .zw = layer mask (64 bit)
457 uvec4 indices;
458 // .x multi-view camera additional layer count, .yzw 3 multi-view camera indices
459 // yzw are packed, use unpack functions
460 uvec4 multiViewIndices;
461
462 vec4 frustumPlanes[CORE_DEFAULT_CAMERA_FRUSTUM_PLANE_COUNT];
463
464 // .x environment count
465 uvec4 counts;
466 // padding to 256
467 uvec4 pad0;
468 mat4 matPad0;
469 mat4 matPad1;
470 };
471
472 #ifdef VULKAN
473
474 // General data unpacking
475
476 struct DefaultMaterialUnpackedSceneTimingStruct {
477 // scene delta time (ms)
478 float sceneDeltaTime;
479 // tick delta time (ms)
480 float tickDeltaTime;
481 // tick total time (s)
482 float tickTotalTime;
483 // frame index of the current ECS scene render system
484 uint frameIndex;
485 };
486
487 struct DefaultMaterialUnpackedPostProcessStruct {
488 float tonemapExposure;
489 float vignetteCoeff;
490 float vignettePower;
491 };
492
GetPackFlatIndices(const uint cameraIdx,const uint instanceIdx)493 uint GetPackFlatIndices(const uint cameraIdx, const uint instanceIdx)
494 {
495 return ((instanceIdx << 16) | (cameraIdx & 0xffff));
496 }
497
GetUnpackFlatIndices(in uint indices,out uint cameraIdx,out uint instanceIdx)498 void GetUnpackFlatIndices(in uint indices, out uint cameraIdx, out uint instanceIdx)
499 {
500 cameraIdx = indices & 0xffff;
501 instanceIdx = indices >> 16;
502 }
503
GetUnpackFlatIndicesInstanceIdx(in uint indices)504 uint GetUnpackFlatIndicesInstanceIdx(in uint indices)
505 {
506 return (indices >> 16);
507 }
508
GetUnpackFlatIndicesCameraIdx(in uint indices)509 uint GetUnpackFlatIndicesCameraIdx(in uint indices)
510 {
511 return (indices & 0xffff);
512 }
513
GetUnpackCameraIndex(const DefaultMaterialGeneralDataStruct dmgds)514 uint GetUnpackCameraIndex(const DefaultMaterialGeneralDataStruct dmgds)
515 {
516 return dmgds.indices.x;
517 }
518
GetMaterialInstanceIndex(const uint materialFLags,const uint indices)519 uint GetMaterialInstanceIndex(const uint materialFLags, const uint indices)
520 {
521 uint instanceIdx = 0U;
522 if (((materialFLags & CORE_MATERIAL_GPU_INSTANCING_BIT) == CORE_MATERIAL_GPU_INSTANCING_BIT) &&
523 ((materialFLags & CORE_MATERIAL_GPU_INSTANCING_MATERIAL_BIT) == CORE_MATERIAL_GPU_INSTANCING_MATERIAL_BIT)) {
524 instanceIdx = GetUnpackFlatIndicesInstanceIdx(indices);
525 }
526 return instanceIdx;
527 }
528
GetUnpackSceneTiming(const DefaultMaterialGeneralDataStruct dmgds)529 DefaultMaterialUnpackedSceneTimingStruct GetUnpackSceneTiming(const DefaultMaterialGeneralDataStruct dmgds)
530 {
531 DefaultMaterialUnpackedSceneTimingStruct dm;
532 dm.sceneDeltaTime = dmgds.sceneTimingData.x;
533 dm.tickDeltaTime = dmgds.sceneTimingData.y;
534 dm.tickTotalTime = dmgds.sceneTimingData.z;
535 dm.frameIndex = floatBitsToUint(dmgds.sceneTimingData.w);
536 return dm;
537 }
538
GetUnpackViewport(const DefaultMaterialGeneralDataStruct dmgds)539 vec4 GetUnpackViewport(const DefaultMaterialGeneralDataStruct dmgds)
540 {
541 return dmgds.viewportSizeInvViewportSize;
542 }
543
544 // Default material unpacking
545
546 struct DefaultMaterialUnpackedTexTransformStruct {
547 vec4 rotateScale;
548 vec2 translate;
549 };
550
GetUnpackTextureTransform(const uvec4 packedTexTransform)551 DefaultMaterialUnpackedTexTransformStruct GetUnpackTextureTransform(const uvec4 packedTexTransform)
552 {
553 DefaultMaterialUnpackedTexTransformStruct dm;
554 dm.rotateScale = UnpackVec4Half2x16(packedTexTransform.xy);
555 dm.translate = UnpackVec4Half2x16(packedTexTransform.zw).xy;
556 return dm;
557 }
558
GetUnpackCameraMultiViewIndex(const uint inputCameraIndex,const uint glViewIndex,const uint viewCount,const uvec4 multiViewIndices)559 uint GetUnpackCameraMultiViewIndex(
560 const uint inputCameraIndex, const uint glViewIndex, const uint viewCount, const uvec4 multiViewIndices)
561 {
562 uint cameraIdx = inputCameraIndex;
563 // NOTE: when gl_ViewIndex is 0 the "main" camera is used and no multi-view indexing
564 if ((viewCount > 0u) && (glViewIndex <= viewCount) && (glViewIndex > 0u)) {
565 // additional index packed to uints
566 const uint viewIndexShift = (glViewIndex >= 4U) ? CORE_MULTI_VIEW_VIEW_INDEX_SHIFT : 0U;
567 const uint finalViewIndex = glViewIndex % 4U;
568 cameraIdx = multiViewIndices[finalViewIndex];
569 cameraIdx = (cameraIdx >> viewIndexShift) & CORE_MULTI_VIEW_VIEW_INDEX_MASK;
570 }
571 return cameraIdx;
572 }
573
574 // DEPRECATED base methods with struct inputs
575 #ifdef CORE3D_USE_DEPRECATED_UNPACK_METHODS
576
GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms)577 vec4 GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms)
578 {
579 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_BASE_IDX];
580 }
GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)581 vec4 GetUnpackBaseColor(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
582 {
583 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_BASE_IDX];
584 }
585
GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms)586 vec4 GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms)
587 {
588 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_MATERIAL_IDX];
589 }
GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)590 vec4 GetUnpackMaterial(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
591 {
592 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_MATERIAL_IDX];
593 }
594
GetUnpackAO(const DefaultMaterialMaterialStruct dmms)595 float GetUnpackAO(const DefaultMaterialMaterialStruct dmms)
596 {
597 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_AO_IDX].x;
598 }
GetUnpackAO(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)599 float GetUnpackAO(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
600 {
601 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_AO_IDX].x;
602 }
603
GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms)604 float GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms)
605 {
606 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_IDX].x;
607 }
GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)608 float GetUnpackClearcoat(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
609 {
610 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_IDX].x;
611 }
612
613 // NOTE: sampling from .y
GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms)614 float GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms)
615 {
616 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_ROUGHNESS_IDX].y;
617 }
GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)618 float GetUnpackClearcoatRoughness(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
619 {
620 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_CLEARCOAT_ROUGHNESS_IDX].y;
621 }
622
623 // .xyz = sheen factor, .w = sheen roughness
GetUnpackSheen(const DefaultMaterialMaterialStruct dmms)624 vec4 GetUnpackSheen(const DefaultMaterialMaterialStruct dmms)
625 {
626 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_SHEEN_IDX];
627 }
GetUnpackSheen(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)628 vec4 GetUnpackSheen(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
629 {
630 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_SHEEN_IDX];
631 }
632
GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms)633 float GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms)
634 {
635 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_TRANSMISSION_IDX].x;
636 }
GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)637 float GetUnpackTransmission(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
638 {
639 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_TRANSMISSION_IDX].x;
640 }
641
642 // .xyz = specular color, .w = specular strength
GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms)643 vec4 GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms)
644 {
645 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_SPECULAR_IDX];
646 }
GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)647 vec4 GetUnpackSpecular(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
648 {
649 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_SPECULAR_IDX];
650 }
651
GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms)652 vec3 GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms)
653 {
654 const vec4 emissive = dmms.material[0].factors[CORE_MATERIAL_FACTOR_EMISSIVE_IDX];
655 return emissive.rgb * emissive.a;
656 }
GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)657 vec3 GetUnpackEmissiveColor(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
658 {
659 const vec4 emissive = dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_EMISSIVE_IDX];
660 return emissive.rgb * emissive.a;
661 }
662
GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms)663 float GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms)
664 {
665 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_NORMAL_IDX].x;
666 }
GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)667 float GetUnpackNormalScale(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
668 {
669 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_NORMAL_IDX].x;
670 }
671
GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms)672 float GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms)
673 {
674 return dmms.material[0].factors[CORE_MATERIAL_FACTOR_ADDITIONAL_IDX].x;
675 }
GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms,const uint instanceIdx)676 float GetUnpackAlphaCutoff(const DefaultMaterialMaterialStruct dmms, const uint instanceIdx)
677 {
678 return dmms.material[instanceIdx].factors[CORE_MATERIAL_FACTOR_ADDITIONAL_IDX].x;
679 }
680
GetUnpackMaterialTextureInfoSlotFactor(const DefaultMaterialMaterialStruct dmms,const uint materialIndexSlot)681 vec4 GetUnpackMaterialTextureInfoSlotFactor(const DefaultMaterialMaterialStruct dmms, const uint materialIndexSlot)
682 {
683 const uint maxIndex = min(materialIndexSlot, CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT - 1);
684 return dmms.material[0].factors[maxIndex].xyzw;
685 }
GetUnpackMaterialTextureInfoSlotFactor(const DefaultMaterialMaterialStruct dmms,const uint materialIndexSlot,const uint instanceIdx)686 vec4 GetUnpackMaterialTextureInfoSlotFactor(
687 const DefaultMaterialMaterialStruct dmms, const uint materialIndexSlot, const uint instanceIdx)
688 {
689 const uint maxIndex = min(materialIndexSlot, CORE_MATERIAL_FACTOR_UNIFORM_VEC4_COUNT - 1);
690 return dmms.material[instanceIdx].factors[maxIndex].xyzw;
691 }
692
693 // transform = high bits (16)
694 // uv set bit = low bits (0)
GetUnpackTexCoordInfo(const DefaultMaterialMaterialStruct dmms)695 uint GetUnpackTexCoordInfo(const DefaultMaterialMaterialStruct dmms)
696 {
697 return floatBitsToUint(dmms.material[0].factors[CORE_MATERIAL_FACTOR_ADDITIONAL_IDX].y);
698 }
699
700 // transform = high bits (16)
701 // uv set bit = low bits (0)
GetUnpackTexCoordInfo(const DefaultMaterialTransformMaterialStruct dmms)702 uint GetUnpackTexCoordInfo(const DefaultMaterialTransformMaterialStruct dmms)
703 {
704 return dmms.material[0].packed[CORE_MATERIAL_PACK_ADDITIONAL_IDX].y;
705 }
706
707 #endif // CORE3D_USE_DEPRECATED_UNPACK_METHODS
708
709 #endif
710
711 #endif // SHADERS_COMMON_3D_DEFAULT_MATERIAL_STRUCTURES_COMMON_H
712