This commit is contained in:
Isuru Samarathunga
2025-10-16 22:59:33 +05:30
parent f742dcfaff
commit dfce12ee10
9 changed files with 89 additions and 31 deletions

View File

@ -36,6 +36,9 @@ namespace ia::iae
g_linearClampSampler = SDL_CreateGPUSampler(Renderer::GetDevice(), &createInfo);
createInfo.min_filter = SDL_GPU_FILTER_NEAREST;
createInfo.mag_filter = SDL_GPU_FILTER_NEAREST;
createInfo.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR;
createInfo.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
createInfo.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
createInfo.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_REPEAT,
@ -62,15 +65,19 @@ namespace ia::iae
SDL_GPUTexture *GPUResourceManager::CreateTexture(IN SDL_GPUTextureUsageFlags usage, IN INT32 width,
IN INT32 height, IN PCUINT8 rgbaData,
IN SDL_GPUTextureFormat format)
IN SDL_GPUTextureFormat format, IN BOOL generateMipmaps)
{
const auto mipLevels = generateMipmaps ? floor(log2(ia_max(width, height))) + 1 : 1;
STATIC Vector<UINT8> TMP_COLOR_BUFFER;
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
.format = format,
.usage = usage,
.width = (UINT32) width,
.height = (UINT32) height,
.layer_count_or_depth = 1,
.num_levels = 1,
.num_levels = (UINT32) mipLevels,
.sample_count = SDL_GPU_SAMPLECOUNT_1};
const auto result = SDL_CreateGPUTexture(Renderer::GetDevice(), &createInfo);
if (!result)
@ -80,14 +87,25 @@ namespace ia::iae
}
if (rgbaData)
{
TMP_COLOR_BUFFER.reset();
TMP_COLOR_BUFFER.resize(width * height * 4);
for (SIZE_T i = 0; i < TMP_COLOR_BUFFER.size() >> 2; i++)
{
const auto a = static_cast<FLOAT32>(rgbaData[i * 4 + 3]) / 255.0f;
TMP_COLOR_BUFFER[i * 4 + 0] = static_cast<UINT8>(rgbaData[i * 4 + 0] * a);
TMP_COLOR_BUFFER[i * 4 + 1] = static_cast<UINT8>(rgbaData[i * 4 + 1] * a);
TMP_COLOR_BUFFER[i * 4 + 2] = static_cast<UINT8>(rgbaData[i * 4 + 2] * a);
TMP_COLOR_BUFFER[i * 4 + 3] = rgbaData[i * 4 + 3];
}
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = (UINT32) width * (UINT32) height * 4};
const auto stagingBuffer = SDL_CreateGPUTransferBuffer(Renderer::GetDevice(), &stagingBufferCreateInfo);
const auto mappedPtr = SDL_MapGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer, false);
SDL_memcpy(mappedPtr, rgbaData, width * height * 4);
SDL_memcpy(mappedPtr, TMP_COLOR_BUFFER.data(), width * height * 4);
SDL_UnmapGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer);
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
auto cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
SDL_GPUTextureTransferInfo transferInfo{.transfer_buffer = stagingBuffer, .offset = 0};
@ -98,6 +116,14 @@ namespace ia::iae
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(Renderer::GetDevice());
SDL_ReleaseGPUTransferBuffer(Renderer::GetDevice(), stagingBuffer);
if (mipLevels > 1)
{
cmdBuffer = SDL_AcquireGPUCommandBuffer(Renderer::GetDevice());
SDL_GenerateMipmapsForGPUTexture(cmdBuffer, result);
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(Renderer::GetDevice());
}
}
return result;
}
@ -138,7 +164,8 @@ namespace ia::iae
VOID GPUResourceManager::DestroyTexture(IN SDL_GPUTexture *handle)
{
if(!handle) return;
if (!handle)
return;
SDL_ReleaseGPUTexture(Renderer::GetDevice(), handle);
}
@ -175,8 +202,8 @@ namespace ia::iae
return result;
}
SDL_GPUTexture* GPUResourceManager::CombineTextures(IN SDL_GPUTexture** textures, IN INT32 unitWidth, IN INT32 unitHeight,
IN INT32 unitCountX, IN INT32 unitCountY)
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,
@ -198,7 +225,8 @@ namespace ia::iae
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_GPUTextureLocation dst{
.texture = result, .x = (UINT32) (x * unitWidth), .y = (UINT32) (y * unitHeight)};
SDL_CopyGPUTextureToTexture(copyPass, &src, &dst, unitWidth, unitHeight, 1, false);
}
}