HelloWorldMetal
Metal 跟opengl是差不多的,只是使用了新的写法来做绘制渲染
一、opengl的shader则是对应了Metal的.metal文件,但是比shader优势的地方是metal文件可以跟mm共用头文件,metal格式类似于C++的格式,只是增加了[[xxxx]]类似的修饰符来对图片定点进行描述
typedef struct { float4 clipSpacePosition [[position]]; // position的修饰符表示这个是顶点,对应本次的gl渲染的位置 float2 textureCoordinate; // 纹理坐标,会做插值处理 } RasterizerData;
如上改结构体定义了从顶点着色器到片元着色器要传的值。[[position]]标识了是gl顶点,则顶点着色器会根据该标识知道要渲染的位置
metal都是通过标识来知道要穿入什么参数,即使上面的结构体参数调换位置,顶点着色器依旧能够知道要渲染的位置。接下去要说的顶点着色器和片元着色器函数的每个参数也都是有对应的标识,这些参数均可调换位置,只要标识对应的上即可。
vertex RasterizerData // 返回给片元着色器的结构体 vertexShader(uint vertexID [[ vertex_id ]], // vertex_id是顶点shader每次处理的index,用于定位当前的顶点 constant LYVertex *vertexArray [[ buffer(1) ]]) { // buffer表明是缓存数据,1是索引 RasterizerData out; out.clipSpacePosition = vertexArray[vertexID].position; out.textureCoordinate = vertexArray[vertexID].textureCoordinate; return out; }
如上是顶点着色器的函数,buffer(1)的1是对应的如下从cpu设置的索引(一般是从0开始,我这里写1是为了跟offset区分,更能明白是该index)
[renderEncoder setVertexBuffer:self.vertices offset:0 atIndex:1]; // 设置顶点缓存
fragment float4 samplingShader(RasterizerData input [[stage_in]], // stage_in表示这个数据来自光栅化。(光栅化是顶点处理之后的步骤,业务层无法修改) texture2dcolorTexture [[ texture(0) ]]) // texture表明是纹理数据,0是索引 { constexpr sampler textureSampler (mag_filter::linear, min_filter::linear); // sampler是采样器 half4 colorSample = colorTexture.sample(textureSampler, input.textureCoordinate); // 得到纹理对应位置的颜色 return float4(colorSample); }
如上的textureSampler 则是指定了采样器均是采用线性的方式采样
colorSample则是通过采样器在纹理上根据指定的要采样的纹理位置进行采样
二~~~~二、从cpu到gpu到屏幕的渲染的个人理解是:
1、需要有个屏幕view,用来显示:MTKView
2、需要有个渲染管道用来将cpu的数据通过gpu渲染管道的处理在线上到view上面
3、gpu需要有缓冲区用来存储对应要绘制的顶点
4、gpu可以有纹理(texture)对应需要渲染的图片
1、MTKView则需要初始化对应的device、和每次要渲染的回调函数,则需要实现对应的协议
2、渲染管道、顶点缓冲区、纹理等均是以id协议的形式
根据协议对应的函数
- (void)drawInMTKView:(MTKView *)view
每一帧都进行调用该drawInMTKView
同样的渲染命令依旧是:
新建命令缓冲区,并开始执行如下的命令
设置屏幕大小-》设置渲染管道-〉给管道顶点数据-》给管道片元着色器的纹理数据-〉指定要绘制的形状
最后缓冲区显示
具体的代码:
// // ViewController.m // LearnMetal // // Created by loyinglin on 2018/6/21. // Copyright ? 2018年 loyinglin. All rights reserved. // @import MetalKit; #import "LYShaderTypes.h" #import "ViewController.h" @interface ViewController ()// view @property (nonatomic, strong) MTKView *mtkView; // data @property (nonatomic, assign) vector_uint2 viewportSize; @property (nonatomic, strong) id pipelineState; @property (nonatomic, strong) id commandQueue; @property (nonatomic, strong) id texture; @property (nonatomic, strong) id vertices; @property (nonatomic, assign) NSUInteger numVertices; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 初始化 MTKView self.mtkView = [[MTKView alloc] initWithFrame:self.view.bounds]; self.mtkView.device = MTLCreateSystemDefaultDevice(); // 获取默认的device self.view = self.mtkView; self.mtkView.delegate = self; self.viewportSize = (vector_uint2){self.mtkView.drawableSize.width, self.mtkView.drawableSize.height}; [self customInit]; } - (void)customInit { [self setupPipeline]; [self setupVertex]; [self setupTexture]; } // 设置渲染管道 -(void)setupPipeline { id defaultLibrary = [self.mtkView.device newDefaultLibrary]; // .metal id vertexFunction = [defaultLibrary newFunctionWithName:@"vertexShader"]; // 顶点shader,vertexShader是函数名 id fragmentFunction = [defaultLibrary newFunctionWithName:@"samplingShader"]; // 片元shader,samplingShader是函数名 MTLRenderPipelineDescriptor *pipelineStateDescriptor = [[MTLRenderPipelineDescriptor alloc] init]; pipelineStateDescriptor.vertexFunction = vertexFunction; pipelineStateDescriptor.fragmentFunction = fragmentFunction; pipelineStateDescriptor.colorAttachments[0].pixelFormat = self.mtkView.colorPixelFormat; self.pipelineState = [self.mtkView.device newRenderPipelineStateWithDescriptor:pipelineStateDescriptor error:NULL]; // 创建图形渲染管道,耗性能操作不宜频繁调用 self.commandQueue = [self.mtkView.device newCommandQueue]; // CommandQueue是渲染指令队列,保证渲染指令有序地提交到GPU } - (void)setupVertex { static const LYVertex quadVertices[] = { // 顶点坐标,分别是x、y、z、w; 纹理坐标,x、y; { { 0.5, -0.5, 0.0, 1.0 }, { 1.f, 1.f } }, { { -0.5, -0.5, 0.0, 1.0 }, { 0.f, 1.f } }, { { -0.5, 0.5, 0.0, 1.0 }, { 0.f, 0.f } }, { { 0.5, -0.5, 0.0, 1.0 }, { 1.f, 1.f } }, { { -0.5, 0.5, 0.0, 1.0 }, { 0.f, 0.f } }, { { 0.5, 0.5, 0.0, 1.0 }, { 1.f, 0.f } }, }; self.vertices = [self.mtkView.device newBufferWithBytes:quadVertices length:sizeof(quadVertices) options:MTLResourceStorageModeShared]; // 创建顶点缓存 self.numVertices = sizeof(quadVertices) / sizeof(LYVertex); // 顶点个数 } - (void)setupTexture { UIImage *image = [UIImage imageNamed:@"abc"]; // 纹理描述符 MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init]; textureDescriptor.pixelFormat = MTLPixelFormatRGBA8Unorm; textureDescriptor.width = image.size.width; textureDescriptor.height = image.size.height; self.texture = [self.mtkView.device newTextureWithDescriptor:textureDescriptor]; // 创建纹理 MTLRegion region = {{ 0, 0, 0 }, {image.size.width, image.size.height, 1}}; // 纹理上传的范围 Byte *imageBytes = [self loadImage:image]; if (imageBytes) { // UIImage的数据需要转成二进制才能上传,且不用jpg、png的NSData [self.texture replaceRegion:region mipmapLevel:0 withBytes:imageBytes bytesPerRow:4 * image.size.width]; free(imageBytes); // 需要释放资源 imageBytes = NULL; } } - (Byte *)loadImage:(UIImage *)image { // 1获取图片的CGImageRef CGImageRef spriteImage = image.CGImage; // 2 读取图片的大小 size_t width = CGImageGetWidth(spriteImage); size_t height = CGImageGetHeight(spriteImage); Byte * spriteData = (Byte *) calloc(width * height * 4, sizeof(Byte)); //rgba共4个byte CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast); // 3在CGContextRef上绘图 CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage); CGContextRelease(spriteContext); return spriteData; } #pragma mark - delegate - (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size { self.viewportSize = (vector_uint2){size.width, size.height}; } - (void)drawInMTKView:(MTKView *)view { // 每次渲染都要单独创建一个CommandBuffer id commandBuffer = [self.commandQueue commandBuffer]; MTLRenderPassDescriptor *renderPassDescriptor = view.currentRenderPassDescriptor; // MTLRenderPassDescriptor描述一系列attachments的值,类似GL的FrameBuffer;同时也用来创建MTLRenderCommandEncoder if(renderPassDescriptor != nil) { renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.0, 0.5, 0.5, 1.0f); // 设置默认颜色 id renderEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor]; //编码绘制指令的Encoder [renderEncoder setViewport:(MTLViewport){0.0, 0.0, self.viewportSize.x, self.viewportSize.y, -1.0, 1.0 }]; // 设置显示区域 [renderEncoder setRenderPipelineState:self.pipelineState]; // 设置渲染管道,以保证顶点和片元两个shader会被调用 [renderEncoder setVertexBuffer:self.vertices offset:0 atIndex:1]; // 设置顶点缓存 [renderEncoder setFragmentTexture:self.texture atIndex:0]; // 设置纹理 [renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:self.numVertices]; // 绘制 [renderEncoder endEncoding]; // 结束 [commandBuffer presentDrawable:view.currentDrawable]; // 显示 } [commandBuffer commit]; // 提交; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
#include#import "LYShaderTypes.h" using namespace metal; typedef struct { float4 clipSpacePosition [[position]]; // position的修饰符表示这个是顶点 float2 textureCoordinate; // 纹理坐标,会做插值处理 } RasterizerData; vertex RasterizerData // 返回给片元着色器的结构体 vertexShader(uint vertexID [[ vertex_id ]], // vertex_id是顶点shader每次处理的index,用于定位当前的顶点 constant LYVertex *vertexArray [[ buffer(1) ]]) { // buffer表明是缓存数据,0是索引 RasterizerData out; out.clipSpacePosition = vertexArray[vertexID].position; out.textureCoordinate = vertexArray[vertexID].textureCoordinate; return out; } fragment float4 samplingShader(RasterizerData input [[stage_in]], // stage_in表示这个数据来自光栅化。(光栅化是顶点处理之后的步骤,业务层无法修改) texture2d colorTexture [[ texture(0) ]]) // texture表明是纹理数据,0是索引 { constexpr sampler textureSampler (mag_filter::linear, min_filter::linear); // sampler是采样器 half4 colorSample = colorTexture.sample(textureSampler, input.textureCoordinate); // 得到纹理对应位置的颜色 return float4(colorSample); }
#ifndef LYShaderTypes_h #define LYShaderTypes_h typedef struct { vector_float4 position; vector_float2 textureCoordinate; } LYVertex; #endif /* LYShaderTypes_h */
如上的代码则是抄自https://www.jianshu.com/p/cddf73c6c05e
要看更具体的分析可以到该链接看