OpenBarnyard
 
Loading...
Searching...
No Matches
T2SharedRenderBuffer_GL.h
Go to the documentation of this file.
1#pragma once
2#include "T2Render_GL.h"
3
6
8
9template <class RenderBuffer>
11{
12public:
13 struct SubBuffer
14 {
18
19 void SetData( const void* a_pData, GLsizeiptr a_iSize )
20 {
21 TASSERT( a_iSize <= GLsizeiptr( size ) );
22 glNamedBufferSubData( owner->m_oRenderBuffer.GetId(), offset, a_iSize, a_pData );
23 }
24
25 void Deallocate();
26 };
27
29 {
30 TINT operator()( SubBuffer* const& a_rcVal1, SubBuffer* const& a_rcVal2 ) const
31 {
32 return a_rcVal1->offset - a_rcVal2->offset;
33 }
34 };
35
36public:
38 {
39 m_iTotalSize = 0;
40 m_iFreeSize = 0;
41 }
42
44 {
45 if ( IsCreated() )
46 Destroy();
47 }
48
49 void Create( RenderBuffer&& a_rRenderBuffer, TINT a_iSize )
50 {
51 TASSERT( !IsCreated() );
52
53 m_oRenderBuffer = std::move( a_rRenderBuffer );
54 m_oRenderBuffer.Bind();
55 m_oRenderBuffer.SetData( TNULL, a_iSize, GL_DYNAMIC_DRAW );
56 m_oRenderBuffer.Unbind();
57
58 m_iTotalSize = a_iSize;
59 m_iFreeSize = a_iSize;
60 }
61
62 void Destroy()
63 {
64 TASSERT( IsCreated() );
65
66 GLuint uiId = m_oRenderBuffer.GetId();
67 glDeleteBuffers( 1, &uiId );
68 m_iTotalSize = 0;
69 m_iFreeSize = 0;
70
71 m_oRenderBuffer.Clear();
72 }
73
74 SubBuffer* Allocate( TINT a_iSize )
75 {
76 if ( m_iFreeSize < a_iSize )
77 return TNULL;
78
79 TINT iPrevOffset = 0;
80 TINT iNextOffset = 0;
81 TINT iHoleSize = m_iFreeSize;
82 TINT iPrevSize = 0;
83 TINT iNumAllocations = m_vecAllocations.Size();
84
85 // Find a hole that can fit the allocation
86 SubBuffer* pPrevAllocation = TNULL;
87 for ( TINT i = 0; i < iNumAllocations; i++ )
88 {
89 SubBuffer* pAllocation = m_vecAllocations[ i ];
90
91 iHoleSize = pAllocation->offset - iPrevOffset - iPrevSize;
92 iPrevOffset = pAllocation->offset;
93 iPrevSize = pAllocation->size;
94 pPrevAllocation = pAllocation;
95
96 if ( iHoleSize >= a_iSize )
97 break;
98
99 iNextOffset = iPrevOffset + pAllocation->size;
100 }
101
102 // If reached the end of the list, check if the allocation can fit at the end of the buffer
103 if ( pPrevAllocation && iHoleSize < a_iSize && pPrevAllocation == m_vecAllocations[ iNumAllocations - 1 ] )
104 {
105 iHoleSize = m_iTotalSize - ( pPrevAllocation->offset + pPrevAllocation->size );
106
107 if ( iHoleSize < a_iSize )
108 return TNULL;
109 }
110
111 TASSERT( iHoleSize >= a_iSize );
112
113 // Fill allocation info and store in the sorted vector
114 SubBuffer* pSubBuffer = new SubBuffer();
115 pSubBuffer->owner = this;
116 pSubBuffer->offset = iNextOffset;
117 pSubBuffer->size = a_iSize;
118 m_vecAllocations.Push( pSubBuffer );
119
120 // Update number of free vertices
121 m_iFreeSize -= a_iSize;
122
123 return pSubBuffer;
124 }
125
126 void Deallocate( SubBuffer* a_pAllocation )
127 {
128 if ( a_pAllocation )
129 {
130 m_vecAllocations.FindAndErase( a_pAllocation );
131 m_iFreeSize += a_pAllocation->size;
132
133 delete a_pAllocation;
134 }
135 }
136
137 RenderBuffer& GetRenderBuffer() { return m_oRenderBuffer; }
138 TBOOL IsCreated() const { return m_oRenderBuffer; }
139
140private:
141 RenderBuffer m_oRenderBuffer;
142 TINT m_iTotalSize;
143 TINT m_iFreeSize;
144
145 T2SortedVector<SubBuffer*, T2DynamicVector<SubBuffer*>, SubBufferSort> m_vecAllocations;
146};
147
148template <class RenderBuffer>
150{
151 owner->Deallocate( this );
152}
153
156
T2SharedRenderBuffer< T2VertexBuffer > T2SharedVertexBuffer
T2SharedRenderBuffer< T2IndexBuffer > T2SharedIndexBuffer
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
unsigned int TUINT
Definition Typedefs.h:8
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
bool TBOOL
Definition Typedefs.h:6
Dynamic vector container for the Toshi engine.
void Create(RenderBuffer &&a_rRenderBuffer, TINT a_iSize)
SubBuffer * Allocate(TINT a_iSize)
void Deallocate(SubBuffer *a_pAllocation)
void SetData(const void *a_pData, GLsizeiptr a_iSize)
TINT operator()(SubBuffer *const &a_rcVal1, SubBuffer *const &a_rcVal2) const