fix redefination of descriptoSetLayout

ink-soul 2024-04-09 13:31:15 +08:00
parent 2f5fd63dd9
commit 7558e92ab3
12 changed files with 241 additions and 2223 deletions

View File

@ -12,7 +12,7 @@ set(KTX_SOURCES
add_library(base STATIC ${BASE_SRC} ${KTX_SOURCES})
if(WIN32)
target_link_libraries(base ${Vulkan_LIBRARY} ${WINLIBS})
target_link_libraries(base ${Vulkan_LIBRARY})
else(WIN32)
target_link_libraries(base ${Vulkan_LIBRARY} ${XCB_LIBRARIES} ${WAYLAND_CLIENT_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
endif(WIN32)

View File

@ -1,4 +1,4 @@

#pragma once
#include <vector>
#include <array>
#include <map>

File diff suppressed because it is too large Load Diff

View File

@ -1,230 +0,0 @@
/*
* Vulkan Example base class
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#pragma once
#ifdef _WIN32
#pragma comment(linker, "/subsystem:windows")
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
#include <android/native_activity.h>
#include <android/asset_manager.h>
#include <android_native_app_glue.h>
#include <sys/system_properties.h>
#include "VulkanAndroid.h"
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
#include <wayland-client.h>
#elif defined(_DIRECT2DISPLAY)
//
#elif defined(VK_USE_PLATFORM_XCB_KHR)
#include <xcb/xcb.h>
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
#include <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h>
#include <QuartzCore/CAMetalLayer.h>
#include <CoreVideo/CVDisplayLink.h>
#endif
#include <iostream>
#include <chrono>
#include <sys/stat.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <string>
#include <sstream>
#include <array>
#include <numeric>
#include "vulkan/vulkan.h"
#include "VulkanTools.h"
#include "camera.hpp"
#include "keycodes.hpp"
#include "VulkanDevice.hpp"
#include "VulkanSwapChain.hpp"
#include "imgui/imgui.h"
class VulkanExampleBase
{
private:
float fpsTimer = 0.0f;
uint32_t frameCounter = 0;
uint32_t destWidth;
uint32_t destHeight;
bool resizing = false;
void handleMouseMove(int32_t x, int32_t y);
PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallback;
PFN_vkDestroyDebugReportCallbackEXT vkDestroyDebugReportCallback;
VkDebugReportCallbackEXT debugReportCallback;
protected:
VkInstance instance;
VkPhysicalDevice physicalDevice;
VkPhysicalDeviceProperties deviceProperties;
VkPhysicalDeviceFeatures deviceFeatures;
VkPhysicalDeviceMemoryProperties deviceMemoryProperties;
VkDevice device;
vks::VulkanDevice *vulkanDevice;
VkQueue queue;
VkFormat depthFormat;
VkCommandPool cmdPool;
VkRenderPass renderPass;
std::vector<VkFramebuffer>frameBuffers;
uint32_t currentBuffer = 0;
VkDescriptorPool descriptorPool;
VkPipelineCache pipelineCache;
VulkanSwapChain swapChain;
std::string title = "Vulkan Example";
std::string name = "vulkanExample";
void windowResize();
public:
uint32_t selectedPhysicalDeviceIndex = 0;
bool prepared = false;
Camera camera;
bool paused = false;
struct DepthStencil {
VkImage image;
VkDeviceMemory mem;
VkImageView view;
} depthStencil;
// OS specific
#if defined(_WIN32)
HWND window;
HINSTANCE windowInstance;
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
// true if application has focused, false if moved to background
bool focused = false;
std::string androidProduct;
struct TouchPoint {
int32_t id;
float x;
float y;
bool down = false;
};
float pinchDist = 0.0f;
std::array<TouchPoint, 2> touchPoints;
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
wl_display *display = nullptr;
wl_registry *registry = nullptr;
wl_compositor *compositor = nullptr;
wl_shell *shell = nullptr;
wl_seat *seat = nullptr;
wl_pointer *pointer = nullptr;
wl_keyboard *keyboard = nullptr;
wl_surface *surface = nullptr;
wl_shell_surface *shell_surface = nullptr;
bool quit = false;
#elif defined(_DIRECT2DISPLAY)
bool quit = false;
#elif defined(VK_USE_PLATFORM_XCB_KHR)
bool quit = false;
xcb_connection_t *connection;
xcb_screen_t *screen;
xcb_window_t window;
xcb_intern_atom_reply_t *atom_wm_delete_window;
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
NSWindow* window;
#endif
#if defined(_WIN32)
HWND setupWindow(HINSTANCE hinstance, WNDPROC wndproc);
void handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
#elif defined(VK_USE_PLATFORM_ANDROID_KHR)
static int32_t handleAppInput(struct android_app* app, AInputEvent* event);
static void handleAppCommand(android_app* app, int32_t cmd);
#elif defined(VK_USE_PLATFORM_WAYLAND_KHR)
wl_shell_surface *setupWindow();
void initWaylandConnection();
static void registryGlobalCb(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version);
void registryGlobal(struct wl_registry *registry, uint32_t name,
const char *interface, uint32_t version);
static void registryGlobalRemoveCb(void *data, struct wl_registry *registry,
uint32_t name);
static void seatCapabilitiesCb(void *data, wl_seat *seat, uint32_t caps);
void seatCapabilities(wl_seat *seat, uint32_t caps);
static void pointerEnterCb(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface, wl_fixed_t sx,
wl_fixed_t sy);
static void pointerLeaveCb(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface);
static void pointerMotionCb(void *data, struct wl_pointer *pointer,
uint32_t time, wl_fixed_t sx, wl_fixed_t sy);
void pointerMotion(struct wl_pointer *pointer,
uint32_t time, wl_fixed_t sx, wl_fixed_t sy);
static void pointerButtonCb(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time, uint32_t button, uint32_t state);
void pointerButton(struct wl_pointer *wl_pointer,
uint32_t serial, uint32_t time, uint32_t button, uint32_t state);
static void pointerAxisCb(void *data, struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis, wl_fixed_t value);
void pointerAxis(struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis, wl_fixed_t value);
static void keyboardKeymapCb(void *data, struct wl_keyboard *keyboard,
uint32_t format, int fd, uint32_t size);
static void keyboardEnterCb(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface, struct wl_array *keys);
static void keyboardLeaveCb(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface);
static void keyboardKeyCb(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
void keyboardKey(struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key, uint32_t state);
static void keyboardModifiersCb(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched,
uint32_t mods_locked, uint32_t group);
#elif defined(_DIRECT2DISPLAY)
//
#elif defined(VK_USE_PLATFORM_XCB_KHR)
xcb_window_t setupWindow();
void initxcbConnection();
void handleEvent(const xcb_generic_event_t *event);
#elif defined(VK_USE_PLATFORM_MACOS_MVK)
NSWindow* setupWindow();
void mouseDragged(float x, float y);
void windowWillResize(float x, float y);
void windowDidResize();
#endif
VulkanExampleBase();
virtual ~VulkanExampleBase();
void initVulkan();
virtual VkResult createInstance(bool enableValidation);
virtual void render() = 0;
virtual void windowResized();
virtual void setupFrameBuffer();
virtual void prepare();
virtual void fileDropped(std::string filename);
void initSwapchain();
void setupSwapChain();
void renderLoop();
void renderFrame();
};

View File

@ -10,14 +10,8 @@ namespace PBR
{
public:
Material();
~Material()
{
textures.environmentCube.destroy();
textures.irradianceCube.destroy();
textures.prefilteredCube.destroy();
textures.lutBrdf.destroy();
textures.empty.destroy();
}
~Material();
struct PushConstBlockMaterial {
glm::vec4 baseColorFactor;
@ -103,5 +97,10 @@ namespace PBR
Material::~Material()
{
textures.environmentCube.destroy();
textures.irradianceCube.destroy();
textures.prefilteredCube.destroy();
textures.lutBrdf.destroy();
textures.empty.destroy();
}
}

View File

@ -105,26 +105,12 @@ void PlumageRender::renderMain::render()
renderGUI.updateUIOverlay();
}
//加入写到文件的函数
//swapChainImage = swapChain.images[frameIndex];
//outputImageSequeue(swapChainImage,filePath.imageSequenceFilePath);
uint32_t imageIndex;
VK_CHECK_RESULT(vkWaitForFences(VulkanBackend::VulkanFoundation::device, 1, &vkFoundation.waitFences[frameIndex], VK_TRUE, UINT64_MAX));
VkResult result = vkAcquireNextImageKHR(VulkanBackend::VulkanFoundation::device, VulkanBackend::VulkanFoundation::swapChain, UINT64_MAX, vkFoundation.renderCompleteSemaphores[frameIndex], VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebufferResized)
{
framebufferResized = false;
vkFoundation.recreateSwapChain();
return;
}
else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
{
throw std::runtime_error("failed to acquire swap chain image in drawFrame");
}
framebufferResized = vkFoundation.acqureNextSwapchainImage(framebufferResized, imageIndex, frameIndex);
VK_CHECK_RESULT(vkResetFences(VulkanBackend::VulkanFoundation::device, 1, &vkFoundation.waitFences[frameIndex]));
renderOutput.outputImageSequence();
@ -137,44 +123,9 @@ void PlumageRender::renderMain::render()
memcpy(currentUB.params.mapped, &PBR::Material::shaderData, sizeof(PBR::Material::shaderData));
memcpy(currentUB.skybox.mapped, &vkFoundation.shaderDataSkybox, sizeof(vkFoundation.shaderDataSkybox));
const VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.pWaitDstStageMask = &waitDstStageMask;
submitInfo.pWaitSemaphores = &vkFoundation.presentCompleteSemaphores[frameIndex];
submitInfo.waitSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &vkFoundation.renderCompleteSemaphores[frameIndex];
submitInfo.signalSemaphoreCount = 1;
submitInfo.pCommandBuffers = &vkFoundation.commandbuffers[currentBuffer];
submitInfo.commandBufferCount = 1;
VK_CHECK_RESULT(vkQueueSubmit(vkFoundation.graphicQueue, 1, &submitInfo, vkFoundation.waitFences[frameIndex]));
vkFoundation.submitToGraphicQueue(frameIndex, currentBuffer);
//显示队列
VkSemaphore signalSemaphores[] = { vkFoundation.renderCompleteSemaphores[frameIndex] };
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { vkFoundation.swapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = nullptr;
result = vkQueuePresentKHR(vkFoundation.presentQueue, &presentInfo);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
{
framebufferResized = false;
vkFoundation.recreateSwapChain();
}
else if (result != VK_SUCCESS)
{
throw std::runtime_error("failed to present swap chain image in drawFrame");
}
vkFoundation.imageToQueuePresent(frameIndex, imageIndex, framebufferResized);
frameIndex += 1;
frameIndex %= PlumageRender::Setter::settings.MaxFrameInFlight;
@ -206,15 +157,43 @@ void PlumageRender::renderMain::render()
void PlumageRender::renderMain::renderLoop(GLFWwindow* window)
{
while (!glfwWindowShouldClose(window))
if (PlumageRender::Setter::settings.headless)
{
glfwPollEvents();
render();
uint32_t frameRange = PlumageRender::Setter::settings.endFrameIndex - PlumageRender::Setter::settings.startFrameCount;
for (size_t i = 0; i < frameRange; i++)
{
drawFrame();
}
}
else
{
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
drawFrame();
}
}
vkDeviceWaitIdle(VulkanBackend::VulkanFoundation::device);
}
void PlumageRender::renderMain::drawFrame()
{
auto tStart = std::chrono::high_resolution_clock::now();
render();
frameCounter++;
auto tEnd = std::chrono::high_resolution_clock::now();
auto tDiff = std::chrono::duration<double, std::milli>(tEnd - tStart).count();
frameTimer = (float)tDiff / 1000.0f;
camera.update(frameTimer);
fpsTimer += (float)tDiff;
if (fpsTimer > 1000.0f) {
lastFPS = static_cast<uint32_t>((float)frameCounter * (1000.0f / fpsTimer));
fpsTimer = 0.0f;
frameCounter = 0;
}

View File

@ -17,7 +17,7 @@
#include <filesystem>
#include <vulkan/vulkan.h>
#include "VulkanExampleBase.h"
#include "glTFModel.h"
#include "GLFW/glfw3.h"
#include <VulkanTexture.hpp>
@ -26,7 +26,7 @@
#include <VulkanUtils.hpp>
#include "renderSetter.h"
#include "vulkanFoundation.h"
#include "camera.hpp"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
@ -43,8 +43,7 @@ namespace PlumageRender
GLFWwindow* window;
static Camera camera;
struct Models
{
glTFModel::Model scene;
@ -62,6 +61,7 @@ namespace PlumageRender
glm::vec3 modelPos = glm::vec3(0.0f);
uint32_t frameIndex = 0;
uint32_t frameCounter = 0;
//VkImage swapChainImage;
@ -75,20 +75,15 @@ namespace PlumageRender
uint32_t lastFPS = 0;
static const float frameTimer = 1.0f;
float frameTimer = 1.0f;
float fpsTimer = 0.0f;
static int32_t animationIndex;
float animationTimer = 0.0f;
renderMain();
~renderMain()
{
models.scene.destroy(VulkanBackend::VulkanFoundation::device);
models.skybox.destroy(VulkanBackend::VulkanFoundation::device);
delete gui;
}
renderMain();
~renderMain();
void initWindow(int width, int height);
static void framebufferResizeCallback(GLFWwindow* window, int width, int height);
@ -100,6 +95,7 @@ namespace PlumageRender
virtual void render();
void renderLoop(GLFWwindow* window);
void drawFrame();
private:
PlumageRender::RenderInput renderInput;
@ -117,7 +113,21 @@ namespace PlumageRender
bool prepared = false;
};
renderMain::renderMain()
{
}
renderMain::~renderMain()
{
models.scene.destroy(VulkanBackend::VulkanFoundation::device);
models.skybox.destroy(VulkanBackend::VulkanFoundation::device);
delete gui;
}
}

View File

@ -83,6 +83,10 @@ void PlumageRender::RenderOutput::writeImageToFile(std::string filePath)
}
// Source for the copy is the last rendered swapchain image
if (PlumageRender::Setter::settings.headless)
{
VkImage srcImage = VulkanBackend::VulkanFoundation::colorAttachment.image;
}
VkImage srcImage = VulkanBackend::VulkanFoundation::swapChainImages[PlumageRender::renderMain::currentBuffer];
// Create the linear tiled destination image to copy to and to read the memory from
@ -128,18 +132,22 @@ void PlumageRender::RenderOutput::writeImageToFile(std::string filePath)
VK_PIPELINE_STAGE_TRANSFER_BIT,
VkImageSubresourceRange{ VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 });
// Transition swapchain image from present to transfer source layout
vks::tools::insertImageMemoryBarrier(
copyCmd,
srcImage,
VK_ACCESS_MEMORY_READ_BIT,
VK_ACCESS_TRANSFER_READ_BIT,
//VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VkImageSubresourceRange{ VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 });
if (!PlumageRender::Setter::settings.headless)
{
// Transition swapchain image from present to transfer source layout
vks::tools::insertImageMemoryBarrier(
copyCmd,
srcImage,
VK_ACCESS_MEMORY_READ_BIT,
VK_ACCESS_TRANSFER_READ_BIT,
//VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT,
VkImageSubresourceRange{ VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 });
}
// If source and destination support blit we'll blit as this also does automatic format conversion (e.g. from BGR to RGB)
if (supportsBlit)

View File

@ -178,12 +178,12 @@ void PlumageRender::PlumageGUI::updateUIOverlay()
if (gui->vertexBuffer.buffer) {
gui->vertexBuffer.destroy();
}
gui->vertexBuffer.create(PlumageRender::renderMain::vkFoundation.vulkanDevice, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, vertexBufferSize);
gui->vertexBuffer.create(VulkanBackend::VulkanFoundation::vulkanDevice, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, vertexBufferSize);
gui->vertexBuffer.count = imDrawData->TotalVtxCount;
if (gui->indexBuffer.buffer) {
gui->indexBuffer.destroy();
}
gui->indexBuffer.create(PlumageRender::renderMain::vkFoundation.vulkanDevice, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, indexBufferSize);
gui->indexBuffer.create(VulkanBackend::VulkanFoundation::vulkanDevice, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, indexBufferSize);
gui->indexBuffer.count = imDrawData->TotalIdxCount;
}
@ -209,12 +209,12 @@ void PlumageRender::PlumageGUI::updateUIOverlay()
}
if (updateCBs) {
vkDeviceWaitIdle(PlumageRender::renderMain::vkFoundation.device);
PlumageRender::renderMain::vkFoundation.createCommandBuffer();
vkDeviceWaitIdle(PlumageRender::renderMain::vkFoundation.device);
vkDeviceWaitIdle(VulkanBackend::VulkanFoundation::device);
vkFoundation.createCommandBuffer();
vkDeviceWaitIdle(VulkanBackend::VulkanFoundation::device);
}
if (updateShaderParams) {
PlumageRender::renderMain::vkFoundation.createCommandBuffer();
vkFoundation.createCommandBuffer();
}
}

View File

@ -3,7 +3,7 @@
#include "ui.hpp"
#include "VulkanDevice.hpp"
#include "vulkanFoundation.h"
#include "renderSetter.h"
#include "renderIO.h"
#include "render.h"
@ -26,6 +26,7 @@ namespace PlumageRender
};
static DebugView debugView;
private:

View File

@ -1047,6 +1047,13 @@ bool VulkanBackend::VulkanFoundation::hasStencilComponent(VkFormat format)
void VulkanBackend::VulkanFoundation::createDescriptorSetLayout()
{
createSceneDescriptorSetLayout();
createMaterialDescriptorSetLayout();
createNodeDescriptorSetLayout();
}
void VulkanBackend::VulkanFoundation::createSceneDescriptorSetLayout()
{
// scene场景的资源描述符
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
@ -1061,7 +1068,10 @@ void VulkanBackend::VulkanFoundation::createDescriptorSetLayout()
descriptorSetLayoutCI.pBindings = setLayoutBindings.data();
descriptorSetLayoutCI.bindingCount = static_cast<uint32_t>(setLayoutBindings.size());
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.scene));
}
void VulkanBackend::VulkanFoundation::createMaterialDescriptorSetLayout()
{
// 材质或材质采样器的资源描述符
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
{ 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr },
@ -1075,7 +1085,10 @@ void VulkanBackend::VulkanFoundation::createDescriptorSetLayout()
descriptorSetLayoutCI.pBindings = setLayoutBindings.data();
descriptorSetLayoutCI.bindingCount = static_cast<uint32_t>(setLayoutBindings.size());
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.material));
}
void VulkanBackend::VulkanFoundation::createNodeDescriptorSetLayout()
{
// 模型结点或矩阵的资源描述符
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
{ 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT, nullptr },
@ -1775,7 +1788,7 @@ void VulkanBackend::VulkanFoundation::createCommandBuffer()
// User interface
if (!PlumageRender::Setter::settings.headless)
{
gui->draw(currentCB);
plumageGui.gui->draw(currentCB);
}
vkCmdEndRenderPass(currentCB);
@ -1889,6 +1902,80 @@ void VulkanBackend::VulkanFoundation::createFenceAndSemaphore()
}
}
void VulkanBackend::VulkanFoundation::DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator)
{
auto func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT");
if (func != nullptr)
{
func(instance, debugMessenger, pAllocator);
}
}
bool VulkanBackend::VulkanFoundation::acqureNextSwapchainImage(bool framebuffeerResized,uint32_t imageIndex,uint32_t frameIndex)
{
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX, renderCompleteSemaphores[frameIndex], VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || framebuffeerResized)
{
framebuffeerResized = false;
recreateSwapChain();
return framebuffeerResized;
}
else if (result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
{
throw std::runtime_error("failed to acquire swap chain image in drawFrame");
}
}
void VulkanBackend::VulkanFoundation::submitToGraphicQueue(uint32_t frameIndex, uint32_t currentBuffer)
{
const VkPipelineStageFlags waitDstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
VkSubmitInfo submitInfo{};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.pWaitDstStageMask = &waitDstStageMask;
submitInfo.pWaitSemaphores = &presentCompleteSemaphores[frameIndex];
submitInfo.waitSemaphoreCount = 1;
submitInfo.pSignalSemaphores = &renderCompleteSemaphores[frameIndex];
submitInfo.signalSemaphoreCount = 1;
submitInfo.pCommandBuffers = &commandbuffers[currentBuffer];
submitInfo.commandBufferCount = 1;
VK_CHECK_RESULT(vkQueueSubmit(graphicQueue, 1, &submitInfo, waitFences[frameIndex]));
}
void VulkanBackend::VulkanFoundation::imageToQueuePresent(uint32_t frameIndex,uint32_t imageIndex,bool framebufferResized)
{
//显示队列
VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[frameIndex] };
VkPresentInfoKHR presentInfo{};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { swapChain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
presentInfo.pResults = nullptr;
VkResult result = vkQueuePresentKHR(presentQueue, &presentInfo);
if (result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR)
{
framebufferResized = false;
recreateSwapChain();
}
else if (result != VK_SUCCESS)
{
throw std::runtime_error("failed to present swap chain image in drawFrame");
}
}
void VulkanBackend::VulkanFoundation::updateShaderData()
{
PBR::Material::shaderData.lightDir = glm::vec4(

View File

@ -13,8 +13,9 @@
#include "VulkanUtils.hpp"
#include "PBR.h"
#include "VulkanDevice.hpp"
#include <camera.hpp>
#include "renderUI.h"
#include <camera.hpp>
namespace VulkanBackend
{
@ -24,12 +25,37 @@ namespace VulkanBackend
VulkanFoundation();
~VulkanFoundation()
{
// Clean up Vulkan resources
cleanupSwapChain();
vkDestroyDescriptorPool(device, descriptorPool, nullptr);
vkDestroyRenderPass(device, renderPass, nullptr);
for (uint32_t i = 0; i < framebuffers.size(); i++) {
vkDestroyFramebuffer(device, framebuffers[i], nullptr);
}
vkDestroyImageView(device, colorAttachment.view, nullptr);
vkDestroyImage(device, colorAttachment.image, nullptr);
vkFreeMemory(device, colorAttachment.memory, nullptr);
vkDestroyImageView(device, depthAttachment.view, nullptr);
vkDestroyImage(device, depthAttachment.image, nullptr);
vkFreeMemory(device, depthAttachment.memory, nullptr);
if (PlumageRender::Setter::settings.multiSampling) {
vkDestroyImage(device, multisampleTarget.colorAttachment.image, nullptr);
vkDestroyImageView(device, multisampleTarget.colorAttachment.view, nullptr);
vkFreeMemory(device, multisampleTarget.colorAttachment.memory, nullptr);
vkDestroyImage(device, multisampleTarget.depthAttachment.image, nullptr);
vkDestroyImageView(device, multisampleTarget.depthAttachment.view, nullptr);
vkFreeMemory(device, multisampleTarget.depthAttachment.memory, nullptr);
}
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
vkDestroyPipeline(device, pipelines.skybox, nullptr);
vkDestroyPipeline(device, pipelines.pbr, nullptr);
vkDestroyPipeline(device, pipelines.pbrAlphaBlend, nullptr);
vkDestroyPipelineCache(device, pipelineCache, nullptr);
vkDestroyCommandPool(device, commandPool, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.scene, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.material, nullptr);
@ -51,6 +77,11 @@ namespace VulkanBackend
for (auto semaphore : presentCompleteSemaphores) {
vkDestroySemaphore(device, semaphore, nullptr);
}
delete vulkanDevice;
if (PlumageRender::Setter::settings.validation) {
DestroyDebugUtilsMessengerEXT(instance, debugMessenger, nullptr);
}
vkDestroyInstance(instance, nullptr);
}
const std::vector<const char*> validationLayers = {
@ -72,7 +103,14 @@ namespace VulkanBackend
static VkQueue graphicQueue;
struct FrameBufferAttachment
{
VkImage image;
VkDeviceMemory memory;
VkImageView view;
}depthAttachment;
static FrameBufferAttachment colorAttachment;
static VkPipelineCache pipelineCache;
@ -119,10 +157,16 @@ namespace VulkanBackend
void updateShaderData();
void recreateSwapChain();
bool acqureNextSwapchainImage(bool framebuffeerResized, uint32_t imageIndex, uint32_t frameIndex);
void submitToGraphicQueue(uint32_t frameIndex, uint32_t currentBuffer);
void imageToQueuePresent(uint32_t frameIndex, uint32_t imageIndex, bool framebufferResized);
private:
PlumageRender::PlumageGUI plumageGui;
VkInstance instance;
@ -178,12 +222,7 @@ namespace VulkanBackend
struct FrameBufferAttachment
{
VkImage image;
VkDeviceMemory memory;
VkImageView view;
}colorAttachment,depthAttachment;
struct MultisampleTarget {
FrameBufferAttachment colorAttachment;
@ -255,6 +294,7 @@ namespace VulkanBackend
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData);
VkResult CreateDebugUtilsMessengerEXT(VkInstance instance, const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDebugUtilsMessengerEXT* pDebugMessenger);
void DestroyDebugUtilsMessengerEXT(VkInstance instance, VkDebugUtilsMessengerEXT debugMessenger, const VkAllocationCallbacks* pAllocator);
// 通过检查队列、交换链、扩展支持,选择合适的显卡
void pickPhysicalDevice();
@ -284,6 +324,9 @@ namespace VulkanBackend
// 创建资源描述符层级,用于访问绑定的资源
void createDescriptorSetLayout();
void createSceneDescriptorSetLayout();
void createMaterialDescriptorSetLayout();
void createNodeDescriptorSetLayout();
// 创建管线缓存
void createPipelineCache();
@ -324,6 +367,7 @@ namespace VulkanBackend
void createFenceAndSemaphore();
};