@@ -186,8 +186,7 @@ - (CCEffectPrepareStatus)prepareForRendering
186186 int effectIndex = 0 ;
187187 for (NSArray *stitchList in stitchLists)
188188 {
189- NSAssert (stitchList.count > 0 , @" Encountered an empty stitch list which shouldn't happen." );
190- [stitchedEffects addObject: [self stitchEffects: stitchList startIndex: effectIndex]];
189+ [stitchedEffects addObject: [CCEffectStack stitchEffects: stitchList startIndex: effectIndex]];
191190 effectIndex += stitchList.count ;
192191 }
193192 }
@@ -222,16 +221,20 @@ - (BOOL)readyForRendering
222221
223222#pragma mark - Internal
224223
225- - (CCEffect *)stitchEffects : (NSArray *)effects startIndex : (int )startIndex
224+ + (CCEffect *)stitchEffects : (NSArray *)stitchList startIndex : (int )startIndex
226225{
226+ NSAssert (stitchList.count > 0 , @" Encountered an empty stitch list which shouldn't happen." );
227+
227228 NSMutableArray * allFragFunctions = [[NSMutableArray alloc ] init ];
228229 NSMutableArray * allFragUniforms = [[NSMutableArray alloc ] init ];
229230 NSMutableArray * allVertexFunctions = [[NSMutableArray alloc ] init ];
230231 NSMutableArray * allVertexUniforms = [[NSMutableArray alloc ] init ];
231232 NSMutableArray * allVaryings = [[NSMutableArray alloc ] init ];
232233
234+ // Even if we're only handed one effect in this stitch list, we have to run it through the
235+ // name mangling code below because all effects in a stack share one uniform namespace.
233236 int effectIndex = startIndex;
234- for (CCEffect* effect in effects )
237+ for (CCEffect* effect in stitchList )
235238 {
236239 // Construct the prefix to use for name mangling.
237240 NSString *effectPrefix = [NSString stringWithFormat: @" %@ _%d _" , effect.debugName, effectIndex];
@@ -291,34 +294,53 @@ -(CCEffect *)stitchEffects:(NSArray*)effects startIndex:(int)startIndex
291294 // and last effects in the stitch list. If the "stitch before" flag is set on the
292295 // first effect then set it in the resulting effect. If the "stitch after" flag is
293296 // set in the last effect then set it in the resulting effect.
294- CCEffect *firstEffect = [effects firstObject ];
295- CCEffect *lastEffect = [effects lastObject ];
297+ CCEffect *firstEffect = [stitchList firstObject ];
298+ CCEffect *lastEffect = [stitchList lastObject ];
296299 stitchedEffect.stitchFlags = (firstEffect.stitchFlags & CCEffectFunctionStitchBefore) | (lastEffect.stitchFlags & CCEffectFunctionStitchAfter);
297300
298- // Create a new render pass object and set its shader from the stitched effect
299- // that was created above.
300- CCEffectRenderPass *newPass = [[CCEffectRenderPass alloc ] init ];
301- newPass.debugLabel = @" CCEffectStack_Stitched pass 0" ;
302- newPass.shader = stitchedEffect.shader ;
303-
304- NSMutableArray *beginBlocks = [[NSMutableArray alloc ] init ];
305- NSMutableArray *endBlocks = [[NSMutableArray alloc ] init ];
306-
307- for (CCEffect *effect in effects)
301+ if (stitchList.count == 1 )
308302 {
309- // Copy the begin and end blocks from the input passes into the new pass.
310- for (CCEffectRenderPass *pass in effect.renderPasses )
303+ // If there was only one effect in the stitch list copy its render
304+ // passes into the output stitched effect. Update the copied passes
305+ // so they point to the new shader in the stitched effect.
306+
307+ NSMutableArray *renderPasses = [[NSMutableArray alloc ] init ];
308+ for (CCEffectRenderPass *pass in firstEffect.renderPasses )
311309 {
312- [beginBlocks addObjectsFromArray: pass.beginBlocks];
313- [endBlocks addObjectsFromArray: pass.endBlocks];
310+ CCEffectRenderPass *newPass = [pass copy ];
311+ newPass.shader = stitchedEffect.shader ;
312+ [renderPasses addObject: newPass];
314313 }
314+ stitchedEffect.renderPasses = renderPasses;
315315 }
316-
317- newPass.beginBlocks = beginBlocks;
318- newPass.endBlocks = endBlocks;
319-
320- stitchedEffect.renderPasses = @[newPass];
321-
316+ else
317+ {
318+ // If there were multiple effects in the stitch list, create a new render
319+ // pass object, set its shader to the shader from the stitched effect, and
320+ // copy all blocks from the input passes.
321+ CCEffectRenderPass *newPass = [[CCEffectRenderPass alloc ] init ];
322+ newPass.debugLabel = @" CCEffectStack_Stitched pass 0" ;
323+ newPass.shader = stitchedEffect.shader ;
324+
325+ NSMutableArray *beginBlocks = [[NSMutableArray alloc ] init ];
326+ NSMutableArray *endBlocks = [[NSMutableArray alloc ] init ];
327+
328+ for (CCEffect *effect in stitchList)
329+ {
330+ // Copy the begin and end blocks from the input passes into the new pass.
331+ for (CCEffectRenderPass *pass in effect.renderPasses )
332+ {
333+ [beginBlocks addObjectsFromArray: pass.beginBlocks];
334+ [endBlocks addObjectsFromArray: pass.endBlocks];
335+ }
336+ }
337+
338+ newPass.beginBlocks = beginBlocks;
339+ newPass.endBlocks = endBlocks;
340+
341+ stitchedEffect.renderPasses = @[newPass];
342+ }
343+
322344 return stitchedEffect;
323345}
324346
@@ -328,7 +350,7 @@ + (NSDictionary *)varyingsByApplyingPrefix:(NSString *)prefix toVaryings:(NSArra
328350 for (CCEffectVarying *varying in varyings)
329351 {
330352 NSString *prefixedName = [NSString stringWithFormat: @" %@%@ " , prefix, varying.name];
331- CCEffectVarying *prefixedVarying = [[CCEffectVarying alloc ] initWithType: varying.type name: prefixedName];
353+ CCEffectVarying *prefixedVarying = [[CCEffectVarying alloc ] initWithType: varying.type name: prefixedName count: varying.count ];
332354 [varyingReplacements setObject: prefixedVarying forKey: varying.name];
333355 }
334356 return [varyingReplacements copy ];
0 commit comments