Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed Apr 19, 2024
2 parents 13b3d2d + 9948424 commit 6117dfd
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 29 deletions.
8 changes: 3 additions & 5 deletions include/vsg/state/ViewDependentState.h
Expand Up @@ -133,18 +133,16 @@ namespace vsg

View* view = nullptr;

// ShaderSet to guide the set up of lightData, viewportData and shadowMap Descriptors & DescriptorSet(s).
ref_ptr<ShaderSet> shaderSet;

ref_ptr<vec4Array> lightData;
ref_ptr<BufferInfo> lightDataBufferInfo;
ref_ptr<DescriptorBuffer> lightDataDescriptor;

ref_ptr<vec4Array> viewportData;
ref_ptr<BufferInfo> viewportDataBufferInfo;
ref_ptr<DescriptorBuffer> viewportDescriptor;

ref_ptr<Image> shadowDepthImage;
ref_ptr<DescriptorImage> shadowMapImages;
ref_ptr<DescriptorImage> shadowMapDirectSamplerDescriptor;
ref_ptr<DescriptorImage> shadowMapShadowSamplerDescriptor;

ref_ptr<DescriptorSetLayout> descriptorSetLayout;
ref_ptr<DescriptorSet> descriptorSet;
Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/CommandGraph.cpp
Expand Up @@ -16,6 +16,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/io/DatabasePager.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/ui/ApplicationEvent.h>
#include <vsg/utils/ShaderSet.h>
#include <vsg/vk/State.h>

using namespace vsg;
Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/CompileManager.cpp
Expand Up @@ -17,6 +17,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/io/Logger.h>
#include <vsg/io/Options.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/utils/ShaderSet.h>

using namespace vsg;

Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/CompileTraversal.cpp
Expand Up @@ -24,6 +24,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/nodes/StateGroup.h>
#include <vsg/state/MultisampleState.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/utils/ShaderSet.h>
#include <vsg/vk/RenderPass.h>
#include <vsg/vk/State.h>

Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/View.cpp
Expand Up @@ -14,6 +14,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/io/Options.h>
#include <vsg/nodes/Bin.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/utils/ShaderSet.h>
#include <vsg/vk/Context.h>

using namespace vsg;
Expand Down
55 changes: 31 additions & 24 deletions src/vsg/state/ViewDependentState.cpp
Expand Up @@ -26,6 +26,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/nodes/RegionOfInterest.h>
#include <vsg/state/DescriptorImage.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/utils/GraphicsPipelineConfigurator.h>
#include <vsg/utils/ShaderSet.h>
#include <vsg/vk/Context.h>

using namespace vsg;
Expand Down Expand Up @@ -212,6 +214,14 @@ void ViewDependentState::init(ResourceRequirements& requirements)
// check if ViewDependentState has already been initialized
if (lightData) return;

if (!shaderSet)
{
// fallback to using the standard PBR ShaderSet
shaderSet = vsg::createPhysicsBasedRenderingShaderSet();
}

auto descriptorConfigurator = DescriptorConfigurator::create(shaderSet);

uint32_t maxNumberLights = 64;
uint32_t maxViewports = 1;

Expand Down Expand Up @@ -280,14 +290,12 @@ void ViewDependentState::init(ResourceRequirements& requirements)
lightData = vec4Array::create(lightDataSize);
lightData->properties.dataVariance = DYNAMIC_DATA_TRANSFER_AFTER_RECORD;
lightDataBufferInfo = BufferInfo::create(lightData.get());

lightDataDescriptor = DescriptorBuffer::create(BufferInfoList{lightDataBufferInfo}, 0, 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); // hardwired position for now
descriptorConfigurator->assignDescriptor("lightData", BufferInfoList{lightDataBufferInfo});

viewportData = vec4Array::create(maxViewports);
viewportData->properties.dataVariance = DYNAMIC_DATA_TRANSFER_AFTER_RECORD;
viewportDataBufferInfo = BufferInfo::create(viewportData.get());

viewportDescriptor = DescriptorBuffer::create(BufferInfoList{viewportDataBufferInfo}, 1, 0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER); // hardwired position for now
descriptorConfigurator->assignDescriptor("viewportData", BufferInfoList{viewportDataBufferInfo});

