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_DM_INPLACE_ENV_COMMON_H
17 #define SHADERS__COMMON__3D_DM_INPLACE_ENV_COMMON_H
18
19 #ifdef VULKAN
20
21 #include "3d/shaders/common/3d_dm_inplace_fog_common.h"
22 #include "3d/shaders/common/3d_dm_structures_common.h"
23
24 /*
25 * Needs to be included after the descriptor set descriptions
26 */
27
28 #define CORE3D_DEFAULT_ENV_PI 3.1415926535897932384626433832795
29
30 /**
31 * Outputs the default environment type (CORE_DEFAULT_ENV_TYPE) with default material env
32 */
EnvironmentTypeBlock(out uint environmentType)33 void EnvironmentTypeBlock(out uint environmentType)
34 {
35 environmentType = CORE_DEFAULT_ENV_TYPE;
36 }
37
38 /**
39 * Environment sampling based on flags (CORE_DEFAULT_ENV_TYPE)
40 * The value is return in out vec3 color
41 * Uses data from common sets (which needs to be defined with descriptor sets)
42 * uGeneralData (camera index)
43 * uCameras (cameras for near/far plane)
44 * uEnvironmentData (orientation, lod level, and factors)
45 */
InplaceEnvironmentBlock(in uint environmentType,in uint cameraIdx,in vec2 uv,in samplerCube cubeMap,in sampler2D texMap,out vec4 color)46 void InplaceEnvironmentBlock(
47 in uint environmentType, in uint cameraIdx, in vec2 uv, in samplerCube cubeMap, in sampler2D texMap, out vec4 color)
48 {
49 color = vec4(0.0, 0.0, 0.0, 1.0);
50 CORE_RELAXEDP const float lodLevel = uEnvironmentData.values.y;
51
52 // NOTE: would be nicer to calculate in the vertex shader
53
54 // remove translation from view
55 mat4 viewProjInv = uCameras[cameraIdx].viewInv;
56 viewProjInv[3] = vec4(0.0, 0.0, 0.0, 1.0);
57 viewProjInv = viewProjInv * uCameras[cameraIdx].projInv;
58
59 vec4 farPlane = viewProjInv * vec4(uv.x, uv.y, 1.0, 1.0);
60 farPlane.xyz = farPlane.xyz / farPlane.w;
61
62 if ((environmentType == CORE_BACKGROUND_TYPE_CUBEMAP) ||
63 (environmentType == CORE_BACKGROUND_TYPE_EQUIRECTANGULAR)) {
64 vec4 nearPlane = viewProjInv * vec4(uv.x, uv.y, 0.0, 1.0);
65 nearPlane.xyz = nearPlane.xyz / nearPlane.w;
66
67 const vec3 worldView = mat3(uEnvironmentData.envRotation) * normalize(farPlane.xyz - nearPlane.xyz);
68
69 if (environmentType == CORE_BACKGROUND_TYPE_CUBEMAP) {
70 color.rgb = unpackEnvMap(textureLod(cubeMap, worldView, lodLevel));
71 } else {
72 const vec2 texCoord = vec2(atan(worldView.z, worldView.x) + CORE3D_DEFAULT_ENV_PI, acos(worldView.y)) /
73 vec2(2.0 * CORE3D_DEFAULT_ENV_PI, CORE3D_DEFAULT_ENV_PI);
74 color = textureLod(texMap, texCoord, lodLevel);
75 }
76 } else if (environmentType == CORE_BACKGROUND_TYPE_IMAGE) {
77 const vec2 texCoord = (uv + vec2(1.0)) * 0.5;
78 color = textureLod(texMap, texCoord, lodLevel);
79 }
80
81 color.xyz *= uEnvironmentData.envMapColorFactor.xyz;
82
83 // fog
84 const vec3 camPos = uCameras[cameraIdx].viewInv[3].xyz;
85 vec3 fogColor = color.rgb;
86 InplaceFogBlock(CORE_CAMERA_FLAGS, farPlane.xyz, camPos, color, fogColor);
87 color.rgb = fogColor.rgb;
88 }
89
90 #else
91
92 #endif
93
94 #endif // SHADERS__COMMON__3D_DM_INPLACE_ENV_COMMON_H
95