Files
IAEngine/Src/RenderCore/imp/cpp/Texture.cpp
Isuru Samarathunga 2de8634184 RenderCore
2025-11-04 22:18:48 +05:30

121 lines
5.5 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <RenderCore/Texture.hpp>
namespace ia::iae
{
RDC_Texture::RDC_Texture(IN EType type, IN INT32 width, IN INT32 height, IN BOOL generateMipMaps)
: m_type(type), m_width(width), m_height(height),
m_mipLevels(generateMipMaps ? ia_max((UINT32) (floor(log2(ia_max(width, height))) + 1), (UINT32) 1)
: (UINT32) 1)
{
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
.width = (UINT32) width,
.height = (UINT32) height,
.layer_count_or_depth = 1,
.num_levels = m_mipLevels,
.sample_count = SDL_GPU_SAMPLECOUNT_1};
switch (type)
{
case EType::SAMPLED:
createInfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER;
break;
case EType::RENDER_TARGET:
createInfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
break;
}
m_handle = SDL_CreateGPUTexture(RDC_Device::GetHandle(), &createInfo);
if (!m_handle)
THROW_UNKNOWN("Failed to create a SDL GPU Texture: ", SDL_GetError());
}
RDC_Texture::~RDC_Texture()
{
SDL_ReleaseGPUTexture(RDC_Device::GetHandle(), m_handle);
}
VOID RDC_Texture::SetImageData(IN PCUINT8 rgbaData, IN INT32 stride)
{
STATIC Vector<UINT8> TMP_COLOR_BUFFER;
TMP_COLOR_BUFFER.resize(m_width * m_height * 4);
if (stride == -1)
{
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];
}
}
else
{
for (INT32 y = 0; y < m_height; y++)
{
for (INT32 x = 0; x < m_width; x++)
{
const auto p = &rgbaData[(x + y * stride) * 4];
const auto a = static_cast<FLOAT32>(p[3]) / 255.0f;
TMP_COLOR_BUFFER[(x + y * m_width) * 4 + 0] = static_cast<UINT8>(p[0] * a);
TMP_COLOR_BUFFER[(x + y * m_width) * 4 + 1] = static_cast<UINT8>(p[1] * a);
TMP_COLOR_BUFFER[(x + y * m_width) * 4 + 2] = static_cast<UINT8>(p[2] * a);
TMP_COLOR_BUFFER[(x + y * m_width) * 4 + 3] = p[3];
}
}
}
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = (UINT32) m_width * (UINT32) m_height * 4};
const auto stagingBuffer = SDL_CreateGPUTransferBuffer(RDC_Device::GetHandle(), &stagingBufferCreateInfo);
const auto mappedPtr = SDL_MapGPUTransferBuffer(RDC_Device::GetHandle(), stagingBuffer, false);
SDL_memcpy(mappedPtr, TMP_COLOR_BUFFER.data(), m_width * m_height * 4);
SDL_UnmapGPUTransferBuffer(RDC_Device::GetHandle(), stagingBuffer);
auto cmdBuffer = SDL_AcquireGPUCommandBuffer(RDC_Device::GetHandle());
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
SDL_GPUTextureTransferInfo transferInfo{.transfer_buffer = stagingBuffer, .offset = 0};
SDL_GPUTextureRegion region{.texture = m_handle, .w = (UINT32) m_width, .h = (UINT32) m_height, .d = 1};
SDL_UploadToGPUTexture(copyPass, &transferInfo, &region, false);
SDL_EndGPUCopyPass(copyPass);
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(RDC_Device::GetHandle());
SDL_ReleaseGPUTransferBuffer(RDC_Device::GetHandle(), stagingBuffer);
if (m_mipLevels > 1)
{
cmdBuffer = SDL_AcquireGPUCommandBuffer(RDC_Device::GetHandle());
SDL_GenerateMipmapsForGPUTexture(cmdBuffer, m_handle);
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(RDC_Device::GetHandle());
}
}
VOID RDC_Texture::BindAsSampler(IN SDL_GPURenderPass *renderPass, IN INT32 index, IN SDL_GPUSampler *sampler)
{
SDL_GPUTextureSamplerBinding textureBinding{.texture = m_handle, .sampler = sampler};
SDL_BindGPUFragmentSamplers(renderPass, index, &textureBinding, 1);
}
} // namespace ia::iae