我有一个OpenGL程序,它显示出(巨大的)可重复性不足。在片段着色器中,我查找特定的条件。如果满足该条件,我将与该片段相关联的世界空间坐标写入SSBO,这是我从顶点着色器中保留的。
我的问题是,从程序的一次重复到下一次重复,对SSBO的写操作的数量变化很大。如果多个着色器想要同时写入SSBO,会不会不总是这样呢?
我不从其他着色器读取SSBO。因此,这不是读/写同步的问题。只有当我返回到CPU应用程序时,我才读取SSBO。
我在NVIDIA GTX645卡上使用OpenGL 4.3。
腾讯云文字识别用户实践征文来了
产品使用攻略、上云技术实践,有奖征集,多重好礼等您带回家~
在CPU应用程序中:
//Create one Shader Storage Buffer Object for vertices of detected points GLuint Detected_Vertices_SSBO; glGenBuffers(1,&Detected_Vertices_SSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER,Detected_Vertices_SSBO); glBufferData(GL_SHADER_STORAGE_BUFFER,(sizeN-NbTotalreflectionPoints.at(i))*sizeof(glm::vec4),NULL,GL_DYNAMIC_DRAW); sprintf(OpenGLErrorMessage,"%s\n","Error on Vertices SSBO at creation time."); QueryForOpenGLErrors(OpenGLErrorMessage); glBindBufferBase(GL_SHADER_STORAGE_BUFFER,0,Detected_Vertices_SSBO); sprintf(OpenGLErrorMessage,"%s\n","Error on Vertices SSBO at binding time."); QueryForOpenGLErrors(OpenGLErrorMessage); //Create one Shader Storage Buffer Object for colors of detected points GLuint Detected_Vertices_Colors_SSBO; glGenBuffers(1,&Detected_Vertices_Colors_SSBO); glBindBuffer(GL_SHADER_STORAGE_BUFFER,Detected_Vertices_Colors_SSBO); glBufferData(GL_SHADER_STORAGE_BUFFER,(sizeN-NbTotalreflectionPoints.at(i))*sizeof(glm::vec4),NULL,GL_DYNAMIC_DRAW); sprintf(OpenGLErrorMessage,"%s\n","Error on Colors of Vertices SSBO at creation time."); QueryForOpenGLErrors(OpenGLErrorMessage); glBindBufferBase(GL_SHADER_STORAGE_BUFFER,1,Detected_Vertices_Colors_SSBO); sprintf(OpenGLErrorMessage,"%s\n","Error on Vertices Colors SSBO at binding time."); QueryForOpenGLErrors(OpenGLErrorMessage); glDrawArrays(GL_POINTS, 2*3+NbTargets1*12*3, NbPoints); // glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); glFinish(); glBindBuffer(GL_SHADER_STORAGE_BUFFER, Detected_Vertices_SSBO); glm::vec4 * SSBO_Position_ptr = (glm::vec4*)glMapBuffer(GL_SHADER_STORAGE_BUFFER,GL_READ_ONLY); sprintf(OpenGLErrorMessage,"%s\n","Error on Vertices SSBO at mapping time."); QueryForOpenGLErrors(OpenGLErrorMessage); glFinish(); glBindBuffer(GL_SHADER_STORAGE_BUFFER, Detected_Vertices_Colors_SSBO); glm::vec4 * SSBO_Color_ptr = (glm::vec4*)glMapBuffer(GL_SHADER_STORAGE_BUFFER,GL_READ_ONLY); sprintf(OpenGLErrorMessage,"%s\n","Error on Vertices Colors SSBO at mapping time."); QueryForOpenGLErrors(OpenGLErrorMessage); glFinish();
然后,在片段着色器中:
#version 430 core
布局(Early_fragment_tests)在;
// Interpolated values from the vertex shaders in vec4 fragmentColor; in vec4 worldspace; //SSBO's for detected points and their colors layout (binding=0, std430) coherent buffer detected_vertices_storage vec4 detected_vertices[]; }Positions; layout (binding=1, std430) coherent buffer detected_vertices_colors_storage vec4 detected_vertices_colors[]; }Colors;