From 4a5cd74dc337a323f536fdb5cc606a64f8634bdb Mon Sep 17 00:00:00 2001 From: ink-soul Date: Fri, 26 May 2023 15:20:44 +0800 Subject: [PATCH] reconstructed header file --- CMakeLists.txt | 2 +- {homework => src}/CMakeLists.txt | 2 +- .../render/render.h => src/render/glTFModel.h | 190 +--------------- {homework => src}/render/render.cpp | 3 +- src/render/render.h | 206 ++++++++++++++++++ 5 files changed, 216 insertions(+), 187 deletions(-) rename {homework => src}/CMakeLists.txt (97%) rename homework/render/render.h => src/render/glTFModel.h (56%) rename {homework => src}/render/render.cpp (99%) create mode 100644 src/render/render.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 751a347..248c860 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -148,6 +148,6 @@ ENDIF(WIN32) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/") add_subdirectory(base) -add_subdirectory(homework) +add_subdirectory(src) # add_subdirectory(examples) diff --git a/homework/CMakeLists.txt b/src/CMakeLists.txt similarity index 97% rename from homework/CMakeLists.txt rename to src/CMakeLists.txt index 6934113..a51ef90 100644 --- a/homework/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -30,7 +30,7 @@ function(buildHomework HOMEWORK_NAME) # Add optional readme / tutorial file(GLOB README_FILES "${HOMEWORK_FOLDER}/*.md") if(WIN32) - add_executable(${HOMEWORK_NAME} WIN32 ${MAIN_CPP} ${SOURCE} ${MAIN_HEADER} ${SHADERS_GLSL} ${SHADERS_HLSL} ${README_FILES}) + add_executable(${HOMEWORK_NAME} WIN32 ${MAIN_CPP} ${SOURCE} ${MAIN_HEADER} ${SHADERS_GLSL} ${SHADERS_HLSL} ${README_FILES} "render/glTFModel.h") target_link_libraries(${HOMEWORK_NAME} base ${Vulkan_LIBRARY} ${WINLIBS}) else(WIN32) add_executable(${HOMEWORK_NAME} ${MAIN_CPP} ${SOURCE} ${MAIN_HEADER} ${SHADERS_GLSL} ${SHADERS_HLSL} ${README_FILES}) diff --git a/homework/render/render.h b/src/render/glTFModel.h similarity index 56% rename from homework/render/render.h rename to src/render/glTFModel.h index 1385b9c..bfc5ca7 100644 --- a/homework/render/render.h +++ b/src/render/glTFModel.h @@ -1,4 +1,4 @@ - +#pragma once #include #include #include @@ -16,7 +16,7 @@ #define TINYGLTF_NO_STB_IMAGE_WRITE #ifdef VK_USE_PLATFORM_ANDROID_KHR - #define TINYGLTF_ANDROID_LOAD_FROM_ASSETS +#define TINYGLTF_ANDROID_LOAD_FROM_ASSETS #endif #include "tiny_gltf.h" @@ -91,14 +91,14 @@ public: } glm::mat4 matrix; bool bAnimateNode = false; - + ~Node() { for (auto& child : children) { delete child; }; } - - + + }; // material data for pbr @@ -145,7 +145,7 @@ public: std::vector joints; vks::Buffer ssbo; VkDescriptorSet descriptorSet; - + }; struct AnimationSampler @@ -189,7 +189,7 @@ public: - //VulkanglTFModel(); + //VulkanglTFModel(); ~VulkanglTFModel() { for (auto node : nodes) { @@ -225,180 +225,4 @@ public: void updateAnimation(float deltaTime, vks::Buffer& buffer); void drawNode(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, VulkanglTFModel::Node* node, bool bPushConstants); void draw(VkCommandBuffer commandBuffer, VkPipelineLayout pipelineLayout, bool flag); -}; - -class VulkanExample : public VulkanExampleBase -{ -public: - bool wireframe = false; - bool normalMapping = true; - bool ToneMapping = true; - bool pbrEnabled = true; - - VulkanglTFModel glTFModel; - - struct ShaderData { - vks::Buffer buffer; - struct Values { - glm::mat4 projection; - glm::mat4 model; - glm::vec4 lightPos = glm::vec4(5.0f, 5.0f, 5.0f, 1.0f); - glm::vec4 viewPos; - glm::vec4 bFlagSet = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f); - } values; - vks::Buffer skinSSBO; - } shaderData; - - struct StagingBuffer { - VkBuffer buffer; - VkDeviceMemory memory; - } vertexStaging, indexStaging; - - struct Pipelines { - VkPipeline solid; - VkPipeline wireframe = VK_NULL_HANDLE; - VkPipeline toneMapping = VK_NULL_HANDLE; - } pipelines; - - struct PipelineLayouts - { - VkPipelineLayout pbrLayout; - VkPipelineLayout tonemappingLayout; - } pipelineLayouts; - - VkPipelineLayout pipelineLayout; - - VkDescriptorSet descriptorSet; - VkDescriptorSet skinDescriptorSet; - VkDescriptorSet tonemappingDescriptorSet = VK_NULL_HANDLE; - - struct FrameBufferAttachment - { - VkImage image; - VkDeviceMemory deviceMemory; - VkImageView imageView; - VkFormat format; - - - void destroy(VkDevice device) - { - vkDestroyImage(device, image, nullptr); - vkDestroyImageView(device, imageView,nullptr); - vkFreeMemory(device, deviceMemory, nullptr); - - } - }; - - struct FrameBuffer - { - int32_t width, height; - VkFramebuffer frameBuffer; - VkRenderPass renderPass; - void setSize(int32_t w, int32_t h) - { - this->width = w; - this->height = h; - } - void destroy(VkDevice device) - { - vkDestroyFramebuffer(device, frameBuffer, nullptr); - vkDestroyRenderPass(device, renderPass, nullptr); - } - }; - - struct PBRFrameBuffer { - FrameBufferAttachment color, depth; - FrameBuffer fbo; - bool bCreate = false; - } pbrFrameBuffer; - - VkSampler colorSampler; - - struct DescriptorSetLayouts { - VkDescriptorSetLayout matrices; - VkDescriptorSetLayout textures; - VkDescriptorSetLayout materialUniform; - VkDescriptorSetLayout ssbo; - VkDescriptorSetLayout jointMatrices; - } descriptorSetLayouts; - - struct IBLTextures - { - vks::TextureCubeMap skyboxCube; - vks::TextureCubeMap irradianceCube; - vks::TextureCubeMap prefilteredCube; - vks::Texture2D lutBrdf; - } ibltextures; - - struct OffScreen - { - VkImage image; - VkImageView view; - VkDeviceMemory memory; - VkFramebuffer framebuffer; - } offscreen; - - struct IrradiancePushBlock - { - glm::mat4 mvp; - // Sampling deltas - float deltaPhi = (2.0f * float(M_PI)) / 180.0f; - float deltaTheta = (0.5f * float(M_PI)) / 64.0f; - } irradiancePushBlock; - - struct PrefilterPushBlock { - glm::mat4 mvp; - float roughness; - uint32_t numSamples = 32u; - } prefilterPushBlock; - - VulkanglTFModel skyboxModel; - - VulkanExample(); - ~VulkanExample() - { - // Clean up used Vulkan resources - // Note : Inherited destructor cleans up resources stored in base class - vkDestroyPipeline(device, pipelines.solid, nullptr); - vkDestroyPipeline(device, pipelines.toneMapping, nullptr); - if (pipelines.wireframe != VK_NULL_HANDLE) { - vkDestroyPipeline(device, pipelines.wireframe, nullptr); - } - - vkDestroyPipelineLayout(device, pipelineLayouts.pbrLayout, nullptr); - vkDestroyPipelineLayout(device, pipelineLayouts.tonemappingLayout, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.materialUniform, nullptr); - vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.ssbo, nullptr); - ibltextures.irradianceCube.destroy(); - ibltextures.skyboxCube.destroy(); - ibltextures.prefilteredCube.destroy(); - ibltextures.lutBrdf.destroy(); - pbrFrameBuffer.color.destroy(device); - pbrFrameBuffer.depth.destroy(device); - pbrFrameBuffer.fbo.destroy(device); - vkDestroySampler(device, colorSampler, nullptr); - - shaderData.buffer.destroy(); - shaderData.skinSSBO.destroy(); - } - void loadglTFFile(std::string filename, VulkanglTFModel& model, bool bSkyboxFlag); - virtual void getEnabledFeatures(); - void createAttachment(VkFormat format, VkImageUsageFlagBits usage, FrameBufferAttachment* attachment, uint32_t width, uint32_t height); - virtual void setupFrameBuffer(); - void buildCommandBuffers(); - void loadAssets(); - void setupDescriptors(); - void preparePipelines(); - void CreateToneMappingPipeline(); - void GenerateIrradianceCubemap(); - void GeneratePrefilteredCubemap(); - void GenerateBRDFLUT(); - void prepareUniformBuffers(); - void updateUniformBuffers(); - void prepare(); - virtual void render(); - virtual void viewChanged(); - virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay); }; \ No newline at end of file diff --git a/homework/render/render.cpp b/src/render/render.cpp similarity index 99% rename from homework/render/render.cpp rename to src/render/render.cpp index 6fbcab0..b6d3dbc 100644 --- a/homework/render/render.cpp +++ b/src/render/render.cpp @@ -5,7 +5,6 @@ * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ - /* * Shows how to load and display a simple scene from a glTF file * Note that this isn't a complete glTF loader and only basic functions are shown here @@ -17,7 +16,7 @@ * If you are looking for a complete glTF implementation, check out https://github.com/SaschaWillems/Vulkan-glTF-PBR/ */ #include "render.h" - +#include "glTFModel.h" /* glTF loading functions diff --git a/src/render/render.h b/src/render/render.h new file mode 100644 index 0000000..1465592 --- /dev/null +++ b/src/render/render.h @@ -0,0 +1,206 @@ +/* +#include +#include +#include +#include +#include + +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include +#include +#include + + + +#ifdef VK_USE_PLATFORM_ANDROID_KHR + #define TINYGLTF_ANDROID_LOAD_FROM_ASSETS +#endif + +#define TINYGLTF_IMPLEMENTATION +#define STB_IMAGE_IMPLEMENTATION +#define TINYGLTF_NO_STB_IMAGE_WRITE +#include "tiny_gltf.h" +*/ +#include "vulkanexamplebase.h" +#include "glTFModel.h" + +#define ENABLE_VALIDATION false + + + +class VulkanExample : public VulkanExampleBase +{ +public: + bool wireframe = false; + bool normalMapping = true; + bool ToneMapping = true; + bool pbrEnabled = true; + + VulkanglTFModel glTFModel; + + struct ShaderData { + vks::Buffer buffer; + struct Values { + glm::mat4 projection; + glm::mat4 model; + glm::vec4 lightPos = glm::vec4(5.0f, 5.0f, 5.0f, 1.0f); + glm::vec4 viewPos; + glm::vec4 bFlagSet = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f); + } values; + vks::Buffer skinSSBO; + } shaderData; + + struct StagingBuffer { + VkBuffer buffer; + VkDeviceMemory memory; + } vertexStaging, indexStaging; + + struct Pipelines { + VkPipeline solid; + VkPipeline wireframe = VK_NULL_HANDLE; + VkPipeline toneMapping = VK_NULL_HANDLE; + } pipelines; + + struct PipelineLayouts + { + VkPipelineLayout pbrLayout; + VkPipelineLayout tonemappingLayout; + } pipelineLayouts; + + VkPipelineLayout pipelineLayout; + + VkDescriptorSet descriptorSet; + VkDescriptorSet skinDescriptorSet; + VkDescriptorSet tonemappingDescriptorSet = VK_NULL_HANDLE; + + struct FrameBufferAttachment + { + VkImage image; + VkDeviceMemory deviceMemory; + VkImageView imageView; + VkFormat format; + + + void destroy(VkDevice device) + { + vkDestroyImage(device, image, nullptr); + vkDestroyImageView(device, imageView,nullptr); + vkFreeMemory(device, deviceMemory, nullptr); + + } + }; + + struct FrameBuffer + { + int32_t width, height; + VkFramebuffer frameBuffer; + VkRenderPass renderPass; + void setSize(int32_t w, int32_t h) + { + this->width = w; + this->height = h; + } + void destroy(VkDevice device) + { + vkDestroyFramebuffer(device, frameBuffer, nullptr); + vkDestroyRenderPass(device, renderPass, nullptr); + } + }; + + struct PBRFrameBuffer { + FrameBufferAttachment color, depth; + FrameBuffer fbo; + bool bCreate = false; + } pbrFrameBuffer; + + VkSampler colorSampler; + + struct DescriptorSetLayouts { + VkDescriptorSetLayout matrices; + VkDescriptorSetLayout textures; + VkDescriptorSetLayout materialUniform; + VkDescriptorSetLayout ssbo; + VkDescriptorSetLayout jointMatrices; + } descriptorSetLayouts; + + struct IBLTextures + { + vks::TextureCubeMap skyboxCube; + vks::TextureCubeMap irradianceCube; + vks::TextureCubeMap prefilteredCube; + vks::Texture2D lutBrdf; + } ibltextures; + + struct OffScreen + { + VkImage image; + VkImageView view; + VkDeviceMemory memory; + VkFramebuffer framebuffer; + } offscreen; + + struct IrradiancePushBlock + { + glm::mat4 mvp; + // Sampling deltas + float deltaPhi = (2.0f * float(M_PI)) / 180.0f; + float deltaTheta = (0.5f * float(M_PI)) / 64.0f; + } irradiancePushBlock; + + struct PrefilterPushBlock { + glm::mat4 mvp; + float roughness; + uint32_t numSamples = 32u; + } prefilterPushBlock; + + VulkanglTFModel skyboxModel; + + VulkanExample(); + ~VulkanExample() + { + // Clean up used Vulkan resources + // Note : Inherited destructor cleans up resources stored in base class + vkDestroyPipeline(device, pipelines.solid, nullptr); + vkDestroyPipeline(device, pipelines.toneMapping, nullptr); + if (pipelines.wireframe != VK_NULL_HANDLE) { + vkDestroyPipeline(device, pipelines.wireframe, nullptr); + } + + vkDestroyPipelineLayout(device, pipelineLayouts.pbrLayout, nullptr); + vkDestroyPipelineLayout(device, pipelineLayouts.tonemappingLayout, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.matrices, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.materialUniform, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.ssbo, nullptr); + ibltextures.irradianceCube.destroy(); + ibltextures.skyboxCube.destroy(); + ibltextures.prefilteredCube.destroy(); + ibltextures.lutBrdf.destroy(); + pbrFrameBuffer.color.destroy(device); + pbrFrameBuffer.depth.destroy(device); + pbrFrameBuffer.fbo.destroy(device); + vkDestroySampler(device, colorSampler, nullptr); + + shaderData.buffer.destroy(); + shaderData.skinSSBO.destroy(); + } + void loadglTFFile(std::string filename, VulkanglTFModel& model, bool bSkyboxFlag); + virtual void getEnabledFeatures(); + void createAttachment(VkFormat format, VkImageUsageFlagBits usage, FrameBufferAttachment* attachment, uint32_t width, uint32_t height); + virtual void setupFrameBuffer(); + void buildCommandBuffers(); + void loadAssets(); + void setupDescriptors(); + void preparePipelines(); + void CreateToneMappingPipeline(); + void GenerateIrradianceCubemap(); + void GeneratePrefilteredCubemap(); + void GenerateBRDFLUT(); + void prepareUniformBuffers(); + void updateUniformBuffers(); + void prepare(); + virtual void render(); + virtual void viewChanged(); + virtual void OnUpdateUIOverlay(vks::UIOverlay* overlay); +}; \ No newline at end of file