RenderCore

This commit is contained in:
Isuru Samarathunga
2025-11-04 22:18:48 +05:30
parent ca75777f19
commit 2de8634184
38 changed files with 1465 additions and 1100 deletions

View File

@ -0,0 +1,124 @@
// 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/Buffer.hpp>
namespace ia::iae
{
RDC_Buffer::RDC_Buffer() : m_type(EType::NONE), m_size(0)
{
}
RDC_Buffer::RDC_Buffer(IN EType type, IN UINT32 size) : m_type(type), m_size(size)
{
}
RDC_StagingBuffer::RDC_StagingBuffer(IN UINT32 size) : RDC_Buffer(EType::NONE, size)
{
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = size};
m_buffer = SDL_CreateGPUTransferBuffer(RDC_Device::GetHandle(), &stagingBufferCreateInfo);
}
RDC_StagingBuffer::~RDC_StagingBuffer()
{
SDL_ReleaseGPUTransferBuffer(RDC_Device::GetHandle(), m_buffer);
}
VOID RDC_StagingBuffer::CopyFrom(IN PCVOID data, IN UINT32 size)
{
IA_ASSERT(size <= m_size);
const auto mappedPtr = SDL_MapGPUTransferBuffer(RDC_Device::GetHandle(), m_buffer, false);
SDL_memcpy(mappedPtr, data, size);
SDL_UnmapGPUTransferBuffer(RDC_Device::GetHandle(), m_buffer);
}
RDC_DeviceLocalBuffer::RDC_DeviceLocalBuffer()
{
}
RDC_DeviceLocalBuffer::RDC_DeviceLocalBuffer(IN RDC_Buffer::EType type, IN UINT32 size) : RDC_Buffer(type, size)
{
SDL_GPUBufferCreateInfo createInfo{.size = size};
switch (type)
{
case RDC_Buffer::EType::NONE:
THROW_INVALID_DATA();
break;
case RDC_Buffer::EType::VERTEX:
createInfo.usage = SDL_GPU_BUFFERUSAGE_VERTEX;
break;
case RDC_Buffer::EType::INDEX:
createInfo.usage = SDL_GPU_BUFFERUSAGE_INDEX;
break;
case RDC_Buffer::EType::STORAGE:
createInfo.usage = SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ;
break;
}
m_buffer = SDL_CreateGPUBuffer(RDC_Device::GetHandle(), &createInfo);
}
RDC_DeviceLocalBuffer::~RDC_DeviceLocalBuffer()
{
if (m_buffer)
SDL_ReleaseGPUBuffer(RDC_Device::GetHandle(), m_buffer);
}
VOID RDC_DeviceLocalBuffer::CopyFrom(IN RDC_StagingBuffer *stagingBuffer, IN UINT32 size)
{
CopyFrom(stagingBuffer->m_buffer, size);
}
VOID RDC_DeviceLocalBuffer::CopyFrom(IN SDL_GPUTransferBuffer *stagingBuffer, IN UINT32 size)
{
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(RDC_Device::GetHandle());
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
SDL_GPUTransferBufferLocation src{.transfer_buffer = stagingBuffer, .offset = 0};
SDL_GPUBufferRegion dst{.buffer = m_buffer, .offset = 0, .size = size};
SDL_UploadToGPUBuffer(copyPass, &src, &dst, false);
SDL_EndGPUCopyPass(copyPass);
SDL_SubmitGPUCommandBuffer(cmdBuffer);
SDL_WaitForGPUIdle(RDC_Device::GetHandle());
}
RDC_HostVisibleBuffer::RDC_HostVisibleBuffer(IN RDC_Buffer::EType type, IN UINT32 size)
: RDC_DeviceLocalBuffer(type, size)
{
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
.size = size};
m_stagingBuffer = SDL_CreateGPUTransferBuffer(RDC_Device::GetHandle(), &stagingBufferCreateInfo);
}
RDC_HostVisibleBuffer::~RDC_HostVisibleBuffer()
{
SDL_ReleaseGPUTransferBuffer(RDC_Device::GetHandle(), m_stagingBuffer);
}
VOID RDC_HostVisibleBuffer::CopyFrom(IN PCVOID data, IN UINT32 size)
{
IA_ASSERT(size <= m_size);
const auto mappedPtr = SDL_MapGPUTransferBuffer(RDC_Device::GetHandle(), m_stagingBuffer, false);
SDL_memcpy(mappedPtr, data, size);
SDL_UnmapGPUTransferBuffer(RDC_Device::GetHandle(), m_stagingBuffer);
RDC_DeviceLocalBuffer::CopyFrom(m_stagingBuffer, size);
}
} // namespace ia::iae

