Add missing inverseModelView UBO to fix hdr sample

This commit is contained in:
Ben Clayton 2020-05-21 14:39:30 +01:00
parent b6f2577174
commit 13c081664e
10 changed files with 40 additions and 35 deletions

View file

@ -34,7 +34,7 @@ Shaders written to mirror the GLSL versions at `eddd724`. There have been change
| gears | ☑ | ☑ | ☑ | ☑ | gears | ☑ | ☑ | ☑ | ☑
| geometryshader | ☑ | ☑ | ☑ | ☑ | geometryshader | ☑ | ☑ | ☑ | ☑
| gltfscene | - | - | ☑ | ☑ | gltfscene | - | - | ☑ | ☑
| hdr | ☑ | ❌ | ☑ | ❌ | hdr | ☑ | ❌ | ☑ | ☑
| imgui | ☑ | ☑ | ☑ | ☑ | imgui | ☑ | ☑ | ☑ | ☑
| indirectdraw | ☑ | ☑ | ☑ | ☑ | indirectdraw | ☑ | ☑ | ☑ | ☑
| inlineuniformblocks | ☑ | ☑ | ☑ | ☑ | inlineuniformblocks | ☑ | ☑ | ☑ | ☑

Binary file not shown.

Binary file not shown.

View file

@ -2,12 +2,17 @@
layout (binding = 1) uniform samplerCube samplerEnvMap; layout (binding = 1) uniform samplerCube samplerEnvMap;
layout (binding = 0) uniform UBO {
mat4 projection;
mat4 modelview;
mat4 inverseModelview;
} ubo;
layout (location = 0) in vec3 inUVW; layout (location = 0) in vec3 inUVW;
layout (location = 1) in vec3 inPos; layout (location = 1) in vec3 inPos;
layout (location = 2) in vec3 inNormal; layout (location = 2) in vec3 inNormal;
layout (location = 3) in vec3 inViewVec; layout (location = 3) in vec3 inViewVec;
layout (location = 4) in vec3 inLightVec; layout (location = 4) in vec3 inLightVec;
layout (location = 5) in mat4 inInvModelView;
layout (location = 0) out vec4 outColor0; layout (location = 0) out vec4 outColor0;
layout (location = 1) out vec4 outColor1; layout (location = 1) out vec4 outColor1;
@ -17,37 +22,37 @@ layout (constant_id = 0) const int type = 0;
#define PI 3.1415926 #define PI 3.1415926
#define TwoPI (2.0 * PI) #define TwoPI (2.0 * PI)
layout (binding = 2) uniform UBO { layout (binding = 2) uniform Exposure {
float exposure; float exposure;
} ubo; } exposure;
void main() void main()
{ {
vec4 color; vec4 color;
vec3 wcNormal; vec3 wcNormal;
switch (type) { switch (type) {
case 0: // Skybox case 0: // Skybox
{ {
vec3 normal = normalize(inUVW); vec3 normal = normalize(inUVW);
color = texture(samplerEnvMap, normal); color = texture(samplerEnvMap, normal);
} }
break; break;
case 1: // Reflect case 1: // Reflect
{ {
vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec); vec3 wViewVec = mat3(ubo.inverseModelview) * normalize(inViewVec);
vec3 normal = normalize(inNormal); vec3 normal = normalize(inNormal);
vec3 wNormal = mat3(inInvModelView) * normal; vec3 wNormal = mat3(ubo.inverseModelview) * normal;
float NdotL = max(dot(normal, inLightVec), 0.0); float NdotL = max(dot(normal, inLightVec), 0.0);
vec3 eyeDir = normalize(inViewVec); vec3 eyeDir = normalize(inViewVec);
vec3 halfVec = normalize(inLightVec + eyeDir); vec3 halfVec = normalize(inLightVec + eyeDir);
float NdotH = max(dot(normal, halfVec), 0.0); float NdotH = max(dot(normal, halfVec), 0.0);
float NdotV = max(dot(normal, eyeDir), 0.0); float NdotV = max(dot(normal, eyeDir), 0.0);
float VdotH = max(dot(eyeDir, halfVec), 0.0); float VdotH = max(dot(eyeDir, halfVec), 0.0);
// Geometric attenuation // Geometric attenuation
float NH2 = 2.0 * NdotH; float NH2 = 2.0 * NdotH;
float g1 = (NH2 * NdotV) / VdotH; float g1 = (NH2 * NdotV) / VdotH;
@ -61,27 +66,27 @@ void main()
float fresnel = pow(1.0 - VdotH, 5.0); float fresnel = pow(1.0 - VdotH, 5.0);
fresnel *= (1.0 - F0); fresnel *= (1.0 - F0);
fresnel += F0; fresnel += F0;
float spec = (fresnel * geoAtt) / (NdotV * NdotL * 3.14); float spec = (fresnel * geoAtt) / (NdotV * NdotL * 3.14);
color = texture(samplerEnvMap, reflect(-wViewVec, wNormal)); color = texture(samplerEnvMap, reflect(-wViewVec, wNormal));
color = vec4(color.rgb * NdotL * (k + spec * (1.0 - k)), 1.0); color = vec4(color.rgb * NdotL * (k + spec * (1.0 - k)), 1.0);
} }
break; break;
case 2: // Refract case 2: // Refract
{ {
vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec); vec3 wViewVec = mat3(ubo.inverseModelview) * normalize(inViewVec);
vec3 wNormal = mat3(inInvModelView) * inNormal; vec3 wNormal = mat3(ubo.inverseModelview) * inNormal;
color = texture(samplerEnvMap, refract(-wViewVec, wNormal, 1.0/1.6)); color = texture(samplerEnvMap, refract(-wViewVec, wNormal, 1.0/1.6));
} }
break; break;
} }
// Color with manual exposure into attachment 0 // Color with manual exposure into attachment 0
outColor0.rgb = vec3(1.0) - exp(-color.rgb * ubo.exposure); outColor0.rgb = vec3(1.0) - exp(-color.rgb * exposure.exposure);
// Bright parts for bloom into attachment 1 // Bright parts for bloom into attachment 1
float l = dot(outColor0.rgb, vec3(0.2126, 0.7152, 0.0722)); float l = dot(outColor0.rgb, vec3(0.2126, 0.7152, 0.0722));

