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