Advertisement
Guest User

Metal simple shader

a guest
Jun 23rd, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. //
  2. //  Shaders.metal
  3. //  Metaltest
  4. //
  5. //  Created by Vlad Shcherban on 2014-06-23.
  6. //  Copyright (c) 2014 Vlad Shcherban. All rights reserved.
  7. //
  8.  
  9. #include <metal_stdlib>
  10. #include <simd/simd.h>
  11.  
  12. using namespace metal;
  13.  
  14. // Variables in constant address space
  15. constant float3 light_position = float3(0.0, 0.0, 1.0);
  16. constant float4 ambient_color  = float4(0.18, 0.24, 0.8, 1.0);
  17. constant float4 diffuse_color  = float4(0.4, 0.4, 1.0, 1.0);
  18.  
  19. typedef struct
  20. {
  21.     matrix_float4x4 modelview_projection_matrix;
  22.     matrix_float4x4 normal_matrix;
  23. } uniforms_t;
  24.  
  25. typedef struct
  26. {
  27.     packed_float3 position;
  28.     packed_float3 normal;
  29. } vertex_t;
  30.  
  31. typedef struct {
  32.     float4 position [[position]];
  33.     half4  color;
  34. } ColorInOut;
  35.  
  36. // Vertex shader function
  37. vertex ColorInOut lighting_vertex(global vertex_t* vertex_array [[ buffer(0) ]],
  38.                                   constant uniforms_t& uniforms [[ buffer(1) ]],
  39.                                   unsigned int vid [[ vertex_id ]])
  40. {
  41.     ColorInOut out;
  42.    
  43.     float4 in_position = float4(float3(vertex_array[vid].position), 1.0);
  44.     out.position = uniforms.modelview_projection_matrix * in_position;
  45.    
  46.     float3 normal = vertex_array[vid].normal;
  47.     float4 eye_normal = normalize(uniforms.normal_matrix * float4(normal, 0.0));
  48.     float n_dot_l = dot(eye_normal.rgb, normalize(light_position));
  49.     n_dot_l = fmax(0.0, n_dot_l);
  50.    
  51.     out.color = half4(ambient_color + diffuse_color * n_dot_l);
  52.    
  53.     return out;
  54. }
  55.  
  56. // Fragment shader function
  57. fragment half4 lighting_fragment(ColorInOut in [[stage_in]])
  58. {
  59.     return in.color;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement