BindVertexBuffer for a Buffer

94 views
Skip to first unread message

Denis Taniguchi

unread,
Jul 5, 2022, 12:21:28 PM7/5/22
to vsg-...@googlegroups.com
Hello,

What is the correct way to BindVertexBuffer for a Buffer?

I am trying to render a point cloud, but the vertices are computed in a
compute shader, therefore my vertices comes from a buffer, and not a Data.

Cheers,

Denis

Robert Osfield

unread,
Jul 5, 2022, 12:59:00 PM7/5/22
to vsg-users : VulkanSceneGraph Developer Discussion Group
Hi Denis,


What is the correct way to BindVertexBuffer for a Buffer?

BindVertexBuffer::arrays is a BufferInfoList, which is defined as:

     using BufferInfoList = std::vector<ref_ptr<BufferInfo>>;

vsg::BufferInfo contains the details of the part of the vsg::Buffer/vkBuffer that you want to use.  The parts of vsg::BufferInfo that are relevant to your task are defined as:

    ref_ptr<Buffer> buffer;

    VkDeviceSize offset = 0;

    VkDeviceSize range = 0;
 
I am trying to render a point cloud, but the vertices are computed in a
compute shader, therefore my vertices comes from a buffer, and not a Data.

This vsg::BufferInfo entry is the one that you'll want to use to write to from the compute shader, and the same one that you'll want to assign to the BindVertexBuffers::arrays container.

While there isn't a vsgExample that illustrates doing exactly this there is the vsgdynamictexture_cs example that uses a compute shader to convert a RGB image into a RGBA image that is then used for rendering.

This case you describe would be an awesome example to have in vsgExamples so if you feel like it'd be fun to write then if you have problems getting it work you'll be able to share it and we can all learn/debug it together :-)

Cheers,
Robert.

Denis Taniguchi

unread,
Jul 6, 2022, 6:04:28 AM7/6/22
to vsg-...@googlegroups.com

Hi Robert,

Thanks for your prompt feedback as always.

