1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * @brief Implementation for Frontend which handles vertex processing,
26 * primitive assembly, clipping, binning, etc.
28 ******************************************************************************/
34 #include "rdtsc_core.h"
35 #include "rasterizer.h"
36 #include "conservativeRast.h"
42 #include "tessellator.h"
45 //////////////////////////////////////////////////////////////////////////
46 /// @brief Helper macro to generate a bitmask
47 static INLINE
uint32_t GenMask(uint32_t numBits
)
49 SWR_ASSERT(numBits
<= (sizeof(uint32_t) * 8), "Too many bits (%d) for %s", numBits
, __FUNCTION__
);
50 return ((1U << numBits
) - 1);
53 //////////////////////////////////////////////////////////////////////////
54 /// @brief Offsets added to post-viewport vertex positions based on
56 static const simdscalar g_pixelOffsets
[SWR_PIXEL_LOCATION_UL
+ 1] =
58 _simd_set1_ps(0.0f
), // SWR_PIXEL_LOCATION_CENTER
59 _simd_set1_ps(0.5f
), // SWR_PIXEL_LOCATION_UL
62 //////////////////////////////////////////////////////////////////////////
63 /// @brief FE handler for SwrSync.
64 /// @param pContext - pointer to SWR context.
65 /// @param pDC - pointer to draw context.
66 /// @param workerId - thread's worker id. Even thread has a unique id.
67 /// @param pUserData - Pointer to user data passed back to sync callback.
68 /// @todo This should go away when we switch this to use compute threading.
70 SWR_CONTEXT
*pContext
,
77 work
.pfnWork
= ProcessSyncBE
;
79 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
80 pTileMgr
->enqueue(0, 0, &work
);
83 //////////////////////////////////////////////////////////////////////////
84 /// @brief FE handler for SwrClearRenderTarget.
85 /// @param pContext - pointer to SWR context.
86 /// @param pDC - pointer to draw context.
87 /// @param workerId - thread's worker id. Even thread has a unique id.
88 /// @param pUserData - Pointer to user data passed back to clear callback.
89 /// @todo This should go away when we switch this to use compute threading.
91 SWR_CONTEXT
*pContext
,
96 CLEAR_DESC
*pDesc
= (CLEAR_DESC
*)pUserData
;
97 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
99 // queue a clear to each macro tile
100 // compute macro tile bounds for the specified rect
101 uint32_t macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
102 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
103 uint32_t macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
104 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
108 work
.pfnWork
= ProcessClearBE
;
109 work
.desc
.clear
= *pDesc
;
111 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
113 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
115 pTileMgr
->enqueue(x
, y
, &work
);
120 //////////////////////////////////////////////////////////////////////////
121 /// @brief FE handler for SwrStoreTiles.
122 /// @param pContext - pointer to SWR context.
123 /// @param pDC - pointer to draw context.
124 /// @param workerId - thread's worker id. Even thread has a unique id.
125 /// @param pUserData - Pointer to user data passed back to callback.
126 /// @todo This should go away when we switch this to use compute threading.
127 void ProcessStoreTiles(
128 SWR_CONTEXT
*pContext
,
133 RDTSC_START(FEProcessStoreTiles
);
134 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
135 STORE_TILES_DESC
* pDesc
= (STORE_TILES_DESC
*)pUserData
;
137 // queue a store to each macro tile
138 // compute macro tile bounds for the specified rect
139 uint32_t macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
140 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
141 uint32_t macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
142 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
146 work
.type
= STORETILES
;
147 work
.pfnWork
= ProcessStoreTileBE
;
148 work
.desc
.storeTiles
= *pDesc
;
150 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
152 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
154 pTileMgr
->enqueue(x
, y
, &work
);
158 RDTSC_STOP(FEProcessStoreTiles
, 0, pDC
->drawId
);
161 //////////////////////////////////////////////////////////////////////////
162 /// @brief FE handler for SwrInvalidateTiles.
163 /// @param pContext - pointer to SWR context.
164 /// @param pDC - pointer to draw context.
165 /// @param workerId - thread's worker id. Even thread has a unique id.
166 /// @param pUserData - Pointer to user data passed back to callback.
167 /// @todo This should go away when we switch this to use compute threading.
168 void ProcessDiscardInvalidateTiles(
169 SWR_CONTEXT
*pContext
,
174 RDTSC_START(FEProcessInvalidateTiles
);
175 DISCARD_INVALIDATE_TILES_DESC
*pDesc
= (DISCARD_INVALIDATE_TILES_DESC
*)pUserData
;
176 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
178 // compute macro tile bounds for the specified rect
179 uint32_t macroTileXMin
= (pDesc
->rect
.xmin
+ KNOB_MACROTILE_X_DIM
- 1) / KNOB_MACROTILE_X_DIM
;
180 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
/ KNOB_MACROTILE_X_DIM
) - 1;
181 uint32_t macroTileYMin
= (pDesc
->rect
.ymin
+ KNOB_MACROTILE_Y_DIM
- 1) / KNOB_MACROTILE_Y_DIM
;
182 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
/ KNOB_MACROTILE_Y_DIM
) - 1;
184 if (pDesc
->fullTilesOnly
== false)
186 // include partial tiles
187 macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
188 macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
189 macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
190 macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
193 SWR_ASSERT(macroTileXMax
<= KNOB_NUM_HOT_TILES_X
);
194 SWR_ASSERT(macroTileYMax
<= KNOB_NUM_HOT_TILES_Y
);
196 macroTileXMax
= std::min
<int32_t>(macroTileXMax
, KNOB_NUM_HOT_TILES_X
);
197 macroTileYMax
= std::min
<int32_t>(macroTileYMax
, KNOB_NUM_HOT_TILES_Y
);
201 work
.type
= DISCARDINVALIDATETILES
;
202 work
.pfnWork
= ProcessDiscardInvalidateTilesBE
;
203 work
.desc
.discardInvalidateTiles
= *pDesc
;
205 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
207 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
209 pTileMgr
->enqueue(x
, y
, &work
);
213 RDTSC_STOP(FEProcessInvalidateTiles
, 0, pDC
->drawId
);
216 //////////////////////////////////////////////////////////////////////////
217 /// @brief Computes the number of primitives given the number of verts.
218 /// @param mode - primitive topology for draw operation.
219 /// @param numPrims - number of vertices or indices for draw.
220 /// @todo Frontend needs to be refactored. This will go in appropriate place then.
221 uint32_t GetNumPrims(
222 PRIMITIVE_TOPOLOGY mode
,
227 case TOP_POINT_LIST
: return numPrims
;
228 case TOP_TRIANGLE_LIST
: return numPrims
/ 3;
229 case TOP_TRIANGLE_STRIP
: return numPrims
< 3 ? 0 : numPrims
- 2;
230 case TOP_TRIANGLE_FAN
: return numPrims
< 3 ? 0 : numPrims
- 2;
231 case TOP_TRIANGLE_DISC
: return numPrims
< 2 ? 0 : numPrims
- 1;
232 case TOP_QUAD_LIST
: return numPrims
/ 4;
233 case TOP_QUAD_STRIP
: return numPrims
< 4 ? 0 : (numPrims
- 2) / 2;
234 case TOP_LINE_STRIP
: return numPrims
< 2 ? 0 : numPrims
- 1;
235 case TOP_LINE_LIST
: return numPrims
/ 2;
236 case TOP_LINE_LOOP
: return numPrims
;
237 case TOP_RECT_LIST
: return numPrims
/ 3;
238 case TOP_LINE_LIST_ADJ
: return numPrims
/ 4;
239 case TOP_LISTSTRIP_ADJ
: return numPrims
< 3 ? 0 : numPrims
- 3;
240 case TOP_TRI_LIST_ADJ
: return numPrims
/ 6;
241 case TOP_TRI_STRIP_ADJ
: return numPrims
< 4 ? 0 : (numPrims
/ 2) - 2;
243 case TOP_PATCHLIST_1
:
244 case TOP_PATCHLIST_2
:
245 case TOP_PATCHLIST_3
:
246 case TOP_PATCHLIST_4
:
247 case TOP_PATCHLIST_5
:
248 case TOP_PATCHLIST_6
:
249 case TOP_PATCHLIST_7
:
250 case TOP_PATCHLIST_8
:
251 case TOP_PATCHLIST_9
:
252 case TOP_PATCHLIST_10
:
253 case TOP_PATCHLIST_11
:
254 case TOP_PATCHLIST_12
:
255 case TOP_PATCHLIST_13
:
256 case TOP_PATCHLIST_14
:
257 case TOP_PATCHLIST_15
:
258 case TOP_PATCHLIST_16
:
259 case TOP_PATCHLIST_17
:
260 case TOP_PATCHLIST_18
:
261 case TOP_PATCHLIST_19
:
262 case TOP_PATCHLIST_20
:
263 case TOP_PATCHLIST_21
:
264 case TOP_PATCHLIST_22
:
265 case TOP_PATCHLIST_23
:
266 case TOP_PATCHLIST_24
:
267 case TOP_PATCHLIST_25
:
268 case TOP_PATCHLIST_26
:
269 case TOP_PATCHLIST_27
:
270 case TOP_PATCHLIST_28
:
271 case TOP_PATCHLIST_29
:
272 case TOP_PATCHLIST_30
:
273 case TOP_PATCHLIST_31
:
274 case TOP_PATCHLIST_32
:
275 return numPrims
/ (mode
- TOP_PATCHLIST_BASE
);
278 case TOP_POINT_LIST_BF
:
279 case TOP_LINE_STRIP_CONT
:
280 case TOP_LINE_STRIP_BF
:
281 case TOP_LINE_STRIP_CONT_BF
:
282 case TOP_TRIANGLE_FAN_NOSTIPPLE
:
283 case TOP_TRI_STRIP_REVERSE
:
284 case TOP_PATCHLIST_BASE
:
286 SWR_ASSERT(false, "Unsupported topology: %d", mode
);
293 //////////////////////////////////////////////////////////////////////////
294 /// @brief Computes the number of verts given the number of primitives.
295 /// @param mode - primitive topology for draw operation.
296 /// @param numPrims - number of primitives for draw.
297 uint32_t GetNumVerts(
298 PRIMITIVE_TOPOLOGY mode
,
303 case TOP_POINT_LIST
: return numPrims
;
304 case TOP_TRIANGLE_LIST
: return numPrims
* 3;
305 case TOP_TRIANGLE_STRIP
: return numPrims
? numPrims
+ 2 : 0;
306 case TOP_TRIANGLE_FAN
: return numPrims
? numPrims
+ 2 : 0;
307 case TOP_TRIANGLE_DISC
: return numPrims
? numPrims
+ 1 : 0;
308 case TOP_QUAD_LIST
: return numPrims
* 4;
309 case TOP_QUAD_STRIP
: return numPrims
? numPrims
* 2 + 2 : 0;
310 case TOP_LINE_STRIP
: return numPrims
? numPrims
+ 1 : 0;
311 case TOP_LINE_LIST
: return numPrims
* 2;
312 case TOP_LINE_LOOP
: return numPrims
;
313 case TOP_RECT_LIST
: return numPrims
* 3;
314 case TOP_LINE_LIST_ADJ
: return numPrims
* 4;
315 case TOP_LISTSTRIP_ADJ
: return numPrims
? numPrims
+ 3 : 0;
316 case TOP_TRI_LIST_ADJ
: return numPrims
* 6;
317 case TOP_TRI_STRIP_ADJ
: return numPrims
? (numPrims
+ 2) * 2 : 0;
319 case TOP_PATCHLIST_1
:
320 case TOP_PATCHLIST_2
:
321 case TOP_PATCHLIST_3
:
322 case TOP_PATCHLIST_4
:
323 case TOP_PATCHLIST_5
:
324 case TOP_PATCHLIST_6
:
325 case TOP_PATCHLIST_7
:
326 case TOP_PATCHLIST_8
:
327 case TOP_PATCHLIST_9
:
328 case TOP_PATCHLIST_10
:
329 case TOP_PATCHLIST_11
:
330 case TOP_PATCHLIST_12
:
331 case TOP_PATCHLIST_13
:
332 case TOP_PATCHLIST_14
:
333 case TOP_PATCHLIST_15
:
334 case TOP_PATCHLIST_16
:
335 case TOP_PATCHLIST_17
:
336 case TOP_PATCHLIST_18
:
337 case TOP_PATCHLIST_19
:
338 case TOP_PATCHLIST_20
:
339 case TOP_PATCHLIST_21
:
340 case TOP_PATCHLIST_22
:
341 case TOP_PATCHLIST_23
:
342 case TOP_PATCHLIST_24
:
343 case TOP_PATCHLIST_25
:
344 case TOP_PATCHLIST_26
:
345 case TOP_PATCHLIST_27
:
346 case TOP_PATCHLIST_28
:
347 case TOP_PATCHLIST_29
:
348 case TOP_PATCHLIST_30
:
349 case TOP_PATCHLIST_31
:
350 case TOP_PATCHLIST_32
:
351 return numPrims
* (mode
- TOP_PATCHLIST_BASE
);
354 case TOP_POINT_LIST_BF
:
355 case TOP_LINE_STRIP_CONT
:
356 case TOP_LINE_STRIP_BF
:
357 case TOP_LINE_STRIP_CONT_BF
:
358 case TOP_TRIANGLE_FAN_NOSTIPPLE
:
359 case TOP_TRI_STRIP_REVERSE
:
360 case TOP_PATCHLIST_BASE
:
362 SWR_ASSERT(false, "Unsupported topology: %d", mode
);
369 //////////////////////////////////////////////////////////////////////////
370 /// @brief Return number of verts per primitive.
371 /// @param topology - topology
372 /// @param includeAdjVerts - include adjacent verts in primitive vertices
373 INLINE
uint32_t NumVertsPerPrim(PRIMITIVE_TOPOLOGY topology
, bool includeAdjVerts
)
375 uint32_t numVerts
= 0;
379 case TOP_POINT_LIST_BF
:
384 case TOP_LINE_LIST_ADJ
:
386 case TOP_LINE_STRIP_CONT
:
387 case TOP_LINE_STRIP_BF
:
388 case TOP_LISTSTRIP_ADJ
:
391 case TOP_TRIANGLE_LIST
:
392 case TOP_TRIANGLE_STRIP
:
393 case TOP_TRIANGLE_FAN
:
394 case TOP_TRI_LIST_ADJ
:
395 case TOP_TRI_STRIP_ADJ
:
396 case TOP_TRI_STRIP_REVERSE
:
404 case TOP_PATCHLIST_1
:
405 case TOP_PATCHLIST_2
:
406 case TOP_PATCHLIST_3
:
407 case TOP_PATCHLIST_4
:
408 case TOP_PATCHLIST_5
:
409 case TOP_PATCHLIST_6
:
410 case TOP_PATCHLIST_7
:
411 case TOP_PATCHLIST_8
:
412 case TOP_PATCHLIST_9
:
413 case TOP_PATCHLIST_10
:
414 case TOP_PATCHLIST_11
:
415 case TOP_PATCHLIST_12
:
416 case TOP_PATCHLIST_13
:
417 case TOP_PATCHLIST_14
:
418 case TOP_PATCHLIST_15
:
419 case TOP_PATCHLIST_16
:
420 case TOP_PATCHLIST_17
:
421 case TOP_PATCHLIST_18
:
422 case TOP_PATCHLIST_19
:
423 case TOP_PATCHLIST_20
:
424 case TOP_PATCHLIST_21
:
425 case TOP_PATCHLIST_22
:
426 case TOP_PATCHLIST_23
:
427 case TOP_PATCHLIST_24
:
428 case TOP_PATCHLIST_25
:
429 case TOP_PATCHLIST_26
:
430 case TOP_PATCHLIST_27
:
431 case TOP_PATCHLIST_28
:
432 case TOP_PATCHLIST_29
:
433 case TOP_PATCHLIST_30
:
434 case TOP_PATCHLIST_31
:
435 case TOP_PATCHLIST_32
:
436 numVerts
= topology
- TOP_PATCHLIST_BASE
;
439 SWR_ASSERT(false, "Unsupported topology: %d", topology
);
447 case TOP_LISTSTRIP_ADJ
:
448 case TOP_LINE_LIST_ADJ
: numVerts
= 4; break;
449 case TOP_TRI_STRIP_ADJ
:
450 case TOP_TRI_LIST_ADJ
: numVerts
= 6; break;
458 //////////////////////////////////////////////////////////////////////////
459 /// @brief Generate mask from remaining work.
460 /// @param numWorkItems - Number of items being worked on by a SIMD.
461 static INLINE simdscalari
GenerateMask(uint32_t numItemsRemaining
)
463 uint32_t numActive
= (numItemsRemaining
>= KNOB_SIMD_WIDTH
) ? KNOB_SIMD_WIDTH
: numItemsRemaining
;
464 uint32_t mask
= (numActive
> 0) ? ((1 << numActive
) - 1) : 0;
465 return _simd_castps_si(vMask(mask
));
469 //////////////////////////////////////////////////////////////////////////
470 /// @brief Gather scissor rect data based on per-prim viewport indices.
471 /// @param pScissorsInFixedPoint - array of scissor rects in 16.8 fixed point.
472 /// @param pViewportIndex - array of per-primitive vewport indexes.
473 /// @param scisXmin - output vector of per-prmitive scissor rect Xmin data.
474 /// @param scisYmin - output vector of per-prmitive scissor rect Ymin data.
475 /// @param scisXmax - output vector of per-prmitive scissor rect Xmax data.
476 /// @param scisYmax - output vector of per-prmitive scissor rect Ymax data.
478 /// @todo: Look at speeding this up -- weigh against corresponding costs in rasterizer.
479 template<size_t SimdWidth
>
480 struct GatherScissors
482 static void Gather(const SWR_RECT
* pScissorsInFixedPoint
, const uint32_t* pViewportIndex
,
483 simdscalari
&scisXmin
, simdscalari
&scisYmin
,
484 simdscalari
&scisXmax
, simdscalari
&scisYmax
)
486 SWR_ASSERT(0, "Unhandled Simd Width in Scissor Rect Gather");
491 struct GatherScissors
<8>
493 static void Gather(const SWR_RECT
* pScissorsInFixedPoint
, const uint32_t* pViewportIndex
,
494 simdscalari
&scisXmin
, simdscalari
&scisYmin
,
495 simdscalari
&scisXmax
, simdscalari
&scisYmax
)
497 scisXmin
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].xmin
,
498 pScissorsInFixedPoint
[pViewportIndex
[1]].xmin
,
499 pScissorsInFixedPoint
[pViewportIndex
[2]].xmin
,
500 pScissorsInFixedPoint
[pViewportIndex
[3]].xmin
,
501 pScissorsInFixedPoint
[pViewportIndex
[4]].xmin
,
502 pScissorsInFixedPoint
[pViewportIndex
[5]].xmin
,
503 pScissorsInFixedPoint
[pViewportIndex
[6]].xmin
,
504 pScissorsInFixedPoint
[pViewportIndex
[7]].xmin
);
505 scisYmin
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].ymin
,
506 pScissorsInFixedPoint
[pViewportIndex
[1]].ymin
,
507 pScissorsInFixedPoint
[pViewportIndex
[2]].ymin
,
508 pScissorsInFixedPoint
[pViewportIndex
[3]].ymin
,
509 pScissorsInFixedPoint
[pViewportIndex
[4]].ymin
,
510 pScissorsInFixedPoint
[pViewportIndex
[5]].ymin
,
511 pScissorsInFixedPoint
[pViewportIndex
[6]].ymin
,
512 pScissorsInFixedPoint
[pViewportIndex
[7]].ymin
);
513 scisXmax
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].xmax
,
514 pScissorsInFixedPoint
[pViewportIndex
[1]].xmax
,
515 pScissorsInFixedPoint
[pViewportIndex
[2]].xmax
,
516 pScissorsInFixedPoint
[pViewportIndex
[3]].xmax
,
517 pScissorsInFixedPoint
[pViewportIndex
[4]].xmax
,
518 pScissorsInFixedPoint
[pViewportIndex
[5]].xmax
,
519 pScissorsInFixedPoint
[pViewportIndex
[6]].xmax
,
520 pScissorsInFixedPoint
[pViewportIndex
[7]].xmax
);
521 scisYmax
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].ymax
,
522 pScissorsInFixedPoint
[pViewportIndex
[1]].ymax
,
523 pScissorsInFixedPoint
[pViewportIndex
[2]].ymax
,
524 pScissorsInFixedPoint
[pViewportIndex
[3]].ymax
,
525 pScissorsInFixedPoint
[pViewportIndex
[4]].ymax
,
526 pScissorsInFixedPoint
[pViewportIndex
[5]].ymax
,
527 pScissorsInFixedPoint
[pViewportIndex
[6]].ymax
,
528 pScissorsInFixedPoint
[pViewportIndex
[7]].ymax
);
532 //////////////////////////////////////////////////////////////////////////
533 /// @brief StreamOut - Streams vertex data out to SO buffers.
534 /// Generally, we are only streaming out a SIMDs worth of triangles.
535 /// @param pDC - pointer to draw context.
536 /// @param workerId - thread's worker id. Even thread has a unique id.
537 /// @param numPrims - Number of prims to streamout (e.g. points, lines, tris)
538 static void StreamOut(
543 uint32_t streamIndex
)
545 RDTSC_START(FEStreamout
);
547 const API_STATE
& state
= GetApiState(pDC
);
548 const SWR_STREAMOUT_STATE
&soState
= state
.soState
;
550 uint32_t soVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, false);
552 // The pPrimData buffer is sparse in that we allocate memory for all 32 attributes for each vertex.
553 uint32_t primDataDwordVertexStride
= (KNOB_NUM_ATTRIBUTES
* sizeof(float) * 4) / sizeof(uint32_t);
555 SWR_STREAMOUT_CONTEXT soContext
= { 0 };
557 // Setup buffer state pointers.
558 for (uint32_t i
= 0; i
< 4; ++i
)
560 soContext
.pBuffer
[i
] = &state
.soBuffer
[i
];
563 uint32_t numPrims
= pa
.NumPrims();
564 for (uint32_t primIndex
= 0; primIndex
< numPrims
; ++primIndex
)
567 uint32_t soMask
= soState
.streamMasks
[streamIndex
];
569 // Write all entries into primitive data buffer for SOS.
570 while (_BitScanForward(&slot
, soMask
))
572 __m128 attrib
[MAX_NUM_VERTS_PER_PRIM
]; // prim attribs (always 4 wide)
573 uint32_t paSlot
= slot
+ VERTEX_ATTRIB_START_SLOT
;
574 pa
.AssembleSingle(paSlot
, primIndex
, attrib
);
576 // Attribute offset is relative offset from start of vertex.
577 // Note that attributes start at slot 1 in the PA buffer. We need to write this
578 // to prim data starting at slot 0. Which is why we do (slot - 1).
579 // Also note: GL works slightly differently, and needs slot 0
580 uint32_t primDataAttribOffset
= slot
* sizeof(float) * 4 / sizeof(uint32_t);
582 // Store each vertex's attrib at appropriate locations in pPrimData buffer.
583 for (uint32_t v
= 0; v
< soVertsPerPrim
; ++v
)
585 uint32_t* pPrimDataAttrib
= pPrimData
+ primDataAttribOffset
+ (v
* primDataDwordVertexStride
);
587 _mm_store_ps((float*)pPrimDataAttrib
, attrib
[v
]);
589 soMask
&= ~(1 << slot
);
592 // Update pPrimData pointer
593 soContext
.pPrimData
= pPrimData
;
596 SWR_ASSERT(state
.pfnSoFunc
[streamIndex
] != nullptr, "Trying to execute uninitialized streamout jit function.");
597 state
.pfnSoFunc
[streamIndex
](soContext
);
600 // Update SO write offset. The driver provides memory for the update.
601 for (uint32_t i
= 0; i
< 4; ++i
)
603 if (state
.soBuffer
[i
].pWriteOffset
)
605 *state
.soBuffer
[i
].pWriteOffset
= soContext
.pBuffer
[i
]->streamOffset
* sizeof(uint32_t);
608 if (state
.soBuffer
[i
].soWriteEnable
)
610 pDC
->dynState
.SoWriteOffset
[i
] = soContext
.pBuffer
[i
]->streamOffset
* sizeof(uint32_t);
611 pDC
->dynState
.SoWriteOffsetDirty
[i
] = true;
615 UPDATE_STAT_FE(SoPrimStorageNeeded
[streamIndex
], soContext
.numPrimStorageNeeded
);
616 UPDATE_STAT_FE(SoNumPrimsWritten
[streamIndex
], soContext
.numPrimsWritten
);
618 RDTSC_STOP(FEStreamout
, 1, 0);
621 //////////////////////////////////////////////////////////////////////////
622 /// @brief Computes number of invocations. The current index represents
623 /// the start of the SIMD. The max index represents how much work
624 /// items are remaining. If there is less then a SIMD's xmin of work
625 /// then return the remaining amount of work.
626 /// @param curIndex - The start index for the SIMD.
627 /// @param maxIndex - The last index for all work items.
628 static INLINE
uint32_t GetNumInvocations(
632 uint32_t remainder
= (maxIndex
- curIndex
);
633 return (remainder
>= KNOB_SIMD_WIDTH
) ? KNOB_SIMD_WIDTH
: remainder
;
636 //////////////////////////////////////////////////////////////////////////
637 /// @brief Converts a streamId buffer to a cut buffer for the given stream id.
638 /// The geometry shader will loop over each active streamout buffer, assembling
639 /// primitives for the downstream stages. When multistream output is enabled,
640 /// the generated stream ID buffer from the GS needs to be converted to a cut
641 /// buffer for the primitive assembler.
642 /// @param stream - stream id to generate the cut buffer for
643 /// @param pStreamIdBase - pointer to the stream ID buffer
644 /// @param numEmittedVerts - Number of total verts emitted by the GS
645 /// @param pCutBuffer - output buffer to write cuts to
646 void ProcessStreamIdBuffer(uint32_t stream
, uint8_t* pStreamIdBase
, uint32_t numEmittedVerts
, uint8_t *pCutBuffer
)
648 SWR_ASSERT(stream
< MAX_SO_STREAMS
);
650 uint32_t numInputBytes
= (numEmittedVerts
* 2 + 7) / 8;
651 uint32_t numOutputBytes
= std::max(numInputBytes
/ 2, 1U);
653 for (uint32_t b
= 0; b
< numOutputBytes
; ++b
)
655 uint8_t curInputByte
= pStreamIdBase
[2*b
];
657 for (uint32_t i
= 0; i
< 4; ++i
)
659 if ((curInputByte
& 0x3) != stream
)
666 curInputByte
= pStreamIdBase
[2 * b
+ 1];
667 for (uint32_t i
= 0; i
< 4; ++i
)
669 if ((curInputByte
& 0x3) != stream
)
671 outByte
|= (1 << (i
+ 4));
676 *pCutBuffer
++ = outByte
;
680 THREAD SWR_GS_CONTEXT tlsGsContext
;
682 //////////////////////////////////////////////////////////////////////////
683 /// @brief Implements GS stage.
684 /// @param pDC - pointer to draw context.
685 /// @param workerId - thread's worker id. Even thread has a unique id.
686 /// @param pa - The primitive assembly object.
687 /// @param pGsOut - output stream for GS
689 typename HasStreamOutT
,
691 static void GeometryShaderStage(
697 void* pStreamCutBuffer
,
698 uint32_t* pSoPrimData
,
701 RDTSC_START(FEGeometryShader
);
703 const API_STATE
& state
= GetApiState(pDC
);
704 const SWR_GS_STATE
* pState
= &state
.gsState
;
706 SWR_ASSERT(pGsOut
!= nullptr, "GS output buffer should be initialized");
707 SWR_ASSERT(pCutBuffer
!= nullptr, "GS output cut buffer should be initialized");
709 tlsGsContext
.pStream
= (uint8_t*)pGsOut
;
710 tlsGsContext
.pCutOrStreamIdBuffer
= (uint8_t*)pCutBuffer
;
711 tlsGsContext
.PrimitiveID
= primID
;
713 uint32_t numVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, true);
714 simdvector attrib
[MAX_ATTRIBUTES
];
716 // assemble all attributes for the input primitive
717 for (uint32_t slot
= 0; slot
< pState
->numInputAttribs
; ++slot
)
719 uint32_t attribSlot
= VERTEX_ATTRIB_START_SLOT
+ slot
;
720 pa
.Assemble(attribSlot
, attrib
);
722 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
724 tlsGsContext
.vert
[i
].attrib
[attribSlot
] = attrib
[i
];
729 pa
.Assemble(VERTEX_POSITION_SLOT
, attrib
);
730 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
732 tlsGsContext
.vert
[i
].attrib
[VERTEX_POSITION_SLOT
] = attrib
[i
];
735 const uint32_t vertexStride
= sizeof(simdvertex
);
736 const uint32_t numSimdBatches
= (state
.gsState
.maxNumVerts
+ KNOB_SIMD_WIDTH
- 1) / KNOB_SIMD_WIDTH
;
737 const uint32_t inputPrimStride
= numSimdBatches
* vertexStride
;
738 const uint32_t instanceStride
= inputPrimStride
* KNOB_SIMD_WIDTH
;
739 uint32_t cutPrimStride
;
740 uint32_t cutInstanceStride
;
742 if (pState
->isSingleStream
)
744 cutPrimStride
= (state
.gsState
.maxNumVerts
+ 7) / 8;
745 cutInstanceStride
= cutPrimStride
* KNOB_SIMD_WIDTH
;
749 cutPrimStride
= AlignUp(state
.gsState
.maxNumVerts
* 2 / 8, 4);
750 cutInstanceStride
= cutPrimStride
* KNOB_SIMD_WIDTH
;
753 // record valid prims from the frontend to avoid over binning the newly generated
755 uint32_t numInputPrims
= pa
.NumPrims();
757 for (uint32_t instance
= 0; instance
< pState
->instanceCount
; ++instance
)
759 tlsGsContext
.InstanceID
= instance
;
760 tlsGsContext
.mask
= GenerateMask(numInputPrims
);
762 // execute the geometry shader
763 state
.pfnGsFunc(GetPrivateState(pDC
), &tlsGsContext
);
765 tlsGsContext
.pStream
+= instanceStride
;
766 tlsGsContext
.pCutOrStreamIdBuffer
+= cutInstanceStride
;
769 // set up new binner and state for the GS output topology
770 PFN_PROCESS_PRIMS pfnClipFunc
= nullptr;
773 switch (pState
->outputTopology
)
775 case TOP_TRIANGLE_STRIP
: pfnClipFunc
= ClipTriangles
; break;
776 case TOP_LINE_STRIP
: pfnClipFunc
= ClipLines
; break;
777 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints
; break;
778 default: SWR_ASSERT(false, "Unexpected GS output topology: %d", pState
->outputTopology
);
782 // foreach input prim:
783 // - setup a new PA based on the emitted verts for that prim
784 // - loop over the new verts, calling PA to assemble each prim
785 uint32_t* pVertexCount
= (uint32_t*)&tlsGsContext
.vertexCount
;
786 uint32_t* pPrimitiveId
= (uint32_t*)&primID
;
788 uint32_t totalPrimsGenerated
= 0;
789 for (uint32_t inputPrim
= 0; inputPrim
< numInputPrims
; ++inputPrim
)
791 uint8_t* pInstanceBase
= (uint8_t*)pGsOut
+ inputPrim
* inputPrimStride
;
792 uint8_t* pCutBufferBase
= (uint8_t*)pCutBuffer
+ inputPrim
* cutPrimStride
;
793 for (uint32_t instance
= 0; instance
< pState
->instanceCount
; ++instance
)
795 uint32_t numEmittedVerts
= pVertexCount
[inputPrim
];
796 if (numEmittedVerts
== 0)
801 uint8_t* pBase
= pInstanceBase
+ instance
* instanceStride
;
802 uint8_t* pCutBase
= pCutBufferBase
+ instance
* cutInstanceStride
;
804 uint32_t numAttribs
= state
.feNumAttributes
;
806 for (uint32_t stream
= 0; stream
< MAX_SO_STREAMS
; ++stream
)
808 bool processCutVerts
= false;
810 uint8_t* pCutBuffer
= pCutBase
;
812 // assign default stream ID, only relevant when GS is outputting a single stream
813 uint32_t streamID
= 0;
814 if (pState
->isSingleStream
)
816 processCutVerts
= true;
817 streamID
= pState
->singleStreamID
;
818 if (streamID
!= stream
) continue;
822 // early exit if this stream is not enabled for streamout
823 if (HasStreamOutT::value
&& !state
.soState
.streamEnable
[stream
])
828 // multi-stream output, need to translate StreamID buffer to a cut buffer
829 ProcessStreamIdBuffer(stream
, pCutBase
, numEmittedVerts
, (uint8_t*)pStreamCutBuffer
);
830 pCutBuffer
= (uint8_t*)pStreamCutBuffer
;
831 processCutVerts
= false;
834 PA_STATE_CUT
gsPa(pDC
, pBase
, numEmittedVerts
, pCutBuffer
, numEmittedVerts
, numAttribs
, pState
->outputTopology
, processCutVerts
);
836 while (gsPa
.GetNextStreamOutput())
840 bool assemble
= gsPa
.Assemble(VERTEX_POSITION_SLOT
, attrib
);
844 totalPrimsGenerated
+= gsPa
.NumPrims();
846 if (HasStreamOutT::value
)
848 StreamOut(pDC
, gsPa
, workerId
, pSoPrimData
, stream
);
851 if (HasRastT::value
&& state
.soState
.streamToRasterizer
== stream
)
854 // pull primitiveID from the GS output if available
855 if (state
.gsState
.emitsPrimitiveID
)
857 simdvector primIdAttrib
[3];
858 gsPa
.Assemble(VERTEX_PRIMID_SLOT
, primIdAttrib
);
859 vPrimId
= _simd_castps_si(primIdAttrib
[0].x
);
863 vPrimId
= _simd_set1_epi32(pPrimitiveId
[inputPrim
]);
866 // use viewport array index if GS declares it as an output attribute. Otherwise use index 0.
867 simdscalari vViewPortIdx
;
868 if (state
.gsState
.emitsViewportArrayIndex
)
870 simdvector vpiAttrib
[3];
871 gsPa
.Assemble(VERTEX_VIEWPORT_ARRAY_INDEX_SLOT
, vpiAttrib
);
873 // OOB indices => forced to zero.
874 simdscalari vNumViewports
= _simd_set1_epi32(KNOB_NUM_VIEWPORTS_SCISSORS
);
875 simdscalari vClearMask
= _simd_cmplt_epi32(_simd_castps_si(vpiAttrib
[0].x
), vNumViewports
);
876 vpiAttrib
[0].x
= _simd_and_ps(_simd_castsi_ps(vClearMask
), vpiAttrib
[0].x
);
878 vViewPortIdx
= _simd_castps_si(vpiAttrib
[0].x
);
882 vViewPortIdx
= _simd_set1_epi32(0);
885 pfnClipFunc(pDC
, gsPa
, workerId
, attrib
, GenMask(gsPa
.NumPrims()), vPrimId
, vViewPortIdx
);
888 } while (gsPa
.NextPrim());
894 // update GS pipeline stats
895 UPDATE_STAT_FE(GsInvocations
, numInputPrims
* pState
->instanceCount
);
896 UPDATE_STAT_FE(GsPrimitives
, totalPrimsGenerated
);
898 RDTSC_STOP(FEGeometryShader
, 1, 0);
901 //////////////////////////////////////////////////////////////////////////
902 /// @brief Allocate GS buffers
903 /// @param pDC - pointer to draw context.
904 /// @param state - API state
905 /// @param ppGsOut - pointer to GS output buffer allocation
906 /// @param ppCutBuffer - pointer to GS output cut buffer allocation
907 static INLINE
void AllocateGsBuffers(DRAW_CONTEXT
* pDC
, const API_STATE
& state
, void** ppGsOut
, void** ppCutBuffer
,
908 void **ppStreamCutBuffer
)
910 auto pArena
= pDC
->pArena
;
911 SWR_ASSERT(pArena
!= nullptr);
912 SWR_ASSERT(state
.gsState
.gsEnable
);
913 // allocate arena space to hold GS output verts
914 // @todo pack attribs
915 // @todo support multiple streams
916 const uint32_t vertexStride
= sizeof(simdvertex
);
917 const uint32_t numSimdBatches
= (state
.gsState
.maxNumVerts
+ KNOB_SIMD_WIDTH
- 1) / KNOB_SIMD_WIDTH
;
918 uint32_t size
= state
.gsState
.instanceCount
* numSimdBatches
* vertexStride
* KNOB_SIMD_WIDTH
;
919 *ppGsOut
= pArena
->AllocAligned(size
, KNOB_SIMD_WIDTH
* sizeof(float));
921 const uint32_t cutPrimStride
= (state
.gsState
.maxNumVerts
+ 7) / 8;
922 const uint32_t streamIdPrimStride
= AlignUp(state
.gsState
.maxNumVerts
* 2 / 8, 4);
923 const uint32_t cutBufferSize
= cutPrimStride
* state
.gsState
.instanceCount
* KNOB_SIMD_WIDTH
;
924 const uint32_t streamIdSize
= streamIdPrimStride
* state
.gsState
.instanceCount
* KNOB_SIMD_WIDTH
;
926 // allocate arena space to hold cut or streamid buffer, which is essentially a bitfield sized to the
927 // maximum vertex output as defined by the GS state, per SIMD lane, per GS instance
929 // allocate space for temporary per-stream cut buffer if multi-stream is enabled
930 if (state
.gsState
.isSingleStream
)
932 *ppCutBuffer
= pArena
->AllocAligned(cutBufferSize
, KNOB_SIMD_WIDTH
* sizeof(float));
933 *ppStreamCutBuffer
= nullptr;
937 *ppCutBuffer
= pArena
->AllocAligned(streamIdSize
, KNOB_SIMD_WIDTH
* sizeof(float));
938 *ppStreamCutBuffer
= pArena
->AllocAligned(cutBufferSize
, KNOB_SIMD_WIDTH
* sizeof(float));
943 //////////////////////////////////////////////////////////////////////////
944 /// @brief Contains all data generated by the HS and passed to the
945 /// tessellator and DS.
946 struct TessellationThreadLocalData
948 SWR_HS_CONTEXT hsContext
;
949 ScalarPatch patchData
[KNOB_SIMD_WIDTH
];
953 simdscalar
* pDSOutput
;
954 size_t numDSOutputVectors
;
957 THREAD TessellationThreadLocalData
* gt_pTessellationThreadData
= nullptr;
959 //////////////////////////////////////////////////////////////////////////
960 /// @brief Allocate tessellation data for this worker thread.
962 static void AllocateTessellationData(SWR_CONTEXT
* pContext
)
964 /// @TODO - Don't use thread local storage. Use Worker local storage instead.
965 if (gt_pTessellationThreadData
== nullptr)
967 gt_pTessellationThreadData
= (TessellationThreadLocalData
*)
968 AlignedMalloc(sizeof(TessellationThreadLocalData
), 64);
969 memset(gt_pTessellationThreadData
, 0, sizeof(*gt_pTessellationThreadData
));
973 //////////////////////////////////////////////////////////////////////////
974 /// @brief Implements Tessellation Stages.
975 /// @param pDC - pointer to draw context.
976 /// @param workerId - thread's worker id. Even thread has a unique id.
977 /// @param pa - The primitive assembly object.
978 /// @param pGsOut - output stream for GS
980 typename HasGeometryShaderT
,
981 typename HasStreamOutT
,
983 static void TessellationStages(
989 void* pCutStreamBuffer
,
990 uint32_t* pSoPrimData
,
993 const API_STATE
& state
= GetApiState(pDC
);
994 const SWR_TS_STATE
& tsState
= state
.tsState
;
996 SWR_ASSERT(gt_pTessellationThreadData
);
998 HANDLE tsCtx
= TSInitCtx(
1000 tsState
.partitioning
,
1001 tsState
.tsOutputTopology
,
1002 gt_pTessellationThreadData
->pTxCtx
,
1003 gt_pTessellationThreadData
->tsCtxSize
);
1004 if (tsCtx
== nullptr)
1006 gt_pTessellationThreadData
->pTxCtx
= AlignedMalloc(gt_pTessellationThreadData
->tsCtxSize
, 64);
1009 tsState
.partitioning
,
1010 tsState
.tsOutputTopology
,
1011 gt_pTessellationThreadData
->pTxCtx
,
1012 gt_pTessellationThreadData
->tsCtxSize
);
1016 PFN_PROCESS_PRIMS pfnClipFunc
= nullptr;
1017 if (HasRastT::value
)
1019 switch (tsState
.postDSTopology
)
1021 case TOP_TRIANGLE_LIST
: pfnClipFunc
= ClipTriangles
; break;
1022 case TOP_LINE_LIST
: pfnClipFunc
= ClipLines
; break;
1023 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints
; break;
1024 default: SWR_ASSERT(false, "Unexpected DS output topology: %d", tsState
.postDSTopology
);
1028 SWR_HS_CONTEXT
& hsContext
= gt_pTessellationThreadData
->hsContext
;
1029 hsContext
.pCPout
= gt_pTessellationThreadData
->patchData
;
1030 hsContext
.PrimitiveID
= primID
;
1032 uint32_t numVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, false);
1033 // Max storage for one attribute for an entire simdprimitive
1034 simdvector simdattrib
[MAX_NUM_VERTS_PER_PRIM
];
1036 // assemble all attributes for the input primitives
1037 for (uint32_t slot
= 0; slot
< tsState
.numHsInputAttribs
; ++slot
)
1039 uint32_t attribSlot
= VERTEX_ATTRIB_START_SLOT
+ slot
;
1040 pa
.Assemble(attribSlot
, simdattrib
);
1042 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
1044 hsContext
.vert
[i
].attrib
[attribSlot
] = simdattrib
[i
];
1049 memset(hsContext
.pCPout
, 0x90, sizeof(ScalarPatch
) * KNOB_SIMD_WIDTH
);
1052 uint32_t numPrims
= pa
.NumPrims();
1053 hsContext
.mask
= GenerateMask(numPrims
);
1056 RDTSC_START(FEHullShader
);
1057 state
.pfnHsFunc(GetPrivateState(pDC
), &hsContext
);
1058 RDTSC_STOP(FEHullShader
, 0, 0);
1060 UPDATE_STAT_FE(HsInvocations
, numPrims
);
1062 const uint32_t* pPrimId
= (const uint32_t*)&primID
;
1064 for (uint32_t p
= 0; p
< numPrims
; ++p
)
1067 SWR_TS_TESSELLATED_DATA tsData
= { 0 };
1068 RDTSC_START(FETessellation
);
1069 TSTessellate(tsCtx
, hsContext
.pCPout
[p
].tessFactors
, tsData
);
1070 RDTSC_STOP(FETessellation
, 0, 0);
1072 if (tsData
.NumPrimitives
== 0)
1076 SWR_ASSERT(tsData
.NumDomainPoints
);
1078 // Allocate DS Output memory
1079 uint32_t requiredDSVectorInvocations
= AlignUp(tsData
.NumDomainPoints
, KNOB_SIMD_WIDTH
) / KNOB_SIMD_WIDTH
;
1080 size_t requiredDSOutputVectors
= requiredDSVectorInvocations
* tsState
.numDsOutputAttribs
;
1081 size_t requiredAllocSize
= sizeof(simdvector
) * requiredDSOutputVectors
;
1082 if (requiredDSOutputVectors
> gt_pTessellationThreadData
->numDSOutputVectors
)
1084 AlignedFree(gt_pTessellationThreadData
->pDSOutput
);
1085 gt_pTessellationThreadData
->pDSOutput
= (simdscalar
*)AlignedMalloc(requiredAllocSize
, 64);
1086 gt_pTessellationThreadData
->numDSOutputVectors
= requiredDSOutputVectors
;
1088 SWR_ASSERT(gt_pTessellationThreadData
->pDSOutput
);
1089 SWR_ASSERT(gt_pTessellationThreadData
->numDSOutputVectors
>= requiredDSOutputVectors
);
1092 memset(gt_pTessellationThreadData
->pDSOutput
, 0x90, requiredAllocSize
);
1095 // Run Domain Shader
1096 SWR_DS_CONTEXT dsContext
;
1097 dsContext
.PrimitiveID
= pPrimId
[p
];
1098 dsContext
.pCpIn
= &hsContext
.pCPout
[p
];
1099 dsContext
.pDomainU
= (simdscalar
*)tsData
.pDomainPointsU
;
1100 dsContext
.pDomainV
= (simdscalar
*)tsData
.pDomainPointsV
;
1101 dsContext
.pOutputData
= gt_pTessellationThreadData
->pDSOutput
;
1102 dsContext
.vectorStride
= requiredDSVectorInvocations
;
1104 uint32_t dsInvocations
= 0;
1106 for (dsContext
.vectorOffset
= 0; dsContext
.vectorOffset
< requiredDSVectorInvocations
; ++dsContext
.vectorOffset
)
1108 dsContext
.mask
= GenerateMask(tsData
.NumDomainPoints
- dsInvocations
);
1110 RDTSC_START(FEDomainShader
);
1111 state
.pfnDsFunc(GetPrivateState(pDC
), &dsContext
);
1112 RDTSC_STOP(FEDomainShader
, 0, 0);
1114 dsInvocations
+= KNOB_SIMD_WIDTH
;
1116 UPDATE_STAT_FE(DsInvocations
, tsData
.NumDomainPoints
);
1120 dsContext
.pOutputData
,
1121 dsContext
.vectorStride
,
1122 tsState
.numDsOutputAttribs
,
1124 tsData
.NumPrimitives
,
1125 tsState
.postDSTopology
);
1127 while (tessPa
.HasWork())
1129 if (HasGeometryShaderT::value
)
1131 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(
1132 pDC
, workerId
, tessPa
, pGsOut
, pCutBuffer
, pCutStreamBuffer
, pSoPrimData
,
1133 _simd_set1_epi32(dsContext
.PrimitiveID
));
1137 if (HasStreamOutT::value
)
1139 StreamOut(pDC
, tessPa
, workerId
, pSoPrimData
, 0);
1142 if (HasRastT::value
)
1144 simdvector prim
[3]; // Only deal with triangles, lines, or points
1145 RDTSC_START(FEPAAssemble
);
1146 #if SWR_ENABLE_ASSERTS
1149 tessPa
.Assemble(VERTEX_POSITION_SLOT
, prim
);
1150 RDTSC_STOP(FEPAAssemble
, 1, 0);
1151 SWR_ASSERT(assemble
);
1153 SWR_ASSERT(pfnClipFunc
);
1154 pfnClipFunc(pDC
, tessPa
, workerId
, prim
,
1155 GenMask(tessPa
.NumPrims()), _simd_set1_epi32(dsContext
.PrimitiveID
), _simd_set1_epi32(0));
1161 } // while (tessPa.HasWork())
1162 } // for (uint32_t p = 0; p < numPrims; ++p)
1164 TSDestroyCtx(tsCtx
);
1167 //////////////////////////////////////////////////////////////////////////
1168 /// @brief FE handler for SwrDraw.
1169 /// @tparam IsIndexedT - Is indexed drawing enabled
1170 /// @tparam HasTessellationT - Is tessellation enabled
1171 /// @tparam HasGeometryShaderT::value - Is the geometry shader stage enabled
1172 /// @tparam HasStreamOutT - Is stream-out enabled
1173 /// @tparam HasRastT - Is rasterization enabled
1174 /// @param pContext - pointer to SWR context.
1175 /// @param pDC - pointer to draw context.
1176 /// @param workerId - thread's worker id.
1177 /// @param pUserData - Pointer to DRAW_WORK
1179 typename IsIndexedT
,
1180 typename IsCutIndexEnabledT
,
1181 typename HasTessellationT
,
1182 typename HasGeometryShaderT
,
1183 typename HasStreamOutT
,
1186 SWR_CONTEXT
*pContext
,
1192 #if KNOB_ENABLE_TOSS_POINTS
1193 if (KNOB_TOSS_QUEUE_FE
)
1199 RDTSC_START(FEProcessDraw
);
1201 DRAW_WORK
& work
= *(DRAW_WORK
*)pUserData
;
1202 const API_STATE
& state
= GetApiState(pDC
);
1203 __m256i vScale
= _mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0);
1204 SWR_VS_CONTEXT vsContext
;
1208 uint32_t endVertex
= work
.numVerts
;
1210 const int32_t* pLastRequestedIndex
= nullptr;
1211 if (IsIndexedT::value
)
1216 indexSize
= sizeof(uint32_t);
1217 pLastRequestedIndex
= &(work
.pIB
[endVertex
]);
1220 indexSize
= sizeof(uint16_t);
1221 // nasty address offset to last index
1222 pLastRequestedIndex
= (int32_t*)(&(((uint16_t*)work
.pIB
)[endVertex
]));
1225 indexSize
= sizeof(uint8_t);
1226 // nasty address offset to last index
1227 pLastRequestedIndex
= (int32_t*)(&(((uint8_t*)work
.pIB
)[endVertex
]));
1235 // No cuts, prune partial primitives.
1236 endVertex
= GetNumVerts(state
.topology
, GetNumPrims(state
.topology
, work
.numVerts
));
1239 SWR_FETCH_CONTEXT fetchInfo
= { 0 };
1240 fetchInfo
.pStreams
= &state
.vertexBuffers
[0];
1241 fetchInfo
.StartInstance
= work
.startInstance
;
1242 fetchInfo
.StartVertex
= 0;
1244 vsContext
.pVin
= &vin
;
1246 if (IsIndexedT::value
)
1248 fetchInfo
.BaseVertex
= work
.baseVertex
;
1250 // if the entire index buffer isn't being consumed, set the last index
1251 // so that fetches < a SIMD wide will be masked off
1252 fetchInfo
.pLastIndex
= (const int32_t*)(((uint8_t*)state
.indexBuffer
.pIndices
) + state
.indexBuffer
.size
);
1253 if (pLastRequestedIndex
< fetchInfo
.pLastIndex
)
1255 fetchInfo
.pLastIndex
= pLastRequestedIndex
;
1260 fetchInfo
.StartVertex
= work
.startVertex
;
1263 #ifdef KNOB_ENABLE_RDTSC
1264 uint32_t numPrims
= GetNumPrims(state
.topology
, work
.numVerts
);
1267 void* pGsOut
= nullptr;
1268 void* pCutBuffer
= nullptr;
1269 void* pStreamCutBuffer
= nullptr;
1270 if (HasGeometryShaderT::value
)
1272 AllocateGsBuffers(pDC
, state
, &pGsOut
, &pCutBuffer
, &pStreamCutBuffer
);
1275 if (HasTessellationT::value
)
1277 SWR_ASSERT(state
.tsState
.tsEnable
== true);
1278 SWR_ASSERT(state
.pfnHsFunc
!= nullptr);
1279 SWR_ASSERT(state
.pfnDsFunc
!= nullptr);
1281 AllocateTessellationData(pContext
);
1285 SWR_ASSERT(state
.tsState
.tsEnable
== false);
1286 SWR_ASSERT(state
.pfnHsFunc
== nullptr);
1287 SWR_ASSERT(state
.pfnDsFunc
== nullptr);
1290 // allocate space for streamout input prim data
1291 uint32_t* pSoPrimData
= nullptr;
1292 if (HasStreamOutT::value
)
1294 pSoPrimData
= (uint32_t*)pDC
->pArena
->AllocAligned(4096, 16);
1297 // choose primitive assembler
1298 PA_FACTORY
<IsIndexedT
, IsCutIndexEnabledT
> paFactory(pDC
, state
.topology
, work
.numVerts
);
1299 PA_STATE
& pa
= paFactory
.GetPA();
1301 /// @todo: temporarily move instance loop in the FE to ensure SO ordering
1302 for (uint32_t instanceNum
= 0; instanceNum
< work
.numInstances
; instanceNum
++)
1307 if (IsIndexedT::value
)
1309 fetchInfo
.pIndices
= work
.pIB
;
1313 vIndex
= _simd_add_epi32(_simd_set1_epi32(work
.startVertexID
), vScale
);
1314 fetchInfo
.pIndices
= (const int32_t*)&vIndex
;
1317 fetchInfo
.CurInstance
= instanceNum
;
1318 vsContext
.InstanceID
= instanceNum
;
1320 while (pa
.HasWork())
1322 // PaGetNextVsOutput currently has the side effect of updating some PA state machine state.
1323 // So we need to keep this outside of (i < endVertex) check.
1324 simdmask
* pvCutIndices
= nullptr;
1325 if (IsIndexedT::value
)
1327 pvCutIndices
= &pa
.GetNextVsIndices();
1330 simdvertex
& vout
= pa
.GetNextVsOutput();
1331 vsContext
.pVout
= &vout
;
1336 // 1. Execute FS/VS for a single SIMD.
1337 RDTSC_START(FEFetchShader
);
1338 state
.pfnFetchFunc(fetchInfo
, vin
);
1339 RDTSC_STOP(FEFetchShader
, 0, 0);
1341 // forward fetch generated vertex IDs to the vertex shader
1342 vsContext
.VertexID
= fetchInfo
.VertexID
;
1344 // Setup active mask for vertex shader.
1345 vsContext
.mask
= GenerateMask(endVertex
- i
);
1347 // forward cut mask to the PA
1348 if (IsIndexedT::value
)
1350 *pvCutIndices
= _simd_movemask_ps(_simd_castsi_ps(fetchInfo
.CutMask
));
1353 UPDATE_STAT_FE(IaVertices
, GetNumInvocations(i
, endVertex
));
1355 #if KNOB_ENABLE_TOSS_POINTS
1356 if (!KNOB_TOSS_FETCH
)
1359 RDTSC_START(FEVertexShader
);
1360 state
.pfnVertexFunc(GetPrivateState(pDC
), &vsContext
);
1361 RDTSC_STOP(FEVertexShader
, 0, 0);
1363 UPDATE_STAT_FE(VsInvocations
, GetNumInvocations(i
, endVertex
));
1367 // 2. Assemble primitives given the last two SIMD.
1370 simdvector prim
[MAX_NUM_VERTS_PER_PRIM
];
1371 // PaAssemble returns false if there is not enough verts to assemble.
1372 RDTSC_START(FEPAAssemble
);
1373 bool assemble
= pa
.Assemble(VERTEX_POSITION_SLOT
, prim
);
1374 RDTSC_STOP(FEPAAssemble
, 1, 0);
1376 #if KNOB_ENABLE_TOSS_POINTS
1377 if (!KNOB_TOSS_FETCH
)
1380 #if KNOB_ENABLE_TOSS_POINTS
1386 UPDATE_STAT_FE(IaPrimitives
, pa
.NumPrims());
1388 if (HasTessellationT::value
)
1390 TessellationStages
<HasGeometryShaderT
, HasStreamOutT
, HasRastT
>(
1391 pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, pa
.GetPrimID(work
.startPrimID
));
1393 else if (HasGeometryShaderT::value
)
1395 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(
1396 pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, pa
.GetPrimID(work
.startPrimID
));
1400 // If streamout is enabled then stream vertices out to memory.
1401 if (HasStreamOutT::value
)
1403 StreamOut(pDC
, pa
, workerId
, pSoPrimData
, 0);
1406 if (HasRastT::value
)
1408 SWR_ASSERT(pDC
->pState
->pfnProcessPrims
);
1409 pDC
->pState
->pfnProcessPrims(pDC
, pa
, workerId
, prim
,
1410 GenMask(pa
.NumPrims()), pa
.GetPrimID(work
.startPrimID
), _simd_set1_epi32(0));
1416 } while (pa
.NextPrim());
1418 i
+= KNOB_SIMD_WIDTH
;
1419 if (IsIndexedT::value
)
1421 fetchInfo
.pIndices
= (int*)((uint8_t*)fetchInfo
.pIndices
+ KNOB_SIMD_WIDTH
* indexSize
);
1425 vIndex
= _simd_add_epi32(vIndex
, _simd_set1_epi32(KNOB_SIMD_WIDTH
));
1431 RDTSC_STOP(FEProcessDraw
, numPrims
* work
.numInstances
, pDC
->drawId
);
1434 struct FEDrawChooser
1436 typedef PFN_FE_WORK_FUNC FuncType
;
1438 template <typename
... ArgsB
>
1439 static FuncType
GetFunc()
1441 return ProcessDraw
<ArgsB
...>;
1446 // Selector for correct templated Draw front-end function
1447 PFN_FE_WORK_FUNC
GetProcessDrawFunc(
1449 bool IsCutIndexEnabled
,
1450 bool HasTessellation
,
1451 bool HasGeometryShader
,
1453 bool HasRasterization
)
1455 return TemplateArgUnroller
<FEDrawChooser
>::GetFunc(IsIndexed
, IsCutIndexEnabled
, HasTessellation
, HasGeometryShader
, HasStreamOut
, HasRasterization
);
1458 //////////////////////////////////////////////////////////////////////////
1459 /// @brief Processes attributes for the backend based on linkage mask and
1460 /// linkage map. Essentially just doing an SOA->AOS conversion and pack.
1461 /// @param pDC - Draw context
1462 /// @param pa - Primitive Assembly state
1463 /// @param linkageMask - Specifies which VS outputs are routed to PS.
1464 /// @param pLinkageMap - maps VS attribute slot to PS slot
1465 /// @param triIndex - Triangle to process attributes for
1466 /// @param pBuffer - Output result
1467 template<typename NumVertsT
, typename IsSwizzledT
, typename HasConstantInterpT
, typename IsDegenerate
>
1468 INLINE
void ProcessAttributes(
1475 static_assert(NumVertsT::value
> 0 && NumVertsT::value
<= 3, "Invalid value for NumVertsT");
1476 const SWR_BACKEND_STATE
& backendState
= pDC
->pState
->state
.backendState
;
1477 // Conservative Rasterization requires degenerate tris to have constant attribute interpolation
1478 LONG constantInterpMask
= IsDegenerate::value
? 0xFFFFFFFF : backendState
.constantInterpolationMask
;
1479 const uint32_t provokingVertex
= pDC
->pState
->state
.frontendState
.topologyProvokingVertex
;
1480 const PRIMITIVE_TOPOLOGY topo
= pDC
->pState
->state
.topology
;
1482 static const float constTable
[3][4] = {
1483 {0.0f
, 0.0f
, 0.0f
, 0.0f
},
1484 {0.0f
, 0.0f
, 0.0f
, 1.0f
},
1485 {1.0f
, 1.0f
, 1.0f
, 1.0f
}
1488 for (uint32_t i
= 0; i
< backendState
.numAttributes
; ++i
)
1491 if (IsSwizzledT::value
)
1493 SWR_ATTRIB_SWIZZLE attribSwizzle
= backendState
.swizzleMap
[i
];
1494 inputSlot
= VERTEX_ATTRIB_START_SLOT
+ attribSwizzle
.sourceAttrib
;
1499 inputSlot
= VERTEX_ATTRIB_START_SLOT
+ i
;
1502 __m128 attrib
[3]; // triangle attribs (always 4 wide)
1503 float* pAttribStart
= pBuffer
;
1505 if (HasConstantInterpT::value
|| IsDegenerate::value
)
1507 if (_bittest(&constantInterpMask
, i
))
1510 uint32_t adjustedTriIndex
;
1511 static const uint32_t tristripProvokingVertex
[] = { 0, 2, 1 };
1512 static const int32_t quadProvokingTri
[2][4] = { {0, 0, 0, 1}, {0, -1, 0, 0} };
1513 static const uint32_t quadProvokingVertex
[2][4] = { {0, 1, 2, 2}, {0, 1, 1, 2} };
1514 static const int32_t qstripProvokingTri
[2][4] = { {0, 0, 0, 1}, {-1, 0, 0, 0} };
1515 static const uint32_t qstripProvokingVertex
[2][4] = { {0, 1, 2, 1}, {0, 0, 2, 1} };
1519 adjustedTriIndex
= triIndex
+ quadProvokingTri
[triIndex
& 1][provokingVertex
];
1520 vid
= quadProvokingVertex
[triIndex
& 1][provokingVertex
];
1522 case TOP_QUAD_STRIP
:
1523 adjustedTriIndex
= triIndex
+ qstripProvokingTri
[triIndex
& 1][provokingVertex
];
1524 vid
= qstripProvokingVertex
[triIndex
& 1][provokingVertex
];
1526 case TOP_TRIANGLE_STRIP
:
1527 adjustedTriIndex
= triIndex
;
1528 vid
= (triIndex
& 1)
1529 ? tristripProvokingVertex
[provokingVertex
]
1533 adjustedTriIndex
= triIndex
;
1534 vid
= provokingVertex
;
1538 pa
.AssembleSingle(inputSlot
, adjustedTriIndex
, attrib
);
1540 for (uint32_t i
= 0; i
< NumVertsT::value
; ++i
)
1542 _mm_store_ps(pBuffer
, attrib
[vid
]);
1548 pa
.AssembleSingle(inputSlot
, triIndex
, attrib
);
1550 for (uint32_t i
= 0; i
< NumVertsT::value
; ++i
)
1552 _mm_store_ps(pBuffer
, attrib
[i
]);
1559 pa
.AssembleSingle(inputSlot
, triIndex
, attrib
);
1561 for (uint32_t i
= 0; i
< NumVertsT::value
; ++i
)
1563 _mm_store_ps(pBuffer
, attrib
[i
]);
1568 // pad out the attrib buffer to 3 verts to ensure the triangle
1569 // interpolation code in the pixel shader works correctly for the
1570 // 3 topologies - point, line, tri. This effectively zeros out the
1571 // effect of the missing vertices in the triangle interpolation.
1572 for (uint32_t v
= NumVertsT::value
; v
< 3; ++v
)
1574 _mm_store_ps(pBuffer
, attrib
[NumVertsT::value
- 1]);
1578 // check for constant source overrides
1579 if (IsSwizzledT::value
)
1581 uint32_t mask
= backendState
.swizzleMap
[i
].componentOverrideMask
;
1585 while (_BitScanForward(&comp
, mask
))
1587 mask
&= ~(1 << comp
);
1589 float constantValue
= 0.0f
;
1590 switch ((SWR_CONSTANT_SOURCE
)backendState
.swizzleMap
[i
].constantSource
)
1592 case SWR_CONSTANT_SOURCE_CONST_0000
:
1593 case SWR_CONSTANT_SOURCE_CONST_0001_FLOAT
:
1594 case SWR_CONSTANT_SOURCE_CONST_1111_FLOAT
:
1595 constantValue
= constTable
[backendState
.swizzleMap
[i
].constantSource
][comp
];
1597 case SWR_CONSTANT_SOURCE_PRIM_ID
:
1598 constantValue
= *(float*)&primId
;
1602 // apply constant value to all 3 vertices
1603 for (uint32_t v
= 0; v
< 3; ++v
)
1605 pAttribStart
[comp
+ v
* 4] = constantValue
;
1614 typedef void(*PFN_PROCESS_ATTRIBUTES
)(DRAW_CONTEXT
*, PA_STATE
&, uint32_t, uint32_t, float*);
1616 struct ProcessAttributesChooser
1618 typedef PFN_PROCESS_ATTRIBUTES FuncType
;
1620 template <typename
... ArgsB
>
1621 static FuncType
GetFunc()
1623 return ProcessAttributes
<ArgsB
...>;
1627 PFN_PROCESS_ATTRIBUTES
GetProcessAttributesFunc(uint32_t NumVerts
, bool IsSwizzled
, bool HasConstantInterp
, bool IsDegenerate
= false)
1629 return TemplateArgUnroller
<ProcessAttributesChooser
>::GetFunc(IntArg
<1, 3>{NumVerts
}, IsSwizzled
, HasConstantInterp
, IsDegenerate
);
1632 //////////////////////////////////////////////////////////////////////////
1633 /// @brief Processes enabled user clip distances. Loads the active clip
1634 /// distances from the PA, sets up barycentric equations, and
1635 /// stores the results to the output buffer
1636 /// @param pa - Primitive Assembly state
1637 /// @param primIndex - primitive index to process
1638 /// @param clipDistMask - mask of enabled clip distances
1639 /// @param pUserClipBuffer - buffer to store results
1640 template<uint32_t NumVerts
>
1641 void ProcessUserClipDist(PA_STATE
& pa
, uint32_t primIndex
, uint8_t clipDistMask
, float* pUserClipBuffer
)
1644 while (_BitScanForward(&clipDist
, clipDistMask
))
1646 clipDistMask
&= ~(1 << clipDist
);
1647 uint32_t clipSlot
= clipDist
>> 2;
1648 uint32_t clipComp
= clipDist
& 0x3;
1649 uint32_t clipAttribSlot
= clipSlot
== 0 ?
1650 VERTEX_CLIPCULL_DIST_LO_SLOT
: VERTEX_CLIPCULL_DIST_HI_SLOT
;
1652 __m128 primClipDist
[3];
1653 pa
.AssembleSingle(clipAttribSlot
, primIndex
, primClipDist
);
1655 float vertClipDist
[NumVerts
];
1656 for (uint32_t e
= 0; e
< NumVerts
; ++e
)
1658 OSALIGNSIMD(float) aVertClipDist
[4];
1659 _mm_store_ps(aVertClipDist
, primClipDist
[e
]);
1660 vertClipDist
[e
] = aVertClipDist
[clipComp
];
1663 // setup plane equations for barycentric interpolation in the backend
1664 float baryCoeff
[NumVerts
];
1665 for (uint32_t e
= 0; e
< NumVerts
- 1; ++e
)
1667 baryCoeff
[e
] = vertClipDist
[e
] - vertClipDist
[NumVerts
- 1];
1669 baryCoeff
[NumVerts
- 1] = vertClipDist
[NumVerts
- 1];
1671 for (uint32_t e
= 0; e
< NumVerts
; ++e
)
1673 *(pUserClipBuffer
++) = baryCoeff
[e
];
1678 //////////////////////////////////////////////////////////////////////////
1679 /// @brief Convert the X,Y coords of a triangle to the requested Fixed
1680 /// Point precision from FP32.
1681 template <typename PT
= FixedPointTraits
<Fixed_16_8
>>
1682 INLINE simdscalari
fpToFixedPointVertical(const simdscalar vIn
)
1684 simdscalar vFixed
= _simd_mul_ps(vIn
, _simd_set1_ps(PT::ScaleT::value
));
1685 return _simd_cvtps_epi32(vFixed
);
1688 //////////////////////////////////////////////////////////////////////////
1689 /// @brief Helper function to set the X,Y coords of a triangle to the
1690 /// requested Fixed Point precision from FP32.
1691 /// @param tri: simdvector[3] of FP triangle verts
1692 /// @param vXi: fixed point X coords of tri verts
1693 /// @param vYi: fixed point Y coords of tri verts
1694 INLINE
static void FPToFixedPoint(const simdvector
* const tri
, simdscalari (&vXi
)[3], simdscalari (&vYi
)[3])
1696 vXi
[0] = fpToFixedPointVertical(tri
[0].x
);
1697 vYi
[0] = fpToFixedPointVertical(tri
[0].y
);
1698 vXi
[1] = fpToFixedPointVertical(tri
[1].x
);
1699 vYi
[1] = fpToFixedPointVertical(tri
[1].y
);
1700 vXi
[2] = fpToFixedPointVertical(tri
[2].x
);
1701 vYi
[2] = fpToFixedPointVertical(tri
[2].y
);
1704 //////////////////////////////////////////////////////////////////////////
1705 /// @brief Calculate bounding box for current triangle
1706 /// @tparam CT: ConservativeRastFETraits type
1707 /// @param vX: fixed point X position for triangle verts
1708 /// @param vY: fixed point Y position for triangle verts
1709 /// @param bbox: fixed point bbox
1710 /// *Note*: expects vX, vY to be in the correct precision for the type
1711 /// of rasterization. This avoids unnecessary FP->fixed conversions.
1712 template <typename CT
>
1713 INLINE
void calcBoundingBoxIntVertical(const simdvector
* const tri
, simdscalari (&vX
)[3], simdscalari (&vY
)[3], simdBBox
&bbox
)
1715 simdscalari vMinX
= vX
[0];
1716 vMinX
= _simd_min_epi32(vMinX
, vX
[1]);
1717 vMinX
= _simd_min_epi32(vMinX
, vX
[2]);
1719 simdscalari vMaxX
= vX
[0];
1720 vMaxX
= _simd_max_epi32(vMaxX
, vX
[1]);
1721 vMaxX
= _simd_max_epi32(vMaxX
, vX
[2]);
1723 simdscalari vMinY
= vY
[0];
1724 vMinY
= _simd_min_epi32(vMinY
, vY
[1]);
1725 vMinY
= _simd_min_epi32(vMinY
, vY
[2]);
1727 simdscalari vMaxY
= vY
[0];
1728 vMaxY
= _simd_max_epi32(vMaxY
, vY
[1]);
1729 vMaxY
= _simd_max_epi32(vMaxY
, vY
[2]);
1737 //////////////////////////////////////////////////////////////////////////
1738 /// @brief FEConservativeRastT specialization of calcBoundingBoxIntVertical
1739 /// Offsets BBox for conservative rast
1741 INLINE
void calcBoundingBoxIntVertical
<FEConservativeRastT
>(const simdvector
* const tri
, simdscalari (&vX
)[3], simdscalari (&vY
)[3], simdBBox
&bbox
)
1743 // FE conservative rast traits
1744 typedef FEConservativeRastT CT
;
1746 simdscalari vMinX
= vX
[0];
1747 vMinX
= _simd_min_epi32(vMinX
, vX
[1]);
1748 vMinX
= _simd_min_epi32(vMinX
, vX
[2]);
1750 simdscalari vMaxX
= vX
[0];
1751 vMaxX
= _simd_max_epi32(vMaxX
, vX
[1]);
1752 vMaxX
= _simd_max_epi32(vMaxX
, vX
[2]);
1754 simdscalari vMinY
= vY
[0];
1755 vMinY
= _simd_min_epi32(vMinY
, vY
[1]);
1756 vMinY
= _simd_min_epi32(vMinY
, vY
[2]);
1758 simdscalari vMaxY
= vY
[0];
1759 vMaxY
= _simd_max_epi32(vMaxY
, vY
[1]);
1760 vMaxY
= _simd_max_epi32(vMaxY
, vY
[2]);
1762 /// Bounding box needs to be expanded by 1/512 before snapping to 16.8 for conservative rasterization
1763 /// expand bbox by 1/256; coverage will be correctly handled in the rasterizer.
1764 bbox
.xmin
= _simd_sub_epi32(vMinX
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1765 bbox
.xmax
= _simd_add_epi32(vMaxX
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1766 bbox
.ymin
= _simd_sub_epi32(vMinY
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1767 bbox
.ymax
= _simd_add_epi32(vMaxY
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1770 //////////////////////////////////////////////////////////////////////////
1771 /// @brief Bin triangle primitives to macro tiles. Performs setup, clipping
1772 /// culling, viewport transform, etc.
1773 /// @param pDC - pointer to draw context.
1774 /// @param pa - The primitive assembly object.
1775 /// @param workerId - thread's worker id. Even thread has a unique id.
1776 /// @param tri - Contains triangle position data for SIMDs worth of triangles.
1777 /// @param primID - Primitive ID for each triangle.
1778 /// @param viewportIdx - viewport array index for each triangle.
1779 /// @tparam CT - ConservativeRastFETraits
1780 template <typename CT
>
1788 simdscalari viewportIdx
)
1790 RDTSC_START(FEBinTriangles
);
1792 const API_STATE
& state
= GetApiState(pDC
);
1793 const SWR_RASTSTATE
& rastState
= state
.rastState
;
1794 const SWR_FRONTEND_STATE
& feState
= state
.frontendState
;
1795 const SWR_GS_STATE
& gsState
= state
.gsState
;
1796 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
1799 simdscalar vRecipW0
= _simd_set1_ps(1.0f
);
1800 simdscalar vRecipW1
= _simd_set1_ps(1.0f
);
1801 simdscalar vRecipW2
= _simd_set1_ps(1.0f
);
1803 if (feState
.vpTransformDisable
)
1805 // RHW is passed in directly when VP transform is disabled
1806 vRecipW0
= tri
[0].v
[3];
1807 vRecipW1
= tri
[1].v
[3];
1808 vRecipW2
= tri
[2].v
[3];
1812 // Perspective divide
1813 vRecipW0
= _simd_div_ps(_simd_set1_ps(1.0f
), tri
[0].w
);
1814 vRecipW1
= _simd_div_ps(_simd_set1_ps(1.0f
), tri
[1].w
);
1815 vRecipW2
= _simd_div_ps(_simd_set1_ps(1.0f
), tri
[2].w
);
1817 tri
[0].v
[0] = _simd_mul_ps(tri
[0].v
[0], vRecipW0
);
1818 tri
[1].v
[0] = _simd_mul_ps(tri
[1].v
[0], vRecipW1
);
1819 tri
[2].v
[0] = _simd_mul_ps(tri
[2].v
[0], vRecipW2
);
1821 tri
[0].v
[1] = _simd_mul_ps(tri
[0].v
[1], vRecipW0
);
1822 tri
[1].v
[1] = _simd_mul_ps(tri
[1].v
[1], vRecipW1
);
1823 tri
[2].v
[1] = _simd_mul_ps(tri
[2].v
[1], vRecipW2
);
1825 tri
[0].v
[2] = _simd_mul_ps(tri
[0].v
[2], vRecipW0
);
1826 tri
[1].v
[2] = _simd_mul_ps(tri
[1].v
[2], vRecipW1
);
1827 tri
[2].v
[2] = _simd_mul_ps(tri
[2].v
[2], vRecipW2
);
1829 // Viewport transform to screen space coords
1830 if (state
.gsState
.emitsViewportArrayIndex
)
1832 viewportTransform
<3>(tri
, state
.vpMatrices
, viewportIdx
);
1836 viewportTransform
<3>(tri
, state
.vpMatrices
);
1840 // Adjust for pixel center location
1841 simdscalar offset
= g_pixelOffsets
[rastState
.pixelLocation
];
1842 tri
[0].x
= _simd_add_ps(tri
[0].x
, offset
);
1843 tri
[0].y
= _simd_add_ps(tri
[0].y
, offset
);
1845 tri
[1].x
= _simd_add_ps(tri
[1].x
, offset
);
1846 tri
[1].y
= _simd_add_ps(tri
[1].y
, offset
);
1848 tri
[2].x
= _simd_add_ps(tri
[2].x
, offset
);
1849 tri
[2].y
= _simd_add_ps(tri
[2].y
, offset
);
1851 simdscalari vXi
[3], vYi
[3];
1852 // Set vXi, vYi to required fixed point precision
1853 FPToFixedPoint(tri
, vXi
, vYi
);
1856 simdscalari vAi
[3], vBi
[3];
1857 triangleSetupABIntVertical(vXi
, vYi
, vAi
, vBi
);
1860 simdscalari vDet
[2];
1861 calcDeterminantIntVertical(vAi
, vBi
, vDet
);
1864 int maskLo
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpeq_epi64(vDet
[0], _simd_setzero_si())));
1865 int maskHi
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpeq_epi64(vDet
[1], _simd_setzero_si())));
1867 int cullZeroAreaMask
= maskLo
| (maskHi
<< (KNOB_SIMD_WIDTH
/ 2));
1869 uint32_t origTriMask
= triMask
;
1870 // don't cull degenerate triangles if we're conservatively rasterizing
1871 if(!CT::IsConservativeT::value
)
1873 triMask
&= ~cullZeroAreaMask
;
1876 // determine front winding tris
1878 // CCW det <= 0; 0 area triangles are marked as backfacing, which is required behavior for conservative rast
1879 maskLo
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpgt_epi64(vDet
[0], _simd_setzero_si())));
1880 maskHi
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpgt_epi64(vDet
[1], _simd_setzero_si())));
1881 int cwTriMask
= maskLo
| (maskHi
<< (KNOB_SIMD_WIDTH
/2) );
1883 uint32_t frontWindingTris
;
1884 if (rastState
.frontWinding
== SWR_FRONTWINDING_CW
)
1886 frontWindingTris
= cwTriMask
;
1890 frontWindingTris
= ~cwTriMask
;
1895 switch ((SWR_CULLMODE
)rastState
.cullMode
)
1897 case SWR_CULLMODE_BOTH
: cullTris
= 0xffffffff; break;
1898 case SWR_CULLMODE_NONE
: cullTris
= 0x0; break;
1899 case SWR_CULLMODE_FRONT
: cullTris
= frontWindingTris
; break;
1900 // 0 area triangles are marked as backfacing, which is required behavior for conservative rast
1901 case SWR_CULLMODE_BACK
: cullTris
= ~frontWindingTris
; break;
1902 default: SWR_ASSERT(false, "Invalid cull mode: %d", rastState
.cullMode
); cullTris
= 0x0; break;
1905 triMask
&= ~cullTris
;
1907 if (origTriMask
^ triMask
)
1909 RDTSC_EVENT(FECullZeroAreaAndBackface
, _mm_popcnt_u32(origTriMask
^ triMask
), 0);
1912 /// Note: these variable initializations must stay above any 'goto endBenTriangles'
1913 // compute per tri backface
1914 uint32_t frontFaceMask
= frontWindingTris
;
1915 uint32_t *pPrimID
= (uint32_t *)&primID
;
1916 const uint32_t *pViewportIndex
= (uint32_t *)&viewportIdx
;
1918 // for center sample pattern, all samples are at pixel center; calculate coverage
1919 // once at center and broadcast the results in the backend
1920 const SWR_MULTISAMPLE_COUNT sampleCount
= (rastState
.samplePattern
== SWR_MSAA_STANDARD_PATTERN
) ? rastState
.sampleCount
: SWR_MULTISAMPLE_1X
;
1921 uint32_t edgeEnable
;
1922 PFN_WORK_FUNC pfnWork
;
1923 if(CT::IsConservativeT::value
)
1925 // determine which edges of the degenerate tri, if any, are valid to rasterize.
1926 // used to call the appropriate templated rasterizer function
1927 if(cullZeroAreaMask
> 0)
1930 simdscalari x0x1Mask
= _simd_cmpeq_epi32(vXi
[0], vXi
[1]);
1931 simdscalari y0y1Mask
= _simd_cmpeq_epi32(vYi
[0], vYi
[1]);
1932 uint32_t e0Mask
= _simd_movemask_ps(_simd_castsi_ps(_simd_and_si(x0x1Mask
, y0y1Mask
)));
1935 simdscalari x1x2Mask
= _simd_cmpeq_epi32(vXi
[1], vXi
[2]);
1936 simdscalari y1y2Mask
= _simd_cmpeq_epi32(vYi
[1], vYi
[2]);
1937 uint32_t e1Mask
= _simd_movemask_ps(_simd_castsi_ps(_simd_and_si(x1x2Mask
, y1y2Mask
)));
1940 // if v0 == v1 & v1 == v2, v0 == v2
1941 uint32_t e2Mask
= e0Mask
& e1Mask
;
1942 SWR_ASSERT(KNOB_SIMD_WIDTH
== 8, "Need to update degenerate mask code for avx512");
1944 // edge order: e0 = v0v1, e1 = v1v2, e2 = v0v2
1945 // 32 bit binary: 0000 0000 0010 0100 1001 0010 0100 1001
1946 e0Mask
= pdep_u32(e0Mask
, 0x00249249);
1947 // 32 bit binary: 0000 0000 0100 1001 0010 0100 1001 0010
1948 e1Mask
= pdep_u32(e1Mask
, 0x00492492);
1949 // 32 bit binary: 0000 0000 1001 0010 0100 1001 0010 0100
1950 e2Mask
= pdep_u32(e2Mask
, 0x00924924);
1952 edgeEnable
= (0x00FFFFFF & (~(e0Mask
| e1Mask
| e2Mask
)));
1956 edgeEnable
= 0x00FFFFFF;
1961 // degenerate triangles won't be sent to rasterizer; just enable all edges
1962 pfnWork
= GetRasterizerFunc(sampleCount
, (rastState
.conservativeRast
> 0),
1963 (SWR_INPUT_COVERAGE
)pDC
->pState
->state
.psState
.inputCoverage
, ALL_EDGES_VALID
,
1964 (rastState
.scissorEnable
> 0));
1969 goto endBinTriangles
;
1972 // Calc bounding box of triangles
1974 calcBoundingBoxIntVertical
<CT
>(tri
, vXi
, vYi
, bbox
);
1976 // determine if triangle falls between pixel centers and discard
1977 // only discard for non-MSAA case and when conservative rast is disabled
1978 // (xmin + 127) & ~255
1979 // (xmax + 128) & ~255
1980 if(rastState
.sampleCount
== SWR_MULTISAMPLE_1X
&& (!CT::IsConservativeT::value
))
1982 origTriMask
= triMask
;
1986 simdscalari xmin
= _simd_add_epi32(bbox
.xmin
, _simd_set1_epi32(127));
1987 xmin
= _simd_and_si(xmin
, _simd_set1_epi32(~255));
1988 simdscalari xmax
= _simd_add_epi32(bbox
.xmax
, _simd_set1_epi32(128));
1989 xmax
= _simd_and_si(xmax
, _simd_set1_epi32(~255));
1991 simdscalari vMaskH
= _simd_cmpeq_epi32(xmin
, xmax
);
1993 simdscalari ymin
= _simd_add_epi32(bbox
.ymin
, _simd_set1_epi32(127));
1994 ymin
= _simd_and_si(ymin
, _simd_set1_epi32(~255));
1995 simdscalari ymax
= _simd_add_epi32(bbox
.ymax
, _simd_set1_epi32(128));
1996 ymax
= _simd_and_si(ymax
, _simd_set1_epi32(~255));
1998 simdscalari vMaskV
= _simd_cmpeq_epi32(ymin
, ymax
);
1999 vMaskV
= _simd_or_si(vMaskH
, vMaskV
);
2000 cullCenterMask
= _simd_movemask_ps(_simd_castsi_ps(vMaskV
));
2003 triMask
&= ~cullCenterMask
;
2005 if(origTriMask
^ triMask
)
2007 RDTSC_EVENT(FECullBetweenCenters
, _mm_popcnt_u32(origTriMask
^ triMask
), 0);
2011 // Intersect with scissor/viewport. Subtract 1 ULP in x.8 fixed point since xmax/ymax edge is exclusive.
2012 // Gather the AOS effective scissor rects based on the per-prim VP index.
2013 /// @todo: Look at speeding this up -- weigh against corresponding costs in rasterizer.
2014 simdscalari scisXmin
, scisYmin
, scisXmax
, scisYmax
;
2015 if (state
.gsState
.emitsViewportArrayIndex
)
2017 GatherScissors
<KNOB_SIMD_WIDTH
>::Gather(&state
.scissorsInFixedPoint
[0], pViewportIndex
,
2018 scisXmin
, scisYmin
, scisXmax
, scisYmax
);
2020 else // broadcast fast path for non-VPAI case.
2022 scisXmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmin
);
2023 scisYmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymin
);
2024 scisXmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmax
);
2025 scisYmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymax
);
2028 bbox
.xmin
= _simd_max_epi32(bbox
.xmin
, scisXmin
);
2029 bbox
.ymin
= _simd_max_epi32(bbox
.ymin
, scisYmin
);
2030 bbox
.xmax
= _simd_min_epi32(_simd_sub_epi32(bbox
.xmax
, _simd_set1_epi32(1)), scisXmax
);
2031 bbox
.ymax
= _simd_min_epi32(_simd_sub_epi32(bbox
.ymax
, _simd_set1_epi32(1)), scisYmax
);
2033 if(CT::IsConservativeT::value
)
2035 // in the case where a degenerate triangle is on a scissor edge, we need to make sure the primitive bbox has
2036 // some area. Bump the xmax/ymax edges out
2037 simdscalari topEqualsBottom
= _simd_cmpeq_epi32(bbox
.ymin
, bbox
.ymax
);
2038 bbox
.ymax
= _simd_blendv_epi32(bbox
.ymax
, _simd_add_epi32(bbox
.ymax
, _simd_set1_epi32(1)), topEqualsBottom
);
2039 simdscalari leftEqualsRight
= _simd_cmpeq_epi32(bbox
.xmin
, bbox
.xmax
);
2040 bbox
.xmax
= _simd_blendv_epi32(bbox
.xmax
, _simd_add_epi32(bbox
.xmax
, _simd_set1_epi32(1)), leftEqualsRight
);
2043 // Cull tris completely outside scissor
2045 simdscalari maskOutsideScissorX
= _simd_cmpgt_epi32(bbox
.xmin
, bbox
.xmax
);
2046 simdscalari maskOutsideScissorY
= _simd_cmpgt_epi32(bbox
.ymin
, bbox
.ymax
);
2047 simdscalari maskOutsideScissorXY
= _simd_or_si(maskOutsideScissorX
, maskOutsideScissorY
);
2048 uint32_t maskOutsideScissor
= _simd_movemask_ps(_simd_castsi_ps(maskOutsideScissorXY
));
2049 triMask
= triMask
& ~maskOutsideScissor
;
2054 goto endBinTriangles
;
2057 // Convert triangle bbox to macrotile units.
2058 bbox
.xmin
= _simd_srai_epi32(bbox
.xmin
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2059 bbox
.ymin
= _simd_srai_epi32(bbox
.ymin
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2060 bbox
.xmax
= _simd_srai_epi32(bbox
.xmax
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2061 bbox
.ymax
= _simd_srai_epi32(bbox
.ymax
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2063 OSALIGNSIMD(uint32_t) aMTLeft
[KNOB_SIMD_WIDTH
], aMTRight
[KNOB_SIMD_WIDTH
], aMTTop
[KNOB_SIMD_WIDTH
], aMTBottom
[KNOB_SIMD_WIDTH
];
2064 _simd_store_si((simdscalari
*)aMTLeft
, bbox
.xmin
);
2065 _simd_store_si((simdscalari
*)aMTRight
, bbox
.xmax
);
2066 _simd_store_si((simdscalari
*)aMTTop
, bbox
.ymin
);
2067 _simd_store_si((simdscalari
*)aMTBottom
, bbox
.ymax
);
2069 // transpose verts needed for backend
2070 /// @todo modify BE to take non-transformed verts
2071 __m128 vHorizX
[8], vHorizY
[8], vHorizZ
[8], vHorizW
[8];
2072 vTranspose3x8(vHorizX
, tri
[0].x
, tri
[1].x
, tri
[2].x
);
2073 vTranspose3x8(vHorizY
, tri
[0].y
, tri
[1].y
, tri
[2].y
);
2074 vTranspose3x8(vHorizZ
, tri
[0].z
, tri
[1].z
, tri
[2].z
);
2075 vTranspose3x8(vHorizW
, vRecipW0
, vRecipW1
, vRecipW2
);
2077 // store render target array index
2078 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2079 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2081 simdvector vRtai
[3];
2082 pa
.Assemble(VERTEX_RTAI_SLOT
, vRtai
);
2084 vRtaii
= _simd_castps_si(vRtai
[0].x
);
2085 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2089 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2092 // scan remaining valid triangles and bin each separately
2093 while (_BitScanForward(&triIndex
, triMask
))
2095 uint32_t linkageCount
= state
.backendState
.numAttributes
;
2096 uint32_t numScalarAttribs
= linkageCount
* 4;
2102 if(CT::IsConservativeT::value
)
2104 // only rasterize valid edges if we have a degenerate primitive
2105 int32_t triEdgeEnable
= (edgeEnable
>> (triIndex
* 3)) & ALL_EDGES_VALID
;
2106 work
.pfnWork
= GetRasterizerFunc(sampleCount
, (rastState
.conservativeRast
> 0),
2107 (SWR_INPUT_COVERAGE
)pDC
->pState
->state
.psState
.inputCoverage
, triEdgeEnable
,
2108 (rastState
.scissorEnable
> 0));
2110 // Degenerate triangles are required to be constant interpolated
2111 isDegenerate
= (triEdgeEnable
!= ALL_EDGES_VALID
) ? true : false;
2115 isDegenerate
= false;
2116 work
.pfnWork
= pfnWork
;
2119 // Select attribute processor
2120 PFN_PROCESS_ATTRIBUTES pfnProcessAttribs
= GetProcessAttributesFunc(3,
2121 state
.backendState
.swizzleEnable
, state
.backendState
.constantInterpolationMask
, isDegenerate
);
2123 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2125 desc
.triFlags
.frontFacing
= state
.forceFront
? 1 : ((frontFaceMask
>> triIndex
) & 1);
2126 desc
.triFlags
.primID
= pPrimID
[triIndex
];
2127 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[triIndex
];
2128 desc
.triFlags
.viewportIndex
= pViewportIndex
[triIndex
];
2130 auto pArena
= pDC
->pArena
;
2131 SWR_ASSERT(pArena
!= nullptr);
2133 // store active attribs
2134 float *pAttribs
= (float*)pArena
->AllocAligned(numScalarAttribs
* 3 * sizeof(float), 16);
2135 desc
.pAttribs
= pAttribs
;
2136 desc
.numAttribs
= linkageCount
;
2137 pfnProcessAttribs(pDC
, pa
, triIndex
, pPrimID
[triIndex
], desc
.pAttribs
);
2139 // store triangle vertex data
2140 desc
.pTriBuffer
= (float*)pArena
->AllocAligned(4 * 4 * sizeof(float), 16);
2142 _mm_store_ps(&desc
.pTriBuffer
[0], vHorizX
[triIndex
]);
2143 _mm_store_ps(&desc
.pTriBuffer
[4], vHorizY
[triIndex
]);
2144 _mm_store_ps(&desc
.pTriBuffer
[8], vHorizZ
[triIndex
]);
2145 _mm_store_ps(&desc
.pTriBuffer
[12], vHorizW
[triIndex
]);
2147 // store user clip distances
2148 if (rastState
.clipDistanceMask
)
2150 uint32_t numClipDist
= _mm_popcnt_u32(rastState
.clipDistanceMask
);
2151 desc
.pUserClipBuffer
= (float*)pArena
->Alloc(numClipDist
* 3 * sizeof(float));
2152 ProcessUserClipDist
<3>(pa
, triIndex
, rastState
.clipDistanceMask
, desc
.pUserClipBuffer
);
2155 for (uint32_t y
= aMTTop
[triIndex
]; y
<= aMTBottom
[triIndex
]; ++y
)
2157 for (uint32_t x
= aMTLeft
[triIndex
]; x
<= aMTRight
[triIndex
]; ++x
)
2159 #if KNOB_ENABLE_TOSS_POINTS
2160 if (!KNOB_TOSS_SETUP_TRIS
)
2163 pTileMgr
->enqueue(x
, y
, &work
);
2167 triMask
&= ~(1 << triIndex
);
2171 RDTSC_STOP(FEBinTriangles
, 1, 0);
2174 struct FEBinTrianglesChooser
2176 typedef PFN_PROCESS_PRIMS FuncType
;
2178 template <typename
... ArgsB
>
2179 static FuncType
GetFunc()
2181 return BinTriangles
<ConservativeRastFETraits
<ArgsB
...>>;
2185 // Selector for correct templated BinTrinagles function
2186 PFN_PROCESS_PRIMS
GetBinTrianglesFunc(bool IsConservative
)
2188 return TemplateArgUnroller
<FEBinTrianglesChooser
>::GetFunc(IsConservative
);
2191 //////////////////////////////////////////////////////////////////////////
2192 /// @brief Bin SIMD points to the backend. Only supports point size of 1
2193 /// @param pDC - pointer to draw context.
2194 /// @param pa - The primitive assembly object.
2195 /// @param workerId - thread's worker id. Even thread has a unique id.
2196 /// @param tri - Contains point position data for SIMDs worth of points.
2197 /// @param primID - Primitive ID for each point.
2205 simdscalari viewportIdx
)
2207 RDTSC_START(FEBinPoints
);
2209 simdvector
& primVerts
= prim
[0];
2211 const API_STATE
& state
= GetApiState(pDC
);
2212 const SWR_FRONTEND_STATE
& feState
= state
.frontendState
;
2213 const SWR_GS_STATE
& gsState
= state
.gsState
;
2214 const SWR_RASTSTATE
& rastState
= state
.rastState
;
2215 const uint32_t *pViewportIndex
= (uint32_t *)&viewportIdx
;
2217 // Select attribute processor
2218 PFN_PROCESS_ATTRIBUTES pfnProcessAttribs
= GetProcessAttributesFunc(1,
2219 state
.backendState
.swizzleEnable
, state
.backendState
.constantInterpolationMask
);
2221 if (!feState
.vpTransformDisable
)
2223 // perspective divide
2224 simdscalar vRecipW0
= _simd_div_ps(_simd_set1_ps(1.0f
), primVerts
.w
);
2225 primVerts
.x
= _simd_mul_ps(primVerts
.x
, vRecipW0
);
2226 primVerts
.y
= _simd_mul_ps(primVerts
.y
, vRecipW0
);
2227 primVerts
.z
= _simd_mul_ps(primVerts
.z
, vRecipW0
);
2229 // viewport transform to screen coords
2230 if (state
.gsState
.emitsViewportArrayIndex
)
2232 viewportTransform
<1>(&primVerts
, state
.vpMatrices
, viewportIdx
);
2236 viewportTransform
<1>(&primVerts
, state
.vpMatrices
);
2240 // adjust for pixel center location
2241 simdscalar offset
= g_pixelOffsets
[rastState
.pixelLocation
];
2242 primVerts
.x
= _simd_add_ps(primVerts
.x
, offset
);
2243 primVerts
.y
= _simd_add_ps(primVerts
.y
, offset
);
2245 // convert to fixed point
2246 simdscalari vXi
, vYi
;
2247 vXi
= fpToFixedPointVertical(primVerts
.x
);
2248 vYi
= fpToFixedPointVertical(primVerts
.y
);
2250 if (CanUseSimplePoints(pDC
))
2252 // adjust for ymin-xmin rule
2253 vXi
= _simd_sub_epi32(vXi
, _simd_set1_epi32(1));
2254 vYi
= _simd_sub_epi32(vYi
, _simd_set1_epi32(1));
2256 // cull points off the ymin-xmin edge of the viewport
2257 primMask
&= ~_simd_movemask_ps(_simd_castsi_ps(vXi
));
2258 primMask
&= ~_simd_movemask_ps(_simd_castsi_ps(vYi
));
2260 // compute macro tile coordinates
2261 simdscalari macroX
= _simd_srai_epi32(vXi
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2262 simdscalari macroY
= _simd_srai_epi32(vYi
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2264 OSALIGNSIMD(uint32_t) aMacroX
[KNOB_SIMD_WIDTH
], aMacroY
[KNOB_SIMD_WIDTH
];
2265 _simd_store_si((simdscalari
*)aMacroX
, macroX
);
2266 _simd_store_si((simdscalari
*)aMacroY
, macroY
);
2268 // compute raster tile coordinates
2269 simdscalari rasterX
= _simd_srai_epi32(vXi
, KNOB_TILE_X_DIM_SHIFT
+ FIXED_POINT_SHIFT
);
2270 simdscalari rasterY
= _simd_srai_epi32(vYi
, KNOB_TILE_Y_DIM_SHIFT
+ FIXED_POINT_SHIFT
);
2272 // compute raster tile relative x,y for coverage mask
2273 simdscalari tileAlignedX
= _simd_slli_epi32(rasterX
, KNOB_TILE_X_DIM_SHIFT
);
2274 simdscalari tileAlignedY
= _simd_slli_epi32(rasterY
, KNOB_TILE_Y_DIM_SHIFT
);
2276 simdscalari tileRelativeX
= _simd_sub_epi32(_simd_srai_epi32(vXi
, FIXED_POINT_SHIFT
), tileAlignedX
);
2277 simdscalari tileRelativeY
= _simd_sub_epi32(_simd_srai_epi32(vYi
, FIXED_POINT_SHIFT
), tileAlignedY
);
2279 OSALIGNSIMD(uint32_t) aTileRelativeX
[KNOB_SIMD_WIDTH
];
2280 OSALIGNSIMD(uint32_t) aTileRelativeY
[KNOB_SIMD_WIDTH
];
2281 _simd_store_si((simdscalari
*)aTileRelativeX
, tileRelativeX
);
2282 _simd_store_si((simdscalari
*)aTileRelativeY
, tileRelativeY
);
2284 OSALIGNSIMD(uint32_t) aTileAlignedX
[KNOB_SIMD_WIDTH
];
2285 OSALIGNSIMD(uint32_t) aTileAlignedY
[KNOB_SIMD_WIDTH
];
2286 _simd_store_si((simdscalari
*)aTileAlignedX
, tileAlignedX
);
2287 _simd_store_si((simdscalari
*)aTileAlignedY
, tileAlignedY
);
2289 OSALIGNSIMD(float) aZ
[KNOB_SIMD_WIDTH
];
2290 _simd_store_ps((float*)aZ
, primVerts
.z
);
2292 // store render target array index
2293 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2294 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2297 pa
.Assemble(VERTEX_RTAI_SLOT
, &vRtai
);
2298 simdscalari vRtaii
= _simd_castps_si(vRtai
.x
);
2299 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2303 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2306 uint32_t *pPrimID
= (uint32_t *)&primID
;
2307 DWORD primIndex
= 0;
2309 const SWR_BACKEND_STATE
& backendState
= pDC
->pState
->state
.backendState
;
2311 // scan remaining valid triangles and bin each separately
2312 while (_BitScanForward(&primIndex
, primMask
))
2314 uint32_t linkageCount
= backendState
.numAttributes
;
2315 uint32_t numScalarAttribs
= linkageCount
* 4;
2320 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2322 // points are always front facing
2323 desc
.triFlags
.frontFacing
= 1;
2324 desc
.triFlags
.primID
= pPrimID
[primIndex
];
2325 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[primIndex
];
2326 desc
.triFlags
.viewportIndex
= pViewportIndex
[primIndex
];
2328 work
.pfnWork
= RasterizeSimplePoint
;
2330 auto pArena
= pDC
->pArena
;
2331 SWR_ASSERT(pArena
!= nullptr);
2334 float *pAttribs
= (float*)pArena
->AllocAligned(3 * numScalarAttribs
* sizeof(float), 16);
2335 desc
.pAttribs
= pAttribs
;
2336 desc
.numAttribs
= linkageCount
;
2338 pfnProcessAttribs(pDC
, pa
, primIndex
, pPrimID
[primIndex
], pAttribs
);
2340 // store raster tile aligned x, y, perspective correct z
2341 float *pTriBuffer
= (float*)pArena
->AllocAligned(4 * sizeof(float), 16);
2342 desc
.pTriBuffer
= pTriBuffer
;
2343 *(uint32_t*)pTriBuffer
++ = aTileAlignedX
[primIndex
];
2344 *(uint32_t*)pTriBuffer
++ = aTileAlignedY
[primIndex
];
2345 *pTriBuffer
= aZ
[primIndex
];
2347 uint32_t tX
= aTileRelativeX
[primIndex
];
2348 uint32_t tY
= aTileRelativeY
[primIndex
];
2350 // pack the relative x,y into the coverageMask, the rasterizer will
2351 // generate the true coverage mask from it
2352 work
.desc
.tri
.triFlags
.coverageMask
= tX
| (tY
<< 4);
2355 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
2356 #if KNOB_ENABLE_TOSS_POINTS
2357 if (!KNOB_TOSS_SETUP_TRIS
)
2360 pTileMgr
->enqueue(aMacroX
[primIndex
], aMacroY
[primIndex
], &work
);
2362 primMask
&= ~(1 << primIndex
);
2367 // non simple points need to be potentially binned to multiple macro tiles
2368 simdscalar vPointSize
;
2369 if (rastState
.pointParam
)
2372 pa
.Assemble(VERTEX_POINT_SIZE_SLOT
, size
);
2373 vPointSize
= size
[0].x
;
2377 vPointSize
= _simd_set1_ps(rastState
.pointSize
);
2380 // bloat point to bbox
2382 bbox
.xmin
= bbox
.xmax
= vXi
;
2383 bbox
.ymin
= bbox
.ymax
= vYi
;
2385 simdscalar vHalfWidth
= _simd_mul_ps(vPointSize
, _simd_set1_ps(0.5f
));
2386 simdscalari vHalfWidthi
= fpToFixedPointVertical(vHalfWidth
);
2387 bbox
.xmin
= _simd_sub_epi32(bbox
.xmin
, vHalfWidthi
);
2388 bbox
.xmax
= _simd_add_epi32(bbox
.xmax
, vHalfWidthi
);
2389 bbox
.ymin
= _simd_sub_epi32(bbox
.ymin
, vHalfWidthi
);
2390 bbox
.ymax
= _simd_add_epi32(bbox
.ymax
, vHalfWidthi
);
2392 // Intersect with scissor/viewport. Subtract 1 ULP in x.8 fixed point since xmax/ymax edge is exclusive.
2393 // Gather the AOS effective scissor rects based on the per-prim VP index.
2394 /// @todo: Look at speeding this up -- weigh against corresponding costs in rasterizer.
2395 simdscalari scisXmin
, scisYmin
, scisXmax
, scisYmax
;
2396 if (state
.gsState
.emitsViewportArrayIndex
)
2398 GatherScissors
<KNOB_SIMD_WIDTH
>::Gather(&state
.scissorsInFixedPoint
[0], pViewportIndex
,
2399 scisXmin
, scisYmin
, scisXmax
, scisYmax
);
2401 else // broadcast fast path for non-VPAI case.
2403 scisXmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmin
);
2404 scisYmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymin
);
2405 scisXmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmax
);
2406 scisYmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymax
);
2409 bbox
.xmin
= _simd_max_epi32(bbox
.xmin
, scisXmin
);
2410 bbox
.ymin
= _simd_max_epi32(bbox
.ymin
, scisYmin
);
2411 bbox
.xmax
= _simd_min_epi32(_simd_sub_epi32(bbox
.xmax
, _simd_set1_epi32(1)), scisXmax
);
2412 bbox
.ymax
= _simd_min_epi32(_simd_sub_epi32(bbox
.ymax
, _simd_set1_epi32(1)), scisYmax
);
2414 // Cull bloated points completely outside scissor
2415 simdscalari maskOutsideScissorX
= _simd_cmpgt_epi32(bbox
.xmin
, bbox
.xmax
);
2416 simdscalari maskOutsideScissorY
= _simd_cmpgt_epi32(bbox
.ymin
, bbox
.ymax
);
2417 simdscalari maskOutsideScissorXY
= _simd_or_si(maskOutsideScissorX
, maskOutsideScissorY
);
2418 uint32_t maskOutsideScissor
= _simd_movemask_ps(_simd_castsi_ps(maskOutsideScissorXY
));
2419 primMask
= primMask
& ~maskOutsideScissor
;
2421 // Convert bbox to macrotile units.
2422 bbox
.xmin
= _simd_srai_epi32(bbox
.xmin
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2423 bbox
.ymin
= _simd_srai_epi32(bbox
.ymin
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2424 bbox
.xmax
= _simd_srai_epi32(bbox
.xmax
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2425 bbox
.ymax
= _simd_srai_epi32(bbox
.ymax
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2427 OSALIGNSIMD(uint32_t) aMTLeft
[KNOB_SIMD_WIDTH
], aMTRight
[KNOB_SIMD_WIDTH
], aMTTop
[KNOB_SIMD_WIDTH
], aMTBottom
[KNOB_SIMD_WIDTH
];
2428 _simd_store_si((simdscalari
*)aMTLeft
, bbox
.xmin
);
2429 _simd_store_si((simdscalari
*)aMTRight
, bbox
.xmax
);
2430 _simd_store_si((simdscalari
*)aMTTop
, bbox
.ymin
);
2431 _simd_store_si((simdscalari
*)aMTBottom
, bbox
.ymax
);
2433 // store render target array index
2434 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2435 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2437 simdvector vRtai
[2];
2438 pa
.Assemble(VERTEX_RTAI_SLOT
, vRtai
);
2439 simdscalari vRtaii
= _simd_castps_si(vRtai
[0].x
);
2440 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2444 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2447 OSALIGNSIMD(float) aPointSize
[KNOB_SIMD_WIDTH
];
2448 _simd_store_ps((float*)aPointSize
, vPointSize
);
2450 uint32_t *pPrimID
= (uint32_t *)&primID
;
2452 OSALIGNSIMD(float) aPrimVertsX
[KNOB_SIMD_WIDTH
];
2453 OSALIGNSIMD(float) aPrimVertsY
[KNOB_SIMD_WIDTH
];
2454 OSALIGNSIMD(float) aPrimVertsZ
[KNOB_SIMD_WIDTH
];
2456 _simd_store_ps((float*)aPrimVertsX
, primVerts
.x
);
2457 _simd_store_ps((float*)aPrimVertsY
, primVerts
.y
);
2458 _simd_store_ps((float*)aPrimVertsZ
, primVerts
.z
);
2460 // scan remaining valid prims and bin each separately
2461 const SWR_BACKEND_STATE
& backendState
= state
.backendState
;
2463 while (_BitScanForward(&primIndex
, primMask
))
2465 uint32_t linkageCount
= backendState
.numAttributes
;
2466 uint32_t numScalarAttribs
= linkageCount
* 4;
2471 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2473 desc
.triFlags
.frontFacing
= 1;
2474 desc
.triFlags
.primID
= pPrimID
[primIndex
];
2475 desc
.triFlags
.pointSize
= aPointSize
[primIndex
];
2476 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[primIndex
];
2477 desc
.triFlags
.viewportIndex
= pViewportIndex
[primIndex
];
2479 work
.pfnWork
= RasterizeTriPoint
;
2481 auto pArena
= pDC
->pArena
;
2482 SWR_ASSERT(pArena
!= nullptr);
2484 // store active attribs
2485 desc
.pAttribs
= (float*)pArena
->AllocAligned(numScalarAttribs
* 3 * sizeof(float), 16);
2486 desc
.numAttribs
= linkageCount
;
2487 pfnProcessAttribs(pDC
, pa
, primIndex
, pPrimID
[primIndex
], desc
.pAttribs
);
2489 // store point vertex data
2490 float *pTriBuffer
= (float*)pArena
->AllocAligned(4 * sizeof(float), 16);
2491 desc
.pTriBuffer
= pTriBuffer
;
2492 *pTriBuffer
++ = aPrimVertsX
[primIndex
];
2493 *pTriBuffer
++ = aPrimVertsY
[primIndex
];
2494 *pTriBuffer
= aPrimVertsZ
[primIndex
];
2496 // store user clip distances
2497 if (rastState
.clipDistanceMask
)
2499 uint32_t numClipDist
= _mm_popcnt_u32(rastState
.clipDistanceMask
);
2500 desc
.pUserClipBuffer
= (float*)pArena
->Alloc(numClipDist
* 2 * sizeof(float));
2501 ProcessUserClipDist
<2>(pa
, primIndex
, rastState
.clipDistanceMask
, desc
.pUserClipBuffer
);
2504 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
2505 for (uint32_t y
= aMTTop
[primIndex
]; y
<= aMTBottom
[primIndex
]; ++y
)
2507 for (uint32_t x
= aMTLeft
[primIndex
]; x
<= aMTRight
[primIndex
]; ++x
)
2509 #if KNOB_ENABLE_TOSS_POINTS
2510 if (!KNOB_TOSS_SETUP_TRIS
)
2513 pTileMgr
->enqueue(x
, y
, &work
);
2518 primMask
&= ~(1 << primIndex
);
2525 RDTSC_STOP(FEBinPoints
, 1, 0);
2528 //////////////////////////////////////////////////////////////////////////
2529 /// @brief Bin SIMD lines to the backend.
2530 /// @param pDC - pointer to draw context.
2531 /// @param pa - The primitive assembly object.
2532 /// @param workerId - thread's worker id. Even thread has a unique id.
2533 /// @param tri - Contains line position data for SIMDs worth of points.
2534 /// @param primID - Primitive ID for each line.
2535 /// @param viewportIdx - Viewport Array Index for each line.
2543 simdscalari viewportIdx
)
2545 RDTSC_START(FEBinLines
);
2547 const API_STATE
& state
= GetApiState(pDC
);
2548 const SWR_RASTSTATE
& rastState
= state
.rastState
;
2549 const SWR_FRONTEND_STATE
& feState
= state
.frontendState
;
2550 const SWR_GS_STATE
& gsState
= state
.gsState
;
2552 // Select attribute processor
2553 PFN_PROCESS_ATTRIBUTES pfnProcessAttribs
= GetProcessAttributesFunc(2,
2554 state
.backendState
.swizzleEnable
, state
.backendState
.constantInterpolationMask
);
2556 simdscalar vRecipW0
= _simd_set1_ps(1.0f
);
2557 simdscalar vRecipW1
= _simd_set1_ps(1.0f
);
2559 if (!feState
.vpTransformDisable
)
2561 // perspective divide
2562 vRecipW0
= _simd_div_ps(_simd_set1_ps(1.0f
), prim
[0].w
);
2563 vRecipW1
= _simd_div_ps(_simd_set1_ps(1.0f
), prim
[1].w
);
2565 prim
[0].v
[0] = _simd_mul_ps(prim
[0].v
[0], vRecipW0
);
2566 prim
[1].v
[0] = _simd_mul_ps(prim
[1].v
[0], vRecipW1
);
2568 prim
[0].v
[1] = _simd_mul_ps(prim
[0].v
[1], vRecipW0
);
2569 prim
[1].v
[1] = _simd_mul_ps(prim
[1].v
[1], vRecipW1
);
2571 prim
[0].v
[2] = _simd_mul_ps(prim
[0].v
[2], vRecipW0
);
2572 prim
[1].v
[2] = _simd_mul_ps(prim
[1].v
[2], vRecipW1
);
2574 // viewport transform to screen coords
2575 if (state
.gsState
.emitsViewportArrayIndex
)
2577 viewportTransform
<2>(prim
, state
.vpMatrices
, viewportIdx
);
2581 viewportTransform
<2>(prim
, state
.vpMatrices
);
2585 // adjust for pixel center location
2586 simdscalar offset
= g_pixelOffsets
[rastState
.pixelLocation
];
2587 prim
[0].x
= _simd_add_ps(prim
[0].x
, offset
);
2588 prim
[0].y
= _simd_add_ps(prim
[0].y
, offset
);
2590 prim
[1].x
= _simd_add_ps(prim
[1].x
, offset
);
2591 prim
[1].y
= _simd_add_ps(prim
[1].y
, offset
);
2593 // convert to fixed point
2594 simdscalari vXi
[2], vYi
[2];
2595 vXi
[0] = fpToFixedPointVertical(prim
[0].x
);
2596 vYi
[0] = fpToFixedPointVertical(prim
[0].y
);
2597 vXi
[1] = fpToFixedPointVertical(prim
[1].x
);
2598 vYi
[1] = fpToFixedPointVertical(prim
[1].y
);
2600 // compute x-major vs y-major mask
2601 simdscalari xLength
= _simd_abs_epi32(_simd_sub_epi32(vXi
[0], vXi
[1]));
2602 simdscalari yLength
= _simd_abs_epi32(_simd_sub_epi32(vYi
[0], vYi
[1]));
2603 simdscalar vYmajorMask
= _simd_castsi_ps(_simd_cmpgt_epi32(yLength
, xLength
));
2604 uint32_t yMajorMask
= _simd_movemask_ps(vYmajorMask
);
2606 // cull zero-length lines
2607 simdscalari vZeroLengthMask
= _simd_cmpeq_epi32(xLength
, _simd_setzero_si());
2608 vZeroLengthMask
= _simd_and_si(vZeroLengthMask
, _simd_cmpeq_epi32(yLength
, _simd_setzero_si()));
2610 primMask
&= ~_simd_movemask_ps(_simd_castsi_ps(vZeroLengthMask
));
2612 uint32_t *pPrimID
= (uint32_t *)&primID
;
2613 const uint32_t *pViewportIndex
= (uint32_t *)&viewportIdx
;
2615 simdscalar vUnused
= _simd_setzero_ps();
2617 // Calc bounding box of lines
2619 bbox
.xmin
= _simd_min_epi32(vXi
[0], vXi
[1]);
2620 bbox
.xmax
= _simd_max_epi32(vXi
[0], vXi
[1]);
2621 bbox
.ymin
= _simd_min_epi32(vYi
[0], vYi
[1]);
2622 bbox
.ymax
= _simd_max_epi32(vYi
[0], vYi
[1]);
2624 // bloat bbox by line width along minor axis
2625 simdscalar vHalfWidth
= _simd_set1_ps(rastState
.lineWidth
/ 2.0f
);
2626 simdscalari vHalfWidthi
= fpToFixedPointVertical(vHalfWidth
);
2628 bloatBox
.xmin
= _simd_sub_epi32(bbox
.xmin
, vHalfWidthi
);
2629 bloatBox
.xmax
= _simd_add_epi32(bbox
.xmax
, vHalfWidthi
);
2630 bloatBox
.ymin
= _simd_sub_epi32(bbox
.ymin
, vHalfWidthi
);
2631 bloatBox
.ymax
= _simd_add_epi32(bbox
.ymax
, vHalfWidthi
);
2633 bbox
.xmin
= _simd_blendv_epi32(bbox
.xmin
, bloatBox
.xmin
, vYmajorMask
);
2634 bbox
.xmax
= _simd_blendv_epi32(bbox
.xmax
, bloatBox
.xmax
, vYmajorMask
);
2635 bbox
.ymin
= _simd_blendv_epi32(bloatBox
.ymin
, bbox
.ymin
, vYmajorMask
);
2636 bbox
.ymax
= _simd_blendv_epi32(bloatBox
.ymax
, bbox
.ymax
, vYmajorMask
);
2638 // Intersect with scissor/viewport. Subtract 1 ULP in x.8 fixed point since xmax/ymax edge is exclusive.
2639 simdscalari scisXmin
, scisYmin
, scisXmax
, scisYmax
;
2640 if (state
.gsState
.emitsViewportArrayIndex
)
2642 GatherScissors
<KNOB_SIMD_WIDTH
>::Gather(&state
.scissorsInFixedPoint
[0], pViewportIndex
,
2643 scisXmin
, scisYmin
, scisXmax
, scisYmax
);
2645 else // broadcast fast path for non-VPAI case.
2647 scisXmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmin
);
2648 scisYmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymin
);
2649 scisXmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmax
);
2650 scisYmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymax
);
2653 bbox
.xmin
= _simd_max_epi32(bbox
.xmin
, scisXmin
);
2654 bbox
.ymin
= _simd_max_epi32(bbox
.ymin
, scisYmin
);
2655 bbox
.xmax
= _simd_min_epi32(_simd_sub_epi32(bbox
.xmax
, _simd_set1_epi32(1)), scisXmax
);
2656 bbox
.ymax
= _simd_min_epi32(_simd_sub_epi32(bbox
.ymax
, _simd_set1_epi32(1)), scisYmax
);
2658 // Cull prims completely outside scissor
2660 simdscalari maskOutsideScissorX
= _simd_cmpgt_epi32(bbox
.xmin
, bbox
.xmax
);
2661 simdscalari maskOutsideScissorY
= _simd_cmpgt_epi32(bbox
.ymin
, bbox
.ymax
);
2662 simdscalari maskOutsideScissorXY
= _simd_or_si(maskOutsideScissorX
, maskOutsideScissorY
);
2663 uint32_t maskOutsideScissor
= _simd_movemask_ps(_simd_castsi_ps(maskOutsideScissorXY
));
2664 primMask
= primMask
& ~maskOutsideScissor
;
2672 // Convert triangle bbox to macrotile units.
2673 bbox
.xmin
= _simd_srai_epi32(bbox
.xmin
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2674 bbox
.ymin
= _simd_srai_epi32(bbox
.ymin
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2675 bbox
.xmax
= _simd_srai_epi32(bbox
.xmax
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2676 bbox
.ymax
= _simd_srai_epi32(bbox
.ymax
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2678 OSALIGNSIMD(uint32_t) aMTLeft
[KNOB_SIMD_WIDTH
], aMTRight
[KNOB_SIMD_WIDTH
], aMTTop
[KNOB_SIMD_WIDTH
], aMTBottom
[KNOB_SIMD_WIDTH
];
2679 _simd_store_si((simdscalari
*)aMTLeft
, bbox
.xmin
);
2680 _simd_store_si((simdscalari
*)aMTRight
, bbox
.xmax
);
2681 _simd_store_si((simdscalari
*)aMTTop
, bbox
.ymin
);
2682 _simd_store_si((simdscalari
*)aMTBottom
, bbox
.ymax
);
2684 // transpose verts needed for backend
2685 /// @todo modify BE to take non-transformed verts
2686 __m128 vHorizX
[8], vHorizY
[8], vHorizZ
[8], vHorizW
[8];
2687 vTranspose3x8(vHorizX
, prim
[0].x
, prim
[1].x
, vUnused
);
2688 vTranspose3x8(vHorizY
, prim
[0].y
, prim
[1].y
, vUnused
);
2689 vTranspose3x8(vHorizZ
, prim
[0].z
, prim
[1].z
, vUnused
);
2690 vTranspose3x8(vHorizW
, vRecipW0
, vRecipW1
, vUnused
);
2692 // store render target array index
2693 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2694 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2696 simdvector vRtai
[2];
2697 pa
.Assemble(VERTEX_RTAI_SLOT
, vRtai
);
2698 simdscalari vRtaii
= _simd_castps_si(vRtai
[0].x
);
2699 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2703 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2706 // scan remaining valid prims and bin each separately
2708 while (_BitScanForward(&primIndex
, primMask
))
2710 uint32_t linkageCount
= state
.backendState
.numAttributes
;
2711 uint32_t numScalarAttribs
= linkageCount
* 4;
2716 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2718 desc
.triFlags
.frontFacing
= 1;
2719 desc
.triFlags
.primID
= pPrimID
[primIndex
];
2720 desc
.triFlags
.yMajor
= (yMajorMask
>> primIndex
) & 1;
2721 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[primIndex
];
2722 desc
.triFlags
.viewportIndex
= pViewportIndex
[primIndex
];
2724 work
.pfnWork
= RasterizeLine
;
2726 auto pArena
= pDC
->pArena
;
2727 SWR_ASSERT(pArena
!= nullptr);
2729 // store active attribs
2730 desc
.pAttribs
= (float*)pArena
->AllocAligned(numScalarAttribs
* 3 * sizeof(float), 16);
2731 desc
.numAttribs
= linkageCount
;
2732 pfnProcessAttribs(pDC
, pa
, primIndex
, pPrimID
[primIndex
], desc
.pAttribs
);
2734 // store line vertex data
2735 desc
.pTriBuffer
= (float*)pArena
->AllocAligned(4 * 4 * sizeof(float), 16);
2736 _mm_store_ps(&desc
.pTriBuffer
[0], vHorizX
[primIndex
]);
2737 _mm_store_ps(&desc
.pTriBuffer
[4], vHorizY
[primIndex
]);
2738 _mm_store_ps(&desc
.pTriBuffer
[8], vHorizZ
[primIndex
]);
2739 _mm_store_ps(&desc
.pTriBuffer
[12], vHorizW
[primIndex
]);
2741 // store user clip distances
2742 if (rastState
.clipDistanceMask
)
2744 uint32_t numClipDist
= _mm_popcnt_u32(rastState
.clipDistanceMask
);
2745 desc
.pUserClipBuffer
= (float*)pArena
->Alloc(numClipDist
* 2 * sizeof(float));
2746 ProcessUserClipDist
<2>(pa
, primIndex
, rastState
.clipDistanceMask
, desc
.pUserClipBuffer
);
2749 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
2750 for (uint32_t y
= aMTTop
[primIndex
]; y
<= aMTBottom
[primIndex
]; ++y
)
2752 for (uint32_t x
= aMTLeft
[primIndex
]; x
<= aMTRight
[primIndex
]; ++x
)
2754 #if KNOB_ENABLE_TOSS_POINTS
2755 if (!KNOB_TOSS_SETUP_TRIS
)
2758 pTileMgr
->enqueue(x
, y
, &work
);
2763 primMask
&= ~(1 << primIndex
);
2768 RDTSC_STOP(FEBinLines
, 1, 0);