Skip to content

Commit 43bd397

Browse files
committed
Making renderers responsible for their own globals. Render textures use pooled renderers.
1 parent 67630cb commit 43bd397

File tree

8 files changed

+107
-119
lines changed

8 files changed

+107
-119
lines changed

cocos2d/CCDirector.m

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -219,41 +219,6 @@ - (void) dealloc
219219

220220
}
221221

222-
-(NSDictionary *)updateGlobalShaderUniforms
223-
{
224-
// Group all of the standard globals into one value.
225-
// Used by Metal, will be used eventually by a GL3 renderer.
226-
CCGlobalUniforms globals = {};
227-
228-
globals.projection = self.projectionMatrix;
229-
globals.projectionInv = GLKMatrix4Invert(globals.projection, NULL);
230-
_globalShaderUniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:globals.projection];
231-
_globalShaderUniforms[CCShaderUniformProjectionInv] = [NSValue valueWithGLKMatrix4:globals.projectionInv];
232-
233-
CGSize size = self.viewSize;
234-
globals.viewSize = GLKVector2Make(size.width, size.height);
235-
_globalShaderUniforms[CCShaderUniformViewSize] = [NSValue valueWithGLKVector2:globals.viewSize];
236-
237-
CGSize pixelSize = self.viewSizeInPixels;
238-
globals.viewSizeInPixels = GLKVector2Make(pixelSize.width, pixelSize.height);
239-
_globalShaderUniforms[CCShaderUniformViewSizeInPixels] = [NSValue valueWithGLKVector2:globals.viewSizeInPixels];
240-
241-
CCTime t = self.scheduler.currentTime;
242-
globals.time = GLKVector4Make(t, t/2.0f, t/8.0f, t/8.0f);
243-
globals.sinTime = GLKVector4Make(sinf(t*2.0f), sinf(t), sinf(t/2.0f), sinf(t/4.0f));
244-
globals.cosTime = GLKVector4Make(cosf(t*2.0f), cosf(t), cosf(t/2.0f), cosf(t/4.0f));
245-
_globalShaderUniforms[CCShaderUniformTime] = [NSValue valueWithGLKVector4:globals.time];
246-
_globalShaderUniforms[CCShaderUniformSinTime] = [NSValue valueWithGLKVector4:globals.sinTime];
247-
_globalShaderUniforms[CCShaderUniformCosTime] = [NSValue valueWithGLKVector4:globals.cosTime];
248-
249-
globals.random01 = GLKVector4Make(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
250-
_globalShaderUniforms[CCShaderUniformRandom01] = [NSValue valueWithGLKVector4:globals.random01];
251-
252-
_globalShaderUniforms[CCShaderUniformDefaultGlobals] = [NSValue valueWithBytes:&globals objCType:@encode(CCGlobalUniforms)];
253-
254-
return _globalShaderUniforms;
255-
}
256-
257222
- (void) drawScene
258223
{
259224
/* calculate "global" dt */
@@ -273,7 +238,7 @@ - (void) drawScene
273238
GLKMatrix4 projection = self.projectionMatrix;
274239

275240
CCRenderer *renderer = [self rendererFromPool];
276-
[renderer prepareWithGlobals:[self updateGlobalShaderUniforms]];
241+
[renderer prepareWithProjection:&projection viewSize:self.viewSize contentScale:self.contentScaleFactor];
277242
[CCRenderer bindRenderer:renderer];
278243

279244
[renderer enqueueClear:(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) color:_runningScene.colorRGBA.glkVector4 depth:1.0f stencil:0 globalSortOrder:NSIntegerMin];

cocos2d/CCDirector_Private.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@
6060
/// Rect of the visible screen area in GL coordinates.
6161
@property(nonatomic, readonly) CGRect viewportRect;
6262

63-
/// Update the list of default globals and return them.
64-
-(NSDictionary *)updateGlobalShaderUniforms;
65-
6663
/* Sets the glViewport*/
6764
-(void) setViewport;
6865

cocos2d/CCEffectNode.m

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -178,27 +178,27 @@ -(void)configureRender
178178
// bind renderer
179179
_renderer = [CCRenderer currentRenderer];
180180

181-
if(_renderer == nil)
182-
{
183-
_renderer = [[CCRenderer alloc] init];
184-
185-
NSMutableDictionary *uniforms = [[CCDirector sharedDirector].globalShaderUniforms mutableCopy];
186-
uniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:_projection];
187-
#warning FIXME
188-
// _renderer.globalShaderUniforms = uniforms;
189-
190-
[CCRenderer bindRenderer:_renderer];
191-
_privateRenderer = YES;
192-
}
193-
else if(_privateRenderer == NO)
194-
{
195-
_oldGlobalUniforms = _renderer.globalShaderUniforms;
196-
197-
NSMutableDictionary *uniforms = [_oldGlobalUniforms mutableCopy];
198-
uniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:_projection];
199-
#warning FIXME
200-
// _renderer.globalShaderUniforms = uniforms;
201-
}
181+
// if(_renderer == nil)
182+
// {
183+
// _renderer = [[CCRenderer alloc] init];
184+
//
185+
// NSMutableDictionary *uniforms = [[CCDirector sharedDirector].globalShaderUniforms mutableCopy];
186+
// uniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:_projection];
187+
// #warning FIXME
188+
//// _renderer.globalShaderUniforms = uniforms;
189+
//
190+
// [CCRenderer bindRenderer:_renderer];
191+
// _privateRenderer = YES;
192+
// }
193+
// else if(_privateRenderer == NO)
194+
// {
195+
// _oldGlobalUniforms = _renderer.globalShaderUniforms;
196+
//
197+
// NSMutableDictionary *uniforms = [_oldGlobalUniforms mutableCopy];
198+
// uniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:_projection];
199+
// #warning FIXME
200+
//// _renderer.globalShaderUniforms = uniforms;
201+
// }
202202
}
203203

