LCOV - code coverage report
Current view: top level - source/renderer - VertexArray.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 45 0.0 %
Date: 2022-03-08 13:03:03 Functions: 0 23 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2022 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_VERTEXARRAY
      19             : #define INCLUDED_VERTEXARRAY
      20             : 
      21             : #include "renderer/backend/gl/Buffer.h"
      22             : #include "renderer/backend/gl/DeviceCommandContext.h"
      23             : #include "renderer/VertexBufferManager.h"
      24             : 
      25             : #include <vector>
      26             : 
      27             : // Iterator
      28             : template<typename T>
      29             : class VertexArrayIterator
      30             : {
      31             : public:
      32             :     typedef T Type;
      33             : 
      34             : public:
      35             :     VertexArrayIterator() :
      36           0 :         m_Data(0), m_Stride(0)
      37             :     {
      38             :     }
      39             : 
      40             :     VertexArrayIterator(char* data, size_t stride) :
      41           0 :         m_Data(data), m_Stride(stride)
      42             :     {
      43             :     }
      44             : 
      45             :     VertexArrayIterator(const VertexArrayIterator& rhs) :
      46           0 :         m_Data(rhs.m_Data), m_Stride(rhs.m_Stride)
      47             :     {
      48             :     }
      49             : 
      50           0 :     VertexArrayIterator& operator=(const VertexArrayIterator& rhs)
      51             :     {
      52           0 :         m_Data = rhs.m_Data;
      53           0 :         m_Stride = rhs.m_Stride;
      54           0 :         return *this;
      55             :     }
      56             : 
      57             :     // Accessors
      58           0 :     T& operator*() const { return *(T*)m_Data; }
      59           0 :     T* operator->() const { return (T*)m_Data; }
      60           0 :     T& operator[](size_t idx) const { return *(T*)(m_Data + idx*m_Stride); }
      61             : 
      62             :     // Walking
      63             :     VertexArrayIterator& operator++()
      64             :     {
      65           0 :         m_Data += m_Stride;
      66             :         return *this;
      67             :     }
      68           0 :     VertexArrayIterator operator++(int)
      69             :     {
      70           0 :         VertexArrayIterator tmp = *this;
      71           0 :         m_Data += m_Stride;
      72           0 :         return tmp;
      73             :     }
      74           0 :     VertexArrayIterator& operator--()
      75             :     {
      76           0 :         m_Data -= m_Stride;
      77           0 :         return *this;
      78           0 :     }
      79             :     VertexArrayIterator operator--(int)
      80           0 :     {
      81             :         VertexArrayIterator tmp = *this;
      82           0 :         m_Data -= m_Stride;
      83           0 :         return tmp;
      84           0 :     }
      85             : 
      86           0 :     VertexArrayIterator& operator+=(ssize_t rhs)
      87             :     {
      88           0 :         m_Data += rhs*m_Stride;
      89           0 :         return *this;
      90           0 :     }
      91             :     VertexArrayIterator& operator-=(ssize_t rhs)
      92             :     {
      93             :         m_Data -= rhs*m_Stride;
      94             :         return *this;
      95             :     }
      96             : 
      97             :     VertexArrayIterator operator+(ssize_t rhs) const
      98             :     {
      99             :         VertexArrayIterator tmp = *this;
     100             :         tmp.m_Data += rhs*m_Stride;
     101             :         return tmp;
     102             :     }
     103             :     VertexArrayIterator operator-(ssize_t rhs) const
     104             :     {
     105             :         VertexArrayIterator tmp = *this;
     106             :         tmp.m_Data -= rhs*m_Stride;
     107             :         return tmp;
     108             :     }
     109             : 
     110             :     // Accessors for raw buffer data, for performance-critical code
     111           0 :     char* GetData() const
     112             :     {
     113           0 :         return m_Data;
     114             :     }
     115           0 :     size_t GetStride() const
     116             :     {
     117           0 :         return m_Stride;
     118             :     }
     119             : 
     120             : private:
     121             :     char* m_Data;
     122             :     size_t m_Stride;
     123             : };
     124             : 
     125             : 
     126             : // Manage a vertex array with a runtime-determined set of attributes.
     127             : //
     128             : // Purpose: Different rendering paths sometimes require different sets of
     129             : // attributes (e.g. normal vector vs. color data), which is difficult to
     130             : // support with hardcoded vertex structures.
     131             : // This class chooses the vertex layout at runtime, based on the attributes
     132             : // that are actually needed.
     133             : //
     134             : // Note that this class will not allocate any OpenGL resources until one
     135             : // of the Upload functions is called.
     136             : class VertexArray
     137             : {
     138             : public:
     139             :     struct Attribute
     140             :     {
     141             :         // Data type. Currently supported: GL_FLOAT, GL_SHORT, GL_UNSIGNED_SHORT, GL_UNSIGNED_BYTE.
     142             :         GLenum type;
     143             :         // How many elements per vertex (e.g. 3 for RGB, 2 for UV)
     144             :         GLuint elems;
     145             : 
     146             :         // Offset (in bytes) into a vertex structure (filled in by Layout())
     147             :         size_t offset;
     148             : 
     149             :         VertexArray* vertexArray;
     150             : 
     151           0 :         Attribute() : type(0), elems(0), offset(0), vertexArray(0) { }
     152             : 
     153             :         // Get an iterator over the backing store for the given attribute that
     154             :         // initially points at the first vertex.
     155             :         // Supported types T: CVector3D, CVector4D, float[2], SColor3ub, SColor4ub,
     156             :         // u16, u16[2], u8, u8[4], short, short[2].
     157             :         // This function verifies at runtime that the requested type T matches
     158             :         // the attribute definition passed to AddAttribute().
     159             :         template<typename T>
     160             :         VertexArrayIterator<T> GetIterator() const;
     161             :     };
     162             : 
     163             : public:
     164             :     VertexArray(
     165             :         const Renderer::Backend::GL::CBuffer::Type type, const bool dynamic);
     166             :     ~VertexArray();
     167             : 
     168             :     // Set the number of vertices stored in the array
     169           0 :     void SetNumberOfVertices(const size_t numberOfVertices);
     170             :     // Add vertex attributes
     171             :     void AddAttribute(Attribute* attr);
     172             : 
     173           0 :     size_t GetNumberOfVertices() const { return m_NumberOfVertices; }
     174           0 :     size_t GetStride() const { return m_Stride; }
     175             : 
     176             :     // Layout the vertex array format and create backing buffer in RAM.
     177             :     // You must call Layout() after changing the number of vertices or
     178             :     // attributes.
     179             :     // All vertex data is lost when a vertex array is re-layouted.
     180             :     void Layout();
     181             :     // (Re-)Upload the attributes of the vertex array from the backing store to
     182             :     // the underlying buffer.
     183             :     void Upload();
     184             :     // Make this vertex array's data available for the next series of calls to Bind
     185             :     void PrepareForRendering();
     186             :     // Bind this array, returns the base address for calls to glVertexPointer etc.
     187             :     u8* Bind(Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext);
     188             :     void UploadIfNeeded(Renderer::Backend::GL::CDeviceCommandContext* deviceCommandContext);
     189             : 
     190             :     // If you know for certain that you'll never have to change the data again,
     191             :     // call this to free some memory.
     192           0 :     void FreeBackingStore();
     193             : 
     194           0 :     Renderer::Backend::GL::CBuffer* GetBuffer() { return m_VB ? m_VB->m_Owner->GetBuffer() : nullptr; }
     195             : 
     196           0 :     uint32_t GetOffset() const { return m_VB ? m_VB->m_Index : 0; }
     197             : 
     198             : private:
     199             :     void Free();
     200             : 
     201             :     template<typename T>
     202           0 :     VertexArrayIterator<T> MakeIterator(const Attribute* attr)
     203             :     {
     204           0 :         ENSURE(attr->type && attr->elems);
     205           0 :         return VertexArrayIterator<T>(m_BackingStore + attr->offset, m_Stride);
     206             :     }
     207             : 
     208             :     Renderer::Backend::GL::CBuffer::Type m_Type;
     209             :     bool m_Dynamic;
     210             :     size_t m_NumberOfVertices;
     211             :     std::vector<Attribute*> m_Attributes;
     212           0 : 
     213             :     CVertexBufferManager::Handle m_VB;
     214           0 :     size_t m_Stride;
     215             :     char* m_BackingStore; // 16-byte aligned, to allow fast SSE access
     216             : };
     217             : 
     218             : /**
     219             :  * A VertexArray that is specialised to handle 16-bit array indices.
     220             :  * Call UploadIfNeeded() before use in Draw/DrawIndexed.
     221             :  */
     222           0 : class VertexIndexArray : public VertexArray
     223             : {
     224             : public:
     225             :     VertexIndexArray(const bool dynamic);
     226             : 
     227             :     /// Gets the iterator over the (only) attribute in this array, i.e. a u16.
     228             :     VertexArrayIterator<u16> GetIterator() const;
     229             : 
     230             : private:
     231             :     Attribute m_Attr;
     232             : };
     233             : 
     234             : #endif // INCLUDED_VERTEXARRAY

Generated by: LCOV version 1.13