Debian Codegraph @

drivers/gles3/effects/cubemap_filter.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* cubemap_filter.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #ifdef GLES3_ENABLED
32
33 #include "cubemap_filter.h"
34
35 #include "../storage/texture_storage.h"
36 #include "core/config/project_settings.h"
37
38 using namespace GLES3;
39
40 CubemapFilter *CubemapFilter::singleton = nullptr;
41
42 CubemapFilter::CubemapFilter() {
43 singleton = this;
44 // Use a factor 4 larger for the compatibility renderer to make up for the fact
45 // That we don't use an array texture. We will reduce samples on low roughness
46 // to compensate.
47 ggx_samples = 4 * uint32_t(GLOBAL_GET("rendering/reflections/sky_reflections/ggx_samples"));
48
49 {
50 String defines;
51 defines += "\n#define MAX_SAMPLE_COUNT " + itos(ggx_samples) + "\n";
52 cubemap_filter.shader.initialize(defines);
53 cubemap_filter.shader_version = cubemap_filter.shader.version_create();
54 }
55
56 { // Screen Triangle.
57 glGenBuffers(1, &screen_triangle);
58 glBindBuffer(GL_ARRAY_BUFFER, screen_triangle);
59
60 const float qv[6] = {
61 -1.0f,
62 -1.0f,
63 -1.0f,
64 3.0f,
65 3.0f,
66 -1.0f,
67 };
68
69 glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6, qv, GL_STATIC_DRAW);
70 glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
71
72 glGenVertexArrays(1, &screen_triangle_array);
73 glBindVertexArray(screen_triangle_array);
74 glBindBuffer(GL_ARRAY_BUFFER, screen_triangle);
75 glVertexAttribPointer(RS::ARRAY_VERTEX, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, nullptr);
76 glEnableVertexAttribArray(RS::ARRAY_VERTEX);
77 glBindVertexArray(0);
78 glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
79 }
80 }
81
82 CubemapFilter::~CubemapFilter() {
83 glDeleteBuffers(1, &screen_triangle);
84 glDeleteVertexArrays(1, &screen_triangle_array);
85
86 cubemap_filter.shader.version_free(cubemap_filter.shader_version);
87 singleton = nullptr;
88 }
89
90 // Helper functions for IBL filtering
91
92 Vector3 importance_sample_GGX(Vector2 xi, float roughness4) {
93 // Compute distribution direction
94 float phi = 2.0 * Math::PI * xi.x;
95 float cos_theta = std::sqrt((1.0 - xi.y) / (1.0 + (roughness4 - 1.0) * xi.y));
96 float sin_theta = std::sqrt(1.0 - cos_theta * cos_theta);
97
98 // Convert to spherical direction
99 Vector3 half_vector;
100 half_vector.x = sin_theta * std::cos(phi);
101 half_vector.y = sin_theta * std::sin(phi);
102 half_vector.z = cos_theta;
103
104 return half_vector;
105 }
106
107 float distribution_GGX(float NdotH, float roughness4) {
108 float NdotH2 = NdotH * NdotH;
109 float denom = (NdotH2 * (roughness4 - 1.0) + 1.0);
110 denom = Math::PI * denom * denom;
111
112 return roughness4 / denom;
113 }
114
115 float radical_inverse_vdC(uint32_t bits) {
116 bits = (bits << 16) | (bits >> 16);
117 bits = ((bits & 0x55555555) << 1) | ((bits & 0xAAAAAAAA) >> 1);
118 bits = ((bits & 0x33333333) << 2) | ((bits & 0xCCCCCCCC) >> 2);
119 bits = ((bits & 0x0F0F0F0F) << 4) | ((bits & 0xF0F0F0F0) >> 4);
120 bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >> 8);
121
122 return float(bits) * 2.3283064365386963e-10;
123 }
124
125 Vector2 hammersley(uint32_t i, uint32_t N) {
126 return Vector2(float(i) / float(N), radical_inverse_vdC(i));
127 }
128
129 void CubemapFilter::filter_radiance(GLuint p_source_cubemap, GLuint p_dest_cubemap, GLuint p_dest_framebuffer, int p_source_size, int p_mipmap_count, int p_layer) {
130 glActiveTexture(GL_TEXTURE0);
131 glBindTexture(GL_TEXTURE_CUBE_MAP, p_source_cubemap);
132 glBindFramebuffer(GL_FRAMEBUFFER, p_dest_framebuffer);
133
134 CubemapFilterShaderGLES3::ShaderVariant mode = CubemapFilterShaderGLES3::MODE_DEFAULT;
135
136 if (p_layer == 0) {
137 glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
138 // Copy over base layer without filtering.
139 mode = CubemapFilterShaderGLES3::MODE_COPY;
140 }
141
142 int size = p_source_size >> p_layer;
143 glViewport(0, 0, size, size);
144 glBindVertexArray(screen_triangle_array);
145
146 bool success = cubemap_filter.shader.version_bind_shader(cubemap_filter.shader_version, mode);
147 if (!success) {
148 return;
149 }
150
151 if (p_layer > 0) {
152 const uint32_t sample_counts[5] = { 1, ggx_samples / 16, ggx_samples / 8, ggx_samples / 4, ggx_samples };
153 uint32_t sample_count = sample_counts[MIN(4, p_layer)];
154
155 float roughness = float(p_layer) / (p_mipmap_count - 1);
156 roughness *= roughness; // Convert to non-perceptual roughness.
157 float roughness4 = roughness * roughness;
158 roughness4 *= roughness4;
159
160 float solid_angle_texel = 4.0 * Math::PI / float(6 * size * size);
161
162 LocalVector<float> sample_directions;
163 sample_directions.resize(4 * sample_count);
164
165 uint32_t index = 0;
166 float weight = 0.0;
167 for (uint32_t i = 0; i < sample_count; i++) {
168 Vector2 xi = hammersley(i, sample_count);
169 Vector3 dir = importance_sample_GGX(xi, roughness4);
170 Vector3 light_vec = (2.0 * dir.z * dir - Vector3(0.0, 0.0, 1.0));
171
172 if (light_vec.z <= 0.0) {
173 continue;
174 }
175
176 sample_directions[index * 4] = light_vec.x;
177 sample_directions[index * 4 + 1] = light_vec.y;
178 sample_directions[index * 4 + 2] = light_vec.z;
179
180 float D = distribution_GGX(dir.z, roughness4);
181 float pdf = D * dir.z / (4.0 * dir.z) + 0.0001;
182
183 float solid_angle_sample = 1.0 / (float(sample_count) * pdf + 0.0001);
184
185 float mip_level = MAX(0.5 * std::log2(solid_angle_sample / solid_angle_texel) + float(MAX(1, p_layer - 3)), 1.0);
186
187 sample_directions[index * 4 + 3] = mip_level;
188 weight += light_vec.z;
189 index++;
190 }
191
192 glUniform4fv(cubemap_filter.shader.version_get_uniform(CubemapFilterShaderGLES3::SAMPLE_DIRECTIONS_MIP, cubemap_filter.shader_version, mode), sample_count, sample_directions.ptr());
193 cubemap_filter.shader.version_set_uniform(CubemapFilterShaderGLES3::WEIGHT, weight, cubemap_filter.shader_version, mode);
194 cubemap_filter.shader.version_set_uniform(CubemapFilterShaderGLES3::SAMPLE_COUNT, index, cubemap_filter.shader_version, mode);
195 }
196
197 for (int i = 0; i < 6; i++) {
198 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, p_dest_cubemap, p_layer);
199 #ifdef DEBUG_ENABLED
200 GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
201 if (status != GL_FRAMEBUFFER_COMPLETE) {
202 WARN_PRINT("Could not bind sky radiance face: " + itos(i) + ", status: " + GLES3::TextureStorage::get_singleton()->get_framebuffer_error(status));
203 }
204 #endif
205 cubemap_filter.shader.version_set_uniform(CubemapFilterShaderGLES3::FACE_ID, i, cubemap_filter.shader_version, mode);
206
207 glDrawArrays(GL_TRIANGLES, 0, 3);
208 }
209 glBindVertexArray(0);
210 glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);
211 }
212
213 #endif // GLES3_ENABLED