@@ -186,7 +186,6 @@ - (CCEffectPrepareStatus)prepareForRendering
186
186
int effectIndex = 0 ;
187
187
for (NSArray *stitchList in stitchLists)
188
188
{
189
- NSAssert (stitchList.count > 0 , @" Encountered an empty stitch list which shouldn't happen." );
190
189
[stitchedEffects addObject: [CCEffectStack stitchEffects: stitchList startIndex: effectIndex]];
191
190
effectIndex += stitchList.count ;
192
191
}
@@ -222,16 +221,20 @@ - (BOOL)readyForRendering
222
221
223
222
#pragma mark - Internal
224
223
225
- + (CCEffect *)stitchEffects : (NSArray *)effects startIndex : (int )startIndex
224
+ + (CCEffect *)stitchEffects : (NSArray *)stitchList startIndex : (int )startIndex
226
225
{
226
+ NSAssert (stitchList.count > 0 , @" Encountered an empty stitch list which shouldn't happen." );
227
+
227
228
NSMutableArray * allFragFunctions = [[NSMutableArray alloc ] init ];
228
229
NSMutableArray * allFragUniforms = [[NSMutableArray alloc ] init ];
229
230
NSMutableArray * allVertexFunctions = [[NSMutableArray alloc ] init ];
230
231
NSMutableArray * allVertexUniforms = [[NSMutableArray alloc ] init ];
231
232
NSMutableArray * allVaryings = [[NSMutableArray alloc ] init ];
232
233
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.
233
236
int effectIndex = startIndex;
234
- for (CCEffect* effect in effects )
237
+ for (CCEffect* effect in stitchList )
235
238
{
236
239
// Construct the prefix to use for name mangling.
237
240
NSString *effectPrefix = [NSString stringWithFormat: @" %@ _%d _" , effect.debugName, effectIndex];
@@ -291,34 +294,53 @@ + (CCEffect *)stitchEffects:(NSArray*)effects startIndex:(int)startIndex
291
294
// and last effects in the stitch list. If the "stitch before" flag is set on the
292
295
// first effect then set it in the resulting effect. If the "stitch after" flag is
293
296
// 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 ];
296
299
stitchedEffect.stitchFlags = (firstEffect.stitchFlags & CCEffectFunctionStitchBefore) | (lastEffect.stitchFlags & CCEffectFunctionStitchAfter);
297
300
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 )
308
302
{
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 )
311
309
{
312
- [beginBlocks addObjectsFromArray: pass.beginBlocks];
313
- [endBlocks addObjectsFromArray: pass.endBlocks];
310
+ CCEffectRenderPass *newPass = [pass copy ];
311
+ newPass.shader = stitchedEffect.shader ;
312
+ [renderPasses addObject: newPass];
314
313
}
314
+ stitchedEffect.renderPasses = renderPasses;
315
315
}
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
+
322
344
return stitchedEffect;
323
345
}
324
346
@@ -328,7 +350,7 @@ + (NSDictionary *)varyingsByApplyingPrefix:(NSString *)prefix toVaryings:(NSArra
328
350
for (CCEffectVarying *varying in varyings)
329
351
{
330
352
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 ];
332
354
[varyingReplacements setObject: prefixedVarying forKey: varying.name];
333
355
}
334
356
return [varyingReplacements copy ];
0 commit comments