#include "common/simdintrin.h"
#include "common/os.h"
-#include "archrast/archrast.h"
-
static const SWR_RECT g_MaxScissorRect = { 0, 0, KNOB_MAX_SCISSOR_X, KNOB_MAX_SCISSOR_Y };
void SetupDefaultState(SWR_CONTEXT *pContext);
return (SWR_CONTEXT*)hContext;
}
+void WakeAllThreads(SWR_CONTEXT *pContext)
+{
+ pContext->FifosNotEmpty.notify_all();
+}
+
//////////////////////////////////////////////////////////////////////////
/// @brief Create SWR Context.
/// @param pCreateInfo - pointer to creation info.
memset(pContextMem, 0, sizeof(SWR_CONTEXT));
SWR_CONTEXT *pContext = new (pContextMem) SWR_CONTEXT();
- pContext->driverType = pCreateInfo->driver;
pContext->privateStateSize = pCreateInfo->privateStateSize;
pContext->dcRing.Init(KNOB_MAX_DRAWS_IN_FLIGHT);
pContext->ppScratch = new uint8_t*[pContext->NumWorkerThreads];
pContext->pStats = new SWR_STATS[pContext->NumWorkerThreads];
+#if defined(KNOB_ENABLE_AR)
// Setup ArchRast thread contexts which includes +1 for API thread.
pContext->pArContext = new HANDLE[pContext->NumWorkerThreads+1];
- pContext->pArContext[pContext->NumWorkerThreads] = ArchRast::CreateThreadContext();
+ pContext->pArContext[pContext->NumWorkerThreads] = ArchRast::CreateThreadContext(ArchRast::AR_THREAD::API);
+#endif
// Allocate scratch space for workers.
///@note We could lazily allocate this but its rather small amount of memory.
pContext->ppScratch[i] = (uint8_t*)AlignedMalloc(32 * sizeof(KILOBYTE), KNOB_SIMD_WIDTH * 4);
#endif
+#if defined(KNOB_ENABLE_AR)
// Initialize worker thread context for ArchRast.
- pContext->pArContext[i] = ArchRast::CreateThreadContext();
+ pContext->pArContext[i] = ArchRast::CreateThreadContext(ArchRast::AR_THREAD::WORKER);
+#endif
}
// State setup AFTER context is fully initialized
pContext->pfnUpdateSoWriteOffset = pCreateInfo->pfnUpdateSoWriteOffset;
pContext->pfnUpdateStats = pCreateInfo->pfnUpdateStats;
pContext->pfnUpdateStatsFE = pCreateInfo->pfnUpdateStatsFE;
+
// pass pointer to bucket manager back to caller
#ifdef KNOB_ENABLE_RDTSC
pCreateInfo->contextSaveSize = sizeof(API_STATE);
- return (HANDLE)pContext;
-}
-
-void SwrDestroyContext(HANDLE hContext)
-{
- SWR_CONTEXT *pContext = GetContext(hContext);
- DestroyThreadPool(pContext, &pContext->threadPool);
-
- // free the fifos
- for (uint32_t i = 0; i < KNOB_MAX_DRAWS_IN_FLIGHT; ++i)
- {
- delete [] pContext->dcRing[i].dynState.pStats;
- delete pContext->dcRing[i].pArena;
- delete pContext->dsRing[i].pArena;
- pContext->pMacroTileManagerArray[i].~MacroTileMgr();
- pContext->pDispatchQueueArray[i].~DispatchQueue();
- }
-
- AlignedFree(pContext->pDispatchQueueArray);
- AlignedFree(pContext->pMacroTileManagerArray);
-
- // Free scratch space.
- for (uint32_t i = 0; i < pContext->NumWorkerThreads; ++i)
- {
-#if defined(_WIN32)
- VirtualFree(pContext->ppScratch[i], 0, MEM_RELEASE);
-#else
- AlignedFree(pContext->ppScratch[i]);
-#endif
-
- ArchRast::DestroyThreadContext(pContext->pArContext[i]);
- }
-
- delete [] pContext->ppScratch;
- delete [] pContext->pArContext;
- delete [] pContext->pStats;
-
- delete(pContext->pHotTileMgr);
+ StartThreadPool(pContext, &pContext->threadPool);
- pContext->~SWR_CONTEXT();
- AlignedFree(GetContext(hContext));
+ return (HANDLE)pContext;
}
void CopyState(DRAW_STATE& dst, const DRAW_STATE& src)
memcpy(&dst.state, &src.state, sizeof(API_STATE));
}
-void WakeAllThreads(SWR_CONTEXT *pContext)
-{
- pContext->FifosNotEmpty.notify_all();
-}
-
template<bool IsDraw>
void QueueWork(SWR_CONTEXT *pContext)
{
}
else
{
- RDTSC_START(APIDrawWakeAllThreads);
+ AR_API_BEGIN(APIDrawWakeAllThreads, pDC->drawId);
WakeAllThreads(pContext);
- RDTSC_STOP(APIDrawWakeAllThreads, 1, 0);
+ AR_API_END(APIDrawWakeAllThreads, 1);
}
// Set current draw context to NULL so that next state call forces a new draw context to be created and populated.
DRAW_CONTEXT* GetDrawContext(SWR_CONTEXT *pContext, bool isSplitDraw = false)
{
- RDTSC_START(APIGetDrawContext);
+ AR_API_BEGIN(APIGetDrawContext, 0);
// If current draw context is null then need to obtain a new draw context to use from ring.
if (pContext->pCurDrawContext == nullptr)
{
SWR_ASSERT(pCurDrawContext->pArena->IsEmpty() == true);
+ // Reset dependency
pCurDrawContext->dependent = false;
+ pCurDrawContext->dependentFE = false;
+
pCurDrawContext->pContext = pContext;
pCurDrawContext->isCompute = false; // Dispatch has to set this to true.
pCurDrawContext->drawId = pContext->dcRing.GetHead();
pCurDrawContext->cleanupState = true;
+
}
else
{
SWR_ASSERT(isSplitDraw == false, "Split draw should only be used when obtaining a new DC");
}
- RDTSC_STOP(APIGetDrawContext, 0, 0);
+ AR_API_END(APIGetDrawContext, 0);
return pContext->pCurDrawContext;
}
return &pDC->pState->state;
}
+void SwrDestroyContext(HANDLE hContext)
+{
+ SWR_CONTEXT *pContext = GetContext(hContext);
+ DRAW_CONTEXT* pDC = GetDrawContext(pContext);
+
+ pDC->FeWork.type = SHUTDOWN;
+ pDC->FeWork.pfnWork = ProcessShutdown;
+
+ //enqueue
+ QueueDraw(pContext);
+
+ DestroyThreadPool(pContext, &pContext->threadPool);
+
+ // free the fifos
+ for (uint32_t i = 0; i < KNOB_MAX_DRAWS_IN_FLIGHT; ++i)
+ {
+ delete[] pContext->dcRing[i].dynState.pStats;
+ delete pContext->dcRing[i].pArena;
+ delete pContext->dsRing[i].pArena;
+ pContext->pMacroTileManagerArray[i].~MacroTileMgr();
+ pContext->pDispatchQueueArray[i].~DispatchQueue();
+ }
+
+ AlignedFree(pContext->pDispatchQueueArray);
+ AlignedFree(pContext->pMacroTileManagerArray);
+
+ // Free scratch space.
+ for (uint32_t i = 0; i < pContext->NumWorkerThreads; ++i)
+ {
+#if defined(_WIN32)
+ VirtualFree(pContext->ppScratch[i], 0, MEM_RELEASE);
+#else
+ AlignedFree(pContext->ppScratch[i]);
+#endif
+
+#if defined(KNOB_ENABLE_AR)
+ ArchRast::DestroyThreadContext(pContext->pArContext[i]);
+#endif
+ }
+
+ delete[] pContext->ppScratch;
+ delete[] pContext->pStats;
+
+ delete(pContext->pHotTileMgr);
+
+ pContext->~SWR_CONTEXT();
+ AlignedFree(GetContext(hContext));
+}
+
void SWR_API SwrSaveState(
HANDLE hContext,
void* pOutputStateBlock,
pState->rastState.cullMode = SWR_CULLMODE_NONE;
pState->rastState.frontWinding = SWR_FRONTWINDING_CCW;
+
+ pState->depthBoundsState.depthBoundsTestEnable = false;
+ pState->depthBoundsState.depthBoundsTestMinValue = 0.0f;
+ pState->depthBoundsState.depthBoundsTestMaxValue = 1.0f;
}
void SwrSync(HANDLE hContext, PFN_CALLBACK_FUNC pfnFunc, uint64_t userData, uint64_t userData2, uint64_t userData3)
{
- RDTSC_START(APISync);
-
SWR_ASSERT(pfnFunc != nullptr);
SWR_CONTEXT *pContext = GetContext(hContext);
DRAW_CONTEXT* pDC = GetDrawContext(pContext);
+ AR_API_BEGIN(APISync, 0);
+
pDC->FeWork.type = SYNC;
pDC->FeWork.pfnWork = ProcessSync;
//enqueue
QueueDraw(pContext);
- RDTSC_STOP(APISync, 1, 0);
+ AR_API_END(APISync, 1);
}
void SwrWaitForIdle(HANDLE hContext)
{
SWR_CONTEXT *pContext = GetContext(hContext);
- RDTSC_START(APIWaitForIdle);
+ AR_API_BEGIN(APIWaitForIdle, 0);
while (!pContext->dcRing.IsEmpty())
{
_mm_pause();
}
- RDTSC_STOP(APIWaitForIdle, 1, 0);
+ AR_API_END(APIWaitForIdle, 1);
}
void SwrWaitForIdleFE(HANDLE hContext)
{
SWR_CONTEXT *pContext = GetContext(hContext);
- RDTSC_START(APIWaitForIdle);
+ AR_API_BEGIN(APIWaitForIdle, 0);
while (pContext->drawsOutstandingFE > 0)
{
_mm_pause();
}
- RDTSC_STOP(APIWaitForIdle, 1, 0);
+ AR_API_END(APIWaitForIdle, 1);
}
void SwrSetVertexBuffers(
pState->backendState = *pBEState;
}
+void SwrSetDepthBoundsState(
+ HANDLE hContext,
+ SWR_DEPTH_BOUNDS_STATE *pDBState)
+{
+ API_STATE* pState = GetDrawState(GetContext(hContext));
+
+ pState->depthBoundsState = *pDBState;
+}
+
void SwrSetPixelShaderState(
HANDLE hContext,
SWR_PS_STATE *pPSState)
API_STATE* pState = GetDrawState(pContext);
memcpy(&pState->vp[0], pViewports, sizeof(SWR_VIEWPORT) * numViewports);
-
- if (pMatrices != nullptr)
- {
- // @todo Faster to copy portions of the SOA or just copy all of it?
- memcpy(&pState->vpMatrices, pMatrices, sizeof(SWR_VIEWPORT_MATRICES));
- }
- else
- {
- // Compute default viewport transform.
- for (uint32_t i = 0; i < numViewports; ++i)
- {
- if (pContext->driverType == DX)
- {
- pState->vpMatrices.m00[i] = pState->vp[i].width / 2.0f;
- pState->vpMatrices.m11[i] = -pState->vp[i].height / 2.0f;
- pState->vpMatrices.m22[i] = pState->vp[i].maxZ - pState->vp[i].minZ;
- pState->vpMatrices.m30[i] = pState->vp[i].x + pState->vpMatrices.m00[i];
- pState->vpMatrices.m31[i] = pState->vp[i].y - pState->vpMatrices.m11[i];
- pState->vpMatrices.m32[i] = pState->vp[i].minZ;
- }
- else
- {
- // Standard, with the exception that Y is inverted.
- pState->vpMatrices.m00[i] = (pState->vp[i].width - pState->vp[i].x) / 2.0f;
- pState->vpMatrices.m11[i] = (pState->vp[i].y - pState->vp[i].height) / 2.0f;
- pState->vpMatrices.m22[i] = (pState->vp[i].maxZ - pState->vp[i].minZ) / 2.0f;
- pState->vpMatrices.m30[i] = pState->vp[i].x + pState->vpMatrices.m00[i];
- pState->vpMatrices.m31[i] = pState->vp[i].height + pState->vpMatrices.m11[i];
- pState->vpMatrices.m32[i] = pState->vp[i].minZ + pState->vpMatrices.m22[i];
-
- // Now that the matrix is calculated, clip the view coords to screen size.
- // OpenGL allows for -ve x,y in the viewport.
- pState->vp[i].x = std::max(pState->vp[i].x, 0.0f);
- pState->vp[i].y = std::max(pState->vp[i].y, 0.0f);
- }
- }
- }
+ // @todo Faster to copy portions of the SOA or just copy all of it?
+ memcpy(&pState->vpMatrices, pMatrices, sizeof(SWR_VIEWPORT_MATRICES));
updateGuardbands(pState);
}
extern PFN_BACKEND_FUNC gBackendSampleRateTable[SWR_MULTISAMPLE_TYPE_COUNT][SWR_INPUT_COVERAGE_COUNT][2][2];
void SetupPipeline(DRAW_CONTEXT *pDC)
{
+ SWR_CONTEXT* pContext = pDC->pContext;
DRAW_STATE* pState = pDC->pState;
const SWR_RASTSTATE &rastState = pState->state.rastState;
const SWR_PS_STATE &psState = pState->state.psState;
break;
};
+
// disable clipper if viewport transform is disabled
if (pState->state.frontendState.vpTransformDisable)
{
pState->pfnProcessPrims = nullptr;
}
+
// set up the frontend attribute count
pState->state.feNumAttributes = 0;
const SWR_BACKEND_STATE& backendState = pState->state.backendState;
// have to check for the special case where depth/stencil test is enabled but depthwrite is disabled.
pState->state.depthHottileEnable = ((!(pState->state.depthStencilState.depthTestEnable &&
!pState->state.depthStencilState.depthWriteEnable &&
+ !pState->state.depthBoundsState.depthBoundsTestEnable &&
pState->state.depthStencilState.depthTestFunc == ZFUNC_ALWAYS)) &&
(pState->state.depthStencilState.depthTestEnable ||
- pState->state.depthStencilState.depthWriteEnable)) ? true : false;
+ pState->state.depthStencilState.depthWriteEnable ||
+ pState->state.depthBoundsState.depthBoundsTestEnable)) ? true : false;
pState->state.stencilHottileEnable = (((!(pState->state.depthStencilState.stencilTestEnable &&
!pState->state.depthStencilState.stencilWriteEnable &&
SetupMacroTileScissors(pDC);
SetupPipeline(pDC);
}
+
+
}
//////////////////////////////////////////////////////////////////////////
return;
}
- RDTSC_START(APIDraw);
-
SWR_CONTEXT *pContext = GetContext(hContext);
DRAW_CONTEXT* pDC = GetDrawContext(pContext);
+ AR_API_BEGIN(APIDraw, pDC->drawId);
+ AR_API_EVENT(DrawInstancedEvent(pDC->drawId, topology, numVertices, startVertex, numInstances, startInstance));
+
uint32_t maxVertsPerDraw = MaxVertsPerDraw(pDC, numVertices, topology);
uint32_t primsPerDraw = GetNumPrims(topology, maxVertsPerDraw);
uint32_t remainingVerts = numVertices;
pState->rastState.cullMode = SWR_CULLMODE_NONE;
pState->forceFront = true;
}
+ else if (topology == TOP_RECT_LIST)
+ {
+ pState->rastState.cullMode = SWR_CULLMODE_NONE;
+ }
+
int draw = 0;
while (remainingVerts)
pDC = GetDrawContext(pContext);
pDC->pState->state.rastState.cullMode = oldCullMode;
- RDTSC_STOP(APIDraw, numVertices * numInstances, 0);
+
+ AR_API_END(APIDraw, numVertices * numInstances);
}
//////////////////////////////////////////////////////////////////////////
return;
}
- RDTSC_START(APIDrawIndexed);
-
SWR_CONTEXT *pContext = GetContext(hContext);
DRAW_CONTEXT* pDC = GetDrawContext(pContext);
API_STATE* pState = &pDC->pState->state;
- AR_BEGIN(AR_API_CTX, APIDrawIndexed, pDC->drawId);
- AR_EVENT(AR_API_CTX, DrawIndexedInstance(topology, numIndices, indexOffset, baseVertex, numInstances, startInstance));
+ AR_API_BEGIN(APIDrawIndexed, pDC->drawId);
+ AR_API_EVENT(DrawIndexedInstancedEvent(pDC->drawId, topology, numIndices, indexOffset, baseVertex, numInstances, startInstance));
uint32_t maxIndicesPerDraw = MaxVertsPerDraw(pDC, numIndices, topology);
uint32_t primsPerDraw = GetNumPrims(topology, maxIndicesPerDraw);
pState->rastState.cullMode = SWR_CULLMODE_NONE;
pState->forceFront = true;
}
+ else if (topology == TOP_RECT_LIST)
+ {
+ pState->rastState.cullMode = SWR_CULLMODE_NONE;
+ }
+
while (remainingIndices)
{
// When breaking up draw, we need to obtain new draw context for each iteration.
bool isSplitDraw = (draw > 0) ? true : false;
+
pDC = GetDrawContext(pContext, isSplitDraw);
InitDraw(pDC, isSplitDraw);
draw++;
}
- // restore culling state
+ // Restore culling state
pDC = GetDrawContext(pContext);
pDC->pState->state.rastState.cullMode = oldCullMode;
+
- AR_END(AR_API_CTX, APIDrawIndexed, numIndices * numInstances);
- RDTSC_STOP(APIDrawIndexed, numIndices * numInstances, 0);
+ AR_API_END(APIDrawIndexed, numIndices * numInstances);
}
return;
}
- RDTSC_START(APIDispatch);
SWR_CONTEXT *pContext = GetContext(hContext);
DRAW_CONTEXT* pDC = GetDrawContext(pContext);
+ AR_API_BEGIN(APIDispatch, pDC->drawId);
+ AR_API_EVENT(DispatchEvent(pDC->drawId, threadGroupCountX, threadGroupCountY, threadGroupCountZ));
pDC->isCompute = true; // This is a compute context.
COMPUTE_DESC* pTaskData = (COMPUTE_DESC*)pDC->pArena->AllocAligned(sizeof(COMPUTE_DESC), 64);
uint32_t totalThreadGroups = threadGroupCountX * threadGroupCountY * threadGroupCountZ;
uint32_t dcIndex = pDC->drawId % KNOB_MAX_DRAWS_IN_FLIGHT;
pDC->pDispatch = &pContext->pDispatchQueueArray[dcIndex];
- pDC->pDispatch->initialize(totalThreadGroups, pTaskData);
+ pDC->pDispatch->initialize(totalThreadGroups, pTaskData, &ProcessComputeBE);
QueueDispatch(pContext);
- RDTSC_STOP(APIDispatch, threadGroupCountX * threadGroupCountY * threadGroupCountZ, 0);
+ AR_API_END(APIDispatch, threadGroupCountX * threadGroupCountY * threadGroupCountZ);
}
// Deswizzles, converts and stores current contents of the hot tiles to surface
// described by pState
void SWR_API SwrStoreTiles(
HANDLE hContext,
- SWR_RENDERTARGET_ATTACHMENT attachment,
+ uint32_t attachmentMask,
SWR_TILE_STATE postStoreTileState,
const SWR_RECT& storeRect)
{
return;
}
- RDTSC_START(APIStoreTiles);
-
SWR_CONTEXT *pContext = GetContext(hContext);
DRAW_CONTEXT* pDC = GetDrawContext(pContext);
+ AR_API_BEGIN(APIStoreTiles, pDC->drawId);
+
pDC->FeWork.type = STORETILES;
pDC->FeWork.pfnWork = ProcessStoreTiles;
- pDC->FeWork.desc.storeTiles.attachment = attachment;
+ pDC->FeWork.desc.storeTiles.attachmentMask = attachmentMask;
pDC->FeWork.desc.storeTiles.postStoreTileState = postStoreTileState;
pDC->FeWork.desc.storeTiles.rect = storeRect;
pDC->FeWork.desc.storeTiles.rect &= g_MaxScissorRect;
//enqueue
QueueDraw(pContext);
- RDTSC_STOP(APIStoreTiles, 0, 0);
+ AR_API_END(APIStoreTiles, 1);
}
//////////////////////////////////////////////////////////////////////////
/// @brief SwrClearRenderTarget - Clear attached render targets / depth / stencil
/// @param hContext - Handle passed back from SwrCreateContext
-/// @param clearMask - combination of SWR_CLEAR_COLOR / SWR_CLEAR_DEPTH / SWR_CLEAR_STENCIL flags (or SWR_CLEAR_NONE)
+/// @param attachmentMask - combination of SWR_ATTACHMENT_*_BIT attachments to clear
/// @param clearColor - color use for clearing render targets
/// @param z - depth value use for clearing depth buffer
/// @param stencil - stencil value used for clearing stencil buffer
/// @param clearRect - The pixel-coordinate rectangle to clear in all cleared buffers
void SWR_API SwrClearRenderTarget(
HANDLE hContext,
- uint32_t clearMask,
+ uint32_t attachmentMask,
const float clearColor[4],
float z,
uint8_t stencil,
return;
}
- RDTSC_START(APIClearRenderTarget);
-
SWR_CONTEXT *pContext = GetContext(hContext);
DRAW_CONTEXT* pDC = GetDrawContext(pContext);
- CLEAR_FLAGS flags;
- flags.bits = 0;
- flags.mask = clearMask;
+ AR_API_BEGIN(APIClearRenderTarget, pDC->drawId);
pDC->FeWork.type = CLEAR;
pDC->FeWork.pfnWork = ProcessClear;
pDC->FeWork.desc.clear.rect = clearRect;
pDC->FeWork.desc.clear.rect &= g_MaxScissorRect;
- pDC->FeWork.desc.clear.flags = flags;
+ pDC->FeWork.desc.clear.attachmentMask = attachmentMask;
pDC->FeWork.desc.clear.clearDepth = z;
pDC->FeWork.desc.clear.clearRTColor[0] = clearColor[0];
pDC->FeWork.desc.clear.clearRTColor[1] = clearColor[1];
// enqueue draw
QueueDraw(pContext);
- RDTSC_STOP(APIClearRenderTarget, 0, pDC->drawId);
+ AR_API_END(APIClearRenderTarget, 1);
}
//////////////////////////////////////////////////////////////////////////
/// @brief Enables stats counting
/// @param hContext - Handle passed back from SwrCreateContext
/// @param enable - If true then counts are incremented.
-void SwrEnableStats(
+void SwrEnableStatsFE(
HANDLE hContext,
bool enable)
{
SWR_CONTEXT *pContext = GetContext(hContext);
DRAW_CONTEXT* pDC = GetDrawContext(pContext);
- pDC->pState->state.enableStats = enable;
+ pDC->pState->state.enableStatsFE = enable;
+}
+
+//////////////////////////////////////////////////////////////////////////
+/// @brief Enables stats counting
+/// @param hContext - Handle passed back from SwrCreateContext
+/// @param enable - If true then counts are incremented.
+void SwrEnableStatsBE(
+ HANDLE hContext,
+ bool enable)
+{
+ SWR_CONTEXT *pContext = GetContext(hContext);
+ DRAW_CONTEXT* pDC = GetDrawContext(pContext);
+
+ pDC->pState->state.enableStatsBE = enable;
}
//////////////////////////////////////////////////////////////////////////
void SWR_API SwrEndFrame(
HANDLE hContext)
{
- RDTSC_ENDFRAME();
SWR_CONTEXT *pContext = GetContext(hContext);
+ DRAW_CONTEXT* pDC = GetDrawContext(pContext);
+
+ RDTSC_ENDFRAME();
+ AR_API_EVENT(FrameEndEvent(pContext->frameCount, pDC->drawId));
+
pContext->frameCount++;
}
+