View File

@ -0,0 +1,108 @@
// 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/Buffer.hpp>
#include <RenderCore/Device.hpp>
namespace ia::iae
{
struct Geometry
{
INT32 IndexCount{};
RDC_DeviceLocalBuffer IndexBuffer;
RDC_DeviceLocalBuffer VertexBuffer;
};
} // namespace ia::iae
namespace ia::iae
{
SDL_GPUDevice *RDC_Device::s_handle{};
SDL_Window *RDC_Device::s_windowHandle{};
VOID RDC_Device::Initialize(IN SDL_Window *windowHandle, IN BOOL isDebugMode)
{
s_windowHandle = windowHandle;
SDL_PropertiesID deviceCreateProps = SDL_CreateProperties();
SDL_SetStringProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, nullptr);
SDL_SetBooleanProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, true);
SDL_SetBooleanProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, isDebugMode);
SDL_SetBooleanProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_DEPTH_CLAMPING_BOOLEAN, false);
if (!(s_handle = SDL_CreateGPUDeviceWithProperties(deviceCreateProps)))
THROW_UNKNOWN("Failed to create the SDL GPU Device: ", SDL_GetError());
SDL_DestroyProperties(deviceCreateProps);
if (!SDL_ClaimWindowForGPUDevice(s_handle, windowHandle))
THROW_UNKNOWN("Failed to initialize SDL GPU for the window: ", SDL_GetError());
SDL_SetGPUSwapchainParameters(s_handle, windowHandle, SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
SDL_GPU_PRESENTMODE_VSYNC);
}
VOID RDC_Device::Terminate()
{
WaitForIdle();
SDL_ReleaseWindowFromGPUDevice(s_handle, s_windowHandle);
SDL_DestroyGPUDevice(s_handle);
}
VOID RDC_Device::WaitForIdle()
{
SDL_WaitForGPUIdle(s_handle);
}
Handle RDC_Device::CreateGeometry(IN CONST Vector<GeometryVertex> &vertices, IN CONST Vector<INT32> &indices)
{
const auto geometry = new Geometry();
const auto vertexDataSize = static_cast<UINT32>(vertices.size() * sizeof(vertices[0]));
const auto indexDataSize = static_cast<UINT32>(indices.size() * sizeof(indices[0]));
std::construct_at(&geometry->VertexBuffer, RDC_Buffer::EType::VERTEX, vertexDataSize);
std::construct_at(&geometry->IndexBuffer, RDC_Buffer::EType::INDEX, indexDataSize);
const auto stagingBuffer = new RDC_StagingBuffer(ia_max(vertexDataSize, indexDataSize));
stagingBuffer->CopyFrom(vertices.data(), vertexDataSize);
geometry->VertexBuffer.CopyFrom(stagingBuffer, vertexDataSize);
stagingBuffer->CopyFrom(indices.data(), indexDataSize);
geometry->IndexBuffer.CopyFrom(stagingBuffer, indexDataSize);
delete stagingBuffer;
geometry->IndexCount = static_cast<UINT32>(indices.size());
return (Handle) geometry;
}
VOID RDC_Device::DestroyGeometry(IN Handle _geometry)
{
const auto geometry = (Geometry *) _geometry;
delete geometry;
}
VOID RDC_Device::BindGeometry(IN SDL_GPURenderPass* renderPass, IN Handle _geometry)
{
const auto geometry = (Geometry *) _geometry;
SDL_GPUBufferBinding bufferBindings[] = {{.buffer = geometry->VertexBuffer.GetHandle(), .offset = 0},
{.buffer = geometry->IndexBuffer.GetHandle(), .offset = 0}};
SDL_BindGPUVertexBuffers(renderPass, 0, &bufferBindings[0], 1);
SDL_BindGPUIndexBuffer(renderPass, &bufferBindings[1], SDL_GPU_INDEXELEMENTSIZE_32BIT);
}
SDL_GPUTextureFormat RDC_Device::GetSwapchainTextureFormat()
{
return SDL_GetGPUSwapchainTextureFormat(s_handle, s_windowHandle);
}
} // namespace ia::iae

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,107 @@
// 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/Pipeline.hpp>
namespace ia::iae
{
RDC_Pipeline::RDC_Pipeline(IN SDL_GPUTextureFormat renderTargetFormat, IN CONST StageDesc &vertexStageDesc,
IN CONST StageDesc &pixelStageDesc, IN BOOL enableVertexBuffer)
{
SDL_GPUShader *vertexShader{};
SDL_GPUShader *pixelShader{};
SDL_GPUShaderCreateInfo shaderCreateInfo = {
.entrypoint = "main",
.format = SDL_GPU_SHADERFORMAT_SPIRV,
.num_storage_textures = 0,
.num_storage_buffers = 0,
};
shaderCreateInfo.stage = SDL_GPU_SHADERSTAGE_VERTEX;
shaderCreateInfo.code = vertexStageDesc.SourceData.data();
shaderCreateInfo.code_size = vertexStageDesc.SourceData.size();
shaderCreateInfo.num_samplers = vertexStageDesc.SamplerCount;
shaderCreateInfo.num_uniform_buffers = vertexStageDesc.UniformBufferCount;
shaderCreateInfo.num_storage_buffers = vertexStageDesc.StorageBufferCount;
if (!(vertexShader = SDL_CreateGPUShader(RDC_Device::GetHandle(), &shaderCreateInfo)))
THROW_UNKNOWN("Failed to create a SDL shader: ", SDL_GetError());
shaderCreateInfo.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
shaderCreateInfo.code = pixelStageDesc.SourceData.data();
shaderCreateInfo.code_size = pixelStageDesc.SourceData.size();
shaderCreateInfo.num_samplers = pixelStageDesc.SamplerCount;
shaderCreateInfo.num_uniform_buffers = pixelStageDesc.UniformBufferCount;
shaderCreateInfo.num_storage_buffers = pixelStageDesc.StorageBufferCount;
if (!(pixelShader = SDL_CreateGPUShader(RDC_Device::GetHandle(), &shaderCreateInfo)))
THROW_UNKNOWN("Failed to create a SDL shader: ", SDL_GetError());
SDL_GPUColorTargetDescription colorTargetDesc = {
.format = renderTargetFormat,
.blend_state = {.src_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE,
.dst_color_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
.color_blend_op = SDL_GPU_BLENDOP_ADD,
.src_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE,
.dst_alpha_blendfactor = SDL_GPU_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
.alpha_blend_op = SDL_GPU_BLENDOP_ADD,
.enable_blend = true,
.enable_color_write_mask = false}};
SDL_GPUVertexBufferDescription vertexBufferDesc = {
.slot = 0,
.pitch = sizeof(GeometryVertex),
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
.instance_step_rate = 0,
};
SDL_GPUVertexAttribute vertexAttributes[] = {
{.location = 0, .buffer_slot = 0, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, .offset = 0},
{.location = 1, .buffer_slot = 0, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2, .offset = sizeof(Vec2)}};
SDL_GPUGraphicsPipelineCreateInfo createInfo = {
.vertex_shader = vertexShader,
.fragment_shader = pixelShader,
.vertex_input_state = SDL_GPUVertexInputState{.vertex_buffer_descriptions = &vertexBufferDesc,
.num_vertex_buffers = enableVertexBuffer ? (UINT32) 1 : 0,
.vertex_attributes = vertexAttributes,
.num_vertex_attributes = enableVertexBuffer ? (UINT32) 2 : 0},
.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST,
.rasterizer_state = SDL_GPURasterizerState{.fill_mode = SDL_GPU_FILLMODE_FILL,
.cull_mode = SDL_GPU_CULLMODE_NONE,
.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE,
.enable_depth_clip = true},
.target_info = {.color_target_descriptions = &colorTargetDesc,
.num_color_targets = 1,
.has_depth_stencil_target = false},
};
if (!(m_handle = SDL_CreateGPUGraphicsPipeline(RDC_Device::GetHandle(), &createInfo)))
THROW_UNKNOWN("Failed to create a SDL graphics pipeline: ", SDL_GetError());
SDL_ReleaseGPUShader(RDC_Device::GetHandle(), pixelShader);
SDL_ReleaseGPUShader(RDC_Device::GetHandle(), vertexShader);
}
RDC_Pipeline::~RDC_Pipeline()
{
SDL_ReleaseGPUGraphicsPipeline(RDC_Device::GetHandle(), m_handle);
}
VOID RDC_Pipeline::Bind(IN SDL_GPURenderPass *renderPass)
{
SDL_BindGPUGraphicsPipeline(renderPass, m_handle);
}
} // namespace ia::iae

