Metal Shading Language Specification



Yüklə 4,82 Kb.
Pdf görüntüsü
səhifə31/51
tarix25.05.2018
ölçüsü4,82 Kb.
#45967
1   ...   27   28   29   30   31   32   33   34   ...   51

The corresponding function written using function constant variables is: 
constant bool offset_defined [[function_constant(0)]]; 
constant bool color_defined [[function_constant(1)]]; 
vertex VertexOutput 
myVertex(VertexInput vIn [[stage_in]]) 

VertexOutput vOut; 
vOut.position = vIn.position; 
if (offset_defined
vOut.position += vIn.offset; 
if (color_defined
vOut.color = vIn.color; 
else 
vOut.color = float4(0.0f); 
return vOut; 

Functions constants can only be a scalar or vector type. Using a user-defined type or an array 
of a scalar or vector type for a function constant results in a compilation error. 
4.10.1.2 
Function Constants when Declaring the Arguments of Functions  
Arguments to a graphics, kernel or user functions can be declared with the 
[[function_constant(name)]] 
attribute attribute to identify that the argument is optional. 
name
 refers to a function constant variable. If the value of the function constant variable given 
by 
name
 is 
non-zero
 or 
true
 (determined when the render or compute pipeline state is 
created), the argument is considered to be declared in the function signature. If the value of the 
function constant variable given by 
name
 is 
0
 or 
false
, the argument is not considered to be 
declared in the function signature. If 
name
 refers to a function constant variable that has not 
been defined (determined when the render or compute pipeline state is created), the behavior is 
the same as if 
is_function_constant_defined(name)
 is used and its value is 
false

Consider the following fragment function that uses pre-processor macros in its function 
declaration: 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page  
 of  
100
174


fragment half4 
myFragment(constant GlobalUniformData *globalUniform [[buffer(0)]], 
 constant RenderUniformData_ModelWithLightmap *renderUniform 
[[buffer(1)]], 
 constant MaterialUniformData *materialUniform [[buffer(2)]], 
 texture2d DiffuseTexture [[texture(0)]], 
 texture2d LightmapTexture [[texture(1)]], 
 texture2d FogTexture [[texture(3)]], 
 #ifdef MED_QUALITY 
 texture2d LookupTexture [[texture(4)]], 
 #endif 
 #ifdef REALTIME_SHADOW 
 texture2d RealtimeShadowMapTexture [[texture(10)]], 
 #endif 
 sampler DiffuseTextureSampler [[sampler(0)]], 
 sampler LightmapTextureSampler [[sampler(1)]], 
 sampler FogTextureSampler [[sampler(3)]], 
 #ifdef MED_QUALITY 
 sampler LookupTextureSampler [[sampler(4)]], 
 #endif        
 
 
 
 #ifdef REALTIME_SHADOW 
 sampler RealtimeShadowMapTextureSampler [[sampler(10)]], 
 #endif 
 VertexOutput fragIn [[stage_in]]) 
The corresponding fragment function now written using function constants is: 
constant bool realtime_shadow [[function_constant(0)]]; 
constant bool med_quality [[function_constant(1)]]; 
constant bool med_quality_defined = 
is_function_constant_defined(med_quality); 
constant bool realtime_shadow_defined = 
is_function_constant_defined(realtime_shadow); 
fragment half4 
myFragment(constant GlobalUniformData *globalUniform [[buffer(0)]], 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page  
 of  
101
174


 constant RenderUniformData_ModelWithLightmap *renderUniform 
[[buffer(1)]], 
 constant MaterialUniformData *materialUniform [[buffer(2)]], 
 texture2d DiffuseTexture [[texture(0)]], 
 texture2d LightmapTexture [[texture(1)]], 
 texture2d FogTexture [[texture(3)]], 
 texture2d LookupTexture [[texture(4), 
function_constant(med_quality_defined)]], 
 texture2d RealtimeShadowMapTexture [[texture(10), 
function_constant(realtime_shadow_defined)]], 
 sampler DiffuseTextureSampler [[sampler(0)]], 
 sampler LightmapTextureSampler [[sampler(1)]], 
 sampler FogTextureSampler [[sampler(3)]], 
 sampler LookupTextureSampler [[sampler(4), 
function_constant(med_quality_defined)]], 
 sampler RealtimeShadowMapTextureSampler [[sampler(10), 
function_constant(realtime_shadow_defined)]], 
 VertexOutput fragIn [[stage_in]]) 
Below is another example that shows how to use function constants with arguments to a 
function: 
constant bool hasInputBuffer [[function_constant(0)]]; 
kernel void 
kernelOptionalBuffer(device int *input [[buffer(0), 
function_constant(inputBufferDefined)]], 
device int *output [[buffer(1)]], 
uint tid [[thread_position_in_grid]]) 

if (hasInputBuffer) 
output[tid] = inputA[0] * tid; 
else 
output[tid] = tid; 

4.10.1.3 
Function Constants for Elements of a [[stage_in]] Struct 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page  
 of  
102
174


Elements of a struct declared with the 
[[stage_in]]
 attribute passed to a graphics function 
(or post-tessellation vertex function) can also be declared with the 
[[function_constant(name)]]
 attribute to identify that the element is optional. 
name
 refers 
to a function constant variable. If the value of the function constant variable given by 
name
 is 
non-zero
 or 
true
 (determined when the render or compute pipeline state is created), the 
element in the struct is considered to be declared in the function signature. If the value of the 
function constant variable given by 
name
 is 
0
 or 
false
, the element is not considered to be 
declared in the struct. If 
name
 refers to a function constant variable that has not been defined 
(determined when the render or compute pipeline state is created), the behavior is the same as 
if 
is_function_constant_defined(name)
 is used and its value is 
false
.  
Example: 
constant bool offset_defined [[function_constant(0)]]; 
constant bool color_defined [[function_constant(1)]]; 
struct VertexOutput { 
float4 position [[position]]; 
float4 color; 
}; 
struct VertexInput { 
float4 position [[attribute(0)]]; 
float4 offset   [[attribute(1), function_constant(offset_defined)]]; 
float4 color    [[attribute(2), function_constant(color_defined)]]; 
}; 
vertex VertexOutput 
myVertex(VertexInput vIn [[stage_in]]) 

VertexOutput vOut; 
vOut.position = vIn.position; 
if (offset_defined) 
vOut.position += vIn.offset; 
if (color_defined) 
vOut.color = vIn.color; 
else 
 
2017-9-12   |  Copyright © 2017 Apple Inc. All Rights Reserved.  
Page  
 of  
103
174


Yüklə 4,82 Kb.

Dostları ilə paylaş:
1   ...   27   28   29   30   31   32   33   34   ...   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ə