Cleanup 2/2
This commit is contained in:
File diff suppressed because one or more lines are too long
@ -19,7 +19,7 @@
|
||||
namespace ia::iae
|
||||
{
|
||||
RDC_Pipeline::RDC_Pipeline(IN SDL_GPUTextureFormat renderTargetFormat, IN CONST StageDesc &vertexStageDesc,
|
||||
IN CONST StageDesc &pixelStageDesc, IN BOOL enableVertexBuffer)
|
||||
IN CONST StageDesc &pixelStageDesc, IN SDL_GPUPrimitiveType primitiveType, IN BOOL enableVertexBuffer)
|
||||
{
|
||||
SDL_GPUShader *vertexShader{};
|
||||
SDL_GPUShader *pixelShader{};
|
||||
@ -69,7 +69,7 @@ namespace ia::iae
|
||||
|
||||
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)}};
|
||||
{.location = 1, .buffer_slot = 0, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT4, .offset = sizeof(Vec2)}};
|
||||
|
||||
SDL_GPUGraphicsPipelineCreateInfo createInfo = {
|
||||
.vertex_shader = vertexShader,
|
||||
@ -77,8 +77,8 @@ namespace ia::iae
|
||||
.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,
|
||||
.num_vertex_attributes = enableVertexBuffer ? (UINT32)2 : 0},
|
||||
.primitive_type = primitiveType,
|
||||
.rasterizer_state = SDL_GPURasterizerState{.fill_mode = SDL_GPU_FILLMODE_FILL,
|
||||
.cull_mode = SDL_GPU_CULLMODE_NONE,
|
||||
.front_face = SDL_GPU_FRONTFACE_COUNTER_CLOCKWISE,
|
||||
|
||||
@ -26,19 +26,30 @@ namespace ia::iae
|
||||
Mat4 RDC::s_projectionMatrix;
|
||||
SDL_Window *RDC::s_windowHandle;
|
||||
Handle RDC::s_quadGeometry;
|
||||
|
||||
RDC_Pipeline *RDC::s_primitiveDrawPipeline;
|
||||
RDC_Pipeline *RDC::s_dynamicSpritePipeline;
|
||||
|
||||
SDL_GPUSampler *RDC::s_linearClampSampler;
|
||||
SDL_GPUSampler *RDC::s_linearRepeatSampler;
|
||||
Vec2 RDC::s_cameraPosition{};
|
||||
IVec2 RDC::s_viewportExtent;
|
||||
|
||||
RDC_Texture *RDC::s_defaultTexture{};
|
||||
|
||||
RDC_TextureAtlas *RDC::s_staticSpriteAtlas{};
|
||||
RDC_TextureAtlas *RDC::s_dynamicSpriteAtlas{};
|
||||
INT32 RDC::s_spriteInstanceCount{};
|
||||
|
||||
RDC_HostVisibleBuffer *RDC::s_primitiveInstanceBuffer{};
|
||||
RDC_HostVisibleBuffer *RDC::s_staticSpriteInstanceBuffer{};
|
||||
RDC_HostVisibleBuffer *RDC::s_dynamicSpriteInstanceBuffer{};
|
||||
RDC_Texture *RDC::s_defaultTexture{};
|
||||
|
||||
INT32 RDC::s_spriteInstanceCount{};
|
||||
RDC_SpriteInstanceData RDC::s_spriteInstances[RDC::MAX_SPRITE_COUNT];
|
||||
|
||||
INT32 RDC::s_primitiveInstanceCount{};
|
||||
GeometryVertex RDC::s_primitiveInstances[RDC::MAX_PRIMITIVE_COUNT];
|
||||
|
||||
VOID RDC::Initialize(IN IVec2 viewportExtent, IN SDL_Window *windowHandle, IN BOOL isDebugMode)
|
||||
{
|
||||
EmbeddedResources::Initialize();
|
||||
@ -69,7 +80,11 @@ namespace ia::iae
|
||||
delete s_defaultTexture;
|
||||
delete s_staticSpriteAtlas;
|
||||
delete s_dynamicSpriteAtlas;
|
||||
|
||||
delete s_primitiveDrawPipeline;
|
||||
delete s_dynamicSpritePipeline;
|
||||
|
||||
delete s_primitiveInstanceBuffer;
|
||||
delete s_staticSpriteInstanceBuffer;
|
||||
delete s_dynamicSpriteInstanceBuffer;
|
||||
|
||||
@ -85,11 +100,12 @@ namespace ia::iae
|
||||
glm::orthoLH(0.0f, (FLOAT32) s_viewportExtent.x, (FLOAT32) s_viewportExtent.y, 0.0f, -1.0f, 1.0f);
|
||||
}
|
||||
|
||||
VOID RDC::Render(IN SDL_GPURenderPass* renderPass, IN SDL_GPUCommandBuffer* commandBuffer)
|
||||
VOID RDC::Render(IN SDL_GPURenderPass *renderPass, IN SDL_GPUCommandBuffer *commandBuffer)
|
||||
{
|
||||
s_dynamicSpritePipeline->Bind(renderPass);
|
||||
SDL_PushGPUVertexUniformData(commandBuffer, 0, &s_projectionMatrix, sizeof(Mat4));
|
||||
SDL_PushGPUVertexUniformData(commandBuffer, 1, &s_viewMatrix, sizeof(Mat4));
|
||||
|
||||
SDL_GPUTextureSamplerBinding textureBinding{.texture = s_dynamicSpriteAtlas
|
||||
? s_dynamicSpriteAtlas->GetTexture()->GetHandle()
|
||||
: s_defaultTexture->GetHandle(),
|
||||
@ -98,13 +114,27 @@ namespace ia::iae
|
||||
RDC_Device::BindGeometry(renderPass, s_quadGeometry);
|
||||
if (s_spriteInstanceCount)
|
||||
{
|
||||
const auto spriteInstanceBuffer = s_dynamicSpriteInstanceBuffer->GetHandle();
|
||||
const auto buffer = s_dynamicSpriteInstanceBuffer->GetHandle();
|
||||
s_dynamicSpriteInstanceBuffer->CopyFrom(s_spriteInstances,
|
||||
sizeof(RDC_SpriteInstanceData) * s_spriteInstanceCount);
|
||||
SDL_BindGPUVertexStorageBuffers(renderPass, 0, &spriteInstanceBuffer, 1);
|
||||
SDL_BindGPUVertexStorageBuffers(renderPass, 0, &buffer, 1);
|
||||
SDL_DrawGPUIndexedPrimitives(renderPass, 6, s_spriteInstanceCount, 0, 0, 0);
|
||||
}
|
||||
s_spriteInstanceCount = 0;
|
||||
|
||||
s_primitiveDrawPipeline->Bind(renderPass);
|
||||
SDL_PushGPUVertexUniformData(commandBuffer, 0, &s_projectionMatrix, sizeof(Mat4));
|
||||
SDL_PushGPUVertexUniformData(commandBuffer, 1, &s_viewMatrix, sizeof(Mat4));
|
||||
|
||||
if (s_primitiveInstanceCount)
|
||||
{
|
||||
s_primitiveInstanceBuffer->CopyFrom(s_primitiveInstances,
|
||||
sizeof(GeometryVertex) * s_primitiveInstanceCount);
|
||||
SDL_GPUBufferBinding bufferBindings[] = {{.buffer = s_primitiveInstanceBuffer->GetHandle(), .offset = 0}};
|
||||
SDL_BindGPUVertexBuffers(renderPass, 0, &bufferBindings[0], 1);
|
||||
SDL_DrawGPUPrimitives(renderPass, s_primitiveInstanceCount << 1, 1, 0, 0);
|
||||
}
|
||||
s_primitiveInstanceCount = 0;
|
||||
}
|
||||
|
||||
VOID RDC::RenderToWindow()
|
||||
@ -192,6 +222,27 @@ namespace ia::iae
|
||||
.Color = {1.0f, 1.0f, 1.0f, 1.0f}};
|
||||
return _s;
|
||||
}
|
||||
|
||||
VOID RDC::DrawLine(IN Vec2 start, IN Vec2 end, IN Vec4 color)
|
||||
{
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {start, color};
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = { end, color };
|
||||
}
|
||||
|
||||
VOID RDC::DrawRect(IN Vec2 start, IN Vec2 end, IN Vec4 color)
|
||||
{
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {start, color};
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {Vec2{end.x, start.y}, color};
|
||||
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {Vec2{end.x, start.y}, color};
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {end, color};
|
||||
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {end, color};
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {Vec2{start.x, end.y}, color};
|
||||
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {Vec2{start.x, end.y}, color};
|
||||
s_primitiveInstances[s_primitiveInstanceCount++] = {start, color};
|
||||
}
|
||||
} // namespace ia::iae
|
||||
|
||||
namespace ia::iae
|
||||
@ -271,6 +322,7 @@ namespace ia::iae
|
||||
{
|
||||
s_dynamicSpriteInstanceBuffer =
|
||||
new RDC_HostVisibleBuffer(RDC_Buffer::EType::STORAGE, sizeof(s_spriteInstances));
|
||||
s_primitiveInstanceBuffer = new RDC_HostVisibleBuffer(RDC_Buffer::EType::VERTEX, sizeof(s_primitiveInstances));
|
||||
}
|
||||
|
||||
VOID RDC::InitializeTextures()
|
||||
@ -286,6 +338,22 @@ namespace ia::iae
|
||||
|
||||
VOID RDC::InitializePipelines()
|
||||
{
|
||||
s_primitiveDrawPipeline =
|
||||
new RDC_Pipeline(RDC_Device::GetSwapchainTextureFormat(),
|
||||
RDC_Pipeline::StageDesc{
|
||||
.SourceData = EmbeddedResources::GetResource("Shaders/PrimitiveDraw.vert"),
|
||||
.SamplerCount = 0,
|
||||
.UniformBufferCount = 2,
|
||||
.StorageBufferCount = 0,
|
||||
},
|
||||
RDC_Pipeline::StageDesc{
|
||||
.SourceData = EmbeddedResources::GetResource("Shaders/PrimitiveDraw.frag"),
|
||||
.SamplerCount = 0,
|
||||
.UniformBufferCount = 0,
|
||||
.StorageBufferCount = 0,
|
||||
},
|
||||
SDL_GPU_PRIMITIVETYPE_LINELIST, true);
|
||||
|
||||
s_dynamicSpritePipeline =
|
||||
new RDC_Pipeline(RDC_Device::GetSwapchainTextureFormat(),
|
||||
RDC_Pipeline::StageDesc{
|
||||
@ -300,17 +368,17 @@ namespace ia::iae
|
||||
.UniformBufferCount = 0,
|
||||
.StorageBufferCount = 0,
|
||||
},
|
||||
true);
|
||||
SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, 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}},
|
||||
{glm::vec2{0.0f, 1.0f}, glm::vec4{1.0f, 1.0f, 0.0f, 1.0f}},
|
||||
{glm::vec2{1.0f, 1.0f}, glm::vec4{1.0f, 1.0f, 1.0f, 1.0f}},
|
||||
{glm::vec2{1.0f, 0.0f}, glm::vec4{1.0f, 1.0f, 1.0f, 0.0f}},
|
||||
{glm::vec2{0.0f, 0.0f}, glm::vec4{1.0f, 1.0f, 0.0f, 0.0f}},
|
||||
},
|
||||
{0, 1, 2, 2, 3, 0});
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ namespace ia::iae
|
||||
: (UINT32) 1)
|
||||
{
|
||||
SDL_GPUTextureCreateInfo createInfo{.type = SDL_GPU_TEXTURETYPE_2D,
|
||||
.format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
|
||||
.format = RDC_Device::GetSwapchainTextureFormat(),
|
||||
.width = (UINT32) width,
|
||||
.height = (UINT32) height,
|
||||
.layer_count_or_depth = 1,
|
||||
@ -54,6 +54,10 @@ namespace ia::iae
|
||||
|
||||
TMP_COLOR_BUFFER.resize(m_width * m_height * 4);
|
||||
|
||||
const auto swapchainFormat = RDC_Device::GetSwapchainTextureFormat();
|
||||
const auto swapRB = (swapchainFormat == SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM) ||
|
||||
(swapchainFormat == SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB);
|
||||
|
||||
if (stride == -1)
|
||||
{
|
||||
for (SIZE_T i = 0; i < TMP_COLOR_BUFFER.size() >> 2; i++)
|
||||
@ -63,6 +67,9 @@ namespace ia::iae
|
||||
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];
|
||||
|
||||
if (swapRB)
|
||||
std::swap(TMP_COLOR_BUFFER[i * 4 + 0], TMP_COLOR_BUFFER[i * 4 + 2]);
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -77,6 +84,9 @@ namespace ia::iae
|
||||
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];
|
||||
if (swapRB)
|
||||
std::swap(TMP_COLOR_BUFFER[(x + y * m_width) * 4 + 0],
|
||||
TMP_COLOR_BUFFER[(x + y * m_width) * 4 + 0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user