Better pipeline...

This commit is contained in:
2026-02-22 15:43:21 +00:00
parent 8f4792aa2d
commit c7bed7d58e
7 changed files with 42 additions and 31 deletions

View File

@@ -15,7 +15,7 @@ namespace cve {
};
FirstApp::FirstApp() {
loadModels();
loadGameObjects();
createPipelineLayout();
recreateSwapChain();
createCommandBuffers();
@@ -34,14 +34,21 @@ namespace cve {
vkDeviceWaitIdle(cveDevice.device());
}
void FirstApp::loadModels() {
void FirstApp::loadGameObjects() {
std::vector<CveModel::Vertex> verticies {
{{0.0f, -0.5f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
{{-0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}}
};
cveModel = std::make_unique<CveModel>(cveDevice, verticies);
auto cveModel = std::make_shared<CveModel>(cveDevice, verticies);
auto triangle = CveGameObject::createGameObject();
triangle.model = cveModel;
triangle.color = {0.1f, 0.8f, 0.1f};
triangle.transform2d.translation.x = .2f;
gameObjects.push_back(std::move(triangle));
}
void FirstApp::createPipelineLayout() {
@@ -123,8 +130,6 @@ namespace cve {
}
void FirstApp::recordCommandBuffer(int imageIndex) {
static int frame = 30;
frame = (frame+1) % 2000;
VkCommandBufferBeginInfo beginInfo{};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
@@ -160,24 +165,7 @@ namespace cve {
vkCmdSetViewport(commandBuffers[imageIndex], 0, 1, &viewport);
vkCmdSetScissor(commandBuffers[imageIndex], 0, 1, &scissor);
cvePipeline->bind(commandBuffers[imageIndex]);
cveModel->bind(commandBuffers[imageIndex]);
for (int j = 0; j < 4; j++) {
SimplePushConstantData push{};
push.offset = {-0.5f + frame * 0.0005f, -0.4f + j * 0.25f};
push.color = {0.0f, 0.0f, 0.2f + 0.2f * j};
vkCmdPushConstants(
commandBuffers[imageIndex],
pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(SimplePushConstantData),
&push
);
cveModel->draw(commandBuffers[imageIndex]);
}
renderGameObjects(commandBuffers[imageIndex]);
@@ -187,6 +175,29 @@ namespace cve {
}
}
void FirstApp::renderGameObjects(VkCommandBuffer commandBuffer) {
cvePipeline->bind(commandBuffer);
for (auto& obj: gameObjects) {
SimplePushConstantData push{};
push.offset = obj.transform2d.translation;
push.color = obj.color;
push.transform = obj.transform2d.mat2();
vkCmdPushConstants(
commandBuffer,
pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(SimplePushConstantData),
&push
);
obj.model->bind(commandBuffer);
obj.model->draw(commandBuffer);
}
}
void FirstApp::drawFrame() {
uint32_t imageIndex;
auto result = cveSwapChain->acquireNextImage(&imageIndex);