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/os.h"
48 static const SWR_RECT g_MaxScissorRect
= { 0, 0, KNOB_MAX_SCISSOR_X
, KNOB_MAX_SCISSOR_Y
};
50 void SetupDefaultState(SWR_CONTEXT
*pContext
);
52 static INLINE SWR_CONTEXT
* GetContext(HANDLE hContext
)
54 return (SWR_CONTEXT
*)hContext
;
57 void WakeAllThreads(SWR_CONTEXT
*pContext
)
59 pContext
->FifosNotEmpty
.notify_all();
62 //////////////////////////////////////////////////////////////////////////
63 /// @brief Create SWR Context.
64 /// @param pCreateInfo - pointer to creation info.
65 HANDLE
SwrCreateContext(
66 SWR_CREATECONTEXT_INFO
* pCreateInfo
)
71 void* pContextMem
= AlignedMalloc(sizeof(SWR_CONTEXT
), KNOB_SIMD_WIDTH
* 4);
72 memset(pContextMem
, 0, sizeof(SWR_CONTEXT
));
73 SWR_CONTEXT
*pContext
= new (pContextMem
) SWR_CONTEXT();
75 pContext
->privateStateSize
= pCreateInfo
->privateStateSize
;
77 pContext
->dcRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
78 pContext
->dsRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
80 pContext
->pMacroTileManagerArray
= (MacroTileMgr
*)AlignedMalloc(sizeof(MacroTileMgr
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
81 pContext
->pDispatchQueueArray
= (DispatchQueue
*)AlignedMalloc(sizeof(DispatchQueue
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
83 for (uint32_t dc
= 0; dc
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++dc
)
85 pContext
->dcRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
86 new (&pContext
->pMacroTileManagerArray
[dc
]) MacroTileMgr(*pContext
->dcRing
[dc
].pArena
);
87 new (&pContext
->pDispatchQueueArray
[dc
]) DispatchQueue();
89 pContext
->dsRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
92 pContext
->threadInfo
.MAX_WORKER_THREADS
= KNOB_MAX_WORKER_THREADS
;
93 pContext
->threadInfo
.MAX_NUMA_NODES
= KNOB_MAX_NUMA_NODES
;
94 pContext
->threadInfo
.MAX_CORES_PER_NUMA_NODE
= KNOB_MAX_CORES_PER_NUMA_NODE
;
95 pContext
->threadInfo
.MAX_THREADS_PER_CORE
= KNOB_MAX_THREADS_PER_CORE
;
96 pContext
->threadInfo
.SINGLE_THREADED
= KNOB_SINGLE_THREADED
;
98 if (pCreateInfo
->pThreadInfo
)
100 pContext
->threadInfo
= *pCreateInfo
->pThreadInfo
;
103 memset(&pContext
->WaitLock
, 0, sizeof(pContext
->WaitLock
));
104 memset(&pContext
->FifosNotEmpty
, 0, sizeof(pContext
->FifosNotEmpty
));
105 new (&pContext
->WaitLock
) std::mutex();
106 new (&pContext
->FifosNotEmpty
) std::condition_variable();
108 CreateThreadPool(pContext
, &pContext
->threadPool
);
110 pContext
->ppScratch
= new uint8_t*[pContext
->NumWorkerThreads
];
111 pContext
->pStats
= new SWR_STATS
[pContext
->NumWorkerThreads
];
113 #if defined(KNOB_ENABLE_AR)
114 // Setup ArchRast thread contexts which includes +1 for API thread.
115 pContext
->pArContext
= new HANDLE
[pContext
->NumWorkerThreads
+1];
116 pContext
->pArContext
[pContext
->NumWorkerThreads
] = ArchRast::CreateThreadContext(ArchRast::AR_THREAD::API
);
119 // Allocate scratch space for workers.
120 ///@note We could lazily allocate this but its rather small amount of memory.
121 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
124 uint32_t numaNode
= pContext
->threadPool
.pThreadData
?
125 pContext
->threadPool
.pThreadData
[i
].numaId
: 0;
126 pContext
->ppScratch
[i
] = (uint8_t*)VirtualAllocExNuma(
127 GetCurrentProcess(), nullptr, 32 * sizeof(KILOBYTE
),
128 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
,
131 pContext
->ppScratch
[i
] = (uint8_t*)AlignedMalloc(32 * sizeof(KILOBYTE
), KNOB_SIMD_WIDTH
* 4);
134 #if defined(KNOB_ENABLE_AR)
135 // Initialize worker thread context for ArchRast.
136 pContext
->pArContext
[i
] = ArchRast::CreateThreadContext(ArchRast::AR_THREAD::WORKER
);
140 // State setup AFTER context is fully initialized
141 SetupDefaultState(pContext
);
143 // initialize hot tile manager
144 pContext
->pHotTileMgr
= new HotTileMgr();
146 // initialize callback functions
147 pContext
->pfnLoadTile
= pCreateInfo
->pfnLoadTile
;
148 pContext
->pfnStoreTile
= pCreateInfo
->pfnStoreTile
;
149 pContext
->pfnClearTile
= pCreateInfo
->pfnClearTile
;
150 pContext
->pfnUpdateSoWriteOffset
= pCreateInfo
->pfnUpdateSoWriteOffset
;
151 pContext
->pfnUpdateStats
= pCreateInfo
->pfnUpdateStats
;
152 pContext
->pfnUpdateStatsFE
= pCreateInfo
->pfnUpdateStatsFE
;
155 // pass pointer to bucket manager back to caller
156 #ifdef KNOB_ENABLE_RDTSC
157 pCreateInfo
->pBucketMgr
= &gBucketMgr
;
160 pCreateInfo
->contextSaveSize
= sizeof(API_STATE
);
162 StartThreadPool(pContext
, &pContext
->threadPool
);
164 return (HANDLE
)pContext
;
167 void CopyState(DRAW_STATE
& dst
, const DRAW_STATE
& src
)
169 memcpy(&dst
.state
, &src
.state
, sizeof(API_STATE
));
172 template<bool IsDraw
>
173 void QueueWork(SWR_CONTEXT
*pContext
)
175 DRAW_CONTEXT
* pDC
= pContext
->pCurDrawContext
;
176 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
180 pDC
->pTileMgr
= &pContext
->pMacroTileManagerArray
[dcIndex
];
181 pDC
->pTileMgr
->initialize();
184 // Each worker thread looks at a DC for both FE and BE work at different times and so we
185 // multiply threadDone by 2. When the threadDone counter has reached 0 then all workers
186 // have moved past this DC. (i.e. Each worker has checked this DC for both FE and BE work and
187 // then moved on if all work is done.)
188 pContext
->pCurDrawContext
->threadsDone
= pContext
->NumFEThreads
+ pContext
->NumBEThreads
;
192 InterlockedIncrement((volatile LONG
*)&pContext
->drawsOutstandingFE
);
197 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
);
198 pContext
->dcRing
.Enqueue();
201 if (pContext
->threadInfo
.SINGLE_THREADED
)
203 // flush denormals to 0
204 uint32_t mxcsr
= _mm_getcsr();
205 _mm_setcsr(mxcsr
| _MM_FLUSH_ZERO_ON
| _MM_DENORMALS_ZERO_ON
);
209 uint32_t curDraw
[2] = { pContext
->pCurDrawContext
->drawId
, pContext
->pCurDrawContext
->drawId
};
210 WorkOnFifoFE(pContext
, 0, curDraw
[0]);
211 WorkOnFifoBE(pContext
, 0, curDraw
[1], pContext
->singleThreadLockedTiles
, 0, 0);
215 uint32_t curDispatch
= pContext
->pCurDrawContext
->drawId
;
216 WorkOnCompute(pContext
, 0, curDispatch
);
219 // Dequeue the work here, if not already done, since we're single threaded (i.e. no workers).
220 while (CompleteDrawContext(pContext
, pContext
->pCurDrawContext
) > 0) {}
227 AR_API_BEGIN(APIDrawWakeAllThreads
, pDC
->drawId
);
228 WakeAllThreads(pContext
);
229 AR_API_END(APIDrawWakeAllThreads
, 1);
232 // Set current draw context to NULL so that next state call forces a new draw context to be created and populated.
233 pContext
->pPrevDrawContext
= pContext
->pCurDrawContext
;
234 pContext
->pCurDrawContext
= nullptr;
237 INLINE
void QueueDraw(SWR_CONTEXT
* pContext
)
239 QueueWork
<true>(pContext
);
242 INLINE
void QueueDispatch(SWR_CONTEXT
* pContext
)
244 QueueWork
<false>(pContext
);
247 DRAW_CONTEXT
* GetDrawContext(SWR_CONTEXT
*pContext
, bool isSplitDraw
= false)
249 AR_API_BEGIN(APIGetDrawContext
, 0);
250 // If current draw context is null then need to obtain a new draw context to use from ring.
251 if (pContext
->pCurDrawContext
== nullptr)
253 // Need to wait for a free entry.
254 while (pContext
->dcRing
.IsFull())
259 uint64_t curDraw
= pContext
->dcRing
.GetHead();
260 uint32_t dcIndex
= curDraw
% KNOB_MAX_DRAWS_IN_FLIGHT
;
262 if ((pContext
->frameCount
- pContext
->lastFrameChecked
) > 2 ||
263 (curDraw
- pContext
->lastDrawChecked
) > 0x10000)
265 // Take this opportunity to clean-up old arena allocations
266 pContext
->cachingArenaAllocator
.FreeOldBlocks();
268 pContext
->lastFrameChecked
= pContext
->frameCount
;
269 pContext
->lastDrawChecked
= curDraw
;
272 DRAW_CONTEXT
* pCurDrawContext
= &pContext
->dcRing
[dcIndex
];
273 pContext
->pCurDrawContext
= pCurDrawContext
;
275 // Assign next available entry in DS ring to this DC.
276 uint32_t dsIndex
= pContext
->curStateId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
277 pCurDrawContext
->pState
= &pContext
->dsRing
[dsIndex
];
279 // Copy previous state to current state.
280 if (pContext
->pPrevDrawContext
)
282 DRAW_CONTEXT
* pPrevDrawContext
= pContext
->pPrevDrawContext
;
284 // If we're splitting our draw then we can just use the same state from the previous
285 // draw. In this case, we won't increment the DS ring index so the next non-split
286 // draw can receive the state.
287 if (isSplitDraw
== false)
289 CopyState(*pCurDrawContext
->pState
, *pPrevDrawContext
->pState
);
291 // Should have been cleaned up previously
292 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
294 pCurDrawContext
->pState
->pPrivateState
= nullptr;
296 pContext
->curStateId
++; // Progress state ring index forward.
300 // If its a split draw then just copy the state pointer over
301 // since its the same draw.
302 pCurDrawContext
->pState
= pPrevDrawContext
->pState
;
303 SWR_ASSERT(pPrevDrawContext
->cleanupState
== false);
308 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
309 pContext
->curStateId
++; // Progress state ring index forward.
312 SWR_ASSERT(pCurDrawContext
->pArena
->IsEmpty() == true);
315 pCurDrawContext
->dependent
= false;
316 pCurDrawContext
->dependentFE
= false;
318 pCurDrawContext
->pContext
= pContext
;
319 pCurDrawContext
->isCompute
= false; // Dispatch has to set this to true.
321 pCurDrawContext
->doneFE
= false;
322 pCurDrawContext
->FeLock
= 0;
323 pCurDrawContext
->threadsDone
= 0;
324 pCurDrawContext
->retireCallback
.pfnCallbackFunc
= nullptr;
326 pCurDrawContext
->dynState
.Reset(pContext
->NumWorkerThreads
);
328 // Assign unique drawId for this DC
329 pCurDrawContext
->drawId
= pContext
->dcRing
.GetHead();
331 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
]);
386 #if defined(KNOB_ENABLE_AR)
387 ArchRast::DestroyThreadContext(pContext
->pArContext
[i
]);
391 delete[] pContext
->ppScratch
;
392 delete[] pContext
->pStats
;
394 delete(pContext
->pHotTileMgr
);
396 pContext
->~SWR_CONTEXT();
397 AlignedFree(GetContext(hContext
));
400 void SWR_API
SwrSaveState(
402 void* pOutputStateBlock
,
405 SWR_CONTEXT
*pContext
= GetContext(hContext
);
406 auto pSrc
= GetDrawState(pContext
);
407 SWR_ASSERT(pOutputStateBlock
&& memSize
>= sizeof(*pSrc
));
409 memcpy(pOutputStateBlock
, pSrc
, sizeof(*pSrc
));
412 void SWR_API
SwrRestoreState(
414 const void* pStateBlock
,
417 SWR_CONTEXT
*pContext
= GetContext(hContext
);
418 auto pDst
= GetDrawState(pContext
);
419 SWR_ASSERT(pStateBlock
&& memSize
>= sizeof(*pDst
));
421 memcpy(pDst
, pStateBlock
, sizeof(*pDst
));
424 void SetupDefaultState(SWR_CONTEXT
*pContext
)
426 API_STATE
* pState
= GetDrawState(pContext
);
428 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
429 pState
->rastState
.frontWinding
= SWR_FRONTWINDING_CCW
;
431 pState
->depthBoundsState
.depthBoundsTestEnable
= false;
432 pState
->depthBoundsState
.depthBoundsTestMinValue
= 0.0f
;
433 pState
->depthBoundsState
.depthBoundsTestMaxValue
= 1.0f
;
436 void SwrSync(HANDLE hContext
, PFN_CALLBACK_FUNC pfnFunc
, uint64_t userData
, uint64_t userData2
, uint64_t userData3
)
438 SWR_ASSERT(pfnFunc
!= nullptr);
440 SWR_CONTEXT
*pContext
= GetContext(hContext
);
441 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
443 AR_API_BEGIN(APISync
, 0);
445 pDC
->FeWork
.type
= SYNC
;
446 pDC
->FeWork
.pfnWork
= ProcessSync
;
448 // Setup callback function
449 pDC
->retireCallback
.pfnCallbackFunc
= pfnFunc
;
450 pDC
->retireCallback
.userData
= userData
;
451 pDC
->retireCallback
.userData2
= userData2
;
452 pDC
->retireCallback
.userData3
= userData3
;
454 AR_API_EVENT(SwrSyncEvent(pDC
->drawId
));
459 AR_API_END(APISync
, 1);
462 void SwrWaitForIdle(HANDLE hContext
)
464 SWR_CONTEXT
*pContext
= GetContext(hContext
);
466 AR_API_BEGIN(APIWaitForIdle
, 0);
468 while (!pContext
->dcRing
.IsEmpty())
473 AR_API_END(APIWaitForIdle
, 1);
476 void SwrWaitForIdleFE(HANDLE hContext
)
478 SWR_CONTEXT
*pContext
= GetContext(hContext
);
480 AR_API_BEGIN(APIWaitForIdle
, 0);
482 while (pContext
->drawsOutstandingFE
> 0)
487 AR_API_END(APIWaitForIdle
, 1);
490 void SwrSetVertexBuffers(
493 const SWR_VERTEX_BUFFER_STATE
* pVertexBuffers
)
495 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
497 for (uint32_t i
= 0; i
< numBuffers
; ++i
)
499 const SWR_VERTEX_BUFFER_STATE
*pVB
= &pVertexBuffers
[i
];
500 pState
->vertexBuffers
[pVB
->index
] = *pVB
;
504 void SwrSetIndexBuffer(
506 const SWR_INDEX_BUFFER_STATE
* pIndexBuffer
)
508 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
510 pState
->indexBuffer
= *pIndexBuffer
;
513 void SwrSetFetchFunc(
515 PFN_FETCH_FUNC pfnFetchFunc
)
517 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
519 pState
->pfnFetchFunc
= pfnFetchFunc
;
524 PFN_SO_FUNC pfnSoFunc
,
525 uint32_t streamIndex
)
527 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
529 SWR_ASSERT(streamIndex
< MAX_SO_STREAMS
);
531 pState
->pfnSoFunc
[streamIndex
] = pfnSoFunc
;
536 SWR_STREAMOUT_STATE
* pSoState
)
538 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
540 pState
->soState
= *pSoState
;
543 void SwrSetSoBuffers(
545 SWR_STREAMOUT_BUFFER
* pSoBuffer
,
548 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
550 SWR_ASSERT((slot
< 4), "There are only 4 SO buffer slots [0, 3]\nSlot requested: %d", slot
);
552 pState
->soBuffer
[slot
] = *pSoBuffer
;
555 void SwrSetVertexFunc(
557 PFN_VERTEX_FUNC pfnVertexFunc
)
559 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
561 pState
->pfnVertexFunc
= pfnVertexFunc
;
564 void SwrSetFrontendState(
566 SWR_FRONTEND_STATE
*pFEState
)
568 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
569 pState
->frontendState
= *pFEState
;
574 SWR_GS_STATE
*pGSState
)
576 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
577 pState
->gsState
= *pGSState
;
582 PFN_GS_FUNC pfnGsFunc
)
584 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
585 pState
->pfnGsFunc
= pfnGsFunc
;
590 PFN_CS_FUNC pfnCsFunc
,
591 uint32_t totalThreadsInGroup
,
592 uint32_t totalSpillFillSize
,
593 uint32_t scratchSpaceSizePerInstance
,
594 uint32_t numInstances
)
596 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
597 pState
->pfnCsFunc
= pfnCsFunc
;
598 pState
->totalThreadsInGroup
= totalThreadsInGroup
;
599 pState
->totalSpillFillSize
= totalSpillFillSize
;
600 pState
->scratchSpaceSize
= scratchSpaceSizePerInstance
;
601 pState
->scratchSpaceNumInstances
= numInstances
;
606 SWR_TS_STATE
*pState
)
608 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
609 pApiState
->tsState
= *pState
;
616 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
617 pApiState
->pfnHsFunc
= pfnFunc
;
624 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
625 pApiState
->pfnDsFunc
= pfnFunc
;
628 void SwrSetDepthStencilState(
630 SWR_DEPTH_STENCIL_STATE
*pDSState
)
632 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
634 pState
->depthStencilState
= *pDSState
;
637 void SwrSetBackendState(
639 SWR_BACKEND_STATE
*pBEState
)
641 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
643 pState
->backendState
= *pBEState
;
646 void SwrSetDepthBoundsState(
648 SWR_DEPTH_BOUNDS_STATE
*pDBState
)
650 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
652 pState
->depthBoundsState
= *pDBState
;
655 void SwrSetPixelShaderState(
657 SWR_PS_STATE
*pPSState
)
659 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
660 pState
->psState
= *pPSState
;
663 void SwrSetBlendState(
665 SWR_BLEND_STATE
*pBlendState
)
667 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
668 memcpy(&pState
->blendState
, pBlendState
, sizeof(SWR_BLEND_STATE
));
671 void SwrSetBlendFunc(
673 uint32_t renderTarget
,
674 PFN_BLEND_JIT_FUNC pfnBlendFunc
)
676 SWR_ASSERT(renderTarget
< SWR_NUM_RENDERTARGETS
);
677 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
678 pState
->pfnBlendFunc
[renderTarget
] = pfnBlendFunc
;
681 // update guardband multipliers for the viewport
682 void updateGuardbands(API_STATE
*pState
)
684 uint32_t numGbs
= pState
->gsState
.emitsRenderTargetArrayIndex
? KNOB_NUM_VIEWPORTS_SCISSORS
: 1;
686 for(uint32_t i
= 0; i
< numGbs
; ++i
)
688 // guardband center is viewport center
689 pState
->gbState
.left
[i
] = KNOB_GUARDBAND_WIDTH
/ pState
->vp
[i
].width
;
690 pState
->gbState
.right
[i
] = KNOB_GUARDBAND_WIDTH
/ pState
->vp
[i
].width
;
691 pState
->gbState
.top
[i
] = KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[i
].height
;
692 pState
->gbState
.bottom
[i
] = KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[i
].height
;
696 void SwrSetRastState(
698 const SWR_RASTSTATE
*pRastState
)
700 SWR_CONTEXT
*pContext
= GetContext(hContext
);
701 API_STATE
* pState
= GetDrawState(pContext
);
703 memcpy(&pState
->rastState
, pRastState
, sizeof(SWR_RASTSTATE
));
706 void SwrSetViewports(
708 uint32_t numViewports
,
709 const SWR_VIEWPORT
* pViewports
,
710 const SWR_VIEWPORT_MATRICES
* pMatrices
)
712 SWR_ASSERT(numViewports
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
713 "Invalid number of viewports.");
715 SWR_CONTEXT
*pContext
= GetContext(hContext
);
716 API_STATE
* pState
= GetDrawState(pContext
);
718 memcpy(&pState
->vp
[0], pViewports
, sizeof(SWR_VIEWPORT
) * numViewports
);
719 // @todo Faster to copy portions of the SOA or just copy all of it?
720 memcpy(&pState
->vpMatrices
, pMatrices
, sizeof(SWR_VIEWPORT_MATRICES
));
722 updateGuardbands(pState
);
725 void SwrSetScissorRects(
727 uint32_t numScissors
,
728 const SWR_RECT
* pScissors
)
730 SWR_ASSERT(numScissors
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
731 "Invalid number of scissor rects.");
733 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
734 memcpy(&pState
->scissorRects
[0], pScissors
, numScissors
* sizeof(pScissors
[0]));
737 void SetupMacroTileScissors(DRAW_CONTEXT
*pDC
)
739 API_STATE
*pState
= &pDC
->pState
->state
;
740 uint32_t numScissors
= pState
->gsState
.emitsViewportArrayIndex
? KNOB_NUM_VIEWPORTS_SCISSORS
: 1;
741 pState
->scissorsTileAligned
= true;
743 for (uint32_t index
= 0; index
< numScissors
; ++index
)
745 SWR_RECT
&scissorInFixedPoint
= pState
->scissorsInFixedPoint
[index
];
747 // Set up scissor dimensions based on scissor or viewport
748 if (pState
->rastState
.scissorEnable
)
750 scissorInFixedPoint
= pState
->scissorRects
[index
];
754 // the vp width and height must be added to origin un-rounded then the result round to -inf.
755 // The cast to int works for rounding assuming all [left, right, top, bottom] are positive.
756 scissorInFixedPoint
.xmin
= (int32_t)pState
->vp
[index
].x
;
757 scissorInFixedPoint
.xmax
= (int32_t)(pState
->vp
[index
].x
+ pState
->vp
[index
].width
);
758 scissorInFixedPoint
.ymin
= (int32_t)pState
->vp
[index
].y
;
759 scissorInFixedPoint
.ymax
= (int32_t)(pState
->vp
[index
].y
+ pState
->vp
[index
].height
);
763 scissorInFixedPoint
&= g_MaxScissorRect
;
765 // Test for tile alignment
767 tileAligned
= (scissorInFixedPoint
.xmin
% KNOB_TILE_X_DIM
) == 0;
768 tileAligned
&= (scissorInFixedPoint
.ymin
% KNOB_TILE_Y_DIM
) == 0;
769 tileAligned
&= (scissorInFixedPoint
.xmax
% KNOB_TILE_X_DIM
) == 0;
770 tileAligned
&= (scissorInFixedPoint
.ymax
% KNOB_TILE_Y_DIM
) == 0;
772 pState
->scissorsTileAligned
&= tileAligned
;
774 // Scale to fixed point
775 scissorInFixedPoint
.xmin
*= FIXED_POINT_SCALE
;
776 scissorInFixedPoint
.xmax
*= FIXED_POINT_SCALE
;
777 scissorInFixedPoint
.ymin
*= FIXED_POINT_SCALE
;
778 scissorInFixedPoint
.ymax
*= FIXED_POINT_SCALE
;
780 // Make scissor inclusive
781 scissorInFixedPoint
.xmax
-= 1;
782 scissorInFixedPoint
.ymax
-= 1;
786 // templated backend function tables
787 extern PFN_BACKEND_FUNC gBackendNullPs
[SWR_MULTISAMPLE_TYPE_COUNT
];
788 extern PFN_BACKEND_FUNC gBackendSingleSample
[SWR_INPUT_COVERAGE_COUNT
][2][2];
789 extern PFN_BACKEND_FUNC gBackendSampleRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2];
790 void SetupPipeline(DRAW_CONTEXT
*pDC
)
792 DRAW_STATE
* pState
= pDC
->pState
;
793 const SWR_RASTSTATE
&rastState
= pState
->state
.rastState
;
794 const SWR_PS_STATE
&psState
= pState
->state
.psState
;
795 BACKEND_FUNCS
& backendFuncs
= pState
->backendFuncs
;
798 if (psState
.pfnPixelShader
== nullptr)
800 backendFuncs
.pfnBackend
= gBackendNullPs
[pState
->state
.rastState
.sampleCount
];
804 const uint32_t forcedSampleCount
= (rastState
.forcedSampleCount
) ? 1 : 0;
805 const bool bMultisampleEnable
= ((rastState
.sampleCount
> SWR_MULTISAMPLE_1X
) || forcedSampleCount
) ? 1 : 0;
806 const uint32_t centroid
= ((psState
.barycentricsMask
& SWR_BARYCENTRIC_CENTROID_MASK
) > 0) ? 1 : 0;
807 const uint32_t canEarlyZ
= (psState
.forceEarlyZ
|| (!psState
.writesODepth
&& !psState
.usesUAV
)) ? 1 : 0;
808 SWR_BARYCENTRICS_MASK barycentricsMask
= (SWR_BARYCENTRICS_MASK
)psState
.barycentricsMask
;
810 // select backend function
811 switch(psState
.shadingRate
)
813 case SWR_SHADING_RATE_PIXEL
:
814 if(bMultisampleEnable
)
816 // always need to generate I & J per sample for Z interpolation
817 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
818 backendFuncs
.pfnBackend
= gBackendPixelRateTable
[rastState
.sampleCount
][rastState
.bIsCenterPattern
][psState
.inputCoverage
]
819 [centroid
][forcedSampleCount
][canEarlyZ
]
824 // always need to generate I & J per pixel for Z interpolation
825 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_PIXEL_MASK
);
826 backendFuncs
.pfnBackend
= gBackendSingleSample
[psState
.inputCoverage
][centroid
][canEarlyZ
];
829 case SWR_SHADING_RATE_SAMPLE
:
830 SWR_ASSERT(rastState
.bIsCenterPattern
!= true);
831 // always need to generate I & J per sample for Z interpolation
832 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
833 backendFuncs
.pfnBackend
= gBackendSampleRateTable
[rastState
.sampleCount
][psState
.inputCoverage
][centroid
][canEarlyZ
];
836 SWR_ASSERT(0 && "Invalid shading rate");
841 PFN_PROCESS_PRIMS pfnBinner
;
842 #if USE_SIMD16_FRONTEND
843 PFN_PROCESS_PRIMS_SIMD16 pfnBinner_simd16
;
845 switch (pState
->state
.topology
)
848 pState
->pfnProcessPrims
= ClipPoints
;
849 pfnBinner
= BinPoints
;
850 #if USE_SIMD16_FRONTEND
851 pState
->pfnProcessPrims_simd16
= ClipPoints_simd16
;
852 pfnBinner_simd16
= BinPoints_simd16
;
858 case TOP_LINE_LIST_ADJ
:
859 case TOP_LISTSTRIP_ADJ
:
860 pState
->pfnProcessPrims
= ClipLines
;
861 pfnBinner
= BinLines
;
862 #if USE_SIMD16_FRONTEND
863 pState
->pfnProcessPrims_simd16
= ClipLines_simd16
;
864 pfnBinner_simd16
= BinLines_simd16
;
868 pState
->pfnProcessPrims
= ClipTriangles
;
869 pfnBinner
= GetBinTrianglesFunc((rastState
.conservativeRast
> 0));
870 #if USE_SIMD16_FRONTEND
871 pState
->pfnProcessPrims_simd16
= ClipTriangles_simd16
;
872 pfnBinner_simd16
= GetBinTrianglesFunc_simd16((rastState
.conservativeRast
> 0));
878 // disable clipper if viewport transform is disabled
879 if (pState
->state
.frontendState
.vpTransformDisable
)
881 pState
->pfnProcessPrims
= pfnBinner
;
882 #if USE_SIMD16_FRONTEND
883 pState
->pfnProcessPrims_simd16
= pfnBinner_simd16
;
887 if ((pState
->state
.psState
.pfnPixelShader
== nullptr) &&
888 (pState
->state
.depthStencilState
.depthTestEnable
== FALSE
) &&
889 (pState
->state
.depthStencilState
.depthWriteEnable
== FALSE
) &&
890 (pState
->state
.depthStencilState
.stencilTestEnable
== FALSE
) &&
891 (pState
->state
.depthStencilState
.stencilWriteEnable
== FALSE
) &&
892 (pState
->state
.backendState
.numAttributes
== 0))
894 pState
->pfnProcessPrims
= nullptr;
895 #if USE_SIMD16_FRONTEND
896 pState
->pfnProcessPrims_simd16
= nullptr;
900 if (pState
->state
.soState
.rasterizerDisable
== true)
902 pState
->pfnProcessPrims
= nullptr;
903 #if USE_SIMD16_FRONTEND
904 pState
->pfnProcessPrims_simd16
= nullptr;
909 // set up the frontend attribute count
910 pState
->state
.feNumAttributes
= 0;
911 const SWR_BACKEND_STATE
& backendState
= pState
->state
.backendState
;
912 if (backendState
.swizzleEnable
)
914 // attribute swizzling is enabled, iterate over the map and record the max attribute used
915 for (uint32_t i
= 0; i
< backendState
.numAttributes
; ++i
)
917 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)backendState
.swizzleMap
[i
].sourceAttrib
+ 1);
922 pState
->state
.feNumAttributes
= pState
->state
.backendState
.numAttributes
;
925 if (pState
->state
.soState
.soEnable
)
927 uint32_t streamMasks
= 0;
928 for (uint32_t i
= 0; i
< 4; ++i
)
930 streamMasks
|= pState
->state
.soState
.streamMasks
[i
];
934 if (_BitScanReverse(&maxAttrib
, streamMasks
))
936 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)(maxAttrib
+ 1));
940 // complicated logic to test for cases where we don't need backing hottile memory for a draw
941 // have to check for the special case where depth/stencil test is enabled but depthwrite is disabled.
942 pState
->state
.depthHottileEnable
= ((!(pState
->state
.depthStencilState
.depthTestEnable
&&
943 !pState
->state
.depthStencilState
.depthWriteEnable
&&
944 !pState
->state
.depthBoundsState
.depthBoundsTestEnable
&&
945 pState
->state
.depthStencilState
.depthTestFunc
== ZFUNC_ALWAYS
)) &&
946 (pState
->state
.depthStencilState
.depthTestEnable
||
947 pState
->state
.depthStencilState
.depthWriteEnable
||
948 pState
->state
.depthBoundsState
.depthBoundsTestEnable
)) ? true : false;
950 pState
->state
.stencilHottileEnable
= (((!(pState
->state
.depthStencilState
.stencilTestEnable
&&
951 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
952 pState
->state
.depthStencilState
.stencilTestFunc
== ZFUNC_ALWAYS
)) ||
953 // for stencil we have to check the double sided state as well
954 (!(pState
->state
.depthStencilState
.doubleSidedStencilTestEnable
&&
955 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
956 pState
->state
.depthStencilState
.backfaceStencilTestFunc
== ZFUNC_ALWAYS
))) &&
957 (pState
->state
.depthStencilState
.stencilTestEnable
||
958 pState
->state
.depthStencilState
.stencilWriteEnable
)) ? true : false;
960 uint32_t numRTs
= pState
->state
.psState
.numRenderTargets
;
961 pState
->state
.colorHottileEnable
= 0;
962 if (psState
.pfnPixelShader
!= nullptr)
964 for (uint32_t rt
= 0; rt
< numRTs
; ++rt
)
966 pState
->state
.colorHottileEnable
|=
967 (!pState
->state
.blendState
.renderTarget
[rt
].writeDisableAlpha
||
968 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableRed
||
969 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableGreen
||
970 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableBlue
) ? (1 << rt
) : 0;
974 // Setup depth quantization function
975 if (pState
->state
.depthHottileEnable
)
977 switch (pState
->state
.rastState
.depthFormat
)
979 case R32_FLOAT_X8X24_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT_X8X24_TYPELESS
> ; break;
980 case R32_FLOAT
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ; break;
981 case R24_UNORM_X8_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R24_UNORM_X8_TYPELESS
> ; break;
982 case R16_UNORM
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R16_UNORM
> ; break;
983 default: SWR_INVALID("Unsupported depth format for depth quantiztion.");
984 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
989 // set up pass-through quantize if depth isn't enabled
990 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
994 //////////////////////////////////////////////////////////////////////////
996 /// @param pDC - Draw context to initialize for this draw.
1001 // We don't need to re-setup the scissors/pipeline state again for split draw.
1002 if (isSplitDraw
== false)
1004 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
;
1138 while (remainingVerts
)
1140 uint32_t numVertsForDraw
= (remainingVerts
< maxVertsPerDraw
) ?
1141 remainingVerts
: maxVertsPerDraw
;
1143 bool isSplitDraw
= (draw
> 0) ? true : false;
1144 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
, isSplitDraw
);
1145 InitDraw(pDC
, isSplitDraw
);
1147 pDC
->FeWork
.type
= DRAW
;
1148 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1150 false, // bEnableCutIndex
1151 pState
->tsState
.tsEnable
,
1152 pState
->gsState
.gsEnable
,
1153 pState
->soState
.soEnable
,
1154 pDC
->pState
->pfnProcessPrims
!= nullptr);
1155 pDC
->FeWork
.desc
.draw
.numVerts
= numVertsForDraw
;
1156 pDC
->FeWork
.desc
.draw
.startVertex
= startVertex
;
1157 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1158 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1159 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1160 pDC
->FeWork
.desc
.draw
.startVertexID
= draw
* maxVertsPerDraw
;
1162 pDC
->cleanupState
= (remainingVerts
== numVertsForDraw
);
1165 QueueDraw(pContext
);
1167 AR_API_EVENT(DrawInstancedSplitEvent(pDC
->drawId
));
1169 remainingVerts
-= numVertsForDraw
;
1173 // restore culling state
1174 pDC
= GetDrawContext(pContext
);
1175 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1178 AR_API_END(APIDraw
, numVertices
* numInstances
);
1181 //////////////////////////////////////////////////////////////////////////
1183 /// @param hContext - Handle passed back from SwrCreateContext
1184 /// @param topology - Specifies topology for draw.
1185 /// @param startVertex - Specifies start vertex in vertex buffer for draw.
1186 /// @param primCount - Number of vertices.
1189 PRIMITIVE_TOPOLOGY topology
,
1190 uint32_t startVertex
,
1191 uint32_t numVertices
)
1193 DrawInstanced(hContext
, topology
, numVertices
, startVertex
);
1196 //////////////////////////////////////////////////////////////////////////
1197 /// @brief SwrDrawInstanced
1198 /// @param hContext - Handle passed back from SwrCreateContext
1199 /// @param topology - Specifies topology for draw.
1200 /// @param numVertsPerInstance - How many vertices to read sequentially from vertex data.
1201 /// @param numInstances - How many instances to render.
1202 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1203 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1204 void SwrDrawInstanced(
1206 PRIMITIVE_TOPOLOGY topology
,
1207 uint32_t numVertsPerInstance
,
1208 uint32_t numInstances
,
1209 uint32_t startVertex
,
1210 uint32_t startInstance
1213 DrawInstanced(hContext
, topology
, numVertsPerInstance
, startVertex
, numInstances
, startInstance
);
1216 //////////////////////////////////////////////////////////////////////////
1217 /// @brief DrawIndexedInstanced
1218 /// @param hContext - Handle passed back from SwrCreateContext
1219 /// @param topology - Specifies topology for draw.
1220 /// @param numIndices - Number of indices to read sequentially from index buffer.
1221 /// @param indexOffset - Starting index into index buffer.
1222 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1223 /// @param numInstances - Number of instances to render.
1224 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1225 void DrawIndexedInstance(
1227 PRIMITIVE_TOPOLOGY topology
,
1228 uint32_t numIndices
,
1229 uint32_t indexOffset
,
1231 uint32_t numInstances
= 1,
1232 uint32_t startInstance
= 0)
1239 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1240 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1241 API_STATE
* pState
= &pDC
->pState
->state
;
1243 AR_API_BEGIN(APIDrawIndexed
, pDC
->drawId
);
1244 AR_API_EVENT(DrawIndexedInstancedEvent(pDC
->drawId
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
));
1246 uint32_t maxIndicesPerDraw
= MaxVertsPerDraw(pDC
, numIndices
, topology
);
1247 uint32_t primsPerDraw
= GetNumPrims(topology
, maxIndicesPerDraw
);
1248 uint32_t remainingIndices
= numIndices
;
1250 uint32_t indexSize
= 0;
1251 switch (pState
->indexBuffer
.format
)
1253 case R32_UINT
: indexSize
= sizeof(uint32_t); break;
1254 case R16_UINT
: indexSize
= sizeof(uint16_t); break;
1255 case R8_UINT
: indexSize
= sizeof(uint8_t); break;
1257 SWR_INVALID("Invalid index buffer format: %d", pState
->indexBuffer
.format
);
1261 uint8_t *pIB
= (uint8_t*)pState
->indexBuffer
.pIndices
;
1262 pIB
+= (uint64_t)indexOffset
* (uint64_t)indexSize
;
1264 pState
->topology
= topology
;
1265 pState
->forceFront
= false;
1267 // disable culling for points/lines
1268 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1269 if (topology
== TOP_POINT_LIST
)
1271 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1272 pState
->forceFront
= true;
1274 else if (topology
== TOP_RECT_LIST
)
1276 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1280 while (remainingIndices
)
1282 uint32_t numIndicesForDraw
= (remainingIndices
< maxIndicesPerDraw
) ?
1283 remainingIndices
: maxIndicesPerDraw
;
1285 // When breaking up draw, we need to obtain new draw context for each iteration.
1286 bool isSplitDraw
= (draw
> 0) ? true : false;
1288 pDC
= GetDrawContext(pContext
, isSplitDraw
);
1289 InitDraw(pDC
, isSplitDraw
);
1291 pDC
->FeWork
.type
= DRAW
;
1292 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1294 pState
->frontendState
.bEnableCutIndex
,
1295 pState
->tsState
.tsEnable
,
1296 pState
->gsState
.gsEnable
,
1297 pState
->soState
.soEnable
,
1298 pDC
->pState
->pfnProcessPrims
!= nullptr);
1299 pDC
->FeWork
.desc
.draw
.pDC
= pDC
;
1300 pDC
->FeWork
.desc
.draw
.numIndices
= numIndicesForDraw
;
1301 pDC
->FeWork
.desc
.draw
.pIB
= (int*)pIB
;
1302 pDC
->FeWork
.desc
.draw
.type
= pDC
->pState
->state
.indexBuffer
.format
;
1304 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1305 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1306 pDC
->FeWork
.desc
.draw
.baseVertex
= baseVertex
;
1307 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1309 pDC
->cleanupState
= (remainingIndices
== numIndicesForDraw
);
1312 QueueDraw(pContext
);
1314 AR_API_EVENT(DrawIndexedInstancedSplitEvent(pDC
->drawId
));
1316 pIB
+= maxIndicesPerDraw
* indexSize
;
1317 remainingIndices
-= numIndicesForDraw
;
1321 // Restore culling state
1322 pDC
= GetDrawContext(pContext
);
1323 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1326 AR_API_END(APIDrawIndexed
, numIndices
* numInstances
);
1330 //////////////////////////////////////////////////////////////////////////
1331 /// @brief DrawIndexed
1332 /// @param hContext - Handle passed back from SwrCreateContext
1333 /// @param topology - Specifies topology for draw.
1334 /// @param numIndices - Number of indices to read sequentially from index buffer.
1335 /// @param indexOffset - Starting index into index buffer.
1336 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1337 void SwrDrawIndexed(
1339 PRIMITIVE_TOPOLOGY topology
,
1340 uint32_t numIndices
,
1341 uint32_t indexOffset
,
1345 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
);
1348 //////////////////////////////////////////////////////////////////////////
1349 /// @brief SwrDrawIndexedInstanced
1350 /// @param hContext - Handle passed back from SwrCreateContext
1351 /// @param topology - Specifies topology for draw.
1352 /// @param numIndices - Number of indices to read sequentially from index buffer.
1353 /// @param numInstances - Number of instances to render.
1354 /// @param indexOffset - Starting index into index buffer.
1355 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1356 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1357 void SwrDrawIndexedInstanced(
1359 PRIMITIVE_TOPOLOGY topology
,
1360 uint32_t numIndices
,
1361 uint32_t numInstances
,
1362 uint32_t indexOffset
,
1364 uint32_t startInstance
)
1366 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
);
1369 //////////////////////////////////////////////////////////////////////////
1370 /// @brief SwrInvalidateTiles
1371 /// @param hContext - Handle passed back from SwrCreateContext
1372 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to invalidate.
1373 /// @param invalidateRect - The pixel-coordinate rectangle to invalidate. This will be expanded to
1374 /// be hottile size-aligned.
1375 void SWR_API
SwrInvalidateTiles(
1377 uint32_t attachmentMask
,
1378 const SWR_RECT
& invalidateRect
)
1385 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1386 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1388 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1389 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1390 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1391 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= invalidateRect
;
1392 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1393 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_INVALID
;
1394 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= false;
1395 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= false;
1398 QueueDraw(pContext
);
1400 AR_API_EVENT(SwrInvalidateTilesEvent(pDC
->drawId
));
1403 //////////////////////////////////////////////////////////////////////////
1404 /// @brief SwrDiscardRect
1405 /// @param hContext - Handle passed back from SwrCreateContext
1406 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to discard.
1407 /// @param rect - The pixel-coordinate rectangle to discard. Only fully-covered hottiles will be
1409 void SWR_API
SwrDiscardRect(
1411 uint32_t attachmentMask
,
1412 const SWR_RECT
& rect
)
1419 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1420 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1422 // Queue a load to the hottile
1423 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1424 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1425 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1426 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= rect
;
1427 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1428 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_RESOLVED
;
1429 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= true;
1430 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= true;
1433 QueueDraw(pContext
);
1435 AR_API_EVENT(SwrDiscardRectEvent(pDC
->drawId
));
1438 //////////////////////////////////////////////////////////////////////////
1439 /// @brief SwrDispatch
1440 /// @param hContext - Handle passed back from SwrCreateContext
1441 /// @param threadGroupCountX - Number of thread groups dispatched in X direction
1442 /// @param threadGroupCountY - Number of thread groups dispatched in Y direction
1443 /// @param threadGroupCountZ - Number of thread groups dispatched in Z direction
1446 uint32_t threadGroupCountX
,
1447 uint32_t threadGroupCountY
,
1448 uint32_t threadGroupCountZ
)
1455 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1456 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1458 AR_API_BEGIN(APIDispatch
, pDC
->drawId
);
1459 AR_API_EVENT(DispatchEvent(pDC
->drawId
, threadGroupCountX
, threadGroupCountY
, threadGroupCountZ
));
1460 pDC
->isCompute
= true; // This is a compute context.
1462 COMPUTE_DESC
* pTaskData
= (COMPUTE_DESC
*)pDC
->pArena
->AllocAligned(sizeof(COMPUTE_DESC
), 64);
1464 pTaskData
->threadGroupCountX
= threadGroupCountX
;
1465 pTaskData
->threadGroupCountY
= threadGroupCountY
;
1466 pTaskData
->threadGroupCountZ
= threadGroupCountZ
;
1468 uint32_t totalThreadGroups
= threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
;
1469 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
1470 pDC
->pDispatch
= &pContext
->pDispatchQueueArray
[dcIndex
];
1471 pDC
->pDispatch
->initialize(totalThreadGroups
, pTaskData
, &ProcessComputeBE
);
1473 QueueDispatch(pContext
);
1474 AR_API_END(APIDispatch
, threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
);
1477 // Deswizzles, converts and stores current contents of the hot tiles to surface
1478 // described by pState
1479 void SWR_API
SwrStoreTiles(
1481 uint32_t attachmentMask
,
1482 SWR_TILE_STATE postStoreTileState
,
1483 const SWR_RECT
& storeRect
)
1490 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1491 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1493 AR_API_BEGIN(APIStoreTiles
, pDC
->drawId
);
1495 pDC
->FeWork
.type
= STORETILES
;
1496 pDC
->FeWork
.pfnWork
= ProcessStoreTiles
;
1497 pDC
->FeWork
.desc
.storeTiles
.attachmentMask
= attachmentMask
;
1498 pDC
->FeWork
.desc
.storeTiles
.postStoreTileState
= postStoreTileState
;
1499 pDC
->FeWork
.desc
.storeTiles
.rect
= storeRect
;
1500 pDC
->FeWork
.desc
.storeTiles
.rect
&= g_MaxScissorRect
;
1503 QueueDraw(pContext
);
1505 AR_API_EVENT(SwrStoreTilesEvent(pDC
->drawId
));
1507 AR_API_END(APIStoreTiles
, 1);
1510 //////////////////////////////////////////////////////////////////////////
1511 /// @brief SwrClearRenderTarget - Clear attached render targets / depth / stencil
1512 /// @param hContext - Handle passed back from SwrCreateContext
1513 /// @param attachmentMask - combination of SWR_ATTACHMENT_*_BIT attachments to clear
1514 /// @param renderTargetArrayIndex - the RT array index to clear
1515 /// @param clearColor - color use for clearing render targets
1516 /// @param z - depth value use for clearing depth buffer
1517 /// @param stencil - stencil value used for clearing stencil buffer
1518 /// @param clearRect - The pixel-coordinate rectangle to clear in all cleared buffers
1519 void SWR_API
SwrClearRenderTarget(
1521 uint32_t attachmentMask
,
1522 uint32_t renderTargetArrayIndex
,
1523 const float clearColor
[4],
1526 const SWR_RECT
& clearRect
)
1533 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1534 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1536 AR_API_BEGIN(APIClearRenderTarget
, pDC
->drawId
);
1538 pDC
->FeWork
.type
= CLEAR
;
1539 pDC
->FeWork
.pfnWork
= ProcessClear
;
1540 pDC
->FeWork
.desc
.clear
.rect
= clearRect
;
1541 pDC
->FeWork
.desc
.clear
.rect
&= g_MaxScissorRect
;
1542 pDC
->FeWork
.desc
.clear
.attachmentMask
= attachmentMask
;
1543 pDC
->FeWork
.desc
.clear
.renderTargetArrayIndex
= renderTargetArrayIndex
;
1544 pDC
->FeWork
.desc
.clear
.clearDepth
= z
;
1545 pDC
->FeWork
.desc
.clear
.clearRTColor
[0] = clearColor
[0];
1546 pDC
->FeWork
.desc
.clear
.clearRTColor
[1] = clearColor
[1];
1547 pDC
->FeWork
.desc
.clear
.clearRTColor
[2] = clearColor
[2];
1548 pDC
->FeWork
.desc
.clear
.clearRTColor
[3] = clearColor
[3];
1549 pDC
->FeWork
.desc
.clear
.clearStencil
= stencil
;
1552 QueueDraw(pContext
);
1554 AR_API_END(APIClearRenderTarget
, 1);
1557 //////////////////////////////////////////////////////////////////////////
1558 /// @brief Returns a pointer to the private context state for the current
1559 /// draw operation. This is used for external componets such as the
1561 /// SWR is responsible for the allocation of the private context state.
1562 /// @param hContext - Handle passed back from SwrCreateContext
1563 VOID
* SwrGetPrivateContextState(
1566 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1567 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1568 DRAW_STATE
* pState
= pDC
->pState
;
1570 if (pState
->pPrivateState
== nullptr)
1572 pState
->pPrivateState
= pState
->pArena
->AllocAligned(pContext
->privateStateSize
, KNOB_SIMD_WIDTH
*sizeof(float));
1575 return pState
->pPrivateState
;
1578 //////////////////////////////////////////////////////////////////////////
1579 /// @brief Clients can use this to allocate memory for draw/dispatch
1580 /// operations. The memory will automatically be freed once operation
1581 /// has completed. Client can use this to allocate binding tables,
1582 /// etc. needed for shader execution.
1583 /// @param hContext - Handle passed back from SwrCreateContext
1584 /// @param size - Size of allocation
1585 /// @param align - Alignment needed for allocation.
1586 VOID
* SwrAllocDrawContextMemory(
1591 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1592 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1594 return pDC
->pState
->pArena
->AllocAligned(size
, align
);
1597 //////////////////////////////////////////////////////////////////////////
1598 /// @brief Enables stats counting
1599 /// @param hContext - Handle passed back from SwrCreateContext
1600 /// @param enable - If true then counts are incremented.
1601 void SwrEnableStatsFE(
1605 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1606 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1608 pDC
->pState
->state
.enableStatsFE
= enable
;
1611 //////////////////////////////////////////////////////////////////////////
1612 /// @brief Enables stats counting
1613 /// @param hContext - Handle passed back from SwrCreateContext
1614 /// @param enable - If true then counts are incremented.
1615 void SwrEnableStatsBE(
1619 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1620 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1622 pDC
->pState
->state
.enableStatsBE
= enable
;
1625 //////////////////////////////////////////////////////////////////////////
1626 /// @brief Mark end of frame - used for performance profiling
1627 /// @param hContext - Handle passed back from SwrCreateContext
1628 void SWR_API
SwrEndFrame(
1631 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1632 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1633 (void)pDC
; // var used
1636 AR_API_EVENT(FrameEndEvent(pContext
->frameCount
, pDC
->drawId
));
1638 pContext
->frameCount
++;
1641 void InitSimLoadTilesTable();
1642 void InitSimStoreTilesTable();
1643 void InitSimClearTilesTable();
1645 void InitClearTilesTable();
1646 void InitBackendFuncTables();
1648 //////////////////////////////////////////////////////////////////////////
1649 /// @brief Initialize swr backend and memory internal tables
1652 InitSimLoadTilesTable();
1653 InitSimStoreTilesTable();
1654 InitSimClearTilesTable();
1656 InitClearTilesTable();
1657 InitBackendFuncTables();
1661 void SwrGetInterface(SWR_INTERFACE
&out_funcs
)
1663 out_funcs
.pfnSwrCreateContext
= SwrCreateContext
;
1664 out_funcs
.pfnSwrDestroyContext
= SwrDestroyContext
;
1665 out_funcs
.pfnSwrSaveState
= SwrSaveState
;
1666 out_funcs
.pfnSwrRestoreState
= SwrRestoreState
;
1667 out_funcs
.pfnSwrSync
= SwrSync
;
1668 out_funcs
.pfnSwrWaitForIdle
= SwrWaitForIdle
;
1669 out_funcs
.pfnSwrWaitForIdleFE
= SwrWaitForIdleFE
;
1670 out_funcs
.pfnSwrSetVertexBuffers
= SwrSetVertexBuffers
;
1671 out_funcs
.pfnSwrSetIndexBuffer
= SwrSetIndexBuffer
;
1672 out_funcs
.pfnSwrSetFetchFunc
= SwrSetFetchFunc
;
1673 out_funcs
.pfnSwrSetSoFunc
= SwrSetSoFunc
;
1674 out_funcs
.pfnSwrSetSoState
= SwrSetSoState
;
1675 out_funcs
.pfnSwrSetSoBuffers
= SwrSetSoBuffers
;
1676 out_funcs
.pfnSwrSetVertexFunc
= SwrSetVertexFunc
;
1677 out_funcs
.pfnSwrSetFrontendState
= SwrSetFrontendState
;
1678 out_funcs
.pfnSwrSetGsState
= SwrSetGsState
;
1679 out_funcs
.pfnSwrSetGsFunc
= SwrSetGsFunc
;
1680 out_funcs
.pfnSwrSetCsFunc
= SwrSetCsFunc
;
1681 out_funcs
.pfnSwrSetTsState
= SwrSetTsState
;
1682 out_funcs
.pfnSwrSetHsFunc
= SwrSetHsFunc
;
1683 out_funcs
.pfnSwrSetDsFunc
= SwrSetDsFunc
;
1684 out_funcs
.pfnSwrSetDepthStencilState
= SwrSetDepthStencilState
;
1685 out_funcs
.pfnSwrSetBackendState
= SwrSetBackendState
;
1686 out_funcs
.pfnSwrSetDepthBoundsState
= SwrSetDepthBoundsState
;
1687 out_funcs
.pfnSwrSetPixelShaderState
= SwrSetPixelShaderState
;
1688 out_funcs
.pfnSwrSetBlendState
= SwrSetBlendState
;
1689 out_funcs
.pfnSwrSetBlendFunc
= SwrSetBlendFunc
;
1690 out_funcs
.pfnSwrDraw
= SwrDraw
;
1691 out_funcs
.pfnSwrDrawInstanced
= SwrDrawInstanced
;
1692 out_funcs
.pfnSwrDrawIndexed
= SwrDrawIndexed
;
1693 out_funcs
.pfnSwrDrawIndexedInstanced
= SwrDrawIndexedInstanced
;
1694 out_funcs
.pfnSwrInvalidateTiles
= SwrInvalidateTiles
;
1695 out_funcs
.pfnSwrDiscardRect
= SwrDiscardRect
;
1696 out_funcs
.pfnSwrDispatch
= SwrDispatch
;
1697 out_funcs
.pfnSwrStoreTiles
= SwrStoreTiles
;
1698 out_funcs
.pfnSwrClearRenderTarget
= SwrClearRenderTarget
;
1699 out_funcs
.pfnSwrSetRastState
= SwrSetRastState
;
1700 out_funcs
.pfnSwrSetViewports
= SwrSetViewports
;
1701 out_funcs
.pfnSwrSetScissorRects
= SwrSetScissorRects
;
1702 out_funcs
.pfnSwrGetPrivateContextState
= SwrGetPrivateContextState
;
1703 out_funcs
.pfnSwrAllocDrawContextMemory
= SwrAllocDrawContextMemory
;
1704 out_funcs
.pfnSwrEnableStatsFE
= SwrEnableStatsFE
;
1705 out_funcs
.pfnSwrEnableStatsBE
= SwrEnableStatsBE
;
1706 out_funcs
.pfnSwrEndFrame
= SwrEndFrame
;
1707 out_funcs
.pfnSwrInit
= SwrInit
;
1708 out_funcs
.pfnSwrLoadHotTile
= SwrLoadHotTile
;
1709 out_funcs
.pfnSwrStoreHotTileToSurface
= SwrStoreHotTileToSurface
;
1710 out_funcs
.pfnSwrStoreHotTileClear
= SwrStoreHotTileClear
;