1 /****************************************************************************
2 * Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 * @brief API implementation
27 ******************************************************************************/
35 #include "core/backend.h"
36 #include "core/context.h"
37 #include "core/depthstencil.h"
38 #include "core/frontend.h"
39 #include "core/rasterizer.h"
40 #include "core/rdtsc_core.h"
41 #include "core/threads.h"
42 #include "core/tilemgr.h"
43 #include "core/clip.h"
44 #include "core/utils.h"
46 #include "common/simdintrin.h"
47 #include "common/os.h"
49 static const SWR_RECT g_MaxScissorRect
= { 0, 0, KNOB_MAX_SCISSOR_X
, KNOB_MAX_SCISSOR_Y
};
51 void SetupDefaultState(SWR_CONTEXT
*pContext
);
53 static INLINE SWR_CONTEXT
* GetContext(HANDLE hContext
)
55 return (SWR_CONTEXT
*)hContext
;
58 //////////////////////////////////////////////////////////////////////////
59 /// @brief Create SWR Context.
60 /// @param pCreateInfo - pointer to creation info.
61 HANDLE
SwrCreateContext(
62 SWR_CREATECONTEXT_INFO
* pCreateInfo
)
67 void* pContextMem
= AlignedMalloc(sizeof(SWR_CONTEXT
), KNOB_SIMD_WIDTH
* 4);
68 memset(pContextMem
, 0, sizeof(SWR_CONTEXT
));
69 SWR_CONTEXT
*pContext
= new (pContextMem
) SWR_CONTEXT();
71 pContext
->driverType
= pCreateInfo
->driver
;
72 pContext
->privateStateSize
= pCreateInfo
->privateStateSize
;
74 pContext
->dcRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
75 pContext
->dsRing
.Init(KNOB_MAX_DRAWS_IN_FLIGHT
);
77 pContext
->pMacroTileManagerArray
= (MacroTileMgr
*)AlignedMalloc(sizeof(MacroTileMgr
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
78 pContext
->pDispatchQueueArray
= (DispatchQueue
*)AlignedMalloc(sizeof(DispatchQueue
) * KNOB_MAX_DRAWS_IN_FLIGHT
, 64);
80 pContext
->threadInfo
.MAX_WORKER_THREADS
= KNOB_MAX_WORKER_THREADS
;
81 pContext
->threadInfo
.MAX_NUMA_NODES
= KNOB_MAX_NUMA_NODES
;
82 pContext
->threadInfo
.MAX_CORES_PER_NUMA_NODE
= KNOB_MAX_CORES_PER_NUMA_NODE
;
83 pContext
->threadInfo
.MAX_THREADS_PER_CORE
= KNOB_MAX_THREADS_PER_CORE
;
84 pContext
->threadInfo
.SINGLE_THREADED
= KNOB_SINGLE_THREADED
;
86 if (pCreateInfo
->pThreadInfo
)
88 pContext
->threadInfo
= *pCreateInfo
->pThreadInfo
;
91 for (uint32_t dc
= 0; dc
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++dc
)
93 pContext
->dcRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
94 new (&pContext
->pMacroTileManagerArray
[dc
]) MacroTileMgr(*pContext
->dcRing
[dc
].pArena
);
95 new (&pContext
->pDispatchQueueArray
[dc
]) DispatchQueue();
97 pContext
->dsRing
[dc
].pArena
= new CachingArena(pContext
->cachingArenaAllocator
);
100 if (!pContext
->threadInfo
.SINGLE_THREADED
)
102 memset(&pContext
->WaitLock
, 0, sizeof(pContext
->WaitLock
));
103 memset(&pContext
->FifosNotEmpty
, 0, sizeof(pContext
->FifosNotEmpty
));
104 new (&pContext
->WaitLock
) std::mutex();
105 new (&pContext
->FifosNotEmpty
) std::condition_variable();
107 CreateThreadPool(pContext
, &pContext
->threadPool
);
110 // Calling createThreadPool() above can set SINGLE_THREADED
111 if (pContext
->threadInfo
.SINGLE_THREADED
)
113 pContext
->NumWorkerThreads
= 1;
114 pContext
->NumFEThreads
= 1;
115 pContext
->NumBEThreads
= 1;
118 // Allocate scratch space for workers.
119 ///@note We could lazily allocate this but its rather small amount of memory.
120 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
123 uint32_t numaNode
= pContext
->threadPool
.pThreadData
?
124 pContext
->threadPool
.pThreadData
[i
].numaId
: 0;
125 pContext
->pScratch
[i
] = (uint8_t*)VirtualAllocExNuma(
126 GetCurrentProcess(), nullptr, 32 * sizeof(KILOBYTE
),
127 MEM_RESERVE
| MEM_COMMIT
, PAGE_READWRITE
,
130 pContext
->pScratch
[i
] = (uint8_t*)AlignedMalloc(32 * sizeof(KILOBYTE
), KNOB_SIMD_WIDTH
* 4);
134 // State setup AFTER context is fully initialized
135 SetupDefaultState(pContext
);
137 // initialize hot tile manager
138 pContext
->pHotTileMgr
= new HotTileMgr();
140 // initialize function pointer tables
141 InitClearTilesTable();
143 // initialize callback functions
144 pContext
->pfnLoadTile
= pCreateInfo
->pfnLoadTile
;
145 pContext
->pfnStoreTile
= pCreateInfo
->pfnStoreTile
;
146 pContext
->pfnClearTile
= pCreateInfo
->pfnClearTile
;
147 pContext
->pfnUpdateSoWriteOffset
= pCreateInfo
->pfnUpdateSoWriteOffset
;
148 pContext
->pfnUpdateStats
= pCreateInfo
->pfnUpdateStats
;
149 pContext
->pfnUpdateStatsFE
= pCreateInfo
->pfnUpdateStatsFE
;
151 // pass pointer to bucket manager back to caller
152 #ifdef KNOB_ENABLE_RDTSC
153 pCreateInfo
->pBucketMgr
= &gBucketMgr
;
156 pCreateInfo
->contextSaveSize
= sizeof(API_STATE
);
158 return (HANDLE
)pContext
;
161 void SwrDestroyContext(HANDLE hContext
)
163 SWR_CONTEXT
*pContext
= GetContext(hContext
);
164 DestroyThreadPool(pContext
, &pContext
->threadPool
);
167 for (uint32_t i
= 0; i
< KNOB_MAX_DRAWS_IN_FLIGHT
; ++i
)
169 delete pContext
->dcRing
[i
].pArena
;
170 delete pContext
->dsRing
[i
].pArena
;
171 pContext
->pMacroTileManagerArray
[i
].~MacroTileMgr();
172 pContext
->pDispatchQueueArray
[i
].~DispatchQueue();
175 AlignedFree(pContext
->pDispatchQueueArray
);
176 AlignedFree(pContext
->pMacroTileManagerArray
);
178 // Free scratch space.
179 for (uint32_t i
= 0; i
< pContext
->NumWorkerThreads
; ++i
)
182 VirtualFree(pContext
->pScratch
[i
], 0, MEM_RELEASE
);
184 AlignedFree(pContext
->pScratch
[i
]);
188 delete(pContext
->pHotTileMgr
);
190 pContext
->~SWR_CONTEXT();
191 AlignedFree(GetContext(hContext
));
194 void CopyState(DRAW_STATE
& dst
, const DRAW_STATE
& src
)
196 memcpy(&dst
.state
, &src
.state
, sizeof(API_STATE
));
199 void WakeAllThreads(SWR_CONTEXT
*pContext
)
201 pContext
->FifosNotEmpty
.notify_all();
204 template<bool IsDraw
>
205 void QueueWork(SWR_CONTEXT
*pContext
)
207 DRAW_CONTEXT
* pDC
= pContext
->pCurDrawContext
;
208 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
212 pDC
->pTileMgr
= &pContext
->pMacroTileManagerArray
[dcIndex
];
213 pDC
->pTileMgr
->initialize();
216 // Each worker thread looks at a DC for both FE and BE work at different times and so we
217 // multiply threadDone by 2. When the threadDone counter has reached 0 then all workers
218 // have moved past this DC. (i.e. Each worker has checked this DC for both FE and BE work and
219 // then moved on if all work is done.)
220 pContext
->pCurDrawContext
->threadsDone
= pContext
->NumFEThreads
+ pContext
->NumBEThreads
;
224 InterlockedIncrement((volatile LONG
*)&pContext
->drawsOutstandingFE
);
229 std::unique_lock
<std::mutex
> lock(pContext
->WaitLock
);
230 pContext
->dcRing
.Enqueue();
233 if (pContext
->threadInfo
.SINGLE_THREADED
)
235 // flush denormals to 0
236 uint32_t mxcsr
= _mm_getcsr();
237 _mm_setcsr(mxcsr
| _MM_FLUSH_ZERO_ON
| _MM_DENORMALS_ZERO_ON
);
241 uint32_t curDraw
[2] = { pContext
->pCurDrawContext
->drawId
, pContext
->pCurDrawContext
->drawId
};
242 WorkOnFifoFE(pContext
, 0, curDraw
[0]);
243 WorkOnFifoBE(pContext
, 0, curDraw
[1], pContext
->singleThreadLockedTiles
, 0, 0);
247 uint32_t curDispatch
= pContext
->pCurDrawContext
->drawId
;
248 WorkOnCompute(pContext
, 0, curDispatch
);
251 // Dequeue the work here, if not already done, since we're single threaded (i.e. no workers).
252 while (CompleteDrawContext(pContext
, pContext
->pCurDrawContext
) > 0) {}
259 RDTSC_START(APIDrawWakeAllThreads
);
260 WakeAllThreads(pContext
);
261 RDTSC_STOP(APIDrawWakeAllThreads
, 1, 0);
264 // Set current draw context to NULL so that next state call forces a new draw context to be created and populated.
265 pContext
->pPrevDrawContext
= pContext
->pCurDrawContext
;
266 pContext
->pCurDrawContext
= nullptr;
269 INLINE
void QueueDraw(SWR_CONTEXT
* pContext
)
271 QueueWork
<true>(pContext
);
274 INLINE
void QueueDispatch(SWR_CONTEXT
* pContext
)
276 QueueWork
<false>(pContext
);
279 DRAW_CONTEXT
* GetDrawContext(SWR_CONTEXT
*pContext
, bool isSplitDraw
= false)
281 RDTSC_START(APIGetDrawContext
);
282 // If current draw context is null then need to obtain a new draw context to use from ring.
283 if (pContext
->pCurDrawContext
== nullptr)
285 // Need to wait for a free entry.
286 while (pContext
->dcRing
.IsFull())
291 uint64_t curDraw
= pContext
->dcRing
.GetHead();
292 uint32_t dcIndex
= curDraw
% KNOB_MAX_DRAWS_IN_FLIGHT
;
294 if ((pContext
->frameCount
- pContext
->lastFrameChecked
) > 2 ||
295 (curDraw
- pContext
->lastDrawChecked
) > 0x10000)
297 // Take this opportunity to clean-up old arena allocations
298 pContext
->cachingArenaAllocator
.FreeOldBlocks();
300 pContext
->lastFrameChecked
= pContext
->frameCount
;
301 pContext
->lastDrawChecked
= curDraw
;
304 DRAW_CONTEXT
* pCurDrawContext
= &pContext
->dcRing
[dcIndex
];
305 pContext
->pCurDrawContext
= pCurDrawContext
;
307 // Assign next available entry in DS ring to this DC.
308 uint32_t dsIndex
= pContext
->curStateId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
309 pCurDrawContext
->pState
= &pContext
->dsRing
[dsIndex
];
311 // Copy previous state to current state.
312 if (pContext
->pPrevDrawContext
)
314 DRAW_CONTEXT
* pPrevDrawContext
= pContext
->pPrevDrawContext
;
316 // If we're splitting our draw then we can just use the same state from the previous
317 // draw. In this case, we won't increment the DS ring index so the next non-split
318 // draw can receive the state.
319 if (isSplitDraw
== false)
321 CopyState(*pCurDrawContext
->pState
, *pPrevDrawContext
->pState
);
323 // Should have been cleaned up previously
324 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
326 pCurDrawContext
->pState
->pPrivateState
= nullptr;
328 pContext
->curStateId
++; // Progress state ring index forward.
332 // If its a split draw then just copy the state pointer over
333 // since its the same draw.
334 pCurDrawContext
->pState
= pPrevDrawContext
->pState
;
335 SWR_ASSERT(pPrevDrawContext
->cleanupState
== false);
340 SWR_ASSERT(pCurDrawContext
->pState
->pArena
->IsEmpty() == true);
341 pContext
->curStateId
++; // Progress state ring index forward.
344 SWR_ASSERT(pCurDrawContext
->pArena
->IsEmpty() == true);
346 pCurDrawContext
->dependent
= false;
347 pCurDrawContext
->pContext
= pContext
;
348 pCurDrawContext
->isCompute
= false; // Dispatch has to set this to true.
350 pCurDrawContext
->doneFE
= false;
351 pCurDrawContext
->FeLock
= 0;
352 pCurDrawContext
->threadsDone
= 0;
353 pCurDrawContext
->retireCallback
.pfnCallbackFunc
= nullptr;
355 memset(&pCurDrawContext
->dynState
, 0, sizeof(pCurDrawContext
->dynState
));
357 // Assign unique drawId for this DC
358 pCurDrawContext
->drawId
= pContext
->dcRing
.GetHead();
360 pCurDrawContext
->cleanupState
= true;
364 SWR_ASSERT(isSplitDraw
== false, "Split draw should only be used when obtaining a new DC");
367 RDTSC_STOP(APIGetDrawContext
, 0, 0);
368 return pContext
->pCurDrawContext
;
371 API_STATE
* GetDrawState(SWR_CONTEXT
*pContext
)
373 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
374 SWR_ASSERT(pDC
->pState
!= nullptr);
376 return &pDC
->pState
->state
;
379 void SWR_API
SwrSaveState(
381 void* pOutputStateBlock
,
384 SWR_CONTEXT
*pContext
= GetContext(hContext
);
385 auto pSrc
= GetDrawState(pContext
);
386 SWR_ASSERT(pOutputStateBlock
&& memSize
>= sizeof(*pSrc
));
388 memcpy(pOutputStateBlock
, pSrc
, sizeof(*pSrc
));
391 void SWR_API
SwrRestoreState(
393 const void* pStateBlock
,
396 SWR_CONTEXT
*pContext
= GetContext(hContext
);
397 auto pDst
= GetDrawState(pContext
);
398 SWR_ASSERT(pStateBlock
&& memSize
>= sizeof(*pDst
));
400 memcpy(pDst
, pStateBlock
, sizeof(*pDst
));
403 void SetupDefaultState(SWR_CONTEXT
*pContext
)
405 API_STATE
* pState
= GetDrawState(pContext
);
407 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
408 pState
->rastState
.frontWinding
= SWR_FRONTWINDING_CCW
;
411 void SwrSync(HANDLE hContext
, PFN_CALLBACK_FUNC pfnFunc
, uint64_t userData
, uint64_t userData2
, uint64_t userData3
)
413 RDTSC_START(APISync
);
415 SWR_ASSERT(pfnFunc
!= nullptr);
417 SWR_CONTEXT
*pContext
= GetContext(hContext
);
418 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
420 pDC
->FeWork
.type
= SYNC
;
421 pDC
->FeWork
.pfnWork
= ProcessSync
;
423 // Setup callback function
424 pDC
->retireCallback
.pfnCallbackFunc
= pfnFunc
;
425 pDC
->retireCallback
.userData
= userData
;
426 pDC
->retireCallback
.userData2
= userData2
;
427 pDC
->retireCallback
.userData3
= userData3
;
432 RDTSC_STOP(APISync
, 1, 0);
435 void SwrWaitForIdle(HANDLE hContext
)
437 SWR_CONTEXT
*pContext
= GetContext(hContext
);
439 RDTSC_START(APIWaitForIdle
);
441 while (!pContext
->dcRing
.IsEmpty())
446 RDTSC_STOP(APIWaitForIdle
, 1, 0);
449 void SwrWaitForIdleFE(HANDLE hContext
)
451 SWR_CONTEXT
*pContext
= GetContext(hContext
);
453 RDTSC_START(APIWaitForIdle
);
455 while (pContext
->drawsOutstandingFE
> 0)
460 RDTSC_STOP(APIWaitForIdle
, 1, 0);
463 void SwrSetVertexBuffers(
466 const SWR_VERTEX_BUFFER_STATE
* pVertexBuffers
)
468 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
470 for (uint32_t i
= 0; i
< numBuffers
; ++i
)
472 const SWR_VERTEX_BUFFER_STATE
*pVB
= &pVertexBuffers
[i
];
473 pState
->vertexBuffers
[pVB
->index
] = *pVB
;
477 void SwrSetIndexBuffer(
479 const SWR_INDEX_BUFFER_STATE
* pIndexBuffer
)
481 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
483 pState
->indexBuffer
= *pIndexBuffer
;
486 void SwrSetFetchFunc(
488 PFN_FETCH_FUNC pfnFetchFunc
)
490 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
492 pState
->pfnFetchFunc
= pfnFetchFunc
;
497 PFN_SO_FUNC pfnSoFunc
,
498 uint32_t streamIndex
)
500 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
502 SWR_ASSERT(streamIndex
< MAX_SO_STREAMS
);
504 pState
->pfnSoFunc
[streamIndex
] = pfnSoFunc
;
509 SWR_STREAMOUT_STATE
* pSoState
)
511 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
513 pState
->soState
= *pSoState
;
516 void SwrSetSoBuffers(
518 SWR_STREAMOUT_BUFFER
* pSoBuffer
,
521 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
523 SWR_ASSERT((slot
< 4), "There are only 4 SO buffer slots [0, 3]\nSlot requested: %d", slot
);
525 pState
->soBuffer
[slot
] = *pSoBuffer
;
528 void SwrSetVertexFunc(
530 PFN_VERTEX_FUNC pfnVertexFunc
)
532 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
534 pState
->pfnVertexFunc
= pfnVertexFunc
;
537 void SwrSetFrontendState(
539 SWR_FRONTEND_STATE
*pFEState
)
541 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
542 pState
->frontendState
= *pFEState
;
547 SWR_GS_STATE
*pGSState
)
549 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
550 pState
->gsState
= *pGSState
;
555 PFN_GS_FUNC pfnGsFunc
)
557 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
558 pState
->pfnGsFunc
= pfnGsFunc
;
563 PFN_CS_FUNC pfnCsFunc
,
564 uint32_t totalThreadsInGroup
,
565 uint32_t totalSpillFillSize
)
567 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
568 pState
->pfnCsFunc
= pfnCsFunc
;
569 pState
->totalThreadsInGroup
= totalThreadsInGroup
;
570 pState
->totalSpillFillSize
= totalSpillFillSize
;
575 SWR_TS_STATE
*pState
)
577 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
578 pApiState
->tsState
= *pState
;
585 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
586 pApiState
->pfnHsFunc
= pfnFunc
;
593 API_STATE
* pApiState
= GetDrawState(GetContext(hContext
));
594 pApiState
->pfnDsFunc
= pfnFunc
;
597 void SwrSetDepthStencilState(
599 SWR_DEPTH_STENCIL_STATE
*pDSState
)
601 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
603 pState
->depthStencilState
= *pDSState
;
606 void SwrSetBackendState(
608 SWR_BACKEND_STATE
*pBEState
)
610 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
612 pState
->backendState
= *pBEState
;
615 void SwrSetPixelShaderState(
617 SWR_PS_STATE
*pPSState
)
619 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
620 pState
->psState
= *pPSState
;
623 void SwrSetBlendState(
625 SWR_BLEND_STATE
*pBlendState
)
627 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
628 memcpy(&pState
->blendState
, pBlendState
, sizeof(SWR_BLEND_STATE
));
631 void SwrSetBlendFunc(
633 uint32_t renderTarget
,
634 PFN_BLEND_JIT_FUNC pfnBlendFunc
)
636 SWR_ASSERT(renderTarget
< SWR_NUM_RENDERTARGETS
);
637 API_STATE
*pState
= GetDrawState(GetContext(hContext
));
638 pState
->pfnBlendFunc
[renderTarget
] = pfnBlendFunc
;
641 // update guardband multipliers for the viewport
642 void updateGuardband(API_STATE
*pState
)
644 // guardband center is viewport center
645 pState
->gbState
.left
= KNOB_GUARDBAND_WIDTH
/ pState
->vp
[0].width
;
646 pState
->gbState
.right
= KNOB_GUARDBAND_WIDTH
/ pState
->vp
[0].width
;
647 pState
->gbState
.top
= KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[0].height
;
648 pState
->gbState
.bottom
= KNOB_GUARDBAND_HEIGHT
/ pState
->vp
[0].height
;
651 void SwrSetRastState(
653 const SWR_RASTSTATE
*pRastState
)
655 SWR_CONTEXT
*pContext
= GetContext(hContext
);
656 API_STATE
* pState
= GetDrawState(pContext
);
658 memcpy(&pState
->rastState
, pRastState
, sizeof(SWR_RASTSTATE
));
661 void SwrSetViewports(
663 uint32_t numViewports
,
664 const SWR_VIEWPORT
* pViewports
,
665 const SWR_VIEWPORT_MATRICES
* pMatrices
)
667 SWR_ASSERT(numViewports
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
668 "Invalid number of viewports.");
670 SWR_CONTEXT
*pContext
= GetContext(hContext
);
671 API_STATE
* pState
= GetDrawState(pContext
);
673 memcpy(&pState
->vp
[0], pViewports
, sizeof(SWR_VIEWPORT
) * numViewports
);
675 if (pMatrices
!= nullptr)
677 // @todo Faster to copy portions of the SOA or just copy all of it?
678 memcpy(&pState
->vpMatrices
, pMatrices
, sizeof(SWR_VIEWPORT_MATRICES
));
682 // Compute default viewport transform.
683 for (uint32_t i
= 0; i
< numViewports
; ++i
)
685 if (pContext
->driverType
== DX
)
687 pState
->vpMatrices
.m00
[i
] = pState
->vp
[i
].width
/ 2.0f
;
688 pState
->vpMatrices
.m11
[i
] = -pState
->vp
[i
].height
/ 2.0f
;
689 pState
->vpMatrices
.m22
[i
] = pState
->vp
[i
].maxZ
- pState
->vp
[i
].minZ
;
690 pState
->vpMatrices
.m30
[i
] = pState
->vp
[i
].x
+ pState
->vpMatrices
.m00
[i
];
691 pState
->vpMatrices
.m31
[i
] = pState
->vp
[i
].y
- pState
->vpMatrices
.m11
[i
];
692 pState
->vpMatrices
.m32
[i
] = pState
->vp
[i
].minZ
;
696 // Standard, with the exception that Y is inverted.
697 pState
->vpMatrices
.m00
[i
] = (pState
->vp
[i
].width
- pState
->vp
[i
].x
) / 2.0f
;
698 pState
->vpMatrices
.m11
[i
] = (pState
->vp
[i
].y
- pState
->vp
[i
].height
) / 2.0f
;
699 pState
->vpMatrices
.m22
[i
] = (pState
->vp
[i
].maxZ
- pState
->vp
[i
].minZ
) / 2.0f
;
700 pState
->vpMatrices
.m30
[i
] = pState
->vp
[i
].x
+ pState
->vpMatrices
.m00
[i
];
701 pState
->vpMatrices
.m31
[i
] = pState
->vp
[i
].height
+ pState
->vpMatrices
.m11
[i
];
702 pState
->vpMatrices
.m32
[i
] = pState
->vp
[i
].minZ
+ pState
->vpMatrices
.m22
[i
];
704 // Now that the matrix is calculated, clip the view coords to screen size.
705 // OpenGL allows for -ve x,y in the viewport.
706 pState
->vp
[i
].x
= std::max(pState
->vp
[i
].x
, 0.0f
);
707 pState
->vp
[i
].y
= std::max(pState
->vp
[i
].y
, 0.0f
);
712 updateGuardband(pState
);
715 void SwrSetScissorRects(
717 uint32_t numScissors
,
718 const SWR_RECT
* pScissors
)
720 SWR_ASSERT(numScissors
<= KNOB_NUM_VIEWPORTS_SCISSORS
,
721 "Invalid number of scissor rects.");
723 API_STATE
* pState
= GetDrawState(GetContext(hContext
));
724 memcpy(&pState
->scissorRects
[0], pScissors
, numScissors
* sizeof(pScissors
[0]));
727 void SetupMacroTileScissors(DRAW_CONTEXT
*pDC
)
729 API_STATE
*pState
= &pDC
->pState
->state
;
730 uint32_t numScissors
= pState
->gsState
.emitsViewportArrayIndex
? KNOB_NUM_VIEWPORTS_SCISSORS
: 1;
731 pState
->scissorsTileAligned
= true;
733 for (uint32_t index
= 0; index
< numScissors
; ++index
)
735 SWR_RECT
&scissorInFixedPoint
= pState
->scissorsInFixedPoint
[index
];
737 // Set up scissor dimensions based on scissor or viewport
738 if (pState
->rastState
.scissorEnable
)
740 scissorInFixedPoint
= pState
->scissorRects
[index
];
744 // the vp width and height must be added to origin un-rounded then the result round to -inf.
745 // The cast to int works for rounding assuming all [left, right, top, bottom] are positive.
746 scissorInFixedPoint
.xmin
= (int32_t)pState
->vp
[index
].x
;
747 scissorInFixedPoint
.xmax
= (int32_t)(pState
->vp
[index
].x
+ pState
->vp
[index
].width
);
748 scissorInFixedPoint
.ymin
= (int32_t)pState
->vp
[index
].y
;
749 scissorInFixedPoint
.ymax
= (int32_t)(pState
->vp
[index
].y
+ pState
->vp
[index
].height
);
753 scissorInFixedPoint
&= g_MaxScissorRect
;
755 // Test for tile alignment
757 tileAligned
= (scissorInFixedPoint
.xmin
% KNOB_TILE_X_DIM
) == 0;
758 tileAligned
&= (scissorInFixedPoint
.ymin
% KNOB_TILE_Y_DIM
) == 0;
759 tileAligned
&= (scissorInFixedPoint
.xmax
% KNOB_TILE_X_DIM
) == 0;
760 tileAligned
&= (scissorInFixedPoint
.xmax
% KNOB_TILE_Y_DIM
) == 0;
762 pState
->scissorsTileAligned
&= tileAligned
;
764 // Scale to fixed point
765 scissorInFixedPoint
.xmin
*= FIXED_POINT_SCALE
;
766 scissorInFixedPoint
.xmax
*= FIXED_POINT_SCALE
;
767 scissorInFixedPoint
.ymin
*= FIXED_POINT_SCALE
;
768 scissorInFixedPoint
.ymax
*= FIXED_POINT_SCALE
;
770 // Make scissor inclusive
771 scissorInFixedPoint
.xmax
-= 1;
772 scissorInFixedPoint
.ymax
-= 1;
778 // templated backend function tables
779 extern PFN_BACKEND_FUNC gBackendNullPs
[SWR_MULTISAMPLE_TYPE_COUNT
];
780 extern PFN_BACKEND_FUNC gBackendSingleSample
[SWR_INPUT_COVERAGE_COUNT
][2][2];
781 extern PFN_BACKEND_FUNC gBackendPixelRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_MSAA_SAMPLE_PATTERN_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2][2];
782 extern PFN_BACKEND_FUNC gBackendSampleRateTable
[SWR_MULTISAMPLE_TYPE_COUNT
][SWR_INPUT_COVERAGE_COUNT
][2][2];
783 void SetupPipeline(DRAW_CONTEXT
*pDC
)
785 DRAW_STATE
* pState
= pDC
->pState
;
786 const SWR_RASTSTATE
&rastState
= pState
->state
.rastState
;
787 const SWR_PS_STATE
&psState
= pState
->state
.psState
;
788 BACKEND_FUNCS
& backendFuncs
= pState
->backendFuncs
;
789 const uint32_t forcedSampleCount
= (rastState
.forcedSampleCount
) ? 1 : 0;
792 if (psState
.pfnPixelShader
== nullptr)
794 backendFuncs
.pfnBackend
= gBackendNullPs
[pState
->state
.rastState
.sampleCount
];
798 const bool bMultisampleEnable
= ((rastState
.sampleCount
> SWR_MULTISAMPLE_1X
) || rastState
.forcedSampleCount
) ? 1 : 0;
799 const uint32_t centroid
= ((psState
.barycentricsMask
& SWR_BARYCENTRIC_CENTROID_MASK
) > 0) ? 1 : 0;
800 const uint32_t canEarlyZ
= (psState
.forceEarlyZ
|| (!psState
.writesODepth
&& !psState
.usesSourceDepth
&& !psState
.usesUAV
)) ? 1 : 0;
802 SWR_BARYCENTRICS_MASK barycentricsMask
= (SWR_BARYCENTRICS_MASK
)psState
.barycentricsMask
;
804 // select backend function
805 switch(psState
.shadingRate
)
807 case SWR_SHADING_RATE_PIXEL
:
808 if(bMultisampleEnable
)
810 // always need to generate I & J per sample for Z interpolation
811 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
812 backendFuncs
.pfnBackend
= gBackendPixelRateTable
[rastState
.sampleCount
][rastState
.samplePattern
][psState
.inputCoverage
][centroid
][forcedSampleCount
][canEarlyZ
];
816 // always need to generate I & J per pixel for Z interpolation
817 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_PIXEL_MASK
);
818 backendFuncs
.pfnBackend
= gBackendSingleSample
[psState
.inputCoverage
][centroid
][canEarlyZ
];
821 case SWR_SHADING_RATE_SAMPLE
:
822 SWR_ASSERT(rastState
.samplePattern
== SWR_MSAA_STANDARD_PATTERN
);
823 // always need to generate I & J per sample for Z interpolation
824 barycentricsMask
= (SWR_BARYCENTRICS_MASK
)(barycentricsMask
| SWR_BARYCENTRIC_PER_SAMPLE_MASK
);
825 backendFuncs
.pfnBackend
= gBackendSampleRateTable
[rastState
.sampleCount
][psState
.inputCoverage
][centroid
][canEarlyZ
];
828 SWR_ASSERT(0 && "Invalid shading rate");
833 PFN_PROCESS_PRIMS pfnBinner
;
834 switch (pState
->state
.topology
)
837 pState
->pfnProcessPrims
= ClipPoints
;
838 pfnBinner
= BinPoints
;
843 case TOP_LINE_LIST_ADJ
:
844 case TOP_LISTSTRIP_ADJ
:
845 pState
->pfnProcessPrims
= ClipLines
;
846 pfnBinner
= BinLines
;
849 pState
->pfnProcessPrims
= ClipTriangles
;
850 pfnBinner
= GetBinTrianglesFunc((rastState
.conservativeRast
> 0));
854 // disable clipper if viewport transform is disabled
855 if (pState
->state
.frontendState
.vpTransformDisable
)
857 pState
->pfnProcessPrims
= pfnBinner
;
860 if ((pState
->state
.psState
.pfnPixelShader
== nullptr) &&
861 (pState
->state
.depthStencilState
.depthTestEnable
== FALSE
) &&
862 (pState
->state
.depthStencilState
.depthWriteEnable
== FALSE
) &&
863 (pState
->state
.depthStencilState
.stencilTestEnable
== FALSE
) &&
864 (pState
->state
.depthStencilState
.stencilWriteEnable
== FALSE
) &&
865 (pState
->state
.backendState
.numAttributes
== 0))
867 pState
->pfnProcessPrims
= nullptr;
870 if (pState
->state
.soState
.rasterizerDisable
== true)
872 pState
->pfnProcessPrims
= nullptr;
875 // set up the frontend attribute count
876 pState
->state
.feNumAttributes
= 0;
877 const SWR_BACKEND_STATE
& backendState
= pState
->state
.backendState
;
878 if (backendState
.swizzleEnable
)
880 // attribute swizzling is enabled, iterate over the map and record the max attribute used
881 for (uint32_t i
= 0; i
< backendState
.numAttributes
; ++i
)
883 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)backendState
.swizzleMap
[i
].sourceAttrib
+ 1);
888 pState
->state
.feNumAttributes
= pState
->state
.backendState
.numAttributes
;
891 if (pState
->state
.soState
.soEnable
)
893 uint32_t streamMasks
= 0;
894 for (uint32_t i
= 0; i
< 4; ++i
)
896 streamMasks
|= pState
->state
.soState
.streamMasks
[i
];
900 if (_BitScanReverse(&maxAttrib
, streamMasks
))
902 pState
->state
.feNumAttributes
= std::max(pState
->state
.feNumAttributes
, (uint32_t)(maxAttrib
+ 1));
906 // complicated logic to test for cases where we don't need backing hottile memory for a draw
907 // have to check for the special case where depth/stencil test is enabled but depthwrite is disabled.
908 pState
->state
.depthHottileEnable
= ((!(pState
->state
.depthStencilState
.depthTestEnable
&&
909 !pState
->state
.depthStencilState
.depthWriteEnable
&&
910 pState
->state
.depthStencilState
.depthTestFunc
== ZFUNC_ALWAYS
)) &&
911 (pState
->state
.depthStencilState
.depthTestEnable
||
912 pState
->state
.depthStencilState
.depthWriteEnable
)) ? true : false;
914 pState
->state
.stencilHottileEnable
= (((!(pState
->state
.depthStencilState
.stencilTestEnable
&&
915 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
916 pState
->state
.depthStencilState
.stencilTestFunc
== ZFUNC_ALWAYS
)) ||
917 // for stencil we have to check the double sided state as well
918 (!(pState
->state
.depthStencilState
.doubleSidedStencilTestEnable
&&
919 !pState
->state
.depthStencilState
.stencilWriteEnable
&&
920 pState
->state
.depthStencilState
.backfaceStencilTestFunc
== ZFUNC_ALWAYS
))) &&
921 (pState
->state
.depthStencilState
.stencilTestEnable
||
922 pState
->state
.depthStencilState
.stencilWriteEnable
)) ? true : false;
924 uint32_t numRTs
= pState
->state
.psState
.numRenderTargets
;
925 pState
->state
.colorHottileEnable
= 0;
926 if (psState
.pfnPixelShader
!= nullptr)
928 for (uint32_t rt
= 0; rt
< numRTs
; ++rt
)
930 pState
->state
.colorHottileEnable
|=
931 (!pState
->state
.blendState
.renderTarget
[rt
].writeDisableAlpha
||
932 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableRed
||
933 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableGreen
||
934 !pState
->state
.blendState
.renderTarget
[rt
].writeDisableBlue
) ? (1 << rt
) : 0;
938 // Setup depth quantization function
939 if (pState
->state
.depthHottileEnable
)
941 switch (pState
->state
.rastState
.depthFormat
)
943 case R32_FLOAT_X8X24_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT_X8X24_TYPELESS
> ; break;
944 case R32_FLOAT
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ; break;
945 case R24_UNORM_X8_TYPELESS
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R24_UNORM_X8_TYPELESS
> ; break;
946 case R16_UNORM
: pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R16_UNORM
> ; break;
947 default: SWR_ASSERT(false, "Unsupported depth format for depth quantiztion.");
948 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
953 // set up pass-through quantize if depth isn't enabled
954 pState
->state
.pfnQuantizeDepth
= QuantizeDepth
< R32_FLOAT
> ;
958 //////////////////////////////////////////////////////////////////////////
960 /// @param pDC - Draw context to initialize for this draw.
965 // We don't need to re-setup the scissors/pipeline state again for split draw.
966 if (isSplitDraw
== false)
968 SetupMacroTileScissors(pDC
);
973 //////////////////////////////////////////////////////////////////////////
974 /// @brief We can split the draw for certain topologies for better performance.
975 /// @param totalVerts - Total vertices for draw
976 /// @param topology - Topology used for draw
977 uint32_t MaxVertsPerDraw(
980 PRIMITIVE_TOPOLOGY topology
)
982 API_STATE
& state
= pDC
->pState
->state
;
984 uint32_t vertsPerDraw
= totalVerts
;
986 if (state
.soState
.soEnable
)
994 case TOP_TRIANGLE_LIST
:
995 vertsPerDraw
= KNOB_MAX_PRIMS_PER_DRAW
;
998 case TOP_PATCHLIST_1
:
999 case TOP_PATCHLIST_2
:
1000 case TOP_PATCHLIST_3
:
1001 case TOP_PATCHLIST_4
:
1002 case TOP_PATCHLIST_5
:
1003 case TOP_PATCHLIST_6
:
1004 case TOP_PATCHLIST_7
:
1005 case TOP_PATCHLIST_8
:
1006 case TOP_PATCHLIST_9
:
1007 case TOP_PATCHLIST_10
:
1008 case TOP_PATCHLIST_11
:
1009 case TOP_PATCHLIST_12
:
1010 case TOP_PATCHLIST_13
:
1011 case TOP_PATCHLIST_14
:
1012 case TOP_PATCHLIST_15
:
1013 case TOP_PATCHLIST_16
:
1014 case TOP_PATCHLIST_17
:
1015 case TOP_PATCHLIST_18
:
1016 case TOP_PATCHLIST_19
:
1017 case TOP_PATCHLIST_20
:
1018 case TOP_PATCHLIST_21
:
1019 case TOP_PATCHLIST_22
:
1020 case TOP_PATCHLIST_23
:
1021 case TOP_PATCHLIST_24
:
1022 case TOP_PATCHLIST_25
:
1023 case TOP_PATCHLIST_26
:
1024 case TOP_PATCHLIST_27
:
1025 case TOP_PATCHLIST_28
:
1026 case TOP_PATCHLIST_29
:
1027 case TOP_PATCHLIST_30
:
1028 case TOP_PATCHLIST_31
:
1029 case TOP_PATCHLIST_32
:
1030 if (pDC
->pState
->state
.tsState
.tsEnable
)
1032 uint32_t vertsPerPrim
= topology
- TOP_PATCHLIST_BASE
;
1033 vertsPerDraw
= vertsPerPrim
* KNOB_MAX_TESS_PRIMS_PER_DRAW
;
1037 // The Primitive Assembly code can only handle 1 RECT at a time.
1043 // We are not splitting up draws for other topologies.
1047 return vertsPerDraw
;
1051 //////////////////////////////////////////////////////////////////////////
1052 /// @brief DrawInstanced
1053 /// @param hContext - Handle passed back from SwrCreateContext
1054 /// @param topology - Specifies topology for draw.
1055 /// @param numVerts - How many vertices to read sequentially from vertex data (per instance).
1056 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1057 /// @param numInstances - How many instances to render.
1058 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1061 PRIMITIVE_TOPOLOGY topology
,
1062 uint32_t numVertices
,
1063 uint32_t startVertex
,
1064 uint32_t numInstances
= 1,
1065 uint32_t startInstance
= 0)
1072 RDTSC_START(APIDraw
);
1074 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1075 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1077 uint32_t maxVertsPerDraw
= MaxVertsPerDraw(pDC
, numVertices
, topology
);
1078 uint32_t primsPerDraw
= GetNumPrims(topology
, maxVertsPerDraw
);
1079 uint32_t remainingVerts
= numVertices
;
1081 API_STATE
*pState
= &pDC
->pState
->state
;
1082 pState
->topology
= topology
;
1083 pState
->forceFront
= false;
1085 // disable culling for points/lines
1086 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1087 if (topology
== TOP_POINT_LIST
)
1089 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1090 pState
->forceFront
= true;
1094 while (remainingVerts
)
1096 uint32_t numVertsForDraw
= (remainingVerts
< maxVertsPerDraw
) ?
1097 remainingVerts
: maxVertsPerDraw
;
1099 bool isSplitDraw
= (draw
> 0) ? true : false;
1100 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
, isSplitDraw
);
1101 InitDraw(pDC
, isSplitDraw
);
1103 pDC
->FeWork
.type
= DRAW
;
1104 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1106 false, // bEnableCutIndex
1107 pState
->tsState
.tsEnable
,
1108 pState
->gsState
.gsEnable
,
1109 pState
->soState
.soEnable
,
1110 pDC
->pState
->pfnProcessPrims
!= nullptr);
1111 pDC
->FeWork
.desc
.draw
.numVerts
= numVertsForDraw
;
1112 pDC
->FeWork
.desc
.draw
.startVertex
= startVertex
;
1113 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1114 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1115 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1116 pDC
->FeWork
.desc
.draw
.startVertexID
= draw
* maxVertsPerDraw
;
1118 pDC
->cleanupState
= (remainingVerts
== numVertsForDraw
);
1121 QueueDraw(pContext
);
1123 remainingVerts
-= numVertsForDraw
;
1127 // restore culling state
1128 pDC
= GetDrawContext(pContext
);
1129 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1131 RDTSC_STOP(APIDraw
, numVertices
* numInstances
, 0);
1134 //////////////////////////////////////////////////////////////////////////
1136 /// @param hContext - Handle passed back from SwrCreateContext
1137 /// @param topology - Specifies topology for draw.
1138 /// @param startVertex - Specifies start vertex in vertex buffer for draw.
1139 /// @param primCount - Number of vertices.
1142 PRIMITIVE_TOPOLOGY topology
,
1143 uint32_t startVertex
,
1144 uint32_t numVertices
)
1146 DrawInstanced(hContext
, topology
, numVertices
, startVertex
);
1149 //////////////////////////////////////////////////////////////////////////
1150 /// @brief SwrDrawInstanced
1151 /// @param hContext - Handle passed back from SwrCreateContext
1152 /// @param topology - Specifies topology for draw.
1153 /// @param numVertsPerInstance - How many vertices to read sequentially from vertex data.
1154 /// @param numInstances - How many instances to render.
1155 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1156 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1157 void SwrDrawInstanced(
1159 PRIMITIVE_TOPOLOGY topology
,
1160 uint32_t numVertsPerInstance
,
1161 uint32_t numInstances
,
1162 uint32_t startVertex
,
1163 uint32_t startInstance
1166 DrawInstanced(hContext
, topology
, numVertsPerInstance
, startVertex
, numInstances
, startInstance
);
1169 //////////////////////////////////////////////////////////////////////////
1170 /// @brief DrawIndexedInstanced
1171 /// @param hContext - Handle passed back from SwrCreateContext
1172 /// @param topology - Specifies topology for draw.
1173 /// @param numIndices - Number of indices to read sequentially from index buffer.
1174 /// @param indexOffset - Starting index into index buffer.
1175 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1176 /// @param numInstances - Number of instances to render.
1177 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1178 void DrawIndexedInstance(
1180 PRIMITIVE_TOPOLOGY topology
,
1181 uint32_t numIndices
,
1182 uint32_t indexOffset
,
1184 uint32_t numInstances
= 1,
1185 uint32_t startInstance
= 0)
1192 RDTSC_START(APIDrawIndexed
);
1194 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1195 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1196 API_STATE
* pState
= &pDC
->pState
->state
;
1198 uint32_t maxIndicesPerDraw
= MaxVertsPerDraw(pDC
, numIndices
, topology
);
1199 uint32_t primsPerDraw
= GetNumPrims(topology
, maxIndicesPerDraw
);
1200 uint32_t remainingIndices
= numIndices
;
1202 uint32_t indexSize
= 0;
1203 switch (pState
->indexBuffer
.format
)
1205 case R32_UINT
: indexSize
= sizeof(uint32_t); break;
1206 case R16_UINT
: indexSize
= sizeof(uint16_t); break;
1207 case R8_UINT
: indexSize
= sizeof(uint8_t); break;
1213 uint8_t *pIB
= (uint8_t*)pState
->indexBuffer
.pIndices
;
1214 pIB
+= (uint64_t)indexOffset
* (uint64_t)indexSize
;
1216 pState
->topology
= topology
;
1217 pState
->forceFront
= false;
1219 // disable culling for points/lines
1220 uint32_t oldCullMode
= pState
->rastState
.cullMode
;
1221 if (topology
== TOP_POINT_LIST
)
1223 pState
->rastState
.cullMode
= SWR_CULLMODE_NONE
;
1224 pState
->forceFront
= true;
1227 while (remainingIndices
)
1229 uint32_t numIndicesForDraw
= (remainingIndices
< maxIndicesPerDraw
) ?
1230 remainingIndices
: maxIndicesPerDraw
;
1232 // When breaking up draw, we need to obtain new draw context for each iteration.
1233 bool isSplitDraw
= (draw
> 0) ? true : false;
1234 pDC
= GetDrawContext(pContext
, isSplitDraw
);
1235 InitDraw(pDC
, isSplitDraw
);
1237 pDC
->FeWork
.type
= DRAW
;
1238 pDC
->FeWork
.pfnWork
= GetProcessDrawFunc(
1240 pState
->frontendState
.bEnableCutIndex
,
1241 pState
->tsState
.tsEnable
,
1242 pState
->gsState
.gsEnable
,
1243 pState
->soState
.soEnable
,
1244 pDC
->pState
->pfnProcessPrims
!= nullptr);
1245 pDC
->FeWork
.desc
.draw
.pDC
= pDC
;
1246 pDC
->FeWork
.desc
.draw
.numIndices
= numIndicesForDraw
;
1247 pDC
->FeWork
.desc
.draw
.pIB
= (int*)pIB
;
1248 pDC
->FeWork
.desc
.draw
.type
= pDC
->pState
->state
.indexBuffer
.format
;
1250 pDC
->FeWork
.desc
.draw
.numInstances
= numInstances
;
1251 pDC
->FeWork
.desc
.draw
.startInstance
= startInstance
;
1252 pDC
->FeWork
.desc
.draw
.baseVertex
= baseVertex
;
1253 pDC
->FeWork
.desc
.draw
.startPrimID
= draw
* primsPerDraw
;
1255 pDC
->cleanupState
= (remainingIndices
== numIndicesForDraw
);
1258 QueueDraw(pContext
);
1260 pIB
+= maxIndicesPerDraw
* indexSize
;
1261 remainingIndices
-= numIndicesForDraw
;
1265 // restore culling state
1266 pDC
= GetDrawContext(pContext
);
1267 pDC
->pState
->state
.rastState
.cullMode
= oldCullMode
;
1269 RDTSC_STOP(APIDrawIndexed
, numIndices
* numInstances
, 0);
1273 //////////////////////////////////////////////////////////////////////////
1274 /// @brief DrawIndexed
1275 /// @param hContext - Handle passed back from SwrCreateContext
1276 /// @param topology - Specifies topology for draw.
1277 /// @param numIndices - Number of indices to read sequentially from index buffer.
1278 /// @param indexOffset - Starting index into index buffer.
1279 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1280 void SwrDrawIndexed(
1282 PRIMITIVE_TOPOLOGY topology
,
1283 uint32_t numIndices
,
1284 uint32_t indexOffset
,
1288 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
);
1291 //////////////////////////////////////////////////////////////////////////
1292 /// @brief SwrDrawIndexedInstanced
1293 /// @param hContext - Handle passed back from SwrCreateContext
1294 /// @param topology - Specifies topology for draw.
1295 /// @param numIndices - Number of indices to read sequentially from index buffer.
1296 /// @param numInstances - Number of instances to render.
1297 /// @param indexOffset - Starting index into index buffer.
1298 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1299 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1300 void SwrDrawIndexedInstanced(
1302 PRIMITIVE_TOPOLOGY topology
,
1303 uint32_t numIndices
,
1304 uint32_t numInstances
,
1305 uint32_t indexOffset
,
1307 uint32_t startInstance
)
1309 DrawIndexedInstance(hContext
, topology
, numIndices
, indexOffset
, baseVertex
, numInstances
, startInstance
);
1312 //////////////////////////////////////////////////////////////////////////
1313 /// @brief SwrInvalidateTiles
1314 /// @param hContext - Handle passed back from SwrCreateContext
1315 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to invalidate.
1316 /// @param invalidateRect - The pixel-coordinate rectangle to invalidate. This will be expanded to
1317 /// be hottile size-aligned.
1318 void SWR_API
SwrInvalidateTiles(
1320 uint32_t attachmentMask
,
1321 const SWR_RECT
& invalidateRect
)
1328 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1329 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1331 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1332 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1333 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1334 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= invalidateRect
;
1335 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1336 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_INVALID
;
1337 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= false;
1338 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= false;
1341 QueueDraw(pContext
);
1344 //////////////////////////////////////////////////////////////////////////
1345 /// @brief SwrDiscardRect
1346 /// @param hContext - Handle passed back from SwrCreateContext
1347 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to discard.
1348 /// @param rect - The pixel-coordinate rectangle to discard. Only fully-covered hottiles will be
1350 void SWR_API
SwrDiscardRect(
1352 uint32_t attachmentMask
,
1353 const SWR_RECT
& rect
)
1360 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1361 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1363 // Queue a load to the hottile
1364 pDC
->FeWork
.type
= DISCARDINVALIDATETILES
;
1365 pDC
->FeWork
.pfnWork
= ProcessDiscardInvalidateTiles
;
1366 pDC
->FeWork
.desc
.discardInvalidateTiles
.attachmentMask
= attachmentMask
;
1367 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
= rect
;
1368 pDC
->FeWork
.desc
.discardInvalidateTiles
.rect
&= g_MaxScissorRect
;
1369 pDC
->FeWork
.desc
.discardInvalidateTiles
.newTileState
= SWR_TILE_RESOLVED
;
1370 pDC
->FeWork
.desc
.discardInvalidateTiles
.createNewTiles
= true;
1371 pDC
->FeWork
.desc
.discardInvalidateTiles
.fullTilesOnly
= true;
1374 QueueDraw(pContext
);
1377 //////////////////////////////////////////////////////////////////////////
1378 /// @brief SwrDispatch
1379 /// @param hContext - Handle passed back from SwrCreateContext
1380 /// @param threadGroupCountX - Number of thread groups dispatched in X direction
1381 /// @param threadGroupCountY - Number of thread groups dispatched in Y direction
1382 /// @param threadGroupCountZ - Number of thread groups dispatched in Z direction
1385 uint32_t threadGroupCountX
,
1386 uint32_t threadGroupCountY
,
1387 uint32_t threadGroupCountZ
)
1394 RDTSC_START(APIDispatch
);
1395 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1396 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1398 pDC
->isCompute
= true; // This is a compute context.
1400 COMPUTE_DESC
* pTaskData
= (COMPUTE_DESC
*)pDC
->pArena
->AllocAligned(sizeof(COMPUTE_DESC
), 64);
1402 pTaskData
->threadGroupCountX
= threadGroupCountX
;
1403 pTaskData
->threadGroupCountY
= threadGroupCountY
;
1404 pTaskData
->threadGroupCountZ
= threadGroupCountZ
;
1406 uint32_t totalThreadGroups
= threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
;
1407 uint32_t dcIndex
= pDC
->drawId
% KNOB_MAX_DRAWS_IN_FLIGHT
;
1408 pDC
->pDispatch
= &pContext
->pDispatchQueueArray
[dcIndex
];
1409 pDC
->pDispatch
->initialize(totalThreadGroups
, pTaskData
);
1411 QueueDispatch(pContext
);
1412 RDTSC_STOP(APIDispatch
, threadGroupCountX
* threadGroupCountY
* threadGroupCountZ
, 0);
1415 // Deswizzles, converts and stores current contents of the hot tiles to surface
1416 // described by pState
1417 void SWR_API
SwrStoreTiles(
1419 SWR_RENDERTARGET_ATTACHMENT attachment
,
1420 SWR_TILE_STATE postStoreTileState
,
1421 const SWR_RECT
& storeRect
)
1428 RDTSC_START(APIStoreTiles
);
1430 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1431 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1433 pDC
->FeWork
.type
= STORETILES
;
1434 pDC
->FeWork
.pfnWork
= ProcessStoreTiles
;
1435 pDC
->FeWork
.desc
.storeTiles
.attachment
= attachment
;
1436 pDC
->FeWork
.desc
.storeTiles
.postStoreTileState
= postStoreTileState
;
1437 pDC
->FeWork
.desc
.storeTiles
.rect
= storeRect
;
1438 pDC
->FeWork
.desc
.storeTiles
.rect
&= g_MaxScissorRect
;
1441 QueueDraw(pContext
);
1443 RDTSC_STOP(APIStoreTiles
, 0, 0);
1446 //////////////////////////////////////////////////////////////////////////
1447 /// @brief SwrClearRenderTarget - Clear attached render targets / depth / stencil
1448 /// @param hContext - Handle passed back from SwrCreateContext
1449 /// @param clearMask - combination of SWR_CLEAR_COLOR / SWR_CLEAR_DEPTH / SWR_CLEAR_STENCIL flags (or SWR_CLEAR_NONE)
1450 /// @param clearColor - color use for clearing render targets
1451 /// @param z - depth value use for clearing depth buffer
1452 /// @param stencil - stencil value used for clearing stencil buffer
1453 /// @param clearRect - The pixel-coordinate rectangle to clear in all cleared buffers
1454 void SWR_API
SwrClearRenderTarget(
1457 const float clearColor
[4],
1460 const SWR_RECT
& clearRect
)
1467 RDTSC_START(APIClearRenderTarget
);
1469 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1470 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1474 flags
.mask
= clearMask
;
1476 pDC
->FeWork
.type
= CLEAR
;
1477 pDC
->FeWork
.pfnWork
= ProcessClear
;
1478 pDC
->FeWork
.desc
.clear
.rect
= clearRect
;
1479 pDC
->FeWork
.desc
.clear
.rect
&= g_MaxScissorRect
;
1480 pDC
->FeWork
.desc
.clear
.flags
= flags
;
1481 pDC
->FeWork
.desc
.clear
.clearDepth
= z
;
1482 pDC
->FeWork
.desc
.clear
.clearRTColor
[0] = clearColor
[0];
1483 pDC
->FeWork
.desc
.clear
.clearRTColor
[1] = clearColor
[1];
1484 pDC
->FeWork
.desc
.clear
.clearRTColor
[2] = clearColor
[2];
1485 pDC
->FeWork
.desc
.clear
.clearRTColor
[3] = clearColor
[3];
1486 pDC
->FeWork
.desc
.clear
.clearStencil
= stencil
;
1489 QueueDraw(pContext
);
1491 RDTSC_STOP(APIClearRenderTarget
, 0, pDC
->drawId
);
1494 //////////////////////////////////////////////////////////////////////////
1495 /// @brief Returns a pointer to the private context state for the current
1496 /// draw operation. This is used for external componets such as the
1498 /// SWR is responsible for the allocation of the private context state.
1499 /// @param hContext - Handle passed back from SwrCreateContext
1500 VOID
* SwrGetPrivateContextState(
1503 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1504 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1505 DRAW_STATE
* pState
= pDC
->pState
;
1507 if (pState
->pPrivateState
== nullptr)
1509 pState
->pPrivateState
= pState
->pArena
->AllocAligned(pContext
->privateStateSize
, KNOB_SIMD_WIDTH
*sizeof(float));
1512 return pState
->pPrivateState
;
1515 //////////////////////////////////////////////////////////////////////////
1516 /// @brief Clients can use this to allocate memory for draw/dispatch
1517 /// operations. The memory will automatically be freed once operation
1518 /// has completed. Client can use this to allocate binding tables,
1519 /// etc. needed for shader execution.
1520 /// @param hContext - Handle passed back from SwrCreateContext
1521 /// @param size - Size of allocation
1522 /// @param align - Alignment needed for allocation.
1523 VOID
* SwrAllocDrawContextMemory(
1528 SWR_CONTEXT
* pContext
= GetContext(hContext
);
1529 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1531 return pDC
->pState
->pArena
->AllocAligned(size
, align
);
1534 //////////////////////////////////////////////////////////////////////////
1535 /// @brief Enables stats counting
1536 /// @param hContext - Handle passed back from SwrCreateContext
1537 /// @param enable - If true then counts are incremented.
1538 void SwrEnableStats(
1542 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1543 DRAW_CONTEXT
* pDC
= GetDrawContext(pContext
);
1545 pDC
->pState
->state
.enableStats
= enable
;
1548 //////////////////////////////////////////////////////////////////////////
1549 /// @brief Mark end of frame - used for performance profiling
1550 /// @param hContext - Handle passed back from SwrCreateContext
1551 void SWR_API
SwrEndFrame(
1555 SWR_CONTEXT
*pContext
= GetContext(hContext
);
1556 pContext
->frameCount
++;