0

I am trying to learn Vulkan, coming from OpenGL/OpenCL. I am studying this example: https://vulkan-tutorial.com/Drawing_a_triangle/Setup/Base_code and this one: https://cppdig.com/c/minimal-example-of-using-vulkan-for-compute-operations-only-loc .

I am able to have the Vulkan's base code example working on my systems (Mac and Linux) and now I would like to add to it a compute shader whose purpose would be to programmatically compute the positions of the vertices of the triangles to be rendered graphically (e.g. for a physics simulation where I need to compute in runtime the positions of the nodes of a mesh via Verlet's integration).

It is not clear to me how to create a buffer which can be used both as a vertex buffer by the vertex shader and as a storage buffer by a compute shader.

As far as I understand, both mentioned examples use a custom function similar to the following to create a vertex buffer providing some specific parameters:

createBuffer (                                                                                                        
                bufferSize,                                                                                             
                VK_BUFFER_USAGE_TRANSFER_SRC_BIT,                                                                       
                VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,                             
                stagingBuffer,                                                                                          
                stagingBufferMemory                                                                                     
               );

and this one to create a storage buffer:

createBuffer (                                                                                                        
                bufferSize,                                                                                             
                VK_BUFFER_USAGE_STORAGE_BUFFER_BIT,                                                                       
                VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,                             
                stagingBuffer,                                                                                          
                stagingBufferMemory                                                                                     
               );

I wonder if I should specify a usage bit mask as:

VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT

as second argument of that function in order to create a single buffer that can be shared between the two shaders.

Moreover, it would be great if you could suggest me a complete example where this done.

P.S. In OpenGL/OpenCL I was used to use SSBO buffers, taking advantage of the GL/CL interop. But now I don't want to use OpenCL anymore, I want to use compute shaders.

Thanks

ezor
  • 1
  • 1
  • 1
    I am not entirely sure what the question is, except soliciting a complete premade example. Obviously, you provide the parameters you want to the Buffer creation. If the abstractions in the tutorials confuse you, just look up [vkCreateBuffer](https://registry.khronos.org/vulkan/specs/1.0/html/vkspec.html#vkCreateBuffer) in the specification. Storage is `VK_BUFFER_USAGE_STORAGE_BUFFER_BIT` and Vertex Buffer is `VK_BUFFER_USAGE_VERTEX_BUFFER_BIT`. – krOoze Sep 27 '22 at 11:52
  • What I don't understand is whether specifying both bits in the same vKCreateBuffer function invocation is the correct approach in order to access that very same buffer from both shaders. – ezor Sep 27 '22 at 12:31
  • @zorzin: The Vulkan API isn't trying to trick you. "buffer usage" means exactly what it says it means. Maybe you're confused because the example you looked at uses `VK_BUFFER_USAGE_TRANSFER_SRC_BIT` which is *not* the right flag for vertex buffer usage. But if you're going to use one buffer for two usages, what do you expect to have to do except combining the two usage bits? – Nicol Bolas Sep 27 '22 at 13:24
  • What is not forbidden, is allowed. Simply read the Valid Usage sections in the specification. – krOoze Sep 27 '22 at 13:52
  • 1
    So, if I correctly understand, I can simply set the two usage bits "VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT" for the same buffer and then bind it as a vertex array buffer and a shader storage buffer at the same time? Of course I guess then I have to carefully use appropriate memory barriers according to what I need to do with that buffer. – ezor Sep 27 '22 at 17:27
  • Pretty much, yes. – krOoze Sep 27 '22 at 19:22

0 Answers0