Dre4msNoEvil
Dre4msNoEvil

Reputation: 55

Pass an array to the fragment shader using GL_SHADER_STORAGE_BUFFER

I want to pass a class to the fragment shader that contains some data. The problem is that I cannot pass an array of size 100.

I pass the class as data:

struct MetaData {
    int rows;
    int columns;

    float cellSizeX;
    float cellSizeY;

    int mask[10][10];
}metaData;

glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(metaData), &metaData, GL_DYNAMIC_COPY);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo);

glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
GLvoid* p = glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
memcpy(p, &metaData, sizeof(metaData));
glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

And in the shader accept it in the vertex shader as:

layout (std430, binding=3) buffer shader_data
{ 
    int rows;
    int columns;

    float cellSizeX;
    float cellSizeY;

    int mask[10][10];
};

flat out int Mask[10][10];

int main(){
    ...some code...

    Mask = mask;

    ...some code...
}

and accept it in the fragment shader, but the following error is displayed, which I don’t know how to solve.

cannot locate suitable resource to bind variable "Mask". Possibly large array.

As I understand from the search on the Internet there is a certain limit on the number of glGet(GL_MAX_VARYING_FLOATS_ARB). Is there any way to avoid this?

Upvotes: 2

Views: 1672

Answers (1)

robthebloke
robthebloke

Reputation: 9682

Uhm, You do know that you can access shader storage buffers in shaders right? 'Fragment shaders' fall under the umbrella term 'shaders', so rather than copying the struct for each vertex, maybe just add the following to your fragment shader, and access the data directly? :p


// Add to the fragment shader source..... 
layout (std430, binding=3) buffer shader_data
{ 
    int rows;
    int columns;

    float cellSizeX;
    float cellSizeY;

    int mask[10][10];
};

Upvotes: 4

Related Questions