texture_rd.cpp 11 KB

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