Skip to content

Instantly share code, notes, and snippets.

@zeux

zeux/shader.cpp Secret

Created June 2, 2014 19:55
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zeux/7f1b88ebca2efd47b696 to your computer and use it in GitHub Desktop.
Metal example shader from the documentation
#include <metal_stdlib>
using namespace metal;
struct VertexOutput {
float4 position [[position]];
float4 color;
float2 texcoord;
};
struct VertexInput {
float4 position;
float3 normal;
float2 texcoord;
};
constexpr constant uint MAX_LIGHTS = 4;
struct LightDesc {
uint num_lights;
float4 light_position[MAX_LIGHTS];
float4 light_color[MAX_LIGHTS];
float4 light_attenuation_factors[MAX_LIGHTS];
};
vertex VertexOutput
render_vertex(const global VertexInput* v_in [[ buffer(0) ]],
constant float4x4& mvp_matrix [[ buffer(1) ]],
constant LightDesc& light_desc [[ buffer(2) ]],
global float4* xform_pos_output [[ buffer(3) ]],
uint v_id [[ vertex_id ]] )
{
VertexOutput v_out;
v_out.position = v_in[v_id].position * mvp_matrix;
v_out.color = do_lighting(v_in[v_id].position,
v_in[v_id].normal,
light_desc);
v_out.texcoord = v_in[v_id].texcoord;
// output position to a buffer
xform_pos_output[v_id] = v_out.position;
return v_out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment