我正在尝试使用glfw使用Tiled Deferred Shading实现一个新项目,但由于某些原因我无法访问Sampler2Ds,我之前已经在我的另一个项目中运行此代码所以我不会知道它是否是一个glfw问题或一些驱动程序问题(使用nvidia 358.50驱动程序)。这里是depthTexture是sampler2D的代码:
void main()
{
if(gl_LocalInvocationIndex == 0){
minDepth = 0xFFFFFFFF;
maxDepth = 0;
}
//Use the GlobalInvocation ID as a pixel position since we dont have built in GLSL features
ivec2 pixelPosition = ivec2(gl_GlobalInvocationID.xy);
//ivec2 texCoord = getUVCoordinates(pixelPosition);
vec2 texCoord = vec2(pixelPosition.x / SCREEN_WIDTH, pixelPosition.y / SCREEN_HEIGHT);
float depthFloat = texture(depthTexture, texCoord).z;
//float depthFloat = 0.6f;
//Atomics only works on integers
uint depthInt = uint(depthFloat * 0xFFFFFFFF);
//Calculate the max depth of this work group/tile
atomicMin(minDepth, depthInt);
atomicMax(maxDepth, depthInt);
//A barrier is required at this point since we need all the calculations to be done before we proceed
barrier();
imageStore(finalImage, pixelPosition, vec4(vec3(depthFloat),1.0f));
imageStore(otherImage, pixelPosition, vec4(float(float(minDepth) / float(0xFFFFFFFF))));
barrier();
};
我如何绑定纹理,现在我可以写入finalImage和otherImage,但我无法对深度纹理进行采样
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, mDepthTexture);
glUniform1i(shader.getUniformLocation("depthTexture"), 2);
glBindImageTexture(0, mFinalTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
glBindImageTexture(1, mOtherTexture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
如何设置深度纹理
glGenTextures(1, &mDepthTexture);
glBindTexture(GL_TEXTURE_2D, mDepthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepthTexture, 0);
glfw提示:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
我已经使用gdebugger确认深度纹理有效,所以我不确定错误在哪里
修改 我也尝试了glBindImage这样的深度:
glBindImageTexture(2, mDepthTexture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_R32F);
然后
....
layout(binding = 0, rgba32f) uniform writeonly image2D finalImage;
layout(binding = 1, rgba32f) uniform writeonly image2D otherImage;
layout(binding = 2, r32f) uniform readonly image2D depthTexture;
....
float depthFloat = imageLoad(depthTexture, texCoord).z;
....
修改
好的..两件事,首先是一个愚蠢的错误,纹理采样器应该是.x而不是.z,因为它是一个单一的值。我不确定为什么这个在另一台计算机上运行,其次0xFFFFFFFF似乎不支持或导致溢出,因为我必须删除一个F才能使实现工作,而且我不知道因为每个例子我都是#&# 39;我遇到过使用它并且有点担心,因为这意味着一些精度会丢失