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