AAPLShaders.metal 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. See LICENSE folder for this sample’s licensing information.
  3. Abstract:
  4. Metal shaders used for this sample
  5. */
  6. #include <metal_stdlib>
  7. using namespace metal;
  8. // Include header shared between this Metal shader code and C code executing Metal API commands.
  9. #include "AAPLShaderTypes.h"
  10. // Vertex shader outputs and fragment shader inputs
  11. struct RasterizerData
  12. {
  13. // The [[position]] attribute of this member indicates that this value
  14. // is the clip space position of the vertex when this structure is
  15. // returned from the vertex function.
  16. float4 position [[position]];
  17. // Since this member does not have a special attribute, the rasterizer
  18. // interpolates its value with the values of the other triangle vertices
  19. // and then passes the interpolated value to the fragment shader for each
  20. // fragment in the triangle.
  21. float4 color;
  22. };
  23. vertex RasterizerData
  24. vertexShader(uint vertexID [[vertex_id]],
  25. constant AAPLVertex *vertices [[buffer(AAPLVertexInputIndexVertices)]],
  26. constant vector_uint2 *viewportSizePointer [[buffer(AAPLVertexInputIndexViewportSize)]])
  27. {
  28. RasterizerData out;
  29. // Index into the array of positions to get the current vertex.
  30. // The positions are specified in pixel dimensions (i.e. a value of 100
  31. // is 100 pixels from the origin).
  32. float2 pixelSpacePosition = vertices[vertexID].position.xy;
  33. // Get the viewport size and cast to float.
  34. vector_float2 viewportSize = vector_float2(*viewportSizePointer);
  35. // To convert from positions in pixel space to positions in clip-space,
  36. // divide the pixel coordinates by half the size of the viewport.
  37. out.position = vector_float4(0.0, 0.0, 0.0, 1.0);
  38. out.position.xy = pixelSpacePosition / (viewportSize / 2.0);
  39. // Pass the input color directly to the rasterizer.
  40. out.color = vertices[vertexID].color;
  41. return out;
  42. }
  43. fragment float4 fragmentShader(RasterizerData in [[stage_in]])
  44. {
  45. // Return the interpolated color.
  46. return in.color;
  47. }