What is the difference between [[textureCoordinates]] and [[position]] in Metal Shading Language
I am processing videos with metal and swift. I have a vertex shader that scales the frames to aspect fit inside my view. In my fragment shader it works well if I sample with textureCoordinates like so:
inputTexture.sample(sampler, inputFragment.textureCoordinates);
But if I use read with position instead, it returns the unscaled frame:
inputTexture.read(uint2(inputFragment.position.xy));
I am assuming that this is usual behavior, I just don't know why so I was wondering if someone could explain the difference between the two. Thanks!
textureCoordinates
- Normalized texture coordinate.
For 2D textures, normalized texture coordinates are values from 0.0 to 1.0 in both x and y directions. A value of (0.0, 0.0) specifies the texel at the first byte of the texture data (the top-left corner of the image). A value of (1.0, 1.0) specifies the texel at the last byte of the texture data (the bottom-right corner of the image).
position.xy
- The clip space position of the vertex.
struct RasterizerData
{
// The [[position]] attribute qualifier of this member indicates this value is
// the clip space position of the vertex when this structure is returned from
// the vertex shader
float4 position [[position]];
// Since this member does not have a special attribute qualifier, the rasterizer
// will interpolate its value with values of other vertices making up the triangle
// and pass that interpolated value to the fragment shader for each fragment in
// that triangle.
float2 textureCoordinate;
};
-
Creating and Sampling Textures
-
Metal Coordinate Systems