This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathAABBoxRasterizerAVX.cpp
306 lines (265 loc) · 10.4 KB
/
AABBoxRasterizerAVX.cpp
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
////////////////////////////////////////////////////////////////////////////////
// Copyright 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
////////////////////////////////////////////////////////////////////////////////
#include "AABBoxRasterizerAVX.h"
struct AABBoxRasterizerAVX::WorldBBoxPacket
{
__m128 mCenter[3];
__m128 mHalf[3];
inline void SetLane(UINT lane, const float3& center, const float3& half)
{
mCenter[0].m128_f32[lane] = center.x;
mCenter[1].m128_f32[lane] = center.y;
mCenter[2].m128_f32[lane] = center.z;
mHalf[0].m128_f32[lane] = half.x;
mHalf[1].m128_f32[lane] = half.y;
mHalf[2].m128_f32[lane] = half.z;
}
};
AABBoxRasterizerAVX::AABBoxRasterizerAVX()
: mNumModels(0),
mpTransformedAABBox(NULL),
mpWorldBoxes(NULL),
mpModels(NULL),
mpNumTriangles(NULL),
mNumDepthTestTasks(0),
mOccludeeSizeThreshold(0.0f),
mTimeCounter(0),
mEnableFCulling(true)
{
mpCamera[0] = mpCamera[1] = NULL;
mpVisible[0] = mpVisible[1] = NULL;
mViewMatrix[0] = (__m128*)_aligned_malloc(sizeof(float) * 4 * 4, 16);
mViewMatrix[1] = (__m128*)_aligned_malloc(sizeof(float) * 4 * 4, 16);
mProjMatrix[0] = (__m128*)_aligned_malloc(sizeof(float) * 4 * 4, 16);
mProjMatrix[1] = (__m128*)_aligned_malloc(sizeof(float) * 4 * 4, 16);
mpInsideFrustum[0] = mpInsideFrustum[1] = NULL;
mpRenderTargetPixels[0] = NULL;
mpRenderTargetPixels[1] = NULL;
mpDepthSummary[0] = mpDepthSummary[1] = NULL;
mNumCulled[0] = mNumCulled[1] = 0;
for(UINT i = 0; i < AVG_COUNTER; i++)
{
mDepthTestTime[i] = 0.0;
}
}
AABBoxRasterizerAVX::~AABBoxRasterizerAVX()
{
for(UINT i = 0; i < mNumModels; i++)
{
mpModels[i]->Release();
}
_aligned_free(mViewMatrix[0]);
_aligned_free(mViewMatrix[1]);
_aligned_free(mProjMatrix[0]);
_aligned_free(mProjMatrix[1]);
_aligned_free(mpWorldBoxes);
SAFE_DELETE_ARRAY(mpVisible[0]);
SAFE_DELETE_ARRAY(mpVisible[1]);
SAFE_DELETE_ARRAY(mpTransformedAABBox);
SAFE_DELETE_ARRAY(mpInsideFrustum[0]);
SAFE_DELETE_ARRAY(mpInsideFrustum[1]);
SAFE_DELETE_ARRAY(mpNumTriangles);
SAFE_DELETE_ARRAY(mpModels);
}
//--------------------------------------------------------------------
// * Go through the asset set and determine the model count in it
// * Create data structures aor all the models in the asset set
// * For each model create the axis aligned bounding box triangle
// vertex and index list
//--------------------------------------------------------------------
void AABBoxRasterizerAVX::CreateTransformedAABBoxes(CPUTAssetSet **pAssetSet, UINT numAssetSets)
{
for(UINT assetId = 0; assetId < numAssetSets; assetId++)
{
for(UINT nodeId = 0; nodeId < pAssetSet[assetId]->GetAssetCount(); nodeId++)
{
CPUTRenderNode* pRenderNode = NULL;
CPUTResult result = pAssetSet[assetId]->GetAssetByIndex(nodeId, &pRenderNode);
ASSERT((CPUT_SUCCESS == result), _L ("Failed getting asset by index"));
if(pRenderNode->IsModel())
{
mNumModels++;
}
pRenderNode->Release();
}
}
mpVisible[0] = new bool[mNumModels];
mpVisible[1] = new bool[mNumModels];
mpTransformedAABBox = new TransformedAABBoxAVX[mNumModels];
mpModels = new CPUTModelDX11 *[mNumModels];
UINT numPackets = (mNumModels + 3) / 4;
mpWorldBoxes = (WorldBBoxPacket *)_aligned_malloc(numPackets * sizeof(WorldBBoxPacket), 16);
memset(mpWorldBoxes, 0, numPackets * sizeof(WorldBBoxPacket));
mpInsideFrustum[0] = new bool[numPackets * 4];
mpInsideFrustum[1] = new bool[numPackets * 4];
mpNumTriangles = new UINT[mNumModels];
for(UINT assetId = 0, modelId = 0; assetId < numAssetSets; assetId++)
{
for(UINT nodeId = 0; nodeId < pAssetSet[assetId]->GetAssetCount(); nodeId++)
{
CPUTRenderNode* pRenderNode = NULL;
CPUTResult result = pAssetSet[assetId]->GetAssetByIndex(nodeId, &pRenderNode);
ASSERT((CPUT_SUCCESS == result), _L ("Failed getting asset by index"));
if(pRenderNode->IsModel())
{
CPUTModelDX11 *pModel = (CPUTModelDX11*)pRenderNode;
pModel = (CPUTModelDX11*)pRenderNode;
mpModels[modelId] = pModel;
pModel->AddRef();
mpTransformedAABBox[modelId].CreateAABBVertexIndexList(pModel);
float3 bbCenter, bbHalf;
pModel->GetBoundsWorldSpace(&bbCenter, &bbHalf);
mpWorldBoxes[modelId / 4].SetLane(modelId & 3, bbCenter, bbHalf);
mpNumTriangles[modelId] = 0;
for(int meshId = 0; meshId < pModel->GetMeshCount(); meshId++)
{
mpNumTriangles[modelId] += pModel->GetMesh(meshId)->GetTriangleCount();
}
modelId++;
}
pRenderNode->Release();
}
}
}
void AABBoxRasterizerAVX::SetViewProjMatrix(float4x4 *viewMatrix, float4x4 *projMatrix, UINT idx)
{
mViewMatrix[idx][0] = _mm_loadu_ps((float*)&viewMatrix->r0);
mViewMatrix[idx][1] = _mm_loadu_ps((float*)&viewMatrix->r1);
mViewMatrix[idx][2] = _mm_loadu_ps((float*)&viewMatrix->r2);
mViewMatrix[idx][3] = _mm_loadu_ps((float*)&viewMatrix->r3);
mProjMatrix[idx][0] = _mm_loadu_ps((float*)&projMatrix->r0);
mProjMatrix[idx][1] = _mm_loadu_ps((float*)&projMatrix->r1);
mProjMatrix[idx][2] = _mm_loadu_ps((float*)&projMatrix->r2);
mProjMatrix[idx][3] = _mm_loadu_ps((float*)&projMatrix->r3);
}
//------------------------------------------------------------------------
// Go through the list of models in the asset set and render only those
// models that are marked as visible by the software occlusion culling test
//------------------------------------------------------------------------
void AABBoxRasterizerAVX::RenderVisible(CPUTAssetSet **pAssetSet,
CPUTRenderParametersDX &renderParams,
UINT numAssetSets,
UINT idx)
{
int count = 0;
for(UINT modelId = 0; modelId < mNumModels; modelId++)
{
if(mpVisible[idx][modelId])
{
mpModels[modelId]->Render(renderParams);
count++;
}
}
mNumCulled[idx] = mNumModels - count;
}
//------------------------------------------------------------------------
// Go through the list of models in the asset set and render only those
// models that are not marked as too small by the software occlusion culling test
//------------------------------------------------------------------------
void AABBoxRasterizerAVX::Render(CPUTAssetSet **pAssetSet,
CPUTRenderParametersDX &renderParams,
UINT numAssetSets,
UINT idx)
{
int count = 0;
int triCount = 0;
CPUTModelDX11::ResetFCullCount();
BoxTestSetupSSE setup;
setup.Init(mViewMatrix[idx], mProjMatrix[idx], viewportMatrix, mpCamera[idx], mOccludeeSizeThreshold);
__m128 cumulativeMatrix[4];
for(UINT assetId = 0, modelId = 0; assetId < numAssetSets; assetId++)
{
for(UINT nodeId = 0; nodeId < pAssetSet[assetId]->GetAssetCount(); nodeId++)
{
CPUTRenderNode* pRenderNode = NULL;
CPUTResult result = pAssetSet[assetId]->GetAssetByIndex(nodeId, &pRenderNode);
ASSERT((CPUT_SUCCESS == result), _L ("Failed getting asset by index"));
if(pRenderNode->IsModel())
{
if(!mpTransformedAABBox[modelId].IsTooSmall(setup, cumulativeMatrix))
{
CPUTModelDX11* model = (CPUTModelDX11*)pRenderNode;
ASSERT((model != NULL), _L("model is NULL"));
model = (CPUTModelDX11*)pRenderNode;
model->Render(renderParams);
count++;
triCount += mpNumTriangles[modelId];
}
modelId++;
}
pRenderNode->Release();
}
}
mNumFCullCount = CPUTModelDX11::GetFCullCount();
mNumCulled[idx] = mNumModels - count;
mNumTrisRendered = triCount;
}
//------------------------------------------------------------------------
// Calculate frustum culling state for a bunch of models
//------------------------------------------------------------------------
void AABBoxRasterizerAVX::CalcInsideFrustum(CPUTFrustum *pFrustum, UINT start, UINT end, UINT idx)
{
// Packet start/end (rounding up)
UINT packetStart = (start + 3) / 4;
UINT packetEnd = (end + 3) / 4;
// Prepare plane equations
__m128 planeNormal[6][3];
__m128 planeNormalSign[6][3];
__m128 planeDist[6];
__m128 signMask = _mm_castsi128_ps(_mm_set1_epi32(0x80000000));
for(UINT i = 0; i < 6; i++)
{
for (UINT j = 0; j < 3; j++)
{
planeNormal[i][j] = _mm_set1_ps(pFrustum->mpNormal[i].f[j]);
planeNormalSign[i][j] = _mm_and_ps(planeNormal[i][j], signMask);
}
planeDist[i] = _mm_set1_ps(pFrustum->mPlanes[3*8 + i]);
}
// Loop over packets
bool * __restrict visible = mpInsideFrustum[idx];
for(UINT i = packetStart; i < packetEnd; i++)
{
// Start assuming all 4 boxes are inside
__m128 inMask = _mm_castsi128_ps(_mm_set1_epi32(~0));
// Loop over planes
for (UINT j = 0; j < 6; j++)
{
// Sign for half[XYZ] so that dot product with plane normal would be maximal
__m128 halfSignX = _mm_xor_ps(mpWorldBoxes[i].mHalf[0], planeNormalSign[j][0]);
__m128 halfSignY = _mm_xor_ps(mpWorldBoxes[i].mHalf[1], planeNormalSign[j][1]);
__m128 halfSignZ = _mm_xor_ps(mpWorldBoxes[i].mHalf[2], planeNormalSign[j][2]);
// Bounding box corner to test (min corner)
__m128 cornerX = _mm_sub_ps(mpWorldBoxes[i].mCenter[0], halfSignX);
__m128 cornerY = _mm_sub_ps(mpWorldBoxes[i].mCenter[1], halfSignY);
__m128 cornerZ = _mm_sub_ps(mpWorldBoxes[i].mCenter[2], halfSignZ);
// Compute dot product
__m128 dot = planeDist[j];
dot = _mm_add_ps(dot, _mm_mul_ps(cornerX, planeNormal[j][0]));
dot = _mm_add_ps(dot, _mm_mul_ps(cornerY, planeNormal[j][1]));
dot = _mm_add_ps(dot, _mm_mul_ps(cornerZ, planeNormal[j][2]));
// The plane box is inside as long as the dot product is negative -> sign bit set
// So AND result together with current mask
inMask = _mm_and_ps(inMask, dot);
}
// Write the results for this packet
int packetMask = _mm_movemask_ps(inMask);
visible[i*4 + 0] = (packetMask >> 0) & 1;
visible[i*4 + 1] = (packetMask >> 1) & 1;
visible[i*4 + 2] = (packetMask >> 2) & 1;
visible[i*4 + 3] = (packetMask >> 3) & 1;
}
}