diff options
Diffstat (limited to 'assets/shaders/triangle.metal')
-rw-r--r-- | assets/shaders/triangle.metal | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/assets/shaders/triangle.metal b/assets/shaders/triangle.metal index 6055705..ccb7ca7 100644 --- a/assets/shaders/triangle.metal +++ b/assets/shaders/triangle.metal @@ -1,33 +1,40 @@ #include <metal_stdlib> - using namespace metal; -struct VertexIn { - float2 position; - float3 color; +struct VertexData { + float4 position; + float2 texCoords; }; struct VertexOut { - float4 computedPosition [[position]]; - float3 fragColor; -}; + // 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]]; -// Vertex shader -vertex VertexOut basic_vertex( - const device VertexIn* vertex_array [[ buffer(0) ]], - unsigned int vid [[ vertex_id ]] - ) { - VertexIn v = vertex_array[vid]; + // 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; +}; - VertexOut outVertex = VertexOut(); - outVertex.computedPosition = float4(v.position.xy, 0.0, 1.0); - outVertex.fragColor = v.color; - return outVertex; +vertex VertexOut +vertexShader(uint vertexID [[vertex_id]], + constant VertexData* vertexData) +{ + VertexOut out; + out.position = vertexData[vertexID].position; + out.textureCoordinate = vertexData[vertexID].texCoords; + return out; } -// Fragment shader -fragment float4 basic_fragment( - VertexOut interpolated [[stage_in]] -) { - return float4(interpolated.fragColor, 1.0); +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 |