user5297047
user5297047

Reputation:

How to create Shared Buffer on Vulkan Host Program?

  1. Goal
    I want to set 'shared' buffer's size in GLSL on runtime.

Question is "How to create shared memory in Vulkan Host C/C++ program?" \

  1. Example
    In OpenCL, below OpenCL Kernel function has '__local' argument.
// foo.cl
__kernel void foo(__global float *dst, __global float *src, __local *buf ){
   /*do something great*/
}

and in host C++ program, I create __local memory and pass it kernel args.

void main(){
    ...
    cl_uint args_index = 2;
    foo.setArg(args_index,__local(128) ); // make 128bytes local memory and pass it to kernel.
}

I want to do same thing on Vulkan Compute Pipeline. and I tried below.

  1. GLSL
//foo.comp
#version 450
layout(binding=0) buffer dstBuffer{
   uint dst[];
};
layout(binding=1) buffer srcBuffer{
   uint src[];
};

// try 1
layout(binding=2) uniform SharedMemSize{
   uint size[];
};
shared uint buf[size[0]]; // compile Error! msg : array size must be a constant integer expression
// try 2
layout(binding=2) shared SharedBuffer{
   uint buf[]; 
}; // compile Error! msg :'shared block' not supported
//try 3
layout(binding=2) shared uint buf[]; // compile Error! msg : binding requires uniform or buffer.

I failed above things.
need your help. thanks.

Upvotes: 1

Views: 489

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474436

GLSL has shared variables, which represent storage accessible to any member of a work group. However, it doesn't have "shared memory" in the same way as OpenCL. You cannot directly upload data to shared variables from the CPU, for example. You can't get a pointer to shared variables.

The closest you might get to this is to have some kind of shared variable array whose size is determined from outside of the shader. But even then, you're not influencing the size of memory in total; you're influencing the size of just that array.

And you can sort of do that. SPIR-V in OpenGL allows specialization constants. These are values specified by the outside world during the compilation process for the shader object. Such constants can be used as the size for a shared variable array. Of course, this means that changing it requires a full recompile/relink process.

Upvotes: 1

Related Questions