Skip to content

Commit

Permalink
Add shadow sampler, tho it broke something
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-han committed Sep 28, 2024
1 parent b1eb808 commit d3ea2d7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 31 deletions.
23 changes: 18 additions & 5 deletions Shaders/blinn-phong.frag
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,21 @@ layout(location = 0) out vec4 outColor;
#include "mesh_push_constants.glsl"

// TODO subpasses w/ VK_KHR_dynamic_rendering_local_read
layout (set = 0, binding = 0) uniform sampler linearSampler;
// layout (set = 0, binding = 0) uniform sampler linearSampler;
#define LINEAR_SAMPLER 0
#define SHADOW_SAMPLER 1
layout (set = 0, binding = 0) uniform sampler samplers[];
layout (set = 0, binding = 1) uniform texture2D textures[];

vec4 sampleTextureLinear(texture2D tex, vec2 texCoords) {
return texture(sampler2D(tex, samplers[LINEAR_SAMPLER]), texCoords);
}

vec4 sampleTextureShadow(texture2D tex, vec2 texCoords) {
return texture(sampler2D(tex, samplers[SHADOW_SAMPLER]), texCoords);
}


layout (set = 1, binding = 0) uniform texture2D albedoBuffer;
layout (set = 1, binding = 1) uniform texture2D normalsBuffer;
layout (set = 1, binding = 2) uniform texture2D metallicRoughnessBuffer;
Expand All @@ -31,7 +43,8 @@ float calculateShadow(vec3 norm, vec3 fragWorldPos) {
vec3 lightDir = normalize(pushConstants.sceneData.directionalLight.direction);
float bias = max(0.005, 0.05 * (1.0 - dot(norm, lightDir))); // Goes from 0.005 to 0.05 as the angle between the light and the normal increases
float currentDepth = fragDirLightSpacePos.z;
float shadowMapDepth = texture(sampler2D(directionalLightShadowMap, linearSampler), shadowSamplePos.xy).r;
// float shadowMapDepth = texture(sampler2D(directionalLightShadowMap, linearSampler), shadowSamplePos.xy).r;
float shadowMapDepth = sampleTextureLinear(directionalLightShadowMap, shadowSamplePos.xy).r;
if (shadowMapDepth < (currentDepth - bias)) {
inShadow = 1.0;
}
Expand Down Expand Up @@ -109,9 +122,9 @@ vec3 calculatePointLightsContribution(int pointLightIndex, vec3 diffuseTexColor,

void main() {
// Sample GBuffer
vec3 sampledColor = texelFetch(sampler2D(albedoBuffer, linearSampler), ivec2(gl_FragCoord.xy), 0).rgb;
vec3 sampledNormal = normalize(texelFetch(sampler2D(normalsBuffer, linearSampler), ivec2(gl_FragCoord.xy), 0).rgb * 2.0 - 1.0);
vec2 sampledMetallicRoughness = texelFetch(sampler2D(metallicRoughnessBuffer, linearSampler), ivec2(gl_FragCoord.xy), 0).rg;
vec3 sampledColor = texelFetch(sampler2D(albedoBuffer, samplers[LINEAR_SAMPLER]), ivec2(gl_FragCoord.xy), 0).rgb;
vec3 sampledNormal = normalize(texelFetch(sampler2D(normalsBuffer, samplers[LINEAR_SAMPLER]), ivec2(gl_FragCoord.xy), 0).rgb * 2.0 - 1.0);
vec2 sampledMetallicRoughness = texelFetch(sampler2D(metallicRoughnessBuffer, samplers[LINEAR_SAMPLER]), ivec2(gl_FragCoord.xy), 0).rg;
float sampledDepth = texelFetch(depthBuffer, ivec2(gl_FragCoord.xy), 0).r;
// x,y are [0, 1] and so is depth-z [0, 1]
// sampledDepth = sampledDepth * 2.0 - 1.0; // [-1, 1] // In Vulkan, NDC is [0, 1] in z, unlike OpenGL which expects [-1, 1]
Expand Down
52 changes: 37 additions & 15 deletions Source/Rendering/Core/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,26 @@ namespace MagicRed::Rendering
// .maxAnisotropy = maxAnisotropy,
};
vkCreateSampler(m_GfxDevice, &linearCI, nullptr, &m_linearSampler);
VkSamplerCreateInfo nearestCI = {
VkSamplerCreateInfo shadowSamplerCI = {
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
.magFilter = VK_FILTER_NEAREST,
.minFilter = VK_FILTER_NEAREST,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
.magFilter = VK_FILTER_LINEAR,
.minFilter = VK_FILTER_LINEAR,
.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
};
vkCreateSampler(m_GfxDevice, &nearestCI, nullptr, &m_nearestSampler);
vkCreateSampler(m_GfxDevice, &shadowSamplerCI, nullptr, &m_shadowSampler);
}

void Renderer::init_bindless_descriptors() {
constexpr uint32_t maxBindlessResourceCount = 16536; // Requires MVK_CONFIG_USE_METAL_ARGUMENT_BUFFERS
constexpr uint32_t maxSamplerCount = 1;
constexpr uint32_t maxSamplerCount = 2;

// Create a global descriptor pool, and let it know how many of each descriptor type we want up front
std::array<VkDescriptorPoolSize, 2> bindlessDescriptorPoolSizes {{
{ VK_DESCRIPTOR_TYPE_SAMPLER, maxSamplerCount}, // TODO: We'll just have 1 nearest and 1 linear sampler for now
{ VK_DESCRIPTOR_TYPE_SAMPLER, maxSamplerCount},
{ VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, maxBindlessResourceCount}
}};
VkDescriptorPoolCreateInfo poolCreateInfo = {
Expand Down Expand Up @@ -218,11 +222,11 @@ namespace MagicRed::Rendering
vkCreateDescriptorSetLayout(m_GfxDevice, &bindlessSetLayoutCreateInfo, nullptr, &m_bindlessDescriptorSetLayout);

// Allocate the descriptor set
uint32_t maxBinding = maxBindlessResourceCount - 1;
uint32_t maxBinding = maxBindlessResourceCount;
VkDescriptorSetVariableDescriptorCountAllocateInfoEXT variableDescriptorCountInfo {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO_EXT,
.descriptorSetCount = 1,
.pDescriptorCounts = &maxBinding // Number of descriptors, -1?
.pDescriptorCounts = &maxBinding
};
VkDescriptorSetAllocateInfo allocateInfo = {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
Expand Down Expand Up @@ -542,6 +546,8 @@ namespace MagicRed::Rendering
}

void Renderer::update_bindless_texture_descriptors() {
constexpr uint32_t bindlessSamplerBinding = 0;
constexpr uint32_t bindlessTextureBinding = 1;

// TODO: should batch things per frame?

Expand All @@ -559,7 +565,7 @@ namespace MagicRed::Rendering
textureDescriptorWrites[i].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
textureDescriptorWrites[i].pNext = nullptr;
textureDescriptorWrites[i].dstSet = m_bindlessDescriptorSet;
textureDescriptorWrites[i].dstBinding = 1;
textureDescriptorWrites[i].dstBinding = bindlessTextureBinding;
textureDescriptorWrites[i].dstArrayElement = i;
textureDescriptorWrites[i].descriptorCount = 1;
textureDescriptorWrites[i].descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
Expand All @@ -574,20 +580,36 @@ namespace MagicRed::Rendering
VkDescriptorImageInfo linearSamplerInfo = {
.sampler = m_linearSampler
};
VkDescriptorImageInfo shadowSamplerInfo = {
.sampler = m_shadowSampler
};
VkWriteDescriptorSet linearSamplerDescriptorWrite = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = m_bindlessDescriptorSet,
.dstBinding = 0,
.dstArrayElement = {},
.dstBinding = bindlessSamplerBinding,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.pImageInfo = &linearSamplerInfo,
.pBufferInfo = nullptr,
.pTexelBufferView = nullptr
};
VkWriteDescriptorSet shadowSamplerDescriptorWrite = {
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = m_bindlessDescriptorSet,
.dstBinding = bindlessSamplerBinding,
.dstArrayElement = 1,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER,
.pImageInfo = &shadowSamplerInfo,
.pBufferInfo = nullptr,
.pTexelBufferView = nullptr
};
std::array<VkWriteDescriptorSet, 2> samplerDescriptorWrites = {linearSamplerDescriptorWrite, shadowSamplerDescriptorWrite};

vkUpdateDescriptorSets(m_GfxDevice, 1, &linearSamplerDescriptorWrite, 0, nullptr);
vkUpdateDescriptorSets(m_GfxDevice, static_cast<uint32_t>(samplerDescriptorWrites.size()), samplerDescriptorWrites.data(), 0, nullptr);
}

void Renderer::init_imgui() {
Expand Down Expand Up @@ -1291,8 +1313,8 @@ namespace MagicRed::Rendering
vkDestroyDescriptorPool(m_GfxDevice, m_globalDescriptorPool, nullptr);


vkDestroySampler(m_GfxDevice, m_linearSampler, nullptr);
vkDestroySampler(m_GfxDevice, m_nearestSampler, nullptr);
vkDestroySampler(m_GfxDevice, m_linearSampler, nullptr);
vkDestroySampler(m_GfxDevice, m_shadowSampler, nullptr);

m_RenderTextureCache.cleanup(m_GfxDevice);
m_TextureCache.cleanup(m_GfxDevice);
Expand Down
2 changes: 1 addition & 1 deletion Source/Rendering/Core/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace MagicRed::Rendering

// Samplers
VkSampler m_linearSampler;
VkSampler m_nearestSampler;
VkSampler m_shadowSampler;

// Imgui
bool m_bShowRenderingMenu = true;
Expand Down
10 changes: 0 additions & 10 deletions Source/Rendering/RenderStages/BlinnPhongLightingStage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,6 @@ namespace MagicRed::Rendering
.SetBlendEnable(false)
.SetCullMode(VK_CULL_MODE_NONE)
.Build();
// VertexInputDescription vertexDescription;
// m_pipeline.BuildPipeline(
// _pipelineRenderingCreateInfo
// , m_vertexShaderPath, m_fragmentShaderPath
// , vertexDescription
// , m_pushConstantRanges
// , descriptorSetLayouts
// , m_extent
// , false
// );
}

BlinnPhongLightingStage::~BlinnPhongLightingStage() {}
Expand Down

0 comments on commit d3ea2d7

Please sign in to comment.