Bil*_*ngs 12
通常,您有三种方法可以在Metal中加载着色器库:
通过MTLDevice newLibraryWithSource:options:error:或newLibraryWithSource:options:completionHandler:方法从着色器源代码中使用运行时着色器编译.尽管纯粹主义者可能会回避运行时编译,但此选项具有最小的实际开销,因此完全可行.避免此选项的主要实际原因可能是避免将着色器源代码作为应用程序的一部分提供,以保护您的IP.
使用MTLLibrary newLibraryWithFile:error:或newLibraryWithData:error:方法加载编译的二进制库.按照使用命令行实用程序构建库中的说明在构建时创建这些单独的二进制库.
让Xcode*.metal在构建时将各种文件编译到可用的默认库中MTLDevice newDefaultLibrary.
这是从字符串创建顶点和片段程序的实际代码。使用它可以使您在运行时编译着色器(显示在着色器字符串方法后面的代码中)。
为了消除使用转义序列(例如n ...)的需要,我使用了STRINGIFY宏。为了解决双引号使用的限制,我编写了一个块,该块采用头文件名数组并从中创建导入语句。然后,将它们插入到适当位置的着色器中。对于include语句,我也做同样的事情。它简化并加快了有时是冗长的列表的插入。
合并此代码不仅可以让您基于本地化选择要使用的特定着色器,而且,如有必要,还可以用于更新应用程序的着色器而不必更新应用程序。您只需创建并发送一个包含着色器代码的文本文件,即可对您的应用程序进行预编程以将其用作着色器源。
#if !defined(_STRINGIFY)
#define __STRINGIFY( _x ) # _x
#define _STRINGIFY( _x ) __STRINGIFY( _x )
#endif
typedef NSString *(^StringifyArrayOfIncludes)(NSArray <NSString *> *includes);
static NSString *(^stringifyHeaderFileNamesArray)(NSArray <NSString *> *) = ^(NSArray <NSString *> *includes) {
NSMutableString *importStatements = [NSMutableString new];
[includes enumerateObjectsUsingBlock:^(NSString * _Nonnull include, NSUInteger idx, BOOL * _Nonnull stop) {
[importStatements appendString:@"#include <"];
[importStatements appendString:include];
[importStatements appendString:@">\n"];
}];
return [NSString new];
};
typedef NSString *(^StringifyArrayOfHeaderFileNames)(NSArray <NSString *> *headerFileNames);
static NSString *(^stringifyIncludesArray)(NSArray *) = ^(NSArray *headerFileNames) {
NSMutableString *importStatements = [NSMutableString new];
[headerFileNames enumerateObjectsUsingBlock:^(NSString * _Nonnull headerFileName, NSUInteger idx, BOOL * _Nonnull stop) {
[importStatements appendString:@"#import "];
[importStatements appendString:@_STRINGIFY("")];
[importStatements appendString:headerFileName];
[importStatements appendString:@_STRINGIFY("")];
[importStatements appendString:@"\n"];
}];
return [NSString new];
};
- (NSString *)shader
{
NSString *includes = stringifyIncludesArray(@[@"metal_stdlib", @"simd/simd.h"]);
NSString *imports = stringifyHeaderFileNamesArray(@[@"ShaderTypes.h"]);
NSString *code = [NSString stringWithFormat:@"%s",
_STRINGIFY(
using namespace metal;
typedef struct {
float scale_factor;
float display_configuration;
} Uniforms;
typedef struct {
float4 renderedCoordinate [[position]];
float2 textureCoordinate;
} TextureMappingVertex;
vertex TextureMappingVertex mapTexture(unsigned int vertex_id [[ vertex_id ]],
constant Uniforms &uniform [[ buffer(1) ]])
{
float4x4 renderedCoordinates;
float4x2 textureCoordinates;
if (uniform.display_configuration == 0 ||
uniform.display_configuration == 2 ||
uniform.display_configuration == 4 ||
uniform.display_configuration == 6)
{
renderedCoordinates = float4x4(float4( -1.0, -1.0, 0.0, 1.0 ),
float4( 1.0, -1.0, 0.0, 1.0 ),
float4( -1.0, 1.0, 0.0, 1.0 ),
float4( 1.0, 1.0, 0.0, 1.0 ));
textureCoordinates = float4x2(float2( 0.0, 1.0 ),
float2( 2.0, 1.0 ),
float2( 0.0, 0.0 ),
float2( 2.0, 0.0 ));
} else if (uniform.display_configuration == 1 ||
uniform.display_configuration == 3 ||
uniform.display_configuration == 5 ||
uniform.display_configuration == 7)
{
renderedCoordinates = float4x4(float4( -1.0, -1.0, 0.0, 1.0 ),
float4( -1.0, 1.0, 0.0, 1.0 ),
float4( 1.0, -1.0, 0.0, 1.0 ),
float4( 1.0, 1.0, 0.0, 1.0 ));
if (uniform.display_configuration == 1 ||
uniform.display_configuration == 5)
{
textureCoordinates = float4x2(float2( 0.0, 1.0 ),
float2( 1.0, 1.0 ),
float2( 0.0, -1.0 ),
float2( 1.0, -1.0 ));
} else if (uniform.display_configuration == 3 ||
uniform.display_configuration == 7)
{
textureCoordinates = float4x2(float2( 0.0, 2.0 ),
float2( 1.0, 2.0 ),
float2( 0.0, 0.0 ),
float2( 1.0, 0.0 ));
}
}
TextureMappingVertex outVertex;
outVertex.renderedCoordinate = float4(uniform.scale_factor, uniform.scale_factor , 1.0f, 1.0f ) * renderedCoordinates[vertex_id];
outVertex.textureCoordinate = textureCoordinates[vertex_id];
return outVertex;
}
fragment half4 displayTexture(TextureMappingVertex mappingVertex [[ stage_in ]],
texture2d<float, access::sample> texture [[ texture(0) ]],
sampler samplr [[sampler(0)]],
constant Uniforms &uniform [[ buffer(1) ]]) {
if (uniform.display_configuration == 1 ||
uniform.display_configuration == 2 ||
uniform.display_configuration == 4 ||
uniform.display_configuration == 6 ||
uniform.display_configuration == 7)
{
mappingVertex.textureCoordinate.x = 1 - mappingVertex.textureCoordinate.x;
}
if (uniform.display_configuration == 2 ||
uniform.display_configuration == 6)
{
mappingVertex.textureCoordinate.y = 1 - mappingVertex.textureCoordinate.y;
}
if (uniform.scale_factor < 1.0)
{
mappingVertex.textureCoordinate.y += (texture.get_height(0) - (texture.get_height(0) * uniform.scale_factor));
}
half4 new_texture = half4(texture.sample(samplr, mappingVertex.textureCoordinate));
return new_texture;
}
)];
return [NSString stringWithFormat:@"%@\n%@", includes, imports, code];
}
/*
* Metal setup: Library
*/
__autoreleasing NSError *error = nil;
NSString* librarySrc = [self shader];
if(!librarySrc) {
[NSException raise:@"Failed to read shaders" format:@"%@", [error localizedDescription]];
}
_library = [_device newLibraryWithSource:librarySrc options:nil error:&error];
if(!_library) {
[NSException raise:@"Failed to compile shaders" format:@"%@", [error localizedDescription]];
}
id <MTLFunction> vertexProgram = [_library newFunctionWithName:@"mapTexture"];
id <MTLFunction> fragmentProgram = [_library newFunctionWithName:@"displayTexture"];
.
.
.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4817 次 |
| 最近记录: |