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