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 SwrDestroyContext.
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 sync callback.
90 SWR_CONTEXT
*pContext
,
97 work
.pfnWork
= ProcessShutdownBE
;
99 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
100 // Enqueue at least 1 work item for each worker thread
101 // account for number of numa nodes
102 uint32_t numNumaNodes
= pContext
->threadPool
.numaMask
+ 1;
104 for (uint32_t i
= 0; i
< pContext
->threadPool
.numThreads
; ++i
)
106 for (uint32_t n
= 0; n
< numNumaNodes
; ++n
)
108 pTileMgr
->enqueue(i
, n
, &work
);
113 //////////////////////////////////////////////////////////////////////////
114 /// @brief FE handler for SwrClearRenderTarget.
115 /// @param pContext - pointer to SWR context.
116 /// @param pDC - pointer to draw context.
117 /// @param workerId - thread's worker id. Even thread has a unique id.
118 /// @param pUserData - Pointer to user data passed back to clear callback.
119 /// @todo This should go away when we switch this to use compute threading.
121 SWR_CONTEXT
*pContext
,
126 CLEAR_DESC
*pDesc
= (CLEAR_DESC
*)pUserData
;
127 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
129 // queue a clear to each macro tile
130 // compute macro tile bounds for the specified rect
131 uint32_t macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
132 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
133 uint32_t macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
134 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
138 work
.pfnWork
= ProcessClearBE
;
139 work
.desc
.clear
= *pDesc
;
141 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
143 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
145 pTileMgr
->enqueue(x
, y
, &work
);
150 //////////////////////////////////////////////////////////////////////////
151 /// @brief FE handler for SwrStoreTiles.
152 /// @param pContext - pointer to SWR context.
153 /// @param pDC - pointer to draw context.
154 /// @param workerId - thread's worker id. Even thread has a unique id.
155 /// @param pUserData - Pointer to user data passed back to callback.
156 /// @todo This should go away when we switch this to use compute threading.
157 void ProcessStoreTiles(
158 SWR_CONTEXT
*pContext
,
163 AR_BEGIN(FEProcessStoreTiles
, pDC
->drawId
);
164 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
165 STORE_TILES_DESC
* pDesc
= (STORE_TILES_DESC
*)pUserData
;
167 // queue a store to each macro tile
168 // compute macro tile bounds for the specified rect
169 uint32_t macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
170 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
171 uint32_t macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
172 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
176 work
.type
= STORETILES
;
177 work
.pfnWork
= ProcessStoreTileBE
;
178 work
.desc
.storeTiles
= *pDesc
;
180 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
182 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
184 pTileMgr
->enqueue(x
, y
, &work
);
188 AR_END(FEProcessStoreTiles
, 0);
191 //////////////////////////////////////////////////////////////////////////
192 /// @brief FE handler for SwrInvalidateTiles.
193 /// @param pContext - pointer to SWR context.
194 /// @param pDC - pointer to draw context.
195 /// @param workerId - thread's worker id. Even thread has a unique id.
196 /// @param pUserData - Pointer to user data passed back to callback.
197 /// @todo This should go away when we switch this to use compute threading.
198 void ProcessDiscardInvalidateTiles(
199 SWR_CONTEXT
*pContext
,
204 AR_BEGIN(FEProcessInvalidateTiles
, pDC
->drawId
);
205 DISCARD_INVALIDATE_TILES_DESC
*pDesc
= (DISCARD_INVALIDATE_TILES_DESC
*)pUserData
;
206 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
208 // compute macro tile bounds for the specified rect
209 uint32_t macroTileXMin
= (pDesc
->rect
.xmin
+ KNOB_MACROTILE_X_DIM
- 1) / KNOB_MACROTILE_X_DIM
;
210 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
/ KNOB_MACROTILE_X_DIM
) - 1;
211 uint32_t macroTileYMin
= (pDesc
->rect
.ymin
+ KNOB_MACROTILE_Y_DIM
- 1) / KNOB_MACROTILE_Y_DIM
;
212 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
/ KNOB_MACROTILE_Y_DIM
) - 1;
214 if (pDesc
->fullTilesOnly
== false)
216 // include partial tiles
217 macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
218 macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
219 macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
220 macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
223 SWR_ASSERT(macroTileXMax
<= KNOB_NUM_HOT_TILES_X
);
224 SWR_ASSERT(macroTileYMax
<= KNOB_NUM_HOT_TILES_Y
);
226 macroTileXMax
= std::min
<int32_t>(macroTileXMax
, KNOB_NUM_HOT_TILES_X
);
227 macroTileYMax
= std::min
<int32_t>(macroTileYMax
, KNOB_NUM_HOT_TILES_Y
);
231 work
.type
= DISCARDINVALIDATETILES
;
232 work
.pfnWork
= ProcessDiscardInvalidateTilesBE
;
233 work
.desc
.discardInvalidateTiles
= *pDesc
;
235 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
237 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
239 pTileMgr
->enqueue(x
, y
, &work
);
243 AR_END(FEProcessInvalidateTiles
, 0);
246 //////////////////////////////////////////////////////////////////////////
247 /// @brief Computes the number of primitives given the number of verts.
248 /// @param mode - primitive topology for draw operation.
249 /// @param numPrims - number of vertices or indices for draw.
250 /// @todo Frontend needs to be refactored. This will go in appropriate place then.
251 uint32_t GetNumPrims(
252 PRIMITIVE_TOPOLOGY mode
,
257 case TOP_POINT_LIST
: return numPrims
;
258 case TOP_TRIANGLE_LIST
: return numPrims
/ 3;
259 case TOP_TRIANGLE_STRIP
: return numPrims
< 3 ? 0 : numPrims
- 2;
260 case TOP_TRIANGLE_FAN
: return numPrims
< 3 ? 0 : numPrims
- 2;
261 case TOP_TRIANGLE_DISC
: return numPrims
< 2 ? 0 : numPrims
- 1;
262 case TOP_QUAD_LIST
: return numPrims
/ 4;
263 case TOP_QUAD_STRIP
: return numPrims
< 4 ? 0 : (numPrims
- 2) / 2;
264 case TOP_LINE_STRIP
: return numPrims
< 2 ? 0 : numPrims
- 1;
265 case TOP_LINE_LIST
: return numPrims
/ 2;
266 case TOP_LINE_LOOP
: return numPrims
;
267 case TOP_RECT_LIST
: return numPrims
/ 3;
268 case TOP_LINE_LIST_ADJ
: return numPrims
/ 4;
269 case TOP_LISTSTRIP_ADJ
: return numPrims
< 3 ? 0 : numPrims
- 3;
270 case TOP_TRI_LIST_ADJ
: return numPrims
/ 6;
271 case TOP_TRI_STRIP_ADJ
: return numPrims
< 4 ? 0 : (numPrims
/ 2) - 2;
273 case TOP_PATCHLIST_1
:
274 case TOP_PATCHLIST_2
:
275 case TOP_PATCHLIST_3
:
276 case TOP_PATCHLIST_4
:
277 case TOP_PATCHLIST_5
:
278 case TOP_PATCHLIST_6
:
279 case TOP_PATCHLIST_7
:
280 case TOP_PATCHLIST_8
:
281 case TOP_PATCHLIST_9
:
282 case TOP_PATCHLIST_10
:
283 case TOP_PATCHLIST_11
:
284 case TOP_PATCHLIST_12
:
285 case TOP_PATCHLIST_13
:
286 case TOP_PATCHLIST_14
:
287 case TOP_PATCHLIST_15
:
288 case TOP_PATCHLIST_16
:
289 case TOP_PATCHLIST_17
:
290 case TOP_PATCHLIST_18
:
291 case TOP_PATCHLIST_19
:
292 case TOP_PATCHLIST_20
:
293 case TOP_PATCHLIST_21
:
294 case TOP_PATCHLIST_22
:
295 case TOP_PATCHLIST_23
:
296 case TOP_PATCHLIST_24
:
297 case TOP_PATCHLIST_25
:
298 case TOP_PATCHLIST_26
:
299 case TOP_PATCHLIST_27
:
300 case TOP_PATCHLIST_28
:
301 case TOP_PATCHLIST_29
:
302 case TOP_PATCHLIST_30
:
303 case TOP_PATCHLIST_31
:
304 case TOP_PATCHLIST_32
:
305 return numPrims
/ (mode
- TOP_PATCHLIST_BASE
);
308 case TOP_POINT_LIST_BF
:
309 case TOP_LINE_STRIP_CONT
:
310 case TOP_LINE_STRIP_BF
:
311 case TOP_LINE_STRIP_CONT_BF
:
312 case TOP_TRIANGLE_FAN_NOSTIPPLE
:
313 case TOP_TRI_STRIP_REVERSE
:
314 case TOP_PATCHLIST_BASE
:
316 SWR_ASSERT(false, "Unsupported topology: %d", mode
);
323 //////////////////////////////////////////////////////////////////////////
324 /// @brief Computes the number of verts given the number of primitives.
325 /// @param mode - primitive topology for draw operation.
326 /// @param numPrims - number of primitives for draw.
327 uint32_t GetNumVerts(
328 PRIMITIVE_TOPOLOGY mode
,
333 case TOP_POINT_LIST
: return numPrims
;
334 case TOP_TRIANGLE_LIST
: return numPrims
* 3;
335 case TOP_TRIANGLE_STRIP
: return numPrims
? numPrims
+ 2 : 0;
336 case TOP_TRIANGLE_FAN
: return numPrims
? numPrims
+ 2 : 0;
337 case TOP_TRIANGLE_DISC
: return numPrims
? numPrims
+ 1 : 0;
338 case TOP_QUAD_LIST
: return numPrims
* 4;
339 case TOP_QUAD_STRIP
: return numPrims
? numPrims
* 2 + 2 : 0;
340 case TOP_LINE_STRIP
: return numPrims
? numPrims
+ 1 : 0;
341 case TOP_LINE_LIST
: return numPrims
* 2;
342 case TOP_LINE_LOOP
: return numPrims
;
343 case TOP_RECT_LIST
: return numPrims
* 3;
344 case TOP_LINE_LIST_ADJ
: return numPrims
* 4;
345 case TOP_LISTSTRIP_ADJ
: return numPrims
? numPrims
+ 3 : 0;
346 case TOP_TRI_LIST_ADJ
: return numPrims
* 6;
347 case TOP_TRI_STRIP_ADJ
: return numPrims
? (numPrims
+ 2) * 2 : 0;
349 case TOP_PATCHLIST_1
:
350 case TOP_PATCHLIST_2
:
351 case TOP_PATCHLIST_3
:
352 case TOP_PATCHLIST_4
:
353 case TOP_PATCHLIST_5
:
354 case TOP_PATCHLIST_6
:
355 case TOP_PATCHLIST_7
:
356 case TOP_PATCHLIST_8
:
357 case TOP_PATCHLIST_9
:
358 case TOP_PATCHLIST_10
:
359 case TOP_PATCHLIST_11
:
360 case TOP_PATCHLIST_12
:
361 case TOP_PATCHLIST_13
:
362 case TOP_PATCHLIST_14
:
363 case TOP_PATCHLIST_15
:
364 case TOP_PATCHLIST_16
:
365 case TOP_PATCHLIST_17
:
366 case TOP_PATCHLIST_18
:
367 case TOP_PATCHLIST_19
:
368 case TOP_PATCHLIST_20
:
369 case TOP_PATCHLIST_21
:
370 case TOP_PATCHLIST_22
:
371 case TOP_PATCHLIST_23
:
372 case TOP_PATCHLIST_24
:
373 case TOP_PATCHLIST_25
:
374 case TOP_PATCHLIST_26
:
375 case TOP_PATCHLIST_27
:
376 case TOP_PATCHLIST_28
:
377 case TOP_PATCHLIST_29
:
378 case TOP_PATCHLIST_30
:
379 case TOP_PATCHLIST_31
:
380 case TOP_PATCHLIST_32
:
381 return numPrims
* (mode
- TOP_PATCHLIST_BASE
);
384 case TOP_POINT_LIST_BF
:
385 case TOP_LINE_STRIP_CONT
:
386 case TOP_LINE_STRIP_BF
:
387 case TOP_LINE_STRIP_CONT_BF
:
388 case TOP_TRIANGLE_FAN_NOSTIPPLE
:
389 case TOP_TRI_STRIP_REVERSE
:
390 case TOP_PATCHLIST_BASE
:
392 SWR_ASSERT(false, "Unsupported topology: %d", mode
);
399 //////////////////////////////////////////////////////////////////////////
400 /// @brief Return number of verts per primitive.
401 /// @param topology - topology
402 /// @param includeAdjVerts - include adjacent verts in primitive vertices
403 INLINE
uint32_t NumVertsPerPrim(PRIMITIVE_TOPOLOGY topology
, bool includeAdjVerts
)
405 uint32_t numVerts
= 0;
409 case TOP_POINT_LIST_BF
:
414 case TOP_LINE_LIST_ADJ
:
416 case TOP_LINE_STRIP_CONT
:
417 case TOP_LINE_STRIP_BF
:
418 case TOP_LISTSTRIP_ADJ
:
421 case TOP_TRIANGLE_LIST
:
422 case TOP_TRIANGLE_STRIP
:
423 case TOP_TRIANGLE_FAN
:
424 case TOP_TRI_LIST_ADJ
:
425 case TOP_TRI_STRIP_ADJ
:
426 case TOP_TRI_STRIP_REVERSE
:
434 case TOP_PATCHLIST_1
:
435 case TOP_PATCHLIST_2
:
436 case TOP_PATCHLIST_3
:
437 case TOP_PATCHLIST_4
:
438 case TOP_PATCHLIST_5
:
439 case TOP_PATCHLIST_6
:
440 case TOP_PATCHLIST_7
:
441 case TOP_PATCHLIST_8
:
442 case TOP_PATCHLIST_9
:
443 case TOP_PATCHLIST_10
:
444 case TOP_PATCHLIST_11
:
445 case TOP_PATCHLIST_12
:
446 case TOP_PATCHLIST_13
:
447 case TOP_PATCHLIST_14
:
448 case TOP_PATCHLIST_15
:
449 case TOP_PATCHLIST_16
:
450 case TOP_PATCHLIST_17
:
451 case TOP_PATCHLIST_18
:
452 case TOP_PATCHLIST_19
:
453 case TOP_PATCHLIST_20
:
454 case TOP_PATCHLIST_21
:
455 case TOP_PATCHLIST_22
:
456 case TOP_PATCHLIST_23
:
457 case TOP_PATCHLIST_24
:
458 case TOP_PATCHLIST_25
:
459 case TOP_PATCHLIST_26
:
460 case TOP_PATCHLIST_27
:
461 case TOP_PATCHLIST_28
:
462 case TOP_PATCHLIST_29
:
463 case TOP_PATCHLIST_30
:
464 case TOP_PATCHLIST_31
:
465 case TOP_PATCHLIST_32
:
466 numVerts
= topology
- TOP_PATCHLIST_BASE
;
469 SWR_ASSERT(false, "Unsupported topology: %d", topology
);
477 case TOP_LISTSTRIP_ADJ
:
478 case TOP_LINE_LIST_ADJ
: numVerts
= 4; break;
479 case TOP_TRI_STRIP_ADJ
:
480 case TOP_TRI_LIST_ADJ
: numVerts
= 6; break;
488 //////////////////////////////////////////////////////////////////////////
489 /// @brief Generate mask from remaining work.
490 /// @param numWorkItems - Number of items being worked on by a SIMD.
491 static INLINE simdscalari
GenerateMask(uint32_t numItemsRemaining
)
493 uint32_t numActive
= (numItemsRemaining
>= KNOB_SIMD_WIDTH
) ? KNOB_SIMD_WIDTH
: numItemsRemaining
;
494 uint32_t mask
= (numActive
> 0) ? ((1 << numActive
) - 1) : 0;
495 return _simd_castps_si(vMask(mask
));
499 //////////////////////////////////////////////////////////////////////////
500 /// @brief Gather scissor rect data based on per-prim viewport indices.
501 /// @param pScissorsInFixedPoint - array of scissor rects in 16.8 fixed point.
502 /// @param pViewportIndex - array of per-primitive vewport indexes.
503 /// @param scisXmin - output vector of per-prmitive scissor rect Xmin data.
504 /// @param scisYmin - output vector of per-prmitive scissor rect Ymin data.
505 /// @param scisXmax - output vector of per-prmitive scissor rect Xmax data.
506 /// @param scisYmax - output vector of per-prmitive scissor rect Ymax data.
508 /// @todo: Look at speeding this up -- weigh against corresponding costs in rasterizer.
509 template<size_t SimdWidth
>
510 struct GatherScissors
512 static void Gather(const SWR_RECT
* pScissorsInFixedPoint
, const uint32_t* pViewportIndex
,
513 simdscalari
&scisXmin
, simdscalari
&scisYmin
,
514 simdscalari
&scisXmax
, simdscalari
&scisYmax
)
516 SWR_ASSERT(0, "Unhandled Simd Width in Scissor Rect Gather");
521 struct GatherScissors
<8>
523 static void Gather(const SWR_RECT
* pScissorsInFixedPoint
, const uint32_t* pViewportIndex
,
524 simdscalari
&scisXmin
, simdscalari
&scisYmin
,
525 simdscalari
&scisXmax
, simdscalari
&scisYmax
)
527 scisXmin
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].xmin
,
528 pScissorsInFixedPoint
[pViewportIndex
[1]].xmin
,
529 pScissorsInFixedPoint
[pViewportIndex
[2]].xmin
,
530 pScissorsInFixedPoint
[pViewportIndex
[3]].xmin
,
531 pScissorsInFixedPoint
[pViewportIndex
[4]].xmin
,
532 pScissorsInFixedPoint
[pViewportIndex
[5]].xmin
,
533 pScissorsInFixedPoint
[pViewportIndex
[6]].xmin
,
534 pScissorsInFixedPoint
[pViewportIndex
[7]].xmin
);
535 scisYmin
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].ymin
,
536 pScissorsInFixedPoint
[pViewportIndex
[1]].ymin
,
537 pScissorsInFixedPoint
[pViewportIndex
[2]].ymin
,
538 pScissorsInFixedPoint
[pViewportIndex
[3]].ymin
,
539 pScissorsInFixedPoint
[pViewportIndex
[4]].ymin
,
540 pScissorsInFixedPoint
[pViewportIndex
[5]].ymin
,
541 pScissorsInFixedPoint
[pViewportIndex
[6]].ymin
,
542 pScissorsInFixedPoint
[pViewportIndex
[7]].ymin
);
543 scisXmax
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].xmax
,
544 pScissorsInFixedPoint
[pViewportIndex
[1]].xmax
,
545 pScissorsInFixedPoint
[pViewportIndex
[2]].xmax
,
546 pScissorsInFixedPoint
[pViewportIndex
[3]].xmax
,
547 pScissorsInFixedPoint
[pViewportIndex
[4]].xmax
,
548 pScissorsInFixedPoint
[pViewportIndex
[5]].xmax
,
549 pScissorsInFixedPoint
[pViewportIndex
[6]].xmax
,
550 pScissorsInFixedPoint
[pViewportIndex
[7]].xmax
);
551 scisYmax
= _simd_set_epi32(pScissorsInFixedPoint
[pViewportIndex
[0]].ymax
,
552 pScissorsInFixedPoint
[pViewportIndex
[1]].ymax
,
553 pScissorsInFixedPoint
[pViewportIndex
[2]].ymax
,
554 pScissorsInFixedPoint
[pViewportIndex
[3]].ymax
,
555 pScissorsInFixedPoint
[pViewportIndex
[4]].ymax
,
556 pScissorsInFixedPoint
[pViewportIndex
[5]].ymax
,
557 pScissorsInFixedPoint
[pViewportIndex
[6]].ymax
,
558 pScissorsInFixedPoint
[pViewportIndex
[7]].ymax
);
562 //////////////////////////////////////////////////////////////////////////
563 /// @brief StreamOut - Streams vertex data out to SO buffers.
564 /// Generally, we are only streaming out a SIMDs worth of triangles.
565 /// @param pDC - pointer to draw context.
566 /// @param workerId - thread's worker id. Even thread has a unique id.
567 /// @param numPrims - Number of prims to streamout (e.g. points, lines, tris)
568 static void StreamOut(
573 uint32_t streamIndex
)
575 SWR_CONTEXT
*pContext
= pDC
->pContext
;
577 AR_BEGIN(FEStreamout
, pDC
->drawId
);
579 const API_STATE
& state
= GetApiState(pDC
);
580 const SWR_STREAMOUT_STATE
&soState
= state
.soState
;
582 uint32_t soVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, false);
584 // The pPrimData buffer is sparse in that we allocate memory for all 32 attributes for each vertex.
585 uint32_t primDataDwordVertexStride
= (KNOB_NUM_ATTRIBUTES
* sizeof(float) * 4) / sizeof(uint32_t);
587 SWR_STREAMOUT_CONTEXT soContext
= { 0 };
589 // Setup buffer state pointers.
590 for (uint32_t i
= 0; i
< 4; ++i
)
592 soContext
.pBuffer
[i
] = &state
.soBuffer
[i
];
595 uint32_t numPrims
= pa
.NumPrims();
596 for (uint32_t primIndex
= 0; primIndex
< numPrims
; ++primIndex
)
599 uint32_t soMask
= soState
.streamMasks
[streamIndex
];
601 // Write all entries into primitive data buffer for SOS.
602 while (_BitScanForward(&slot
, soMask
))
604 __m128 attrib
[MAX_NUM_VERTS_PER_PRIM
]; // prim attribs (always 4 wide)
605 uint32_t paSlot
= slot
+ VERTEX_ATTRIB_START_SLOT
;
606 pa
.AssembleSingle(paSlot
, primIndex
, attrib
);
608 // Attribute offset is relative offset from start of vertex.
609 // Note that attributes start at slot 1 in the PA buffer. We need to write this
610 // to prim data starting at slot 0. Which is why we do (slot - 1).
611 // Also note: GL works slightly differently, and needs slot 0
612 uint32_t primDataAttribOffset
= slot
* sizeof(float) * 4 / sizeof(uint32_t);
614 // Store each vertex's attrib at appropriate locations in pPrimData buffer.
615 for (uint32_t v
= 0; v
< soVertsPerPrim
; ++v
)
617 uint32_t* pPrimDataAttrib
= pPrimData
+ primDataAttribOffset
+ (v
* primDataDwordVertexStride
);
619 _mm_store_ps((float*)pPrimDataAttrib
, attrib
[v
]);
621 soMask
&= ~(1 << slot
);
624 // Update pPrimData pointer
625 soContext
.pPrimData
= pPrimData
;
628 SWR_ASSERT(state
.pfnSoFunc
[streamIndex
] != nullptr, "Trying to execute uninitialized streamout jit function.");
629 state
.pfnSoFunc
[streamIndex
](soContext
);
632 // Update SO write offset. The driver provides memory for the update.
633 for (uint32_t i
= 0; i
< 4; ++i
)
635 if (state
.soBuffer
[i
].pWriteOffset
)
637 *state
.soBuffer
[i
].pWriteOffset
= soContext
.pBuffer
[i
]->streamOffset
* sizeof(uint32_t);
640 if (state
.soBuffer
[i
].soWriteEnable
)
642 pDC
->dynState
.SoWriteOffset
[i
] = soContext
.pBuffer
[i
]->streamOffset
* sizeof(uint32_t);
643 pDC
->dynState
.SoWriteOffsetDirty
[i
] = true;
647 UPDATE_STAT_FE(SoPrimStorageNeeded
[streamIndex
], soContext
.numPrimStorageNeeded
);
648 UPDATE_STAT_FE(SoNumPrimsWritten
[streamIndex
], soContext
.numPrimsWritten
);
650 AR_END(FEStreamout
, 1);
653 //////////////////////////////////////////////////////////////////////////
654 /// @brief Computes number of invocations. The current index represents
655 /// the start of the SIMD. The max index represents how much work
656 /// items are remaining. If there is less then a SIMD's xmin of work
657 /// then return the remaining amount of work.
658 /// @param curIndex - The start index for the SIMD.
659 /// @param maxIndex - The last index for all work items.
660 static INLINE
uint32_t GetNumInvocations(
664 uint32_t remainder
= (maxIndex
- curIndex
);
665 return (remainder
>= KNOB_SIMD_WIDTH
) ? KNOB_SIMD_WIDTH
: remainder
;
668 //////////////////////////////////////////////////////////////////////////
669 /// @brief Converts a streamId buffer to a cut buffer for the given stream id.
670 /// The geometry shader will loop over each active streamout buffer, assembling
671 /// primitives for the downstream stages. When multistream output is enabled,
672 /// the generated stream ID buffer from the GS needs to be converted to a cut
673 /// buffer for the primitive assembler.
674 /// @param stream - stream id to generate the cut buffer for
675 /// @param pStreamIdBase - pointer to the stream ID buffer
676 /// @param numEmittedVerts - Number of total verts emitted by the GS
677 /// @param pCutBuffer - output buffer to write cuts to
678 void ProcessStreamIdBuffer(uint32_t stream
, uint8_t* pStreamIdBase
, uint32_t numEmittedVerts
, uint8_t *pCutBuffer
)
680 SWR_ASSERT(stream
< MAX_SO_STREAMS
);
682 uint32_t numInputBytes
= (numEmittedVerts
* 2 + 7) / 8;
683 uint32_t numOutputBytes
= std::max(numInputBytes
/ 2, 1U);
685 for (uint32_t b
= 0; b
< numOutputBytes
; ++b
)
687 uint8_t curInputByte
= pStreamIdBase
[2*b
];
689 for (uint32_t i
= 0; i
< 4; ++i
)
691 if ((curInputByte
& 0x3) != stream
)
698 curInputByte
= pStreamIdBase
[2 * b
+ 1];
699 for (uint32_t i
= 0; i
< 4; ++i
)
701 if ((curInputByte
& 0x3) != stream
)
703 outByte
|= (1 << (i
+ 4));
708 *pCutBuffer
++ = outByte
;
712 THREAD SWR_GS_CONTEXT tlsGsContext
;
714 //////////////////////////////////////////////////////////////////////////
715 /// @brief Implements GS stage.
716 /// @param pDC - pointer to draw context.
717 /// @param workerId - thread's worker id. Even thread has a unique id.
718 /// @param pa - The primitive assembly object.
719 /// @param pGsOut - output stream for GS
721 typename HasStreamOutT
,
723 static void GeometryShaderStage(
729 void* pStreamCutBuffer
,
730 uint32_t* pSoPrimData
,
733 SWR_CONTEXT
*pContext
= pDC
->pContext
;
735 AR_BEGIN(FEGeometryShader
, pDC
->drawId
);
737 const API_STATE
& state
= GetApiState(pDC
);
738 const SWR_GS_STATE
* pState
= &state
.gsState
;
740 SWR_ASSERT(pGsOut
!= nullptr, "GS output buffer should be initialized");
741 SWR_ASSERT(pCutBuffer
!= nullptr, "GS output cut buffer should be initialized");
743 tlsGsContext
.pStream
= (uint8_t*)pGsOut
;
744 tlsGsContext
.pCutOrStreamIdBuffer
= (uint8_t*)pCutBuffer
;
745 tlsGsContext
.PrimitiveID
= primID
;
747 uint32_t numVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, true);
748 simdvector attrib
[MAX_ATTRIBUTES
];
750 // assemble all attributes for the input primitive
751 for (uint32_t slot
= 0; slot
< pState
->numInputAttribs
; ++slot
)
753 uint32_t attribSlot
= VERTEX_ATTRIB_START_SLOT
+ slot
;
754 pa
.Assemble(attribSlot
, attrib
);
756 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
758 tlsGsContext
.vert
[i
].attrib
[attribSlot
] = attrib
[i
];
763 pa
.Assemble(VERTEX_POSITION_SLOT
, attrib
);
764 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
766 tlsGsContext
.vert
[i
].attrib
[VERTEX_POSITION_SLOT
] = attrib
[i
];
769 const uint32_t vertexStride
= sizeof(simdvertex
);
770 const uint32_t numSimdBatches
= (state
.gsState
.maxNumVerts
+ KNOB_SIMD_WIDTH
- 1) / KNOB_SIMD_WIDTH
;
771 const uint32_t inputPrimStride
= numSimdBatches
* vertexStride
;
772 const uint32_t instanceStride
= inputPrimStride
* KNOB_SIMD_WIDTH
;
773 uint32_t cutPrimStride
;
774 uint32_t cutInstanceStride
;
776 if (pState
->isSingleStream
)
778 cutPrimStride
= (state
.gsState
.maxNumVerts
+ 7) / 8;
779 cutInstanceStride
= cutPrimStride
* KNOB_SIMD_WIDTH
;
783 cutPrimStride
= AlignUp(state
.gsState
.maxNumVerts
* 2 / 8, 4);
784 cutInstanceStride
= cutPrimStride
* KNOB_SIMD_WIDTH
;
787 // record valid prims from the frontend to avoid over binning the newly generated
789 uint32_t numInputPrims
= pa
.NumPrims();
791 for (uint32_t instance
= 0; instance
< pState
->instanceCount
; ++instance
)
793 tlsGsContext
.InstanceID
= instance
;
794 tlsGsContext
.mask
= GenerateMask(numInputPrims
);
796 // execute the geometry shader
797 state
.pfnGsFunc(GetPrivateState(pDC
), &tlsGsContext
);
799 tlsGsContext
.pStream
+= instanceStride
;
800 tlsGsContext
.pCutOrStreamIdBuffer
+= cutInstanceStride
;
803 // set up new binner and state for the GS output topology
804 PFN_PROCESS_PRIMS pfnClipFunc
= nullptr;
807 switch (pState
->outputTopology
)
809 case TOP_TRIANGLE_STRIP
: pfnClipFunc
= ClipTriangles
; break;
810 case TOP_LINE_STRIP
: pfnClipFunc
= ClipLines
; break;
811 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints
; break;
812 default: SWR_ASSERT(false, "Unexpected GS output topology: %d", pState
->outputTopology
);
816 // foreach input prim:
817 // - setup a new PA based on the emitted verts for that prim
818 // - loop over the new verts, calling PA to assemble each prim
819 uint32_t* pVertexCount
= (uint32_t*)&tlsGsContext
.vertexCount
;
820 uint32_t* pPrimitiveId
= (uint32_t*)&primID
;
822 uint32_t totalPrimsGenerated
= 0;
823 for (uint32_t inputPrim
= 0; inputPrim
< numInputPrims
; ++inputPrim
)
825 uint8_t* pInstanceBase
= (uint8_t*)pGsOut
+ inputPrim
* inputPrimStride
;
826 uint8_t* pCutBufferBase
= (uint8_t*)pCutBuffer
+ inputPrim
* cutPrimStride
;
827 for (uint32_t instance
= 0; instance
< pState
->instanceCount
; ++instance
)
829 uint32_t numEmittedVerts
= pVertexCount
[inputPrim
];
830 if (numEmittedVerts
== 0)
835 uint8_t* pBase
= pInstanceBase
+ instance
* instanceStride
;
836 uint8_t* pCutBase
= pCutBufferBase
+ instance
* cutInstanceStride
;
838 uint32_t numAttribs
= state
.feNumAttributes
;
840 for (uint32_t stream
= 0; stream
< MAX_SO_STREAMS
; ++stream
)
842 bool processCutVerts
= false;
844 uint8_t* pCutBuffer
= pCutBase
;
846 // assign default stream ID, only relevant when GS is outputting a single stream
847 uint32_t streamID
= 0;
848 if (pState
->isSingleStream
)
850 processCutVerts
= true;
851 streamID
= pState
->singleStreamID
;
852 if (streamID
!= stream
) continue;
856 // early exit if this stream is not enabled for streamout
857 if (HasStreamOutT::value
&& !state
.soState
.streamEnable
[stream
])
862 // multi-stream output, need to translate StreamID buffer to a cut buffer
863 ProcessStreamIdBuffer(stream
, pCutBase
, numEmittedVerts
, (uint8_t*)pStreamCutBuffer
);
864 pCutBuffer
= (uint8_t*)pStreamCutBuffer
;
865 processCutVerts
= false;
868 PA_STATE_CUT
gsPa(pDC
, pBase
, numEmittedVerts
, pCutBuffer
, numEmittedVerts
, numAttribs
, pState
->outputTopology
, processCutVerts
);
870 while (gsPa
.GetNextStreamOutput())
874 bool assemble
= gsPa
.Assemble(VERTEX_POSITION_SLOT
, attrib
);
878 totalPrimsGenerated
+= gsPa
.NumPrims();
880 if (HasStreamOutT::value
)
882 StreamOut(pDC
, gsPa
, workerId
, pSoPrimData
, stream
);
885 if (HasRastT::value
&& state
.soState
.streamToRasterizer
== stream
)
888 // pull primitiveID from the GS output if available
889 if (state
.gsState
.emitsPrimitiveID
)
891 simdvector primIdAttrib
[3];
892 gsPa
.Assemble(VERTEX_PRIMID_SLOT
, primIdAttrib
);
893 vPrimId
= _simd_castps_si(primIdAttrib
[0].x
);
897 vPrimId
= _simd_set1_epi32(pPrimitiveId
[inputPrim
]);
900 // use viewport array index if GS declares it as an output attribute. Otherwise use index 0.
901 simdscalari vViewPortIdx
;
902 if (state
.gsState
.emitsViewportArrayIndex
)
904 simdvector vpiAttrib
[3];
905 gsPa
.Assemble(VERTEX_VIEWPORT_ARRAY_INDEX_SLOT
, vpiAttrib
);
907 // OOB indices => forced to zero.
908 simdscalari vNumViewports
= _simd_set1_epi32(KNOB_NUM_VIEWPORTS_SCISSORS
);
909 simdscalari vClearMask
= _simd_cmplt_epi32(_simd_castps_si(vpiAttrib
[0].x
), vNumViewports
);
910 vpiAttrib
[0].x
= _simd_and_ps(_simd_castsi_ps(vClearMask
), vpiAttrib
[0].x
);
912 vViewPortIdx
= _simd_castps_si(vpiAttrib
[0].x
);
916 vViewPortIdx
= _simd_set1_epi32(0);
919 pfnClipFunc(pDC
, gsPa
, workerId
, attrib
, GenMask(gsPa
.NumPrims()), vPrimId
, vViewPortIdx
);
922 } while (gsPa
.NextPrim());
928 // update GS pipeline stats
929 UPDATE_STAT_FE(GsInvocations
, numInputPrims
* pState
->instanceCount
);
930 UPDATE_STAT_FE(GsPrimitives
, totalPrimsGenerated
);
932 AR_END(FEGeometryShader
, 1);
935 //////////////////////////////////////////////////////////////////////////
936 /// @brief Allocate GS buffers
937 /// @param pDC - pointer to draw context.
938 /// @param state - API state
939 /// @param ppGsOut - pointer to GS output buffer allocation
940 /// @param ppCutBuffer - pointer to GS output cut buffer allocation
941 static INLINE
void AllocateGsBuffers(DRAW_CONTEXT
* pDC
, const API_STATE
& state
, void** ppGsOut
, void** ppCutBuffer
,
942 void **ppStreamCutBuffer
)
944 auto pArena
= pDC
->pArena
;
945 SWR_ASSERT(pArena
!= nullptr);
946 SWR_ASSERT(state
.gsState
.gsEnable
);
947 // allocate arena space to hold GS output verts
948 // @todo pack attribs
949 // @todo support multiple streams
950 const uint32_t vertexStride
= sizeof(simdvertex
);
951 const uint32_t numSimdBatches
= (state
.gsState
.maxNumVerts
+ KNOB_SIMD_WIDTH
- 1) / KNOB_SIMD_WIDTH
;
952 uint32_t size
= state
.gsState
.instanceCount
* numSimdBatches
* vertexStride
* KNOB_SIMD_WIDTH
;
953 *ppGsOut
= pArena
->AllocAligned(size
, KNOB_SIMD_WIDTH
* sizeof(float));
955 const uint32_t cutPrimStride
= (state
.gsState
.maxNumVerts
+ 7) / 8;
956 const uint32_t streamIdPrimStride
= AlignUp(state
.gsState
.maxNumVerts
* 2 / 8, 4);
957 const uint32_t cutBufferSize
= cutPrimStride
* state
.gsState
.instanceCount
* KNOB_SIMD_WIDTH
;
958 const uint32_t streamIdSize
= streamIdPrimStride
* state
.gsState
.instanceCount
* KNOB_SIMD_WIDTH
;
960 // allocate arena space to hold cut or streamid buffer, which is essentially a bitfield sized to the
961 // maximum vertex output as defined by the GS state, per SIMD lane, per GS instance
963 // allocate space for temporary per-stream cut buffer if multi-stream is enabled
964 if (state
.gsState
.isSingleStream
)
966 *ppCutBuffer
= pArena
->AllocAligned(cutBufferSize
, KNOB_SIMD_WIDTH
* sizeof(float));
967 *ppStreamCutBuffer
= nullptr;
971 *ppCutBuffer
= pArena
->AllocAligned(streamIdSize
, KNOB_SIMD_WIDTH
* sizeof(float));
972 *ppStreamCutBuffer
= pArena
->AllocAligned(cutBufferSize
, KNOB_SIMD_WIDTH
* sizeof(float));
977 //////////////////////////////////////////////////////////////////////////
978 /// @brief Contains all data generated by the HS and passed to the
979 /// tessellator and DS.
980 struct TessellationThreadLocalData
982 SWR_HS_CONTEXT hsContext
;
983 ScalarPatch patchData
[KNOB_SIMD_WIDTH
];
987 simdscalar
* pDSOutput
;
988 size_t numDSOutputVectors
;
991 THREAD TessellationThreadLocalData
* gt_pTessellationThreadData
= nullptr;
993 //////////////////////////////////////////////////////////////////////////
994 /// @brief Allocate tessellation data for this worker thread.
996 static void AllocateTessellationData(SWR_CONTEXT
* pContext
)
998 /// @TODO - Don't use thread local storage. Use Worker local storage instead.
999 if (gt_pTessellationThreadData
== nullptr)
1001 gt_pTessellationThreadData
= (TessellationThreadLocalData
*)
1002 AlignedMalloc(sizeof(TessellationThreadLocalData
), 64);
1003 memset(gt_pTessellationThreadData
, 0, sizeof(*gt_pTessellationThreadData
));
1007 //////////////////////////////////////////////////////////////////////////
1008 /// @brief Implements Tessellation Stages.
1009 /// @param pDC - pointer to draw context.
1010 /// @param workerId - thread's worker id. Even thread has a unique id.
1011 /// @param pa - The primitive assembly object.
1012 /// @param pGsOut - output stream for GS
1014 typename HasGeometryShaderT
,
1015 typename HasStreamOutT
,
1017 static void TessellationStages(
1023 void* pCutStreamBuffer
,
1024 uint32_t* pSoPrimData
,
1027 SWR_CONTEXT
*pContext
= pDC
->pContext
;
1028 const API_STATE
& state
= GetApiState(pDC
);
1029 const SWR_TS_STATE
& tsState
= state
.tsState
;
1031 SWR_ASSERT(gt_pTessellationThreadData
);
1033 HANDLE tsCtx
= TSInitCtx(
1035 tsState
.partitioning
,
1036 tsState
.tsOutputTopology
,
1037 gt_pTessellationThreadData
->pTxCtx
,
1038 gt_pTessellationThreadData
->tsCtxSize
);
1039 if (tsCtx
== nullptr)
1041 gt_pTessellationThreadData
->pTxCtx
= AlignedMalloc(gt_pTessellationThreadData
->tsCtxSize
, 64);
1044 tsState
.partitioning
,
1045 tsState
.tsOutputTopology
,
1046 gt_pTessellationThreadData
->pTxCtx
,
1047 gt_pTessellationThreadData
->tsCtxSize
);
1051 PFN_PROCESS_PRIMS pfnClipFunc
= nullptr;
1052 if (HasRastT::value
)
1054 switch (tsState
.postDSTopology
)
1056 case TOP_TRIANGLE_LIST
: pfnClipFunc
= ClipTriangles
; break;
1057 case TOP_LINE_LIST
: pfnClipFunc
= ClipLines
; break;
1058 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints
; break;
1059 default: SWR_ASSERT(false, "Unexpected DS output topology: %d", tsState
.postDSTopology
);
1063 SWR_HS_CONTEXT
& hsContext
= gt_pTessellationThreadData
->hsContext
;
1064 hsContext
.pCPout
= gt_pTessellationThreadData
->patchData
;
1065 hsContext
.PrimitiveID
= primID
;
1067 uint32_t numVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, false);
1068 // Max storage for one attribute for an entire simdprimitive
1069 simdvector simdattrib
[MAX_NUM_VERTS_PER_PRIM
];
1071 // assemble all attributes for the input primitives
1072 for (uint32_t slot
= 0; slot
< tsState
.numHsInputAttribs
; ++slot
)
1074 uint32_t attribSlot
= VERTEX_ATTRIB_START_SLOT
+ slot
;
1075 pa
.Assemble(attribSlot
, simdattrib
);
1077 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
1079 hsContext
.vert
[i
].attrib
[attribSlot
] = simdattrib
[i
];
1084 memset(hsContext
.pCPout
, 0x90, sizeof(ScalarPatch
) * KNOB_SIMD_WIDTH
);
1087 uint32_t numPrims
= pa
.NumPrims();
1088 hsContext
.mask
= GenerateMask(numPrims
);
1091 AR_BEGIN(FEHullShader
, pDC
->drawId
);
1092 state
.pfnHsFunc(GetPrivateState(pDC
), &hsContext
);
1093 AR_END(FEHullShader
, 0);
1095 UPDATE_STAT_FE(HsInvocations
, numPrims
);
1097 const uint32_t* pPrimId
= (const uint32_t*)&primID
;
1099 for (uint32_t p
= 0; p
< numPrims
; ++p
)
1102 SWR_TS_TESSELLATED_DATA tsData
= { 0 };
1103 AR_BEGIN(FETessellation
, pDC
->drawId
);
1104 TSTessellate(tsCtx
, hsContext
.pCPout
[p
].tessFactors
, tsData
);
1105 AR_END(FETessellation
, 0);
1107 if (tsData
.NumPrimitives
== 0)
1111 SWR_ASSERT(tsData
.NumDomainPoints
);
1113 // Allocate DS Output memory
1114 uint32_t requiredDSVectorInvocations
= AlignUp(tsData
.NumDomainPoints
, KNOB_SIMD_WIDTH
) / KNOB_SIMD_WIDTH
;
1115 size_t requiredDSOutputVectors
= requiredDSVectorInvocations
* tsState
.numDsOutputAttribs
;
1116 size_t requiredAllocSize
= sizeof(simdvector
) * requiredDSOutputVectors
;
1117 if (requiredDSOutputVectors
> gt_pTessellationThreadData
->numDSOutputVectors
)
1119 AlignedFree(gt_pTessellationThreadData
->pDSOutput
);
1120 gt_pTessellationThreadData
->pDSOutput
= (simdscalar
*)AlignedMalloc(requiredAllocSize
, 64);
1121 gt_pTessellationThreadData
->numDSOutputVectors
= requiredDSOutputVectors
;
1123 SWR_ASSERT(gt_pTessellationThreadData
->pDSOutput
);
1124 SWR_ASSERT(gt_pTessellationThreadData
->numDSOutputVectors
>= requiredDSOutputVectors
);
1127 memset(gt_pTessellationThreadData
->pDSOutput
, 0x90, requiredAllocSize
);
1130 // Run Domain Shader
1131 SWR_DS_CONTEXT dsContext
;
1132 dsContext
.PrimitiveID
= pPrimId
[p
];
1133 dsContext
.pCpIn
= &hsContext
.pCPout
[p
];
1134 dsContext
.pDomainU
= (simdscalar
*)tsData
.pDomainPointsU
;
1135 dsContext
.pDomainV
= (simdscalar
*)tsData
.pDomainPointsV
;
1136 dsContext
.pOutputData
= gt_pTessellationThreadData
->pDSOutput
;
1137 dsContext
.vectorStride
= requiredDSVectorInvocations
;
1139 uint32_t dsInvocations
= 0;
1141 for (dsContext
.vectorOffset
= 0; dsContext
.vectorOffset
< requiredDSVectorInvocations
; ++dsContext
.vectorOffset
)
1143 dsContext
.mask
= GenerateMask(tsData
.NumDomainPoints
- dsInvocations
);
1145 AR_BEGIN(FEDomainShader
, pDC
->drawId
);
1146 state
.pfnDsFunc(GetPrivateState(pDC
), &dsContext
);
1147 AR_END(FEDomainShader
, 0);
1149 dsInvocations
+= KNOB_SIMD_WIDTH
;
1151 UPDATE_STAT_FE(DsInvocations
, tsData
.NumDomainPoints
);
1155 dsContext
.pOutputData
,
1156 dsContext
.vectorStride
,
1157 tsState
.numDsOutputAttribs
,
1159 tsData
.NumPrimitives
,
1160 tsState
.postDSTopology
);
1162 while (tessPa
.HasWork())
1164 if (HasGeometryShaderT::value
)
1166 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(
1167 pDC
, workerId
, tessPa
, pGsOut
, pCutBuffer
, pCutStreamBuffer
, pSoPrimData
,
1168 _simd_set1_epi32(dsContext
.PrimitiveID
));
1172 if (HasStreamOutT::value
)
1174 StreamOut(pDC
, tessPa
, workerId
, pSoPrimData
, 0);
1177 if (HasRastT::value
)
1179 simdvector prim
[3]; // Only deal with triangles, lines, or points
1180 AR_BEGIN(FEPAAssemble
, pDC
->drawId
);
1181 #if SWR_ENABLE_ASSERTS
1184 tessPa
.Assemble(VERTEX_POSITION_SLOT
, prim
);
1185 AR_END(FEPAAssemble
, 1);
1186 SWR_ASSERT(assemble
);
1188 SWR_ASSERT(pfnClipFunc
);
1189 pfnClipFunc(pDC
, tessPa
, workerId
, prim
,
1190 GenMask(tessPa
.NumPrims()), _simd_set1_epi32(dsContext
.PrimitiveID
), _simd_set1_epi32(0));
1196 } // while (tessPa.HasWork())
1197 } // for (uint32_t p = 0; p < numPrims; ++p)
1199 TSDestroyCtx(tsCtx
);
1202 //////////////////////////////////////////////////////////////////////////
1203 /// @brief FE handler for SwrDraw.
1204 /// @tparam IsIndexedT - Is indexed drawing enabled
1205 /// @tparam HasTessellationT - Is tessellation enabled
1206 /// @tparam HasGeometryShaderT::value - Is the geometry shader stage enabled
1207 /// @tparam HasStreamOutT - Is stream-out enabled
1208 /// @tparam HasRastT - Is rasterization enabled
1209 /// @param pContext - pointer to SWR context.
1210 /// @param pDC - pointer to draw context.
1211 /// @param workerId - thread's worker id.
1212 /// @param pUserData - Pointer to DRAW_WORK
1214 typename IsIndexedT
,
1215 typename IsCutIndexEnabledT
,
1216 typename HasTessellationT
,
1217 typename HasGeometryShaderT
,
1218 typename HasStreamOutT
,
1221 SWR_CONTEXT
*pContext
,
1227 #if KNOB_ENABLE_TOSS_POINTS
1228 if (KNOB_TOSS_QUEUE_FE
)
1234 AR_BEGIN(FEProcessDraw
, pDC
->drawId
);
1236 DRAW_WORK
& work
= *(DRAW_WORK
*)pUserData
;
1237 const API_STATE
& state
= GetApiState(pDC
);
1238 __m256i vScale
= _mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0);
1239 SWR_VS_CONTEXT vsContext
;
1243 uint32_t endVertex
= work
.numVerts
;
1245 const int32_t* pLastRequestedIndex
= nullptr;
1246 if (IsIndexedT::value
)
1251 indexSize
= sizeof(uint32_t);
1252 pLastRequestedIndex
= &(work
.pIB
[endVertex
]);
1255 indexSize
= sizeof(uint16_t);
1256 // nasty address offset to last index
1257 pLastRequestedIndex
= (int32_t*)(&(((uint16_t*)work
.pIB
)[endVertex
]));
1260 indexSize
= sizeof(uint8_t);
1261 // nasty address offset to last index
1262 pLastRequestedIndex
= (int32_t*)(&(((uint8_t*)work
.pIB
)[endVertex
]));
1270 // No cuts, prune partial primitives.
1271 endVertex
= GetNumVerts(state
.topology
, GetNumPrims(state
.topology
, work
.numVerts
));
1274 SWR_FETCH_CONTEXT fetchInfo
= { 0 };
1275 fetchInfo
.pStreams
= &state
.vertexBuffers
[0];
1276 fetchInfo
.StartInstance
= work
.startInstance
;
1277 fetchInfo
.StartVertex
= 0;
1279 vsContext
.pVin
= &vin
;
1281 if (IsIndexedT::value
)
1283 fetchInfo
.BaseVertex
= work
.baseVertex
;
1285 // if the entire index buffer isn't being consumed, set the last index
1286 // so that fetches < a SIMD wide will be masked off
1287 fetchInfo
.pLastIndex
= (const int32_t*)(((uint8_t*)state
.indexBuffer
.pIndices
) + state
.indexBuffer
.size
);
1288 if (pLastRequestedIndex
< fetchInfo
.pLastIndex
)
1290 fetchInfo
.pLastIndex
= pLastRequestedIndex
;
1295 fetchInfo
.StartVertex
= work
.startVertex
;
1298 #ifdef KNOB_ENABLE_RDTSC
1299 uint32_t numPrims
= GetNumPrims(state
.topology
, work
.numVerts
);
1302 void* pGsOut
= nullptr;
1303 void* pCutBuffer
= nullptr;
1304 void* pStreamCutBuffer
= nullptr;
1305 if (HasGeometryShaderT::value
)
1307 AllocateGsBuffers(pDC
, state
, &pGsOut
, &pCutBuffer
, &pStreamCutBuffer
);
1310 if (HasTessellationT::value
)
1312 SWR_ASSERT(state
.tsState
.tsEnable
== true);
1313 SWR_ASSERT(state
.pfnHsFunc
!= nullptr);
1314 SWR_ASSERT(state
.pfnDsFunc
!= nullptr);
1316 AllocateTessellationData(pContext
);
1320 SWR_ASSERT(state
.tsState
.tsEnable
== false);
1321 SWR_ASSERT(state
.pfnHsFunc
== nullptr);
1322 SWR_ASSERT(state
.pfnDsFunc
== nullptr);
1325 // allocate space for streamout input prim data
1326 uint32_t* pSoPrimData
= nullptr;
1327 if (HasStreamOutT::value
)
1329 pSoPrimData
= (uint32_t*)pDC
->pArena
->AllocAligned(4096, 16);
1332 // choose primitive assembler
1333 PA_FACTORY
<IsIndexedT
, IsCutIndexEnabledT
> paFactory(pDC
, state
.topology
, work
.numVerts
);
1334 PA_STATE
& pa
= paFactory
.GetPA();
1336 /// @todo: temporarily move instance loop in the FE to ensure SO ordering
1337 for (uint32_t instanceNum
= 0; instanceNum
< work
.numInstances
; instanceNum
++)
1342 if (IsIndexedT::value
)
1344 fetchInfo
.pIndices
= work
.pIB
;
1348 vIndex
= _simd_add_epi32(_simd_set1_epi32(work
.startVertexID
), vScale
);
1349 fetchInfo
.pIndices
= (const int32_t*)&vIndex
;
1352 fetchInfo
.CurInstance
= instanceNum
;
1353 vsContext
.InstanceID
= instanceNum
;
1355 while (pa
.HasWork())
1357 // PaGetNextVsOutput currently has the side effect of updating some PA state machine state.
1358 // So we need to keep this outside of (i < endVertex) check.
1359 simdmask
* pvCutIndices
= nullptr;
1360 if (IsIndexedT::value
)
1362 pvCutIndices
= &pa
.GetNextVsIndices();
1365 simdvertex
& vout
= pa
.GetNextVsOutput();
1366 vsContext
.pVout
= &vout
;
1371 // 1. Execute FS/VS for a single SIMD.
1372 AR_BEGIN(FEFetchShader
, pDC
->drawId
);
1373 state
.pfnFetchFunc(fetchInfo
, vin
);
1374 AR_END(FEFetchShader
, 0);
1376 // forward fetch generated vertex IDs to the vertex shader
1377 vsContext
.VertexID
= fetchInfo
.VertexID
;
1379 // Setup active mask for vertex shader.
1380 vsContext
.mask
= GenerateMask(endVertex
- i
);
1382 // forward cut mask to the PA
1383 if (IsIndexedT::value
)
1385 *pvCutIndices
= _simd_movemask_ps(_simd_castsi_ps(fetchInfo
.CutMask
));
1388 UPDATE_STAT_FE(IaVertices
, GetNumInvocations(i
, endVertex
));
1390 #if KNOB_ENABLE_TOSS_POINTS
1391 if (!KNOB_TOSS_FETCH
)
1394 AR_BEGIN(FEVertexShader
, pDC
->drawId
);
1395 state
.pfnVertexFunc(GetPrivateState(pDC
), &vsContext
);
1396 AR_END(FEVertexShader
, 0);
1398 UPDATE_STAT_FE(VsInvocations
, GetNumInvocations(i
, endVertex
));
1402 // 2. Assemble primitives given the last two SIMD.
1405 simdvector prim
[MAX_NUM_VERTS_PER_PRIM
];
1406 // PaAssemble returns false if there is not enough verts to assemble.
1407 AR_BEGIN(FEPAAssemble
, pDC
->drawId
);
1408 bool assemble
= pa
.Assemble(VERTEX_POSITION_SLOT
, prim
);
1409 AR_END(FEPAAssemble
, 1);
1411 #if KNOB_ENABLE_TOSS_POINTS
1412 if (!KNOB_TOSS_FETCH
)
1415 #if KNOB_ENABLE_TOSS_POINTS
1421 UPDATE_STAT_FE(IaPrimitives
, pa
.NumPrims());
1423 if (HasTessellationT::value
)
1425 TessellationStages
<HasGeometryShaderT
, HasStreamOutT
, HasRastT
>(
1426 pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, pa
.GetPrimID(work
.startPrimID
));
1428 else if (HasGeometryShaderT::value
)
1430 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(
1431 pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, pa
.GetPrimID(work
.startPrimID
));
1435 // If streamout is enabled then stream vertices out to memory.
1436 if (HasStreamOutT::value
)
1438 StreamOut(pDC
, pa
, workerId
, pSoPrimData
, 0);
1441 if (HasRastT::value
)
1443 SWR_ASSERT(pDC
->pState
->pfnProcessPrims
);
1444 pDC
->pState
->pfnProcessPrims(pDC
, pa
, workerId
, prim
,
1445 GenMask(pa
.NumPrims()), pa
.GetPrimID(work
.startPrimID
), _simd_set1_epi32(0));
1451 } while (pa
.NextPrim());
1453 i
+= KNOB_SIMD_WIDTH
;
1454 if (IsIndexedT::value
)
1456 fetchInfo
.pIndices
= (int*)((uint8_t*)fetchInfo
.pIndices
+ KNOB_SIMD_WIDTH
* indexSize
);
1460 vIndex
= _simd_add_epi32(vIndex
, _simd_set1_epi32(KNOB_SIMD_WIDTH
));
1466 AR_END(FEProcessDraw
, numPrims
* work
.numInstances
);
1469 struct FEDrawChooser
1471 typedef PFN_FE_WORK_FUNC FuncType
;
1473 template <typename
... ArgsB
>
1474 static FuncType
GetFunc()
1476 return ProcessDraw
<ArgsB
...>;
1481 // Selector for correct templated Draw front-end function
1482 PFN_FE_WORK_FUNC
GetProcessDrawFunc(
1484 bool IsCutIndexEnabled
,
1485 bool HasTessellation
,
1486 bool HasGeometryShader
,
1488 bool HasRasterization
)
1490 return TemplateArgUnroller
<FEDrawChooser
>::GetFunc(IsIndexed
, IsCutIndexEnabled
, HasTessellation
, HasGeometryShader
, HasStreamOut
, HasRasterization
);
1493 //////////////////////////////////////////////////////////////////////////
1494 /// @brief Processes attributes for the backend based on linkage mask and
1495 /// linkage map. Essentially just doing an SOA->AOS conversion and pack.
1496 /// @param pDC - Draw context
1497 /// @param pa - Primitive Assembly state
1498 /// @param linkageMask - Specifies which VS outputs are routed to PS.
1499 /// @param pLinkageMap - maps VS attribute slot to PS slot
1500 /// @param triIndex - Triangle to process attributes for
1501 /// @param pBuffer - Output result
1502 template<typename NumVertsT
, typename IsSwizzledT
, typename HasConstantInterpT
, typename IsDegenerate
>
1503 INLINE
void ProcessAttributes(
1510 static_assert(NumVertsT::value
> 0 && NumVertsT::value
<= 3, "Invalid value for NumVertsT");
1511 const SWR_BACKEND_STATE
& backendState
= pDC
->pState
->state
.backendState
;
1512 // Conservative Rasterization requires degenerate tris to have constant attribute interpolation
1513 LONG constantInterpMask
= IsDegenerate::value
? 0xFFFFFFFF : backendState
.constantInterpolationMask
;
1514 const uint32_t provokingVertex
= pDC
->pState
->state
.frontendState
.topologyProvokingVertex
;
1515 const PRIMITIVE_TOPOLOGY topo
= pDC
->pState
->state
.topology
;
1517 static const float constTable
[3][4] = {
1518 {0.0f
, 0.0f
, 0.0f
, 0.0f
},
1519 {0.0f
, 0.0f
, 0.0f
, 1.0f
},
1520 {1.0f
, 1.0f
, 1.0f
, 1.0f
}
1523 for (uint32_t i
= 0; i
< backendState
.numAttributes
; ++i
)
1526 if (IsSwizzledT::value
)
1528 SWR_ATTRIB_SWIZZLE attribSwizzle
= backendState
.swizzleMap
[i
];
1529 inputSlot
= VERTEX_ATTRIB_START_SLOT
+ attribSwizzle
.sourceAttrib
;
1534 inputSlot
= VERTEX_ATTRIB_START_SLOT
+ i
;
1537 __m128 attrib
[3]; // triangle attribs (always 4 wide)
1538 float* pAttribStart
= pBuffer
;
1540 if (HasConstantInterpT::value
|| IsDegenerate::value
)
1542 if (_bittest(&constantInterpMask
, i
))
1545 uint32_t adjustedTriIndex
;
1546 static const uint32_t tristripProvokingVertex
[] = { 0, 2, 1 };
1547 static const int32_t quadProvokingTri
[2][4] = { {0, 0, 0, 1}, {0, -1, 0, 0} };
1548 static const uint32_t quadProvokingVertex
[2][4] = { {0, 1, 2, 2}, {0, 1, 1, 2} };
1549 static const int32_t qstripProvokingTri
[2][4] = { {0, 0, 0, 1}, {-1, 0, 0, 0} };
1550 static const uint32_t qstripProvokingVertex
[2][4] = { {0, 1, 2, 1}, {0, 0, 2, 1} };
1554 adjustedTriIndex
= triIndex
+ quadProvokingTri
[triIndex
& 1][provokingVertex
];
1555 vid
= quadProvokingVertex
[triIndex
& 1][provokingVertex
];
1557 case TOP_QUAD_STRIP
:
1558 adjustedTriIndex
= triIndex
+ qstripProvokingTri
[triIndex
& 1][provokingVertex
];
1559 vid
= qstripProvokingVertex
[triIndex
& 1][provokingVertex
];
1561 case TOP_TRIANGLE_STRIP
:
1562 adjustedTriIndex
= triIndex
;
1563 vid
= (triIndex
& 1)
1564 ? tristripProvokingVertex
[provokingVertex
]
1568 adjustedTriIndex
= triIndex
;
1569 vid
= provokingVertex
;
1573 pa
.AssembleSingle(inputSlot
, adjustedTriIndex
, attrib
);
1575 for (uint32_t i
= 0; i
< NumVertsT::value
; ++i
)
1577 _mm_store_ps(pBuffer
, attrib
[vid
]);
1583 pa
.AssembleSingle(inputSlot
, triIndex
, attrib
);
1585 for (uint32_t i
= 0; i
< NumVertsT::value
; ++i
)
1587 _mm_store_ps(pBuffer
, attrib
[i
]);
1594 pa
.AssembleSingle(inputSlot
, triIndex
, attrib
);
1596 for (uint32_t i
= 0; i
< NumVertsT::value
; ++i
)
1598 _mm_store_ps(pBuffer
, attrib
[i
]);
1603 // pad out the attrib buffer to 3 verts to ensure the triangle
1604 // interpolation code in the pixel shader works correctly for the
1605 // 3 topologies - point, line, tri. This effectively zeros out the
1606 // effect of the missing vertices in the triangle interpolation.
1607 for (uint32_t v
= NumVertsT::value
; v
< 3; ++v
)
1609 _mm_store_ps(pBuffer
, attrib
[NumVertsT::value
- 1]);
1613 // check for constant source overrides
1614 if (IsSwizzledT::value
)
1616 uint32_t mask
= backendState
.swizzleMap
[i
].componentOverrideMask
;
1620 while (_BitScanForward(&comp
, mask
))
1622 mask
&= ~(1 << comp
);
1624 float constantValue
= 0.0f
;
1625 switch ((SWR_CONSTANT_SOURCE
)backendState
.swizzleMap
[i
].constantSource
)
1627 case SWR_CONSTANT_SOURCE_CONST_0000
:
1628 case SWR_CONSTANT_SOURCE_CONST_0001_FLOAT
:
1629 case SWR_CONSTANT_SOURCE_CONST_1111_FLOAT
:
1630 constantValue
= constTable
[backendState
.swizzleMap
[i
].constantSource
][comp
];
1632 case SWR_CONSTANT_SOURCE_PRIM_ID
:
1633 constantValue
= *(float*)&primId
;
1637 // apply constant value to all 3 vertices
1638 for (uint32_t v
= 0; v
< 3; ++v
)
1640 pAttribStart
[comp
+ v
* 4] = constantValue
;
1649 typedef void(*PFN_PROCESS_ATTRIBUTES
)(DRAW_CONTEXT
*, PA_STATE
&, uint32_t, uint32_t, float*);
1651 struct ProcessAttributesChooser
1653 typedef PFN_PROCESS_ATTRIBUTES FuncType
;
1655 template <typename
... ArgsB
>
1656 static FuncType
GetFunc()
1658 return ProcessAttributes
<ArgsB
...>;
1662 PFN_PROCESS_ATTRIBUTES
GetProcessAttributesFunc(uint32_t NumVerts
, bool IsSwizzled
, bool HasConstantInterp
, bool IsDegenerate
= false)
1664 return TemplateArgUnroller
<ProcessAttributesChooser
>::GetFunc(IntArg
<1, 3>{NumVerts
}, IsSwizzled
, HasConstantInterp
, IsDegenerate
);
1667 //////////////////////////////////////////////////////////////////////////
1668 /// @brief Processes enabled user clip distances. Loads the active clip
1669 /// distances from the PA, sets up barycentric equations, and
1670 /// stores the results to the output buffer
1671 /// @param pa - Primitive Assembly state
1672 /// @param primIndex - primitive index to process
1673 /// @param clipDistMask - mask of enabled clip distances
1674 /// @param pUserClipBuffer - buffer to store results
1675 template<uint32_t NumVerts
>
1676 void ProcessUserClipDist(PA_STATE
& pa
, uint32_t primIndex
, uint8_t clipDistMask
, float* pUserClipBuffer
)
1679 while (_BitScanForward(&clipDist
, clipDistMask
))
1681 clipDistMask
&= ~(1 << clipDist
);
1682 uint32_t clipSlot
= clipDist
>> 2;
1683 uint32_t clipComp
= clipDist
& 0x3;
1684 uint32_t clipAttribSlot
= clipSlot
== 0 ?
1685 VERTEX_CLIPCULL_DIST_LO_SLOT
: VERTEX_CLIPCULL_DIST_HI_SLOT
;
1687 __m128 primClipDist
[3];
1688 pa
.AssembleSingle(clipAttribSlot
, primIndex
, primClipDist
);
1690 float vertClipDist
[NumVerts
];
1691 for (uint32_t e
= 0; e
< NumVerts
; ++e
)
1693 OSALIGNSIMD(float) aVertClipDist
[4];
1694 _mm_store_ps(aVertClipDist
, primClipDist
[e
]);
1695 vertClipDist
[e
] = aVertClipDist
[clipComp
];
1698 // setup plane equations for barycentric interpolation in the backend
1699 float baryCoeff
[NumVerts
];
1700 for (uint32_t e
= 0; e
< NumVerts
- 1; ++e
)
1702 baryCoeff
[e
] = vertClipDist
[e
] - vertClipDist
[NumVerts
- 1];
1704 baryCoeff
[NumVerts
- 1] = vertClipDist
[NumVerts
- 1];
1706 for (uint32_t e
= 0; e
< NumVerts
; ++e
)
1708 *(pUserClipBuffer
++) = baryCoeff
[e
];
1713 //////////////////////////////////////////////////////////////////////////
1714 /// @brief Convert the X,Y coords of a triangle to the requested Fixed
1715 /// Point precision from FP32.
1716 template <typename PT
= FixedPointTraits
<Fixed_16_8
>>
1717 INLINE simdscalari
fpToFixedPointVertical(const simdscalar vIn
)
1719 simdscalar vFixed
= _simd_mul_ps(vIn
, _simd_set1_ps(PT::ScaleT::value
));
1720 return _simd_cvtps_epi32(vFixed
);
1723 //////////////////////////////////////////////////////////////////////////
1724 /// @brief Helper function to set the X,Y coords of a triangle to the
1725 /// requested Fixed Point precision from FP32.
1726 /// @param tri: simdvector[3] of FP triangle verts
1727 /// @param vXi: fixed point X coords of tri verts
1728 /// @param vYi: fixed point Y coords of tri verts
1729 INLINE
static void FPToFixedPoint(const simdvector
* const tri
, simdscalari (&vXi
)[3], simdscalari (&vYi
)[3])
1731 vXi
[0] = fpToFixedPointVertical(tri
[0].x
);
1732 vYi
[0] = fpToFixedPointVertical(tri
[0].y
);
1733 vXi
[1] = fpToFixedPointVertical(tri
[1].x
);
1734 vYi
[1] = fpToFixedPointVertical(tri
[1].y
);
1735 vXi
[2] = fpToFixedPointVertical(tri
[2].x
);
1736 vYi
[2] = fpToFixedPointVertical(tri
[2].y
);
1739 //////////////////////////////////////////////////////////////////////////
1740 /// @brief Calculate bounding box for current triangle
1741 /// @tparam CT: ConservativeRastFETraits type
1742 /// @param vX: fixed point X position for triangle verts
1743 /// @param vY: fixed point Y position for triangle verts
1744 /// @param bbox: fixed point bbox
1745 /// *Note*: expects vX, vY to be in the correct precision for the type
1746 /// of rasterization. This avoids unnecessary FP->fixed conversions.
1747 template <typename CT
>
1748 INLINE
void calcBoundingBoxIntVertical(const simdvector
* const tri
, simdscalari (&vX
)[3], simdscalari (&vY
)[3], simdBBox
&bbox
)
1750 simdscalari vMinX
= vX
[0];
1751 vMinX
= _simd_min_epi32(vMinX
, vX
[1]);
1752 vMinX
= _simd_min_epi32(vMinX
, vX
[2]);
1754 simdscalari vMaxX
= vX
[0];
1755 vMaxX
= _simd_max_epi32(vMaxX
, vX
[1]);
1756 vMaxX
= _simd_max_epi32(vMaxX
, vX
[2]);
1758 simdscalari vMinY
= vY
[0];
1759 vMinY
= _simd_min_epi32(vMinY
, vY
[1]);
1760 vMinY
= _simd_min_epi32(vMinY
, vY
[2]);
1762 simdscalari vMaxY
= vY
[0];
1763 vMaxY
= _simd_max_epi32(vMaxY
, vY
[1]);
1764 vMaxY
= _simd_max_epi32(vMaxY
, vY
[2]);
1772 //////////////////////////////////////////////////////////////////////////
1773 /// @brief FEConservativeRastT specialization of calcBoundingBoxIntVertical
1774 /// Offsets BBox for conservative rast
1776 INLINE
void calcBoundingBoxIntVertical
<FEConservativeRastT
>(const simdvector
* const tri
, simdscalari (&vX
)[3], simdscalari (&vY
)[3], simdBBox
&bbox
)
1778 // FE conservative rast traits
1779 typedef FEConservativeRastT CT
;
1781 simdscalari vMinX
= vX
[0];
1782 vMinX
= _simd_min_epi32(vMinX
, vX
[1]);
1783 vMinX
= _simd_min_epi32(vMinX
, vX
[2]);
1785 simdscalari vMaxX
= vX
[0];
1786 vMaxX
= _simd_max_epi32(vMaxX
, vX
[1]);
1787 vMaxX
= _simd_max_epi32(vMaxX
, vX
[2]);
1789 simdscalari vMinY
= vY
[0];
1790 vMinY
= _simd_min_epi32(vMinY
, vY
[1]);
1791 vMinY
= _simd_min_epi32(vMinY
, vY
[2]);
1793 simdscalari vMaxY
= vY
[0];
1794 vMaxY
= _simd_max_epi32(vMaxY
, vY
[1]);
1795 vMaxY
= _simd_max_epi32(vMaxY
, vY
[2]);
1797 /// Bounding box needs to be expanded by 1/512 before snapping to 16.8 for conservative rasterization
1798 /// expand bbox by 1/256; coverage will be correctly handled in the rasterizer.
1799 bbox
.xmin
= _simd_sub_epi32(vMinX
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1800 bbox
.xmax
= _simd_add_epi32(vMaxX
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1801 bbox
.ymin
= _simd_sub_epi32(vMinY
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1802 bbox
.ymax
= _simd_add_epi32(vMaxY
, _simd_set1_epi32(CT::BoundingBoxOffsetT::value
));
1805 //////////////////////////////////////////////////////////////////////////
1806 /// @brief Bin triangle primitives to macro tiles. Performs setup, clipping
1807 /// culling, viewport transform, etc.
1808 /// @param pDC - pointer to draw context.
1809 /// @param pa - The primitive assembly object.
1810 /// @param workerId - thread's worker id. Even thread has a unique id.
1811 /// @param tri - Contains triangle position data for SIMDs worth of triangles.
1812 /// @param primID - Primitive ID for each triangle.
1813 /// @param viewportIdx - viewport array index for each triangle.
1814 /// @tparam CT - ConservativeRastFETraits
1815 template <typename CT
>
1823 simdscalari viewportIdx
)
1825 SWR_CONTEXT
*pContext
= pDC
->pContext
;
1827 AR_BEGIN(FEBinTriangles
, pDC
->drawId
);
1829 const API_STATE
& state
= GetApiState(pDC
);
1830 const SWR_RASTSTATE
& rastState
= state
.rastState
;
1831 const SWR_FRONTEND_STATE
& feState
= state
.frontendState
;
1832 const SWR_GS_STATE
& gsState
= state
.gsState
;
1833 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
1836 simdscalar vRecipW0
= _simd_set1_ps(1.0f
);
1837 simdscalar vRecipW1
= _simd_set1_ps(1.0f
);
1838 simdscalar vRecipW2
= _simd_set1_ps(1.0f
);
1840 if (feState
.vpTransformDisable
)
1842 // RHW is passed in directly when VP transform is disabled
1843 vRecipW0
= tri
[0].v
[3];
1844 vRecipW1
= tri
[1].v
[3];
1845 vRecipW2
= tri
[2].v
[3];
1849 // Perspective divide
1850 vRecipW0
= _simd_div_ps(_simd_set1_ps(1.0f
), tri
[0].w
);
1851 vRecipW1
= _simd_div_ps(_simd_set1_ps(1.0f
), tri
[1].w
);
1852 vRecipW2
= _simd_div_ps(_simd_set1_ps(1.0f
), tri
[2].w
);
1854 tri
[0].v
[0] = _simd_mul_ps(tri
[0].v
[0], vRecipW0
);
1855 tri
[1].v
[0] = _simd_mul_ps(tri
[1].v
[0], vRecipW1
);
1856 tri
[2].v
[0] = _simd_mul_ps(tri
[2].v
[0], vRecipW2
);
1858 tri
[0].v
[1] = _simd_mul_ps(tri
[0].v
[1], vRecipW0
);
1859 tri
[1].v
[1] = _simd_mul_ps(tri
[1].v
[1], vRecipW1
);
1860 tri
[2].v
[1] = _simd_mul_ps(tri
[2].v
[1], vRecipW2
);
1862 tri
[0].v
[2] = _simd_mul_ps(tri
[0].v
[2], vRecipW0
);
1863 tri
[1].v
[2] = _simd_mul_ps(tri
[1].v
[2], vRecipW1
);
1864 tri
[2].v
[2] = _simd_mul_ps(tri
[2].v
[2], vRecipW2
);
1866 // Viewport transform to screen space coords
1867 if (state
.gsState
.emitsViewportArrayIndex
)
1869 viewportTransform
<3>(tri
, state
.vpMatrices
, viewportIdx
);
1873 viewportTransform
<3>(tri
, state
.vpMatrices
);
1877 // Adjust for pixel center location
1878 simdscalar offset
= g_pixelOffsets
[rastState
.pixelLocation
];
1879 tri
[0].x
= _simd_add_ps(tri
[0].x
, offset
);
1880 tri
[0].y
= _simd_add_ps(tri
[0].y
, offset
);
1882 tri
[1].x
= _simd_add_ps(tri
[1].x
, offset
);
1883 tri
[1].y
= _simd_add_ps(tri
[1].y
, offset
);
1885 tri
[2].x
= _simd_add_ps(tri
[2].x
, offset
);
1886 tri
[2].y
= _simd_add_ps(tri
[2].y
, offset
);
1888 simdscalari vXi
[3], vYi
[3];
1889 // Set vXi, vYi to required fixed point precision
1890 FPToFixedPoint(tri
, vXi
, vYi
);
1893 simdscalari vAi
[3], vBi
[3];
1894 triangleSetupABIntVertical(vXi
, vYi
, vAi
, vBi
);
1897 simdscalari vDet
[2];
1898 calcDeterminantIntVertical(vAi
, vBi
, vDet
);
1901 int maskLo
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpeq_epi64(vDet
[0], _simd_setzero_si())));
1902 int maskHi
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpeq_epi64(vDet
[1], _simd_setzero_si())));
1904 int cullZeroAreaMask
= maskLo
| (maskHi
<< (KNOB_SIMD_WIDTH
/ 2));
1906 uint32_t origTriMask
= triMask
;
1907 // don't cull degenerate triangles if we're conservatively rasterizing
1908 if(!CT::IsConservativeT::value
)
1910 triMask
&= ~cullZeroAreaMask
;
1913 // determine front winding tris
1915 // CCW det <= 0; 0 area triangles are marked as backfacing, which is required behavior for conservative rast
1916 maskLo
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpgt_epi64(vDet
[0], _simd_setzero_si())));
1917 maskHi
= _simd_movemask_pd(_simd_castsi_pd(_simd_cmpgt_epi64(vDet
[1], _simd_setzero_si())));
1918 int cwTriMask
= maskLo
| (maskHi
<< (KNOB_SIMD_WIDTH
/2) );
1920 uint32_t frontWindingTris
;
1921 if (rastState
.frontWinding
== SWR_FRONTWINDING_CW
)
1923 frontWindingTris
= cwTriMask
;
1927 frontWindingTris
= ~cwTriMask
;
1932 switch ((SWR_CULLMODE
)rastState
.cullMode
)
1934 case SWR_CULLMODE_BOTH
: cullTris
= 0xffffffff; break;
1935 case SWR_CULLMODE_NONE
: cullTris
= 0x0; break;
1936 case SWR_CULLMODE_FRONT
: cullTris
= frontWindingTris
; break;
1937 // 0 area triangles are marked as backfacing, which is required behavior for conservative rast
1938 case SWR_CULLMODE_BACK
: cullTris
= ~frontWindingTris
; break;
1939 default: SWR_ASSERT(false, "Invalid cull mode: %d", rastState
.cullMode
); cullTris
= 0x0; break;
1942 triMask
&= ~cullTris
;
1944 if (origTriMask
^ triMask
)
1946 RDTSC_EVENT(FECullZeroAreaAndBackface
, _mm_popcnt_u32(origTriMask
^ triMask
), 0);
1949 /// Note: these variable initializations must stay above any 'goto endBenTriangles'
1950 // compute per tri backface
1951 uint32_t frontFaceMask
= frontWindingTris
;
1952 uint32_t *pPrimID
= (uint32_t *)&primID
;
1953 const uint32_t *pViewportIndex
= (uint32_t *)&viewportIdx
;
1955 // for center sample pattern, all samples are at pixel center; calculate coverage
1956 // once at center and broadcast the results in the backend
1957 const SWR_MULTISAMPLE_COUNT sampleCount
= (rastState
.samplePattern
== SWR_MSAA_STANDARD_PATTERN
) ? rastState
.sampleCount
: SWR_MULTISAMPLE_1X
;
1958 uint32_t edgeEnable
;
1959 PFN_WORK_FUNC pfnWork
;
1960 if(CT::IsConservativeT::value
)
1962 // determine which edges of the degenerate tri, if any, are valid to rasterize.
1963 // used to call the appropriate templated rasterizer function
1964 if(cullZeroAreaMask
> 0)
1967 simdscalari x0x1Mask
= _simd_cmpeq_epi32(vXi
[0], vXi
[1]);
1968 simdscalari y0y1Mask
= _simd_cmpeq_epi32(vYi
[0], vYi
[1]);
1969 uint32_t e0Mask
= _simd_movemask_ps(_simd_castsi_ps(_simd_and_si(x0x1Mask
, y0y1Mask
)));
1972 simdscalari x1x2Mask
= _simd_cmpeq_epi32(vXi
[1], vXi
[2]);
1973 simdscalari y1y2Mask
= _simd_cmpeq_epi32(vYi
[1], vYi
[2]);
1974 uint32_t e1Mask
= _simd_movemask_ps(_simd_castsi_ps(_simd_and_si(x1x2Mask
, y1y2Mask
)));
1977 // if v0 == v1 & v1 == v2, v0 == v2
1978 uint32_t e2Mask
= e0Mask
& e1Mask
;
1979 SWR_ASSERT(KNOB_SIMD_WIDTH
== 8, "Need to update degenerate mask code for avx512");
1981 // edge order: e0 = v0v1, e1 = v1v2, e2 = v0v2
1982 // 32 bit binary: 0000 0000 0010 0100 1001 0010 0100 1001
1983 e0Mask
= pdep_u32(e0Mask
, 0x00249249);
1984 // 32 bit binary: 0000 0000 0100 1001 0010 0100 1001 0010
1985 e1Mask
= pdep_u32(e1Mask
, 0x00492492);
1986 // 32 bit binary: 0000 0000 1001 0010 0100 1001 0010 0100
1987 e2Mask
= pdep_u32(e2Mask
, 0x00924924);
1989 edgeEnable
= (0x00FFFFFF & (~(e0Mask
| e1Mask
| e2Mask
)));
1993 edgeEnable
= 0x00FFFFFF;
1998 // degenerate triangles won't be sent to rasterizer; just enable all edges
1999 pfnWork
= GetRasterizerFunc(sampleCount
, (rastState
.conservativeRast
> 0),
2000 (SWR_INPUT_COVERAGE
)pDC
->pState
->state
.psState
.inputCoverage
, ALL_EDGES_VALID
,
2001 (state
.scissorsTileAligned
== false));
2006 goto endBinTriangles
;
2009 // Calc bounding box of triangles
2011 calcBoundingBoxIntVertical
<CT
>(tri
, vXi
, vYi
, bbox
);
2013 // determine if triangle falls between pixel centers and discard
2014 // only discard for non-MSAA case and when conservative rast is disabled
2015 // (xmin + 127) & ~255
2016 // (xmax + 128) & ~255
2017 if(rastState
.sampleCount
== SWR_MULTISAMPLE_1X
&& (!CT::IsConservativeT::value
))
2019 origTriMask
= triMask
;
2023 simdscalari xmin
= _simd_add_epi32(bbox
.xmin
, _simd_set1_epi32(127));
2024 xmin
= _simd_and_si(xmin
, _simd_set1_epi32(~255));
2025 simdscalari xmax
= _simd_add_epi32(bbox
.xmax
, _simd_set1_epi32(128));
2026 xmax
= _simd_and_si(xmax
, _simd_set1_epi32(~255));
2028 simdscalari vMaskH
= _simd_cmpeq_epi32(xmin
, xmax
);
2030 simdscalari ymin
= _simd_add_epi32(bbox
.ymin
, _simd_set1_epi32(127));
2031 ymin
= _simd_and_si(ymin
, _simd_set1_epi32(~255));
2032 simdscalari ymax
= _simd_add_epi32(bbox
.ymax
, _simd_set1_epi32(128));
2033 ymax
= _simd_and_si(ymax
, _simd_set1_epi32(~255));
2035 simdscalari vMaskV
= _simd_cmpeq_epi32(ymin
, ymax
);
2036 vMaskV
= _simd_or_si(vMaskH
, vMaskV
);
2037 cullCenterMask
= _simd_movemask_ps(_simd_castsi_ps(vMaskV
));
2040 triMask
&= ~cullCenterMask
;
2042 if(origTriMask
^ triMask
)
2044 RDTSC_EVENT(FECullBetweenCenters
, _mm_popcnt_u32(origTriMask
^ triMask
), 0);
2048 // Intersect with scissor/viewport. Subtract 1 ULP in x.8 fixed point since xmax/ymax edge is exclusive.
2049 // Gather the AOS effective scissor rects based on the per-prim VP index.
2050 /// @todo: Look at speeding this up -- weigh against corresponding costs in rasterizer.
2051 simdscalari scisXmin
, scisYmin
, scisXmax
, scisYmax
;
2052 if (state
.gsState
.emitsViewportArrayIndex
)
2054 GatherScissors
<KNOB_SIMD_WIDTH
>::Gather(&state
.scissorsInFixedPoint
[0], pViewportIndex
,
2055 scisXmin
, scisYmin
, scisXmax
, scisYmax
);
2057 else // broadcast fast path for non-VPAI case.
2059 scisXmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmin
);
2060 scisYmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymin
);
2061 scisXmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmax
);
2062 scisYmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymax
);
2065 bbox
.xmin
= _simd_max_epi32(bbox
.xmin
, scisXmin
);
2066 bbox
.ymin
= _simd_max_epi32(bbox
.ymin
, scisYmin
);
2067 bbox
.xmax
= _simd_min_epi32(_simd_sub_epi32(bbox
.xmax
, _simd_set1_epi32(1)), scisXmax
);
2068 bbox
.ymax
= _simd_min_epi32(_simd_sub_epi32(bbox
.ymax
, _simd_set1_epi32(1)), scisYmax
);
2070 if(CT::IsConservativeT::value
)
2072 // in the case where a degenerate triangle is on a scissor edge, we need to make sure the primitive bbox has
2073 // some area. Bump the xmax/ymax edges out
2074 simdscalari topEqualsBottom
= _simd_cmpeq_epi32(bbox
.ymin
, bbox
.ymax
);
2075 bbox
.ymax
= _simd_blendv_epi32(bbox
.ymax
, _simd_add_epi32(bbox
.ymax
, _simd_set1_epi32(1)), topEqualsBottom
);
2076 simdscalari leftEqualsRight
= _simd_cmpeq_epi32(bbox
.xmin
, bbox
.xmax
);
2077 bbox
.xmax
= _simd_blendv_epi32(bbox
.xmax
, _simd_add_epi32(bbox
.xmax
, _simd_set1_epi32(1)), leftEqualsRight
);
2080 // Cull tris completely outside scissor
2082 simdscalari maskOutsideScissorX
= _simd_cmpgt_epi32(bbox
.xmin
, bbox
.xmax
);
2083 simdscalari maskOutsideScissorY
= _simd_cmpgt_epi32(bbox
.ymin
, bbox
.ymax
);
2084 simdscalari maskOutsideScissorXY
= _simd_or_si(maskOutsideScissorX
, maskOutsideScissorY
);
2085 uint32_t maskOutsideScissor
= _simd_movemask_ps(_simd_castsi_ps(maskOutsideScissorXY
));
2086 triMask
= triMask
& ~maskOutsideScissor
;
2091 goto endBinTriangles
;
2094 // Convert triangle bbox to macrotile units.
2095 bbox
.xmin
= _simd_srai_epi32(bbox
.xmin
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2096 bbox
.ymin
= _simd_srai_epi32(bbox
.ymin
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2097 bbox
.xmax
= _simd_srai_epi32(bbox
.xmax
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2098 bbox
.ymax
= _simd_srai_epi32(bbox
.ymax
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2100 OSALIGNSIMD(uint32_t) aMTLeft
[KNOB_SIMD_WIDTH
], aMTRight
[KNOB_SIMD_WIDTH
], aMTTop
[KNOB_SIMD_WIDTH
], aMTBottom
[KNOB_SIMD_WIDTH
];
2101 _simd_store_si((simdscalari
*)aMTLeft
, bbox
.xmin
);
2102 _simd_store_si((simdscalari
*)aMTRight
, bbox
.xmax
);
2103 _simd_store_si((simdscalari
*)aMTTop
, bbox
.ymin
);
2104 _simd_store_si((simdscalari
*)aMTBottom
, bbox
.ymax
);
2106 // transpose verts needed for backend
2107 /// @todo modify BE to take non-transformed verts
2108 __m128 vHorizX
[8], vHorizY
[8], vHorizZ
[8], vHorizW
[8];
2109 vTranspose3x8(vHorizX
, tri
[0].x
, tri
[1].x
, tri
[2].x
);
2110 vTranspose3x8(vHorizY
, tri
[0].y
, tri
[1].y
, tri
[2].y
);
2111 vTranspose3x8(vHorizZ
, tri
[0].z
, tri
[1].z
, tri
[2].z
);
2112 vTranspose3x8(vHorizW
, vRecipW0
, vRecipW1
, vRecipW2
);
2114 // store render target array index
2115 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2116 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2118 simdvector vRtai
[3];
2119 pa
.Assemble(VERTEX_RTAI_SLOT
, vRtai
);
2121 vRtaii
= _simd_castps_si(vRtai
[0].x
);
2122 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2126 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2129 // scan remaining valid triangles and bin each separately
2130 while (_BitScanForward(&triIndex
, triMask
))
2132 uint32_t linkageCount
= state
.backendState
.numAttributes
;
2133 uint32_t numScalarAttribs
= linkageCount
* 4;
2139 if(CT::IsConservativeT::value
)
2141 // only rasterize valid edges if we have a degenerate primitive
2142 int32_t triEdgeEnable
= (edgeEnable
>> (triIndex
* 3)) & ALL_EDGES_VALID
;
2143 work
.pfnWork
= GetRasterizerFunc(sampleCount
, (rastState
.conservativeRast
> 0),
2144 (SWR_INPUT_COVERAGE
)pDC
->pState
->state
.psState
.inputCoverage
, triEdgeEnable
,
2145 (state
.scissorsTileAligned
== false));
2147 // Degenerate triangles are required to be constant interpolated
2148 isDegenerate
= (triEdgeEnable
!= ALL_EDGES_VALID
) ? true : false;
2152 isDegenerate
= false;
2153 work
.pfnWork
= pfnWork
;
2156 // Select attribute processor
2157 PFN_PROCESS_ATTRIBUTES pfnProcessAttribs
= GetProcessAttributesFunc(3,
2158 state
.backendState
.swizzleEnable
, state
.backendState
.constantInterpolationMask
, isDegenerate
);
2160 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2162 desc
.triFlags
.frontFacing
= state
.forceFront
? 1 : ((frontFaceMask
>> triIndex
) & 1);
2163 desc
.triFlags
.primID
= pPrimID
[triIndex
];
2164 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[triIndex
];
2165 desc
.triFlags
.viewportIndex
= pViewportIndex
[triIndex
];
2167 auto pArena
= pDC
->pArena
;
2168 SWR_ASSERT(pArena
!= nullptr);
2170 // store active attribs
2171 float *pAttribs
= (float*)pArena
->AllocAligned(numScalarAttribs
* 3 * sizeof(float), 16);
2172 desc
.pAttribs
= pAttribs
;
2173 desc
.numAttribs
= linkageCount
;
2174 pfnProcessAttribs(pDC
, pa
, triIndex
, pPrimID
[triIndex
], desc
.pAttribs
);
2176 // store triangle vertex data
2177 desc
.pTriBuffer
= (float*)pArena
->AllocAligned(4 * 4 * sizeof(float), 16);
2179 _mm_store_ps(&desc
.pTriBuffer
[0], vHorizX
[triIndex
]);
2180 _mm_store_ps(&desc
.pTriBuffer
[4], vHorizY
[triIndex
]);
2181 _mm_store_ps(&desc
.pTriBuffer
[8], vHorizZ
[triIndex
]);
2182 _mm_store_ps(&desc
.pTriBuffer
[12], vHorizW
[triIndex
]);
2184 // store user clip distances
2185 if (rastState
.clipDistanceMask
)
2187 uint32_t numClipDist
= _mm_popcnt_u32(rastState
.clipDistanceMask
);
2188 desc
.pUserClipBuffer
= (float*)pArena
->Alloc(numClipDist
* 3 * sizeof(float));
2189 ProcessUserClipDist
<3>(pa
, triIndex
, rastState
.clipDistanceMask
, desc
.pUserClipBuffer
);
2192 for (uint32_t y
= aMTTop
[triIndex
]; y
<= aMTBottom
[triIndex
]; ++y
)
2194 for (uint32_t x
= aMTLeft
[triIndex
]; x
<= aMTRight
[triIndex
]; ++x
)
2196 #if KNOB_ENABLE_TOSS_POINTS
2197 if (!KNOB_TOSS_SETUP_TRIS
)
2200 pTileMgr
->enqueue(x
, y
, &work
);
2204 triMask
&= ~(1 << triIndex
);
2208 AR_END(FEBinTriangles
, 1);
2211 struct FEBinTrianglesChooser
2213 typedef PFN_PROCESS_PRIMS FuncType
;
2215 template <typename
... ArgsB
>
2216 static FuncType
GetFunc()
2218 return BinTriangles
<ConservativeRastFETraits
<ArgsB
...>>;
2222 // Selector for correct templated BinTrinagles function
2223 PFN_PROCESS_PRIMS
GetBinTrianglesFunc(bool IsConservative
)
2225 return TemplateArgUnroller
<FEBinTrianglesChooser
>::GetFunc(IsConservative
);
2228 //////////////////////////////////////////////////////////////////////////
2229 /// @brief Bin SIMD points to the backend. Only supports point size of 1
2230 /// @param pDC - pointer to draw context.
2231 /// @param pa - The primitive assembly object.
2232 /// @param workerId - thread's worker id. Even thread has a unique id.
2233 /// @param tri - Contains point position data for SIMDs worth of points.
2234 /// @param primID - Primitive ID for each point.
2242 simdscalari viewportIdx
)
2244 SWR_CONTEXT
*pContext
= pDC
->pContext
;
2246 AR_BEGIN(FEBinPoints
, pDC
->drawId
);
2248 simdvector
& primVerts
= prim
[0];
2250 const API_STATE
& state
= GetApiState(pDC
);
2251 const SWR_FRONTEND_STATE
& feState
= state
.frontendState
;
2252 const SWR_GS_STATE
& gsState
= state
.gsState
;
2253 const SWR_RASTSTATE
& rastState
= state
.rastState
;
2254 const uint32_t *pViewportIndex
= (uint32_t *)&viewportIdx
;
2256 // Select attribute processor
2257 PFN_PROCESS_ATTRIBUTES pfnProcessAttribs
= GetProcessAttributesFunc(1,
2258 state
.backendState
.swizzleEnable
, state
.backendState
.constantInterpolationMask
);
2260 if (!feState
.vpTransformDisable
)
2262 // perspective divide
2263 simdscalar vRecipW0
= _simd_div_ps(_simd_set1_ps(1.0f
), primVerts
.w
);
2264 primVerts
.x
= _simd_mul_ps(primVerts
.x
, vRecipW0
);
2265 primVerts
.y
= _simd_mul_ps(primVerts
.y
, vRecipW0
);
2266 primVerts
.z
= _simd_mul_ps(primVerts
.z
, vRecipW0
);
2268 // viewport transform to screen coords
2269 if (state
.gsState
.emitsViewportArrayIndex
)
2271 viewportTransform
<1>(&primVerts
, state
.vpMatrices
, viewportIdx
);
2275 viewportTransform
<1>(&primVerts
, state
.vpMatrices
);
2279 // adjust for pixel center location
2280 simdscalar offset
= g_pixelOffsets
[rastState
.pixelLocation
];
2281 primVerts
.x
= _simd_add_ps(primVerts
.x
, offset
);
2282 primVerts
.y
= _simd_add_ps(primVerts
.y
, offset
);
2284 // convert to fixed point
2285 simdscalari vXi
, vYi
;
2286 vXi
= fpToFixedPointVertical(primVerts
.x
);
2287 vYi
= fpToFixedPointVertical(primVerts
.y
);
2289 if (CanUseSimplePoints(pDC
))
2291 // adjust for ymin-xmin rule
2292 vXi
= _simd_sub_epi32(vXi
, _simd_set1_epi32(1));
2293 vYi
= _simd_sub_epi32(vYi
, _simd_set1_epi32(1));
2295 // cull points off the ymin-xmin edge of the viewport
2296 primMask
&= ~_simd_movemask_ps(_simd_castsi_ps(vXi
));
2297 primMask
&= ~_simd_movemask_ps(_simd_castsi_ps(vYi
));
2299 // compute macro tile coordinates
2300 simdscalari macroX
= _simd_srai_epi32(vXi
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2301 simdscalari macroY
= _simd_srai_epi32(vYi
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2303 OSALIGNSIMD(uint32_t) aMacroX
[KNOB_SIMD_WIDTH
], aMacroY
[KNOB_SIMD_WIDTH
];
2304 _simd_store_si((simdscalari
*)aMacroX
, macroX
);
2305 _simd_store_si((simdscalari
*)aMacroY
, macroY
);
2307 // compute raster tile coordinates
2308 simdscalari rasterX
= _simd_srai_epi32(vXi
, KNOB_TILE_X_DIM_SHIFT
+ FIXED_POINT_SHIFT
);
2309 simdscalari rasterY
= _simd_srai_epi32(vYi
, KNOB_TILE_Y_DIM_SHIFT
+ FIXED_POINT_SHIFT
);
2311 // compute raster tile relative x,y for coverage mask
2312 simdscalari tileAlignedX
= _simd_slli_epi32(rasterX
, KNOB_TILE_X_DIM_SHIFT
);
2313 simdscalari tileAlignedY
= _simd_slli_epi32(rasterY
, KNOB_TILE_Y_DIM_SHIFT
);
2315 simdscalari tileRelativeX
= _simd_sub_epi32(_simd_srai_epi32(vXi
, FIXED_POINT_SHIFT
), tileAlignedX
);
2316 simdscalari tileRelativeY
= _simd_sub_epi32(_simd_srai_epi32(vYi
, FIXED_POINT_SHIFT
), tileAlignedY
);
2318 OSALIGNSIMD(uint32_t) aTileRelativeX
[KNOB_SIMD_WIDTH
];
2319 OSALIGNSIMD(uint32_t) aTileRelativeY
[KNOB_SIMD_WIDTH
];
2320 _simd_store_si((simdscalari
*)aTileRelativeX
, tileRelativeX
);
2321 _simd_store_si((simdscalari
*)aTileRelativeY
, tileRelativeY
);
2323 OSALIGNSIMD(uint32_t) aTileAlignedX
[KNOB_SIMD_WIDTH
];
2324 OSALIGNSIMD(uint32_t) aTileAlignedY
[KNOB_SIMD_WIDTH
];
2325 _simd_store_si((simdscalari
*)aTileAlignedX
, tileAlignedX
);
2326 _simd_store_si((simdscalari
*)aTileAlignedY
, tileAlignedY
);
2328 OSALIGNSIMD(float) aZ
[KNOB_SIMD_WIDTH
];
2329 _simd_store_ps((float*)aZ
, primVerts
.z
);
2331 // store render target array index
2332 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2333 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2336 pa
.Assemble(VERTEX_RTAI_SLOT
, &vRtai
);
2337 simdscalari vRtaii
= _simd_castps_si(vRtai
.x
);
2338 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2342 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2345 uint32_t *pPrimID
= (uint32_t *)&primID
;
2346 DWORD primIndex
= 0;
2348 const SWR_BACKEND_STATE
& backendState
= pDC
->pState
->state
.backendState
;
2350 // scan remaining valid triangles and bin each separately
2351 while (_BitScanForward(&primIndex
, primMask
))
2353 uint32_t linkageCount
= backendState
.numAttributes
;
2354 uint32_t numScalarAttribs
= linkageCount
* 4;
2359 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2361 // points are always front facing
2362 desc
.triFlags
.frontFacing
= 1;
2363 desc
.triFlags
.primID
= pPrimID
[primIndex
];
2364 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[primIndex
];
2365 desc
.triFlags
.viewportIndex
= pViewportIndex
[primIndex
];
2367 work
.pfnWork
= RasterizeSimplePoint
;
2369 auto pArena
= pDC
->pArena
;
2370 SWR_ASSERT(pArena
!= nullptr);
2373 float *pAttribs
= (float*)pArena
->AllocAligned(3 * numScalarAttribs
* sizeof(float), 16);
2374 desc
.pAttribs
= pAttribs
;
2375 desc
.numAttribs
= linkageCount
;
2377 pfnProcessAttribs(pDC
, pa
, primIndex
, pPrimID
[primIndex
], pAttribs
);
2379 // store raster tile aligned x, y, perspective correct z
2380 float *pTriBuffer
= (float*)pArena
->AllocAligned(4 * sizeof(float), 16);
2381 desc
.pTriBuffer
= pTriBuffer
;
2382 *(uint32_t*)pTriBuffer
++ = aTileAlignedX
[primIndex
];
2383 *(uint32_t*)pTriBuffer
++ = aTileAlignedY
[primIndex
];
2384 *pTriBuffer
= aZ
[primIndex
];
2386 uint32_t tX
= aTileRelativeX
[primIndex
];
2387 uint32_t tY
= aTileRelativeY
[primIndex
];
2389 // pack the relative x,y into the coverageMask, the rasterizer will
2390 // generate the true coverage mask from it
2391 work
.desc
.tri
.triFlags
.coverageMask
= tX
| (tY
<< 4);
2394 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
2395 #if KNOB_ENABLE_TOSS_POINTS
2396 if (!KNOB_TOSS_SETUP_TRIS
)
2399 pTileMgr
->enqueue(aMacroX
[primIndex
], aMacroY
[primIndex
], &work
);
2401 primMask
&= ~(1 << primIndex
);
2406 // non simple points need to be potentially binned to multiple macro tiles
2407 simdscalar vPointSize
;
2408 if (rastState
.pointParam
)
2411 pa
.Assemble(VERTEX_POINT_SIZE_SLOT
, size
);
2412 vPointSize
= size
[0].x
;
2416 vPointSize
= _simd_set1_ps(rastState
.pointSize
);
2419 // bloat point to bbox
2421 bbox
.xmin
= bbox
.xmax
= vXi
;
2422 bbox
.ymin
= bbox
.ymax
= vYi
;
2424 simdscalar vHalfWidth
= _simd_mul_ps(vPointSize
, _simd_set1_ps(0.5f
));
2425 simdscalari vHalfWidthi
= fpToFixedPointVertical(vHalfWidth
);
2426 bbox
.xmin
= _simd_sub_epi32(bbox
.xmin
, vHalfWidthi
);
2427 bbox
.xmax
= _simd_add_epi32(bbox
.xmax
, vHalfWidthi
);
2428 bbox
.ymin
= _simd_sub_epi32(bbox
.ymin
, vHalfWidthi
);
2429 bbox
.ymax
= _simd_add_epi32(bbox
.ymax
, vHalfWidthi
);
2431 // Intersect with scissor/viewport. Subtract 1 ULP in x.8 fixed point since xmax/ymax edge is exclusive.
2432 // Gather the AOS effective scissor rects based on the per-prim VP index.
2433 /// @todo: Look at speeding this up -- weigh against corresponding costs in rasterizer.
2434 simdscalari scisXmin
, scisYmin
, scisXmax
, scisYmax
;
2435 if (state
.gsState
.emitsViewportArrayIndex
)
2437 GatherScissors
<KNOB_SIMD_WIDTH
>::Gather(&state
.scissorsInFixedPoint
[0], pViewportIndex
,
2438 scisXmin
, scisYmin
, scisXmax
, scisYmax
);
2440 else // broadcast fast path for non-VPAI case.
2442 scisXmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmin
);
2443 scisYmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymin
);
2444 scisXmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmax
);
2445 scisYmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymax
);
2448 bbox
.xmin
= _simd_max_epi32(bbox
.xmin
, scisXmin
);
2449 bbox
.ymin
= _simd_max_epi32(bbox
.ymin
, scisYmin
);
2450 bbox
.xmax
= _simd_min_epi32(_simd_sub_epi32(bbox
.xmax
, _simd_set1_epi32(1)), scisXmax
);
2451 bbox
.ymax
= _simd_min_epi32(_simd_sub_epi32(bbox
.ymax
, _simd_set1_epi32(1)), scisYmax
);
2453 // Cull bloated points completely outside scissor
2454 simdscalari maskOutsideScissorX
= _simd_cmpgt_epi32(bbox
.xmin
, bbox
.xmax
);
2455 simdscalari maskOutsideScissorY
= _simd_cmpgt_epi32(bbox
.ymin
, bbox
.ymax
);
2456 simdscalari maskOutsideScissorXY
= _simd_or_si(maskOutsideScissorX
, maskOutsideScissorY
);
2457 uint32_t maskOutsideScissor
= _simd_movemask_ps(_simd_castsi_ps(maskOutsideScissorXY
));
2458 primMask
= primMask
& ~maskOutsideScissor
;
2460 // Convert bbox to macrotile units.
2461 bbox
.xmin
= _simd_srai_epi32(bbox
.xmin
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2462 bbox
.ymin
= _simd_srai_epi32(bbox
.ymin
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2463 bbox
.xmax
= _simd_srai_epi32(bbox
.xmax
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2464 bbox
.ymax
= _simd_srai_epi32(bbox
.ymax
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2466 OSALIGNSIMD(uint32_t) aMTLeft
[KNOB_SIMD_WIDTH
], aMTRight
[KNOB_SIMD_WIDTH
], aMTTop
[KNOB_SIMD_WIDTH
], aMTBottom
[KNOB_SIMD_WIDTH
];
2467 _simd_store_si((simdscalari
*)aMTLeft
, bbox
.xmin
);
2468 _simd_store_si((simdscalari
*)aMTRight
, bbox
.xmax
);
2469 _simd_store_si((simdscalari
*)aMTTop
, bbox
.ymin
);
2470 _simd_store_si((simdscalari
*)aMTBottom
, bbox
.ymax
);
2472 // store render target array index
2473 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2474 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2476 simdvector vRtai
[2];
2477 pa
.Assemble(VERTEX_RTAI_SLOT
, vRtai
);
2478 simdscalari vRtaii
= _simd_castps_si(vRtai
[0].x
);
2479 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2483 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2486 OSALIGNSIMD(float) aPointSize
[KNOB_SIMD_WIDTH
];
2487 _simd_store_ps((float*)aPointSize
, vPointSize
);
2489 uint32_t *pPrimID
= (uint32_t *)&primID
;
2491 OSALIGNSIMD(float) aPrimVertsX
[KNOB_SIMD_WIDTH
];
2492 OSALIGNSIMD(float) aPrimVertsY
[KNOB_SIMD_WIDTH
];
2493 OSALIGNSIMD(float) aPrimVertsZ
[KNOB_SIMD_WIDTH
];
2495 _simd_store_ps((float*)aPrimVertsX
, primVerts
.x
);
2496 _simd_store_ps((float*)aPrimVertsY
, primVerts
.y
);
2497 _simd_store_ps((float*)aPrimVertsZ
, primVerts
.z
);
2499 // scan remaining valid prims and bin each separately
2500 const SWR_BACKEND_STATE
& backendState
= state
.backendState
;
2502 while (_BitScanForward(&primIndex
, primMask
))
2504 uint32_t linkageCount
= backendState
.numAttributes
;
2505 uint32_t numScalarAttribs
= linkageCount
* 4;
2510 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2512 desc
.triFlags
.frontFacing
= 1;
2513 desc
.triFlags
.primID
= pPrimID
[primIndex
];
2514 desc
.triFlags
.pointSize
= aPointSize
[primIndex
];
2515 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[primIndex
];
2516 desc
.triFlags
.viewportIndex
= pViewportIndex
[primIndex
];
2518 work
.pfnWork
= RasterizeTriPoint
;
2520 auto pArena
= pDC
->pArena
;
2521 SWR_ASSERT(pArena
!= nullptr);
2523 // store active attribs
2524 desc
.pAttribs
= (float*)pArena
->AllocAligned(numScalarAttribs
* 3 * sizeof(float), 16);
2525 desc
.numAttribs
= linkageCount
;
2526 pfnProcessAttribs(pDC
, pa
, primIndex
, pPrimID
[primIndex
], desc
.pAttribs
);
2528 // store point vertex data
2529 float *pTriBuffer
= (float*)pArena
->AllocAligned(4 * sizeof(float), 16);
2530 desc
.pTriBuffer
= pTriBuffer
;
2531 *pTriBuffer
++ = aPrimVertsX
[primIndex
];
2532 *pTriBuffer
++ = aPrimVertsY
[primIndex
];
2533 *pTriBuffer
= aPrimVertsZ
[primIndex
];
2535 // store user clip distances
2536 if (rastState
.clipDistanceMask
)
2538 uint32_t numClipDist
= _mm_popcnt_u32(rastState
.clipDistanceMask
);
2539 desc
.pUserClipBuffer
= (float*)pArena
->Alloc(numClipDist
* 2 * sizeof(float));
2540 ProcessUserClipDist
<2>(pa
, primIndex
, rastState
.clipDistanceMask
, desc
.pUserClipBuffer
);
2543 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
2544 for (uint32_t y
= aMTTop
[primIndex
]; y
<= aMTBottom
[primIndex
]; ++y
)
2546 for (uint32_t x
= aMTLeft
[primIndex
]; x
<= aMTRight
[primIndex
]; ++x
)
2548 #if KNOB_ENABLE_TOSS_POINTS
2549 if (!KNOB_TOSS_SETUP_TRIS
)
2552 pTileMgr
->enqueue(x
, y
, &work
);
2557 primMask
&= ~(1 << primIndex
);
2561 AR_END(FEBinPoints
, 1);
2564 //////////////////////////////////////////////////////////////////////////
2565 /// @brief Bin SIMD lines to the backend.
2566 /// @param pDC - pointer to draw context.
2567 /// @param pa - The primitive assembly object.
2568 /// @param workerId - thread's worker id. Even thread has a unique id.
2569 /// @param tri - Contains line position data for SIMDs worth of points.
2570 /// @param primID - Primitive ID for each line.
2571 /// @param viewportIdx - Viewport Array Index for each line.
2579 simdscalari viewportIdx
)
2581 SWR_CONTEXT
*pContext
= pDC
->pContext
;
2583 AR_BEGIN(FEBinLines
, pDC
->drawId
);
2585 const API_STATE
& state
= GetApiState(pDC
);
2586 const SWR_RASTSTATE
& rastState
= state
.rastState
;
2587 const SWR_FRONTEND_STATE
& feState
= state
.frontendState
;
2588 const SWR_GS_STATE
& gsState
= state
.gsState
;
2590 // Select attribute processor
2591 PFN_PROCESS_ATTRIBUTES pfnProcessAttribs
= GetProcessAttributesFunc(2,
2592 state
.backendState
.swizzleEnable
, state
.backendState
.constantInterpolationMask
);
2594 simdscalar vRecipW0
= _simd_set1_ps(1.0f
);
2595 simdscalar vRecipW1
= _simd_set1_ps(1.0f
);
2597 if (!feState
.vpTransformDisable
)
2599 // perspective divide
2600 vRecipW0
= _simd_div_ps(_simd_set1_ps(1.0f
), prim
[0].w
);
2601 vRecipW1
= _simd_div_ps(_simd_set1_ps(1.0f
), prim
[1].w
);
2603 prim
[0].v
[0] = _simd_mul_ps(prim
[0].v
[0], vRecipW0
);
2604 prim
[1].v
[0] = _simd_mul_ps(prim
[1].v
[0], vRecipW1
);
2606 prim
[0].v
[1] = _simd_mul_ps(prim
[0].v
[1], vRecipW0
);
2607 prim
[1].v
[1] = _simd_mul_ps(prim
[1].v
[1], vRecipW1
);
2609 prim
[0].v
[2] = _simd_mul_ps(prim
[0].v
[2], vRecipW0
);
2610 prim
[1].v
[2] = _simd_mul_ps(prim
[1].v
[2], vRecipW1
);
2612 // viewport transform to screen coords
2613 if (state
.gsState
.emitsViewportArrayIndex
)
2615 viewportTransform
<2>(prim
, state
.vpMatrices
, viewportIdx
);
2619 viewportTransform
<2>(prim
, state
.vpMatrices
);
2623 // adjust for pixel center location
2624 simdscalar offset
= g_pixelOffsets
[rastState
.pixelLocation
];
2625 prim
[0].x
= _simd_add_ps(prim
[0].x
, offset
);
2626 prim
[0].y
= _simd_add_ps(prim
[0].y
, offset
);
2628 prim
[1].x
= _simd_add_ps(prim
[1].x
, offset
);
2629 prim
[1].y
= _simd_add_ps(prim
[1].y
, offset
);
2631 // convert to fixed point
2632 simdscalari vXi
[2], vYi
[2];
2633 vXi
[0] = fpToFixedPointVertical(prim
[0].x
);
2634 vYi
[0] = fpToFixedPointVertical(prim
[0].y
);
2635 vXi
[1] = fpToFixedPointVertical(prim
[1].x
);
2636 vYi
[1] = fpToFixedPointVertical(prim
[1].y
);
2638 // compute x-major vs y-major mask
2639 simdscalari xLength
= _simd_abs_epi32(_simd_sub_epi32(vXi
[0], vXi
[1]));
2640 simdscalari yLength
= _simd_abs_epi32(_simd_sub_epi32(vYi
[0], vYi
[1]));
2641 simdscalar vYmajorMask
= _simd_castsi_ps(_simd_cmpgt_epi32(yLength
, xLength
));
2642 uint32_t yMajorMask
= _simd_movemask_ps(vYmajorMask
);
2644 // cull zero-length lines
2645 simdscalari vZeroLengthMask
= _simd_cmpeq_epi32(xLength
, _simd_setzero_si());
2646 vZeroLengthMask
= _simd_and_si(vZeroLengthMask
, _simd_cmpeq_epi32(yLength
, _simd_setzero_si()));
2648 primMask
&= ~_simd_movemask_ps(_simd_castsi_ps(vZeroLengthMask
));
2650 uint32_t *pPrimID
= (uint32_t *)&primID
;
2651 const uint32_t *pViewportIndex
= (uint32_t *)&viewportIdx
;
2653 simdscalar vUnused
= _simd_setzero_ps();
2655 // Calc bounding box of lines
2657 bbox
.xmin
= _simd_min_epi32(vXi
[0], vXi
[1]);
2658 bbox
.xmax
= _simd_max_epi32(vXi
[0], vXi
[1]);
2659 bbox
.ymin
= _simd_min_epi32(vYi
[0], vYi
[1]);
2660 bbox
.ymax
= _simd_max_epi32(vYi
[0], vYi
[1]);
2662 // bloat bbox by line width along minor axis
2663 simdscalar vHalfWidth
= _simd_set1_ps(rastState
.lineWidth
/ 2.0f
);
2664 simdscalari vHalfWidthi
= fpToFixedPointVertical(vHalfWidth
);
2666 bloatBox
.xmin
= _simd_sub_epi32(bbox
.xmin
, vHalfWidthi
);
2667 bloatBox
.xmax
= _simd_add_epi32(bbox
.xmax
, vHalfWidthi
);
2668 bloatBox
.ymin
= _simd_sub_epi32(bbox
.ymin
, vHalfWidthi
);
2669 bloatBox
.ymax
= _simd_add_epi32(bbox
.ymax
, vHalfWidthi
);
2671 bbox
.xmin
= _simd_blendv_epi32(bbox
.xmin
, bloatBox
.xmin
, vYmajorMask
);
2672 bbox
.xmax
= _simd_blendv_epi32(bbox
.xmax
, bloatBox
.xmax
, vYmajorMask
);
2673 bbox
.ymin
= _simd_blendv_epi32(bloatBox
.ymin
, bbox
.ymin
, vYmajorMask
);
2674 bbox
.ymax
= _simd_blendv_epi32(bloatBox
.ymax
, bbox
.ymax
, vYmajorMask
);
2676 // Intersect with scissor/viewport. Subtract 1 ULP in x.8 fixed point since xmax/ymax edge is exclusive.
2677 simdscalari scisXmin
, scisYmin
, scisXmax
, scisYmax
;
2678 if (state
.gsState
.emitsViewportArrayIndex
)
2680 GatherScissors
<KNOB_SIMD_WIDTH
>::Gather(&state
.scissorsInFixedPoint
[0], pViewportIndex
,
2681 scisXmin
, scisYmin
, scisXmax
, scisYmax
);
2683 else // broadcast fast path for non-VPAI case.
2685 scisXmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmin
);
2686 scisYmin
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymin
);
2687 scisXmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].xmax
);
2688 scisYmax
= _simd_set1_epi32(state
.scissorsInFixedPoint
[0].ymax
);
2691 bbox
.xmin
= _simd_max_epi32(bbox
.xmin
, scisXmin
);
2692 bbox
.ymin
= _simd_max_epi32(bbox
.ymin
, scisYmin
);
2693 bbox
.xmax
= _simd_min_epi32(_simd_sub_epi32(bbox
.xmax
, _simd_set1_epi32(1)), scisXmax
);
2694 bbox
.ymax
= _simd_min_epi32(_simd_sub_epi32(bbox
.ymax
, _simd_set1_epi32(1)), scisYmax
);
2696 // Cull prims completely outside scissor
2698 simdscalari maskOutsideScissorX
= _simd_cmpgt_epi32(bbox
.xmin
, bbox
.xmax
);
2699 simdscalari maskOutsideScissorY
= _simd_cmpgt_epi32(bbox
.ymin
, bbox
.ymax
);
2700 simdscalari maskOutsideScissorXY
= _simd_or_si(maskOutsideScissorX
, maskOutsideScissorY
);
2701 uint32_t maskOutsideScissor
= _simd_movemask_ps(_simd_castsi_ps(maskOutsideScissorXY
));
2702 primMask
= primMask
& ~maskOutsideScissor
;
2710 // Convert triangle bbox to macrotile units.
2711 bbox
.xmin
= _simd_srai_epi32(bbox
.xmin
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2712 bbox
.ymin
= _simd_srai_epi32(bbox
.ymin
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2713 bbox
.xmax
= _simd_srai_epi32(bbox
.xmax
, KNOB_MACROTILE_X_DIM_FIXED_SHIFT
);
2714 bbox
.ymax
= _simd_srai_epi32(bbox
.ymax
, KNOB_MACROTILE_Y_DIM_FIXED_SHIFT
);
2716 OSALIGNSIMD(uint32_t) aMTLeft
[KNOB_SIMD_WIDTH
], aMTRight
[KNOB_SIMD_WIDTH
], aMTTop
[KNOB_SIMD_WIDTH
], aMTBottom
[KNOB_SIMD_WIDTH
];
2717 _simd_store_si((simdscalari
*)aMTLeft
, bbox
.xmin
);
2718 _simd_store_si((simdscalari
*)aMTRight
, bbox
.xmax
);
2719 _simd_store_si((simdscalari
*)aMTTop
, bbox
.ymin
);
2720 _simd_store_si((simdscalari
*)aMTBottom
, bbox
.ymax
);
2722 // transpose verts needed for backend
2723 /// @todo modify BE to take non-transformed verts
2724 __m128 vHorizX
[8], vHorizY
[8], vHorizZ
[8], vHorizW
[8];
2725 vTranspose3x8(vHorizX
, prim
[0].x
, prim
[1].x
, vUnused
);
2726 vTranspose3x8(vHorizY
, prim
[0].y
, prim
[1].y
, vUnused
);
2727 vTranspose3x8(vHorizZ
, prim
[0].z
, prim
[1].z
, vUnused
);
2728 vTranspose3x8(vHorizW
, vRecipW0
, vRecipW1
, vUnused
);
2730 // store render target array index
2731 OSALIGNSIMD(uint32_t) aRTAI
[KNOB_SIMD_WIDTH
];
2732 if (gsState
.gsEnable
&& gsState
.emitsRenderTargetArrayIndex
)
2734 simdvector vRtai
[2];
2735 pa
.Assemble(VERTEX_RTAI_SLOT
, vRtai
);
2736 simdscalari vRtaii
= _simd_castps_si(vRtai
[0].x
);
2737 _simd_store_si((simdscalari
*)aRTAI
, vRtaii
);
2741 _simd_store_si((simdscalari
*)aRTAI
, _simd_setzero_si());
2744 // scan remaining valid prims and bin each separately
2746 while (_BitScanForward(&primIndex
, primMask
))
2748 uint32_t linkageCount
= state
.backendState
.numAttributes
;
2749 uint32_t numScalarAttribs
= linkageCount
* 4;
2754 TRIANGLE_WORK_DESC
&desc
= work
.desc
.tri
;
2756 desc
.triFlags
.frontFacing
= 1;
2757 desc
.triFlags
.primID
= pPrimID
[primIndex
];
2758 desc
.triFlags
.yMajor
= (yMajorMask
>> primIndex
) & 1;
2759 desc
.triFlags
.renderTargetArrayIndex
= aRTAI
[primIndex
];
2760 desc
.triFlags
.viewportIndex
= pViewportIndex
[primIndex
];
2762 work
.pfnWork
= RasterizeLine
;
2764 auto pArena
= pDC
->pArena
;
2765 SWR_ASSERT(pArena
!= nullptr);
2767 // store active attribs
2768 desc
.pAttribs
= (float*)pArena
->AllocAligned(numScalarAttribs
* 3 * sizeof(float), 16);
2769 desc
.numAttribs
= linkageCount
;
2770 pfnProcessAttribs(pDC
, pa
, primIndex
, pPrimID
[primIndex
], desc
.pAttribs
);
2772 // store line vertex data
2773 desc
.pTriBuffer
= (float*)pArena
->AllocAligned(4 * 4 * sizeof(float), 16);
2774 _mm_store_ps(&desc
.pTriBuffer
[0], vHorizX
[primIndex
]);
2775 _mm_store_ps(&desc
.pTriBuffer
[4], vHorizY
[primIndex
]);
2776 _mm_store_ps(&desc
.pTriBuffer
[8], vHorizZ
[primIndex
]);
2777 _mm_store_ps(&desc
.pTriBuffer
[12], vHorizW
[primIndex
]);
2779 // store user clip distances
2780 if (rastState
.clipDistanceMask
)
2782 uint32_t numClipDist
= _mm_popcnt_u32(rastState
.clipDistanceMask
);
2783 desc
.pUserClipBuffer
= (float*)pArena
->Alloc(numClipDist
* 2 * sizeof(float));
2784 ProcessUserClipDist
<2>(pa
, primIndex
, rastState
.clipDistanceMask
, desc
.pUserClipBuffer
);
2787 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
2788 for (uint32_t y
= aMTTop
[primIndex
]; y
<= aMTBottom
[primIndex
]; ++y
)
2790 for (uint32_t x
= aMTLeft
[primIndex
]; x
<= aMTRight
[primIndex
]; ++x
)
2792 #if KNOB_ENABLE_TOSS_POINTS
2793 if (!KNOB_TOSS_SETUP_TRIS
)
2796 pTileMgr
->enqueue(x
, y
, &work
);
2801 primMask
&= ~(1 << primIndex
);
2806 AR_END(FEBinLines
, 1);