duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

metal_shaders.metal (1818B)


      1 /// A custom resolve kernel that averages color at all sample points.
      2 #include <metal_stdlib>
      3 using namespace metal;
      4 
      5 // https://developer.apple.com/documentation/metal/metal_sample_code_library/improving_edge-rendering_quality_with_multisample_antialiasing_msaa?language=objc
      6 kernel void colorResolveKernel(texture2d_ms<float, access::read> multisampledTexture [[texture(0)]],
      7                                texture2d<float, access::write> resolvedTexture [[texture(1)]],
      8                                uint2 gid [[thread_position_in_grid]])
      9 {
     10   const uint count = multisampledTexture.get_num_samples();
     11 
     12   float4 resolved_color = 0;
     13 
     14   for (uint i = 0; i < count; ++i)
     15   {
     16     resolved_color += multisampledTexture.read(gid, i);
     17   }
     18 
     19   resolved_color /= count;
     20 
     21   resolvedTexture.write(resolved_color, gid);
     22 }
     23 
     24 kernel void depthResolveKernel(texture2d_ms<float, access::read> multisampledTexture [[texture(0)]],
     25                                texture2d<float, access::write> resolvedTexture [[texture(1)]],
     26                                uint2 gid [[thread_position_in_grid]])
     27 {
     28   const uint count = multisampledTexture.get_num_samples();
     29 
     30   float resolved_depth = 0;
     31 
     32   for (uint i = 0; i < count; ++i)
     33   {
     34     resolved_depth += multisampledTexture.read(gid, i).r;
     35   }
     36 
     37   resolved_depth /= count;
     38 
     39   resolvedTexture.write(float4(resolved_depth, 0, 0, 0), gid);
     40 }
     41 
     42 struct DepthClearUBO
     43 {
     44   float depth;
     45 };
     46 
     47 struct DepthClearOut
     48 {
     49   float4 pos [[position]];
     50 };
     51 
     52 vertex DepthClearOut depthClearVertex(constant DepthClearUBO& ubo [[buffer(0)]], uint vertexId [[vertex_id]])
     53 {
     54   DepthClearOut out = {};
     55   float2 uv = float2(float((vertexId << uint(1)) & 2u), float(vertexId & 2u));
     56   out.pos = float4((uv * float2(2.0, -2.0)) + float2(-1.0, 1.0), ubo.depth, 1.0);
     57   return out;
     58 }
     59 
     60 fragment void depthClearFragment()
     61 {
     62 }