简体   繁体   中英

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.

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.

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? Should I pass the two coordinates to the shader through setVertexBytes: then use a different pipeline to draw this line?

I have found the solution. I modify the line MTLBuffer content at each render pass. I just set the 2 endpoints coordinates value then I set the vertexBuffer and draw the line.

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.

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.

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 . Neither the CPU or GPU can update the buffer during this time interval. For better performance, use immutable buffers whenever possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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