/* * Vulkan physical based rendering glTF 2.0 renderer * * Copyright (C) 2018-2025 by Sascha Willems - www.saschawillems.de * * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) */ // glTF format: https://github.com/KhronosGroup/glTF // tinyglTF loader: https://github.com/syoyo/tinygltf #include #include #include #include #include #include #include #include #include "algorithm" #include #include "VulkanExampleBase.h" #include "VulkanTexture.hpp" #include "VulkanglTFModel.h" #include "VulkanUtils.hpp" #include "ui.hpp" #define GLM_FORCE_RADIANS #define GLM_FORCE_DEPTH_ZERO_TO_ONE #include #include /* PBR example main class */ class VulkanApplication : public VulkanExampleBase { public: struct Textures { vks::TextureCubeMap environmentCube; vks::Texture2D empty; vks::Texture2D lutBrdf; vks::TextureCubeMap irradianceCube; vks::TextureCubeMap prefilteredCube; } textures; struct Models { vkglTF::Model scene; vkglTF::Model skybox; } models; struct UniformBufferSet { Buffer scene; Buffer skybox; Buffer params; }; struct UBOMatrices { glm::mat4 projection{ 1.0f }; glm::mat4 model{ 1.0f }; glm::mat4 view{ 1.0f }; glm::vec3 camPos{ 0.0f }; } shaderValuesScene, shaderValuesSkybox; struct shaderValuesParams { glm::vec4 lightDir; float exposure = 4.5f; float gamma = 2.2f; float prefilteredCubeMipLevels; float scaleIBLAmbient = 1.0f; float debugViewInputs = 0; float debugViewEquation = 0; } shaderValuesParams; VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; std::unordered_map pipelines; VkPipeline boundPipeline{ VK_NULL_HANDLE }; struct DescriptorSetLayouts { VkDescriptorSetLayout scene{ VK_NULL_HANDLE }; VkDescriptorSetLayout material{ VK_NULL_HANDLE }; VkDescriptorSetLayout materialBuffer{ VK_NULL_HANDLE }; VkDescriptorSetLayout meshDataBuffer{ VK_NULL_HANDLE }; } descriptorSetLayouts; struct DescriptorSets { VkDescriptorSet scene; VkDescriptorSet skybox; }; std::vector descriptorSets; std::vector commandBuffers; std::vector uniformBuffers; std::vector waitFences; std::vector renderCompleteSemaphores; std::vector presentCompleteSemaphores; const uint32_t renderAhead = 2; uint32_t frameIndex = 0; int32_t animationIndex = 0; float animationTimer = 0.0f; bool animate = true; bool displayBackground = true; struct LightSource { glm::vec3 color = glm::vec3(1.0f); glm::vec3 rotation = glm::vec3(75.0f, -40.0f, 0.0f); } lightSource; UI* ui{ nullptr }; #if defined(VK_USE_PLATFORM_ANDROID_KHR) const std::string assetpath = ""; #else const std::string assetpath = "./../data/"; #endif enum PBRWorkflows{ PBR_WORKFLOW_METALLIC_ROUGHNESS = 0, PBR_WORKFLOW_SPECULAR_GLOSSINESS = 1 }; // We use a material buffer to pass material data ind image indices to the shader struct alignas(16) ShaderMaterial { 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; float emissiveStrength; }; Buffer shaderMaterialBuffer; VkDescriptorSet descriptorSetMaterials{ VK_NULL_HANDLE }; struct MeshPushConstantBlock { int32_t meshIndex; int32_t materialIndex; }; // We use a large buffer to store all per mesh data that needs to be passed to the shader struct alignas(16) ShaderMeshData { glm::mat4 matrix; glm::mat4 jointMatrix[MAX_NUM_JOINTS]{}; uint32_t jointcount{ 0 }; }; std::vector shaderMeshDataBuffers; std::vector descriptorSetsMeshData; std::map environments; std::string selectedEnvironment = "papermill"; #if !defined(_WIN32) std::map scenes; std::string selectedScene = "DamagedHelmet"; #endif int32_t debugViewInputs = 0; int32_t debugViewEquation = 0; // List of glTF extensions supported by this application // Models with un-supported extensions may not work/look as expected const std::vector supportedExtensions = { "KHR_texture_basisu", "KHR_materials_pbrSpecularGlossiness", "KHR_materials_unlit", "KHR_materials_emissive_strength" }; VulkanApplication() : VulkanExampleBase() { title = "Vulkan glTF 2.0 PBR - (C) Sascha Willems (www.saschawillems.de)"; #if defined(TINYGLTF_ENABLE_DRACO) std::cout << "Draco mesh compression is enabled" << std::endl; #endif } ~VulkanApplication() { for (auto& pipeline : pipelines) { vkDestroyPipeline(device, pipeline.second, nullptr); } vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.scene, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.material, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.materialBuffer, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayouts.meshDataBuffer, nullptr); models.scene.destroy(device); models.skybox.destroy(device); for (auto buffer : uniformBuffers) { buffer.params.destroy(); buffer.scene.destroy(); buffer.skybox.destroy(); } for (auto fence : waitFences) { vkDestroyFence(device, fence, nullptr); } for (auto semaphore : renderCompleteSemaphores) { vkDestroySemaphore(device, semaphore, nullptr); } for (auto semaphore : presentCompleteSemaphores) { vkDestroySemaphore(device, semaphore, nullptr); } textures.environmentCube.destroy(); textures.irradianceCube.destroy(); textures.prefilteredCube.destroy(); textures.lutBrdf.destroy(); textures.empty.destroy(); delete ui; } void resetCamera() { camera.setPosition({ 0.0f, 0.0f, 1.0f }); camera.setRotation({ 0.0f, 0.0f, 0.0f }); camera.updateViewMatrix(); } void renderNode(vkglTF::Node *node, uint32_t cbIndex, vkglTF::Material::AlphaMode alphaMode) { if (node->mesh) { // Render mesh primitives for (vkglTF::Primitive * primitive : node->mesh->primitives) { if (primitive->material.alphaMode == alphaMode) { std::string pipelineName = "pbr"; std::string pipelineVariant = ""; if (primitive->material.unlit) { // KHR_materials_unlit pipelineName = "unlit"; }; // Material properties define if we e.g. need to bind a pipeline variant with culling disabled (double sided) if (alphaMode == vkglTF::Material::ALPHAMODE_BLEND) { pipelineVariant = "_alpha_blending"; } else { if (primitive->material.doubleSided) { pipelineVariant = "_double_sided"; } } const VkPipeline pipeline = pipelines[pipelineName + pipelineVariant]; if (pipeline != boundPipeline) { vkCmdBindPipeline(commandBuffers[cbIndex], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); boundPipeline = pipeline; } const std::vector descriptorsets = { descriptorSets[cbIndex].scene, primitive->material.descriptorSet, // @todo: per frame-in-flight descriptorSetsMeshData[cbIndex], descriptorSetMaterials }; vkCmdBindDescriptorSets(commandBuffers[cbIndex], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, static_cast(descriptorsets.size()), descriptorsets.data(), 0, NULL); // Pass material index for this primitive using a push constant, the shader uses this to index into the material buffer MeshPushConstantBlock pushConstantBlock{}; // @todo: index pushConstantBlock.meshIndex = node->mesh->index; pushConstantBlock.materialIndex = primitive->material.index; vkCmdPushConstants(commandBuffers[cbIndex], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(MeshPushConstantBlock), &pushConstantBlock); if (primitive->hasIndices) { vkCmdDrawIndexed(commandBuffers[cbIndex], primitive->indexCount, 1, primitive->firstIndex, 0, 0); } else { vkCmdDraw(commandBuffers[cbIndex], primitive->vertexCount, 1, 0, 0); } } } }; for (auto child : node->children) { renderNode(child, cbIndex, alphaMode); } } void recordCommandBuffer() { vkResetCommandBuffer(commandBuffers[frameIndex], 0); VkCommandBufferBeginInfo cmdBufferBeginInfo{}; cmdBufferBeginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; VkClearValue clearValues[3]; if (settings.multiSampling) { clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } }; clearValues[1].color = { { 0.0f, 0.0f, 0.0f, 1.0f } }; clearValues[2].depthStencil = { 1.0f, 0 }; } else { clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } }; clearValues[1].depthStencil = { 1.0f, 0 }; } VkRenderPassBeginInfo renderPassBeginInfo{}; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.renderPass = renderPass; renderPassBeginInfo.renderArea.offset.x = 0; renderPassBeginInfo.renderArea.offset.y = 0; renderPassBeginInfo.renderArea.extent.width = width; renderPassBeginInfo.renderArea.extent.height = height; renderPassBeginInfo.clearValueCount = settings.multiSampling ? 3 : 2; renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.framebuffer = frameBuffers[imageIndex]; VkCommandBuffer currentCB = commandBuffers[frameIndex]; VK_CHECK_RESULT(vkBeginCommandBuffer(currentCB, &cmdBufferBeginInfo)); vkCmdBeginRenderPass(currentCB, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); VkViewport viewport{}; viewport.width = (float)width; viewport.height = (float)height; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; vkCmdSetViewport(currentCB, 0, 1, &viewport); VkRect2D scissor{}; scissor.extent = { width, height }; vkCmdSetScissor(currentCB, 0, 1, &scissor); VkDeviceSize offsets[1] = { 0 }; if (displayBackground) { vkCmdBindDescriptorSets(currentCB, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[frameIndex].skybox, 0, nullptr); vkCmdBindPipeline(currentCB, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines["skybox"]); models.skybox.draw(currentCB); } vkglTF::Model &model = models.scene; vkCmdBindVertexBuffers(currentCB, 0, 1, &model.vertices.buffer, offsets); if (model.indices.buffer != VK_NULL_HANDLE) { vkCmdBindIndexBuffer(currentCB, model.indices.buffer, 0, VK_INDEX_TYPE_UINT32); } boundPipeline = VK_NULL_HANDLE; // Opaque primitives first for (auto node : model.nodes) { renderNode(node, frameIndex, vkglTF::Material::ALPHAMODE_OPAQUE); } // Alpha masked primitives for (auto node : model.nodes) { renderNode(node, frameIndex, vkglTF::Material::ALPHAMODE_MASK); } // Transparent primitives // TODO: Correct depth sorting for (auto node : model.nodes) { renderNode(node, frameIndex, vkglTF::Material::ALPHAMODE_BLEND); } // User interface ui->draw(currentCB); vkCmdEndRenderPass(currentCB); VK_CHECK_RESULT(vkEndCommandBuffer(currentCB)); } // We place all materials for the current scene into a shader storage buffer stored on the GPU // This allows us to use arbitrary large material defintions // The fragment shader then get's the index into this material array from a push constant set per primitive void createMaterialBuffer() { std::vector shaderMaterials{}; for (auto& material : models.scene.materials) { ShaderMaterial shaderMaterial{}; shaderMaterial.emissiveFactor = material.emissiveFactor; // To save space, availabilty and texture coordinate set are combined // -1 = texture not used for this material, >= 0 texture used and index of texture coordinate set shaderMaterial.colorTextureSet = material.baseColorTexture != nullptr ? material.texCoordSets.baseColor : -1; shaderMaterial.normalTextureSet = material.normalTexture != nullptr ? material.texCoordSets.normal : -1; shaderMaterial.occlusionTextureSet = material.occlusionTexture != nullptr ? material.texCoordSets.occlusion : -1; shaderMaterial.emissiveTextureSet = material.emissiveTexture != nullptr ? material.texCoordSets.emissive : -1; shaderMaterial.alphaMask = static_cast(material.alphaMode == vkglTF::Material::ALPHAMODE_MASK); shaderMaterial.alphaMaskCutoff = material.alphaCutoff; shaderMaterial.emissiveStrength = material.emissiveStrength; if (material.pbrWorkflows.metallicRoughness) { // Metallic roughness workflow shaderMaterial.workflow = static_cast(PBR_WORKFLOW_METALLIC_ROUGHNESS); shaderMaterial.baseColorFactor = material.baseColorFactor; shaderMaterial.metallicFactor = material.metallicFactor; shaderMaterial.roughnessFactor = material.roughnessFactor; shaderMaterial.PhysicalDescriptorTextureSet = material.metallicRoughnessTexture != nullptr ? material.texCoordSets.metallicRoughness : -1; shaderMaterial.colorTextureSet = material.baseColorTexture != nullptr ? material.texCoordSets.baseColor : -1; } else { if (material.pbrWorkflows.specularGlossiness) { // Specular glossiness workflow shaderMaterial.workflow = static_cast(PBR_WORKFLOW_SPECULAR_GLOSSINESS); shaderMaterial.PhysicalDescriptorTextureSet = material.extension.specularGlossinessTexture != nullptr ? material.texCoordSets.specularGlossiness : -1; shaderMaterial.colorTextureSet = material.extension.diffuseTexture != nullptr ? material.texCoordSets.baseColor : -1; shaderMaterial.diffuseFactor = material.extension.diffuseFactor; shaderMaterial.specularFactor = glm::vec4(material.extension.specularFactor, 1.0f); } } shaderMaterials.push_back(shaderMaterial); } if (shaderMaterialBuffer.buffer != VK_NULL_HANDLE) { shaderMaterialBuffer.destroy(); } VkDeviceSize bufferSize = shaderMaterials.size() * sizeof(ShaderMaterial); Buffer stagingBuffer; VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, &stagingBuffer.buffer, &stagingBuffer.memory, shaderMaterials.data())); VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, bufferSize, &shaderMaterialBuffer.buffer, &shaderMaterialBuffer.memory)); // Copy from staging buffers VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkBufferCopy copyRegion{}; copyRegion.size = bufferSize; vkCmdCopyBuffer(copyCmd, stagingBuffer.buffer, shaderMaterialBuffer.buffer, 1, ©Region); vulkanDevice->flushCommandBuffer(copyCmd, queue, true); stagingBuffer.device = device; stagingBuffer.destroy(); // Update descriptor shaderMaterialBuffer.descriptor.buffer = shaderMaterialBuffer.buffer; shaderMaterialBuffer.descriptor.offset = 0; shaderMaterialBuffer.descriptor.range = bufferSize; shaderMaterialBuffer.device = device; } // We place all the shader data blocks for all meshes (node) into a single buffer // This allows us to use one singular allocation instead of having to do lots of small allocations per mesh // The vertex shader then get's the index into this buffer from a push constant set per mesh // @todo: Needs to be adjusted to work with frames-in-flight (duplicate buffer) // @todo: Update void createMeshDataBuffer() { std::vector shaderMeshData{}; for (auto& node : models.scene.linearNodes) { ShaderMeshData meshData{}; if (node->mesh) { memcpy(meshData.jointMatrix, node->mesh->jointMatrix, sizeof(glm::mat4) * MAX_NUM_JOINTS); meshData.jointcount = node->mesh->jointcount; meshData.matrix = node->mesh->matrix; shaderMeshData.push_back(meshData); } } for (auto& shaderMeshDataBuffer : shaderMeshDataBuffers) { if (shaderMeshDataBuffer.buffer != VK_NULL_HANDLE) { shaderMeshDataBuffer.destroy(); } VkDeviceSize bufferSize = shaderMeshData.size() * sizeof(ShaderMeshData); if (!vulkanDevice->requiresStaging) { // Prefer a host visible device buffer (ReBAR/SAM on discreate GPUs, always available on integrated GPUs) VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, &shaderMeshDataBuffer.buffer, &shaderMeshDataBuffer.memory)); shaderMeshDataBuffer.device = device; shaderMeshDataBuffer.map(); memcpy(shaderMeshDataBuffer.mapped, shaderMeshData.data(), bufferSize); } else { Buffer stagingBuffer; VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, &stagingBuffer.buffer, &stagingBuffer.memory, shaderMeshData.data())); VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, bufferSize, &shaderMeshDataBuffer.buffer, &shaderMeshDataBuffer.memory)); // Copy from staging buffers VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkBufferCopy copyRegion{}; copyRegion.size = bufferSize; vkCmdCopyBuffer(copyCmd, stagingBuffer.buffer, shaderMeshDataBuffer.buffer, 1, ©Region); vulkanDevice->flushCommandBuffer(copyCmd, queue, true); stagingBuffer.device = device; stagingBuffer.destroy(); } // Update descriptor shaderMeshDataBuffer.descriptor.buffer = shaderMeshDataBuffer.buffer; shaderMeshDataBuffer.descriptor.offset = 0; shaderMeshDataBuffer.descriptor.range = bufferSize; shaderMeshDataBuffer.device = device; } } void updateMeshDataBuffer(uint32_t index) { // @todo: optimize (no push, use fixed size) std::vector shaderMeshData{}; for (auto& node : models.scene.linearNodes) { ShaderMeshData meshData{}; if (node->mesh) { memcpy(meshData.jointMatrix, node->mesh->jointMatrix, sizeof(glm::mat4) * MAX_NUM_JOINTS); meshData.jointcount = node->mesh->jointcount; meshData.matrix = node->mesh->matrix; shaderMeshData.push_back(meshData); } } VkDeviceSize bufferSize = shaderMeshData.size() * sizeof(ShaderMeshData); if (!vulkanDevice->requiresStaging) { memcpy(shaderMeshDataBuffers[index].mapped, shaderMeshData.data(), bufferSize); } else { Buffer stagingBuffer; VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, bufferSize, &stagingBuffer.buffer, &stagingBuffer.memory, shaderMeshData.data())); // Copy from staging buffers VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkBufferCopy copyRegion{}; copyRegion.size = bufferSize; vkCmdCopyBuffer(copyCmd, stagingBuffer.buffer, shaderMeshDataBuffers[index].buffer, 1, ©Region); vulkanDevice->flushCommandBuffer(copyCmd, queue, true); stagingBuffer.device = device; stagingBuffer.destroy(); } } void loadScene(std::string filename) { std::cout << "Loading scene from " << filename << std::endl; models.scene.destroy(device); animationIndex = 0; animationTimer = 0.0f; auto tStart = std::chrono::high_resolution_clock::now(); models.scene.loadFromFile(filename, vulkanDevice, queue); createMaterialBuffer(); createMeshDataBuffer(); auto tFileLoad = std::chrono::duration(std::chrono::high_resolution_clock::now() - tStart).count(); std::cout << "Loading took " << tFileLoad << " ms" << std::endl; // Check and list unsupported extensions for (auto& ext : models.scene.extensions) { if (std::find(supportedExtensions.begin(), supportedExtensions.end(), ext) == supportedExtensions.end()) { std::cout << "[WARN] Unsupported extension " << ext << " detected. Scene may not work or display as intended\n"; } } resetCamera(); } void loadEnvironment(std::string filename) { std::cout << "Loading environment from " << filename << std::endl; if (textures.environmentCube.image) { textures.environmentCube.destroy(); textures.irradianceCube.destroy(); textures.prefilteredCube.destroy(); } textures.environmentCube.loadFromFile(filename, VK_FORMAT_R16G16B16A16_SFLOAT, vulkanDevice, queue); generateCubemaps(); } void loadAssets() { #if defined(VK_USE_PLATFORM_ANDROID_KHR) tinygltf::asset_manager = androidApp->activity->assetManager; readDirectory(assetpath + "models", "gltf", scenes, true); #else struct stat info; if (stat(assetpath.c_str(), &info) != 0) { std::string msg = "Could not locate asset path in \"" + assetpath + "\".\nMake sure binary is run from correct relative directory!"; std::cerr << msg << std::endl; exit(-1); } #endif readDirectory(assetpath + "environments", "ktx", environments, false); textures.empty.loadFromFile(assetpath + "textures/empty.ktx", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue); std::string sceneFile = assetpath + "models/DamagedHelmet/glTF-Embedded/DamagedHelmet.gltf"; std::string envMapFile = assetpath + "environments/papermill.ktx"; for (size_t i = 0; i < args.size(); i++) { if ((std::string(args[i]).find(".gltf") != std::string::npos) || (std::string(args[i]).find(".glb") != std::string::npos)) { std::ifstream file(args[i]); if (file.good()) { sceneFile = args[i]; } else { std::cout << "could not load \"" << args[i] << "\"" << std::endl; } } if (std::string(args[i]).find(".ktx") != std::string::npos) { std::ifstream file(args[i]); if (file.good()) { envMapFile = args[i]; } else { std::cout << "could not load \"" << args[i] << "\"" << std::endl; } } } loadScene(sceneFile.c_str()); models.skybox.loadFromFile(assetpath + "models/Box/glTF-Embedded/Box.gltf", vulkanDevice, queue); loadEnvironment(envMapFile.c_str()); } void setupDescriptors() { /* Descriptor Pool */ uint32_t imageSamplerCount = 0; uint32_t materialCount = 0; uint32_t meshCount = 0; // Environment samplers (radiance, irradiance, brdf lut) imageSamplerCount += 3; std::vector modellist = { &models.skybox, &models.scene }; for (auto &model : modellist) { for (auto &material : model->materials) { imageSamplerCount += 5; materialCount++; } for (auto node : model->linearNodes) { if (node->mesh) { meshCount++; } } } std::vector poolSizes = { { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, (4 + meshCount) * swapChain.imageCount }, { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, imageSamplerCount * swapChain.imageCount }, // One SSBO for the shader material buffer and one SSBO for the mesh data buffer { VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1 + static_cast(shaderMeshDataBuffers.size())} }; VkDescriptorPoolCreateInfo descriptorPoolCI{}; descriptorPoolCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; descriptorPoolCI.poolSizeCount = static_cast(poolSizes.size()); descriptorPoolCI.pPoolSizes = poolSizes.data(); descriptorPoolCI.maxSets = (2 + materialCount + meshCount) * swapChain.imageCount; VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolCI, nullptr, &descriptorPool)); /* Descriptor sets */ // Scene (matrices and environment maps) { std::vector setLayoutBindings = { { 0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, }; VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI{}; descriptorSetLayoutCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetLayoutCI.pBindings = setLayoutBindings.data(); descriptorSetLayoutCI.bindingCount = static_cast(setLayoutBindings.size()); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.scene)); for (auto i = 0; i < descriptorSets.size(); i++) { VkDescriptorSetAllocateInfo descriptorSetAllocInfo{}; descriptorSetAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; descriptorSetAllocInfo.descriptorPool = descriptorPool; descriptorSetAllocInfo.pSetLayouts = &descriptorSetLayouts.scene; descriptorSetAllocInfo.descriptorSetCount = 1; VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &descriptorSets[i].scene)); std::array writeDescriptorSets{}; writeDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; writeDescriptorSets[0].descriptorCount = 1; writeDescriptorSets[0].dstSet = descriptorSets[i].scene; writeDescriptorSets[0].dstBinding = 0; writeDescriptorSets[0].pBufferInfo = &uniformBuffers[i].scene.descriptor; writeDescriptorSets[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; writeDescriptorSets[1].descriptorCount = 1; writeDescriptorSets[1].dstSet = descriptorSets[i].scene; writeDescriptorSets[1].dstBinding = 1; writeDescriptorSets[1].pBufferInfo = &uniformBuffers[i].params.descriptor; writeDescriptorSets[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[2].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSets[2].descriptorCount = 1; writeDescriptorSets[2].dstSet = descriptorSets[i].scene; writeDescriptorSets[2].dstBinding = 2; writeDescriptorSets[2].pImageInfo = &textures.irradianceCube.descriptor; writeDescriptorSets[3].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[3].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSets[3].descriptorCount = 1; writeDescriptorSets[3].dstSet = descriptorSets[i].scene; writeDescriptorSets[3].dstBinding = 3; writeDescriptorSets[3].pImageInfo = &textures.prefilteredCube.descriptor; writeDescriptorSets[4].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[4].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSets[4].descriptorCount = 1; writeDescriptorSets[4].dstSet = descriptorSets[i].scene; writeDescriptorSets[4].dstBinding = 4; writeDescriptorSets[4].pImageInfo = &textures.lutBrdf.descriptor; vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); } } // Material (samplers) { std::vector setLayoutBindings = { { 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, { 4, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, }; VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI{}; descriptorSetLayoutCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetLayoutCI.pBindings = setLayoutBindings.data(); descriptorSetLayoutCI.bindingCount = static_cast(setLayoutBindings.size()); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.material)); // Per-Material descriptor sets for (auto &material : models.scene.materials) { VkDescriptorSetAllocateInfo descriptorSetAllocInfo{}; descriptorSetAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; descriptorSetAllocInfo.descriptorPool = descriptorPool; descriptorSetAllocInfo.pSetLayouts = &descriptorSetLayouts.material; descriptorSetAllocInfo.descriptorSetCount = 1; VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &material.descriptorSet)); std::vector imageDescriptors = { textures.empty.descriptor, textures.empty.descriptor, material.normalTexture ? material.normalTexture->descriptor : textures.empty.descriptor, material.occlusionTexture ? material.occlusionTexture->descriptor : textures.empty.descriptor, material.emissiveTexture ? material.emissiveTexture->descriptor : textures.empty.descriptor }; if (material.pbrWorkflows.metallicRoughness) { if (material.baseColorTexture) { imageDescriptors[0] = material.baseColorTexture->descriptor; } if (material.metallicRoughnessTexture) { imageDescriptors[1] = material.metallicRoughnessTexture->descriptor; } } else { if (material.pbrWorkflows.specularGlossiness) { if (material.extension.diffuseTexture) { imageDescriptors[0] = material.extension.diffuseTexture->descriptor; } if (material.extension.specularGlossinessTexture) { imageDescriptors[1] = material.extension.specularGlossinessTexture->descriptor; } } } std::array writeDescriptorSets{}; for (size_t i = 0; i < imageDescriptors.size(); i++) { writeDescriptorSets[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[i].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSets[i].descriptorCount = 1; writeDescriptorSets[i].dstSet = material.descriptorSet; writeDescriptorSets[i].dstBinding = static_cast(i); writeDescriptorSets[i].pImageInfo = &imageDescriptors[i]; } vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); } // Material buffer { std::vector setLayoutBindings = { { 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }, }; VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI{}; descriptorSetLayoutCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetLayoutCI.pBindings = setLayoutBindings.data(); descriptorSetLayoutCI.bindingCount = static_cast(setLayoutBindings.size()); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.materialBuffer)); VkDescriptorSetAllocateInfo descriptorSetAllocInfo{}; descriptorSetAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; descriptorSetAllocInfo.descriptorPool = descriptorPool; descriptorSetAllocInfo.pSetLayouts = &descriptorSetLayouts.materialBuffer; descriptorSetAllocInfo.descriptorSetCount = 1; VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &descriptorSetMaterials)); VkWriteDescriptorSet writeDescriptorSet{}; writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; writeDescriptorSet.descriptorCount = 1; writeDescriptorSet.dstSet = descriptorSetMaterials; writeDescriptorSet.dstBinding = 0; writeDescriptorSet.pBufferInfo = &shaderMaterialBuffer.descriptor; vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr); } // Mesh data buffer { std::vector setLayoutBindings = { { 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT, nullptr }, }; VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI{}; descriptorSetLayoutCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetLayoutCI.pBindings = setLayoutBindings.data(); descriptorSetLayoutCI.bindingCount = static_cast(setLayoutBindings.size()); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorSetLayouts.meshDataBuffer)); for (auto i = 0; i < descriptorSetsMeshData.size(); i++) { VkDescriptorSetAllocateInfo descriptorSetAllocInfo{}; descriptorSetAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; descriptorSetAllocInfo.descriptorPool = descriptorPool; descriptorSetAllocInfo.pSetLayouts = &descriptorSetLayouts.meshDataBuffer; descriptorSetAllocInfo.descriptorSetCount = 1; VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &descriptorSetsMeshData[i])); VkWriteDescriptorSet writeDescriptorSet{}; writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; writeDescriptorSet.descriptorCount = 1; writeDescriptorSet.dstSet = descriptorSetsMeshData[i]; writeDescriptorSet.dstBinding = 0; writeDescriptorSet.pBufferInfo = &shaderMeshDataBuffers[i].descriptor; vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr); } } } // Skybox (fixed set) for (auto i = 0; i < uniformBuffers.size(); i++) { VkDescriptorSetAllocateInfo descriptorSetAllocInfo{}; descriptorSetAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; descriptorSetAllocInfo.descriptorPool = descriptorPool; descriptorSetAllocInfo.pSetLayouts = &descriptorSetLayouts.scene; descriptorSetAllocInfo.descriptorSetCount = 1; VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &descriptorSets[i].skybox)); std::array writeDescriptorSets{}; writeDescriptorSets[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; writeDescriptorSets[0].descriptorCount = 1; writeDescriptorSets[0].dstSet = descriptorSets[i].skybox; writeDescriptorSets[0].dstBinding = 0; writeDescriptorSets[0].pBufferInfo = &uniformBuffers[i].skybox.descriptor; writeDescriptorSets[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; writeDescriptorSets[1].descriptorCount = 1; writeDescriptorSets[1].dstSet = descriptorSets[i].skybox; writeDescriptorSets[1].dstBinding = 1; writeDescriptorSets[1].pBufferInfo = &uniformBuffers[i].params.descriptor; writeDescriptorSets[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSets[2].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSets[2].descriptorCount = 1; writeDescriptorSets[2].dstSet = descriptorSets[i].skybox; writeDescriptorSets[2].dstBinding = 2; writeDescriptorSets[2].pImageInfo = &textures.prefilteredCube.descriptor; vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr); } } // Depending on material setting, we need different pipeline variants per set, e.g. one with back-face culling, one without and one with alpha-blending enabled. This function generates such a set. void addPipelineSet(const std::string prefix, const std::string vertexShader, const std::string fragmentShader) { VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI{}; inputAssemblyStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; inputAssemblyStateCI.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; VkPipelineRasterizationStateCreateInfo rasterizationStateCI{}; rasterizationStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizationStateCI.polygonMode = VK_POLYGON_MODE_FILL; rasterizationStateCI.cullMode = VK_CULL_MODE_BACK_BIT; rasterizationStateCI.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; rasterizationStateCI.lineWidth = 1.0f; VkPipelineColorBlendAttachmentState blendAttachmentState{}; blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; blendAttachmentState.blendEnable = VK_FALSE; VkPipelineColorBlendStateCreateInfo colorBlendStateCI{}; colorBlendStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; colorBlendStateCI.attachmentCount = 1; colorBlendStateCI.pAttachments = &blendAttachmentState; VkPipelineDepthStencilStateCreateInfo depthStencilStateCI{}; depthStencilStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; depthStencilStateCI.depthTestEnable = (prefix == "skybox" ? VK_FALSE : VK_TRUE); depthStencilStateCI.depthWriteEnable = (prefix == "skybox" ? VK_FALSE : VK_TRUE); depthStencilStateCI.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; depthStencilStateCI.front = depthStencilStateCI.back; depthStencilStateCI.back.compareOp = VK_COMPARE_OP_ALWAYS; VkPipelineViewportStateCreateInfo viewportStateCI{}; viewportStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportStateCI.viewportCount = 1; viewportStateCI.scissorCount = 1; VkPipelineMultisampleStateCreateInfo multisampleStateCI{}; multisampleStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; if (settings.multiSampling) { multisampleStateCI.rasterizationSamples = settings.sampleCount; } std::vector dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; VkPipelineDynamicStateCreateInfo dynamicStateCI{}; dynamicStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; dynamicStateCI.pDynamicStates = dynamicStateEnables.data(); dynamicStateCI.dynamicStateCount = static_cast(dynamicStateEnables.size()); // Pipeline layout const std::vector setLayouts = { descriptorSetLayouts.scene, descriptorSetLayouts.material, descriptorSetLayouts.meshDataBuffer, descriptorSetLayouts.materialBuffer }; VkPipelineLayoutCreateInfo pipelineLayoutCI{}; pipelineLayoutCI.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutCI.setLayoutCount = static_cast(setLayouts.size()); pipelineLayoutCI.pSetLayouts = setLayouts.data(); VkPushConstantRange pushConstantRange{}; pushConstantRange.size = sizeof(MeshPushConstantBlock); pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; pipelineLayoutCI.pushConstantRangeCount = 1; pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelineLayout)); // Vertex bindings and attributes VkVertexInputBindingDescription vertexInputBinding = { 0, sizeof(vkglTF::Model::Vertex), VK_VERTEX_INPUT_RATE_VERTEX }; std::vector vertexInputAttributes = { { 0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(vkglTF::Model::Vertex, pos)}, { 1, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(vkglTF::Model::Vertex, normal) }, { 2, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(vkglTF::Model::Vertex, uv0) }, { 3, 0, VK_FORMAT_R32G32_SFLOAT, offsetof(vkglTF::Model::Vertex, uv1) }, { 4, 0, VK_FORMAT_R32G32B32A32_UINT, offsetof(vkglTF::Model::Vertex, joint0) }, { 5, 0, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(vkglTF::Model::Vertex, weight0) }, { 6, 0, VK_FORMAT_R32G32B32A32_SFLOAT, offsetof(vkglTF::Model::Vertex, color) } }; VkPipelineVertexInputStateCreateInfo vertexInputStateCI{}; vertexInputStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputStateCI.vertexBindingDescriptionCount = 1; vertexInputStateCI.pVertexBindingDescriptions = &vertexInputBinding; vertexInputStateCI.vertexAttributeDescriptionCount = static_cast(vertexInputAttributes.size()); vertexInputStateCI.pVertexAttributeDescriptions = vertexInputAttributes.data(); // Pipelines std::array shaderStages; VkGraphicsPipelineCreateInfo pipelineCI{}; pipelineCI.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineCI.layout = pipelineLayout; pipelineCI.renderPass = renderPass; pipelineCI.pInputAssemblyState = &inputAssemblyStateCI; pipelineCI.pVertexInputState = &vertexInputStateCI; pipelineCI.pRasterizationState = &rasterizationStateCI; pipelineCI.pColorBlendState = &colorBlendStateCI; pipelineCI.pMultisampleState = &multisampleStateCI; pipelineCI.pViewportState = &viewportStateCI; pipelineCI.pDepthStencilState = &depthStencilStateCI; pipelineCI.pDynamicState = &dynamicStateCI; pipelineCI.stageCount = static_cast(shaderStages.size()); pipelineCI.pStages = shaderStages.data(); shaderStages[0] = loadShader(device, vertexShader, VK_SHADER_STAGE_VERTEX_BIT); shaderStages[1] = loadShader(device, fragmentShader, VK_SHADER_STAGE_FRAGMENT_BIT); VkPipeline pipeline{}; // Default pipeline with back-face culling VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline)); pipelines[prefix] = pipeline; // Double sided rasterizationStateCI.cullMode = VK_CULL_MODE_NONE; VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline)); pipelines[prefix + "_double_sided"] = pipeline; // Alpha blending rasterizationStateCI.cullMode = VK_CULL_MODE_NONE; blendAttachmentState.blendEnable = VK_TRUE; blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA; blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD; blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD; VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline)); pipelines[prefix + "_alpha_blending"] = pipeline; for (auto shaderStage : shaderStages) { vkDestroyShaderModule(device, shaderStage.module, nullptr); } }; void preparePipelines() { // Skybox pipeline (background cube) addPipelineSet("skybox", "skybox.vert.spv", "skybox.frag.spv"); // PBR pipelines addPipelineSet("pbr", "pbr.vert.spv", "material_pbr.frag.spv"); // KHR_materials_unlit addPipelineSet("unlit", "pbr.vert.spv", "material_unlit.frag.spv"); } /* Generate a BRDF integration map storing roughness/NdotV as a look-up-table */ void generateBRDFLUT() { auto tStart = std::chrono::high_resolution_clock::now(); const VkFormat format = VK_FORMAT_R16G16_SFLOAT; const int32_t dim = 512; // Image VkImageCreateInfo imageCI{}; imageCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageCI.imageType = VK_IMAGE_TYPE_2D; imageCI.format = format; imageCI.extent.width = dim; imageCI.extent.height = dim; imageCI.extent.depth = 1; imageCI.mipLevels = 1; imageCI.arrayLayers = 1; imageCI.samples = VK_SAMPLE_COUNT_1_BIT; imageCI.tiling = VK_IMAGE_TILING_OPTIMAL; imageCI.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; VK_CHECK_RESULT(vkCreateImage(device, &imageCI, nullptr, &textures.lutBrdf.image)); VkMemoryRequirements memReqs; vkGetImageMemoryRequirements(device, textures.lutBrdf.image, &memReqs); VkMemoryAllocateInfo memAllocInfo{}; memAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; memAllocInfo.allocationSize = memReqs.size; memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &textures.lutBrdf.deviceMemory)); VK_CHECK_RESULT(vkBindImageMemory(device, textures.lutBrdf.image, textures.lutBrdf.deviceMemory, 0)); // View VkImageViewCreateInfo viewCI{}; viewCI.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; viewCI.viewType = VK_IMAGE_VIEW_TYPE_2D; viewCI.format = format; viewCI.subresourceRange = {}; viewCI.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; viewCI.subresourceRange.levelCount = 1; viewCI.subresourceRange.layerCount = 1; viewCI.image = textures.lutBrdf.image; VK_CHECK_RESULT(vkCreateImageView(device, &viewCI, nullptr, &textures.lutBrdf.view)); // Sampler VkSamplerCreateInfo samplerCI{}; samplerCI.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerCI.magFilter = VK_FILTER_LINEAR; samplerCI.minFilter = VK_FILTER_LINEAR; 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.minLod = 0.0f; samplerCI.maxLod = 1.0f; samplerCI.maxAnisotropy = 1.0f; samplerCI.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; VK_CHECK_RESULT(vkCreateSampler(device, &samplerCI, nullptr, &textures.lutBrdf.sampler)); // FB, Att, RP, Pipe, etc. VkAttachmentDescription attDesc{}; // Color attachment attDesc.format = format; attDesc.samples = VK_SAMPLE_COUNT_1_BIT; attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attDesc.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; VkAttachmentReference colorReference = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; VkSubpassDescription subpassDescription{}; subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpassDescription.colorAttachmentCount = 1; subpassDescription.pColorAttachments = &colorReference; // Use subpass dependencies for layout transitions std::array dependencies; dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; dependencies[0].dstSubpass = 0; dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT; dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | 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_BOTTOM_OF_PIPE_BIT; dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; dependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; // Create the actual renderpass VkRenderPassCreateInfo renderPassCI{}; renderPassCI.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; renderPassCI.attachmentCount = 1; renderPassCI.pAttachments = &attDesc; renderPassCI.subpassCount = 1; renderPassCI.pSubpasses = &subpassDescription; renderPassCI.dependencyCount = 2; renderPassCI.pDependencies = dependencies.data(); VkRenderPass renderpass; VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCI, nullptr, &renderpass)); VkFramebufferCreateInfo framebufferCI{}; framebufferCI.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferCI.renderPass = renderpass; framebufferCI.attachmentCount = 1; framebufferCI.pAttachments = &textures.lutBrdf.view; framebufferCI.width = dim; framebufferCI.height = dim; framebufferCI.layers = 1; VkFramebuffer framebuffer; VK_CHECK_RESULT(vkCreateFramebuffer(device, &framebufferCI, nullptr, &framebuffer)); // Desriptors VkDescriptorSetLayout descriptorsetlayout; VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI{}; descriptorSetLayoutCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorsetlayout)); // Pipeline layout VkPipelineLayout pipelinelayout; VkPipelineLayoutCreateInfo pipelineLayoutCI{}; pipelineLayoutCI.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutCI.setLayoutCount = 1; pipelineLayoutCI.pSetLayouts = &descriptorsetlayout; VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelinelayout)); // Pipeline VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI{}; inputAssemblyStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; inputAssemblyStateCI.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; VkPipelineRasterizationStateCreateInfo rasterizationStateCI{}; rasterizationStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizationStateCI.polygonMode = VK_POLYGON_MODE_FILL; rasterizationStateCI.cullMode = VK_CULL_MODE_NONE; rasterizationStateCI.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; rasterizationStateCI.lineWidth = 1.0f; VkPipelineColorBlendAttachmentState blendAttachmentState{}; blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; blendAttachmentState.blendEnable = VK_FALSE; VkPipelineColorBlendStateCreateInfo colorBlendStateCI{}; colorBlendStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; colorBlendStateCI.attachmentCount = 1; colorBlendStateCI.pAttachments = &blendAttachmentState; VkPipelineDepthStencilStateCreateInfo depthStencilStateCI{}; depthStencilStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; depthStencilStateCI.depthTestEnable = VK_FALSE; depthStencilStateCI.depthWriteEnable = VK_FALSE; depthStencilStateCI.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; depthStencilStateCI.front = depthStencilStateCI.back; depthStencilStateCI.back.compareOp = VK_COMPARE_OP_ALWAYS; VkPipelineViewportStateCreateInfo viewportStateCI{}; viewportStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportStateCI.viewportCount = 1; viewportStateCI.scissorCount = 1; VkPipelineMultisampleStateCreateInfo multisampleStateCI{}; multisampleStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; multisampleStateCI.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; std::vector dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; VkPipelineDynamicStateCreateInfo dynamicStateCI{}; dynamicStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; dynamicStateCI.pDynamicStates = dynamicStateEnables.data(); dynamicStateCI.dynamicStateCount = static_cast(dynamicStateEnables.size()); VkPipelineVertexInputStateCreateInfo emptyInputStateCI{}; emptyInputStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; std::array shaderStages; VkGraphicsPipelineCreateInfo pipelineCI{}; pipelineCI.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineCI.layout = pipelinelayout; pipelineCI.renderPass = renderpass; pipelineCI.pInputAssemblyState = &inputAssemblyStateCI; pipelineCI.pVertexInputState = &emptyInputStateCI; pipelineCI.pRasterizationState = &rasterizationStateCI; pipelineCI.pColorBlendState = &colorBlendStateCI; pipelineCI.pMultisampleState = &multisampleStateCI; pipelineCI.pViewportState = &viewportStateCI; pipelineCI.pDepthStencilState = &depthStencilStateCI; pipelineCI.pDynamicState = &dynamicStateCI; pipelineCI.stageCount = 2; pipelineCI.pStages = shaderStages.data(); // Look-up-table (from BRDF) pipeline shaderStages = { loadShader(device, "genbrdflut.vert.spv", VK_SHADER_STAGE_VERTEX_BIT), loadShader(device, "genbrdflut.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT) }; VkPipeline pipeline; VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline)); for (auto shaderStage : shaderStages) { vkDestroyShaderModule(device, shaderStage.module, nullptr); } // Render VkClearValue clearValues[1]; clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } }; VkRenderPassBeginInfo renderPassBeginInfo{}; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.renderPass = renderpass; renderPassBeginInfo.renderArea.extent.width = dim; renderPassBeginInfo.renderArea.extent.height = dim; renderPassBeginInfo.clearValueCount = 1; renderPassBeginInfo.pClearValues = clearValues; renderPassBeginInfo.framebuffer = framebuffer; VkCommandBuffer cmdBuf = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); vkCmdBeginRenderPass(cmdBuf, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); VkViewport viewport{}; viewport.width = (float)dim; viewport.height = (float)dim; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; VkRect2D scissor{}; scissor.extent.width = dim; scissor.extent.height = dim; vkCmdSetViewport(cmdBuf, 0, 1, &viewport); vkCmdSetScissor(cmdBuf, 0, 1, &scissor); vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); vkCmdDraw(cmdBuf, 3, 1, 0, 0); vkCmdEndRenderPass(cmdBuf); vulkanDevice->flushCommandBuffer(cmdBuf, queue); vkQueueWaitIdle(queue); vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipelineLayout(device, pipelinelayout, nullptr); vkDestroyRenderPass(device, renderpass, nullptr); vkDestroyFramebuffer(device, framebuffer, nullptr); vkDestroyDescriptorSetLayout(device, descriptorsetlayout, nullptr); textures.lutBrdf.descriptor.imageView = textures.lutBrdf.view; textures.lutBrdf.descriptor.sampler = textures.lutBrdf.sampler; textures.lutBrdf.descriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; textures.lutBrdf.device = vulkanDevice; auto tEnd = std::chrono::high_resolution_clock::now(); auto tDiff = std::chrono::duration(tEnd - tStart).count(); std::cout << "Generating BRDF LUT took " << tDiff << " ms" << std::endl; } /* Offline generation for the cube maps used for PBR lighting - Irradiance cube map - Pre-filterd environment cubemap */ void generateCubemaps() { enum Target { IRRADIANCE = 0, PREFILTEREDENV = 1 }; for (uint32_t target = 0; target < PREFILTEREDENV + 1; target++) { vks::TextureCubeMap cubemap; auto tStart = std::chrono::high_resolution_clock::now(); VkFormat format; int32_t dim; switch (target) { case IRRADIANCE: format = VK_FORMAT_R32G32B32A32_SFLOAT; dim = 64; break; case PREFILTEREDENV: format = VK_FORMAT_R16G16B16A16_SFLOAT; dim = 512; break; }; const uint32_t numMips = static_cast(floor(log2(dim))) + 1; // Create target cubemap { // Image VkImageCreateInfo imageCI{}; imageCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageCI.imageType = VK_IMAGE_TYPE_2D; imageCI.format = format; imageCI.extent.width = dim; imageCI.extent.height = dim; imageCI.extent.depth = 1; imageCI.mipLevels = numMips; imageCI.arrayLayers = 6; imageCI.samples = VK_SAMPLE_COUNT_1_BIT; imageCI.tiling = VK_IMAGE_TILING_OPTIMAL; imageCI.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; imageCI.flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT; VK_CHECK_RESULT(vkCreateImage(device, &imageCI, nullptr, &cubemap.image)); VkMemoryRequirements memReqs; vkGetImageMemoryRequirements(device, cubemap.image, &memReqs); VkMemoryAllocateInfo memAllocInfo{}; memAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; memAllocInfo.allocationSize = memReqs.size; memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &cubemap.deviceMemory)); VK_CHECK_RESULT(vkBindImageMemory(device, cubemap.image, cubemap.deviceMemory, 0)); // View VkImageViewCreateInfo viewCI{}; viewCI.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; viewCI.viewType = VK_IMAGE_VIEW_TYPE_CUBE; viewCI.format = format; viewCI.subresourceRange = {}; viewCI.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; viewCI.subresourceRange.levelCount = numMips; viewCI.subresourceRange.layerCount = 6; viewCI.image = cubemap.image; VK_CHECK_RESULT(vkCreateImageView(device, &viewCI, nullptr, &cubemap.view)); // Sampler VkSamplerCreateInfo samplerCI{}; samplerCI.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; samplerCI.magFilter = VK_FILTER_LINEAR; samplerCI.minFilter = VK_FILTER_LINEAR; 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.minLod = 0.0f; samplerCI.maxLod = static_cast(numMips); samplerCI.maxAnisotropy = 1.0f; samplerCI.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; VK_CHECK_RESULT(vkCreateSampler(device, &samplerCI, nullptr, &cubemap.sampler)); } // FB, Att, RP, Pipe, etc. VkAttachmentDescription attDesc{}; // Color attachment attDesc.format = format; attDesc.samples = VK_SAMPLE_COUNT_1_BIT; attDesc.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; attDesc.storeOp = VK_ATTACHMENT_STORE_OP_STORE; attDesc.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; attDesc.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; attDesc.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; attDesc.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; VkAttachmentReference colorReference = { 0, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL }; VkSubpassDescription subpassDescription{}; subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpassDescription.colorAttachmentCount = 1; subpassDescription.pColorAttachments = &colorReference; // Use subpass dependencies for layout transitions std::array dependencies; dependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL; dependencies[0].dstSubpass = 0; dependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; dependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; dependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT; dependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | 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_BOTTOM_OF_PIPE_BIT; dependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; dependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; dependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT; // Renderpass VkRenderPassCreateInfo renderPassCI{}; renderPassCI.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; renderPassCI.attachmentCount = 1; renderPassCI.pAttachments = &attDesc; renderPassCI.subpassCount = 1; renderPassCI.pSubpasses = &subpassDescription; renderPassCI.dependencyCount = 2; renderPassCI.pDependencies = dependencies.data(); VkRenderPass renderpass; VK_CHECK_RESULT(vkCreateRenderPass(device, &renderPassCI, nullptr, &renderpass)); struct Offscreen { VkImage image; VkImageView view; VkDeviceMemory memory; VkFramebuffer framebuffer; } offscreen; // Create offscreen framebuffer { // Image VkImageCreateInfo imageCI{}; imageCI.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageCI.imageType = VK_IMAGE_TYPE_2D; imageCI.format = format; imageCI.extent.width = dim; imageCI.extent.height = dim; imageCI.extent.depth = 1; imageCI.mipLevels = 1; imageCI.arrayLayers = 1; imageCI.samples = VK_SAMPLE_COUNT_1_BIT; imageCI.tiling = VK_IMAGE_TILING_OPTIMAL; imageCI.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; imageCI.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT; imageCI.sharingMode = VK_SHARING_MODE_EXCLUSIVE; VK_CHECK_RESULT(vkCreateImage(device, &imageCI, nullptr, &offscreen.image)); VkMemoryRequirements memReqs; vkGetImageMemoryRequirements(device, offscreen.image, &memReqs); VkMemoryAllocateInfo memAllocInfo{}; memAllocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; memAllocInfo.allocationSize = memReqs.size; memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &offscreen.memory)); VK_CHECK_RESULT(vkBindImageMemory(device, offscreen.image, offscreen.memory, 0)); // View VkImageViewCreateInfo viewCI{}; viewCI.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; viewCI.viewType = VK_IMAGE_VIEW_TYPE_2D; viewCI.format = format; viewCI.flags = 0; viewCI.subresourceRange = {}; viewCI.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; viewCI.subresourceRange.baseMipLevel = 0; viewCI.subresourceRange.levelCount = 1; viewCI.subresourceRange.baseArrayLayer = 0; viewCI.subresourceRange.layerCount = 1; viewCI.image = offscreen.image; VK_CHECK_RESULT(vkCreateImageView(device, &viewCI, nullptr, &offscreen.view)); // Framebuffer VkFramebufferCreateInfo framebufferCI{}; framebufferCI.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; framebufferCI.renderPass = renderpass; framebufferCI.attachmentCount = 1; framebufferCI.pAttachments = &offscreen.view; framebufferCI.width = dim; framebufferCI.height = dim; framebufferCI.layers = 1; VK_CHECK_RESULT(vkCreateFramebuffer(device, &framebufferCI, nullptr, &offscreen.framebuffer)); VkCommandBuffer layoutCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkImageMemoryBarrier imageMemoryBarrier{}; imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; imageMemoryBarrier.image = offscreen.image; imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; imageMemoryBarrier.srcAccessMask = 0; imageMemoryBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; imageMemoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; vkCmdPipelineBarrier(layoutCmd, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier); vulkanDevice->flushCommandBuffer(layoutCmd, queue, true); } // Descriptors VkDescriptorSetLayout descriptorsetlayout; VkDescriptorSetLayoutBinding setLayoutBinding = { 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr }; VkDescriptorSetLayoutCreateInfo descriptorSetLayoutCI{}; descriptorSetLayoutCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; descriptorSetLayoutCI.pBindings = &setLayoutBinding; descriptorSetLayoutCI.bindingCount = 1; VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorSetLayoutCI, nullptr, &descriptorsetlayout)); // Descriptor Pool VkDescriptorPoolSize poolSize = { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1 }; VkDescriptorPoolCreateInfo descriptorPoolCI{}; descriptorPoolCI.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; descriptorPoolCI.poolSizeCount = 1; descriptorPoolCI.pPoolSizes = &poolSize; descriptorPoolCI.maxSets = 2; VkDescriptorPool descriptorpool; VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolCI, nullptr, &descriptorpool)); // Descriptor sets VkDescriptorSet descriptorset; VkDescriptorSetAllocateInfo descriptorSetAllocInfo{}; descriptorSetAllocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; descriptorSetAllocInfo.descriptorPool = descriptorpool; descriptorSetAllocInfo.pSetLayouts = &descriptorsetlayout; descriptorSetAllocInfo.descriptorSetCount = 1; VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &descriptorSetAllocInfo, &descriptorset)); VkWriteDescriptorSet writeDescriptorSet{}; writeDescriptorSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; writeDescriptorSet.descriptorCount = 1; writeDescriptorSet.dstSet = descriptorset; writeDescriptorSet.dstBinding = 0; writeDescriptorSet.pImageInfo = &textures.environmentCube.descriptor; vkUpdateDescriptorSets(device, 1, &writeDescriptorSet, 0, nullptr); struct PushBlockIrradiance { glm::mat4 mvp; float deltaPhi = (2.0f * float(M_PI)) / 180.0f; float deltaTheta = (0.5f * float(M_PI)) / 64.0f; } pushBlockIrradiance; struct PushBlockPrefilterEnv { glm::mat4 mvp; float roughness; uint32_t numSamples = 32u; } pushBlockPrefilterEnv; // Pipeline layout VkPipelineLayout pipelinelayout; VkPushConstantRange pushConstantRange{}; pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT; switch (target) { case IRRADIANCE: pushConstantRange.size = sizeof(PushBlockIrradiance); break; case PREFILTEREDENV: pushConstantRange.size = sizeof(PushBlockPrefilterEnv); break; }; VkPipelineLayoutCreateInfo pipelineLayoutCI{}; pipelineLayoutCI.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; pipelineLayoutCI.setLayoutCount = 1; pipelineLayoutCI.pSetLayouts = &descriptorsetlayout; pipelineLayoutCI.pushConstantRangeCount = 1; pipelineLayoutCI.pPushConstantRanges = &pushConstantRange; VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCI, nullptr, &pipelinelayout)); // Pipeline VkPipelineInputAssemblyStateCreateInfo inputAssemblyStateCI{}; inputAssemblyStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; inputAssemblyStateCI.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; VkPipelineRasterizationStateCreateInfo rasterizationStateCI{}; rasterizationStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; rasterizationStateCI.polygonMode = VK_POLYGON_MODE_FILL; rasterizationStateCI.cullMode = VK_CULL_MODE_NONE; rasterizationStateCI.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; rasterizationStateCI.lineWidth = 1.0f; VkPipelineColorBlendAttachmentState blendAttachmentState{}; blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT; blendAttachmentState.blendEnable = VK_FALSE; VkPipelineColorBlendStateCreateInfo colorBlendStateCI{}; colorBlendStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; colorBlendStateCI.attachmentCount = 1; colorBlendStateCI.pAttachments = &blendAttachmentState; VkPipelineDepthStencilStateCreateInfo depthStencilStateCI{}; depthStencilStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; depthStencilStateCI.depthTestEnable = VK_FALSE; depthStencilStateCI.depthWriteEnable = VK_FALSE; depthStencilStateCI.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL; depthStencilStateCI.front = depthStencilStateCI.back; depthStencilStateCI.back.compareOp = VK_COMPARE_OP_ALWAYS; VkPipelineViewportStateCreateInfo viewportStateCI{}; viewportStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; viewportStateCI.viewportCount = 1; viewportStateCI.scissorCount = 1; VkPipelineMultisampleStateCreateInfo multisampleStateCI{}; multisampleStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; multisampleStateCI.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT; std::vector dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR }; VkPipelineDynamicStateCreateInfo dynamicStateCI{}; dynamicStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; dynamicStateCI.pDynamicStates = dynamicStateEnables.data(); dynamicStateCI.dynamicStateCount = static_cast(dynamicStateEnables.size()); // Vertex input state VkVertexInputBindingDescription vertexInputBinding = { 0, sizeof(vkglTF::Model::Vertex), VK_VERTEX_INPUT_RATE_VERTEX }; VkVertexInputAttributeDescription vertexInputAttribute = { 0, 0, VK_FORMAT_R32G32B32_SFLOAT, 0 }; VkPipelineVertexInputStateCreateInfo vertexInputStateCI{}; vertexInputStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; vertexInputStateCI.vertexBindingDescriptionCount = 1; vertexInputStateCI.pVertexBindingDescriptions = &vertexInputBinding; vertexInputStateCI.vertexAttributeDescriptionCount = 1; vertexInputStateCI.pVertexAttributeDescriptions = &vertexInputAttribute; std::array shaderStages; VkGraphicsPipelineCreateInfo pipelineCI{}; pipelineCI.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineCI.layout = pipelinelayout; pipelineCI.renderPass = renderpass; pipelineCI.pInputAssemblyState = &inputAssemblyStateCI; pipelineCI.pVertexInputState = &vertexInputStateCI; pipelineCI.pRasterizationState = &rasterizationStateCI; pipelineCI.pColorBlendState = &colorBlendStateCI; pipelineCI.pMultisampleState = &multisampleStateCI; pipelineCI.pViewportState = &viewportStateCI; pipelineCI.pDepthStencilState = &depthStencilStateCI; pipelineCI.pDynamicState = &dynamicStateCI; pipelineCI.stageCount = 2; pipelineCI.pStages = shaderStages.data(); pipelineCI.renderPass = renderpass; shaderStages[0] = loadShader(device, "filtercube.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); switch (target) { case IRRADIANCE: shaderStages[1] = loadShader(device, "irradiancecube.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); break; case PREFILTEREDENV: shaderStages[1] = loadShader(device, "prefilterenvmap.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); break; }; VkPipeline pipeline; VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline)); for (auto shaderStage : shaderStages) { vkDestroyShaderModule(device, shaderStage.module, nullptr); } // Render cubemap VkClearValue clearValues[1]; clearValues[0].color = { { 0.0f, 0.0f, 0.2f, 0.0f } }; VkRenderPassBeginInfo renderPassBeginInfo{}; renderPassBeginInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; renderPassBeginInfo.renderPass = renderpass; renderPassBeginInfo.framebuffer = offscreen.framebuffer; renderPassBeginInfo.renderArea.extent.width = dim; renderPassBeginInfo.renderArea.extent.height = dim; renderPassBeginInfo.clearValueCount = 1; renderPassBeginInfo.pClearValues = clearValues; std::vector matrices = { glm::rotate(glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f)), glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f)), glm::rotate(glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(0.0f, 1.0f, 0.0f)), glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f)), glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)), glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f)), glm::rotate(glm::mat4(1.0f), glm::radians(180.0f), glm::vec3(1.0f, 0.0f, 0.0f)), glm::rotate(glm::mat4(1.0f), glm::radians(180.0f), glm::vec3(0.0f, 0.0f, 1.0f)), }; VkCommandBuffer cmdBuf = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, false); VkViewport viewport{}; viewport.width = (float)dim; viewport.height = (float)dim; viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; VkRect2D scissor{}; scissor.extent.width = dim; scissor.extent.height = dim; VkImageSubresourceRange subresourceRange{}; subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; subresourceRange.baseMipLevel = 0; subresourceRange.levelCount = numMips; subresourceRange.layerCount = 6; // Change image layout for all cubemap faces to transfer destination { vulkanDevice->beginCommandBuffer(cmdBuf); VkImageMemoryBarrier imageMemoryBarrier{}; imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; imageMemoryBarrier.image = cubemap.image; imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; imageMemoryBarrier.srcAccessMask = 0; imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; imageMemoryBarrier.subresourceRange = subresourceRange; vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier); vulkanDevice->flushCommandBuffer(cmdBuf, queue, false); } for (uint32_t m = 0; m < numMips; m++) { for (uint32_t f = 0; f < 6; f++) { vulkanDevice->beginCommandBuffer(cmdBuf); viewport.width = static_cast(dim * std::pow(0.5f, m)); viewport.height = static_cast(dim * std::pow(0.5f, m)); vkCmdSetViewport(cmdBuf, 0, 1, &viewport); vkCmdSetScissor(cmdBuf, 0, 1, &scissor); // Render scene from cube face's point of view vkCmdBeginRenderPass(cmdBuf, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); // Pass parameters for current pass using a push constant block switch (target) { case IRRADIANCE: pushBlockIrradiance.mvp = glm::perspective((float)(M_PI / 2.0), 1.0f, 0.1f, 512.0f) * matrices[f]; vkCmdPushConstants(cmdBuf, pipelinelayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushBlockIrradiance), &pushBlockIrradiance); break; case PREFILTEREDENV: pushBlockPrefilterEnv.mvp = glm::perspective((float)(M_PI / 2.0), 1.0f, 0.1f, 512.0f) * matrices[f]; pushBlockPrefilterEnv.roughness = (float)m / (float)(numMips - 1); vkCmdPushConstants(cmdBuf, pipelinelayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PushBlockPrefilterEnv), &pushBlockPrefilterEnv); break; }; vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); vkCmdBindDescriptorSets(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelinelayout, 0, 1, &descriptorset, 0, NULL); VkDeviceSize offsets[1] = { 0 }; models.skybox.draw(cmdBuf); vkCmdEndRenderPass(cmdBuf); VkImageSubresourceRange subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; subresourceRange.baseMipLevel = 0; subresourceRange.levelCount = numMips; subresourceRange.layerCount = 6; { VkImageMemoryBarrier imageMemoryBarrier{}; imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; imageMemoryBarrier.image = offscreen.image; imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; imageMemoryBarrier.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; imageMemoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier); } // Copy region for transfer from framebuffer to cube face VkImageCopy copyRegion{}; copyRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; copyRegion.srcSubresource.baseArrayLayer = 0; copyRegion.srcSubresource.mipLevel = 0; copyRegion.srcSubresource.layerCount = 1; copyRegion.srcOffset = { 0, 0, 0 }; copyRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; copyRegion.dstSubresource.baseArrayLayer = f; copyRegion.dstSubresource.mipLevel = m; copyRegion.dstSubresource.layerCount = 1; copyRegion.dstOffset = { 0, 0, 0 }; copyRegion.extent.width = static_cast(viewport.width); copyRegion.extent.height = static_cast(viewport.height); copyRegion.extent.depth = 1; vkCmdCopyImage( cmdBuf, offscreen.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, cubemap.image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Region); { VkImageMemoryBarrier imageMemoryBarrier{}; imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; imageMemoryBarrier.image = offscreen.image; imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; imageMemoryBarrier.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; imageMemoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier); } vulkanDevice->flushCommandBuffer(cmdBuf, queue, false); } } { vulkanDevice->beginCommandBuffer(cmdBuf); VkImageMemoryBarrier imageMemoryBarrier{}; imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; imageMemoryBarrier.image = cubemap.image; imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; imageMemoryBarrier.dstAccessMask = VK_ACCESS_HOST_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT; imageMemoryBarrier.subresourceRange = subresourceRange; vkCmdPipelineBarrier(cmdBuf, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 0, nullptr, 0, nullptr, 1, &imageMemoryBarrier); vulkanDevice->flushCommandBuffer(cmdBuf, queue, false); } vkDestroyRenderPass(device, renderpass, nullptr); vkDestroyFramebuffer(device, offscreen.framebuffer, nullptr); vkFreeMemory(device, offscreen.memory, nullptr); vkDestroyImageView(device, offscreen.view, nullptr); vkDestroyImage(device, offscreen.image, nullptr); vkDestroyDescriptorPool(device, descriptorpool, nullptr); vkDestroyDescriptorSetLayout(device, descriptorsetlayout, nullptr); vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipelineLayout(device, pipelinelayout, nullptr); cubemap.descriptor.imageView = cubemap.view; cubemap.descriptor.sampler = cubemap.sampler; cubemap.descriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; cubemap.device = vulkanDevice; switch (target) { case IRRADIANCE: textures.irradianceCube = cubemap; break; case PREFILTEREDENV: textures.prefilteredCube = cubemap; shaderValuesParams.prefilteredCubeMipLevels = static_cast(numMips); break; }; auto tEnd = std::chrono::high_resolution_clock::now(); auto tDiff = std::chrono::duration(tEnd - tStart).count(); std::cout << "Generating cube map with " << numMips << " mip levels took " << tDiff << " ms" << std::endl; } } /* Prepare and initialize uniform buffers containing shader parameters */ void prepareUniformBuffers() { for (auto &uniformBuffer : uniformBuffers) { uniformBuffer.scene.create(vulkanDevice, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, sizeof(shaderValuesScene)); uniformBuffer.skybox.create(vulkanDevice, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, sizeof(shaderValuesSkybox)); uniformBuffer.params.create(vulkanDevice, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, sizeof(shaderValuesParams)); } updateUniformData(); } void updateUniformData() { // Scene shaderValuesScene.projection = camera.matrices.perspective; shaderValuesScene.view = camera.matrices.view; // Center and scale model float scale = (1.0f / std::max(models.scene.aabb[0][0], std::max(models.scene.aabb[1][1], models.scene.aabb[2][2]))) * 0.5f; glm::vec3 translate = -glm::vec3(models.scene.aabb[3][0], models.scene.aabb[3][1], models.scene.aabb[3][2]); translate += -0.5f * glm::vec3(models.scene.aabb[0][0], models.scene.aabb[1][1], models.scene.aabb[2][2]); shaderValuesScene.model = glm::mat4(1.0f); shaderValuesScene.model[0][0] = scale; shaderValuesScene.model[1][1] = scale; shaderValuesScene.model[2][2] = scale; shaderValuesScene.model = glm::translate(shaderValuesScene.model, translate); // Shader requires camera position in world space glm::mat4 cv = glm::inverse(camera.matrices.view); shaderValuesScene.camPos = glm::vec3(cv[3]); // Skybox shaderValuesSkybox.projection = camera.matrices.perspective; shaderValuesSkybox.view = camera.matrices.view; shaderValuesSkybox.model = glm::mat4(glm::mat3(camera.matrices.view)); } void updateParams() { shaderValuesParams.lightDir = glm::vec4( sin(glm::radians(lightSource.rotation.x)) * cos(glm::radians(lightSource.rotation.y)), sin(glm::radians(lightSource.rotation.y)), cos(glm::radians(lightSource.rotation.x)) * cos(glm::radians(lightSource.rotation.y)), 0.0f); } void windowResized() { vkDeviceWaitIdle(device); updateUniformData(); updateOverlay(); } void prepare() { VulkanExampleBase::prepare(); camera.type = Camera::CameraType::lookat; camera.setPerspective(45.0f, (float)width / (float)height, 0.01f, 256.0f); camera.rotationSpeed = 0.25f; camera.movementSpeed = 0.1f; camera.setPosition({ 0.0f, 0.0f, 1.0f }); camera.setRotation({ 0.0f, 0.0f, 0.0f }); waitFences.resize(renderAhead); presentCompleteSemaphores.resize(swapChain.imageCount); renderCompleteSemaphores.resize(swapChain.imageCount); commandBuffers.resize(renderAhead); uniformBuffers.resize(renderAhead); descriptorSets.resize(renderAhead); shaderMeshDataBuffers.resize(renderAhead); descriptorSetsMeshData.resize(renderAhead); // Command buffer execution fences for (auto &waitFence : waitFences) { VkFenceCreateInfo fenceCI{ VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, nullptr, VK_FENCE_CREATE_SIGNALED_BIT }; VK_CHECK_RESULT(vkCreateFence(device, &fenceCI, nullptr, &waitFence)); } // Queue ordering semaphores for (auto &semaphore : presentCompleteSemaphores) { VkSemaphoreCreateInfo semaphoreCI{ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0 }; VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCI, nullptr, &semaphore)); } for (auto &semaphore : renderCompleteSemaphores) { VkSemaphoreCreateInfo semaphoreCI{ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, nullptr, 0 }; VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCI, nullptr, &semaphore)); } // Command buffers { VkCommandBufferAllocateInfo cmdBufAllocateInfo{}; cmdBufAllocateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; cmdBufAllocateInfo.commandPool = cmdPool; cmdBufAllocateInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; cmdBufAllocateInfo.commandBufferCount = static_cast(commandBuffers.size()); VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, commandBuffers.data())); } loadAssets(); generateBRDFLUT(); prepareUniformBuffers(); setupDescriptors(); preparePipelines(); ui = new UI(vulkanDevice, renderPass, queue, pipelineCache, settings.sampleCount); updateOverlay(); prepared = true; } /* Update ImGui user interface */ void updateOverlay() { ImGuiIO& io = ImGui::GetIO(); ImVec2 lastDisplaySize = io.DisplaySize; io.DisplaySize = ImVec2((float)width, (float)height); io.DeltaTime = frameTimer; io.MousePos = ImVec2(mousePos.x, mousePos.y); io.MouseDown[0] = mouseButtons.left; io.MouseDown[1] = mouseButtons.right; ui->pushConstBlock.scale = glm::vec2(2.0f / io.DisplaySize.x, 2.0f / io.DisplaySize.y); ui->pushConstBlock.translate = glm::vec2(-1.0f); float scale = 1.0f; #if defined(VK_USE_PLATFORM_ANDROID_KHR) scale = (float)vks::android::screenDensity / (float)ACONFIGURATION_DENSITY_MEDIUM; #endif ImGui::NewFrame(); ImGui::SetNextWindowPos(ImVec2(10, 10)); ImGui::SetNextWindowSize(ImVec2(200 * scale, (models.scene.animations.size() > 0 ? 500 : 420) * scale), ImGuiSetCond_Always); ImGui::Begin("Vulkan glTF 2.0 PBR", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove); ImGui::PushItemWidth(100.0f * scale); ui->text("www.saschawillems.de"); ui->text("%.1d fps (%.2f ms)", lastFPS, (1000.0f / lastFPS)); if (ui->header("Scene")) { #if defined(VK_USE_PLATFORM_ANDROID_KHR) if (ui->combo("File", selectedScene, scenes)) { vkDeviceWaitIdle(device); loadScene(scenes[selectedScene]); setupDescriptors(); } #else if (ui->button("Open gltf file")) { std::string filename = ""; #if defined(_WIN32) char buffer[MAX_PATH]; OPENFILENAME ofn; ZeroMemory(&buffer, sizeof(buffer)); ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.lpstrFilter = "glTF files\0*.gltf;*.glb\0"; ofn.lpstrFile = buffer; ofn.nMaxFile = MAX_PATH; ofn.lpstrTitle = "Select a glTF file to load"; ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR; if (GetOpenFileNameA(&ofn)) { filename = buffer; } #elif defined(__linux__) && !defined(VK_USE_PLATFORM_ANDROID_KHR) char buffer[1024]; FILE *file = popen("zenity --title=\"Select a glTF file to load\" --file-filter=\"glTF files | *.gltf *.glb\" --file-selection", "r"); if (file) { while (fgets(buffer, sizeof(buffer), file)) { filename += buffer; }; filename.erase(std::remove(filename.begin(), filename.end(), '\n'), filename.end()); std::cout << filename << std::endl; } #elif defined(VK_USE_PLATFORM_MACOS_MVK) opengltfFileButtonClicked = true; // We must load file in render() #endif if (!filename.empty()) { vkDeviceWaitIdle(device); loadScene(filename); setupDescriptors(); } } #endif if (ui->combo("Environment##env", selectedEnvironment, environments)) { vkDeviceWaitIdle(device); loadEnvironment(environments[selectedEnvironment]); setupDescriptors(); } } if (ui->header("Environment")) { ui->checkbox("Background", &displayBackground); ui->slider("Exposure", &shaderValuesParams.exposure, 0.1f, 10.0f); ui->slider("Gamma", &shaderValuesParams.gamma, 0.1f, 4.0f); ui->slider("IBL", &shaderValuesParams.scaleIBLAmbient, 0.0f, 1.0f); } if (ui->header("Camera")) { const std::vector cameraTypes = { "Look at", "First Person" }; int32_t cameraTypeSelection = (int32_t)camera.type; if (ui->combo("Type", &cameraTypeSelection, cameraTypes)) { camera.type = (Camera::CameraType)cameraTypeSelection; resetCamera(); } } if (ui->header("Debug view")) { const std::vector debugNamesInputs = { "none", "Base color", "Normal", "Occlusion", "Emissive", "Metallic", "Roughness" }; if (ui->combo("Inputs", &debugViewInputs, debugNamesInputs)) { shaderValuesParams.debugViewInputs = static_cast(debugViewInputs); } const std::vector debugNamesEquation = { "none", "Diff (l,n)", "F (l,h)", "G (l,v,h)", "D (h)", "Specular" }; if (ui->combo("PBR equation", &debugViewEquation, debugNamesEquation)) { shaderValuesParams.debugViewEquation = static_cast(debugViewEquation); } } if (models.scene.animations.size() > 0) { if (ui->header("Animations")) { ui->checkbox("Animate", &animate); std::vector animationNames; for (auto animation : models.scene.animations) { animationNames.push_back(animation.name); } ui->combo("Animation", &animationIndex, animationNames); } } ImGui::PopItemWidth(); ImGui::End(); ImGui::Render(); ImDrawData* imDrawData = ImGui::GetDrawData(); // Check if ui buffers need to be recreated if (imDrawData) { VkDeviceSize vertexBufferSize = imDrawData->TotalVtxCount * sizeof(ImDrawVert); VkDeviceSize indexBufferSize = imDrawData->TotalIdxCount * sizeof(ImDrawIdx); bool updateBuffers = (ui->vertexBuffer.buffer == VK_NULL_HANDLE) || (ui->vertexBuffer.count != imDrawData->TotalVtxCount) || (ui->indexBuffer.buffer == VK_NULL_HANDLE) || (ui->indexBuffer.count != imDrawData->TotalIdxCount); if (updateBuffers) { vkDeviceWaitIdle(device); if (ui->vertexBuffer.buffer) { ui->vertexBuffer.destroy(); } ui->vertexBuffer.create(vulkanDevice, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, vertexBufferSize); ui->vertexBuffer.count = imDrawData->TotalVtxCount; if (ui->indexBuffer.buffer) { ui->indexBuffer.destroy(); } ui->indexBuffer.create(vulkanDevice, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, indexBufferSize); ui->indexBuffer.count = imDrawData->TotalIdxCount; } // Upload data ImDrawVert* vtxDst = (ImDrawVert*)ui->vertexBuffer.mapped; ImDrawIdx* idxDst = (ImDrawIdx*)ui->indexBuffer.mapped; for (int n = 0; n < imDrawData->CmdListsCount; n++) { const ImDrawList* cmd_list = imDrawData->CmdLists[n]; memcpy(vtxDst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert)); memcpy(idxDst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx)); vtxDst += cmd_list->VtxBuffer.Size; idxDst += cmd_list->IdxBuffer.Size; } ui->vertexBuffer.flush(); ui->indexBuffer.flush(); } #if defined(VK_USE_PLATFORM_ANDROID_KHR) if (mouseButtons.left) { mouseButtons.left = false; } #endif } virtual void render() { if (!prepared) { return; } ui->updateTimer -= frameTimer; if (ui->updateTimer <= 0.0f) { updateOverlay(); ui->updateTimer = 1.0f / 60.0f; } #if defined(VK_USE_PLATFORM_MACOS_MVK) // Need this for smooth imgui controls updateOverlay(); // Must load file here in render loop if (!gltfFileName.empty()) { vkDeviceWaitIdle(device); loadScene(gltfFileName); setupDescriptors(); gltfFileName = ""; } #endif VK_CHECK_RESULT(vkWaitForFences(device, 1, &waitFences[frameIndex], VK_TRUE, UINT64_MAX)); VK_CHECK_RESULT(vkResetFences(device, 1, &waitFences[frameIndex])); VkResult acquire = swapChain.acquireNextImage(presentCompleteSemaphores[frameIndex], &imageIndex); if ((acquire == VK_ERROR_OUT_OF_DATE_KHR) || (acquire == VK_SUBOPTIMAL_KHR)) { windowResize(); } else { VK_CHECK_RESULT(acquire); } recordCommandBuffer(); // Update UBOs updateUniformData(); UniformBufferSet currentUB = uniformBuffers[frameIndex]; memcpy(currentUB.scene.mapped, &shaderValuesScene, sizeof(shaderValuesScene)); memcpy(currentUB.params.mapped, &shaderValuesParams, sizeof(shaderValuesParams)); memcpy(currentUB.skybox.mapped, &shaderValuesSkybox, sizeof(shaderValuesSkybox)); 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[imageIndex]; submitInfo.signalSemaphoreCount = 1; submitInfo.pCommandBuffers = &commandBuffers[frameIndex]; submitInfo.commandBufferCount = 1; VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, waitFences[frameIndex])); VkResult present = swapChain.queuePresent(queue, imageIndex, renderCompleteSemaphores[imageIndex]); if (!((present == VK_SUCCESS) || (present == VK_SUBOPTIMAL_KHR))) { if (present == VK_ERROR_OUT_OF_DATE_KHR) { windowResize(); return; } else { VK_CHECK_RESULT(present); } } if (!paused) { if ((animate) && (models.scene.animations.size() > 0)) { animationTimer += frameTimer; if (animationTimer > models.scene.animations[animationIndex].end) { animationTimer -= models.scene.animations[animationIndex].end; } models.scene.updateAnimation(animationIndex, animationTimer); updateMeshDataBuffer(frameIndex); } updateParams(); } frameIndex = (frameIndex + 1) % renderAhead; } virtual void fileDropped(std::string filename) { vkDeviceWaitIdle(device); loadScene(filename); setupDescriptors(); } }; VulkanApplication *vulkanApplication; // OS specific macros for the example main entry points #if defined(_WIN32) LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { if (vulkanApplication != NULL) { vulkanApplication->handleMessages(hWnd, uMsg, wParam, lParam); } return (DefWindowProc(hWnd, uMsg, wParam, lParam)); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int) { for (int32_t i = 0; i < __argc; i++) { VulkanApplication::args.push_back(__argv[i]); }; vulkanApplication = new VulkanApplication(); vulkanApplication->initVulkan(); vulkanApplication->setupWindow(hInstance, WndProc); vulkanApplication->prepare(); vulkanApplication->renderLoop(); delete(vulkanApplication); return 0; } #elif defined(VK_USE_PLATFORM_ANDROID_KHR) // Android entry point void android_main(android_app* state) { vulkanApplication = new VulkanApplication(); state->userData = vulkanApplication; state->onAppCmd = VulkanApplication::handleAppCommand; state->onInputEvent = VulkanApplication::handleAppInput; androidApp = state; vks::android::getDeviceConfig(); vulkanApplication->renderLoop(); delete(vulkanApplication); } #elif defined(_DIRECT2DISPLAY) // Linux entry point with direct to display wsi static void handleEvent() { } int main(const int argc, const char *argv[]) { for (size_t i = 0; i < argc; i++) { VulkanApplication::args.push_back(argv[i]); }; vulkanApplication = new VulkanApplication(); vulkanApplication->initVulkan(); vulkanApplication->prepare(); vulkanApplication->renderLoop(); delete(vulkanApplication); return 0; } #elif defined(VK_USE_PLATFORM_WAYLAND_KHR) int main(const int argc, const char *argv[]) { for (size_t i = 0; i < argc; i++) { VulkanApplication::args.push_back(argv[i]); }; vulkanApplication = new VulkanApplication(); vulkanApplication->initVulkan(); vulkanApplication->setupWindow(); vulkanApplication->prepare(); vulkanApplication->renderLoop(); delete(vulkanApplication); return 0; } #elif defined(VK_USE_PLATFORM_XCB_KHR) static void handleEvent(const xcb_generic_event_t *event) { if (vulkanApplication != NULL) { vulkanApplication->handleEvent(event); } } int main(const int argc, const char *argv[]) { for (size_t i = 0; i < argc; i++) { VulkanApplication::args.push_back(argv[i]); }; vulkanApplication = new VulkanApplication(); vulkanApplication->initVulkan(); vulkanApplication->setupWindow(); vulkanApplication->prepare(); vulkanApplication->renderLoop(); delete(vulkanApplication); return 0; } #elif defined(VK_USE_PLATFORM_MACOS_MVK) int main(const int argc, const char *argv[]) { @autoreleasepool { for (size_t i = 0; i < argc; i++) { VulkanApplication::args.push_back(argv[i]); }; vulkanApplication = new VulkanApplication(); vulkanApplication->initVulkan(); vulkanApplication->setupWindow(); vulkanApplication->prepare(); vulkanApplication->renderLoop(); delete(vulkanApplication); } return 0; } #endif