diff --git a/renderer/CMakeLists.txt b/renderer/CMakeLists.txt index 7a983be..1b768e3 100644 --- a/renderer/CMakeLists.txt +++ b/renderer/CMakeLists.txt @@ -1,5 +1,13 @@ find_package(Vulkan REQUIRED) +FetchContent_Declare( + glm + GIT_REPOSITORY https://github.com/g-truc/glm.git + GIT_TAG 0.9.9.8 +) + +FetchContent_MakeAvailable(glm) + add_library(renderer src/renderer.cpp) target_include_directories(renderer PUBLIC include) -target_link_libraries(renderer PUBLIC Vulkan::Vulkan fmt::fmt libxiv) \ No newline at end of file +target_link_libraries(renderer PUBLIC Vulkan::Vulkan fmt::fmt libxiv glm::glm) \ No newline at end of file diff --git a/renderer/shaders/mesh.vert b/renderer/shaders/mesh.vert index b22c1b2..f981623 100644 --- a/renderer/shaders/mesh.vert +++ b/renderer/shaders/mesh.vert @@ -2,6 +2,10 @@ layout(location = 0) in vec3 inPosition; +layout( push_constant ) uniform PushConstant { + mat4 mvp; +}; + void main() { - gl_Position = vec4(inPosition, 1.0); + gl_Position = mvp * vec4(inPosition, 1.0); } diff --git a/renderer/shaders/mesh.vert.spv b/renderer/shaders/mesh.vert.spv index b9cc072..16ad614 100644 Binary files a/renderer/shaders/mesh.vert.spv and b/renderer/shaders/mesh.vert.spv differ diff --git a/renderer/src/renderer.cpp b/renderer/src/renderer.cpp index f915f5c..6601836 100644 --- a/renderer/src/renderer.cpp +++ b/renderer/src/renderer.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include Renderer::Renderer() { VkApplicationInfo applicationInfo = {}; @@ -383,6 +385,12 @@ void Renderer::render(std::vector models) { vkCmdBindVertexBuffers(commandBuffer, 0, 1, &model.vertexBuffer, offsets); vkCmdBindIndexBuffer(commandBuffer, model.indexBuffer, 0, VK_INDEX_TYPE_UINT16); + glm::mat4 p = glm::perspective(glm::radians(90.0f), 1.0f, 0.1f, 100.0f); + glm::mat4 v = glm::lookAt(glm::vec3(2), glm::vec3(0), glm::vec3(0, 1, 0)); + glm::mat4 mvp = p * v; + + vkCmdPushConstants(commandBuffer, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::mat4), &mvp); + vkCmdDrawIndexed(commandBuffer, model.numIndices, 1, 0, 0, 0); } @@ -562,13 +570,12 @@ void Renderer::initPipeline() { inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; VkViewport viewport = {}; - viewport.width = 640; - viewport.height = 480; + viewport.width = swapchainExtent.width; + viewport.height = swapchainExtent.height; viewport.maxDepth = 1.0f; VkRect2D scissor = {}; - scissor.extent.width = 640; - scissor.extent.height = 489; + scissor.extent = swapchainExtent; VkPipelineViewportStateCreateInfo viewportState = {}; viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; @@ -595,15 +602,17 @@ void Renderer::initPipeline() { colorBlending.attachmentCount = 1; colorBlending.pAttachments = &colorBlendAttachment; - std::array dynamicStates = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR}; - VkPipelineDynamicStateCreateInfo dynamicState = {}; dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; - dynamicState.dynamicStateCount = static_cast(dynamicStates.size()); - dynamicState.pDynamicStates = dynamicStates.data(); + + VkPushConstantRange pushConstantRange = {}; + pushConstantRange.size = sizeof(glm::mat4); + pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT; VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + pipelineLayoutInfo.pushConstantRangeCount = 1; + pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange; vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout);