Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track UBOs on acquisition from pool #86

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 12 additions & 24 deletions src/gpu/d3d11/SDL_gpu_d3d11.c
Original file line number Diff line number Diff line change
Expand Up @@ -3352,8 +3352,10 @@ static SDL_GpuCommandBuffer *D3D11_AcquireCommandBuffer(
return (SDL_GpuCommandBuffer *)commandBuffer;
}

static D3D11UniformBuffer *D3D11_INTERNAL_AcquireUniformBufferFromPool(D3D11Renderer *renderer)
static D3D11UniformBuffer *D3D11_INTERNAL_AcquireUniformBufferFromPool(
D3D11CommandBuffer *commandBuffer)
{
D3D11Renderer *renderer = commandBuffer->renderer;
D3D11UniformBuffer *uniformBuffer;

SDL_LockMutex(renderer->acquireUniformBufferLock);
Expand All @@ -3369,6 +3371,8 @@ static D3D11UniformBuffer *D3D11_INTERNAL_AcquireUniformBufferFromPool(D3D11Rend

SDL_UnlockMutex(renderer->acquireUniformBufferLock);

D3D11_INTERNAL_TrackUniformBuffer(commandBuffer, uniformBuffer);

return uniformBuffer;
}

Expand Down Expand Up @@ -3406,19 +3410,19 @@ static void D3D11_INTERNAL_PushUniformData(
if (shaderStage == SDL_GPU_SHADERSTAGE_VERTEX) {
if (d3d11CommandBuffer->vertexUniformBuffers[slotIndex] == NULL) {
d3d11CommandBuffer->vertexUniformBuffers[slotIndex] = D3D11_INTERNAL_AcquireUniformBufferFromPool(
d3d11CommandBuffer->renderer);
d3d11CommandBuffer);
}
d3d11UniformBuffer = d3d11CommandBuffer->vertexUniformBuffers[slotIndex];
} else if (shaderStage == SDL_GPU_SHADERSTAGE_FRAGMENT) {
if (d3d11CommandBuffer->fragmentUniformBuffers[slotIndex] == NULL) {
d3d11CommandBuffer->fragmentUniformBuffers[slotIndex] = D3D11_INTERNAL_AcquireUniformBufferFromPool(
d3d11CommandBuffer->renderer);
d3d11CommandBuffer);
}
d3d11UniformBuffer = d3d11CommandBuffer->fragmentUniformBuffers[slotIndex];
} else if (shaderStage == SDL_GPU_SHADERSTAGE_COMPUTE) {
if (d3d11CommandBuffer->computeUniformBuffers[slotIndex] == NULL) {
d3d11CommandBuffer->computeUniformBuffers[slotIndex] = D3D11_INTERNAL_AcquireUniformBufferFromPool(
d3d11CommandBuffer->renderer);
d3d11CommandBuffer);
}
d3d11UniformBuffer = d3d11CommandBuffer->computeUniformBuffers[slotIndex];
} else {
Expand All @@ -3439,11 +3443,7 @@ static void D3D11_INTERNAL_PushUniformData(
0);
d3d11UniformBuffer->mappedData = NULL;

D3D11_INTERNAL_TrackUniformBuffer(
d3d11CommandBuffer,
d3d11UniformBuffer);

d3d11UniformBuffer = D3D11_INTERNAL_AcquireUniformBufferFromPool(d3d11CommandBuffer->renderer);
d3d11UniformBuffer = D3D11_INTERNAL_AcquireUniformBufferFromPool(d3d11CommandBuffer);

d3d11UniformBuffer->drawOffset = 0;
d3d11UniformBuffer->writeOffset = 0;
Expand Down Expand Up @@ -3705,14 +3705,14 @@ static void D3D11_BindGraphicsPipeline(
for (Uint32 i = 0; i < pipeline->vertexUniformBufferCount; i += 1) {
if (d3d11CommandBuffer->vertexUniformBuffers[i] == NULL) {
d3d11CommandBuffer->vertexUniformBuffers[i] = D3D11_INTERNAL_AcquireUniformBufferFromPool(
d3d11CommandBuffer->renderer);
d3d11CommandBuffer);
}
}

for (Uint32 i = 0; i < pipeline->fragmentUniformBufferCount; i += 1) {
if (d3d11CommandBuffer->fragmentUniformBuffers[i] == NULL) {
d3d11CommandBuffer->fragmentUniformBuffers[i] = D3D11_INTERNAL_AcquireUniformBufferFromPool(
d3d11CommandBuffer->renderer);
d3d11CommandBuffer);
}
}

