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 pathTransformedMeshScalar.cpp
222 lines (195 loc) · 7.66 KB
/
TransformedMeshScalar.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
////////////////////////////////////////////////////////////////////////////////
// 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 "TransformedMeshScalar.h"
TransformedMeshScalar::TransformedMeshScalar()
: mNumVertices(0),
mNumIndices(0),
mNumTriangles(0),
mpVertices(NULL),
mpIndices(NULL)
{
mpXformedPos[0] = mpXformedPos[1] = NULL;
}
TransformedMeshScalar::~TransformedMeshScalar()
{
}
void TransformedMeshScalar::Initialize(CPUTMeshDX11* pMesh)
{
mNumVertices = pMesh->GetDepthVertexCount();
mNumIndices = pMesh->GetIndexCount();
mNumTriangles = pMesh->GetTriangleCount();
mpVertices = pMesh->GetDepthVertices();
mpIndices = pMesh->GetDepthIndices();
}
//-------------------------------------------------------------------
// Trasforms the occluder vertices to screen space once every frame
//-------------------------------------------------------------------
void TransformedMeshScalar::TransformVertices(const float4x4 &cumulativeMatrix,
UINT start,
UINT end,
UINT idx)
{
for(UINT i = start; i <= end; i++)
{
float4 xform = TransformCoords(mpVertices[i].pos, cumulativeMatrix);
float4 projected = xform / xform.w;
//set to all 0s if clipped by near clip plane
mpXformedPos[idx][i] = xform.z <= xform.w ? projected : float4(0.0, 0.0, 0.0, 0.0);
}
}
void TransformedMeshScalar::Gather(float4 pOut[3], UINT triId, UINT idx)
{
for(UINT i = 0; i < 3; i++)
{
UINT index = mpIndices[(triId * 3) + i];
pOut[i] = mpXformedPos[idx][index];
}
}
//--------------------------------------------------------------------------------
// Bin the screen space transformed triangles into tiles. For single threaded version
//--------------------------------------------------------------------------------
void TransformedMeshScalar::BinTransformedTrianglesST(UINT taskId,
UINT modelId,
UINT meshId,
UINT start,
UINT end,
UINT* pBin,
USHORT* pBinModel,
USHORT* pBinMesh,
USHORT* pNumTrisInBin,
UINT idx)
{
// working on one triangle at a time
for(UINT index = start; index <= end; index++)
{
float4 xformedPos[3];
Gather(xformedPos, index, idx);
int fxPtX[3], fxPtY[3];
for(int i = 0; i < 3; i++)
{
fxPtX[i] = (int)(xformedPos[i].x + 0.5);
fxPtY[i] = (int)(xformedPos[i].y + 0.5);
}
// Compute triangle area
int triArea = (fxPtX[1] - fxPtX[0]) * (fxPtY[2] - fxPtY[0]) - (fxPtX[0] - fxPtX[2]) * (fxPtY[0] - fxPtY[1]);
// Find bounding box for screen space triangle in terms of pixels
int startX = max(min(min(fxPtX[0], fxPtX[1]), fxPtX[2]), 0);
int endX = min(max(max(fxPtX[0], fxPtX[1]), fxPtX[2]), SCREENW - 1);
int startY = max(min(min(fxPtY[0], fxPtY[1]), fxPtY[2]), 0 );
int endY = min(max(max(fxPtY[0], fxPtY[1]), fxPtY[2]), SCREENH - 1);
// Skip triangle if area is zero
if(triArea <= 0) continue;
// Dont bin screen-clipped triangles
if(endX < startX || endY < startY) continue;
// Reject the triangle if any of its verts is behind the nearclip plane
if(xformedPos[0].w > 0.0f && xformedPos[1].w > 0.0f && xformedPos[2].w > 0.0f)
{
// Convert bounding box in terms of pixels to bounding box in terms of tiles
int startXx = max(startX/TILE_WIDTH_IN_PIXELS, 0);
int endXx = min(endX/TILE_WIDTH_IN_PIXELS, SCREENW_IN_TILES-1);
int startYy = max(startY/TILE_HEIGHT_IN_PIXELS, 0);
int endYy = min(endY/TILE_HEIGHT_IN_PIXELS, SCREENH_IN_TILES-1);
// Add triangle to the tiles or bins that the bounding box covers
int row, col;
for(row = startYy; row <= endYy; row++)
{
int offset1 = YOFFSET1_ST * row;
int offset2 = YOFFSET2_ST * row;
for(col = startXx; col <= endXx; col++)
{
int idx1 = offset1 + (XOFFSET1_ST * col) + taskId;
int idx2 = offset2 + (XOFFSET2_ST * col) + (taskId * MAX_TRIS_IN_BIN_ST) + pNumTrisInBin[idx1];
pBin[idx2] = index;
pBinModel[idx2] = modelId;
pBinMesh[idx2] = meshId;
pNumTrisInBin[idx1] += 1;
}
}
}
}
}
//--------------------------------------------------------------------------------
// Bin the screen space transformed triangles into tiles. For multi threaded version
//--------------------------------------------------------------------------------
void TransformedMeshScalar::BinTransformedTrianglesMT(UINT taskId,
UINT modelId,
UINT meshId,
UINT start,
UINT end,
UINT* pBin,
USHORT* pBinModel,
USHORT* pBinMesh,
USHORT* pNumTrisInBin,
UINT idx)
{
// working on 4 triangles at a time
for(UINT index = start; index <= end; index++)
{
float4 xformedPos[3];
Gather(xformedPos, index, idx);
int fxPtX[3], fxPtY[3];
for(int i = 0; i < 3; i++)
{
fxPtX[i] = (int)(xformedPos[i].x + 0.5);
fxPtY[i] = (int)(xformedPos[i].y + 0.5);
}
// Compute triangle area
int triArea = (fxPtX[1] - fxPtX[0]) * (fxPtY[2] - fxPtY[0]) - (fxPtX[0] - fxPtX[2]) * (fxPtY[0] - fxPtY[1]);
// Skip triangle if area is zero
if(triArea <= 0) continue;
// Find bounding box for screen space triangle in terms of pixels
int startX = max(min(min(fxPtX[0], fxPtX[1]), fxPtX[2]), 0);
int endX = min(max(max(fxPtX[0], fxPtX[1]), fxPtX[2]), SCREENW-1 );
int startY = max(min(min(fxPtY[0], fxPtY[1]), fxPtY[2]), 0);
int endY = min(max(max(fxPtY[0], fxPtY[1]), fxPtY[2]), SCREENH-1);
// Dont bin screen-clipped triangles
if(endX < startX || endY < startY) continue;
// Reject the triangle if any of its verts is behind the nearclip plane
if(xformedPos[0].w > 0.0f && xformedPos[1].w > 0.0f && xformedPos[2].w > 0.0f)
{
// Convert bounding box in terms of pixels to bounding box in terms of tiles
int startXx = max(startX/TILE_WIDTH_IN_PIXELS, 0);
int endXx = min(endX/TILE_WIDTH_IN_PIXELS, SCREENW_IN_TILES-1);
int startYy = max(startY/TILE_HEIGHT_IN_PIXELS, 0);
int endYy = min(endY/TILE_HEIGHT_IN_PIXELS, SCREENH_IN_TILES-1);
// Add triangle to the tiles or bins that the bounding box covers
int row, col;
for(row = startYy; row <= endYy; row++)
{
int offset1 = YOFFSET1_MT * row;
int offset2 = YOFFSET2_MT * row;
for(col = startXx; col <= endXx; col++)
{
int idx1 = offset1 + (XOFFSET1_MT * col) + (TOFFSET1_MT * taskId);
int idx2 = offset2 + (XOFFSET2_MT * col) + (taskId * MAX_TRIS_IN_BIN_MT) + pNumTrisInBin[idx1];
pBin[idx2] = index;
pBinModel[idx2] = modelId;
pBinMesh[idx2] = meshId;
pNumTrisInBin[idx1] += 1;
}
}
}
}
}
void TransformedMeshScalar::GetOneTriangleData(float* xformedPos, UINT triId, UINT idx)
{
float4* pOut = (float4*) xformedPos;
for(int i = 0; i < 3; i++)
{
UINT index = mpIndices[(triId * 3) + i];
*(pOut + i) = mpXformedPos[idx][index];
}
}