From 5f98bc4fd1968cfabb63af969f9187708f37a946 Mon Sep 17 00:00:00 2001 From: ink-soul Date: Mon, 5 Jun 2023 16:40:39 +0800 Subject: [PATCH] reconstruct render --- src/render/render.cpp | 232 +----------------------------------------- src/render/render.h | 141 +++++++++++++------------ 2 files changed, 72 insertions(+), 301 deletions(-) diff --git a/src/render/render.cpp b/src/render/render.cpp index b99e717..5914d1f 100644 --- a/src/render/render.cpp +++ b/src/render/render.cpp @@ -37,12 +37,8 @@ PlumageRender::PlumageRender(): VulkanExampleBase(ENABLE_VALIDATION) { - title = "render"; - camera.type = Camera::CameraType::lookat; - camera.flipY = true; - camera.setPosition(glm::vec3(0.0f, -0.1f, -1.0f)); - camera.setRotation(glm::vec3(0.0f, 45.0f, 0.0f)); - camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); + title = "plumage render"; + } void PlumageRender::getEnabledFeatures() @@ -53,117 +49,7 @@ PlumageRender::PlumageRender(): }; } - void PlumageRender::setupFrameBuffer() - { - VulkanExampleBase::setupFrameBuffer(); - if (pbrFrameBuffer.bCreate && (pbrFrameBuffer.fbo.width != width || pbrFrameBuffer.fbo.height != height)) - { - pbrFrameBuffer.color.destroy(device); - pbrFrameBuffer.depth.destroy(device); - pbrFrameBuffer.fbo.destroy(device); - vkDestroySampler(device, colorSampler, nullptr); - } - - //Create image color attachment - pbrFrameBuffer.fbo.setSize(width, height); - VkFormat attDepthFormat; - VkBool32 validDepthFormat = vks::tools::getSupportedDepthFormat(physicalDevice, &attDepthFormat); - assert(validDepthFormat); - - createAttachment(VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &pbrFrameBuffer.color, width, height); - createAttachment(attDepthFormat, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, &pbrFrameBuffer.depth, width, height); - { - std::array attachs = {}; - for (uint32_t i = 0; i < static_cast(attachs.size()); ++i) - { - attachs[i].samples = VK_SAMPLE_COUNT_1_BIT; - attachs[i].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; - attachs[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE; - attachs[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; - attachs[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; - attachs[i].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - attachs[i].finalLayout = i == 1 ? VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; - } - attachs[0].format = pbrFrameBuffer.color.format; - attachs[1].format = pbrFrameBuffer.depth.format; - - VkAttachmentReference colorReference = {}; - colorReference.attachment = 0; - colorReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; - - VkAttachmentReference depthReference = {}; - depthReference.attachment = 1; - depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL; - - VkSubpassDescription subpass = {}; - subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; - subpass.pColorAttachments = &colorReference; - subpass.colorAttachmentCount = 1; - subpass.pDepthStencilAttachment = &depthReference; - - std::array dependencies; - //To test src 0 - dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; - dependencies[0].dstSubpass = 0; - dependencies[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; - dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependencies[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT; - dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - dependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; - - dependencies[1].srcSubpass = 0; - dependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL; - dependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; - dependencies[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT; - dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; - dependencies[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT; - dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; - - VkRenderPassCreateInfo renderPassCI = {}; - renderPassCI.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - renderPassCI.pAttachments = attachs.data(); - renderPassCI.attachmentCount = static_cast(attachs.size()); - renderPassCI.pSubpasses = &subpass; - renderPassCI.subpassCount = 1; - renderPassCI.pDependencies = dependencies.data(); - renderPassCI.dependencyCount = 2; - VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCI, nullptr, &pbrFrameBuffer.fbo.renderPass)); - - //Create FBO - VkImageView attachments[2] = { pbrFrameBuffer.color.imageView, pbrFrameBuffer.depth.imageView }; - VkFramebufferCreateInfo fbufCreateInfo = vks::initializers::framebufferCreateInfo(); - fbufCreateInfo.renderPass = pbrFrameBuffer.fbo.renderPass; - fbufCreateInfo.pAttachments = attachments; - fbufCreateInfo.attachmentCount = 2; - fbufCreateInfo.width = pbrFrameBuffer.fbo.width; - fbufCreateInfo.height = pbrFrameBuffer.fbo.height; - fbufCreateInfo.layers = 1; - VK_CHECK_RESULT(vkCreateFramebuffer(device, &fbufCreateInfo, nullptr, &pbrFrameBuffer.fbo.frameBuffer)); - } - - //Create Image sampler - VkSamplerCreateInfo samplerCI = vks::initializers::samplerCreateInfo(); - samplerCI.magFilter = VK_FILTER_NEAREST; - samplerCI.minFilter = VK_FILTER_NEAREST; - samplerCI.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; - samplerCI.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - samplerCI.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - samplerCI.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE; - //samplerCI.mipLodBias = 0.0f; - //samplerCI.maxAnisotropy = 1.0f; - samplerCI.minLod = 0.0f; - samplerCI.maxLod = 1.0f; - samplerCI.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; - VK_CHECK_RESULT(vkCreateSampler(device, &samplerCI, nullptr, &colorSampler)); - - if (tonemappingDescriptorSet != VK_NULL_HANDLE) //Bad logic - { - auto imageInfo = vks::initializers::descriptorImageInfo(colorSampler, pbrFrameBuffer.color.imageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - VkWriteDescriptorSet writeDescriptorSet = vks::initializers::writeDescriptorSet(tonemappingDescriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &imageInfo); - vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr); - } - pbrFrameBuffer.bCreate = true; - } + void PlumageRender::buildCommandBuffers() { @@ -221,119 +107,7 @@ PlumageRender::PlumageRender(): VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); } } -/* - void PlumageRender::loadglTFFile(std::string filename, glTFModel::Model& model, bool bSkyboxFlag = false) - { - tinygltf::Model glTFInput; - tinygltf::TinyGLTF gltfContext; - std::string error, warning; - this->device = device; - -#if defined(__ANDROID__) - // On Android all assets are packed with the apk in a compressed form, so we need to open them using the asset manager - // We let tinygltf handle this, by passing the asset manager of our app - tinygltf::asset_manager = androidApp->activity->assetManager; -#endif - bool fileLoaded = gltfContext.LoadASCIIFromFile(&glTFInput, &error, &warning, filename); - - // Pass some Vulkan resources required for setup and rendering to the glTF model loading class - model.vulkanDevice = vulkanDevice; - model.copyQueue = queue; - - std::vector indexBuffer; - std::vector vertexBuffer; - - if (fileLoaded) { - model.nodeCount = static_cast(glTFInput.nodes.size()); - model.loadImages(glTFInput); - model.loadMaterials(glTFInput); - model.loadTextures(glTFInput); - const tinygltf::Scene& scene = glTFInput.scenes[0]; - for (size_t i = 0; i < scene.nodes.size(); i++) { - const tinygltf::Node node = glTFInput.nodes[scene.nodes[i]]; - model.loadNode(node, glTFInput, nullptr, scene.nodes[i], indexBuffer, vertexBuffer); - } - model.loadAnimations(glTFInput); - } - else { - vks::tools::exitFatal("Could not open the glTF file.\n\nThe file is part of the additional asset pack.\n\nRun \"download_assets.py\" in the repository root to download the latest version.", -1); - return; - } - - // Create and upload vertex and index buffer - // We will be using one single vertex buffer and one single index buffer for the whole glTF scene - // Primitives (of the glTF model) will then index into these using index offsets - - size_t vertexBufferSize = vertexBuffer.size() * sizeof(VulkanglTFModel::Vertex); - size_t indexBufferSize = indexBuffer.size() * sizeof(uint32_t); - model.indices.count = static_cast(indexBuffer.size()); - - struct StagingBuffer { - VkBuffer buffer; - VkDeviceMemory memory; - } vertexStaging, indexStaging; - - // Create host visible staging buffers (source) - VK_CHECK_RESULT(vulkanDevice->createBuffer( - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - vertexBufferSize, - &vertexStaging.buffer, - &vertexStaging.memory, - vertexBuffer.data())); - // Index data - VK_CHECK_RESULT(vulkanDevice->createBuffer( - VK_BUFFER_USAGE_TRANSFER_SRC_BIT, - VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, - indexBufferSize, - &indexStaging.buffer, - &indexStaging.memory, - indexBuffer.data())); - - // Create device local buffers (target) - VK_CHECK_RESULT(vulkanDevice->createBuffer( - VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - vertexBufferSize, - &model.vertices.buffer, - &model.vertices.memory)); - VK_CHECK_RESULT(vulkanDevice->createBuffer( - VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, - VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - indexBufferSize, - &model.indices.buffer, - &model.indices.memory)); - - // Copy data from staging buffers (host) do device local buffer (gpu) - VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - VkBufferCopy copyRegion = {}; - - copyRegion.size = vertexBufferSize; - vkCmdCopyBuffer( - copyCmd, - vertexStaging.buffer, - model.vertices.buffer, - 1, - ©Region); - - copyRegion.size = indexBufferSize; - vkCmdCopyBuffer( - copyCmd, - indexStaging.buffer, - model.indices.buffer, - 1, - ©Region); - - vulkanDevice->flushCommandBuffer(copyCmd, queue, true); - - // Free staging resources - vkDestroyBuffer(device, vertexStaging.buffer, nullptr); - vkFreeMemory(device, vertexStaging.memory, nullptr); - vkDestroyBuffer(device, indexStaging.buffer, nullptr); - vkFreeMemory(device, indexStaging.memory, nullptr); - } -*/ // TO DO:reconstruct with getting file path through struct void PlumageRender::loadAssets() diff --git a/src/render/render.h b/src/render/render.h index 00e2a93..86ada2a 100644 --- a/src/render/render.h +++ b/src/render/render.h @@ -17,20 +17,60 @@ public: bool ToneMapping = true; bool pbrEnabled = true; - glTFModel::Model glTFModel; + struct Models + { + glTFModel::Model scene; + glTFModel::Model skybox; + }; + + struct Textures { + vks::TextureCubeMap environmentCube; + vks::Texture2D empty; + vks::Texture2D lutBrdf; + vks::TextureCubeMap irradianceCube; + vks::TextureCubeMap prefilteredCube; + } textures; 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; + glm::vec4 lightDir; + float exposure = 4.5f; + float gamma = 2.2f; + float prefilteredCubeMipLevels; + float scaleIBLAmbient = 1.0f; + float debugViewInputs = 0; + float debugViewEquation = 0; } shaderData; + struct UniformBufferSet { + Buffer scene; + Buffer skybox; + Buffer params; + }; + + struct UBOMatrices { + glm::mat4 projection; + glm::mat4 model; + glm::mat4 view; + glm::vec3 camPos; + } shaderValuesScene, shaderValuesSkybox; + + struct PushConstBlockMaterial { + glm::vec4 baseColorFactor; + glm::vec4 emissiveFactor; + glm::vec4 diffuseFactor; + glm::vec4 specularFactor; + float workflow; + int colorTextureSet; + int PhysicalDescriptorTextureSet; + int normalTextureSet; + int occlusionTextureSet; + int emissiveTextureSet; + float metallicFactor; + float roughnessFactor; + float alphaMask; + float alphaMaskCutoff; + } pushConstBlockMaterial; + struct FilePath { std::string glTFModelFilePath = getAssetPath() + "buster_drone/busterDrone.gltf"; @@ -57,6 +97,10 @@ public: } vertexStaging, indexStaging; struct Pipelines { + VkPipeline skybox; + VkPipeline pbr; + VkPipeline pbrDoubleSided; + VkPipeline pbrAlphaBlend; VkPipeline solid; VkPipeline wireframe = VK_NULL_HANDLE; VkPipeline toneMapping = VK_NULL_HANDLE; @@ -64,73 +108,29 @@ public: struct PipelineLayouts { - VkPipelineLayout pbrLayout; + VkDescriptorSetLayout scene; + VkDescriptorSetLayout material; + VkDescriptorSetLayout node; VkPipelineLayout tonemappingLayout; } pipelineLayouts; VkPipelineLayout pipelineLayout; + struct DescriptorSets { + VkDescriptorSet scene; + VkDescriptorSet skybox; + VkDescriptorSet tonemappingDescriptorSet = VK_NULL_HANDLE; + }; + - 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; + VkDescriptorSetLayout scene; + VkDescriptorSetLayout material; + VkDescriptorSetLayout node; } descriptorSetLayouts; - struct IBLTextures - { - vks::TextureCubeMap skyboxCube; - vks::TextureCubeMap irradianceCube; - vks::TextureCubeMap prefilteredCube; - vks::Texture2D lutBrdf; - } ibltextures; struct OffScreen { @@ -154,7 +154,7 @@ public: uint32_t numSamples = 32u; } prefilterPushBlock; - glTFModel::Model skyboxModel; + PlumageRender(); ~PlumageRender() @@ -173,10 +173,7 @@ public: 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);