我一直在使用OpenGL开发项目。使用实例绘制调用渲染粒子。
问题在于,有时glDrawElementsInstanced不会呈现任何内容。并且没有错误报告。其他模型和效果可以很好地渲染。但是没有颗粒 我的粒子系统将渲染。抽奖电话看起来像
ec(glBindVertexArray(vao));
ec(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo));
ec(glDrawElementsInstanced(GL_TRIANGLES, triangleElementIndices.size(), GL_UNSIGNED_INT, reinterpret_cast<void*>(0), instanceCount));
ec
是用于e
rrrr c
heck opengl的宏。它可以有效地做到这一点:
while (GLenum error = glGetError()){
std::cerr << "OpenGLError:" << std::hex << error << " " << functionName << " " << file << " " << line << std::endl;
}
在“发布”模式下,而不是在“调试”模式下,问题呈现粒子更为普遍;但在两种模式下都会发生。该问题与释放模式下的8/10
和调试模式下的1/10
有关。
以下是粒子的渲染过程: 对于每个实例调用调用...
所有对象共享相同的VBO,但顺序渲染。整个应用程序当前是单线程的,因此这不应该成为问题。
给定的粒子A(两个vaos)和B(一个vao)的框架将是:
这种方法是否存在明显的问题?
下面的源代码已经简化,但是我保留了大部分相关部分。与上面的内容不同,它实际上使用了2个共享的顶点缓冲对象(VBO),其中一个用于matrix4s,一个用于vector4s。
GLuint instanceMat4VBO = ... //valid created vertex buffer objects
GLuint instanceVec4VBO = ... //valid created vertex buffer objects
//iterate over all the instnaces; data is stored in class EffectInstanceData
for(EffectInstanceData& eid : instancedEffectsData)
{
if (eid.numInstancesThisFrame > 0)
{
// ---- BUFFER data ---- before binding it to all VAOs (model's may have multiple meshes, each with their own VAO)
ec(glBindBuffer(GL_ARRAY_BUFFER, instanceMac4VBO)); //BUFFER MAT4 INSTANCE DATA
ec(glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * eid.mat4Data.size(), &eid.mat4Data[0], GL_STATIC_DRAW));
ec(glBindBuffer(GL_ARRAY_BUFFER, instanceVec4VBO)); //BUFFER VEC4 INSTANCE DATA
ec(glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4) * eid.vec4Data.size(), &eid.vec4Data[0], GL_STATIC_DRAW));
//meshes may have multiple VAO's that need rendering, set up buffers with instance data for each VAO before instance rendering is done
for (GLuint effectVAO : eid.effectData->mesh->getVAOs())
{
ec(glBindVertexArray(effectVAO));
{ //set up mat4 buffer
ec(glBindBuffer(GL_ARRAY_BUFFER, instanceMat4VBO));
GLsizei numVec4AttribsInBuffer = 4 * eid.numMat4PerInstance;
size_t packagedVec4Idx_matbuffer = 0;
//pass built-in data into instanced array vertex attribute
{
//mat4 (these take 4 separate vec4s)
{
//model matrix
ec(glEnableVertexAttribArray(8));
ec(glEnableVertexAttribArray(9));
ec(glEnableVertexAttribArray(10));
ec(glEnableVertexAttribArray(11));
ec(glVertexAttribPointer(8, 4, GL_FLOAT, GL_FALSE, numVec4AttribsInBuffer * sizeof(glm::vec4), reinterpret_cast<void*>(packagedVec4Idx_matbuffer++ * sizeof(glm::vec4))));
ec(glVertexAttribPointer(9, 4, GL_FLOAT, GL_FALSE, numVec4AttribsInBuffer * sizeof(glm::vec4), reinterpret_cast<void*>(packagedVec4Idx_matbuffer++ * sizeof(glm::vec4))));
ec(glVertexAttribPointer(10, 4, GL_FLOAT, GL_FALSE, numVec4AttribsInBuffer * sizeof(glm::vec4), reinterpret_cast<void*>(packagedVec4Idx_matbuffer++ * sizeof(glm::vec4))));
ec(glVertexAttribPointer(11, 4, GL_FLOAT, GL_FALSE, numVec4AttribsInBuffer * sizeof(glm::vec4), reinterpret_cast<void*>(packagedVec4Idx_matbuffer++ * sizeof(glm::vec4))));
ec(glVertexAttribDivisor(8, 1));
ec(glVertexAttribDivisor(9, 1));
ec(glVertexAttribDivisor(10, 1));
ec(glVertexAttribDivisor(11, 1));
}
}
}
{ //set up vec4 buffer
ec(glBindBuffer(GL_ARRAY_BUFFER, instanceVec4VBO));
GLsizei numVec4AttribsInBuffer = eid.numVec4PerInstance;
size_t packagedVec4Idx_v4buffer = 0;
{
//package built-in vec4s
ec(glEnableVertexAttribArray(7));
ec(glVertexAttribPointer(7, 4, GL_FLOAT, GL_FALSE, numVec4AttribsInBuffer * sizeof(glm::vec4), reinterpret_cast<void*>(packagedVec4Idx_v4buffer++ * sizeof(glm::vec4))));
ec(glVertexAttribDivisor(7, 1));
}
}
}
//activate shader
... code setting uniforms on shaders, does not appear to be issue...
//instanced render
for (GLuint vao : eid.effectData->mesh->getVAOs()) //this actually results in function calls to a mesh class instances, but effectively is doing this loop
{
ec(glBindVertexArray(vao));
ec(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo));
ec(glDrawElementsInstanced(GL_TRIANGLES, triangleElementIndices.size(), GL_UNSIGNED_INT, reinterpret_cast<void*>(0), instanceCount));
}
//clear data for next frame
eid.clearFrameData();
}
}
ec(glBindVertexArray(0));//unbind VAO's
这种可见性是否有误?我已经用RenderDoc
进行了调试,当问题不存在时,事件浏览器中就出现了绘图调用,如下图所示:
但是当问题确实发生时,抽奖电话根本不会出现在RenderDoc
中,如下图所示:
这对我来说似乎很奇怪。我已与调试器验证是否正在执行绘制调用。但是它似乎默默地失败了。
我已经尝试使用nvidia nsight进行调试,但是在通过nvidia nsight启动时无法重现它。
我已验证
系统规格:Windows 10; Opengl3.3,8GB内存; i7-8700k,NVIDIA GeForce GTX TITAN X
在笔记本电脑上也观察到问题,复制率大致相同。它具有英特尔图形芯片。
github链接到actual source,如果有人尝试编译,请告诉我,您需要用我制作的副本替换隐藏的.suo,以自动填写链接器设置。函数:ParticleSystem::handlePostRender
答案 0 :(得分:0)
事实证明,这与实例化无关。我实现了非实例版本,并且遇到了同样的问题。真正的问题是我的渲染系统。当前,交换缓冲区和渲染粒子正在侦听同一委托(事件),偶尔,广播事件时,交换缓冲区将排在最前面。因此顺序为:
因此,粒子从不可见,因为它们会在下一帧开始时立即清除。