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