IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发工具 -> Using Metal to Draw a View‘s Contents用Metal绘制视图内容 -> 正文阅读

[开发工具]Using Metal to Draw a View‘s Contents用Metal绘制视图内容

Using Metal to Draw a View’s Contents

时间:2021-9-25
分类:Xcode,Metal开发,
针对人群:初学者,具备初级英语能力
来源:XCode帮助文件
Create a MetalKit view and a render pass to draw the view’s contents.
创建一个MetalKit视图和一个渲染通道来绘制视图内容。

Overview

In this sample, you’ll learn the basics of rendering graphics content with Metal.
You’ll use the MetalKit framework框架 to create a view that uses Metal to draw the contents of the view. Then, you’ll encode commands for a render pass that erases the view to a background color.
在这个例子中,你将学习用Metal渲染图形内容的基本操作。你会用MetalKit框架来创建一个视图,用Metal来绘制视图的内容。然后,你编码命令给一个渲染通道来“擦除”掉视图的背景颜色。

  • Note: MetalKit automates windowing system tasks, loads textures, and handles 3D model data. See [MetalKit][MetalKit] for more information.

Prepare a MetalKit View to Draw

MetalKit provides a class called [MTKView][MTKView], which is a subclass of [NSView][NSView] (in macOS) or [UIView][UIView] (in iOS and tvOS).
MTKView handles many of the details related相关联的细节 to getting the content you draw with Metal onto the screen.

An MTKView needs a reference引用(就是 C++中的指针) to a Metal device object in order to create resources internally, so your first step is to set the view’s device property to an existing [MTLDevice][MTLDevice].

_view.device = MTLCreateSystemDefaultDevice();

Other properties属性 on MTKView allow you to control its behavior行为. To erase the contents of the view to a solid background color, you set its clearColor property. You create the color using the MTLClearColorMake function, specifying the red, green, blue, and alpha透明度 values.

_view.clearColor = MTLClearColorMake(0.0, 0.5, 1.0, 1.0);

Because you won’t be drawing animated content in this sample, configure the view so that it only draws when the contents need to be updated, such as when the view changes shape:
因为在这里例子中,你不会绘制动画内容,所以只要设置视图在内容需要更新的时候重新绘制,例如当视图大小改变时:

_view.enableSetNeedsDisplay = YES;

Delegate Drawing Responsibilities代理绘图职责

MTKView relies依赖 on your app to issue发给 commands to Metal to produce生成 visual视觉 content.
MTKView uses the delegate pattern代理模式 to inform通知 your app when it should draw.
To receive delegate callbacks回调, set the view’s delegate property to an object that conforms符合 to the [MTKViewDelegate][MTKViewDelegate] protocol.

_view.delegate = _renderer;

设置了视图的代理后,就可以在代理对象中来通知程序绘制窗口。
The delegate implements实现 two methods方法:

  • The view calls the mtkView:drawableSizeWillChange: method whenever the size of the contents changes.
    This happens when the window containing the view is resized, or when the device orientation changes (on iOS).
    This allows your app to adapt适应 the resolution at which it renders to the size of the view.

  • The view calls the drawInMTKView: method whenever每当 it’s time to update the view’s contents.
    In this method, you create a command buffer, encode commands that tell the GPU what to draw and when to display it onscreen, and enqueue排列 that command buffer to be executed by the GPU. This is sometimes referred to as drawing a frame. You can think of a frame as all of the work that goes into producing a single image that gets displayed on the screen. In an interactive交互式 app, like a game, you might draw many frames per second.

In this sample, a class called AAPLRenderer implements the delegate methods and takes on the responsibility of drawing.本例中,一个称为AAPLRenderer的类实现了代理方法,并承担绘图响应。
The view controller creates an instance of this class and sets it as the view’s delegate.

Create a Render Pass Descriptor建立一个渲染通道描述符

When you draw, the GPU stores the results into textures, which are blocks of memory that contain image data and are accessible to the GPU. In this sample, the MTKView creates all of the textures you need to draw into the view. It creates multiple textures so that it can display the contents of one texture while you render into another.