Expand Down Expand Up @@ -4033,10 +4033,6 @@ static void D3D11_INTERNAL_BindGraphicsResources(
&commandBuffer->vertexUniformBuffers[i]->buffer,
&offsetInConstants,
&blockSizeInConstants);

D3D11_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->vertexUniformBuffers[i]);
}

commandBuffer->needVertexUniformBufferBind = SDL_FALSE;
Expand Down Expand Up @@ -4087,10 +4083,6 @@ static void D3D11_INTERNAL_BindGraphicsResources(
&commandBuffer->fragmentUniformBuffers[i]->buffer,
&offsetInConstants,
&blockSizeInConstants);

D3D11_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->fragmentUniformBuffers[i]);
}

commandBuffer->needFragmentUniformBufferBind = SDL_FALSE;
Expand Down Expand Up @@ -4415,7 +4407,7 @@ static void D3D11_BindComputePipeline(
for (Uint32 i = 0; i < pipeline->uniformBufferCount; i += 1) {
if (d3d11CommandBuffer->computeUniformBuffers[i] == NULL) {
d3d11CommandBuffer->computeUniformBuffers[i] = D3D11_INTERNAL_AcquireUniformBufferFromPool(
d3d11CommandBuffer->renderer);
d3d11CommandBuffer);
}
}

Expand Down Expand Up @@ -4547,10 +4539,6 @@ static void D3D11_INTERNAL_BindComputeResources(
&commandBuffer->computeUniformBuffers[i]->buffer,
&offsetInConstants,
&blockSizeInConstants);

D3D11_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->computeUniformBuffers[i]);
}
commandBuffer->needComputeUniformBufferBind = SDL_FALSE;
}
Expand Down
35 changes: 11 additions & 24 deletions src/gpu/metal/SDL_gpu_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -2062,8 +2062,9 @@ static Uint8 METAL_INTERNAL_AcquireFence(
}

static MetalUniformBuffer *METAL_INTERNAL_AcquireUniformBufferFromPool(
MetalRenderer *renderer)
MetalCommandBuffer *commandBuffer)
{
MetalRenderer *renderer = commandBuffer->renderer;
MetalUniformBuffer *uniformBuffer;

SDL_LockMutex(renderer->acquireUniformBufferLock);
Expand All @@ -2079,6 +2080,8 @@ static Uint8 METAL_INTERNAL_AcquireFence(

SDL_UnlockMutex(renderer->acquireUniformBufferLock);

METAL_INTERNAL_TrackUniformBuffer(commandBuffer, uniformBuffer);

return uniformBuffer;
}

Expand Down Expand Up @@ -2268,14 +2271,14 @@ static void METAL_BindGraphicsPipeline(
for (Uint32 i = 0; i < metalGraphicsPipeline->vertexUniformBufferCount; i += 1) {
if (metalCommandBuffer->vertexUniformBuffers[i] == NULL) {
metalCommandBuffer->vertexUniformBuffers[i] = METAL_INTERNAL_AcquireUniformBufferFromPool(
metalCommandBuffer->renderer);
metalCommandBuffer);
}
}

for (Uint32 i = 0; i < metalGraphicsPipeline->fragmentUniformBufferCount; i += 1) {
if (metalCommandBuffer->fragmentUniformBuffers[i] == NULL) {
metalCommandBuffer->fragmentUniformBuffers[i] = METAL_INTERNAL_AcquireUniformBufferFromPool(
metalCommandBuffer->renderer);
metalCommandBuffer);
}
}

Expand Down Expand Up @@ -2541,10 +2544,6 @@ static void METAL_INTERNAL_BindGraphicsResources(
setVertexBuffer:commandBuffer->vertexUniformBuffers[i]->handle
offset:commandBuffer->vertexUniformBuffers[i]->drawOffset
atIndex:i];

METAL_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->vertexUniformBuffers[i]);
}

commandBuffer->needVertexUniformBind = SDL_FALSE;
Expand Down Expand Up @@ -2586,10 +2585,6 @@ static void METAL_INTERNAL_BindGraphicsResources(
setFragmentBuffer:commandBuffer->fragmentUniformBuffers[i]->handle
offset:commandBuffer->fragmentUniformBuffers[i]->drawOffset
atIndex:i];

