-
Notifications
You must be signed in to change notification settings - Fork 2
/
scene.vert.glsl
122 lines (99 loc) · 3.75 KB
/
scene.vert.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (c) 2019-2024, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2019-2024 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#version 460 core
/**/
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_buffer_reference : enable
#extension GL_EXT_scalar_block_layout : enable
#include "common.h"
#if UNIFORMS_TECHNIQUE == UNIFORMS_MULTISETSDYNAMIC
layout(set=DRAW_UBO_SCENE, binding=0, scalar) uniform sceneBuffer {
SceneData scene;
};
layout(set=DRAW_UBO_MATRIX, binding=0, scalar) uniform objectBuffer {
MatrixData matrix;
};
#elif UNIFORMS_TECHNIQUE == UNIFORMS_PUSHCONSTANTS_ADDRESS
layout(set=0, binding=DRAW_UBO_SCENE, scalar) uniform sceneBuffer {
SceneData scene;
};
layout(buffer_reference, buffer_reference_align=16, scalar) readonly buffer MatrixBuffer {
MatrixData matrix;
};
layout(push_constant, scalar) uniform pushConstants {
MatrixBuffer v;
};
#define matrix v.matrix
#elif UNIFORMS_TECHNIQUE == UNIFORMS_INDEX_VERTEXATTRIB || UNIFORMS_TECHNIQUE == UNIFORMS_INDEX_BASEINSTANCE
layout(set=0, binding=DRAW_UBO_SCENE, scalar) uniform sceneBuffer {
SceneData scene;
};
layout(set=0, binding=DRAW_SSBO_MATRIX, scalar) readonly buffer MatrixBuffer {
MatrixData matrices[];
};
#if UNIFORMS_TECHNIQUE == UNIFORMS_INDEX_VERTEXATTRIB
in layout(location=VERTEX_COMBINED_INDEX) uint inCombinedIndex;
#define matrix matrices[uint(inCombinedIndex) & ((1u<<INDEXED_MATRIX_BITS)-1)]
#else
#define matrix matrices[uint(gl_BaseInstance) & ((1u<<INDEXED_MATRIX_BITS)-1)]
#endif
#endif
in layout(location=VERTEX_POS_OCTNORMAL) vec4 inPosNormal;
layout(location=0) out Interpolants {
vec3 wPos;
vec3 wNormal;
#if UNIFORMS_TECHNIQUE == UNIFORMS_INDEX_VERTEXATTRIB || UNIFORMS_TECHNIQUE == UNIFORMS_INDEX_BASEINSTANCE
flat uint materialIndex;
#endif
#if SHADER_PERMUTATION
vec3 oNormal;
#endif
} OUT;
// oct functions from http://jcgt.org/published/0003/02/01/paper.pdf
vec2 oct_signNotZero(vec2 v) {
return vec2((v.x >= 0.0) ? +1.0 : -1.0, (v.y >= 0.0) ? +1.0 : -1.0);
}
vec3 oct_to_float32x3(vec2 e) {
vec3 v = vec3(e.xy, 1.0 - abs(e.x) - abs(e.y));
if (v.z < 0) v.xy = (1.0 - abs(v.yx)) * oct_signNotZero(v.xy);
return normalize(v);
}
vec2 float32x3_to_oct(in vec3 v) {
// Project the sphere onto the octahedron, and then onto the xy plane
vec2 p = v.xy * (1.0 / (abs(v.x) + abs(v.y) + abs(v.z)));
// Reflect the folds of the lower hemisphere over the diagonals
return (v.z <= 0.0) ? ((1.0 - abs(p.yx)) * oct_signNotZero(p)) : p;
}
void main()
{
vec3 inNormal = oct_to_float32x3(unpackSnorm2x16(floatBitsToUint(inPosNormal.w)));
vec3 wPos = (matrix.worldMatrix * vec4(inPosNormal.xyz,1)).xyz;
vec3 wNormal = mat3(matrix.worldMatrixIT) * inNormal;
gl_Position = scene.viewProjMatrix * vec4(wPos,1);
#if SHADER_PERMUTATION
OUT.oNormal = inNormal;
#endif
#if UNIFORMS_TECHNIQUE == UNIFORMS_INDEX_BASEINSTANCE
OUT.materialIndex = uint(gl_BaseInstance) >> INDEXED_MATRIX_BITS;
#elif UNIFORMS_TECHNIQUE == UNIFORMS_INDEX_VERTEXATTRIB
OUT.materialIndex = inCombinedIndex >> INDEXED_MATRIX_BITS;
#endif
OUT.wPos = wPos;
OUT.wNormal = wNormal;
}