I am running into some difficulties because of line BindVertexBuffers.cpp:129

    119 void BindVertexBuffers::compile(Context& context)
    120 {
    121     // nothing to compile
    122     if (arrays.empty()) return;
    123
    124     auto deviceID = context.deviceID;
    125
    126     bool requiresCreateAndCopy = false;
    127     for (auto& array : arrays)
    128     {
    129         if (array->requiresCopy(deviceID))
    130         {
    131             requiresCreateAndCopy = true;
    132             break;
    133         }
    134     }

Since I don't use Data, requiresCopy returns false, and it doesn't get to the bit of the code where it sets vkd.vkBuffers and vkd.offsets. I could set a dummy Data, but that seems a little hacky. Any guru advise?

Once I get this working I am glad to provide a vsgExample illustrating this use case.

Cheers,

Denis

--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vsg-users/585787f4-4773-47d7-bc57-f8ba14bde855n%40googlegroups.com.

Robert Osfield

unread,
Jul 6, 2022, 6:43:32 AM7/6/22
to vsg-...@googlegroups.com
Hi Denis,

On Wed, 6 Jul 2022 at 11:04, 'Denis Taniguchi' via vsg-users : VulkanSceneGraph Developer Discussion Group <vsg-...@googlegroups.com> wrote:

Hi Robert,

Thanks for your prompt feedback as always.

I am running into some difficulties because of line BindVertexBuffers.cpp:129

    119 void BindVertexBuffers::compile(Context& context)
    120 {
    121     // nothing to compile
    122     if (arrays.empty()) return;
    123
    124     auto deviceID = context.deviceID;
    125
    126     bool requiresCreateAndCopy = false;
    127     for (auto& array : arrays)
    128     {
    129         if (array->requiresCopy(deviceID))
    130         {
    131             requiresCreateAndCopy = true;
    132             break;
    133         }
    134     }

Since I don't use Data, requiresCopy returns false, and it doesn't get to the bit of the code where it sets vkd.vkBuffers and vkd.offsets. I could set a dummy Data, but that seems a little hacky. Any guru advise?

Having to add a dummy vsg::Data for sure is hacky and not appropriate long term so I'll review the code and come up with a fix.  I'm in the middle of some other work that I'll wrap up soon so I can have a look at it this afternoon.

 

Once I get this working I am glad to provide a vsgExample illustrating this use case.


The best way for me to test any refactoring is to have a testbed, would it be possible for you to throw an example together that includes assign the Data hack and then I can use this as illustration of your usage case and the ability for me to remove the dummy Data and then refactor parts of the VSG to make this assignment redundant.  This way I can be sure that I'm addressing the problem for your usage case rather than guessing.

Cheers,
Robert.


 


 

Denis Taniguchi

unread,
Jul 6, 2022, 12:47:51 PM7/6/22
to vsg-...@googlegroups.com

Hi Robert,

Related to this, is there an easy way to verify the contents of a Buffer? Can I use CopyAndReleaseBuffer to copy from a Buffer to a space in CPU memory so I can print it out?

Cheers,

Denis

--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Robert Osfield

unread,
Jul 6, 2022, 1:19:09 PM7/6/22
to vsg-...@googlegroups.com
HI Denis,

On Wed, 6 Jul 2022 at 17:47, 'Denis Taniguchi' via vsg-users : VulkanSceneGraph Developer Discussion Group <vsg-...@googlegroups.com> wrote:
Related to this, is there an easy way to verify the contents of a Buffer? Can I use CopyAndReleaseBuffer to copy from a Buffer to a space in CPU memory so I can print it out?

I haven't tried it, but it should be possible to map a buffer and then copy the data. but alas we don't have a specific example illustrating it.  The vsgscreenshot example copies the frame buffer then maps it in order to copy the data on the CPU, so something along those   It's more complicated than you should need, but might help.

It's good that you are asking such questions even if I can't answer it with a quick "here's a ready made example", it gives us something to flesh out and make sure is working prior to 1.0.

Creating test examples that can sit in vsgExamples would be a good way of putting flesh on the bones of ideas.

Cheers,
Robert.

Denis Taniguchi

unread,
Jul 11, 2022, 8:59:28 AM7/11/22
to vsg-...@googlegroups.com

Hi Robert,

I was finally able to setup a simple example for this.

In order for it to work, you need to apply the attached patch to vsg.

I didn't use barriers, so you might see some hiccups at times. Whenever I try to set one (BufferMemoryBarrier for instance) I get this error:

VUID-vkCmdPipelineBarrier-pDependencies-02285(ERROR / SPEC): msgNum: 959888396 - Validation Error: [ VUID-vkCmdPipelineBarrier-pDependencies-02285 ] Object 0: handle = 0xf56c9b0000000004, type = VK_OBJECT_TYPE_RENDER_PASS; | MessageID = 0x3936bc0c | vkCmdPipelineBarrier():  Barriers cannot be set during subpass 0 of VkRenderPass 0xf56c9b0000000004[] with no self-dependency specified. The Vulkan spec states: If fname:vkCmdPipelineBarrier is called within a render pass instance, the render pass must have been created with at least one VkSubpassDependency instance in VkRenderPassCreateInfo::pDependencies that expresses a dependency from the current subpass to itself, with synchronization scopes and access scopes that are all supersets of the scopes defined in this command (https://vulkan.lunarg.com/doc/view/1.3.211.0/linux/1.3-extensions/vkspec.html#VUID-vkCmdPipelineBarrier-pDependencies-02285)

Cheers,

Denis

--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.
vsgcomputevertex.cpp
vsg_computevertex.patch

Denis Taniguchi

unread,
Jul 11, 2022, 9:38:51 AM7/11/22
to vsg-users : VulkanSceneGraph Developer Discussion Group
Hi Robert,
Ignore my last email, I forgot to add the shader files, please use this patches for vsg and vsgExamples. You will need to vsgconv the shaders into .spv though.
Cheers,
Denis
vsgExamples_computevertex.patch
vsg_computevertex.patch

Robert Osfield

unread,
Jul 11, 2022, 10:47:04 AM7/11/22
to vsg-...@googlegroups.com
Thanks Denis, I'm heading out right now, but later should have an opportunity to review the code.  How much is functionality right, and what parts still need to be completed?

Denis Taniguchi

unread,
Jul 11, 2022, 10:57:15 AM7/11/22
to vsg-...@googlegroups.com

Hi Robert,

The example code (with the vsg patch applied) shows this:

The points position are setup in the compute shader. I think the question is how we make BindVertexBuffers class accept buffers created outside the class. Seems like the interface was made only to support assigning vertex data through Data. You you push_back a Buffer to BindVertexBuffers::arrays it will fail in BindVertexBuffers::compile to set vkd.vkBuffers which are used during BindVertexBuffers::record. Not sure if the best place to make changes is BindVertexBuffers::compile or in BufferInfo::createBufferAndTransferData, to account to the fact that the user can manually push back Buffers into arrays.

Cheers,

Denis

 On 11/07/2022 15:46, Robert Osfield wrote:
Thanks Denis, I'm heading out right now, but later should have an opportunity to review the code.  How much is functionality right, and what parts still need to be completed?
--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Denis Taniguchi

unread,
Jul 11, 2022, 11:02:13 AM7/11/22
to vsg-...@googlegroups.com

Oh, and we need to add proper memory barriers. The ones that I tried added some nasty vulkan errors.

On 11/07/2022 15:46, Robert Osfield wrote:
Thanks Denis, I'm heading out right now, but later should have an opportunity to review the code.  How much is functionality right, and what parts still need to be completed?
--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Robert Osfield

unread,
Jul 11, 2022, 11:28:21 AM7/11/22
to vsg-...@googlegroups.com
Thanks for the screenshot.  Apart of the issues with the BindVertexBuffers API being awkward for this usage and the memory barrier issues, does this example behave as you intend?

Denis Taniguchi

unread,
Jul 11, 2022, 11:32:28 AM7/11/22
to vsg-...@googlegroups.com

Yes! The compute shader is able to modify the buffer that is subsequently used by a graphics pipeline as a vertex input, that is the minimal use case. I am not as good as you in setting up examples Robert, I know it looks awful. :)

On 11/07/2022 16:28, Robert Osfield wrote:
Thanks for the screenshot.  Apart of the issues with the BindVertexBuffers API being awkward for this usage and the memory barrier issues, does this example behave as you intend?
--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Robert Osfield

unread,
Jul 11, 2022, 11:41:53 AM7/11/22
to vsg-...@googlegroups.com
Hi Denis,

I'm getting errors when I try to use patch or git apply.  How did you generate them?

$ git apply vsgExamples_computevertex.patch
vsgExamples_computevertex.patch:7: trailing whitespace.
#version 450
vsgExamples_computevertex.patch:8: trailing whitespace.
#extension GL_ARB_separate_shader_objects : enable
vsgExamples_computevertex.patch:9: trailing whitespace.

vsgExamples_computevertex.patch:10: trailing whitespace.
layout(std140, binding = 0) buffer Positions
vsgExamples_computevertex.patch:11: trailing whitespace.
{
error: patch failed: examples/state/CMakeLists.txt:2
error: examples/state/CMakeLists.txt: patch does not apply

If you have a public github repo for the changes then I could pull them from there.

Cheers,
Robert.

Robert Osfield

unread,
Jul 11, 2022, 11:52:25 AM7/11/22
to vsg-...@googlegroups.com
Running dos2unx on the path file looks to have made git happier when applying the patch.  Are you working under Windows right now?

Denis Taniguchi

unread,
Jul 11, 2022, 11:53:55 AM7/11/22
to vsg-...@googlegroups.com

Oh, I was just forking vsgExamples into my personal github... No, Linux! Not sure what happened...

On 11/07/2022 16:52, Robert Osfield wrote:
Running dos2unx on the path file looks to have made git happier when applying the patch.  Are you working under Windows right now?
--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Robert Osfield

unread,
Jul 11, 2022, 12:46:23 PM7/11/22
to vsg-...@googlegroups.com
Hi Denis,

I have merged your example with a vsgcomputevertex branch, and tweak the .cpp to read the GLSL shaders rather than require .spv.  To do this I had to add vsgXchange usage to pull in support for reading GLSL.  This change makes it easier to change the GLSL shaders and see instant results.


I have also merged the tweak to BindVertexBuffers with the VSG as a BindVertexBuffer branch:


When I run vsgcomputevertex I get the red squares on screen.  I haven't started digging into the code on the VSG or vsgExam[ples side, it's end of working day here so I'll return to this tomorrow.

Cheers,
Robert.

Denis Taniguchi

unread,
Jul 11, 2022, 12:49:56 PM7/11/22
to vsg-...@googlegroups.com

That's great Robert, thanks!

ps: Removing [External]s...

--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Robert Osfield

unread,
Jul 12, 2022, 6:38:49 AM7/12/22
to vsg-...@googlegroups.com
Hi Denis,

I have merged the vsgcomputevertex example with vsgExample master as I feel it's in a clean enough state.  The changes to the VSG to make it work I'll need to come up with, unfortunately I have a time critical task to complete today so I'll tackle that now and return to resolving that if I can get to it tomorrow.  I'll leave the BindVertexBuffers branch of the VSG with your workaround in place till I get a fix checked in to the VSG.

Cheers,
Robert.

Denis Taniguchi

unread,
Jul 12, 2022, 6:40:37 AM7/12/22
to vsg-...@googlegroups.com

Hi Robert,

Great, thanks for letting me know!

Cheers,

Denis

On 12/07/2022 11:38, Robert Osfield wrote:
--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Robert Osfield

unread,
Jul 12, 2022, 9:29:27 AM7/12/22
to vsg-...@googlegroups.com
Hi Denis,

I am back on the case of resolving how the VSG supports pre-initialized BufferInfo.  I haven't yet come up with a coherent solution across all the relevant VSG classes, but as a first step I've refactored BindVertexBuffers::compile(..) so that it works is more compatible way with BufferInfo with data, but also supports when BufferInfo has be already setup such as in the vsgcomputevertex example, this is checked in as a self_initialized_BufferInfo branch:


vsgcomputevertex -d now runs without any debug errors for me, Could you test this branch and it works fine?

If it works OK I'll close the BindVertexBuffers branch that had your workaround as the above branch is closer to what will be mergeable.

My next step is refine the implementation so it handles the case of BufferInfo already having a Buffer assigned, but the data has been updated so needs copying, and the case where some of the BufferInfo in the array have data, and others have BufferInfo that don't have data.

Once I have that general case sorted for vsg::BindVertexBuffers I'll apply the same approach to vsg::BindIndexBuffer, vsg::Geometry and vsg::VertexIndexDraw so that they all can be used with mixed combinations of BufferInfo usage.

Cheers,
Robert.

Robert Osfield

unread,
Jul 12, 2022, 12:57:47 PM7/12/22
to vsg-...@googlegroups.com
I have added initial support for self initialized BufferInfo into vsg::Geometry, vsg::VertexIndexDraw and vsg::BindIndexBuffers along the same lines the way I implemented vsg::BindVertexBuffers. 


It's not quite as efficient at handling already compiled such you'll have when a subgraph is shared in the scene graph, such that the first pass through during the compile traversal will compile the objects and assign the vulkan handles to the internal buffers, and then subsequent passes through will now additionally recopy the vulkan handles to the internal buffers.   I also haven't yet added support for mixing and matching sets of arrays/indices that may or may not be previously initialized and may or may not have data that requires copying.

It's the end of the working day here so I'll wrap up now and wrap up tomorrow.

Cheers,
Robert.

Denis Taniguchi

unread,
Jul 13, 2022, 3:12:36 AM7/13/22
to vsg-...@googlegroups.com

Hi Robert,

Tested the branches and everything is working fine on my end. Nudge me if I can be of any assistance.

Cheers,

Denis

--
You received this message because you are subscribed to the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vsg-users+...@googlegroups.com.

Robert Osfield

unread,
Jul 27, 2022, 4:16:24 AM7/27/22
to vsg-users : VulkanSceneGraph Developer Discussion Group
To illustrate how to pass dynamically updated uniforms to compute shaders I have extended the vsgcomputevertex example to pass in a vec4 scale uniform that is updated in the main loop - this allows us to animate the vertex positions generated by the compute shader.  The changes to the compute shader and .cpp are:


I have also changed the Camera viewpoint so that it's controlled by vsg::Trackball rather than rely upon a transform node to rotate the scene.

For all of this to work you still need the self_initialized_BufferInfo branch of the VSG.  I still have some further work to do on this branch before it's ready to merge with VSG master, my plan is wrap this up in the next day or two.

Cheers,
Robert.

Denis Taniguchi

unread,
Jul 27, 2022, 4:26:28 AM7/27/22
to vsg-...@googlegroups.com

That is brilliant Robert, thanks a lot!

Cheers,

Denis

--
You received this message because you are subscribed to a topic in the Google Groups "vsg-users : VulkanSceneGraph Developer Discussion Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vsg-users/AAMs6XtxUJs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vsg-users+...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vsg-users/f7f634f0-0ce2-41f6-a431-09741dcdae7fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages