114 lines
4.8 KiB
C++
114 lines
4.8 KiB
C++
// 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/Pipeline/UnlitMesh.hpp>
|
|
|
|
#include <SDL3/SDL_gpu.h>
|
|
|
|
#include <EmbeddedShaders.hpp>
|
|
|
|
namespace ia::iae
|
|
{
|
|
EXTERN SDL_GPUDevice *g_gpuDevice;
|
|
EXTERN SDL_Window *g_windowHandle;
|
|
|
|
Pipeline_UnlitMesh::~Pipeline_UnlitMesh()
|
|
{
|
|
if(m_handle)
|
|
SDL_ReleaseGPUGraphicsPipeline(g_gpuDevice, (SDL_GPUGraphicsPipeline*)m_handle);
|
|
}
|
|
|
|
RefPtr<Pipeline_UnlitMesh> Pipeline_UnlitMesh::Create()
|
|
{
|
|
const auto res = MakeRefPtr<Pipeline_UnlitMesh>();
|
|
|
|
const auto vertexShader = LoadShaderFromMemory(ShaderStage::VERTEX, SHADER_SOURCE_UNLITMESH_VERT, sizeof(SHADER_SOURCE_UNLITMESH_VERT), 0, 3, 0, 0);
|
|
const auto pixelShader = LoadShaderFromMemory(ShaderStage::PIXEL, SHADER_SOURCE_UNLITMESH_FRAG, sizeof(SHADER_SOURCE_UNLITMESH_FRAG), 1, 1, 0, 0);
|
|
|
|
SDL_GPUColorTargetDescription colorTargets[] = {
|
|
{.format = SDL_GetGPUSwapchainTextureFormat(g_gpuDevice, g_windowHandle),
|
|
.blend_state = SDL_GPUColorTargetBlendState{
|
|
.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,
|
|
}}};
|
|
SDL_GPUVertexBufferDescription vertexBuffers[] = {{
|
|
.slot = 0,
|
|
.pitch = sizeof(Vertex_Mesh),
|
|
.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX,
|
|
.instance_step_rate = 0,
|
|
}};
|
|
SDL_GPUVertexAttribute vertexAttributes[] = {
|
|
{.location = 0, .buffer_slot = 0, .format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3, .offset = 0},
|
|
{.location = 1,
|
|
.buffer_slot = 0,
|
|
.format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2,
|
|
.offset = sizeof(glm::vec3)}};
|
|
SDL_GPUGraphicsPipelineCreateInfo createInfo = {
|
|
.vertex_shader = (SDL_GPUShader *) vertexShader,
|
|
.fragment_shader = (SDL_GPUShader *) pixelShader,
|
|
.vertex_input_state =
|
|
SDL_GPUVertexInputState{.vertex_buffer_descriptions = vertexBuffers,
|
|
.num_vertex_buffers = sizeof(vertexBuffers) / sizeof(vertexBuffers[0]),
|
|
.vertex_attributes = vertexAttributes,
|
|
.num_vertex_attributes =
|
|
sizeof(vertexAttributes) / sizeof(vertexAttributes[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
|
|
},
|
|
.depth_stencil_state = SDL_GPUDepthStencilState{
|
|
.compare_op = SDL_GPU_COMPAREOP_LESS,
|
|
.write_mask = 0xFF,
|
|
.enable_depth_test = true,
|
|
.enable_depth_write = true,
|
|
.enable_stencil_test = false
|
|
},
|
|
.target_info =
|
|
{
|
|
.color_target_descriptions = colorTargets,
|
|
.num_color_targets = sizeof(colorTargets) / sizeof(colorTargets[0]),
|
|
.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM,
|
|
.has_depth_stencil_target = true
|
|
},
|
|
};
|
|
|
|
SDL_GPUGraphicsPipeline* handle{};
|
|
if(!(handle = SDL_CreateGPUGraphicsPipeline(g_gpuDevice, &createInfo)))
|
|
{
|
|
IAE_LOG_ERROR("Failed to create a SDL graphics pipeline: ", SDL_GetError());
|
|
return nullptr;
|
|
}
|
|
|
|
UnloadShader(pixelShader);
|
|
UnloadShader(vertexShader);
|
|
|
|
res->m_handle = (Handle)handle;
|
|
|
|
return res;
|
|
}
|
|
|
|
VOID Pipeline_UnlitMesh::Bind(IN Handle renderPassHandle)
|
|
{
|
|
SDL_BindGPUGraphicsPipeline((SDL_GPURenderPass*)renderPassHandle, (SDL_GPUGraphicsPipeline*)m_handle);
|
|
}
|
|
} // namespace ia::iae
|