2

我的片段和顶点着色器都包含以下两个人:

struct Light {
  mat4 view;
  mat4 proj;
  vec4 fragPos;
};

layout (std430, binding = 0) buffer Lights {
  Light lights[];
};

我的问题是最后一个字段 ,fragPos是由顶点着色器计算的,但是片段着色器不到顶点着色器所做的更改fragPos(或根本没有任何更改):

aLight.fragPos = bias * aLight.proj * aLight.view * vec4(vs_frag_pos, 1.0);

...在哪里aLight循环lights[i]。正如您可以想象的那样,我正在计算要在阴影映射中使用的每个灯光的坐标系中顶点的位置。知道这里有什么问题吗?我在做一件根本错误的事情吗?

这是我初始化存储的方式:

struct LightData {
  glm::mat4 view;
  glm::mat4 proj;
  glm::vec4 fragPos;
};

glGenBuffers(1, &BBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, BBO);

glNamedBufferStorage(BBO, lights.size() * sizeof(LightData), NULL, GL_DYNAMIC_STORAGE_BIT);

// lights is a vector of a wrapper class for LightData.
for (unsigned int i = 0; i < lights.size(); i++) {
  glNamedBufferSubData(BBO, i * sizeof(LightData), sizeof(LightData), &(lights[i]->data));
}

可能值得注意的是,如果我在顶点着色器中移动fragPos到一个固定大小的数组变量,将结果留在那里,然后添加片段着色器对应项并将其用于我的其余部分,那么一切都很好。所以我想在这里了解更多的是为什么我的片段着色器看不到顶点着色器计算出来的数字。outout fragPos[2]in fragPos[2]

4

1 回答 1

2

I will not be very accurate, but I will try to explain you why your fragment shader does not see what your vertex shader write :

When your vertex shader write some informations inside your buffer, the value you write are not mandatory to be wrote inside video memory, but can be stored in a kind of cache. The same idea occur when your fragment shader will read your buffer, it may read value in a cache (that is not the same as the vertex shader).

To avoid this problem, you must do two things, first, you have to declare your buffer as coherent (inside the glsl) : layout(std430) coherent buffer ...

Once you have that, after your writes, you must issue a barrier (globally, it says : be careful, I write value inside the buffer, values that you will read may be invalid, please, take the new values I wrote).

How to do such a thing ? Using the function memoryBarrierBuffer after your writes. https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/memoryBarrierBuffer.xhtml

BTW : don't forget to divide by w after your projection.

于 2017-08-03T10:09:42.263 回答