Continued work on PBR example

This commit is contained in:
saschawillems 2017-02-19 15:03:16 +01:00
parent 85b44a0346
commit 0f8bed9fde
9 changed files with 263 additions and 28 deletions

146
data/shaders/pbr/pbr.frag Normal file
View file

@ -0,0 +1,146 @@
#version 450
layout (binding = 1) uniform samplerCube envmap;
layout (binding = 2) uniform samplerCube envmapibldiff;
layout (binding = 3) uniform samplerCube envmapiblrefl;
layout (location = 0) in vec3 inWorldPos;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
vec3 camPos;
} ubo;
layout (location = 0) out vec4 outColor;
layout(push_constant) uniform PushConsts {
layout(offset = 12) float roughness;
layout(offset = 16) float metallic;
layout(offset = 20) float r;
layout(offset = 24) float g;
layout(offset = 28) float b;
} material;
const float PI = 3.14159265359;
//#define ROUGHNESS_PATTERN 1
// Fresnel ------------------------------------------------------------------------
float fresnelSchlick(float ct, float F0)
{
return F0 + (1.0 - F0) * pow(1.0 - ct, 5.0);
}
// Normal distribution functions ---------------------------------------------------
float NDF_blinnPhong(float dotNH, float alphaSqr)
{
return 1.0 / (PI * alphaSqr) * pow(dotNH, 2.0 / alphaSqr - 2.0);
}
float NDF_beckmann(float dotNH, float alphaSqr)
{
float dotNH2 = dotNH * dotNH;
return 1.0 / (PI * alphaSqr * dotNH2 * dotNH2) * exp((dotNH2 - 1.0) / (alphaSqr * dotNH2));
}
float NDF_GGX(float dotNH, float alphaSqr)
{
return alphaSqr / (PI * pow(dotNH * dotNH * (alphaSqr - 1.0) + 1.0, 2.0));
}
// Geometry visibility functions ---------------------------------------------------
float GEOM_SchlickSmith(float dotNL, float dotNV, float alpha)
{
//float k = alpha * sqrt(2.0 / PI);
float k = pow(0.8 + 0.5 * alpha, 2.0) / 2.0;
float GL = 1.0 / (dotNL * (1.0 - k) + k);
float GV = 1.0 / (dotNV * (1.0 - k) + k);
return GL * GV;
}
float PBR_Shade(vec3 N, vec3 V, vec3 L, float roughness, float F0)
{
float alpha = roughness * roughness;
float alphaSqr = alpha * alpha;
vec3 H = normalize (V + L);
float dotNL = clamp(dot(N, L), 0.0, 1.0);
float dotNV = clamp(dot(N, V), 0.0, 1.0);
float dotNH = clamp(dot(N, H), 0.0, 1.0);
float dotLH = clamp(dot(L, H), 0.0, 1.0);
// Normal distribution
float Di = NDF_GGX(dotNH, alphaSqr);
// Fresnel
float Fs = fresnelSchlick(dotNV, F0);
// Visibility term
float Vs = GEOM_SchlickSmith(dotNL, dotNV, alpha);
return dotNL * Di * Fs * Vs;
}
// ----------------------------------------------------------------------------
void main()
{
// Partially based on https://www.shadertoy.com/view/XsfXWX by Alexander Alekseev (https://github.com/tdmaav)
// One fixed light source
vec3 lightPos = vec3(-10.0f, -10.0f, 10.0f);
// Take light color from environment map
vec3 lightColor = texture(envmapiblrefl, vec3(0.5)).xyz;
vec3 N = normalize(inNormal);
vec3 V = normalize(ubo.camPos - inWorldPos);
vec3 L = normalize(lightPos - inWorldPos);
vec3 R = reflect(-V, N);
// Store material values for quick testing/changing inside the shader
float roughness = material.roughness;
float metallic = material.metallic;
// Add striped pattern to roughness based on vertex position
#ifdef ROUGHNESS_PATTERN
roughness = max(roughness, step(fract(inWorldPos.y * 2.02), 0.5));
#endif
// Get IBL components from cube maps
vec3 IBLdiffuse = texture(envmapibldiff, inNormal).rgb;
vec3 IBLreflection = texture(envmapiblrefl, inNormal).rgb;
// Fresnel part
float fresnel = pow(max(1.0 - dot(N, V), 0.0), 1.5);
// Reflection part
// Select mip level based on roughness
ivec2 dim = textureSize(envmap, 0);
float nummips = log2(max(dim.s, dim.y));
vec3 reflection = texture(envmap, R).xyz;
reflection = textureLod(envmap, R, max(roughness * nummips, textureQueryLod(envmap, R).y)).rgb;
reflection = mix(reflection, IBLreflection, (1.0-fresnel) * roughness);
reflection = mix(reflection, IBLreflection, roughness);
// Specular part
// F0 based on metallic factor of material
vec3 F0 = vec3(0.04);
F0 = mix(F0, lightColor, material.metallic);
vec3 spec = lightColor * PBR_Shade(N, V, L, roughness, 0.2);
reflection -= spec;
// Diffuse part
vec3 matColor = vec3(material.r, material.g, material.b);
vec3 diffuse = mix(IBLdiffuse * matColor, reflection, fresnel);
// Final output mixes based on material metalness
outColor.rgb = mix(diffuse, reflection, metallic) + spec;
}

