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