diff --git a/data/hlsl/README.md b/data/hlsl/README.md index 208553bc..33c11849 100644 --- a/data/hlsl/README.md +++ b/data/hlsl/README.md @@ -34,7 +34,7 @@ Shaders written to mirror the GLSL versions at `eddd724`. There have been change | gears | ☑ | ☑ | ☑ | ☑ | geometryshader | ☑ | ☑ | ☑ | ☑ | gltfscene | - | - | ☑ | ☑ -| hdr | ☑ | ❌ | ☑ | ❌ +| hdr | ☑ | ❌ | ☑ | ☑ | imgui | ☑ | ☑ | ☑ | ☑ | indirectdraw | ☑ | ☑ | ☑ | ☑ | inlineuniformblocks | ☑ | ☑ | ☑ | ☑ diff --git a/data/shaders/hdr/bloom.frag.spv b/data/shaders/hdr/bloom.frag.spv index 57d69057..65e7d382 100644 Binary files a/data/shaders/hdr/bloom.frag.spv and b/data/shaders/hdr/bloom.frag.spv differ diff --git a/data/shaders/hdr/bloom.vert.spv b/data/shaders/hdr/bloom.vert.spv index 4040f608..ffcd1a84 100644 Binary files a/data/shaders/hdr/bloom.vert.spv and b/data/shaders/hdr/bloom.vert.spv differ diff --git a/data/shaders/hdr/composition.frag.spv b/data/shaders/hdr/composition.frag.spv index 39ff5dff..1a334e0e 100644 Binary files a/data/shaders/hdr/composition.frag.spv and b/data/shaders/hdr/composition.frag.spv differ diff --git a/data/shaders/hdr/composition.vert.spv b/data/shaders/hdr/composition.vert.spv index 4040f608..ffcd1a84 100644 Binary files a/data/shaders/hdr/composition.vert.spv and b/data/shaders/hdr/composition.vert.spv differ diff --git a/data/shaders/hdr/gbuffer.frag b/data/shaders/hdr/gbuffer.frag index 3ffccf2a..b920dad3 100644 --- a/data/shaders/hdr/gbuffer.frag +++ b/data/shaders/hdr/gbuffer.frag @@ -2,12 +2,17 @@ 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 = 1) in vec3 inPos; layout (location = 2) in vec3 inNormal; layout (location = 3) in vec3 inViewVec; layout (location = 4) in vec3 inLightVec; -layout (location = 5) in mat4 inInvModelView; layout (location = 0) out vec4 outColor0; layout (location = 1) out vec4 outColor1; @@ -17,37 +22,37 @@ layout (constant_id = 0) const int type = 0; #define PI 3.1415926 #define TwoPI (2.0 * PI) -layout (binding = 2) uniform UBO { +layout (binding = 2) uniform Exposure { float exposure; -} ubo; +} exposure; -void main() +void main() { vec4 color; vec3 wcNormal; switch (type) { - case 0: // Skybox + case 0: // Skybox { vec3 normal = normalize(inUVW); color = texture(samplerEnvMap, normal); } break; - + case 1: // Reflect { - vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec); + vec3 wViewVec = mat3(ubo.inverseModelview) * normalize(inViewVec); vec3 normal = normalize(inNormal); - vec3 wNormal = mat3(inInvModelView) * normal; + vec3 wNormal = mat3(ubo.inverseModelview) * normal; float NdotL = max(dot(normal, inLightVec), 0.0); - vec3 eyeDir = normalize(inViewVec); + vec3 eyeDir = normalize(inViewVec); vec3 halfVec = normalize(inLightVec + eyeDir); - float NdotH = max(dot(normal, halfVec), 0.0); - float NdotV = max(dot(normal, eyeDir), 0.0); + float NdotH = max(dot(normal, halfVec), 0.0); + float NdotV = max(dot(normal, eyeDir), 0.0); float VdotH = max(dot(eyeDir, halfVec), 0.0); - + // Geometric attenuation float NH2 = 2.0 * NdotH; float g1 = (NH2 * NdotV) / VdotH; @@ -61,27 +66,27 @@ void main() float fresnel = pow(1.0 - VdotH, 5.0); fresnel *= (1.0 - F0); fresnel += F0; - + 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); } break; - case 2: // Refract + case 2: // Refract { - vec3 wViewVec = mat3(inInvModelView) * normalize(inViewVec); - vec3 wNormal = mat3(inInvModelView) * inNormal; - color = texture(samplerEnvMap, refract(-wViewVec, wNormal, 1.0/1.6)); + vec3 wViewVec = mat3(ubo.inverseModelview) * normalize(inViewVec); + vec3 wNormal = mat3(ubo.inverseModelview) * inNormal; + color = texture(samplerEnvMap, refract(-wViewVec, wNormal, 1.0/1.6)); } break; } // 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 float l = dot(outColor0.rgb, vec3(0.2126, 0.7152, 0.0722)); diff --git a/data/shaders/hdr/gbuffer.frag.spv b/data/shaders/hdr/gbuffer.frag.spv index 9b9afd9b..57f315b3 100644 Binary files a/data/shaders/hdr/gbuffer.frag.spv and b/data/shaders/hdr/gbuffer.frag.spv differ diff --git a/data/shaders/hdr/gbuffer.vert b/data/shaders/hdr/gbuffer.vert index 5d69f03c..ef9f0ad7 100644 --- a/data/shaders/hdr/gbuffer.vert +++ b/data/shaders/hdr/gbuffer.vert @@ -8,6 +8,7 @@ layout (constant_id = 0) const int type = 0; layout (binding = 0) uniform UBO { mat4 projection; mat4 modelview; + mat4 inverseModelview; } ubo; layout (location = 0) out vec3 outUVW; @@ -15,14 +16,13 @@ layout (location = 1) out vec3 outPos; layout (location = 2) out vec3 outNormal; layout (location = 3) out vec3 outViewVec; layout (location = 4) out vec3 outLightVec; -layout (location = 5) out mat4 outInvModelView; -out gl_PerVertex +out gl_PerVertex { vec4 gl_Position; }; -void main() +void main() { outUVW = inPos; @@ -37,11 +37,9 @@ void main() break; } outPos = vec3(ubo.modelview * vec4(inPos, 1.0)); - outNormal = mat3(ubo.modelview) * inNormal; - - outInvModelView = inverse(ubo.modelview); + outNormal = mat3(ubo.modelview) * inNormal; vec3 lightPos = vec3(0.0f, -5.0f, 5.0f); outLightVec = lightPos.xyz - outPos.xyz; - outViewVec = -outPos.xyz; + outViewVec = -outPos.xyz; } diff --git a/data/shaders/hdr/gbuffer.vert.spv b/data/shaders/hdr/gbuffer.vert.spv index 2743ab44..6563548c 100644 Binary files a/data/shaders/hdr/gbuffer.vert.spv and b/data/shaders/hdr/gbuffer.vert.spv differ diff --git a/examples/hdr/hdr.cpp b/examples/hdr/hdr.cpp index 93dd6d92..cedec593 100644 --- a/examples/hdr/hdr.cpp +++ b/examples/hdr/hdr.cpp @@ -58,6 +58,7 @@ public: struct UBOVS { glm::mat4 projection; glm::mat4 modelview; + glm::mat4 inverseModelview; } uboVS; struct UBOParams { @@ -603,7 +604,7 @@ public: models.objects.push_back(model); } // 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() @@ -613,7 +614,7 @@ public: vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 6) }; uint32_t numDescriptorSets = 4; - VkDescriptorPoolCreateInfo descriptorPoolInfo = + VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(static_cast(poolSizes.size()), poolSizes.data(), numDescriptorSets); VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool)); } @@ -621,12 +622,12 @@ public: void setupDescriptorSetLayout() { std::vector 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_UNIFORM_BUFFER, VK_SHADER_STAGE_FRAGMENT_BIT, 2), }; - VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo = + VkDescriptorSetLayoutCreateInfo descriptorLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast(setLayoutBindings.size())); VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayoutInfo, nullptr, &descriptorSetLayouts.models)); @@ -691,7 +692,7 @@ public: }; vkUpdateDescriptorSets(device, static_cast(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL); - // Bloom filter + // Bloom filter allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayouts.bloomFilter, 1); VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.bloomFilter)); @@ -744,7 +745,7 @@ public: VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo( - 1, + 1, &blendAttachmentState); VkPipelineDepthStencilStateCreateInfo depthStencilState = @@ -839,7 +840,7 @@ public: dir = 0; 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; // Vertex bindings an attributes for model rendering @@ -922,6 +923,7 @@ public: { uboVS.projection = camera.matrices.perspective; uboVS.modelview = camera.matrices.view; + uboVS.inverseModelview = glm::inverse(camera.matrices.view); memcpy(uniformBuffers.matrices.mapped, &uboVS, sizeof(uboVS)); }