METAL_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->fragmentUniformBuffers[i]);
}

commandBuffer->needFragmentUniformBind = SDL_FALSE;
Expand Down Expand Up @@ -2646,10 +2641,6 @@ static void METAL_INTERNAL_BindComputeResources(
setBuffer:commandBuffer->computeUniformBuffers[i]->handle
offset:commandBuffer->computeUniformBuffers[i]->drawOffset
atIndex:i];

METAL_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->computeUniformBuffers[i]);
}

commandBuffer->needComputeUniformBind = SDL_FALSE;
Expand Down Expand Up @@ -2769,19 +2760,19 @@ static void METAL_INTERNAL_PushUniformData(
if (shaderStage == SDL_GPU_SHADERSTAGE_VERTEX) {
if (metalCommandBuffer->vertexUniformBuffers[slotIndex] == NULL) {
metalCommandBuffer->vertexUniformBuffers[slotIndex] = METAL_INTERNAL_AcquireUniformBufferFromPool(
metalCommandBuffer->renderer);
metalCommandBuffer);
}
metalUniformBuffer = metalCommandBuffer->vertexUniformBuffers[slotIndex];
} else if (shaderStage == SDL_GPU_SHADERSTAGE_FRAGMENT) {
if (metalCommandBuffer->fragmentUniformBuffers[slotIndex] == NULL) {
metalCommandBuffer->fragmentUniformBuffers[slotIndex] = METAL_INTERNAL_AcquireUniformBufferFromPool(
metalCommandBuffer->renderer);
metalCommandBuffer);
}
metalUniformBuffer = metalCommandBuffer->fragmentUniformBuffers[slotIndex];
} else if (shaderStage == SDL_GPU_SHADERSTAGE_COMPUTE) {
if (metalCommandBuffer->computeUniformBuffers[slotIndex] == NULL) {
metalCommandBuffer->computeUniformBuffers[slotIndex] = METAL_INTERNAL_AcquireUniformBufferFromPool(
metalCommandBuffer->renderer);
metalCommandBuffer);
}
metalUniformBuffer = metalCommandBuffer->computeUniformBuffers[slotIndex];
} else {
Expand All @@ -2794,12 +2785,8 @@ static void METAL_INTERNAL_PushUniformData(
256);

if (metalUniformBuffer->writeOffset + alignedDataLength >= UNIFORM_BUFFER_SIZE) {
METAL_INTERNAL_TrackUniformBuffer(
metalCommandBuffer,
metalUniformBuffer);

metalUniformBuffer = METAL_INTERNAL_AcquireUniformBufferFromPool(
metalCommandBuffer->renderer);
metalCommandBuffer);

metalUniformBuffer->writeOffset = 0;
metalUniformBuffer->drawOffset = 0;
Expand Down Expand Up @@ -3087,7 +3074,7 @@ static void METAL_BindComputePipeline(
for (Uint32 i = 0; i < pipeline->uniformBufferCount; i += 1) {
if (metalCommandBuffer->computeUniformBuffers[i] == NULL) {
metalCommandBuffer->computeUniformBuffers[i] = METAL_INTERNAL_AcquireUniformBufferFromPool(
metalCommandBuffer->renderer);
metalCommandBuffer);
}
}

Expand Down
35 changes: 11 additions & 24 deletions src/gpu/vulkan/SDL_gpu_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -4976,10 +4976,6 @@ static void VULKAN_INTERNAL_BindGraphicsDescriptorSets(
currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];

bufferInfoCount += 1;

VULKAN_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->vertexUniformBuffers[i]);
}

renderer->vkUpdateDescriptorSets(
Expand Down Expand Up @@ -5150,10 +5146,6 @@ static void VULKAN_INTERNAL_BindGraphicsDescriptorSets(
currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];

bufferInfoCount += 1;

VULKAN_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->fragmentUniformBuffers[i]);
}

