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"
40 #include "tessellator.h"
43 //////////////////////////////////////////////////////////////////////////
44 /// @brief Helper macro to generate a bitmask
45 static INLINE
uint32_t GenMask(uint32_t numBits
)
47 SWR_ASSERT(numBits
<= (sizeof(uint32_t) * 8), "Too many bits (%d) for %s", numBits
, __FUNCTION__
);
48 return ((1U << numBits
) - 1);
51 //////////////////////////////////////////////////////////////////////////
52 /// @brief FE handler for SwrSync.
53 /// @param pContext - pointer to SWR context.
54 /// @param pDC - pointer to draw context.
55 /// @param workerId - thread's worker id. Even thread has a unique id.
56 /// @param pUserData - Pointer to user data passed back to sync callback.
57 /// @todo This should go away when we switch this to use compute threading.
59 SWR_CONTEXT
*pContext
,
66 work
.pfnWork
= ProcessSyncBE
;
68 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
69 pTileMgr
->enqueue(0, 0, &work
);
72 //////////////////////////////////////////////////////////////////////////
73 /// @brief FE handler for SwrDestroyContext.
74 /// @param pContext - pointer to SWR context.
75 /// @param pDC - pointer to draw context.
76 /// @param workerId - thread's worker id. Even thread has a unique id.
77 /// @param pUserData - Pointer to user data passed back to sync callback.
79 SWR_CONTEXT
*pContext
,
86 work
.pfnWork
= ProcessShutdownBE
;
88 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
89 // Enqueue at least 1 work item for each worker thread
90 // account for number of numa nodes
91 uint32_t numNumaNodes
= pContext
->threadPool
.numaMask
+ 1;
93 for (uint32_t i
= 0; i
< pContext
->threadPool
.numThreads
; ++i
)
95 for (uint32_t n
= 0; n
< numNumaNodes
; ++n
)
97 pTileMgr
->enqueue(i
, n
, &work
);
102 //////////////////////////////////////////////////////////////////////////
103 /// @brief FE handler for SwrClearRenderTarget.
104 /// @param pContext - pointer to SWR context.
105 /// @param pDC - pointer to draw context.
106 /// @param workerId - thread's worker id. Even thread has a unique id.
107 /// @param pUserData - Pointer to user data passed back to clear callback.
108 /// @todo This should go away when we switch this to use compute threading.
110 SWR_CONTEXT
*pContext
,
115 CLEAR_DESC
*pDesc
= (CLEAR_DESC
*)pUserData
;
116 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
118 // queue a clear to each macro tile
119 // compute macro tile bounds for the specified rect
120 uint32_t macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
121 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
122 uint32_t macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
123 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
127 work
.pfnWork
= ProcessClearBE
;
128 work
.desc
.clear
= *pDesc
;
130 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
132 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
134 pTileMgr
->enqueue(x
, y
, &work
);
139 //////////////////////////////////////////////////////////////////////////
140 /// @brief FE handler for SwrStoreTiles.
141 /// @param pContext - pointer to SWR context.
142 /// @param pDC - pointer to draw context.
143 /// @param workerId - thread's worker id. Even thread has a unique id.
144 /// @param pUserData - Pointer to user data passed back to callback.
145 /// @todo This should go away when we switch this to use compute threading.
146 void ProcessStoreTiles(
147 SWR_CONTEXT
*pContext
,
152 AR_BEGIN(FEProcessStoreTiles
, pDC
->drawId
);
153 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
154 STORE_TILES_DESC
* pDesc
= (STORE_TILES_DESC
*)pUserData
;
156 // queue a store to each macro tile
157 // compute macro tile bounds for the specified rect
158 uint32_t macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
159 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
160 uint32_t macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
161 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
165 work
.type
= STORETILES
;
166 work
.pfnWork
= ProcessStoreTilesBE
;
167 work
.desc
.storeTiles
= *pDesc
;
169 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
171 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
173 pTileMgr
->enqueue(x
, y
, &work
);
177 AR_END(FEProcessStoreTiles
, 0);
180 //////////////////////////////////////////////////////////////////////////
181 /// @brief FE handler for SwrInvalidateTiles.
182 /// @param pContext - pointer to SWR context.
183 /// @param pDC - pointer to draw context.
184 /// @param workerId - thread's worker id. Even thread has a unique id.
185 /// @param pUserData - Pointer to user data passed back to callback.
186 /// @todo This should go away when we switch this to use compute threading.
187 void ProcessDiscardInvalidateTiles(
188 SWR_CONTEXT
*pContext
,
193 AR_BEGIN(FEProcessInvalidateTiles
, pDC
->drawId
);
194 DISCARD_INVALIDATE_TILES_DESC
*pDesc
= (DISCARD_INVALIDATE_TILES_DESC
*)pUserData
;
195 MacroTileMgr
*pTileMgr
= pDC
->pTileMgr
;
197 // compute macro tile bounds for the specified rect
198 uint32_t macroTileXMin
= (pDesc
->rect
.xmin
+ KNOB_MACROTILE_X_DIM
- 1) / KNOB_MACROTILE_X_DIM
;
199 uint32_t macroTileXMax
= (pDesc
->rect
.xmax
/ KNOB_MACROTILE_X_DIM
) - 1;
200 uint32_t macroTileYMin
= (pDesc
->rect
.ymin
+ KNOB_MACROTILE_Y_DIM
- 1) / KNOB_MACROTILE_Y_DIM
;
201 uint32_t macroTileYMax
= (pDesc
->rect
.ymax
/ KNOB_MACROTILE_Y_DIM
) - 1;
203 if (pDesc
->fullTilesOnly
== false)
205 // include partial tiles
206 macroTileXMin
= pDesc
->rect
.xmin
/ KNOB_MACROTILE_X_DIM
;
207 macroTileXMax
= (pDesc
->rect
.xmax
- 1) / KNOB_MACROTILE_X_DIM
;
208 macroTileYMin
= pDesc
->rect
.ymin
/ KNOB_MACROTILE_Y_DIM
;
209 macroTileYMax
= (pDesc
->rect
.ymax
- 1) / KNOB_MACROTILE_Y_DIM
;
212 SWR_ASSERT(macroTileXMax
<= KNOB_NUM_HOT_TILES_X
);
213 SWR_ASSERT(macroTileYMax
<= KNOB_NUM_HOT_TILES_Y
);
215 macroTileXMax
= std::min
<int32_t>(macroTileXMax
, KNOB_NUM_HOT_TILES_X
);
216 macroTileYMax
= std::min
<int32_t>(macroTileYMax
, KNOB_NUM_HOT_TILES_Y
);
220 work
.type
= DISCARDINVALIDATETILES
;
221 work
.pfnWork
= ProcessDiscardInvalidateTilesBE
;
222 work
.desc
.discardInvalidateTiles
= *pDesc
;
224 for (uint32_t x
= macroTileXMin
; x
<= macroTileXMax
; ++x
)
226 for (uint32_t y
= macroTileYMin
; y
<= macroTileYMax
; ++y
)
228 pTileMgr
->enqueue(x
, y
, &work
);
232 AR_END(FEProcessInvalidateTiles
, 0);
235 //////////////////////////////////////////////////////////////////////////
236 /// @brief Computes the number of primitives given the number of verts.
237 /// @param mode - primitive topology for draw operation.
238 /// @param numPrims - number of vertices or indices for draw.
239 /// @todo Frontend needs to be refactored. This will go in appropriate place then.
240 uint32_t GetNumPrims(
241 PRIMITIVE_TOPOLOGY mode
,
246 case TOP_POINT_LIST
: return numPrims
;
247 case TOP_TRIANGLE_LIST
: return numPrims
/ 3;
248 case TOP_TRIANGLE_STRIP
: return numPrims
< 3 ? 0 : numPrims
- 2;
249 case TOP_TRIANGLE_FAN
: return numPrims
< 3 ? 0 : numPrims
- 2;
250 case TOP_TRIANGLE_DISC
: return numPrims
< 2 ? 0 : numPrims
- 1;
251 case TOP_QUAD_LIST
: return numPrims
/ 4;
252 case TOP_QUAD_STRIP
: return numPrims
< 4 ? 0 : (numPrims
- 2) / 2;
253 case TOP_LINE_STRIP
: return numPrims
< 2 ? 0 : numPrims
- 1;
254 case TOP_LINE_LIST
: return numPrims
/ 2;
255 case TOP_LINE_LOOP
: return numPrims
;
256 case TOP_RECT_LIST
: return numPrims
/ 3;
257 case TOP_LINE_LIST_ADJ
: return numPrims
/ 4;
258 case TOP_LISTSTRIP_ADJ
: return numPrims
< 3 ? 0 : numPrims
- 3;
259 case TOP_TRI_LIST_ADJ
: return numPrims
/ 6;
260 case TOP_TRI_STRIP_ADJ
: return numPrims
< 4 ? 0 : (numPrims
/ 2) - 2;
262 case TOP_PATCHLIST_1
:
263 case TOP_PATCHLIST_2
:
264 case TOP_PATCHLIST_3
:
265 case TOP_PATCHLIST_4
:
266 case TOP_PATCHLIST_5
:
267 case TOP_PATCHLIST_6
:
268 case TOP_PATCHLIST_7
:
269 case TOP_PATCHLIST_8
:
270 case TOP_PATCHLIST_9
:
271 case TOP_PATCHLIST_10
:
272 case TOP_PATCHLIST_11
:
273 case TOP_PATCHLIST_12
:
274 case TOP_PATCHLIST_13
:
275 case TOP_PATCHLIST_14
:
276 case TOP_PATCHLIST_15
:
277 case TOP_PATCHLIST_16
:
278 case TOP_PATCHLIST_17
:
279 case TOP_PATCHLIST_18
:
280 case TOP_PATCHLIST_19
:
281 case TOP_PATCHLIST_20
:
282 case TOP_PATCHLIST_21
:
283 case TOP_PATCHLIST_22
:
284 case TOP_PATCHLIST_23
:
285 case TOP_PATCHLIST_24
:
286 case TOP_PATCHLIST_25
:
287 case TOP_PATCHLIST_26
:
288 case TOP_PATCHLIST_27
:
289 case TOP_PATCHLIST_28
:
290 case TOP_PATCHLIST_29
:
291 case TOP_PATCHLIST_30
:
292 case TOP_PATCHLIST_31
:
293 case TOP_PATCHLIST_32
:
294 return numPrims
/ (mode
- TOP_PATCHLIST_BASE
);
297 case TOP_POINT_LIST_BF
:
298 case TOP_LINE_STRIP_CONT
:
299 case TOP_LINE_STRIP_BF
:
300 case TOP_LINE_STRIP_CONT_BF
:
301 case TOP_TRIANGLE_FAN_NOSTIPPLE
:
302 case TOP_TRI_STRIP_REVERSE
:
303 case TOP_PATCHLIST_BASE
:
305 SWR_INVALID("Unsupported topology: %d", mode
);
312 //////////////////////////////////////////////////////////////////////////
313 /// @brief Computes the number of verts given the number of primitives.
314 /// @param mode - primitive topology for draw operation.
315 /// @param numPrims - number of primitives for draw.
316 uint32_t GetNumVerts(
317 PRIMITIVE_TOPOLOGY mode
,
322 case TOP_POINT_LIST
: return numPrims
;
323 case TOP_TRIANGLE_LIST
: return numPrims
* 3;
324 case TOP_TRIANGLE_STRIP
: return numPrims
? numPrims
+ 2 : 0;
325 case TOP_TRIANGLE_FAN
: return numPrims
? numPrims
+ 2 : 0;
326 case TOP_TRIANGLE_DISC
: return numPrims
? numPrims
+ 1 : 0;
327 case TOP_QUAD_LIST
: return numPrims
* 4;
328 case TOP_QUAD_STRIP
: return numPrims
? numPrims
* 2 + 2 : 0;
329 case TOP_LINE_STRIP
: return numPrims
? numPrims
+ 1 : 0;
330 case TOP_LINE_LIST
: return numPrims
* 2;
331 case TOP_LINE_LOOP
: return numPrims
;
332 case TOP_RECT_LIST
: return numPrims
* 3;
333 case TOP_LINE_LIST_ADJ
: return numPrims
* 4;
334 case TOP_LISTSTRIP_ADJ
: return numPrims
? numPrims
+ 3 : 0;
335 case TOP_TRI_LIST_ADJ
: return numPrims
* 6;
336 case TOP_TRI_STRIP_ADJ
: return numPrims
? (numPrims
+ 2) * 2 : 0;
338 case TOP_PATCHLIST_1
:
339 case TOP_PATCHLIST_2
:
340 case TOP_PATCHLIST_3
:
341 case TOP_PATCHLIST_4
:
342 case TOP_PATCHLIST_5
:
343 case TOP_PATCHLIST_6
:
344 case TOP_PATCHLIST_7
:
345 case TOP_PATCHLIST_8
:
346 case TOP_PATCHLIST_9
:
347 case TOP_PATCHLIST_10
:
348 case TOP_PATCHLIST_11
:
349 case TOP_PATCHLIST_12
:
350 case TOP_PATCHLIST_13
:
351 case TOP_PATCHLIST_14
:
352 case TOP_PATCHLIST_15
:
353 case TOP_PATCHLIST_16
:
354 case TOP_PATCHLIST_17
:
355 case TOP_PATCHLIST_18
:
356 case TOP_PATCHLIST_19
:
357 case TOP_PATCHLIST_20
:
358 case TOP_PATCHLIST_21
:
359 case TOP_PATCHLIST_22
:
360 case TOP_PATCHLIST_23
:
361 case TOP_PATCHLIST_24
:
362 case TOP_PATCHLIST_25
:
363 case TOP_PATCHLIST_26
:
364 case TOP_PATCHLIST_27
:
365 case TOP_PATCHLIST_28
:
366 case TOP_PATCHLIST_29
:
367 case TOP_PATCHLIST_30
:
368 case TOP_PATCHLIST_31
:
369 case TOP_PATCHLIST_32
:
370 return numPrims
* (mode
- TOP_PATCHLIST_BASE
);
373 case TOP_POINT_LIST_BF
:
374 case TOP_LINE_STRIP_CONT
:
375 case TOP_LINE_STRIP_BF
:
376 case TOP_LINE_STRIP_CONT_BF
:
377 case TOP_TRIANGLE_FAN_NOSTIPPLE
:
378 case TOP_TRI_STRIP_REVERSE
:
379 case TOP_PATCHLIST_BASE
:
381 SWR_INVALID("Unsupported topology: %d", mode
);
388 //////////////////////////////////////////////////////////////////////////
389 /// @brief Return number of verts per primitive.
390 /// @param topology - topology
391 /// @param includeAdjVerts - include adjacent verts in primitive vertices
392 INLINE
uint32_t NumVertsPerPrim(PRIMITIVE_TOPOLOGY topology
, bool includeAdjVerts
)
394 uint32_t numVerts
= 0;
398 case TOP_POINT_LIST_BF
:
403 case TOP_LINE_LIST_ADJ
:
405 case TOP_LINE_STRIP_CONT
:
406 case TOP_LINE_STRIP_BF
:
407 case TOP_LISTSTRIP_ADJ
:
410 case TOP_TRIANGLE_LIST
:
411 case TOP_TRIANGLE_STRIP
:
412 case TOP_TRIANGLE_FAN
:
413 case TOP_TRI_LIST_ADJ
:
414 case TOP_TRI_STRIP_ADJ
:
415 case TOP_TRI_STRIP_REVERSE
:
423 case TOP_PATCHLIST_1
:
424 case TOP_PATCHLIST_2
:
425 case TOP_PATCHLIST_3
:
426 case TOP_PATCHLIST_4
:
427 case TOP_PATCHLIST_5
:
428 case TOP_PATCHLIST_6
:
429 case TOP_PATCHLIST_7
:
430 case TOP_PATCHLIST_8
:
431 case TOP_PATCHLIST_9
:
432 case TOP_PATCHLIST_10
:
433 case TOP_PATCHLIST_11
:
434 case TOP_PATCHLIST_12
:
435 case TOP_PATCHLIST_13
:
436 case TOP_PATCHLIST_14
:
437 case TOP_PATCHLIST_15
:
438 case TOP_PATCHLIST_16
:
439 case TOP_PATCHLIST_17
:
440 case TOP_PATCHLIST_18
:
441 case TOP_PATCHLIST_19
:
442 case TOP_PATCHLIST_20
:
443 case TOP_PATCHLIST_21
:
444 case TOP_PATCHLIST_22
:
445 case TOP_PATCHLIST_23
:
446 case TOP_PATCHLIST_24
:
447 case TOP_PATCHLIST_25
:
448 case TOP_PATCHLIST_26
:
449 case TOP_PATCHLIST_27
:
450 case TOP_PATCHLIST_28
:
451 case TOP_PATCHLIST_29
:
452 case TOP_PATCHLIST_30
:
453 case TOP_PATCHLIST_31
:
454 case TOP_PATCHLIST_32
:
455 numVerts
= topology
- TOP_PATCHLIST_BASE
;
458 SWR_INVALID("Unsupported topology: %d", topology
);
466 case TOP_LISTSTRIP_ADJ
:
467 case TOP_LINE_LIST_ADJ
: numVerts
= 4; break;
468 case TOP_TRI_STRIP_ADJ
:
469 case TOP_TRI_LIST_ADJ
: numVerts
= 6; break;
477 //////////////////////////////////////////////////////////////////////////
478 /// @brief Generate mask from remaining work.
479 /// @param numWorkItems - Number of items being worked on by a SIMD.
480 static INLINE simdscalari
GenerateMask(uint32_t numItemsRemaining
)
482 uint32_t numActive
= (numItemsRemaining
>= KNOB_SIMD_WIDTH
) ? KNOB_SIMD_WIDTH
: numItemsRemaining
;
483 uint32_t mask
= (numActive
> 0) ? ((1 << numActive
) - 1) : 0;
484 return _simd_castps_si(vMask(mask
));
487 //////////////////////////////////////////////////////////////////////////
488 /// @brief StreamOut - Streams vertex data out to SO buffers.
489 /// Generally, we are only streaming out a SIMDs worth of triangles.
490 /// @param pDC - pointer to draw context.
491 /// @param workerId - thread's worker id. Even thread has a unique id.
492 /// @param numPrims - Number of prims to streamout (e.g. points, lines, tris)
493 static void StreamOut(
498 uint32_t streamIndex
)
500 SWR_CONTEXT
*pContext
= pDC
->pContext
;
502 AR_BEGIN(FEStreamout
, pDC
->drawId
);
504 const API_STATE
& state
= GetApiState(pDC
);
505 const SWR_STREAMOUT_STATE
&soState
= state
.soState
;
507 uint32_t soVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, false);
509 // The pPrimData buffer is sparse in that we allocate memory for all 32 attributes for each vertex.
510 uint32_t primDataDwordVertexStride
= (SWR_VTX_NUM_SLOTS
* sizeof(float) * 4) / sizeof(uint32_t);
512 SWR_STREAMOUT_CONTEXT soContext
= { 0 };
514 // Setup buffer state pointers.
515 for (uint32_t i
= 0; i
< 4; ++i
)
517 soContext
.pBuffer
[i
] = &state
.soBuffer
[i
];
520 uint32_t numPrims
= pa
.NumPrims();
522 for (uint32_t primIndex
= 0; primIndex
< numPrims
; ++primIndex
)
525 uint32_t soMask
= soState
.streamMasks
[streamIndex
];
527 // Write all entries into primitive data buffer for SOS.
528 while (_BitScanForward(&slot
, soMask
))
530 __m128 attrib
[MAX_NUM_VERTS_PER_PRIM
]; // prim attribs (always 4 wide)
531 uint32_t paSlot
= slot
+ VERTEX_ATTRIB_START_SLOT
;
532 pa
.AssembleSingle(paSlot
, primIndex
, attrib
);
534 // Attribute offset is relative offset from start of vertex.
535 // Note that attributes start at slot 1 in the PA buffer. We need to write this
536 // to prim data starting at slot 0. Which is why we do (slot - 1).
537 // Also note: GL works slightly differently, and needs slot 0
538 uint32_t primDataAttribOffset
= slot
* sizeof(float) * 4 / sizeof(uint32_t);
540 // Store each vertex's attrib at appropriate locations in pPrimData buffer.
541 for (uint32_t v
= 0; v
< soVertsPerPrim
; ++v
)
543 uint32_t* pPrimDataAttrib
= pPrimData
+ primDataAttribOffset
+ (v
* primDataDwordVertexStride
);
545 _mm_store_ps((float*)pPrimDataAttrib
, attrib
[v
]);
548 soMask
&= ~(1 << slot
);
551 // Update pPrimData pointer
552 soContext
.pPrimData
= pPrimData
;
555 SWR_ASSERT(state
.pfnSoFunc
[streamIndex
] != nullptr, "Trying to execute uninitialized streamout jit function.");
556 state
.pfnSoFunc
[streamIndex
](soContext
);
559 // Update SO write offset. The driver provides memory for the update.
560 for (uint32_t i
= 0; i
< 4; ++i
)
562 if (state
.soBuffer
[i
].pWriteOffset
)
564 *state
.soBuffer
[i
].pWriteOffset
= soContext
.pBuffer
[i
]->streamOffset
* sizeof(uint32_t);
567 if (state
.soBuffer
[i
].soWriteEnable
)
569 pDC
->dynState
.SoWriteOffset
[i
] = soContext
.pBuffer
[i
]->streamOffset
* sizeof(uint32_t);
570 pDC
->dynState
.SoWriteOffsetDirty
[i
] = true;
574 UPDATE_STAT_FE(SoPrimStorageNeeded
[streamIndex
], soContext
.numPrimStorageNeeded
);
575 UPDATE_STAT_FE(SoNumPrimsWritten
[streamIndex
], soContext
.numPrimsWritten
);
577 AR_END(FEStreamout
, 1);
580 #if USE_SIMD16_FRONTEND
581 //////////////////////////////////////////////////////////////////////////
582 /// Is value an even number (a multiple of two)
584 template <typename T
>
585 INLINE
static bool IsEven(T value
)
587 return (value
& 1) == 0;
590 //////////////////////////////////////////////////////////////////////////
591 /// Round up value to an even number (a multiple of two)
593 template <typename T
>
594 INLINE
static T
RoundUpEven(T value
)
596 return (value
+ 1) & ~1;
599 //////////////////////////////////////////////////////////////////////////
600 /// Round down value to an even number (a multiple of two)
602 template <typename T
>
603 INLINE
static T
RoundDownEven(T value
)
608 //////////////////////////////////////////////////////////////////////////
609 /// Pack pairs of simdvertexes into simd16vertexes, assume non-overlapping
611 /// vertexCount is in terms of the source simdvertexes and must be even
613 /// attribCount will limit the vector copies to those attribs specified
615 /// note: the stride between vertexes is determinded by SWR_VTX_NUM_SLOTS
617 void PackPairsOfSimdVertexIntoSimd16Vertex(simd16vertex
*vertex_simd16
, const simdvertex
*vertex
, uint32_t vertexCount
, uint32_t attribCount
)
620 SWR_ASSERT(vertex_simd16
);
621 SWR_ASSERT(attribCount
<= SWR_VTX_NUM_SLOTS
);
625 for (uint32_t i
= 0; i
< vertexCount
; i
+= 2)
627 for (uint32_t j
= 0; j
< attribCount
; j
+= 1)
629 for (uint32_t k
= 0; k
< 4; k
+= 1)
631 temp
.attrib
[j
][k
] = _simd16_insert_ps(_simd16_setzero_ps(), vertex
[i
].attrib
[j
][k
], 0);
633 if ((i
+ 1) < vertexCount
)
635 temp
.attrib
[j
][k
] = _simd16_insert_ps(temp
.attrib
[j
][k
], vertex
[i
+ 1].attrib
[j
][k
], 1);
640 for (uint32_t j
= 0; j
< attribCount
; j
+= 1)
642 vertex_simd16
[i
>> 1].attrib
[j
] = temp
.attrib
[j
];
648 //////////////////////////////////////////////////////////////////////////
649 /// @brief Computes number of invocations. The current index represents
650 /// the start of the SIMD. The max index represents how much work
651 /// items are remaining. If there is less then a SIMD's xmin of work
652 /// then return the remaining amount of work.
653 /// @param curIndex - The start index for the SIMD.
654 /// @param maxIndex - The last index for all work items.
655 static INLINE
uint32_t GetNumInvocations(
659 uint32_t remainder
= (maxIndex
- curIndex
);
660 #if USE_SIMD16_FRONTEND
661 return (remainder
>= KNOB_SIMD16_WIDTH
) ? KNOB_SIMD16_WIDTH
: remainder
;
663 return (remainder
>= KNOB_SIMD_WIDTH
) ? KNOB_SIMD_WIDTH
: remainder
;
667 //////////////////////////////////////////////////////////////////////////
668 /// @brief Converts a streamId buffer to a cut buffer for the given stream id.
669 /// The geometry shader will loop over each active streamout buffer, assembling
670 /// primitives for the downstream stages. When multistream output is enabled,
671 /// the generated stream ID buffer from the GS needs to be converted to a cut
672 /// buffer for the primitive assembler.
673 /// @param stream - stream id to generate the cut buffer for
674 /// @param pStreamIdBase - pointer to the stream ID buffer
675 /// @param numEmittedVerts - Number of total verts emitted by the GS
676 /// @param pCutBuffer - output buffer to write cuts to
677 void ProcessStreamIdBuffer(uint32_t stream
, uint8_t* pStreamIdBase
, uint32_t numEmittedVerts
, uint8_t *pCutBuffer
)
679 SWR_ASSERT(stream
< MAX_SO_STREAMS
);
681 uint32_t numInputBytes
= (numEmittedVerts
* 2 + 7) / 8;
682 uint32_t numOutputBytes
= std::max(numInputBytes
/ 2, 1U);
684 for (uint32_t b
= 0; b
< numOutputBytes
; ++b
)
686 uint8_t curInputByte
= pStreamIdBase
[2*b
];
688 for (uint32_t i
= 0; i
< 4; ++i
)
690 if ((curInputByte
& 0x3) != stream
)
697 curInputByte
= pStreamIdBase
[2 * b
+ 1];
698 for (uint32_t i
= 0; i
< 4; ++i
)
700 if ((curInputByte
& 0x3) != stream
)
702 outByte
|= (1 << (i
+ 4));
707 *pCutBuffer
++ = outByte
;
711 THREAD SWR_GS_CONTEXT tlsGsContext
;
713 template<typename SIMDVERTEX
, uint32_t SIMD_WIDTH
>
716 GsBufferInfo(const SWR_GS_STATE
&gsState
)
718 const uint32_t vertexCount
= gsState
.maxNumVerts
;
719 const uint32_t vertexStride
= sizeof(SIMDVERTEX
);
720 const uint32_t numSimdBatches
= (vertexCount
+ SIMD_WIDTH
- 1) / SIMD_WIDTH
;
722 vertexPrimitiveStride
= vertexStride
* numSimdBatches
;
723 vertexInstanceStride
= vertexPrimitiveStride
* SIMD_WIDTH
;
725 if (gsState
.isSingleStream
)
727 cutPrimitiveStride
= (vertexCount
+ 7) / 8;
728 cutInstanceStride
= cutPrimitiveStride
* SIMD_WIDTH
;
730 streamCutPrimitiveStride
= 0;
731 streamCutInstanceStride
= 0;
735 cutPrimitiveStride
= AlignUp(vertexCount
* 2 / 8, 4);
736 cutInstanceStride
= cutPrimitiveStride
* SIMD_WIDTH
;
738 streamCutPrimitiveStride
= (vertexCount
+ 7) / 8;
739 streamCutInstanceStride
= streamCutPrimitiveStride
* SIMD_WIDTH
;
743 uint32_t vertexPrimitiveStride
;
744 uint32_t vertexInstanceStride
;
746 uint32_t cutPrimitiveStride
;
747 uint32_t cutInstanceStride
;
749 uint32_t streamCutPrimitiveStride
;
750 uint32_t streamCutInstanceStride
;
753 //////////////////////////////////////////////////////////////////////////
754 /// @brief Implements GS stage.
755 /// @param pDC - pointer to draw context.
756 /// @param workerId - thread's worker id. Even thread has a unique id.
757 /// @param pa - The primitive assembly object.
758 /// @param pGsOut - output stream for GS
760 typename HasStreamOutT
,
762 static void GeometryShaderStage(
768 void* pStreamCutBuffer
,
769 uint32_t* pSoPrimData
,
770 #if USE_SIMD16_FRONTEND
771 uint32_t numPrims_simd8
,
775 SWR_CONTEXT
*pContext
= pDC
->pContext
;
777 AR_BEGIN(FEGeometryShader
, pDC
->drawId
);
779 const API_STATE
& state
= GetApiState(pDC
);
780 const SWR_GS_STATE
* pState
= &state
.gsState
;
782 SWR_ASSERT(pGsOut
!= nullptr, "GS output buffer should be initialized");
783 SWR_ASSERT(pCutBuffer
!= nullptr, "GS output cut buffer should be initialized");
785 tlsGsContext
.pStream
= (uint8_t*)pGsOut
;
786 tlsGsContext
.pCutOrStreamIdBuffer
= (uint8_t*)pCutBuffer
;
787 tlsGsContext
.PrimitiveID
= primID
;
789 uint32_t numVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, true);
790 simdvector attrib
[MAX_ATTRIBUTES
];
792 // assemble all attributes for the input primitive
793 for (uint32_t slot
= 0; slot
< pState
->numInputAttribs
; ++slot
)
795 uint32_t attribSlot
= VERTEX_ATTRIB_START_SLOT
+ slot
;
796 pa
.Assemble(attribSlot
, attrib
);
798 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
800 tlsGsContext
.vert
[i
].attrib
[attribSlot
] = attrib
[i
];
805 pa
.Assemble(VERTEX_POSITION_SLOT
, attrib
);
806 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
808 tlsGsContext
.vert
[i
].attrib
[VERTEX_POSITION_SLOT
] = attrib
[i
];
811 #if USE_SIMD16_FRONTEND
812 const GsBufferInfo
<simd16vertex
, KNOB_SIMD16_WIDTH
> bufferInfo(state
.gsState
);
814 const GsBufferInfo
<simdvertex
, KNOB_SIMD_WIDTH
> bufferInfo(state
.gsState
);
817 // record valid prims from the frontend to avoid over binning the newly generated
819 #if USE_SIMD16_FRONTEND
820 uint32_t numInputPrims
= numPrims_simd8
;
822 uint32_t numInputPrims
= pa
.NumPrims();
825 for (uint32_t instance
= 0; instance
< pState
->instanceCount
; ++instance
)
827 tlsGsContext
.InstanceID
= instance
;
828 tlsGsContext
.mask
= GenerateMask(numInputPrims
);
830 // execute the geometry shader
831 state
.pfnGsFunc(GetPrivateState(pDC
), &tlsGsContext
);
833 tlsGsContext
.pStream
+= bufferInfo
.vertexInstanceStride
;
834 tlsGsContext
.pCutOrStreamIdBuffer
+= bufferInfo
.cutInstanceStride
;
837 // set up new binner and state for the GS output topology
838 #if USE_SIMD16_FRONTEND
839 PFN_PROCESS_PRIMS_SIMD16 pfnClipFunc
= nullptr;
842 switch (pState
->outputTopology
)
844 case TOP_TRIANGLE_STRIP
: pfnClipFunc
= ClipTriangles_simd16
; break;
845 case TOP_LINE_STRIP
: pfnClipFunc
= ClipLines_simd16
; break;
846 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints_simd16
; break;
847 default: SWR_INVALID("Unexpected GS output topology: %d", pState
->outputTopology
);
852 PFN_PROCESS_PRIMS pfnClipFunc
= nullptr;
855 switch (pState
->outputTopology
)
857 case TOP_TRIANGLE_STRIP
: pfnClipFunc
= ClipTriangles
; break;
858 case TOP_LINE_STRIP
: pfnClipFunc
= ClipLines
; break;
859 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints
; break;
860 default: SWR_INVALID("Unexpected GS output topology: %d", pState
->outputTopology
);
865 // foreach input prim:
866 // - setup a new PA based on the emitted verts for that prim
867 // - loop over the new verts, calling PA to assemble each prim
868 uint32_t* pVertexCount
= (uint32_t*)&tlsGsContext
.vertexCount
;
869 uint32_t* pPrimitiveId
= (uint32_t*)&primID
;
871 uint32_t totalPrimsGenerated
= 0;
872 for (uint32_t inputPrim
= 0; inputPrim
< numInputPrims
; ++inputPrim
)
874 uint8_t* pInstanceBase
= (uint8_t*)pGsOut
+ inputPrim
* bufferInfo
.vertexPrimitiveStride
;
875 uint8_t* pCutBufferBase
= (uint8_t*)pCutBuffer
+ inputPrim
* bufferInfo
.cutPrimitiveStride
;
877 for (uint32_t instance
= 0; instance
< pState
->instanceCount
; ++instance
)
879 uint32_t numEmittedVerts
= pVertexCount
[inputPrim
];
880 if (numEmittedVerts
== 0)
885 uint8_t* pBase
= pInstanceBase
+ instance
* bufferInfo
.vertexInstanceStride
;
886 uint8_t* pCutBase
= pCutBufferBase
+ instance
* bufferInfo
.cutInstanceStride
;
888 uint32_t numAttribs
= state
.feNumAttributes
;
890 for (uint32_t stream
= 0; stream
< MAX_SO_STREAMS
; ++stream
)
892 bool processCutVerts
= false;
894 uint8_t* pCutBuffer
= pCutBase
;
896 // assign default stream ID, only relevant when GS is outputting a single stream
897 uint32_t streamID
= 0;
898 if (pState
->isSingleStream
)
900 processCutVerts
= true;
901 streamID
= pState
->singleStreamID
;
902 if (streamID
!= stream
) continue;
906 // early exit if this stream is not enabled for streamout
907 if (HasStreamOutT::value
&& !state
.soState
.streamEnable
[stream
])
912 // multi-stream output, need to translate StreamID buffer to a cut buffer
913 ProcessStreamIdBuffer(stream
, pCutBase
, numEmittedVerts
, (uint8_t*)pStreamCutBuffer
);
914 pCutBuffer
= (uint8_t*)pStreamCutBuffer
;
915 processCutVerts
= false;
918 #if USE_SIMD16_FRONTEND
919 PA_STATE_CUT
gsPa(pDC
, pBase
, numEmittedVerts
, reinterpret_cast<simd16mask
*>(pCutBuffer
), numEmittedVerts
, numAttribs
, pState
->outputTopology
, processCutVerts
);
922 PA_STATE_CUT
gsPa(pDC
, pBase
, numEmittedVerts
, pCutBuffer
, numEmittedVerts
, numAttribs
, pState
->outputTopology
, processCutVerts
);
925 while (gsPa
.GetNextStreamOutput())
929 #if USE_SIMD16_FRONTEND
930 simd16vector attrib_simd16
[3];
932 bool assemble
= gsPa
.Assemble_simd16(VERTEX_POSITION_SLOT
, attrib_simd16
);
935 bool assemble
= gsPa
.Assemble(VERTEX_POSITION_SLOT
, attrib
);
940 totalPrimsGenerated
+= gsPa
.NumPrims();
942 if (HasStreamOutT::value
)
944 gsPa
.useAlternateOffset
= false;
945 StreamOut(pDC
, gsPa
, workerId
, pSoPrimData
, stream
);
948 if (HasRastT::value
&& state
.soState
.streamToRasterizer
== stream
)
950 #if USE_SIMD16_FRONTEND
951 simd16scalari vPrimId
;
952 // pull primitiveID from the GS output if available
953 if (state
.gsState
.emitsPrimitiveID
)
955 simd16vector primIdAttrib
[3];
956 gsPa
.Assemble_simd16(VERTEX_PRIMID_SLOT
, primIdAttrib
);
957 vPrimId
= _simd16_castps_si(primIdAttrib
[state
.frontendState
.topologyProvokingVertex
].x
);
961 vPrimId
= _simd16_set1_epi32(pPrimitiveId
[inputPrim
]);
964 // use viewport array index if GS declares it as an output attribute. Otherwise use index 0.
965 simd16scalari vViewPortIdx
;
966 if (state
.gsState
.emitsViewportArrayIndex
)
968 simd16vector vpiAttrib
[3];
969 gsPa
.Assemble_simd16(VERTEX_VIEWPORT_ARRAY_INDEX_SLOT
, vpiAttrib
);
971 // OOB indices => forced to zero.
972 simd16scalari vNumViewports
= _simd16_set1_epi32(KNOB_NUM_VIEWPORTS_SCISSORS
);
973 simd16scalari vClearMask
= _simd16_cmplt_epi32(_simd16_castps_si(vpiAttrib
[0].x
), vNumViewports
);
974 vpiAttrib
[0].x
= _simd16_and_ps(_simd16_castsi_ps(vClearMask
), vpiAttrib
[0].x
);
976 vViewPortIdx
= _simd16_castps_si(vpiAttrib
[0].x
);
980 vViewPortIdx
= _simd16_set1_epi32(0);
983 gsPa
.useAlternateOffset
= false;
984 pfnClipFunc(pDC
, gsPa
, workerId
, attrib_simd16
, GenMask(gsPa
.NumPrims()), vPrimId
, vViewPortIdx
);
987 // pull primitiveID from the GS output if available
988 if (state
.gsState
.emitsPrimitiveID
)
990 simdvector primIdAttrib
[3];
991 gsPa
.Assemble(VERTEX_PRIMID_SLOT
, primIdAttrib
);
992 vPrimId
= _simd_castps_si(primIdAttrib
[state
.frontendState
.topologyProvokingVertex
].x
);
996 vPrimId
= _simd_set1_epi32(pPrimitiveId
[inputPrim
]);
999 // use viewport array index if GS declares it as an output attribute. Otherwise use index 0.
1000 simdscalari vViewPortIdx
;
1001 if (state
.gsState
.emitsViewportArrayIndex
)
1003 simdvector vpiAttrib
[3];
1004 gsPa
.Assemble(VERTEX_VIEWPORT_ARRAY_INDEX_SLOT
, vpiAttrib
);
1006 // OOB indices => forced to zero.
1007 simdscalari vNumViewports
= _simd_set1_epi32(KNOB_NUM_VIEWPORTS_SCISSORS
);
1008 simdscalari vClearMask
= _simd_cmplt_epi32(_simd_castps_si(vpiAttrib
[0].x
), vNumViewports
);
1009 vpiAttrib
[0].x
= _simd_and_ps(_simd_castsi_ps(vClearMask
), vpiAttrib
[0].x
);
1011 vViewPortIdx
= _simd_castps_si(vpiAttrib
[0].x
);
1015 vViewPortIdx
= _simd_set1_epi32(0);
1018 pfnClipFunc(pDC
, gsPa
, workerId
, attrib
, GenMask(gsPa
.NumPrims()), vPrimId
, vViewPortIdx
);
1022 } while (gsPa
.NextPrim());
1028 // update GS pipeline stats
1029 UPDATE_STAT_FE(GsInvocations
, numInputPrims
* pState
->instanceCount
);
1030 UPDATE_STAT_FE(GsPrimitives
, totalPrimsGenerated
);
1031 AR_EVENT(GSPrimInfo(numInputPrims
, totalPrimsGenerated
, numVertsPerPrim
*numInputPrims
));
1032 AR_END(FEGeometryShader
, 1);
1035 //////////////////////////////////////////////////////////////////////////
1036 /// @brief Allocate GS buffers
1037 /// @param pDC - pointer to draw context.
1038 /// @param state - API state
1039 /// @param ppGsOut - pointer to GS output buffer allocation
1040 /// @param ppCutBuffer - pointer to GS output cut buffer allocation
1041 template<typename SIMDVERTEX
, uint32_t SIMD_WIDTH
>
1042 static INLINE
void AllocateGsBuffers(DRAW_CONTEXT
* pDC
, const API_STATE
& state
, void** ppGsOut
, void** ppCutBuffer
,
1043 void **ppStreamCutBuffer
)
1045 auto pArena
= pDC
->pArena
;
1046 SWR_ASSERT(pArena
!= nullptr);
1047 SWR_ASSERT(state
.gsState
.gsEnable
);
1049 // allocate arena space to hold GS output verts
1050 // @todo pack attribs
1051 // @todo support multiple streams
1053 const GsBufferInfo
<SIMDVERTEX
, SIMD_WIDTH
> bufferInfo(state
.gsState
);
1055 const uint32_t vertexBufferSize
= state
.gsState
.instanceCount
* bufferInfo
.vertexInstanceStride
;
1057 *ppGsOut
= pArena
->AllocAligned(vertexBufferSize
, SIMD_WIDTH
* sizeof(float));
1059 // allocate arena space to hold cut or streamid buffer, which is essentially a bitfield sized to the
1060 // maximum vertex output as defined by the GS state, per SIMD lane, per GS instance
1062 // allocate space for temporary per-stream cut buffer if multi-stream is enabled
1063 if (state
.gsState
.isSingleStream
)
1065 const uint32_t cutBufferSize
= state
.gsState
.instanceCount
* bufferInfo
.cutInstanceStride
;
1067 *ppCutBuffer
= pArena
->AllocAligned(cutBufferSize
, SIMD_WIDTH
* sizeof(float));
1068 *ppStreamCutBuffer
= nullptr;
1072 const uint32_t cutBufferSize
= state
.gsState
.instanceCount
* bufferInfo
.cutInstanceStride
;
1073 const uint32_t streamCutBufferSize
= state
.gsState
.instanceCount
* bufferInfo
.streamCutInstanceStride
;
1075 *ppCutBuffer
= pArena
->AllocAligned(cutBufferSize
, SIMD_WIDTH
* sizeof(float));
1076 *ppStreamCutBuffer
= pArena
->AllocAligned(streamCutBufferSize
, SIMD_WIDTH
* sizeof(float));
1080 //////////////////////////////////////////////////////////////////////////
1081 /// @brief Contains all data generated by the HS and passed to the
1082 /// tessellator and DS.
1083 struct TessellationThreadLocalData
1085 SWR_HS_CONTEXT hsContext
;
1086 ScalarPatch patchData
[KNOB_SIMD_WIDTH
];
1090 simdscalar
* pDSOutput
;
1091 size_t numDSOutputVectors
;
1094 THREAD TessellationThreadLocalData
* gt_pTessellationThreadData
= nullptr;
1096 //////////////////////////////////////////////////////////////////////////
1097 /// @brief Allocate tessellation data for this worker thread.
1099 static void AllocateTessellationData(SWR_CONTEXT
* pContext
)
1101 /// @TODO - Don't use thread local storage. Use Worker local storage instead.
1102 if (gt_pTessellationThreadData
== nullptr)
1104 gt_pTessellationThreadData
= (TessellationThreadLocalData
*)
1105 AlignedMalloc(sizeof(TessellationThreadLocalData
), 64);
1106 memset(gt_pTessellationThreadData
, 0, sizeof(*gt_pTessellationThreadData
));
1110 //////////////////////////////////////////////////////////////////////////
1111 /// @brief Implements Tessellation Stages.
1112 /// @param pDC - pointer to draw context.
1113 /// @param workerId - thread's worker id. Even thread has a unique id.
1114 /// @param pa - The primitive assembly object.
1115 /// @param pGsOut - output stream for GS
1117 typename HasGeometryShaderT
,
1118 typename HasStreamOutT
,
1120 static void TessellationStages(
1126 void* pCutStreamBuffer
,
1127 uint32_t* pSoPrimData
,
1128 #if USE_SIMD16_FRONTEND
1129 uint32_t numPrims_simd8
,
1133 SWR_CONTEXT
*pContext
= pDC
->pContext
;
1134 const API_STATE
& state
= GetApiState(pDC
);
1135 const SWR_TS_STATE
& tsState
= state
.tsState
;
1137 SWR_ASSERT(gt_pTessellationThreadData
);
1139 HANDLE tsCtx
= TSInitCtx(
1141 tsState
.partitioning
,
1142 tsState
.tsOutputTopology
,
1143 gt_pTessellationThreadData
->pTxCtx
,
1144 gt_pTessellationThreadData
->tsCtxSize
);
1145 if (tsCtx
== nullptr)
1147 gt_pTessellationThreadData
->pTxCtx
= AlignedMalloc(gt_pTessellationThreadData
->tsCtxSize
, 64);
1150 tsState
.partitioning
,
1151 tsState
.tsOutputTopology
,
1152 gt_pTessellationThreadData
->pTxCtx
,
1153 gt_pTessellationThreadData
->tsCtxSize
);
1157 #if USE_SIMD16_FRONTEND
1158 PFN_PROCESS_PRIMS_SIMD16 pfnClipFunc
= nullptr;
1159 if (HasRastT::value
)
1161 switch (tsState
.postDSTopology
)
1163 case TOP_TRIANGLE_LIST
: pfnClipFunc
= ClipTriangles_simd16
; break;
1164 case TOP_LINE_LIST
: pfnClipFunc
= ClipLines_simd16
; break;
1165 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints_simd16
; break;
1166 default: SWR_INVALID("Unexpected DS output topology: %d", tsState
.postDSTopology
);
1171 PFN_PROCESS_PRIMS pfnClipFunc
= nullptr;
1172 if (HasRastT::value
)
1174 switch (tsState
.postDSTopology
)
1176 case TOP_TRIANGLE_LIST
: pfnClipFunc
= ClipTriangles
; break;
1177 case TOP_LINE_LIST
: pfnClipFunc
= ClipLines
; break;
1178 case TOP_POINT_LIST
: pfnClipFunc
= ClipPoints
; break;
1179 default: SWR_INVALID("Unexpected DS output topology: %d", tsState
.postDSTopology
);
1184 SWR_HS_CONTEXT
& hsContext
= gt_pTessellationThreadData
->hsContext
;
1185 hsContext
.pCPout
= gt_pTessellationThreadData
->patchData
;
1186 hsContext
.PrimitiveID
= primID
;
1188 uint32_t numVertsPerPrim
= NumVertsPerPrim(pa
.binTopology
, false);
1189 // Max storage for one attribute for an entire simdprimitive
1190 simdvector simdattrib
[MAX_NUM_VERTS_PER_PRIM
];
1192 // assemble all attributes for the input primitives
1193 for (uint32_t slot
= 0; slot
< tsState
.numHsInputAttribs
; ++slot
)
1195 uint32_t attribSlot
= VERTEX_ATTRIB_START_SLOT
+ slot
;
1196 pa
.Assemble(attribSlot
, simdattrib
);
1198 for (uint32_t i
= 0; i
< numVertsPerPrim
; ++i
)
1200 hsContext
.vert
[i
].attrib
[attribSlot
] = simdattrib
[i
];
1205 memset(hsContext
.pCPout
, 0x90, sizeof(ScalarPatch
) * KNOB_SIMD_WIDTH
);
1208 #if USE_SIMD16_FRONTEND
1209 uint32_t numPrims
= numPrims_simd8
;
1211 uint32_t numPrims
= pa
.NumPrims();
1213 hsContext
.mask
= GenerateMask(numPrims
);
1216 AR_BEGIN(FEHullShader
, pDC
->drawId
);
1217 state
.pfnHsFunc(GetPrivateState(pDC
), &hsContext
);
1218 AR_END(FEHullShader
, 0);
1220 UPDATE_STAT_FE(HsInvocations
, numPrims
);
1222 const uint32_t* pPrimId
= (const uint32_t*)&primID
;
1224 for (uint32_t p
= 0; p
< numPrims
; ++p
)
1227 SWR_TS_TESSELLATED_DATA tsData
= { 0 };
1228 AR_BEGIN(FETessellation
, pDC
->drawId
);
1229 TSTessellate(tsCtx
, hsContext
.pCPout
[p
].tessFactors
, tsData
);
1230 AR_EVENT(TessPrimCount(1));
1231 AR_END(FETessellation
, 0);
1233 if (tsData
.NumPrimitives
== 0)
1237 SWR_ASSERT(tsData
.NumDomainPoints
);
1239 // Allocate DS Output memory
1240 uint32_t requiredDSVectorInvocations
= AlignUp(tsData
.NumDomainPoints
, KNOB_SIMD_WIDTH
) / KNOB_SIMD_WIDTH
;
1241 size_t requiredDSOutputVectors
= requiredDSVectorInvocations
* tsState
.numDsOutputAttribs
;
1242 #if USE_SIMD16_FRONTEND
1243 size_t requiredAllocSize
= sizeof(simdvector
) * RoundUpEven(requiredDSVectorInvocations
) * tsState
.numDsOutputAttribs
; // simd8 -> simd16, padding
1245 size_t requiredAllocSize
= sizeof(simdvector
) * requiredDSOutputVectors
;
1247 if (requiredDSOutputVectors
> gt_pTessellationThreadData
->numDSOutputVectors
)
1249 AlignedFree(gt_pTessellationThreadData
->pDSOutput
);
1250 gt_pTessellationThreadData
->pDSOutput
= (simdscalar
*)AlignedMalloc(requiredAllocSize
, 64);
1251 #if USE_SIMD16_FRONTEND
1252 gt_pTessellationThreadData
->numDSOutputVectors
= RoundUpEven(requiredDSVectorInvocations
) * tsState
.numDsOutputAttribs
; // simd8 -> simd16, padding
1254 gt_pTessellationThreadData
->numDSOutputVectors
= requiredDSOutputVectors
;
1257 SWR_ASSERT(gt_pTessellationThreadData
->pDSOutput
);
1258 SWR_ASSERT(gt_pTessellationThreadData
->numDSOutputVectors
>= requiredDSOutputVectors
);
1261 memset(gt_pTessellationThreadData
->pDSOutput
, 0x90, requiredAllocSize
);
1264 // Run Domain Shader
1265 SWR_DS_CONTEXT dsContext
;
1266 dsContext
.PrimitiveID
= pPrimId
[p
];
1267 dsContext
.pCpIn
= &hsContext
.pCPout
[p
];
1268 dsContext
.pDomainU
= (simdscalar
*)tsData
.pDomainPointsU
;
1269 dsContext
.pDomainV
= (simdscalar
*)tsData
.pDomainPointsV
;
1270 dsContext
.pOutputData
= gt_pTessellationThreadData
->pDSOutput
;
1271 #if USE_SIMD16_FRONTEND
1272 dsContext
.vectorStride
= RoundUpEven(requiredDSVectorInvocations
); // simd8 -> simd16
1274 dsContext
.vectorStride
= requiredDSVectorInvocations
;
1277 uint32_t dsInvocations
= 0;
1279 for (dsContext
.vectorOffset
= 0; dsContext
.vectorOffset
< requiredDSVectorInvocations
; ++dsContext
.vectorOffset
)
1281 dsContext
.mask
= GenerateMask(tsData
.NumDomainPoints
- dsInvocations
);
1283 AR_BEGIN(FEDomainShader
, pDC
->drawId
);
1284 state
.pfnDsFunc(GetPrivateState(pDC
), &dsContext
);
1285 AR_END(FEDomainShader
, 0);
1287 dsInvocations
+= KNOB_SIMD_WIDTH
;
1289 UPDATE_STAT_FE(DsInvocations
, tsData
.NumDomainPoints
);
1291 #if USE_SIMD16_FRONTEND
1292 SWR_ASSERT(IsEven(dsContext
.vectorStride
)); // simd8 -> simd16
1297 #if USE_SIMD16_FRONTEND
1298 reinterpret_cast<const simd16scalar
*>(dsContext
.pOutputData
), // simd8 -> simd16
1299 dsContext
.vectorStride
/ 2, // simd8 -> simd16
1301 dsContext
.pOutputData
,
1302 dsContext
.vectorStride
,
1304 tsState
.numDsOutputAttribs
,
1306 tsData
.NumPrimitives
,
1307 tsState
.postDSTopology
);
1309 while (tessPa
.HasWork())
1311 #if USE_SIMD16_FRONTEND
1312 const uint32_t numPrims
= tessPa
.NumPrims();
1313 const uint32_t numPrims_lo
= std::min
<uint32_t>(numPrims
, KNOB_SIMD_WIDTH
);
1314 const uint32_t numPrims_hi
= std::max
<uint32_t>(numPrims
, KNOB_SIMD_WIDTH
) - KNOB_SIMD_WIDTH
;
1316 const simd16scalari primID
= _simd16_set1_epi32(dsContext
.PrimitiveID
);
1317 const simdscalari primID_lo
= _simd16_extract_si(primID
, 0);
1318 const simdscalari primID_hi
= _simd16_extract_si(primID
, 1);
1321 if (HasGeometryShaderT::value
)
1323 #if USE_SIMD16_FRONTEND
1324 tessPa
.useAlternateOffset
= false;
1325 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(pDC
, workerId
, tessPa
, pGsOut
, pCutBuffer
, pCutStreamBuffer
, pSoPrimData
, numPrims_lo
, primID_lo
);
1329 tessPa
.useAlternateOffset
= true;
1330 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(pDC
, workerId
, tessPa
, pGsOut
, pCutBuffer
, pCutStreamBuffer
, pSoPrimData
, numPrims_hi
, primID_hi
);
1333 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(
1334 pDC
, workerId
, tessPa
, pGsOut
, pCutBuffer
, pCutStreamBuffer
, pSoPrimData
,
1335 _simd_set1_epi32(dsContext
.PrimitiveID
));
1340 if (HasStreamOutT::value
)
1342 tessPa
.useAlternateOffset
= false;
1343 StreamOut(pDC
, tessPa
, workerId
, pSoPrimData
, 0);
1346 if (HasRastT::value
)
1348 #if USE_SIMD16_FRONTEND
1349 simd16vector prim_simd16
[3]; // Only deal with triangles, lines, or points
1351 simdvector prim
[3]; // Only deal with triangles, lines, or points
1353 AR_BEGIN(FEPAAssemble
, pDC
->drawId
);
1355 #if USE_SIMD16_FRONTEND
1356 tessPa
.Assemble_simd16(VERTEX_POSITION_SLOT
, prim_simd16
);
1358 tessPa
.Assemble(VERTEX_POSITION_SLOT
, prim
);
1360 AR_END(FEPAAssemble
, 1);
1361 SWR_ASSERT(assemble
);
1363 SWR_ASSERT(pfnClipFunc
);
1364 #if USE_SIMD16_FRONTEND
1365 tessPa
.useAlternateOffset
= false;
1366 pfnClipFunc(pDC
, tessPa
, workerId
, prim_simd16
, GenMask(numPrims
), primID
, _simd16_set1_epi32(0));
1368 pfnClipFunc(pDC
, tessPa
, workerId
, prim
,
1369 GenMask(tessPa
.NumPrims()), _simd_set1_epi32(dsContext
.PrimitiveID
), _simd_set1_epi32(0));
1376 } // while (tessPa.HasWork())
1377 } // for (uint32_t p = 0; p < numPrims; ++p)
1379 #if USE_SIMD16_FRONTEND
1380 if (gt_pTessellationThreadData
->pDSOutput
!= nullptr)
1382 AlignedFree(gt_pTessellationThreadData
->pDSOutput
);
1383 gt_pTessellationThreadData
->pDSOutput
= nullptr;
1385 gt_pTessellationThreadData
->numDSOutputVectors
= 0;
1388 TSDestroyCtx(tsCtx
);
1391 THREAD
PA_STATE::SIMDVERTEX
*pVertexStore
= nullptr;
1392 THREAD
uint32_t gVertexStoreSize
= 0;
1394 //////////////////////////////////////////////////////////////////////////
1395 /// @brief FE handler for SwrDraw.
1396 /// @tparam IsIndexedT - Is indexed drawing enabled
1397 /// @tparam HasTessellationT - Is tessellation enabled
1398 /// @tparam HasGeometryShaderT::value - Is the geometry shader stage enabled
1399 /// @tparam HasStreamOutT - Is stream-out enabled
1400 /// @tparam HasRastT - Is rasterization enabled
1401 /// @param pContext - pointer to SWR context.
1402 /// @param pDC - pointer to draw context.
1403 /// @param workerId - thread's worker id.
1404 /// @param pUserData - Pointer to DRAW_WORK
1406 typename IsIndexedT
,
1407 typename IsCutIndexEnabledT
,
1408 typename HasTessellationT
,
1409 typename HasGeometryShaderT
,
1410 typename HasStreamOutT
,
1413 SWR_CONTEXT
*pContext
,
1419 #if KNOB_ENABLE_TOSS_POINTS
1420 if (KNOB_TOSS_QUEUE_FE
)
1426 AR_BEGIN(FEProcessDraw
, pDC
->drawId
);
1428 DRAW_WORK
& work
= *(DRAW_WORK
*)pUserData
;
1429 const API_STATE
& state
= GetApiState(pDC
);
1431 uint32_t indexSize
= 0;
1432 uint32_t endVertex
= work
.numVerts
;
1434 const int32_t* pLastRequestedIndex
= nullptr;
1435 if (IsIndexedT::value
)
1440 indexSize
= sizeof(uint32_t);
1441 pLastRequestedIndex
= &(work
.pIB
[endVertex
]);
1444 indexSize
= sizeof(uint16_t);
1445 // nasty address offset to last index
1446 pLastRequestedIndex
= (int32_t*)(&(((uint16_t*)work
.pIB
)[endVertex
]));
1449 indexSize
= sizeof(uint8_t);
1450 // nasty address offset to last index
1451 pLastRequestedIndex
= (int32_t*)(&(((uint8_t*)work
.pIB
)[endVertex
]));
1454 SWR_INVALID("Invalid work.type: %d", work
.type
);
1459 // No cuts, prune partial primitives.
1460 endVertex
= GetNumVerts(state
.topology
, GetNumPrims(state
.topology
, work
.numVerts
));
1463 #if defined(KNOB_ENABLE_RDTSC) || defined(KNOB_ENABLE_AR)
1464 uint32_t numPrims
= GetNumPrims(state
.topology
, work
.numVerts
);
1467 void* pGsOut
= nullptr;
1468 void* pCutBuffer
= nullptr;
1469 void* pStreamCutBuffer
= nullptr;
1470 if (HasGeometryShaderT::value
)
1472 #if USE_SIMD16_FRONTEND
1473 AllocateGsBuffers
<simd16vertex
, KNOB_SIMD16_WIDTH
>(pDC
, state
, &pGsOut
, &pCutBuffer
, &pStreamCutBuffer
);
1475 AllocateGsBuffers
<simdvertex
, KNOB_SIMD_WIDTH
>(pDC
, state
, &pGsOut
, &pCutBuffer
, &pStreamCutBuffer
);
1479 if (HasTessellationT::value
)
1481 SWR_ASSERT(state
.tsState
.tsEnable
== true);
1482 SWR_ASSERT(state
.pfnHsFunc
!= nullptr);
1483 SWR_ASSERT(state
.pfnDsFunc
!= nullptr);
1485 AllocateTessellationData(pContext
);
1489 SWR_ASSERT(state
.tsState
.tsEnable
== false);
1490 SWR_ASSERT(state
.pfnHsFunc
== nullptr);
1491 SWR_ASSERT(state
.pfnDsFunc
== nullptr);
1494 // allocate space for streamout input prim data
1495 uint32_t* pSoPrimData
= nullptr;
1496 if (HasStreamOutT::value
)
1498 pSoPrimData
= (uint32_t*)pDC
->pArena
->AllocAligned(4096, 16);
1501 const uint32_t vertexCount
= NumVertsPerPrim(state
.topology
, state
.gsState
.gsEnable
);
1503 SWR_ASSERT(vertexCount
<= MAX_NUM_VERTS_PER_PRIM
);
1505 // grow the vertex store for the PA as necessary
1506 if (gVertexStoreSize
< vertexCount
)
1508 if (pVertexStore
!= nullptr)
1510 AlignedFree(pVertexStore
);
1513 while (gVertexStoreSize
< vertexCount
)
1515 #if USE_SIMD16_FRONTEND
1516 gVertexStoreSize
+= 4; // grow in chunks of 4 simd16vertex
1518 gVertexStoreSize
+= 8; // grow in chunks of 8 simdvertex
1522 SWR_ASSERT(gVertexStoreSize
<= MAX_NUM_VERTS_PER_PRIM
);
1524 pVertexStore
= reinterpret_cast<PA_STATE::SIMDVERTEX
*>(AlignedMalloc(gVertexStoreSize
* sizeof(pVertexStore
[0]), 64));
1526 SWR_ASSERT(pVertexStore
!= nullptr);
1529 // choose primitive assembler
1530 PA_FACTORY
<IsIndexedT
, IsCutIndexEnabledT
> paFactory(pDC
, state
.topology
, work
.numVerts
, pVertexStore
, gVertexStoreSize
);
1531 PA_STATE
& pa
= paFactory
.GetPA();
1533 #if USE_SIMD16_FRONTEND
1536 SWR_VS_CONTEXT vsContext_lo
;
1537 SWR_VS_CONTEXT vsContext_hi
;
1539 vsContext_lo
.pVin
= &vin_lo
;
1540 vsContext_hi
.pVin
= &vin_hi
;
1541 vsContext_lo
.AlternateOffset
= 0;
1542 vsContext_hi
.AlternateOffset
= 1;
1544 SWR_FETCH_CONTEXT fetchInfo_lo
= { 0 };
1546 fetchInfo_lo
.pStreams
= &state
.vertexBuffers
[0];
1547 fetchInfo_lo
.StartInstance
= work
.startInstance
;
1548 fetchInfo_lo
.StartVertex
= 0;
1550 if (IsIndexedT::value
)
1552 fetchInfo_lo
.BaseVertex
= work
.baseVertex
;
1554 // if the entire index buffer isn't being consumed, set the last index
1555 // so that fetches < a SIMD wide will be masked off
1556 fetchInfo_lo
.pLastIndex
= (const int32_t*)(((uint8_t*)state
.indexBuffer
.pIndices
) + state
.indexBuffer
.size
);
1557 if (pLastRequestedIndex
< fetchInfo_lo
.pLastIndex
)
1559 fetchInfo_lo
.pLastIndex
= pLastRequestedIndex
;
1564 fetchInfo_lo
.StartVertex
= work
.startVertex
;
1567 SWR_FETCH_CONTEXT fetchInfo_hi
= fetchInfo_lo
;
1569 const simd16scalari vScale
= _simd16_set_epi32(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
1571 for (uint32_t instanceNum
= 0; instanceNum
< work
.numInstances
; instanceNum
++)
1575 simd16scalari vIndex
;
1577 if (IsIndexedT::value
)
1579 fetchInfo_lo
.pIndices
= work
.pIB
;
1580 fetchInfo_hi
.pIndices
= (int32_t *)((uint8_t *)fetchInfo_lo
.pIndices
+ KNOB_SIMD_WIDTH
* indexSize
); // 1/2 of KNOB_SIMD16_WIDTH
1584 vIndex
= _simd16_add_epi32(_simd16_set1_epi32(work
.startVertexID
), vScale
);
1586 fetchInfo_lo
.pIndices
= (const int32_t *)&vIndex
;
1587 fetchInfo_hi
.pIndices
= (const int32_t *)&vIndex
+ KNOB_SIMD_WIDTH
; // 1/2 of KNOB_SIMD16_WIDTH
1590 fetchInfo_lo
.CurInstance
= instanceNum
;
1591 fetchInfo_hi
.CurInstance
= instanceNum
;
1593 vsContext_lo
.InstanceID
= instanceNum
;
1594 vsContext_hi
.InstanceID
= instanceNum
;
1596 while (pa
.HasWork())
1598 // GetNextVsOutput currently has the side effect of updating some PA state machine state.
1599 // So we need to keep this outside of (i < endVertex) check.
1601 simdmask
*pvCutIndices_lo
= nullptr;
1602 simdmask
*pvCutIndices_hi
= nullptr;
1604 if (IsIndexedT::value
)
1606 // simd16mask <=> simdmask[2]
1608 pvCutIndices_lo
= &reinterpret_cast<simdmask
*>(&pa
.GetNextVsIndices())[0];
1609 pvCutIndices_hi
= &reinterpret_cast<simdmask
*>(&pa
.GetNextVsIndices())[1];
1612 simd16vertex
&vout
= pa
.GetNextVsOutput();
1614 vsContext_lo
.pVout
= reinterpret_cast<simdvertex
*>(&vout
);
1615 vsContext_hi
.pVout
= reinterpret_cast<simdvertex
*>(&vout
);
1619 // 1. Execute FS/VS for a single SIMD.
1620 AR_BEGIN(FEFetchShader
, pDC
->drawId
);
1621 state
.pfnFetchFunc(fetchInfo_lo
, vin_lo
);
1623 if ((i
+ KNOB_SIMD_WIDTH
) < endVertex
) // 1/2 of KNOB_SIMD16_WIDTH
1625 state
.pfnFetchFunc(fetchInfo_hi
, vin_hi
);
1627 AR_END(FEFetchShader
, 0);
1629 // forward fetch generated vertex IDs to the vertex shader
1630 vsContext_lo
.VertexID
= fetchInfo_lo
.VertexID
;
1631 vsContext_hi
.VertexID
= fetchInfo_hi
.VertexID
;
1633 // Setup active mask for vertex shader.
1634 vsContext_lo
.mask
= GenerateMask(endVertex
- i
);
1635 vsContext_hi
.mask
= GenerateMask(endVertex
- (i
+ KNOB_SIMD_WIDTH
));
1637 // forward cut mask to the PA
1638 if (IsIndexedT::value
)
1640 *pvCutIndices_lo
= _simd_movemask_ps(_simd_castsi_ps(fetchInfo_lo
.CutMask
));
1641 *pvCutIndices_hi
= _simd_movemask_ps(_simd_castsi_ps(fetchInfo_hi
.CutMask
));
1644 UPDATE_STAT_FE(IaVertices
, GetNumInvocations(i
, endVertex
));
1646 #if KNOB_ENABLE_TOSS_POINTS
1647 if (!KNOB_TOSS_FETCH
)
1650 AR_BEGIN(FEVertexShader
, pDC
->drawId
);
1651 state
.pfnVertexFunc(GetPrivateState(pDC
), &vsContext_lo
);
1653 if ((i
+ KNOB_SIMD_WIDTH
) < endVertex
) // 1/2 of KNOB_SIMD16_WIDTH
1655 state
.pfnVertexFunc(GetPrivateState(pDC
), &vsContext_hi
);
1657 AR_END(FEVertexShader
, 0);
1659 UPDATE_STAT_FE(VsInvocations
, GetNumInvocations(i
, endVertex
));
1663 // 2. Assemble primitives given the last two SIMD.
1666 simd16vector prim_simd16
[MAX_NUM_VERTS_PER_PRIM
];
1668 RDTSC_START(FEPAAssemble
);
1669 bool assemble
= pa
.Assemble_simd16(VERTEX_POSITION_SLOT
, prim_simd16
);
1670 RDTSC_STOP(FEPAAssemble
, 1, 0);
1672 #if KNOB_ENABLE_TOSS_POINTS
1673 if (!KNOB_TOSS_FETCH
)
1676 #if KNOB_ENABLE_TOSS_POINTS
1682 UPDATE_STAT_FE(IaPrimitives
, pa
.NumPrims());
1684 const uint32_t numPrims
= pa
.NumPrims();
1685 const uint32_t numPrims_lo
= std::min
<uint32_t>(numPrims
, KNOB_SIMD_WIDTH
);
1686 const uint32_t numPrims_hi
= std::max
<uint32_t>(numPrims
, KNOB_SIMD_WIDTH
) - KNOB_SIMD_WIDTH
;
1688 const simd16scalari primID
= pa
.GetPrimID(work
.startPrimID
);
1689 const simdscalari primID_lo
= _simd16_extract_si(primID
, 0);
1690 const simdscalari primID_hi
= _simd16_extract_si(primID
, 1);
1692 if (HasTessellationT::value
)
1694 pa
.useAlternateOffset
= false;
1695 TessellationStages
<HasGeometryShaderT
, HasStreamOutT
, HasRastT
>(pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, numPrims_lo
, primID_lo
);
1699 pa
.useAlternateOffset
= true;
1700 TessellationStages
<HasGeometryShaderT
, HasStreamOutT
, HasRastT
>(pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, numPrims_hi
, primID_hi
);
1703 else if (HasGeometryShaderT::value
)
1705 pa
.useAlternateOffset
= false;
1706 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, numPrims_lo
, primID_lo
);
1710 pa
.useAlternateOffset
= true;
1711 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, numPrims_hi
, primID_hi
);
1716 // If streamout is enabled then stream vertices out to memory.
1717 if (HasStreamOutT::value
)
1719 pa
.useAlternateOffset
= false;
1720 StreamOut(pDC
, pa
, workerId
, pSoPrimData
, 0);
1723 if (HasRastT::value
)
1725 SWR_ASSERT(pDC
->pState
->pfnProcessPrims_simd16
);
1727 pa
.useAlternateOffset
= false;
1728 pDC
->pState
->pfnProcessPrims_simd16(pDC
, pa
, workerId
, prim_simd16
, GenMask(numPrims
), primID
, _simd16_setzero_si());
1734 } while (pa
.NextPrim());
1736 if (IsIndexedT::value
)
1738 fetchInfo_lo
.pIndices
= (int32_t *)((uint8_t*)fetchInfo_lo
.pIndices
+ KNOB_SIMD16_WIDTH
* indexSize
);
1739 fetchInfo_hi
.pIndices
= (int32_t *)((uint8_t*)fetchInfo_hi
.pIndices
+ KNOB_SIMD16_WIDTH
* indexSize
);
1743 vIndex
= _simd16_add_epi32(vIndex
, _simd16_set1_epi32(KNOB_SIMD16_WIDTH
));
1746 i
+= KNOB_SIMD16_WIDTH
;
1754 SWR_VS_CONTEXT vsContext
;
1756 vsContext
.pVin
= &vin
;
1758 SWR_FETCH_CONTEXT fetchInfo
= { 0 };
1760 fetchInfo
.pStreams
= &state
.vertexBuffers
[0];
1761 fetchInfo
.StartInstance
= work
.startInstance
;
1762 fetchInfo
.StartVertex
= 0;
1764 if (IsIndexedT::value
)
1766 fetchInfo
.BaseVertex
= work
.baseVertex
;
1768 // if the entire index buffer isn't being consumed, set the last index
1769 // so that fetches < a SIMD wide will be masked off
1770 fetchInfo
.pLastIndex
= (const int32_t*)(((uint8_t*)state
.indexBuffer
.pIndices
) + state
.indexBuffer
.size
);
1771 if (pLastRequestedIndex
< fetchInfo
.pLastIndex
)
1773 fetchInfo
.pLastIndex
= pLastRequestedIndex
;
1778 fetchInfo
.StartVertex
= work
.startVertex
;
1781 const simdscalari vScale
= _mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0);
1783 /// @todo: temporarily move instance loop in the FE to ensure SO ordering
1784 for (uint32_t instanceNum
= 0; instanceNum
< work
.numInstances
; instanceNum
++)
1789 if (IsIndexedT::value
)
1791 fetchInfo
.pIndices
= work
.pIB
;
1795 vIndex
= _simd_add_epi32(_simd_set1_epi32(work
.startVertexID
), vScale
);
1796 fetchInfo
.pIndices
= (const int32_t*)&vIndex
;
1799 fetchInfo
.CurInstance
= instanceNum
;
1800 vsContext
.InstanceID
= instanceNum
;
1802 while (pa
.HasWork())
1804 // GetNextVsOutput currently has the side effect of updating some PA state machine state.
1805 // So we need to keep this outside of (i < endVertex) check.
1806 simdmask
* pvCutIndices
= nullptr;
1807 if (IsIndexedT::value
)
1809 pvCutIndices
= &pa
.GetNextVsIndices();
1812 simdvertex
& vout
= pa
.GetNextVsOutput();
1813 vsContext
.pVout
= &vout
;
1818 // 1. Execute FS/VS for a single SIMD.
1819 AR_BEGIN(FEFetchShader
, pDC
->drawId
);
1820 state
.pfnFetchFunc(fetchInfo
, vin
);
1821 AR_END(FEFetchShader
, 0);
1823 // forward fetch generated vertex IDs to the vertex shader
1824 vsContext
.VertexID
= fetchInfo
.VertexID
;
1826 // Setup active mask for vertex shader.
1827 vsContext
.mask
= GenerateMask(endVertex
- i
);
1829 // forward cut mask to the PA
1830 if (IsIndexedT::value
)
1832 *pvCutIndices
= _simd_movemask_ps(_simd_castsi_ps(fetchInfo
.CutMask
));
1835 UPDATE_STAT_FE(IaVertices
, GetNumInvocations(i
, endVertex
));
1837 #if KNOB_ENABLE_TOSS_POINTS
1838 if (!KNOB_TOSS_FETCH
)
1841 AR_BEGIN(FEVertexShader
, pDC
->drawId
);
1842 state
.pfnVertexFunc(GetPrivateState(pDC
), &vsContext
);
1843 AR_END(FEVertexShader
, 0);
1845 UPDATE_STAT_FE(VsInvocations
, GetNumInvocations(i
, endVertex
));
1849 // 2. Assemble primitives given the last two SIMD.
1852 simdvector prim
[MAX_NUM_VERTS_PER_PRIM
];
1853 // PaAssemble returns false if there is not enough verts to assemble.
1854 AR_BEGIN(FEPAAssemble
, pDC
->drawId
);
1855 bool assemble
= pa
.Assemble(VERTEX_POSITION_SLOT
, prim
);
1856 AR_END(FEPAAssemble
, 1);
1858 #if KNOB_ENABLE_TOSS_POINTS
1859 if (!KNOB_TOSS_FETCH
)
1862 #if KNOB_ENABLE_TOSS_POINTS
1868 UPDATE_STAT_FE(IaPrimitives
, pa
.NumPrims());
1870 if (HasTessellationT::value
)
1872 TessellationStages
<HasGeometryShaderT
, HasStreamOutT
, HasRastT
>(
1873 pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, pa
.GetPrimID(work
.startPrimID
));
1875 else if (HasGeometryShaderT::value
)
1877 GeometryShaderStage
<HasStreamOutT
, HasRastT
>(
1878 pDC
, workerId
, pa
, pGsOut
, pCutBuffer
, pStreamCutBuffer
, pSoPrimData
, pa
.GetPrimID(work
.startPrimID
));
1882 // If streamout is enabled then stream vertices out to memory.
1883 if (HasStreamOutT::value
)
1885 StreamOut(pDC
, pa
, workerId
, pSoPrimData
, 0);
1888 if (HasRastT::value
)
1890 SWR_ASSERT(pDC
->pState
->pfnProcessPrims
);
1892 pDC
->pState
->pfnProcessPrims(pDC
, pa
, workerId
, prim
,
1893 GenMask(pa
.NumPrims()), pa
.GetPrimID(work
.startPrimID
), _simd_set1_epi32(0));
1899 } while (pa
.NextPrim());
1901 if (IsIndexedT::value
)
1903 fetchInfo
.pIndices
= (int*)((uint8_t*)fetchInfo
.pIndices
+ KNOB_SIMD_WIDTH
* indexSize
);
1907 vIndex
= _simd_add_epi32(vIndex
, _simd_set1_epi32(KNOB_SIMD_WIDTH
));
1910 i
+= KNOB_SIMD_WIDTH
;
1917 AR_END(FEProcessDraw
, numPrims
* work
.numInstances
);
1920 struct FEDrawChooser
1922 typedef PFN_FE_WORK_FUNC FuncType
;
1924 template <typename
... ArgsB
>
1925 static FuncType
GetFunc()
1927 return ProcessDraw
<ArgsB
...>;
1932 // Selector for correct templated Draw front-end function
1933 PFN_FE_WORK_FUNC
GetProcessDrawFunc(
1935 bool IsCutIndexEnabled
,
1936 bool HasTessellation
,
1937 bool HasGeometryShader
,
1939 bool HasRasterization
)
1941 return TemplateArgUnroller
<FEDrawChooser
>::GetFunc(IsIndexed
, IsCutIndexEnabled
, HasTessellation
, HasGeometryShader
, HasStreamOut
, HasRasterization
);