Jump to content
  • Advertisement
Sign in to follow this  
judge dreadz

OpenGL vertex shader texture fetch

This topic is 4618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am really stuck on how to use this new feature. i have a texture containing position data of particles, i need to read this info into a vertex shader and render the particle system with point sprites. i am using Cg with openGL. i have read the nVidia documents and googled extensively but i still cant find any information/tutorials on exactly how to do it. if anyone could explain the basics of how to read texture data into a vertex shader i would be extremely greatful.

Share this post


Link to post
Share on other sites
Advertisement
Well I've only done it with DX but I imagine the proceedure is the same. You just setup and bind a texture as normal and then use a tex2d instruction in the vertex shader to read it, just like you would in the fragment shader. The only difference is there are limitations on the format of textures used in vertex shaders (or at least there were when I was using my GF6). IIRC a 128-bit 4 component floating point format (in DX terminology the format is A32B32G32R32) worked fine while a more normal 32-bit RBGA texture did not work.

Share this post


Link to post
Share on other sites
thanks for your reply Monder. the bit i dont get is how to generate the texture coordinates to read every texel. in the pixel shader this is automatic since it processes every pixel. i dont just want to read the texture at one vertex position, i need to read the whole texture. i suppose i could try using a loop to manually generate the coordinates for every texel, but there must be a better/faster way of doing it?

Share this post


Link to post
Share on other sites
Bear in mind that a vertex shader can only output a single vertex (or in your case, point sprite). If you want to push through the whole of a 256x256 texture, you'll need 256x256 vertices. You could give each of those vertices the appropriate texture coordinates (and, I think, nothing else) then render them as point sprites; in the shader, you'd get that texture coordinate and use it to read the position data in from the texture.

Share this post


Link to post
Share on other sites
If you want to read every texel then you would need to loop through all the different texture coordinates and read the texture at each set. However unless you have a very small texture this is going to be rather slow. Why do you need to read the entire texture for a single vertex anyway?

You mention particles. If you're trying to create a GPU particle system you only need to do one texture read in the vertex position to read the particles position, just set the texture coordinates as appropiate for each vertex (e.g. given a texture of wXh and a particle position at (x, y) in the texture that coordinate is going to be something like (u, v) = (x / w, y / h)).

[edit]Oh and I was wrong when I said using vertex textures is exactly like normal textures, in DX when you bind the texture to a sampler stage you have to use different constants from the consants used when binding a normal texture to a sampler stage for use in a fragment shader, GL probably works in a similar way.[/edit]

Share this post


Link to post
Share on other sites
Quote:
Original post by Monder
Why do you need to read the entire texture for a single vertex anyway?
You mention particles. If you're trying to create a GPU particle system you only need to do one texture read in the vertex position to read the particles position,


now i'm getting confused!

i dont get what you mean that i only need to do one texture read in the vertex position? i dont have any vertex positions, they are all stored in the texture!

the particle positions are updated in the fragment shader, but i need the vertex shader to read the texture to render the particles. what i think i need to do is get the vertex shader to read every texel in the texture, and draw a point sprite at the xyz position given by the RGB value. so yeah, for a 256*256 texture i need to read the data from 65536 texels at each frame. is this not right? is there a better way to do it? thanks.






Share this post


Link to post
Share on other sites
ok hang on, i think i'm getting it. the vertex shader only inputs and outputs one vertex. so basically, i need to run it 256*256 times by drawing a square made up of 256*256 vertices in the main application. is that right?

Share this post


Link to post
Share on other sites
That was a typo I meant you only need to do one texture read in the vertex shader.

So if you have a 256x256 texture generate 65536 verts and assign each of them the texture coordinates of a unique texel in the texture. Then in the vertex shader perform one read using the previously mentioned coordinates that gets the position of the particle from the texture.

Share this post


Link to post
Share on other sites
It's true that for 65536 particles you will need to read from the texture 65536 times per frame. But you have to remember that vertex shaders don't create vertices. You have to send all those vertices to the card like you would if you were drawing them without a vertex shader. Then the vertex shader is processed on each vertex, so inside it you read from the texture once for whichever vertex you are currently processing. They way you differentiate between the different vertices is by giving each vertex the proper texture coordinate to look up its position in the texture.

EDIT: Doh, I type too slow.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!