1 /****************************************************************************
2 * Copyright (C) 2014-2016 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 API implementation
27 ******************************************************************************/
35 #include "core/backend.h"
36 #include "core/context.h"
37 #include "core/depthstencil.h"
38 #include "core/frontend.h"
39 #include "core/rasterizer.h"
40 #include "core/rdtsc_core.h"
41 #include "core/threads.h"
42 #include "core/tilemgr.h"
43 #include "core/clip.h"
44 #include "core/utils.h"
46 #include "common/simdintrin.h"
47 #include "common/os.h"
49 static const SWR_RECT g_MaxScissorRect
= { 0, 0, KNOB_MAX_SCISSOR_X
, KNOB_MAX_SCISSOR_Y
};
51 void SetupDefaultState(SWR_CONTEXT
*pContext
);
53 static INLINE SWR_CONTEXT
* GetContext(HANDLE hContext
)
55 return (SWR_CONTEXT
*)hContext
;
58 void WakeAllThreads(SWR_CONTEXT
*pContext
)
60 pContext
->FifosNotEmpty
.notify_all();
63 //////////////////////////////////////////////////////////////////////////
64 /// @brief Create SWR Context.
65 /// @param pCreateInfo - pointer to creation info.
66 HANDLE
SwrCreateContext(
67 SWR_CREATECONTEXT_INFO
* pCreateInfo
)
72 void* pContextMem
= AlignedMalloc(sizeof(SWR_CONTEXT
), KNOB_SIMD_WIDTH
* 4);
73 memset(pContextMem
, 0, sizeof(SWR_CONTEXT
));
74 SWR_CONTEXT
*pContext
= new (pContextMem
) SWR_CONTEXT();
76 pContext
->driverType
= pCreateInfo
->driver
;
77 pContext
->privateStateSize
= pCreateInfo
->privateStateSize
;
79 pContext
->dcRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
80 pContext
->dsRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
82 pContext
->pMacroTileManagerArray
= (MacroTileMgr
*)AlignedMalloc(sizeof(MacroTileMgr
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
83 pContext
->pDispatchQueueArray
= (DispatchQueue
*)AlignedMalloc(sizeof(DispatchQueue
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
85 for (uint32_t dc
= 0; dc
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++dc
)
87 pContext
->dcRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
88 new (&pContext
->pMacroTileManagerArray
[dc
]) MacroTileMgr(*pContext
->dcRing
[dc
].pArena
);
89 new (&pContext
->pDispatchQueueArray
[dc
]) DispatchQueue();
91 pContext
->dsRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
94 pContext
->threadInfo
.MAX_WORKER_THREADS
= KNOB_MAX_WORKER_THREADS
;
95 pContext
->threadInfo
.MAX_NUMA_NODES
= KNOB_MAX_NUMA_NODES
;
96 pContext
->threadInfo
.MAX_CORES_PER_NUMA_NODE
= KNOB_MAX_CORES_PER_NUMA_NODE
;
97 pContext
->threadInfo
.MAX_THREADS_PER_CORE
= KNOB_MAX_THREADS_PER_CORE
;
98 pContext
->threadInfo
.SINGLE_THREADED
= KNOB_SINGLE_THREADED
;
100 if (pCreateInfo
->pThreadInfo
)
102 pContext
->threadInfo
= *pCreateInfo
->pThreadInfo
;
105 memset(&pContext
->WaitLock
, 0, sizeof(pContext
->WaitLock
));
106 memset(&pContext
->FifosNotEmpty
, 0, sizeof(pContext
->FifosNotEmpty
));
107 new (&pContext
->WaitLock
) std::mutex();
108 new (&pContext
->FifosNotEmpty
) std::condition_variable();
110 CreateThreadPool(pContext
, &pContext
->threadPool
);
112 pContext
->ppScratch
= new uint8_t*[pContext
->NumWorkerThreads
];
113 pContext
->pStats
= new SWR_STATS
[pContext
->NumWorkerThreads
];
116 // Setup ArchRast thread contexts which includes +1 for API thread.
117 pContext
->pArContext
= new HANDLE
[pContext
->NumWorkerThreads
+1];
118 pContext
->pArContext
[pContext
->NumWorkerThreads
] = ArchRast::CreateThreadContext();
121 // Allocate scratch space for workers.
122 ///@note We could lazily allocate this but its rather small amount of memory.
123 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
126 uint32_t numaNode
= pContext
->threadPool
.pThreadData
?
127 pContext
->threadPool
.pThreadData
[i
].numaId
: 0;
128 pContext
->ppScratch
[i
] = (uint8_t*)VirtualAllocExNuma(
129 GetCurrentProcess(), nullptr, 32 * sizeof(KILOBYTE
),
130 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
,
133 pContext
->ppScratch
[i
] = (uint8_t*)AlignedMalloc(32 * sizeof(KILOBYTE
), KNOB_SIMD_WIDTH
* 4);
137 // Initialize worker thread context for ArchRast.
138 pContext
->pArContext
[i
] = ArchRast::CreateThreadContext();
142 // State setup AFTER context is fully initialized
143 SetupDefaultState(pContext
);
145 // initialize hot tile manager
146 pContext
->pHotTileMgr
= new HotTileMgr();
148 // initialize function pointer tables
149 InitClearTilesTable();
151 // initialize callback functions
152 pContext
->pfnLoadTile
= pCreateInfo
->pfnLoadTile
;
153 pContext
->pfnStoreTile
= pCreateInfo
->pfnStoreTile
;
154 pContext
->pfnClearTile
= pCreateInfo
->pfnClearTile
;
155 pContext
->pfnUpdateSoWriteOffset
= pCreateInfo
->pfnUpdateSoWriteOffset
;
156 pContext
->pfnUpdateStats
= pCreateInfo
->pfnUpdateStats
;
157 pContext
->pfnUpdateStatsFE
= pCreateInfo
->pfnUpdateStatsFE
;
159 // pass pointer to bucket manager back to caller
160 #ifdef KNOB_ENABLE_RDTSC
161 pCreateInfo
->pBucketMgr
= &gBucketMgr
;
164 pCreateInfo
->contextSaveSize
= sizeof(API_STATE
);
166 StartThreadPool(pContext
, &pContext
->threadPool
);
168 return (HANDLE
)pContext
;
171 void CopyState(DRAW_STATE
& dst
, const DRAW_STATE
& src
)
173 memcpy(&dst
.state
, &src
.state
, sizeof(API_STATE
));
176 template<bool IsDraw
>
177 void QueueWork(SWR_CONTEXT
*pContext
)
179 DRAW_CONTEXT
* pDC
= pContext
->pCurDrawContext
;
180 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
184 pDC
->pTileMgr
= &pContext
->pMacroTileManagerArray
[dcIndex
];
185 pDC
->pTileMgr
->initialize();
188 // Each worker thread looks at a DC for both FE and BE work at different times and so we
189 // multiply threadDone by 2. When the threadDone counter has reached 0 then all workers
190 // have moved past this DC. (i.e. Each worker has checked this DC for both FE and BE work and
191 // then moved on if all work is done.)
192 pContext
->pCurDrawContext
->threadsDone
= pContext
->NumFEThreads
+ pContext
->NumBEThreads
;
196 InterlockedIncrement((volatile LONG
*)&pContext
->drawsOutstandingFE
);
201 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
);
202 pContext
->dcRing
.Enqueue();
205 if (pContext
->threadInfo
.SINGLE_THREADED
)
207 // flush denormals to 0
208 uint32_t mxcsr
= _mm_getcsr();
209 _mm_setcsr(mxcsr
| _MM_FLUSH_ZERO_ON
| _MM_DENORMALS_ZERO_ON
);
213 uint32_t curDraw
[2] = { pContext
->pCurDrawContext
->drawId
, pContext
->pCurDrawContext
->drawId
};
214 WorkOnFifoFE(pContext
, 0, curDraw
[0]);
215 WorkOnFifoBE(pContext
, 0, curDraw
[1], pContext
->singleThreadLockedTiles
, 0, 0);
219 uint32_t curDispatch
= pContext
->pCurDrawContext
->drawId
;
220 WorkOnCompute(pContext
, 0, curDispatch
);
223 // Dequeue the work here, if not already done, since we're single threaded (i.e. no workers).
224 while (CompleteDrawContext(pContext
, pContext
->pCurDrawContext
) > 0) {}
231 AR_API_BEGIN(APIDrawWakeAllThreads
, pDC
->drawId
);
232 WakeAllThreads(pContext
);
233 AR_API_END(APIDrawWakeAllThreads
, 1);
236 // Set current draw context to NULL so that next state call forces a new draw context to be created and populated.
237 pContext
->pPrevDrawContext
= pContext
->pCurDrawContext
;
238 pContext
->pCurDrawContext
= nullptr;
241 INLINE
void QueueDraw(SWR_CONTEXT
* pContext
)
243 QueueWork
<true>(pContext
);
246 INLINE
void QueueDispatch(SWR_CONTEXT
* pContext
)
248 QueueWork
<false>(pContext
);
251 DRAW_CONTEXT
* GetDrawContext(SWR_CONTEXT
*pContext
, bool isSplitDraw
= false)
253 AR_API_BEGIN(APIGetDrawContext
, 0);
254 // If current draw context is null then need to obtain a new draw context to use from ring.
255 if (pContext
->pCurDrawContext
== nullptr)
257 // Need to wait for a free entry.
258 while (pContext
->dcRing
.IsFull())
263 uint64_t curDraw
= pContext
->dcRing
.GetHead();
264 uint32_t dcIndex
= curDraw
% KNOB_MAX_DRAWS_IN_FLIGHT
;
266 if ((pContext
->frameCount
- pContext
->lastFrameChecked
) > 2 ||
267 (curDraw
- pContext
->lastDrawChecked
) > 0x10000)
269 // Take this opportunity to clean-up old arena allocations
270 pContext
->cachingArenaAllocator
.FreeOldBlocks();
272 pContext
->lastFrameChecked
= pContext
->frameCount
;
273 pContext
->lastDrawChecked
= curDraw
;
276 DRAW_CONTEXT
* pCurDrawContext
= &pContext
->dcRing
[dcIndex
];
277 pContext
->pCurDrawContext
= pCurDrawContext
;
279 // Assign next available entry in DS ring to this DC.
280 uint32_t dsIndex
= pContext
->curStateId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
281 pCurDrawContext
->pState
= &pContext
->dsRing
[dsIndex
];
283 // Copy previous state to current state.
284 if (pContext
->pPrevDrawContext
)
286 DRAW_CONTEXT
* pPrevDrawContext
= pContext
->pPrevDrawContext
;
288 // If we're splitting our draw then we can just use the same state from the previous
289 // draw. In this case, we won't increment the DS ring index so the next non-split
290 // draw can receive the state.
291 if (isSplitDraw
== false)
293 CopyState(*pCurDrawContext
->pState
, *pPrevDrawContext
->pState
);
295 // Should have been cleaned up previously
296 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
298 pCurDrawContext
->pState
->pPrivateState
= nullptr;
300 pContext
->curStateId
++; // Progress state ring index forward.
304 // If its a split draw then just copy the state pointer over
305 // since its the same draw.
306 pCurDrawContext
->pState
= pPrevDrawContext
->pState
;
307 SWR_ASSERT(pPrevDrawContext
->cleanupState
== false);
312 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
313 pContext
->curStateId
++; // Progress state ring index forward.
316 SWR_ASSERT(pCurDrawContext
->pArena
->IsEmpty() == true);
318 pCurDrawContext
->dependent
= false;
319 pCurDrawContext
->pContext
= pContext
;
320 pCurDrawContext
->isCompute
= false; // Dispatch has to set this to true.
322 pCurDrawContext
->doneFE
= false;
323 pCurDrawContext
->FeLock
= 0;
324 pCurDrawContext
->threadsDone
= 0;
325 pCurDrawContext
->retireCallback
.pfnCallbackFunc
= nullptr;
327 pCurDrawContext
->dynState
.Reset(pContext
->NumWorkerThreads
);
329 // Assign unique drawId for this DC
330 pCurDrawContext
->drawId
= pContext
->dcRing
.GetHead();
332 pCurDrawContext
->cleanupState
= true;
336 SWR_ASSERT(isSplitDraw
== false, "Split draw should only be used when obtaining a new DC");
339 AR_API_END(APIGetDrawContext
, 0);
340 return pContext
->pCurDrawContext
;
343 API_STATE
* GetDrawState(SWR_CONTEXT
*pContext
)
345 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
346 SWR_ASSERT(pDC
->pState
!= nullptr);
348 return &pDC
->pState
->state
;
351 void SwrDestroyContext(HANDLE hContext
)
353 SWR_CONTEXT
*pContext
= GetContext(hContext
);
354 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
356 pDC
->FeWork
.type
= SHUTDOWN
;
357 pDC
->FeWork
.pfnWork
= ProcessShutdown
;
362 DestroyThreadPool(pContext
, &pContext
->threadPool
);
365 for (uint32_t i
= 0; i
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++i
)
367 delete[] pContext
->dcRing
[i
].dynState
.pStats
;
368 delete pContext
->dcRing
[i
].pArena
;
369 delete pContext
->dsRing
[i
].pArena
;
370 pContext
->pMacroTileManagerArray
[i
].~MacroTileMgr();
371 pContext
->pDispatchQueueArray
[i
].~DispatchQueue();
374 AlignedFree(pContext
->pDispatchQueueArray
);
375 AlignedFree(pContext
->pMacroTileManagerArray
);
377 // Free scratch space.
378 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
381 VirtualFree(pContext
->ppScratch
[i
], 0, MEM_RELEASE
);
383 AlignedFree(pContext
->ppScratch
[i
]);
387 ArchRast::DestroyThreadContext(pContext
->pArContext
[i
]);
391 delete[] pContext
->ppScratch
;
392 delete[] pContext
->pArContext
;
393 delete[] pContext
->pStats
;
395 delete(pContext
->pHotTileMgr
);
397 pContext
->~SWR_CONTEXT();
398 AlignedFree(GetContext(hContext
));
401 void SWR_API
SwrSaveState(
403 void* pOutputStateBlock
,
406 SWR_CONTEXT
*pContext
= GetContext(hContext
);
407 auto pSrc
= GetDrawState(pContext
);
408 SWR_ASSERT(pOutputStateBlock
&& memSize
>= sizeof(*pSrc
));
410 memcpy(pOutputStateBlock
, pSrc
, sizeof(*pSrc
));
413 void SWR_API
SwrRestoreState(
415 const void* pStateBlock
,
418 SWR_CONTEXT
*pContext
= GetContext(hContext
);
419 auto pDst
= GetDrawState(pContext
);
420 SWR_ASSERT(pStateBlock
&& memSize
>= sizeof(*pDst
));
422 memcpy(pDst
, pStateBlock
, sizeof(*pDst
));
425 void SetupDefaultState(SWR_CONTEXT
*pContext
)
427 API_STATE
* pState
= GetDrawState(pContext
);
429 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
430 pState
->rastState
.frontWinding
= SWR_FRONTWINDING_CCW
;
432 pState
->depthBoundsState
.depthBoundsTestEnable
= false;
433 pState
->depthBoundsState
.depthBoundsTestMinValue
= 0.0f
;
434 pState
->depthBoundsState
.depthBoundsTestMaxValue
= 1.0f
;
437 void SwrSync(HANDLE hContext
, PFN_CALLBACK_FUNC pfnFunc
, uint64_t userData
, uint64_t userData2
, uint64_t userData3
)
439 SWR_ASSERT(pfnFunc
!= nullptr);
441 SWR_CONTEXT
*pContext
= GetContext(hContext
);
442 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
444 AR_API_BEGIN(APISync
, 0);
446 pDC
->FeWork
.type
= SYNC
;
447 pDC
->FeWork
.pfnWork
= ProcessSync
;
449 // Setup callback function
450 pDC
->retireCallback
.pfnCallbackFunc
= pfnFunc
;
451 pDC
->retireCallback
.userData
= userData
;
452 pDC
->retireCallback
.userData2
= userData2
;
453 pDC
->retireCallback
.userData3
= userData3
;
458 AR_API_END(APISync
, 1);
461 void SwrWaitForIdle(HANDLE hContext
)
463 SWR_CONTEXT
*pContext
= GetContext(hContext
);
465 AR_API_BEGIN(APIWaitForIdle
, 0);
467 while (!pContext
->dcRing
.IsEmpty())
472 AR_API_END(APIWaitForIdle
, 1);
475 void SwrWaitForIdleFE(HANDLE hContext
)
477 SWR_CONTEXT
*pContext
= GetContext(hContext
);
479 AR_API_BEGIN(APIWaitForIdle
, 0);
481 while (pContext
->drawsOutstandingFE
> 0)
486 AR_API_END(APIWaitForIdle
, 1);
489 void SwrSetVertexBuffers(
492 const SWR_VERTEX_BUFFER_STATE
* pVertexBuffers
)
494 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
496 for (uint32_t i
= 0; i
< numBuffers
; ++i
)
498 const SWR_VERTEX_BUFFER_STATE
*pVB
= &pVertexBuffers
[i
];
499 pState
->vertexBuffers
[pVB
->index
] = *pVB
;
503 void SwrSetIndexBuffer(
505 const SWR_INDEX_BUFFER_STATE
* pIndexBuffer
)
507 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
509 pState
->indexBuffer
= *pIndexBuffer
;
512 void SwrSetFetchFunc(
514 PFN_FETCH_FUNC pfnFetchFunc
)
516 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
518 pState
->pfnFetchFunc
= pfnFetchFunc
;
523 PFN_SO_FUNC pfnSoFunc
,
524 uint32_t streamIndex
)
526 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
528 SWR_ASSERT(streamIndex
< MAX_SO_STREAMS
);
530 pState
->pfnSoFunc
[streamIndex
] = pfnSoFunc
;
535 SWR_STREAMOUT_STATE
* pSoState
)
537 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
539 pState
->soState
= *pSoState
;
542 void SwrSetSoBuffers(
544 SWR_STREAMOUT_BUFFER
* pSoBuffer
,
547 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
549 SWR_ASSERT((slot
< 4), "There are only 4 SO buffer slots [0, 3]\nSlot requested: %d", slot
);
551 pState
->soBuffer
[slot
] = *pSoBuffer
;
554 void SwrSetVertexFunc(
556 PFN_VERTEX_FUNC pfnVertexFunc
)
558 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
560 pState
->pfnVertexFunc
= pfnVertexFunc
;
563 void SwrSetFrontendState(
565 SWR_FRONTEND_STATE
*pFEState
)
567 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
568 pState
->frontendState
= *pFEState
;
573 SWR_GS_STATE
*pGSState
)
575 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
576 pState
->gsState
= *pGSState
;
581 PFN_GS_FUNC pfnGsFunc
)
583 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
584 pState
->pfnGsFunc
= pfnGsFunc
;
589 PFN_CS_FUNC pfnCsFunc
,
590 uint32_t totalThreadsInGroup
,
591 uint32_t totalSpillFillSize
)
593 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
594 pState
->pfnCsFunc
= pfnCsFunc
;
595 pState
->totalThreadsInGroup
= totalThreadsInGroup
;
596 pState
->totalSpillFillSize
= totalSpillFillSize
;
601 SWR_TS_STATE
*pState
)
603 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
604 pApiState
->tsState
= *pState
;
611 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
612 pApiState
->pfnHsFunc
= pfnFunc
;
619 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
620 pApiState
->pfnDsFunc
= pfnFunc
;
623 void SwrSetDepthStencilState(
625 SWR_DEPTH_STENCIL_STATE
*pDSState
)
627 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
629 pState
->depthStencilState
= *pDSState
;
632 void SwrSetBackendState(
634 SWR_BACKEND_STATE
*pBEState
)
636 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
638 pState
->backendState
= *pBEState
;
641 void SwrSetDepthBoundsState(
643 SWR_DEPTH_BOUNDS_STATE
*pDBState
)
645 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
647 pState
->depthBoundsState
= *pDBState
;
650 void SwrSetPixelShaderState(
652 SWR_PS_STATE
*pPSState
)
654 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
655 pState
->psState
= *pPSState
;
658 void SwrSetBlendState(
660 SWR_BLEND_STATE
*pBlendState
)
662 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
663 memcpy(&pState
->blendState
, pBlendState
, sizeof(SWR_BLEND_STATE
));
666 void SwrSetBlendFunc(
668 uint32_t renderTarget
,
669 PFN_BLEND_JIT_FUNC pfnBlendFunc
)
671 SWR_ASSERT(renderTarget
< SWR_NUM_RENDERTARGETS
);
672 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
673 pState
->pfnBlendFunc
[renderTarget
] = pfnBlendFunc
;
676 // update guardband multipliers for the viewport
677 void updateGuardbands(API_STATE
*pState
)
679 uint32_t numGbs
= pState
->gsState
.emitsRenderTargetArrayIndex
? KNOB_NUM_VIEWPORTS_SCISSORS
: 1;
681 for(uint32_t i
= 0; i
< numGbs
; ++i
)
683 // guardband center is viewport center
684 pState
->gbState
.left
[i
] = KNOB_GUARDBAND_WIDTH
/ pState
->vp
[i
].width
;
685 pState
->gbState
.right
[i
] = KNOB_GUARDBAND_WIDTH
/ pState
->vp
[i
].width
;
686 pState
->gbState
.top
[i
] = KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[i
].height
;
687 pState
->gbState
.bottom
[i
] = KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[i
].height
;
691 void SwrSetRastState(
693 const SWR_RASTSTATE
*pRastState
)
695 SWR_CONTEXT
*pContext
= GetContext(hContext
);
696 API_STATE
* pState
= GetDrawState(pContext
);
698 memcpy(&pState
->rastState
, pRastState
, sizeof(SWR_RASTSTATE
));
701 void SwrSetViewports(
703 uint32_t numViewports
,
704 const SWR_VIEWPORT
* pViewports
,
705 const SWR_VIEWPORT_MATRICES
* pMatrices
)
707 SWR_ASSERT(numViewports
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
708 "Invalid number of viewports.");
710 SWR_CONTEXT
*pContext
= GetContext(hContext
);
711 API_STATE
* pState
= GetDrawState(pContext
);
713 memcpy(&pState
->vp
[0], pViewports
, sizeof(SWR_VIEWPORT
) * numViewports
);
715 if (pMatrices
!= nullptr)
717 // @todo Faster to copy portions of the SOA or just copy all of it?
718 memcpy(&pState
->vpMatrices
, pMatrices
, sizeof(SWR_VIEWPORT_MATRICES
));
722 // Compute default viewport transform.
723 for (uint32_t i
= 0; i
< numViewports
; ++i
)
725 if (pContext
->driverType
== DX
)
727 pState
->vpMatrices
.m00
[i
] = pState
->vp
[i
].width
/ 2.0f
;
728 pState
->vpMatrices
.m11
[i
] = -pState
->vp
[i
].height
/ 2.0f
;
729 pState
->vpMatrices
.m22
[i
] = pState
->vp
[i
].maxZ
- pState
->vp
[i
].minZ
;
730 pState
->vpMatrices
.m30
[i
] = pState
->vp
[i
].x
+ pState
->vpMatrices
.m00
[i
];
731 pState
->vpMatrices
.m31
[i
] = pState
->vp
[i
].y
- pState
->vpMatrices
.m11
[i
];
732 pState
->vpMatrices
.m32
[i
] = pState
->vp
[i
].minZ
;
736 // Standard, with the exception that Y is inverted.
737 pState
->vpMatrices
.m00
[i
] = (pState
->vp
[i
].width
- pState
->vp
[i
].x
) / 2.0f
;
738 pState
->vpMatrices
.m11
[i
] = (pState
->vp
[i
].y
- pState
->vp
[i
].height
) / 2.0f
;
739 pState
->vpMatrices
.m22
[i
] = (pState
->vp
[i
].maxZ
- pState
->vp
[i
].minZ
) / 2.0f
;
740 pState
->vpMatrices
.m30
[i
] = pState
->vp
[i
].x
+ pState
->vpMatrices
.m00
[i
];
741 pState
->vpMatrices
.m31
[i
] = pState
->vp
[i
].height
+ pState
->vpMatrices
.m11
[i
];
742 pState
->vpMatrices
.m32
[i
] = pState
->vp
[i
].minZ
+ pState
->vpMatrices
.m22
[i
];
744 // Now that the matrix is calculated, clip the view coords to screen size.
745 // OpenGL allows for -ve x,y in the viewport.
746 pState
->vp
[i
].x
= std::max(pState
->vp
[i
].x
, 0.0f
);
747 pState
->vp
[i
].y
= std::max(pState
->vp
[i
].y
, 0.0f
);
752 updateGuardbands(pState
);
755 void SwrSetScissorRects(
757 uint32_t numScissors
,
758 const SWR_RECT
* pScissors
)
760 SWR_ASSERT(numScissors
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
761 "Invalid number of scissor rects.");
763 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
764 memcpy(&pState
->scissorRects
[0], pScissors
, numScissors
* sizeof(pScissors
[0]));
767 void SetupMacroTileScissors(DRAW_CONTEXT
*pDC
)
769 API_STATE
*pState
= &pDC
->pState
->state
;
770 uint32_t numScissors
= pState
->gsState
.emitsViewportArrayIndex
? KNOB_NUM_VIEWPORTS_SCISSORS
: 1;
771 pState
->scissorsTileAligned
= true;
773 for (uint32_t index
= 0; index
< numScissors
; ++index
)
775 SWR_RECT
&scissorInFixedPoint
= pState
->scissorsInFixedPoint
[index
];
777 // Set up scissor dimensions based on scissor or viewport
778 if (pState
->rastState
.scissorEnable
)
780 scissorInFixedPoint
= pState
->scissorRects
[index
];
784 // the vp width and height must be added to origin un-rounded then the result round to -inf.
785 // The cast to int works for rounding assuming all [left, right, top, bottom] are positive.
786 scissorInFixedPoint
.xmin
= (int32_t)pState
->vp
[index
].x
;
787 scissorInFixedPoint
.xmax
= (int32_t)(pState
->vp
[index
].x
+ pState
->vp
[index
].width
);
788 scissorInFixedPoint
.ymin
= (int32_t)pState
->vp
[index
].y
;
789 scissorInFixedPoint
.ymax
= (int32_t)(pState
->vp
[index
].y
+ pState
->vp
[index
].height
);
793 scissorInFixedPoint
&= g_MaxScissorRect
;
795 // Test for tile alignment
797 tileAligned
= (scissorInFixedPoint
.xmin
% KNOB_TILE_X_DIM
) == 0;
798 tileAligned
&= (scissorInFixedPoint
.ymin
% KNOB_TILE_Y_DIM
) == 0;
799 tileAligned
&= (scissorInFixedPoint
.xmax
% KNOB_TILE_X_DIM
) == 0;
800 tileAligned
&= (scissorInFixedPoint
.xmax
% KNOB_TILE_Y_DIM
) == 0;
802 pState
->scissorsTileAligned
&= tileAligned
;
804 // Scale to fixed point
805 scissorInFixedPoint
.xmin
*= FIXED_POINT_SCALE
;
806 scissorInFixedPoint
.xmax
*= FIXED_POINT_SCALE
;
807 scissorInFixedPoint
.ymin
*= FIXED_POINT_SCALE
;
808 scissorInFixedPoint
.ymax
*= FIXED_POINT_SCALE
;
810 // Make scissor inclusive
811 scissorInFixedPoint
.xmax
-= 1;
812 scissorInFixedPoint
.ymax
-= 1;
816 // templated backend function tables
817 extern PFN_BACKEND_FUNC gBackendNullPs
[SWR_MULTISAMPLE_TYPE_COUNT
];
818 extern PFN_BACKEND_FUNC gBackendSingleSample
[SWR_INPUT_COVERAGE_COUNT
][2][2];
819 extern PFN_BACKEND_FUNC gBackendPixelRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_MSAA_SAMPLE_PATTERN_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2][2];
820 extern PFN_BACKEND_FUNC gBackendSampleRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2];
821 void SetupPipeline(DRAW_CONTEXT
*pDC
)
823 DRAW_STATE
* pState
= pDC
->pState
;
824 const SWR_RASTSTATE
&rastState
= pState
->state
.rastState
;
825 const SWR_PS_STATE
&psState
= pState
->state
.psState
;
826 BACKEND_FUNCS
& backendFuncs
= pState
->backendFuncs
;
827 const uint32_t forcedSampleCount
= (rastState
.forcedSampleCount
) ? 1 : 0;
830 if (psState
.pfnPixelShader
== nullptr)
832 backendFuncs
.pfnBackend
= gBackendNullPs
[pState
->state
.rastState
.sampleCount
];
836 const bool bMultisampleEnable
= ((rastState
.sampleCount
> SWR_MULTISAMPLE_1X
) || rastState
.forcedSampleCount
) ? 1 : 0;
837 const uint32_t centroid
= ((psState
.barycentricsMask
& SWR_BARYCENTRIC_CENTROID_MASK
) > 0) ? 1 : 0;
838 const uint32_t canEarlyZ
= (psState
.forceEarlyZ
|| (!psState
.writesODepth
&& !psState
.usesSourceDepth
&& !psState
.usesUAV
)) ? 1 : 0;
840 SWR_BARYCENTRICS_MASK barycentricsMask
= (SWR_BARYCENTRICS_MASK
)psState
.barycentricsMask
;
842 // select backend function
843 switch(psState
.shadingRate
)
845 case SWR_SHADING_RATE_PIXEL
:
846 if(bMultisampleEnable
)
848 // always need to generate I & J per sample for Z interpolation
849 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
850 backendFuncs
.pfnBackend
= gBackendPixelRateTable
[rastState
.sampleCount
][rastState
.samplePattern
][psState
.inputCoverage
][centroid
][forcedSampleCount
][canEarlyZ
];
854 // always need to generate I & J per pixel for Z interpolation
855 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_PIXEL_MASK
);
856 backendFuncs
.pfnBackend
= gBackendSingleSample
[psState
.inputCoverage
][centroid
][canEarlyZ
];
859 case SWR_SHADING_RATE_SAMPLE
:
860 SWR_ASSERT(rastState
.samplePattern
== SWR_MSAA_STANDARD_PATTERN
);
861 // always need to generate I & J per sample for Z interpolation
862 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
863 backendFuncs
.pfnBackend
= gBackendSampleRateTable
[rastState
.sampleCount
][psState
.inputCoverage
][centroid
][canEarlyZ
];
866 SWR_ASSERT(0 && "Invalid shading rate");
871 PFN_PROCESS_PRIMS pfnBinner
;
872 switch (pState
->state
.topology
)
875 pState
->pfnProcessPrims
= ClipPoints
;
876 pfnBinner
= BinPoints
;
881 case TOP_LINE_LIST_ADJ
:
882 case TOP_LISTSTRIP_ADJ
:
883 pState
->pfnProcessPrims
= ClipLines
;
884 pfnBinner
= BinLines
;
887 pState
->pfnProcessPrims
= ClipTriangles
;
888 pfnBinner
= GetBinTrianglesFunc((rastState
.conservativeRast
> 0));
892 // disable clipper if viewport transform is disabled
893 if (pState
->state
.frontendState
.vpTransformDisable
)
895 pState
->pfnProcessPrims
= pfnBinner
;
898 if ((pState
->state
.psState
.pfnPixelShader
== nullptr) &&
899 (pState
->state
.depthStencilState
.depthTestEnable
== FALSE
) &&
900 (pState
->state
.depthStencilState
.depthWriteEnable
== FALSE
) &&
901 (pState
->state
.depthStencilState
.stencilTestEnable
== FALSE
) &&
902 (pState
->state
.depthStencilState
.stencilWriteEnable
== FALSE
) &&
903 (pState
->state
.backendState
.numAttributes
== 0))
905 pState
->pfnProcessPrims
= nullptr;
908 if (pState
->state
.soState
.rasterizerDisable
== true)
910 pState
->pfnProcessPrims
= nullptr;
913 // set up the frontend attribute count
914 pState
->state
.feNumAttributes
= 0;
915 const SWR_BACKEND_STATE
& backendState
= pState
->state
.backendState
;
916 if (backendState
.swizzleEnable
)
918 // attribute swizzling is enabled, iterate over the map and record the max attribute used
919 for (uint32_t i
= 0; i
< backendState
.numAttributes
; ++i
)
921 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)backendState
.swizzleMap
[i
].sourceAttrib
+ 1);
926 pState
->state
.feNumAttributes
= pState
->state
.backendState
.numAttributes
;
929 if (pState
->state
.soState
.soEnable
)
931 uint32_t streamMasks
= 0;
932 for (uint32_t i
= 0; i
< 4; ++i
)
934 streamMasks
|= pState
->state
.soState
.streamMasks
[i
];
938 if (_BitScanReverse(&maxAttrib
, streamMasks
))
940 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)(maxAttrib
+ 1));
944 // complicated logic to test for cases where we don't need backing hottile memory for a draw
945 // have to check for the special case where depth/stencil test is enabled but depthwrite is disabled.
946 pState
->state
.depthHottileEnable
= ((!(pState
->state
.depthStencilState
.depthTestEnable
&&
947 !pState
->state
.depthStencilState
.depthWriteEnable
&&
948 pState
->state
.depthStencilState
.depthTestFunc
== ZFUNC_ALWAYS
)) &&
949 (pState
->state
.depthStencilState
.depthTestEnable
||
950 pState
->state
.depthStencilState
.depthWriteEnable
)) ? true : false;
952 pState
->state
.stencilHottileEnable
= (((!(pState
->state
.depthStencilState
.stencilTestEnable
&&
953 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
954 pState
->state
.depthStencilState
.stencilTestFunc
== ZFUNC_ALWAYS
)) ||
955 // for stencil we have to check the double sided state as well
956 (!(pState
->state
.depthStencilState
.doubleSidedStencilTestEnable
&&
957 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
958 pState
->state
.depthStencilState
.backfaceStencilTestFunc
== ZFUNC_ALWAYS
))) &&
959 (pState
->state
.depthStencilState
.stencilTestEnable
||
960 pState
->state
.depthStencilState
.stencilWriteEnable
)) ? true : false;
962 uint32_t numRTs
= pState
->state
.psState
.numRenderTargets
;
963 pState
->state
.colorHottileEnable
= 0;
964 if (psState
.pfnPixelShader
!= nullptr)
966 for (uint32_t rt
= 0; rt
< numRTs
; ++rt
)
968 pState
->state
.colorHottileEnable
|=
969 (!pState
->state
.blendState
.renderTarget
[rt
].writeDisableAlpha
||
970 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableRed
||
971 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableGreen
||
972 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableBlue
) ? (1 << rt
) : 0;
976 // Setup depth quantization function
977 if (pState
->state
.depthHottileEnable
)
979 switch (pState
->state
.rastState
.depthFormat
)
981 case R32_FLOAT_X8X24_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT_X8X24_TYPELESS
> ; break;
982 case R32_FLOAT
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ; break;
983 case R24_UNORM_X8_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R24_UNORM_X8_TYPELESS
> ; break;
984 case R16_UNORM
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R16_UNORM
> ; break;
985 default: SWR_ASSERT(false, "Unsupported depth format for depth quantiztion.");
986 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
991 // set up pass-through quantize if depth isn't enabled
992 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
996 //////////////////////////////////////////////////////////////////////////
998 /// @param pDC - Draw context to initialize for this draw.
1003 // We don't need to re-setup the scissors/pipeline state again for split draw.
1004 if (isSplitDraw
== false)
1006 SetupMacroTileScissors(pDC
);
1011 //////////////////////////////////////////////////////////////////////////
1012 /// @brief We can split the draw for certain topologies for better performance.
1013 /// @param totalVerts - Total vertices for draw
1014 /// @param topology - Topology used for draw
1015 uint32_t MaxVertsPerDraw(
1017 uint32_t totalVerts
,
1018 PRIMITIVE_TOPOLOGY topology
)
1020 API_STATE
& state
= pDC
->pState
->state
;
1022 uint32_t vertsPerDraw
= totalVerts
;
1024 if (state
.soState
.soEnable
)
1031 case TOP_POINT_LIST
:
1032 case TOP_TRIANGLE_LIST
:
1033 vertsPerDraw
= KNOB_MAX_PRIMS_PER_DRAW
;
1036 case TOP_PATCHLIST_1
:
1037 case TOP_PATCHLIST_2
:
1038 case TOP_PATCHLIST_3
:
1039 case TOP_PATCHLIST_4
:
1040 case TOP_PATCHLIST_5
:
1041 case TOP_PATCHLIST_6
:
1042 case TOP_PATCHLIST_7
:
1043 case TOP_PATCHLIST_8
:
1044 case TOP_PATCHLIST_9
:
1045 case TOP_PATCHLIST_10
:
1046 case TOP_PATCHLIST_11
:
1047 case TOP_PATCHLIST_12
:
1048 case TOP_PATCHLIST_13
:
1049 case TOP_PATCHLIST_14
:
1050 case TOP_PATCHLIST_15
:
1051 case TOP_PATCHLIST_16
:
1052 case TOP_PATCHLIST_17
:
1053 case TOP_PATCHLIST_18
:
1054 case TOP_PATCHLIST_19
:
1055 case TOP_PATCHLIST_20
:
1056 case TOP_PATCHLIST_21
:
1057 case TOP_PATCHLIST_22
:
1058 case TOP_PATCHLIST_23
:
1059 case TOP_PATCHLIST_24
:
1060 case TOP_PATCHLIST_25
:
1061 case TOP_PATCHLIST_26
:
1062 case TOP_PATCHLIST_27
:
1063 case TOP_PATCHLIST_28
:
1064 case TOP_PATCHLIST_29
:
1065 case TOP_PATCHLIST_30
:
1066 case TOP_PATCHLIST_31
:
1067 case TOP_PATCHLIST_32
:
1068 if (pDC
->pState
->state
.tsState
.tsEnable
)
1070 uint32_t vertsPerPrim
= topology
- TOP_PATCHLIST_BASE
;
1071 vertsPerDraw
= vertsPerPrim
* KNOB_MAX_TESS_PRIMS_PER_DRAW
;
1075 // The Primitive Assembly code can only handle 1 RECT at a time.
1081 // We are not splitting up draws for other topologies.
1085 return vertsPerDraw
;
1089 //////////////////////////////////////////////////////////////////////////
1090 /// @brief DrawInstanced
1091 /// @param hContext - Handle passed back from SwrCreateContext
1092 /// @param topology - Specifies topology for draw.
1093 /// @param numVerts - How many vertices to read sequentially from vertex data (per instance).
1094 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1095 /// @param numInstances - How many instances to render.
1096 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1099 PRIMITIVE_TOPOLOGY topology
,
1100 uint32_t numVertices
,
1101 uint32_t startVertex
,
1102 uint32_t numInstances
= 1,
1103 uint32_t startInstance
= 0)
1110 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1111 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1113 AR_API_BEGIN(APIDraw
, pDC
->drawId
);
1114 AR_API_EVENT(DrawInstancedEvent(pDC
->drawId
, topology
, numVertices
, startVertex
, numInstances
, startInstance
));
1116 uint32_t maxVertsPerDraw
= MaxVertsPerDraw(pDC
, numVertices
, topology
);
1117 uint32_t primsPerDraw
= GetNumPrims(topology
, maxVertsPerDraw
);
1118 uint32_t remainingVerts
= numVertices
;
1120 API_STATE
*pState
= &pDC
->pState
->state
;
1121 pState
->topology
= topology
;
1122 pState
->forceFront
= false;
1124 // disable culling for points/lines
1125 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1126 if (topology
== TOP_POINT_LIST
)
1128 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1129 pState
->forceFront
= true;
1131 else if (topology
== TOP_RECT_LIST
)
1133 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1137 while (remainingVerts
)
1139 uint32_t numVertsForDraw
= (remainingVerts
< maxVertsPerDraw
) ?
1140 remainingVerts
: maxVertsPerDraw
;
1142 bool isSplitDraw
= (draw
> 0) ? true : false;
1143 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
, isSplitDraw
);
1144 InitDraw(pDC
, isSplitDraw
);
1146 pDC
->FeWork
.type
= DRAW
;
1147 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1149 false, // bEnableCutIndex
1150 pState
->tsState
.tsEnable
,
1151 pState
->gsState
.gsEnable
,
1152 pState
->soState
.soEnable
,
1153 pDC
->pState
->pfnProcessPrims
!= nullptr);
1154 pDC
->FeWork
.desc
.draw
.numVerts
= numVertsForDraw
;
1155 pDC
->FeWork
.desc
.draw
.startVertex
= startVertex
;
1156 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1157 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1158 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1159 pDC
->FeWork
.desc
.draw
.startVertexID
= draw
* maxVertsPerDraw
;
1161 pDC
->cleanupState
= (remainingVerts
== numVertsForDraw
);
1164 QueueDraw(pContext
);
1166 remainingVerts
-= numVertsForDraw
;
1170 // restore culling state
1171 pDC
= GetDrawContext(pContext
);
1172 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1174 AR_API_END(APIDraw
, numVertices
* numInstances
);
1177 //////////////////////////////////////////////////////////////////////////
1179 /// @param hContext - Handle passed back from SwrCreateContext
1180 /// @param topology - Specifies topology for draw.
1181 /// @param startVertex - Specifies start vertex in vertex buffer for draw.
1182 /// @param primCount - Number of vertices.
1185 PRIMITIVE_TOPOLOGY topology
,
1186 uint32_t startVertex
,
1187 uint32_t numVertices
)
1189 DrawInstanced(hContext
, topology
, numVertices
, startVertex
);
1192 //////////////////////////////////////////////////////////////////////////
1193 /// @brief SwrDrawInstanced
1194 /// @param hContext - Handle passed back from SwrCreateContext
1195 /// @param topology - Specifies topology for draw.
1196 /// @param numVertsPerInstance - How many vertices to read sequentially from vertex data.
1197 /// @param numInstances - How many instances to render.
1198 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1199 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1200 void SwrDrawInstanced(
1202 PRIMITIVE_TOPOLOGY topology
,
1203 uint32_t numVertsPerInstance
,
1204 uint32_t numInstances
,
1205 uint32_t startVertex
,
1206 uint32_t startInstance
1209 DrawInstanced(hContext
, topology
, numVertsPerInstance
, startVertex
, numInstances
, startInstance
);
1212 //////////////////////////////////////////////////////////////////////////
1213 /// @brief DrawIndexedInstanced
1214 /// @param hContext - Handle passed back from SwrCreateContext
1215 /// @param topology - Specifies topology for draw.
1216 /// @param numIndices - Number of indices to read sequentially from index buffer.
1217 /// @param indexOffset - Starting index into index buffer.
1218 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1219 /// @param numInstances - Number of instances to render.
1220 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1221 void DrawIndexedInstance(
1223 PRIMITIVE_TOPOLOGY topology
,
1224 uint32_t numIndices
,
1225 uint32_t indexOffset
,
1227 uint32_t numInstances
= 1,
1228 uint32_t startInstance
= 0)
1235 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1236 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1237 API_STATE
* pState
= &pDC
->pState
->state
;
1239 AR_API_BEGIN(APIDrawIndexed
, pDC
->drawId
);
1240 AR_API_EVENT(DrawIndexedInstancedEvent(pDC
->drawId
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
));
1242 uint32_t maxIndicesPerDraw
= MaxVertsPerDraw(pDC
, numIndices
, topology
);
1243 uint32_t primsPerDraw
= GetNumPrims(topology
, maxIndicesPerDraw
);
1244 uint32_t remainingIndices
= numIndices
;
1246 uint32_t indexSize
= 0;
1247 switch (pState
->indexBuffer
.format
)
1249 case R32_UINT
: indexSize
= sizeof(uint32_t); break;
1250 case R16_UINT
: indexSize
= sizeof(uint16_t); break;
1251 case R8_UINT
: indexSize
= sizeof(uint8_t); break;
1257 uint8_t *pIB
= (uint8_t*)pState
->indexBuffer
.pIndices
;
1258 pIB
+= (uint64_t)indexOffset
* (uint64_t)indexSize
;
1260 pState
->topology
= topology
;
1261 pState
->forceFront
= false;
1263 // disable culling for points/lines
1264 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1265 if (topology
== TOP_POINT_LIST
)
1267 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1268 pState
->forceFront
= true;
1270 else if (topology
== TOP_RECT_LIST
)
1272 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1275 while (remainingIndices
)
1277 uint32_t numIndicesForDraw
= (remainingIndices
< maxIndicesPerDraw
) ?
1278 remainingIndices
: maxIndicesPerDraw
;
1280 // When breaking up draw, we need to obtain new draw context for each iteration.
1281 bool isSplitDraw
= (draw
> 0) ? true : false;
1282 pDC
= GetDrawContext(pContext
, isSplitDraw
);
1283 InitDraw(pDC
, isSplitDraw
);
1285 pDC
->FeWork
.type
= DRAW
;
1286 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1288 pState
->frontendState
.bEnableCutIndex
,
1289 pState
->tsState
.tsEnable
,
1290 pState
->gsState
.gsEnable
,
1291 pState
->soState
.soEnable
,
1292 pDC
->pState
->pfnProcessPrims
!= nullptr);
1293 pDC
->FeWork
.desc
.draw
.pDC
= pDC
;
1294 pDC
->FeWork
.desc
.draw
.numIndices
= numIndicesForDraw
;
1295 pDC
->FeWork
.desc
.draw
.pIB
= (int*)pIB
;
1296 pDC
->FeWork
.desc
.draw
.type
= pDC
->pState
->state
.indexBuffer
.format
;
1298 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1299 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1300 pDC
->FeWork
.desc
.draw
.baseVertex
= baseVertex
;
1301 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1303 pDC
->cleanupState
= (remainingIndices
== numIndicesForDraw
);
1306 QueueDraw(pContext
);
1308 pIB
+= maxIndicesPerDraw
* indexSize
;
1309 remainingIndices
-= numIndicesForDraw
;
1313 // restore culling state
1314 pDC
= GetDrawContext(pContext
);
1315 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1317 AR_API_END(APIDrawIndexed
, numIndices
* numInstances
);
1321 //////////////////////////////////////////////////////////////////////////
1322 /// @brief DrawIndexed
1323 /// @param hContext - Handle passed back from SwrCreateContext
1324 /// @param topology - Specifies topology for draw.
1325 /// @param numIndices - Number of indices to read sequentially from index buffer.
1326 /// @param indexOffset - Starting index into index buffer.
1327 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1328 void SwrDrawIndexed(
1330 PRIMITIVE_TOPOLOGY topology
,
1331 uint32_t numIndices
,
1332 uint32_t indexOffset
,
1336 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
);
1339 //////////////////////////////////////////////////////////////////////////
1340 /// @brief SwrDrawIndexedInstanced
1341 /// @param hContext - Handle passed back from SwrCreateContext
1342 /// @param topology - Specifies topology for draw.
1343 /// @param numIndices - Number of indices to read sequentially from index buffer.
1344 /// @param numInstances - Number of instances to render.
1345 /// @param indexOffset - Starting index into index buffer.
1346 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1347 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1348 void SwrDrawIndexedInstanced(
1350 PRIMITIVE_TOPOLOGY topology
,
1351 uint32_t numIndices
,
1352 uint32_t numInstances
,
1353 uint32_t indexOffset
,
1355 uint32_t startInstance
)
1357 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
);
1360 //////////////////////////////////////////////////////////////////////////
1361 /// @brief SwrInvalidateTiles
1362 /// @param hContext - Handle passed back from SwrCreateContext
1363 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to invalidate.
1364 /// @param invalidateRect - The pixel-coordinate rectangle to invalidate. This will be expanded to
1365 /// be hottile size-aligned.
1366 void SWR_API
SwrInvalidateTiles(
1368 uint32_t attachmentMask
,
1369 const SWR_RECT
& invalidateRect
)
1376 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1377 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1379 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1380 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1381 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1382 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= invalidateRect
;
1383 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1384 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_INVALID
;
1385 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= false;
1386 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= false;
1389 QueueDraw(pContext
);
1392 //////////////////////////////////////////////////////////////////////////
1393 /// @brief SwrDiscardRect
1394 /// @param hContext - Handle passed back from SwrCreateContext
1395 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to discard.
1396 /// @param rect - The pixel-coordinate rectangle to discard. Only fully-covered hottiles will be
1398 void SWR_API
SwrDiscardRect(
1400 uint32_t attachmentMask
,
1401 const SWR_RECT
& rect
)
1408 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1409 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1411 // Queue a load to the hottile
1412 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1413 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1414 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1415 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= rect
;
1416 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1417 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_RESOLVED
;
1418 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= true;
1419 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= true;
1422 QueueDraw(pContext
);
1425 //////////////////////////////////////////////////////////////////////////
1426 /// @brief SwrDispatch
1427 /// @param hContext - Handle passed back from SwrCreateContext
1428 /// @param threadGroupCountX - Number of thread groups dispatched in X direction
1429 /// @param threadGroupCountY - Number of thread groups dispatched in Y direction
1430 /// @param threadGroupCountZ - Number of thread groups dispatched in Z direction
1433 uint32_t threadGroupCountX
,
1434 uint32_t threadGroupCountY
,
1435 uint32_t threadGroupCountZ
)
1442 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1443 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1445 AR_API_BEGIN(APIDispatch
, pDC
->drawId
);
1446 AR_API_EVENT(DispatchEvent(pDC
->drawId
, threadGroupCountX
, threadGroupCountY
, threadGroupCountZ
));
1447 pDC
->isCompute
= true; // This is a compute context.
1449 COMPUTE_DESC
* pTaskData
= (COMPUTE_DESC
*)pDC
->pArena
->AllocAligned(sizeof(COMPUTE_DESC
), 64);
1451 pTaskData
->threadGroupCountX
= threadGroupCountX
;
1452 pTaskData
->threadGroupCountY
= threadGroupCountY
;
1453 pTaskData
->threadGroupCountZ
= threadGroupCountZ
;
1455 uint32_t totalThreadGroups
= threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
;
1456 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
1457 pDC
->pDispatch
= &pContext
->pDispatchQueueArray
[dcIndex
];
1458 pDC
->pDispatch
->initialize(totalThreadGroups
, pTaskData
, &ProcessComputeBE
);
1460 QueueDispatch(pContext
);
1461 AR_API_END(APIDispatch
, threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
);
1464 // Deswizzles, converts and stores current contents of the hot tiles to surface
1465 // described by pState
1466 void SWR_API
SwrStoreTiles(
1468 uint32_t attachmentMask
,
1469 SWR_TILE_STATE postStoreTileState
,
1470 const SWR_RECT
& storeRect
)
1477 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1478 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1480 AR_API_BEGIN(APIStoreTiles
, pDC
->drawId
);
1482 pDC
->FeWork
.type
= STORETILES
;
1483 pDC
->FeWork
.pfnWork
= ProcessStoreTiles
;
1484 pDC
->FeWork
.desc
.storeTiles
.attachmentMask
= attachmentMask
;
1485 pDC
->FeWork
.desc
.storeTiles
.postStoreTileState
= postStoreTileState
;
1486 pDC
->FeWork
.desc
.storeTiles
.rect
= storeRect
;
1487 pDC
->FeWork
.desc
.storeTiles
.rect
&= g_MaxScissorRect
;
1490 QueueDraw(pContext
);
1492 AR_API_END(APIStoreTiles
, 1);
1495 //////////////////////////////////////////////////////////////////////////
1496 /// @brief SwrClearRenderTarget - Clear attached render targets / depth / stencil
1497 /// @param hContext - Handle passed back from SwrCreateContext
1498 /// @param clearMask - combination of SWR_CLEAR_COLOR / SWR_CLEAR_DEPTH / SWR_CLEAR_STENCIL flags (or SWR_CLEAR_NONE)
1499 /// @param clearColor - color use for clearing render targets
1500 /// @param z - depth value use for clearing depth buffer
1501 /// @param stencil - stencil value used for clearing stencil buffer
1502 /// @param clearRect - The pixel-coordinate rectangle to clear in all cleared buffers
1503 void SWR_API
SwrClearRenderTarget(
1506 const float clearColor
[4],
1509 const SWR_RECT
& clearRect
)
1516 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1517 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1519 AR_API_BEGIN(APIClearRenderTarget
, pDC
->drawId
);
1523 flags
.mask
= clearMask
;
1525 pDC
->FeWork
.type
= CLEAR
;
1526 pDC
->FeWork
.pfnWork
= ProcessClear
;
1527 pDC
->FeWork
.desc
.clear
.rect
= clearRect
;
1528 pDC
->FeWork
.desc
.clear
.rect
&= g_MaxScissorRect
;
1529 pDC
->FeWork
.desc
.clear
.flags
= flags
;
1530 pDC
->FeWork
.desc
.clear
.clearDepth
= z
;
1531 pDC
->FeWork
.desc
.clear
.clearRTColor
[0] = clearColor
[0];
1532 pDC
->FeWork
.desc
.clear
.clearRTColor
[1] = clearColor
[1];
1533 pDC
->FeWork
.desc
.clear
.clearRTColor
[2] = clearColor
[2];
1534 pDC
->FeWork
.desc
.clear
.clearRTColor
[3] = clearColor
[3];
1535 pDC
->FeWork
.desc
.clear
.clearStencil
= stencil
;
1538 QueueDraw(pContext
);
1540 AR_API_END(APIClearRenderTarget
, 1);
1543 //////////////////////////////////////////////////////////////////////////
1544 /// @brief Returns a pointer to the private context state for the current
1545 /// draw operation. This is used for external componets such as the
1547 /// SWR is responsible for the allocation of the private context state.
1548 /// @param hContext - Handle passed back from SwrCreateContext
1549 VOID
* SwrGetPrivateContextState(
1552 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1553 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1554 DRAW_STATE
* pState
= pDC
->pState
;
1556 if (pState
->pPrivateState
== nullptr)
1558 pState
->pPrivateState
= pState
->pArena
->AllocAligned(pContext
->privateStateSize
, KNOB_SIMD_WIDTH
*sizeof(float));
1561 return pState
->pPrivateState
;
1564 //////////////////////////////////////////////////////////////////////////
1565 /// @brief Clients can use this to allocate memory for draw/dispatch
1566 /// operations. The memory will automatically be freed once operation
1567 /// has completed. Client can use this to allocate binding tables,
1568 /// etc. needed for shader execution.
1569 /// @param hContext - Handle passed back from SwrCreateContext
1570 /// @param size - Size of allocation
1571 /// @param align - Alignment needed for allocation.
1572 VOID
* SwrAllocDrawContextMemory(
1577 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1578 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1580 return pDC
->pState
->pArena
->AllocAligned(size
, align
);
1583 //////////////////////////////////////////////////////////////////////////
1584 /// @brief Enables stats counting
1585 /// @param hContext - Handle passed back from SwrCreateContext
1586 /// @param enable - If true then counts are incremented.
1587 void SwrEnableStats(
1591 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1592 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1594 pDC
->pState
->state
.enableStats
= enable
;
1597 //////////////////////////////////////////////////////////////////////////
1598 /// @brief Mark end of frame - used for performance profiling
1599 /// @param hContext - Handle passed back from SwrCreateContext
1600 void SWR_API
SwrEndFrame(
1603 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1604 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1607 AR_API_EVENT(FrameEndEvent(pContext
->frameCount
, pDC
->drawId
));
1609 pContext
->frameCount
++;