Binary file not shown.

38
data/shaders/pbr/pbr.vert Normal file
View file

@ -0,0 +1,38 @@
#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 vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
mat4 view;
vec3 camPos;
} ubo;
layout (location = 0) out vec3 outWorldPos;
layout (location = 1) out vec3 outNormal;
layout (location = 2) out vec2 outUV;
layout(push_constant) uniform PushConsts {
vec3 objPos;
} pushConsts;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
vec3 locPos = vec3(ubo.model * vec4(inPos, 1.0));
outWorldPos = locPos + pushConsts.objPos;
outNormal = mat3(ubo.model) * inNormal;
outUV = inUV;
gl_Position = ubo.projection * ubo.view * vec4(outWorldPos, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,12 @@
#version 450
layout (binding = 1) uniform samplerCube samplerEnv;
layout (location = 0) in vec3 inUVW;
layout (location = 0) out vec4 outFragColor;
void main()
{
outFragColor = texture(samplerEnv, inUVW);
}

Binary file not shown.

View file

@ -0,0 +1,27 @@
#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 vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (binding = 0) uniform UBO
{
mat4 projection;
mat4 model;
} ubo;
layout (location = 0) out vec3 outUVW;
out gl_PerVertex
{
vec4 gl_Position;
};
void main()
{
outUVW = inPos;
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
}

Binary file not shown.

View file

@ -33,8 +33,7 @@
struct Material {
float roughness;
float metallic;
float ao;
float rim = 0.15f;
float r,g,b; // Color components as single floats because we use push constants
};
class VulkanExample : public VulkanExampleBase
@ -43,7 +42,8 @@ public:
bool displaySkybox = true;
vks::TextureCubeMap envmap;
vks::TextureCubeMap irradiancemap;
vks::TextureCubeMap envmapiblDiff;
vks::TextureCubeMap envmapiblRefl;
// Vertex layout for the models
vks::VertexLayout vertexLayout = vks::VertexLayout({
@ -88,8 +88,8 @@ public:
title = "Vulkan Example - Physical based rendering";
enableTextOverlay = true;
camera.type = Camera::CameraType::firstperson;
camera.setPosition(glm::vec3(-15.0f, 4.0f, -4.0f));
camera.setRotation(glm::vec3(-15.0f, -70.0f, 0.0f));
camera.setPosition(glm::vec3(8.0f, 7.25f, -13.0f));
camera.setRotation(glm::vec3(-31.0f, 24.0f, 0.0f));
camera.movementSpeed = 4.0f;
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
camera.rotationSpeed = 0.25f;
@ -113,7 +113,8 @@ public:
uniformBuffers.object.destroy();
uniformBuffers.skybox.destroy();
envmap.destroy();
irradiancemap.destroy();
envmapiblDiff.destroy();
envmapiblRefl.destroy();
}
void reBuildCommandBuffers()
@ -177,7 +178,19 @@ public:
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.pbr);
Material mat;
mat.ao = 1.0f;
mat.r = 1.0f;
mat.g = 0.0f;
mat.b = 0.0f;
//#define SINGLE_MESH 1
#ifdef SINGLE_MESH
mat.metallic = 0.1;
mat.roughness = 1.0;
glm::vec3 pos = glm::vec3(0.0f);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material), &mat);
vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0);
#else
for (uint32_t y = 0; y < GRID_DIM; y++) {
for (uint32_t x = 0; x < GRID_DIM; x++) {
glm::vec3 pos = glm::vec3(float(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f, float(y - (GRID_DIM / 2.0f)) * 2.5f);
@ -188,7 +201,7 @@ public:
vkCmdDrawIndexed(drawCmdBuffers[i], models.objects[models.objectIndex].indexCount, 1, 0, 0, 0);
}
}
#endif
vkCmdEndRenderPass(drawCmdBuffers[i]);
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
@ -200,14 +213,16 @@ public:
// Skybox
models.skybox.loadFromFile(getAssetPath() + "models/cube.obj", vertexLayout, 1.0f, vulkanDevice, queue);
// Objects
std::vector<std::string> filenames = { "sphere.obj", "teapot.dae", "torusknot.obj", "venus.fbx" };
std::vector<std::string> filenames = { "geosphere.obj", "teapot.dae", "torusknot.obj", "suzanne.obj" };
for (auto file : filenames) {
vks::Model model;
model.loadFromFile(getAssetPath() + "models/" + file, vertexLayout, OBJ_DIM * (file == "venus.fbx" ? 3.0f : 1.0f) , vulkanDevice, queue);
model.loadFromFile(getAssetPath() + "models/" + file, vertexLayout, OBJ_DIM * (file == "suzanne.obj" ? 2.0f : 1.0f), vulkanDevice, queue);
models.objects.push_back(model);
}
envmap.loadFromFile(getAssetPath() + "textures/cube_env_01_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, vulkanDevice, queue);
irradiancemap.loadFromFile(getAssetPath() + "textures/cube_env_01_irradiance_rgba8.ktx", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue);
// Example uses three different cubemaps (environment, diffuse for IBL (irradiance) and reflective for IBL)
envmap.loadFromFile(getAssetPath() + "textures/cubemap_uffizi_env.dds", VK_FORMAT_BC3_UNORM_BLOCK, vulkanDevice, queue);
envmapiblDiff.loadFromFile(getAssetPath() + "textures/cubemap_uffizi_ibl_diff.dds", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue);
envmapiblRefl.loadFromFile(getAssetPath() + "textures/cubemap_uffizi_ibl_refl.dds", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue);
}
void setupDescriptorSetLayout()
@ -216,19 +231,16 @@ public:
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0),
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1),
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 2),
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 3),
};
VkDescriptorSetLayoutCreateInfo descriptorLayout =
vks::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(),
setLayoutBindings.size());
vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo =
vks::initializers::pipelineLayoutCreateInfo(
&descriptorSetLayout,
1);
vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
std::vector<VkPushConstantRange> pushConstantRanges = {
vks::initializers::pushConstantRange(VK_SHADER_STAGE_VERTEX_BIT, sizeof(glm::vec3), 0),
@ -246,11 +258,11 @@ public:
// Descriptor Pool
std::vector<VkDescriptorPoolSize> poolSizes = {
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 4)
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6)
};
VkDescriptorPoolCreateInfo descriptorPoolInfo =
vks::initializers::descriptorPoolCreateInfo(poolSizes.size(), poolSizes.data(), 2);
vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
@ -265,19 +277,19 @@ public:
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
vks::initializers::writeDescriptorSet(descriptorSets.object, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.object.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.object, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &envmap.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.object, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, &irradiancemap.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.object, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, &envmapiblDiff.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.object, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 3, &envmapiblRefl.descriptor),
};
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
// Sky box descriptor set
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.skybox));
writeDescriptorSets = {
vks::initializers::writeDescriptorSet(descriptorSets.skybox, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffers.skybox.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.skybox, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &envmap.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.skybox, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2, &irradiancemap.descriptor),
vks::initializers::writeDescriptorSet(descriptorSets.skybox, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &envmapiblRefl.descriptor),
};
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, NULL);
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
}
void preparePipelines()
@ -308,7 +320,7 @@ public:
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo dynamicState =
vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables.data(), dynamicStateEnables.size());
vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass);
@ -322,7 +334,7 @@ public:
pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState;
pipelineCreateInfo.stageCount = shaderStages.size();
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
pipelineCreateInfo.pStages = shaderStages.data();
// Vertex bindings an attributes
@ -391,7 +403,7 @@ public:
// 3D object
uboVS.projection = camera.matrices.perspective;
uboVS.view = camera.matrices.view;
uboVS.model = glm::rotate(glm::mat4(), glm::radians(45.0f), glm::vec3(0.0f, 1.0f, 0.0f));
uboVS.model = glm::rotate(glm::mat4(), glm::radians(-45.0f), glm::vec3(0.0f, 1.0f, 0.0f));
uboVS.camPos = camera.position * -1.0f;
memcpy(uniformBuffers.object.mapped, &uboVS, sizeof(uboVS));