FIXME fixups, mostly for contextLock

d3d11-revived
Caleb Cornett 2024-02-10 21:49:19 -06:00
parent 54ffdc180d
commit 506c2e4819
1 changed files with 28 additions and 15 deletions

View File

@ -484,8 +484,7 @@ typedef struct D3D11CommandBuffer
/* Compute Pass */ /* Compute Pass */
D3D11ComputePipeline *computePipeline; D3D11ComputePipeline *computePipeline;
/* State */ /* Fences */
SDL_threadID threadID;
D3D11Fence *fence; D3D11Fence *fence;
uint8_t autoReleaseFence; uint8_t autoReleaseFence;
@ -686,6 +685,7 @@ static void D3D11_DestroyDevice(
{ {
D3D11CommandBuffer *commandBuffer = renderer->availableCommandBuffers[i]; D3D11CommandBuffer *commandBuffer = renderer->availableCommandBuffers[i];
ID3D11DeviceContext_Release(commandBuffer->context); ID3D11DeviceContext_Release(commandBuffer->context);
SDL_free(commandBuffer->boundUniformBuffers);
SDL_free(commandBuffer); SDL_free(commandBuffer);
} }
SDL_free(renderer->availableCommandBuffers); SDL_free(renderer->availableCommandBuffers);
@ -2003,6 +2003,7 @@ static void D3D11_GetBufferData(
ERROR_CHECK_RETURN("Could not create staging buffer for readback", ); ERROR_CHECK_RETURN("Could not create staging buffer for readback", );
/* Copy data into staging buffer */ /* Copy data into staging buffer */
SDL_LockMutex(renderer->contextLock);
ID3D11DeviceContext_CopySubresourceRegion( ID3D11DeviceContext_CopySubresourceRegion(
renderer->immediateContext, renderer->immediateContext,
stagingBuffer, stagingBuffer,
@ -2032,6 +2033,7 @@ static void D3D11_GetBufferData(
res res
); );
ID3D11Buffer_Release(stagingBuffer); ID3D11Buffer_Release(stagingBuffer);
SDL_UnlockMutex(renderer->contextLock);
return; return;
} }
@ -2042,6 +2044,7 @@ static void D3D11_GetBufferData(
stagingBuffer, stagingBuffer,
0 0
); );
SDL_UnlockMutex(renderer->contextLock);
/* Clean up the staging buffer */ /* Clean up the staging buffer */
ID3D11Resource_Release(stagingBuffer); ID3D11Resource_Release(stagingBuffer);
@ -2603,15 +2606,12 @@ static Refresh_CommandBuffer* D3D11_AcquireCommandBuffer(
SDL_LockMutex(renderer->acquireCommandBufferLock); SDL_LockMutex(renderer->acquireCommandBufferLock);
commandBuffer = D3D11_INTERNAL_GetInactiveCommandBufferFromPool(renderer); commandBuffer = D3D11_INTERNAL_GetInactiveCommandBufferFromPool(renderer);
commandBuffer->threadID = SDL_ThreadID();
commandBuffer->swapchainData = NULL; commandBuffer->swapchainData = NULL;
commandBuffer->graphicsPipeline = NULL; commandBuffer->graphicsPipeline = NULL;
commandBuffer->computePipeline = NULL; commandBuffer->computePipeline = NULL;
commandBuffer->vertexUniformBuffer = NULL; commandBuffer->vertexUniformBuffer = NULL;
commandBuffer->fragmentUniformBuffer = NULL; commandBuffer->fragmentUniformBuffer = NULL;
commandBuffer->computeUniformBuffer = NULL; commandBuffer->computeUniformBuffer = NULL;
for (uint32_t i = 0; i < MAX_COLOR_TARGET_BINDINGS; i += 1) for (uint32_t i = 0; i < MAX_COLOR_TARGET_BINDINGS; i += 1)
{ {
commandBuffer->colorTargets[i] = NullTargetBinding; commandBuffer->colorTargets[i] = NullTargetBinding;
@ -3182,7 +3182,7 @@ static void D3D11_BindComputeBuffers(
) { ) {
D3D11Renderer *renderer = (D3D11Renderer*) driverData; D3D11Renderer *renderer = (D3D11Renderer*) driverData;
D3D11CommandBuffer *d3d11CommandBuffer = (D3D11CommandBuffer*) commandBuffer; D3D11CommandBuffer *d3d11CommandBuffer = (D3D11CommandBuffer*) commandBuffer;
ID3D11UnorderedAccessView* uavs[MAX_BUFFER_BINDINGS]; /* FIXME: Is this limit right? */ ID3D11UnorderedAccessView* uavs[MAX_BUFFER_BINDINGS];
int32_t numBuffers = d3d11CommandBuffer->computePipeline->numBuffers; int32_t numBuffers = d3d11CommandBuffer->computePipeline->numBuffers;
@ -3207,7 +3207,7 @@ static void D3D11_BindComputeTextures(
) { ) {
D3D11Renderer *renderer = (D3D11Renderer*) driverData; D3D11Renderer *renderer = (D3D11Renderer*) driverData;
D3D11CommandBuffer *d3d11CommandBuffer = (D3D11CommandBuffer*) commandBuffer; D3D11CommandBuffer *d3d11CommandBuffer = (D3D11CommandBuffer*) commandBuffer;
ID3D11UnorderedAccessView *uavs[MAX_TEXTURE_SAMPLERS]; /* FIXME: Is this limit right? */ ID3D11UnorderedAccessView *uavs[MAX_TEXTURE_SAMPLERS];
int32_t numTextures = d3d11CommandBuffer->computePipeline->numTextures; int32_t numTextures = d3d11CommandBuffer->computePipeline->numTextures;
@ -3709,16 +3709,23 @@ static void D3D11_INTERNAL_WaitForFence(
D3D11Fence *fence D3D11Fence *fence
) { ) {
BOOL queryData; BOOL queryData;
HRESULT res;
while (S_OK != ID3D11DeviceContext_GetData( SDL_LockMutex(renderer->contextLock);
res = ID3D11DeviceContext_GetData(
renderer->immediateContext, renderer->immediateContext,
(ID3D11Asynchronous*) fence->handle, (ID3D11Asynchronous*) fence->handle,
&queryData, &queryData,
sizeof(queryData), sizeof(queryData),
0 0
)) { );
while (res != S_OK)
{
/* Spin until we get a result back... */ /* Spin until we get a result back... */
} }
SDL_UnlockMutex(renderer->contextLock);
} }
static void D3D11_Submit( static void D3D11_Submit(
@ -3730,8 +3737,6 @@ static void D3D11_Submit(
ID3D11CommandList *commandList; ID3D11CommandList *commandList;
HRESULT res; HRESULT res;
/* FIXME: Should add sanity check that current thread ID matches the command buffer's threadID. */
SDL_LockMutex(renderer->contextLock); SDL_LockMutex(renderer->contextLock);
/* Notify the command buffer completion query that we have completed recording */ /* Notify the command buffer completion query that we have completed recording */
@ -3799,7 +3804,6 @@ static void D3D11_Submit(
for (int32_t i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) for (int32_t i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1)
{ {
BOOL queryData; BOOL queryData;
res = ID3D11DeviceContext_GetData( res = ID3D11DeviceContext_GetData(
renderer->immediateContext, renderer->immediateContext,
(ID3D11Asynchronous*) renderer->submittedCommandBuffers[i]->fence->handle, (ID3D11Asynchronous*) renderer->submittedCommandBuffers[i]->fence->handle,
@ -3850,7 +3854,7 @@ static void D3D11_Wait(
); );
} }
SDL_LockMutex(renderer->contextLock); SDL_LockMutex(renderer->contextLock); /* This effectively acts as a lock around submittedCommandBuffers */
for (int32_t i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1) for (int32_t i = renderer->submittedCommandBufferCount - 1; i >= 0; i -= 1)
{ {
@ -3882,6 +3886,8 @@ static void D3D11_WaitForFences(
} }
else else
{ {
SDL_LockMutex(renderer->contextLock);
while (res != S_OK) while (res != S_OK)
{ {
for (uint32_t i = 0; i < fenceCount; i += 1) for (uint32_t i = 0; i < fenceCount; i += 1)
@ -3900,6 +3906,8 @@ static void D3D11_WaitForFences(
} }
} }
} }
SDL_UnlockMutex(renderer->contextLock);
} }
} }
@ -3910,8 +3918,11 @@ static int D3D11_QueryFence(
D3D11Renderer *renderer = (D3D11Renderer*) driverData; D3D11Renderer *renderer = (D3D11Renderer*) driverData;
D3D11Fence *d3d11Fence = (D3D11Fence*) fence; D3D11Fence *d3d11Fence = (D3D11Fence*) fence;
BOOL queryData; BOOL queryData;
HRESULT res;
HRESULT res = ID3D11DeviceContext_GetData( SDL_LockMutex(renderer->contextLock);
res = ID3D11DeviceContext_GetData(
renderer->immediateContext, renderer->immediateContext,
(ID3D11Asynchronous*) d3d11Fence->handle, (ID3D11Asynchronous*) d3d11Fence->handle,
&queryData, &queryData,
@ -3919,6 +3930,8 @@ static int D3D11_QueryFence(
0 0
); );
SDL_UnlockMutex(renderer->contextLock);
return res == S_OK; return res == S_OK;
} }
@ -4289,7 +4302,7 @@ tryCreateDevice:
Refresh_LogInfo("D3D11 Adapter: %S", adapterDesc.Description); Refresh_LogInfo("D3D11 Adapter: %S", adapterDesc.Description);
/* Create mutexes */ /* Create mutexes */
renderer->contextLock = SDL_CreateMutex(); /* FIXME: We should be using this *everywhere* the immediate context is accessed! */ renderer->contextLock = SDL_CreateMutex();
renderer->acquireCommandBufferLock = SDL_CreateMutex(); renderer->acquireCommandBufferLock = SDL_CreateMutex();
renderer->uniformBufferLock = SDL_CreateMutex(); renderer->uniformBufferLock = SDL_CreateMutex();
renderer->fenceLock = SDL_CreateMutex(); renderer->fenceLock = SDL_CreateMutex();