kernel void
my_kernel(MyResources r) //
illegal use
{…}
Nested structs are also supported as shown by the following example.
struct Foo {
texture2d
a [[texture(0)]];
depth2d b [[texture(1)]];
};
struct Bar {
Foo f;
sampler s [[sampler(0)]];
};
kernel void
my_kernel(Bar b)
{…}
4.3.3
Attributes to Locate Per-Vertex Inputs
A vertex function can read per-vertex inputs by indexing into a buffer(s) passed as arguments to
the vertex function using the vertex and instance IDs. In addition, per-vertex inputs can also be
passed as an argument to a vertex function by declaring them with the
[[stage_in]]
attribute.
For per-vertex inputs passed as an argument declared with the
[[stage_in]]
attribute, each
element of the per-vertex input must specify the vertex attribute location as
[[attribute(index)]]
.
The
index
value is an unsigned integer that identifies the vertex input location that is being
assigned. The proper syntax is for the attribute to follow the argument/variable name. The Metal
API uses this attribute to identify the location of the vertex buffer and describe the vertex data
such as the buffer to fetch the per-vertex data from, its data format, and its stride.
The example below shows how vertex attributes can be assigned to elements of a vertex input
struct passed to a vertex function using the
stage_in
attribute.
struct VertexInput {
float4 position [[attribute(0)]];
float3 normal [[attribute(1)]];
half4 color [[attribute(2)]];
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
64
174
Returns the number of control-points in the patch
constexpr const_reference operator[] (size_t pos) const;
Returns the data for a specific patch control point identified by
pos
.
Example:
struct ControlPoint {
int3 patchParam [[attribute(0)]];
float3 P [[attribute(1)]];
float3 P1 [[attribute(2)]];
float3 P2 [[attribute(3)]];
float2 vSegments [[attribute(4)]];
};
struct PerPatchData {
float4 patchConstant [[attribute(5)]];
float4 someOtherPatchConstant [[attribute(6)]];
};
struct PatchData {
patch_control_point
cp;
// control-point data
PerPatchData patchData;
// per-patch data
};
[[patch(quad)]]
vertex VertexOutput
post_tess_vertex_func(PatchData input [[stage_in ]}, …)
{…}
4.3.4
Attributes for Built-in Variables
Some graphics operations occur in the fixed-function pipeline stages and need to provide
values to or receive values from graphics functions. Built-in input and output variables are used
to communicate values between the graphics (vertex and fragment) functions and the fixed-
function graphics pipeline stages. Attributes are used with arguments and the return type of
graphics functions to identify these built-in variables.
4.3.4.1
Vertex Function Input Attributes
2017-9-12 | Copyright © 2017 Apple Inc. All Rights Reserved.
Page of
67
174