Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shared View::viewID #958

Merged
merged 6 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/vsg/app/View.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace vsg
public:
View();

// share the specified view's children, viewID, mask and camera ViewportState
View(const View& view);

explicit View(ref_ptr<Camera> in_camera, ref_ptr<Node> in_scenegraph = {});

template<class N, class V>
Expand All @@ -46,6 +49,9 @@ namespace vsg
void accept(ConstVisitor& visitor) const override { t_accept(*this, visitor); }
void accept(RecordTraversal& visitor) const override { t_accept(*this, visitor); }

/// share the specified view's viewID, mask, camera ViewportState, with this View
void share(const View& view);

/// camera controls the viewport state and projection and view matrices
ref_ptr<Camera> camera;

Expand Down
4 changes: 4 additions & 0 deletions include/vsg/state/GraphicsPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ namespace vsg

GraphicsPipeline(PipelineLayout* pipelineLayout, const ShaderStages& shaderStages, const GraphicsPipelineStates& pipelineStates, uint32_t subpass = 0);

/// return the Vukan Pipeline for specified viewID.
VkPipeline vk(uint32_t viewID) const { return _implementation[viewID]->_pipeline; }

/// variant of vk(viewID) method that is slower but adds validation of the viewID parameter
VkPipeline validated_vk(uint32_t viewID) const { return (viewID < _implementation.size()) ? (_implementation[viewID] ? _implementation[viewID]->_pipeline : 0) : 0; }

/// VkGraphicsPipelineCreateInfo settings
ShaderStages stages;
GraphicsPipelineStates pipelineStates;
Expand Down
54 changes: 49 additions & 5 deletions src/vsg/app/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace vsg;

// thread safe container for managing the deviceID for each vsg::View
static std::mutex s_ViewCountMutex;
static std::vector<bool> s_ActiveViews;
static std::vector<uint32_t> s_ActiveViews;

static uint32_t getUniqueViewID()
{
Expand All @@ -27,22 +27,38 @@ static uint32_t getUniqueViewID()
uint32_t viewID = 0;
for (viewID = 0; viewID < static_cast<uint32_t>(s_ActiveViews.size()); ++viewID)
{
if (!s_ActiveViews[viewID])
if (s_ActiveViews[viewID]==0)
{
s_ActiveViews[viewID] = true;
++s_ActiveViews[viewID];
return viewID;
}
}

s_ActiveViews.push_back(true);
s_ActiveViews.push_back(1);

return viewID;
}

static uint32_t sharedViewID(uint32_t viewID)
{
std::scoped_lock<std::mutex> guard(s_ViewCountMutex);

if (viewID < static_cast<uint32_t>(s_ActiveViews.size()))
{
++s_ActiveViews[viewID];
return viewID;
}

viewID = static_cast<uint32_t>(s_ActiveViews.size());
s_ActiveViews.push_back(1);

return viewID;
}

static void releaseViewID(uint32_t viewID)
{
std::scoped_lock<std::mutex> guard(s_ViewCountMutex);
s_ActiveViews[viewID] = false;
--s_ActiveViews[viewID];
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -55,6 +71,18 @@ View::View() :
{
}

View::View(const View& view):
Inherit(view),
viewID(sharedViewID(view.viewID)),
mask(view.mask)
{
if (view.camera && view.camera->viewportState)
{
camera = vsg::Camera::create();
camera->viewportState = view.camera->viewportState;
}
}

View::View(ref_ptr<Camera> in_camera, ref_ptr<Node> in_scenegraph) :
camera(in_camera),
viewID(getUniqueViewID()),
Expand All @@ -67,3 +95,19 @@ View::~View()
{
releaseViewID(viewID);
}

void View::share(const View& view)
{
if (viewID != view.viewID)
{
releaseViewID(viewID);
const_cast<uint32_t&>(viewID) = sharedViewID(view.viewID);
}

mask = view.mask;
if (view.camera && view.camera->viewportState)
{
if (!camera) camera = vsg::Camera::create();
camera->viewportState = view.camera->viewportState;
}
}
Loading