Metal Shading Language Specification



Yüklə 4,82 Kb.
Pdf görüntüsü
səhifə21/51
tarix25.05.2018
ölçüsü4,82 Kb.
#45967
1   ...   17   18   19   20   21   22   23   24   ...   51

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


half2 texcoord  [[attribute(3)]]; 
}; 
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_zero, 
filter::linear); 
vertex VertexOutput 
render_vertex(VertexInput v_in [[stage_in]], 
    constant float4x4& mvp_matrix [[buffer(1)]], 
    constant LightDesc& lights [[buffer(2)]], 
    uint v_id [[vertex_id]]) 

VertexOutput v_out; 
…  
return v_out; 

The example below shows how both buffers and the 
stage_in
 attribute can be used to fetch 
per-vertex inputs in a vertex function. 
struct VertexInput { 
float4 position [[attribute(0)]]; 
float3 normal   [[attribute(1)]]; 
}; 
struct VertexInput2 { 
half4 color; 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page    of  
65
174


half2 texcoord[4]; 
}; 
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_zero, 
filter::linear); 
vertex VertexOutput 
render_vertex(VertexInput v_in [[stage_in]], 
    VertexInput2 v_in2 [[buffer(0)]], 
    constant float4x4& mvp_matrix [[buffer(1)]], 
    constant LightDesc& lights [[buffer(2)]], 
    uint v_id [[vertex_id]]) 

VertexOutput vOut; 
…  
return vOut; 

A post-tessellation vertex function can read the per-patch and patch control-point data. The 
patch control-point data is specified in the post-tessellation vertex function as the following 
templated type: 
patch_control_point 
where 
T
 is a user defined struct. Each element of 
T
 must specify an attribute location using 
[[attribute(index)]]

Member functions are supported by the 
patch_control_point
 type are: 
constexpr size_t size() const; 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page    of  
66
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


Yüklə 4,82 Kb.

Dostları ilə paylaş:
1   ...   17   18   19   20   21   22   23   24   ...   51




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə