0

Fragment Shader

vertex VertexOutBezier bezier_vertex(constant BezierParameters *allParams[[buffer(0)]],
                                    //  constant GlobalParameters& globalParams[[buffer(1)]],
                                     uint vertexId [[vertex_id]],
                                     uint instanceId [[instance_id]])
{

    float t = (float) vertexId / 300;
    rint(t);
    BezierParameters params = allParams[instanceId];

    float lineWidth = (1 - (((float) (vertexId % 2)) * 2.0)) * params.lineThickness;

    float2 a = params.a;
    float2 b = params.b;
    float nt = 1.0f - t;
    float nt_2 = nt * nt;
    float nt_3 = nt_2 * nt;
    float t_2 = t * t;
    float t_3 = t_2 * t;

    float2 point = a * nt_3 + params.p1 * nt_2 * t + params.p2 * nt * t_2 + b * t_3;

    float2 tangent = -3.0 * a * nt_2 + params.p1 * (1.0 - 4.0 * t + 3.0 * t_2) + params.p2 * (2.0 * t - 3.0 * t_2) + 3 * b * t_2;

    tangent = normalize(float2(-tangent.y, tangent.x));
    VertexOutBezier vo;   
    vo.pos.xy = point   + (tangent * (lineWidth / 3.0f));
    vo.pos.zw = float2(0, 1);
    vo.color = params.color ;
    return vo;
}

My Fragment shader is

fragment float4 bezier_fragment(VertexOutBezier params[[stage_in]],
                                texture2d<float> texture [[texture(0)]]
                                )
{
    constexpr sampler defaultSampler;
   float4 canvasColor = texture.sample(defaultSampler, params.pos.xy);
    return canvasColor;   
}

Here i expect to get the pixel color of the texture. But here it is only getting single color. It is not getting the color of the texture according to its position.

Even when I do this in fragment I am getting the single color it is not varying with coordinates

fragment float4 bezier_fragment(VertexOutBezier params[[stage_in]],
                                texture2d<float> texture [[texture(0)]]
                                )
{
    constexpr sampler defaultSampler;
   float4 canvasColor = params.color * params.pos.x;
    return canvasColor;
}

If I do this in Vertex Shader I got color varying according position of x

vo.pos.xy = point   + (tangent * (lineWidth / 3.0f));
vo.pos.zw = float2(0, 1);
vo.color = params.color * vo.pos.x;

What is the Issue in fragment Shader. I cannot get the coordinates from Vertex Shader

1
  • We need to see the definition of the VertexOutBezier structure type. Commented Aug 17, 2018 at 15:56

1 Answer 1

1

Please make sure the VertexOutBezier.pos.xy value is normalization ( 0 ~ 1.0) ,due to the defaultSampler only receive normalization position value, if always return a single may be the position is beyond the bounds.

2
  • Cant we receive not normalized values ? Is there any sampler method to receive not normalized value from Vertex Shader ?
    – Ruban4Axis
    Commented Aug 28, 2018 at 4:14
  • 1
    Of course, try this constexpr sampler defaultSampler(coord::pixel); in the fragment shader.
    – Danny Lau
    Commented Aug 28, 2018 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.