Debian Codegraph @

servers/rendering/renderer_scene_occlusion_cull.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* renderer_scene_occlusion_cull.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 #include "renderer_scene_occlusion_cull.h"
32
33 RendererSceneOcclusionCull *RendererSceneOcclusionCull::singleton = nullptr;
34
35 bool RendererSceneOcclusionCull::HZBuffer::occlusion_jitter_enabled = false;
36
37 void RendererSceneOcclusionCull::HZBuffer::clear() {
38 if (sizes.is_empty()) {
39 return; // Already cleared
40 }
41
42 data.clear();
43 sizes.clear();
44 mips.clear();
45
46 debug_data.clear();
47 if (debug_image.is_valid()) {
48 debug_image.unref();
49 }
50
51 ERR_FAIL_NULL(RenderingServer::get_singleton());
52 RS::get_singleton()->free_rid(debug_texture);
53 }
54
55 void RendererSceneOcclusionCull::HZBuffer::resize(const Size2i &p_size) {
56 occlusion_buffer_size = p_size;
57
58 if (p_size == Size2i()) {
59 clear();
60 return;
61 }
62
63 if (!sizes.is_empty() && p_size == sizes[0]) {
64 return; // Size didn't change
65 }
66
67 int mip_count = 0;
68 int data_size = 0;
69 int w = p_size.x;
70 int h = p_size.y;
71
72 while (true) {
73 data_size += h * w;
74
75 w = MAX(1, w >> 1);
76 h = MAX(1, h >> 1);
77
78 mip_count++;
79
80 if (w == 1U && h == 1U) {
81 data_size += 1U;
82 mip_count++;
83 break;
84 }
85 }
86
87 data.resize(data_size);
88 mips.resize(mip_count);
89 sizes.resize(mip_count);
90
91 w = p_size.x;
92 h = p_size.y;
93 float *ptr = data.ptr();
94
95 for (int i = 0; i < mip_count; i++) {
96 sizes[i] = Size2i(w, h);
97 mips[i] = ptr;
98
99 ptr = &ptr[w * h];
100 w = MAX(1, w >> 1);
101 h = MAX(1, h >> 1);
102 }
103
104 for (int i = 0; i < data_size; i++) {
105 data[i] = FLT_MAX;
106 }
107
108 debug_data.resize(sizes[0].x * sizes[0].y);
109 if (debug_texture.is_valid()) {
110 RS::get_singleton()->free_rid(debug_texture);
111 debug_texture = RID();
112 }
113 }
114
115 void RendererSceneOcclusionCull::HZBuffer::update_mips() {
116 // Keep this up to date as a local to be used for occlusion timers.
117 occlusion_frame = Engine::get_singleton()->get_frames_drawn();
118
119 if (sizes.is_empty()) {
120 return;
121 }
122
123 for (uint32_t mip = 1; mip < mips.size(); mip++) {
124 for (int y = 0; y < sizes[mip].y; y++) {
125 for (int x = 0; x < sizes[mip].x; x++) {
126 int prev_x = x * 2;
127 int prev_y = y * 2;
128
129 int prev_w = sizes[mip - 1].width;
130 int prev_h = sizes[mip - 1].height;
131
132 bool odd_w = (prev_w % 2) != 0;
133 bool odd_h = (prev_h % 2) != 0;
134
135 #define CHECK_OFFSET(xx, yy) max_depth = MAX(max_depth, mips[mip - 1][MIN(prev_h - 1, prev_y + (yy)) * prev_w + MIN(prev_w - 1, prev_x + (xx))])
136
137 float max_depth = mips[mip - 1][prev_y * sizes[mip - 1].x + prev_x];
138 CHECK_OFFSET(0, 1);
139 CHECK_OFFSET(1, 0);
140 CHECK_OFFSET(1, 1);
141
142 if (odd_w) {
143 CHECK_OFFSET(2, 0);
144 CHECK_OFFSET(2, 1);
145 }
146
147 if (odd_h) {
148 CHECK_OFFSET(0, 2);
149 CHECK_OFFSET(1, 2);
150 }
151
152 if (odd_w && odd_h) {
153 CHECK_OFFSET(2, 2);
154 }
155
156 mips[mip][y * sizes[mip].x + x] = max_depth;
157 #undef CHECK_OFFSET
158 }
159 }
160 }
161 }
162
163 RID RendererSceneOcclusionCull::HZBuffer::get_debug_texture() {
164 if (sizes.is_empty() || sizes[0] == Size2i()) {
165 return RID();
166 }
167
168 if (debug_image.is_null()) {
169 debug_image.instantiate();
170 }
171
172 unsigned char *ptrw = debug_data.ptrw();
173 for (int i = 0; i < debug_data.size(); i++) {
174 ptrw[i] = MIN(Math::log(1.0 + mips[0][i]) / Math::log(1.0 + debug_tex_range), 1.0) * 255;
175 }
176
177 debug_image->set_data(sizes[0].x, sizes[0].y, false, Image::FORMAT_L8, debug_data);
178
179 if (debug_texture.is_null()) {
180 debug_texture = RS::get_singleton()->texture_2d_create(debug_image);
181 } else {
182 RenderingServer::get_singleton()->texture_2d_update(debug_texture, debug_image);
183 }
184
185 return debug_texture;
186 }