Skip to content

Commit d231319

Browse files
author
Thayer J Andrews
committed
Fix CCEffectGlow
Instead of the renderer directly setting the render pass uniforms, give the previous pass's render target to the current pass's begin block and let the pass handle it how it likes.
1 parent 9f0bf14 commit d231319

9 files changed

+39
-31
lines changed

cocos2d/CCEffect.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ extern const NSString *CCShaderUniformPreviousPassTexture;
5151

5252
@end
5353

54-
typedef void (^CCEffectRenderPassBlock)();
54+
typedef void (^CCEffectRenderPassBeginBlock)(CCTexture *previousPassTexture);
55+
typedef void (^CCEffectRenderPassUpdateBlock)();
56+
typedef void (^CCEffectRenderPassEndBlock)();
5557

5658
// Note to self: I don't like this pattern, refactor it. I think there should be a CCRenderPass that is used by CCEffect instead. NOTE: convert this to a CCRnderPassProtocol
5759
@interface CCEffectRenderPass : NSObject
@@ -64,9 +66,9 @@ typedef void (^CCEffectRenderPassBlock)();
6466
@property (nonatomic) CCShader* shader;
6567
@property (nonatomic) NSMutableDictionary* shaderUniforms;
6668
@property (nonatomic) BOOL needsClear;
67-
@property (nonatomic,copy) CCEffectRenderPassBlock beginBlock;
68-
@property (nonatomic,copy) CCEffectRenderPassBlock updateBlock;
69-
@property (nonatomic,copy) CCEffectRenderPassBlock endBlock;
69+
@property (nonatomic,copy) CCEffectRenderPassBeginBlock beginBlock;
70+
@property (nonatomic,copy) CCEffectRenderPassUpdateBlock updateBlock;
71+
@property (nonatomic,copy) CCEffectRenderPassEndBlock endBlock;
7072

7173
-(void)enqueueTriangles;
7274

