PDA

View Full Version : passing 2D texture as vertex attribute data



DmitryM
04-14-2010, 10:46 AM
I have a 2D texture with a known internal format. I'd like to use it's pixels as vertex attributes for farther GL processing.

1) I can compute the texture coordinate in the vertex shader & fetch a sample. No problems here, but it is a fall-back solution.

2) I can create PBO, attach the texture to FBO and call glGetTexImage, reading the contents in the buffer object asynchronously. Then rebind this object as VBO & bind attributes as usual. Should work in theory, as far as I can see.

3) Can I use TBO for that? I can attach the texture to the buffer object, but the spec only describes BO->Texture link (the behavior of sampling from the texture), while I need the reverse link.

4) Your way?

Any opinions appreciated.

Alfonse Reinheart
04-14-2010, 11:12 AM
I can compute the texture coordinate in the vertex shader & fetch a sample. No problems here, but it is a fall-back solution.

Why is this a fall-back solution? It seems to me that this does pretty much what you need.


I can create PBO, attach the texture to FBO and call glGetTexImage, reading the contents in the buffer object asynchronously. Then rebind this object as VBO & bind attributes as usual. Should work in theory, as far as I can see.

Yes, that would work. Though really, it seems unlikely to be faster than just accessing the texture directly in the shader.


Can I use TBO for that?

No. Buffer textures are inherently one-dimensional.


the spec only describes BO->Texture link (the behavior of sampling from the texture)

No, it defines that buffer textures use storage allocated from a buffer object, rather than allocated from glTexImage. That means operations on the texture modify data in the buffer object. Buffer textures are not simply a fast means of copying data from a texture to a buffer.

DmitryM
04-14-2010, 11:20 AM
1) I expect data passed via vertex attribute to be accessed faster than a texture fetch, even when the samples accessed in a locality-friendly manner. The calculus of tex-coords is not difficult, but still I wouldn't like to keep them in.

2) Good

3) I know TBO is 1D, but maybe there is a way to copy 2D texture into 1D line by line, is there?

Thanks for your help, Alphonse.

Alfonse Reinheart
04-14-2010, 11:25 AM
I expect data passed via vertex attribute to be accessed faster than a texture fetch

It probably is. However, since you cannot directly bind a texture as an attribute, and because your data is 2D rather than 1D (so buffer textures are out), simply accessing the texture is still likely to be your best alternative. Because anything else is going to require some form of memory copy, and that's almost certainly not as fast as just accessing the texture itself.

DmitryM
04-20-2010, 06:40 AM
Reading the specs, I wonder is it possible to read the texture content into PBO with a fixed offset between elements?
For example, I need pixels to be written in the following sequence:
x1,y1,z1,0,0,x2,y2,z2,0,0,x3,...