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