Skip to content

Commit 560ac9c

Browse files
author
Thayer J Andrews
committed
CCEffects - Adapt the default input snippets to effect padding
When effects that do not require padding are stacked with effects that do, their input snippets need to handle the possibility that they will fetch out of bounds texels.
1 parent abdb92c commit 560ac9c

9 files changed

+37
-10
lines changed

cocos2d/CCEffect.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
NSString * const CCShaderUniformTexCoord2Center = @"cc_FragTexCoord2Center";
1717
NSString * const CCShaderUniformTexCoord2Extents = @"cc_FragTexCoord2Extents";
1818

19+
NSString * const CCEffectDefaultInitialInputSnippet = @"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1); vec2 compare = cc_FragTexCoord1Extents - abs(cc_FragTexCoord1 - cc_FragTexCoord1Center); tmp *= step(0.0, min(compare.x, compare.y))";
20+
NSString * const CCEffectDefaultInputSnippet = @"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1); vec2 compare = cc_FragTexCoord1Extents - abs(cc_FragTexCoord1 - cc_FragTexCoord1Center); tmp *= step(0.0, min(compare.x, compare.y))";
21+
22+
1923

2024
static NSString* fragBase =
2125
@"%@\n\n" // uniforms

cocos2d/CCEffectBrightness.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ -(void)buildFragmentFunctions
5050
{
5151
self.fragmentFunctions = [[NSMutableArray alloc] init];
5252

53-
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:@"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)" snippet:@"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)"];
53+
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:CCEffectDefaultInitialInputSnippet snippet:CCEffectDefaultInputSnippet];
5454

5555
NSString* effectBody = CC_GLSL(
5656
return vec4((inputValue.rgb + vec3(u_brightness * inputValue.a)), inputValue.a);
@@ -71,6 +71,9 @@ -(void)buildRenderPasses
7171

7272
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
7373
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
74+
pass.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:pass.texCoord1Center];
75+
pass.shaderUniforms[CCShaderUniformTexCoord1Extents] = [NSValue valueWithGLKVector2:pass.texCoord1Extents];
76+
7477
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_brightness"]] = weakSelf.conditionedBrightness;
7578
} copy]];
7679

cocos2d/CCEffectContrast.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ -(void)buildFragmentFunctions
5151
{
5252
self.fragmentFunctions = [[NSMutableArray alloc] init];
5353

54-
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:@"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)" snippet:@"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)"];
54+
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:CCEffectDefaultInitialInputSnippet snippet:CCEffectDefaultInputSnippet];
5555

5656
NSString* effectBody = CC_GLSL(
5757
return vec4(((inputValue.rgb - vec3(0.5)) * vec3(u_contrast) + vec3(0.5)), inputValue.a);
@@ -72,6 +72,9 @@ -(void)buildRenderPasses
7272

7373
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
7474
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
75+
pass.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:pass.texCoord1Center];
76+
pass.shaderUniforms[CCShaderUniformTexCoord1Extents] = [NSValue valueWithGLKVector2:pass.texCoord1Extents];
77+
7578
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_contrast"]] = weakSelf.conditionedContrast;
7679
} copy]];
7780

cocos2d/CCEffectGlass.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ -(void)buildFragmentFunctions
110110
{
111111
self.fragmentFunctions = [[NSMutableArray alloc] init];
112112

113-
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:@"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)" snippet:@"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)"];
113+
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:CCEffectDefaultInitialInputSnippet snippet:CCEffectDefaultInputSnippet];
114114

115115
NSString* effectBody = CC_GLSL(
116116
// Index the normal map and expand the color value from [0..1] to [-1..1]
@@ -205,6 +205,9 @@ -(void)buildRenderPasses
205205

206206
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
207207
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
208+
pass.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:pass.texCoord1Center];
209+
pass.shaderUniforms[CCShaderUniformTexCoord1Extents] = [NSValue valueWithGLKVector2:pass.texCoord1Extents];
210+
208211
if (weakSelf.normalMap)
209212
{
210213
pass.shaderUniforms[CCShaderUniformNormalMapTexture] = weakSelf.normalMap.texture;

cocos2d/CCEffectHue.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ -(void)buildFragmentFunctions
5252
{
5353
self.fragmentFunctions = [[NSMutableArray alloc] init];
5454

55-
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:@"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)" snippet:@"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)"];
55+
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:CCEffectDefaultInitialInputSnippet snippet:CCEffectDefaultInputSnippet];
5656

