diff --git a/data/shaders/ssao/ssao.frag b/data/shaders/ssao/ssao.frag index 8380f733..4c78d681 100644 --- a/data/shaders/ssao/ssao.frag +++ b/data/shaders/ssao/ssao.frag @@ -7,9 +7,12 @@ layout (binding = 0) uniform sampler2D samplerPositionDepth; layout (binding = 1) uniform sampler2D samplerNormal; layout (binding = 2) uniform sampler2D ssaoNoise; +layout (constant_id = 0) const int SSAO_KERNEL_SIZE = 64; +layout (constant_id = 1) const float SSAO_RADIUS = 0.5; + layout (binding = 3) uniform UBOSSAOKernel { - vec4 samples[64]; // todo: specialization const + vec4 samples[SSAO_KERNEL_SIZE]; } uboSSAOKernel; layout (binding = 4) uniform UBO @@ -21,10 +24,6 @@ layout (location = 0) in vec2 inUV; layout (location = 0) out float outFragColor; -// todo: specialization const -const int kernelSize = 32; -const float radius = 0.5; - void main() { // Get G-Buffer values @@ -35,7 +34,7 @@ void main() ivec2 texDim = textureSize(samplerPositionDepth, 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; + vec3 randomVec = texture(ssaoNoise, noiseUV).xyz * 2.0 - 1.0; // Create TBN matrix vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)); @@ -44,10 +43,10 @@ void main() // Calculate occlusion value float occlusion = 0.0f; - for(int i = 0; i < kernelSize; i++) + for(int i = 0; i < SSAO_KERNEL_SIZE; i++) { vec3 samplePos = TBN * uboSSAOKernel.samples[i].xyz; - samplePos = fragPos + samplePos * radius; + samplePos = fragPos + samplePos * SSAO_RADIUS; // project vec4 offset = vec4(samplePos, 1.0f); @@ -57,11 +56,16 @@ void main() float sampleDepth = -texture(samplerPositionDepth, offset.xy).w; +#define RANGE_CHECK 1 +#ifdef RANGE_CHECK // Range check - float rangeCheck = smoothstep(0.0f, 1.0f, radius / abs(fragPos.z - sampleDepth)); + float rangeCheck = smoothstep(0.0f, 1.0f, SSAO_RADIUS / abs(fragPos.z - sampleDepth)); occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f) * rangeCheck; +#else + occlusion += (sampleDepth >= samplePos.z ? 1.0f : 0.0f); +#endif } - occlusion = 1.0 - (occlusion / float(kernelSize)); + occlusion = 1.0 - (occlusion / float(SSAO_KERNEL_SIZE)); outFragColor = occlusion; } diff --git a/data/shaders/ssao/ssao.frag.spv b/data/shaders/ssao/ssao.frag.spv index 241fd060..e8009057 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 337adef4..51b2d9a9 100644 --- a/ssao/ssao.cpp +++ b/ssao/ssao.cpp @@ -25,6 +25,7 @@ #define ENABLE_VALIDATION false #define SSAO_KERNEL_SIZE 32 +#define SSAO_RADIUS 0.5f #if defined(__ANDROID__) #define SSAO_NOISE_DIM 8 @@ -948,9 +949,22 @@ public: // SSAO Pass shaderStages[0] = loadShader(getAssetPath() + "shaders/ssao/fullscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT); shaderStages[1] = loadShader(getAssetPath() + "shaders/ssao/ssao.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT); - pipelineCreateInfo.renderPass = frameBuffers.ssao.renderPass; - pipelineCreateInfo.layout = pipelineLayouts.ssao; - VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.ssao)); + { + // Set constant parameters via specialization constants + std::array specializationMapEntries; + specializationMapEntries[0] = vkTools::initializers::specializationMapEntry(0, 0, sizeof(uint32_t)); // SSAO Kernel size + specializationMapEntries[1] = vkTools::initializers::specializationMapEntry(1, sizeof(uint32_t), sizeof(float)); // SSAO radius + struct { + uint32_t kernelSize = SSAO_KERNEL_SIZE; + float radius = SSAO_RADIUS; + } specializationData; + VkSpecializationInfo specializationInfo = vkTools::initializers::specializationInfo(2, specializationMapEntries.data(), sizeof(specializationData), &specializationData); + shaderStages[1].pSpecializationInfo = &specializationInfo; + pipelineCreateInfo.renderPass = frameBuffers.ssao.renderPass; + pipelineCreateInfo.layout = pipelineLayouts.ssao; + VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.ssao)); + } + // SSAO blur pass shaderStages[0] = loadShader(getAssetPath() + "shaders/ssao/fullscreen.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);