Total mess. The instructions significantly fall apart at this point and I'll have to go reverse engineer the working version from the course's git repo.

This commit is contained in:
Zed A. Shaw 2026-02-06 11:55:32 -05:00
parent 7ae6a63295
commit b3cdf37045
8 changed files with 185 additions and 196 deletions

View file

@ -1,4 +1,6 @@
#include "vk_engine.h"
#define GLM_ENABLE_EXPERIMENTAL 1
#include <glm/gtx/transform.hpp>
#include "vk_engine.h"
#include "vk_images.h"
#include "vk_pipelines.h"
#include <print>
@ -58,6 +60,11 @@ void VulkanEngine::cleanup()
_frames[i]._deletionQueue.flush();
}
for(auto& mesh : testMeshes) {
destroy_buffer(mesh->meshBuffers.indexBuffer);
destroy_buffer(mesh->meshBuffers.vertexBuffer);
}
_mainDeletionQueue.flush();
destroy_swapchain();
@ -145,14 +152,21 @@ void VulkanEngine::draw()
draw_background(t.cmd);
vkutil::transition_image(t.cmd, _drawImage.image, VK_IMAGE_LAYOUT_GENERAL, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
vkutil::transition_image(t.cmd, _drawImage.image,
VK_IMAGE_LAYOUT_GENERAL,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
vkutil::transition_image(t.cmd, _depthImage.image,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL);
// ZED: why does removing this make the monkey work?
vkutil::transition_image(t.cmd, _drawImage.image,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
draw_geometry(t.cmd);
// ZED: ?
vkutil::transition_image(t.cmd, _drawImage.image, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
// ZED: ?
vkutil::transition_image(t.cmd, _swapchainImages[t.swapchainImageIndex],
VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
@ -173,14 +187,17 @@ void VulkanEngine::draw()
commit_transaction(t);
}
void VulkanEngine::draw_geometry(VkCommandBuffer cmd)
{
VkRenderingAttachmentInfo colorAttachment = vkinit::attachment_info(_drawImage.imageView, nullptr, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
auto colorAttachment = vkinit::attachment_info(_drawImage.imageView, nullptr, VK_IMAGE_LAYOUT_GENERAL);
auto depthAttachment = vkinit::depth_attachment_info(_depthImage.imageView, VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL);
VkRenderingInfo renderInfo = vkinit::rendering_info(_drawExtent, &colorAttachment, nullptr);
// ZED: this changed from _drawExtent to _windowExtent. Why?
auto renderInfo = vkinit::rendering_info(_windowExtent, &colorAttachment, &depthAttachment);
vkCmdBeginRendering(cmd, &renderInfo);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, _trianglePipeline);
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, _meshPipeline);
VkViewport viewport{
.x = 0,
@ -200,28 +217,25 @@ void VulkanEngine::draw_geometry(VkCommandBuffer cmd)
vkCmdSetScissor(cmd, 0, 1, &scissor);
vkCmdDraw(cmd, 3, 1, 0, 0);
// draw rectangle
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, _meshPipeline);
GPUDrawPushConstants push_constants{
.worldMatrix = glm::mat4{ 1.0f },
.vertexBuffer = rectangle.vertexBufferAddress,
};
GPUDrawPushConstants push_constants{};
vkCmdPushConstants(cmd, _meshPipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(GPUDrawPushConstants),
&push_constants);
vkCmdBindIndexBuffer(cmd, rectangle.indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(cmd, 6, 1, 0, 0, 0);
// draw monkey
size_t model_idx = 2; // 0 cube; 1 sphere; 2 monkey
push_constants.vertexBuffer = testMeshes[model_idx]->meshBuffers.vertexBufferAddress;
glm::mat4 view = glm::translate(glm::vec3{0, 0, -5});
// camera projection
glm::mat4 projection = glm::perspective(glm::radians(70.0f), (float)_drawExtent.width / (float)_drawExtent.height, 10000.0f, 0.1f);
// invert Y direction on projection matrix so that we are smaller to opengl and gltf axis
projection[1][1] *= -1;
push_constants.worldMatrix = projection * view;
vkCmdPushConstants(cmd,
_meshPipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT,
@ -392,10 +406,30 @@ void VulkanEngine::init_swapchain() {
VK_CHECK(vkCreateImageView(_device, &rview_info, nullptr, &_drawImage.imageView));
// add depth image
_depthImage.imageFormat = VK_FORMAT_D32_SFLOAT;
_depthImage.imageExtent = drawImageExtent;
VkImageUsageFlags depthImageUsages{};
depthImageUsages |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
VkImageCreateInfo dimg_info = vkinit::image_create_info(_depthImage.imageFormat, depthImageUsages, drawImageExtent);
vmaCreateImage(_allocator, &dimg_info, &rimg_allocinfo, &_depthImage.image, &_depthImage.allocation, nullptr);
VkImageViewCreateInfo dview_info = vkinit::imageview_create_info(_depthImage.imageFormat, _depthImage.image, VK_IMAGE_ASPECT_DEPTH_BIT);
VK_CHECK(vkCreateImageView(_device, &dview_info, nullptr, &_depthImage.imageView));
//add to deletion queues
_mainDeletionQueue.push_function([=, this]() {
vkDestroyImageView(_device, _drawImage.imageView, nullptr);
vmaDestroyImage(_allocator, _drawImage.image, _drawImage.allocation);
vkDestroyImageView(_device, _depthImage.imageView, nullptr);
vmaDestroyImage(_allocator, _depthImage.image, _depthImage.allocation);
});
}
@ -503,7 +537,6 @@ void VulkanEngine::init_descriptors() {
void VulkanEngine::init_pipelines()
{
init_background_pipelines();
init_triangle_pipelines();
init_mesh_pipeline();
init_shaders();
}
@ -646,9 +679,11 @@ void VulkanEngine::init_mesh_pipeline()
pipelineBuilder.set_cull_mode(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
pipelineBuilder.set_multisampling_none();
pipelineBuilder.disable_blending();
pipelineBuilder.disable_depthtest();
pipelineBuilder.enable_depthtest(true, VK_COMPARE_OP_GREATER_OR_EQUAL);
pipelineBuilder.set_color_attachment_format(_drawImage.imageFormat);
pipelineBuilder.set_depth_format(VK_FORMAT_UNDEFINED);
pipelineBuilder.set_depth_format(_depthImage.imageFormat);
_meshPipeline = pipelineBuilder.build_pipeline(_device);
vkDestroyShaderModule(_device, triangleFragShader, nullptr);
@ -661,53 +696,6 @@ void VulkanEngine::init_mesh_pipeline()
}
void VulkanEngine::init_triangle_pipelines()
{
VkShaderModule triangleFragShader;
if(!vkutil::load_shader_module("colored_triangle.frag.spv",_device, &triangleFragShader)) {
std::print("Error when building the triangle fragment shaders module");
} else {
std::print("Triangle fragment shader successfully loaded");
}
VkShaderModule triangleVertexShader;
if(!vkutil::load_shader_module("colored_triangle.vert.spv", _device, &triangleVertexShader)) {
std::println("Error when building the triangle, vertex shader module");
} else {
std::println("Triangle vertex shader successfully loaded");
}
VkPipelineLayoutCreateInfo pipeline_layout_info = vkinit::pipeline_layout_create_info();
VK_CHECK(vkCreatePipelineLayout(_device, &pipeline_layout_info, nullptr, &_trianglePipelineLayout));
PipelineBuilder pipelineBuilder;
pipelineBuilder._pipelineLayout = _trianglePipelineLayout;
pipelineBuilder.set_shaders(triangleVertexShader, triangleFragShader);
pipelineBuilder.set_input_topology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
pipelineBuilder.set_polygon_mode(VK_POLYGON_MODE_FILL);
pipelineBuilder.set_cull_mode(VK_CULL_MODE_NONE, VK_FRONT_FACE_CLOCKWISE);
pipelineBuilder.set_multisampling_none();
pipelineBuilder.disable_blending();
pipelineBuilder.disable_depthtest();
// connect the image format we will drw into, from draw image
pipelineBuilder.set_color_attachment_format(_drawImage.imageFormat);
pipelineBuilder.set_depth_format(VK_FORMAT_UNDEFINED);
_trianglePipeline = pipelineBuilder.build_pipeline(_device);
vkDestroyShaderModule(_device, triangleFragShader, nullptr);
vkDestroyShaderModule(_device, triangleVertexShader, nullptr);
_mainDeletionQueue.push_function([&]() {
vkDestroyPipelineLayout(_device, _trianglePipelineLayout, nullptr);
vkDestroyPipeline(_device, _trianglePipeline, nullptr);
});
}
AllocatedBuffer VulkanEngine::create_buffer(size_t allocSize, VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage)
{
@ -796,29 +784,8 @@ GPUMeshBuffers VulkanEngine::uploadMesh(std::span<uint32_t> indices, std::span<V
}
void VulkanEngine::init_default_data() {
std::array<Vertex,4> rect_vertices;
rect_vertices[0].position = {0.5,-0.5,0};
rect_vertices[1].position = {0.5,0.5,0};
rect_vertices[2].position = {-0.5,-0.5,0};
rect_vertices[3].position = {-0.5,0.5,0};
rect_vertices[0].color = { 0, 0, 0, 1 };
rect_vertices[1].color = { 0.5,0.5,0.5,1 };
rect_vertices[2].color = { 1,0, 0,1 };
rect_vertices[3].color = { 0,1, 0,1 };
std::array<uint32_t,6> rect_indices{0, 1, 2, 2, 1, 3};
rectangle = uploadMesh(rect_indices, rect_vertices);
auto basicmesh = loadGltfMeshes(this, "./basicmesh.glb");
assert(basicmesh != std::nullopt && "Failed to load basicmesh.glb");
testMeshes = *basicmesh;
_mainDeletionQueue.push_function([&](){
destroy_buffer(rectangle.indexBuffer);
destroy_buffer(rectangle.vertexBuffer);
});
}