5757
// The non-color matrix shader is based on the hue filter in GPUImage - https://github.com/BradLarson/GPUImage
5858
NSString* effectBody = CC_GLSL(
@@ -74,6 +74,9 @@ -(void)buildRenderPasses
7474

7575
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
7676
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
77+
pass.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:pass.texCoord1Center];
78+
pass.shaderUniforms[CCShaderUniformTexCoord1Extents] = [NSValue valueWithGLKVector2:pass.texCoord1Extents];
79+
7780
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_hueRotationMtx"]] = weakSelf.hueRotationMtx;
7881
} copy]];
7982

cocos2d/CCEffectReflection.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ -(void)buildFragmentFunctions
111111
{
112112
self.fragmentFunctions = [[NSMutableArray alloc] init];
113113

114-
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:@"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)" snippet:@"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)"];
115-
114+
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:CCEffectDefaultInitialInputSnippet snippet:CCEffectDefaultInputSnippet];
115+
116116
NSString* effectBody = CC_GLSL(
117117
// Index the normal map and expand the color value from [0..1] to [-1..1]
118118
vec4 normalMap = texture2D(cc_NormalMapTexture, cc_FragTexCoord2);
@@ -176,6 +176,9 @@ -(void)buildRenderPasses
176176

177177
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
178178
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
179+
pass.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:pass.texCoord1Center];
180+
pass.shaderUniforms[CCShaderUniformTexCoord1Extents] = [NSValue valueWithGLKVector2:pass.texCoord1Extents];
181+
179182
if (weakSelf.normalMap)
180183
{
181184
pass.shaderUniforms[CCShaderUniformNormalMapTexture] = weakSelf.normalMap.texture;

cocos2d/CCEffectRefraction.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ -(void)buildFragmentFunctions
8080
{
8181
self.fragmentFunctions = [[NSMutableArray alloc] init];
8282

83-
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:@"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)" snippet:@"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)"];
84-
83+
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:CCEffectDefaultInitialInputSnippet snippet:CCEffectDefaultInputSnippet];
84+
8585
NSString* effectBody = CC_GLSL(
8686
// Index the normal map and expand the color value from [0..1] to [-1..1]
8787
vec4 normalMap = texture2D(cc_NormalMapTexture, cc_FragTexCoord2);
@@ -142,6 +142,9 @@ -(void)buildRenderPasses
142142

143143
pass.shaderUniforms[CCShaderUniformMainTexture] = previousPassTexture;
144144
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
145+
pass.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:pass.texCoord1Center];
146+
pass.shaderUniforms[CCShaderUniformTexCoord1Extents] = [NSValue valueWithGLKVector2:pass.texCoord1Extents];
147+
145148
if (weakSelf.normalMap)
146149
{
147150
pass.shaderUniforms[CCShaderUniformNormalMapTexture] = weakSelf.normalMap.texture;

cocos2d/CCEffectSaturation.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ -(void)buildFragmentFunctions
8686
{
8787
self.fragmentFunctions = [[NSMutableArray alloc] init];
8888

89-
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:@"cc_FragColor * texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)" snippet:@"texture2D(cc_PreviousPassTexture, cc_FragTexCoord1)"];
90-
89+
CCEffectFunctionInput *input = [[CCEffectFunctionInput alloc] initWithType:@"vec4" name:@"inputValue" initialSnippet:CCEffectDefaultInitialInputSnippet snippet:CCEffectDefaultInputSnippet];
90+
9191
// Image saturation shader based on saturation filter in GPUImage - https://github.com/BradLarson/GPUImage
9292
NSString* effectBody = CC_GLSL(
9393
const vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);
@@ -113,6 +113,9 @@ -(void)buildRenderPasses
113113
pass0.beginBlocks = @[[^(CCEffectRenderPass *pass, CCTexture *previousPassTexture){
114114

115115
pass.shaderUniforms[CCShaderUniformPreviousPassTexture] = previousPassTexture;
116+
pass.shaderUniforms[CCShaderUniformTexCoord1Center] = [NSValue valueWithGLKVector2:pass.texCoord1Center];
117+
pass.shaderUniforms[CCShaderUniformTexCoord1Extents] = [NSValue valueWithGLKVector2:pass.texCoord1Extents];
118+
116119
pass.shaderUniforms[weakSelf.uniformTranslationTable[@"u_saturation"]] = weakSelf.conditionedSaturation;
117120
} copy]];
118121

cocos2d/CCEffect_Private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ extern NSString * const CCShaderUniformTexCoord1Center;
1919
extern NSString * const CCShaderUniformTexCoord1Extents;
2020
extern NSString * const CCShaderUniformTexCoord2Center;
2121
extern NSString * const CCShaderUniformTexCoord2Extents;
22+
extern NSString * const CCEffectDefaultInitialInputSnippet;
23+
extern NSString * const CCEffectDefaultInputSnippet;
2224

2325

2426
typedef NS_ENUM(NSUInteger, CCEffectFunctionStitchFlags)

0 commit comments

Comments
 (0)