-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathTr2DirectInstanceData.cpp
More file actions
251 lines (229 loc) · 8.03 KB
/
Copy pathTr2DirectInstanceData.cpp
File metadata and controls
251 lines (229 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright © 2026 CCP ehf.
#include "StdAfx.h"
#include "Tr2DirectInstanceData.h"
using namespace Tr2RenderContextEnum;
// --------------------------------------------------------------------------------------
// Description:
// Tr2DirectInstanceData default constructor
// --------------------------------------------------------------------------------------
Tr2DirectInstanceData::Tr2DirectInstanceData( IRoot* lockobj ) :
m_count( 0 ),
m_stride( 0 ),
m_vertexDeclaration( Tr2EffectStateManager::UNINITIALIZED_DECLARATION ),
m_aabb( Vector3( 0, 0, 0 ), Vector3( 0, 0, 0 ) )
{
}
// --------------------------------------------------------------------------------------
// Description:
// Inherited from ITriDeviceResource interface. Invalidates vertex declaration, and
// resets buffer to an invalid state.
// --------------------------------------------------------------------------------------
void Tr2DirectInstanceData::ReleaseResources( TriStorage s )
{
m_vertexDeclaration = Tr2EffectStateManager::UNINITIALIZED_DECLARATION;
m_vertexBuffer = Tr2BufferAL();
}
// --------------------------------------------------------------------------------------
// Description:
// Inherited from ITriDeviceResource interface. Recreates vertex buffer and vertex
// declaration if needed.
// Return value:
// true If GPU resources are created successfully
// false Otherwise
// --------------------------------------------------------------------------------------
bool Tr2DirectInstanceData::OnPrepareResources()
{
if( m_stride && m_count )
{
if( !m_vertexBuffer.IsValid() || m_vertexBuffer.GetDesc().count < m_count )
{
USE_MAIN_THREAD_RENDER_CONTEXT();
auto hr = m_vertexBuffer.Create(
m_stride,
m_count,
Tr2GpuUsage::VERTEX_BUFFER,
Tr2CpuUsage::WRITE_OFTEN,
nullptr,
renderContext );
if( FAILED( hr ) )
{
return false;
}
}
}
if( m_vertexDeclaration == Tr2EffectStateManager::UNINITIALIZED_DECLARATION && !m_layout.m_items.empty() )
{
m_vertexDeclaration = Tr2EffectStateManager::GetVertexDeclarationHandle( m_layout );
}
return true;
}
// --------------------------------------------------------------------------------------
// Description:
// Implements ITr2InstanceData interface. Query if instance data is available for
// rendering.
// Return value:
// true If instance data is available
// false Otherwise
// --------------------------------------------------------------------------------------
bool Tr2DirectInstanceData::IsInstanceDataReady() const
{
return m_vertexDeclaration != Tr2EffectStateManager::UNINITIALIZED_DECLARATION && m_vertexBuffer.IsValid();
}
// --------------------------------------------------------------------------------------
// Description:
// Implements ITr2InstanceData interface.
// Arguments:
// bufferIndex - (unused) instance buffer index
// screenSize - (unused) screen size
// Return value:
// Returns instance data: the vertex buffer, offset, stride and count.
// --------------------------------------------------------------------------------------
ITr2InstanceData::InstanceData Tr2DirectInstanceData::GetInstanceData( unsigned int bufferIndex, float screenSize ) const
{
return {
m_vertexBuffer, 0, m_stride, m_count
};
}
// --------------------------------------------------------------------------------------
// Description:
// Implements ITr2InstanceData interface.
// Arguments:
// bufferIndex - (unused) instance buffer index
// Return value:
// Returns the vertex declaration.
// --------------------------------------------------------------------------------------
unsigned int Tr2DirectInstanceData::GetInstanceBufferVertexDeclaration( unsigned int bufferIndex ) const
{
return m_vertexDeclaration;
}
CcpMath::AxisAlignedBox Tr2DirectInstanceData::GetInstanceBufferBoundingBox( unsigned int bufferIndex ) const
{
return m_aabb;
}
// --------------------------------------------------------------------------------------
// Description:
// Returns number of instances.
// Return value:
// Nmber of instances
// --------------------------------------------------------------------------------------
unsigned Tr2DirectInstanceData::GetCount() const
{
return m_count;
}
// --------------------------------------------------------------------------------------
// Description:
// Invalidates vertex buffer, prepares instance data for a new vertex format,
// ensures vertex declaration matches the layout provided, and that all attributes
// come from stream 0.
// --------------------------------------------------------------------------------------
void Tr2DirectInstanceData::SetLayout( const Tr2VertexDefinition& layout )
{
m_vertexBuffer = Tr2BufferAL();
m_stride = 0;
m_count = 0;
for( auto it = layout.m_items.begin(); it != layout.m_items.end(); ++it )
{
if( it->m_stream != 0 )
{
CCP_LOGERR( "Tr2DirectInstanceData: vertex layout needs to reference stream 0 only" );
return;
}
m_stride = std::max( m_stride, it->m_offset + layout.GetDataTypeSizeInBytes( it->m_dataType ) );
}
m_layout = layout;
m_vertexDeclaration = Tr2EffectStateManager::GetVertexDeclarationHandle( m_layout );
}
// --------------------------------------------------------------------------------------
// Description:
// Returns current vertex layout for instance data.
// Return value:
// Current vertex layout
// --------------------------------------------------------------------------------------
const Tr2VertexDefinition& Tr2DirectInstanceData::GetLayout() const
{
return m_layout;
}
// --------------------------------------------------------------------------------------
// Description:
// Allocates memory for instance data (if passed instance count is different from
// previous one) and returns it.
// Arguments:
// count - Number of instances
// Return value:
// Instance data buffer
// --------------------------------------------------------------------------------------
void* Tr2DirectInstanceData::GetData( unsigned count )
{
if( count != m_count )
{
m_count = count;
}
if( !m_stride || !m_count )
{
return nullptr;
}
USE_MAIN_THREAD_RENDER_CONTEXT();
if( !m_vertexBuffer.IsValid() || m_vertexBuffer.GetDesc().count < m_count )
{
m_vertexBuffer = Tr2BufferAL();
auto hr = m_vertexBuffer.Create(
m_stride,
m_count,
Tr2GpuUsage::VERTEX_BUFFER,
Tr2CpuUsage::WRITE_OFTEN,
nullptr,
renderContext );
if( FAILED( hr ) )
{
return nullptr;
}
}
uint8_t* data = nullptr;
if( FAILED( m_vertexBuffer.MapForWriting( data, renderContext ) ) )
{
return nullptr;
}
return data;
}
// --------------------------------------------------------------------------------------
// Description:
// Unmaps the vertex buffer that was mapped for writing, making data available to the GPU,
// with an early exit if buffer is invalid. Should be called after data is modified.
// --------------------------------------------------------------------------------------
void Tr2DirectInstanceData::UpdateData()
{
TRINITY_STATS_ZONE( __FUNCTION__ );
if( !m_vertexBuffer.IsValid() )
{
return;
}
USE_MAIN_THREAD_RENDER_CONTEXT();
m_vertexBuffer.UnmapForWriting( renderContext );
}
// --------------------------------------------------------------------------------------
// Description:
// Sets the bounding box.
// --------------------------------------------------------------------------------------
void Tr2DirectInstanceData::SetBoundingBox( const CcpMath::AxisAlignedBox& aabb )
{
m_aabb = aabb;
}
// --------------------------------------------------------------------------------------
// Description:
// Destroys instance data.
// --------------------------------------------------------------------------------------
void Tr2DirectInstanceData::DestroyData()
{
m_vertexBuffer = Tr2BufferAL();
m_count = 0;
}
// --------------------------------------------------------------------------------------
// Description:
// Returns per-instance data size.
// Return value:
// Per-instance data size
// --------------------------------------------------------------------------------------
unsigned Tr2DirectInstanceData::GetStride() const
{
return m_stride;
}