Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Is there a better way to send data to a shader?

  1. #1
    Member Newbie
    Join Date
    Jul 2013
    Posts
    33

    Is there a better way to send data to a shader?

    I'm using transform feedback to perform physics calculations on a set of thousands of particles. I'm sending particle data to the shader in the form of a 256x256 texture but my platform only supports 16-bit floating point textures. Is there a more efficient way to send this amount of data to the shader?

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    Quote Originally Posted by Rakehell View Post
    I'm using transform feedback to perform physics calculations on a set of thousands of particles. I'm sending particle data to the shader in the form of a 256x256 texture but my platform only supports 16-bit floating point textures. Is there a more efficient way to send this amount of data to the shader?
    i have read about a particle system that uses also transform feedback to capture "world geometry" and applies collision detection particle <-> world geometry (some objects in the scene)
    it uses "uniform samplerbuffer" in the shader to access the whole geometry of the world and "streams" the particle data (like position, velocity) through as regular attributes
    it also uses "buffer flipflopping", meaning every frame the system writes the particle data into another buffer ("flop") while reading the data from buffer "flip"
    therefore (i assume) it changes the transform feedback buffer each frame, or maybe it uses 2 different transform feedback object, ... no clue

    you can read about that in "OpenGL Programming Guide (8th Edition)"

  3. #3
    Member Newbie
    Join Date
    Jul 2013
    Posts
    33
    I managed to send a few values to the shader as a



    Code :
    uniform vec4 data[255];


    But the shader crashes if I declare the array to have more than 255 values. Any hints?

  4. #4
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    Quote Originally Posted by Rakehell View Post
    ... to perform physics calculations on a set of thousands of particles ...
    you want to access a bunch of particle data in you vertexshader, right?
    as i've said, declare a uniform samplerBuffer MyParticleData; in your vertexshader, bind the buffer to GL_TEXTURE_BUFFER
    https://www.opengl.org/wiki/Buffer_Texture

    Quote Originally Posted by OpenGL wiki
    In essence, a buffer texture is simply a way for the shader to directly access a large array of data, generally larger than uniform buffer objects allow.

    Buffer textures are defined by the fact that the storage for the texels comes directly from a buffer object.
    Instead of creating storage for a buffer texture, the user associates a buffer texture with an already existing buffer object. This association is made by binding the buffer texture to the GL_TEXTURE_BUFFER target and then calling this function:
    Code :
    void glTexBuffer(GLenum target​, GLenum internalFormat​, GLuint buffer​);

    Code :
    int location = glGetUniformLocation(program, "MyParticleData");
    glUniform1i(location, 0);
    glActiveTexture(GL_TEXTURE0 + 0);
    glBindBuffer(GL_TEXTURE_BUFFER, m_texturebuffer);
    glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, m_particlebuffer_flip);	// and next frame: m_particlebuffer_flop

    m_texturebuffer is a texture (empty, not allocated anything, not bound to any target, just generated)
    m_particlebuffer_flip contains your particle data, by calling glTexBuffer(...) you make it accessible via the texture m_texturebuffer
    then bind m_texturebuffer to GL_TEXTURE_BUFFER, and you can access your data as floating-point RGBA-texels in your shader:

    Code :
    uniform samplerBuffer MyParticleData;   // 1D texture, consider it as array
     
    void main()
    {
    int index = 0;     
     
    vec4 data_in_texel = texelFetch(MyParticleData, index);


    EDIT:

    if you can only access 16bit textures, try using another internal format:
    https://www.opengl.org/wiki/Buffer_T...#Image_formats

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,104
    Quote Originally Posted by Rakehell View Post
    I'm using transform feedback to perform physics calculations on a set of thousands of particles. I'm sending particle data to the shader in the form of a 256x256 texture
    Why? If you're using transform feedback, presumably you're using a vertex shader where each particle is a vertex. In which case, it would seem more logical to use vertex attributes.

  6. #6
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    Quote Originally Posted by GClements View Post
    Why? If you're using transform feedback, presumably you're using a vertex shader where each particle is a vertex. In which case, it would seem more logical to use vertex attributes.
    what if the vertex shader is calculating (for example) gravity?
    to do that, it must access all other particles in the scene, calculating F = sumofallparticles(f*m1*m2/distance^2)

  7. #7
    Newbie OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    I would guess that if you're limited to FP16 textures, and limited to 255 uniforms, then your target platform is just not gonna support any potentially more efficient ways.

  8. #8
    Member Newbie
    Join Date
    Jul 2013
    Posts
    33
    I'm using a uniform buffer ofbject and now it seems I'm limited to 1024 vec4s.

    Code :
    layout (std140) uniform Data
    {
    vec4 data[1024];
    };



    Quote Originally Posted by john_connor View Post
    if you can only access 16bit textures, try using another internal format:
    https://www.opengl.org/wiki/Buffer_T...#Image_formats
    I can do 32-bit ints but not floats.

  9. #9
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674
    Quote Originally Posted by john_connor View Post
    what if the vertex shader is calculating (for example) gravity?
    to do that, it must access all other particles in the scene, calculating F = sumofallparticles(f*m1*m2/distance^2)
    If you're doing computations where every particle needs to know the location of every other particle, then shaders are probably not going to be a particularly good solution for you. Not unless compute shaders are available.

  10. #10
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,674
    Quote Originally Posted by Rakehell View Post
    I'm using transform feedback to perform physics calculations on a set of thousands of particles. I'm sending particle data to the shader in the form of a 256x256 texture but my platform only supports 16-bit floating point textures. Is there a more efficient way to send this amount of data to the shader?
    Wait a minute. What OpenGL implementation are you running on that supports transform feedback (primarily a GL 3.x feature), but doesn't support 32-bit floating point textures? They're required texture formats for GL 3.2 implementations.

Page 1 of 2 12 LastLast

Similar Threads

  1. Send same data to multiples kernel
    By Figa17 in forum OpenCL
    Replies: 1
    Last Post: 06-07-2015, 09:14 AM
  2. Send a texture buffer to a shader
    By Vylsain in forum OpenGL: GLSL
    Replies: 4
    Last Post: 11-16-2012, 01:29 AM
  3. how to send multiple texture coordinates to a shader?
    By therealremi in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 01-19-2007, 09:21 PM
  4. how to send my data into display function
    By suponlyzx in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-17-2005, 10:35 AM
  5. send texture to shader program
    By in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 11-24-2004, 09:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean