rasterizer_array_gles2.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*************************************************************************/
  2. /* rasterizer_array_gles2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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. #pragma once
  31. /*************************************************************************/
  32. /* rasterizer_array_gles2.h */
  33. /*************************************************************************/
  34. /* This file is part of: */
  35. /* GODOT ENGINE */
  36. /* https://godotengine.org */
  37. /*************************************************************************/
  38. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  39. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  40. /* */
  41. /* Permission is hereby granted, free of charge, to any person obtaining */
  42. /* a copy of this software and associated documentation files (the */
  43. /* "Software"), to deal in the Software without restriction, including */
  44. /* without limitation the rights to use, copy, modify, merge, publish, */
  45. /* distribute, sublicense, and/or sell copies of the Software, and to */
  46. /* permit persons to whom the Software is furnished to do so, subject to */
  47. /* the following conditions: */
  48. /* */
  49. /* The above copyright notice and this permission notice shall be */
  50. /* included in all copies or substantial portions of the Software. */
  51. /* */
  52. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  53. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  54. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  55. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  56. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  57. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  58. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  59. /*************************************************************************/
  60. /**
  61. * Fast single-threaded growable array for POD types.
  62. * For use in render drivers, not for general use.
  63. * TO BE REPLACED by local_vector.
  64. */
  65. #include "core/os/memory.h"
  66. #include "core/vector.h"
  67. #include <string.h>
  68. template <class T>
  69. class RasterizerArrayGLES2 {
  70. public:
  71. RasterizerArrayGLES2() {
  72. _list = 0;
  73. _size = 0;
  74. _max_size = 0;
  75. }
  76. ~RasterizerArrayGLES2() { free(); }
  77. T &operator[](unsigned int ui) { return _list[ui]; }
  78. const T &operator[](unsigned int ui) const { return _list[ui]; }
  79. void free() {
  80. if (_list) {
  81. memdelete_arr(_list);
  82. _list = 0;
  83. }
  84. _size = 0;
  85. _max_size = 0;
  86. }
  87. void create(int p_size) {
  88. free();
  89. if (p_size) {
  90. _list = memnew_arr(T, p_size);
  91. }
  92. _size = 0;
  93. _max_size = p_size;
  94. }
  95. void reset() { _size = 0; }
  96. T *request_with_grow() {
  97. T *p = request();
  98. if (!p) {
  99. grow();
  100. return request_with_grow();
  101. }
  102. return p;
  103. }
  104. // none of that inefficient pass by value stuff here, thanks
  105. T *request() {
  106. if (_size < _max_size) {
  107. return &_list[_size++];
  108. }
  109. return 0;
  110. }
  111. // several items at a time
  112. T *request(int p_num_items) {
  113. int old_size = _size;
  114. _size += p_num_items;
  115. if (_size <= _max_size) {
  116. return &_list[old_size];
  117. }
  118. // revert
  119. _size = old_size;
  120. return 0;
  121. }
  122. int size() const { return _size; }
  123. int max_size() const { return _max_size; }
  124. const T *get_data() const { return _list; }
  125. bool copy_from(const RasterizerArrayGLES2<T> &o) {
  126. // no resizing done here, it should be done manually
  127. if (o.size() > _max_size)
  128. return false;
  129. // pod types only please!
  130. memcpy(_list, o.get_data(), o.size() * sizeof(T));
  131. _size = o.size();
  132. return true;
  133. }
  134. // if you want this to be cheap, call reset before grow,
  135. // to ensure there is no data to copy
  136. void grow() {
  137. unsigned int new_max_size = _max_size * 2;
  138. if (!new_max_size)
  139. new_max_size = 1;
  140. T *new_list = memnew_arr(T, new_max_size);
  141. // copy .. pod types only
  142. if (_list) {
  143. memcpy(new_list, _list, _size * sizeof(T));
  144. }
  145. unsigned int new_size = size();
  146. free();
  147. _list = new_list;
  148. _size = new_size;
  149. _max_size = new_max_size;
  150. }
  151. private:
  152. T *_list;
  153. int _size;
  154. int _max_size;
  155. };
  156. template <class T>
  157. class RasterizerArray_non_pod_GLES2 {
  158. public:
  159. RasterizerArray_non_pod_GLES2() {
  160. _size = 0;
  161. }
  162. const T &operator[](unsigned int ui) const { return _list[ui]; }
  163. void create(int p_size) {
  164. _list.resize(p_size);
  165. _size = 0;
  166. }
  167. void reset() { _size = 0; }
  168. void push_back(const T &val) {
  169. while (true) {
  170. if (_size < max_size()) {
  171. _list.set(_size, val);
  172. _size++;
  173. return;
  174. }
  175. grow();
  176. }
  177. }
  178. int size() const { return _size; }
  179. int max_size() const { return _list.size(); }
  180. private:
  181. void grow() {
  182. unsigned int new_max_size = _list.size() * 2;
  183. if (!new_max_size)
  184. new_max_size = 1;
  185. _list.resize(new_max_size);
  186. }
  187. Vector<T> _list;
  188. int _size;
  189. };