# Using a Render Pipeline to Render Primitives
Render a simple 2D triangle.
## Overview
In [Using Metal to Draw a View’s Contents](https://developer.apple.com/documentation/metal/basic_tasks_and_concepts/using_metal_to_draw_a_view_s_contents), 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.
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.
![Simple 2D Triangle Vertices](Documentation/2DTriangleVertices.png)
The Xcode project contains schemes for running the sample on macOS, iOS, and tvOS.
## Understand the Metal Render Pipeline
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
![Main Stages of the Metal Graphics Render Pipeline](Documentation/SimplePipeline.png)
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:
``` objective-c
// 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 input data for the pipeline is the position of a vertex and its color. To demonstrate the kind of transformation you typically perform in a vertex function, input coordinates are defined in a custom coordinate space, measured in pixels from the center of the view. These coordinates need to be translated into Metal's coordinate system.
Declare an `AAPLVertex` structure, using SIMD vector types to hold the position and color data.
To share a single definition for how the structure is laid out in memory, declare the structure in a common header and import it in both the Metal shader and the app.
``` objective-c
typedef struct
{
vector_float2 position;
vector_float4 color;
} AAPLVertex;
```
SIMD types are commonplace in Metal Shading Language, and you should also use them in your app using the simd library.
SIMD types contain multiple channels of a particular data type, so declaring the position as a `vector_float2` means it contains two 32-bit float values (which will hold the x and y coordinates.)
Colors are stored using a `vector_float4`, so they have four channels – red, green, blue, and alpha.
In the app, the input data is specified using a constant array:
``` objective-c
static const AAPLVertex triangleVertices[] =
{
// 2D positions, RGBA colors
{ { 250, -250 }, { 1, 0, 0, 1 } },
{ { -250, -250 }, { 0, 1, 0, 1 } },
{ { 0, 250 }, { 0, 0, 1, 1 } },
};
```
The vertex stage generates data for a vertex, so it needs to provide a color and a transformed position.
Declare a `RasterizerData` structure containing a position and a color value, again using SIMD types.
``` metal
struct RasterizerData
{
// The [[position]] attribute of this member indicates that this value
// is the clip space position of the vertex when this structure is
// returned from the vertex function.
float4 position [[position]];
// Since this member does not have a special attribute, the rasterizer
// interpolates its value with the values of the other triangle vertices
// and then passes the interpolated value to the fragment shader for each
// fragment in the triangle.
float4 color;
};
```
The output position (described in detail below) must be defined as a `vector_float4`. The color is declared as it was in the input data structure.
You need to tell Metal which field in the rasterization data provides position data, because Metal doesn't enforce any particular naming convention for fields in your struct.
Annotate the `position` field with the `[[position]]` attribute qualifier to declare that this field holds the output position.
The fragment function simply passes the rasterization stage's data to later stages so it doesn't need any additional arguments.
## Declare the Vertex Function
Declare the vertex function, including its input arguments and the data it outputs.
Much like compute functions were declared using the `kernel` keyword, you declare a vertex function using the `vertex` keyword.
``` metal
vertex RasterizerData
vertexShader(uint vertexID [[vertex_id]],
constant AAPLVertex *vertices [[buffer(AAPLVertexInputIndexVertices)]],
constant vector_uint2 *viewportSizePointer [[buffer(AAPLVertexInputIndexViewportSize)]])
```
The first argument, `vertexID`, uses the `[[vertex_id]]` attribute qualifier, which is another Metal keyword.
When you execute a render command, the GPU calls your vertex function multiple times, generating a unique value for each vertex.
The second argument, `vertices`, is an array that contains the vertex data, using the `AAPLVertex` struct previously defined.
To transform the position into Metal's coordinates, the function needs the size of the viewport (in pixels) that the triangle is being drawn into, so this is stored in the `viewportSizePointer` argument.
The second and third arguments have the `[[buffer(n)]]` attribute qualifier.
By default, Metal assigns slots in the argument table for each parameter automatically.
When you add the `[[buffer(n)]]` qualifier to a buffer argument, you tell Metal explicitly which slot to use.
Declaring slots explicitly can make it easier to revise your shaders without also needing to change your app code.
Declare the constants for the two indicies in the shared header file.
The function's output is a `RasterizerData` struct.
## Write the Vertex Function
Your vertex function must generate both fields of the output struct.
Use the `vertexID` argument to index into the `vertices` array and read the input data for the vertex.
Also, retrieve the viewport dimensions.
``` metal
float2 pixelSpacePosition = vertices[vertexID].position.xy;
// Get the viewport siz
没有合适的资源?快使用搜索试试~ 我知道了~
xmake:一个基于Lua的轻量级跨平台自动构建工具
共2000个文件
lua:1073个
c:339个
h:227个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 107 浏览量
2024-06-07
19:32:11
上传
评论
收藏 4.16MB ZIP 举报
温馨提示
xmake是一个开源、轻量级且高效的构建工具,使用Lua脚本语言进行项目配置。它旨在简化构建过程,支持跨平台构建、自动依赖管理和并行编译。xmake不仅可以直接构建源代码,还可以生成项目文件,如Visual Studio、Xcode和Makefile等。 特点 跨平台支持:支持Windows、macOS、Linux等多种操作系统。 多工具集支持:兼容Visual Studio、Xcode、GNU Make、Ninja等构建系统。 灵活的脚本语言:使用Lua脚本定义构建配置,提供了编程语言的灵活性和功能性。 自动依赖管理:内置可靠的依赖分析,自动管理项目依赖关系。 并行构建:支持多任务并行构建,提高构建速度。 模块化设计:支持模块化配置,便于管理大型项目和代码复用。
资源推荐
资源详情
资源评论
收起资源包目录
xmake:一个基于Lua的轻量级跨平台自动构建工具 (2000个子文件)
dsmmain.c 332KB
utils.c 266KB
dispvid.c 217KB
display.c 177KB
intrface.c 174KB
wmi.c 123KB
enum.c 102KB
pnp.c 85KB
ioctl.c 62KB
uvcview.c 55KB
utils.c 51KB
read.c 49KB
isr.c 48KB
modmflow.c 44KB
engine.c 44KB
nonpnp.c 41KB
h264.c 39KB
dispaud.c 34KB
write.c 29KB
curses.c 23KB
openclos.c 22KB
cuda_drvapi_dynlink.c 21KB
install.c 19KB
waitmask.c 16KB
file_read.c 16KB
testapp.c 16KB
queue.c 14KB
registry.c 11KB
immediat.c 10KB
openv.c 10KB
interactive.c 10KB
file_open.c 10KB
cpuinfo.c 9KB
power.c 9KB
kcs.c 9KB
loadxmi.c 9KB
select.c 8KB
devnode.c 8KB
registry_query.c 8KB
open.c 8KB
registry_keys.c 8KB
wmi.c 7KB
meminfo.c 7KB
find.c 7KB
file_write.c 6KB
device.c 6KB
registry_values.c 6KB
driver.c 5KB
iscygpty.c 5KB
xxhash.c 5KB
convert.c 5KB
initunlo.c 5KB
ansi.c 5KB
stdfile.c 5KB
signal.c 4KB
debug.c 4KB
args.c 4KB
gid.c 4KB
uid.c 4KB
qsfile.c 4KB
sha.c 4KB
split.c 4KB
md5.c 4KB
trim.c 4KB
argv.c 4KB
getenvs.c 4KB
purge.c 4KB
xmake.c 3KB
poller_wait.c 3KB
rmdir.c 3KB
socket_sendfile.c 3KB
decompress_file.c 3KB
compress_file.c 3KB
satisfies.c 3KB
decompress.c 3KB
file_close.c 3KB
socket_recvfrom.c 3KB
compress.c 3KB
lastof.c 3KB
socket_sendto.c 3KB
block_decompress.c 3KB
file_flush.c 3KB
getenv.c 3KB
file_seek.c 3KB
block_compress.c 3KB
system_ARMCM3.c 2KB
getwinsize.c 2KB
semver.c 2KB
strerror.c 2KB
short_path.c 2KB
file_rawfd.c 2KB
system_ARMCM3.c 2KB
pipe_read.c 2KB
socket_recv.c 2KB
compress_stream_write.c 2KB
decompress_stream_read.c 2KB
compress_stream_read.c 2KB
compare.c 2KB
socket_connect.c 2KB
term_mode.c 2KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
新华
- 粉丝: 1w+
- 资源: 628
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功