2 Replies Latest reply: May 4, 2012 10:21 AM by John Blackburne RSS

    Shader works in toolkit, not in Flash

    John Blackburne Community Member

      The following shader (Which creates a circular gradient fill that interpolates along the angle, not the radius: the full shader code and it working is here) works in Pixel Bender Toolkit but not in Flash: in Flash it seems to go into an infinite loop

       

          void

          evaluatePixel()

          {

              // calculate where we are relative to the center

              float2 relativePos = outCoord() - center;

              // get our bearing relative to start angle, wrap it and scale to (-2, 2)

              float dir = atan(relativePos.y, relativePos.x);

              dir = dir - startAngle;

              float dir2 = dir + TWOPI;

              dir = dir < -PI ? dir2 : dir;

              dir = dir * 4.0 / TWOPI;

       

              // get colours to interpolate between and how far to interpolate

              float3 colFrom, colTo;

              if (dir < 0.0) {

                  if (dir < -1.0) {

                      dir = dir + 2.0;

                      colFrom = col1;

                      colTo = col2;

                  } else {

                      dir = dir + 1.0;

                      colFrom = col2;

                      colTo = col3;

                  }

              } else {

                  if (dir < 1.0) {

                      colFrom = col3;

                      colTo = col4;

                  } else {

                      dir = dir - 1.0;

                      colFrom = col4;

                      colTo = col1;

                  }

              }

              // linearly interpolate

              float3 interp = mix(colFrom, colTo, float3(dir, dir, dir));

              // convert to smooth interpolation through colours

              interp = colFrom + (colTo - colFrom) * smoothStep(colFrom, colTo, interp);

              dst = pixel4(interp.x, interp.y, interp.z, 1.0);

          }

      }

       

      To fix it 'dir' cannot be used both in the 'if' statements and set in the statements that depend on them. I.e. the following which replaces 'dir' with 'dir2' works. It would be better if this were consistent between the toolkit and Flash (the shader is one without an input image so won't work in AE or PS). Tested with Flash 11.2.

       

         void

          evaluatePixel()

          {

              // calculate where we are relative to the center

              float2 relativePos = outCoord() - center;

              // get our bearing relative to start angle, wrap it and scale to (-2, 2)

              float dir = atan(relativePos.y, relativePos.x);

              dir = dir - startAngle;

              float dir2 = dir + TWOPI;

              dir = dir < -PI ? dir2 : dir;

              dir = dir * 4.0 / TWOPI;

       

              // get colours to interpolate between and how far to interpolate

              float3 colFrom, colTo;

              if (dir < 0.0) {

                  if (dir < -1.0) {

                      dir2 = dir + 2.0;

                      colFrom = col1;

                      colTo = col2;

                  } else {

                      dir2 = dir + 1.0;

                      colFrom = col2;

                      colTo = col3;

                  }

              } else {

                  if (dir < 1.0) {

                      dir2 = dir;

                      colFrom = col3;

                      colTo = col4;

                  } else {

                      dir2 = dir - 1.0;

                      colFrom = col4;

                      colTo = col1;

                  }

              }

              // linearly interpolate

              float3 interp = mix(colFrom, colTo, float3(dir2, dir2, dir2));

              // convert to smooth interpolation through colours

              interp = colFrom + (colTo - colFrom) * smoothStep(colFrom, colTo, interp);

              dst = pixel4(interp.x, interp.y, interp.z, 1.0);

          }

      }

        • 1. Re: Shader works in toolkit, not in Flash
          CoreTech::Chuck Adobe Employee

          Hi John,

           

          Thanks for that information, and thank you for the workaround.  I'll add it as a bug.  It sounds like variable scoping might have a bug in the pb -> pb-byte-code conversion for Flash.

           

          I don't have a good feel for when this would be addressed, but unfortunately my best guess is not for some time.  We've largely buttoned up CS6 save for crazy show stopper issues and this is well-below that threshhold.  While Flash isn't tied to that schedule, most current Pixel Bender developement is aimed at the mainline suite apps so that's the schedule compiler fixes would be likely to come down.

           

          Best,

          Chuck.

          • 2. Re: Shader works in toolkit, not in Flash
            John Blackburne Community Member

            A further observation, discovered by accident today. It seems what the precisionHint is set to makes a difference. I've had the 'fixed' shader in since the above but must have commented out the following line

             




            gGradShader.precisionHint = ShaderPrecision.FAST;

             

            at some point trying to get it to work. But adding a similar shader today I re-enabled the line and saw the same locking-up behaviour until I commented out that line again.

             

            I've not tried the various combinations of old/new code, FAST or not precision to get to the bottom of it: the shader doesn't run often enough to worry over performance, I'd just been enabling FAST precision on the assumption that it's a sensible trade off in a game. Just making a note of it in case anyone else encounters a similar issue.

             

            John