reconstruct render

pull/2/head
ink-soul 2023-06-05 16:40:39 +08:00
parent 25402941e3
commit 5f98bc4fd1
2 changed files with 72 additions and 301 deletions

View File

@ -37,12 +37,8 @@
PlumageRender::PlumageRender(): PlumageRender::PlumageRender():
VulkanExampleBase(ENABLE_VALIDATION) VulkanExampleBase(ENABLE_VALIDATION)
{ {
title = "render"; title = "plumage 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);
} }
void PlumageRender::getEnabledFeatures() 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<VkAttachmentDescription, 2> attachs = {};
for (uint32_t i = 0; i < static_cast<uint32_t>(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<VkSubpassDependency, 2> 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<uint32_t>(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() void PlumageRender::buildCommandBuffers()
{ {
@ -221,119 +107,7 @@ PlumageRender::PlumageRender():
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); 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<uint32_t> indexBuffer;
std::vector<VulkanglTFModel::Vertex> vertexBuffer;
if (fileLoaded) {
model.nodeCount = static_cast<uint32_t>(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<uint32_t>(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,
&copyRegion);
copyRegion.size = indexBufferSize;
vkCmdCopyBuffer(
copyCmd,
indexStaging.buffer,
model.indices.buffer,
1,
&copyRegion);
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 // TO DO:reconstruct with getting file path through struct
void PlumageRender::loadAssets() void PlumageRender::loadAssets()

View File

@ -17,19 +17,59 @@ public:
bool ToneMapping = true; bool ToneMapping = true;
bool pbrEnabled = 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 { struct ShaderData {
vks::Buffer buffer; glm::vec4 lightDir;
struct Values { 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 projection;
glm::mat4 model; glm::mat4 model;
glm::vec4 lightPos = glm::vec4(5.0f, 5.0f, 5.0f, 1.0f); glm::mat4 view;
glm::vec4 viewPos; glm::vec3 camPos;
glm::vec4 bFlagSet = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f); } shaderValuesScene, shaderValuesSkybox;
} values;
vks::Buffer skinSSBO; struct PushConstBlockMaterial {
} shaderData; 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 struct FilePath
{ {
@ -57,6 +97,10 @@ public:
} vertexStaging, indexStaging; } vertexStaging, indexStaging;
struct Pipelines { struct Pipelines {
VkPipeline skybox;
VkPipeline pbr;
VkPipeline pbrDoubleSided;
VkPipeline pbrAlphaBlend;
VkPipeline solid; VkPipeline solid;
VkPipeline wireframe = VK_NULL_HANDLE; VkPipeline wireframe = VK_NULL_HANDLE;
VkPipeline toneMapping = VK_NULL_HANDLE; VkPipeline toneMapping = VK_NULL_HANDLE;
@ -64,73 +108,29 @@ public:
struct PipelineLayouts struct PipelineLayouts
{ {
VkPipelineLayout pbrLayout; VkDescriptorSetLayout scene;
VkDescriptorSetLayout material;
VkDescriptorSetLayout node;
VkPipelineLayout tonemappingLayout; VkPipelineLayout tonemappingLayout;
} pipelineLayouts; } pipelineLayouts;
VkPipelineLayout pipelineLayout; VkPipelineLayout pipelineLayout;
struct DescriptorSets {
VkDescriptorSet descriptorSet; VkDescriptorSet scene;
VkDescriptorSet skinDescriptorSet; VkDescriptorSet skybox;
VkDescriptorSet tonemappingDescriptorSet = VK_NULL_HANDLE; 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 { struct DescriptorSetLayouts {
VkDescriptorSetLayout matrices; VkDescriptorSetLayout scene;
VkDescriptorSetLayout textures; VkDescriptorSetLayout material;
VkDescriptorSetLayout materialUniform; VkDescriptorSetLayout node;
VkDescriptorSetLayout ssbo;
VkDescriptorSetLayout jointMatrices;
} descriptorSetLayouts; } descriptorSetLayouts;
struct IBLTextures
{
vks::TextureCubeMap skyboxCube;
vks::TextureCubeMap irradianceCube;
vks::TextureCubeMap prefilteredCube;
vks::Texture2D lutBrdf;
} ibltextures;
struct OffScreen struct OffScreen
{ {
@ -154,7 +154,7 @@ public:
uint32_t numSamples = 32u; uint32_t numSamples = 32u;
} prefilterPushBlock; } prefilterPushBlock;
glTFModel::Model skyboxModel;
PlumageRender(); PlumageRender();
~PlumageRender() ~PlumageRender()
@ -173,10 +173,7 @@ public:
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.textures, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.materialUniform, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.materialUniform, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.ssbo, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.ssbo, nullptr);
ibltextures.irradianceCube.destroy();
ibltextures.skyboxCube.destroy();
ibltextures.prefilteredCube.destroy();
ibltextures.lutBrdf.destroy();
pbrFrameBuffer.color.destroy(device); pbrFrameBuffer.color.destroy(device);
pbrFrameBuffer.depth.destroy(device); pbrFrameBuffer.depth.destroy(device);
pbrFrameBuffer.fbo.destroy(device); pbrFrameBuffer.fbo.destroy(device);