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