This commit is contained in:
2026-09-06 23:40:27 -07:00
parent dc40e973d3
commit 9b1e7d56a3
7 changed files with 196 additions and 2 deletions
@@ -0,0 +1,88 @@
#include "model_render_system.hpp"
#include <cassert>
#include "shaders/model_render_system.frag.spv.h"
#include "shaders/model_render_system.vert.spv.h"
namespace sophia {
ModelRenderSystem::ModelRenderSystem(Device& device,
RenderContext& renderContext)
: device{device}, renderContext{renderContext} {
createPipelineLayout();
assert(this->pipelineLayout.get() != nullptr &&
"Cannot create pipeline before pipeline layout");
auto vertexShader = Shader::Builder()
.fromSPIRV(shaders::model_render_system_vert_spv)
.setStage(ShaderStage::VERTEX)
.build();
auto fragmentShader = Shader::Builder()
.fromSPIRV(shaders::model_render_system_frag_spv)
.setStage(ShaderStage::FRAGMENT)
.build();
auto bindingDescriptions = Model::Vertex::getBindingDescriptions();
auto attributeDescriptions = Model::Vertex::getAttributeDescriptions();
this->graphicsPipeline =
GraphicsPipeline::Builder(device)
.setVertexShader(*vertexShader)
.setFragmentShader(*fragmentShader)
.setPipelineLayout(pipelineLayout.get())
.setColorAttachmentFormats({renderContext.getImageFormat()})
.setVertexInput(bindingDescriptions, attributeDescriptions)
.build();
}
ModelRenderSystem::~ModelRenderSystem() {
log::trace("destroyed sophia::ModelRenderSystem");
}
void ModelRenderSystem::render(vk::CommandBuffer commandBuffer,
std::span<const std::unique_ptr<Model>> models,
const FrameInfo& frameInfo) {
glm::mat4 viewProjection = frameInfo.camera.getViewProjection();
this->graphicsPipeline->bind(commandBuffer);
for (const std::unique_ptr<Model>& model : models) {
PushConstantData push{};
push.modelMatrix = viewProjection;
commandBuffer.pushConstants(
this->pipelineLayout.get(),
vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment,
0, sizeof(PushConstantData), &push);
model->bind(commandBuffer);
model->draw(commandBuffer);
}
}
void ModelRenderSystem::createPipelineLayout() {
vk::PushConstantRange pushConstantRange{};
pushConstantRange.stageFlags =
vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(PushConstantData);
vk::PipelineLayoutCreateInfo pipelineLayoutInfo = {};
pipelineLayoutInfo.setLayoutCount = 0;
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
try {
this->pipelineLayout =
this->device.get()->createPipelineLayoutUnique(pipelineLayoutInfo);
log::trace("created vk::PipelineLayout");
} catch (const vk::SystemError& err) {
log::fatal("failed to create vk::PipelineLayout");
throw std::runtime_error("failed to create vk::PipelineLayout");
}
}
} // namespace sophia