//
    // We will go with 32768 as the maximum number of wraps that's support in 
    // supported since that's what refrast has.
    //
    #define MAX_RELIABLE_WRAP_VALUE 32768

    float u;
    
    float2 sampleToFirstTexelRegionCenter = samplePos - GradInfoParams.ptFirstTexelRegionCenter;
    float firstTexelRegionRadiusSquared = GradInfoParams.flHalfTexelSizeNormalized * GradInfoParams.flHalfTexelSizeNormalized;
    
    if (dot(sampleToFirstTexelRegionCenter, sampleToFirstTexelRegionCenter) <
        firstTexelRegionRadiusSquared)
    {
        u = GradInfoParams.flHalfTexelSizeNormalized;
    }
    else
    {
        // Get distance (in unit circle) from sample point to the gradient origin:
        float2 sampleToOrigin = samplePos - GradInfoParams.ptGradOrigin;
    
        float A = dot(sampleToOrigin, sampleToOrigin);
        
        float B = 2.0f * dot(GradInfoParams.ptGradOrigin, sampleToOrigin);

        float2 ptGradOriginPerp = {GradInfoParams.ptGradOrigin.y, -GradInfoParams.ptGradOrigin.x};
        float sampleToOriginCrossOriginNorm = dot(sampleToOrigin, ptGradOriginPerp);

        // see brushspan.cpp for an explanation of why the determinant is calculated this way.
        float determinant = 
            4.0f * (  GradInfoParams.gradientSpanNormalized * GradInfoParams.gradientSpanNormalized * A
                    - sampleToOriginCrossOriginNorm * sampleToOriginCrossOriginNorm);
        
        if (0.0f > determinant)
        {
            // This complex region appears when the gradient origin is outside the
            // ellipse defining the end of the gradient. When rendering this region
            // we choose the last texel color.
            u = 1.0f - GradInfoParams.flHalfTexelSizeNormalized;
        }
        else
        {
            u = (2 * A * GradInfoParams.gradientSpanNormalized) / (sqrt(determinant) - B);
            
            if (u < GradInfoParams.flHalfTexelSizeNormalized)
            {
                if (u < 0.0)
                {
                    // This negative region appears when the gradient origin is outside the
                    // ellipse defining the end of the gradient. When rendering this region
                    // we choose the last texel color.
                    u = 1.0f - GradInfoParams.flHalfTexelSizeNormalized;
                }
                else                                                         
                {
                    // Ensure that the gradient space does not wrap around,
                    // interpolating with the last stop at the center point.
                    // This value for u picks the first texel in the texture.
                    
                    // Given an infinite precicision machine, we'd never get to this case since
                    // we should have skipped the quadratic equation up top. Nevertheless,
                    // we do not have an infinite precision machine, so we may still get here.
                    u = GradInfoParams.flHalfTexelSizeNormalized;
                }
            }   
            else
            {
                //
                // Refrast & probably hw implement wrapping/clamping logic by first casting
                // the float to an integer and then doing integer math.  They are not robust
                // against integer overflow, so we need to do the check manually.
                //

                if (u > MAX_RELIABLE_WRAP_VALUE)
                {
                    u = 1.0f;
                }
            }
        }
    }    

    color *= tex1D(TextureSampler, u);
}