Binary file not shown.

View file

@ -8,6 +8,7 @@ layout (constant_id = 0) const int type = 0;
layout (binding = 0) uniform UBO { layout (binding = 0) uniform UBO {
mat4 projection; mat4 projection;
mat4 modelview; mat4 modelview;
mat4 inverseModelview;
} ubo; } ubo;
layout (location = 0) out vec3 outUVW; layout (location = 0) out vec3 outUVW;
@ -15,14 +16,13 @@ layout (location = 1) out vec3 outPos;
layout (location = 2) out vec3 outNormal; layout (location = 2) out vec3 outNormal;
layout (location = 3) out vec3 outViewVec; layout (location = 3) out vec3 outViewVec;
layout (location = 4) out vec3 outLightVec; layout (location = 4) out vec3 outLightVec;
layout (location = 5) out mat4 outInvModelView;
out gl_PerVertex out gl_PerVertex
{ {
vec4 gl_Position; vec4 gl_Position;
}; };
void main() void main()
{ {
outUVW = inPos; outUVW = inPos;
@ -37,11 +37,9 @@ void main()
break; break;
} }
outPos = vec3(ubo.modelview * vec4(inPos, 1.0)); outPos = vec3(ubo.modelview * vec4(inPos, 1.0));
outNormal = mat3(ubo.modelview) * inNormal; outNormal = mat3(ubo.modelview) * inNormal;
outInvModelView = inverse(ubo.modelview);
vec3 lightPos = vec3(0.0f, -5.0f, 5.0f); vec3 lightPos = vec3(0.0f, -5.0f, 5.0f);
outLightVec = lightPos.xyz - outPos.xyz; outLightVec = lightPos.xyz - outPos.xyz;
outViewVec = -outPos.xyz; outViewVec = -outPos.xyz;
} }

Binary file not shown.

View file

@ -58,6 +58,7 @@ public:
struct UBOVS { struct UBOVS {
glm::mat4 projection; glm::mat4 projection;
glm::mat4 modelview; glm::mat4 modelview;
glm::mat4 inverseModelview;
} uboVS; } uboVS;
struct UBOParams { struct UBOParams {
@ -603,7 +604,7 @@ public:
models.objects.push_back(model); models.objects.push_back(model);
} }
// Load HDR cube map // Load HDR cube map
textures.envmap.loadFromFile(getAssetPath() + "textures/hdr/uffizi_cube.ktx", VK_FORMAT_R16G16B16A16_SFLOAT, vulkanDevice, queue); textures.envmap.loadFromFile(getAssetPath() + "textures/hdr/uffizi_cube.ktx", VK_FORMAT_R16G16B16A16_SFLOAT, vulkanDevice, queue);
} }
void setupDescriptorPool() void setupDescriptorPool()
@ -613,7 +614,7 @@ public:
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6) vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6)
}; };
uint32_t numDescriptorSets = 4; uint32_t numDescriptorSets = 4;
VkDescriptorPoolCreateInfo descriptorPoolInfo = VkDescriptorPoolCreateInfo descriptorPoolInfo =
vks::initializers::descriptorPoolCreateInfo(static_cast<uint32_t>(poolSizes.size()), poolSizes.data(), numDescriptorSets); vks::initializers::descriptorPoolCreateInfo(static_cast<uint32_t>(poolSizes.size()), poolSizes.data(), numDescriptorSets);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
} }
@ -621,12 +622,12 @@ public:
void setupDescriptorSetLayout() void setupDescriptorSetLayout()
{ {
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = { std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0), 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, 1),
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 2), vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 2),
}; };
VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo = VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo =
vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size())); vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, nullptr, &descriptorSetLayouts.models)); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, nullptr, &descriptorSetLayouts.models));
@ -691,7 +692,7 @@ public:
}; };
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
// Bloom filter // Bloom filter
allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.bloomFilter, 1); allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.bloomFilter, 1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.bloomFilter)); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.bloomFilter));
@ -744,7 +745,7 @@ public:
VkPipelineColorBlendStateCreateInfo colorBlendState = VkPipelineColorBlendStateCreateInfo colorBlendState =
vks::initializers::pipelineColorBlendStateCreateInfo( vks::initializers::pipelineColorBlendStateCreateInfo(
1, 1,
&blendAttachmentState); &blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState = VkPipelineDepthStencilStateCreateInfo depthStencilState =
@ -839,7 +840,7 @@ public:
dir = 0; dir = 0;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.bloom[1])); VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.bloom[1]));
// Object rendering pipelines // Object rendering pipelines
rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT; rasterizationState.cullMode = VK_CULL_MODE_BACK_BIT;
// Vertex bindings an attributes for model rendering // Vertex bindings an attributes for model rendering
@ -922,6 +923,7 @@ public:
{ {
uboVS.projection = camera.matrices.perspective; uboVS.projection = camera.matrices.perspective;
uboVS.modelview = camera.matrices.view; uboVS.modelview = camera.matrices.view;
uboVS.inverseModelview = glm::inverse(camera.matrices.view);
memcpy(uniformBuffers.matrices.mapped, &uboVS, sizeof(uboVS)); memcpy(uniformBuffers.matrices.mapped, &uboVS, sizeof(uboVS));
} }