From 88de01d915777a4b60ccfdac3887a59d5b321e75 Mon Sep 17 00:00:00 2001 From: minggo Date: Thu, 30 Dec 2010 14:22:25 +0800 Subject: [PATCH] update some files in platfomr/iphone to 0.99.5 --- cocos2dx/platform/iphone/EAGLView.h | 14 +- cocos2dx/platform/iphone/EAGLView.mm | 204 +++++++++----- cocos2dx/platform/iphone/ES1Renderer.h | 63 +++-- cocos2dx/platform/iphone/ES1Renderer.m | 372 ++++++++++++++----------- cocos2dx/platform/iphone/ESRenderer.h | 32 ++- 5 files changed, 418 insertions(+), 267 deletions(-) diff --git a/cocos2dx/platform/iphone/EAGLView.h b/cocos2dx/platform/iphone/EAGLView.h index 5c3b7c70e5..43a128f436 100755 --- a/cocos2dx/platform/iphone/EAGLView.h +++ b/cocos2dx/platform/iphone/EAGLView.h @@ -84,9 +84,14 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. NSString *pixelformat_; GLuint depthFormat_; + BOOL preserveBackbuffer_; CGSize size_; BOOL discardFramebufferSupported_; + + //fsaa addition + BOOL multisampling_; + unsigned int requestedSamples_; @private CFMutableDictionaryRef touchesIntergerDict; unsigned int indexBitsUsed; @@ -98,6 +103,9 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format; /** creates an initializes an EAGLView with a frame, a color buffer format, and a depth buffer format */ + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained; +/** creates an initializes an EAGLView with a frame, a color buffer format, a depth buffer format, a sharegroup, and multisamping */ ++ (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)multisampling numberOfSamples:(unsigned int)samples; + // get the view object +(id) sharedEGLView; @@ -105,8 +113,8 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. - (id) initWithFrame:(CGRect)frame; //These also set the current context /** Initializes an EAGLView with a frame, a color buffer format, and 0-bit depth buffer */ - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format; -/** Initializes an EAGLView with a frame, a color buffer format, and a depth buffer format */ -- (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained; +/** Initializes an EAGLView with a frame, a color buffer format, a depth buffer format, a sharegroup and multisampling support */ +- (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)sampling numberOfSamples:(unsigned int)nSamples; /** pixel format: it could be RGBA8 (32-bit) or RGB565 (16-bit) */ @property(nonatomic,readonly) NSString* pixelFormat; @@ -119,6 +127,8 @@ Copyright (C) 2008 Apple Inc. All Rights Reserved. /** OpenGL context */ @property(nonatomic,readonly) EAGLContext *context; +@property(nonatomic,readwrite) BOOL multiSampling; + @property(readonly) CFMutableDictionaryRef touchesIntergerDict; @property(readwrite) unsigned int indexBitsUsed; diff --git a/cocos2dx/platform/iphone/EAGLView.mm b/cocos2dx/platform/iphone/EAGLView.mm index 9a89baab23..f90d961b8a 100755 --- a/cocos2dx/platform/iphone/EAGLView.mm +++ b/cocos2dx/platform/iphone/EAGLView.mm @@ -77,7 +77,8 @@ static EAGLView *view; static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; @interface EAGLView (Private) --(BOOL) setupSurface; +- (BOOL) setupSurfaceWithSharegroup:(EAGLSharegroup*)sharegroup; +- (unsigned int) convertPixelFormat:(NSString*) pixelFormat; @end @implementation EAGLView @@ -87,6 +88,7 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; @synthesize context=context_; @synthesize touchesIntergerDict; @synthesize indexBitsUsed; +@synthesize multiSampling=multiSampling_; + (Class) layerClass { @@ -95,17 +97,22 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; + (id) viewWithFrame:(CGRect)frame { - return [[[self alloc] initWithFrame:frame] autorelease]; + return [[[[self alloc] init] initWithFrame:frame] autorelease]; } + (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format { - return [[[self alloc] initWithFrame:frame pixelFormat:format] autorelease]; + return [[[[self alloc] init] initWithFrame:frame pixelFormat:format] autorelease]; } -+ (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained -{ - return [[[[self alloc] init]initWithFrame:frame pixelFormat:format depthFormat:depth preserveBackbuffer:retained] autorelease]; ++ (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth +{ + return [[[[self alloc] init] initWithFrame:frame pixelFormat:format depthFormat:depth preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0] autorelease]; +} + ++ (id) viewWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained sharegroup:(EAGLSharegroup*)sharegroup multiSampling:(BOOL)multisampling numberOfSamples:(unsigned int)samples +{ + return [[[[self alloc] init] initWithFrame:frame pixelFormat:format depthFormat:depth preserveBackbuffer:retained sharegroup:sharegroup multiSampling:multisampling numberOfSamples:samples] autorelease]; } + (id) sharedEGLView @@ -122,12 +129,12 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; - (id) initWithFrame:(CGRect)frame { - return [self initWithFrame:frame pixelFormat:kEAGLColorFormatRGB565 depthFormat:0 preserveBackbuffer:NO]; + return [self initWithFrame:frame pixelFormat:kEAGLColorFormatRGB565 depthFormat:0 preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0]; } - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format { - return [self initWithFrame:frame pixelFormat:format depthFormat:0 preserveBackbuffer:NO]; + return [self initWithFrame:frame pixelFormat:format depthFormat:0 preserveBackbuffer:NO sharegroup:nil multiSampling:NO numberOfSamples:0]; } - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GLuint)depth preserveBackbuffer:(BOOL)retained @@ -136,9 +143,11 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; { pixelformat_ = format; depthFormat_ = depth; - size_ = frame.size; + multiSampling_ = sampling; + requestedSamples_ = nSamples; + preserveBackbuffer_ = retained; - if( ! [self setupSurface] ) { + if( ! [self setupSurfaceWithSharegroup:sharegroup] ) { [self release]; return nil; } @@ -150,18 +159,20 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; -(id) initWithCoder:(NSCoder *)aDecoder { - if( (self = [super initWithCoder:aDecoder]) ) { - - CAEAGLLayer *eaglLayer = (CAEAGLLayer*)[self layer]; - - pixelformat_ = kEAGLColorFormatRGB565; - depthFormat_ = 0; // GL_DEPTH_COMPONENT24_OES; - size_ = [eaglLayer bounds].size; - - if( ! [self setupSurface] ) { - [self release]; - return nil; - } + if( (self = [super initWithCoder:aDecoder]) ) { + + CAEAGLLayer* eaglLayer = (CAEAGLLayer*)[self layer]; + + pixelformat_ = kEAGLColorFormatRGB565; + depthFormat_ = 0; // GL_DEPTH_COMPONENT24_OES; + multiSampling_= NO; + requestedSamples_ = 0; + size_ = [eaglLayer bounds].size; + + if( ! [self setupSurfaceWithSharegroup:nil] ) { + [self release]; + return nil; + } } view = self; @@ -209,63 +220,128 @@ static cocos2d::CCTouch *s_pTouches[MAX_TOUCHES]; indexBitsUsed &= temp; } --(BOOL) setupSurface -{ - CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; - - eaglLayer.opaque = YES; - eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: - [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, - pixelformat_, kEAGLDrawablePropertyColorFormat, nil]; - - - renderer_ = [[ES1Renderer alloc] initWithDepthFormat:depthFormat_]; - if (!renderer_) - return NO; - - context_ = [renderer_ context]; - [context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]; - - //discardFramebufferSupported_ = [[CCConfiguration sharedConfiguration] supportsDiscardFramebuffer]; - - return YES; +-(BOOL) setupSurfaceWithSharegroup:(EAGLSharegroup*)sharegroup +{ + CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer; + + eaglLayer.opaque = YES; + eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys: + [NSNumber numberWithBool:preserveBackbuffer_], kEAGLDrawablePropertyRetainedBacking, + pixelformat_, kEAGLDrawablePropertyColorFormat, nil]; + + + renderer_ = [[ES1Renderer alloc] initWithDepthFormat:depthFormat_ + withPixelFormat:[self convertPixelFormat:pixelformat_] + withSharegroup:sharegroup + withMultiSampling:multiSampling_ + withNumberOfSamples:requestedSamples_]; + if (!renderer_) + return NO; + + context_ = [renderer_ context]; + [context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:eaglLayer]; + + //discardFramebufferSupported_ = [[CCConfiguration sharedConfiguration] supportsDiscardFramebuffer]; + + return YES; } - (void) dealloc { - CFRelease(touchesIntergerDict); + CFRelease(touchesIntergerDict); [renderer_ release]; [super dealloc]; } - (void) layoutSubviews { - [renderer_ resizeFromLayer:(CAEAGLLayer*)self.layer]; - size_ = [renderer_ backingSize]; - - cocos2d::CCDirector::sharedDirector()->recalculateProjectionAndEAGLViewSize(); + [renderer_ resizeFromLayer:(CAEAGLLayer*)self.layer]; + size_ = [renderer_ backingSize]; + + // Issue #914 #924 +// CCDirector *director = [CCDirector sharedDirector]; +// [director reshapeProjection:size_]; + cocos2d::CGSize size; + size.width = size_.width; + size.height = size_.height; + cocos2d::CCDirector::sharedDirector()->reshapeProjection(size); + + // Avoid flicker. Issue #350 + //[director performSelectorOnMainThread:@selector(drawScene) withObject:nil waitUntilDone:YES]; + cocos2d::CCDirector::sharedDirector()->drawScene(); } - (void) swapBuffers { - // IMPORTANT: - // - preconditions - // -> context_ MUST be the OpenGL context - // -> renderBuffer_ must be the the RENDER BUFFER + // IMPORTANT: + // - preconditions + // -> context_ MUST be the OpenGL context + // -> renderbuffer_ must be the the RENDER BUFFER + +#ifdef __IPHONE_4_0 + + if (multiSampling_) + { + /* Resolve from msaaFramebuffer to resolveFramebuffer */ + //glDisable(GL_SCISSOR_TEST); + glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, [renderer_ msaaFrameBuffer]); + glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, [renderer_ defaultFrameBuffer]); + glResolveMultisampleFramebufferAPPLE(); + } + + if( discardFramebufferSupported_) + { + if (multiSampling_) + { + if (depthFormat_) + { + GLenum attachments[] = {GL_COLOR_ATTACHMENT0_OES, GL_DEPTH_ATTACHMENT_OES}; + glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments); + } + else + { + GLenum attachments[] = {GL_COLOR_ATTACHMENT0_OES}; + glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 1, attachments); + } + + glBindRenderbufferOES(GL_RENDERBUFFER_OES, [renderer_ colorRenderBuffer]); + + } + + // not MSAA + else if (depthFormat_ ) { + GLenum attachments[] = { GL_DEPTH_ATTACHMENT_OES}; + glDiscardFramebufferEXT(GL_FRAMEBUFFER_OES, 1, attachments); + } + } + +#endif // __IPHONE_4_0 + +// if(![context_ presentRenderbuffer:GL_RENDERBUFFER_OES]) +// CCLOG(@"cocos2d: Failed to swap renderbuffer in %s\n", __FUNCTION__); + +#if COCOS2D_DEBUG + CHECK_GL_ERROR(); +#endif + + // We can safely re-bind the framebuffer here, since this will be the + // 1st instruction of the new main loop + if( multiSampling_ ) + glBindFramebufferOES(GL_FRAMEBUFFER_OES, [renderer_ msaaFrameBuffer]); +} -#ifdef __IPHONE_4_0 - if( discardFramebufferSupported_ && depthFormat_ ) { - GLenum attachments[] = { GL_DEPTH_ATTACHMENT_OES }; - glDiscardFramebufferEXT(GL_FRAMEBUFFER_OES, 1, attachments); - } -#endif // __IPHONE_4_0 - - //if(![context_ presentRenderbuffer:GL_RENDERBUFFER_OES]) - //CCLOG(@"cocos2d: Failed to swap renderbuffer in %s\n", __FUNCTION__); - [context_ presentRenderbuffer:GL_RENDERBUFFER_OES]; -#if COCOS2D_DEBUG - CHECK_GL_ERROR(); -#endif +- (unsigned int) convertPixelFormat:(NSString*) pixelFormat +{ + // define the pixel format + GLenum pFormat; + + + if([pixelFormat isEqualToString:@"EAGLColorFormat565"]) + pFormat = GL_RGB565_OES; + else + pFormat = GL_RGBA8_OES; + + return pFormat; } #pragma mark EAGLView - Point conversion diff --git a/cocos2dx/platform/iphone/ES1Renderer.h b/cocos2dx/platform/iphone/ES1Renderer.h index 1cc7b311f3..39971dd898 100644 --- a/cocos2dx/platform/iphone/ES1Renderer.h +++ b/cocos2dx/platform/iphone/ES1Renderer.h @@ -27,32 +27,39 @@ -#import "ESRenderer.h" - -#import -#import - -@interface ES1Renderer : NSObject -{ - // The pixel dimensions of the CAEAGLLayer - GLint backingWidth_; - GLint backingHeight_; - - // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view - GLuint defaultFramebuffer_; - GLuint colorRenderbuffer_; - GLuint depthBuffer_; - - unsigned int depthFormat_; - - - @public - EAGLContext *context_; -} - -/** EAGLContext */ -@property (nonatomic,readonly) EAGLContext* context; - -- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer; - +#import "ESRenderer.h" + +#import +#import + +@interface ES1Renderer : NSObject +{ + // The pixel dimensions of the CAEAGLLayer + GLint backingWidth_; + GLint backingHeight_; + + unsigned int samplesToUse_; + BOOL multiSampling_; + + unsigned int depthFormat_; + unsigned int pixelFormat_; + + // The OpenGL ES names for the framebuffer and renderbuffer used to render to this view + GLuint defaultFramebuffer_; + GLuint colorRenderbuffer_; + GLuint depthBuffer_; + + + //buffers for MSAA + GLuint msaaFramebuffer_; + GLuint msaaColorbuffer_; + + EAGLContext *context_; +} + +/** EAGLContext */ +@property (nonatomic,readonly) EAGLContext* context; + +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer; + @end diff --git a/cocos2dx/platform/iphone/ES1Renderer.m b/cocos2dx/platform/iphone/ES1Renderer.m index 106a08ebfa..51a27f3985 100644 --- a/cocos2dx/platform/iphone/ES1Renderer.m +++ b/cocos2dx/platform/iphone/ES1Renderer.m @@ -1,159 +1,213 @@ -/* - * cocos2d for iPhone: http://www.cocos2d-iphone.org - * - * Copyright (c) 2010 Ricardo Quesada - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * - * File autogenerated with Xcode. Adapted for cocos2d needs. - */ - - -#import "ES1Renderer.h" -#import "OpenGL_Internal.h" -//#import "ccMacros.h" - -@implementation ES1Renderer - -@synthesize context=context_; - -// Create an OpenGL ES 1.1 context -- (id)initWithDepthFormat:(GLuint)depthFormat -{ - if ((self = [super init])) - { - context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; - - if (!context_ || ![EAGLContext setCurrentContext:context_]) - { - [self release]; - return nil; - } - - // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer - glGenFramebuffersOES(1, &defaultFramebuffer_); - NSAssert( defaultFramebuffer_, @"Can't create default frame buffer"); - glGenRenderbuffersOES(1, &colorRenderbuffer_); - NSAssert( colorRenderbuffer_, @"Can't create default render buffer"); - - glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_); - glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); - glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer_); - - depthFormat_ = depthFormat; - - if( depthFormat_ ) { -// glGenRenderbuffersOES(1, &depthBuffer_); -// glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_); -// glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, 100, 100); -// glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_); - - // default buffer -// glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); - } - - CHECK_GL_ERROR(); - } - - return self; -} - -- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer -{ - // Allocate color buffer backing based on the current layer size - - glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); - - if (![context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer]) - { - //CCLOG(@"failed to call context"); - } - - glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth_); - glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight_); - - //CCLOG(@"cocos2d: surface size: %dx%d", (int)backingWidth_, (int)backingHeight_); - - if (depthFormat_) { - if( ! depthBuffer_ ) - glGenRenderbuffersOES(1, &depthBuffer_); - - glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_); - glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, backingWidth_, backingHeight_); - glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_); - - // bind color buffer - glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); - - } - - if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) - { - //CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES)); - return NO; - } - - return YES; -} - --(CGSize) backingSize -{ - return CGSizeMake( backingWidth_, backingHeight_); -} - -- (NSString*) description -{ - return [NSString stringWithFormat:@"<%@ = %08X | size = %ix%i>", [self class], self, backingWidth_, backingHeight_]; -} - -- (void)dealloc -{ - //CCLOGINFO(@"cocos2d: deallocing %@", self); - - // Tear down GL - if(defaultFramebuffer_) - { - glDeleteFramebuffersOES(1, &defaultFramebuffer_); - defaultFramebuffer_ = 0; - } - - if(colorRenderbuffer_) - { - glDeleteRenderbuffersOES(1, &colorRenderbuffer_); - colorRenderbuffer_ = 0; - } - - if( depthBuffer_ ) - { - glDeleteRenderbuffersOES(1, &depthBuffer_); - depthBuffer_ = 0; - } - - // Tear down context - if ([EAGLContext currentContext] == context_) - [EAGLContext setCurrentContext:nil]; - - [context_ release]; - context_ = nil; - - [super dealloc]; -} - -@end +@interface ES1Renderer (private) + +- (GLenum) convertPixelFormat:(int) pixelFormat; + +@end + + +@implementation ES1Renderer + +@synthesize context=context_; + +- (id) initWithDepthFormat:(unsigned int)depthFormat withPixelFormat:(unsigned int)pixelFormat withSharegroup:(EAGLSharegroup*)sharegroup withMultiSampling:(BOOL) multiSampling withNumberOfSamples:(unsigned int) requestedSamples +{ + if ((self = [super init])) + { + if ( sharegroup == nil ) + { + context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1]; + } + else + { + context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:sharegroup]; + } + + if (!context_ || ![EAGLContext setCurrentContext:context_]) + { + [self release]; + return nil; + } + + // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer + glGenFramebuffersOES(1, &defaultFramebuffer_); + NSAssert( defaultFramebuffer_, @"Can't create default frame buffer"); + glGenRenderbuffersOES(1, &colorRenderbuffer_); + NSAssert( colorRenderbuffer_, @"Can't create default render buffer"); + + glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_); + glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer_); + + depthFormat_ = depthFormat; + + if( depthFormat_ ) { +// glGenRenderbuffersOES(1, &depthBuffer_); +// glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_); +// glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, 100, 100); +// glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_); + + // default buffer +// glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); + } + + pixelFormat_ = pixelFormat; + multiSampling_ = multiSampling; + if (multiSampling_) + { + GLint maxSamplesAllowed; + glGetIntegerv(GL_MAX_SAMPLES_APPLE, &maxSamplesAllowed); + samplesToUse_ = MIN(maxSamplesAllowed,requestedSamples); + + /* Create the MSAA framebuffer (offscreen) */ + glGenFramebuffersOES(1, &msaaFramebuffer_); + glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer_); + + } + + CHECK_GL_ERROR(); + } + + return self; +} + +- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer +{ + // Allocate color buffer backing based on the current layer size + + glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); + + if (![context_ renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer]) + { + /*CCLOG(@"failed to call context"); */ + } + + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth_); + glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight_); + + /*CCLOG(@"cocos2d: surface size: %dx%d", (int)backingWidth_, (int)backingHeight_);*/ + + if (multiSampling_) + { + /* Create the offscreen MSAA color buffer. + After rendering, the contents of this will be blitted into ColorRenderbuffer */ + + //msaaFrameBuffer needs to be binded + glBindFramebufferOES(GL_FRAMEBUFFER_OES, msaaFramebuffer_); + glGenRenderbuffersOES(1, &msaaColorbuffer_); + glBindRenderbufferOES(GL_RENDERBUFFER_OES, msaaColorbuffer_); + glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, samplesToUse_,pixelFormat_ , backingWidth_, backingHeight_); + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, msaaColorbuffer_); + + if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) + { + /*CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));*/ + return NO; + } + } + + if (depthFormat_) + { + if( ! depthBuffer_ ) + glGenRenderbuffersOES(1, &depthBuffer_); + + glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthBuffer_); + if( multiSampling_ ) + glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER_OES, samplesToUse_, depthFormat_,backingWidth_, backingHeight_); + else + glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthFormat_, backingWidth_, backingHeight_); + glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBuffer_); + + // bind color buffer + glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer_); + } + + glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer_); + + if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) + { + /*CCLOG(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));*/ + return NO; + } + + CHECK_GL_ERROR(); + + return YES; +} + +-(CGSize) backingSize +{ + return CGSizeMake( backingWidth_, backingHeight_); +} + +- (NSString*) description +{ + return [NSString stringWithFormat:@"<%@ = %08X | size = %ix%i>", [self class], self, backingWidth_, backingHeight_]; +} + + +- (void)dealloc +{ + /*CCLOGINFO(@"cocos2d: deallocing %@", self);*/ + + // Tear down GL + if(defaultFramebuffer_) + { + glDeleteFramebuffersOES(1, &defaultFramebuffer_); + defaultFramebuffer_ = 0; + } + + if(colorRenderbuffer_) + { + glDeleteRenderbuffersOES(1, &colorRenderbuffer_); + colorRenderbuffer_ = 0; + } + + if( depthBuffer_ ) + { + glDeleteRenderbuffersOES(1, &depthBuffer_); + depthBuffer_ = 0; + } + + if ( msaaColorbuffer_) + { + glDeleteRenderbuffersOES(1, &msaaColorbuffer_); + msaaColorbuffer_ = 0; + } + + if ( msaaFramebuffer_) + { + glDeleteRenderbuffersOES(1, &msaaFramebuffer_); + msaaFramebuffer_ = 0; + } + + // Tear down context + if ([EAGLContext currentContext] == context_) + [EAGLContext setCurrentContext:nil]; + + [context_ release]; + context_ = nil; + + [super dealloc]; +} + +- (unsigned int) colorRenderBuffer +{ + return colorRenderbuffer_; +} + +- (unsigned int) defaultFrameBuffer +{ + return defaultFramebuffer_; +} + +- (unsigned int) msaaFrameBuffer +{ + return msaaFramebuffer_; +} + +- (unsigned int) msaaColorBuffer +{ + return msaaColorbuffer_; +} + +@end \ No newline at end of file diff --git a/cocos2dx/platform/iphone/ESRenderer.h b/cocos2dx/platform/iphone/ESRenderer.h index 381b447c72..2874cdc7df 100644 --- a/cocos2dx/platform/iphone/ESRenderer.h +++ b/cocos2dx/platform/iphone/ESRenderer.h @@ -25,18 +25,22 @@ * File autogenerated with Xcode. Adapted for cocos2d needs. */ -#import - -#import -#import - -@protocol ESRenderer - --(id) initWithDepthFormat:(unsigned int)depthFormat; - -- (BOOL)resizeFromLayer:(CAEAGLLayer *)layer; - --(EAGLContext*) context; --(CGSize) backingSize; - +#import + +#import +#import + +@protocol ESRenderer + +- (id) initWithDepthFormat:(unsigned int)depthFormat withPixelFormat:(unsigned int)pixelFormat withSharegroup:(EAGLSharegroup*)sharegroup withMultiSampling:(BOOL) multiSampling withNumberOfSamples:(unsigned int) requestedSamples; + +- (BOOL) resizeFromLayer:(CAEAGLLayer *)layer; + +- (EAGLContext*) context; +- (CGSize) backingSize; + +- (unsigned int) colorRenderBuffer; +- (unsigned int) defaultFrameBuffer; +- (unsigned int) msaaFrameBuffer; +- (unsigned int) msaaColorBuffer; @end