2
\$\begingroup\$

I am doing add with a compute shader. All input-output data are stored in VkImage, the format is VK_FORMAT_R32_SFLOAT. In the shader, the corresponding layout is r32f. I have two questions about this:

  1. When imageLoad a r32f data, imageLoad always returns 4 components vec4 data. So will this waste some memory? It seems imageLoad doesn't support return a single component of data. https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/imageLoad.xhtml
  2. The real output data, every 4 data followed by 4 0.0. Does it mean r32f is 64 bits?

The used shader (The test project: https://github.com/math3d/VulkanCompute/blob/master/examples/add_imager32f/add_imager32f.cpp):

layout(local_size_x = 1, local_size_y = 1) in;
layout(binding = 0, r32f) uniform image2D outputValues;

layout(binding = 1, r32f) uniform readonly image2D values;
layout(binding = 2, r32f) uniform readonly image2D filterValues;

void main() {
  uint row = (gl_GlobalInvocationID.x);
  uint col = (gl_GlobalInvocationID.y);
  vec4 x = imageLoad(values, ivec2(row, col));
  vec4 w = imageLoad(filterValues, ivec2(row, col));
  vec4 res = x + w;
  imageStore(outputValues, ivec2(gl_GlobalInvocationID.xy), res);
}

The result:

Compute input:
0.000000        1.000000        2.000000        3.000000        4.000000        5.000000        6.000000        7.000000        8.000000        9.000000        10.000000       11.000000   12.000000        13.000000       14.000000       15.000000       16.000000       17.000000       18.000000       19.000000       20.000000       21.000000       22.000000       23.000000   24.000000        25.000000       26.000000       27.000000       28.000000       29.000000       30.000000       31.000000

Compute filter:
0.000000        1.000000        2.000000        3.000000        4.000000        5.000000        6.000000        7.000000        8.000000        9.000000        10.000000       11.000000   12.000000        13.000000       14.000000       15.000000       16.000000       17.000000       18.000000       19.000000       20.000000       21.000000       22.000000       23.000000   24.000000        25.000000       26.000000       27.000000       28.000000       29.000000       30.000000       31.000000

Compute output:
0.000000        2.000000        4.000000        6.000000        0.000000        0.000000        0.000000        0.000000        8.000000        10.000000       12.000000       14.000000   0.000000         0.000000        0.000000        0.000000        16.000000       18.000000       20.000000       22.000000       0.000000        0.000000        0.000000        0.000000    24.000000        26.000000       28.000000       30.000000       0.000000        0.000000        0.000000        0.000000        32.000000       34.000000       36.000000       38.000000   0.000000         0.000000        0.000000        0.000000        40.000000       42.000000       44.000000       46.000000       0.000000        0.000000        0.000000        0.000000    48.000000        50.000000       52.000000       54.000000       0.000000        0.000000        0.000000        0.000000        56.000000       58.000000       60.000000       62.000000   0.000000         0.000000        0.000000        0.000000

```
\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .