Skip to content

Commit 23ca2f8

Browse files
author
Thayer J Andrews
committed
CCEffectStack - Fix shader uniform management
This wasn't causing any problems yet but shader uniforms for effect stacks were not being handled properly under all circumstances. After name mangling, the old code would construct a uniform dictionary that was never used at render time. By render time, the effects would just receive the default uniform dictionary from CCNode.
1 parent f06c14e commit 23ca2f8

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

cocos2d/CCEffectStack.m

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,21 @@ - (CCEffectPrepareStatus)prepareForRendering
179179
}
180180
}
181181

182-
// Extract passes from the stacked effects and build a flat list of
183-
// passes.
182+
// Extract passes and uniforms from the stacked and stitched effects and build a flat list of
183+
// both.
184184
NSMutableArray *passes = [[NSMutableArray alloc] init];
185+
NSMutableDictionary *uniforms = [[NSMutableDictionary alloc] init];
185186
for (CCEffect *effect in stitchedEffects)
186187
{
187188
for (CCEffectRenderPass *pass in effect.renderPasses)
188189
{
189190
[passes addObject:pass];
190191
}
192+
193+
[uniforms addEntriesFromDictionary:effect.shaderUniforms];
191194
}
192195
self.renderPasses = [passes copy];
196+
self.shaderUniforms = uniforms;
193197
_passesDirty = NO;
194198

195199
result = CCEffectPrepareSuccess;
@@ -268,25 +272,18 @@ -(CCEffect *)stitchEffects:(NSArray*)effects startIndex:(int)startIndex
268272
CCEffect *lastEffect = [effects lastObject];
269273
stitchedEffect.stitchFlags = (firstEffect.stitchFlags & CCEffectFunctionStitchBefore) | (lastEffect.stitchFlags & CCEffectFunctionStitchAfter);
270274

271-
// Copy the shader for this new pass from the stitched effect.
275+
// Create a new render pass object and set its shader from the stitched effect
276+
// that was created above.
272277
CCEffectRenderPass *newPass = [[CCEffectRenderPass alloc] init];
273278
newPass.debugLabel = @"CCEffectStack_Stitched pass 0";
274279
newPass.shader = stitchedEffect.shader;
275-
newPass.shaderUniforms = [[NSMutableDictionary alloc] init];
276280

277281
NSMutableArray *beginBlocks = [[NSMutableArray alloc] init];
278282
NSMutableArray *endBlocks = [[NSMutableArray alloc] init];
279283

280284
for (CCEffect *effect in effects)
281285
{
282-
// Copy the shader uniforms from each input effect to a shared
283-
// dictionary for the new pass. For example, if we stitch two effects
284-
// together, one with uniforms A and B and one with uniforms C and D,
285-
// we will get one dictionary with A, B, C, and D.
286-
//
287-
// Similarly, copy the begin and end blocks from the input passes into
288-
// the new pass.
289-
[newPass.shaderUniforms addEntriesFromDictionary:effect.shaderUniforms];
286+
// Copy the begin and end blocks from the input passes into the new pass.
290287
for (CCEffectRenderPass *pass in effect.renderPasses)
291288
{
292289
[beginBlocks addObjectsFromArray:pass.beginBlocks];

cocos2d/CCEffect_Private.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ typedef void (^CCEffectRenderPassEndBlock)(CCEffectRenderPass *pass);
117117
@interface CCEffect ()
118118

119119
@property (nonatomic, readonly) CCShader* shader; // Note: consider adding multiple shaders (one for reach renderpass, this will help break up logic and avoid branching in a potential uber shader).
120-
@property (nonatomic, readonly) NSMutableDictionary* shaderUniforms;
120+
@property (nonatomic, strong) NSMutableDictionary* shaderUniforms;
121121
@property (nonatomic, readonly) NSUInteger renderPassesRequired;
122122
@property (nonatomic, readonly) BOOL supportsDirectRendering;
123123
@property (nonatomic, readonly) BOOL readyForRendering;

0 commit comments

Comments
 (0)