Skip to content

Commit

Permalink
Restructured how ViewDependentState is included
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed Sep 13, 2023
1 parent 0f15fdb commit 9ca066f
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 64 deletions.
4 changes: 3 additions & 1 deletion include/vsg/app/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/app/Camera.h>
#include <vsg/app/Window.h>
#include <vsg/nodes/Group.h>
#include <vsg/state/ViewDependentState.h>

namespace vsg
{

// forward declare
class ViewDependentState;

/// View is a Group class that pairs a Camera that defines the view with a subgraph that defines the scene that is being viewed/rendered
class VSG_DECLSPEC View : public Inherit<Group, View>
{
Expand Down
4 changes: 2 additions & 2 deletions include/vsg/state/ResourceHints.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
</editor-fold> */

#include <vsg/vk/DescriptorPool.h>
#include <vsg/maths/vec2.h>
#include <vsg/vk/DescriptorPool.h>

namespace vsg
{
Expand All @@ -35,7 +35,7 @@ namespace vsg

uivec2 numLightsRange = {8, 1024};
uivec2 numShadowMapsRange = {8, 64};
uivec2 shadowMapSize = { 2048, 2028 };
uivec2 shadowMapSize = {2048, 2028};

void read(Input& input) override;
void write(Output& output) const override;
Expand Down
16 changes: 5 additions & 11 deletions include/vsg/utils/GraphicsPipelineConfigurator.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ namespace vsg
bool assignDescriptor(const std::string& name, ref_ptr<Data> data = {}, uint32_t dstArrayElement = 0);
bool assignDescriptor(const std::string& name, const BufferInfoList& bufferInfoList, uint32_t dstArrayElement = 0);

[[deprecated("use enableDescriptor(..)")]]
bool enableUniform(const std::string& name) { return enableDescriptor(name); }
[[deprecated("use enableDescriptor(..)")]] bool enableUniform(const std::string& name) { return enableDescriptor(name); }

[[deprecated("use assignDescriptor(..)")]]
bool assignUniform(const std::string& name, ref_ptr<Data> data = {}, uint32_t dstArrayElement = 0) { return assignDescriptor(name, data, dstArrayElement); }

[[deprecated("use assignDescriptor(..)")]]
bool assignUniform(const std::string& name, const BufferInfoList& bufferInfoList, uint32_t dstArrayElement = 0) { return assignDescriptor(name, bufferInfoList, dstArrayElement); }
[[deprecated("use assignDescriptor(..)")]] bool assignUniform(const std::string& name, ref_ptr<Data> data = {}, uint32_t dstArrayElement = 0) { return assignDescriptor(name, data, dstArrayElement); }

[[deprecated("use assignDescriptor(..)")]] bool assignUniform(const std::string& name, const BufferInfoList& bufferInfoList, uint32_t dstArrayElement = 0) { return assignDescriptor(name, bufferInfoList, dstArrayElement); }

bool assignDescriptor(uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr<Descriptor> descriptor);

Expand Down Expand Up @@ -120,11 +116,9 @@ namespace vsg
bool assignDescriptor(const std::string& name, ref_ptr<Data> data = {});
bool assignTexture(const std::string& name, ref_ptr<Data> textureData = {}, ref_ptr<Sampler> sampler = {});

[[deprecated("use enableDescriptor(..)")]]
bool enableUniform(const std::string& name) { return enableDescriptor(name); }
[[deprecated("use enableDescriptor(..)")]] bool enableUniform(const std::string& name) { return enableDescriptor(name); }

[[deprecated("use assignDescriptor(..)")]]
bool assignUniform(const std::string& name, ref_ptr<Data> data = {}) { return assignDescriptor(name, data); }
[[deprecated("use assignDescriptor(..)")]] bool assignUniform(const std::string& name, ref_ptr<Data> data = {}) { return assignDescriptor(name, data); }

// setup by assign calls
ref_ptr<ShaderCompileSettings> shaderHints;
Expand Down
10 changes: 3 additions & 7 deletions include/vsg/utils/ShaderSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ namespace vsg
/// add an uniform binding. Not thread safe, should only be called when initially setting up the ShaderSet
void addDescriptorBinding(std::string name, std::string define, uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr<Data> data);

[[deprecated("use addDescriptorBinding(..)")]]
void addUniformBinding(std::string name, std::string define, uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr<Data> data) { addDescriptorBinding(name, define, set, binding, descriptorType, descriptorCount, stageFlags, data); }

[[deprecated("use addDescriptorBinding(..)")]] void addUniformBinding(std::string name, std::string define, uint32_t set, uint32_t binding, VkDescriptorType descriptorType, uint32_t descriptorCount, VkShaderStageFlags stageFlags, ref_ptr<Data> data) { addDescriptorBinding(name, define, set, binding, descriptorType, descriptorCount, stageFlags, data); }

/// add a push constant range. Not thread safe, should only be called when initially setting up the ShaderSet
void addPushConstantRange(std::string name, std::string define, VkShaderStageFlags stageFlags, uint32_t offset, uint32_t size);
Expand All @@ -155,11 +153,9 @@ namespace vsg
/// get the const DescriptorBinding associated with name
const DescriptorBinding& getDescriptorBinding(const std::string& name) const;

[[deprecated("use getDescriptorBinding(..)")]]
DescriptorBinding& getUniformBinding(const std::string& name) { return getDescriptorBinding(name); }
[[deprecated("use getDescriptorBinding(..)")]] DescriptorBinding& getUniformBinding(const std::string& name) { return getDescriptorBinding(name); }

[[deprecated("use getDescriptorBinding(..)")]]
const DescriptorBinding& getUnifomrBinding(const std::string& name) const { return getDescriptorBinding(name); }
[[deprecated("use getDescriptorBinding(..)")]] const DescriptorBinding& getUnifomrBinding(const std::string& name) const { return getDescriptorBinding(name); }

/// get the first ArrayState that has matches with defines in the specified list of defines.
ref_ptr<ArrayState> getSuitableArrayState(const std::set<std::string>& defines) const;
Expand Down
2 changes: 1 addition & 1 deletion include/vsg/vk/ResourceRequirements.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace vsg

uivec2 numLightsRange = {8, 1024};
uivec2 numShadowMapsRange = {8, 64};
uivec2 shadowMapSize = { 2048, 2028 };
uivec2 shadowMapSize = {2048, 2028};
};
VSG_type_name(vsg::ResourceRequirements);

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

Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/RecordTraversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/nodes/QuadGroup.h>
#include <vsg/nodes/StateGroup.h>
#include <vsg/nodes/Switch.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/threading/atomics.h>
#include <vsg/ui/ApplicationEvent.h>
#include <vsg/vk/CommandBuffer.h>
Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/RenderGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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/nodes/Bin.h>
#include <vsg/nodes/Light.h>
#include <vsg/state/MultisampleState.h>
#include <vsg/vk/Context.h>
#include <vsg/vk/State.h>
Expand Down
1 change: 1 addition & 0 deletions src/vsg/app/SecondaryCommandGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/app/View.h>
#include <vsg/commands/ExecuteCommands.h>
#include <vsg/io/DatabasePager.h>
#include <vsg/nodes/Light.h>
#include <vsg/ui/ApplicationEvent.h>
#include <vsg/vk/State.h>

Expand Down
5 changes: 3 additions & 2 deletions src/vsg/app/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/app/View.h>
#include <vsg/io/Options.h>
#include <vsg/nodes/Bin.h>
#include <vsg/state/ViewDependentState.h>

using namespace vsg;

Expand All @@ -27,7 +28,7 @@ static uint32_t getUniqueViewID()
uint32_t viewID = 0;
for (viewID = 0; viewID < static_cast<uint32_t>(s_ActiveViews.size()); ++viewID)
{
if (s_ActiveViews[viewID]==0)
if (s_ActiveViews[viewID] == 0)
{
++s_ActiveViews[viewID];
return viewID;
Expand Down Expand Up @@ -76,7 +77,7 @@ View::View(bool assignViewDependentState) :
info("View::View(bool) ", this, ", ", viewDependentState, ", ", viewID);
}

View::View(const View& view):
View::View(const View& view) :
Inherit(view),
viewID(sharedViewID(view.viewID)),
mask(view.mask)
Expand Down
1 change: 0 additions & 1 deletion src/vsg/app/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ void Viewer::assignRecordAndSubmitTaskAndPresentation(CommandGraphs in_commandGr
}
};


// find all the windows
struct FindWindows : public Visitor
{
Expand Down
12 changes: 8 additions & 4 deletions src/vsg/state/BufferInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,10 @@ bool vsg::createBufferAndTransferData(Context& context, const BufferInfoList& bu
Device* device = context.device;

VkDeviceSize alignment = 4;
if (usage == VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) alignment = device->getPhysicalDevice()->getProperties().limits.minUniformBufferOffsetAlignment;
else if (usage == VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) alignment = device->getPhysicalDevice()->getProperties().limits.minStorageBufferOffsetAlignment;
if (usage == VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
alignment = device->getPhysicalDevice()->getProperties().limits.minUniformBufferOffsetAlignment;
else if (usage == VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)
alignment = device->getPhysicalDevice()->getProperties().limits.minStorageBufferOffsetAlignment;

VkDeviceSize totalSize = 0;
VkDeviceSize offset = 0;
Expand Down Expand Up @@ -303,8 +305,10 @@ BufferInfoList vsg::createHostVisibleBuffer(Device* device, const DataList& data
BufferInfoList bufferInfoList;

VkDeviceSize alignment = 4;
if (usage == VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) alignment = device->getPhysicalDevice()->getProperties().limits.minUniformBufferOffsetAlignment;
else if (usage == VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) alignment = device->getPhysicalDevice()->getProperties().limits.minStorageBufferOffsetAlignment;
if (usage == VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
alignment = device->getPhysicalDevice()->getProperties().limits.minUniformBufferOffsetAlignment;
else if (usage == VK_BUFFER_USAGE_STORAGE_BUFFER_BIT)
alignment = device->getPhysicalDevice()->getProperties().limits.minStorageBufferOffsetAlignment;

VkDeviceSize totalSize = 0;
VkDeviceSize offset = 0;
Expand Down
55 changes: 23 additions & 32 deletions src/vsg/state/ViewDependentState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/core/compare.h>
#include <vsg/io/Logger.h>
#include <vsg/io/Options.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/io/write.h>
#include <vsg/state/DescriptorImage.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/vk/Context.h>
#include <vsg/io/write.h>

using namespace vsg;

Expand All @@ -30,8 +30,8 @@ namespace vsg
class TraverseChildrenOfNode : public vsg::Inherit<vsg::Node, TraverseChildrenOfNode>
{
public:

explicit TraverseChildrenOfNode(vsg::Node* in_node) : node(in_node) {}
explicit TraverseChildrenOfNode(vsg::Node* in_node) :
node(in_node) {}

vsg::observer_ptr<vsg::Node> node;

Expand All @@ -46,7 +46,7 @@ namespace vsg
void traverse(RecordTraversal& visitor) const override { t_traverse(*this, visitor); }
};
VSG_type_name(vsg::TraverseChildrenOfNode);
}
} // namespace vsg

//////////////////////////////////////
//
Expand Down Expand Up @@ -179,7 +179,6 @@ void ViewDependentState::init(uint32_t maxNumberLights, uint32_t maxViewports, u
shadowMapData = floatArray3D::create(2048, 2048, maxShadowMaps, vsg::Data::Properties{VK_FORMAT_R32_SFLOAT});
shadowMapImages = DescriptorImage::create(shadwoMapSampler, shadowMapData, 2);


DescriptorSetLayoutBindings descriptorBindings{
VkDescriptorSetLayoutBinding{0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}, // lightData
VkDescriptorSetLayoutBinding{1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, nullptr}, // viewportData
Expand All @@ -189,20 +188,18 @@ void ViewDependentState::init(uint32_t maxNumberLights, uint32_t maxViewports, u
descriptorSetLayout = DescriptorSetLayout::create(descriptorBindings);
descriptorSet = DescriptorSet::create(descriptorSetLayout, Descriptors{descriptor, shadowMapImages});

for(uint32_t k = 0; k < shadowMapData->depth(); ++k)
for (uint32_t k = 0; k < shadowMapData->depth(); ++k)
{
for(uint32_t j = 0; j < shadowMapData->height(); ++j)
for (uint32_t j = 0; j < shadowMapData->height(); ++j)
{
float* data = shadowMapData->data(shadowMapData->index(0, j, k));
for(uint32_t i = 0; i < shadowMapData->width(); ++i)
for (uint32_t i = 0; i < shadowMapData->width(); ++i)
{
*(data++) = static_cast<float>(sin(vsg::PI * static_cast<double>(i)/static_cast<double>(shadowMapData->width()-1)));
*(data++) = static_cast<float>(sin(vsg::PI * static_cast<double>(i) / static_cast<double>(shadowMapData->width() - 1)));
}
}
}



// create a switch to toggle on/off the render to texture subgraphs for each shadowmap layer
preRenderSwitch = Switch::create();

Expand All @@ -216,7 +213,7 @@ void ViewDependentState::init(uint32_t maxNumberLights, uint32_t maxViewports, u

ref_ptr<View> first_view;
shadowMaps.resize(maxShadowMaps);
for(auto& shadowMap : shadowMaps)
for (auto& shadowMap : shadowMaps)
{
if (first_view)
{
Expand Down Expand Up @@ -290,7 +287,6 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
// light direction in world coords
auto light_direction = normalize(light->direction * (inverse_3x3(mv * inverse(viewMatrix))));


info(" directional light : light direction in world = ", light_direction, ", light->shadowMaps = ", light->shadowMaps);
info(" light->direction in model = ", light->direction);
info(" view_direction in world = ", view_direction);
Expand All @@ -303,8 +299,7 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
auto light_y = cross(light_x, light_direction);
auto light_z = light_direction;

auto computeFrustumBounds = [&](double n, double f, const dmat4& clipToWorld) -> dbox
{
auto computeFrustumBounds = [&](double n, double f, const dmat4& clipToWorld) -> dbox {
dbox bounds;
bounds.add(clipToWorld * dvec3(-1.0, -1.0, n));
bounds.add(clipToWorld * dvec3(-1.0, 1.0, n));
Expand All @@ -318,19 +313,16 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
return bounds;
};

auto Clog = [](double n, double f, double i, double m) -> double
{
return n * std::pow((f/n), (i/m));
auto Clog = [](double n, double f, double i, double m) -> double {
return n * std::pow((f / n), (i / m));
};

auto Cuniform = [](double n, double f, double i, double m) -> double
{
return n + (f - n) * (i/m);
auto Cuniform = [](double n, double f, double i, double m) -> double {
return n + (f - n) * (i / m);
};

auto Cpractical = [&Clog, &Cuniform](double n, double f, double i, double m, double lambda) -> double
{
return Clog(n, f, i, m) * lambda + Cuniform(n, f, i, m) * (1.0-lambda);
auto Cpractical = [&Clog, &Cuniform](double n, double f, double i, double m, double lambda) -> double {
return Clog(n, f, i, m) * lambda + Cuniform(n, f, i, m) * (1.0 - lambda);
};

info(" light_x = ", light_x);
Expand All @@ -342,7 +334,7 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
auto n = -(clipToEye * dvec3(0.0, 0.0, 1.0)).z;
auto f = -(clipToEye * dvec3(0.0, 0.0, 0.0)).z;

#if 1
# if 1
// clamp the near and far values
double maxShadowDistance = 1000.0;
if (n > maxShadowDistance)
Expand All @@ -355,8 +347,8 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
{
f = maxShadowDistance;
}
#endif
double range = f-n;
# endif
double range = f - n;
info(" n = ", n, ", f = ", f, ", range = ", range);

auto clipToWorld = inverse(projectionMatrix * viewMatrix);
Expand All @@ -367,10 +359,10 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
{
double lambda = 0.5;
double m = static_cast<double>(numShadowMapsForThisLight);
for(double i = 0; i < m; i += 1.0)
for (double i = 0; i < m; i += 1.0)
{
dvec3 eye_near(0.0, 0.0, -Cpractical(n, f, i, m, lambda));
dvec3 eye_far(0.0, 0.0, -Cpractical(n, f, i+1.0, m, lambda));
dvec3 eye_far(0.0, 0.0, -Cpractical(n, f, i + 1.0, m, lambda));

auto clip_near = projectionMatrix * eye_near;
auto clip_far = projectionMatrix * eye_far;
Expand Down Expand Up @@ -414,7 +406,6 @@ void ViewDependentState::traverse(RecordTraversal& rt) const
auto& camera = shadowMap.view->camera;
info(" need to set camera ", camera);
}

}
#endif

Expand All @@ -427,7 +418,7 @@ void ViewDependentState::traverse(RecordTraversal& rt) const

for (auto& [mv, light] : spotLights)
{
auto eye_position = mv * light->position;
auto eye_position = mv * light->position;
auto eye_direction1 = normalize(light->direction * inverse_3x3(mv));
info(" spot light : light->direction = ", light->direction, ", position = ", eye_position, ", direction = ", eye_direction1, ", light->shadowMaps = ", light->shadowMaps);
}
Expand Down
7 changes: 4 additions & 3 deletions src/vsg/vk/ResourceRequirements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
#include <vsg/nodes/VertexIndexDraw.h>
#include <vsg/state/DescriptorImage.h>
#include <vsg/state/MultisampleState.h>
#include <vsg/state/ViewDependentState.h>
#include <vsg/vk/RenderPass.h>
#include <vsg/vk/ResourceRequirements.h>

Expand All @@ -34,7 +35,7 @@ using namespace vsg;
//
ResourceRequirements::ResourceRequirements(ref_ptr<ResourceHints> hints)
{
vsg::info("ResourceRequirements::ResourceRequirements(" , hints, " ) ", this);
vsg::info("ResourceRequirements::ResourceRequirements(", hints, " ) ", this);

viewDetailsStack.push(ResourceRequirements::ViewDetails{});
if (hints) apply(*hints);
Expand Down Expand Up @@ -214,7 +215,7 @@ void CollectResourceRequirements::apply(const DescriptorImage& descriptorImage)

void CollectResourceRequirements::apply(const Light& light)
{
vsg::info("CollectResourceRequirements::apply(", light.className(),")");
vsg::info("CollectResourceRequirements::apply(", light.className(), ")");
requirements.viewDetailsStack.top().lights.insert(&light);
}

Expand Down Expand Up @@ -254,7 +255,7 @@ void CollectResourceRequirements::apply(const View& view)
vsg::info("CollectResourceRequirements::apply(const View& view) after collecting stats from ", view.viewDependentState);

uint32_t numShadowMaps = 0;
for(auto& light : viewDetails.lights)
for (auto& light : viewDetails.lights)
{
numShadowMaps += light->shadowMaps;
}
Expand Down

0 comments on commit 9ca066f

Please sign in to comment.