diff options
Diffstat (limited to 'assets/shaders/triangle.metal')
-rw-r--r-- | assets/shaders/triangle.metal | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/assets/shaders/triangle.metal b/assets/shaders/triangle.metal index 6522360..ccb7ca7 100644 --- a/assets/shaders/triangle.metal +++ b/assets/shaders/triangle.metal @@ -1,17 +1,40 @@ #include <metal_stdlib> using namespace metal; -vertex float4 +struct VertexData { + float4 position; + float2 texCoords; +}; + +struct VertexOut { + // The [[position]] attribute of this member indicates that this value + // is the clip space position of the vertex when this structure is + // returned from the vertex function. + float4 position [[position]]; + + // Since this member does not have a special attribute, the rasterizer + // interpolates its value with the values of the other triangle vertices + // and then passes the interpolated value to the fragment shader for each + // fragment in the triangle. + float2 textureCoordinate; +}; + +vertex VertexOut vertexShader(uint vertexID [[vertex_id]], - constant simd::float3* vertexPositions) + constant VertexData* vertexData) { - float4 vertexOutPositions = float4(vertexPositions[vertexID][0], - vertexPositions[vertexID][1], - vertexPositions[vertexID][2], - 1.0f); - return vertexOutPositions; + VertexOut out; + out.position = vertexData[vertexID].position; + out.textureCoordinate = vertexData[vertexID].texCoords; + return out; } -fragment float4 fragmentShader(float4 vertexOutPositions [[stage_in]]) { - return float4(182.0f/255.0f, 240.0f/255.0f, 228.0f/255.0f, 1.0f); +fragment float4 fragmentShader(VertexOut in [[stage_in]], + texture2d<float> colorTexture [[texture(0)]]) { + + constexpr sampler textureSampler (mag_filter::linear, + min_filter::linear); + // Sample the texture to obtain a color + const float4 colorSample = colorTexture.sample(textureSampler, in.textureCoordinate); + return colorSample; }
\ No newline at end of file |