CCES2Renderer-ios.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /****************************************************************************
  2. Copyright (c) 2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Corpyight (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. // Only compile this code on iOS. These files should NOT be included on your Mac project.
  24. // But in case they are included, it won't be compiled.
  25. #include "platform/CCPlatformConfig.h"
  26. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  27. #import "platform/ios/CCES2Renderer-ios.h"
  28. #import "platform/CCPlatformMacros.h"
  29. #import "platform/ios/OpenGL_Internal-ios.h"
  30. #if !defined(COCOS2D_DEBUG) || COCOS2D_DEBUG == 0
  31. #define NSLog(...) do {} while (0)
  32. #endif
  33. @implementation CCES2Renderer
  34. @synthesize context=context_;
  35. @synthesize defaultFramebuffer=defaultFramebuffer_;
  36. @synthesize colorRenderbuffer=colorRenderbuffer_;
  37. @synthesize msaaColorbuffer=msaaColorbuffer_;
  38. @synthesize msaaFramebuffer=msaaFramebuffer_;
  39. // Create an OpenGL ES 2.0 context
  40. - (id) initWithDepthFormat:(unsigned int)depthFormat withPixelFormat:(unsigned int)pixelFormat withSharegroup:(EAGLSharegroup*)sharegroup withMultiSampling:(BOOL) multiSampling withNumberOfSamples:(unsigned int) requestedSamples
  41. {
  42. self = [super init];
  43. if (self)
  44. {
  45. if( ! sharegroup )
  46. context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  47. else
  48. context_ = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2 sharegroup:sharegroup];
  49. if (!context_ || ![EAGLContext setCurrentContext:context_] )
  50. {
  51. [self release];
  52. return nil;
  53. }
  54. depthFormat_ = depthFormat;
  55. pixelFormat_ = pixelFormat;
  56. multiSampling_ = multiSampling;
  57. // Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
  58. glGenFramebuffers(1, &defaultFramebuffer_);
  59. NSAssert( defaultFramebuffer_, @"Can't create default frame buffer");
  60. glGenRenderbuffers(1, &colorRenderbuffer_);
  61. NSAssert( colorRenderbuffer_, @"Can't create default render buffer");
  62. glBindFramebuffer(GL_FRAMEBUFFER, defaultFramebuffer_);
  63. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer_);
  64. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorRenderbuffer_);
  65. if (multiSampling_)
  66. {
  67. GLint maxSamplesAllowed;
  68. glGetIntegerv(GL_MAX_SAMPLES_APPLE, &maxSamplesAllowed);
  69. samplesToUse_ = MIN(maxSamplesAllowed,requestedSamples);
  70. /* Create the MSAA framebuffer (offscreen) */
  71. glGenFramebuffers(1, &msaaFramebuffer_);
  72. NSAssert( msaaFramebuffer_, @"Can't create default MSAA frame buffer");
  73. glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer_);
  74. }
  75. CHECK_GL_ERROR();
  76. }
  77. return self;
  78. }
  79. - (BOOL)resizeFromLayer:(CAEAGLLayer *)layer
  80. {
  81. // Allocate color buffer backing based on the current layer size
  82. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer_);
  83. if( ! [context_ renderbufferStorage:GL_RENDERBUFFER fromDrawable:layer] )
  84. {
  85. NSLog(@"failed to call context");
  86. }
  87. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &backingWidth_);
  88. glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &backingHeight_);
  89. NSLog(@"cocos2d: surface size: %dx%d", (int)backingWidth_, (int)backingHeight_);
  90. if (multiSampling_)
  91. {
  92. if ( msaaColorbuffer_) {
  93. glDeleteRenderbuffers(1, &msaaColorbuffer_);
  94. msaaColorbuffer_ = 0;
  95. }
  96. /* Create the offscreen MSAA color buffer.
  97. After rendering, the contents of this will be blitted into ColorRenderbuffer */
  98. //msaaFrameBuffer needs to be binded
  99. glBindFramebuffer(GL_FRAMEBUFFER, msaaFramebuffer_);
  100. glGenRenderbuffers(1, &msaaColorbuffer_);
  101. NSAssert(msaaFramebuffer_, @"Can't create MSAA color buffer");
  102. glBindRenderbuffer(GL_RENDERBUFFER, msaaColorbuffer_);
  103. glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, samplesToUse_, pixelFormat_ , backingWidth_, backingHeight_);
  104. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorbuffer_);
  105. GLenum error;
  106. if ( (error=glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE)
  107. {
  108. NSLog(@"Failed to make complete framebuffer object 0x%X", error);
  109. return NO;
  110. }
  111. }
  112. CHECK_GL_ERROR();
  113. if (depthFormat_)
  114. {
  115. if( ! depthBuffer_ ) {
  116. glGenRenderbuffers(1, &depthBuffer_);
  117. NSAssert(depthBuffer_, @"Can't create depth buffer");
  118. }
  119. glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer_);
  120. if( multiSampling_ )
  121. glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, samplesToUse_, depthFormat_,backingWidth_, backingHeight_);
  122. else
  123. glRenderbufferStorage(GL_RENDERBUFFER, depthFormat_, backingWidth_, backingHeight_);
  124. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer_);
  125. if (depthFormat_ == GL_DEPTH24_STENCIL8_OES) {
  126. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthBuffer_);
  127. }
  128. // bind color buffer
  129. glBindRenderbuffer(GL_RENDERBUFFER, colorRenderbuffer_);
  130. }
  131. CHECK_GL_ERROR();
  132. GLenum error;
  133. if( (error=glCheckFramebufferStatus(GL_FRAMEBUFFER)) != GL_FRAMEBUFFER_COMPLETE)
  134. {
  135. NSLog(@"Failed to make complete framebuffer object 0x%X", error);
  136. return NO;
  137. }
  138. return YES;
  139. }
  140. -(CGSize) backingSize
  141. {
  142. return CGSizeMake( backingWidth_, backingHeight_);
  143. }
  144. - (NSString*) description
  145. {
  146. return [NSString stringWithFormat:@"<%@ = %08X | size = %ix%i>", [self class], (unsigned int)self, backingWidth_, backingHeight_];
  147. }
  148. - (unsigned int) colorRenderBuffer
  149. {
  150. return colorRenderbuffer_;
  151. }
  152. - (unsigned int) defaultFrameBuffer
  153. {
  154. return defaultFramebuffer_;
  155. }
  156. - (unsigned int) msaaFrameBuffer
  157. {
  158. return msaaFramebuffer_;
  159. }
  160. - (unsigned int) msaaColorBuffer
  161. {
  162. return msaaColorbuffer_;
  163. }
  164. - (void)dealloc
  165. {
  166. // CCLOGINFO("deallocing CCES2Renderer: %p", self);
  167. // Tear down GL
  168. if (defaultFramebuffer_) {
  169. glDeleteFramebuffers(1, &defaultFramebuffer_);
  170. defaultFramebuffer_ = 0;
  171. }
  172. if (colorRenderbuffer_) {
  173. glDeleteRenderbuffers(1, &colorRenderbuffer_);
  174. colorRenderbuffer_ = 0;
  175. }
  176. if( depthBuffer_ ) {
  177. glDeleteRenderbuffers(1, &depthBuffer_ );
  178. depthBuffer_ = 0;
  179. }
  180. if ( msaaColorbuffer_)
  181. {
  182. glDeleteRenderbuffers(1, &msaaColorbuffer_);
  183. msaaColorbuffer_ = 0;
  184. }
  185. if ( msaaFramebuffer_)
  186. {
  187. glDeleteRenderbuffers(1, &msaaFramebuffer_);
  188. msaaFramebuffer_ = 0;
  189. }
  190. // Tear down context
  191. if ([EAGLContext currentContext] == context_)
  192. [EAGLContext setCurrentContext:nil];
  193. [context_ release];
  194. context_ = nil;
  195. [super dealloc];
  196. }
  197. @end
  198. #endif // CC_PLATFORM_IOS