Skip to content

Commit 48dee67

Browse files
committed
CCFrameBufferObject bind with clear.
1 parent e473df4 commit 48dee67

File tree

6 files changed

+49
-19
lines changed

6 files changed

+49
-19
lines changed

CCRendererGLSupport.m

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,18 @@ -(void)dealloc
365365
});
366366
}
367367

368-
-(void)bind
368+
-(void)bindWithClear:(GLbitfield)mask color:(GLKVector4)color4 depth:(GLclampf)depth stencil:(GLint)stencil
369369
{
370+
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
371+
370372
CGSize size = self.sizeInPixels;
371373
glViewport(0, 0, size.width, size.height);
372-
glBindFramebuffer(GL_FRAMEBUFFER, _fbo);
374+
375+
if(mask & GL_COLOR_BUFFER_BIT) glClearColor(color4.r, color4.g, color4.b, color4.a);
376+
if(mask & GL_DEPTH_BUFFER_BIT) glClearDepth(depth);
377+
if(mask & GL_STENCIL_BUFFER_BIT) glClearStencil(stencil);
378+
379+
glClear(mask);
373380
}
374381

375382
-(void)syncWithView:(CC_VIEW<CCDirectorView> *)view;

cocos2d/CCRenderer.m

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,23 @@ -(void)prepareWithProjection:(const GLKMatrix4 *)projection framebuffer:(CCFrame
336336

337337
-(void)enqueueClear:(GLbitfield)mask color:(GLKVector4)color4 depth:(GLclampf)depth stencil:(GLint)stencil globalSortOrder:(NSInteger)globalSortOrder
338338
{
339-
#warning GL code.
340-
[self enqueueBlock:^{
341-
if(mask & GL_COLOR_BUFFER_BIT) glClearColor(color4.r, color4.g, color4.b, color4.a);
342-
if(mask & GL_DEPTH_BUFFER_BIT) glClearDepth(depth);
343-
if(mask & GL_STENCIL_BUFFER_BIT) glClearStencil(stencil);
339+
// If a clear is the very first command, then handle it specially.
340+
if(globalSortOrder == NSIntegerMin && _queue.count == 0 && _queueStack.count == 0){
341+
_clearMask = mask;
342+
_clearColor = color4;
343+
_clearDepth = depth;
344+
_clearStencil = stencil;
345+
} else {
346+
NSAssert([CCConfiguration sharedConfiguration].graphicsAPI == CCGraphicsAPIGL, @"Clear commands must be the first command in the queue unless using GL.");
344347

345-
glClear(mask);
346-
} globalSortOrder:globalSortOrder debugLabel:@"CCRenderer: Clear" threadSafe:YES];
348+
[self enqueueBlock:^{
349+
if(mask & GL_COLOR_BUFFER_BIT) glClearColor(color4.r, color4.g, color4.b, color4.a);
350+
if(mask & GL_DEPTH_BUFFER_BIT) glClearDepth(depth);
351+
if(mask & GL_STENCIL_BUFFER_BIT) glClearStencil(stencil);
352+
353+
glClear(mask);
354+
} globalSortOrder:globalSortOrder debugLabel:@"CCRenderer: Clear" threadSafe:YES];
355+
}
347356
}
348357

349358
-(void)enqueueBlock:(void (^)())block globalSortOrder:(NSInteger)globalSortOrder debugLabel:(NSString *)debugLabel threadSafe:(BOOL)threadsafe
@@ -397,7 +406,7 @@ -(void)flush
397406
{
398407
CCRENDERER_DEBUG_PUSH_GROUP_MARKER(@"CCRenderer: Flush");
399408

400-
[_framebuffer bind];
409+
[_framebuffer bindWithClear:_clearMask color:_clearColor depth:_clearDepth stencil:_clearStencil];
401410

402411
// Commit the buffers.
403412
[_buffers commit];

cocos2d/CCRendererBasicTypes_Private.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ CCGraphicsBufferPushElements(CCGraphicsBuffer *buffer, size_t requestedCount)
128128

129129
-(void)syncWithView:(CC_VIEW<CCDirectorView> *)view;
130130

131-
#warning TODO bind with clear.
132-
-(void)bind;
131+
// Not ideal to use GL enumerations here.
132+
// It's out of convenience, and it's a private API.
133+
-(void)bindWithClear:(GLbitfield)mask color:(GLKVector4)color4 depth:(GLclampf)depth stencil:(GLint)stencil;
133134

134135
@end

cocos2d/CCRenderer_Private.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ typedef NS_ENUM(NSUInteger, CCRenderCommandDrawMode){
119119
NSMutableArray *_queue;
120120
NSMutableArray *_queueStack;
121121

122+
// Handle clearing specially if it's the very first command.
123+
GLbitfield _clearMask;
124+
GLKVector4 _clearColor;
125+
GLclampf _clearDepth;
126+
GLint _clearStencil;
127+
122128
// Current renderer bindings for fast state checking.
123129
// Invalidated at the end of each frame.
124130
__unsafe_unretained CCRenderState *_renderState;

cocos2d/Platforms/iOS/CCMetalSupport.m

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,23 @@ -(void)endRenderPass
6969
_currentRenderCommandEncoder = nil;
7070
}
7171

72-
-(void)beginRenderPass:(id<MTLTexture>)destinationTexture;
72+
-(void)beginRenderPass:(id<MTLTexture>)destinationTexture clearMask:(GLbitfield)mask color:(GLKVector4)color4 depth:(GLclampf)depth stencil:(GLint)stencil;
7373
{
7474
// End the previous render pass.
7575
[self endRenderPass];
7676

7777
MTLRenderPassColorAttachmentDescriptor *colorAttachment = [MTLRenderPassColorAttachmentDescriptor new];
7878
colorAttachment.texture = destinationTexture;
79-
colorAttachment.loadAction = MTLLoadActionClear;
80-
colorAttachment.clearColor = MTLClearColorMake(0, 0, 0, 0);
81-
colorAttachment.storeAction = MTLStoreActionStore;
79+
if(mask & GL_COLOR_BUFFER_BIT){
80+
colorAttachment.loadAction = MTLLoadActionClear;
81+
colorAttachment.clearColor = MTLClearColorMake(color4.r, color4.g, color4.b, color4.a);
82+
colorAttachment.storeAction = MTLStoreActionStore;
83+
} else {
84+
colorAttachment.loadAction = MTLLoadActionDontCare;
85+
colorAttachment.storeAction = MTLStoreActionStore;
86+
}
87+
88+
// TODO depth and stencil.
8289

8390
MTLRenderPassDescriptor *renderPassDescriptor = [MTLRenderPassDescriptor renderPassDescriptor];
8491
renderPassDescriptor.colorAttachments[0] = colorAttachment;
@@ -175,9 +182,9 @@ -(instancetype)initWithTexture:(CCTexture *)texture depthStencilFormat:(GLuint)d
175182
return self;
176183
}
177184

178-
-(void)bind
185+
-(void)bindWithClear:(GLbitfield)mask color:(GLKVector4)color4 depth:(GLclampf)depth stencil:(GLint)stencil
179186
{
180-
[[CCMetalContext currentContext] beginRenderPass:_frameBufferTexture];
187+
[[CCMetalContext currentContext] beginRenderPass:_frameBufferTexture clearMask:mask color:color4 depth:depth stencil:stencil];
181188
}
182189

183190
-(void)syncWithView:(CC_VIEW<CCDirectorView> *)view;

cocos2d/Platforms/iOS/CCMetalSupport_Private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
+(instancetype)currentContext;
7272
+(void)setCurrentContext:(CCMetalContext *)context;
7373

74-
-(void)beginRenderPass:(id<MTLTexture>)destinationTexture;
74+
-(void)beginRenderPass:(id<MTLTexture>)destinationTexture clearMask:(GLbitfield)mask color:(GLKVector4)color4 depth:(GLclampf)depth stencil:(GLint)stencil;
7575

7676
-(void)flushCommandBuffer;
7777

0 commit comments

Comments
 (0)