Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run on buffer object and change it's data by shader? [closed]

Tags:

c++

opengl

shader

Is there a way to run a shader on Buffer Object and modifying it with some other data with shader?

In other words: Is there a way to create uniform global variable in shader and modifiable?

like image 664
User is deleted Avatar asked Jan 15 '23 20:01

User is deleted


1 Answers

Yes, though it depends on your hardware. All of these mechanisms require hardware capable of GL 3.x or above.

Transform Feedback

This allows you to capture the results of vertex processing in one or more buffer objects.

This is the most crude mechanism, because each vertex shader can only access the vertex data it is given as input, and can only write to the output vertex. So the shader can't access the vertex before or after it. Also, the amount of outputting is quite limited, usually only 4 output values from GL 3.x-class hardware.

You could use a geometry shader to increase some of your reading and writing abilities; feedback happens after the VS or GS.

Render to Buffer Object

Buffer textures are textures that use a buffer object for storage. They're like really big 1D textures.

Being a texture, you can freely attach them to a Framebuffer Object and render to it.

The downside of this technique, beyond the difficulty of rendering to an image with 1 pixel of height, is that you're dealing with the rasterizer. It's not perfectly exact. You can use gl_FragCoord to know where you are in the fragment, but if you want to write significantly different data to different areas of the image, there may be difficulties.

The other problem is that FBO sizes are limited by the viewport dimensions, usually around 8192 to 16384. That's not a lot of room to write; at best, you can write 65536 individual floats (if the buffer texture uses the GL_RGBA32F format). So you're very restricted in how much data you can actually write.

Image Load/Store

ARB_shader_image_load_store, core in GL 4.2, represents the ability of a shader to arbitrarily read from and write to image data. Combined with buffer textures (textures that use buffer objects as storage), you can arbitrarily read from and write to buffer objects.

The big downside, besides the hardware requirements, is that there's no free lunch. By using Image Load Store, you effectively give up on all of OpenGL's automatic memory synchronization systems. So you have to do all synchronizations manually for writes and reads. This is a big pain and you can very easily screw it up and get undefined behavior without having a clue as to why. It might work on one platform, but not out on a user's machine.

You have to be very careful with this stuff.

Shader Storage Buffer Object

Shader storage buffer objects are really just Image Load/Store from buffer textures, only with a much nicer interface. It's like defining structs and just accessing them.

The same downsides as for Image Load/Store apply. Also, SSBO is really new; only NVIDIA implements it yet, and even then, only in beta drivers.

like image 89
Nicol Bolas Avatar answered Jan 17 '23 08:01

Nicol Bolas