Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/servers/xr/xr_vrs.cpp
10277 views
1
/**************************************************************************/
2
/* xr_vrs.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 "xr_vrs.h"
32
33
#include "servers/rendering/renderer_scene_render.h"
34
#include "servers/rendering_server.h"
35
36
void XRVRS::_bind_methods() {
37
ClassDB::bind_method(D_METHOD("get_vrs_min_radius"), &XRVRS::get_vrs_min_radius);
38
ClassDB::bind_method(D_METHOD("set_vrs_min_radius", "radius"), &XRVRS::set_vrs_min_radius);
39
40
ClassDB::bind_method(D_METHOD("get_vrs_strength"), &XRVRS::get_vrs_strength);
41
ClassDB::bind_method(D_METHOD("set_vrs_strength", "strength"), &XRVRS::set_vrs_strength);
42
43
ClassDB::bind_method(D_METHOD("get_vrs_render_region"), &XRVRS::get_vrs_render_region);
44
ClassDB::bind_method(D_METHOD("set_vrs_render_region", "render_region"), &XRVRS::set_vrs_render_region);
45
46
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_min_radius", PROPERTY_HINT_RANGE, "1.0,100.0,1.0"), "set_vrs_min_radius", "get_vrs_min_radius");
47
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "vrs_strength", PROPERTY_HINT_RANGE, "0.1,10.0,0.1"), "set_vrs_strength", "get_vrs_strength");
48
ADD_PROPERTY(PropertyInfo(Variant::RECT2I, "vrs_render_region"), "set_vrs_render_region", "get_vrs_render_region");
49
50
ClassDB::bind_method(D_METHOD("make_vrs_texture", "target_size", "eye_foci"), &XRVRS::make_vrs_texture);
51
}
52
53
XRVRS::~XRVRS() {
54
if (vrs_texture.is_valid()) {
55
ERR_FAIL_NULL(RS::get_singleton());
56
RS::get_singleton()->free(vrs_texture);
57
vrs_texture = RID();
58
}
59
}
60
61
float XRVRS::get_vrs_min_radius() const {
62
return vrs_min_radius;
63
}
64
65
void XRVRS::set_vrs_min_radius(float p_vrs_min_radius) {
66
if (p_vrs_min_radius < 1.0) {
67
WARN_PRINT_ONCE("VRS minimum radius can not be set below 1.0");
68
vrs_min_radius = 1.0;
69
} else if (p_vrs_min_radius > 100.0) {
70
WARN_PRINT_ONCE("VRS minimum radius can not be set above 100.0");
71
vrs_min_radius = 100.0;
72
} else {
73
vrs_min_radius = p_vrs_min_radius;
74
vrs_dirty = true;
75
}
76
}
77
78
float XRVRS::get_vrs_strength() const {
79
return vrs_strength;
80
}
81
82
void XRVRS::set_vrs_strength(float p_vrs_strength) {
83
if (p_vrs_strength < 0.1) {
84
WARN_PRINT_ONCE("VRS strength can not be set below 0.1");
85
vrs_strength = 0.1;
86
} else if (p_vrs_strength > 10.0) {
87
WARN_PRINT_ONCE("VRS strength can not be set above 10.0");
88
vrs_strength = 10.0;
89
} else {
90
vrs_strength = p_vrs_strength;
91
vrs_dirty = true;
92
}
93
}
94
95
Rect2i XRVRS::get_vrs_render_region() const {
96
return vrs_render_region;
97
}
98
99
void XRVRS::set_vrs_render_region(const Rect2i &p_vrs_render_region) {
100
vrs_render_region = p_vrs_render_region;
101
vrs_dirty = true;
102
}
103
104
RID XRVRS::make_vrs_texture(const Size2 &p_target_size, const PackedVector2Array &p_eye_foci) {
105
ERR_FAIL_COND_V(p_eye_foci.is_empty(), RID());
106
107
Size2i texel_size = RD::get_singleton()->vrs_get_texel_size();
108
109
// Should return sensible data or graphics API does not support VRS.
110
ERR_FAIL_COND_V(texel_size.x < 1 || texel_size.y < 1, RID());
111
112
Size2 vrs_size = Size2(0.5 + p_target_size.x / texel_size.x, 0.5 + p_target_size.y / texel_size.y).floor();
113
114
// Make sure we have at least one pixel.
115
vrs_size = vrs_size.maxf(1.0);
116
117
float max_radius = 0.5 * MIN(vrs_size.x, vrs_size.y); // Maximum radius that fits inside of our image
118
float min_radius = vrs_min_radius * max_radius / 100.0; // Minimum radius as a percentage of our size
119
real_t outer_radius = MAX(1.0, (max_radius - min_radius) / vrs_strength);
120
Size2 vrs_sizei = vrs_size;
121
122
// Our density map is now unified, with a value of (0.0, 0.0) meaning a 1x1 texel size and (1.0, 1.0) an max texel size.
123
// For our standard VRS extension on Vulkan this means a maximum of 8x8.
124
// For the density map extension this scales depending on the max texel size.
125
126
if (target_size != vrs_sizei || eye_foci != p_eye_foci || vrs_dirty) {
127
// Out with the old.
128
if (vrs_texture.is_valid()) {
129
RS::get_singleton()->free(vrs_texture);
130
vrs_texture = RID();
131
}
132
133
// In with the new.
134
Vector<Ref<Image>> images;
135
target_size = vrs_sizei;
136
eye_foci = p_eye_foci;
137
138
Size2 region_ratio = Size2(1.0, 1.0);
139
Point2i region_offset;
140
if (vrs_render_region != Rect2i()) {
141
region_ratio = (Size2)vrs_render_region.size / p_target_size;
142
region_offset = (Point2)vrs_render_region.position / p_target_size * vrs_sizei;
143
}
144
145
for (int i = 0; i < eye_foci.size() && i < RendererSceneRender::MAX_RENDER_VIEWS; i++) {
146
PackedByteArray data;
147
data.resize(vrs_sizei.x * vrs_sizei.y * 2);
148
uint8_t *data_ptr = data.ptrw();
149
150
Vector2i view_center;
151
view_center.x = int(vrs_size.x * (eye_foci[i].x + 1.0) * region_ratio.x * 0.5) + region_offset.x;
152
view_center.y = int(vrs_size.y * (-eye_foci[i].y + 1.0) * region_ratio.y * 0.5) + region_offset.y;
153
154
int d = 0;
155
for (int y = 0; y < vrs_sizei.y; y++) {
156
for (int x = 0; x < vrs_sizei.x; x++) {
157
// Generate a density map that represents the distance to the view focus point. While this leaves the opportunities
158
// offered by the density map being different in each direction currently unused, it was found to give better tile
159
// distribution on hardware that supports the feature natively. This area is open to improvements in the future.
160
Vector2 offset = Vector2(x - view_center.x, y - view_center.y) / region_ratio;
161
real_t density = MAX(offset.length() - min_radius, 0.0) / outer_radius;
162
data_ptr[d++] = CLAMP(255.0 * density, 0, 255);
163
data_ptr[d++] = CLAMP(255.0 * density, 0, 255);
164
}
165
}
166
images.push_back(Image::create_from_data(vrs_sizei.x, vrs_sizei.y, false, Image::FORMAT_RG8, data));
167
}
168
169
if (images.size() == 1) {
170
vrs_texture = RS::get_singleton()->texture_2d_create(images[0]);
171
} else {
172
vrs_texture = RS::get_singleton()->texture_2d_layered_create(images, RS::TEXTURE_LAYERED_2D_ARRAY);
173
}
174
175
vrs_dirty = false;
176
}
177
178
return vrs_texture;
179
}
180
181