Using a Render Pipeline to Render Primitives
使用渲染管道去渲染一个几何体
Render a simple 2D triangle. 渲染一个简单的三角形
需要下载示例代码的请搜索上传文件。
文件名:“Using a Render Pipeline to Render Primitives .zip”
Overview
In Using Metal to Draw a View’s Contents用metal 绘制视图内容一文中, you learned how to set up an MTKView
object and to change the view’s contents using a render pass.
That sample simply erased the view’s contents to a background color. 那个例子简单的擦除掉视图内容并填充了一个背景色。
This sample shows you how to configure a render pipeline and use it as part of the render pass to draw a simple 2D colored triangle into the view.
本立展示如何定义一个渲染管道(管道就是渲染流程的意思,把数据送到一个入口,然后通过已经定义好的渲染管道,加工成图形显示数据后在传出。记住,渲染管道就是渲染流程即可,为了和英文统一,以后统一采用渲染管道),然后用选它来绘制一个简单的2D彩色三角形。
The sample supplies a position and color for each vertex顶点, and the render pipeline uses that data to render the triangle, interpolating内插 color values between the colors specified for the triangle’s vertices.
The Xcode project contains schemes for running the sample on macOS, iOS, and tvOS.
Understand the Metal Render Pipeline 了解Metal渲染管道
注:关于Metal中RenderPipeline的相关术语,并没有严格的规定,希望大家在学习中尽可能使用英文术语,但要明白其中的含义。
A render pipeline processes加工处理 drawing commands and writes data into a render pass’s targets.
A render pipeline has many stages阶段, some programmed using shaders and others with fixed or configurable behavior.这些阶段一些用“着色器”编程,另外一些是不可修改的或行为定义。
This sample focuses on the three main stages of the pipeline: the vertex stage, the rasterization stage, and the fragment stage.
The vertex stage and fragment stage are programmable, so you write functions for them in Metal Shading Language (MSL).
The rasterization stage has fixed固定的 behavior.
Figure 1 Main stages of the Metal graphics render pipeline图形渲染管道的主要阶段(步骤)
Rendering starts with a drawing command, which includes a vertex count and what kind of primitive to render. For example, here’s the drawing command from this sample:
// Draw the triangle.
[renderEncoder drawPrimitives:MTLPrimitiveTypeTriangle
vertexStart:0
vertexCount:3];
The vertex stage provides data for each vertex. When enough vertices have been processed, the render pipeline rasterizes the primitive, determining判断 which pixels像素点 in the render targets lie within the boundaries of the primitive. The fragment stage determines the values to write into the render targets for those pixels.
In the rest of this sample, you will see how to write the vertex and fragment functions, how to create the render pipeline state object, and finally, how to encode a draw command that uses this pipeline.
Decide How Data is Processed by Your Custom Render Pipeline
A vertex function generates data for a single vertex and a fragment function generates data for a single fragment, but you decide how they work.
You configure the stages of the pipeline with a goal in mind, meaning that you know what you want the pipeline to generate and how it generates those results.
Decide what data to pass into your render pipeline and what data is passed to later stages of the pipeline. There are typically three places where you do this:
- The inputs to the pipeline, which are provided by your app and passed to the vertex stage.
- The outputs of the vertex stage, which is passed to the rasterization stage.
- The inputs to the fragment stage, which are provided by your app or generated by the rasterization stage.
In this sample, the in