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];
};
constexpr sampler s = sampler(coord::normalized, address::clamp_to_edge,
filter::linear);
vertex VertexOutput
render_vertex(const device VertexInput *v_in [[buffer(0)]],
constant float4x4& mvp_matrix [[buffer(1)]],
constant LightDesc& lights [[buffer(2)]],
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,
lights);
v_out.texcoord = v_in[v_id].texcoord;
return v_out;
}
fragment float4
render_pixel(
VertexOutput input
[[stage_in]],
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
80
174
declared inside a graphics or kernel function. In the following example, the
static
specifier is
incorrectly used for the variables
b
and
c
declared inside a kernel function.
extern constant float4 noise_table[256];
static constant float4 color_table[256] = { … }; //
static is okay
extern void my_foo(texture2d
img);
extern void my_bar(device float *a);
kernel void
my_kernel(texture2d img [[texture(0)]],
device float *ptr [[buffer(0)]])
{
extern constant float4 a;
static constant float4 b;
// static is an error.
static float c;
// static is an error.
…
my_foo(img);
…
my_bar(ptr);
…
}
4.5 Sampling and Interpolation Attributes
Sampling and interpolation attributes are used with inputs to fragment functions declared with
the
stage_in
attribute. The attribute determines what sampling method the fragment function
uses and how the interpolation is performed, including whether to use perspective-correct
interpolation, linear interpolation, or no interpolation.
The sampling and interpolation attribute can be specified on any structure member declared
with the
stage_in
attribute. The sampling and interpolation attributes supported are:
center_perspective
center_no_perspective
centroid_perspective
centroid_no_perspective
sample_perspective
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
82
174