diff --git a/cocos2d/CCEffect.m b/cocos2d/CCEffect.m index d76820379cb..45233ef2157 100644 --- a/cocos2d/CCEffect.m +++ b/cocos2d/CCEffect.m @@ -27,17 +27,16 @@ @implementation CCEffectImpl --(id)initWithRenderPasses:(NSArray *)renderPasses shaders:(NSArray *)shaders +-(id)initWithRenderPassDescriptors:(NSArray *)renderPassDescriptors shaders:(NSArray *)shaders { if((self = [super init])) { _stitchFlags = CCEffectFunctionStitchBoth; _firstInStack = YES; - // Copy these arrays so the caller can't mutate them - // behind our backs later (they could be NSMutableArray + // Copy the shader array so the caller can't mutate it + // behind our backs later (it could be NSMutableArray // after all). - _renderPasses = [renderPasses copy]; _shaders = [shaders copy]; _shaderUniforms = [[NSMutableDictionary alloc] init]; @@ -54,22 +53,41 @@ -(id)initWithRenderPasses:(NSArray *)renderPasses shaders:(NSArray *)shaders // Setup the pass shaders based on the pass shader indices and // supplied shaders. - for (CCEffectRenderPass *pass in _renderPasses) + NSUInteger passIndex = 0; + NSMutableArray *renderPasses = [NSMutableArray array]; + for (CCEffectRenderPassDescriptor *passDescriptor in renderPassDescriptors) { - NSAssert([pass isKindOfClass:[CCEffectRenderPass class]], @"Expected a CCEffectRenderPass but received something else."); - NSAssert(pass.shaderIndex < _shaders.count, @"Supplied shader index out of range."); - - pass.effectShader = _shaders[pass.shaderIndex]; + NSAssert([passDescriptor isKindOfClass:[CCEffectRenderPassDescriptor class]], @"Expected a CCEffectRenderPassDescriptor but received something else."); + NSAssert(passDescriptor.shaderIndex < _shaders.count, @"Supplied shader index out of range."); // If a uniform translation table is not set already, set it to the default. - for (CCEffectRenderPassBeginBlockContext *blockContext in pass.beginBlocks) + NSMutableArray *beginBlocks = [NSMutableArray array]; + for (CCEffectBeginBlockContext *blockContext in passDescriptor.beginBlocks) { if (!blockContext.uniformTranslationTable) { - blockContext.uniformTranslationTable = allUTTs[pass.shaderIndex]; + [beginBlocks addObject:[[CCEffectBeginBlockContext alloc] initWithBlock:blockContext.block uniformTranslationTable:allUTTs[passDescriptor.shaderIndex]]]; + } + else + { + [beginBlocks addObject:blockContext]; } } + + CCEffectRenderPass *pass = [[CCEffectRenderPass alloc] initWithIndex:passIndex + texCoordsMapping:passDescriptor.texCoordsMapping + blendMode:passDescriptor.blendMode + shaderIndex:passDescriptor.shaderIndex + effectShader:_shaders[passDescriptor.shaderIndex] + beginBlocks:beginBlocks + updateBlocks:passDescriptor.updateBlocks + debugLabel:passDescriptor.debugLabel]; + + [renderPasses addObject:pass]; + + passIndex++; } + _renderPasses = [renderPasses copy]; } return self; } diff --git a/cocos2d/CCEffectBloom.m b/cocos2d/CCEffectBloom.m index 339f767cad5..0d085415dda 100644 --- a/cocos2d/CCEffectBloom.m +++ b/cocos2d/CCEffectBloom.m @@ -110,7 +110,7 @@ -(id)initWithInterface:(CCEffectBloom *)interface NSArray *renderPasses = [CCEffectBloomImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = @[[[CCEffectShader alloc] initWithVertexShaderBuilder:vertShaderBuilder fragmentShaderBuilder:fragShaderBuilder]]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectBloomImplGL"; @@ -318,9 +318,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectBloom *)interface // self is not necesssarily valid. __weak CCEffectBloom *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] initWithIndex:0]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectBloom pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; @@ -337,9 +337,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectBloom *)interface }]]; - CCEffectRenderPass *pass1 = [[CCEffectRenderPass alloc] initWithIndex:1]; + CCEffectRenderPassDescriptor *pass1 = [CCEffectRenderPassDescriptor descriptor]; pass1.debugLabel = @"CCEffectBloom pass 1"; - pass1.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass1.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:GLKVector2Make(0.5f, 0.5f)]; @@ -355,11 +355,11 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectBloom *)interface }]]; - CCEffectRenderPass *pass2 = [[CCEffectRenderPass alloc] initWithIndex:2]; + CCEffectRenderPassDescriptor *pass2 = [CCEffectRenderPassDescriptor descriptor]; pass2.debugLabel = @"CCEffectBloom pass 2"; - pass2.texCoord1Mapping = CCEffectTexCoordMapPreviousPassTex; - pass2.texCoord2Mapping = CCEffectTexCoordMapMainTex; - pass2.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + CCEffectTexCoordsMapping texCoordsMapping = { CCEffectTexCoordMapPreviousPassTex, CCEffectTexCoordMapMainTex }; + pass2.texCoordsMapping = texCoordsMapping; + pass2.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:GLKVector2Make(0.5f, 0.5f)]; diff --git a/cocos2d/CCEffectBlur.m b/cocos2d/CCEffectBlur.m index ff3f287b77f..5502b82149e 100644 --- a/cocos2d/CCEffectBlur.m +++ b/cocos2d/CCEffectBlur.m @@ -98,7 +98,7 @@ -(id)initWithInterface:(CCEffectBlur *)interface NSArray *renderPasses = [CCEffectBlurImplGL buildRenderPasses]; NSArray *shaders = @[[[CCEffectShader alloc] initWithVertexShaderBuilder:vertShaderBuilder fragmentShaderBuilder:fragShaderBuilder]]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectBlurImplGL"; @@ -271,10 +271,10 @@ + (NSArray *)buildRenderPasses // pass 0: blurs (horizontal) texture[0] and outputs blurmap to texture[1] // pass 1: blurs (vertical) texture[1] and outputs to texture[2] - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] initWithIndex:0]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectBlur pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; @@ -288,10 +288,10 @@ + (NSArray *)buildRenderPasses }]]; - CCEffectRenderPass *pass1 = [[CCEffectRenderPass alloc] initWithIndex:1]; + CCEffectRenderPassDescriptor *pass1 = [CCEffectRenderPassDescriptor descriptor]; pass1.debugLabel = @"CCEffectBlur pass 1"; pass1.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass1.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass1.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectBrightness.m b/cocos2d/CCEffectBrightness.m index ef41f08e7be..91821dc8a32 100644 --- a/cocos2d/CCEffectBrightness.m +++ b/cocos2d/CCEffectBrightness.m @@ -31,7 +31,7 @@ -(id)initWithInterface:(CCEffectBrightness *)interface NSArray *renderPasses = [CCEffectBrightnessImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectBrightnessImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectBrightnessImplGL"; @@ -81,9 +81,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectBrightness *)interface { __weak CCEffectBrightness *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectBrightness pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectColorChannelOffset.m b/cocos2d/CCEffectColorChannelOffset.m index f704a1da924..68ef3c4ea91 100644 --- a/cocos2d/CCEffectColorChannelOffset.m +++ b/cocos2d/CCEffectColorChannelOffset.m @@ -30,7 +30,7 @@ -(id)initWithInterface:(CCEffectColorChannelOffset *)interface NSArray *renderPasses = [CCEffectColorChannelOffsetImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectColorChannelOffsetImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectColorChannelOffsetImplGL"; @@ -100,10 +100,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectColorChannelOffset *)interf { __weak CCEffectColorChannelOffset *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectColorChannelOffset pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectContrast.m b/cocos2d/CCEffectContrast.m index 246b39db711..c8d998f7d9f 100644 --- a/cocos2d/CCEffectContrast.m +++ b/cocos2d/CCEffectContrast.m @@ -32,7 +32,7 @@ -(id)initWithInterface:(CCEffectContrast *)interface NSArray *renderPasses = [CCEffectContrastImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectContrastImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectContrastImplGL"; @@ -83,9 +83,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectContrast *)interface { __weak CCEffectContrast *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectContrast pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectDFInnerGlow.m b/cocos2d/CCEffectDFInnerGlow.m index b0ab03c87b0..37c00cce508 100644 --- a/cocos2d/CCEffectDFInnerGlow.m +++ b/cocos2d/CCEffectDFInnerGlow.m @@ -39,7 +39,7 @@ -(id)initWithInterface:(CCEffectDFInnerGlow *)interface NSArray *renderPasses = [CCEffectDFInnerGlowImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectDFInnerGlowImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectDFInnerGlowImplGL"; @@ -157,10 +157,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectDFInnerGlow *)interface { __weak CCEffectDFInnerGlow *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectDFInnerGlow pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformNormalMapTexture] = weakInterface.distanceField; passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectDFOutline.m b/cocos2d/CCEffectDFOutline.m index 282a4b157ee..ba14c3dbf9b 100644 --- a/cocos2d/CCEffectDFOutline.m +++ b/cocos2d/CCEffectDFOutline.m @@ -37,7 +37,7 @@ -(id)initWithInterface:(CCEffectDFOutline *)interface NSArray *renderPasses = [CCEffectDFOutlineImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectDFOutlineImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectDFOutlineImplGL"; @@ -128,10 +128,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectDFOutline *)interface { __weak CCEffectDFOutline *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectDFOutline pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformNormalMapTexture] = weakInterface.distanceField; passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectDistanceField.m b/cocos2d/CCEffectDistanceField.m index b1a9ee8e192..0c93612e61f 100644 --- a/cocos2d/CCEffectDistanceField.m +++ b/cocos2d/CCEffectDistanceField.m @@ -32,7 +32,7 @@ -(id)initWithInterface:(CCEffectDistanceField *)interface NSArray *renderPasses = [CCEffectDistanceFieldImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectDistanceFieldImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectDistanceFieldImplGL"; @@ -145,10 +145,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectDistanceField *)interface { __weak CCEffectDistanceField *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectDistanceField pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectDropShadow.m b/cocos2d/CCEffectDropShadow.m index cf68929d56b..1c1e51bcb72 100644 --- a/cocos2d/CCEffectDropShadow.m +++ b/cocos2d/CCEffectDropShadow.m @@ -115,7 +115,7 @@ -(id)initWithInterface:(CCEffectDropShadow *)interface NSArray *renderPasses = [CCEffectDropShadowImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = @[[[CCEffectShader alloc] initWithVertexShaderBuilder:vertShaderBuilder fragmentShaderBuilder:fragShaderBuilder]]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectDropShadowImplGL"; @@ -279,10 +279,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectDropShadow *)interface __weak CCEffectDropShadow *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectDropShadow pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; @@ -294,10 +294,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectDropShadow *)interface }]]; - CCEffectRenderPass *pass1 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass1 = [CCEffectRenderPassDescriptor descriptor]; pass1.debugLabel = @"CCEffectDropShadow pass 1"; pass1.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass1.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass1.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; GLKVector2 dur = GLKVector2Make(0.0, 1.0 / (passInputs.previousPassTexture.sizeInPixels.height / passInputs.previousPassTexture.contentScale)); @@ -306,10 +306,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectDropShadow *)interface }]]; - CCEffectRenderPass *pass3 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass3 = [CCEffectRenderPassDescriptor descriptor]; pass3.debugLabel = @"CCEffectDropShadow pass 3"; pass3.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass3.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass3.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; GLKVector2 dur = GLKVector2Make(0.0, 1.0 / (passInputs.previousPassTexture.sizeInPixels.height / passInputs.previousPassTexture.contentScale)); diff --git a/cocos2d/CCEffectGlass.m b/cocos2d/CCEffectGlass.m index d3c06caaccf..4e2fb61c3e3 100644 --- a/cocos2d/CCEffectGlass.m +++ b/cocos2d/CCEffectGlass.m @@ -43,7 +43,7 @@ -(id)initWithInterface:(CCEffectGlass *)interface NSArray *renderPasses = [CCEffectGlassImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectGlassImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectGlass"; @@ -217,9 +217,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectGlass *)interface { __weak CCEffectGlass *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectGlass pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectHue.m b/cocos2d/CCEffectHue.m index 25e80a94073..ba4c3266b4e 100644 --- a/cocos2d/CCEffectHue.m +++ b/cocos2d/CCEffectHue.m @@ -33,7 +33,7 @@ -(id)initWithInterface:(CCEffectHue *)interface NSArray *renderPasses = [CCEffectHueImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectHueImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectHueImplGL"; @@ -84,9 +84,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectHue *)interface { __weak CCEffectHue *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectHue pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectInvert.m b/cocos2d/CCEffectInvert.m index 552faed6775..5c0332e8887 100644 --- a/cocos2d/CCEffectInvert.m +++ b/cocos2d/CCEffectInvert.m @@ -26,7 +26,7 @@ -(id)init NSArray *renderPasses = [CCEffectInvertImplGL buildRenderPasses]; NSArray *shaders = [CCEffectInvertImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.debugName = @"CCEffectInvertImplGL"; } @@ -75,10 +75,10 @@ + (NSArray *)buildFragmentFunctions + (NSArray *)buildRenderPasses { - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectInvert pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectLighting.m b/cocos2d/CCEffectLighting.m index bbf05615a53..77b63f57e19 100644 --- a/cocos2d/CCEffectLighting.m +++ b/cocos2d/CCEffectLighting.m @@ -126,7 +126,7 @@ -(id)initWithInterface:(CCEffectLighting *)interface NSArray *shaders = @[[[CCEffectShader alloc] initWithVertexShaderBuilder:vertShaderBuilder fragmentShaderBuilder:fragShaderBuilder]]; NSArray *renderPasses = [CCEffectLightingImplGL buildRenderPassesWithInterface:interface]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectLightingImplGL"; @@ -258,9 +258,9 @@ +(NSArray *)buildRenderPassesWithInterface:(CCEffectLighting *)interface { __weak CCEffectLighting *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectLighting pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectOutline.m b/cocos2d/CCEffectOutline.m index 8e729b5d7fd..2ee626744e5 100644 --- a/cocos2d/CCEffectOutline.m +++ b/cocos2d/CCEffectOutline.m @@ -32,7 +32,7 @@ -(id)initWithInterface:(CCEffectOutline *)interface NSArray *renderPasses = [CCEffectOutlineImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectOutlineImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { _interface = interface; self.debugName = @"CCEffectOutline"; @@ -116,10 +116,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectOutline *)interface { __weak CCEffectOutline *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectOutline pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; @@ -141,10 +141,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectOutline *)interface // Pass 1 is a WIP (trying to scale the outline before applying it. (a bad idea so far..) #if 1 - CCEffectRenderPass *pass1 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass1 = [CCEffectRenderPassDescriptor descriptor]; pass1.debugLabel = @"CCEffectOutline pass 1"; pass1.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass1.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass1.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:passInputs.texCoord1Center]; diff --git a/cocos2d/CCEffectPixellate.m b/cocos2d/CCEffectPixellate.m index bc9f4faa3f3..089472ce28d 100644 --- a/cocos2d/CCEffectPixellate.m +++ b/cocos2d/CCEffectPixellate.m @@ -66,7 +66,7 @@ -(id)initWithInterface:(CCEffectPixellate *)interface NSArray *renderPasses = [CCEffectPixellateImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectPixellateImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectPixellateImplGL"; @@ -121,10 +121,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectPixellate *)interface { __weak CCEffectPixellate *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectPixellate pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectReflection.m b/cocos2d/CCEffectReflection.m index 11cb1808b51..354aa29a374 100644 --- a/cocos2d/CCEffectReflection.m +++ b/cocos2d/CCEffectReflection.m @@ -39,7 +39,7 @@ -(id)initWithInterface:(CCEffectReflection *)interface NSArray *renderPasses = [CCEffectReflectionImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectReflectionImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectReflectionImplGL"; @@ -172,9 +172,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectReflection *)interface { __weak CCEffectReflection *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectReflection pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectRefraction.m b/cocos2d/CCEffectRefraction.m index e14601526ec..e92e46353f7 100644 --- a/cocos2d/CCEffectRefraction.m +++ b/cocos2d/CCEffectRefraction.m @@ -37,7 +37,7 @@ -(id)initWithInterface:(CCEffectRefraction *)interface NSArray *renderPasses = [CCEffectRefractionImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectRefractionImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectRefractionImplGL"; @@ -153,9 +153,9 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectRefraction *)interface { __weak CCEffectRefraction *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectRefraction pass 0"; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectRenderPass.h b/cocos2d/CCEffectRenderPass.h index 29ac0d567df..eb367c3e965 100644 --- a/cocos2d/CCEffectRenderPass.h +++ b/cocos2d/CCEffectRenderPass.h @@ -20,6 +20,16 @@ typedef NS_ENUM(NSUInteger, CCEffectTexCoordMapping) CCEffectTexCoordMapCustomTexNoTransform = 3 }; +typedef struct CCEffectTexCoordsMapping +{ + CCEffectTexCoordMapping tc1; + CCEffectTexCoordMapping tc2; + +} CCEffectTexCoordsMapping; + +static const CCEffectTexCoordsMapping CCEffectTexCoordsMappingDefault = { CCEffectTexCoordMapPreviousPassTex, CCEffectTexCoordMapCustomTex }; + + @interface CCEffectRenderPassInputs : NSObject @@ -43,33 +53,55 @@ typedef NS_ENUM(NSUInteger, CCEffectTexCoordMapping) @class CCEffectRenderPass; -typedef void (^CCEffectRenderPassBeginBlock)(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs); -typedef void (^CCEffectRenderPassUpdateBlock)(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs); +typedef void (^CCEffectBeginBlock)(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs); +typedef void (^CCEffectUpdateBlock)(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs); -@interface CCEffectRenderPassBeginBlockContext : NSObject +@interface CCEffectBeginBlockContext : NSObject -@property (nonatomic, copy) CCEffectRenderPassBeginBlock block; -@property (nonatomic, strong) NSDictionary *uniformTranslationTable; +@property (nonatomic, readonly) CCEffectBeginBlock block; +@property (nonatomic, readonly) NSDictionary *uniformTranslationTable; --(id)initWithBlock:(CCEffectRenderPassBeginBlock)block; +-(id)initWithBlock:(CCEffectBeginBlock)block uniformTranslationTable:(NSDictionary *)utt; +-(id)initWithBlock:(CCEffectBeginBlock)block; @end -@interface CCEffectRenderPass : NSObject +@interface CCEffectRenderPassDescriptor : NSObject -@property (nonatomic, readonly) NSUInteger indexInEffect; @property (nonatomic, assign) NSUInteger shaderIndex; -@property (nonatomic, assign) CCEffectTexCoordMapping texCoord1Mapping; -@property (nonatomic, assign) CCEffectTexCoordMapping texCoord2Mapping; +@property (nonatomic, assign) CCEffectTexCoordsMapping texCoordsMapping; @property (nonatomic, strong) CCBlendMode* blendMode; -@property (nonatomic, strong) CCEffectShader* effectShader; @property (nonatomic, copy) NSArray* beginBlocks; @property (nonatomic, copy) NSArray* updateBlocks; @property (nonatomic, copy) NSString *debugLabel; --(id)initWithIndex:(NSUInteger)indexInEffect; +-(id)init; ++(instancetype)descriptor; + +@end + + +@interface CCEffectRenderPass : NSObject + +@property (nonatomic, readonly) NSUInteger indexInEffect; +@property (nonatomic, readonly) CCEffectTexCoordsMapping texCoordsMapping; +@property (nonatomic, readonly) CCBlendMode *blendMode; +@property (nonatomic, readonly) NSUInteger shaderIndex; +@property (nonatomic, readonly) CCEffectShader *effectShader; +@property (nonatomic, readonly) NSArray *beginBlocks; +@property (nonatomic, readonly) NSArray *updateBlocks; +@property (nonatomic, readonly) NSString *debugLabel; + +-(id)initWithIndex:(NSUInteger)indexInEffect + texCoordsMapping:(CCEffectTexCoordsMapping)texCoordsMapping + blendMode:(CCBlendMode *)blendMode + shaderIndex:(NSUInteger)shaderIndex + effectShader:(CCEffectShader *)effectShader + beginBlocks:(NSArray *)beginBlocks + updateBlocks:(NSArray *)updateBlocks + debugLabel:(NSString *)debugLabel; -(void)begin:(CCEffectRenderPassInputs *)passInputs; -(void)update:(CCEffectRenderPassInputs *)passInputs; diff --git a/cocos2d/CCEffectRenderPass.m b/cocos2d/CCEffectRenderPass.m index fbee5bb7013..1a1ede76fa4 100644 --- a/cocos2d/CCEffectRenderPass.m +++ b/cocos2d/CCEffectRenderPass.m @@ -22,59 +22,111 @@ -(id)init @end -#pragma mark CCEffectRenderPassBeginBlockContext +#pragma mark CCEffectBeginBlockContext -@implementation CCEffectRenderPassBeginBlockContext +@implementation CCEffectBeginBlockContext --(id)initWithBlock:(CCEffectRenderPassBeginBlock)block; +-(id)initWithBlock:(CCEffectBeginBlock)block uniformTranslationTable:(NSDictionary *)utt { if (self = [super init]) { _block = [block copy]; + _uniformTranslationTable = [utt copy]; } return self; } --(instancetype)copyWithZone:(NSZone *)zone +-(id)initWithBlock:(CCEffectBeginBlock)block +{ + return [self initWithBlock:block uniformTranslationTable:nil]; +} + +- (instancetype)copyWithZone:(NSZone *)zone { - CCEffectRenderPassBeginBlockContext *newContext = [[CCEffectRenderPassBeginBlockContext allocWithZone:zone] initWithBlock:_block]; - newContext.uniformTranslationTable = _uniformTranslationTable; - return newContext; + // CCEffectBeginBlockContext is immutable so no need to really copy. + return self; } @end -#pragma mark CCEffectRenderPass +#pragma mark CCEffectRenderPassDescriptor -@implementation CCEffectRenderPass +@implementation CCEffectRenderPassDescriptor -(id)init { - return [self initWithIndex:0]; + if((self = [super init])) + { + _shaderIndex = 0; + _texCoordsMapping = CCEffectTexCoordsMappingDefault; + _blendMode = [CCBlendMode premultipliedAlphaMode]; + _beginBlocks = nil; + _updateBlocks = nil; + _debugLabel = nil; + } + return self; +} + ++(instancetype)descriptor +{ + return [[self alloc] init]; } +-(instancetype)copyWithZone:(NSZone *)zone +{ + CCEffectRenderPassDescriptor *newDescriptor = [[CCEffectRenderPassDescriptor alloc] init]; + newDescriptor.shaderIndex = _shaderIndex; + newDescriptor.texCoordsMapping = _texCoordsMapping; + newDescriptor.blendMode = _blendMode; + newDescriptor.beginBlocks = _beginBlocks; + newDescriptor.updateBlocks = _updateBlocks; + newDescriptor.debugLabel = _debugLabel; + return newDescriptor; +} + +@end + + +#pragma mark CCEffectRenderPass + +@implementation CCEffectRenderPass + -(id)initWithIndex:(NSUInteger)indexInEffect + texCoordsMapping:(CCEffectTexCoordsMapping)texCoordsMapping + blendMode:(CCBlendMode *)blendMode + shaderIndex:(NSUInteger)shaderIndex + effectShader:(CCEffectShader *)effectShader + beginBlocks:(NSArray *)beginBlocks + updateBlocks:(NSArray *)updateBlocks + debugLabel:(NSString *)debugLabel { if((self = [super init])) { _indexInEffect = indexInEffect; - _shaderIndex = 0; + _texCoordsMapping = texCoordsMapping; + _blendMode = blendMode; + _shaderIndex = shaderIndex; + _effectShader = effectShader; + _beginBlocks = [beginBlocks copy]; - _texCoord1Mapping = CCEffectTexCoordMapPreviousPassTex; - _texCoord2Mapping = CCEffectTexCoordMapCustomTex; + if (updateBlocks) + { + _updateBlocks = [updateBlocks copy]; + } + else + { + CCEffectUpdateBlock updateBlock = ^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + if (passInputs.needsClear) + { + [passInputs.renderer enqueueClear:GL_COLOR_BUFFER_BIT color:[CCColor clearColor].glkVector4 depth:0.0f stencil:0 globalSortOrder:NSIntegerMin]; + } + [pass enqueueTriangles:passInputs]; + }; + _updateBlocks = @[[updateBlock copy]]; + } - _beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){}]]; - - CCEffectRenderPassUpdateBlock updateBlock = ^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ - if (passInputs.needsClear) - { - [passInputs.renderer enqueueClear:GL_COLOR_BUFFER_BIT color:[CCColor clearColor].glkVector4 depth:0.0f stencil:0 globalSortOrder:NSIntegerMin]; - } - [pass enqueueTriangles:passInputs]; - }; - _updateBlocks = @[[updateBlock copy]]; - _blendMode = [CCBlendMode premultipliedAlphaMode]; + _debugLabel = [debugLabel copy]; return self; } @@ -82,23 +134,15 @@ -(id)initWithIndex:(NSUInteger)indexInEffect return self; } --(instancetype)copyWithZone:(NSZone *)zone +- (instancetype)copyWithZone:(NSZone *)zone { - CCEffectRenderPass *newPass = [[CCEffectRenderPass allocWithZone:zone] initWithIndex:_indexInEffect]; - newPass.shaderIndex = _shaderIndex; - newPass.texCoord1Mapping = _texCoord1Mapping; - newPass.texCoord2Mapping = _texCoord2Mapping; - newPass.blendMode = _blendMode; - newPass.effectShader = _effectShader; - newPass.beginBlocks = [_beginBlocks copy]; - newPass.updateBlocks = [_updateBlocks copy]; - newPass.debugLabel = _debugLabel; - return newPass; + // CCEffectRenderPass is immutable so no need to really copy. + return self; } -(void)begin:(CCEffectRenderPassInputs *)passInputs { - for (CCEffectRenderPassBeginBlockContext *blockContext in _beginBlocks) + for (CCEffectBeginBlockContext *blockContext in _beginBlocks) { passInputs.uniformTranslationTable = blockContext.uniformTranslationTable; blockContext.block(self, passInputs); @@ -107,7 +151,7 @@ -(void)begin:(CCEffectRenderPassInputs *)passInputs -(void)update:(CCEffectRenderPassInputs *)passInputs { - for (CCEffectRenderPassUpdateBlock block in _updateBlocks) + for (CCEffectUpdateBlock block in _updateBlocks) { block(self, passInputs); } diff --git a/cocos2d/CCEffectRenderer.m b/cocos2d/CCEffectRenderer.m index 7f5692d5494..95ecb4a1959 100644 --- a/cocos2d/CCEffectRenderer.m +++ b/cocos2d/CCEffectRenderer.m @@ -264,14 +264,20 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect uniforms:(NSM } else { - renderPass = [[CCEffectRenderPass alloc] init]; - renderPass.debugLabel = @"CCEffectRenderer composite pass"; - renderPass.effectShader = [CCEffectRenderer sharedCopyShader]; - renderPass.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + NSArray *beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; }]]; + + renderPass = [[CCEffectRenderPass alloc] initWithIndex:0 + texCoordsMapping:CCEffectTexCoordsMappingDefault + blendMode:[CCBlendMode premultipliedAlphaMode] + shaderIndex:0 + effectShader:[CCEffectRenderer sharedCopyShader] + beginBlocks:beginBlocks + updateBlocks:nil + debugLabel:@"CCEffectRenderer composite pass"]; } if (fromIntermediate && (renderPass.indexInEffect == 0)) @@ -292,8 +298,8 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect uniforms:(NSM // - Later pass into intermediate RT : Pad vertices, overwrite texture coordiates so they are lower-left (0,0) upper right (1, 1), add padding to RT, adjust ortho matrix // - CCEffectTexCoordFunc tc1 = selectTexCoordFunc(renderPass.texCoord1Mapping, CCEffectTexCoordSource1, fromIntermediate, padMainTexCoords); - CCEffectTexCoordFunc tc2 = selectTexCoordFunc(renderPass.texCoord2Mapping, CCEffectTexCoordSource2, fromIntermediate, padMainTexCoords); + CCEffectTexCoordFunc tc1 = selectTexCoordFunc(renderPass.texCoordsMapping.tc1, CCEffectTexCoordSource1, fromIntermediate, padMainTexCoords); + CCEffectTexCoordFunc tc2 = selectTexCoordFunc(renderPass.texCoordsMapping.tc2, CCEffectTexCoordSource2, fromIntermediate, padMainTexCoords); renderPassInputs.verts = padVertices(sprite.vertexes, effect.padding, tc1, tc2); diff --git a/cocos2d/CCEffectSaturation.m b/cocos2d/CCEffectSaturation.m index d64ef9dd744..474ab30a9b6 100644 --- a/cocos2d/CCEffectSaturation.m +++ b/cocos2d/CCEffectSaturation.m @@ -68,7 +68,7 @@ -(id)initWithInterface:(CCEffectSaturation *)interface NSArray *renderPasses = [CCEffectSaturationImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectSaturationImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectSaturationImplGL"; @@ -124,10 +124,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectSaturation *)interface { __weak CCEffectSaturation *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectSaturation pass 0"; pass0.blendMode = [CCBlendMode premultipliedAlphaMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:passInputs.texCoord1Center]; diff --git a/cocos2d/CCEffectStack.m b/cocos2d/CCEffectStack.m index 98dc22e8546..2e18f038e4e 100644 --- a/cocos2d/CCEffectStack.m +++ b/cocos2d/CCEffectStack.m @@ -200,7 +200,7 @@ + (CCEffectImpl *)processEffectsWithStitcher:(NSArray *)effects withStitching:(B } // Stitch the stitch lists and collect the resulting render passes and shaders. - NSMutableArray *outputPasses = [[NSMutableArray alloc] init]; + NSMutableArray *outputPassDescriptors = [[NSMutableArray alloc] init]; NSMutableArray *outputShaders = [[NSMutableArray alloc] init]; for (int stitchListIndex = 0; stitchListIndex < stitchLists.count; stitchListIndex++) { @@ -209,12 +209,12 @@ + (CCEffectImpl *)processEffectsWithStitcher:(NSArray *)effects withStitching:(B NSString *prefix = [NSString stringWithFormat:@"SL%d_", stitchListIndex]; CCEffectStitcher *stitcher = [CCEffectStitcher stitcherWithEffects:stitchList manglePrefix:prefix stitchListIndex:stitchListIndex shaderStartIndex:outputShaders.count]; - [outputPasses addObjectsFromArray:stitcher.renderPasses]; + [outputPassDescriptors addObjectsFromArray:stitcher.renderPassDescriptors]; [outputShaders addObjectsFromArray:stitcher.shaders]; } // Create a new effect implementation from all the collected passes and shaders. - return [[CCEffectImpl alloc] initWithRenderPasses:outputPasses shaders:outputShaders]; + return [[CCEffectImpl alloc] initWithRenderPassDescriptors:outputPassDescriptors shaders:outputShaders]; } @end diff --git a/cocos2d/CCEffectStereo.m b/cocos2d/CCEffectStereo.m index 5dce698d742..f14755de869 100644 --- a/cocos2d/CCEffectStereo.m +++ b/cocos2d/CCEffectStereo.m @@ -32,7 +32,7 @@ -(id)initWithInterface:(CCEffectStereo *)interface NSArray *renderPasses = [CCEffectStereoImplGL buildRenderPassesWithInterface:interface]; NSArray *shaders = [CCEffectStereoImplGL buildShaders]; - if((self = [super initWithRenderPasses:renderPasses shaders:shaders])) + if((self = [super initWithRenderPassDescriptors:renderPasses shaders:shaders])) { self.interface = interface; self.debugName = @"CCEffectStereoImpl"; @@ -101,10 +101,10 @@ + (NSArray *)buildRenderPassesWithInterface:(CCEffectStereo *)interface { __weak CCEffectStereo *weakInterface = interface; - CCEffectRenderPass *pass0 = [[CCEffectRenderPass alloc] init]; + CCEffectRenderPassDescriptor *pass0 = [CCEffectRenderPassDescriptor descriptor]; pass0.debugLabel = @"CCEffectPixellate pass 0"; pass0.blendMode = [CCBlendMode disabledMode]; - pass0.beginBlocks = @[[[CCEffectRenderPassBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ + pass0.beginBlocks = @[[[CCEffectBeginBlockContext alloc] initWithBlock:^(CCEffectRenderPass *pass, CCEffectRenderPassInputs *passInputs){ passInputs.shaderUniforms[CCShaderUniformMainTexture] = passInputs.previousPassTexture; passInputs.shaderUniforms[CCShaderUniformPreviousPassTexture] = passInputs.previousPassTexture; diff --git a/cocos2d/CCEffectStitcher.h b/cocos2d/CCEffectStitcher.h index 2f1969a77e7..19cb19847dd 100644 --- a/cocos2d/CCEffectStitcher.h +++ b/cocos2d/CCEffectStitcher.h @@ -10,7 +10,7 @@ @interface CCEffectStitcher : NSObject -@property (nonatomic, readonly) NSArray *renderPasses; +@property (nonatomic, readonly) NSArray *renderPassDescriptors; @property (nonatomic, readonly) NSArray *shaders; - (id)init; diff --git a/cocos2d/CCEffectStitcher.m b/cocos2d/CCEffectStitcher.m index 295fd4ef9be..b5552971962 100644 --- a/cocos2d/CCEffectStitcher.m +++ b/cocos2d/CCEffectStitcher.m @@ -23,7 +23,7 @@ + (instancetype)stitcherWithEffects:(NSArray *)effects manglePrefix:(NSString *) } -- (NSArray *)renderPasses +- (NSArray *)renderPassDescriptors { NSAssert(0, @"Subclasses must override this."); return nil; diff --git a/cocos2d/CCEffectStitcherGL.m b/cocos2d/CCEffectStitcherGL.m index f053f7582ea..03ce5fb9193 100644 --- a/cocos2d/CCEffectStitcherGL.m +++ b/cocos2d/CCEffectStitcherGL.m @@ -32,7 +32,7 @@ @interface CCEffectStitcherGL () @property (nonatomic, assign) NSUInteger shaderStartIndex; // Outputs -@property (nonatomic, strong) NSArray *cachedRenderPasses; +@property (nonatomic, strong) NSArray *cachedRenderPassDescriptors; @property (nonatomic, strong) NSArray *cachedShaders; @end @@ -53,27 +53,27 @@ - (id)initWithEffects:(NSArray *)effects manglePrefix:(NSString *)prefix stitchL _stitchListIndex = stitchListIndex; _shaderStartIndex = shaderStartIndex; - _cachedRenderPasses = nil; + _cachedRenderPassDescriptors = nil; _cachedShaders = nil; } return self; } -- (NSArray *)renderPasses +- (NSArray *)renderPassDescriptors { // The output render pass and shader arrays are computed lazily when requested. // One method computes both of them so we need to make sure everything stays // in sync (ie if we don't have one we don't have the other and if one gets // created so does the other). - if (!_cachedRenderPasses) + if (!_cachedRenderPassDescriptors) { NSAssert(!_cachedShaders, @"The output render pass array is nil but the output shader array is not."); [self stitchEffects:self.effects manglePrefix:self.manglePrefix stitchListIndex:self.stitchListIndex shaderStartIndex:self.shaderStartIndex]; - NSAssert(_cachedRenderPasses, @"Failed to create an output render pass array."); + NSAssert(_cachedRenderPassDescriptors, @"Failed to create an output render pass array."); NSAssert(_cachedShaders, @"Failed to create an output shader array."); } - return _cachedRenderPasses; + return _cachedRenderPassDescriptors; } - (NSArray *)shaders @@ -84,10 +84,10 @@ - (NSArray *)shaders // created so does the other). if (!_cachedShaders) { - NSAssert(!_cachedRenderPasses, @"The output shader array is nil but the output render pass array is not."); + NSAssert(!_cachedRenderPassDescriptors, @"The output shader array is nil but the output render pass array is not."); [self stitchEffects:self.effects manglePrefix:self.manglePrefix stitchListIndex:self.stitchListIndex shaderStartIndex:self.shaderStartIndex]; - NSAssert(_cachedRenderPasses, @"Failed to create an output render pass array."); + NSAssert(_cachedRenderPassDescriptors, @"Failed to create an output render pass array."); NSAssert(_cachedShaders, @"Failed to create an output shader array."); } return _cachedShaders; @@ -169,29 +169,35 @@ - (void)stitchEffects:(NSArray *)effects manglePrefix:(NSString *)prefix stitchL // the uniform translation table. CCEffectImpl *effect = [effects firstObject]; - NSMutableArray *renderPasses = [[NSMutableArray alloc] init]; + NSMutableArray *renderPassDescriptors = [[NSMutableArray alloc] init]; for (CCEffectRenderPass *pass in effect.renderPasses) { - CCEffectRenderPass *newPass = [pass copy]; - newPass.shaderIndex += shaderStartIndex; - + CCEffectRenderPassDescriptor *newDescriptor = [CCEffectRenderPassDescriptor descriptor]; + newDescriptor.shaderIndex = pass.shaderIndex + shaderStartIndex; + newDescriptor.texCoordsMapping = pass.texCoordsMapping; + newDescriptor.blendMode = pass.blendMode; + // Update the uniform translation table in the new pass's begin blocks - for (CCEffectRenderPassBeginBlockContext *blockContext in newPass.beginBlocks) + NSMutableArray *beginBlocks = [[NSMutableArray alloc] init]; + for (CCEffectBeginBlockContext *blockContext in pass.beginBlocks) { - blockContext.uniformTranslationTable = allUTTs[pass.effectShader]; + [beginBlocks addObject:[[CCEffectBeginBlockContext alloc] initWithBlock:blockContext.block uniformTranslationTable:allUTTs[pass.effectShader]]]; } + newDescriptor.beginBlocks = beginBlocks; + newDescriptor.updateBlocks = pass.updateBlocks; + newDescriptor.debugLabel = pass.debugLabel; - [renderPasses addObject:newPass]; + [renderPassDescriptors addObject:newDescriptor]; } - _cachedRenderPasses = [renderPasses copy]; + _cachedRenderPassDescriptors = [renderPassDescriptors copy]; } else - { - // Create a new render pass and point it at the stitched shader. - CCEffectRenderPass *renderPass = [[CCEffectRenderPass alloc] init]; - renderPass.debugLabel = [NSString stringWithFormat:@"CCEffectStack_Stitched_%@", prefix];; - renderPass.shaderIndex = shaderStartIndex; + { + CCEffectRenderPassDescriptor *newDescriptor = [CCEffectRenderPassDescriptor descriptor]; + newDescriptor.shaderIndex = shaderStartIndex; + newDescriptor.texCoordsMapping = CCEffectTexCoordsMappingDefault; + newDescriptor.blendMode = [CCBlendMode premultipliedAlphaMode]; NSMutableArray *beginBlocks = [[NSMutableArray alloc] init]; NSMutableArray *updateBlocks = [[NSMutableArray alloc] init]; @@ -201,26 +207,22 @@ - (void)stitchEffects:(NSArray *)effects manglePrefix:(NSString *)prefix stitchL { for (CCEffectRenderPass *pass in effect.renderPasses) { - for (CCEffectRenderPassBeginBlockContext *blockContext in pass.beginBlocks) + for (CCEffectBeginBlockContext *blockContext in pass.beginBlocks) { - // Copy the context and set the UTT to the new UTT for the corresponding - // shader for this pass. - CCEffectRenderPassBeginBlockContext *newContext = [blockContext copy]; - newContext.uniformTranslationTable = allUTTs[pass.effectShader]; - - [beginBlocks addObject:newContext]; + [beginBlocks addObject:[[CCEffectBeginBlockContext alloc] initWithBlock:blockContext.block uniformTranslationTable:allUTTs[pass.effectShader]]]; } // Copy the update blocks. They don't need any adjustment so they can just // be copied outright. - [updateBlocks addObjectsFromArray:[pass.updateBlocks copy]]; + [updateBlocks addObjectsFromArray:pass.updateBlocks]; } } + + newDescriptor.beginBlocks = beginBlocks; + newDescriptor.updateBlocks = updateBlocks; + newDescriptor.debugLabel = [NSString stringWithFormat:@"CCEffectStack_Stitched_%@", prefix];; - renderPass.beginBlocks = beginBlocks; - renderPass.updateBlocks = updateBlocks; - - _cachedRenderPasses = @[renderPass]; + _cachedRenderPassDescriptors = @[newDescriptor]; } } diff --git a/cocos2d/CCEffect_Private.h b/cocos2d/CCEffect_Private.h index 48d1c05ae39..f76528df7aa 100644 --- a/cocos2d/CCEffect_Private.h +++ b/cocos2d/CCEffect_Private.h @@ -83,7 +83,7 @@ typedef NS_ENUM(NSUInteger, CCEffectFunctionStitchFlags) @property (nonatomic, readonly) BOOL firstInStack; --(id)initWithRenderPasses:(NSArray *)renderPasses shaders:(NSArray *)shaders; +-(id)initWithRenderPassDescriptors:(NSArray *)renderPasses shaders:(NSArray *)shaders; -(CCEffectPrepareResult)prepareForRenderingWithSprite:(CCSprite *)sprite; -(CCEffectRenderPass *)renderPassAtIndex:(NSUInteger)passIndex;