renderer->vkUpdateDescriptorSets(
Expand Down Expand Up @@ -7455,8 +7447,9 @@ static void VULKAN_BindFragmentStorageBuffers(
}

static VulkanUniformBuffer *VULKAN_INTERNAL_AcquireUniformBufferFromPool(
VulkanRenderer *renderer)
VulkanCommandBuffer *commandBuffer)
{
VulkanRenderer *renderer = commandBuffer->renderer;
VulkanUniformBuffer *uniformBuffer;

SDL_LockMutex(renderer->acquireUniformBufferLock);
Expand All @@ -7472,6 +7465,8 @@ static VulkanUniformBuffer *VULKAN_INTERNAL_AcquireUniformBufferFromPool(

SDL_UnlockMutex(renderer->acquireUniformBufferLock);

VULKAN_INTERNAL_TrackUniformBuffer(commandBuffer, uniformBuffer);

return uniformBuffer;
}

Expand Down Expand Up @@ -7510,19 +7505,19 @@ static void VULKAN_INTERNAL_PushUniformData(
if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_VERTEX) {
if (commandBuffer->vertexUniformBuffers[slotIndex] == NULL) {
commandBuffer->vertexUniformBuffers[slotIndex] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
commandBuffer->renderer);
commandBuffer);
}
uniformBuffer = commandBuffer->vertexUniformBuffers[slotIndex];
} else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_FRAGMENT) {
if (commandBuffer->fragmentUniformBuffers[slotIndex] == NULL) {
commandBuffer->fragmentUniformBuffers[slotIndex] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
commandBuffer->renderer);
commandBuffer);
}
uniformBuffer = commandBuffer->fragmentUniformBuffers[slotIndex];
} else if (uniformBufferStage == VULKAN_UNIFORM_BUFFER_STAGE_COMPUTE) {
if (commandBuffer->computeUniformBuffers[slotIndex] == NULL) {
commandBuffer->computeUniformBuffers[slotIndex] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
commandBuffer->renderer);
commandBuffer);
}
uniformBuffer = commandBuffer->computeUniformBuffers[slotIndex];
} else {
Expand All @@ -7532,11 +7527,7 @@ static void VULKAN_INTERNAL_PushUniformData(

/* If there is no more room, acquire a new uniform buffer */
if (uniformBuffer->writeOffset + blockSize + MAX_UBO_SECTION_SIZE >= uniformBuffer->bufferHandle->vulkanBuffer->size) {
VULKAN_INTERNAL_TrackUniformBuffer(
commandBuffer,
uniformBuffer);

uniformBuffer = VULKAN_INTERNAL_AcquireUniformBufferFromPool(commandBuffer->renderer);
uniformBuffer = VULKAN_INTERNAL_AcquireUniformBufferFromPool(commandBuffer);

uniformBuffer->drawOffset = 0;
uniformBuffer->writeOffset = 0;
Expand Down Expand Up @@ -7843,14 +7834,14 @@ static void VULKAN_BindGraphicsPipeline(
for (Uint32 i = 0; i < pipeline->resourceLayout.vertexUniformBufferCount; i += 1) {
if (vulkanCommandBuffer->vertexUniformBuffers[i] == NULL) {
vulkanCommandBuffer->vertexUniformBuffers[i] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
vulkanCommandBuffer->renderer);
vulkanCommandBuffer);
}
}

for (Uint32 i = 0; i < pipeline->resourceLayout.fragmentUniformBufferCount; i += 1) {
if (vulkanCommandBuffer->fragmentUniformBuffers[i] == NULL) {
vulkanCommandBuffer->fragmentUniformBuffers[i] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
vulkanCommandBuffer->renderer);
vulkanCommandBuffer);
}
}

Expand Down Expand Up @@ -8051,7 +8042,7 @@ static void VULKAN_BindComputePipeline(
for (Uint32 i = 0; i < vulkanComputePipeline->resourceLayout.uniformBufferCount; i += 1) {
if (vulkanCommandBuffer->computeUniformBuffers[i] == NULL) {
vulkanCommandBuffer->computeUniformBuffers[i] = VULKAN_INTERNAL_AcquireUniformBufferFromPool(
vulkanCommandBuffer->renderer);
vulkanCommandBuffer);
}
}

Expand Down Expand Up @@ -8369,10 +8360,6 @@ static void VULKAN_INTERNAL_BindComputeDescriptorSets(
currentWriteDescriptorSet->pBufferInfo = &bufferInfos[bufferInfoCount];

bufferInfoCount += 1;

VULKAN_INTERNAL_TrackUniformBuffer(
commandBuffer,
commandBuffer->computeUniformBuffers[i]);
}

renderer->vkUpdateDescriptorSets(
Expand Down
Loading