diff --git a/res/engine/shaders/skybox.vert b/res/engine/shaders/skybox.vert index 210fad8..4a9b183 100644 --- a/res/engine/shaders/skybox.vert +++ b/res/engine/shaders/skybox.vert @@ -2,6 +2,7 @@ layout(binding = 0) uniform UBO { mat4 proj; + mat4 view; } ubo; layout( push_constant ) uniform Constants { diff --git a/res/engine/shaders/textured.vert b/res/engine/shaders/textured.vert index 86749ee..a5b5b2c 100644 --- a/res/engine/shaders/textured.vert +++ b/res/engine/shaders/textured.vert @@ -1,7 +1,8 @@ #version 450 -layout(binding = 0) uniform UBO { +layout(set = 0, binding = 0) uniform UBO { mat4 proj; + mat4 view; } ubo; layout( push_constant ) uniform Constants { diff --git a/src/gfx_device_vulkan.cpp b/src/gfx_device_vulkan.cpp index 2fa5ff1..0e480d9 100644 --- a/src/gfx_device_vulkan.cpp +++ b/src/gfx_device_vulkan.cpp @@ -503,7 +503,7 @@ namespace engine { } // This is called not just on initialisation, but also when the window is resized. - static void createSwapchain(VkDevice device, VkPhysicalDevice physicalDevice, VmaAllocator allocator, std::vector queues, SDL_Window* window, VkSurfaceKHR surface, bool vsync, Swapchain* swapchain) + static void createSwapchain(VkDevice device, VkPhysicalDevice physicalDevice, VmaAllocator allocator, std::vector queues, SDL_Window* window, VkSurfaceKHR surface, bool vsync, bool useMSAA, Swapchain* swapchain) { [[maybe_unused]] VkResult res; @@ -636,7 +636,10 @@ namespace engine { assert(res == VK_SUCCESS); // Use multisample anti-aliasing - swapchain->msaaSamples = getMaxSampleCount(physicalDevice); + if (useMSAA) + swapchain->msaaSamples = getMaxSampleCount(physicalDevice); + else + swapchain->msaaSamples = VK_SAMPLE_COUNT_1_BIT; // create depth buffer if old depth buffer is wrong size. // Also do the same for the MSAA buffer. @@ -663,6 +666,8 @@ namespace engine { * 0: color attachment with msaa samples, * 1: depth attachment with msaa samples, used for fragment shading * 2: present src (resolve) attachment with 1 sample, used for swapchain present + * + * if msaa is disabled, 0 is used for swapchain present and 2 is ignored */ VkAttachmentDescription colorAttachment{}; @@ -673,7 +678,11 @@ namespace engine { colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + if (useMSAA) { + colorAttachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; + } else { + colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; + } colorAttachmentRef.attachment = 0; colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; @@ -709,7 +718,11 @@ namespace engine { subpass.colorAttachmentCount = 1; subpass.pColorAttachments = &colorAttachmentRef; subpass.pDepthStencilAttachment = &depthAttachmentRef; - subpass.pResolveAttachments = &colorAttachmentResolveRef; + if (useMSAA) { + subpass.pResolveAttachments = &colorAttachmentResolveRef; + } else { + subpass.pResolveAttachments = nullptr; + } VkSubpassDependency dependency{}; dependency.srcSubpass = VK_SUBPASS_EXTERNAL; @@ -723,7 +736,11 @@ namespace engine { VkRenderPassCreateInfo createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - createInfo.attachmentCount = (uint32_t)attachments.size(); + if (useMSAA) { + createInfo.attachmentCount = 3; + } else { + createInfo.attachmentCount = 2; + } createInfo.pAttachments = attachments.data(); createInfo.subpassCount = 1; createInfo.pSubpasses = &subpass; @@ -766,7 +783,12 @@ namespace engine { VkFramebufferCreateInfo framebufferInfo{}; framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferInfo.renderPass = swapchain->renderpass; - framebufferInfo.attachmentCount = (uint32_t)attachments.size(); + if (useMSAA) { + framebufferInfo.attachmentCount = 3; + } else { + attachments[0] = swapchain->imageViews[i]; + framebufferInfo.attachmentCount = 2; + } framebufferInfo.pAttachments = attachments.data(); framebufferInfo.width = swapchain->extent.width; framebufferInfo.height = swapchain->extent.height; @@ -1431,7 +1453,7 @@ namespace engine { // Now make the swapchain - createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, window, pimpl->surface, pimpl->vsync, &pimpl->swapchain); + createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, window, pimpl->surface, pimpl->vsync, false, &pimpl->swapchain); @@ -1560,7 +1582,7 @@ namespace engine { if (res == VK_ERROR_OUT_OF_DATE_KHR) { // recreate swapchain waitIdle(); - createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, pimpl->vsync, &pimpl->swapchain); + createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, pimpl->vsync, false, &pimpl->swapchain); return; } else { @@ -1693,7 +1715,7 @@ namespace engine { if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR) { // recreate swapchain waitIdle(); - createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, pimpl->vsync, &pimpl->swapchain); + createSwapchain(pimpl->device, pimpl->physicalDevice, pimpl->allocator, pimpl->queues, pimpl->window, pimpl->surface, pimpl->vsync, false, &pimpl->swapchain); } else { assert(res == VK_SUCCESS); diff --git a/src/resources/shader.cpp b/src/resources/shader.cpp index a5f9c5e..4018b60 100644 --- a/src/resources/shader.cpp +++ b/src/resources/shader.cpp @@ -36,7 +36,7 @@ namespace engine::resources { } vertFormat.stride = stride; - m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4), alphaBlending, cullBackFace); + m_pipeline = m_gfx->createPipeline(vertPath, fragPath, vertFormat, sizeof(glm::mat4) * 2, alphaBlending, cullBackFace); INFO("Loaded shader: {}, vertex attribs: {}", vertPath, vertFormat.attributeDescriptions.size()); } diff --git a/src/systems/render.cpp b/src/systems/render.cpp index b08c6a7..a201703 100644 --- a/src/systems/render.cpp +++ b/src/systems/render.cpp @@ -58,7 +58,15 @@ namespace engine { assert(r->mesh != nullptr); assert(r->material->m_texture != nullptr); - gfx->updateUniformBuffer(r->material->getShader()->getPipeline(), &projMatrix, sizeof(projMatrix), 0); + struct { + glm::mat4 proj; + glm::mat4 view; + } uniform{}; + + uniform.proj = projMatrix; + uniform.view = viewMatrix; + + gfx->updateUniformBuffer(r->material->getShader()->getPipeline(), &uniform, sizeof(glm::mat4) * 2, 0); struct { glm::mat4 model; @@ -84,7 +92,7 @@ namespace engine { void RenderSystem::setCameraEntity(uint32_t entity) { - { m_camera.camEntity = entity; } + m_camera.camEntity = entity; } } diff --git a/src/window.cpp b/src/window.cpp index 1d4b1ca..8154a29 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -1,7 +1,5 @@ #include "window.hpp" -#include "log.hpp" - #include #include diff --git a/test/src/camera_controller.cpp b/test/src/camera_controller.cpp index 04af8bc..f74721e 100644 --- a/test/src/camera_controller.cpp +++ b/test/src/camera_controller.cpp @@ -50,8 +50,6 @@ void CameraControllerSystem::onUpdate(float ts) glm::vec3 dir = glm::normalize(glm::rotateY(glm::vec3{ 1.0f, 0.0f, 0.0f }, c->m_yaw) + glm::rotateY(glm::vec3{ 0.0f, 0.0f, 1.0f }, c->m_yaw)); const float slope = glm::dot(dir, norm); - INFO("slope: {}", slope); - bool isSliding = false; if (c->justCollided) {