Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLSL to HLSL #15

Open
andresiraola opened this issue Jan 8, 2016 · 4 comments
Open

GLSL to HLSL #15

andresiraola opened this issue Jan 8, 2016 · 4 comments

Comments

@andresiraola
Copy link

Have you ever tried to port it to DirectX + HLSL?

I integrated it to my DX/OGL rendering engine. OGL+GLSL works just fine but DX+HLSL renders some glyphs (not all) with some artifacts.

@behdad
Copy link
Owner

behdad commented Jan 21, 2016

Hi,

Apologies for the late response. I haven't tried DirectX + HLSL. How did you convert to that, manual translation, Angle, something else?

In my experience, artifacts typically come from low-precision floats.

@andresiraola
Copy link
Author

Hi,

I had to put this on hold but now I'm retrying to get this working. I did manual translation but I also tried converting it using angle: same result. I get some little artifacts.

I'm using HLSL floats which are supposed to be equivalent to GLSL highp floats (32-bit). I'm compiling it with SM5 and testing it in a GTX 670MX (Kepler)

Here is a screenshot of what I get:

image

I think that if I got this far, it means that it is not related to texture lookup, filtering or something similar so I bet this has something to do with HLSL not doing his math homework exactly as GLSL.

Do you remember having this kind of artifacts? What do you think it could be?

If you have some time and you are interested, I can send you the shader so you give it a quick look.

@behdad
Copy link
Owner

behdad commented Jun 9, 2016

Do you remember having this kind of artifacts? What do you think it could be?

Hey. Thanks for the work. It almost always means we're losing float precision somewhere. Not sure why.

I don't have Windows, but sharing your work wouldn't hurt. Thanks.

@sichbo
Copy link

sichbo commented Jun 17, 2019

@andresiraola It's caused by a HLSL compiler bug in the glyphy_sdf loop.

If you step through the shader code in the Visual Studio graphics debugger on one of the gimpy pixels, pay close attention to the register values and you should see endpoint_prev ends up with the values that were set before entering the loop, and the line endpoint_prev = endpoint is essentially discarded.

If we rekajigger the code slightly so that it flows without the first 'continue' statement, the emitted assembly comes out okay.

Compiled with /Gec /Fh /T ps_5_0:

float glyphy_sdf(const float2 p, const int2 nominal_size GLYPHY_SDF_TEXTURE1D_EXTRA_DECLS) {

    glyphy_arc_list_t arc_list = glyphy_arc_list(p, nominal_size  GLYPHY_SDF_TEXTURE1D_EXTRA_ARGS);

    /* Short-circuits */
    if (arc_list.num_endpoints == 0) {
        /* far-away cell */
        return GLYPHY_INFINITY * float(arc_list.side);
    } if (arc_list.num_endpoints == -1) {
        /* single-line */
        float angle = arc_list.line_angle;
        float2 n = float2(cos(angle), sin(angle));
        return dot(p - (float2(nominal_size) * .5), n) - arc_list.line_distance;
    }

    float side = float(arc_list.side);
    float min_dist = GLYPHY_INFINITY;
    glyphy_arc_t closest_arc;
    closest_arc.p0 = float2(GLYPHY_INFINITY, GLYPHY_INFINITY);
    closest_arc.p1 = float2(GLYPHY_INFINITY, GLYPHY_INFINITY);
    closest_arc.d = 0;

    glyphy_arc_endpoint_t endpoint_prev, endpoint;
    endpoint_prev = glyphy_arc_endpoint_decode(GLYPHY_SDF_TEXTURE1D(arc_list.offset), nominal_size);
    for (int i = 1; i < GLYPHY_MAX_NUM_ENDPOINTS && i < arc_list.num_endpoints; i++) {

        float4 data = GLYPHY_SDF_TEXTURE1D(arc_list.offset + i);
        endpoint = glyphy_arc_endpoint_decode(data, nominal_size);
        glyphy_arc_t a;
        a.p0 = endpoint_prev.p;
        a.p1 = endpoint.p;
        a.d = endpoint.d;
     
        if (!glyphy_isinf(a.d)) {
            if (glyphy_arc_wedge_contains(a, p)) {
                float sdist = glyphy_arc_wedge_signed_dist(a, p);
                float udist = abs(sdist) * (1. - GLYPHY_EPSILON);
                if (udist <= min_dist) {
                    min_dist = udist;
                    side = sdist <= 0. ? -1. : +1.;
                }
            } else {
                float udist = min(distance(p, a.p0), distance(p, a.p1));
                if (udist < min_dist) {
                    min_dist = udist;
                    side = 0.; /* unsure */
                    closest_arc = a;
                } else if (side == 0. && udist == min_dist) {
                    /* If this new distance is the same as the current minimum,
                     * compare extended distances.  Take the sign from the arc
                     * with larger extended distance. */
                    float old_ext_dist = glyphy_arc_extended_dist(closest_arc, p);
                    float new_ext_dist = glyphy_arc_extended_dist(a, p);

                    float ext_dist = abs(new_ext_dist) <= abs(old_ext_dist) ? old_ext_dist : new_ext_dist;

#ifdef GLYPHY_SDF_PSEUDO_DISTANCE
                    /* For emboldening and stuff: */
                    min_dist = abs(ext_dist);
#endif
                    side = sign(ext_dist);
                }
            }
        }
        endpoint_prev = endpoint;

    }

    if (side == 0.) {
        // Technically speaking this should not happen, but it does.  So try to fix it.
        float ext_dist = glyphy_arc_extended_dist(closest_arc, p);
        side = sign(ext_dist);
    }

    return min_dist * side;
}

Seems good now:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants