0
votes

I was playing with applying dithering to a simple colored quad, and found a strange issue. I have a fragment shader which should calculate dithering at some uv and return a dithered color. This works fine on a textured quad, but strangely enough, when I access color data from my inVertex, the uv coordinates change to some bizarre values, and y value seems to be mapped to x axis. I'll try to illustrate what happens when I change stuff around the fragment shader code.

fragment float4 fragment_colored_dithered(ColoredVertex inVertex [[stage_in]],
                                              float2 uv[[point_coord]]) {
        float4 color = inVertex.color;
        uv = (uv/2) + 0.5;
        if (uv.y < 0.67) {
            return float4(uv.y, 0, 0, 1);
        }
        else {
            return color;
        }
    }

Produces the following result: enter image description here

Where the left side of the image shows my gradient quad, notice that if (uv.y < 0.67) maps to x values in the image ????.

If I change this fragment shader and nothing else in the code, like so, where I return float4(0, 0, 1, 0) instead of inVertex.color, the uv coordinates are mapped correctly.

fragment float4 fragment_colored_dithered(ColoredVertex inVertex [[stage_in]],
                                          float2 uv[[point_coord]]) {
    float4 color = inVertex.color;
    uv = (uv/2) + 0.5;
    if (uv.y < 0.67) {
        return float4(uv.y, 0, 0, 1);
    }
    else {
        return float4(0, 0, 1, 0); //return color;
    }
} 

Produces this (correct) result: enter image description here

I think I can hack around this problem by applying a 1x1 texture to the gradient and using texture coordinates, but I'd really like to know what is happening here, is this a bug or a feature that I don't understand?

1

1 Answers

3
votes

Why are you using [[point_coord]]? What do you think it represents?

Unless you're drawing point primitives, you shouldn't be using that. Since you're drawing a "quad", and given the screenshots, I assume you're not drawing point primitives.

I suspect [[point_coord]] is simply undefined and subject to random-ish behavior when you're drawing triangles. The randomness is apparently affected by the specifics (such as stack layout) of the fragment shader.

You should either be using [[position]] and scaling by the window size or using an interpolated field within your ColoredVertex struct to carry "texture" coordinates.