Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metal Prep Step 9 - Immutable render passes #1313

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions cocos2d/CCEffect.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
}
Expand Down
18 changes: 9 additions & 9 deletions cocos2d/CCEffectBloom.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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)];
Expand All @@ -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)];
Expand Down
10 changes: 5 additions & 5 deletions cocos2d/CCEffectBlur.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions cocos2d/CCEffectBrightness.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions cocos2d/CCEffectColorChannelOffset.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions cocos2d/CCEffectContrast.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions cocos2d/CCEffectDFInnerGlow.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions cocos2d/CCEffectDFOutline.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions cocos2d/CCEffectDistanceField.m
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
Loading