Skip to content

Commit

Permalink
Merge pull request #298 from vsg-dev/TransferOnDemand
Browse files Browse the repository at this point in the history
Added test for using dataVaraicene set to vsg::STATIC_DATA but manually adding the BufferInfo each time it's updated.
  • Loading branch information
robertosfield authored Mar 28, 2024
2 parents 58c2982 + 83f39a4 commit 412fae7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
10 changes: 6 additions & 4 deletions examples/state/vsgdynamictexture/vsgdynamictexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ int main(int argc, char** argv)
if (arguments.read("--rgb")) arrayType = USE_RGB;
if (arguments.read("--rgba")) arrayType = USE_RGBA;
auto image_size = arguments.value<uint32_t>(256, "-s");
bool lateTransfer = arguments.read("--late");
bool multiThreading = arguments.read("--mt");
vsg::DataVariance dataVariance = vsg::DYNAMIC_DATA;
if (arguments.read("--static")) dataVariance = vsg::STATIC_DATA;
else if (arguments.read("--late")) dataVariance = vsg::DYNAMIC_DATA_TRANSFER_AFTER_RECORD;

vsg::GeometryInfo geomInfo;
vsg::StateInfo stateInfo;
Expand Down Expand Up @@ -147,6 +149,7 @@ int main(int argc, char** argv)
// use float image - typically for displacementMap
textureData = vsg::floatArray2D::create(image_size, image_size);
textureData->properties.format = VK_FORMAT_R32_SFLOAT;
textureData->properties.dataVariance = dataVariance;
break;
case (USE_RGB):
// note, RGB image data has to be converted to RGBA when copying to a VkImage,
Expand All @@ -155,17 +158,16 @@ int main(int argc, char** argv)
// one approach, illustrated in the vsgdynamictexture_cs example, for avoiding this conversion overhead is to use a compute shader to map the RGB data to RGBA.
textureData = vsg::vec3Array2D::create(image_size, image_size);
textureData->properties.format = VK_FORMAT_R32G32B32_SFLOAT;
textureData->properties.dataVariance = dataVariance;
break;
case (USE_RGBA):
// R, RG and RGBA data can be copied to VkImage without any conversion so is efficient, while RGB requires conversion, see above explanation
textureData = vsg::vec4Array2D::create(image_size, image_size);
textureData->properties.format = VK_FORMAT_R32G32B32A32_SFLOAT;
textureData->properties.dataVariance = dataVariance;
break;
}

// set the dynamic hint to tell the Viewer::compile() to assign this vsg::Data to a vsg::TransferTask
textureData->properties.dataVariance = lateTransfer ? vsg::DYNAMIC_DATA_TRANSFER_AFTER_RECORD : vsg::DYNAMIC_DATA;

// initialize the image
UpdateImage updateImage;
updateImage(textureData, 0.0);
Expand Down
43 changes: 33 additions & 10 deletions examples/state/vsgdynamicvertex/vsgdynamicvertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ class FindVertexData : public vsg::Visitor
{
if (geometry.arrays.empty()) return;
geometry.arrays[0]->data->accept(*this);
bufferInfoSet.insert(geometry.arrays[0]);
}

void apply(vsg::VertexIndexDraw& vid)
{
if (vid.arrays.empty()) return;
vid.arrays[0]->data->accept(*this);
bufferInfoSet.insert(vid.arrays[0]);
}

void apply(vsg::BindVertexBuffers& bvd)
{
if (bvd.arrays.empty()) return;
bvd.arrays[0]->data->accept(*this);
bufferInfoSet.insert(bvd.arrays[0]);
}

void apply(vsg::vec3Array& vertices)
Expand All @@ -54,6 +57,8 @@ class FindVertexData : public vsg::Visitor
}

std::set<vsg::vec3Array*> verticesSet;
std::set<vsg::ref_ptr<vsg::BufferInfo>> bufferInfoSet;

};

int main(int argc, char** argv)
Expand Down Expand Up @@ -99,10 +104,13 @@ int main(int argc, char** argv)
auto numFrames = arguments.value(-1, "-f");

bool multiThreading = arguments.read("--mt");
auto modify = arguments.read("--modify");
auto modify = !arguments.read("--no-modify");
auto dirty = arguments.read("--dirty");
auto dynamic = arguments.read("--dynamic") || dirty || modify;
bool lateTransfer = arguments.read("--late");

// set the dynamic hint to tell the Viewer::compile() to assign this vsg::Data to a vsg::TransferTask
vsg::DataVariance dataVariance = vsg::DYNAMIC_DATA;
if (arguments.read("--static")) dataVariance = vsg::STATIC_DATA;
else if (arguments.read("--late")) dataVariance = vsg::DYNAMIC_DATA_TRANSFER_AFTER_RECORD;

if (arguments.errors()) return arguments.writeErrorMessages(std::cerr);

Expand All @@ -122,14 +130,13 @@ int main(int argc, char** argv)

// visit the scene graph to collect all the vertex arrays
size_t numVertices = 0;
auto verticesList = vsg::visit<FindVertexData>(vsg_scene).getVerticesList();
if (dynamic)
FindVertexData fdv;
vsg_scene->accept(fdv);
auto verticesList = fdv.getVerticesList();
for(auto& vertices : verticesList)
{
for(auto& vertices : verticesList)
{
vertices->properties.dataVariance = lateTransfer ? vsg::DYNAMIC_DATA_TRANSFER_AFTER_RECORD : vsg::DYNAMIC_DATA;
numVertices += vertices->size();
}
vertices->properties.dataVariance = dataVariance;
numVertices += vertices->size();
}

vsg::info("number of dynamic vertex arrays : ", verticesList.size());
Expand Down Expand Up @@ -211,6 +218,22 @@ int main(int argc, char** argv)
}
vertices->dirty();
}

if (dataVariance == vsg::STATIC_DATA)
{
// If the data variance is static then we have to manually
// assign the buffer info we want to transfer on each frame.
// This approach is most apporopriate for occassional updates
// for updates every frame it's best to declare the dataVaraince as DYANMIC_DATA
for(auto& tasks : viewer->recordAndSubmitTasks)
{
auto transferTask = tasks->earlyTransferTask;
for(auto& bufferInfo : fdv.bufferInfoSet)
{
transferTask->assign(vsg::BufferInfoList{bufferInfo});
}
}
}
}
else if (dirty)
{
Expand Down

0 comments on commit 412fae7

Please sign in to comment.