diff --git a/base/vulkantools.cpp b/base/vulkantools.cpp index 782e4fd9..a9c86604 100644 --- a/base/vulkantools.cpp +++ b/base/vulkantools.cpp @@ -665,6 +665,15 @@ VkPipelineLayoutCreateInfo vkTools::initializers::pipelineLayoutCreateInfo( return pipelineLayoutCreateInfo; } +VkPipelineLayoutCreateInfo vkTools::initializers::pipelineLayoutCreateInfo( + uint32_t setLayoutCount) +{ + VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo{}; + pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; + pipelineLayoutCreateInfo.setLayoutCount = setLayoutCount; + return pipelineLayoutCreateInfo; +} + VkDescriptorSetAllocateInfo vkTools::initializers::descriptorSetAllocateInfo( VkDescriptorPool descriptorPool, const VkDescriptorSetLayout* pSetLayouts, diff --git a/base/vulkantools.h b/base/vulkantools.h index dada8d73..90f369a9 100644 --- a/base/vulkantools.h +++ b/base/vulkantools.h @@ -189,7 +189,9 @@ namespace vkTools VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo( const VkDescriptorSetLayout* pSetLayouts, - uint32_t setLayoutCount ); + uint32_t setLayoutCount); + VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo( + uint32_t setLayoutCount = 1); VkDescriptorSetAllocateInfo descriptorSetAllocateInfo( VkDescriptorPool descriptorPool, diff --git a/data/shaders/ssao/blur.frag b/data/shaders/ssao/blur.frag index 33830d00..b7f4bc78 100644 --- a/data/shaders/ssao/blur.frag +++ b/data/shaders/ssao/blur.frag @@ -9,11 +9,10 @@ layout (location = 0) in vec2 inUV; layout (location = 0) out float outFragColor; -const int blurSize = 4; - void main() { const int blurRange = 2; + int n = 0; vec2 texelSize = 1.0 / vec2(textureSize(samplerSSAO, 0)); float result = 0.0; for (int x = -blurRange; x < blurRange; x++) @@ -22,7 +21,8 @@ void main() { vec2 offset = vec2(float(x), float(y)) * texelSize; result += texture(samplerSSAO, inUV + offset).r; + n++; } } - outFragColor = result / (blurRange * blurRange * blurRange * blurRange); + outFragColor = result / (float(n)); } \ No newline at end of file diff --git a/data/shaders/ssao/blur.frag.spv b/data/shaders/ssao/blur.frag.spv index 5b0439dc..3286abc1 100644 Binary files a/data/shaders/ssao/blur.frag.spv and b/data/shaders/ssao/blur.frag.spv differ diff --git a/data/shaders/ssao/composition.frag b/data/shaders/ssao/composition.frag index 96d9e041..321c6d6d 100644 --- a/data/shaders/ssao/composition.frag +++ b/data/shaders/ssao/composition.frag @@ -23,12 +23,14 @@ layout (location = 0) out vec4 outFragColor; void main() { vec3 fragPos = texture(samplerposition, inUV).rgb; - vec3 normal = texture(samplerNormal, inUV).rgb; + vec3 normal = normalize(texture(samplerNormal, inUV).rgb * 2.0 - 1.0); vec4 albedo = texture(samplerAlbedo, inUV); float ssao = (uboParams.ssaoBlur == 1) ? texture(samplerSSAOBlur, inUV).r : texture(samplerSSAO, inUV).r; - outFragColor = vec4(vec3(0.0), 1.0); + vec3 lightPos = vec3(0.0); + vec3 L = normalize(lightPos - fragPos); + float NdotL = max(0.5, dot(normal, L)); if (uboParams.ssaoOnly == 1) { @@ -36,16 +38,18 @@ void main() } else { + vec3 baseColor = albedo.rgb * NdotL; + if (uboParams.ssao == 1) { outFragColor.rgb = ssao.rrr; if (uboParams.ssaoOnly != 1) - outFragColor.rgb *= albedo.rgb; + outFragColor.rgb *= baseColor; } else { - outFragColor.rgb = albedo.rgb; + outFragColor.rgb = baseColor; } } } \ No newline at end of file diff --git a/data/shaders/ssao/composition.frag.spv b/data/shaders/ssao/composition.frag.spv index b421dc90..baa06fdd 100644 Binary files a/data/shaders/ssao/composition.frag.spv and b/data/shaders/ssao/composition.frag.spv differ diff --git a/data/shaders/ssao/gbuffer.frag b/data/shaders/ssao/gbuffer.frag index 13ed06f0..177031ed 100644 --- a/data/shaders/ssao/gbuffer.frag +++ b/data/shaders/ssao/gbuffer.frag @@ -13,7 +13,7 @@ layout (location = 1) out vec4 outNormal; layout (location = 2) out vec4 outAlbedo; const float NEAR_PLANE = 0.1f; //todo: specialization const -const float FAR_PLANE = 50.0f; //todo: specialization const +const float FAR_PLANE = 64.0f; //todo: specialization const float linearDepth(float depth) { diff --git a/data/shaders/ssao/gbuffer.frag.spv b/data/shaders/ssao/gbuffer.frag.spv index 869f86cb..85718122 100644 Binary files a/data/shaders/ssao/gbuffer.frag.spv and b/data/shaders/ssao/gbuffer.frag.spv differ diff --git a/data/shaders/ssao/ssao.frag b/data/shaders/ssao/ssao.frag index fcb3e2b8..877fef7c 100644 --- a/data/shaders/ssao/ssao.frag +++ b/data/shaders/ssao/ssao.frag @@ -23,18 +23,19 @@ layout (location = 0) out float outFragColor; // todo: specialization const const int kernelSize = 64; -const float radius = 1.0; +const float radius = 0.5; void main() { // Get G-Buffer values vec3 fragPos = texture(samplerPositionDepth, inUV).rgb; - vec3 normal = normalize(texture(samplerNormal, inUV).rgb) * 2.0 - 1.0f; + vec3 normal = normalize(texture(samplerNormal, inUV).rgb * 2.0 - 1.0); // Get a random vector using a noise lookup ivec2 texDim = textureSize(samplerPositionDepth, 0); - const vec2 noiseUV = vec2(float(texDim.x)/4.0f, float(texDim.y)/4.0f) * inUV; - vec3 randomVec = texture(ssaoNoise, noiseUV).xyz * 2.0 - 1.0; + ivec2 noiseDim = textureSize(ssaoNoise, 0); + const vec2 noiseUV = vec2(float(texDim.x)/float(noiseDim.x), float(texDim.y)/(noiseDim.y)) * inUV; + vec3 randomVec = texture(ssaoNoise, noiseUV).xyz; // Create TBN matrix vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)); diff --git a/data/shaders/ssao/ssao.frag.spv b/data/shaders/ssao/ssao.frag.spv index a2f67cb5..db4eebcb 100644 Binary files a/data/shaders/ssao/ssao.frag.spv and b/data/shaders/ssao/ssao.frag.spv differ diff --git a/ssao/ssao.cpp b/ssao/ssao.cpp index 586559ee..9b795737 100644 --- a/ssao/ssao.cpp +++ b/ssao/ssao.cpp @@ -159,9 +159,9 @@ public: #ifndef __ANDROID__ camera.rotationSpeed = 0.25f; #endif - camera.position = { 1.9f, 2.6f, -9.25f }; - camera.setRotation(glm::vec3(-15.5f, 10.75f, 0.0f)); - camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 50.0f); + camera.position = { 7.5f, -6.75f, 0.0f }; + camera.setRotation(glm::vec3(5.0f, 90.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 64.0f); } ~VulkanExample() @@ -238,8 +238,8 @@ public: VkImageCreateInfo image = vkTools::initializers::imageCreateInfo(); image.imageType = VK_IMAGE_TYPE_2D; image.format = format; - image.extent.width = frameBuffers.offscreen.width; - image.extent.height = frameBuffers.offscreen.height; + image.extent.width = width; + image.extent.height = height; image.extent.depth = 1; image.mipLevels = 1; image.arrayLayers = 1; @@ -274,7 +274,7 @@ public: { // Attachments VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); - + frameBuffers.offscreen.setSize(width, height); frameBuffers.ssao.setSize(width, height); frameBuffers.ssaoBlur.setSize(width, height); @@ -285,7 +285,7 @@ public: assert(validDepthFormat); // G-Buffer - createAttachment(VK_FORMAT_R16G16B16A16_SFLOAT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &frameBuffers.offscreen.position, layoutCmd); // Position + Depth + createAttachment(VK_FORMAT_R32G32B32A32_SFLOAT, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &frameBuffers.offscreen.position, layoutCmd); // Position + Depth createAttachment(VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &frameBuffers.offscreen.normal, layoutCmd); // Normals createAttachment(VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &frameBuffers.offscreen.albedo, layoutCmd); // Albedo (color) createAttachment(attDepthFormat, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, &frameBuffers.offscreen.depth, layoutCmd); // Depth @@ -496,7 +496,6 @@ public: } // Shared sampler used for all color attachments - // todo: separate for nearest (G-Buffer) an linear (ssao, blur, etc.) VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo(); sampler.magFilter = VK_FILTER_NEAREST; sampler.minFilter = VK_FILTER_NEAREST; @@ -618,10 +617,10 @@ public: void loadAssets() { vkMeshLoader::MeshCreateInfo meshCreateInfo; - meshCreateInfo.scale = glm::vec3(1.0f); + meshCreateInfo.scale = glm::vec3(0.5f); meshCreateInfo.uvscale = glm::vec2(1.0f); meshCreateInfo.center = glm::vec3(0.0f, 0.0f, 0.0f); - loadMesh(getAssetPath() + "models/treasure_smooth.dae", &meshes.scene, vertexLayout, &meshCreateInfo); + loadMesh(getAssetPath() + "models/sibenik/sibenik.dae", &meshes.scene, vertexLayout, &meshCreateInfo); } void reBuildCommandBuffers() @@ -994,7 +993,7 @@ public: // SSAO std::uniform_real_distribution rndDist(0.0f, 1.0f); std::random_device rndDev; - std::default_random_engine rndGen(rndDev()); + std::default_random_engine rndGen; // Sample kernel std::vector ssaoKernel(SSAO_KERNEL_SIZE); @@ -1023,7 +1022,7 @@ public: ssaoNoise[i] = glm::vec4(rndDist(rndGen) * 2.0f - 1.0f, rndDist(rndGen) * 2.0f - 1.0f, 0.0f, 0.0f); } // Upload as texture - textureLoader->createTexture(ssaoNoise.data(), ssaoNoise.size() * sizeof(glm::vec4), VK_FORMAT_R32G32B32A32_SFLOAT, SSAO_NOISE_DIM, SSAO_NOISE_DIM, &textures.ssaoNoise, VK_FILTER_LINEAR); + textureLoader->createTexture(ssaoNoise.data(), ssaoNoise.size() * sizeof(glm::vec4), VK_FORMAT_R32G32B32A32_SFLOAT, SSAO_NOISE_DIM, SSAO_NOISE_DIM, &textures.ssaoNoise, VK_FILTER_NEAREST); } void updateUniformBufferMatrices()