View File

@ -0,0 +1,292 @@
// 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/RenderCore.hpp>
#include <RenderCore/Texture.hpp>
#include <RenderCore/TextureAtlas.hpp>
#include <EmbeddedResources.hpp>
namespace ia::iae
{
Mat4 RDC::s_viewMatrix;
Mat4 RDC::s_projectionMatrix;
SDL_Window *RDC::s_windowHandle;
Handle RDC::s_quadGeometry;
RDC_Pipeline *RDC::s_dynamicSpritePipeline;
SDL_GPUSampler *RDC::s_linearClampSampler;
SDL_GPUSampler *RDC::s_linearRepeatSampler;
Vec2 RDC::s_cameraPosition{};
IVec2 RDC::s_viewportExtent;
RDC_TextureAtlas *RDC::s_staticSpriteAtlas{};
RDC_TextureAtlas *RDC::s_dynamicSpriteAtlas{};
INT32 RDC::s_spriteInstanceCount{};
RDC_HostVisibleBuffer *RDC::s_staticSpriteInstanceBuffer{};
RDC_HostVisibleBuffer *RDC::s_dynamicSpriteInstanceBuffer{};
RDC_Texture *RDC::s_defaultTexture{};
RDC_SpriteInstanceData RDC::s_spriteInstances[RDC::MAX_SPRITE_COUNT];
VOID RDC::Initialize(IN IVec2 viewportExtent, IN SDL_Window *windowHandle, IN BOOL isDebugMode)
{
EmbeddedResources::Initialize();
s_windowHandle = windowHandle;
RDC_Device::Initialize(s_windowHandle, isDebugMode);
InitializePipelines();
InitializeSamplers();
InitializeGeometries();
InitializeTextures();
InitializeDrawData();
ResizeScreen(viewportExtent);
s_viewMatrix = glm::lookAtLH(glm::vec3{s_cameraPosition, -1.0f}, {s_cameraPosition, 0.0f}, {0.0f, 1.0f, 0.0f});
}
VOID RDC::Terminate()
{
RDC_Device::WaitForIdle();
RDC_Device::DestroyGeometry(s_quadGeometry);
SDL_ReleaseGPUSampler(RDC_Device::GetHandle(), s_linearClampSampler);
SDL_ReleaseGPUSampler(RDC_Device::GetHandle(), s_linearRepeatSampler);
delete s_defaultTexture;
delete s_staticSpriteAtlas;
delete s_dynamicSpriteAtlas;
delete s_dynamicSpritePipeline;
delete s_staticSpriteInstanceBuffer;
delete s_dynamicSpriteInstanceBuffer;
RDC_Device::Terminate();
EmbeddedResources::Terminate();
}
VOID RDC::ResizeScreen(IN IVec2 newExtent)
{
s_viewportExtent = newExtent;
s_projectionMatrix =
glm::orthoLH(0.0f, (FLOAT32) s_viewportExtent.x, (FLOAT32) s_viewportExtent.y, 0.0f, -1.0f, 1.0f);
}
VOID RDC::RenderToWindow()
{
STATIC SDL_GPURenderPass *ActiveRenderPass{};
STATIC SDL_GPUCommandBuffer *ActiveCommandBuffer{};
STATIC SDL_GPUColorTargetInfo ActiveColorTargetInfo{.clear_color = SDL_FColor{0.0f, 0.0f, 0.0f, 1.0f},
.load_op = SDL_GPU_LOADOP_CLEAR,
.store_op = SDL_GPU_STOREOP_STORE};
if (!(ActiveCommandBuffer = SDL_AcquireGPUCommandBuffer(RDC_Device::GetHandle())))
THROW_UNKNOWN("Failed to acquire SDL GPU command buffer: ", SDL_GetError());
SDL_GPUTexture *swapChainTexture{};
if (!SDL_WaitAndAcquireGPUSwapchainTexture(ActiveCommandBuffer, s_windowHandle, &swapChainTexture, nullptr,
nullptr))
THROW_UNKNOWN("Failed to acquire SDL GPU Swapchain texture: ", SDL_GetError());
if (!swapChainTexture)
return;
ActiveColorTargetInfo.texture = swapChainTexture;
ActiveColorTargetInfo.clear_color = SDL_FColor{0.3f, 0.3f, 0.3f, 1.0f};
ActiveRenderPass = SDL_BeginGPURenderPass(ActiveCommandBuffer, &ActiveColorTargetInfo, 1, nullptr);
s_dynamicSpritePipeline->Bind(ActiveRenderPass);
SDL_PushGPUVertexUniformData(ActiveCommandBuffer, 0, &s_projectionMatrix, sizeof(Mat4));
SDL_PushGPUVertexUniformData(ActiveCommandBuffer, 1, &s_viewMatrix, sizeof(Mat4));
SDL_GPUTextureSamplerBinding textureBinding{.texture = s_dynamicSpriteAtlas
? s_dynamicSpriteAtlas->GetTexture()->GetHandle()
: s_defaultTexture->GetHandle(),
.sampler = s_linearRepeatSampler};
SDL_BindGPUFragmentSamplers(ActiveRenderPass, 0, &textureBinding, 1);
RDC_Device::BindGeometry(ActiveRenderPass, s_quadGeometry);
if (s_spriteInstanceCount)
{
const auto spriteInstanceBuffer = s_dynamicSpriteInstanceBuffer->GetHandle();
s_dynamicSpriteInstanceBuffer->CopyFrom(s_spriteInstances,
sizeof(RDC_SpriteInstanceData) * s_spriteInstanceCount);
SDL_BindGPUVertexStorageBuffers(ActiveRenderPass, 0, &spriteInstanceBuffer, 1);
SDL_DrawGPUIndexedPrimitives(ActiveRenderPass, 6, s_spriteInstanceCount, 0, 0, 0);
}
s_spriteInstanceCount = 0;
SDL_EndGPURenderPass(ActiveRenderPass);
SDL_SubmitGPUCommandBuffer(ActiveCommandBuffer);
}
Vec2 RDC::DrawSpriteTopLeft(IN Handle _image, IN INT32 tileIndexX, IN INT32 tileIndexY, IN Vec2 position,
IN Vec2 scale, IN FLOAT32 rotation, IN BOOL flipH, IN BOOL flipV, IN Vec2 uvOffset)
{
const auto image = (ImageData *) _image;
const auto _s = Vec2{scale.x * image->TileWidth, scale.y * image->TileHeight};
Mat4 transform = glm::translate(glm::mat4(1.0f), glm::vec3{position.x, position.y, 0});
transform = glm::rotate(transform, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
transform = glm::scale(transform, glm::vec3(_s, 1.0f));
s_spriteInstances[s_spriteInstanceCount++] = {
.Transform = transform,
.TexCoords = s_dynamicSpriteAtlas ? s_dynamicSpriteAtlas->GetTextureCoordinates(
_image, tileIndexX, tileIndexY, flipH, flipV, uvOffset)
: Vec4{0.0f, 0.0f, 1.0f, 1.0f},
.Color = {1.0f, 1.0f, 1.0f, 1.0f}};
return _s;
}
Vec2 RDC::DrawSpriteCentered(IN Handle _image, IN INT32 tileIndexX, IN INT32 tileIndexY, IN Vec2 position,
IN Vec2 scale, IN FLOAT32 rotation, IN BOOL flipH, IN BOOL flipV, IN Vec2 uvOffset)
{
const auto image = (ImageData *) _image;
const auto _s = Vec2{scale.x * image->TileWidth, scale.y * image->TileHeight};
Mat4 transform =
glm::translate(glm::mat4(1.0f), glm::vec3{position.x - _s.x / 2.0f, position.y - _s.y / 2.0f, 0});
transform = glm::rotate(transform, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
transform = glm::scale(transform, glm::vec3(_s, 1.0f));
s_spriteInstances[s_spriteInstanceCount++] = {
.Transform = transform,
.TexCoords = s_dynamicSpriteAtlas ? s_dynamicSpriteAtlas->GetTextureCoordinates(
_image, tileIndexX, tileIndexY, flipH, flipV, uvOffset)
: Vec4{0.0f, 0.0f, 1.0f, 1.0f},
.Color = {1.0f, 1.0f, 1.0f, 1.0f}};
return _s;
}
} // namespace ia::iae
namespace ia::iae
{
Vec2 RDC::GetCameraPosition()
{
return s_cameraPosition;
}
VOID RDC::SetCameraPosition(IN Vec2 position)
{
if B_LIKELY (s_cameraPosition == position)
return;
s_cameraPosition = position;
s_viewMatrix = glm::lookAtLH(glm::vec3{s_cameraPosition, -1.0f}, {s_cameraPosition, 0.0f}, {0.0f, 1.0f, 0.0f});
}
Handle RDC::CreateImage(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height, IN INT32 tileCountX,
IN INT32 tileCountY)
{
const auto pixelDataSize = width * height * 4;
const auto image = new ImageData{
.Pixels = new UINT8[pixelDataSize],
.Width = width,
.Height = height,
.TileWidth = width / tileCountX,
.TileHeight = height / tileCountY,
.TileCountX = tileCountX,
.TileCountY = tileCountY,
};
ia_memcpy(image->Pixels, rgbaData, pixelDataSize);
return (Handle) image;
}
VOID RDC::DestroyImage(IN Handle _image)
{
const auto image = (ImageData *) _image;
delete[] image->Pixels;
delete image;
}
VOID RDC::CompileTextures(IN CONST Vector<Handle> &images)
{
delete s_dynamicSpriteAtlas;
s_dynamicSpriteAtlas = new RDC_TextureAtlas(images);
}
} // namespace ia::iae
namespace ia::iae
{
VOID RDC::InitializeSamplers()
{
SDL_GPUSamplerCreateInfo createInfo{.min_filter = SDL_GPU_FILTER_NEAREST,
.mag_filter = SDL_GPU_FILTER_NEAREST,
.mipmap_mode = SDL_GPU_SAMPLERMIPMAPMODE_LINEAR,
.address_mode_u = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
.address_mode_v = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
.address_mode_w = SDL_GPU_SAMPLERADDRESSMODE_CLAMP_TO_EDGE,
.enable_anisotropy = false};
s_linearClampSampler = SDL_CreateGPUSampler(RDC_Device::GetHandle(), &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,
s_linearRepeatSampler = SDL_CreateGPUSampler(RDC_Device::GetHandle(), &createInfo);
}
VOID RDC::InitializeDrawData()
{
s_dynamicSpriteInstanceBuffer =
new RDC_HostVisibleBuffer(RDC_Buffer::EType::STORAGE, sizeof(s_spriteInstances));
}
VOID RDC::InitializeTextures()
{
{ // Create Default Texture
const auto pixels = new UINT8[100 * 100 * 4];
ia_memset(pixels, 0xFF, 100 * 100 * 4);
s_defaultTexture = new RDC_Texture(RDC_Texture::EType::SAMPLED, 100, 100);
s_defaultTexture->SetImageData(pixels);
delete[] pixels;
}
}
VOID RDC::InitializePipelines()
{
s_dynamicSpritePipeline =
new RDC_Pipeline(RDC_Device::GetSwapchainTextureFormat(),
RDC_Pipeline::StageDesc{
.SourceData = EmbeddedResources::GetResource("Shaders/DynamicSprite.vert"),
.SamplerCount = 0,
.UniformBufferCount = 2,
.StorageBufferCount = 1,
},
RDC_Pipeline::StageDesc{
.SourceData = EmbeddedResources::GetResource("Shaders/DynamicSprite.frag"),
.SamplerCount = 1,
.UniformBufferCount = 0,
.StorageBufferCount = 0,
},
true);
}
VOID RDC::InitializeGeometries()
{
s_quadGeometry = RDC_Device::CreateGeometry(
{
{glm::vec2{0, 1}, glm::vec2{0, 1}},
{glm::vec2{1, 1}, glm::vec2{1, 1}},
{glm::vec2{1, 0}, glm::vec2{1, 0}},
{glm::vec2{0, 0}, glm::vec2{0, 0}},
},
{0, 1, 2, 2, 3, 0});
}
} // namespace ia::iae

View File

@ -0,0 +1,121 @@
// 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

View File

@ -0,0 +1,82 @@
// 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/TextureAtlas.hpp>
namespace ia::iae
{
RDC_TextureAtlas::RDC_TextureAtlas(IN CONST Vector<Handle> &images)
{
if (images.empty())
return;
m_atlasSize.x = 0;
m_atlasSize.y = 0;
for (const auto &_image : images)
{
const auto d = (ImageData *) _image;
m_atlasSize.x += d->Width;
if (d->Height > m_atlasSize.y)
m_atlasSize.y = d->Height;
}
m_inverseAtlasSize = {1.0f / ((FLOAT32) m_atlasSize.x), 1.0f / ((FLOAT32) m_atlasSize.y)};
const auto pixels = new UINT8[m_atlasSize.x * m_atlasSize.y * 4];
INT32 atlasCursor{0};
for (const auto &_image : images)
{
const auto d = (ImageData *) _image;
for (INT32 y = 0; y < d->Height; y++)
ia_memcpy(&pixels[(atlasCursor + (y * m_atlasSize.x)) * 4], &d->Pixels[y * d->Width * 4], d->Width * 4);
m_texCoordMap[_image] = Vec2(((FLOAT32) atlasCursor) / ((FLOAT32) m_atlasSize.x), 0.0f);
atlasCursor += d->Width;
}
m_texture = new RDC_Texture(RDC_Texture::EType::SAMPLED, m_atlasSize.x, m_atlasSize.y);
m_texture->SetImageData(pixels);
delete[] pixels;
}
RDC_TextureAtlas::~RDC_TextureAtlas()
{
delete m_texture;
}
Vec4 RDC_TextureAtlas::GetTextureCoordinates(IN Handle _image, IN INT32 tileIndexX, IN INT32 tileIndexY, IN BOOL flipH,
IN BOOL flipV, IN Vec2 uvOffset)
{
const auto d = (ImageData *) _image;
const auto &t = m_texCoordMap[_image];
const auto pX = ((tileIndexX + uvOffset.x) * ((FLOAT32) d->TileWidth)) * m_inverseAtlasSize.x;
const auto pY = ((tileIndexY + uvOffset.y) * ((FLOAT32) d->TileHeight)) * m_inverseAtlasSize.y;
auto texCoords = Vec4(t.x + pX, t.y + pY, d->TileWidth * m_inverseAtlasSize.x,
d->TileHeight * m_inverseAtlasSize.y);
if (flipH)
{
texCoords.x += texCoords.z;
texCoords.z *= -1;
}
if (flipV)
{
texCoords.y += texCoords.w;
texCoords.w *= -1;
}
return texCoords;
}
} // namespace ia::iae