Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define AEGLE_BUFFER_ALIGNPAD 256
- //Encapsulates a buffer.
- // Note: untyped for ease-of-use (no need to specify types when handling). To set/get typed data,
- // construct `BufferView<...>` instances.
- class Buffer final {
- public:
- uint8_t* backing;
- private:
- size_t _size_max;
- public:
- size_t size;
- public:
- Buffer();
- explicit Buffer(size_t size);
- explicit Buffer(Buffer const& other);
- ~Buffer();
- void resize(size_t size);
- };
- //Encapsulates typed views of a `Buffer`.
- // Exposes an "array-of-structures" representation, regardless of whether the underlying data looks
- // like that. Surprisingly, with good compilers, this has no runtime cost.
- #define AEGLE_BUFFER_INDEXING_METHODS(PREFIX,ASSERT)\
- private:\
- /*E.g. `resolution[0]*(resolution[1]*(resolution[2]*index[0] + index[1]) + index[2]) + index[3]`*/\
- PREFIX size_t _convert_to_index_helper(size_t inner, size_t index ) const { return resolution[0]*inner+index; }\
- template <typename... size_ts> PREFIX size_t _convert_to_index_helper(size_t inner, size_t index,size_ts... indices) const { return _convert_to_index_helper( resolution[sizeof...(indices)]*inner+index, indices... ); }\
- template <typename... size_ts> PREFIX size_t _convert_to_index(size_ts... indices) const { ASSERT return _convert_to_index_helper(indices...); }\
- public:\
- template <bool b=!type_elem::CONST_ACCESS,typename=typename std::enable_if_t<b> > type_elem operator()(size_t index ) { return type_elem(buffer, index ); }\
- type_elem const operator()(size_t index ) const { return type_elem(buffer, index ); }\
- template <bool b=!type_elem::CONST_ACCESS,typename=typename std::enable_if_t<b>, typename... size_ts> type_elem operator()(size_ts... indices) { return type_elem(buffer,_convert_to_index(indices...)); }\
- template < typename... size_ts> type_elem const operator()(size_ts... indices) const { return type_elem(buffer,_convert_to_index(indices...)); }
- template <class type_elem, size_t... reses> class BufferView final {
- public:
- std::add_const_if_t<Buffer,type_elem::CONST_ACCESS>*const buffer;
- enum : size_t { DIMENSIONS=sizeof...(reses) };
- enum class ORDERING { LINEAR, Z_FRACTAL };
- size_t const resolution[sizeof...(reses)] = { reses... };
- public:
- explicit BufferView(std::add_const_if_t<Buffer,type_elem::CONST_ACCESS>* buffer) : buffer(buffer) {}
- ~BufferView() = default;
- constexpr size_t get_length() const {
- size_t length = resolution[0];
- for (size_t i=1;i<sizeof...(reses);++i) length*=resolution[i];
- return length;
- }
- template <bool b=!type_elem::CONST_ACCESS,typename=typename std::enable_if_t<b>>
- void resize_backing() { buffer->resize(get_length()*type_elem::SIZE); }
- AEGLE_BUFFER_INDEXING_METHODS(constexpr,static_assert(sizeof...(size_ts)==DIMENSIONS,"Invalid number of indices!");)
- };
- template <class type_elem > class BufferView<type_elem> final {
- public:
- std::add_const_if_t<Buffer,type_elem::CONST_ACCESS>*const buffer;
- enum class ORDERING { LINEAR, Z_FRACTAL };
- size_t const*const resolution;
- public:
- BufferView(std::add_const_if_t<Buffer,type_elem::CONST_ACCESS>* buffer, size_t const reses[]) : buffer(buffer), resolution(reses) {}
- ~BufferView() = default;
- size_t get_length(size_t dimensions) const {
- size_t length = resolution[0];
- for (size_t i=1;i<dimensions;++i) length*=resolution[i];
- return length;
- }
- template <bool b=!type_elem::CONST_ACCESS,typename=typename std::enable_if_t<b>>
- void resize_backing(size_t dimensions) { buffer->resize(get_length(dimensions)*type_elem::SIZE); }
- AEGLE_BUFFER_INDEXING_METHODS(NOTHING,NOTHING)
- };
- template <typename type, bool const_access> class BufferAccessor;
- template <typename type > class BufferAccessor<type,false> final {
- public:
- type* ptr;
- public:
- explicit BufferAccessor(type* ptr) : ptr(ptr) {}
- ~BufferAccessor() = default;
- operator type &() { return *ptr; }
- operator type const&() const { return *ptr; }
- template <typename type_other> type& operator=(type_other const& other) { return static_cast<type&>(*this)=other; }
- };
- template <typename type > class BufferAccessor<type,true > final {
- public:
- type const* ptr;
- public:
- explicit BufferAccessor(type const* ptr) : ptr(ptr) {}
- ~BufferAccessor() = default;
- operator type const&() const { return *ptr; }
- };
- template <bool const_access> class BufferElemXYZ final {
- public:
- enum : size_t { SIZE=3*sizeof(float) };
- enum : bool { CONST_ACCESS=const_access };
- BufferAccessor<float,const_access> x, y, z;
- public:
- explicit BufferElemXYZ(float* ptr) : x(ptr), y(ptr+1), z(ptr+2) {}
- BufferElemXYZ(Buffer const* parent, size_t index) : BufferElemXYZ(reinterpret_cast<float*>(parent->backing)+index) {}
- ~BufferElemXYZ() = default;
- };
- template <bool const_access> class BufferElemRGB final {
- public:
- enum : size_t { SIZE=3*sizeof(float) };
- enum : bool { CONST_ACCESS=const_access };
- BufferAccessor<float,const_access> r, g, b;
- public:
- explicit BufferElemRGB(float* ptr) : r(ptr), g(ptr+1), b(ptr+2) {}
- BufferElemRGB(Buffer const* parent, size_t index) : BufferElemRGB(reinterpret_cast<float*>(parent->backing)+index) {}
- ~BufferElemRGB() = default;
- };
- template <bool const_access> class BufferElemPlanarRGB final {
- public:
- enum : size_t { SIZE=3*sizeof(float) };
- enum : bool { CONST_ACCESS=const_access };
- BufferAccessor<float,const_access> r, g, b;
- public:
- BufferElemPlanarRGB(float*__restrict ptr_r, float*__restrict ptr_g, float*__restrict ptr_b) : r(ptr_r), g(ptr_g), b(ptr_b) {}
- BufferElemPlanarRGB(Buffer const* parent, size_t index) : BufferElemPlanarRGB(
- reinterpret_cast<float*>(parent->backing)+index,
- reinterpret_cast<float*>(parent->backing)+index+ sizeof(float)*parent->length,
- reinterpret_cast<float*>(parent->backing)+index+2*sizeof(float)*parent->length
- ) {}
- ~BufferElemPlanarRGB() = default;
- };
- template <bool const_access> class BufferElemVert final {
- public:
- enum : size_t { SIZE=BufferElemXYZ<const_access>::SIZE+BufferElemPlanarRGB<const_access>::SIZE };
- enum : bool { CONST_ACCESS=const_access };
- BufferElemXYZ<const_access> vert;
- BufferElemPlanarRGB<const_access> color;
- public:
- explicit BufferElemVert(
- float*__restrict ptr_v,
- float*__restrict ptr_col_r, float*__restrict ptr_col_g, float*__restrict ptr_col_b
- ) :
- vert(ptr_v),
- color(ptr_col_r,ptr_col_g,ptr_col_b)
- {}
- template <size_t... reses> BufferElemVert(Buffer const* parent, size_t index) : BufferElemVert(
- reinterpret_cast<float*>(parent->data)+index,
- reinterpret_cast<float*>(parent->data)+index+ 3 *sizeof(float)*parent->length,
- reinterpret_cast<float*>(parent->data)+index+(3+1)*sizeof(float)*parent->length,
- reinterpret_cast<float*>(parent->data)+index+(3+2)*sizeof(float)*parent->length
- ) {}
- ~BufferElemVert() = default;
- };
- Buffer* foo() {
- Buffer* buffer = _new Buffer;
- BufferView<BufferElemVert<false>,32,32,32> view(buffer);
- view.resize_backing();
- return buffer;
- }
- Buffer* bar() {
- Buffer* buffer = _new Buffer;
- Ash::LA::Vec2zu res(800,600);
- BufferView<BufferElemVert<false>> view(buffer,res[0]);
- view.resize_backing(2);
- return buffer;
- }
- float bar(Buffer const& buf) {
- BufferView<BufferElemVert<true>,32,32,32> view(&buf);
- //view(6,7,11).color.g = 6.0f;
- return view(6,7,11).color.g;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement