2017-02-15 63 views
-1

OpenGLで単純な計算シェーダを使って、それがどのように動作するかを理解するために、私は必要な結果を得ることができません。Compute Shaderへの構造体の配列

私の計算シェーダに構造体の配列colourStructを渡して、出力テクスチャの色付けを行いたいとします。

私は赤のイメージがしたいとき「wantedColor」私のコンピュートシェーダで= 0と2

のための青緑の画像「wantedColor」= 1、しかし、私は実際にするとき、「wantedColor」のみ赤持って= 1または2または3、黒の場合 "wantedColor"> 2 ...

誰かがアイデアを持っている場合、または計算シェーダがアイデアを入力したことを理解できない場合は、

ありがとう、私のコードの興味深い部分です。

マイコンピュートシェーダ:

#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); 

} 

計算シェーダとSSBO初期化:

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); 

答えて

0

vec3は常に16バイトアラインされています。したがって、配列内にあるときは、vec4のように動作します。 std430のレイアウトでさえ。

Never use vec3 in interface blocksfloatの配列(必要な3つのメンバーに個別にアクセスする)またはvec4(未使用の要素を持つ配列)のいずれかを使用する必要があります。

+0

これはfloatとvec4を使用して動作しています!どうもありがとうございました! :)。 – bRiocHe

関連する問題