2015-04-15 65 views
0

在我的應用程序中,着色器存儲緩衝區的所有值在幾何着色器中似乎爲0幾何着色器中的着色器存儲緩衝區的所有值都爲0

這是我簡單幾何着色器測試值:

#version 430 
#extension GL_ARB_shader_storage_buffer_object : enable 

layout (points) in; 
layout(invocations = 20) in; 
layout (points, max_vertices = 256) out; 

uniform mat4x4 matModel; 
uniform mat4x4 matView; 
uniform mat4x4 matProjection; 
uniform mat4x4 matLightView; 


struct Info { 
    vec4 position; 
    vec4 normal; 
    vec4 color; 
}; 

layout(std430, binding=0) buffer pointData 
{ 
    Info pointElements[]; 
}; 

void main() { 
    if(pointElements[0].position.x == 0) { 
    gl_Position = matProjection * matView * matModel * gl_in[0].gl_Position; 
    EmitVertex(); 
    } 

    EndPrimitive(); 
} 

只爲pointElements[0].position.x == 0點呈現(pointElements[0].position.x > 0不會呈現任何東西)。 緩衝區的設置已經完成這種方式:

struct PointData { 
    float px, py, pz, pw; 
    float nx, ny, nz, nw; 
    float r, g, b, a; 
} 


void setupSSBO() { 
    std::vector<PointData> data; 
    int count = 15; 
    for (int x = 0; x< count; x++) { 
    for (int y = 0; y< count; y++) { 
     PointData pt; 
     pt.px = 1; 
     pt.py = 2; 
     pt.pz = 3; 
     pt.pw = 4; 
     pt.nx = 5; 
     pt.ny = 6; 
     pt.nz = 7; 
     pt.nw = 8; 
     pt.r = 9; 
     pt.g = 10; 
     pt.b = 11; 
     pt.a = 12; 
     data.push_back(pt); 
    } 
    } 

    GLuint ssbo = 0; 
    std::cout << "errors bnefore: " << glGetError() << std::endl; 
    glGenBuffers(1, &m_pointData); 
    std::cout << "glGenBuffers: " << m_pointData << " " << glGetError() << std::endl; 
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_pointData); 
    std::cout << "glBindBuffer: " << glGetError() << std::endl; 
    glBufferData(GL_SHADER_STORAGE_BUFFER, data.size()*sizeof(PointData), NULL, GL_STATIC_DRAW/*GL_DYNAMIC_COPY*/); 
    std::cout << "glBufferData: " << glGetError() << std::endl; 
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); 
    std::cout << "glBindBuffer: " << glGetError() << std::endl; 


    glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_pointData); 
    PointData* p = (PointData*)glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, data.size() *sizeof(PointData), GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); 
    std::cout << "glMapBufferRange: " << glGetError() << std::endl; 
    memcpy(p, &data[0], data.size()*sizeof(PointData)); 
    glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); 
    std::cout << "glUnmapBuffer: " << glGetError() << std::endl; 
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); 
    std::cout << "glBindBuffer: " << glGetError() << std::endl; 
} 

着色器的結合完成這種方式:

GLuint block_index = glGetProgramResourceIndex(progamId, GL_SHADER_STORAGE_BLOCK, "pointData"); 
glShaderStorageBlockBinding(progamId, block_index, m_pointData); 

glGetError()調用不返回一個錯誤。着色器被執行是因爲當測試結果爲pointElements[0].position.x == 0時,我看到了這些點。可能是值爲0的原因是什麼? (這個問題也與xyw,也爲normalcolor

回答

1

的問題似乎是

glShaderStorageBlockBinding(progamId, block_index, m_pointData); 

一切工作正常,當我將其替換爲:

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, block_index, m_pointData); 

編輯

根據我的觀察,再次閱讀規格後,glGetProgramResourceIndexglBindBufferBase的組合也不正確。使用glBindBufferBase與定義爲layout(std430, binding=0)的綁定是正確的,但glGetProgramResourceIndex不會返回該綁定,而是返回資源索引。