To draw, you create a render pass渲染通道, which is a sequence序列 of rendering commands that draw into a set of textures. When used in a render pass, textures are also called render targets. To create a render pass, you need a render pass descriptor, an instance of [MTLRenderPassDescriptor][MTLRenderPassDescriptor]. In this sample, rather than configuring your own render pass descriptor, ask the MetalKit view to create one for you.在本例中,相比你自己定义一个渲染通道描述符,更好的方法是让MetalKit视图来建立一个。

MTLRenderPassDescriptor *renderPassDescriptor = view.currentRenderPassDescriptor;
if (renderPassDescriptor == nil)
{
    return;
}

A render pass descriptor 描述了 the set of render targets, and how they should be processed at the start and end of the render pass. Render passes also define some other aspects面貌 of rendering that are not part of this sample. The view returns a render pass descriptor with a single color attachment附着 that points to one of the view’s textures, and otherwise configures the render pass based on the view’s properties. By default, this means that at the start of the render pass, the render target is erased to a solid color that matches the view’s clearColor property, and at the end of the render pass, all changes are stored back to the texture.

Because a view’s render pass descriptor might be nil, you should test to make sure the render pass descriptor object is non-nil before creating the render pass.

Create a Render Pass 建立一个渲染通道

You create the render pass by encoding it into the command buffer using a [MTLRenderCommandEncoder] object. Call the command buffer’s [renderCommandEncoderWithDescriptor:] method and pass in the render pass descriptor.
通过渲染命令编码器对象,调用命令缓冲的renderCommandEncoderWithDescriptor方法来传送进渲染通道描述符。

id<MTLRenderCommandEncoder> commandEncoder = [commandBuffer renderCommandEncoderWithDescriptor:renderPassDescriptor];

In this sample, you don’t encode编码 any drawing commands, so the only thing the render pass does is erase the texture. Call the encoder’s endEncoding method to indicate表明 that the pass is complete.

[commandEncoder endEncoding];

Present a Drawable to the Screen 准备一个“可绘制”到屏幕

Drawing to a texture doesn’t automatically display the new contents onscreen. In fact, only some textures can be presented onscreen. In Metal, textures that can be displayed onscreen are managed by drawable objects, and to display the content, you present the drawable. 在Metal中,在屏幕能被显示的贴图由drawable对象管理、显示内容,你(负责)准备这个可绘制。

MTKView automatically creates drawable objects to manage its textures. Read the currentDrawable property to get the drawable that owns the texture that is the render pass’s target. The view returns a [CAMetalDrawable][CAMetalDrawable] object, an object connected to Core Animation.
MTKView自动创建可绘制对象来管理它的贴图。读取视图的currentDrawable属性以获得拥有渲染通道目标纹理的drawable。视图返回一个[CAMetalDrawable]对象——一个连接到Core Animation的对象。

id<MTLDrawable> drawable = view.currentDrawable;

Call the presentDrawable: method on the command buffer, passing in the drawable.

[commandBuffer presentDrawable:drawable];

This method tells Metal that when the command buffer is scheduled for execution, Metal should coordinate协调配合 with Core Animation to display the texture after rendering completes. When Core Animation presents the texture, it becomes the view’s new contents. In this sample, this means that the erased texture becomes the new background for the view. The change happens alongside与…一起 any other visual updates that Core Animation makes for onscreen user interface elements.

Commit the Command Buffer 提交命令缓冲

Now that you’ve issued发布了 all the commands for the frame, commit the command buffer.

[commandBuffer commit];

该程序运行后,仅仅显示一个窗口,如下:
Using Metal to Draw a View's Contents程序运行结果

【结束】Using Metal to Draw a View’s Contents用Metal绘制视图内容

  开发工具 最新文章
Postman接口测试之Mock快速入门
ASCII码空格替换查表_最全ASCII码对照表0-2
如何使用 ssh 建立 socks 代理
Typora配合PicGo阿里云图床配置
SoapUI、Jmeter、Postman三种接口测试工具的
github用相对路径显示图片_GitHub 中 readm
Windows编译g2o及其g2o viewer
解决jupyter notebook无法连接/ jupyter连接
Git恢复到之前版本
VScode常用快捷键
上一篇文章      下一篇文章      查看所有文章
加:2021-09-26 10:23:24  更:2021-09-26 10:23:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/23 9:08:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计