gpt4 book ai didi

iphone - 异步纹理加载 iPhone OpenGL ES 2

转载 作者:行者123 更新时间:2023-12-03 20:15:20 27 4
gpt4 key购买 nike

我正在创建并加载大量纹理(由字符串组成)。为了保持动画顺利运行,我将工作卸载到单独的工作线程。它似乎或多或少完全按照我想要的方式工作,但在较旧的设备(iPhone 3GS)上,我有时会注意到很长(1 秒)的延迟。它只是有时发生。现在我想知道我这样做是否正确或者是否存在任何概念问题。我将源代码粘贴在下面。

我还应该提到,我不想使用 GLKit TextureLoader,因为我还想将纹理生成工作卸载到其他线程,而不仅仅是加载部分。

如果您想知道我需要这些纹理做什么,请观看此视频:http://youtu.be/U03p4ZhLjvY?hd=1

NSLock*                     _textureLock;
NSMutableDictionary* _texturesWithString;
NSMutableArray* _texturesWithStringLoading;

// This is called when I request a new texture from the drawing routine.
// If this function returns 0, it means the texture is not ready and Im not displaying it.

-(unsigned int)getTextureWithString:(NSString*)string {
Texture2D* _texture = [_texturesWithString objectForKey:string];
if (_texture==nil){
if (![_texturesWithStringLoading containsObject:string]){
[_texturesWithStringLoading addObject:string];
NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:string,@"string", nil];
NSThread* thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadTextureWithDictionary:)object:dic];
thread.threadPriority = 0.01;
[thread start];
[thread release];
}
return 0;
}
return _texture.name;
}

// This is executed on a separate worker thread.
// The lock makes sure that there are not hundreds of separate threads all creating a texture at the same time and therefore slowing everything down.
// There must be a smarter way of doing that. Please let me know if you know how! ;-)
-(void)loadTextureWithOptions:(NSDictionary*)_dic{
[_textureLock lock];
EAGLContext* context = [[SharegroupManager defaultSharegroup] getNewContext];
[EAGLContext setCurrentContext: context];

NSString* string = [_dic objectForKey:@"string"];
Texture2D* _texture = [[Texture2D alloc] initWithStringModified:string];

if (_texture!=nil) {
NSDictionary* _newdic = [[NSDictionary alloc] initWithObjectsAndKeys:_texture,@"texture",string,@"string", nil];
[self performSelectorOnMainThread:@selector(doneLoadingTexture:) withObject:_newdic waitUntilDone:NO];
[_newdic release];
[_texture release];
}
[EAGLContext setCurrentContext: nil];
[context release];
[_textureLock unlock];
}

// This callback is executed on the main thread and marks adds the texture to the texture cache.
-(void)doneLoadingTextureWithDictionary:(NSDictionary*)_dic{
[_texturesWithString setValue:[_dic objectForKey:@"texture"] forKey:[_dic objectForKey:@"string"]];
[_texturesWithStringLoading removeObject:[_dic objectForKey:@"string"]];
}

最佳答案

问题是同时启动了太多线程。现在我使用的是 NSOperationQueue 而不是 NSThreads。这允许我设置 maxConcurrentOperationCount 并仅运行一个额外的后台线程来加载纹理。

关于iphone - 异步纹理加载 iPhone OpenGL ES 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371247/

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