src/render/metal/SDL_shaders_metal.metal
author Alex Szpakowski <slime73@gmail.com>
Sat, 30 Dec 2017 20:32:22 -0400
changeset 11789 01f9851b2c9d
parent 11755 e9a1b9ea8da0
child 11799 cecf6b192ced
permissions -rw-r--r--
metal: use a projection matrix instead of manually transforming vertices into clip space on the CPU.
     1 #include <metal_texture>
     2 #include <metal_matrix>
     3 
     4 using namespace metal;
     5 
     6 struct SolidVertexOutput
     7 {
     8 	float4 position [[position]];
     9 	float pointSize [[point_size]];
    10 };
    11 
    12 vertex SolidVertexOutput SDL_Solid_vertex(const device float2 *position [[buffer(0)]],
    13                                           constant float4x4 &projection [[buffer(2)]],
    14                                           uint vid [[vertex_id]])
    15 {
    16     SolidVertexOutput v;
    17 	v.position = projection * float4(position[vid].x, position[vid].y, 0.0f, 1.0f);
    18 	v.pointSize = 0.5f;
    19 	return v;
    20 }
    21  
    22 fragment float4 SDL_Solid_fragment(constant float4 &col [[buffer(0)]])
    23 {
    24     return col;
    25 }
    26 
    27 struct CopyVertexOutput
    28 {
    29     float4 position [[position]];
    30     float2 texcoord;
    31 };
    32 
    33 vertex CopyVertexOutput SDL_Copy_vertex(const device float2 *position [[buffer(0)]],
    34                                         const device float2 *texcoords [[buffer(1)]],
    35                                         constant float4x4 &projection [[buffer(2)]],
    36                                         uint vid [[vertex_id]])
    37 {
    38     CopyVertexOutput v;
    39     v.position = projection * float4(position[vid].x, position[vid].y, 0.0f, 1.0f);
    40     v.texcoord = texcoords[vid];
    41     return v;
    42 }
    43 
    44 fragment float4 SDL_Copy_fragment_nearest(CopyVertexOutput vert [[stage_in]],
    45                                           constant float4 &col [[buffer(0)]],
    46                                           texture2d<float> tex [[texture(0)]])
    47 {
    48     constexpr sampler s(coord::normalized, address::clamp_to_edge, filter::nearest);
    49     return tex.sample(s, vert.texcoord) * col;
    50 }
    51 
    52 fragment float4 SDL_Copy_fragment_linear(CopyVertexOutput vert [[stage_in]],
    53                                          constant float4 &col [[buffer(0)]],
    54                                          texture2d<float> tex [[texture(0)]])
    55 {
    56     constexpr sampler s(coord::normalized, address::clamp_to_edge, filter::linear);
    57     return tex.sample(s, vert.texcoord) * col;
    58 }
    59