| 1 | /**************************************************************************/ |
| 2 | /* texture_rd.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 "texture_rd.h" |
| 32 | |
| 33 | #include "servers/rendering/rendering_server.h" |
| 34 | |
| 35 | //////////////////////////////////////////////////////////////////////////// |
| 36 | // Texture2DRD |
| 37 | |
| 38 | void Texture2DRD::_bind_methods() { |
| 39 | ClassDB::bind_method(D_METHOD("set_texture_rd_rid", "texture_rd_rid"), &Texture2DRD::set_texture_rd_rid); |
| 40 | ClassDB::bind_method(D_METHOD("get_texture_rd_rid"), &Texture2DRD::get_texture_rd_rid); |
| 41 | |
| 42 | ADD_PROPERTY(PropertyInfo(Variant::RID, "texture_rd_rid", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_texture_rd_rid", "get_texture_rd_rid"); |
| 43 | } |
| 44 | |
| 45 | int Texture2DRD::get_width() const { |
| 46 | return size.width; |
| 47 | } |
| 48 | |
| 49 | int Texture2DRD::get_height() const { |
| 50 | return size.height; |
| 51 | } |
| 52 | |
| 53 | RID Texture2DRD::get_rid() const { |
| 54 | if (texture_rid.is_null()) { |
| 55 | // We are in trouble, create something temporary. |
| 56 | texture_rid = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 57 | } |
| 58 | |
| 59 | return texture_rid; |
| 60 | } |
| 61 | |
| 62 | bool Texture2DRD::has_alpha() const { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | Ref<Image> Texture2DRD::get_image() const { |
| 67 | ERR_FAIL_NULL_V(RS::get_singleton(), Ref<Image>()); |
| 68 | if (texture_rid.is_valid()) { |
| 69 | return RS::get_singleton()->texture_2d_get(texture_rid); |
| 70 | } else { |
| 71 | return Ref<Image>(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void Texture2DRD::set_texture_rd_rid(RID p_texture_rd_rid) { |
| 76 | ERR_FAIL_NULL(RS::get_singleton()); |
| 77 | |
| 78 | if (p_texture_rd_rid.is_valid()) { |
| 79 | RS::get_singleton()->call_on_render_thread(callable_mp(this, &Texture2DRD::_set_texture_rd_rid).bind(p_texture_rd_rid)); |
| 80 | } else if (texture_rid.is_valid()) { |
| 81 | RS::get_singleton()->free_rid(texture_rid); |
| 82 | texture_rid = RID(); |
| 83 | size = Size2i(); |
| 84 | |
| 85 | notify_property_list_changed(); |
| 86 | emit_changed(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void Texture2DRD::_set_texture_rd_rid(RID p_texture_rd_rid) { |
| 91 | ERR_FAIL_NULL(RD::get_singleton()); |
| 92 | ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid)); |
| 93 | |
| 94 | RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid); |
| 95 | ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_2D); |
| 96 | ERR_FAIL_COND(tf.depth > 1); |
| 97 | ERR_FAIL_COND(tf.array_layers > 1); |
| 98 | |
| 99 | size.width = tf.width; |
| 100 | size.height = tf.height; |
| 101 | |
| 102 | texture_rd_rid = p_texture_rd_rid; |
| 103 | |
| 104 | if (texture_rid.is_valid()) { |
| 105 | RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid)); |
| 106 | } else { |
| 107 | texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid); |
| 108 | } |
| 109 | |
| 110 | notify_property_list_changed(); |
| 111 | emit_changed(); |
| 112 | } |
| 113 | |
| 114 | RID Texture2DRD::get_texture_rd_rid() const { |
| 115 | return texture_rd_rid; |
| 116 | } |
| 117 | |
| 118 | Texture2DRD::Texture2DRD() { |
| 119 | size = Size2i(); |
| 120 | } |
| 121 | |
| 122 | Texture2DRD::~Texture2DRD() { |
| 123 | if (texture_rid.is_valid()) { |
| 124 | ERR_FAIL_NULL(RS::get_singleton()); |
| 125 | RS::get_singleton()->free_rid(texture_rid); |
| 126 | texture_rid = RID(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | //////////////////////////////////////////////////////////////////////////// |
| 131 | // TextureLayeredRD |
| 132 | |
| 133 | void TextureLayeredRD::_bind_methods() { |
| 134 | ClassDB::bind_method(D_METHOD("set_texture_rd_rid", "texture_rd_rid"), &TextureLayeredRD::set_texture_rd_rid); |
| 135 | ClassDB::bind_method(D_METHOD("get_texture_rd_rid"), &TextureLayeredRD::get_texture_rd_rid); |
| 136 | |
| 137 | ADD_PROPERTY(PropertyInfo(Variant::RID, "texture_rd_rid", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_texture_rd_rid", "get_texture_rd_rid"); |
| 138 | } |
| 139 | |
| 140 | TextureLayered::LayeredType TextureLayeredRD::get_layered_type() const { |
| 141 | return layer_type; |
| 142 | } |
| 143 | |
| 144 | Image::Format TextureLayeredRD::get_format() const { |
| 145 | return image_format; |
| 146 | } |
| 147 | |
| 148 | int TextureLayeredRD::get_width() const { |
| 149 | return size.width; |
| 150 | } |
| 151 | |
| 152 | int TextureLayeredRD::get_height() const { |
| 153 | return size.height; |
| 154 | } |
| 155 | |
| 156 | int TextureLayeredRD::get_layers() const { |
| 157 | return (int)layers; |
| 158 | } |
| 159 | |
| 160 | bool TextureLayeredRD::has_mipmaps() const { |
| 161 | return mipmaps > 1; |
| 162 | } |
| 163 | |
| 164 | RID TextureLayeredRD::get_rid() const { |
| 165 | if (texture_rid.is_null()) { |
| 166 | // We are in trouble, create something temporary. |
| 167 | texture_rid = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 168 | } |
| 169 | |
| 170 | return texture_rid; |
| 171 | } |
| 172 | |
| 173 | Ref<Image> TextureLayeredRD::get_layer_data(int p_layer) const { |
| 174 | ERR_FAIL_INDEX_V(p_layer, (int)layers, Ref<Image>()); |
| 175 | return RS::get_singleton()->texture_2d_layer_get(texture_rid, p_layer); |
| 176 | } |
| 177 | |
| 178 | void TextureLayeredRD::set_texture_rd_rid(RID p_texture_rd_rid) { |
| 179 | ERR_FAIL_NULL(RS::get_singleton()); |
| 180 | |
| 181 | if (p_texture_rd_rid.is_valid()) { |
| 182 | RS::get_singleton()->call_on_render_thread(callable_mp(this, &TextureLayeredRD::_set_texture_rd_rid).bind(p_texture_rd_rid)); |
| 183 | } else if (texture_rid.is_valid()) { |
| 184 | RS::get_singleton()->free_rid(texture_rid); |
| 185 | texture_rid = RID(); |
| 186 | image_format = Image::FORMAT_MAX; |
| 187 | size = Size2i(); |
| 188 | layers = 0; |
| 189 | mipmaps = 0; |
| 190 | |
| 191 | notify_property_list_changed(); |
| 192 | emit_changed(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void TextureLayeredRD::_set_texture_rd_rid(RID p_texture_rd_rid) { |
| 197 | ERR_FAIL_NULL(RD::get_singleton()); |
| 198 | ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid)); |
| 199 | |
| 200 | RS::TextureLayeredType rs_layer_type; |
| 201 | RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid); |
| 202 | ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_2D_ARRAY && tf.texture_type != RD::TEXTURE_TYPE_CUBE && tf.texture_type != RD::TEXTURE_TYPE_CUBE_ARRAY); |
| 203 | ERR_FAIL_COND(tf.depth > 1); |
| 204 | switch (layer_type) { |
| 205 | case LAYERED_TYPE_2D_ARRAY: { |
| 206 | ERR_FAIL_COND(tf.array_layers <= 1); |
| 207 | rs_layer_type = RS::TEXTURE_LAYERED_2D_ARRAY; |
| 208 | } break; |
| 209 | case LAYERED_TYPE_CUBEMAP: { |
| 210 | ERR_FAIL_COND(tf.array_layers != 6); |
| 211 | rs_layer_type = RS::TEXTURE_LAYERED_CUBEMAP; |
| 212 | } break; |
| 213 | case LAYERED_TYPE_CUBEMAP_ARRAY: { |
| 214 | ERR_FAIL_COND((tf.array_layers == 0) || ((tf.array_layers % 6) != 0)); |
| 215 | rs_layer_type = RS::TEXTURE_LAYERED_CUBEMAP_ARRAY; |
| 216 | } break; |
| 217 | default: { |
| 218 | ERR_FAIL_MSG("Unknown layer type selected"); |
| 219 | } break; |
| 220 | } |
| 221 | |
| 222 | size.width = tf.width; |
| 223 | size.height = tf.height; |
| 224 | layers = tf.array_layers; |
| 225 | mipmaps = tf.mipmaps; |
| 226 | |
| 227 | texture_rd_rid = p_texture_rd_rid; |
| 228 | |
| 229 | if (texture_rid.is_valid()) { |
| 230 | RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid, rs_layer_type)); |
| 231 | } else { |
| 232 | texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid, rs_layer_type); |
| 233 | } |
| 234 | |
| 235 | image_format = RS::get_singleton()->texture_get_format(texture_rid); |
| 236 | |
| 237 | notify_property_list_changed(); |
| 238 | emit_changed(); |
| 239 | } |
| 240 | |
| 241 | RID TextureLayeredRD::get_texture_rd_rid() const { |
| 242 | return texture_rd_rid; |
| 243 | } |
| 244 | |
| 245 | TextureLayeredRD::TextureLayeredRD(LayeredType p_layer_type) { |
| 246 | layer_type = p_layer_type; |
| 247 | size = Size2i(); |
| 248 | image_format = Image::FORMAT_MAX; |
| 249 | layers = 0; |
| 250 | mipmaps = 0; |
| 251 | } |
| 252 | |
| 253 | TextureLayeredRD::~TextureLayeredRD() { |
| 254 | if (texture_rid.is_valid()) { |
| 255 | ERR_FAIL_NULL(RS::get_singleton()); |
| 256 | RS::get_singleton()->free_rid(texture_rid); |
| 257 | texture_rid = RID(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | //////////////////////////////////////////////////////////////////////////// |
| 262 | // Texture3DRD |
| 263 | |
| 264 | void Texture3DRD::_bind_methods() { |
| 265 | ClassDB::bind_method(D_METHOD("set_texture_rd_rid", "texture_rd_rid"), &Texture3DRD::set_texture_rd_rid); |
| 266 | ClassDB::bind_method(D_METHOD("get_texture_rd_rid"), &Texture3DRD::get_texture_rd_rid); |
| 267 | |
| 268 | ADD_PROPERTY(PropertyInfo(Variant::RID, "texture_rd_rid", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_texture_rd_rid", "get_texture_rd_rid"); |
| 269 | } |
| 270 | |
| 271 | Image::Format Texture3DRD::get_format() const { |
| 272 | return image_format; |
| 273 | } |
| 274 | |
| 275 | int Texture3DRD::get_width() const { |
| 276 | return size.x; |
| 277 | } |
| 278 | |
| 279 | int Texture3DRD::get_height() const { |
| 280 | return size.y; |
| 281 | } |
| 282 | |
| 283 | int Texture3DRD::get_depth() const { |
| 284 | return size.z; |
| 285 | } |
| 286 | |
| 287 | bool Texture3DRD::has_mipmaps() const { |
| 288 | return mipmaps > 1; |
| 289 | } |
| 290 | |
| 291 | RID Texture3DRD::get_rid() const { |
| 292 | if (texture_rid.is_null()) { |
| 293 | // We are in trouble, create something temporary. |
| 294 | texture_rid = RenderingServer::get_singleton()->texture_2d_placeholder_create(); |
| 295 | } |
| 296 | |
| 297 | return texture_rid; |
| 298 | } |
| 299 | |
| 300 | void Texture3DRD::set_texture_rd_rid(RID p_texture_rd_rid) { |
| 301 | ERR_FAIL_NULL(RS::get_singleton()); |
| 302 | |
| 303 | if (p_texture_rd_rid.is_valid()) { |
| 304 | RS::get_singleton()->call_on_render_thread(callable_mp(this, &Texture3DRD::_set_texture_rd_rid).bind(p_texture_rd_rid)); |
| 305 | } else if (texture_rid.is_valid()) { |
| 306 | RS::get_singleton()->free_rid(texture_rid); |
| 307 | texture_rid = RID(); |
| 308 | image_format = Image::FORMAT_MAX; |
| 309 | size = Vector3i(); |
| 310 | mipmaps = 0; |
| 311 | |
| 312 | notify_property_list_changed(); |
| 313 | emit_changed(); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void Texture3DRD::_set_texture_rd_rid(RID p_texture_rd_rid) { |
| 318 | ERR_FAIL_NULL(RD::get_singleton()); |
| 319 | ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid)); |
| 320 | |
| 321 | RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid); |
| 322 | ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_3D); |
| 323 | ERR_FAIL_COND(tf.array_layers > 1); |
| 324 | |
| 325 | size.x = tf.width; |
| 326 | size.y = tf.height; |
| 327 | size.z = tf.depth; |
| 328 | mipmaps = tf.mipmaps; |
| 329 | |
| 330 | texture_rd_rid = p_texture_rd_rid; |
| 331 | |
| 332 | if (texture_rid.is_valid()) { |
| 333 | RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid)); |
| 334 | } else { |
| 335 | texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid); |
| 336 | } |
| 337 | |
| 338 | image_format = RS::get_singleton()->texture_get_format(texture_rid); |
| 339 | |
| 340 | notify_property_list_changed(); |
| 341 | emit_changed(); |
| 342 | } |
| 343 | |
| 344 | RID Texture3DRD::get_texture_rd_rid() const { |
| 345 | return texture_rd_rid; |
| 346 | } |
| 347 | |
| 348 | Texture3DRD::Texture3DRD() { |
| 349 | image_format = Image::FORMAT_MAX; |
| 350 | size = Vector3i(); |
| 351 | mipmaps = 0; |
| 352 | } |
| 353 | |
| 354 | Texture3DRD::~Texture3DRD() { |
| 355 | if (texture_rid.is_valid()) { |
| 356 | ERR_FAIL_NULL(RS::get_singleton()); |
| 357 | RS::get_singleton()->free_rid(texture_rid); |
| 358 | texture_rid = RID(); |
| 359 | } |
| 360 | } |