Metal官方文档-Using Metal to Draw a View’s Contents
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Metal官方文档-Using Metal to Draw a View’s Contents相关的知识,希望对你有一定的参考价值。
参考技术A Example In GitHub使用Metal绘制视图内容
Create a MetalKit view and a render pass to draw the view’s contents.
创建MetalKit视图和渲染过程以绘制视图的内容。
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 for more information.
MetalKit自动执行窗口系统任务、加载纹理和处理三维模型数据。有关详细信息,请参阅MetalKit。
准备要绘制的MetalKit视图
MetalKit provides a class called MTKView, which is a subclass of NSView (in macOS) or UIView (in ios and tvOS). MTKView handles many of the details related to getting the content you draw with Metal onto the screen.
MetalKit提供了一个名为MTKView的类,它是NSView(在macOS中)或UIView(在iOS和tvOS中)的子类。MTKView处理许多细节(将用Metal绘制的内容显示在屏幕上有关的)。
An MTKView needs a reference 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.
为了在内部创建资源,MTKView需要引用Metal的设备对象,因此第一步是将视图的device属性设置为现有的MTLDevice。
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.
MTKView上的其他属性允许您控制其行为。将视图内容清除为纯色背景时,需要设置其clearColor属性。使用MTLClearColorMake(::::)函数创建颜色,指定红色、绿色、蓝色和alpha值。
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:
由于您不会在本示例中绘制动画内容,所以配置视图使其仅在需要更新内容时才绘制,例如当视图更改形状时:
设置负责绘制的代理
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 protocol.
MTKView依赖于您的应用程序向Metal发出命令以生成可视内容。MTKView使用委托模式通知app何时应该绘制。要接收回调方法,请将视图的delegate属性设置为符合MTKViewDelegate协议的对象。
The delegate implements two methods:
代理实现两个方法:
In this sample, a class called AAPLRenderer implements the delegate methods and takes on the responsibility of drawing. The view controller creates an instance of this class and sets it as the view’s delegate.
在这个示例中,一个名为AAPLRenderer的类实现了委托方法并承担了绘图的责任。VC创建此类的一个实例,并将其设置为视图的委托。
创建渲染过程描述符
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.
绘制时,GPU将结果存储到纹理中,纹理是包含图像数据的内存块,可供GPU访问。在本示例中,MTKView将创建需要绘制到视图中的所有纹理。它创建多个纹理,以便在渲染到另一个纹理时显示一个纹理的内容。
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. In this sample, rather than configuring your own render pass descriptor, ask the MetalKit view to create one for you.
若要绘制,请创建渲染过程,该过程是绘制到一组纹理中的渲染命令序列。在渲染过程中使用时,纹理也称为渲染目标。若要创建渲染过程,需要一个渲染过程描述符,即MTLRenderPassDescriptor的实例。在本示例中,请使用MetalKit视图为您创建一个,而不是配置自己的渲染过程描述符。
A render pass descriptor describes 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.
渲染过程描述符描述了一组渲染目标,以及在渲染过程开始和结束时应如何处理这些目标。渲染过程还定义了渲染的某些其他方面,这些方面不是此示例的一部分。该视图返回一个渲染过程描述符,该描述符具有指向该视图的某个纹理的单色附加,否则将基于该视图的属性配置渲染过程。默认情况下,这意味着在渲染过程开始时,渲染目标将被清除为与视图的clearColor属性匹配的纯色,在渲染过程结束时,所有更改都存储回纹理。
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.
由于视图的渲染过程描述符可能为nil,因此在创建渲染过程之前,应测试以确保渲染过程描述符对象为非空。
创建渲染过程
You create the render pass by encoding it into the command buffer using a MTLRenderCommandEncoder object. Call the command buffer’s makeRenderCommandEncoder(descriptor:) method and pass in the render pass descriptor.
通过使用MTLRenderCommandEncoder对象将其编码到命令缓冲区来创建渲染过程。调用命令缓冲区的makeRenderCommandEncoder(descriptor:)方法并传入呈现过程描述符。
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.
在本示例中,您不编码任何绘图命令,因此渲染过程只会删除纹理。调用编码器的endEncoding方法以指示该过程已完成。
在屏幕上呈现一幅图画
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中,可以在屏幕上显示的纹理是由可绘制对象管理的,要显示内容,需要显示可绘制对象。
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 object, an object connected to Core Animation.
MTKView会自动创建可绘制对象来管理其纹理。读取currentDrawable属性以获取拥有作为渲染过程目标的纹理的drawable。视图返回一个CAMetalDrawable对象,一个连接到核心动画的对象。
Call the present(_:) method on the command buffer, passing in the drawable.
对命令缓冲区调用present(:)方法,传递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.
此方法告诉Metal,当计划执行命令缓冲区时,Metal应与核心动画协调以在渲染完成后显示纹理。当核心动画呈现纹理时,它将成为视图的新内容。在本示例中,这意味着删除的纹理将成为视图的新背景。这种变化与核心动画为屏幕用户界面元素所做的任何其他视觉更新一起发生。
提交命令缓冲区
Now that you’ve issued all the commands for the frame, commit the command buffer.
现在已经发出了帧的所有命令,提交命令缓冲区。
听力Why Is NASA Sending a Spacecraft to a Metal World?
NB: This may not be a word-for-word transcript.
Why Is NASA Sending a Spacecraft to a Metal World?
Linda T. Elkins-Tanton
Somewhere between the orbits of Mars and Jupiter, about 500 million kilometers away from Earth, floats a metallic orb the size of Massachusetts. That’s no moon...it’s 16 Psyche, one of the most massive asteroids in the solar system. And it is the asteroid our droids are looking for.
We humans have managed to send robotic spacecraft to all sorts of environments in space – the gas clouds of Saturn and Jupiter, the icy wastes of Europa, and the rocky dunes of Mars. But Psyche’s surface isn’t just hard rock – it’s heavy metal. The asteroid mostly consists of nickel and iron, by far, the largest known body with such a composition. But we don’t yet know what it looks like; our best current radar images show a pixelated smudge. That’ll change in 2026, when an unmanned spacecraft sent as part of NASA’s Discovery Program is scheduled to arrive.
So, why is NASA so interested in Psyche? Are we going to mine all that metal, or build a giant space magnet?
Actually, the real reason is right under our feet. The core of the Earth is thought to consist of a solid nickel-iron center with a molten outer layer. But we’re prevented from studying it up close by 2,800 kilometers of solid rock. The deepest we’ve been able to drill is 12 kilometers. Even if we could go further, the pressure at the core is 3 million times higher than at the surface, with a temperature of 5,000 degrees Celsius. Simply put, a journey to the center of the Earth is out of the question for now. So scientists have had to resort to indirect ways of studying the core, like measuring earthquake waves that pass through it, or studying minerals thought to have formed there.
But what if the best way to study Earth’s inner space is by visiting outer space?
After all, we have a pretty good idea of how our planets formed. Dust and gas orbiting our young Sun cooled and collided to form a few thousand miniature bodies we call planetesimals. As these continued to orbit, some combined to grow larger, eventually forming our planets. Others experienced impacts that broke them apart into smaller chunks – the asteroids we see today in the belt between Mars and Jupiter.
What makes Psyche so special is that it appears to have been a planetesimal well on its way to becoming a planet, with a rocky exterior surrounding a metal core. But its progress was cut short by a series of hit-and-run collisions with other planetesimals that knocked off the rocky crust until only the core remained. Experiencing that many destructive collisions with no additive ones in between is statistically very unlikely, making Psyche an amazingly rare opportunity to study an exposed metallic core.
To do that, NASA’s robotic orbiter will be equipped with an array of advanced instruments. A spectrometer will analyze the gamma rays and neutrons produced when Psyche is struck by cosmic rays. Each element in the periodic table releases gamma rays of specific wavelengths, so these measurements will tell us what elements are found on the surface. A magnetometer will measure Psyche’s magnetic field, allowing us to learn more about how Earth’s magnetic field is generated at its core. And of course, an imager will give us a closer look at the surface than ever before.
Visiting a whole new kind of world is exciting enough on its own. But the mission to Psyche gives us a unique chance to discover our own planet’s innermost secrets in an orbit far, far away.
以上是关于Metal官方文档-Using Metal to Draw a View’s Contents的主要内容,如果未能解决你的问题,请参考以下文章
Metal2剖析:Forward+ with Tile Shading