FontShader.metal 964 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <metal_stdlib>
  2. using namespace metal;
  3. #include "VertexConstants.h"
  4. constexpr sampler alphaTextureSampler(mag_filter::linear, min_filter::linear);
  5. struct FontVertex
  6. {
  7. float3 vPos [[attribute(0)]];
  8. float2 vTex [[attribute(1)]];
  9. uchar4 vCol [[attribute(2)]];
  10. };
  11. struct FontOut
  12. {
  13. float4 oPosition [[position]];
  14. float2 oTex;
  15. float4 oColor;
  16. };
  17. vertex FontOut FontVertexShader(FontVertex vert [[stage_in]], constant VertexShaderConstantBuffer *constants [[buffer(2)]])
  18. {
  19. FontOut out;
  20. out.oPosition = constants->Projection * constants->View * float4(vert.vPos, 1.0);
  21. out.oTex = vert.vTex;
  22. out.oColor = float4(vert.vCol) / 255.0;
  23. return out;
  24. }
  25. fragment float4 FontPixelShader(FontOut in [[stage_in]], texture2d<float> alphaTexture [[texture(0)]])
  26. {
  27. const float4 sample = alphaTexture.sample(alphaTextureSampler, in.oTex);
  28. if (sample.x < 0.5)
  29. discard_fragment();
  30. return float4(in.oColor.xyz, sample.x);
  31. }