gpt4 book ai didi

c++ - OpenGL 批处理 : Why does my draw call exceed array buffer bounds?

转载 作者:行者123 更新时间:2023-11-27 23:23:28 26 4
gpt4 key购买 nike

我尝试使用顶点缓冲对象在 OpenGL ES 2.0 中实现一些相对简单的 2D Sprite 批处理。但是,我的几何图形绘制不正确,一些我似乎无法定位的错误导致 Instruments 中的 GL ES 分析器报告:

Draw Call Exceeded Array Buffer Bounds

A draw call accessed a vertex outside the range of an array buffer in use. This is a serious error, and may result in a crash.

我通过一次绘制单个四边形而不是批处理来测试具有相同顶点布局的绘图,它按预期绘制。

// This technique doesn't necessarily result in correct layering, 
// but for this game it is unlikely that the same texture will
// need to be drawn both in front of and behind other images.
while (!renderQueue.empty())
{
vector<GLfloat> batchVertices;
GLuint texture = renderQueue.front()->textureName;

// find all the draw descriptors with the same texture as the first
// item in the vector and batch them together, back to front
for (int i = 0; i < renderQueue.size(); i++)
{
if (renderQueue[i]->textureName == texture)
{
for (int vertIndex = 0; vertIndex < 24; vertIndex++)
{
batchVertices.push_back(renderQueue[i]->vertexData[vertIndex]);
}

// Remove the item as it has been added to the batch to be drawn
renderQueue.erase(renderQueue.begin() + i);
i--;
}
}

int elements = batchVertices.size();
GLfloat *batchVertArray = new GLfloat[elements];
memcpy(batchVertArray, &batchVertices[0], elements * sizeof(GLfloat));

// Draw the batch
bindTexture(texture);
glBufferData(GL_ARRAY_BUFFER, elements, batchVertArray, GL_STREAM_DRAW);
prepareToDraw();
glDrawArrays(GL_TRIANGLES, 0, elements / BufferStride);

delete [] batchVertArray;
}

其他可能相关的信息:renderQueue 是 DrawDescriptors 的 vector 。 BufferStride 是 4,因为我的顶点缓冲区格式是交错的 position2,texcoord2: X,Y,U,V...

谢谢。

最佳答案

glBufferData期望它的第二个参数是以字节为单位的数据大小。因此,将顶点数据复制到 GPU 的正确方法是:

glBufferData(GL_ARRAY_BUFFER, elements * sizeof(GLfloat), batchVertArray, GL_STREAM_DRAW);

还要确保在调用 glBufferData 时绑定(bind)了正确的顶点缓冲区。

在性能方面,这里绝对不需要分配临时数组。直接使用 vector 即可:

glBufferData(GL_ARRAY_BUFFER, batchVertices.size() * sizeof(GLfloat), &batchVertices[0], GL_STREAM_DRAW);

关于c++ - OpenGL 批处理 : Why does my draw call exceed array buffer bounds?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11169452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com