简体   繁体   English

iPhone上的OpenGL ES 2.0:无法超过小索引/顶点数

[英]OpenGL ES 2.0 on iPhone: Unable to Exceed Small Index/Vertex Count

I'll try to keep it brief: I'm using OpenGL ES 2.0 on iPhone, and am utilising a Vertex Buffer Object to render many shapes on-screen at once. 我会尽量简短:我在iPhone上使用OpenGL ES 2.0,并且正在利用“顶点缓冲对象”一次在屏幕上渲染许多形状。

A series of zero-upwards indices are used for GL_ELEMENT_ARRAY_BUFFER , these are stored in: 一系列的零向上索引用于GL_ELEMENT_ARRAY_BUFFER ,这些索引存储在:

GLushort *ixData;

Each shape has a vertex count. 每个形状都有一个顶点数。 When a new shape is created, the total vertex count 'vxCount' is used to reallocate memory for ixData: 创建新形状时,总顶点数'vxCount'用于为ixData重新分配内存:

NSLog(@"allocating ixData for %i shapes, %i vertices",shapes.size(),vxCount);
ixData = (GLushort*)realloc(ixData, vxCount*sizeof(GLushort));

There is of course an initial malloc for ixData too. 当然,还有一个用于ixData的初始malloc。

Each shape has 6 vertex attributes (2 position, 4 colour) and these are all GLfloat . 每个形状都有6个顶点属性(2个位置,4种颜色),它们都是GLfloat Each shape has 24 vertices in total. 每个形状共有24个顶点。 The shapes are stored in a vector<Shape> since the code is C++. 由于代码是C ++,因此形状存储在vector<Shape> The value vxCount is calculated by multiplying the size of this shapes vector by the number of vertices per shape (ie shapes.size()*24 ). 通过将该形状矢量的shapes.size()*24每个形状的顶点数(即shapes.size()*24 ),可以计算出值vxCount

The position of each shape changes every frame so I resubmit the buffer data just before calling glDrawElements on each render: 每个形状的位置每帧都会改变,因此我在每个渲染器上调用glDrawElements之前重新提交缓冲区数据:

glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
glBufferData(GL_ARRAY_BUFFER, vxDataSize, vxData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
NSLog(@"render vxcount is %i",vxCount);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ixDataSize, ixData, GL_STATIC_DRAW); //*

The app works great for any number of shapes up to and including 508. However, when I try to add the 509th shape I get an EXC_BAD_ACCESS for the indices buffer data; 该应用程序非常适合508以下(包括508)的任意数量的形状。但是,当我尝试添加第509个形状时,将获得索引缓冲区数据的EXC_BAD_ACCESS relevant line is asterisked in the code above. 相关代码在上面的代码中标有星号。

The NSLog printouts in the code above are shown below. 上面的代码中的NSLog打印输出如下所示。 As you can see the latest allocated vertex count is consistent with the count upon submission of the primitive indices. 如您所见,最新分配的顶点计数与提交原始索引时的计数一致。

... ...

2011-08-31 20:35:59.438 nibfree[26409:707] allocating ixData for 503 shapes, 12072 vertices 2011-08-31 20:35:59.438 nibfree [26409:707]为503个形状,12072个顶点分配ixData

2011-08-31 20:35:59.441 nibfree[26409:707] allocating ixData for 504 shapes, 12096 vertices 2011-08-31 20:35:59.441 nibfree [26409:707]为504个形状,12096个顶点分配ixData

2011-08-31 20:35:59.444 nibfree[26409:707] allocating ixData for 505 shapes, 12120 vertices 2011-08-31 20:35:59.444 nibfree [26409:707]为505个形状,12120个顶点分配ixData

2011-08-31 20:35:59.448 nibfree[26409:707] allocating ixData for 506 shapes, 12144 vertices 2011-08-31 20:35:59.448 nibfree [26409:707]为506个形状,12144个顶点分配ixData

2011-08-31 20:35:59.451 nibfree[26409:707] allocating ixData for 507 shapes, 12168 vertices 2011-08-31 20:35:59.451 nibfree [26409:707]为507个形状,12168个顶点分配ixData

2011-08-31 20:35:59.454 nibfree[26409:707] allocating ixData for 508 shapes, 12192 vertices 2011-08-31 20:35:59.454 nibfree [26409:707]为508个形状,12192个顶点分配ixData

2011-08-31 20:35:59.457 nibfree[26409:707] allocating ixData for 509 shapes, 12216 vertices 2011-08-31 20:35:59.457 nibfree [26409:707]为509个形状,12216个顶点分配ixData

2011-08-31 20:35:59.746 nibfree[26409:707] render vxcount is 12216 2011-08-31 20:35:59.746 nibfree [26409:707]渲染vxcount是12216

~12k vertices should be no problem for the device, and the type of the index data is GLushort meaning 0-65535 vertex indices should be possible. 对于该设备,大约12k个顶点应该没有问题,索引数据的类型为GLushort这意味着0-65535顶点索引应该是可能的。

I'm truly stumped, can anyone surmise what's gone wrong? 我真的很沮丧,任何人都可以猜测出了什么问题吗? Am I exceeding some vertex/index buffer limit that's iPhone specific? 我是否超出了iPhone特定的顶点/索引缓冲区限制?

UPDATE UPDATE

To add insight I've halved the shape size from 24 verts to 12 verts. 为了增加见解,我将形状大小从24个顶点减半到12个顶点。 I then tried again, and reached a limit of 1536 shapes, 18432 vertices . 然后,我再次尝试,并达到了1536 shapes, 18432 vertices的限制。 If I try adding 1537 shapes with 12 verts per shape then it crashes as before with an EXC_BAD_ACCESS . 如果我尝试添加1537个形状(每个形状12个顶点),则它会像以前一样使用EXC_BAD_ACCESS崩溃。

This latest test indicates that the vxData/ixData storage is not an issue, nor is there a limiting number of vertices or vertex indices. 最新测试表明vxData / ixData存储不是问题,也没有数量有限的顶点或顶点索引。 The possible number of shapes has increased by roughly factor 3 - does this suggest its a GL_TRIANGLES rendering quirk I'm encountering? 可能的形状数量增加了大约3倍-这是否表示我遇到的GL_TRIANGLES渲染怪癖? Or maybe it's a misuse of realloc? 也许这是对realloc的滥用? I'm really not sure why it's a problem :( 我真的不知道为什么这是一个问题:(

UPDATE 2 更新2

Another set of numbers to spot the pattern: 300 verts has max 61 shapes, 62 causes the crash. 另一组数字可以识别模式:300个顶点最多具有61个形状,其中62个导致崩溃。 At 62 there is vxcount 18300, last ixData index 18299 as expected. 在62,有vxcount 18300,​​如预期的那样,最后是ixData索引18299。

The EXC_BAD_ACCESS at upload is likely caused when the GL driver attempts to read memory that your application doesn't own. GL驱动程序尝试读取您的应用程序不拥有的内存时,可能会导致上传时出现EXC_BAD_ACCESS。 So it's likely that your ixDataSize has a value larger than vxCount*sizeof(GLushort) . 因此,您的ixDataSize的值可能大于vxCount*sizeof(GLushort) Such a problem may have gone undiscovered with smaller buffer sizes because the ixDataSize you're coming up with in those cases doesn't run outside the area given to your application and, because it's larger than the number you actually want, all of the data you go on to use is copied. 使用较小的缓冲区大小可能不会发现这样的问题,因为在这种情况下,您要使用的ixDataSize不会在给您的应用程序指定的区域之外运行,并且因为它大于您实际想要的数量,所以所有数据您继续使用被复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM