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 function pointer tables
147 InitClearTilesTable();
149 // initialize callback functions
150 pContext
->pfnLoadTile
= pCreateInfo
->pfnLoadTile
;
151 pContext
->pfnStoreTile
= pCreateInfo
->pfnStoreTile
;
152 pContext
->pfnClearTile
= pCreateInfo
->pfnClearTile
;
153 pContext
->pfnUpdateSoWriteOffset
= pCreateInfo
->pfnUpdateSoWriteOffset
;
154 pContext
->pfnUpdateStats
= pCreateInfo
->pfnUpdateStats
;
155 pContext
->pfnUpdateStatsFE
= pCreateInfo
->pfnUpdateStatsFE
;
158 // pass pointer to bucket manager back to caller
159 #ifdef KNOB_ENABLE_RDTSC
160 pCreateInfo
->pBucketMgr
= &gBucketMgr
;
163 pCreateInfo
->contextSaveSize
= sizeof(API_STATE
);
165 StartThreadPool(pContext
, &pContext
->threadPool
);
167 return (HANDLE
)pContext
;
170 void CopyState(DRAW_STATE
& dst
, const DRAW_STATE
& src
)
172 memcpy(&dst
.state
, &src
.state
, sizeof(API_STATE
));
175 template<bool IsDraw
>
176 void QueueWork(SWR_CONTEXT
*pContext
)
178 DRAW_CONTEXT
* pDC
= pContext
->pCurDrawContext
;
179 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
183 pDC
->pTileMgr
= &pContext
->pMacroTileManagerArray
[dcIndex
];
184 pDC
->pTileMgr
->initialize();
187 // Each worker thread looks at a DC for both FE and BE work at different times and so we
188 // multiply threadDone by 2. When the threadDone counter has reached 0 then all workers
189 // have moved past this DC. (i.e. Each worker has checked this DC for both FE and BE work and
190 // then moved on if all work is done.)
191 pContext
->pCurDrawContext
->threadsDone
= pContext
->NumFEThreads
+ pContext
->NumBEThreads
;
195 InterlockedIncrement((volatile LONG
*)&pContext
->drawsOutstandingFE
);
200 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
);
201 pContext
->dcRing
.Enqueue();
204 if (pContext
->threadInfo
.SINGLE_THREADED
)
206 // flush denormals to 0
207 uint32_t mxcsr
= _mm_getcsr();
208 _mm_setcsr(mxcsr
| _MM_FLUSH_ZERO_ON
| _MM_DENORMALS_ZERO_ON
);
212 uint32_t curDraw
[2] = { pContext
->pCurDrawContext
->drawId
, pContext
->pCurDrawContext
->drawId
};
213 WorkOnFifoFE(pContext
, 0, curDraw
[0]);
214 WorkOnFifoBE(pContext
, 0, curDraw
[1], pContext
->singleThreadLockedTiles
, 0, 0);
218 uint32_t curDispatch
= pContext
->pCurDrawContext
->drawId
;
219 WorkOnCompute(pContext
, 0, curDispatch
);
222 // Dequeue the work here, if not already done, since we're single threaded (i.e. no workers).
223 while (CompleteDrawContext(pContext
, pContext
->pCurDrawContext
) > 0) {}
230 AR_API_BEGIN(APIDrawWakeAllThreads
, pDC
->drawId
);
231 WakeAllThreads(pContext
);
232 AR_API_END(APIDrawWakeAllThreads
, 1);
235 // Set current draw context to NULL so that next state call forces a new draw context to be created and populated.
236 pContext
->pPrevDrawContext
= pContext
->pCurDrawContext
;
237 pContext
->pCurDrawContext
= nullptr;
240 INLINE
void QueueDraw(SWR_CONTEXT
* pContext
)
242 QueueWork
<true>(pContext
);
245 INLINE
void QueueDispatch(SWR_CONTEXT
* pContext
)
247 QueueWork
<false>(pContext
);
250 DRAW_CONTEXT
* GetDrawContext(SWR_CONTEXT
*pContext
, bool isSplitDraw
= false)
252 AR_API_BEGIN(APIGetDrawContext
, 0);
253 // If current draw context is null then need to obtain a new draw context to use from ring.
254 if (pContext
->pCurDrawContext
== nullptr)
256 // Need to wait for a free entry.
257 while (pContext
->dcRing
.IsFull())
262 uint64_t curDraw
= pContext
->dcRing
.GetHead();
263 uint32_t dcIndex
= curDraw
% KNOB_MAX_DRAWS_IN_FLIGHT
;
265 if ((pContext
->frameCount
- pContext
->lastFrameChecked
) > 2 ||
266 (curDraw
- pContext
->lastDrawChecked
) > 0x10000)
268 // Take this opportunity to clean-up old arena allocations
269 pContext
->cachingArenaAllocator
.FreeOldBlocks();
271 pContext
->lastFrameChecked
= pContext
->frameCount
;
272 pContext
->lastDrawChecked
= curDraw
;
275 DRAW_CONTEXT
* pCurDrawContext
= &pContext
->dcRing
[dcIndex
];
276 pContext
->pCurDrawContext
= pCurDrawContext
;
278 // Assign next available entry in DS ring to this DC.
279 uint32_t dsIndex
= pContext
->curStateId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
280 pCurDrawContext
->pState
= &pContext
->dsRing
[dsIndex
];
282 // Copy previous state to current state.
283 if (pContext
->pPrevDrawContext
)
285 DRAW_CONTEXT
* pPrevDrawContext
= pContext
->pPrevDrawContext
;
287 // If we're splitting our draw then we can just use the same state from the previous
288 // draw. In this case, we won't increment the DS ring index so the next non-split
289 // draw can receive the state.
290 if (isSplitDraw
== false)
292 CopyState(*pCurDrawContext
->pState
, *pPrevDrawContext
->pState
);
294 // Should have been cleaned up previously
295 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
297 pCurDrawContext
->pState
->pPrivateState
= nullptr;
299 pContext
->curStateId
++; // Progress state ring index forward.
303 // If its a split draw then just copy the state pointer over
304 // since its the same draw.
305 pCurDrawContext
->pState
= pPrevDrawContext
->pState
;
306 SWR_ASSERT(pPrevDrawContext
->cleanupState
== false);
311 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
312 pContext
->curStateId
++; // Progress state ring index forward.
315 SWR_ASSERT(pCurDrawContext
->pArena
->IsEmpty() == true);
318 pCurDrawContext
->dependent
= false;
319 pCurDrawContext
->dependentFE
= false;
321 pCurDrawContext
->pContext
= pContext
;
322 pCurDrawContext
->isCompute
= false; // Dispatch has to set this to true.
324 pCurDrawContext
->doneFE
= false;
325 pCurDrawContext
->FeLock
= 0;
326 pCurDrawContext
->threadsDone
= 0;
327 pCurDrawContext
->retireCallback
.pfnCallbackFunc
= nullptr;
329 pCurDrawContext
->dynState
.Reset(pContext
->NumWorkerThreads
);
331 // Assign unique drawId for this DC
332 pCurDrawContext
->drawId
= pContext
->dcRing
.GetHead();
334 pCurDrawContext
->cleanupState
= true;
339 SWR_ASSERT(isSplitDraw
== false, "Split draw should only be used when obtaining a new DC");
342 AR_API_END(APIGetDrawContext
, 0);
343 return pContext
->pCurDrawContext
;
346 API_STATE
* GetDrawState(SWR_CONTEXT
*pContext
)
348 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
349 SWR_ASSERT(pDC
->pState
!= nullptr);
351 return &pDC
->pState
->state
;
354 void SwrDestroyContext(HANDLE hContext
)
356 SWR_CONTEXT
*pContext
= GetContext(hContext
);
357 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
359 pDC
->FeWork
.type
= SHUTDOWN
;
360 pDC
->FeWork
.pfnWork
= ProcessShutdown
;
365 DestroyThreadPool(pContext
, &pContext
->threadPool
);
368 for (uint32_t i
= 0; i
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++i
)
370 delete[] pContext
->dcRing
[i
].dynState
.pStats
;
371 delete pContext
->dcRing
[i
].pArena
;
372 delete pContext
->dsRing
[i
].pArena
;
373 pContext
->pMacroTileManagerArray
[i
].~MacroTileMgr();
374 pContext
->pDispatchQueueArray
[i
].~DispatchQueue();
377 AlignedFree(pContext
->pDispatchQueueArray
);
378 AlignedFree(pContext
->pMacroTileManagerArray
);
380 // Free scratch space.
381 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
384 VirtualFree(pContext
->ppScratch
[i
], 0, MEM_RELEASE
);
386 AlignedFree(pContext
->ppScratch
[i
]);
389 #if defined(KNOB_ENABLE_AR)
390 ArchRast::DestroyThreadContext(pContext
->pArContext
[i
]);
394 delete[] pContext
->ppScratch
;
395 delete[] pContext
->pStats
;
397 delete(pContext
->pHotTileMgr
);
399 pContext
->~SWR_CONTEXT();
400 AlignedFree(GetContext(hContext
));
403 void SWR_API
SwrSaveState(
405 void* pOutputStateBlock
,
408 SWR_CONTEXT
*pContext
= GetContext(hContext
);
409 auto pSrc
= GetDrawState(pContext
);
410 SWR_ASSERT(pOutputStateBlock
&& memSize
>= sizeof(*pSrc
));
412 memcpy(pOutputStateBlock
, pSrc
, sizeof(*pSrc
));
415 void SWR_API
SwrRestoreState(
417 const void* pStateBlock
,
420 SWR_CONTEXT
*pContext
= GetContext(hContext
);
421 auto pDst
= GetDrawState(pContext
);
422 SWR_ASSERT(pStateBlock
&& memSize
>= sizeof(*pDst
));
424 memcpy(pDst
, pStateBlock
, sizeof(*pDst
));
427 void SetupDefaultState(SWR_CONTEXT
*pContext
)
429 API_STATE
* pState
= GetDrawState(pContext
);
431 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
432 pState
->rastState
.frontWinding
= SWR_FRONTWINDING_CCW
;
434 pState
->depthBoundsState
.depthBoundsTestEnable
= false;
435 pState
->depthBoundsState
.depthBoundsTestMinValue
= 0.0f
;
436 pState
->depthBoundsState
.depthBoundsTestMaxValue
= 1.0f
;
439 void SwrSync(HANDLE hContext
, PFN_CALLBACK_FUNC pfnFunc
, uint64_t userData
, uint64_t userData2
, uint64_t userData3
)
441 SWR_ASSERT(pfnFunc
!= nullptr);
443 SWR_CONTEXT
*pContext
= GetContext(hContext
);
444 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
446 AR_API_BEGIN(APISync
, 0);
448 pDC
->FeWork
.type
= SYNC
;
449 pDC
->FeWork
.pfnWork
= ProcessSync
;
451 // Setup callback function
452 pDC
->retireCallback
.pfnCallbackFunc
= pfnFunc
;
453 pDC
->retireCallback
.userData
= userData
;
454 pDC
->retireCallback
.userData2
= userData2
;
455 pDC
->retireCallback
.userData3
= userData3
;
457 AR_API_EVENT(SwrSyncEvent(pDC
->drawId
));
462 AR_API_END(APISync
, 1);
465 void SwrWaitForIdle(HANDLE hContext
)
467 SWR_CONTEXT
*pContext
= GetContext(hContext
);
469 AR_API_BEGIN(APIWaitForIdle
, 0);
471 while (!pContext
->dcRing
.IsEmpty())
476 AR_API_END(APIWaitForIdle
, 1);
479 void SwrWaitForIdleFE(HANDLE hContext
)
481 SWR_CONTEXT
*pContext
= GetContext(hContext
);
483 AR_API_BEGIN(APIWaitForIdle
, 0);
485 while (pContext
->drawsOutstandingFE
> 0)
490 AR_API_END(APIWaitForIdle
, 1);
493 void SwrSetVertexBuffers(
496 const SWR_VERTEX_BUFFER_STATE
* pVertexBuffers
)
498 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
500 for (uint32_t i
= 0; i
< numBuffers
; ++i
)
502 const SWR_VERTEX_BUFFER_STATE
*pVB
= &pVertexBuffers
[i
];
503 pState
->vertexBuffers
[pVB
->index
] = *pVB
;
507 void SwrSetIndexBuffer(
509 const SWR_INDEX_BUFFER_STATE
* pIndexBuffer
)
511 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
513 pState
->indexBuffer
= *pIndexBuffer
;
516 void SwrSetFetchFunc(
518 PFN_FETCH_FUNC pfnFetchFunc
)
520 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
522 pState
->pfnFetchFunc
= pfnFetchFunc
;
527 PFN_SO_FUNC pfnSoFunc
,
528 uint32_t streamIndex
)
530 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
532 SWR_ASSERT(streamIndex
< MAX_SO_STREAMS
);
534 pState
->pfnSoFunc
[streamIndex
] = pfnSoFunc
;
539 SWR_STREAMOUT_STATE
* pSoState
)
541 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
543 pState
->soState
= *pSoState
;
546 void SwrSetSoBuffers(
548 SWR_STREAMOUT_BUFFER
* pSoBuffer
,
551 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
553 SWR_ASSERT((slot
< 4), "There are only 4 SO buffer slots [0, 3]\nSlot requested: %d", slot
);
555 pState
->soBuffer
[slot
] = *pSoBuffer
;
558 void SwrSetVertexFunc(
560 PFN_VERTEX_FUNC pfnVertexFunc
)
562 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
564 pState
->pfnVertexFunc
= pfnVertexFunc
;
567 void SwrSetFrontendState(
569 SWR_FRONTEND_STATE
*pFEState
)
571 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
572 pState
->frontendState
= *pFEState
;
577 SWR_GS_STATE
*pGSState
)
579 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
580 pState
->gsState
= *pGSState
;
585 PFN_GS_FUNC pfnGsFunc
)
587 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
588 pState
->pfnGsFunc
= pfnGsFunc
;
593 PFN_CS_FUNC pfnCsFunc
,
594 uint32_t totalThreadsInGroup
,
595 uint32_t totalSpillFillSize
,
596 uint32_t scratchSpaceSizePerInstance
,
597 uint32_t numInstances
)
599 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
600 pState
->pfnCsFunc
= pfnCsFunc
;
601 pState
->totalThreadsInGroup
= totalThreadsInGroup
;
602 pState
->totalSpillFillSize
= totalSpillFillSize
;
603 pState
->scratchSpaceSize
= scratchSpaceSizePerInstance
;
604 pState
->scratchSpaceNumInstances
= numInstances
;
609 SWR_TS_STATE
*pState
)
611 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
612 pApiState
->tsState
= *pState
;
619 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
620 pApiState
->pfnHsFunc
= pfnFunc
;
627 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
628 pApiState
->pfnDsFunc
= pfnFunc
;
631 void SwrSetDepthStencilState(
633 SWR_DEPTH_STENCIL_STATE
*pDSState
)
635 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
637 pState
->depthStencilState
= *pDSState
;
640 void SwrSetBackendState(
642 SWR_BACKEND_STATE
*pBEState
)
644 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
646 pState
->backendState
= *pBEState
;
649 void SwrSetDepthBoundsState(
651 SWR_DEPTH_BOUNDS_STATE
*pDBState
)
653 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
655 pState
->depthBoundsState
= *pDBState
;
658 void SwrSetPixelShaderState(
660 SWR_PS_STATE
*pPSState
)
662 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
663 pState
->psState
= *pPSState
;
666 void SwrSetBlendState(
668 SWR_BLEND_STATE
*pBlendState
)
670 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
671 memcpy(&pState
->blendState
, pBlendState
, sizeof(SWR_BLEND_STATE
));
674 void SwrSetBlendFunc(
676 uint32_t renderTarget
,
677 PFN_BLEND_JIT_FUNC pfnBlendFunc
)
679 SWR_ASSERT(renderTarget
< SWR_NUM_RENDERTARGETS
);
680 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
681 pState
->pfnBlendFunc
[renderTarget
] = pfnBlendFunc
;
684 // update guardband multipliers for the viewport
685 void updateGuardbands(API_STATE
*pState
)
687 uint32_t numGbs
= pState
->gsState
.emitsRenderTargetArrayIndex
? KNOB_NUM_VIEWPORTS_SCISSORS
: 1;
689 for(uint32_t i
= 0; i
< numGbs
; ++i
)
691 // guardband center is viewport center
692 pState
->gbState
.left
[i
] = KNOB_GUARDBAND_WIDTH
/ pState
->vp
[i
].width
;
693 pState
->gbState
.right
[i
] = KNOB_GUARDBAND_WIDTH
/ pState
->vp
[i
].width
;
694 pState
->gbState
.top
[i
] = KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[i
].height
;
695 pState
->gbState
.bottom
[i
] = KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[i
].height
;
699 void SwrSetRastState(
701 const SWR_RASTSTATE
*pRastState
)
703 SWR_CONTEXT
*pContext
= GetContext(hContext
);
704 API_STATE
* pState
= GetDrawState(pContext
);
706 memcpy(&pState
->rastState
, pRastState
, sizeof(SWR_RASTSTATE
));
709 void SwrSetViewports(
711 uint32_t numViewports
,
712 const SWR_VIEWPORT
* pViewports
,
713 const SWR_VIEWPORT_MATRICES
* pMatrices
)
715 SWR_ASSERT(numViewports
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
716 "Invalid number of viewports.");
718 SWR_CONTEXT
*pContext
= GetContext(hContext
);
719 API_STATE
* pState
= GetDrawState(pContext
);
721 memcpy(&pState
->vp
[0], pViewports
, sizeof(SWR_VIEWPORT
) * numViewports
);
722 // @todo Faster to copy portions of the SOA or just copy all of it?
723 memcpy(&pState
->vpMatrices
, pMatrices
, sizeof(SWR_VIEWPORT_MATRICES
));
725 updateGuardbands(pState
);
728 void SwrSetScissorRects(
730 uint32_t numScissors
,
731 const SWR_RECT
* pScissors
)
733 SWR_ASSERT(numScissors
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
734 "Invalid number of scissor rects.");
736 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
737 memcpy(&pState
->scissorRects
[0], pScissors
, numScissors
* sizeof(pScissors
[0]));
740 void SetupMacroTileScissors(DRAW_CONTEXT
*pDC
)
742 API_STATE
*pState
= &pDC
->pState
->state
;
743 uint32_t numScissors
= pState
->gsState
.emitsViewportArrayIndex
? KNOB_NUM_VIEWPORTS_SCISSORS
: 1;
744 pState
->scissorsTileAligned
= true;
746 for (uint32_t index
= 0; index
< numScissors
; ++index
)
748 SWR_RECT
&scissorInFixedPoint
= pState
->scissorsInFixedPoint
[index
];
750 // Set up scissor dimensions based on scissor or viewport
751 if (pState
->rastState
.scissorEnable
)
753 scissorInFixedPoint
= pState
->scissorRects
[index
];
757 // the vp width and height must be added to origin un-rounded then the result round to -inf.
758 // The cast to int works for rounding assuming all [left, right, top, bottom] are positive.
759 scissorInFixedPoint
.xmin
= (int32_t)pState
->vp
[index
].x
;
760 scissorInFixedPoint
.xmax
= (int32_t)(pState
->vp
[index
].x
+ pState
->vp
[index
].width
);
761 scissorInFixedPoint
.ymin
= (int32_t)pState
->vp
[index
].y
;
762 scissorInFixedPoint
.ymax
= (int32_t)(pState
->vp
[index
].y
+ pState
->vp
[index
].height
);
766 scissorInFixedPoint
&= g_MaxScissorRect
;
768 // Test for tile alignment
770 tileAligned
= (scissorInFixedPoint
.xmin
% KNOB_TILE_X_DIM
) == 0;
771 tileAligned
&= (scissorInFixedPoint
.ymin
% KNOB_TILE_Y_DIM
) == 0;
772 tileAligned
&= (scissorInFixedPoint
.xmax
% KNOB_TILE_X_DIM
) == 0;
773 tileAligned
&= (scissorInFixedPoint
.ymax
% KNOB_TILE_Y_DIM
) == 0;
775 pState
->scissorsTileAligned
&= tileAligned
;
777 // Scale to fixed point
778 scissorInFixedPoint
.xmin
*= FIXED_POINT_SCALE
;
779 scissorInFixedPoint
.xmax
*= FIXED_POINT_SCALE
;
780 scissorInFixedPoint
.ymin
*= FIXED_POINT_SCALE
;
781 scissorInFixedPoint
.ymax
*= FIXED_POINT_SCALE
;
783 // Make scissor inclusive
784 scissorInFixedPoint
.xmax
-= 1;
785 scissorInFixedPoint
.ymax
-= 1;
789 // templated backend function tables
790 extern PFN_BACKEND_FUNC gBackendNullPs
[SWR_MULTISAMPLE_TYPE_COUNT
];
791 extern PFN_BACKEND_FUNC gBackendSingleSample
[SWR_INPUT_COVERAGE_COUNT
][2][2];
792 extern PFN_BACKEND_FUNC gBackendSampleRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2];
793 void SetupPipeline(DRAW_CONTEXT
*pDC
)
795 DRAW_STATE
* pState
= pDC
->pState
;
796 const SWR_RASTSTATE
&rastState
= pState
->state
.rastState
;
797 const SWR_PS_STATE
&psState
= pState
->state
.psState
;
798 BACKEND_FUNCS
& backendFuncs
= pState
->backendFuncs
;
801 if (psState
.pfnPixelShader
== nullptr)
803 backendFuncs
.pfnBackend
= gBackendNullPs
[pState
->state
.rastState
.sampleCount
];
807 const uint32_t forcedSampleCount
= (rastState
.forcedSampleCount
) ? 1 : 0;
808 const bool bMultisampleEnable
= ((rastState
.sampleCount
> SWR_MULTISAMPLE_1X
) || forcedSampleCount
) ? 1 : 0;
809 const uint32_t centroid
= ((psState
.barycentricsMask
& SWR_BARYCENTRIC_CENTROID_MASK
) > 0) ? 1 : 0;
810 const uint32_t canEarlyZ
= (psState
.forceEarlyZ
|| (!psState
.writesODepth
&& !psState
.usesSourceDepth
&& !psState
.usesUAV
)) ? 1 : 0;
811 SWR_BARYCENTRICS_MASK barycentricsMask
= (SWR_BARYCENTRICS_MASK
)psState
.barycentricsMask
;
813 // select backend function
814 switch(psState
.shadingRate
)
816 case SWR_SHADING_RATE_PIXEL
:
817 if(bMultisampleEnable
)
819 // always need to generate I & J per sample for Z interpolation
820 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
821 backendFuncs
.pfnBackend
= gBackendPixelRateTable
[rastState
.sampleCount
][rastState
.bIsCenterPattern
][psState
.inputCoverage
]
822 [centroid
][forcedSampleCount
][canEarlyZ
]
827 // always need to generate I & J per pixel for Z interpolation
828 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_PIXEL_MASK
);
829 backendFuncs
.pfnBackend
= gBackendSingleSample
[psState
.inputCoverage
][centroid
][canEarlyZ
];
832 case SWR_SHADING_RATE_SAMPLE
:
833 SWR_ASSERT(rastState
.bIsCenterPattern
!= true);
834 // always need to generate I & J per sample for Z interpolation
835 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
836 backendFuncs
.pfnBackend
= gBackendSampleRateTable
[rastState
.sampleCount
][psState
.inputCoverage
][centroid
][canEarlyZ
];
839 SWR_ASSERT(0 && "Invalid shading rate");
844 PFN_PROCESS_PRIMS pfnBinner
;
845 #if USE_SIMD16_FRONTEND
846 PFN_PROCESS_PRIMS_SIMD16 pfnBinner_simd16
;
848 switch (pState
->state
.topology
)
851 pState
->pfnProcessPrims
= ClipPoints
;
852 pfnBinner
= BinPoints
;
853 #if USE_SIMD16_FRONTEND
854 pState
->pfnProcessPrims_simd16
= ClipPoints_simd16
;
855 pfnBinner_simd16
= BinPoints_simd16
;
861 case TOP_LINE_LIST_ADJ
:
862 case TOP_LISTSTRIP_ADJ
:
863 pState
->pfnProcessPrims
= ClipLines
;
864 pfnBinner
= BinLines
;
865 #if USE_SIMD16_FRONTEND
866 pState
->pfnProcessPrims_simd16
= ClipLines_simd16
;
867 pfnBinner_simd16
= BinLines_simd16
;
871 pState
->pfnProcessPrims
= ClipTriangles
;
872 pfnBinner
= GetBinTrianglesFunc((rastState
.conservativeRast
> 0));
873 #if USE_SIMD16_FRONTEND
874 pState
->pfnProcessPrims_simd16
= ClipTriangles_simd16
;
875 pfnBinner_simd16
= GetBinTrianglesFunc_simd16((rastState
.conservativeRast
> 0));
881 // disable clipper if viewport transform is disabled
882 if (pState
->state
.frontendState
.vpTransformDisable
)
884 pState
->pfnProcessPrims
= pfnBinner
;
885 #if USE_SIMD16_FRONTEND
886 pState
->pfnProcessPrims_simd16
= pfnBinner_simd16
;
890 if ((pState
->state
.psState
.pfnPixelShader
== nullptr) &&
891 (pState
->state
.depthStencilState
.depthTestEnable
== FALSE
) &&
892 (pState
->state
.depthStencilState
.depthWriteEnable
== FALSE
) &&
893 (pState
->state
.depthStencilState
.stencilTestEnable
== FALSE
) &&
894 (pState
->state
.depthStencilState
.stencilWriteEnable
== FALSE
) &&
895 (pState
->state
.backendState
.numAttributes
== 0))
897 pState
->pfnProcessPrims
= nullptr;
898 #if USE_SIMD16_FRONTEND
899 pState
->pfnProcessPrims_simd16
= nullptr;
903 if (pState
->state
.soState
.rasterizerDisable
== true)
905 pState
->pfnProcessPrims
= nullptr;
906 #if USE_SIMD16_FRONTEND
907 pState
->pfnProcessPrims_simd16
= nullptr;
912 // set up the frontend attribute count
913 pState
->state
.feNumAttributes
= 0;
914 const SWR_BACKEND_STATE
& backendState
= pState
->state
.backendState
;
915 if (backendState
.swizzleEnable
)
917 // attribute swizzling is enabled, iterate over the map and record the max attribute used
918 for (uint32_t i
= 0; i
< backendState
.numAttributes
; ++i
)
920 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)backendState
.swizzleMap
[i
].sourceAttrib
+ 1);
925 pState
->state
.feNumAttributes
= pState
->state
.backendState
.numAttributes
;
928 if (pState
->state
.soState
.soEnable
)
930 uint32_t streamMasks
= 0;
931 for (uint32_t i
= 0; i
< 4; ++i
)
933 streamMasks
|= pState
->state
.soState
.streamMasks
[i
];
937 if (_BitScanReverse(&maxAttrib
, streamMasks
))
939 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)(maxAttrib
+ 1));
943 // complicated logic to test for cases where we don't need backing hottile memory for a draw
944 // have to check for the special case where depth/stencil test is enabled but depthwrite is disabled.
945 pState
->state
.depthHottileEnable
= ((!(pState
->state
.depthStencilState
.depthTestEnable
&&
946 !pState
->state
.depthStencilState
.depthWriteEnable
&&
947 !pState
->state
.depthBoundsState
.depthBoundsTestEnable
&&
948 pState
->state
.depthStencilState
.depthTestFunc
== ZFUNC_ALWAYS
)) &&
949 (pState
->state
.depthStencilState
.depthTestEnable
||
950 pState
->state
.depthStencilState
.depthWriteEnable
||
951 pState
->state
.depthBoundsState
.depthBoundsTestEnable
)) ? true : false;
953 pState
->state
.stencilHottileEnable
= (((!(pState
->state
.depthStencilState
.stencilTestEnable
&&
954 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
955 pState
->state
.depthStencilState
.stencilTestFunc
== ZFUNC_ALWAYS
)) ||
956 // for stencil we have to check the double sided state as well
957 (!(pState
->state
.depthStencilState
.doubleSidedStencilTestEnable
&&
958 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
959 pState
->state
.depthStencilState
.backfaceStencilTestFunc
== ZFUNC_ALWAYS
))) &&
960 (pState
->state
.depthStencilState
.stencilTestEnable
||
961 pState
->state
.depthStencilState
.stencilWriteEnable
)) ? true : false;
963 uint32_t numRTs
= pState
->state
.psState
.numRenderTargets
;
964 pState
->state
.colorHottileEnable
= 0;
965 if (psState
.pfnPixelShader
!= nullptr)
967 for (uint32_t rt
= 0; rt
< numRTs
; ++rt
)
969 pState
->state
.colorHottileEnable
|=
970 (!pState
->state
.blendState
.renderTarget
[rt
].writeDisableAlpha
||
971 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableRed
||
972 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableGreen
||
973 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableBlue
) ? (1 << rt
) : 0;
977 // Setup depth quantization function
978 if (pState
->state
.depthHottileEnable
)
980 switch (pState
->state
.rastState
.depthFormat
)
982 case R32_FLOAT_X8X24_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT_X8X24_TYPELESS
> ; break;
983 case R32_FLOAT
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ; break;
984 case R24_UNORM_X8_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R24_UNORM_X8_TYPELESS
> ; break;
985 case R16_UNORM
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R16_UNORM
> ; break;
986 default: SWR_INVALID("Unsupported depth format for depth quantiztion.");
987 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
992 // set up pass-through quantize if depth isn't enabled
993 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
997 //////////////////////////////////////////////////////////////////////////
999 /// @param pDC - Draw context to initialize for this draw.
1004 // We don't need to re-setup the scissors/pipeline state again for split draw.
1005 if (isSplitDraw
== false)
1007 SetupMacroTileScissors(pDC
);
1014 //////////////////////////////////////////////////////////////////////////
1015 /// @brief We can split the draw for certain topologies for better performance.
1016 /// @param totalVerts - Total vertices for draw
1017 /// @param topology - Topology used for draw
1018 uint32_t MaxVertsPerDraw(
1020 uint32_t totalVerts
,
1021 PRIMITIVE_TOPOLOGY topology
)
1023 API_STATE
& state
= pDC
->pState
->state
;
1025 uint32_t vertsPerDraw
= totalVerts
;
1027 if (state
.soState
.soEnable
)
1034 case TOP_POINT_LIST
:
1035 case TOP_TRIANGLE_LIST
:
1036 vertsPerDraw
= KNOB_MAX_PRIMS_PER_DRAW
;
1039 case TOP_PATCHLIST_1
:
1040 case TOP_PATCHLIST_2
:
1041 case TOP_PATCHLIST_3
:
1042 case TOP_PATCHLIST_4
:
1043 case TOP_PATCHLIST_5
:
1044 case TOP_PATCHLIST_6
:
1045 case TOP_PATCHLIST_7
:
1046 case TOP_PATCHLIST_8
:
1047 case TOP_PATCHLIST_9
:
1048 case TOP_PATCHLIST_10
:
1049 case TOP_PATCHLIST_11
:
1050 case TOP_PATCHLIST_12
:
1051 case TOP_PATCHLIST_13
:
1052 case TOP_PATCHLIST_14
:
1053 case TOP_PATCHLIST_15
:
1054 case TOP_PATCHLIST_16
:
1055 case TOP_PATCHLIST_17
:
1056 case TOP_PATCHLIST_18
:
1057 case TOP_PATCHLIST_19
:
1058 case TOP_PATCHLIST_20
:
1059 case TOP_PATCHLIST_21
:
1060 case TOP_PATCHLIST_22
:
1061 case TOP_PATCHLIST_23
:
1062 case TOP_PATCHLIST_24
:
1063 case TOP_PATCHLIST_25
:
1064 case TOP_PATCHLIST_26
:
1065 case TOP_PATCHLIST_27
:
1066 case TOP_PATCHLIST_28
:
1067 case TOP_PATCHLIST_29
:
1068 case TOP_PATCHLIST_30
:
1069 case TOP_PATCHLIST_31
:
1070 case TOP_PATCHLIST_32
:
1071 if (pDC
->pState
->state
.tsState
.tsEnable
)
1073 uint32_t vertsPerPrim
= topology
- TOP_PATCHLIST_BASE
;
1074 vertsPerDraw
= vertsPerPrim
* KNOB_MAX_TESS_PRIMS_PER_DRAW
;
1078 // The Primitive Assembly code can only handle 1 RECT at a time.
1084 // We are not splitting up draws for other topologies.
1088 return vertsPerDraw
;
1092 //////////////////////////////////////////////////////////////////////////
1093 /// @brief DrawInstanced
1094 /// @param hContext - Handle passed back from SwrCreateContext
1095 /// @param topology - Specifies topology for draw.
1096 /// @param numVerts - How many vertices to read sequentially from vertex data (per instance).
1097 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1098 /// @param numInstances - How many instances to render.
1099 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1102 PRIMITIVE_TOPOLOGY topology
,
1103 uint32_t numVertices
,
1104 uint32_t startVertex
,
1105 uint32_t numInstances
= 1,
1106 uint32_t startInstance
= 0)
1113 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1114 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1116 AR_API_BEGIN(APIDraw
, pDC
->drawId
);
1117 AR_API_EVENT(DrawInstancedEvent(pDC
->drawId
, topology
, numVertices
, startVertex
, numInstances
, startInstance
));
1119 uint32_t maxVertsPerDraw
= MaxVertsPerDraw(pDC
, numVertices
, topology
);
1120 uint32_t primsPerDraw
= GetNumPrims(topology
, maxVertsPerDraw
);
1121 uint32_t remainingVerts
= numVertices
;
1123 API_STATE
*pState
= &pDC
->pState
->state
;
1124 pState
->topology
= topology
;
1125 pState
->forceFront
= false;
1127 // disable culling for points/lines
1128 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1129 if (topology
== TOP_POINT_LIST
)
1131 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1132 pState
->forceFront
= true;
1134 else if (topology
== TOP_RECT_LIST
)
1136 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1141 while (remainingVerts
)
1143 uint32_t numVertsForDraw
= (remainingVerts
< maxVertsPerDraw
) ?
1144 remainingVerts
: maxVertsPerDraw
;
1146 bool isSplitDraw
= (draw
> 0) ? true : false;
1147 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
, isSplitDraw
);
1148 InitDraw(pDC
, isSplitDraw
);
1150 pDC
->FeWork
.type
= DRAW
;
1151 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1153 false, // bEnableCutIndex
1154 pState
->tsState
.tsEnable
,
1155 pState
->gsState
.gsEnable
,
1156 pState
->soState
.soEnable
,
1157 pDC
->pState
->pfnProcessPrims
!= nullptr);
1158 pDC
->FeWork
.desc
.draw
.numVerts
= numVertsForDraw
;
1159 pDC
->FeWork
.desc
.draw
.startVertex
= startVertex
;
1160 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1161 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1162 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1163 pDC
->FeWork
.desc
.draw
.startVertexID
= draw
* maxVertsPerDraw
;
1165 pDC
->cleanupState
= (remainingVerts
== numVertsForDraw
);
1168 QueueDraw(pContext
);
1170 AR_API_EVENT(DrawInstancedSplitEvent(pDC
->drawId
));
1172 remainingVerts
-= numVertsForDraw
;
1176 // restore culling state
1177 pDC
= GetDrawContext(pContext
);
1178 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1181 AR_API_END(APIDraw
, numVertices
* numInstances
);
1184 //////////////////////////////////////////////////////////////////////////
1186 /// @param hContext - Handle passed back from SwrCreateContext
1187 /// @param topology - Specifies topology for draw.
1188 /// @param startVertex - Specifies start vertex in vertex buffer for draw.
1189 /// @param primCount - Number of vertices.
1192 PRIMITIVE_TOPOLOGY topology
,
1193 uint32_t startVertex
,
1194 uint32_t numVertices
)
1196 DrawInstanced(hContext
, topology
, numVertices
, startVertex
);
1199 //////////////////////////////////////////////////////////////////////////
1200 /// @brief SwrDrawInstanced
1201 /// @param hContext - Handle passed back from SwrCreateContext
1202 /// @param topology - Specifies topology for draw.
1203 /// @param numVertsPerInstance - How many vertices to read sequentially from vertex data.
1204 /// @param numInstances - How many instances to render.
1205 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1206 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1207 void SwrDrawInstanced(
1209 PRIMITIVE_TOPOLOGY topology
,
1210 uint32_t numVertsPerInstance
,
1211 uint32_t numInstances
,
1212 uint32_t startVertex
,
1213 uint32_t startInstance
1216 DrawInstanced(hContext
, topology
, numVertsPerInstance
, startVertex
, numInstances
, startInstance
);
1219 //////////////////////////////////////////////////////////////////////////
1220 /// @brief DrawIndexedInstanced
1221 /// @param hContext - Handle passed back from SwrCreateContext
1222 /// @param topology - Specifies topology for draw.
1223 /// @param numIndices - Number of indices to read sequentially from index buffer.
1224 /// @param indexOffset - Starting index into index buffer.
1225 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1226 /// @param numInstances - Number of instances to render.
1227 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1228 void DrawIndexedInstance(
1230 PRIMITIVE_TOPOLOGY topology
,
1231 uint32_t numIndices
,
1232 uint32_t indexOffset
,
1234 uint32_t numInstances
= 1,
1235 uint32_t startInstance
= 0)
1242 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1243 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1244 API_STATE
* pState
= &pDC
->pState
->state
;
1246 AR_API_BEGIN(APIDrawIndexed
, pDC
->drawId
);
1247 AR_API_EVENT(DrawIndexedInstancedEvent(pDC
->drawId
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
));
1249 uint32_t maxIndicesPerDraw
= MaxVertsPerDraw(pDC
, numIndices
, topology
);
1250 uint32_t primsPerDraw
= GetNumPrims(topology
, maxIndicesPerDraw
);
1251 uint32_t remainingIndices
= numIndices
;
1253 uint32_t indexSize
= 0;
1254 switch (pState
->indexBuffer
.format
)
1256 case R32_UINT
: indexSize
= sizeof(uint32_t); break;
1257 case R16_UINT
: indexSize
= sizeof(uint16_t); break;
1258 case R8_UINT
: indexSize
= sizeof(uint8_t); break;
1260 SWR_INVALID("Invalid index buffer format: %d", pState
->indexBuffer
.format
);
1264 uint8_t *pIB
= (uint8_t*)pState
->indexBuffer
.pIndices
;
1265 pIB
+= (uint64_t)indexOffset
* (uint64_t)indexSize
;
1267 pState
->topology
= topology
;
1268 pState
->forceFront
= false;
1270 // disable culling for points/lines
1271 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1272 if (topology
== TOP_POINT_LIST
)
1274 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1275 pState
->forceFront
= true;
1277 else if (topology
== TOP_RECT_LIST
)
1279 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1283 while (remainingIndices
)
1285 uint32_t numIndicesForDraw
= (remainingIndices
< maxIndicesPerDraw
) ?
1286 remainingIndices
: maxIndicesPerDraw
;
1288 // When breaking up draw, we need to obtain new draw context for each iteration.
1289 bool isSplitDraw
= (draw
> 0) ? true : false;
1291 pDC
= GetDrawContext(pContext
, isSplitDraw
);
1292 InitDraw(pDC
, isSplitDraw
);
1294 pDC
->FeWork
.type
= DRAW
;
1295 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1297 pState
->frontendState
.bEnableCutIndex
,
1298 pState
->tsState
.tsEnable
,
1299 pState
->gsState
.gsEnable
,
1300 pState
->soState
.soEnable
,
1301 pDC
->pState
->pfnProcessPrims
!= nullptr);
1302 pDC
->FeWork
.desc
.draw
.pDC
= pDC
;
1303 pDC
->FeWork
.desc
.draw
.numIndices
= numIndicesForDraw
;
1304 pDC
->FeWork
.desc
.draw
.pIB
= (int*)pIB
;
1305 pDC
->FeWork
.desc
.draw
.type
= pDC
->pState
->state
.indexBuffer
.format
;
1307 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1308 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1309 pDC
->FeWork
.desc
.draw
.baseVertex
= baseVertex
;
1310 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1312 pDC
->cleanupState
= (remainingIndices
== numIndicesForDraw
);
1315 QueueDraw(pContext
);
1317 AR_API_EVENT(DrawIndexedInstancedSplitEvent(pDC
->drawId
));
1319 pIB
+= maxIndicesPerDraw
* indexSize
;
1320 remainingIndices
-= numIndicesForDraw
;
1324 // Restore culling state
1325 pDC
= GetDrawContext(pContext
);
1326 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1329 AR_API_END(APIDrawIndexed
, numIndices
* numInstances
);
1333 //////////////////////////////////////////////////////////////////////////
1334 /// @brief DrawIndexed
1335 /// @param hContext - Handle passed back from SwrCreateContext
1336 /// @param topology - Specifies topology for draw.
1337 /// @param numIndices - Number of indices to read sequentially from index buffer.
1338 /// @param indexOffset - Starting index into index buffer.
1339 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1340 void SwrDrawIndexed(
1342 PRIMITIVE_TOPOLOGY topology
,
1343 uint32_t numIndices
,
1344 uint32_t indexOffset
,
1348 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
);
1351 //////////////////////////////////////////////////////////////////////////
1352 /// @brief SwrDrawIndexedInstanced
1353 /// @param hContext - Handle passed back from SwrCreateContext
1354 /// @param topology - Specifies topology for draw.
1355 /// @param numIndices - Number of indices to read sequentially from index buffer.
1356 /// @param numInstances - Number of instances to render.
1357 /// @param indexOffset - Starting index into index buffer.
1358 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1359 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1360 void SwrDrawIndexedInstanced(
1362 PRIMITIVE_TOPOLOGY topology
,
1363 uint32_t numIndices
,
1364 uint32_t numInstances
,
1365 uint32_t indexOffset
,
1367 uint32_t startInstance
)
1369 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
);
1372 //////////////////////////////////////////////////////////////////////////
1373 /// @brief SwrInvalidateTiles
1374 /// @param hContext - Handle passed back from SwrCreateContext
1375 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to invalidate.
1376 /// @param invalidateRect - The pixel-coordinate rectangle to invalidate. This will be expanded to
1377 /// be hottile size-aligned.
1378 void SWR_API
SwrInvalidateTiles(
1380 uint32_t attachmentMask
,
1381 const SWR_RECT
& invalidateRect
)
1388 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1389 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1391 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1392 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1393 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1394 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= invalidateRect
;
1395 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1396 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_INVALID
;
1397 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= false;
1398 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= false;
1401 QueueDraw(pContext
);
1403 AR_API_EVENT(SwrInvalidateTilesEvent(pDC
->drawId
));
1406 //////////////////////////////////////////////////////////////////////////
1407 /// @brief SwrDiscardRect
1408 /// @param hContext - Handle passed back from SwrCreateContext
1409 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to discard.
1410 /// @param rect - The pixel-coordinate rectangle to discard. Only fully-covered hottiles will be
1412 void SWR_API
SwrDiscardRect(
1414 uint32_t attachmentMask
,
1415 const SWR_RECT
& rect
)
1422 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1423 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1425 // Queue a load to the hottile
1426 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1427 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1428 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1429 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= rect
;
1430 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1431 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_RESOLVED
;
1432 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= true;
1433 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= true;
1436 QueueDraw(pContext
);
1438 AR_API_EVENT(SwrDiscardRectEvent(pDC
->drawId
));
1441 //////////////////////////////////////////////////////////////////////////
1442 /// @brief SwrDispatch
1443 /// @param hContext - Handle passed back from SwrCreateContext
1444 /// @param threadGroupCountX - Number of thread groups dispatched in X direction
1445 /// @param threadGroupCountY - Number of thread groups dispatched in Y direction
1446 /// @param threadGroupCountZ - Number of thread groups dispatched in Z direction
1449 uint32_t threadGroupCountX
,
1450 uint32_t threadGroupCountY
,
1451 uint32_t threadGroupCountZ
)
1458 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1459 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1461 AR_API_BEGIN(APIDispatch
, pDC
->drawId
);
1462 AR_API_EVENT(DispatchEvent(pDC
->drawId
, threadGroupCountX
, threadGroupCountY
, threadGroupCountZ
));
1463 pDC
->isCompute
= true; // This is a compute context.
1465 COMPUTE_DESC
* pTaskData
= (COMPUTE_DESC
*)pDC
->pArena
->AllocAligned(sizeof(COMPUTE_DESC
), 64);
1467 pTaskData
->threadGroupCountX
= threadGroupCountX
;
1468 pTaskData
->threadGroupCountY
= threadGroupCountY
;
1469 pTaskData
->threadGroupCountZ
= threadGroupCountZ
;
1471 uint32_t totalThreadGroups
= threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
;
1472 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
1473 pDC
->pDispatch
= &pContext
->pDispatchQueueArray
[dcIndex
];
1474 pDC
->pDispatch
->initialize(totalThreadGroups
, pTaskData
, &ProcessComputeBE
);
1476 QueueDispatch(pContext
);
1477 AR_API_END(APIDispatch
, threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
);
1480 // Deswizzles, converts and stores current contents of the hot tiles to surface
1481 // described by pState
1482 void SWR_API
SwrStoreTiles(
1484 uint32_t attachmentMask
,
1485 SWR_TILE_STATE postStoreTileState
,
1486 const SWR_RECT
& storeRect
)
1493 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1494 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1496 AR_API_BEGIN(APIStoreTiles
, pDC
->drawId
);
1498 pDC
->FeWork
.type
= STORETILES
;
1499 pDC
->FeWork
.pfnWork
= ProcessStoreTiles
;
1500 pDC
->FeWork
.desc
.storeTiles
.attachmentMask
= attachmentMask
;
1501 pDC
->FeWork
.desc
.storeTiles
.postStoreTileState
= postStoreTileState
;
1502 pDC
->FeWork
.desc
.storeTiles
.rect
= storeRect
;
1503 pDC
->FeWork
.desc
.storeTiles
.rect
&= g_MaxScissorRect
;
1506 QueueDraw(pContext
);
1508 AR_API_EVENT(SwrStoreTilesEvent(pDC
->drawId
));
1510 AR_API_END(APIStoreTiles
, 1);
1513 //////////////////////////////////////////////////////////////////////////
1514 /// @brief SwrClearRenderTarget - Clear attached render targets / depth / stencil
1515 /// @param hContext - Handle passed back from SwrCreateContext
1516 /// @param attachmentMask - combination of SWR_ATTACHMENT_*_BIT attachments to clear
1517 /// @param renderTargetArrayIndex - the RT array index to clear
1518 /// @param clearColor - color use for clearing render targets
1519 /// @param z - depth value use for clearing depth buffer
1520 /// @param stencil - stencil value used for clearing stencil buffer
1521 /// @param clearRect - The pixel-coordinate rectangle to clear in all cleared buffers
1522 void SWR_API
SwrClearRenderTarget(
1524 uint32_t attachmentMask
,
1525 uint32_t renderTargetArrayIndex
,
1526 const float clearColor
[4],
1529 const SWR_RECT
& clearRect
)
1536 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1537 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1539 AR_API_BEGIN(APIClearRenderTarget
, pDC
->drawId
);
1541 pDC
->FeWork
.type
= CLEAR
;
1542 pDC
->FeWork
.pfnWork
= ProcessClear
;
1543 pDC
->FeWork
.desc
.clear
.rect
= clearRect
;
1544 pDC
->FeWork
.desc
.clear
.rect
&= g_MaxScissorRect
;
1545 pDC
->FeWork
.desc
.clear
.attachmentMask
= attachmentMask
;
1546 pDC
->FeWork
.desc
.clear
.renderTargetArrayIndex
= renderTargetArrayIndex
;
1547 pDC
->FeWork
.desc
.clear
.clearDepth
= z
;
1548 pDC
->FeWork
.desc
.clear
.clearRTColor
[0] = clearColor
[0];
1549 pDC
->FeWork
.desc
.clear
.clearRTColor
[1] = clearColor
[1];
1550 pDC
->FeWork
.desc
.clear
.clearRTColor
[2] = clearColor
[2];
1551 pDC
->FeWork
.desc
.clear
.clearRTColor
[3] = clearColor
[3];
1552 pDC
->FeWork
.desc
.clear
.clearStencil
= stencil
;
1555 QueueDraw(pContext
);
1557 AR_API_END(APIClearRenderTarget
, 1);
1560 //////////////////////////////////////////////////////////////////////////
1561 /// @brief Returns a pointer to the private context state for the current
1562 /// draw operation. This is used for external componets such as the
1564 /// SWR is responsible for the allocation of the private context state.
1565 /// @param hContext - Handle passed back from SwrCreateContext
1566 VOID
* SwrGetPrivateContextState(
1569 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1570 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1571 DRAW_STATE
* pState
= pDC
->pState
;
1573 if (pState
->pPrivateState
== nullptr)
1575 pState
->pPrivateState
= pState
->pArena
->AllocAligned(pContext
->privateStateSize
, KNOB_SIMD_WIDTH
*sizeof(float));
1578 return pState
->pPrivateState
;
1581 //////////////////////////////////////////////////////////////////////////
1582 /// @brief Clients can use this to allocate memory for draw/dispatch
1583 /// operations. The memory will automatically be freed once operation
1584 /// has completed. Client can use this to allocate binding tables,
1585 /// etc. needed for shader execution.
1586 /// @param hContext - Handle passed back from SwrCreateContext
1587 /// @param size - Size of allocation
1588 /// @param align - Alignment needed for allocation.
1589 VOID
* SwrAllocDrawContextMemory(
1594 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1595 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1597 return pDC
->pState
->pArena
->AllocAligned(size
, align
);
1600 //////////////////////////////////////////////////////////////////////////
1601 /// @brief Enables stats counting
1602 /// @param hContext - Handle passed back from SwrCreateContext
1603 /// @param enable - If true then counts are incremented.
1604 void SwrEnableStatsFE(
1608 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1609 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1611 pDC
->pState
->state
.enableStatsFE
= enable
;
1614 //////////////////////////////////////////////////////////////////////////
1615 /// @brief Enables stats counting
1616 /// @param hContext - Handle passed back from SwrCreateContext
1617 /// @param enable - If true then counts are incremented.
1618 void SwrEnableStatsBE(
1622 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1623 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1625 pDC
->pState
->state
.enableStatsBE
= enable
;
1628 //////////////////////////////////////////////////////////////////////////
1629 /// @brief Mark end of frame - used for performance profiling
1630 /// @param hContext - Handle passed back from SwrCreateContext
1631 void SWR_API
SwrEndFrame(
1634 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1635 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1636 (void)pDC
; // var used
1639 AR_API_EVENT(FrameEndEvent(pContext
->frameCount
, pDC
->drawId
));
1641 pContext
->frameCount
++;
1645 void SwrGetInterface(SWR_INTERFACE
&out_funcs
)
1647 out_funcs
.pfnSwrCreateContext
= SwrCreateContext
;
1648 out_funcs
.pfnSwrDestroyContext
= SwrDestroyContext
;
1649 out_funcs
.pfnSwrSaveState
= SwrSaveState
;
1650 out_funcs
.pfnSwrRestoreState
= SwrRestoreState
;
1651 out_funcs
.pfnSwrSync
= SwrSync
;
1652 out_funcs
.pfnSwrWaitForIdle
= SwrWaitForIdle
;
1653 out_funcs
.pfnSwrWaitForIdleFE
= SwrWaitForIdleFE
;
1654 out_funcs
.pfnSwrSetVertexBuffers
= SwrSetVertexBuffers
;
1655 out_funcs
.pfnSwrSetIndexBuffer
= SwrSetIndexBuffer
;
1656 out_funcs
.pfnSwrSetFetchFunc
= SwrSetFetchFunc
;
1657 out_funcs
.pfnSwrSetSoFunc
= SwrSetSoFunc
;
1658 out_funcs
.pfnSwrSetSoState
= SwrSetSoState
;
1659 out_funcs
.pfnSwrSetSoBuffers
= SwrSetSoBuffers
;
1660 out_funcs
.pfnSwrSetVertexFunc
= SwrSetVertexFunc
;
1661 out_funcs
.pfnSwrSetFrontendState
= SwrSetFrontendState
;
1662 out_funcs
.pfnSwrSetGsState
= SwrSetGsState
;
1663 out_funcs
.pfnSwrSetGsFunc
= SwrSetGsFunc
;
1664 out_funcs
.pfnSwrSetCsFunc
= SwrSetCsFunc
;
1665 out_funcs
.pfnSwrSetTsState
= SwrSetTsState
;
1666 out_funcs
.pfnSwrSetHsFunc
= SwrSetHsFunc
;
1667 out_funcs
.pfnSwrSetDsFunc
= SwrSetDsFunc
;
1668 out_funcs
.pfnSwrSetDepthStencilState
= SwrSetDepthStencilState
;
1669 out_funcs
.pfnSwrSetBackendState
= SwrSetBackendState
;
1670 out_funcs
.pfnSwrSetDepthBoundsState
= SwrSetDepthBoundsState
;
1671 out_funcs
.pfnSwrSetPixelShaderState
= SwrSetPixelShaderState
;
1672 out_funcs
.pfnSwrSetBlendState
= SwrSetBlendState
;
1673 out_funcs
.pfnSwrSetBlendFunc
= SwrSetBlendFunc
;
1674 out_funcs
.pfnSwrDraw
= SwrDraw
;
1675 out_funcs
.pfnSwrDrawInstanced
= SwrDrawInstanced
;
1676 out_funcs
.pfnSwrDrawIndexed
= SwrDrawIndexed
;
1677 out_funcs
.pfnSwrDrawIndexedInstanced
= SwrDrawIndexedInstanced
;
1678 out_funcs
.pfnSwrInvalidateTiles
= SwrInvalidateTiles
;
1679 out_funcs
.pfnSwrDiscardRect
= SwrDiscardRect
;
1680 out_funcs
.pfnSwrDispatch
= SwrDispatch
;
1681 out_funcs
.pfnSwrStoreTiles
= SwrStoreTiles
;
1682 out_funcs
.pfnSwrClearRenderTarget
= SwrClearRenderTarget
;
1683 out_funcs
.pfnSwrSetRastState
= SwrSetRastState
;
1684 out_funcs
.pfnSwrSetViewports
= SwrSetViewports
;
1685 out_funcs
.pfnSwrSetScissorRects
= SwrSetScissorRects
;
1686 out_funcs
.pfnSwrGetPrivateContextState
= SwrGetPrivateContextState
;
1687 out_funcs
.pfnSwrAllocDrawContextMemory
= SwrAllocDrawContextMemory
;
1688 out_funcs
.pfnSwrEnableStatsFE
= SwrEnableStatsFE
;
1689 out_funcs
.pfnSwrEnableStatsBE
= SwrEnableStatsBE
;
1690 out_funcs
.pfnSwrEndFrame
= SwrEndFrame
;