@@ -235,39 +235,6 @@ GrGLSLFragmentProcessor* GrTextureEffect::onCreateGLSLInstance() const {
235235
236236 public:
237237 void emitCode (EmitArgs& args) override {
238- auto appendWrap = [](GrGLSLShaderBuilder* builder, ShaderMode mode, const char * inCoord,
239- const char * domainStart, const char * domainEnd, bool is2D,
240- const char * out) {
241- switch (mode) {
242- case ShaderMode::kNone :
243- builder->codeAppendf (" %s = %s;\n " , out, inCoord);
244- break ;
245- case ShaderMode::kDecal :
246- // The lookup coordinate to use for decal will be clamped just like
247- // kClamp_Mode, it's just that the post-processing will be different, so
248- // fall through
249- case ShaderMode::kClamp :
250- builder->codeAppendf (" %s = clamp(%s, %s, %s);" , out, inCoord, domainStart,
251- domainEnd);
252- break ;
253- case ShaderMode::kRepeat :
254- builder->codeAppendf (" %s = mod(%s - %s, %s - %s) + %s;" , out, inCoord,
255- domainStart, domainEnd, domainStart, domainStart);
256- break ;
257- case ShaderMode::kMirrorRepeat : {
258- const char * type = is2D ? " float2" : " float" ;
259- builder->codeAppend (" {" );
260- builder->codeAppendf (" %s w = %s - %s;" , type, domainEnd, domainStart);
261- builder->codeAppendf (" %s w2 = 2 * w;" , type);
262- builder->codeAppendf (" %s m = mod(%s - %s, w2);" , type, inCoord,
263- domainStart);
264- builder->codeAppendf (" %s = mix(m, w2 - m, step(w, m)) + %s;" , out,
265- domainStart);
266- builder->codeAppend (" }" );
267- break ;
268- }
269- }
270- };
271238 auto te = args.fFp .cast <GrTextureEffect>();
272239 const char * coords;
273240 if (args.fFp .coordTransformsApplyToLocalCoords ()) {
@@ -290,33 +257,53 @@ GrGLSLFragmentProcessor* GrTextureEffect::onCreateGLSLInstance() const {
290257
291258 // Always use a local variable for the input coordinates; often callers pass in an
292259 // expression and we want to cache it across all of its references in the code below
293- auto inCoords = fb->ensureCoords2D (args.fTransformedCoords [0 ].fVaryingPoint );
260+ fb->codeAppendf (
261+ " float2 inCoord = %s;" ,
262+ fb->ensureCoords2D (args.fTransformedCoords [0 ].fVaryingPoint ).c_str ());
294263 fb->codeAppend (" float2 clampedCoord;" );
295- SkString start;
296- SkString end;
297- if (te.fShaderModes [0 ] == te.fShaderModes [1 ]) {
298- // Doing the domain setup using vectors seems to avoid shader compilation issues
299- // on Chromecast, possibly due to reducing shader length.
300- start.printf (" %s.xy" , subsetName);
301- end.printf (" %s.zw" , subsetName);
302- appendWrap (fb, te.fShaderModes [0 ], inCoords.c_str (), start.c_str (), end.c_str (),
303- true , " clampedCoord" );
304- } else {
305- SkString origX, origY;
306- // Apply x mode to the x coordinate using the left and right edges of the domain
307- // rect (stored as the x and z components of the domain uniform).
308- start.printf (" %s.x" , subsetName);
309- end.printf (" %s.z" , subsetName);
310- origX.printf (" %s.x" , inCoords.c_str ());
311- appendWrap (fb, te.fShaderModes [0 ], origX.c_str (), start.c_str (), end.c_str (),
312- false , " clampedCoord.x" );
313- // Repeat the same logic for y.
314- start.printf (" %s.y" , subsetName);
315- end.printf (" %s.w" , subsetName);
316- origY.printf (" %s.y" , inCoords.c_str ());
317- appendWrap (fb, te.fShaderModes [1 ], origY.c_str (), start.c_str (), end.c_str (),
318- false , " clampedCoord.y" );
319- }
264+
265+ auto subsetCoord = [fb, subsetName](ShaderMode mode, const char * coordSwizzle,
266+ const char * subsetStartSwizzle,
267+ const char * subsetStopSwizzle) {
268+ switch (mode) {
269+ case ShaderMode::kNone :
270+ fb->codeAppendf (" clampedCoord.%s = inCoord.%s;\n " , coordSwizzle,
271+ coordSwizzle);
272+ break ;
273+ case ShaderMode::kDecal :
274+ // The lookup coordinate to use for decal will be clamped just like
275+ // kClamp_Mode, it's just that the post-processing will be different, so
276+ // fall through
277+ case ShaderMode::kClamp :
278+ fb->codeAppendf (" clampedCoord.%s = clamp(inCoord.%s, %s.%s, %s.%s);" ,
279+ coordSwizzle, coordSwizzle, subsetName,
280+ subsetStartSwizzle, subsetName, subsetStopSwizzle);
281+ break ;
282+ case ShaderMode::kRepeat :
283+ fb->codeAppendf (
284+ " clampedCoord.%s = mod(inCoord.%s - %s.%s, %s.%s - %s.%s) + "
285+ " %s.%s;" ,
286+ coordSwizzle, coordSwizzle, subsetName, subsetStartSwizzle,
287+ subsetName, subsetStopSwizzle, subsetName, subsetStartSwizzle,
288+ subsetName, subsetStartSwizzle);
289+ break ;
290+ case ShaderMode::kMirrorRepeat : {
291+ fb->codeAppend (" {" );
292+ fb->codeAppendf (" float w = %s.%s - %s.%s;" , subsetName,
293+ subsetStopSwizzle, subsetName, subsetStartSwizzle);
294+ fb->codeAppendf (" float w2 = 2 * w;" );
295+ fb->codeAppendf (" float m = mod(inCoord.%s - %s.%s, w2);" , coordSwizzle,
296+ subsetName, subsetStartSwizzle);
297+ fb->codeAppendf (" clampedCoord.%s = mix(m, w2 - m, step(w, m)) + %s.%s;" ,
298+ coordSwizzle, subsetName, subsetStartSwizzle);
299+ fb->codeAppend (" }" );
300+ break ;
301+ }
302+ }
303+ };
304+
305+ subsetCoord (te.fShaderModes [0 ], " x" , " x" , " z" );
306+ subsetCoord (te.fShaderModes [1 ], " y" , " y" , " w" );
320307 SkString textureLookup;
321308 fb->appendTextureLookup (&textureLookup, args.fTexSamplers [0 ], " clampedCoord" );
322309 fb->codeAppendf (" half4 textureColor = %s;" , textureLookup.c_str ());
@@ -336,16 +323,16 @@ GrGLSLFragmentProcessor* GrTextureEffect::onCreateGLSLInstance() const {
336323 // behavior depending on if it's 0 or 1.
337324 if (decalX && decalY) {
338325 fb->codeAppendf (
339- " half err = max(half(abs(clampedCoord.x - %s .x) * %s.x), "
340- " half(abs(clampedCoord.y - %s .y) * %s.y));" ,
341- inCoords. c_str (), decalName, inCoords. c_str () , decalName);
326+ " half err = max(half(abs(clampedCoord.x - inCoord .x) * %s.x), "
327+ " half(abs(clampedCoord.y - inCoord .y) * %s.y));" ,
328+ decalName, decalName);
342329 } else if (decalX) {
343- fb->codeAppendf (" half err = half(abs(clampedCoord.x - %s .x) * %s.x);" ,
344- inCoords. c_str (), decalName);
330+ fb->codeAppendf (" half err = half(abs(clampedCoord.x - inCoord .x) * %s.x);" ,
331+ decalName);
345332 } else {
346333 SkASSERT (decalY);
347- fb->codeAppendf (" half err = half(abs(clampedCoord.y - %s .y) * %s.y);" ,
348- inCoords. c_str (), decalName);
334+ fb->codeAppendf (" half err = half(abs(clampedCoord.y - inCoord .y) * %s.y);" ,
335+ decalName);
349336 }
350337
351338 // Apply a transform to the error rate, which let's us simulate nearest or
0 commit comments