1 /****************************************************************************
2 * Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * @brief API implementation
27 ******************************************************************************/
35 #include "core/backend.h"
36 #include "core/context.h"
37 #include "core/depthstencil.h"
38 #include "core/frontend.h"
39 #include "core/rasterizer.h"
40 #include "core/rdtsc_core.h"
41 #include "core/threads.h"
42 #include "core/tilemgr.h"
43 #include "core/clip.h"
44 #include "core/utils.h"
46 #include "common/simdintrin.h"
47 #include "common/os.h"
49 void SetupDefaultState(SWR_CONTEXT
*pContext
);
51 static INLINE SWR_CONTEXT
* GetContext(HANDLE hContext
)
53 return (SWR_CONTEXT
*)hContext
;
56 //////////////////////////////////////////////////////////////////////////
57 /// @brief Create SWR Context.
58 /// @param pCreateInfo - pointer to creation info.
59 HANDLE
SwrCreateContext(
60 SWR_CREATECONTEXT_INFO
* pCreateInfo
)
65 void* pContextMem
= AlignedMalloc(sizeof(SWR_CONTEXT
), KNOB_SIMD_WIDTH
* 4);
66 memset(pContextMem
, 0, sizeof(SWR_CONTEXT
));
67 SWR_CONTEXT
*pContext
= new (pContextMem
) SWR_CONTEXT();
69 pContext
->driverType
= pCreateInfo
->driver
;
70 pContext
->privateStateSize
= pCreateInfo
->privateStateSize
;
72 pContext
->dcRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
73 pContext
->dsRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
75 pContext
->pMacroTileManagerArray
= (MacroTileMgr
*)AlignedMalloc(sizeof(MacroTileMgr
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
76 pContext
->pDispatchQueueArray
= (DispatchQueue
*)AlignedMalloc(sizeof(DispatchQueue
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
78 pContext
->threadInfo
.MAX_WORKER_THREADS
= KNOB_MAX_WORKER_THREADS
;
79 pContext
->threadInfo
.MAX_NUMA_NODES
= KNOB_MAX_NUMA_NODES
;
80 pContext
->threadInfo
.MAX_CORES_PER_NUMA_NODE
= KNOB_MAX_CORES_PER_NUMA_NODE
;
81 pContext
->threadInfo
.MAX_THREADS_PER_CORE
= KNOB_MAX_THREADS_PER_CORE
;
82 pContext
->threadInfo
.SINGLE_THREADED
= KNOB_SINGLE_THREADED
;
84 if (pCreateInfo
->pThreadInfo
)
86 pContext
->threadInfo
= *pCreateInfo
->pThreadInfo
;
89 for (uint32_t dc
= 0; dc
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++dc
)
91 pContext
->dcRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
92 new (&pContext
->pMacroTileManagerArray
[dc
]) MacroTileMgr(*pContext
->dcRing
[dc
].pArena
);
93 new (&pContext
->pDispatchQueueArray
[dc
]) DispatchQueue();
95 pContext
->dsRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
98 if (!pContext
->threadInfo
.SINGLE_THREADED
)
100 memset(&pContext
->WaitLock
, 0, sizeof(pContext
->WaitLock
));
101 memset(&pContext
->FifosNotEmpty
, 0, sizeof(pContext
->FifosNotEmpty
));
102 new (&pContext
->WaitLock
) std::mutex();
103 new (&pContext
->FifosNotEmpty
) std::condition_variable();
105 CreateThreadPool(pContext
, &pContext
->threadPool
);
108 // Calling createThreadPool() above can set SINGLE_THREADED
109 if (pContext
->threadInfo
.SINGLE_THREADED
)
111 pContext
->NumWorkerThreads
= 1;
112 pContext
->NumFEThreads
= 1;
113 pContext
->NumBEThreads
= 1;
116 // Allocate scratch space for workers.
117 ///@note We could lazily allocate this but its rather small amount of memory.
118 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
121 uint32_t numaNode
= pContext
->threadPool
.pThreadData
?
122 pContext
->threadPool
.pThreadData
[i
].numaId
: 0;
123 pContext
->pScratch
[i
] = (uint8_t*)VirtualAllocExNuma(
124 GetCurrentProcess(), nullptr, 32 * sizeof(KILOBYTE
),
125 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
,
128 pContext
->pScratch
[i
] = (uint8_t*)AlignedMalloc(32 * sizeof(KILOBYTE
), KNOB_SIMD_WIDTH
* 4);
132 // State setup AFTER context is fully initialized
133 SetupDefaultState(pContext
);
135 // initialize hot tile manager
136 pContext
->pHotTileMgr
= new HotTileMgr();
138 // initialize function pointer tables
139 InitClearTilesTable();
141 // initialize callback functions
142 pContext
->pfnLoadTile
= pCreateInfo
->pfnLoadTile
;
143 pContext
->pfnStoreTile
= pCreateInfo
->pfnStoreTile
;
144 pContext
->pfnClearTile
= pCreateInfo
->pfnClearTile
;
145 pContext
->pfnUpdateSoWriteOffset
= pCreateInfo
->pfnUpdateSoWriteOffset
;
146 pContext
->pfnUpdateStats
= pCreateInfo
->pfnUpdateStats
;
147 pContext
->pfnUpdateStatsFE
= pCreateInfo
->pfnUpdateStatsFE
;
149 // pass pointer to bucket manager back to caller
150 #ifdef KNOB_ENABLE_RDTSC
151 pCreateInfo
->pBucketMgr
= &gBucketMgr
;
154 pCreateInfo
->contextSaveSize
= sizeof(API_STATE
);
156 return (HANDLE
)pContext
;
159 void SwrDestroyContext(HANDLE hContext
)
161 SWR_CONTEXT
*pContext
= GetContext(hContext
);
162 DestroyThreadPool(pContext
, &pContext
->threadPool
);
165 for (uint32_t i
= 0; i
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++i
)
167 delete pContext
->dcRing
[i
].pArena
;
168 delete pContext
->dsRing
[i
].pArena
;
169 pContext
->pMacroTileManagerArray
[i
].~MacroTileMgr();
170 pContext
->pDispatchQueueArray
[i
].~DispatchQueue();
173 AlignedFree(pContext
->pDispatchQueueArray
);
174 AlignedFree(pContext
->pMacroTileManagerArray
);
176 // Free scratch space.
177 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
180 VirtualFree(pContext
->pScratch
[i
], 0, MEM_RELEASE
);
182 AlignedFree(pContext
->pScratch
[i
]);
186 delete(pContext
->pHotTileMgr
);
188 pContext
->~SWR_CONTEXT();
189 AlignedFree(GetContext(hContext
));
192 void CopyState(DRAW_STATE
& dst
, const DRAW_STATE
& src
)
194 memcpy(&dst
.state
, &src
.state
, sizeof(API_STATE
));
197 void WakeAllThreads(SWR_CONTEXT
*pContext
)
199 pContext
->FifosNotEmpty
.notify_all();
202 template<bool IsDraw
>
203 void QueueWork(SWR_CONTEXT
*pContext
)
205 DRAW_CONTEXT
* pDC
= pContext
->pCurDrawContext
;
206 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
210 pDC
->pTileMgr
= &pContext
->pMacroTileManagerArray
[dcIndex
];
211 pDC
->pTileMgr
->initialize();
214 // Each worker thread looks at a DC for both FE and BE work at different times and so we
215 // multiply threadDone by 2. When the threadDone counter has reached 0 then all workers
216 // have moved past this DC. (i.e. Each worker has checked this DC for both FE and BE work and
217 // then moved on if all work is done.)
218 pContext
->pCurDrawContext
->threadsDone
= pContext
->NumFEThreads
+ pContext
->NumBEThreads
;
222 InterlockedIncrement((volatile LONG
*)&pContext
->drawsOutstandingFE
);
227 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
);
228 pContext
->dcRing
.Enqueue();
231 if (pContext
->threadInfo
.SINGLE_THREADED
)
233 // flush denormals to 0
234 uint32_t mxcsr
= _mm_getcsr();
235 _mm_setcsr(mxcsr
| _MM_FLUSH_ZERO_ON
| _MM_DENORMALS_ZERO_ON
);
239 uint32_t curDraw
[2] = { pContext
->pCurDrawContext
->drawId
, pContext
->pCurDrawContext
->drawId
};
240 WorkOnFifoFE(pContext
, 0, curDraw
[0]);
241 WorkOnFifoBE(pContext
, 0, curDraw
[1], pContext
->singleThreadLockedTiles
, 0, 0);
245 uint32_t curDispatch
= pContext
->pCurDrawContext
->drawId
;
246 WorkOnCompute(pContext
, 0, curDispatch
);
249 // Dequeue the work here, if not already done, since we're single threaded (i.e. no workers).
250 while (CompleteDrawContext(pContext
, pContext
->pCurDrawContext
) > 0) {}
257 RDTSC_START(APIDrawWakeAllThreads
);
258 WakeAllThreads(pContext
);
259 RDTSC_STOP(APIDrawWakeAllThreads
, 1, 0);
262 // Set current draw context to NULL so that next state call forces a new draw context to be created and populated.
263 pContext
->pPrevDrawContext
= pContext
->pCurDrawContext
;
264 pContext
->pCurDrawContext
= nullptr;
267 INLINE
void QueueDraw(SWR_CONTEXT
* pContext
)
269 QueueWork
<true>(pContext
);
272 INLINE
void QueueDispatch(SWR_CONTEXT
* pContext
)
274 QueueWork
<false>(pContext
);
277 DRAW_CONTEXT
* GetDrawContext(SWR_CONTEXT
*pContext
, bool isSplitDraw
= false)
279 RDTSC_START(APIGetDrawContext
);
280 // If current draw context is null then need to obtain a new draw context to use from ring.
281 if (pContext
->pCurDrawContext
== nullptr)
283 // Need to wait for a free entry.
284 while (pContext
->dcRing
.IsFull())
289 uint64_t curDraw
= pContext
->dcRing
.GetHead();
290 uint32_t dcIndex
= curDraw
% KNOB_MAX_DRAWS_IN_FLIGHT
;
292 if ((pContext
->frameCount
- pContext
->lastFrameChecked
) > 2 ||
293 (curDraw
- pContext
->lastDrawChecked
) > 0x10000)
295 // Take this opportunity to clean-up old arena allocations
296 pContext
->cachingArenaAllocator
.FreeOldBlocks();
298 pContext
->lastFrameChecked
= pContext
->frameCount
;
299 pContext
->lastDrawChecked
= curDraw
;
302 DRAW_CONTEXT
* pCurDrawContext
= &pContext
->dcRing
[dcIndex
];
303 pContext
->pCurDrawContext
= pCurDrawContext
;
305 // Assign next available entry in DS ring to this DC.
306 uint32_t dsIndex
= pContext
->curStateId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
307 pCurDrawContext
->pState
= &pContext
->dsRing
[dsIndex
];
309 // Copy previous state to current state.
310 if (pContext
->pPrevDrawContext
)
312 DRAW_CONTEXT
* pPrevDrawContext
= pContext
->pPrevDrawContext
;
314 // If we're splitting our draw then we can just use the same state from the previous
315 // draw. In this case, we won't increment the DS ring index so the next non-split
316 // draw can receive the state.
317 if (isSplitDraw
== false)
319 CopyState(*pCurDrawContext
->pState
, *pPrevDrawContext
->pState
);
321 // Should have been cleaned up previously
322 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
324 pCurDrawContext
->pState
->pPrivateState
= nullptr;
326 pContext
->curStateId
++; // Progress state ring index forward.
330 // If its a split draw then just copy the state pointer over
331 // since its the same draw.
332 pCurDrawContext
->pState
= pPrevDrawContext
->pState
;
333 SWR_ASSERT(pPrevDrawContext
->cleanupState
== false);
338 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
339 pContext
->curStateId
++; // Progress state ring index forward.
342 SWR_ASSERT(pCurDrawContext
->pArena
->IsEmpty() == true);
344 pCurDrawContext
->dependent
= false;
345 pCurDrawContext
->pContext
= pContext
;
346 pCurDrawContext
->isCompute
= false; // Dispatch has to set this to true.
348 pCurDrawContext
->doneFE
= false;
349 pCurDrawContext
->FeLock
= 0;
350 pCurDrawContext
->threadsDone
= 0;
351 pCurDrawContext
->retireCallback
.pfnCallbackFunc
= nullptr;
353 memset(&pCurDrawContext
->dynState
, 0, sizeof(pCurDrawContext
->dynState
));
355 // Assign unique drawId for this DC
356 pCurDrawContext
->drawId
= pContext
->dcRing
.GetHead();
358 pCurDrawContext
->cleanupState
= true;
362 SWR_ASSERT(isSplitDraw
== false, "Split draw should only be used when obtaining a new DC");
365 RDTSC_STOP(APIGetDrawContext
, 0, 0);
366 return pContext
->pCurDrawContext
;
369 API_STATE
* GetDrawState(SWR_CONTEXT
*pContext
)
371 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
372 SWR_ASSERT(pDC
->pState
!= nullptr);
374 return &pDC
->pState
->state
;
377 void SWR_API
SwrSaveState(
379 void* pOutputStateBlock
,
382 SWR_CONTEXT
*pContext
= GetContext(hContext
);
383 auto pSrc
= GetDrawState(pContext
);
384 SWR_ASSERT(pOutputStateBlock
&& memSize
>= sizeof(*pSrc
));
386 memcpy(pOutputStateBlock
, pSrc
, sizeof(*pSrc
));
389 void SWR_API
SwrRestoreState(
391 const void* pStateBlock
,
394 SWR_CONTEXT
*pContext
= GetContext(hContext
);
395 auto pDst
= GetDrawState(pContext
);
396 SWR_ASSERT(pStateBlock
&& memSize
>= sizeof(*pDst
));
398 memcpy(pDst
, pStateBlock
, sizeof(*pDst
));
401 void SetupDefaultState(SWR_CONTEXT
*pContext
)
403 API_STATE
* pState
= GetDrawState(pContext
);
405 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
406 pState
->rastState
.frontWinding
= SWR_FRONTWINDING_CCW
;
409 void SwrSync(HANDLE hContext
, PFN_CALLBACK_FUNC pfnFunc
, uint64_t userData
, uint64_t userData2
, uint64_t userData3
)
411 RDTSC_START(APISync
);
413 SWR_ASSERT(pfnFunc
!= nullptr);
415 SWR_CONTEXT
*pContext
= GetContext(hContext
);
416 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
418 pDC
->FeWork
.type
= SYNC
;
419 pDC
->FeWork
.pfnWork
= ProcessSync
;
421 // Setup callback function
422 pDC
->retireCallback
.pfnCallbackFunc
= pfnFunc
;
423 pDC
->retireCallback
.userData
= userData
;
424 pDC
->retireCallback
.userData2
= userData2
;
425 pDC
->retireCallback
.userData3
= userData3
;
430 RDTSC_STOP(APISync
, 1, 0);
433 void SwrWaitForIdle(HANDLE hContext
)
435 SWR_CONTEXT
*pContext
= GetContext(hContext
);
437 RDTSC_START(APIWaitForIdle
);
439 while (!pContext
->dcRing
.IsEmpty())
444 RDTSC_STOP(APIWaitForIdle
, 1, 0);
447 void SwrWaitForIdleFE(HANDLE hContext
)
449 SWR_CONTEXT
*pContext
= GetContext(hContext
);
451 RDTSC_START(APIWaitForIdle
);
453 while (pContext
->drawsOutstandingFE
> 0)
458 RDTSC_STOP(APIWaitForIdle
, 1, 0);
461 void SwrSetVertexBuffers(
464 const SWR_VERTEX_BUFFER_STATE
* pVertexBuffers
)
466 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
468 for (uint32_t i
= 0; i
< numBuffers
; ++i
)
470 const SWR_VERTEX_BUFFER_STATE
*pVB
= &pVertexBuffers
[i
];
471 pState
->vertexBuffers
[pVB
->index
] = *pVB
;
475 void SwrSetIndexBuffer(
477 const SWR_INDEX_BUFFER_STATE
* pIndexBuffer
)
479 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
481 pState
->indexBuffer
= *pIndexBuffer
;
484 void SwrSetFetchFunc(
486 PFN_FETCH_FUNC pfnFetchFunc
)
488 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
490 pState
->pfnFetchFunc
= pfnFetchFunc
;
495 PFN_SO_FUNC pfnSoFunc
,
496 uint32_t streamIndex
)
498 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
500 SWR_ASSERT(streamIndex
< MAX_SO_STREAMS
);
502 pState
->pfnSoFunc
[streamIndex
] = pfnSoFunc
;
507 SWR_STREAMOUT_STATE
* pSoState
)
509 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
511 pState
->soState
= *pSoState
;
514 void SwrSetSoBuffers(
516 SWR_STREAMOUT_BUFFER
* pSoBuffer
,
519 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
521 SWR_ASSERT((slot
< 4), "There are only 4 SO buffer slots [0, 3]\nSlot requested: %d", slot
);
523 pState
->soBuffer
[slot
] = *pSoBuffer
;
526 void SwrSetVertexFunc(
528 PFN_VERTEX_FUNC pfnVertexFunc
)
530 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
532 pState
->pfnVertexFunc
= pfnVertexFunc
;
535 void SwrSetFrontendState(
537 SWR_FRONTEND_STATE
*pFEState
)
539 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
540 pState
->frontendState
= *pFEState
;
545 SWR_GS_STATE
*pGSState
)
547 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
548 pState
->gsState
= *pGSState
;
553 PFN_GS_FUNC pfnGsFunc
)
555 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
556 pState
->pfnGsFunc
= pfnGsFunc
;
561 PFN_CS_FUNC pfnCsFunc
,
562 uint32_t totalThreadsInGroup
,
563 uint32_t totalSpillFillSize
)
565 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
566 pState
->pfnCsFunc
= pfnCsFunc
;
567 pState
->totalThreadsInGroup
= totalThreadsInGroup
;
568 pState
->totalSpillFillSize
= totalSpillFillSize
;
573 SWR_TS_STATE
*pState
)
575 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
576 pApiState
->tsState
= *pState
;
583 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
584 pApiState
->pfnHsFunc
= pfnFunc
;
591 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
592 pApiState
->pfnDsFunc
= pfnFunc
;
595 void SwrSetDepthStencilState(
597 SWR_DEPTH_STENCIL_STATE
*pDSState
)
599 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
601 pState
->depthStencilState
= *pDSState
;
604 void SwrSetBackendState(
606 SWR_BACKEND_STATE
*pBEState
)
608 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
610 pState
->backendState
= *pBEState
;
613 void SwrSetPixelShaderState(
615 SWR_PS_STATE
*pPSState
)
617 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
618 pState
->psState
= *pPSState
;
621 void SwrSetBlendState(
623 SWR_BLEND_STATE
*pBlendState
)
625 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
626 memcpy(&pState
->blendState
, pBlendState
, sizeof(SWR_BLEND_STATE
));
629 void SwrSetBlendFunc(
631 uint32_t renderTarget
,
632 PFN_BLEND_JIT_FUNC pfnBlendFunc
)
634 SWR_ASSERT(renderTarget
< SWR_NUM_RENDERTARGETS
);
635 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
636 pState
->pfnBlendFunc
[renderTarget
] = pfnBlendFunc
;
639 // update guardband multipliers for the viewport
640 void updateGuardband(API_STATE
*pState
)
642 // guardband center is viewport center
643 pState
->gbState
.left
= KNOB_GUARDBAND_WIDTH
/ pState
->vp
[0].width
;
644 pState
->gbState
.right
= KNOB_GUARDBAND_WIDTH
/ pState
->vp
[0].width
;
645 pState
->gbState
.top
= KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[0].height
;
646 pState
->gbState
.bottom
= KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[0].height
;
649 void SwrSetRastState(
651 const SWR_RASTSTATE
*pRastState
)
653 SWR_CONTEXT
*pContext
= GetContext(hContext
);
654 API_STATE
* pState
= GetDrawState(pContext
);
656 memcpy(&pState
->rastState
, pRastState
, sizeof(SWR_RASTSTATE
));
659 void SwrSetViewports(
661 uint32_t numViewports
,
662 const SWR_VIEWPORT
* pViewports
,
663 const SWR_VIEWPORT_MATRICES
* pMatrices
)
665 SWR_ASSERT(numViewports
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
666 "Invalid number of viewports.");
668 SWR_CONTEXT
*pContext
= GetContext(hContext
);
669 API_STATE
* pState
= GetDrawState(pContext
);
671 memcpy(&pState
->vp
[0], pViewports
, sizeof(SWR_VIEWPORT
) * numViewports
);
673 if (pMatrices
!= nullptr)
675 // @todo Faster to copy portions of the SOA or just copy all of it?
676 memcpy(&pState
->vpMatrices
, pMatrices
, sizeof(SWR_VIEWPORT_MATRICES
));
680 // Compute default viewport transform.
681 for (uint32_t i
= 0; i
< numViewports
; ++i
)
683 if (pContext
->driverType
== DX
)
685 pState
->vpMatrices
.m00
[i
] = pState
->vp
[i
].width
/ 2.0f
;
686 pState
->vpMatrices
.m11
[i
] = -pState
->vp
[i
].height
/ 2.0f
;
687 pState
->vpMatrices
.m22
[i
] = pState
->vp
[i
].maxZ
- pState
->vp
[i
].minZ
;
688 pState
->vpMatrices
.m30
[i
] = pState
->vp
[i
].x
+ pState
->vpMatrices
.m00
[i
];
689 pState
->vpMatrices
.m31
[i
] = pState
->vp
[i
].y
- pState
->vpMatrices
.m11
[i
];
690 pState
->vpMatrices
.m32
[i
] = pState
->vp
[i
].minZ
;
694 // Standard, with the exception that Y is inverted.
695 pState
->vpMatrices
.m00
[i
] = (pState
->vp
[i
].width
- pState
->vp
[i
].x
) / 2.0f
;
696 pState
->vpMatrices
.m11
[i
] = (pState
->vp
[i
].y
- pState
->vp
[i
].height
) / 2.0f
;
697 pState
->vpMatrices
.m22
[i
] = (pState
->vp
[i
].maxZ
- pState
->vp
[i
].minZ
) / 2.0f
;
698 pState
->vpMatrices
.m30
[i
] = pState
->vp
[i
].x
+ pState
->vpMatrices
.m00
[i
];
699 pState
->vpMatrices
.m31
[i
] = pState
->vp
[i
].height
+ pState
->vpMatrices
.m11
[i
];
700 pState
->vpMatrices
.m32
[i
] = pState
->vp
[i
].minZ
+ pState
->vpMatrices
.m22
[i
];
702 // Now that the matrix is calculated, clip the view coords to screen size.
703 // OpenGL allows for -ve x,y in the viewport.
704 pState
->vp
[i
].x
= std::max(pState
->vp
[i
].x
, 0.0f
);
705 pState
->vp
[i
].y
= std::max(pState
->vp
[i
].y
, 0.0f
);
710 updateGuardband(pState
);
713 void SwrSetScissorRects(
715 uint32_t numScissors
,
716 const BBOX
* pScissors
)
718 SWR_ASSERT(numScissors
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
719 "Invalid number of scissor rects.");
721 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
722 memcpy(&pState
->scissorRects
[0], pScissors
, numScissors
* sizeof(BBOX
));
725 void SetupMacroTileScissors(DRAW_CONTEXT
*pDC
)
727 API_STATE
*pState
= &pDC
->pState
->state
;
728 uint32_t left
, right
, top
, bottom
;
730 // Set up scissor dimensions based on scissor or viewport
731 if (pState
->rastState
.scissorEnable
)
733 // scissor rect right/bottom edge are exclusive, core expects scissor dimensions to be inclusive, so subtract one pixel from right/bottom edges
734 left
= pState
->scissorRects
[0].left
;
735 right
= pState
->scissorRects
[0].right
;
736 top
= pState
->scissorRects
[0].top
;
737 bottom
= pState
->scissorRects
[0].bottom
;
741 // the vp width and height must be added to origin un-rounded then the result round to -inf.
742 // The cast to int works for rounding assuming all [left, right, top, bottom] are positive.
743 left
= (int32_t)pState
->vp
[0].x
;
744 right
= (int32_t)(pState
->vp
[0].x
+ pState
->vp
[0].width
);
745 top
= (int32_t)pState
->vp
[0].y
;
746 bottom
= (int32_t)(pState
->vp
[0].y
+ pState
->vp
[0].height
);
749 right
= std::min
<uint32_t>(right
, KNOB_MAX_SCISSOR_X
);
750 bottom
= std::min
<uint32_t>(bottom
, KNOB_MAX_SCISSOR_Y
);
752 if (left
> KNOB_MAX_SCISSOR_X
|| top
> KNOB_MAX_SCISSOR_Y
)
754 pState
->scissorInFixedPoint
.left
= 0;
755 pState
->scissorInFixedPoint
.right
= 0;
756 pState
->scissorInFixedPoint
.top
= 0;
757 pState
->scissorInFixedPoint
.bottom
= 0;
761 pState
->scissorInFixedPoint
.left
= left
* FIXED_POINT_SCALE
;
762 pState
->scissorInFixedPoint
.right
= right
* FIXED_POINT_SCALE
- 1;
763 pState
->scissorInFixedPoint
.top
= top
* FIXED_POINT_SCALE
;
764 pState
->scissorInFixedPoint
.bottom
= bottom
* FIXED_POINT_SCALE
- 1;
768 // templated backend function tables
769 extern PFN_BACKEND_FUNC gBackendNullPs
[SWR_MULTISAMPLE_TYPE_COUNT
];
770 extern PFN_BACKEND_FUNC gBackendSingleSample
[SWR_INPUT_COVERAGE_COUNT
][2][2];
771 extern PFN_BACKEND_FUNC gBackendPixelRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_MSAA_SAMPLE_PATTERN_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2][2];
772 extern PFN_BACKEND_FUNC gBackendSampleRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2];
773 void SetupPipeline(DRAW_CONTEXT
*pDC
)
775 DRAW_STATE
* pState
= pDC
->pState
;
776 const SWR_RASTSTATE
&rastState
= pState
->state
.rastState
;
777 const SWR_PS_STATE
&psState
= pState
->state
.psState
;
778 BACKEND_FUNCS
& backendFuncs
= pState
->backendFuncs
;
779 const uint32_t forcedSampleCount
= (rastState
.forcedSampleCount
) ? 1 : 0;
782 if (psState
.pfnPixelShader
== nullptr)
784 backendFuncs
.pfnBackend
= gBackendNullPs
[pState
->state
.rastState
.sampleCount
];
788 const bool bMultisampleEnable
= ((rastState
.sampleCount
> SWR_MULTISAMPLE_1X
) || rastState
.forcedSampleCount
) ? 1 : 0;
789 const uint32_t centroid
= ((psState
.barycentricsMask
& SWR_BARYCENTRIC_CENTROID_MASK
) > 0) ? 1 : 0;
790 const uint32_t canEarlyZ
= (psState
.forceEarlyZ
|| (!psState
.writesODepth
&& !psState
.usesSourceDepth
&& !psState
.usesUAV
)) ? 1 : 0;
792 SWR_BARYCENTRICS_MASK barycentricsMask
= (SWR_BARYCENTRICS_MASK
)psState
.barycentricsMask
;
794 // select backend function
795 switch(psState
.shadingRate
)
797 case SWR_SHADING_RATE_PIXEL
:
798 if(bMultisampleEnable
)
800 // always need to generate I & J per sample for Z interpolation
801 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
802 backendFuncs
.pfnBackend
= gBackendPixelRateTable
[rastState
.sampleCount
][rastState
.samplePattern
][psState
.inputCoverage
][centroid
][forcedSampleCount
][canEarlyZ
];
806 // always need to generate I & J per pixel for Z interpolation
807 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_PIXEL_MASK
);
808 backendFuncs
.pfnBackend
= gBackendSingleSample
[psState
.inputCoverage
][centroid
][canEarlyZ
];
811 case SWR_SHADING_RATE_SAMPLE
:
812 SWR_ASSERT(rastState
.samplePattern
== SWR_MSAA_STANDARD_PATTERN
);
813 // always need to generate I & J per sample for Z interpolation
814 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
815 backendFuncs
.pfnBackend
= gBackendSampleRateTable
[rastState
.sampleCount
][psState
.inputCoverage
][centroid
][canEarlyZ
];
818 SWR_ASSERT(0 && "Invalid shading rate");
823 PFN_PROCESS_PRIMS pfnBinner
;
824 switch (pState
->state
.topology
)
827 pState
->pfnProcessPrims
= ClipPoints
;
828 pfnBinner
= BinPoints
;
833 case TOP_LINE_LIST_ADJ
:
834 case TOP_LISTSTRIP_ADJ
:
835 pState
->pfnProcessPrims
= ClipLines
;
836 pfnBinner
= BinLines
;
839 pState
->pfnProcessPrims
= ClipTriangles
;
840 pfnBinner
= GetBinTrianglesFunc((rastState
.conservativeRast
> 0));
844 // disable clipper if viewport transform is disabled
845 if (pState
->state
.frontendState
.vpTransformDisable
)
847 pState
->pfnProcessPrims
= pfnBinner
;
850 if ((pState
->state
.psState
.pfnPixelShader
== nullptr) &&
851 (pState
->state
.depthStencilState
.depthTestEnable
== FALSE
) &&
852 (pState
->state
.depthStencilState
.depthWriteEnable
== FALSE
) &&
853 (pState
->state
.depthStencilState
.stencilTestEnable
== FALSE
) &&
854 (pState
->state
.depthStencilState
.stencilWriteEnable
== FALSE
) &&
855 (pState
->state
.backendState
.numAttributes
== 0))
857 pState
->pfnProcessPrims
= nullptr;
860 if (pState
->state
.soState
.rasterizerDisable
== true)
862 pState
->pfnProcessPrims
= nullptr;
865 // set up the frontend attribute count
866 pState
->state
.feNumAttributes
= 0;
867 const SWR_BACKEND_STATE
& backendState
= pState
->state
.backendState
;
868 if (backendState
.swizzleEnable
)
870 // attribute swizzling is enabled, iterate over the map and record the max attribute used
871 for (uint32_t i
= 0; i
< backendState
.numAttributes
; ++i
)
873 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)backendState
.swizzleMap
[i
].sourceAttrib
+ 1);
878 pState
->state
.feNumAttributes
= pState
->state
.backendState
.numAttributes
;
881 if (pState
->state
.soState
.soEnable
)
883 uint32_t streamMasks
= 0;
884 for (uint32_t i
= 0; i
< 4; ++i
)
886 streamMasks
|= pState
->state
.soState
.streamMasks
[i
];
890 if (_BitScanReverse(&maxAttrib
, streamMasks
))
892 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)(maxAttrib
+ 1));
896 // complicated logic to test for cases where we don't need backing hottile memory for a draw
897 // have to check for the special case where depth/stencil test is enabled but depthwrite is disabled.
898 pState
->state
.depthHottileEnable
= ((!(pState
->state
.depthStencilState
.depthTestEnable
&&
899 !pState
->state
.depthStencilState
.depthWriteEnable
&&
900 pState
->state
.depthStencilState
.depthTestFunc
== ZFUNC_ALWAYS
)) &&
901 (pState
->state
.depthStencilState
.depthTestEnable
||
902 pState
->state
.depthStencilState
.depthWriteEnable
)) ? true : false;
904 pState
->state
.stencilHottileEnable
= (((!(pState
->state
.depthStencilState
.stencilTestEnable
&&
905 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
906 pState
->state
.depthStencilState
.stencilTestFunc
== ZFUNC_ALWAYS
)) ||
907 // for stencil we have to check the double sided state as well
908 (!(pState
->state
.depthStencilState
.doubleSidedStencilTestEnable
&&
909 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
910 pState
->state
.depthStencilState
.backfaceStencilTestFunc
== ZFUNC_ALWAYS
))) &&
911 (pState
->state
.depthStencilState
.stencilTestEnable
||
912 pState
->state
.depthStencilState
.stencilWriteEnable
)) ? true : false;
914 uint32_t numRTs
= pState
->state
.psState
.numRenderTargets
;
915 pState
->state
.colorHottileEnable
= 0;
916 if (psState
.pfnPixelShader
!= nullptr)
918 for (uint32_t rt
= 0; rt
< numRTs
; ++rt
)
920 pState
->state
.colorHottileEnable
|=
921 (!pState
->state
.blendState
.renderTarget
[rt
].writeDisableAlpha
||
922 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableRed
||
923 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableGreen
||
924 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableBlue
) ? (1 << rt
) : 0;
928 // Setup depth quantization function
929 if (pState
->state
.depthHottileEnable
)
931 switch (pState
->state
.rastState
.depthFormat
)
933 case R32_FLOAT_X8X24_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT_X8X24_TYPELESS
> ; break;
934 case R32_FLOAT
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ; break;
935 case R24_UNORM_X8_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R24_UNORM_X8_TYPELESS
> ; break;
936 case R16_UNORM
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R16_UNORM
> ; break;
937 default: SWR_ASSERT(false, "Unsupported depth format for depth quantiztion.");
938 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
943 // set up pass-through quantize if depth isn't enabled
944 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
948 //////////////////////////////////////////////////////////////////////////
950 /// @param pDC - Draw context to initialize for this draw.
955 // We don't need to re-setup the scissors/pipeline state again for split draw.
956 if (isSplitDraw
== false)
958 SetupMacroTileScissors(pDC
);
963 //////////////////////////////////////////////////////////////////////////
964 /// @brief We can split the draw for certain topologies for better performance.
965 /// @param totalVerts - Total vertices for draw
966 /// @param topology - Topology used for draw
967 uint32_t MaxVertsPerDraw(
970 PRIMITIVE_TOPOLOGY topology
)
972 API_STATE
& state
= pDC
->pState
->state
;
974 uint32_t vertsPerDraw
= totalVerts
;
976 if (state
.soState
.soEnable
)
984 case TOP_TRIANGLE_LIST
:
985 vertsPerDraw
= KNOB_MAX_PRIMS_PER_DRAW
;
988 case TOP_PATCHLIST_1
:
989 case TOP_PATCHLIST_2
:
990 case TOP_PATCHLIST_3
:
991 case TOP_PATCHLIST_4
:
992 case TOP_PATCHLIST_5
:
993 case TOP_PATCHLIST_6
:
994 case TOP_PATCHLIST_7
:
995 case TOP_PATCHLIST_8
:
996 case TOP_PATCHLIST_9
:
997 case TOP_PATCHLIST_10
:
998 case TOP_PATCHLIST_11
:
999 case TOP_PATCHLIST_12
:
1000 case TOP_PATCHLIST_13
:
1001 case TOP_PATCHLIST_14
:
1002 case TOP_PATCHLIST_15
:
1003 case TOP_PATCHLIST_16
:
1004 case TOP_PATCHLIST_17
:
1005 case TOP_PATCHLIST_18
:
1006 case TOP_PATCHLIST_19
:
1007 case TOP_PATCHLIST_20
:
1008 case TOP_PATCHLIST_21
:
1009 case TOP_PATCHLIST_22
:
1010 case TOP_PATCHLIST_23
:
1011 case TOP_PATCHLIST_24
:
1012 case TOP_PATCHLIST_25
:
1013 case TOP_PATCHLIST_26
:
1014 case TOP_PATCHLIST_27
:
1015 case TOP_PATCHLIST_28
:
1016 case TOP_PATCHLIST_29
:
1017 case TOP_PATCHLIST_30
:
1018 case TOP_PATCHLIST_31
:
1019 case TOP_PATCHLIST_32
:
1020 if (pDC
->pState
->state
.tsState
.tsEnable
)
1022 uint32_t vertsPerPrim
= topology
- TOP_PATCHLIST_BASE
;
1023 vertsPerDraw
= vertsPerPrim
* KNOB_MAX_TESS_PRIMS_PER_DRAW
;
1027 // The Primitive Assembly code can only handle 1 RECT at a time.
1033 // We are not splitting up draws for other topologies.
1037 return vertsPerDraw
;
1041 //////////////////////////////////////////////////////////////////////////
1042 /// @brief DrawInstanced
1043 /// @param hContext - Handle passed back from SwrCreateContext
1044 /// @param topology - Specifies topology for draw.
1045 /// @param numVerts - How many vertices to read sequentially from vertex data (per instance).
1046 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1047 /// @param numInstances - How many instances to render.
1048 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1051 PRIMITIVE_TOPOLOGY topology
,
1052 uint32_t numVertices
,
1053 uint32_t startVertex
,
1054 uint32_t numInstances
= 1,
1055 uint32_t startInstance
= 0)
1062 RDTSC_START(APIDraw
);
1064 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1065 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1067 uint32_t maxVertsPerDraw
= MaxVertsPerDraw(pDC
, numVertices
, topology
);
1068 uint32_t primsPerDraw
= GetNumPrims(topology
, maxVertsPerDraw
);
1069 uint32_t remainingVerts
= numVertices
;
1071 API_STATE
*pState
= &pDC
->pState
->state
;
1072 pState
->topology
= topology
;
1073 pState
->forceFront
= false;
1075 // disable culling for points/lines
1076 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1077 if (topology
== TOP_POINT_LIST
)
1079 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1080 pState
->forceFront
= true;
1084 while (remainingVerts
)
1086 uint32_t numVertsForDraw
= (remainingVerts
< maxVertsPerDraw
) ?
1087 remainingVerts
: maxVertsPerDraw
;
1089 bool isSplitDraw
= (draw
> 0) ? true : false;
1090 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
, isSplitDraw
);
1091 InitDraw(pDC
, isSplitDraw
);
1093 pDC
->FeWork
.type
= DRAW
;
1094 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1096 false, // bEnableCutIndex
1097 pState
->tsState
.tsEnable
,
1098 pState
->gsState
.gsEnable
,
1099 pState
->soState
.soEnable
,
1100 pDC
->pState
->pfnProcessPrims
!= nullptr);
1101 pDC
->FeWork
.desc
.draw
.numVerts
= numVertsForDraw
;
1102 pDC
->FeWork
.desc
.draw
.startVertex
= startVertex
;
1103 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1104 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1105 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1106 pDC
->FeWork
.desc
.draw
.startVertexID
= draw
* maxVertsPerDraw
;
1108 pDC
->cleanupState
= (remainingVerts
== numVertsForDraw
);
1111 QueueDraw(pContext
);
1113 remainingVerts
-= numVertsForDraw
;
1117 // restore culling state
1118 pDC
= GetDrawContext(pContext
);
1119 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1121 RDTSC_STOP(APIDraw
, numVertices
* numInstances
, 0);
1124 //////////////////////////////////////////////////////////////////////////
1126 /// @param hContext - Handle passed back from SwrCreateContext
1127 /// @param topology - Specifies topology for draw.
1128 /// @param startVertex - Specifies start vertex in vertex buffer for draw.
1129 /// @param primCount - Number of vertices.
1132 PRIMITIVE_TOPOLOGY topology
,
1133 uint32_t startVertex
,
1134 uint32_t numVertices
)
1136 DrawInstanced(hContext
, topology
, numVertices
, startVertex
);
1139 //////////////////////////////////////////////////////////////////////////
1140 /// @brief SwrDrawInstanced
1141 /// @param hContext - Handle passed back from SwrCreateContext
1142 /// @param topology - Specifies topology for draw.
1143 /// @param numVertsPerInstance - How many vertices to read sequentially from vertex data.
1144 /// @param numInstances - How many instances to render.
1145 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1146 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1147 void SwrDrawInstanced(
1149 PRIMITIVE_TOPOLOGY topology
,
1150 uint32_t numVertsPerInstance
,
1151 uint32_t numInstances
,
1152 uint32_t startVertex
,
1153 uint32_t startInstance
1156 DrawInstanced(hContext
, topology
, numVertsPerInstance
, startVertex
, numInstances
, startInstance
);
1159 //////////////////////////////////////////////////////////////////////////
1160 /// @brief DrawIndexedInstanced
1161 /// @param hContext - Handle passed back from SwrCreateContext
1162 /// @param topology - Specifies topology for draw.
1163 /// @param numIndices - Number of indices to read sequentially from index buffer.
1164 /// @param indexOffset - Starting index into index buffer.
1165 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1166 /// @param numInstances - Number of instances to render.
1167 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1168 void DrawIndexedInstance(
1170 PRIMITIVE_TOPOLOGY topology
,
1171 uint32_t numIndices
,
1172 uint32_t indexOffset
,
1174 uint32_t numInstances
= 1,
1175 uint32_t startInstance
= 0)
1182 RDTSC_START(APIDrawIndexed
);
1184 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1185 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1186 API_STATE
* pState
= &pDC
->pState
->state
;
1188 uint32_t maxIndicesPerDraw
= MaxVertsPerDraw(pDC
, numIndices
, topology
);
1189 uint32_t primsPerDraw
= GetNumPrims(topology
, maxIndicesPerDraw
);
1190 uint32_t remainingIndices
= numIndices
;
1192 uint32_t indexSize
= 0;
1193 switch (pState
->indexBuffer
.format
)
1195 case R32_UINT
: indexSize
= sizeof(uint32_t); break;
1196 case R16_UINT
: indexSize
= sizeof(uint16_t); break;
1197 case R8_UINT
: indexSize
= sizeof(uint8_t); break;
1203 uint8_t *pIB
= (uint8_t*)pState
->indexBuffer
.pIndices
;
1204 pIB
+= (uint64_t)indexOffset
* (uint64_t)indexSize
;
1206 pState
->topology
= topology
;
1207 pState
->forceFront
= false;
1209 // disable culling for points/lines
1210 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1211 if (topology
== TOP_POINT_LIST
)
1213 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1214 pState
->forceFront
= true;
1217 while (remainingIndices
)
1219 uint32_t numIndicesForDraw
= (remainingIndices
< maxIndicesPerDraw
) ?
1220 remainingIndices
: maxIndicesPerDraw
;
1222 // When breaking up draw, we need to obtain new draw context for each iteration.
1223 bool isSplitDraw
= (draw
> 0) ? true : false;
1224 pDC
= GetDrawContext(pContext
, isSplitDraw
);
1225 InitDraw(pDC
, isSplitDraw
);
1227 pDC
->FeWork
.type
= DRAW
;
1228 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1230 pState
->frontendState
.bEnableCutIndex
,
1231 pState
->tsState
.tsEnable
,
1232 pState
->gsState
.gsEnable
,
1233 pState
->soState
.soEnable
,
1234 pDC
->pState
->pfnProcessPrims
!= nullptr);
1235 pDC
->FeWork
.desc
.draw
.pDC
= pDC
;
1236 pDC
->FeWork
.desc
.draw
.numIndices
= numIndicesForDraw
;
1237 pDC
->FeWork
.desc
.draw
.pIB
= (int*)pIB
;
1238 pDC
->FeWork
.desc
.draw
.type
= pDC
->pState
->state
.indexBuffer
.format
;
1240 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1241 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1242 pDC
->FeWork
.desc
.draw
.baseVertex
= baseVertex
;
1243 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1245 pDC
->cleanupState
= (remainingIndices
== numIndicesForDraw
);
1248 QueueDraw(pContext
);
1250 pIB
+= maxIndicesPerDraw
* indexSize
;
1251 remainingIndices
-= numIndicesForDraw
;
1255 // restore culling state
1256 pDC
= GetDrawContext(pContext
);
1257 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1259 RDTSC_STOP(APIDrawIndexed
, numIndices
* numInstances
, 0);
1263 //////////////////////////////////////////////////////////////////////////
1264 /// @brief DrawIndexed
1265 /// @param hContext - Handle passed back from SwrCreateContext
1266 /// @param topology - Specifies topology for draw.
1267 /// @param numIndices - Number of indices to read sequentially from index buffer.
1268 /// @param indexOffset - Starting index into index buffer.
1269 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1270 void SwrDrawIndexed(
1272 PRIMITIVE_TOPOLOGY topology
,
1273 uint32_t numIndices
,
1274 uint32_t indexOffset
,
1278 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
);
1281 //////////////////////////////////////////////////////////////////////////
1282 /// @brief SwrDrawIndexedInstanced
1283 /// @param hContext - Handle passed back from SwrCreateContext
1284 /// @param topology - Specifies topology for draw.
1285 /// @param numIndices - Number of indices to read sequentially from index buffer.
1286 /// @param numInstances - Number of instances to render.
1287 /// @param indexOffset - Starting index into index buffer.
1288 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1289 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1290 void SwrDrawIndexedInstanced(
1292 PRIMITIVE_TOPOLOGY topology
,
1293 uint32_t numIndices
,
1294 uint32_t numInstances
,
1295 uint32_t indexOffset
,
1297 uint32_t startInstance
)
1299 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
);
1302 //////////////////////////////////////////////////////////////////////////
1303 /// @brief SwrInvalidateTiles
1304 /// @param hContext - Handle passed back from SwrCreateContext
1305 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to invalidate.
1306 void SwrInvalidateTiles(
1308 uint32_t attachmentMask
)
1315 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1316 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1318 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1319 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1320 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1321 memset(&pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
, 0, sizeof(SWR_RECT
));
1322 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_INVALID
;
1323 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= false;
1324 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= false;
1327 QueueDraw(pContext
);
1330 //////////////////////////////////////////////////////////////////////////
1331 /// @brief SwrDiscardRect
1332 /// @param hContext - Handle passed back from SwrCreateContext
1333 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to discard.
1334 /// @param rect - if rect is all zeros, the entire attachment surface will be discarded
1335 void SwrDiscardRect(
1337 uint32_t attachmentMask
,
1345 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1346 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1348 // Queue a load to the hottile
1349 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1350 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1351 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1352 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= rect
;
1353 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_RESOLVED
;
1354 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= true;
1355 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= true;
1358 QueueDraw(pContext
);
1361 //////////////////////////////////////////////////////////////////////////
1362 /// @brief SwrDispatch
1363 /// @param hContext - Handle passed back from SwrCreateContext
1364 /// @param threadGroupCountX - Number of thread groups dispatched in X direction
1365 /// @param threadGroupCountY - Number of thread groups dispatched in Y direction
1366 /// @param threadGroupCountZ - Number of thread groups dispatched in Z direction
1369 uint32_t threadGroupCountX
,
1370 uint32_t threadGroupCountY
,
1371 uint32_t threadGroupCountZ
)
1378 RDTSC_START(APIDispatch
);
1379 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1380 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1382 pDC
->isCompute
= true; // This is a compute context.
1384 COMPUTE_DESC
* pTaskData
= (COMPUTE_DESC
*)pDC
->pArena
->AllocAligned(sizeof(COMPUTE_DESC
), 64);
1386 pTaskData
->threadGroupCountX
= threadGroupCountX
;
1387 pTaskData
->threadGroupCountY
= threadGroupCountY
;
1388 pTaskData
->threadGroupCountZ
= threadGroupCountZ
;
1390 uint32_t totalThreadGroups
= threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
;
1391 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
1392 pDC
->pDispatch
= &pContext
->pDispatchQueueArray
[dcIndex
];
1393 pDC
->pDispatch
->initialize(totalThreadGroups
, pTaskData
);
1395 QueueDispatch(pContext
);
1396 RDTSC_STOP(APIDispatch
, threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
, 0);
1399 // Deswizzles, converts and stores current contents of the hot tiles to surface
1400 // described by pState
1403 SWR_RENDERTARGET_ATTACHMENT attachment
,
1404 SWR_TILE_STATE postStoreTileState
)
1411 RDTSC_START(APIStoreTiles
);
1413 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1414 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1416 SetupMacroTileScissors(pDC
);
1418 pDC
->FeWork
.type
= STORETILES
;
1419 pDC
->FeWork
.pfnWork
= ProcessStoreTiles
;
1420 pDC
->FeWork
.desc
.storeTiles
.attachment
= attachment
;
1421 pDC
->FeWork
.desc
.storeTiles
.postStoreTileState
= postStoreTileState
;
1424 QueueDraw(pContext
);
1426 RDTSC_STOP(APIStoreTiles
, 0, 0);
1429 void SwrClearRenderTarget(
1432 const float clearColor
[4],
1441 RDTSC_START(APIClearRenderTarget
);
1443 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1445 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1447 SetupMacroTileScissors(pDC
);
1450 flags
.mask
= clearMask
;
1452 pDC
->FeWork
.type
= CLEAR
;
1453 pDC
->FeWork
.pfnWork
= ProcessClear
;
1454 pDC
->FeWork
.desc
.clear
.flags
= flags
;
1455 pDC
->FeWork
.desc
.clear
.clearDepth
= z
;
1456 pDC
->FeWork
.desc
.clear
.clearRTColor
[0] = clearColor
[0];
1457 pDC
->FeWork
.desc
.clear
.clearRTColor
[1] = clearColor
[1];
1458 pDC
->FeWork
.desc
.clear
.clearRTColor
[2] = clearColor
[2];
1459 pDC
->FeWork
.desc
.clear
.clearRTColor
[3] = clearColor
[3];
1460 pDC
->FeWork
.desc
.clear
.clearStencil
= stencil
;
1463 QueueDraw(pContext
);
1465 RDTSC_STOP(APIClearRenderTarget
, 0, pDC
->drawId
);
1468 //////////////////////////////////////////////////////////////////////////
1469 /// @brief Returns a pointer to the private context state for the current
1470 /// draw operation. This is used for external componets such as the
1472 /// SWR is responsible for the allocation of the private context state.
1473 /// @param hContext - Handle passed back from SwrCreateContext
1474 VOID
* SwrGetPrivateContextState(
1477 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1478 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1479 DRAW_STATE
* pState
= pDC
->pState
;
1481 if (pState
->pPrivateState
== nullptr)
1483 pState
->pPrivateState
= pState
->pArena
->AllocAligned(pContext
->privateStateSize
, KNOB_SIMD_WIDTH
*sizeof(float));
1486 return pState
->pPrivateState
;
1489 //////////////////////////////////////////////////////////////////////////
1490 /// @brief Clients can use this to allocate memory for draw/dispatch
1491 /// operations. The memory will automatically be freed once operation
1492 /// has completed. Client can use this to allocate binding tables,
1493 /// etc. needed for shader execution.
1494 /// @param hContext - Handle passed back from SwrCreateContext
1495 /// @param size - Size of allocation
1496 /// @param align - Alignment needed for allocation.
1497 VOID
* SwrAllocDrawContextMemory(
1502 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1503 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1505 return pDC
->pState
->pArena
->AllocAligned(size
, align
);
1508 //////////////////////////////////////////////////////////////////////////
1509 /// @brief Enables stats counting
1510 /// @param hContext - Handle passed back from SwrCreateContext
1511 /// @param enable - If true then counts are incremented.
1512 void SwrEnableStats(
1516 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1517 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1519 pDC
->pState
->state
.enableStats
= enable
;
1522 //////////////////////////////////////////////////////////////////////////
1523 /// @brief Mark end of frame - used for performance profiling
1524 /// @param hContext - Handle passed back from SwrCreateContext
1525 void SWR_API
SwrEndFrame(
1529 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1530 pContext
->frameCount
++;