Input & Resource Managers

This commit is contained in:
Isuru Samarathunga
2025-10-06 01:23:54 +05:30
parent 71c7499226
commit c23f5f4559
26 changed files with 20904 additions and 93 deletions

View File

@ -93,7 +93,7 @@ namespace ia::iae
SDL_GPUTextureTransferInfo transferInfo{.transfer_buffer = stagingBuffer, .offset = 0};
SDL_GPUTextureRegion region{.texture = result, .w = (UINT32) width, .h = (UINT32) height, .d = 1};
SDL_UploadToGPUTexture(copyPass, &transferInfo, &region, false);
SDL_EndGPUCopyPass(copyPass);
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(Renderer::GetDevice());
@ -103,7 +103,7 @@ namespace ia::iae
}
SDL_GPUBuffer *GPUResourceManager::CreateDeviceLocalBuffer(IN SDL_GPUBufferUsageFlags usage, IN PCVOID data,
IN UINT32 dataSize)
IN UINT32 dataSize)
{
SDL_GPUBufferCreateInfo createInfo{.usage = usage, .size = dataSize};
const auto result = SDL_CreateGPUBuffer(Renderer::GetDevice(), &createInfo);
@ -112,7 +112,7 @@ namespace ia::iae
THROW_UNKNOWN("Failed to create a SDL GPU Buffer: ", SDL_GetError());
return nullptr;
}
if(dataSize && dataSize)
if (dataSize && dataSize)
{
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = dataSize};
@ -123,7 +123,7 @@ namespace ia::iae
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
SDL_GPUTransferBufferLocation src{.transfer_buffer = stagingBuffer, .offset = 0};
SDL_GPUBufferRegion dst{.buffer = result, .offset = 0, .size = dataSize};
SDL_UploadToGPUBuffer(copyPass, &src, &dst, false);
@ -145,4 +145,67 @@ namespace ia::iae
{
SDL_ReleaseGPUBuffer(Renderer::GetDevice(), handle);
}
Vector<UINT8> GPUResourceManager::GetTexturePixelData(IN SDL_GPUTexture *texture, IN INT32 width, IN INT32 height)
{
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = (UINT32) width * (UINT32) height * 4};
const auto stagingBuffer = SDL_CreateGPUTransferBuffer(Renderer::GetDevice(), &stagingBufferCreateInfo);
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
SDL_GPUTextureTransferInfo transferInfo{.transfer_buffer = stagingBuffer, .offset = 0};
SDL_GPUTextureRegion region{.texture = texture, .w = (UINT32) width, .h = (UINT32) height, .d = 1};
SDL_DownloadFromGPUTexture(copyPass, &region, &transferInfo);
SDL_EndGPUCopyPass(copyPass);
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(Renderer::GetDevice());
Vector<UINT8> result;
result.resize(width * height * 4);
const auto mappedPtr = SDL_MapGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer, false);
SDL_UnmapGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer);
SDL_memcpy(result.data(), mappedPtr, result.size());
SDL_ReleaseGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer);
return result;
}
SDL_GPUTexture* GPUResourceManager::CombineTextures(IN SDL_GPUTexture** textures, IN INT32 unitWidth, IN INT32 unitHeight,
IN INT32 unitCountX, IN INT32 unitCountY)
{
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER,
.width = (UINT32) unitCountX * unitWidth,
.height = (UINT32) unitCountY * unitHeight,
.layer_count_or_depth = 1,
.num_levels = 1};
SDL_GPUTexture *result{};
if (!(result = SDL_CreateGPUTexture(Renderer::GetDevice(), &createInfo)))
THROW_UNKNOWN("Failed to create a SDL GPU Texture: ", SDL_GetError());
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
for (INT32 y = 0; y < unitCountY; y++)
{
for (INT32 x = 0; x < unitCountX; x++)
{
SDL_GPUTextureLocation src{.texture = textures[x + (y * unitCountX)], .x = 0, .y = 0};
SDL_GPUTextureLocation dst{.texture = result, .x = (UINT32)(x * unitWidth), .y = (UINT32)(y * unitHeight)};
SDL_CopyGPUTextureToTexture(copyPass, &src, &dst, unitWidth, unitHeight, 1, false);
}
}
SDL_EndGPUCopyPass(copyPass);
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(Renderer::GetDevice());
return result;
}
} // namespace ia::iae