Debian Codegraph @

servers/rendering/renderer_rd/shaders/effects/octmap_downsampler.glsl

raw
1 // Copyright 2016 Activision Publishing, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the "Software"),
5 // to deal in the Software without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Software, and to permit persons to whom the Software
8 // is furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in all
11 // copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20
21 #[compute]
22
23 #version 450
24
25 #VERSION_DEFINES
26
27 #define BLOCK_SIZE 8
28
29 #include "../oct_inc.glsl"
30
31 layout(local_size_x = BLOCK_SIZE, local_size_y = BLOCK_SIZE, local_size_z = 1) in;
32
33 layout(set = 0, binding = 0) uniform sampler2D source_octmap;
34
35 layout(OCTMAP_FORMAT, set = 1, binding = 0) uniform restrict writeonly image2D dest_octmap;
36
37 layout(push_constant, std430) uniform Params {
38 float border_size;
39 uint size;
40 uint pad;
41 uint pad2;
42 }
43 params;
44
45 // Use an approximation of the Jacobian.
46 float calcWeight(float u, float v) {
47 vec3 d = oct_to_vec3_with_border(vec2(u, v) * 0.5 + 0.5, params.border_size);
48 return 1.0 / pow(abs(d.z) + 1.0, 3.0);
49 }
50
51 void main() {
52 uvec2 id = gl_GlobalInvocationID.xy;
53 if (id.x < params.size && id.y < params.size) {
54 float inv_size = 1.0 / float(params.size);
55 uvec2 sample_id = id;
56
57 float u0 = (float(sample_id.x) * 2.0f + 1.0f - 0.75f) * inv_size - 1.0f;
58 float u1 = (float(sample_id.x) * 2.0f + 1.0f + 0.75f) * inv_size - 1.0f;
59 float v0 = (float(sample_id.y) * 2.0f + 1.0f - 0.75f) * inv_size - 1.0f;
60 float v1 = (float(sample_id.y) * 2.0f + 1.0f + 0.75f) * inv_size - 1.0f;
61 float weights[4];
62 weights[0] = calcWeight(u0, v0);
63 weights[1] = calcWeight(u1, v0);
64 weights[2] = calcWeight(u0, v1);
65 weights[3] = calcWeight(u1, v1);
66
67 const float wsum = 0.5 / (weights[0] + weights[1] + weights[2] + weights[3]);
68 for (int i = 0; i < 4; i++) {
69 weights[i] = weights[i] * wsum + 0.125;
70 }
71
72 vec4 color = textureLod(source_octmap, vec2(u0, v0) * 0.5f + 0.5f, 0.0) * weights[0];
73 color += textureLod(source_octmap, vec2(u1, v0) * 0.5f + 0.5f, 0.0) * weights[1];
74 color += textureLod(source_octmap, vec2(u0, v1) * 0.5f + 0.5f, 0.0) * weights[2];
75 color += textureLod(source_octmap, vec2(u1, v1) * 0.5f + 0.5f, 0.0) * weights[3];
76 imageStore(dest_octmap, ivec2(id), color);
77 }
78 }