简体   繁体   English

如何在 macOS 上使用 Metal 绘制具有可变端点的线?

[英]How to draw a line with variable endpoints with Metal on macOS?

I need to draw a line (light-source/light-target) with Metal on macOS within a 60 FPS animation while the two endpoints coordinates of the line change on each frame.我需要在 60 FPS animation 的 macOS 上用 Metal 画一条线(光源/光目标),而线的两个端点坐标在每一帧上都发生变化。

Actually I can properly draw a static line with Metal using a MTLBuffer containing just two fixed endpoints, but any transformation I apply on the line matrix affects both the endpoints, not each endpoint independently.实际上,我可以使用仅包含两个固定端点的MTLBuffer用 Metal 正确绘制 static 线,但是我在线矩阵上应用的任何转换都会影响两个端点,而不是每个端点独立。

So how can a vary each of the two endpoints coordinates at each render pass?那么如何在每个渲染过程中改变两个端点坐标中的每一个呢? Is a way to modify the MTLBuffer at each frame?是一种在每一帧修改MTLBuffer的方法吗? Should I pass the two coordinates to the shader through setVertexBytes: then use a different pipeline to draw this line?我是否应该通过setVertexBytes:将两个坐标传递给着色器,然后使用不同的管道来绘制这条线?

I have found the solution.我找到了解决方案。 I modify the line MTLBuffer content at each render pass.我在每个渲染过程中修改行 MTLBuffer 内容。 I just set the 2 endpoints coordinates value then I set the vertexBuffer and draw the line.我只是设置了 2 个端点坐标值,然后设置了 vertexBuffer 并画了线。

Vertex *vertex = mLineVertexBuffer.contents;
vertex[0].position = lightSourcePos3D.xyz;
vertex[1].position = lightTargetPos3D.xyz;
[cmdEncoder setVertexBuffer:mLineVertexBuffer offset:0 atIndex:0];
[cmdEncoder drawPrimitives:MTLPrimitiveTypeLine vertexStart:0 vertexCount:2];

The strange think is that my vertexBuffer is described as immutable while it seems that I can still flawlessly modify it.奇怪的想法是我的 vertexBuffer 被描述为不可变的,而我似乎仍然可以完美地修改它。

pipelineDescriptor.vertexBuffers[0].mutability = MTLMutabilityImmutable;

The strange think is that my vertexBuffer is described as immutable while it seems that I can still flawlessly modify it.奇怪的想法是我的 vertexBuffer 被描述为不可变的,而我似乎仍然可以完美地修改它。

Metal can improve perfomance if you declare that you won't modify a buffer's contents between when you set the buffer in an encoder's argument table and when the associated command buffer finishes executing .如果您声明在编码器的参数表中设置缓冲区和相关命令缓冲区完成执行之间不会修改缓冲区的内容, Metal可以提高性能。 Neither the CPU or GPU can update the buffer during this time interval. CPU 或 GPU 都无法在此时间间隔内更新缓冲区。 For better performance, use immutable buffers whenever possible.为了获得更好的性能,请尽可能使用不可变缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM