How to use buffer textures on the gpu module?

I’m trying to figure out how to pass buffer textures to compute shaders using the gpu module but it seems like it does nothing at all despite accepting the texture, what am I doing wrong here?

“FLOAT_2D” seems to work fine but for sake of simplicity, I wanted to use a 1D addressing for my data since I’d be dumping vertex coordinates on the texels to perform some transformations. gpu.capabilities.max_texture_size_get() returns 16384 for my machine, which is smaller than the size of most meshes I would want to fit on a texture, so it would require a 2D texture, which complicates indexing.

from gpu.types import GPUShaderCreateInfo, GPUTexture
import gpu
    

cr_info = GPUShaderCreateInfo()
cr_info.local_group_size(1, 1, 1)
cr_info.image(0, 'R32F', 'FLOAT_BUFFER', 'imageb', qualifiers={'WRITE'})

cr_info.compute_source('''//glsl
    void main(){
        int idx = int(gl_GlobalInvocationID.x);
        imageStore(imageb, idx, vec4(idx, 0, 0, 0));
                       
    }
    ''')
shader = gpu.shader.create_from_info(cr_info)


tex = GPUTexture((10, 10), format='R32F')
tex.clear(format='FLOAT', value=(2, 2, 2, 2))

shader.bind()
shader.image('imageb', tex)
gpu.compute.dispatch(shader, 100, 1, 1)
print(tex.read())
3 Likes

Might be a misunderstanding what a texel buffer is. A texel buffer is a buffer that can be accessed as a texture using imageLoad/Store. Your script seems to use a texture and not a buffer. Have you tried to replace the the texture with a vertex buffer?

1 Like

I get this error when I try:

TypeError: GPUShader.image() argument 2 must be GPUTexture, not GPUVertBuf

And also, it seems like GPUVertBuf only has one method attr_fill() and no way to read back the data, so it woudn’t be very useful for GPGPU.


I settled on using FLOAT_2D for now. It makes it a bit annoying to use indexes on a 2D texture but its not so bad. its just being a bit of a pain to write a function to pack arbitrary data on a 2D grid.