Mikuro
Crotchety UI Nitpicker
I've always heard that if speed matters, you should use OpenGL. So I just started learning OpenGL for the purpose of some 2D sprite-based gaming work. I've only been using it for a day at this point, but I figured out how to draw a 32-bit image, and that's practically all I need. So I ran some benchmarks.
I drew a 128x128 png (with an 8-bit alpha channel) 1000 times in an NSView. Using OpenGL, this took roughly 0.62 seconds (that's the average of several successive runs). Using Quartz, it took 0.13. Not what I was expecting. Here's the code I used, in an NSOpenGLView's drawFrame method:
..and...
(Where "pixels" and "image" are an unsigned char pointer to the bitmap data and an NSImage using an NSBitmapImageRep of that same image.)
Is my OpenGL code completely backwards? Or is it really that much slower than Quartz? I hope I'm just doing something stupid, because I was hoping for much better performance than that. Especially since Quartz seems much slower in Tiger than it was in Panther (I was perfectly happy with the speed I was getting in Panther, which is why I never bothered with OpenGL until now).
Relevant system data is in my sig.
I drew a 128x128 png (with an 8-bit alpha channel) 1000 times in an NSView. Using OpenGL, this took roughly 0.62 seconds (that's the average of several successive runs). Using Quartz, it took 0.13. Not what I was expecting. Here's the code I used, in an NSOpenGLView's drawFrame method:
Code:
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, [self frame].size.width, [self frame].size.height);
glOrtho(0, [self frame].size.width, 0, [self frame].size.height, -1.0, 1.0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
NSDate *d = [NSDate date];
for (i=1; i<1000; i++) {
glRasterPos2f(random() % (int)([self frame].size.width-128),random() % (int)([self frame].size.height-128));
glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
}
NSLog(@"%f",[d timeIntervalSinceNow]*-1.0);
Code:
NSDate *d = [NSDate date];
for (i=1; i<1000; i++) {
[image compositeToPoint:NSMakePoint(random() % (int)([self frame].size.width-128),random() % (int)([self frame].size.height-128)) operation:NSCompositeSourceOver];
}
NSLog(@"%f",[d timeIntervalSinceNow]*-1.0);
Is my OpenGL code completely backwards? Or is it really that much slower than Quartz? I hope I'm just doing something stupid, because I was hoping for much better performance than that. Especially since Quartz seems much slower in Tiger than it was in Panther (I was perfectly happy with the speed I was getting in Panther, which is why I never bothered with OpenGL until now).
Relevant system data is in my sig.