summaryrefslogtreecommitdiff
path: root/servers/rendering/renderer_rd/shaders/giprobe_debug.glsl
blob: 7d4d72967a56d7ba062ae66c060e255127fa92a7 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#[vertex]

#version 450

#VERSION_DEFINES

struct CellData {
	uint position; // xyz 10 bits
	uint albedo; //rgb albedo
	uint emission; //rgb normalized with e as multiplier
	uint normal; //RGB normal encoded
};

layout(set = 0, binding = 1, std140) buffer CellDataBuffer {
	CellData data[];
}
cell_data;

layout(set = 0, binding = 2) uniform texture3D color_tex;

layout(set = 0, binding = 3) uniform sampler tex_sampler;

#ifdef USE_ANISOTROPY
layout(set = 0, binding = 4) uniform texture3D aniso_pos_tex;
layout(set = 0, binding = 5) uniform texture3D aniso_neg_tex;
#endif

layout(push_constant, binding = 0, std430) uniform Params {
	mat4 projection;
	uint cell_offset;
	float dynamic_range;
	float alpha;
	uint level;
	ivec3 bounds;
	uint pad;
}
params;

layout(location = 0) out vec4 color_interp;

void main() {
	const vec3 cube_triangles[36] = vec3[](
			vec3(-1.0f, -1.0f, -1.0f),
			vec3(-1.0f, -1.0f, 1.0f),
			vec3(-1.0f, 1.0f, 1.0f),
			vec3(1.0f, 1.0f, -1.0f),
			vec3(-1.0f, -1.0f, -1.0f),
			vec3(-1.0f, 1.0f, -1.0f),
			vec3(1.0f, -1.0f, 1.0f),
			vec3(-1.0f, -1.0f, -1.0f),
			vec3(1.0f, -1.0f, -1.0f),
			vec3(1.0f, 1.0f, -1.0f),
			vec3(1.0f, -1.0f, -1.0f),
			vec3(-1.0f, -1.0f, -1.0f),
			vec3(-1.0f, -1.0f, -1.0f),
			vec3(-1.0f, 1.0f, 1.0f),
			vec3(-1.0f, 1.0f, -1.0f),
			vec3(1.0f, -1.0f, 1.0f),
			vec3(-1.0f, -1.0f, 1.0f),
			vec3(-1.0f, -1.0f, -1.0f),
			vec3(-1.0f, 1.0f, 1.0f),
			vec3(-1.0f, -1.0f, 1.0f),
			vec3(1.0f, -1.0f, 1.0f),
			vec3(1.0f, 1.0f, 1.0f),
			vec3(1.0f, -1.0f, -1.0f),
			vec3(1.0f, 1.0f, -1.0f),
			vec3(1.0f, -1.0f, -1.0f),
			vec3(1.0f, 1.0f, 1.0f),
			vec3(1.0f, -1.0f, 1.0f),
			vec3(1.0f, 1.0f, 1.0f),
			vec3(1.0f, 1.0f, -1.0f),
			vec3(-1.0f, 1.0f, -1.0f),
			vec3(1.0f, 1.0f, 1.0f),
			vec3(-1.0f, 1.0f, -1.0f),
			vec3(-1.0f, 1.0f, 1.0f),
			vec3(1.0f, 1.0f, 1.0f),
			vec3(-1.0f, 1.0f, 1.0f),
			vec3(1.0f, -1.0f, 1.0f));

	vec3 vertex = cube_triangles[gl_VertexIndex] * 0.5 + 0.5;
#ifdef MODE_DEBUG_LIGHT_FULL
	uvec3 posu = uvec3(gl_InstanceIndex % params.bounds.x, (gl_InstanceIndex / params.bounds.x) % params.bounds.y, gl_InstanceIndex / (params.bounds.y * params.bounds.x));
#else
	uint cell_index = gl_InstanceIndex + params.cell_offset;

	uvec3 posu = uvec3(cell_data.data[cell_index].position & 0x7FF, (cell_data.data[cell_index].position >> 11) & 0x3FF, cell_data.data[cell_index].position >> 21);
#endif

#ifdef MODE_DEBUG_EMISSION
	color_interp.xyz = vec3(uvec3(cell_data.data[cell_index].emission & 0x1ff, (cell_data.data[cell_index].emission >> 9) & 0x1ff, (cell_data.data[cell_index].emission >> 18) & 0x1ff)) * pow(2.0, float(cell_data.data[cell_index].emission >> 27) - 15.0 - 9.0);
#endif

#ifdef MODE_DEBUG_COLOR
	color_interp.xyz = unpackUnorm4x8(cell_data.data[cell_index].albedo).xyz;
#endif

#ifdef MODE_DEBUG_LIGHT

#ifdef USE_ANISOTROPY

#define POS_X 0
#define POS_Y 1
#define POS_Z 2
#define NEG_X 3
#define NEG_Y 4
#define NEG_Z 5

	const uint triangle_aniso[12] = uint[](
			NEG_X,
			NEG_Z,
			NEG_Y,
			NEG_Z,
			NEG_X,
			NEG_Y,
			POS_Z,
			POS_X,
			POS_X,
			POS_Y,
			POS_Y,
			POS_Z);

	color_interp.xyz = texelFetch(sampler3D(color_tex, tex_sampler), ivec3(posu), int(params.level)).xyz * params.dynamic_range;
	vec3 aniso_pos = texelFetch(sampler3D(aniso_pos_tex, tex_sampler), ivec3(posu), int(params.level)).xyz;
	vec3 aniso_neg = texelFetch(sampler3D(aniso_neg_tex, tex_sampler), ivec3(posu), int(params.level)).xyz;
	uint side = triangle_aniso[gl_VertexIndex / 3];

	float strength = 0.0;
	switch (side) {
		case POS_X:
			strength = aniso_pos.x;
			break;
		case POS_Y:
			strength = aniso_pos.y;
			break;
		case POS_Z:
			strength = aniso_pos.z;
			break;
		case NEG_X:
			strength = aniso_neg.x;
			break;
		case NEG_Y:
			strength = aniso_neg.y;
			break;
		case NEG_Z:
			strength = aniso_neg.z;
			break;
	}

	color_interp.xyz *= strength;

#else
	color_interp = texelFetch(sampler3D(color_tex, tex_sampler), ivec3(posu), int(params.level));
	color_interp.xyz *params.dynamic_range;

#endif

#endif
	float scale = (1 << params.level);

	gl_Position = params.projection * vec4((vec3(posu) + vertex) * scale, 1.0);

#ifdef MODE_DEBUG_LIGHT_FULL
	if (color_interp.a == 0.0) {
		gl_Position = vec4(0.0); //force clip and not draw
	}
#else
	color_interp.a = params.alpha;
#endif
}

