Skip to content

Commit 6edcca8

Browse files
committed
MSAA fixes.
1 parent 48dee67 commit 6edcca8

File tree

1 file changed

+16
-37
lines changed

1 file changed

+16
-37
lines changed

cocos2d/Platforms/iOS/CCGLView.m

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,7 @@ - (id) initWithFrame:(CGRect)frame pixelFormat:(NSString*)format depthFormat:(GL
213213
_depthFormat = depth;
214214
_multiSampling = sampling;
215215
_preserveBackbuffer = retained;
216-
217-
GLint maxSamplesAllowed;
218-
glGetIntegerv(GL_MAX_SAMPLES_APPLE, &maxSamplesAllowed);
219-
_msaaSamples = MIN(maxSamplesAllowed, nSamples);
216+
_msaaSamples = nSamples;
220217

221218
// Default to "retina" being enabled.
222219
self.contentScaleFactor = [UIScreen mainScreen].scale;
@@ -281,10 +278,9 @@ -(BOOL) setupSurfaceWithSharegroup:(EAGLSharegroup*)sharegroup
281278
CCRenderDispatch(NO, ^{
282279
// Create default framebuffer object. The backing will be allocated for the current layer in -resizeFromLayer
283280
glGenFramebuffers(1, &_defaultFramebuffer);
284-
285-
glGenRenderbuffers(1, &_colorRenderbuffer);
286-
287281
glBindFramebuffer(GL_FRAMEBUFFER, _defaultFramebuffer);
282+
283+
glGenRenderbuffers(1, &_colorRenderbuffer);
288284
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderbuffer);
289285
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _colorRenderbuffer);
290286

@@ -313,6 +309,10 @@ - (void) dealloc
313309
-(void)resizeFromLayer:(CAEAGLLayer *)layer
314310
{
315311
CCRenderDispatch(NO, ^{
312+
GLint maxSamples;
313+
glGetIntegerv(GL_MAX_SAMPLES_APPLE, &maxSamples);
314+
GLint msaaSamples = MIN(maxSamples, _msaaSamples);
315+
316316
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderbuffer);
317317

318318
// Allocate color buffer backing based on the current layer size
@@ -329,21 +329,19 @@ -(void)resizeFromLayer:(CAEAGLLayer *)layer
329329
glGenRenderbuffers(1, &_msaaColorbuffer);
330330

331331
glBindRenderbuffer(GL_RENDERBUFFER, _msaaColorbuffer);
332-
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, _msaaSamples, [self convertPixelFormat:_pixelFormat] , _backingWidth, _backingHeight);
332+
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, msaaSamples, [self convertPixelFormat:_pixelFormat] , _backingWidth, _backingHeight);
333333

334334
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _msaaColorbuffer);
335335
}
336336

337-
CC_CHECK_GL_ERROR_DEBUG();
338-
339337
if(_depthFormat){
340338
glDeleteRenderbuffers(1, &_depthBuffer);
341339
glGenRenderbuffers(1, &_depthBuffer);
342340

343341
glBindRenderbuffer(GL_RENDERBUFFER, _depthBuffer);
344342

345343
if(_multiSampling){
346-
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, _msaaSamples, _depthFormat,_backingWidth, _backingHeight);
344+
glRenderbufferStorageMultisampleAPPLE(GL_RENDERBUFFER, msaaSamples, _depthFormat,_backingWidth, _backingHeight);
347345
} else {
348346
glRenderbufferStorage(GL_RENDERBUFFER, _depthFormat, _backingWidth, _backingHeight);
349347
}
@@ -411,11 +409,6 @@ -(void)beginFrame {}
411409

412410
-(void)presentFrame
413411
{
414-
// IMPORTANT:
415-
// - preconditions
416-
// -> _context MUST be the OpenGL context
417-
// -> renderbuffer_ must be the the RENDER BUFFER
418-
419412
{
420413
CCGLViewFence *fence = _fences.lastObject;
421414
if(fence.isReady){
@@ -424,47 +417,33 @@ -(void)presentFrame
424417
}
425418
}
426419

427-
if (_multiSampling)
428-
{
429-
/* Resolve from msaaFramebuffer to resolveFramebuffer */
430-
//glDisable(GL_SCISSOR_TEST);
420+
if (_multiSampling){
431421
glBindFramebuffer(GL_READ_FRAMEBUFFER_APPLE, _msaaFramebuffer);
432422
glBindFramebuffer(GL_DRAW_FRAMEBUFFER_APPLE, _defaultFramebuffer);
433423
glResolveMultisampleFramebufferAPPLE();
434424
}
435425

436-
if( _discardFramebufferSupported)
437-
{
438-
if (_multiSampling)
439-
{
440-
if (_depthFormat)
441-
{
426+
if(_discardFramebufferSupported){
427+
if(_multiSampling){
428+
if(_depthFormat){
442429
GLenum attachments[] = {GL_COLOR_ATTACHMENT0, GL_DEPTH_ATTACHMENT};
443430
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 2, attachments);
444-
}
445-
else
446-
{
431+
} else {
447432
GLenum attachments[] = {GL_COLOR_ATTACHMENT0};
448433
glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 1, attachments);
449434
}
450-
}
451-
452-
// not MSAA
453-
else if (_depthFormat ) {
435+
} else if(_depthFormat){
454436
GLenum attachments[] = { GL_DEPTH_ATTACHMENT};
455437
glDiscardFramebufferEXT(GL_FRAMEBUFFER, 1, attachments);
456438
}
457439
}
458440

459441
glBindRenderbuffer(GL_RENDERBUFFER, _colorRenderbuffer);
460-
461442
if(![_context presentRenderbuffer:GL_RENDERBUFFER]){
462443
CCLOG(@"cocos2d: Failed to swap renderbuffer in %s\n", __FUNCTION__);
463444
}
464445

465-
// We can safely re-bind the framebuffer here, since this will be the
466-
// 1st instruction of the new main loop
467-
if( _multiSampling ){
446+
if(_multiSampling){
468447
glBindFramebuffer(GL_FRAMEBUFFER, _msaaFramebuffer);
469448
}
470449

0 commit comments

Comments
 (0)