diff --git a/data/shaders/texturesparseresidency/sparseresidency.frag b/data/shaders/texturesparseresidency/sparseresidency.frag new file mode 100644 index 00000000..1eb60ff6 --- /dev/null +++ b/data/shaders/texturesparseresidency/sparseresidency.frag @@ -0,0 +1,37 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable +// Required for the sparse* commands used in this shader +#extension GL_ARB_sparse_texture2 : enable + +layout (binding = 1) uniform sampler2D samplerColor; + +layout (location = 0) in vec2 inUV; +layout (location = 1) in float inLodBias; +layout (location = 2) in vec3 inNormal; +layout (location = 3) in vec3 inViewVec; +layout (location = 4) in vec3 inLightVec; + +layout (location = 0) out vec4 outFragColor; + +void main() +{ + // Get residency code for current texel + int residencyCode = sparseTextureARB(samplerColor, inUV, outFragColor); + // Check if texel is resident + bool texelResident = sparseTexelsResidentARB(residencyCode); + + vec4 color; + + if (texelResident) + { + color = texture(samplerColor, inUV, inLodBias); + } + else + { + color = vec4(1.0, 0.0, 0.0, 0.0); + } + + outFragColor = vec4(color.rgb, 1.0); +} \ No newline at end of file diff --git a/data/shaders/texturesparseresidency/sparseresidency.frag.spv b/data/shaders/texturesparseresidency/sparseresidency.frag.spv new file mode 100644 index 00000000..b23cf22d Binary files /dev/null and b/data/shaders/texturesparseresidency/sparseresidency.frag.spv differ diff --git a/data/shaders/texturesparseresidency/sparseresidency.vert b/data/shaders/texturesparseresidency/sparseresidency.vert new file mode 100644 index 00000000..51ace177 --- /dev/null +++ b/data/shaders/texturesparseresidency/sparseresidency.vert @@ -0,0 +1,44 @@ +#version 450 + +#extension GL_ARB_separate_shader_objects : enable +#extension GL_ARB_shading_language_420pack : enable + +layout (location = 0) in vec3 inPos; +layout (location = 1) in vec2 inUV; +layout (location = 2) in vec3 inNormal; + +layout (binding = 0) uniform UBO +{ + mat4 projection; + mat4 model; + vec4 viewPos; + float lodBias; +} ubo; + +layout (location = 0) out vec2 outUV; +layout (location = 1) out float outLodBias; +layout (location = 2) out vec3 outNormal; +layout (location = 3) out vec3 outViewVec; +layout (location = 4) out vec3 outLightVec; + +out gl_PerVertex +{ + vec4 gl_Position; +}; + +void main() +{ + outUV = inUV; + outLodBias = ubo.lodBias; + + vec3 worldPos = vec3(ubo.model * vec4(inPos, 1.0)); + + gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0); + + vec4 pos = ubo.model * vec4(inPos, 1.0); + outNormal = mat3(inverse(transpose(ubo.model))) * inNormal; + vec3 lightPos = vec3(0.0); + vec3 lPos = mat3(ubo.model) * lightPos.xyz; + outLightVec = lPos - pos.xyz; + outViewVec = ubo.viewPos.xyz - pos.xyz; +} diff --git a/data/shaders/texturesparseresidency/sparseresidency.vert.spv b/data/shaders/texturesparseresidency/sparseresidency.vert.spv new file mode 100644 index 00000000..ce9b686d Binary files /dev/null and b/data/shaders/texturesparseresidency/sparseresidency.vert.spv differ diff --git a/texturesparseresidency/texturesparseresidency.cpp b/texturesparseresidency/texturesparseresidency.cpp new file mode 100644 index 00000000..b7aeaf32 --- /dev/null +++ b/texturesparseresidency/texturesparseresidency.cpp @@ -0,0 +1,892 @@ +/* +* Vulkan Example - Sparse texture residency example +* +* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de +* +* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +*/ + +/* +todos: +- check sparse binding support on queue +- residencyNonResidentStrict +- meta data +- Run-time image data upload +*/ + +#include +#include +#include +#include +#include +#include + +#define GLM_FORCE_RADIANS +#define GLM_FORCE_DEPTH_ZERO_TO_ONE +#include +#include + +#include +#include "vulkanexamplebase.h" +#include "vulkandevice.hpp" +#include "vulkanbuffer.hpp" + +#define VERTEX_BUFFER_BIND_ID 0 +#define ENABLE_VALIDATION false + +// Vertex layout for this example +struct Vertex { + float pos[3]; + float uv[2]; + float normal[3]; +}; + +class VulkanExample : public VulkanExampleBase +{ +public: + //todo: comments + struct SparseTexture { + VkSampler sampler; + VkImage image; + VkImageLayout imageLayout; + VkImageView view; + VkDescriptorImageInfo descriptor; + VkFormat format; + uint32_t width, height; + uint32_t mipLevels; + uint32_t layerCount; + std::vector residencyMemoryBinds; // Sparse image mempory bindings for the resident part of the image + std::vector opaqueMemoryBinds; // Sparse memory bindings for the mip tail (if present) + VkSparseImageMemoryBindInfo imageMemoryBindInfo; // Bind info for queue + VkSparseImageOpaqueMemoryBindInfo opaqueMemoryBindInfo; // Opaque bind info for queue + } texture; + + struct { + VkPipelineVertexInputStateCreateInfo inputState; + std::vector bindingDescriptions; + std::vector attributeDescriptions; + } vertices; + + vk::Buffer vertexBuffer; + vk::Buffer indexBuffer; + uint32_t indexCount; + + vk::Buffer uniformBufferVS; + + struct UboVS { + glm::mat4 projection; + glm::mat4 model; + glm::vec4 viewPos; + float lodBias = 0.0f; + } uboVS; + + struct { + VkPipeline solid; + } pipelines; + + VkPipelineLayout pipelineLayout; + VkDescriptorSet descriptorSet; + VkDescriptorSetLayout descriptorSetLayout; + + //todo: comment + VkBindSparseInfo bindSparseInfo; + VkSemaphore bindSparseSemaphore = VK_NULL_HANDLE; + + // Device features to be enabled for this example + static VkPhysicalDeviceFeatures getEnabledFeatures() + { + VkPhysicalDeviceFeatures enabledFeatures = {}; + enabledFeatures.shaderResourceResidency = VK_TRUE; + return enabledFeatures; + } + + VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION, getEnabledFeatures) + { + zoom = -2.5f; + rotation = { 0.0f, 15.0f, 0.0f }; + title = "Vulkan Example - Sparse textures residency"; + enableTextOverlay = true; + std::cout.imbue(std::locale("")); + //todo: check if GPU supports sparse binding feature + } + + ~VulkanExample() + { + // Clean up used Vulkan resources + // Note : Inherited destructor cleans up resources stored in base class + + destroyTextureImage(texture); + + vkDestroySemaphore(device, bindSparseSemaphore, nullptr); + + vkDestroyPipeline(device, pipelines.solid, nullptr); + + vkDestroyPipelineLayout(device, pipelineLayout, nullptr); + vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); + + vertexBuffer.destroy(); + indexBuffer.destroy(); + uniformBufferVS.destroy(); + } + + // Create an image memory barrier for changing the layout of + // an image and put it into an active command buffer + void setImageLayout(VkCommandBuffer cmdBuffer, VkImage image, VkImageAspectFlags aspectMask, VkImageLayout oldImageLayout, VkImageLayout newImageLayout, VkImageSubresourceRange subresourceRange) + { + // Create an image barrier object + VkImageMemoryBarrier imageMemoryBarrier = vkTools::initializers::imageMemoryBarrier();; + imageMemoryBarrier.oldLayout = oldImageLayout; + imageMemoryBarrier.newLayout = newImageLayout; + imageMemoryBarrier.image = image; + imageMemoryBarrier.subresourceRange = subresourceRange; + + // Only sets masks for layouts used in this example + // For a more complete version that can be used with other layouts see vkTools::setImageLayout + + // Source layouts (old) + switch (oldImageLayout) + { + case VK_IMAGE_LAYOUT_UNDEFINED: + // Only valid as initial layout, memory contents are not preserved + // Can be accessed directly, no source dependency required + imageMemoryBarrier.srcAccessMask = 0; + break; + case VK_IMAGE_LAYOUT_PREINITIALIZED: + // Only valid as initial layout for linear images, preserves memory contents + // Make sure host writes to the image have been finished + imageMemoryBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT; + break; + case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: + // Old layout is transfer destination + // Make sure any writes to the image have been finished + imageMemoryBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + break; + } + + // Target layouts (new) + switch (newImageLayout) + { + case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL: + // Transfer source (copy, blit) + // Make sure any reads from the image have been finished + imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; + break; + case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL: + // Transfer destination (copy, blit) + // Make sure any writes to the image have been finished + imageMemoryBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + break; + case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL: + // Shader read (sampler, input attachment) + imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; + break; + } + + // Put barrier on top of pipeline + VkPipelineStageFlags srcStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; + VkPipelineStageFlags destStageFlags = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; + + // Put barrier inside setup command buffer + vkCmdPipelineBarrier( + cmdBuffer, + srcStageFlags, + destStageFlags, + VK_FLAGS_NONE, + 0, nullptr, + 0, nullptr, + 1, &imageMemoryBarrier); + } + + glm::uvec3 alignedDivision(const VkExtent3D& extent, const VkExtent3D& granularity) + { + glm::uvec3 res; + res.x = extent.width / granularity.width + ((extent.width % granularity.width) ? 1u : 0u); + res.y = extent.height / granularity.height + ((extent.height % granularity.height) ? 1u : 0u); + res.z = extent.depth / granularity.depth + ((extent.depth % granularity.depth) ? 1u : 0u); + return res; + } + + void prepareSparseTexture(uint32_t width, uint32_t height, uint32_t layerCount, VkFormat format) + { + texture.width = width; + texture.height = height; + //texture.mipLevels = floor(log2(std::max(width, height))) + 1; //todo + texture.mipLevels = 1; + texture.layerCount = layerCount; + texture.format = format; + + // Get device properites for the requested texture format + VkFormatProperties formatProperties; + vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties); + + // Get sparse image properties + std::vector sparseProperties(32); + // Sparse properties count for the desired format + uint32_t sparsePropertiesCount; + // todo: Temporary workaround, crashes in NV driver if last param is nullptr (to get just count) + vkGetPhysicalDeviceSparseImageFormatProperties( + physicalDevice, + format, + VK_IMAGE_TYPE_2D, + VK_SAMPLE_COUNT_1_BIT, + VK_IMAGE_USAGE_SAMPLED_BIT, + VK_IMAGE_TILING_OPTIMAL, + &sparsePropertiesCount, + sparseProperties.data()); + sparseProperties.resize(sparsePropertiesCount); + + // Check if sparse is supported for this format + if (sparsePropertiesCount == 0) + { + std::cout << "Error: Requested format does not support sparse features!" << std::endl; + return; + } + + std::cout << "Sparse image format properties: " << sparsePropertiesCount << std::endl; + for (auto props : sparseProperties) + { + std::cout << "\t Image granularity: w = " << props.imageGranularity.width << " h = " << props.imageGranularity.height << " d = " << props.imageGranularity.depth << std::endl; + std::cout << "\t Aspect mask: " << props.aspectMask << std::endl; + std::cout << "\t Flags: " << props.flags << std::endl; + } + + // Create sparse image + VkImageCreateInfo sparseImageCreateInfo = vkTools::initializers::imageCreateInfo(); + sparseImageCreateInfo.imageType = VK_IMAGE_TYPE_2D; + sparseImageCreateInfo.format = texture.format; + sparseImageCreateInfo.mipLevels = texture.mipLevels; + sparseImageCreateInfo.arrayLayers = texture.layerCount; + sparseImageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; + sparseImageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; + sparseImageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT; + sparseImageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + sparseImageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; + sparseImageCreateInfo.extent = { texture.width, texture.height, 1 }; + sparseImageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; + sparseImageCreateInfo.flags = VK_IMAGE_CREATE_SPARSE_BINDING_BIT | VK_IMAGE_CREATE_SPARSE_RESIDENCY_BIT; + VK_CHECK_RESULT(vkCreateImage(device, &sparseImageCreateInfo, nullptr, &texture.image)); + + // Get memory requirements + VkMemoryRequirements sparseImageMemoryReqs; + vkGetImageMemoryRequirements(device, texture.image, &sparseImageMemoryReqs); + + std::cout << "Image memory requirements:" << std::endl; + std::cout << "\t Size: " << sparseImageMemoryReqs.size << std::endl; + std::cout << "\t Alignment: " << sparseImageMemoryReqs.alignment << std::endl; + + // Check requested image size against hardware sparse limit + if (sparseImageMemoryReqs.size > vulkanDevice->properties.limits.sparseAddressSpaceSize) + { + std::cout << "Error: Requested sparse image size exceeds supportes sparse address space size!" << std::endl; + return; + }; + + // Get sparse memory requirements + uint32_t sparseMemoryReqsCount; + std::vector sparseMemoryReqs(32); + // todo: Temporary workaround, crashes in NV driver if last param is nullptr (to get just count) + vkGetImageSparseMemoryRequirements(device, texture.image, &sparseMemoryReqsCount, sparseMemoryReqs.data()); + if (sparseMemoryReqsCount == 0) + { + std::cout << "Error: No memory requirements for the sparse image!" << std::endl; + return; + } + sparseMemoryReqs.resize(sparseMemoryReqsCount); + + std::cout << "Sparse image memory requirements: " << sparseMemoryReqsCount << std::endl; + for (auto reqs : sparseMemoryReqs) + { + std::cout << "\t Image granularity: w = " << reqs.formatProperties.imageGranularity.width << " h = " << reqs.formatProperties.imageGranularity.height << " d = " << reqs.formatProperties.imageGranularity.depth << std::endl; + std::cout << "\t Mip tail first LOD: " << reqs.imageMipTailFirstLod << std::endl; + std::cout << "\t Mip tail size: " << reqs.imageMipTailSize << std::endl; + std::cout << "\t Mip tail offset: " << reqs.imageMipTailOffset << std::endl; + std::cout << "\t Mip tail stride: " << reqs.imageMipTailStride << std::endl; + } + + // Get sparse image requirements for the color aspect + VkSparseImageMemoryRequirements sparseMemoryReq; + bool colorAspectFound = false; + for (auto reqs : sparseMemoryReqs) + { + if (reqs.formatProperties.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) + { + sparseMemoryReq = reqs; + colorAspectFound = true; + break; + } + } + if (!colorAspectFound) + { + std::cout << "Error: Could not find sparse image memory requirements for color aspect bit!" << std::endl; + return; + } + + // todo: + // Calculate number of required sparse memory bindings by alignment + assert((sparseImageMemoryReqs.size % sparseImageMemoryReqs.alignment) == 0); + uint32_t memoryTypeIndex = vulkanDevice->getMemoryType(sparseImageMemoryReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); + + // Get sparse bindings + uint32_t sparseBindsCount = static_cast(sparseImageMemoryReqs.size / sparseImageMemoryReqs.alignment); + std::vector sparseMemoryBinds(sparseBindsCount); + + // Sparse bindings for each mip level of all layers outside of the mip tail + for (uint32_t layer = 0; layer < texture.layerCount; layer++) + { + for (uint32_t mipLevel = 0; mipLevel < sparseMemoryReq.imageMipTailFirstLod; mipLevel++) + { + VkExtent3D extent; + extent.width = std::max(sparseImageCreateInfo.extent.width >> mipLevel, 1u); + extent.height = std::max(sparseImageCreateInfo.extent.height >> mipLevel, 1u); + extent.depth = std::max(sparseImageCreateInfo.extent.depth >> mipLevel, 1u); + + VkImageSubresource subResource{}; + subResource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + subResource.mipLevel = mipLevel; + subResource.arrayLayer = layer; + + // Aligned sizes by image granularity + VkExtent3D imageGranularity = sparseMemoryReq.formatProperties.imageGranularity; + glm::uvec3 sparseBindCounts = alignedDivision(extent, imageGranularity); + glm::uvec3 lastBlockExtent; + lastBlockExtent.x = (extent.width % imageGranularity.width) ? extent.width % imageGranularity.width : imageGranularity.width; + lastBlockExtent.y = (extent.height % imageGranularity.height) ? extent.height % imageGranularity.height : imageGranularity.height; + lastBlockExtent.z = (extent.depth % imageGranularity.depth) ? extent.depth % imageGranularity.depth : imageGranularity.depth; + + // Alllocate memory for some blocks + uint32_t index = 0; + for (uint32_t z = 0; z < sparseBindCounts.z; z++) + { + for (uint32_t y = 0; y < sparseBindCounts.y; y++) + { + for (uint32_t x = 0; x < sparseBindCounts.x; x++) + { + + if ((x % 2 == 1) || (y % 2 == 1)) + continue; + + VkOffset3D offset; + offset.x = x * imageGranularity.width; + offset.y = y * imageGranularity.height; + offset.z = z * imageGranularity.depth; + + VkExtent3D extent; + extent.width = (x == sparseBindCounts.x - 1) ? lastBlockExtent.x : imageGranularity.width; + extent.height = (y == sparseBindCounts.y - 1) ? lastBlockExtent.y : imageGranularity.height; + extent.depth = (z == sparseBindCounts.z - 1) ? lastBlockExtent.z : imageGranularity.depth; + + // Allocate memory for this sparse block + VkMemoryAllocateInfo allocInfo = vkTools::initializers::memoryAllocateInfo(); + allocInfo.allocationSize = sparseImageMemoryReqs.alignment; + allocInfo.memoryTypeIndex = memoryTypeIndex; + + VkDeviceMemory deviceMemory; + VK_CHECK_RESULT(vkAllocateMemory(device, &allocInfo, nullptr, &deviceMemory)); + + // Sparse image memory binding + VkSparseImageMemoryBind sparseImageMemoryBind{}; + sparseImageMemoryBind.subresource = subResource; + sparseImageMemoryBind.extent = extent; + sparseImageMemoryBind.offset = offset; + sparseImageMemoryBind.memory = deviceMemory; + + texture.residencyMemoryBinds.push_back(sparseImageMemoryBind); + } + } + } + } + + // Sparse binding for the mip tail (if present) containing the remaining mip levels + // The mip tail contains all mip levels > sparseMemoryReq.imageMipTailFirstLod + if ((sparseMemoryReq.formatProperties.flags & VK_SPARSE_IMAGE_FORMAT_SINGLE_MIPTAIL_BIT) && (sparseMemoryReq.imageMipTailFirstLod < texture.mipLevels)) + { + //todo + } + } // end layers and mips + + // Create signal semaphore for sparse binding + VkSemaphoreCreateInfo semaphoreCreateInfo = vkTools::initializers::semaphoreCreateInfo(); + VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &bindSparseSemaphore)); + + // Prepare bind sparse info for reuse in queue submission + bindSparseInfo = vkTools::initializers::bindSparseInfo(); + //bindSparseInfo.signalSemaphoreCount = 1; + //bindSparseInfo.pSignalSemaphores = &bindSparseSemaphore; + + if (texture.residencyMemoryBinds.size() > 0) + { + texture.imageMemoryBindInfo.image = texture.image; + texture.imageMemoryBindInfo.bindCount = static_cast(texture.residencyMemoryBinds.size()); + texture.imageMemoryBindInfo.pBinds = texture.residencyMemoryBinds.data(); + bindSparseInfo.imageBindCount = 1; + bindSparseInfo.pImageBinds = &texture.imageMemoryBindInfo; + } + + if (texture.opaqueMemoryBinds.size() > 0) + { + texture.opaqueMemoryBindInfo.image = texture.image; + texture.opaqueMemoryBindInfo.bindCount = static_cast(texture.opaqueMemoryBinds.size()); + texture.opaqueMemoryBindInfo.pBinds = texture.opaqueMemoryBinds.data(); + bindSparseInfo.imageOpaqueBindCount = 1; + bindSparseInfo.pImageOpaqueBinds = &texture.opaqueMemoryBindInfo; + } + + // Bind to queue + // todo: in draw? + vkQueueBindSparse(queue, 1, &bindSparseInfo, VK_NULL_HANDLE); + //todo: use sparse bind semaphore + vkQueueWaitIdle(queue); + + // Create sampler + VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo(); + sampler.magFilter = VK_FILTER_LINEAR; + sampler.minFilter = VK_FILTER_LINEAR; + sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; + sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; + sampler.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; + sampler.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; + sampler.mipLodBias = 0.0f; + sampler.compareOp = VK_COMPARE_OP_NEVER; + sampler.minLod = 0.0f; + sampler.maxLod = (float)texture.mipLevels; + sampler.maxAnisotropy = 1.0; + sampler.anisotropyEnable = VK_FALSE; + if (vulkanDevice->features.samplerAnisotropy) + { + // Use max. level of anisotropy for this example + sampler.maxAnisotropy = vulkanDevice->properties.limits.maxSamplerAnisotropy; + sampler.anisotropyEnable = VK_TRUE; + } + sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; + VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &texture.sampler)); + + // Create image view + VkImageViewCreateInfo view = vkTools::initializers::imageViewCreateInfo(); + view.image = VK_NULL_HANDLE; + view.viewType = VK_IMAGE_VIEW_TYPE_2D; + view.format = format; + view.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A }; + view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + view.subresourceRange.baseMipLevel = 0; + view.subresourceRange.baseArrayLayer = 0; + view.subresourceRange.layerCount = 1; + view.subresourceRange.levelCount = texture.mipLevels; + view.image = texture.image; + VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &texture.view)); + + // Fill image descriptor image info that can be used during the descriptor set setup + texture.descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL; + texture.descriptor.imageView = texture.view; + texture.descriptor.sampler = texture.sampler; + } + + // Free all Vulkan resources used a texture object + void destroyTextureImage(SparseTexture texture) + { + vkDestroyImageView(device, texture.view, nullptr); + vkDestroyImage(device, texture.image, nullptr); + vkDestroySampler(device, texture.sampler, nullptr); + //vkFreeMemory(device, texture.deviceMemory, nullptr); + // Sparse memory + for (auto residency : texture.residencyMemoryBinds) + { + vkFreeMemory(device, residency.memory, nullptr); + } + } + + void buildCommandBuffers() + { + VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo(); + + VkClearValue clearValues[2]; + clearValues[0].color = defaultClearColor; + clearValues[1].depthStencil = { 1.0f, 0 }; + + VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo(); + 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 = 2; + renderPassBeginInfo.pClearValues = clearValues; + + for (int32_t i = 0; i < drawCmdBuffers.size(); ++i) + { + // Set target frame buffer + renderPassBeginInfo.framebuffer = frameBuffers[i]; + + VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo)); + + vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE); + + VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f); + vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport); + + VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0); + vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); + + vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL); + vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.solid); + + VkDeviceSize offsets[1] = { 0 }; + vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertexBuffer.buffer, offsets); + vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32); + + vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0); + + vkCmdEndRenderPass(drawCmdBuffers[i]); + + VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i])); + } + } + + void draw() + { + VulkanExampleBase::prepareFrame(); + + // Sparse bindings +// vkQueueBindSparse(queue, 1, &bindSparseInfo, VK_NULL_HANDLE); + //todo: use sparse bind semaphore +// vkQueueWaitIdle(queue); + + // Command buffer to be sumitted to the queue + submitInfo.commandBufferCount = 1; + submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer]; + + // Submit to queue + VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE)); + + VulkanExampleBase::submitFrame(); + } + + void generateQuad() + { + // Setup vertices for a single uv-mapped quad made from two triangles + std::vector vertices = + { + { { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f },{ 0.0f, 0.0f, 1.0f } }, + { { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f },{ 0.0f, 0.0f, 1.0f } }, + { { -1.0f, -1.0f, 0.0f }, { 0.0f, 0.0f },{ 0.0f, 0.0f, 1.0f } }, + { { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f },{ 0.0f, 0.0f, 1.0f } } + }; + + // Setup indices + std::vector indices = { 0,1,2, 2,3,0 }; + indexCount = static_cast(indices.size()); + + // Create buffers + // For the sake of simplicity we won't stage the vertex data to the gpu memory + // Vertex buffer + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + &vertexBuffer, + vertices.size() * sizeof(Vertex), + vertices.data())); + // Index buffer + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_INDEX_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + &indexBuffer, + indices.size() * sizeof(uint32_t), + indices.data())); + } + + void setupVertexDescriptions() + { + // Binding description + vertices.bindingDescriptions.resize(1); + vertices.bindingDescriptions[0] = + vkTools::initializers::vertexInputBindingDescription( + VERTEX_BUFFER_BIND_ID, + sizeof(Vertex), + VK_VERTEX_INPUT_RATE_VERTEX); + + // Attribute descriptions + // Describes memory layout and shader positions + vertices.attributeDescriptions.resize(3); + // Location 0 : Position + vertices.attributeDescriptions[0] = + vkTools::initializers::vertexInputAttributeDescription( + VERTEX_BUFFER_BIND_ID, + 0, + VK_FORMAT_R32G32B32_SFLOAT, + offsetof(Vertex, pos)); + // Location 1 : Texture coordinates + vertices.attributeDescriptions[1] = + vkTools::initializers::vertexInputAttributeDescription( + VERTEX_BUFFER_BIND_ID, + 1, + VK_FORMAT_R32G32_SFLOAT, + offsetof(Vertex, uv)); + // Location 1 : Vertex normal + vertices.attributeDescriptions[2] = + vkTools::initializers::vertexInputAttributeDescription( + VERTEX_BUFFER_BIND_ID, + 2, + VK_FORMAT_R32G32B32_SFLOAT, + offsetof(Vertex, normal)); + + vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo(); + vertices.inputState.vertexBindingDescriptionCount = static_cast(vertices.bindingDescriptions.size()); + vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data(); + vertices.inputState.vertexAttributeDescriptionCount = static_cast(vertices.attributeDescriptions.size()); + vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data(); + } + + void setupDescriptorPool() + { + // Example uses one ubo and one image sampler + std::vector poolSizes = + { + vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1), + vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1) + }; + + VkDescriptorPoolCreateInfo descriptorPoolInfo = + vkTools::initializers::descriptorPoolCreateInfo( + static_cast(poolSizes.size()), + poolSizes.data(), + 2); + + VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); + } + + void setupDescriptorSetLayout() + { + std::vector setLayoutBindings = + { + // Binding 0 : Vertex shader uniform buffer + vkTools::initializers::descriptorSetLayoutBinding( + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + VK_SHADER_STAGE_VERTEX_BIT, + 0), + // Binding 1 : Fragment shader image sampler + vkTools::initializers::descriptorSetLayoutBinding( + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + VK_SHADER_STAGE_FRAGMENT_BIT, + 1) + }; + + VkDescriptorSetLayoutCreateInfo descriptorLayout = + vkTools::initializers::descriptorSetLayoutCreateInfo( + setLayoutBindings.data(), + static_cast(setLayoutBindings.size())); + + VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout)); + + VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = + vkTools::initializers::pipelineLayoutCreateInfo( + &descriptorSetLayout, + 1); + + VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout)); + } + + void setupDescriptorSet() + { + VkDescriptorSetAllocateInfo allocInfo = + vkTools::initializers::descriptorSetAllocateInfo( + descriptorPool, + &descriptorSetLayout, + 1); + + VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet)); + + std::vector writeDescriptorSets = + { + // Binding 0 : Vertex shader uniform buffer + vkTools::initializers::writeDescriptorSet( + descriptorSet, + VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, + 0, + &uniformBufferVS.descriptor), + // Binding 1 : Fragment shader texture sampler + vkTools::initializers::writeDescriptorSet( + descriptorSet, + VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, + 1, + &texture.descriptor) + }; + + vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); + } + + void preparePipelines() + { + VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = + vkTools::initializers::pipelineInputAssemblyStateCreateInfo( + VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, + 0, + VK_FALSE); + + VkPipelineRasterizationStateCreateInfo rasterizationState = + vkTools::initializers::pipelineRasterizationStateCreateInfo( + VK_POLYGON_MODE_FILL, + VK_CULL_MODE_NONE, + VK_FRONT_FACE_COUNTER_CLOCKWISE, + 0); + + VkPipelineColorBlendAttachmentState blendAttachmentState = + vkTools::initializers::pipelineColorBlendAttachmentState( + 0xf, + VK_FALSE); + + VkPipelineColorBlendStateCreateInfo colorBlendState = + vkTools::initializers::pipelineColorBlendStateCreateInfo( + 1, + &blendAttachmentState); + + VkPipelineDepthStencilStateCreateInfo depthStencilState = + vkTools::initializers::pipelineDepthStencilStateCreateInfo( + VK_TRUE, + VK_TRUE, + VK_COMPARE_OP_LESS_OR_EQUAL); + + VkPipelineViewportStateCreateInfo viewportState = + vkTools::initializers::pipelineViewportStateCreateInfo(1, 1, 0); + + VkPipelineMultisampleStateCreateInfo multisampleState = + vkTools::initializers::pipelineMultisampleStateCreateInfo( + VK_SAMPLE_COUNT_1_BIT, + 0); + + std::vector dynamicStateEnables = { + VK_DYNAMIC_STATE_VIEWPORT, + VK_DYNAMIC_STATE_SCISSOR + }; + VkPipelineDynamicStateCreateInfo dynamicState = + vkTools::initializers::pipelineDynamicStateCreateInfo( + dynamicStateEnables.data(), + static_cast(dynamicStateEnables.size()), + 0); + + // Load shaders + std::array shaderStages; + + shaderStages[0] = loadShader(getAssetPath() + "shaders/texturesparseresidency/sparseresidency.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); + shaderStages[1] = loadShader(getAssetPath() + "shaders/texturesparseresidency/sparseresidency.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); + + VkGraphicsPipelineCreateInfo pipelineCreateInfo = + vkTools::initializers::pipelineCreateInfo( + pipelineLayout, + renderPass, + 0); + + pipelineCreateInfo.pVertexInputState = &vertices.inputState; + pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState; + pipelineCreateInfo.pRasterizationState = &rasterizationState; + pipelineCreateInfo.pColorBlendState = &colorBlendState; + pipelineCreateInfo.pMultisampleState = &multisampleState; + pipelineCreateInfo.pViewportState = &viewportState; + pipelineCreateInfo.pDepthStencilState = &depthStencilState; + pipelineCreateInfo.pDynamicState = &dynamicState; + pipelineCreateInfo.stageCount = static_cast(shaderStages.size()); + pipelineCreateInfo.pStages = shaderStages.data(); + + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid)); + } + + // Prepare and initialize uniform buffer containing shader uniforms + void prepareUniformBuffers() + { + // Vertex shader uniform buffer block + VK_CHECK_RESULT(vulkanDevice->createBuffer( + VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, + &uniformBufferVS, + sizeof(uboVS), + &uboVS)); + + updateUniformBuffers(); + } + + void updateUniformBuffers() + { + // Vertex shader + uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); + glm::mat4 viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom)); + + uboVS.model = viewMatrix * glm::translate(glm::mat4(), cameraPos); + uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); + uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); + uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + + uboVS.viewPos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f); + + VK_CHECK_RESULT(uniformBufferVS.map()); + memcpy(uniformBufferVS.mapped, &uboVS, sizeof(uboVS)); + uniformBufferVS.unmap(); + } + + void prepare() + { + VulkanExampleBase::prepare(); + generateQuad(); + setupVertexDescriptions(); + prepareUniformBuffers(); + prepareSparseTexture(8192, 8192, 1, VK_FORMAT_R8G8B8A8_UNORM); + setupDescriptorSetLayout(); + preparePipelines(); + setupDescriptorPool(); + setupDescriptorSet(); + buildCommandBuffers(); + prepared = true; + } + + virtual void render() + { + if (!prepared) + return; + draw(); + } + + virtual void viewChanged() + { + updateUniformBuffers(); + } + + void changeLodBias(float delta) + { + uboVS.lodBias += delta; + if (uboVS.lodBias < 0.0f) + { + uboVS.lodBias = 0.0f; + } + if (uboVS.lodBias > texture.mipLevels) + { + uboVS.lodBias = (float)texture.mipLevels; + } + updateUniformBuffers(); + updateTextOverlay(); + } + + virtual void keyPressed(uint32_t keyCode) + { + switch (keyCode) + { + case KEY_KPADD: + case GAMEPAD_BUTTON_R1: + changeLodBias(0.1f); + break; + case KEY_KPSUB: + case GAMEPAD_BUTTON_L1: + changeLodBias(-0.1f); + break; + } + } + + virtual void getOverlayText(VulkanTextOverlay *textOverlay) + { + std::stringstream ss; + ss << std::setprecision(2) << std::fixed << uboVS.lodBias; +#if defined(__ANDROID__) + textOverlay->addText("LOD bias: " + ss.str() + " (Buttons L1/R1 to change)", 5.0f, 85.0f, VulkanTextOverlay::alignLeft); +#else + textOverlay->addText("LOD bias: " + ss.str() + " (numpad +/- to change)", 5.0f, 85.0f, VulkanTextOverlay::alignLeft); +#endif + } +}; + +VULKAN_EXAMPLE_MAIN() diff --git a/texturesparseresidency/texturesparseresidency.vcxproj b/texturesparseresidency/texturesparseresidency.vcxproj new file mode 100644 index 00000000..e33c425a --- /dev/null +++ b/texturesparseresidency/texturesparseresidency.vcxproj @@ -0,0 +1,99 @@ + + + + + Debug + x64 + + + Release + x64 + + + + {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9} + Win32Proj + 8.1 + + + + Application + true + v140 + + + Application + false + v140 + + + + + + + + + + + + + true + $(SolutionDir)\bin\ + $(SolutionDir)\bin\intermediate\$(ProjectName)\$(ConfigurationName) + + + true + $(SolutionDir)\bin\ + $(SolutionDir)\bin\intermediate\$(ProjectName)\$(ConfigurationName) + + + + WIN32;_DEBUG;_WINDOWS;VK_USE_PLATFORM_WIN32_KHR;_USE_MATH_DEFINES;NOMINMAX;%(PreprocessorDefinitions) + MultiThreadedDebugDLL + Level3 + ProgramDatabase + Disabled + ..\base;..\external\glm;..\external\gli;..\external\assimp;..\external;%(AdditionalIncludeDirectories) + /FS %(AdditionalOptions) + + + true + Windows + ..\libs\vulkan\vulkan-1.lib;..\libs\assimp\assimp.lib;%(AdditionalDependencies) + + + + + WIN32;NDEBUG;_WINDOWS;VK_USE_PLATFORM_WIN32_KHR;_USE_MATH_DEFINES;NOMINMAX;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDLL + Level3 + ProgramDatabase + ..\base;..\external\glm;..\external\gli;..\external\assimp;..\external;%(AdditionalIncludeDirectories) + + + true + Windows + true + true + ..\libs\vulkan\vulkan-1.lib;..\libs\assimp\assimp.lib;%(AdditionalDependencies) + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/texturesparseresidency/texturesparseresidency.vcxproj.filters b/texturesparseresidency/texturesparseresidency.vcxproj.filters new file mode 100644 index 00000000..ab373ccf --- /dev/null +++ b/texturesparseresidency/texturesparseresidency.vcxproj.filters @@ -0,0 +1,53 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav + + + {bbed2435-ec0f-473f-8082-787e5582bc19} + + + + + Source Files + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + + + Shaders + + + Shaders + + + \ No newline at end of file diff --git a/vulkanExamples.sln b/vulkanExamples.sln index 3840114b..0167ca84 100644 --- a/vulkanExamples.sln +++ b/vulkanExamples.sln @@ -107,6 +107,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tessellation", "Tessellatio EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texture3d", "texture3d\texture3d.vcxproj", "{D36B82DD-9114-44D2-A9C9-8DF0F8544BD5}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "texturesparseresidency", "texturesparseresidency\texturesparseresidency.vcxproj", "{1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -265,6 +267,10 @@ Global {D36B82DD-9114-44D2-A9C9-8DF0F8544BD5}.Debug|x64.Build.0 = Debug|x64 {D36B82DD-9114-44D2-A9C9-8DF0F8544BD5}.Release|x64.ActiveCfg = Release|x64 {D36B82DD-9114-44D2-A9C9-8DF0F8544BD5}.Release|x64.Build.0 = Release|x64 + {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Debug|x64.ActiveCfg = Debug|x64 + {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Debug|x64.Build.0 = Debug|x64 + {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Release|x64.ActiveCfg = Release|x64 + {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -281,5 +287,6 @@ Global {6C6E48B1-5946-4754-9E31-E1C989EC25FE} = {C93DE675-A816-4412-8E9F-1EFE9AC07750} {24AD09B4-6ABE-4A82-83E7-6A7799A88762} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} {D36B82DD-9114-44D2-A9C9-8DF0F8544BD5} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} + {1FA0178C-F5E9-4B2E-A488-14F310F8DBD9} = {A8492D6D-5243-456E-8173-39B99F1FEA9C} EndGlobalSection EndGlobal