FlowChart of METAL?

i am new to Metal or game developing. I was studing the documentation but i have yet to see anthing that explains the flow of the game deisgn in METAl.

What i mean is what is the steps or the flow of the code. Wht do i need to import declare. what are the classes i need to instantiate.

For example : when you create a funaction. You write code but when you do . You are thinking of these steps in general.

1. type func

2. give a function name

3. ()

4. parameters ,why how

5. var inside

6. bunch of code

7. return type why when how etc.


you know the flow of the function . so even if you don't know how to write a full program . We can still get an idea.


I don't know what you need and why you need them in METAL. can somone help me with this stupid request.

What i know.

1. import metal

2. create a device var metalLayer = MTLDevice!

thats it. LOL.

3. create a CAMetalLayer! for IOS

4. can give fram etc.

vertex.

and thats it. I can't find anything.

All the documentation doesn't tell anything about the flow.

Does anyone have/ create a FLOWCHRT for METAL? for IOS development

Replies

I guess you realize that this is quite a general question where not all aspects can be discussed in only one answer. Lets try anyway..



Setup:

In the center of the Metal API is the MTLDevice. It represents the acual hardware that is going to compute your graphics functions. The MTLDevice is one of the non-transient Metal-objects. That means after creating a device object you can reuse this object throughout the lifetime of your program. After you created the device, you can create buffers (MTLBuffer), textures(MTLTexture) and pipelines (MTLRenderPipelineState) and command queues (MTLCommandQueue) from the device. These three types of objects can also stay in your devices memory throughout the program's lifetime.


To create textures and pipeline-states you need descriptors that define the textures and pipeline-state's properties so the device knows how to it is going to create the objects. For buffers you don't need to specify a descriptor.


Textures of corse hold the data for your images.

Buffers can hold any other data that might be important for rendering.


For understanding the Pipeline State you might first have a look at what the graphics pipeline is. The pipeline state saves all it needs to to know about how the pipeline should be configured. That includes which MTLFunction shall be used. (By the way you can retrieve your MTLFunction only after creating a MTLLibrary and then get the function from the library)


Note: Once you have created a pipeline state or texture, you cannot change its properties. You can however change a textures pixel values. If you want to change a texutres read/write options or the dimensions, you'll have to change the textures descriptor and create a new texture. Practically that means you should not create a new texture or change its properties on every frame but instead create it in the beginning of your program and reuse them as long as possible.


Additionaly you need to create a MTLCommandQueue. This will queue up all the instructions you want to send to the GPU.


Running instructions on the GPU:


From the initially created MTLCommandQueue you create a MTLCommandBuffer every frame. With the command buffer you can create MTLRenderCommandEncoders and MTLComputeCommandEncoders depending on wether you are going to render an image or want to use Metal for general purpose computations.


Now finally you can use all the textures, buffers and pipeline states that you previously created. You use the render command encoder to set the pipeline (after that the GPU will also know which MTLFunction it will use, since you previously saved the function into the pipeline state) Then you can set all your buffers, and textures. For small objects up to 4K of size you can also use the encoder's setBytes method. The indexes used to set your data needs to correspond to the indexes defined in the shader via [[buffer(<i>)]] and [[texture(<i>)]].


When you're done encoding you call endEncoding on the encoder. After all encoders are done encoding you call commit on your command buffer so that it commits the encoded instructions to the command queue, which will then send that information to the GPU in the order of commited command buffers.


When you use MetalKit's MTLView you can use it's CAMetalLayer to get the layer's drawable which holds a texture. That is the texture you need to draw/render into to actually see anything.


I guess that's it to render one frame. Now repeat creating command buffers, encoders and encode what you need in every frame.



So to extract a flow from that:

INIT

1. get the device

2.1.1 create pipeline state descriptors

2.1.2 create pipeline states from descriptors

2.2.1 create texture descriptors textures

2.2.2 create texture from descriptors

2.3 create buffers

2.4 create command queue


LOOP

3.1 create command buffer from command queue

3.2 create render command encoders from command buffer

3.3 encode pipeline state, textures, buffers and endEncoding when ready.

3.3 commit command buffer to command queue

3.4 dont forget to present the texture that you have rendered into



Best resources for additional information on how to use the metal framework can be found at Warren Moore's website Metal By Example and The Metal Framework (metalkit org). I can highly recommend Moore's talk on Metal he held in 2014 (blog post from January 10. 2015). Not only does he explain Metal in the talk but also lots of the computer graphics basics that you need to know.


I hope I could help a little. But I guess now that you know how everything is connected the work only begins. You'll now need to look into the documentations to know how to initialize your pipelines, textures and everything else.

Try Apple's sample app: Adopting Metal I: A practical approach to your first Metal app