cocos2d/CCEffect.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ -(id)init
139139
{
140140
__weak CCEffectRenderPass *weakSelf = self;
141141

142-
CCEffectRenderPassBlock emptyBlock = ^{};
143-
_beginBlock = [emptyBlock copy];
144-
_endBlock = [emptyBlock copy];
142+
_beginBlock = [^(CCTexture *previousPassTexture){} copy];
143+
_endBlock = [^{} copy];
145144

146-
CCEffectRenderPassBlock updateBlock = ^{
145+
CCEffectRenderPassUpdateBlock updateBlock = ^{
147146
if (weakSelf.needsClear)
148147
{
149148
[weakSelf.renderer enqueueClear:GL_COLOR_BUFFER_BIT color:[CCColor clearColor].glkVector4 depth:0.0f stencil:0 globalSortOrder:NSIntegerMin];

cocos2d/CCEffectBrightness.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ -(void)buildRenderPasses
6262
weakPass = pass0;
6363
pass0.shader = self.shader;
6464
pass0.shaderUniforms = self.shaderUniforms;
65-
pass0.beginBlock = ^{
65+
pass0.beginBlock = ^(CCTexture *previousPassTexture){
66+
weakPass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
67+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
6668
weakPass.shaderUniforms[@"u_brightness"] = [NSNumber numberWithFloat:weakSelf.brightness];
6769
};
6870

cocos2d/CCEffectContrast.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ -(void)buildRenderPasses
6262
weakPass = pass0;
6363
pass0.shader = self.shader;
6464
pass0.shaderUniforms = self.shaderUniforms;
65-
pass0.beginBlock = ^{
65+
pass0.beginBlock = ^(CCTexture *previousPassTexture){
66+
weakPass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
67+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
6668
weakPass.shaderUniforms[@"u_contrast"] = [NSNumber numberWithFloat:weakSelf.contrast];
6769
};
6870

cocos2d/CCEffectGaussianBlur.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ -(void)buildRenderPasses
131131
pass0.shader = self.shader;
132132
pass0.shaderUniforms = self.shaderUniforms;
133133
pass0.blendMode = [CCBlendMode premultipliedAlphaMode];
134-
pass0.beginBlock = ^{
134+
pass0.beginBlock = ^(CCTexture *previousPassTexture){
135+
weakPass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
136+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
135137
if([self radialBlur])
136138
{
137139
weakPass.shaderUniforms[@"u_blurDirection"] = [NSValue valueWithGLKVector2:GLKVector2Make(weakSelf.blurStrength, 0.0f)];
@@ -149,7 +151,8 @@ -(void)buildRenderPasses
149151
pass1.shader = self.shader;
150152
pass1.shaderUniforms = self.shaderUniforms;
151153
pass1.blendMode = [CCBlendMode premultipliedAlphaMode];
152-
pass1.beginBlock = ^{
154+
pass1.beginBlock = ^(CCTexture *previousPassTexture){
155+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
153156
weakPass.shaderUniforms[@"u_blurDirection"] = [NSValue valueWithGLKVector2:GLKVector2Make(0.0f, weakSelf.blurStrength)];
154157
};
155158

cocos2d/CCEffectGlow.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ -(void)buildRenderPasses
152152
weakPass = pass0;
153153
pass0.shader = self.shader;
154154
pass0.shaderUniforms = self.shaderUniforms;
155-
pass0.beginBlock = ^{
155+
pass0.beginBlock = ^(CCTexture *previousPassTexture){
156+
weakPass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
157+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
156158
weakPass.shaderUniforms[@"u_enableGlowMap"] = [NSNumber numberWithFloat:0.0f];
157159
weakPass.shaderUniforms[@"u_blurDirection"] = [NSValue valueWithGLKVector2:GLKVector2Make(weakSelf.blurStrength, 0.0f)];
158160
};
@@ -162,7 +164,8 @@ -(void)buildRenderPasses
162164
weakPass = pass1;
163165
pass1.shader = self.shader;
164166
pass1.shaderUniforms = self.shaderUniforms;
165-
pass1.beginBlock = ^{
167+
pass1.beginBlock = ^(CCTexture *previousPassTexture){
168+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
166169
weakPass.shaderUniforms[@"u_enableGlowMap"] = [NSNumber numberWithFloat:0.0f];
167170
weakPass.shaderUniforms[@"u_blurDirection"] = [NSValue valueWithGLKVector2:GLKVector2Make(0.0f, weakSelf.blurStrength)];
168171
};
@@ -172,7 +175,8 @@ -(void)buildRenderPasses
172175
weakPass = pass2;
173176
pass2.shader = self.shader;
174177
pass2.shaderUniforms = self.shaderUniforms;
175-
pass2.beginBlock = ^{
178+
pass2.beginBlock = ^(CCTexture *previousPassTexture){
179+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
176180
weakPass.shaderUniforms[@"u_enableGlowMap"] = [NSNumber numberWithFloat:1.0f];
177181
};
178182

cocos2d/CCEffectPixellate.m

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@ -(void)buildRenderPasses
100100
pass0.shader = self.shader;
101101
pass0.shaderUniforms = self.shaderUniforms;
102102
pass0.blendMode = [CCBlendMode premultipliedAlphaMode];
103-
pass0.beginBlock = ^{
104-
CCTexture *texture = weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture];
103+
pass0.beginBlock = ^(CCTexture *previousPassTexture){
105104

106-
float aspect = texture.contentSize.width / texture.contentSize.height;
107-
float uStep = self.blockSize / texture.contentSize.width;
105+
weakPass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
106+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
107+
108+
float aspect = previousPassTexture.contentSize.width / previousPassTexture.contentSize.height;
109+
float uStep = self.blockSize / previousPassTexture.contentSize.width;
108110
float vStep = uStep * aspect;
109111

110112
weakPass.shaderUniforms[@"u_uStep"] = [NSNumber numberWithFloat:uStep];

cocos2d/CCEffectRenderer.m

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,21 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect renderer:(CCR
158158

159159
GLKMatrix4 projection = GLKMatrix4MakeOrtho(0.0f, _contentSize.width, _contentSize.height, 0.0f, -1024.0f, 1024.0f);
160160

161-
CCTexture *inputTexture = sprite.texture;
162-
CCTexture *previousPassTexture = nil;
163-
CCTexture *mainTexture = nil;
164-
165161
CCEffectRenderTarget *previousPassRT = nil;
166162

167163
for(int i = 0; i < effect.renderPassesRequired; i++)
168164
{
169165
BOOL lastPass = (i == (effect.renderPassesRequired - 1));
170166
BOOL directRendering = lastPass && effect.supportsDirectRendering;
171167

168+
CCTexture *previousPassTexture = nil;
172169
if (previousPassRT)
173170
{
174-
mainTexture = previousPassRT.texture;
175171
previousPassTexture = previousPassRT.texture;
176172
}
177173
else
178174
{
179-
mainTexture = inputTexture;
180-
previousPassTexture = inputTexture;
175+
previousPassTexture = sprite.texture;
181176
}
182177

183178
CCEffectRenderPass* renderPass = [effect renderPassAtIndex:i];
@@ -186,8 +181,6 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect renderer:(CCR
186181
renderPass.verts = *(sprite.vertexes);
187182
renderPass.blendMode = [CCBlendMode premultipliedAlphaMode];
188183
renderPass.needsClear = !directRendering;
189-
renderPass.shaderUniforms[CCShaderUniformMainTexture] = mainTexture;
190-
renderPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
191184

192185
CCEffectRenderTarget *rt = nil;
193186

@@ -196,7 +189,7 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect renderer:(CCR
196189
{
197190
renderPass.transform = *transform;
198191

199-
renderPass.beginBlock();
192+
renderPass.beginBlock(previousPassTexture);
200193
renderPass.updateBlock();
201194
renderPass.endBlock();
202195
}
@@ -207,7 +200,7 @@ -(void)drawSprite:(CCSprite *)sprite withEffect:(CCEffect *)effect renderer:(CCR
207200
CGSize rtSize = CGSizeMake(_contentSize.width * _contentScale, _contentSize.height * _contentScale);
208201
rt = [self renderTargetWithSize:rtSize];
209202

210-
renderPass.beginBlock();
203+
renderPass.beginBlock(previousPassTexture);
211204
[self bindRenderTarget:rt withRenderer:renderer];
212205
renderPass.updateBlock();
213206
[self restoreRenderTargetWithRenderer:renderer];

cocos2d/CCEffectSaturation.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ -(void)buildRenderPasses
102102
pass0.shader = self.shader;
103103
pass0.shaderUniforms = self.shaderUniforms;
104104
pass0.blendMode = [CCBlendMode premultipliedAlphaMode];
105-
pass0.beginBlock = ^{
105+
pass0.beginBlock = ^(CCTexture *previousPassTexture){
106+
weakPass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
106107
weakPass.shaderUniforms[@"u_saturation"] = [NSNumber numberWithFloat:weakSelf.saturation];
107108
};
108109

0 commit comments

Comments
 (0)