-1

Schreiben eines einfachen Compute-Shaders in OpenGL, um zu verstehen, wie es funktioniert, kann ich nicht das gewünschte Ergebnis erhalten.Array von Strukturen in Compute Shader

Ich möchte zu meinem compute Shader ein Array von Strukturen colourStruct übergeben, um eine Ausgabentextur zu färben.

Ich möchte ein rotes Bild haben, wenn „wantedColor“ = 0 in meinem Compute Shader und ein grünes Bild „wantedColor“ = 1, blau für 2.

Aber ich habe eigentlich nur rot, wenn „wantedColor“ = 1 oder 2 oder 3 und schwarz wenn "wantedColor"> 2 ...

Wenn jemand eine Idee hat, oder vielleicht habe ich nicht verstanden, die Compute Shader Input-Ideen.

Vielen Dank für Ihre Hilfe, hier ist der interessante Teil meines Codes.

My Compute Shader:

#version 430 compatibility 

layout(std430, binding=4) buffer Couleureuh 
{ 
    vec3 Coul[3]; // array of structures 
}; 

layout(local_size_x = 1, local_size_y = 1) in; 
layout(rgba32f, binding = 0) uniform image2D img_output; 

void main() { 

    // base pixel colour for image 
    vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0); 

    // get index in global work group i.e x,y, position 
    ivec2 pixel_coords = ivec2(gl_GlobalInvocationID.xy); 
    ivec2 dims = imageSize (img_output); 


    int colorWanted = 0; 
    pixel = vec4(Coul[colorWanted], 1.0); 

    // output to a secific pixel in the image 
    imageStore (img_output, pixel_coords, pixel); 

} 

Compute Shader und SSBO Initialisierung:

GLuint structBuffer; 
    glGenBuffers(1, &structBuffer); 
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, structBuffer); 
    glBufferData(GL_SHADER_STORAGE_BUFFER, 3*sizeof(colorStruct), NULL, GL_STATIC_DRAW); 

     GLint bufMask = GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT; // invalidate makes a ig difference when re-writting 

    colorStruct *coul; 
    coul = (colorStruct *) glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 3*sizeof(colorStruct), bufMask); 


    coul[0].r = 1.0f; 
    coul[0].g = 0.0f; 
    coul[0].b = 0.0f; 

    coul[1].r = 0.0f; 
    coul[1].g = 1.0f; 
    coul[1].b = 0.0f; 

    coul[2].r = 0.0f; 
    coul[2].g = 0.0f; 
    coul[2].b = 1.0f; 

    glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); 

    glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, structBuffer); 

    m_out_texture.bindImage(); 

    // Launch compute shader 
    m_shader.use(); 

    glDispatchCompute(m_tex_w, m_tex_h, 1); 

    // Prevent samplign before all writes to image are done 
    glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); 

Antwort

0

vec3 sind immer 16-Byte-ausgerichtet sind. Wenn sie sich in einem Array befinden, verhalten sie sich wie vec4 s. Auch mit std430 Layout.

Never use vec3 in interface blocks. Sie sollten entweder ein Array von float s (einzeln auf die 3 gewünschten Mitglieder zugreifen) oder ein Array von vec4 (mit einem nicht verwendeten Element) verwenden.

+0

Es funktioniert mit floats und vec4! Vielen Dank! :). – bRiocHe