GLKBaseEffect prepareToDraw is Leaking

╄→гoц情女王★ 提交于 2019-12-11 03:43:38

问题


Seems that everyone agrees that this is broken and and you need to get rid of GLKBaseEffect to stop it leaking. But no one mentions what you'd replace it with.

Can someone can point me in the right direction? Some sample code or a tutorial would be amazingly useful!

I'm doing very basic stuff, just drawing 2D sprites. Works great apart from all the leaks :p

I just need to know what prepareToDraw is doing and replace it with some code that works. All the tutorials I've found seem to focus on 3D rendering...

Could I use OpenGL ES1 instead of 2 perhaps?

//---Sprite drawing code ----------------------

effect.transform.modelviewMatrix = viewMatrix;

effect.texture2d0.name = textureInfo.name;
effect.texture2d0.envMode = GLKTextureEnvModeReplace;

// LEAK Here
[effect prepareToDraw];

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, vertices);

glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0, textureVerts);

glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribTexCoord0);

glDisable(GL_BLEND);

回答1:


So... in the end I did what other people had to do and wrote my own version of GLKBaseEffect.

It's fiddly but it's not THAT hard, took about a day. There are tutorials out there but none offered exactly what I needed. In the end I used the OGL default project as my starting point as that gave me the framework - compiling and running shaders and all that, even though it wasn't doing what I wanted.

Here is the gist of what I did. It took a lot of iterations to get working...

  1. Remove all GLKBaseEffect from the default OGL project (one cube is drawn with GLKBaseEffect but the other is with shaders)
  2. Change projection Matrix to be GLKMatrix4MakeOrtho.
  3. Change vertices from Cube to 2D. Get it working.
  4. Add textures.
  5. Get it all working.
  6. Move all OGL code into an OpenGLHelper Class.
  7. Move code into my project and replace all GLKEffects with my new class

The end result may not be the best code, but it works, and there are no leaks any more.

Here are my shaders

// FRAGMENT SHADER //////////////

precision mediump float;
varying lowp vec2 texCoordOut; 
uniform sampler2D textureUniform; 
uniform float     alphaUniform;
uniform lowp vec4 colorUniform;

void main()
{
    vec4 color  = colorUniform * texture2D(textureUniform, texCoordOut); ;
    gl_FragColor = color; 
}

// VERTEX SHADER ////////////////////

attribute vec4 position;
uniform mat4 projection;
uniform mat4 modelView;

attribute vec2 texCoordIn; 
varying vec2 texCoordOut; 

void main()
{
    gl_Position = projection * modelView * position;
    texCoordOut = texCoordIn; 
}

I'd happily share the rest of the code but there's a bit much of it to put in here!




回答2:


Not directly related to your question, but I got some similar issue. The tough part was to figure out what was leaking:

You seem to be using a texture. You must call glDeleteTextures otherwise each update of your textureInfo will leak.

See also this related post : Release textures (GLKTextureInfo objects) allocated by GLKTextureLoader



来源:https://stackoverflow.com/questions/11112517/glkbaseeffect-preparetodraw-is-leaking

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!