rasterizer_array.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**************************************************************************/
  2. /* rasterizer_array.h */
  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. #ifndef RASTERIZER_ARRAY_H
  31. #define RASTERIZER_ARRAY_H
  32. /**
  33. * Fast single-threaded growable array for POD types.
  34. * For use in render drivers, not for general use.
  35. * TO BE REPLACED by local_vector.
  36. */
  37. #include "core/os/memory.h"
  38. #include "core/vector.h"
  39. #include <string.h>
  40. // very simple non-growable array, that keeps track of the size of a 'unit'
  41. // which can be cast to whatever vertex format FVF required, and is initially
  42. // created with enough memory to hold the biggest FVF.
  43. // This allows multiple FVFs to use the same array.
  44. class RasterizerUnitArrayGLES2 {
  45. public:
  46. RasterizerUnitArrayGLES2() {
  47. _list = nullptr;
  48. free();
  49. }
  50. ~RasterizerUnitArrayGLES2() { free(); }
  51. uint8_t *get_unit(unsigned int ui) { return &_list[ui * _unit_size_bytes]; }
  52. const uint8_t *get_unit(unsigned int ui) const { return &_list[ui * _unit_size_bytes]; }
  53. int size() const { return _size; }
  54. int max_size() const { return _max_size; }
  55. void free() {
  56. if (_list) {
  57. memdelete_arr(_list);
  58. _list = nullptr;
  59. }
  60. _size = 0;
  61. _max_size = 0;
  62. _max_size_bytes = 0;
  63. _unit_size_bytes = 0;
  64. }
  65. void create(int p_max_size_units, int p_max_unit_size_bytes) {
  66. free();
  67. _max_unit_size_bytes = p_max_unit_size_bytes;
  68. _max_size = p_max_size_units;
  69. _max_size_bytes = p_max_size_units * p_max_unit_size_bytes;
  70. if (_max_size_bytes) {
  71. _list = memnew_arr(uint8_t, _max_size_bytes);
  72. }
  73. }
  74. void prepare(int p_unit_size_bytes) {
  75. _unit_size_bytes = p_unit_size_bytes;
  76. _size = 0;
  77. }
  78. // several items at a time
  79. uint8_t *request(int p_num_items = 1) {
  80. int old_size = _size;
  81. _size += p_num_items;
  82. if (_size <= _max_size) {
  83. return get_unit(old_size);
  84. }
  85. // revert
  86. _size = old_size;
  87. return nullptr;
  88. }
  89. private:
  90. uint8_t *_list;
  91. int _size; // in units
  92. int _max_size; // in units
  93. int _max_size_bytes;
  94. int _unit_size_bytes;
  95. int _max_unit_size_bytes;
  96. };
  97. template <class T>
  98. class RasterizerArray {
  99. public:
  100. RasterizerArray() {
  101. _list = nullptr;
  102. _size = 0;
  103. _max_size = 0;
  104. }
  105. ~RasterizerArray() { free(); }
  106. T &operator[](unsigned int ui) { return _list[ui]; }
  107. const T &operator[](unsigned int ui) const { return _list[ui]; }
  108. void free() {
  109. if (_list) {
  110. memdelete_arr(_list);
  111. _list = nullptr;
  112. }
  113. _size = 0;
  114. _max_size = 0;
  115. }
  116. void create(int p_size) {
  117. free();
  118. if (p_size) {
  119. _list = memnew_arr(T, p_size);
  120. }
  121. _size = 0;
  122. _max_size = p_size;
  123. }
  124. void reset() { _size = 0; }
  125. T *request_with_grow() {
  126. T *p = request();
  127. if (!p) {
  128. grow();
  129. return request_with_grow();
  130. }
  131. return p;
  132. }
  133. // none of that inefficient pass by value stuff here, thanks
  134. T *request() {
  135. if (_size < _max_size) {
  136. return &_list[_size++];
  137. }
  138. return nullptr;
  139. }
  140. // several items at a time
  141. T *request(int p_num_items) {
  142. int old_size = _size;
  143. _size += p_num_items;
  144. if (_size <= _max_size) {
  145. return &_list[old_size];
  146. }
  147. // revert
  148. _size = old_size;
  149. return nullptr;
  150. }
  151. int size() const { return _size; }
  152. int max_size() const { return _max_size; }
  153. const T *get_data() const { return _list; }
  154. bool copy_from(const RasterizerArray<T> &o) {
  155. // no resizing done here, it should be done manually
  156. if (o.size() > _max_size) {
  157. return false;
  158. }
  159. // pod types only please!
  160. memcpy(_list, o.get_data(), o.size() * sizeof(T));
  161. _size = o.size();
  162. return true;
  163. }
  164. // if you want this to be cheap, call reset before grow,
  165. // to ensure there is no data to copy
  166. void grow() {
  167. unsigned int new_max_size = _max_size * 2;
  168. if (!new_max_size) {
  169. new_max_size = 1;
  170. }
  171. T *new_list = memnew_arr(T, new_max_size);
  172. // copy .. pod types only
  173. if (_list) {
  174. memcpy(new_list, _list, _size * sizeof(T));
  175. }
  176. unsigned int new_size = size();
  177. free();
  178. _list = new_list;
  179. _size = new_size;
  180. _max_size = new_max_size;
  181. }
  182. private:
  183. T *_list;
  184. int _size;
  185. int _max_size;
  186. };
  187. template <class T>
  188. class RasterizerArray_non_pod {
  189. public:
  190. RasterizerArray_non_pod() {
  191. _size = 0;
  192. }
  193. const T &operator[](unsigned int ui) const { return _list[ui]; }
  194. void create(int p_size) {
  195. _list.resize(p_size);
  196. _size = 0;
  197. }
  198. void reset() { _size = 0; }
  199. void push_back(const T &val) {
  200. while (true) {
  201. if (_size < max_size()) {
  202. _list.set(_size, val);
  203. _size++;
  204. return;
  205. }
  206. grow();
  207. }
  208. }
  209. int size() const { return _size; }
  210. int max_size() const { return _list.size(); }
  211. private:
  212. void grow() {
  213. unsigned int new_max_size = _list.size() * 2;
  214. if (!new_max_size) {
  215. new_max_size = 1;
  216. }
  217. _list.resize(new_max_size);
  218. }
  219. Vector<T> _list;
  220. int _size;
  221. };
  222. // very simple non-growable array, that keeps track of the size of a 'unit'
  223. // which can be cast to whatever vertex format FVF required, and is initially
  224. // created with enough memory to hold the biggest FVF.
  225. // This allows multiple FVFs to use the same array.
  226. class RasterizerUnitArray {
  227. public:
  228. RasterizerUnitArray() {
  229. _list = nullptr;
  230. free();
  231. }
  232. ~RasterizerUnitArray() { free(); }
  233. uint8_t *get_unit(unsigned int ui) { return &_list[ui * _unit_size_bytes]; }
  234. const uint8_t *get_unit(unsigned int ui) const { return &_list[ui * _unit_size_bytes]; }
  235. int size() const { return _size; }
  236. int max_size() const { return _max_size; }
  237. int get_unit_size_bytes() const { return _unit_size_bytes; }
  238. void free() {
  239. if (_list) {
  240. memdelete_arr(_list);
  241. _list = nullptr;
  242. }
  243. _size = 0;
  244. _max_size = 0;
  245. _max_size_bytes = 0;
  246. _unit_size_bytes = 0;
  247. }
  248. void create(int p_max_size_units, int p_max_unit_size_bytes) {
  249. free();
  250. _max_unit_size_bytes = p_max_unit_size_bytes;
  251. _max_size = p_max_size_units;
  252. _max_size_bytes = p_max_size_units * p_max_unit_size_bytes;
  253. if (_max_size_bytes) {
  254. _list = memnew_arr(uint8_t, _max_size_bytes);
  255. }
  256. }
  257. void prepare(int p_unit_size_bytes) {
  258. _unit_size_bytes = p_unit_size_bytes;
  259. _size = 0;
  260. }
  261. // several items at a time
  262. uint8_t *request(int p_num_items = 1) {
  263. int old_size = _size;
  264. _size += p_num_items;
  265. if (_size <= _max_size) {
  266. return get_unit(old_size);
  267. }
  268. // revert
  269. _size = old_size;
  270. return nullptr;
  271. }
  272. private:
  273. uint8_t *_list;
  274. int _size; // in units
  275. int _max_size; // in units
  276. int _max_size_bytes;
  277. int _unit_size_bytes;
  278. int _max_unit_size_bytes;
  279. };
  280. #endif // RASTERIZER_ARRAY_H