685 lines
29 KiB
C++
685 lines
29 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 <Renderer.hpp>
|
|
|
|
#include <EmbeddedResources.hpp>
|
|
|
|
namespace ia::iae
|
|
{
|
|
STATIC CONSTEXPR INT32 MAX_SPRITE_COUNT = 100000;
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct SpriteInstanceData
|
|
{
|
|
Mat4 Transform{1.0f};
|
|
Vec4 TexCoords{};
|
|
Vec4 Color{1.0f, 1.0f, 1.0f, 1.0f};
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
struct TextureData
|
|
{
|
|
PUINT8 Pixels{};
|
|
INT32 Width{};
|
|
INT32 Height{};
|
|
INT32 TileWidth{};
|
|
INT32 TileHeight{};
|
|
INT32 TileCountX{};
|
|
INT32 TileCountY{};
|
|
SDL_GPUTexture *BakedHandle{};
|
|
};
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
IVec2 g_screenExtent{};
|
|
|
|
SDL_GPUDevice *g_gpuDevice{};
|
|
|
|
RenderPipeline *g_debugPipeline{};
|
|
RenderPipeline *g_geometryPipeline{};
|
|
SDL_GPUSampler *g_linearClampSampler{};
|
|
SDL_GPUSampler *g_linearRepeatSampler{};
|
|
|
|
Mat4 g_viewMatrix{1.0f};
|
|
Vec2 g_cameraPosition{};
|
|
Mat4 g_projectionMatrix{1.0f};
|
|
|
|
Geometry *g_quadGeometry;
|
|
|
|
Vector<TextureData> g_texureData;
|
|
|
|
Vector<SpriteInstanceData> g_staticSprites;
|
|
Vector<SpriteInstanceData> g_dynamicSprites;
|
|
|
|
SDL_GPUBuffer *g_staticSpriteDataBuffer{};
|
|
SDL_GPUBuffer *g_dynamicSpriteDataBuffer{};
|
|
|
|
SDL_GPUTransferBuffer *g_spriteDataStagingBuffer{};
|
|
|
|
SDL_GPUTexture *g_defaultTexture{};
|
|
SDL_GPUTexture *g_activeTextureAtlas{};
|
|
|
|
Map<Handle, Vec4> g_activeTextureAtlasUVMap;
|
|
|
|
EXTERN BOOL g_isDebugMode;
|
|
EXTERN Vec2 g_designViewport;
|
|
EXTERN SDL_Window *g_windowHandle;
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
VOID Renderer::Initialize(IN IVec2 screenExtent)
|
|
{
|
|
g_screenExtent = screenExtent;
|
|
|
|
InitializeGPU();
|
|
InitializeSampler();
|
|
InitializePipelines();
|
|
InitializeGeometries();
|
|
InitializeDrawData();
|
|
InitializeTextures();
|
|
}
|
|
|
|
VOID Renderer::Terminate()
|
|
{
|
|
SDL_WaitForGPUIdle(g_gpuDevice);
|
|
|
|
delete g_debugPipeline;
|
|
delete g_geometryPipeline;
|
|
|
|
if (g_defaultTexture)
|
|
DestroyTexture(g_defaultTexture);
|
|
if (g_activeTextureAtlas && (g_activeTextureAtlas != g_defaultTexture))
|
|
DestroyTexture(g_activeTextureAtlas);
|
|
|
|
DestroyBuffer(g_staticSpriteDataBuffer);
|
|
DestroyBuffer(g_dynamicSpriteDataBuffer);
|
|
|
|
SDL_ReleaseGPUTransferBuffer(g_gpuDevice, g_spriteDataStagingBuffer);
|
|
|
|
SDL_ReleaseGPUSampler(g_gpuDevice, g_linearClampSampler);
|
|
SDL_ReleaseGPUSampler(g_gpuDevice, g_linearRepeatSampler);
|
|
|
|
DestroyGeometry(g_quadGeometry);
|
|
|
|
SDL_ReleaseWindowFromGPUDevice(g_gpuDevice, g_windowHandle);
|
|
SDL_DestroyGPUDevice(g_gpuDevice);
|
|
}
|
|
|
|
VOID Renderer::Update()
|
|
{
|
|
DrawStaticSpriteTopLeft(0, 0, {10.0f, 0.0f}, {100.0f, 100.0f}, 0.0f);
|
|
DrawStaticSpriteTopLeft(0, 0, {200.0f, 300.0f}, {100.0f, 100.0f}, 0.0f);
|
|
Render();
|
|
}
|
|
|
|
VOID Renderer::Render()
|
|
{
|
|
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(g_gpuDevice)))
|
|
THROW_UNKNOWN("Failed to acquire SDL GPU command buffer: ", SDL_GetError());
|
|
|
|
SDL_GPUTexture *swapChainTexture{};
|
|
if (!SDL_WaitAndAcquireGPUSwapchainTexture(ActiveCommandBuffer, g_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);
|
|
|
|
SDL_BindGPUGraphicsPipeline(ActiveRenderPass, g_debugPipeline->GetHandle());
|
|
SDL_PushGPUVertexUniformData(ActiveCommandBuffer, 0, &g_projectionMatrix, sizeof(Mat4));
|
|
SDL_PushGPUVertexUniformData(ActiveCommandBuffer, 1, &g_viewMatrix, sizeof(Mat4));
|
|
SDL_GPUBufferBinding bufferBindings[] = {{.buffer = ((Geometry *) g_quadGeometry)->VertexBuffer, .offset = 0},
|
|
{.buffer = ((Geometry *) g_quadGeometry)->IndexBuffer, .offset = 0}};
|
|
SDL_BindGPUVertexBuffers(ActiveRenderPass, 0, &bufferBindings[0], 1);
|
|
SDL_BindGPUIndexBuffer(ActiveRenderPass, &bufferBindings[1], SDL_GPU_INDEXELEMENTSIZE_32BIT);
|
|
|
|
SDL_GPUTextureSamplerBinding textureBinding{.texture = g_activeTextureAtlas,
|
|
.sampler = GetSampler_LinearRepeat()};
|
|
SDL_BindGPUFragmentSamplers(ActiveRenderPass, 0, &textureBinding, 1);
|
|
|
|
if (g_staticSprites.size())
|
|
{
|
|
CopyToDeviceLocalBuffer(g_spriteDataStagingBuffer, g_staticSpriteDataBuffer, g_staticSprites.data(),
|
|
g_staticSprites.size() * sizeof(SpriteInstanceData));
|
|
SDL_BindGPUVertexStorageBuffers(ActiveRenderPass, 0, &g_staticSpriteDataBuffer, 1);
|
|
SDL_DrawGPUIndexedPrimitives(ActiveRenderPass, g_quadGeometry->IndexCount, g_staticSprites.size(), 0, 0, 0);
|
|
}
|
|
|
|
if (g_dynamicSprites.size())
|
|
{
|
|
CopyToDeviceLocalBuffer(g_spriteDataStagingBuffer, g_dynamicSpriteDataBuffer, g_dynamicSprites.data(),
|
|
g_dynamicSprites.size() * sizeof(SpriteInstanceData));
|
|
SDL_BindGPUVertexStorageBuffers(ActiveRenderPass, 0, &g_dynamicSpriteDataBuffer, 1);
|
|
SDL_DrawGPUIndexedPrimitives(ActiveRenderPass, g_quadGeometry->IndexCount, g_dynamicSprites.size(), 0, 0,
|
|
0);
|
|
}
|
|
|
|
g_staticSprites.resize(0);
|
|
g_dynamicSprites.resize(0);
|
|
|
|
SDL_EndGPURenderPass(ActiveRenderPass);
|
|
|
|
SDL_SubmitGPUCommandBuffer(ActiveCommandBuffer);
|
|
}
|
|
|
|
Vec2 Renderer::GetCameraPosition()
|
|
{
|
|
return g_cameraPosition;
|
|
}
|
|
|
|
VOID Renderer::SetCameraPosition(IN Vec2 position)
|
|
{
|
|
g_cameraPosition = position;
|
|
g_viewMatrix = glm::lookAtLH(glm::vec3{g_cameraPosition, -1.0f}, {g_cameraPosition, 0.0f}, {0.0f, 1.0f, 0.0f});
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
VOID Renderer::DrawStaticSpriteTopLeft(IN Handle texture, IN INT32 tileIndex, IN Vec2 position, IN Vec2 scale,
|
|
IN FLOAT32 rotation)
|
|
{
|
|
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{scale.x, scale.y, 1.0f});
|
|
g_staticSprites.pushBack({.Transform = transform,
|
|
.TexCoords = GetTextureAtlasCoordinates(texture, tileIndex),
|
|
.Color = {1.0f, 1.0f, 1.0f, 1.0f}});
|
|
}
|
|
|
|
VOID Renderer::DrawDynamicSpriteTopLeft(IN Handle texture, IN INT32 tileIndex, IN Vec2 position, IN Vec2 scale,
|
|
IN FLOAT32 rotation)
|
|
{
|
|
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{scale.x, scale.y, 1.0f});
|
|
g_dynamicSprites.pushBack({.Transform = transform,
|
|
.TexCoords = GetTextureAtlasCoordinates(texture, tileIndex),
|
|
.Color = {1.0f, 1.0f, 1.0f, 1.0f}});
|
|
}
|
|
|
|
Geometry *Renderer::CreateGeometry(IN CONST Vector<GeometryVertex> &vertices, IN CONST Vector<INT32> &indices)
|
|
{
|
|
const auto mesh = new Geometry();
|
|
mesh->VertexBuffer = CreateDeviceLocalBuffer(SDL_GPU_BUFFERUSAGE_VERTEX, vertices.data(),
|
|
static_cast<UINT32>(vertices.size() * sizeof(vertices[0])));
|
|
mesh->IndexBuffer = CreateDeviceLocalBuffer(SDL_GPU_BUFFERUSAGE_INDEX, indices.data(),
|
|
static_cast<UINT32>(indices.size() * sizeof(indices[0])));
|
|
mesh->IndexCount = static_cast<UINT32>(indices.size());
|
|
return mesh;
|
|
}
|
|
|
|
VOID Renderer::DestroyGeometry(IN Geometry *geometry)
|
|
{
|
|
DestroyBuffer(geometry->VertexBuffer);
|
|
DestroyBuffer(geometry->IndexBuffer);
|
|
delete geometry;
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
Handle Renderer::CreateTexture(IN PCUINT8 rgbaData, IN INT32 width, IN INT32 height, IN INT32 tileCountX,
|
|
IN INT32 tileCountY)
|
|
{
|
|
const auto pixelDataSize = width * height * 4;
|
|
|
|
g_texureData.pushBack({.Pixels = new UINT8[pixelDataSize],
|
|
.Width = width,
|
|
.Height = height,
|
|
.TileWidth = width / tileCountX,
|
|
.TileHeight = height / tileCountY,
|
|
.TileCountX = tileCountX,
|
|
.TileCountY = tileCountY,
|
|
.BakedHandle = nullptr});
|
|
|
|
ia_memcpy(g_texureData.back().Pixels, rgbaData, pixelDataSize);
|
|
|
|
return g_texureData.size() - 1;
|
|
}
|
|
|
|
VOID Renderer::DestroyTexture(IN Handle texture)
|
|
{
|
|
auto &t = g_texureData[texture];
|
|
if (t.Pixels)
|
|
delete[] t.Pixels;
|
|
if (t.BakedHandle)
|
|
DestroyTexture(t.BakedHandle);
|
|
t.Pixels = nullptr;
|
|
t.BakedHandle = nullptr;
|
|
}
|
|
|
|
VOID Renderer::BakeTexture(IN Handle texture)
|
|
{
|
|
auto &t = g_texureData[texture];
|
|
t.BakedHandle = CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER | SDL_GPU_TEXTUREUSAGE_COLOR_TARGET, t.Width,
|
|
t.Height, t.Width, t.Pixels);
|
|
}
|
|
|
|
VOID Renderer::SetTextureAtlas(IN CONST Vector<Handle> textures)
|
|
{
|
|
g_activeTextureAtlasUVMap = Map<Handle, Vec4>();
|
|
if (g_activeTextureAtlas)
|
|
DestroyTexture(g_activeTextureAtlas);
|
|
|
|
INT32 atlasWidth{0}, atlasHeight{0};
|
|
for (const auto &t : textures)
|
|
{
|
|
const auto &d = g_texureData[t];
|
|
atlasWidth += d.Width;
|
|
if (d.Height > atlasHeight)
|
|
atlasHeight = d.Height;
|
|
}
|
|
|
|
const auto pixels = new UINT8[atlasWidth * atlasHeight * 4];
|
|
|
|
INT32 atlasCursor{0};
|
|
for (const auto &t : textures)
|
|
{
|
|
const auto &d = g_texureData[t];
|
|
for (INT32 y = 0; y < d.Height; y++)
|
|
ia_memcpy(&pixels[atlasCursor + (y * atlasWidth)], d.Pixels, d.Width * 4);
|
|
g_activeTextureAtlasUVMap[t] =
|
|
Vec4(((FLOAT32) atlasCursor) / ((FLOAT32) atlasWidth), 0.0f,
|
|
((FLOAT32) d.Width) / ((FLOAT32) atlasWidth), ((FLOAT32) d.Height) / ((FLOAT32) atlasHeight));
|
|
atlasCursor += d.Width;
|
|
}
|
|
|
|
g_activeTextureAtlas = CreateTexture(SDL_GPU_TEXTUREUSAGE_SAMPLER | SDL_GPU_TEXTUREUSAGE_COLOR_TARGET,
|
|
atlasWidth, atlasHeight, atlasWidth, pixels);
|
|
|
|
delete[] pixels;
|
|
}
|
|
|
|
Vec4 Renderer::GetTextureAtlasCoordinates(IN Handle texture, IN INT32 tileIndex)
|
|
{
|
|
const auto &d = g_texureData[texture];
|
|
const auto &t = g_activeTextureAtlasUVMap[texture];
|
|
return t;
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
SDL_GPUTexture *Renderer::CreateTexture(IN SDL_GPUTextureUsageFlags usage, IN INT32 width, IN INT32 height,
|
|
IN INT32 stride, IN PCUINT8 rgbaData, IN SDL_GPUTextureFormat format,
|
|
IN BOOL generateMipmaps)
|
|
{
|
|
const auto mipLevels =
|
|
generateMipmaps ? ia_max((UINT32) (floor(log2(ia_max(width, height))) + 1), (UINT32) 1) : (UINT32) 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 = (UINT32) mipLevels,
|
|
.sample_count = SDL_GPU_SAMPLECOUNT_1};
|
|
const auto result = SDL_CreateGPUTexture(g_gpuDevice, &createInfo);
|
|
if (!result)
|
|
{
|
|
THROW_UNKNOWN("Failed to create a SDL GPU Texture: ", SDL_GetError());
|
|
return nullptr;
|
|
}
|
|
if (rgbaData)
|
|
{
|
|
TMP_COLOR_BUFFER.resize(width * height * 4);
|
|
|
|
if (stride == width)
|
|
{
|
|
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 < height; y++)
|
|
{
|
|
for (INT32 x = 0; x < 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 * width) * 4 + 0] = static_cast<UINT8>(p[0] * a);
|
|
TMP_COLOR_BUFFER[(x + y * width) * 4 + 1] = static_cast<UINT8>(p[1] * a);
|
|
TMP_COLOR_BUFFER[(x + y * width) * 4 + 2] = static_cast<UINT8>(p[2] * a);
|
|
TMP_COLOR_BUFFER[(x + y * width) * 4 + 3] = p[3];
|
|
}
|
|
}
|
|
}
|
|
|
|
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, TMP_COLOR_BUFFER.data(), width * height * 4);
|
|
SDL_UnmapGPUTransferBuffer(g_gpuDevice, stagingBuffer);
|
|
|
|
auto cmdBuffer = SDL_AcquireGPUCommandBuffer(g_gpuDevice);
|
|
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
|
|
|
|
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, ®ion, false);
|
|
|
|
SDL_EndGPUCopyPass(copyPass);
|
|
SDL_SubmitGPUCommandBuffer(cmdBuffer);
|
|
SDL_WaitForGPUIdle(g_gpuDevice);
|
|
SDL_ReleaseGPUTransferBuffer(g_gpuDevice, stagingBuffer);
|
|
|
|
if (mipLevels > 1)
|
|
{
|
|
cmdBuffer = SDL_AcquireGPUCommandBuffer(g_gpuDevice);
|
|
SDL_GenerateMipmapsForGPUTexture(cmdBuffer, result);
|
|
SDL_SubmitGPUCommandBuffer(cmdBuffer);
|
|
SDL_WaitForGPUIdle(g_gpuDevice);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
SDL_GPUBuffer *Renderer::CreateDeviceLocalBuffer(IN SDL_GPUBufferUsageFlags usage, IN PCVOID data,
|
|
IN UINT32 dataSize)
|
|
{
|
|
SDL_GPUBufferCreateInfo createInfo{.usage = usage, .size = dataSize};
|
|
const auto result = SDL_CreateGPUBuffer(g_gpuDevice, &createInfo);
|
|
if (!result)
|
|
{
|
|
THROW_UNKNOWN("Failed to create a SDL GPU Buffer: ", SDL_GetError());
|
|
return nullptr;
|
|
}
|
|
if (data && dataSize)
|
|
CopyToDeviceLocalBuffer(result, data, dataSize);
|
|
return result;
|
|
}
|
|
|
|
VOID Renderer::CopyToDeviceLocalBuffer(IN SDL_GPUBuffer *buffer, IN PCVOID data, IN UINT32 dataSize)
|
|
{
|
|
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
|
.size = dataSize};
|
|
const auto stagingBuffer = SDL_CreateGPUTransferBuffer(g_gpuDevice, &stagingBufferCreateInfo);
|
|
CopyToDeviceLocalBuffer(stagingBuffer, buffer, data, dataSize);
|
|
SDL_ReleaseGPUTransferBuffer(g_gpuDevice, stagingBuffer);
|
|
}
|
|
|
|
VOID Renderer::CopyToDeviceLocalBuffer(IN SDL_GPUTransferBuffer *stagingBuffer, IN SDL_GPUBuffer *buffer,
|
|
IN PCVOID data, IN UINT32 dataSize)
|
|
{
|
|
const auto mappedPtr = SDL_MapGPUTransferBuffer(g_gpuDevice, stagingBuffer, false);
|
|
SDL_memcpy(mappedPtr, data, dataSize);
|
|
SDL_UnmapGPUTransferBuffer(g_gpuDevice, stagingBuffer);
|
|
|
|
const auto cmdBuffer = SDL_AcquireGPUCommandBuffer(g_gpuDevice);
|
|
const auto copyPass = SDL_BeginGPUCopyPass(cmdBuffer);
|
|
|
|
SDL_GPUTransferBufferLocation src{.transfer_buffer = stagingBuffer, .offset = 0};
|
|
SDL_GPUBufferRegion dst{.buffer = buffer, .offset = 0, .size = dataSize};
|
|
SDL_UploadToGPUBuffer(copyPass, &src, &dst, false);
|
|
|
|
SDL_EndGPUCopyPass(copyPass);
|
|
SDL_SubmitGPUCommandBuffer(cmdBuffer);
|
|
SDL_WaitForGPUIdle(g_gpuDevice);
|
|
}
|
|
|
|
VOID Renderer::DestroyTexture(IN SDL_GPUTexture *handle)
|
|
{
|
|
if (!handle)
|
|
return;
|
|
SDL_ReleaseGPUTexture(g_gpuDevice, handle);
|
|
}
|
|
|
|
VOID Renderer::DestroyBuffer(IN SDL_GPUBuffer *handle)
|
|
{
|
|
if (!handle)
|
|
return;
|
|
SDL_ReleaseGPUBuffer(g_gpuDevice, handle);
|
|
}
|
|
|
|
SDL_GPUSampler *Renderer::GetSampler_LinearClamp()
|
|
{
|
|
return g_linearClampSampler;
|
|
}
|
|
|
|
SDL_GPUSampler *Renderer::GetSampler_LinearRepeat()
|
|
{
|
|
return g_linearRepeatSampler;
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
VOID Renderer::InitializePipelines()
|
|
{
|
|
g_debugPipeline = new RenderPipeline(
|
|
RenderPipeline::StageDesc{
|
|
.SourceData = EmbeddedResources::GetResource("Shaders/Debug.vert"),
|
|
.SamplerCount = 0,
|
|
.UniformBufferCount = 2,
|
|
.StorageBufferCount = 1,
|
|
},
|
|
RenderPipeline::StageDesc{
|
|
.SourceData = EmbeddedResources::GetResource("Shaders/Debug.frag"),
|
|
.SamplerCount = 1,
|
|
.UniformBufferCount = 0,
|
|
.StorageBufferCount = 0,
|
|
},
|
|
true);
|
|
|
|
g_geometryPipeline = new RenderPipeline(
|
|
RenderPipeline::StageDesc{
|
|
.SourceData = EmbeddedResources::GetResource("Shaders/Geometry.vert"),
|
|
.SamplerCount = 0,
|
|
.UniformBufferCount = 3,
|
|
.StorageBufferCount = 0,
|
|
},
|
|
RenderPipeline::StageDesc{
|
|
.SourceData = EmbeddedResources::GetResource("Shaders/Geometry.frag"),
|
|
.SamplerCount = 1,
|
|
.UniformBufferCount = 1,
|
|
.StorageBufferCount = 0,
|
|
},
|
|
true);
|
|
}
|
|
|
|
RenderPipeline::RenderPipeline(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(g_gpuDevice, &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(g_gpuDevice, &shaderCreateInfo)))
|
|
THROW_UNKNOWN("Failed to create a SDL shader: ", SDL_GetError());
|
|
|
|
SDL_GPUColorTargetDescription colorTargetDesc = {
|
|
.format = SDL_GetGPUSwapchainTextureFormat(g_gpuDevice, g_windowHandle),
|
|
.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(g_gpuDevice, &createInfo)))
|
|
THROW_UNKNOWN("Failed to create a SDL graphics pipeline: ", SDL_GetError());
|
|
|
|
SDL_ReleaseGPUShader(g_gpuDevice, pixelShader);
|
|
SDL_ReleaseGPUShader(g_gpuDevice, vertexShader);
|
|
}
|
|
|
|
RenderPipeline::~RenderPipeline()
|
|
{
|
|
SDL_ReleaseGPUGraphicsPipeline(g_gpuDevice, m_handle);
|
|
}
|
|
} // namespace ia::iae
|
|
|
|
namespace ia::iae
|
|
{
|
|
VOID Renderer::InitializeGPU()
|
|
{
|
|
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, g_isDebugMode);
|
|
SDL_SetBooleanProperty(deviceCreateProps, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_DEPTH_CLAMPING_BOOLEAN, false);
|
|
if (!(g_gpuDevice = SDL_CreateGPUDeviceWithProperties(deviceCreateProps)))
|
|
THROW_UNKNOWN("Failed to create the SDL GPU Device: ", SDL_GetError());
|
|
SDL_DestroyProperties(deviceCreateProps);
|
|
|
|
if (!SDL_ClaimWindowForGPUDevice(g_gpuDevice, g_windowHandle))
|
|
THROW_UNKNOWN("Failed to initialize SDL GPU for the window: ", SDL_GetError());
|
|
|
|
SDL_SetGPUSwapchainParameters(g_gpuDevice, g_windowHandle, SDL_GPU_SWAPCHAINCOMPOSITION_SDR,
|
|
SDL_GPU_PRESENTMODE_VSYNC);
|
|
}
|
|
|
|
VOID Renderer::InitializeSampler()
|
|
{
|
|
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};
|
|
|
|
g_linearClampSampler = SDL_CreateGPUSampler(g_gpuDevice, &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,
|
|
|
|
g_linearRepeatSampler = SDL_CreateGPUSampler(g_gpuDevice, &createInfo);
|
|
}
|
|
|
|
VOID Renderer::InitializeGeometries()
|
|
{
|
|
g_quadGeometry = Renderer::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});
|
|
}
|
|
|
|
VOID Renderer::InitializeDrawData()
|
|
{
|
|
g_staticSpriteDataBuffer = CreateDeviceLocalBuffer(SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ, nullptr,
|
|
sizeof(SpriteInstanceData) * MAX_SPRITE_COUNT);
|
|
g_dynamicSpriteDataBuffer = CreateDeviceLocalBuffer(SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ, nullptr,
|
|
sizeof(SpriteInstanceData) * MAX_SPRITE_COUNT);
|
|
|
|
SDL_GPUTransferBufferCreateInfo stagingBufferCreateInfo{.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD,
|
|
.size = sizeof(SpriteInstanceData) * MAX_SPRITE_COUNT};
|
|
g_spriteDataStagingBuffer = SDL_CreateGPUTransferBuffer(g_gpuDevice, &stagingBufferCreateInfo);
|
|
|
|
g_projectionMatrix =
|
|
glm::orthoLH(0.0f, (FLOAT32) g_screenExtent.x, (FLOAT32) g_screenExtent.y, 0.0f, -1.0f, 1.0f);
|
|
SetCameraPosition({});
|
|
}
|
|
|
|
VOID Renderer::InitializeTextures()
|
|
{
|
|
{ // Create Default Texture
|
|
const auto pixels = new UINT8[100 * 100 * 4];
|
|
ia_memset(pixels, 0xFF, 100 * 100 * 4);
|
|
const auto t = CreateTexture(pixels, 100, 100);
|
|
BakeTexture(t);
|
|
g_defaultTexture = g_texureData[t].BakedHandle;
|
|
delete[] pixels;
|
|
}
|
|
|
|
g_activeTextureAtlas = g_defaultTexture;
|
|
}
|
|
} // namespace ia::iae
|