Using a Render Pipeline to Render Primitives 使用渲染管道去渲染一个几何体(本例为渲染一个三角形)

本文档详细介绍了如何使用Metal渲染管道来绘制一个2D彩色三角形,涵盖了从理解渲染管道到声明顶点和片段函数,创建渲染管道状态对象,设置视口,编码绘制命令等步骤。示例代码适用于iOS、macOS和tvOS平台。
摘要由CSDN通过智能技术生成

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.

oop4587

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图形渲染管道的主要阶段(步骤)
o o p4587

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值