Added lighting to SSAO example. small changes and fixes

This commit is contained in:
saschawillems 2016-10-29 13:18:20 +02:00
parent 9212a9e3cb
commit 06369fc72e
11 changed files with 40 additions and 25 deletions

View file

@ -665,6 +665,15 @@ VkPipelineLayoutCreateInfo vkTools::initializers::pipelineLayoutCreateInfo(
return 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( VkDescriptorSetAllocateInfo vkTools::initializers::descriptorSetAllocateInfo(
VkDescriptorPool descriptorPool, VkDescriptorPool descriptorPool,
const VkDescriptorSetLayout* pSetLayouts, const VkDescriptorSetLayout* pSetLayouts,

View file

@ -189,7 +189,9 @@ namespace vkTools
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo( VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo(
const VkDescriptorSetLayout* pSetLayouts, const VkDescriptorSetLayout* pSetLayouts,
uint32_t setLayoutCount ); uint32_t setLayoutCount);
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo(
uint32_t setLayoutCount = 1);
VkDescriptorSetAllocateInfo descriptorSetAllocateInfo( VkDescriptorSetAllocateInfo descriptorSetAllocateInfo(
VkDescriptorPool descriptorPool, VkDescriptorPool descriptorPool,

View file

@ -9,11 +9,10 @@ layout (location = 0) in vec2 inUV;
layout (location = 0) out float outFragColor; layout (location = 0) out float outFragColor;
const int blurSize = 4;
void main() void main()
{ {
const int blurRange = 2; const int blurRange = 2;
int n = 0;
vec2 texelSize = 1.0 / vec2(textureSize(samplerSSAO, 0)); vec2 texelSize = 1.0 / vec2(textureSize(samplerSSAO, 0));
float result = 0.0; float result = 0.0;
for (int x = -blurRange; x < blurRange; x++) for (int x = -blurRange; x < blurRange; x++)
@ -22,7 +21,8 @@ void main()
{ {
vec2 offset = vec2(float(x), float(y)) * texelSize; vec2 offset = vec2(float(x), float(y)) * texelSize;
result += texture(samplerSSAO, inUV + offset).r; result += texture(samplerSSAO, inUV + offset).r;
n++;
} }
} }
outFragColor = result / (blurRange * blurRange * blurRange * blurRange); outFragColor = result / (float(n));
} }

Binary file not shown.

View file

@ -23,12 +23,14 @@ layout (location = 0) out vec4 outFragColor;
void main() void main()
{ {
vec3 fragPos = texture(samplerposition, inUV).rgb; 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); vec4 albedo = texture(samplerAlbedo, inUV);
float ssao = (uboParams.ssaoBlur == 1) ? texture(samplerSSAOBlur, inUV).r : texture(samplerSSAO, inUV).r; 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) if (uboParams.ssaoOnly == 1)
{ {
@ -36,16 +38,18 @@ void main()
} }
else else
{ {
vec3 baseColor = albedo.rgb * NdotL;
if (uboParams.ssao == 1) if (uboParams.ssao == 1)
{ {
outFragColor.rgb = ssao.rrr; outFragColor.rgb = ssao.rrr;
if (uboParams.ssaoOnly != 1) if (uboParams.ssaoOnly != 1)
outFragColor.rgb *= albedo.rgb; outFragColor.rgb *= baseColor;
} }
else else
{ {
outFragColor.rgb = albedo.rgb; outFragColor.rgb = baseColor;
} }
} }
} }

View file

@ -13,7 +13,7 @@ layout (location = 1) out vec4 outNormal;
layout (location = 2) out vec4 outAlbedo; layout (location = 2) out vec4 outAlbedo;
const float NEAR_PLANE = 0.1f; //todo: specialization const 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) float linearDepth(float depth)
{ {

Binary file not shown.

View file

@ -23,18 +23,19 @@ layout (location = 0) out float outFragColor;
// todo: specialization const // todo: specialization const
const int kernelSize = 64; const int kernelSize = 64;
const float radius = 1.0; const float radius = 0.5;
void main() void main()
{ {
// Get G-Buffer values // Get G-Buffer values
vec3 fragPos = texture(samplerPositionDepth, inUV).rgb; 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 // Get a random vector using a noise lookup
ivec2 texDim = textureSize(samplerPositionDepth, 0); ivec2 texDim = textureSize(samplerPositionDepth, 0);
const vec2 noiseUV = vec2(float(texDim.x)/4.0f, float(texDim.y)/4.0f) * inUV; ivec2 noiseDim = textureSize(ssaoNoise, 0);
vec3 randomVec = texture(ssaoNoise, noiseUV).xyz * 2.0 - 1.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 // Create TBN matrix
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal)); vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));

Binary file not shown.

View file

@ -159,9 +159,9 @@ public:
#ifndef __ANDROID__ #ifndef __ANDROID__
camera.rotationSpeed = 0.25f; camera.rotationSpeed = 0.25f;
#endif #endif
camera.position = { 1.9f, 2.6f, -9.25f }; camera.position = { 7.5f, -6.75f, 0.0f };
camera.setRotation(glm::vec3(-15.5f, 10.75f, 0.0f)); camera.setRotation(glm::vec3(5.0f, 90.0f, 0.0f));
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 50.0f); camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 64.0f);
} }
~VulkanExample() ~VulkanExample()
@ -238,8 +238,8 @@ public:
VkImageCreateInfo image = vkTools::initializers::imageCreateInfo(); VkImageCreateInfo image = vkTools::initializers::imageCreateInfo();
image.imageType = VK_IMAGE_TYPE_2D; image.imageType = VK_IMAGE_TYPE_2D;
image.format = format; image.format = format;
image.extent.width = frameBuffers.offscreen.width; image.extent.width = width;
image.extent.height = frameBuffers.offscreen.height; image.extent.height = height;
image.extent.depth = 1; image.extent.depth = 1;
image.mipLevels = 1; image.mipLevels = 1;
image.arrayLayers = 1; image.arrayLayers = 1;
@ -274,7 +274,7 @@ public:
{ {
// Attachments // Attachments
VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true); VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
frameBuffers.offscreen.setSize(width, height); frameBuffers.offscreen.setSize(width, height);
frameBuffers.ssao.setSize(width, height); frameBuffers.ssao.setSize(width, height);
frameBuffers.ssaoBlur.setSize(width, height); frameBuffers.ssaoBlur.setSize(width, height);
@ -285,7 +285,7 @@ public:
assert(validDepthFormat); assert(validDepthFormat);
// G-Buffer // 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.normal, layoutCmd); // Normals
createAttachment(VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, &frameBuffers.offscreen.albedo, layoutCmd); // Albedo (color) 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 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 // Shared sampler used for all color attachments
// todo: separate for nearest (G-Buffer) an linear (ssao, blur, etc.)
VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo(); VkSamplerCreateInfo sampler = vkTools::initializers::samplerCreateInfo();
sampler.magFilter = VK_FILTER_NEAREST; sampler.magFilter = VK_FILTER_NEAREST;
sampler.minFilter = VK_FILTER_NEAREST; sampler.minFilter = VK_FILTER_NEAREST;
@ -618,10 +617,10 @@ public:
void loadAssets() void loadAssets()
{ {
vkMeshLoader::MeshCreateInfo meshCreateInfo; vkMeshLoader::MeshCreateInfo meshCreateInfo;
meshCreateInfo.scale = glm::vec3(1.0f); meshCreateInfo.scale = glm::vec3(0.5f);
meshCreateInfo.uvscale = glm::vec2(1.0f); meshCreateInfo.uvscale = glm::vec2(1.0f);
meshCreateInfo.center = glm::vec3(0.0f, 0.0f, 0.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() void reBuildCommandBuffers()
@ -994,7 +993,7 @@ public:
// SSAO // SSAO
std::uniform_real_distribution<float> rndDist(0.0f, 1.0f); std::uniform_real_distribution<float> rndDist(0.0f, 1.0f);
std::random_device rndDev; std::random_device rndDev;
std::default_random_engine rndGen(rndDev()); std::default_random_engine rndGen;
// Sample kernel // Sample kernel
std::vector<glm::vec4> ssaoKernel(SSAO_KERNEL_SIZE); std::vector<glm::vec4> 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); ssaoNoise[i] = glm::vec4(rndDist(rndGen) * 2.0f - 1.0f, rndDist(rndGen) * 2.0f - 1.0f, 0.0f, 0.0f);
} }
// Upload as texture // 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() void updateUniformBufferMatrices()