204204
-(void)draw:(CCRenderer *)renderer transform:(const GLKMatrix4 *)transform
@@ -242,12 +242,12 @@ -(void)draw:(CCRenderer *)renderer transform:(const GLKMatrix4 *)transform
242242
[_sprite visit:_renderer parentTransform:transform];
243243
}
244244

245-
if(_privateRenderer == NO)
246-
;
247-
#warning FIXME
248-
// _renderer.globalShaderUniforms = _oldGlobalUniforms;
249-
else
250-
[CCRenderer bindRenderer:nil];
245+
// if(_privateRenderer == NO)
246+
// ;
247+
// #warning FIXME
248+
//// _renderer.globalShaderUniforms = _oldGlobalUniforms;
249+
// else
250+
// [CCRenderer bindRenderer:nil];
251251

252252
_renderer = nil;
253253
}

cocos2d/CCRenderTexture.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ typedef NS_ENUM(NSInteger, CCRenderTextureImageFormat)
147147
/**
148148
* Starts rendering to the texture whitout clearing the texture first.
149149
*/
150-
-(void)begin;
150+
-(CCRenderer *)begin;
151151

152152
/**
153153
* starts rendering to the texture while clearing the texture first.

cocos2d/CCRenderTexture.m

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -376,47 +376,36 @@ -(GLuint)fbo
376376
return _FBO.FBO;
377377
}
378378

379-
-(void)begin
379+
-(CCRenderer *)begin
380380
{
381-
_renderer = [CCRenderer currentRenderer];
381+
CCTexture *texture = self.texture;
382382

383-
if(_renderer == nil){
384-
_renderer = [[CCRenderer alloc] init];
385-
386-
NSMutableDictionary *uniforms = [[CCDirector sharedDirector].globalShaderUniforms mutableCopy];
387-
uniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:_projection];
388-
#warning FIXME
389-
// _renderer.globalShaderUniforms = uniforms;
390-
391-
[CCRenderer bindRenderer:_renderer];
392-
_privateRenderer = YES;
393-
} else {
394-
_oldGlobalUniforms = _renderer.globalShaderUniforms;
395-
396-
NSMutableDictionary *uniforms = [_oldGlobalUniforms mutableCopy];
397-
uniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:_projection];
398-
#warning FIXME
399-
// _renderer.globalShaderUniforms = uniforms;
400-
}
383+
CCRenderer *renderer = [[CCDirector sharedDirector] rendererFromPool];
384+
[renderer prepareWithProjection:&_projection viewSize:texture.contentSize contentScale:texture.contentScale];
385+
386+
_previousRenderer = [CCRenderer currentRenderer];
387+
[CCRenderer bindRenderer:renderer];
401388

402-
CGSize pixelSize = self.texture.contentSizeInPixels;
389+
CGSize pixelSize = texture.contentSizeInPixels;
403390
GLuint fbo = [self fbo];
404391

405-
[_renderer pushGroup];
392+
[renderer pushGroup];
406393

407-
[_renderer enqueueBlock:^{
394+
[renderer enqueueBlock:^{
408395
glGetFloatv(GL_VIEWPORT, _oldViewport.v);
409396
glViewport(0, 0, pixelSize.width, pixelSize.height );
410397

411398
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_oldFBO);
412399
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
413400
} globalSortOrder:NSIntegerMin debugLabel:@"CCRenderTexture: Bind FBO" threadSafe:NO];
401+
402+
return renderer;
414403
}
415404

416405
-(void)beginWithClear:(float)r g:(float)g b:(float)b a:(float)a depth:(float)depthValue stencil:(int)stencilValue flags:(GLbitfield)flags
417406
{
418-
[self begin];
419-
[_renderer enqueueClear:flags color:GLKVector4Make(r, g, b, a) depth:depthValue stencil:stencilValue globalSortOrder:NSIntegerMin];
407+
CCRenderer *renderer = [self begin];
408+
[renderer enqueueClear:flags color:GLKVector4Make(r, g, b, a) depth:depthValue stencil:stencilValue globalSortOrder:NSIntegerMin];
420409
}
421410

422411
-(void)beginWithClear:(float)r g:(float)g b:(float)b a:(float)a
@@ -436,24 +425,26 @@ -(void)beginWithClear:(float)r g:(float)g b:(float)b a:(float)a depth:(float)dep
436425

437426
-(void)end
438427
{
439-
[_renderer enqueueBlock:^{
428+
CCRenderer *renderer = [CCRenderer currentRenderer];
429+
430+
[renderer enqueueBlock:^{
440431
glBindFramebuffer(GL_FRAMEBUFFER, _oldFBO);
441432
glViewport(_oldViewport.v[0], _oldViewport.v[1], _oldViewport.v[2], _oldViewport.v[3]);
442433
} globalSortOrder:NSIntegerMax debugLabel:@"CCRenderTexture: Restore FBO" threadSafe:NO];
443434

444-
[_renderer popGroupWithDebugLabel:@"CCRenderTexture begin/end" globalSortOrder:0];
435+
[renderer popGroupWithDebugLabel:@"CCRenderTexture begin/end" globalSortOrder:0];
445436

446-
if(_privateRenderer){
447-
[CCRenderer bindRenderer:nil];
448-
_privateRenderer = NO;
437+
__unsafe_unretained CCDirector *director = [CCDirector sharedDirector];
438+
CCRenderDispatch(renderer.threadsafe, ^{
439+
[director addFrameCompletionHandler:^{
440+
// Return the renderer to the pool when the frame completes.
441+
[director poolRenderer:renderer];
442+
}];
449443

450-
CCRenderDispatch(_renderer.threadsafe, ^{[_renderer flush];});
451-
} else {
452-
#warning FIXME
453-
// _renderer.globalShaderUniforms = _oldGlobalUniforms;
454-
}
444+
[renderer flush];
445+
});
455446

456-
_renderer = nil;
447+
[CCRenderer bindRenderer:_previousRenderer];
457448
}
458449

459450
-(void)clear:(float)r g:(float)g b:(float)b a:(float)a
@@ -464,15 +455,15 @@ -(void)clear:(float)r g:(float)g b:(float)b a:(float)a
464455

465456
- (void)clearDepth:(float)depthValue
466457
{
467-
[self begin];
468-
[_renderer enqueueClear:GL_DEPTH_BUFFER_BIT color:GLKVector4Make(0, 0, 0, 0) depth:depthValue stencil:0 globalSortOrder:NSIntegerMin];
458+
CCRenderer *renderer = [self begin];
459+
[renderer enqueueClear:GL_DEPTH_BUFFER_BIT color:GLKVector4Make(0, 0, 0, 0) depth:depthValue stencil:0 globalSortOrder:NSIntegerMin];
469460
[self end];
470461
}
471462

472463
- (void)clearStencil:(int)stencilValue
473464
{
474-
[self begin];
475-
[_renderer enqueueClear:GL_DEPTH_BUFFER_BIT color:GLKVector4Make(0, 0, 0, 0) depth:0.0 stencil:stencilValue globalSortOrder:NSIntegerMin];
465+
CCRenderer *renderer = [self begin];
466+
[renderer enqueueClear:GL_DEPTH_BUFFER_BIT color:GLKVector4Make(0, 0, 0, 0) depth:0.0 stencil:stencilValue globalSortOrder:NSIntegerMin];
476467
[self end];
477468
}
478469

@@ -490,16 +481,16 @@ - (void)visit:(CCRenderer *)renderer parentTransform:(const GLKMatrix4 *)parentT
490481
_contentSizeChanged = NO;
491482
}
492483

493-
[self begin];
494-
NSAssert(_renderer == renderer, @"CCRenderTexture error!");
484+
CCRenderer *rtRenderer = [self begin];
485+
NSAssert(renderer == renderer, @"CCRenderTexture error!");
495486

496-
[_renderer enqueueClear:_clearFlags color:_clearColor depth:_clearDepth stencil:_clearStencil globalSortOrder:NSIntegerMin];
487+
[rtRenderer enqueueClear:_clearFlags color:_clearColor depth:_clearDepth stencil:_clearStencil globalSortOrder:NSIntegerMin];
497488

498489
//! make sure all children are drawn
499490
[self sortAllChildren];
500491

501492
for(CCNode *child in _children){
502-
[child visit:renderer parentTransform:&_projection];
493+
[child visit:rtRenderer parentTransform:&_projection];
503494
}
504495

505496
[self end];
@@ -549,8 +540,8 @@ -(CGImageRef) newCGImage
549540
return nil;
550541
}
551542

552-
[self begin];
553-
[_renderer enqueueBlock:^{
543+
CCRenderer *renderer = [self begin];
544+
[renderer enqueueBlock:^{
554545
glReadPixels(0,0,tx,ty,GL_RGBA,GL_UNSIGNED_BYTE, buffer);
555546
} globalSortOrder:NSIntegerMax debugLabel:@"CCRenderTexture reading pixels for new image" threadSafe:NO];
556547
[self end];

cocos2d/CCRenderTexture_Private.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
@protected
1616
GLenum _pixelFormat;
1717
GLuint _depthStencilFormat;
18-
18+
19+
#warning TODO remove me.
1920
CCRenderer *_renderer;
20-
BOOL _privateRenderer;
21+
// Reference to the previous render to be restored by end.
22+
CCRenderer *_previousRenderer;
2123

2224
GLKVector4 _clearColor;
2325

cocos2d/CCRenderer.m

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,17 +765,50 @@ +(instancetype)currentRenderer
765765
+(void)bindRenderer:(CCRenderer *)renderer
766766
{
767767
if(renderer){
768-
NSAssert(self.currentRenderer == nil, @"Internal Error: Already have a renderer bound.");
769768
[NSThread currentThread].threadDictionary[CURRENT_RENDERER_KEY] = renderer;
770769
} else {
771770
[[NSThread currentThread].threadDictionary removeObjectForKey:CURRENT_RENDERER_KEY];
772771
}
773772
}
774773

775-
-(void)prepareWithGlobals:(NSDictionary *)globalShaderUniforms
774+
-(void)prepareWithProjection:(const GLKMatrix4 *)projection viewSize:(CGSize)viewSize contentScale:(CGFloat)contentScale;
776775
{
777-
_globalShaderUniforms = [globalShaderUniforms copy];
776+
CCDirector *director = [CCDirector sharedDirector];
778777

778+
// Copy in the globals from the director.
779+
NSMutableDictionary *globalShaderUniforms = [director.globalShaderUniforms mutableCopy];
780+
781+
// Group all of the standard globals into one value.
782+
// Used by Metal, will be used eventually by a GL3 renderer.
783+
CCGlobalUniforms globals = {};
784+
785+
globals.projection = *projection;
786+
globals.projectionInv = GLKMatrix4Invert(globals.projection, NULL);
787+
globalShaderUniforms[CCShaderUniformProjection] = [NSValue valueWithGLKMatrix4:globals.projection];
788+
globalShaderUniforms[CCShaderUniformProjectionInv] = [NSValue valueWithGLKMatrix4:globals.projectionInv];
789+
790+
globals.viewSize = GLKVector2Make(viewSize.width, viewSize.height);
791+
globalShaderUniforms[CCShaderUniformViewSize] = [NSValue valueWithGLKVector2:globals.viewSize];
792+
793+
CGSize pixelSize = viewSize;//CC_SIZE_SCALE(viewSize, contentScale);
794+
globals.viewSizeInPixels = GLKVector2Make(pixelSize.width, pixelSize.height);
795+
globalShaderUniforms[CCShaderUniformViewSizeInPixels] = [NSValue valueWithGLKVector2:globals.viewSizeInPixels];
796+
797+
CCTime t = director.scheduler.currentTime;
798+
globals.time = GLKVector4Make(t, t/2.0f, t/8.0f, t/8.0f);
799+
globals.sinTime = GLKVector4Make(sinf(t*2.0f), sinf(t), sinf(t/2.0f), sinf(t/4.0f));
800+
globals.cosTime = GLKVector4Make(cosf(t*2.0f), cosf(t), cosf(t/2.0f), cosf(t/4.0f));
801+
globalShaderUniforms[CCShaderUniformTime] = [NSValue valueWithGLKVector4:globals.time];
802+
globalShaderUniforms[CCShaderUniformSinTime] = [NSValue valueWithGLKVector4:globals.sinTime];
803+
globalShaderUniforms[CCShaderUniformCosTime] = [NSValue valueWithGLKVector4:globals.cosTime];
804+
805+
globals.random01 = GLKVector4Make(CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
806+
globalShaderUniforms[CCShaderUniformRandom01] = [NSValue valueWithGLKVector4:globals.random01];
807+
808+
globalShaderUniforms[CCShaderUniformDefaultGlobals] = [NSValue valueWithBytes:&globals objCType:@encode(CCGlobalUniforms)];
809+
810+
_globalShaderUniforms = globalShaderUniforms;
811+
779812
// If we are using a uniform buffer (ex: Metal) copy the global uniforms into it.
780813
CCGraphicsBuffer *uniformBuffer = _buffers->_uniformBuffer;
781814
if(uniformBuffer){

cocos2d/CCRenderer_private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ CCGraphicsBufferPushElements(CCGraphicsBuffer *buffer, size_t requestedCount)
238238

239239
@interface CCRenderer(NoARCPrivate)
240240

241-
-(void)prepareWithGlobals:(NSDictionary *)globalShaderUniforms;
241+
-(void)prepareWithProjection:(const GLKMatrix4 *)projection viewSize:(CGSize)viewSize contentScale:(CGFloat)contentScale;
242242

243243
-(void)setRenderState:(CCRenderState *)renderState;
244244

0 commit comments

Comments
 (0)