Shader Example Using Texture Data Storage

Does anyone have a pointer to using Textures for storing arbitrary program data?

The project I have in mind is to send several objects (triangles) to the shader, all at the origin and with a common heading, using the standard ArrayBuffer approach … i.e. bind a buffer to the GPU with lots of identical triangles at the origin.

I then want the GLSL program itself (not my webgl program) to create a struct for each triangle consisting of: an ID, an x,y (z=0) position, and an angle. I want to do this in any way possible using GLSL storage.

I will then use that storage within DrawArrays (i.e. the vertex shader) to access the data stored in the texture storage for the position and heading of the triangles. I’ll have two array buffers bound: the vertices, and the IDs of the object they belong to.

Any hints/pointers/docs/laughs?

-- Owen

I must admit that I don’t understand what you want to achieve.
There are many different ways to calculate something in the shader and retrieve/store results.

  1. You could use transform feedback and render into a buffer which later can be reused (this approach doesn’t require textures), or

  2. You could render into a texture (probably that’s what you asked) or

  3. You could use shader_image_load_store extension (which requires SM5 graphics card)

not my webgl program

That kinda limits your options.

First, I would suggest asking on a dedicated WebGL/OpenGL ES 2.0 forum, as this forum is about desktop OpenGL. So you’re more likely to get accurate information there.

Second, you (I think?) don’t have transform feedback or shader_image_load_store available to you. So rendering to a texture is your best bet, assuming you have access to FBOs. Or failing that, reading back from the backbuffer.

I’m taking a graphics class (Ed Angel’s book) and have thus far used only simple shaders. I want to expand into more complicated shaders that actually store most of the data they need rather than updating that data from the CPU each frame. This particular program is an agent based model with thousands of triangles moving on a grid.

Thus I want to store data in the shader that changes each time through the DrawArrays pipeline. Indeed, my ARRAY_BUFFER could simply be integer indices into that storage, and each DrawArrays calculates new values for that data. My first “hello shader” will store only position and heading of the many triangles.

I know that I can store arrays of floats etc, and even structs in the shader. I’m told, however, it might be better to use texture memory.

So my post is to find an example of such a stunt.

– Owen

Maybe you know what you want to do, but you cannot ask right question.
First, you cannot “store” anything in the shader. You probably mean “buffer” when you say “shader”.

Textures as well as buffers have to be updated either from CPU or shaders. To update buffers and textures from within the shader, you can use all three methods I’ve already listed.

That’s OK. It’s feasible. I think that the easiest way to do that is to use transform feedback. With glDrawArray() execute your shader for transforming vertices, and then use the output buffer for the drawing. Transforming parameters can be stored in a texture. If you want to modify those parameters as well, render new values into the texture, or update texture from the CPU. Furthermore, you can use CUDA or OpenCL to make even more exotic solution. :slight_smile:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.