Texture Rendering
This commit is contained in:
@ -21,64 +21,81 @@
|
||||
namespace ia::iae
|
||||
{
|
||||
EXTERN SDL_GPUDevice *g_gpuDevice;
|
||||
|
||||
UINT32 g_stagingBufferSize = 4096;
|
||||
SDL_GPUTransferBuffer* g_stagingBuffer{};
|
||||
|
||||
RefPtr<GPUBuffer> GPUBuffer::Create(IN PCVOID data, IN UINT32 dataSize)
|
||||
UINT32 g_stagingBufferSize = 4096;
|
||||
SDL_GPUTransferBuffer *g_stagingBuffer{};
|
||||
|
||||
RefPtr<GPUBuffer> GPUBuffer::Create(IN Usage usage, IN PCVOID data, IN UINT32 dataSize)
|
||||
{
|
||||
const auto res = MakeRefPtr<GPUBuffer>();
|
||||
|
||||
SDL_GPUBufferCreateInfo createInfo{
|
||||
.usage = SDL_GPU_BUFFERUSAGE_VERTEX,
|
||||
.size = dataSize
|
||||
};
|
||||
SDL_GPUBuffer* handle{};
|
||||
if(!(handle = SDL_CreateGPUBuffer(g_gpuDevice, &createInfo)))
|
||||
SDL_GPUBufferUsageFlags _usage{};
|
||||
switch (usage)
|
||||
{
|
||||
case Usage::VERTEX:
|
||||
_usage = SDL_GPU_BUFFERUSAGE_VERTEX;
|
||||
break;
|
||||
|
||||
case Usage::INDEX:
|
||||
_usage = SDL_GPU_BUFFERUSAGE_INDEX;
|
||||
break;
|
||||
|
||||
case Usage::STORAGE:
|
||||
_usage = SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ;
|
||||
break;
|
||||
|
||||
default:
|
||||
THROW_INVALID_DATA("Invalid usage value for GPUBuffer");
|
||||
break;
|
||||
}
|
||||
res->m_usage = usage;
|
||||
|
||||
SDL_GPUBufferCreateInfo createInfo{.usage = _usage, .size = dataSize};
|
||||
SDL_GPUBuffer *handle{};
|
||||
if (!(handle = SDL_CreateGPUBuffer(g_gpuDevice, &createInfo)))
|
||||
{
|
||||
IAE_LOG_ERROR("Couldn't create a SDL3 GPU Buffer: ", SDL_GetError());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if(data && dataSize)
|
||||
if (data && dataSize)
|
||||
{
|
||||
if(!EnsureStagingBufferSize((UINT32)dataSize))
|
||||
if (!EnsureStagingBufferSize((UINT32) dataSize))
|
||||
return nullptr;
|
||||
|
||||
res->m_size = dataSize;
|
||||
|
||||
const auto mappedPtr = SDL_MapGPUTransferBuffer(g_gpuDevice, g_stagingBuffer, false);
|
||||
memcpy(mappedPtr, data, dataSize);
|
||||
SDL_UnmapGPUTransferBuffer(g_gpuDevice, g_stagingBuffer);
|
||||
|
||||
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(g_gpuDevice);
|
||||
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
|
||||
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
|
||||
|
||||
SDL_GPUTransferBufferLocation src{
|
||||
.transfer_buffer = g_stagingBuffer,
|
||||
.offset = 0
|
||||
};
|
||||
SDL_GPUBufferRegion dst{
|
||||
.buffer = handle,
|
||||
.offset = 0,
|
||||
.size = dataSize
|
||||
};
|
||||
SDL_GPUTransferBufferLocation src{.transfer_buffer = g_stagingBuffer, .offset = 0};
|
||||
SDL_GPUBufferRegion dst{.buffer = handle, .offset = 0, .size = dataSize};
|
||||
SDL_UploadToGPUBuffer(copyPass, &src, &dst, false);
|
||||
|
||||
SDL_EndGPUCopyPass(copyPass);
|
||||
SDL_SubmitGPUCommandBuffer(cmdBuffer);
|
||||
}
|
||||
|
||||
res->m_handle = (Handle)handle;
|
||||
res->m_handle = (Handle) handle;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
GPUBuffer::~GPUBuffer()
|
||||
{
|
||||
if (m_handle)
|
||||
SDL_ReleaseGPUBuffer(g_gpuDevice, (SDL_GPUBuffer *) m_handle);
|
||||
}
|
||||
|
||||
BOOL GPUBuffer::InitializeStagingBuffer()
|
||||
{
|
||||
SDL_GPUTransferBufferCreateInfo createInfo{
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = g_stagingBufferSize
|
||||
};
|
||||
if(!(g_stagingBuffer = SDL_CreateGPUTransferBuffer(g_gpuDevice, &createInfo)))
|
||||
SDL_GPUTransferBufferCreateInfo createInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = g_stagingBufferSize};
|
||||
if (!(g_stagingBuffer = SDL_CreateGPUTransferBuffer(g_gpuDevice, &createInfo)))
|
||||
{
|
||||
IAE_LOG_ERROR("Couldn't create a SDL3 GPU Staging Buffer: ", SDL_GetError());
|
||||
return false;
|
||||
@ -88,7 +105,7 @@ namespace ia::iae
|
||||
|
||||
VOID GPUBuffer::TerminateStagingBuffer()
|
||||
{
|
||||
if(!g_stagingBuffer)
|
||||
if (!g_stagingBuffer)
|
||||
return;
|
||||
|
||||
SDL_ReleaseGPUTransferBuffer(g_gpuDevice, g_stagingBuffer);
|
||||
@ -96,16 +113,14 @@ namespace ia::iae
|
||||
|
||||
BOOL GPUBuffer::EnsureStagingBufferSize(IN UINT32 size)
|
||||
{
|
||||
if(!g_stagingBuffer)
|
||||
if (!g_stagingBuffer)
|
||||
return false;
|
||||
if(size <= g_stagingBufferSize)
|
||||
if (size <= g_stagingBufferSize)
|
||||
return true;
|
||||
SDL_ReleaseGPUTransferBuffer(g_gpuDevice, g_stagingBuffer);
|
||||
SDL_GPUTransferBufferCreateInfo createInfo{
|
||||
.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = (g_stagingBufferSize = size)
|
||||
};
|
||||
if(!(g_stagingBuffer = SDL_CreateGPUTransferBuffer(g_gpuDevice, &createInfo)))
|
||||
SDL_GPUTransferBufferCreateInfo createInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = (g_stagingBufferSize = size)};
|
||||
if (!(g_stagingBuffer = SDL_CreateGPUTransferBuffer(g_gpuDevice, &createInfo)))
|
||||
{
|
||||
IAE_LOG_ERROR("Couldn't create a SDL3 GPU Staging Buffer: ", SDL_GetError());
|
||||
return false;
|
||||
|
||||
98
Src/IAEngine/imp/cpp/Rendering/GPUTexture.cpp
Normal file
98
Src/IAEngine/imp/cpp/Rendering/GPUTexture.cpp
Normal file
@ -0,0 +1,98 @@
|
||||
// IAEngine: 2D Game Engine by IA
|
||||
// Copyright (C) 2025 IAS (ias@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 <IAEngine/Rendering/GPUTexture.hpp>
|
||||
|
||||
#include <SDL3/SDL_gpu.h>
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
EXTERN SDL_GPUDevice *g_gpuDevice;
|
||||
|
||||
SDL_GPUSampler *g_defaultSampler{};
|
||||
|
||||
VOID GPUTexture::Initialize()
|
||||
{
|
||||
if (g_defaultSampler)
|
||||
return;
|
||||
SDL_GPUSamplerCreateInfo createInfo{
|
||||
.min_filter = SDL_GPU_FILTER_LINEAR,
|
||||
.mag_filter = SDL_GPU_FILTER_LINEAR,
|
||||
.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,
|
||||
};
|
||||
g_defaultSampler = SDL_CreateGPUSampler(g_gpuDevice, &createInfo);
|
||||
}
|
||||
|
||||
VOID GPUTexture::Terminate()
|
||||
{
|
||||
if (g_defaultSampler)
|
||||
SDL_ReleaseGPUSampler(g_gpuDevice, g_defaultSampler);
|
||||
}
|
||||
|
||||
Handle GPUTexture::GetDefaultSampler()
|
||||
{
|
||||
return (Handle) g_defaultSampler;
|
||||
}
|
||||
|
||||
RefPtr<GPUTexture> GPUTexture::Create(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height)
|
||||
{
|
||||
const auto res = MakeRefPtr<GPUTexture>();
|
||||
|
||||
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
|
||||
.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
|
||||
.usage = SDL_GPU_TEXTUREUSAGE_SAMPLER,
|
||||
.width = (UINT32)width,
|
||||
.height = (UINT32)height,
|
||||
.layer_count_or_depth = 1,
|
||||
.num_levels = 1};
|
||||
|
||||
SDL_GPUTexture *handle{};
|
||||
if (!(handle = SDL_CreateGPUTexture(g_gpuDevice, &createInfo)))
|
||||
{
|
||||
IAE_LOG_ERROR("Failed to create a SDL GPU Texture");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
||||
.size = (UINT32)width * (UINT32)height * 4};
|
||||
const auto stagingBuffer = SDL_CreateGPUTransferBuffer(g_gpuDevice, &stagingBufferCreateInfo);
|
||||
const auto mappedPtr = SDL_MapGPUTransferBuffer(g_gpuDevice, stagingBuffer, false);
|
||||
SDL_memcpy(mappedPtr, rgbaData, width * height * 4);
|
||||
SDL_UnmapGPUTransferBuffer(g_gpuDevice, stagingBuffer);
|
||||
|
||||
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(g_gpuDevice);
|
||||
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
|
||||
SDL_GPUTextureTransferInfo transferInfo{.transfer_buffer = stagingBuffer, .offset = 0};
|
||||
SDL_GPUTextureRegion region{.texture = handle, .w = (UINT32)width, .h = (UINT32)height, .d = 1};
|
||||
SDL_UploadToGPUTexture(copyPass, &transferInfo, ®ion, false);
|
||||
SDL_EndGPUCopyPass(copyPass);
|
||||
SDL_SubmitGPUCommandBuffer(cmdBuffer);
|
||||
SDL_ReleaseGPUTransferBuffer(g_gpuDevice, stagingBuffer);
|
||||
|
||||
res->m_handle = (Handle) handle;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
GPUTexture::~GPUTexture()
|
||||
{
|
||||
if (m_handle)
|
||||
SDL_ReleaseGPUTexture(g_gpuDevice, (SDL_GPUTexture *) m_handle);
|
||||
}
|
||||
} // namespace ia::iae
|
||||
@ -28,7 +28,7 @@ namespace ia::iae
|
||||
Vertex_Mesh vertices[6] = {{iam::Vec3f{-1, 1, 0}, iam::Vec2f{0, 0}}, {iam::Vec3f{1, 1, 0}, iam::Vec2f{1, 0}},
|
||||
{iam::Vec3f{1, -1, 0}, iam::Vec2f{1, 1}}, {iam::Vec3f{-1, 1, 0}, iam::Vec2f{0, 0}},
|
||||
{iam::Vec3f{1, -1, 0}, iam::Vec2f{1, 1}}, {iam::Vec3f{-1, -1, 0}, iam::Vec2f{0, 1}}};
|
||||
g_quadMeshVertexBuffer = GPUBuffer::Create(&vertices, sizeof(vertices));
|
||||
g_quadMeshVertexBuffer = GPUBuffer::Create(GPUBuffer::Usage::VERTEX, &vertices, sizeof(vertices));
|
||||
}
|
||||
|
||||
VOID QuadMesh::Terminate()
|
||||
@ -39,6 +39,6 @@ namespace ia::iae
|
||||
|
||||
VOID QuadMesh::Draw(IN CONST iam::Vec3f &position, IN CONST iam::Vec3f &scale, IN FLOAT32 rotation)
|
||||
{
|
||||
Renderer::Draw((Handle)g_quadMeshVertexBuffer.get(), 6);
|
||||
Renderer::Draw(g_quadMeshVertexBuffer->GetHandle(), 6);
|
||||
}
|
||||
} // namespace ia::iae
|
||||
@ -27,6 +27,8 @@ namespace ia::iae
|
||||
|
||||
Pipeline_UnlitMesh::~Pipeline_UnlitMesh()
|
||||
{
|
||||
if(m_handle)
|
||||
SDL_ReleaseGPUGraphicsPipeline(g_gpuDevice, (SDL_GPUGraphicsPipeline*)m_handle);
|
||||
}
|
||||
|
||||
RefPtr<Pipeline_UnlitMesh> Pipeline_UnlitMesh::Create()
|
||||
@ -34,7 +36,7 @@ namespace ia::iae
|
||||
const auto res = MakeRefPtr<Pipeline_UnlitMesh>();
|
||||
|
||||
const auto vertexShader = LoadShaderFromMemory(ShaderStage::VERTEX, SHADER_SOURCE_UNLITMESH_VERT, sizeof(SHADER_SOURCE_UNLITMESH_VERT), 0, 0, 0, 0);
|
||||
const auto pixelShader = LoadShaderFromMemory(ShaderStage::PIXEL, SHADER_SOURCE_UNLITMESH_FRAG, sizeof(SHADER_SOURCE_UNLITMESH_FRAG), 0, 0, 0, 0);
|
||||
const auto pixelShader = LoadShaderFromMemory(ShaderStage::PIXEL, SHADER_SOURCE_UNLITMESH_FRAG, sizeof(SHADER_SOURCE_UNLITMESH_FRAG), 1, 0, 0, 0);
|
||||
|
||||
SDL_GPUColorTargetDescription colorTargets[] = {
|
||||
{.format = SDL_GetGPUSwapchainTextureFormat(g_gpuDevice, g_windowHandle)}};
|
||||
@ -49,7 +51,7 @@ namespace ia::iae
|
||||
{.location = 1,
|
||||
.buffer_slot = 0,
|
||||
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
|
||||
.offset = sizeof(float) * 3}};
|
||||
.offset = sizeof(iam::Vec3f)}};
|
||||
SDL_GPUGraphicsPipelineCreateInfo createInfo = {
|
||||
.vertex_shader = (SDL_GPUShader *) vertexShader,
|
||||
.fragment_shader = (SDL_GPUShader *) pixelShader,
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
|
||||
#include <IAEngine/IAEngine.hpp>
|
||||
#include <IAEngine/Rendering/GPUBuffer.hpp>
|
||||
#include <IAEngine/Rendering/GPUTexture.hpp>
|
||||
#include <IAEngine/Rendering/Mesh/Quad.hpp>
|
||||
#include <IAEngine/Rendering/Pipeline/UnlitMesh.hpp>
|
||||
#include <IAEngine/Rendering/Renderer.hpp>
|
||||
@ -87,6 +88,8 @@ namespace ia::iae
|
||||
if (!GPUBuffer::InitializeStagingBuffer())
|
||||
return false;
|
||||
|
||||
GPUTexture::Initialize();
|
||||
|
||||
g_pipelineUnlitMesh = Pipeline_UnlitMesh::Create();
|
||||
|
||||
QuadMesh::Initialize();
|
||||
@ -98,8 +101,12 @@ namespace ia::iae
|
||||
{
|
||||
SDL_WaitForGPUIdle(g_gpuDevice);
|
||||
|
||||
g_pipelineUnlitMesh.reset();
|
||||
|
||||
QuadMesh::Terminate();
|
||||
|
||||
GPUTexture::Terminate();
|
||||
|
||||
GPUBuffer::TerminateStagingBuffer();
|
||||
|
||||
ImGui_ImplSDL3_Shutdown();
|
||||
@ -172,6 +179,15 @@ namespace ia::iae
|
||||
SDL_SubmitGPUCommandBuffer(g_cmdBuffer);
|
||||
}
|
||||
|
||||
VOID Renderer::BindTexture(IN Handle handle, IN INT32 index)
|
||||
{
|
||||
SDL_GPUTextureSamplerBinding binding {
|
||||
.texture = (SDL_GPUTexture*)handle,
|
||||
.sampler = (SDL_GPUSampler*)GPUTexture::GetDefaultSampler()
|
||||
};
|
||||
SDL_BindGPUFragmentSamplers(g_renderPass, index, &binding, 1);
|
||||
}
|
||||
|
||||
VOID Renderer::Draw(IN Handle vertexBufferHandle, IN INT32 vertexCount)
|
||||
{
|
||||
SDL_GPUBufferBinding bindings[] = {{.buffer = (SDL_GPUBuffer *) vertexBufferHandle, .offset = 0}};
|
||||
|
||||
@ -1,46 +0,0 @@
|
||||
// IAEngine: 2D Game Engine by IA
|
||||
// Copyright (C) 2025 IAS (ias@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 <IAEngine/IAEngine.hpp>
|
||||
#include <IAEngine/Rendering/Texture.hpp>
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#define TEXTURE_HANDLE (SDL_Texture *) m_handle
|
||||
|
||||
namespace ia::iae
|
||||
{
|
||||
Texture::Texture()
|
||||
{
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
SDL_DestroyTexture(TEXTURE_HANDLE);
|
||||
}
|
||||
|
||||
VOID Texture::Draw(IN CONST iam::Vec3f &position, IN CONST iam::Vec3f &scale, IN FLOAT32 rotation, IN BOOL flipH,
|
||||
IN BOOL flipV, IN CONST iam::Vec4f &colorOverlay) CONST
|
||||
{
|
||||
// SDL_FRect rect{.x = position.X, .y = position.Y, .w = m_width * scale.X, .h = m_height * scale.Y};
|
||||
//SDL_SetTextureColorModFloat(TEXTURE_HANDLE, colorOverlay.X, colorOverlay.Y, colorOverlay.Z);
|
||||
//SDL_SetTextureAlphaModFloat(TEXTURE_HANDLE, colorOverlay.W);
|
||||
//SDL_RenderTextureRotated((SDL_Renderer *) m_engine->GetRendererHandle(), TEXTURE_HANDLE, nullptr, &rect,
|
||||
// rotation, nullptr,
|
||||
// (SDL_FlipMode) (SDL_FLIP_NONE | (flipV ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE) |
|
||||
// (flipH ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE)));
|
||||
}
|
||||
} // namespace ia::iae
|
||||
Reference in New Issue
Block a user