#[fragment]

#version 450

#VERSION_DEFINES

layout(location = 0) in vec4 color_interp;
layout(location = 0) out vec4 frag_color;

void main() {
	frag_color = color_interp;

#ifdef MODE_DEBUG_LIGHT_FULL

	//there really is no alpha, so use dither

	int x = int(gl_FragCoord.x) % 4;
	int y = int(gl_FragCoord.y) % 4;
	int index = x + y * 4;
	float limit = 0.0;
	if (x < 8) {
		if (index == 0)
			limit = 0.0625;
		if (index == 1)
			limit = 0.5625;
		if (index == 2)
			limit = 0.1875;
		if (index == 3)
			limit = 0.6875;
		if (index == 4)
			limit = 0.8125;
		if (index == 5)
			limit = 0.3125;
		if (index == 6)
			limit = 0.9375;
		if (index == 7)
			limit = 0.4375;
		if (index == 8)
			limit = 0.25;
		if (index == 9)
			limit = 0.75;
		if (index == 10)
			limit = 0.125;
		if (index == 11)
			limit = 0.625;
		if (index == 12)
			limit = 1.0;
		if (index == 13)
			limit = 0.5;
		if (index == 14)
			limit = 0.875;
		if (index == 15)
			limit = 0.375;
	}
	if (frag_color.a < limit) {
		discard;
	}
#endif
}