swr: [rasterizer core] add SwrWaitForIdleFE
[mesa.git] / src / gallium / drivers / swr / rasterizer / core / api.cpp
1 /****************************************************************************
2 * Copyright (C) 2014-2016 Intel Corporation. All Rights Reserved.
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * @file api.cpp
24 *
25 * @brief API implementation
26 *
27 ******************************************************************************/
28
29 #include <cfloat>
30 #include <cmath>
31 #include <cstdio>
32 #include <new>
33
34 #include "core/api.h"
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"
45
46 #include "common/simdintrin.h"
47 #include "common/os.h"
48
49 void SetupDefaultState(SWR_CONTEXT *pContext);
50
51 static INLINE SWR_CONTEXT* GetContext(HANDLE hContext)
52 {
53 return (SWR_CONTEXT*)hContext;
54 }
55
56 //////////////////////////////////////////////////////////////////////////
57 /// @brief Create SWR Context.
58 /// @param pCreateInfo - pointer to creation info.
59 HANDLE SwrCreateContext(
60 SWR_CREATECONTEXT_INFO* pCreateInfo)
61 {
62 RDTSC_RESET();
63 RDTSC_INIT(0);
64
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();
68
69 pContext->driverType = pCreateInfo->driver;
70 pContext->privateStateSize = pCreateInfo->privateStateSize;
71
72 pContext->dcRing.Init(KNOB_MAX_DRAWS_IN_FLIGHT);
73 pContext->dsRing.Init(KNOB_MAX_DRAWS_IN_FLIGHT);
74
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);
77
78 for (uint32_t dc = 0; dc < KNOB_MAX_DRAWS_IN_FLIGHT; ++dc)
79 {
80 pContext->dcRing[dc].pArena = new CachingArena(pContext->cachingArenaAllocator);
81 new (&pContext->pMacroTileManagerArray[dc]) MacroTileMgr(*pContext->dcRing[dc].pArena);
82 new (&pContext->pDispatchQueueArray[dc]) DispatchQueue();
83
84 pContext->dsRing[dc].pArena = new CachingArena(pContext->cachingArenaAllocator);
85 }
86
87 if (!KNOB_SINGLE_THREADED)
88 {
89 memset(&pContext->WaitLock, 0, sizeof(pContext->WaitLock));
90 memset(&pContext->FifosNotEmpty, 0, sizeof(pContext->FifosNotEmpty));
91 new (&pContext->WaitLock) std::mutex();
92 new (&pContext->FifosNotEmpty) std::condition_variable();
93
94 CreateThreadPool(pContext, &pContext->threadPool);
95 }
96
97 // Calling createThreadPool() above can set SINGLE_THREADED
98 if (KNOB_SINGLE_THREADED)
99 {
100 SET_KNOB(HYPERTHREADED_FE, false);
101 pContext->NumWorkerThreads = 1;
102 pContext->NumFEThreads = 1;
103 pContext->NumBEThreads = 1;
104 }
105
106 // Allocate scratch space for workers.
107 ///@note We could lazily allocate this but its rather small amount of memory.
108 for (uint32_t i = 0; i < pContext->NumWorkerThreads; ++i)
109 {
110 #if defined(_WIN32)
111 uint32_t numaNode = pContext->threadPool.pThreadData ?
112 pContext->threadPool.pThreadData[i].numaId : 0;
113 pContext->pScratch[i] = (uint8_t*)VirtualAllocExNuma(
114 GetCurrentProcess(), nullptr, 32 * sizeof(KILOBYTE),
115 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE,
116 numaNode);
117 #else
118 pContext->pScratch[i] = (uint8_t*)AlignedMalloc(32 * sizeof(KILOBYTE), KNOB_SIMD_WIDTH * 4);
119 #endif
120 }
121
122 // State setup AFTER context is fully initialized
123 SetupDefaultState(pContext);
124
125 // initialize hot tile manager
126 pContext->pHotTileMgr = new HotTileMgr();
127
128 // initialize function pointer tables
129 InitClearTilesTable();
130
131 // initialize callback functions
132 pContext->pfnLoadTile = pCreateInfo->pfnLoadTile;
133 pContext->pfnStoreTile = pCreateInfo->pfnStoreTile;
134 pContext->pfnClearTile = pCreateInfo->pfnClearTile;
135 pContext->pfnUpdateSoWriteOffset = pCreateInfo->pfnUpdateSoWriteOffset;
136
137 // pass pointer to bucket manager back to caller
138 #ifdef KNOB_ENABLE_RDTSC
139 pCreateInfo->pBucketMgr = &gBucketMgr;
140 #endif
141
142 pCreateInfo->contextSaveSize = sizeof(API_STATE);
143
144 return (HANDLE)pContext;
145 }
146
147 void SwrDestroyContext(HANDLE hContext)
148 {
149 SWR_CONTEXT *pContext = GetContext(hContext);
150 DestroyThreadPool(pContext, &pContext->threadPool);
151
152 // free the fifos
153 for (uint32_t i = 0; i < KNOB_MAX_DRAWS_IN_FLIGHT; ++i)
154 {
155 delete pContext->dcRing[i].pArena;
156 delete pContext->dsRing[i].pArena;
157 pContext->pMacroTileManagerArray[i].~MacroTileMgr();
158 pContext->pDispatchQueueArray[i].~DispatchQueue();
159 }
160
161 AlignedFree(pContext->pDispatchQueueArray);
162 AlignedFree(pContext->pMacroTileManagerArray);
163
164 // Free scratch space.
165 for (uint32_t i = 0; i < pContext->NumWorkerThreads; ++i)
166 {
167 #if defined(_WIN32)
168 VirtualFree(pContext->pScratch[i], 0, MEM_RELEASE);
169 #else
170 AlignedFree(pContext->pScratch[i]);
171 #endif
172 }
173
174 delete(pContext->pHotTileMgr);
175
176 pContext->~SWR_CONTEXT();
177 AlignedFree(GetContext(hContext));
178 }
179
180 void CopyState(DRAW_STATE& dst, const DRAW_STATE& src)
181 {
182 memcpy(&dst.state, &src.state, sizeof(API_STATE));
183 }
184
185 void WakeAllThreads(SWR_CONTEXT *pContext)
186 {
187 pContext->FifosNotEmpty.notify_all();
188 }
189
190 static TileSet gSingleThreadLockedTiles;
191
192 template<bool IsDraw>
193 void QueueWork(SWR_CONTEXT *pContext)
194 {
195 DRAW_CONTEXT* pDC = pContext->pCurDrawContext;
196 uint32_t dcIndex = pDC->drawId % KNOB_MAX_DRAWS_IN_FLIGHT;
197
198 if (IsDraw)
199 {
200 pDC->pTileMgr = &pContext->pMacroTileManagerArray[dcIndex];
201 pDC->pTileMgr->initialize();
202 }
203
204 // Each worker thread looks at a DC for both FE and BE work at different times and so we
205 // multiply threadDone by 2. When the threadDone counter has reached 0 then all workers
206 // have moved past this DC. (i.e. Each worker has checked this DC for both FE and BE work and
207 // then moved on if all work is done.)
208 pContext->pCurDrawContext->threadsDone = pContext->NumFEThreads + pContext->NumBEThreads;
209
210 if (IsDraw)
211 {
212 InterlockedIncrement((volatile LONG*)&pContext->drawsOutstandingFE);
213 }
214
215 _ReadWriteBarrier();
216 {
217 std::unique_lock<std::mutex> lock(pContext->WaitLock);
218 pContext->dcRing.Enqueue();
219 }
220
221 if (KNOB_SINGLE_THREADED)
222 {
223 // flush denormals to 0
224 uint32_t mxcsr = _mm_getcsr();
225 _mm_setcsr(mxcsr | _MM_FLUSH_ZERO_ON | _MM_DENORMALS_ZERO_ON);
226
227 if (IsDraw)
228 {
229 uint32_t curDraw[2] = { pContext->pCurDrawContext->drawId, pContext->pCurDrawContext->drawId };
230 WorkOnFifoFE(pContext, 0, curDraw[0]);
231 WorkOnFifoBE(pContext, 0, curDraw[1], gSingleThreadLockedTiles, 0, 0);
232 }
233 else
234 {
235 uint32_t curDispatch = pContext->pCurDrawContext->drawId;
236 WorkOnCompute(pContext, 0, curDispatch);
237 }
238
239 // Dequeue the work here, if not already done, since we're single threaded (i.e. no workers).
240 while (CompleteDrawContext(pContext, pContext->pCurDrawContext) > 0) {}
241
242 // restore csr
243 _mm_setcsr(mxcsr);
244 }
245 else
246 {
247 RDTSC_START(APIDrawWakeAllThreads);
248 WakeAllThreads(pContext);
249 RDTSC_STOP(APIDrawWakeAllThreads, 1, 0);
250 }
251
252 // Set current draw context to NULL so that next state call forces a new draw context to be created and populated.
253 pContext->pPrevDrawContext = pContext->pCurDrawContext;
254 pContext->pCurDrawContext = nullptr;
255 }
256
257 INLINE void QueueDraw(SWR_CONTEXT* pContext)
258 {
259 QueueWork<true>(pContext);
260 }
261
262 INLINE void QueueDispatch(SWR_CONTEXT* pContext)
263 {
264 QueueWork<false>(pContext);
265 }
266
267 DRAW_CONTEXT* GetDrawContext(SWR_CONTEXT *pContext, bool isSplitDraw = false)
268 {
269 RDTSC_START(APIGetDrawContext);
270 // If current draw context is null then need to obtain a new draw context to use from ring.
271 if (pContext->pCurDrawContext == nullptr)
272 {
273 // Need to wait for a free entry.
274 while (pContext->dcRing.IsFull())
275 {
276 _mm_pause();
277 }
278
279 uint64_t curDraw = pContext->dcRing.GetHead();
280 uint32_t dcIndex = curDraw % KNOB_MAX_DRAWS_IN_FLIGHT;
281
282 static uint64_t lastDrawChecked;
283 static uint32_t lastFrameChecked;
284 if ((pContext->frameCount - lastFrameChecked) > 2 ||
285 (curDraw - lastDrawChecked) > 0x10000)
286 {
287 // Take this opportunity to clean-up old arena allocations
288 pContext->cachingArenaAllocator.FreeOldBlocks();
289
290 lastFrameChecked = pContext->frameCount;
291 lastDrawChecked = curDraw;
292 }
293
294 DRAW_CONTEXT* pCurDrawContext = &pContext->dcRing[dcIndex];
295 pContext->pCurDrawContext = pCurDrawContext;
296
297 // Assign next available entry in DS ring to this DC.
298 uint32_t dsIndex = pContext->curStateId % KNOB_MAX_DRAWS_IN_FLIGHT;
299 pCurDrawContext->pState = &pContext->dsRing[dsIndex];
300
301 // Copy previous state to current state.
302 if (pContext->pPrevDrawContext)
303 {
304 DRAW_CONTEXT* pPrevDrawContext = pContext->pPrevDrawContext;
305
306 // If we're splitting our draw then we can just use the same state from the previous
307 // draw. In this case, we won't increment the DS ring index so the next non-split
308 // draw can receive the state.
309 if (isSplitDraw == false)
310 {
311 CopyState(*pCurDrawContext->pState, *pPrevDrawContext->pState);
312
313 // Should have been cleaned up previously
314 SWR_ASSERT(pCurDrawContext->pState->pArena->IsEmpty() == true);
315
316 pCurDrawContext->pState->pPrivateState = nullptr;
317
318 pContext->curStateId++; // Progress state ring index forward.
319 }
320 else
321 {
322 // If its a split draw then just copy the state pointer over
323 // since its the same draw.
324 pCurDrawContext->pState = pPrevDrawContext->pState;
325 SWR_ASSERT(pPrevDrawContext->cleanupState == false);
326 }
327 }
328 else
329 {
330 SWR_ASSERT(pCurDrawContext->pState->pArena->IsEmpty() == true);
331 pContext->curStateId++; // Progress state ring index forward.
332 }
333
334 SWR_ASSERT(pCurDrawContext->pArena->IsEmpty() == true);
335
336 pCurDrawContext->dependent = false;
337 pCurDrawContext->pContext = pContext;
338 pCurDrawContext->isCompute = false; // Dispatch has to set this to true.
339
340 pCurDrawContext->doneFE = false;
341 pCurDrawContext->FeLock = 0;
342 pCurDrawContext->threadsDone = 0;
343 pCurDrawContext->retireCallback.pfnCallbackFunc = nullptr;
344
345 memset(&pCurDrawContext->dynState, 0, sizeof(pCurDrawContext->dynState));
346
347 // Assign unique drawId for this DC
348 pCurDrawContext->drawId = pContext->dcRing.GetHead();
349
350 pCurDrawContext->cleanupState = true;
351 }
352 else
353 {
354 SWR_ASSERT(isSplitDraw == false, "Split draw should only be used when obtaining a new DC");
355 }
356
357 RDTSC_STOP(APIGetDrawContext, 0, 0);
358 return pContext->pCurDrawContext;
359 }
360
361 API_STATE* GetDrawState(SWR_CONTEXT *pContext)
362 {
363 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
364 SWR_ASSERT(pDC->pState != nullptr);
365
366 return &pDC->pState->state;
367 }
368
369 void SWR_API SwrSaveState(
370 HANDLE hContext,
371 void* pOutputStateBlock,
372 size_t memSize)
373 {
374 SWR_CONTEXT *pContext = GetContext(hContext);
375 auto pSrc = GetDrawState(pContext);
376 SWR_ASSERT(pOutputStateBlock && memSize >= sizeof(*pSrc));
377
378 memcpy(pOutputStateBlock, pSrc, sizeof(*pSrc));
379 }
380
381 void SWR_API SwrRestoreState(
382 HANDLE hContext,
383 const void* pStateBlock,
384 size_t memSize)
385 {
386 SWR_CONTEXT *pContext = GetContext(hContext);
387 auto pDst = GetDrawState(pContext);
388 SWR_ASSERT(pStateBlock && memSize >= sizeof(*pDst));
389
390 memcpy(pDst, pStateBlock, sizeof(*pDst));
391 }
392
393 void SetupDefaultState(SWR_CONTEXT *pContext)
394 {
395 API_STATE* pState = GetDrawState(pContext);
396
397 pState->rastState.cullMode = SWR_CULLMODE_NONE;
398 pState->rastState.frontWinding = SWR_FRONTWINDING_CCW;
399 }
400
401 void SwrSync(HANDLE hContext, PFN_CALLBACK_FUNC pfnFunc, uint64_t userData, uint64_t userData2, uint64_t userData3)
402 {
403 RDTSC_START(APISync);
404
405 SWR_ASSERT(pfnFunc != nullptr);
406
407 SWR_CONTEXT *pContext = GetContext(hContext);
408 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
409
410 pDC->FeWork.type = SYNC;
411 pDC->FeWork.pfnWork = ProcessSync;
412
413 // Setup callback function
414 pDC->retireCallback.pfnCallbackFunc = pfnFunc;
415 pDC->retireCallback.userData = userData;
416 pDC->retireCallback.userData2 = userData2;
417 pDC->retireCallback.userData3 = userData3;
418
419 //enqueue
420 QueueDraw(pContext);
421
422 RDTSC_STOP(APISync, 1, 0);
423 }
424
425 void SwrWaitForIdle(HANDLE hContext)
426 {
427 SWR_CONTEXT *pContext = GetContext(hContext);
428
429 RDTSC_START(APIWaitForIdle);
430
431 while (!pContext->dcRing.IsEmpty())
432 {
433 _mm_pause();
434 }
435
436 RDTSC_STOP(APIWaitForIdle, 1, 0);
437 }
438
439 void SwrWaitForIdleFE(HANDLE hContext)
440 {
441 SWR_CONTEXT *pContext = GetContext(hContext);
442
443 RDTSC_START(APIWaitForIdle);
444
445 while (pContext->drawsOutstandingFE > 0)
446 {
447 _mm_pause();
448 }
449
450 RDTSC_STOP(APIWaitForIdle, 1, 0);
451 }
452
453 void SwrSetVertexBuffers(
454 HANDLE hContext,
455 uint32_t numBuffers,
456 const SWR_VERTEX_BUFFER_STATE* pVertexBuffers)
457 {
458 API_STATE* pState = GetDrawState(GetContext(hContext));
459
460 for (uint32_t i = 0; i < numBuffers; ++i)
461 {
462 const SWR_VERTEX_BUFFER_STATE *pVB = &pVertexBuffers[i];
463 pState->vertexBuffers[pVB->index] = *pVB;
464 }
465 }
466
467 void SwrSetIndexBuffer(
468 HANDLE hContext,
469 const SWR_INDEX_BUFFER_STATE* pIndexBuffer)
470 {
471 API_STATE* pState = GetDrawState(GetContext(hContext));
472
473 pState->indexBuffer = *pIndexBuffer;
474 }
475
476 void SwrSetFetchFunc(
477 HANDLE hContext,
478 PFN_FETCH_FUNC pfnFetchFunc)
479 {
480 API_STATE* pState = GetDrawState(GetContext(hContext));
481
482 pState->pfnFetchFunc = pfnFetchFunc;
483 }
484
485 void SwrSetSoFunc(
486 HANDLE hContext,
487 PFN_SO_FUNC pfnSoFunc,
488 uint32_t streamIndex)
489 {
490 API_STATE* pState = GetDrawState(GetContext(hContext));
491
492 SWR_ASSERT(streamIndex < MAX_SO_STREAMS);
493
494 pState->pfnSoFunc[streamIndex] = pfnSoFunc;
495 }
496
497 void SwrSetSoState(
498 HANDLE hContext,
499 SWR_STREAMOUT_STATE* pSoState)
500 {
501 API_STATE* pState = GetDrawState(GetContext(hContext));
502
503 pState->soState = *pSoState;
504 }
505
506 void SwrSetSoBuffers(
507 HANDLE hContext,
508 SWR_STREAMOUT_BUFFER* pSoBuffer,
509 uint32_t slot)
510 {
511 API_STATE* pState = GetDrawState(GetContext(hContext));
512
513 SWR_ASSERT((slot < 4), "There are only 4 SO buffer slots [0, 3]\nSlot requested: %d", slot);
514
515 pState->soBuffer[slot] = *pSoBuffer;
516 }
517
518 void SwrSetVertexFunc(
519 HANDLE hContext,
520 PFN_VERTEX_FUNC pfnVertexFunc)
521 {
522 API_STATE* pState = GetDrawState(GetContext(hContext));
523
524 pState->pfnVertexFunc = pfnVertexFunc;
525 }
526
527 void SwrSetFrontendState(
528 HANDLE hContext,
529 SWR_FRONTEND_STATE *pFEState)
530 {
531 API_STATE* pState = GetDrawState(GetContext(hContext));
532 pState->frontendState = *pFEState;
533 }
534
535 void SwrSetGsState(
536 HANDLE hContext,
537 SWR_GS_STATE *pGSState)
538 {
539 API_STATE* pState = GetDrawState(GetContext(hContext));
540 pState->gsState = *pGSState;
541 }
542
543 void SwrSetGsFunc(
544 HANDLE hContext,
545 PFN_GS_FUNC pfnGsFunc)
546 {
547 API_STATE* pState = GetDrawState(GetContext(hContext));
548 pState->pfnGsFunc = pfnGsFunc;
549 }
550
551 void SwrSetCsFunc(
552 HANDLE hContext,
553 PFN_CS_FUNC pfnCsFunc,
554 uint32_t totalThreadsInGroup,
555 uint32_t totalSpillFillSize)
556 {
557 API_STATE* pState = GetDrawState(GetContext(hContext));
558 pState->pfnCsFunc = pfnCsFunc;
559 pState->totalThreadsInGroup = totalThreadsInGroup;
560 pState->totalSpillFillSize = totalSpillFillSize;
561 }
562
563 void SwrSetTsState(
564 HANDLE hContext,
565 SWR_TS_STATE *pState)
566 {
567 API_STATE* pApiState = GetDrawState(GetContext(hContext));
568 pApiState->tsState = *pState;
569 }
570
571 void SwrSetHsFunc(
572 HANDLE hContext,
573 PFN_HS_FUNC pfnFunc)
574 {
575 API_STATE* pApiState = GetDrawState(GetContext(hContext));
576 pApiState->pfnHsFunc = pfnFunc;
577 }
578
579 void SwrSetDsFunc(
580 HANDLE hContext,
581 PFN_DS_FUNC pfnFunc)
582 {
583 API_STATE* pApiState = GetDrawState(GetContext(hContext));
584 pApiState->pfnDsFunc = pfnFunc;
585 }
586
587 void SwrSetDepthStencilState(
588 HANDLE hContext,
589 SWR_DEPTH_STENCIL_STATE *pDSState)
590 {
591 API_STATE* pState = GetDrawState(GetContext(hContext));
592
593 pState->depthStencilState = *pDSState;
594 }
595
596 void SwrSetBackendState(
597 HANDLE hContext,
598 SWR_BACKEND_STATE *pBEState)
599 {
600 API_STATE* pState = GetDrawState(GetContext(hContext));
601
602 pState->backendState = *pBEState;
603 }
604
605 void SwrSetPixelShaderState(
606 HANDLE hContext,
607 SWR_PS_STATE *pPSState)
608 {
609 API_STATE *pState = GetDrawState(GetContext(hContext));
610 pState->psState = *pPSState;
611 }
612
613 void SwrSetBlendState(
614 HANDLE hContext,
615 SWR_BLEND_STATE *pBlendState)
616 {
617 API_STATE *pState = GetDrawState(GetContext(hContext));
618 memcpy(&pState->blendState, pBlendState, sizeof(SWR_BLEND_STATE));
619 }
620
621 void SwrSetBlendFunc(
622 HANDLE hContext,
623 uint32_t renderTarget,
624 PFN_BLEND_JIT_FUNC pfnBlendFunc)
625 {
626 SWR_ASSERT(renderTarget < SWR_NUM_RENDERTARGETS);
627 API_STATE *pState = GetDrawState(GetContext(hContext));
628 pState->pfnBlendFunc[renderTarget] = pfnBlendFunc;
629 }
630
631 // update guardband multipliers for the viewport
632 void updateGuardband(API_STATE *pState)
633 {
634 // guardband center is viewport center
635 pState->gbState.left = KNOB_GUARDBAND_WIDTH / pState->vp[0].width;
636 pState->gbState.right = KNOB_GUARDBAND_WIDTH / pState->vp[0].width;
637 pState->gbState.top = KNOB_GUARDBAND_HEIGHT / pState->vp[0].height;
638 pState->gbState.bottom = KNOB_GUARDBAND_HEIGHT / pState->vp[0].height;
639 }
640
641 void SwrSetRastState(
642 HANDLE hContext,
643 const SWR_RASTSTATE *pRastState)
644 {
645 SWR_CONTEXT *pContext = GetContext(hContext);
646 API_STATE* pState = GetDrawState(pContext);
647
648 memcpy(&pState->rastState, pRastState, sizeof(SWR_RASTSTATE));
649 }
650
651 void SwrSetViewports(
652 HANDLE hContext,
653 uint32_t numViewports,
654 const SWR_VIEWPORT* pViewports,
655 const SWR_VIEWPORT_MATRIX* pMatrices)
656 {
657 SWR_ASSERT(numViewports <= KNOB_NUM_VIEWPORTS_SCISSORS,
658 "Invalid number of viewports.");
659
660 SWR_CONTEXT *pContext = GetContext(hContext);
661 API_STATE* pState = GetDrawState(pContext);
662
663 memcpy(&pState->vp[0], pViewports, sizeof(SWR_VIEWPORT) * numViewports);
664
665 if (pMatrices != nullptr)
666 {
667 memcpy(&pState->vpMatrix[0], pMatrices, sizeof(SWR_VIEWPORT_MATRIX) * numViewports);
668 }
669 else
670 {
671 // Compute default viewport transform.
672 for (uint32_t i = 0; i < numViewports; ++i)
673 {
674 if (pContext->driverType == DX)
675 {
676 pState->vpMatrix[i].m00 = pState->vp[i].width / 2.0f;
677 pState->vpMatrix[i].m11 = -pState->vp[i].height / 2.0f;
678 pState->vpMatrix[i].m22 = pState->vp[i].maxZ - pState->vp[i].minZ;
679 pState->vpMatrix[i].m30 = pState->vp[i].x + pState->vpMatrix[i].m00;
680 pState->vpMatrix[i].m31 = pState->vp[i].y - pState->vpMatrix[i].m11;
681 pState->vpMatrix[i].m32 = pState->vp[i].minZ;
682 }
683 else
684 {
685 // Standard, with the exception that Y is inverted.
686 pState->vpMatrix[i].m00 = (pState->vp[i].width - pState->vp[i].x) / 2.0f;
687 pState->vpMatrix[i].m11 = (pState->vp[i].y - pState->vp[i].height) / 2.0f;
688 pState->vpMatrix[i].m22 = (pState->vp[i].maxZ - pState->vp[i].minZ) / 2.0f;
689 pState->vpMatrix[i].m30 = pState->vp[i].x + pState->vpMatrix[i].m00;
690 pState->vpMatrix[i].m31 = pState->vp[i].height + pState->vpMatrix[i].m11;
691 pState->vpMatrix[i].m32 = pState->vp[i].minZ + pState->vpMatrix[i].m22;
692
693 // Now that the matrix is calculated, clip the view coords to screen size.
694 // OpenGL allows for -ve x,y in the viewport.
695 pState->vp[i].x = std::max(pState->vp[i].x, 0.0f);
696 pState->vp[i].y = std::max(pState->vp[i].y, 0.0f);
697 }
698 }
699 }
700
701 updateGuardband(pState);
702 }
703
704 void SwrSetScissorRects(
705 HANDLE hContext,
706 uint32_t numScissors,
707 const BBOX* pScissors)
708 {
709 SWR_ASSERT(numScissors <= KNOB_NUM_VIEWPORTS_SCISSORS,
710 "Invalid number of scissor rects.");
711
712 API_STATE* pState = GetDrawState(GetContext(hContext));
713 memcpy(&pState->scissorRects[0], pScissors, numScissors * sizeof(BBOX));
714 };
715
716 void SetupMacroTileScissors(DRAW_CONTEXT *pDC)
717 {
718 API_STATE *pState = &pDC->pState->state;
719 uint32_t left, right, top, bottom;
720
721 // Set up scissor dimensions based on scissor or viewport
722 if (pState->rastState.scissorEnable)
723 {
724 // scissor rect right/bottom edge are exclusive, core expects scissor dimensions to be inclusive, so subtract one pixel from right/bottom edges
725 left = pState->scissorRects[0].left;
726 right = pState->scissorRects[0].right;
727 top = pState->scissorRects[0].top;
728 bottom = pState->scissorRects[0].bottom;
729 }
730 else
731 {
732 // the vp width and height must be added to origin un-rounded then the result round to -inf.
733 // The cast to int works for rounding assuming all [left, right, top, bottom] are positive.
734 left = (int32_t)pState->vp[0].x;
735 right = (int32_t)(pState->vp[0].x + pState->vp[0].width);
736 top = (int32_t)pState->vp[0].y;
737 bottom = (int32_t)(pState->vp[0].y + pState->vp[0].height);
738 }
739
740 right = std::min<uint32_t>(right, KNOB_MAX_SCISSOR_X);
741 bottom = std::min<uint32_t>(bottom, KNOB_MAX_SCISSOR_Y);
742
743 if (left > KNOB_MAX_SCISSOR_X || top > KNOB_MAX_SCISSOR_Y)
744 {
745 pState->scissorInFixedPoint.left = 0;
746 pState->scissorInFixedPoint.right = 0;
747 pState->scissorInFixedPoint.top = 0;
748 pState->scissorInFixedPoint.bottom = 0;
749 }
750 else
751 {
752 pState->scissorInFixedPoint.left = left * FIXED_POINT_SCALE;
753 pState->scissorInFixedPoint.right = right * FIXED_POINT_SCALE - 1;
754 pState->scissorInFixedPoint.top = top * FIXED_POINT_SCALE;
755 pState->scissorInFixedPoint.bottom = bottom * FIXED_POINT_SCALE - 1;
756 }
757 }
758
759 // templated backend function tables
760 extern PFN_BACKEND_FUNC gBackendNullPs[SWR_MULTISAMPLE_TYPE_COUNT];
761 extern PFN_BACKEND_FUNC gBackendSingleSample[SWR_INPUT_COVERAGE_COUNT][2][2];
762 extern PFN_BACKEND_FUNC gBackendPixelRateTable[SWR_MULTISAMPLE_TYPE_COUNT][SWR_MSAA_SAMPLE_PATTERN_COUNT][SWR_INPUT_COVERAGE_COUNT][2][2][2];
763 extern PFN_BACKEND_FUNC gBackendSampleRateTable[SWR_MULTISAMPLE_TYPE_COUNT][SWR_INPUT_COVERAGE_COUNT][2][2];
764 void SetupPipeline(DRAW_CONTEXT *pDC)
765 {
766 DRAW_STATE* pState = pDC->pState;
767 const SWR_RASTSTATE &rastState = pState->state.rastState;
768 const SWR_PS_STATE &psState = pState->state.psState;
769 BACKEND_FUNCS& backendFuncs = pState->backendFuncs;
770 const uint32_t forcedSampleCount = (rastState.forcedSampleCount) ? 1 : 0;
771
772 // setup backend
773 if (psState.pfnPixelShader == nullptr)
774 {
775 backendFuncs.pfnBackend = gBackendNullPs[pState->state.rastState.sampleCount];
776 }
777 else
778 {
779 const bool bMultisampleEnable = ((rastState.sampleCount > SWR_MULTISAMPLE_1X) || rastState.forcedSampleCount) ? 1 : 0;
780 const uint32_t centroid = ((psState.barycentricsMask & SWR_BARYCENTRIC_CENTROID_MASK) > 0) ? 1 : 0;
781 const uint32_t canEarlyZ = (psState.forceEarlyZ || (!psState.writesODepth && !psState.usesSourceDepth && !psState.usesUAV)) ? 1 : 0;
782
783 SWR_BARYCENTRICS_MASK barycentricsMask = (SWR_BARYCENTRICS_MASK)psState.barycentricsMask;
784
785 // select backend function
786 switch(psState.shadingRate)
787 {
788 case SWR_SHADING_RATE_PIXEL:
789 if(bMultisampleEnable)
790 {
791 // always need to generate I & J per sample for Z interpolation
792 barycentricsMask = (SWR_BARYCENTRICS_MASK)(barycentricsMask | SWR_BARYCENTRIC_PER_SAMPLE_MASK);
793 backendFuncs.pfnBackend = gBackendPixelRateTable[rastState.sampleCount][rastState.samplePattern][psState.inputCoverage][centroid][forcedSampleCount][canEarlyZ];
794 }
795 else
796 {
797 // always need to generate I & J per pixel for Z interpolation
798 barycentricsMask = (SWR_BARYCENTRICS_MASK)(barycentricsMask | SWR_BARYCENTRIC_PER_PIXEL_MASK);
799 backendFuncs.pfnBackend = gBackendSingleSample[psState.inputCoverage][centroid][canEarlyZ];
800 }
801 break;
802 case SWR_SHADING_RATE_SAMPLE:
803 SWR_ASSERT(rastState.samplePattern == SWR_MSAA_STANDARD_PATTERN);
804 // always need to generate I & J per sample for Z interpolation
805 barycentricsMask = (SWR_BARYCENTRICS_MASK)(barycentricsMask | SWR_BARYCENTRIC_PER_SAMPLE_MASK);
806 backendFuncs.pfnBackend = gBackendSampleRateTable[rastState.sampleCount][psState.inputCoverage][centroid][canEarlyZ];
807 break;
808 default:
809 SWR_ASSERT(0 && "Invalid shading rate");
810 break;
811 }
812 }
813
814 PFN_PROCESS_PRIMS pfnBinner;
815 switch (pState->state.topology)
816 {
817 case TOP_POINT_LIST:
818 pState->pfnProcessPrims = ClipPoints;
819 pfnBinner = BinPoints;
820 break;
821 case TOP_LINE_LIST:
822 case TOP_LINE_STRIP:
823 case TOP_LINE_LOOP:
824 case TOP_LINE_LIST_ADJ:
825 case TOP_LISTSTRIP_ADJ:
826 pState->pfnProcessPrims = ClipLines;
827 pfnBinner = BinLines;
828 break;
829 default:
830 pState->pfnProcessPrims = ClipTriangles;
831 pfnBinner = GetBinTrianglesFunc((rastState.conservativeRast > 0));
832 break;
833 };
834
835 // disable clipper if viewport transform is disabled
836 if (pState->state.frontendState.vpTransformDisable)
837 {
838 pState->pfnProcessPrims = pfnBinner;
839 }
840
841 if ((pState->state.psState.pfnPixelShader == nullptr) &&
842 (pState->state.depthStencilState.depthTestEnable == FALSE) &&
843 (pState->state.depthStencilState.depthWriteEnable == FALSE) &&
844 (pState->state.depthStencilState.stencilTestEnable == FALSE) &&
845 (pState->state.depthStencilState.stencilWriteEnable == FALSE) &&
846 (pState->state.backendState.numAttributes == 0))
847 {
848 pState->pfnProcessPrims = nullptr;
849 }
850
851 if (pState->state.soState.rasterizerDisable == true)
852 {
853 pState->pfnProcessPrims = nullptr;
854 }
855
856 // set up the frontend attribute count
857 pState->state.feNumAttributes = 0;
858 const SWR_BACKEND_STATE& backendState = pState->state.backendState;
859 if (backendState.swizzleEnable)
860 {
861 // attribute swizzling is enabled, iterate over the map and record the max attribute used
862 for (uint32_t i = 0; i < backendState.numAttributes; ++i)
863 {
864 pState->state.feNumAttributes = std::max(pState->state.feNumAttributes, (uint32_t)backendState.swizzleMap[i].sourceAttrib + 1);
865 }
866 }
867 else
868 {
869 pState->state.feNumAttributes = pState->state.backendState.numAttributes;
870 }
871
872 if (pState->state.soState.soEnable)
873 {
874 uint32_t streamMasks = 0;
875 for (uint32_t i = 0; i < 4; ++i)
876 {
877 streamMasks |= pState->state.soState.streamMasks[i];
878 }
879
880 DWORD maxAttrib;
881 if (_BitScanReverse(&maxAttrib, streamMasks))
882 {
883 pState->state.feNumAttributes = std::max(pState->state.feNumAttributes, (uint32_t)(maxAttrib + 1));
884 }
885 }
886
887 // complicated logic to test for cases where we don't need backing hottile memory for a draw
888 // have to check for the special case where depth/stencil test is enabled but depthwrite is disabled.
889 pState->state.depthHottileEnable = ((!(pState->state.depthStencilState.depthTestEnable &&
890 !pState->state.depthStencilState.depthWriteEnable &&
891 pState->state.depthStencilState.depthTestFunc == ZFUNC_ALWAYS)) &&
892 (pState->state.depthStencilState.depthTestEnable ||
893 pState->state.depthStencilState.depthWriteEnable)) ? true : false;
894
895 pState->state.stencilHottileEnable = (((!(pState->state.depthStencilState.stencilTestEnable &&
896 !pState->state.depthStencilState.stencilWriteEnable &&
897 pState->state.depthStencilState.stencilTestFunc == ZFUNC_ALWAYS)) ||
898 // for stencil we have to check the double sided state as well
899 (!(pState->state.depthStencilState.doubleSidedStencilTestEnable &&
900 !pState->state.depthStencilState.stencilWriteEnable &&
901 pState->state.depthStencilState.backfaceStencilTestFunc == ZFUNC_ALWAYS))) &&
902 (pState->state.depthStencilState.stencilTestEnable ||
903 pState->state.depthStencilState.stencilWriteEnable)) ? true : false;
904
905 uint32_t numRTs = pState->state.psState.numRenderTargets;
906 pState->state.colorHottileEnable = 0;
907 if (psState.pfnPixelShader != nullptr)
908 {
909 for (uint32_t rt = 0; rt < numRTs; ++rt)
910 {
911 pState->state.colorHottileEnable |=
912 (!pState->state.blendState.renderTarget[rt].writeDisableAlpha ||
913 !pState->state.blendState.renderTarget[rt].writeDisableRed ||
914 !pState->state.blendState.renderTarget[rt].writeDisableGreen ||
915 !pState->state.blendState.renderTarget[rt].writeDisableBlue) ? (1 << rt) : 0;
916 }
917 }
918
919 // Setup depth quantization function
920 if (pState->state.depthHottileEnable)
921 {
922 switch (pState->state.rastState.depthFormat)
923 {
924 case R32_FLOAT_X8X24_TYPELESS: pState->state.pfnQuantizeDepth = QuantizeDepth < R32_FLOAT_X8X24_TYPELESS > ; break;
925 case R32_FLOAT: pState->state.pfnQuantizeDepth = QuantizeDepth < R32_FLOAT > ; break;
926 case R24_UNORM_X8_TYPELESS: pState->state.pfnQuantizeDepth = QuantizeDepth < R24_UNORM_X8_TYPELESS > ; break;
927 case R16_UNORM: pState->state.pfnQuantizeDepth = QuantizeDepth < R16_UNORM > ; break;
928 default: SWR_ASSERT(false, "Unsupported depth format for depth quantiztion.");
929 pState->state.pfnQuantizeDepth = QuantizeDepth < R32_FLOAT > ;
930 }
931 }
932 else
933 {
934 // set up pass-through quantize if depth isn't enabled
935 pState->state.pfnQuantizeDepth = QuantizeDepth < R32_FLOAT > ;
936 }
937 }
938
939 //////////////////////////////////////////////////////////////////////////
940 /// @brief InitDraw
941 /// @param pDC - Draw context to initialize for this draw.
942 void InitDraw(
943 DRAW_CONTEXT *pDC,
944 bool isSplitDraw)
945 {
946 // We don't need to re-setup the scissors/pipeline state again for split draw.
947 if (isSplitDraw == false)
948 {
949 SetupMacroTileScissors(pDC);
950 SetupPipeline(pDC);
951 }
952 }
953
954 //////////////////////////////////////////////////////////////////////////
955 /// @brief We can split the draw for certain topologies for better performance.
956 /// @param totalVerts - Total vertices for draw
957 /// @param topology - Topology used for draw
958 uint32_t MaxVertsPerDraw(
959 DRAW_CONTEXT* pDC,
960 uint32_t totalVerts,
961 PRIMITIVE_TOPOLOGY topology)
962 {
963 API_STATE& state = pDC->pState->state;
964
965 uint32_t vertsPerDraw = totalVerts;
966
967 if (state.soState.soEnable)
968 {
969 return totalVerts;
970 }
971
972 switch (topology)
973 {
974 case TOP_POINT_LIST:
975 case TOP_TRIANGLE_LIST:
976 vertsPerDraw = KNOB_MAX_PRIMS_PER_DRAW;
977 break;
978
979 case TOP_PATCHLIST_1:
980 case TOP_PATCHLIST_2:
981 case TOP_PATCHLIST_3:
982 case TOP_PATCHLIST_4:
983 case TOP_PATCHLIST_5:
984 case TOP_PATCHLIST_6:
985 case TOP_PATCHLIST_7:
986 case TOP_PATCHLIST_8:
987 case TOP_PATCHLIST_9:
988 case TOP_PATCHLIST_10:
989 case TOP_PATCHLIST_11:
990 case TOP_PATCHLIST_12:
991 case TOP_PATCHLIST_13:
992 case TOP_PATCHLIST_14:
993 case TOP_PATCHLIST_15:
994 case TOP_PATCHLIST_16:
995 case TOP_PATCHLIST_17:
996 case TOP_PATCHLIST_18:
997 case TOP_PATCHLIST_19:
998 case TOP_PATCHLIST_20:
999 case TOP_PATCHLIST_21:
1000 case TOP_PATCHLIST_22:
1001 case TOP_PATCHLIST_23:
1002 case TOP_PATCHLIST_24:
1003 case TOP_PATCHLIST_25:
1004 case TOP_PATCHLIST_26:
1005 case TOP_PATCHLIST_27:
1006 case TOP_PATCHLIST_28:
1007 case TOP_PATCHLIST_29:
1008 case TOP_PATCHLIST_30:
1009 case TOP_PATCHLIST_31:
1010 case TOP_PATCHLIST_32:
1011 if (pDC->pState->state.tsState.tsEnable)
1012 {
1013 uint32_t vertsPerPrim = topology - TOP_PATCHLIST_BASE;
1014 vertsPerDraw = vertsPerPrim * KNOB_MAX_TESS_PRIMS_PER_DRAW;
1015 }
1016 break;
1017
1018 // The Primitive Assembly code can only handle 1 RECT at a time.
1019 case TOP_RECT_LIST:
1020 vertsPerDraw = 3;
1021 break;
1022
1023 default:
1024 // We are not splitting up draws for other topologies.
1025 break;
1026 }
1027
1028 return vertsPerDraw;
1029 }
1030
1031
1032 //////////////////////////////////////////////////////////////////////////
1033 /// @brief DrawInstanced
1034 /// @param hContext - Handle passed back from SwrCreateContext
1035 /// @param topology - Specifies topology for draw.
1036 /// @param numVerts - How many vertices to read sequentially from vertex data (per instance).
1037 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1038 /// @param numInstances - How many instances to render.
1039 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1040 void DrawInstanced(
1041 HANDLE hContext,
1042 PRIMITIVE_TOPOLOGY topology,
1043 uint32_t numVertices,
1044 uint32_t startVertex,
1045 uint32_t numInstances = 1,
1046 uint32_t startInstance = 0)
1047 {
1048 if (KNOB_TOSS_DRAW)
1049 {
1050 return;
1051 }
1052
1053 RDTSC_START(APIDraw);
1054
1055 SWR_CONTEXT *pContext = GetContext(hContext);
1056 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1057
1058 uint32_t maxVertsPerDraw = MaxVertsPerDraw(pDC, numVertices, topology);
1059 uint32_t primsPerDraw = GetNumPrims(topology, maxVertsPerDraw);
1060 uint32_t remainingVerts = numVertices;
1061
1062 API_STATE *pState = &pDC->pState->state;
1063 pState->topology = topology;
1064 pState->forceFront = false;
1065
1066 // disable culling for points/lines
1067 uint32_t oldCullMode = pState->rastState.cullMode;
1068 if (topology == TOP_POINT_LIST)
1069 {
1070 pState->rastState.cullMode = SWR_CULLMODE_NONE;
1071 pState->forceFront = true;
1072 }
1073
1074 int draw = 0;
1075 while (remainingVerts)
1076 {
1077 uint32_t numVertsForDraw = (remainingVerts < maxVertsPerDraw) ?
1078 remainingVerts : maxVertsPerDraw;
1079
1080 bool isSplitDraw = (draw > 0) ? true : false;
1081 DRAW_CONTEXT* pDC = GetDrawContext(pContext, isSplitDraw);
1082 InitDraw(pDC, isSplitDraw);
1083
1084 pDC->FeWork.type = DRAW;
1085 pDC->FeWork.pfnWork = GetProcessDrawFunc(
1086 false, // IsIndexed
1087 false, // bEnableCutIndex
1088 pState->tsState.tsEnable,
1089 pState->gsState.gsEnable,
1090 pState->soState.soEnable,
1091 pDC->pState->pfnProcessPrims != nullptr);
1092 pDC->FeWork.desc.draw.numVerts = numVertsForDraw;
1093 pDC->FeWork.desc.draw.startVertex = startVertex;
1094 pDC->FeWork.desc.draw.numInstances = numInstances;
1095 pDC->FeWork.desc.draw.startInstance = startInstance;
1096 pDC->FeWork.desc.draw.startPrimID = draw * primsPerDraw;
1097 pDC->FeWork.desc.draw.startVertexID = draw * maxVertsPerDraw;
1098
1099 pDC->cleanupState = (remainingVerts == numVertsForDraw);
1100
1101 //enqueue DC
1102 QueueDraw(pContext);
1103
1104 remainingVerts -= numVertsForDraw;
1105 draw++;
1106 }
1107
1108 // restore culling state
1109 pDC = GetDrawContext(pContext);
1110 pDC->pState->state.rastState.cullMode = oldCullMode;
1111
1112 RDTSC_STOP(APIDraw, numVertices * numInstances, 0);
1113 }
1114
1115 //////////////////////////////////////////////////////////////////////////
1116 /// @brief SwrDraw
1117 /// @param hContext - Handle passed back from SwrCreateContext
1118 /// @param topology - Specifies topology for draw.
1119 /// @param startVertex - Specifies start vertex in vertex buffer for draw.
1120 /// @param primCount - Number of vertices.
1121 void SwrDraw(
1122 HANDLE hContext,
1123 PRIMITIVE_TOPOLOGY topology,
1124 uint32_t startVertex,
1125 uint32_t numVertices)
1126 {
1127 DrawInstanced(hContext, topology, numVertices, startVertex);
1128 }
1129
1130 //////////////////////////////////////////////////////////////////////////
1131 /// @brief SwrDrawInstanced
1132 /// @param hContext - Handle passed back from SwrCreateContext
1133 /// @param topology - Specifies topology for draw.
1134 /// @param numVertsPerInstance - How many vertices to read sequentially from vertex data.
1135 /// @param numInstances - How many instances to render.
1136 /// @param startVertex - Specifies start vertex for draw. (vertex data)
1137 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1138 void SwrDrawInstanced(
1139 HANDLE hContext,
1140 PRIMITIVE_TOPOLOGY topology,
1141 uint32_t numVertsPerInstance,
1142 uint32_t numInstances,
1143 uint32_t startVertex,
1144 uint32_t startInstance
1145 )
1146 {
1147 DrawInstanced(hContext, topology, numVertsPerInstance, startVertex, numInstances, startInstance);
1148 }
1149
1150 //////////////////////////////////////////////////////////////////////////
1151 /// @brief DrawIndexedInstanced
1152 /// @param hContext - Handle passed back from SwrCreateContext
1153 /// @param topology - Specifies topology for draw.
1154 /// @param numIndices - Number of indices to read sequentially from index buffer.
1155 /// @param indexOffset - Starting index into index buffer.
1156 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1157 /// @param numInstances - Number of instances to render.
1158 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1159 void DrawIndexedInstance(
1160 HANDLE hContext,
1161 PRIMITIVE_TOPOLOGY topology,
1162 uint32_t numIndices,
1163 uint32_t indexOffset,
1164 int32_t baseVertex,
1165 uint32_t numInstances = 1,
1166 uint32_t startInstance = 0)
1167 {
1168 if (KNOB_TOSS_DRAW)
1169 {
1170 return;
1171 }
1172
1173 RDTSC_START(APIDrawIndexed);
1174
1175 SWR_CONTEXT *pContext = GetContext(hContext);
1176 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1177 API_STATE* pState = &pDC->pState->state;
1178
1179 uint32_t maxIndicesPerDraw = MaxVertsPerDraw(pDC, numIndices, topology);
1180 uint32_t primsPerDraw = GetNumPrims(topology, maxIndicesPerDraw);
1181 uint32_t remainingIndices = numIndices;
1182
1183 uint32_t indexSize = 0;
1184 switch (pState->indexBuffer.format)
1185 {
1186 case R32_UINT: indexSize = sizeof(uint32_t); break;
1187 case R16_UINT: indexSize = sizeof(uint16_t); break;
1188 case R8_UINT: indexSize = sizeof(uint8_t); break;
1189 default:
1190 SWR_ASSERT(0);
1191 }
1192
1193 int draw = 0;
1194 uint8_t *pIB = (uint8_t*)pState->indexBuffer.pIndices;
1195 pIB += (uint64_t)indexOffset * (uint64_t)indexSize;
1196
1197 pState->topology = topology;
1198 pState->forceFront = false;
1199
1200 // disable culling for points/lines
1201 uint32_t oldCullMode = pState->rastState.cullMode;
1202 if (topology == TOP_POINT_LIST)
1203 {
1204 pState->rastState.cullMode = SWR_CULLMODE_NONE;
1205 pState->forceFront = true;
1206 }
1207
1208 while (remainingIndices)
1209 {
1210 uint32_t numIndicesForDraw = (remainingIndices < maxIndicesPerDraw) ?
1211 remainingIndices : maxIndicesPerDraw;
1212
1213 // When breaking up draw, we need to obtain new draw context for each iteration.
1214 bool isSplitDraw = (draw > 0) ? true : false;
1215 pDC = GetDrawContext(pContext, isSplitDraw);
1216 InitDraw(pDC, isSplitDraw);
1217
1218 pDC->FeWork.type = DRAW;
1219 pDC->FeWork.pfnWork = GetProcessDrawFunc(
1220 true, // IsIndexed
1221 pState->frontendState.bEnableCutIndex,
1222 pState->tsState.tsEnable,
1223 pState->gsState.gsEnable,
1224 pState->soState.soEnable,
1225 pDC->pState->pfnProcessPrims != nullptr);
1226 pDC->FeWork.desc.draw.pDC = pDC;
1227 pDC->FeWork.desc.draw.numIndices = numIndicesForDraw;
1228 pDC->FeWork.desc.draw.pIB = (int*)pIB;
1229 pDC->FeWork.desc.draw.type = pDC->pState->state.indexBuffer.format;
1230
1231 pDC->FeWork.desc.draw.numInstances = numInstances;
1232 pDC->FeWork.desc.draw.startInstance = startInstance;
1233 pDC->FeWork.desc.draw.baseVertex = baseVertex;
1234 pDC->FeWork.desc.draw.startPrimID = draw * primsPerDraw;
1235
1236 pDC->cleanupState = (remainingIndices == numIndicesForDraw);
1237
1238 //enqueue DC
1239 QueueDraw(pContext);
1240
1241 pIB += maxIndicesPerDraw * indexSize;
1242 remainingIndices -= numIndicesForDraw;
1243 draw++;
1244 }
1245
1246 // restore culling state
1247 pDC = GetDrawContext(pContext);
1248 pDC->pState->state.rastState.cullMode = oldCullMode;
1249
1250 RDTSC_STOP(APIDrawIndexed, numIndices * numInstances, 0);
1251 }
1252
1253
1254 //////////////////////////////////////////////////////////////////////////
1255 /// @brief DrawIndexed
1256 /// @param hContext - Handle passed back from SwrCreateContext
1257 /// @param topology - Specifies topology for draw.
1258 /// @param numIndices - Number of indices to read sequentially from index buffer.
1259 /// @param indexOffset - Starting index into index buffer.
1260 /// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
1261 void SwrDrawIndexed(
1262 HANDLE hContext,
1263 PRIMITIVE_TOPOLOGY topology,
1264 uint32_t numIndices,
1265 uint32_t indexOffset,
1266 int32_t baseVertex
1267 )
1268 {
1269 DrawIndexedInstance(hContext, topology, numIndices, indexOffset, baseVertex);
1270 }
1271
1272 //////////////////////////////////////////////////////////////////////////
1273 /// @brief SwrDrawIndexedInstanced
1274 /// @param hContext - Handle passed back from SwrCreateContext
1275 /// @param topology - Specifies topology for draw.
1276 /// @param numIndices - Number of indices to read sequentially from index buffer.
1277 /// @param numInstances - Number of instances to render.
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 /// @param startInstance - Which instance to start sequentially fetching from in each buffer (instanced data)
1281 void SwrDrawIndexedInstanced(
1282 HANDLE hContext,
1283 PRIMITIVE_TOPOLOGY topology,
1284 uint32_t numIndices,
1285 uint32_t numInstances,
1286 uint32_t indexOffset,
1287 int32_t baseVertex,
1288 uint32_t startInstance)
1289 {
1290 DrawIndexedInstance(hContext, topology, numIndices, indexOffset, baseVertex, numInstances, startInstance);
1291 }
1292
1293 //////////////////////////////////////////////////////////////////////////
1294 /// @brief SwrInvalidateTiles
1295 /// @param hContext - Handle passed back from SwrCreateContext
1296 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to invalidate.
1297 void SwrInvalidateTiles(
1298 HANDLE hContext,
1299 uint32_t attachmentMask)
1300 {
1301 if (KNOB_TOSS_DRAW)
1302 {
1303 return;
1304 }
1305
1306 SWR_CONTEXT *pContext = GetContext(hContext);
1307 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1308
1309 pDC->FeWork.type = DISCARDINVALIDATETILES;
1310 pDC->FeWork.pfnWork = ProcessDiscardInvalidateTiles;
1311 pDC->FeWork.desc.discardInvalidateTiles.attachmentMask = attachmentMask;
1312 memset(&pDC->FeWork.desc.discardInvalidateTiles.rect, 0, sizeof(SWR_RECT));
1313 pDC->FeWork.desc.discardInvalidateTiles.newTileState = SWR_TILE_INVALID;
1314 pDC->FeWork.desc.discardInvalidateTiles.createNewTiles = false;
1315 pDC->FeWork.desc.discardInvalidateTiles.fullTilesOnly = false;
1316
1317 //enqueue
1318 QueueDraw(pContext);
1319 }
1320
1321 //////////////////////////////////////////////////////////////////////////
1322 /// @brief SwrDiscardRect
1323 /// @param hContext - Handle passed back from SwrCreateContext
1324 /// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to discard.
1325 /// @param rect - if rect is all zeros, the entire attachment surface will be discarded
1326 void SwrDiscardRect(
1327 HANDLE hContext,
1328 uint32_t attachmentMask,
1329 SWR_RECT rect)
1330 {
1331 if (KNOB_TOSS_DRAW)
1332 {
1333 return;
1334 }
1335
1336 SWR_CONTEXT *pContext = GetContext(hContext);
1337 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1338
1339 // Queue a load to the hottile
1340 pDC->FeWork.type = DISCARDINVALIDATETILES;
1341 pDC->FeWork.pfnWork = ProcessDiscardInvalidateTiles;
1342 pDC->FeWork.desc.discardInvalidateTiles.attachmentMask = attachmentMask;
1343 pDC->FeWork.desc.discardInvalidateTiles.rect = rect;
1344 pDC->FeWork.desc.discardInvalidateTiles.newTileState = SWR_TILE_RESOLVED;
1345 pDC->FeWork.desc.discardInvalidateTiles.createNewTiles = true;
1346 pDC->FeWork.desc.discardInvalidateTiles.fullTilesOnly = true;
1347
1348 //enqueue
1349 QueueDraw(pContext);
1350 }
1351
1352 //////////////////////////////////////////////////////////////////////////
1353 /// @brief SwrDispatch
1354 /// @param hContext - Handle passed back from SwrCreateContext
1355 /// @param threadGroupCountX - Number of thread groups dispatched in X direction
1356 /// @param threadGroupCountY - Number of thread groups dispatched in Y direction
1357 /// @param threadGroupCountZ - Number of thread groups dispatched in Z direction
1358 void SwrDispatch(
1359 HANDLE hContext,
1360 uint32_t threadGroupCountX,
1361 uint32_t threadGroupCountY,
1362 uint32_t threadGroupCountZ)
1363 {
1364 if (KNOB_TOSS_DRAW)
1365 {
1366 return;
1367 }
1368
1369 RDTSC_START(APIDispatch);
1370 SWR_CONTEXT *pContext = GetContext(hContext);
1371 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1372
1373 pDC->isCompute = true; // This is a compute context.
1374
1375 COMPUTE_DESC* pTaskData = (COMPUTE_DESC*)pDC->pArena->AllocAligned(sizeof(COMPUTE_DESC), 64);
1376
1377 pTaskData->threadGroupCountX = threadGroupCountX;
1378 pTaskData->threadGroupCountY = threadGroupCountY;
1379 pTaskData->threadGroupCountZ = threadGroupCountZ;
1380
1381 uint32_t totalThreadGroups = threadGroupCountX * threadGroupCountY * threadGroupCountZ;
1382 uint32_t dcIndex = pDC->drawId % KNOB_MAX_DRAWS_IN_FLIGHT;
1383 pDC->pDispatch = &pContext->pDispatchQueueArray[dcIndex];
1384 pDC->pDispatch->initialize(totalThreadGroups, pTaskData);
1385
1386 QueueDispatch(pContext);
1387 RDTSC_STOP(APIDispatch, threadGroupCountX * threadGroupCountY * threadGroupCountZ, 0);
1388 }
1389
1390 // Deswizzles, converts and stores current contents of the hot tiles to surface
1391 // described by pState
1392 void SwrStoreTiles(
1393 HANDLE hContext,
1394 SWR_RENDERTARGET_ATTACHMENT attachment,
1395 SWR_TILE_STATE postStoreTileState)
1396 {
1397 if (KNOB_TOSS_DRAW)
1398 {
1399 return;
1400 }
1401
1402 RDTSC_START(APIStoreTiles);
1403
1404 SWR_CONTEXT *pContext = GetContext(hContext);
1405 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1406
1407 SetupMacroTileScissors(pDC);
1408
1409 pDC->FeWork.type = STORETILES;
1410 pDC->FeWork.pfnWork = ProcessStoreTiles;
1411 pDC->FeWork.desc.storeTiles.attachment = attachment;
1412 pDC->FeWork.desc.storeTiles.postStoreTileState = postStoreTileState;
1413
1414 //enqueue
1415 QueueDraw(pContext);
1416
1417 RDTSC_STOP(APIStoreTiles, 0, 0);
1418 }
1419
1420 void SwrClearRenderTarget(
1421 HANDLE hContext,
1422 uint32_t clearMask,
1423 const float clearColor[4],
1424 float z,
1425 uint8_t stencil)
1426 {
1427 if (KNOB_TOSS_DRAW)
1428 {
1429 return;
1430 }
1431
1432 RDTSC_START(APIClearRenderTarget);
1433
1434 SWR_CONTEXT *pContext = GetContext(hContext);
1435
1436 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1437
1438 SetupMacroTileScissors(pDC);
1439
1440 CLEAR_FLAGS flags;
1441 flags.mask = clearMask;
1442
1443 pDC->FeWork.type = CLEAR;
1444 pDC->FeWork.pfnWork = ProcessClear;
1445 pDC->FeWork.desc.clear.flags = flags;
1446 pDC->FeWork.desc.clear.clearDepth = z;
1447 pDC->FeWork.desc.clear.clearRTColor[0] = clearColor[0];
1448 pDC->FeWork.desc.clear.clearRTColor[1] = clearColor[1];
1449 pDC->FeWork.desc.clear.clearRTColor[2] = clearColor[2];
1450 pDC->FeWork.desc.clear.clearRTColor[3] = clearColor[3];
1451 pDC->FeWork.desc.clear.clearStencil = stencil;
1452
1453 // enqueue draw
1454 QueueDraw(pContext);
1455
1456 RDTSC_STOP(APIClearRenderTarget, 0, pDC->drawId);
1457 }
1458
1459 //////////////////////////////////////////////////////////////////////////
1460 /// @brief Returns a pointer to the private context state for the current
1461 /// draw operation. This is used for external componets such as the
1462 /// sampler.
1463 /// SWR is responsible for the allocation of the private context state.
1464 /// @param hContext - Handle passed back from SwrCreateContext
1465 VOID* SwrGetPrivateContextState(
1466 HANDLE hContext)
1467 {
1468 SWR_CONTEXT* pContext = GetContext(hContext);
1469 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1470 DRAW_STATE* pState = pDC->pState;
1471
1472 if (pState->pPrivateState == nullptr)
1473 {
1474 pState->pPrivateState = pState->pArena->AllocAligned(pContext->privateStateSize, KNOB_SIMD_WIDTH*sizeof(float));
1475 }
1476
1477 return pState->pPrivateState;
1478 }
1479
1480 //////////////////////////////////////////////////////////////////////////
1481 /// @brief Clients can use this to allocate memory for draw/dispatch
1482 /// operations. The memory will automatically be freed once operation
1483 /// has completed. Client can use this to allocate binding tables,
1484 /// etc. needed for shader execution.
1485 /// @param hContext - Handle passed back from SwrCreateContext
1486 /// @param size - Size of allocation
1487 /// @param align - Alignment needed for allocation.
1488 VOID* SwrAllocDrawContextMemory(
1489 HANDLE hContext,
1490 uint32_t size,
1491 uint32_t align)
1492 {
1493 SWR_CONTEXT* pContext = GetContext(hContext);
1494 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1495
1496 return pDC->pState->pArena->AllocAligned(size, align);
1497 }
1498
1499 //////////////////////////////////////////////////////////////////////////
1500 /// @brief Returns pointer to SWR stats.
1501 /// @note The counters are atomically incremented by multiple threads.
1502 /// When calling this, you need to ensure all previous operations
1503 /// have completed.
1504 /// @todo If necessary, add a callback to avoid stalling the pipe to
1505 /// sample the counters.
1506 /// @param hContext - Handle passed back from SwrCreateContext
1507 /// @param pStats - SWR will fill this out for caller.
1508 void SwrGetStats(
1509 HANDLE hContext,
1510 SWR_STATS* pStats)
1511 {
1512 SWR_CONTEXT *pContext = GetContext(hContext);
1513 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1514
1515 pDC->FeWork.type = QUERYSTATS;
1516 pDC->FeWork.pfnWork = ProcessQueryStats;
1517 pDC->FeWork.desc.queryStats.pStats = pStats;
1518
1519 // cannot execute until all previous draws have completed
1520 pDC->dependent = true;
1521
1522 //enqueue
1523 QueueDraw(pContext);
1524 }
1525
1526 //////////////////////////////////////////////////////////////////////////
1527 /// @brief Enables stats counting
1528 /// @param hContext - Handle passed back from SwrCreateContext
1529 /// @param enable - If true then counts are incremented.
1530 void SwrEnableStats(
1531 HANDLE hContext,
1532 bool enable)
1533 {
1534 SWR_CONTEXT *pContext = GetContext(hContext);
1535 DRAW_CONTEXT* pDC = GetDrawContext(pContext);
1536
1537 pDC->pState->state.enableStats = enable;
1538 }
1539
1540 //////////////////////////////////////////////////////////////////////////
1541 /// @brief Mark end of frame - used for performance profiling
1542 /// @param hContext - Handle passed back from SwrCreateContext
1543 void SWR_API SwrEndFrame(
1544 HANDLE hContext)
1545 {
1546 RDTSC_ENDFRAME();
1547 SWR_CONTEXT *pContext = GetContext(hContext);
1548 pContext->frameCount++;
1549 }