123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- /*************************************************************************/
- /* rasterizer_array_gles2.h */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #pragma once
- /*************************************************************************/
- /* rasterizer_array_gles2.h */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- /**
- * Fast single-threaded growable array for POD types.
- * For use in render drivers, not for general use.
- * TO BE REPLACED by local_vector.
- */
- #include "core/os/memory.h"
- #include "core/vector.h"
- #include <string.h>
- template <class T>
- class RasterizerArrayGLES2 {
- public:
- RasterizerArrayGLES2() {
- _list = 0;
- _size = 0;
- _max_size = 0;
- }
- ~RasterizerArrayGLES2() { free(); }
- T &operator[](unsigned int ui) { return _list[ui]; }
- const T &operator[](unsigned int ui) const { return _list[ui]; }
- void free() {
- if (_list) {
- memdelete_arr(_list);
- _list = 0;
- }
- _size = 0;
- _max_size = 0;
- }
- void create(int p_size) {
- free();
- if (p_size) {
- _list = memnew_arr(T, p_size);
- }
- _size = 0;
- _max_size = p_size;
- }
- void reset() { _size = 0; }
- T *request_with_grow() {
- T *p = request();
- if (!p) {
- grow();
- return request_with_grow();
- }
- return p;
- }
- // none of that inefficient pass by value stuff here, thanks
- T *request() {
- if (_size < _max_size) {
- return &_list[_size++];
- }
- return 0;
- }
- // several items at a time
- T *request(int p_num_items) {
- int old_size = _size;
- _size += p_num_items;
- if (_size <= _max_size) {
- return &_list[old_size];
- }
- // revert
- _size = old_size;
- return 0;
- }
- int size() const { return _size; }
- int max_size() const { return _max_size; }
- const T *get_data() const { return _list; }
- bool copy_from(const RasterizerArrayGLES2<T> &o) {
- // no resizing done here, it should be done manually
- if (o.size() > _max_size)
- return false;
- // pod types only please!
- memcpy(_list, o.get_data(), o.size() * sizeof(T));
- _size = o.size();
- return true;
- }
- // if you want this to be cheap, call reset before grow,
- // to ensure there is no data to copy
- void grow() {
- unsigned int new_max_size = _max_size * 2;
- if (!new_max_size)
- new_max_size = 1;
- T *new_list = memnew_arr(T, new_max_size);
- // copy .. pod types only
- if (_list) {
- memcpy(new_list, _list, _size * sizeof(T));
- }
- unsigned int new_size = size();
- free();
- _list = new_list;
- _size = new_size;
- _max_size = new_max_size;
- }
- private:
- T *_list;
- int _size;
- int _max_size;
- };
- template <class T>
- class RasterizerArray_non_pod_GLES2 {
- public:
- RasterizerArray_non_pod_GLES2() {
- _size = 0;
- }
- const T &operator[](unsigned int ui) const { return _list[ui]; }
- void create(int p_size) {
- _list.resize(p_size);
- _size = 0;
- }
- void reset() { _size = 0; }
- void push_back(const T &val) {
- while (true) {
- if (_size < max_size()) {
- _list.set(_size, val);
- _size++;
- return;
- }
- grow();
- }
- }
- int size() const { return _size; }
- int max_size() const { return _list.size(); }
- private:
- void grow() {
- unsigned int new_max_size = _list.size() * 2;
- if (!new_max_size)
- new_max_size = 1;
- _list.resize(new_max_size);
- }
- Vector<T> _list;
- int _size;
- };
|