// set up ShadowMaps
auto shadowMapDirectSampler = Sampler::create();
Expand All @@ -297,29 +305,23 @@ void ViewDependentState::init(ResourceRequirements& requirements)
shadowMapDirectSampler->addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapDirectSampler->addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapDirectSampler->addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapDirectSamplerDescriptor = createSamplerDescriptor(shadowMapDirectSampler, 3);

auto shadowMapSampler = Sampler::create();
shadowMapSampler->addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapSampler->addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapSampler->addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
#define HARDWARE_PCF 1
#if HARDWARE_PCF == 1
shadowMapSampler->minFilter = VK_FILTER_LINEAR;
shadowMapSampler->magFilter = VK_FILTER_LINEAR;
shadowMapSampler->mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
shadowMapSampler->addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapSampler->addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapSampler->addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapSampler->compareEnable = VK_TRUE;
shadowMapSampler->compareOp = VK_COMPARE_OP_LESS;
#else
shadowMapSampler->minFilter = VK_FILTER_NEAREST;
shadowMapSampler->magFilter = VK_FILTER_NEAREST;
shadowMapSampler->mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
shadowMapSampler->addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapSampler->addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
shadowMapSampler->addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
#endif
shadowMapShadowSamplerDescriptor = createSamplerDescriptor(shadowMapSampler, 4);

if (maxShadowMaps > 0)
{
shadowDepthImage = createShadowImage(shadowWidth, shadowHeight, maxShadowMaps, VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT);
Expand All @@ -333,7 +335,7 @@ void ViewDependentState::init(ResourceRequirements& requirements)

auto depthImageInfo = ImageInfo::create(nullptr, depthImageView, VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);

shadowMapImages = DescriptorImage::create(ImageInfoList{depthImageInfo}, 2, 0, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
descriptorConfigurator->assignTexture("shadowMaps", ImageInfoList{depthImageInfo});
}
else
{
Expand All @@ -357,19 +359,24 @@ void ViewDependentState::init(ResourceRequirements& requirements)

auto depthImageInfo = ImageInfo::create(nullptr, depthImageView, VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);

shadowMapImages = DescriptorImage::create(ImageInfoList{depthImageInfo}, 2, 0, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE);
descriptorConfigurator->assignTexture("shadowMaps", ImageInfoList{depthImageInfo});
}

DescriptorSetLayoutBindings descriptorBindings{
VkDescriptorSetLayoutBinding{0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}, // lightData
VkDescriptorSetLayoutBinding{1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}, // viewportData
VkDescriptorSetLayoutBinding{2, VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}, // shadow map 2D texture array
VkDescriptorSetLayoutBinding{3, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}, // shadow map direct sampler
VkDescriptorSetLayoutBinding{4, VK_DESCRIPTOR_TYPE_SAMPLER, 1, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}, // shadow map shadow sampler
};
auto shadowMapDirectSamplerInfo = ImageInfo::create(shadowMapDirectSampler, nullptr, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
auto shadowMapSamplerInfo = ImageInfo::create(shadowMapSampler, nullptr, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);

descriptorSetLayout = DescriptorSetLayout::create(descriptorBindings);
descriptorSet = DescriptorSet::create(descriptorSetLayout, Descriptors{lightDataDescriptor, viewportDescriptor, shadowMapImages, shadowMapDirectSamplerDescriptor, shadowMapShadowSamplerDescriptor});
descriptorConfigurator->assignTexture("shadowMapDirectSampler", ImageInfoList{shadowMapDirectSamplerInfo});
descriptorConfigurator->assignTexture("shadowMapShadowSampler", ImageInfoList{shadowMapSamplerInfo});

// assign the DescriptorSet and layout created by the descriptorConfigurator
for (size_t set = 0; set < descriptorConfigurator->descriptorSets.size(); ++set)
{
if (auto ds = descriptorConfigurator->descriptorSets[set])
{
descriptorSet = ds;
descriptorSetLayout = ds->setLayout;
}
}

// if not active then don't enable shadow maps
if (maxShadowMaps == 0) return;
Expand Down
1 change: 1 addition & 0 deletions src/vsg/vk/ResourceRequirements.cpp
Expand Up @@ -25,6 +25,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/state/DescriptorImage.h>
#include <vsg/state/MultisampleState.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/utils/ShaderSet.h>
#include <vsg/vk/Context.h>
#include <vsg/vk/RenderPass.h>
#include <vsg/vk/ResourceRequirements.h>
Expand Down

0 comments on commit 6117dfd

Please sign in to comment.