Metal Shading Language Specification



Yüklə 4,82 Kb.
Pdf görüntüsü
səhifə26/51
tarix25.05.2018
ölçüsü4,82 Kb.
#45967
1   ...   22   23   24   25   26   27   28   29   ...   51

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


   texture2d imgA [[texture(0)]],  
   texture2d imgB [[texture(1)]]) 

float4 tex_clr0 = imgA.sample(s, input.texcoord); 
float4 tex_clr1 = imgB.sample(s, input.texcoord); 
// compute color 
float4 clr = compute_color(tex_clr0, tex_clr1, …); 
return clr; 

4.3.5.3  
Kernel Function Example that Uses the stage_in Attribute 
The following example shows how to pass per-thread inputs using the 
stage_in
 attribute.  
Using the 
stage_in
 attribute in a kernel function allows you to decouple the data type used to 
declare the per-thread inputs in the function from the actual data type used to store the per-
thread inputs. 
struct PerThreadInput { 
float4 a [[attribute(0)]]; 
float3 b [[attribute(1)]]; 
half4 c  [[attribute(2)]]; 
half2 d  [[attribute(3)]]; 
}; 
kernel void 
my_kernel(PerThreadInput thread_input [[stage_in]], 
… 
uint t_id [[thread_position_in_grid]]) 
{…} 
4.4  Storage Class Specifiers 
Metal supports the 
static
 and 
extern
 storage class specifiers. Metal does not support the 
thread_local
 storage class specifiers. 
The 
extern
 storage-class specifier can only be used for functions and variables declared in 
program scope or variables declared inside a function. The 
static
 storage-class specifier is 
only for device variables declared in program scope (see section 4.2.3) and is not for variables 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page    of  
81
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


sample_no_perspective 
flat 
center_perspective
 is the default sampling and interpolation attribute, with the following 
exceptions: 
• For a variable with the 
[[position]]
 attribute, the only valid sampling and interpolation 
attribute is 
center_no_perspective

• For an integer variable, the only valid sampling and interpolation attribute is 
flat

The sampling attribute variants (
sample_perspective
 and 
sample_no_perspective

interpolate at a sample location rather than at the pixel center. With one of these attributes, the 
fragment function (or code blocks in the fragment function) that use these variables execute 
per-sample rather than per-fragment. 
The following example is user-defined struct that specifies how data in certain members are 
interpolated: 
struct FragmentInput { 
float4 pos [[center_no_perspective]]; 
float4 color [[center_perspective]]; 
float2 texcoord; 
int index [[flat]];  
float f [[sample_perspective]];  
}; 
4.6  Per-Fragment Function vs. Per-Sample Function 
The fragment function is typically executed per-fragment. The sampling attribute identifies if 
any fragment input is to be interpolated at per-sample vs. per-fragment. Similarly, the 
[[sample_id]]
 attribute is used to identify the current sample index and the 
[[color(m)]]
 
attribute is used to identify the destination fragment color or sample color (for a multisampled 
color attachment) value. If any of these attributes are used with arguments to a fragment 
function, the fragment function may execute per-sample instead of per-pixel. (The 
implementation may decide to only execute the code that depends on the per-sample values to 
execute per-sample and the rest of the fragment function may execute per-fragment.) 
Only the inputs with sample specified (or declared with the 
[[sample_id]]
 or 
[[color(m)]]
 
attribute) differ between invocations per-fragment or per-sample, whereas other inputs still 
interpolate at the pixel center.  
The following example uses the 
[[color(m)]]
 attribute to specify that this fragment function 
is executed on a per-sample basis. 
fragment float4 
my_fragment(float2 tex_coord [[stage_in]], 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page    of  
83
174


Yüklə 4,82 Kb.

Dostları ilə paylaş:
1   ...   22   23   24   25   26   27   28   29   ...   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ə