rasterizer_array.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*************************************************************************/
  2. /* rasterizer_array.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. #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 <string.h>
  39. #include "core/templates/local_vector.h"
  40. #include "core/templates/vector.h"
  41. // very simple non-growable array, that keeps track of the size of a 'unit'
  42. // which can be cast to whatever vertex format FVF required, and is initially
  43. // created with enough memory to hold the biggest FVF.
  44. // This allows multiple FVFs to use the same array.
  45. class RasterizerUnitArrayGLES3 {
  46. public:
  47. RasterizerUnitArrayGLES3() {
  48. _list = nullptr;
  49. free();
  50. }
  51. ~RasterizerUnitArrayGLES3() { free(); }
  52. uint8_t *get_unit(unsigned int ui) { return &_list[ui * _unit_size_bytes]; }
  53. const uint8_t *get_unit(unsigned int ui) const { return &_list[ui * _unit_size_bytes]; }
  54. int size() const { return _size; }
  55. int max_size() const { return _max_size; }
  56. void free() {
  57. if (_list) {
  58. memdelete_arr(_list);
  59. _list = 0;
  60. }
  61. _size = 0;
  62. _max_size = 0;
  63. _max_size_bytes = 0;
  64. _unit_size_bytes = 0;
  65. }
  66. void create(int p_max_size_units, int p_max_unit_size_bytes) {
  67. free();
  68. _max_unit_size_bytes = p_max_unit_size_bytes;
  69. _max_size = p_max_size_units;
  70. _max_size_bytes = p_max_size_units * p_max_unit_size_bytes;
  71. if (_max_size_bytes) {
  72. _list = memnew_arr(uint8_t, _max_size_bytes);
  73. }
  74. }
  75. void prepare(int p_unit_size_bytes) {
  76. _unit_size_bytes = p_unit_size_bytes;
  77. _size = 0;
  78. }
  79. // several items at a time
  80. uint8_t *request(int p_num_items = 1) {
  81. int old_size = _size;
  82. _size += p_num_items;
  83. if (_size <= _max_size) {
  84. return get_unit(old_size);
  85. }
  86. // revert
  87. _size = old_size;
  88. return nullptr;
  89. }
  90. private:
  91. uint8_t *_list;
  92. int _size; // in units
  93. int _max_size; // in units
  94. int _max_size_bytes;
  95. int _unit_size_bytes;
  96. int _max_unit_size_bytes;
  97. };
  98. template <class T>
  99. class RasterizerArray {
  100. public:
  101. RasterizerArray() {
  102. _list = 0;
  103. _size = 0;
  104. _max_size = 0;
  105. }
  106. ~RasterizerArray() { free(); }
  107. T &operator[](unsigned int ui) { return _list[ui]; }
  108. const T &operator[](unsigned int ui) const { return _list[ui]; }
  109. void free() {
  110. if (_list) {
  111. memdelete_arr(_list);
  112. _list = 0;
  113. }
  114. _size = 0;
  115. _max_size = 0;
  116. }
  117. void create(int p_size) {
  118. free();
  119. if (p_size) {
  120. _list = memnew_arr(T, p_size);
  121. }
  122. _size = 0;
  123. _max_size = p_size;
  124. }
  125. void reset() { _size = 0; }
  126. T *request_with_grow() {
  127. T *p = request();
  128. if (!p) {
  129. grow();
  130. return request_with_grow();
  131. }
  132. return p;
  133. }
  134. // none of that inefficient pass by value stuff here, thanks
  135. T *request() {
  136. if (_size < _max_size) {
  137. return &_list[_size++];
  138. }
  139. return 0;
  140. }
  141. // several items at a time
  142. T *request(int p_num_items) {
  143. int old_size = _size;
  144. _size += p_num_items;
  145. if (_size <= _max_size) {
  146. return &_list[old_size];
  147. }
  148. // revert
  149. _size = old_size;
  150. return 0;
  151. }
  152. int size() const { return _size; }
  153. int max_size() const { return _max_size; }
  154. const T *get_data() const { return _list; }
  155. bool copy_from(const RasterizerArray<T> &o) {
  156. // no resizing done here, it should be done manually
  157. if (o.size() > _max_size)
  158. return false;
  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. T *new_list = memnew_arr(T, new_max_size);
  171. // copy .. pod types only
  172. if (_list) {
  173. memcpy(new_list, _list, _size * sizeof(T));
  174. }
  175. unsigned int new_size = size();
  176. free();
  177. _list = new_list;
  178. _size = new_size;
  179. _max_size = new_max_size;
  180. }
  181. private:
  182. T *_list;
  183. int _size;
  184. int _max_size;
  185. };
  186. template <class T>
  187. class RasterizerArray_non_pod {
  188. public:
  189. RasterizerArray_non_pod() {
  190. _size = 0;
  191. }
  192. const T &operator[](unsigned int ui) const { return _list[ui]; }
  193. void create(int p_size) {
  194. _list.resize(p_size);
  195. _size = 0;
  196. }
  197. void reset() { _size = 0; }
  198. void push_back(const T &val) {
  199. while (true) {
  200. if (_size < max_size()) {
  201. _list.set(_size, val);
  202. _size++;
  203. return;
  204. }
  205. grow();
  206. }
  207. }
  208. int size() const { return _size; }
  209. int max_size() const { return _list.size(); }
  210. private:
  211. void grow() {
  212. unsigned int new_max_size = _list.size() * 2;
  213. if (!new_max_size)
  214. new_max_size = 1;
  215. _list.resize(new_max_size);
  216. }
  217. Vector<T> _list;
  218. int _size;
  219. };
  220. // very simple non-growable array, that keeps track of the size of a 'unit'
  221. // which can be cast to whatever vertex format FVF required, and is initially
  222. // created with enough memory to hold the biggest FVF.
  223. // This allows multiple FVFs to use the same array.
  224. class RasterizerUnitArray {
  225. public:
  226. RasterizerUnitArray() {
  227. _list = nullptr;
  228. free();
  229. }
  230. ~RasterizerUnitArray() { free(); }
  231. uint8_t *get_unit(unsigned int ui) { return &_list[ui * _unit_size_bytes]; }
  232. const uint8_t *get_unit(unsigned int ui) const { return &_list[ui * _unit_size_bytes]; }
  233. int size() const { return _size; }
  234. int max_size() const { return _max_size; }
  235. int get_unit_size_bytes() const { return _unit_size_bytes; }
  236. void free() {
  237. if (_list) {
  238. memdelete_arr(_list);
  239. _list = 0;
  240. }
  241. _size = 0;
  242. _max_size = 0;
  243. _max_size_bytes = 0;
  244. _unit_size_bytes = 0;
  245. }
  246. void create(int p_max_size_units, int p_max_unit_size_bytes) {
  247. free();
  248. _max_unit_size_bytes = p_max_unit_size_bytes;
  249. _max_size = p_max_size_units;
  250. _max_size_bytes = p_max_size_units * p_max_unit_size_bytes;
  251. if (_max_size_bytes) {
  252. _list = memnew_arr(uint8_t, _max_size_bytes);
  253. }
  254. }
  255. void prepare(int p_unit_size_bytes) {
  256. _unit_size_bytes = p_unit_size_bytes;
  257. _size = 0;
  258. }
  259. // several items at a time
  260. uint8_t *request(int p_num_items = 1) {
  261. int old_size = _size;
  262. _size += p_num_items;
  263. if (_size <= _max_size) {
  264. return get_unit(old_size);
  265. }
  266. // revert
  267. _size = old_size;
  268. return nullptr;
  269. }
  270. private:
  271. uint8_t *_list;
  272. int _size; // in units
  273. int _max_size; // in units
  274. int _max_size_bytes;
  275. int _unit_size_bytes;
  276. int _max_unit_size_bytes;
  277. };
  278. template <class T, bool force_trivial = false>
  279. class RasterizerPooledList {
  280. LocalVector<T, uint32_t, force_trivial> list;
  281. LocalVector<uint32_t, uint32_t, true> freelist;
  282. // not all list members are necessarily used
  283. int _used_size;
  284. public:
  285. RasterizerPooledList() {
  286. _used_size = 0;
  287. }
  288. int estimate_memory_use() const {
  289. return (list.size() * sizeof(T)) + (freelist.size() * sizeof(uint32_t));
  290. }
  291. const T &operator[](uint32_t p_index) const {
  292. return list[p_index];
  293. }
  294. T &operator[](uint32_t p_index) {
  295. return list[p_index];
  296. }
  297. int size() const { return _used_size; }
  298. // returns the list id of the allocated item
  299. uint32_t alloc() {
  300. uint32_t id = 0;
  301. _used_size++;
  302. if (freelist.size()) {
  303. // pop from freelist
  304. int new_size = freelist.size() - 1;
  305. id = freelist[new_size];
  306. freelist.resize(new_size);
  307. return id;
  308. // return &list[r_id];
  309. }
  310. id = list.size();
  311. list.resize(id + 1);
  312. return id;
  313. // return &list[r_id];
  314. }
  315. void free(const uint32_t &p_id) {
  316. // should not be on free list already
  317. CRASH_COND(p_id >= list.size());
  318. freelist.push_back(p_id);
  319. _used_size--;
  320. }
  321. };
  322. template <class T, bool force_trivial = false>
  323. class RasterizerPooledIndirectList {
  324. public:
  325. const T &operator[](uint32_t p_index) const {
  326. return *_list[p_index];
  327. }
  328. T &operator[](uint32_t p_index) {
  329. return *_list[p_index];
  330. }
  331. uint32_t alloc() {
  332. uint32_t id = _list.alloc();
  333. _list[id] = memnew(T);
  334. return id;
  335. }
  336. void free(const uint32_t &p_id) {
  337. CRASH_COND(!_list[p_id]);
  338. memdelete_notnull(_list[p_id]);
  339. _list[p_id] = nullptr;
  340. _list.free(p_id);
  341. }
  342. ~RasterizerPooledIndirectList() {
  343. // autodelete
  344. for (int n = 0; n < _list.size(); n++) {
  345. if (_list[n]) {
  346. memdelete_notnull(_list[n]);
  347. }
  348. }
  349. }
  350. private:
  351. RasterizerPooledList<T *, true> _list;
  352. };
  353. #endif // RASTERIZER_ARRAY_H