Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Poking Metal with s̶t̶i̶c̶k̶ swift

uaMobiTech
November 14, 2015

Poking Metal with s̶t̶i̶c̶k̶ swift

By Andrew Khacrhyshyn - https://www.facebook.com/andrew.kharchyshyn
Lightning intro to 3D graphics. Metal API overview. Going through all required steps to get 3D object rendered to the screen. Discussing how Metal works (and doesn’t work) with Swift.

uaMobiTech

November 14, 2015
Tweet

More Decks by uaMobiTech

Other Decks in Programming

Transcript

  1. About myself: • iOS Dev • RayWenderlich.com tutorial team member

    • One of WWDC15 scholarship winners • Hackathon enthusiast • Student @haawa799
  2. 3D Graphics basics Rendering steps: 1. Set scene geometry 2.

    Apply transformations: Translate Rotate
  3. 3D Graphics basics Rendering steps: 1. Set scene geometry 2.

    Apply transformations: Translate Rotate Scale
  4. 3D Graphics basics Rendering steps: 1. Set scene geometry 2.

    Apply transformations 3. Map textures
  5. 3D Graphics basics Rendering steps: 1. Set scene geometry 2.

    Apply transformations 3. Map textures 4. Add lighting
  6. GPU

  7. Shaders vertex VertexOut vertexShader(const Vertex vertexIn [[stage_in]], constant Uniforms &uniformBuffer

    [[buffer(1)]], unsigned int vid [[vertex_id]]) { float4x4 projectionMatrix = uniformBuffer.projectionMatrix; float4x4 modelViewMatrix = uniformBuffer.modelViewMatrix; float3 position = vertexIn.position; float4 fragmentPosition = float4(position, 1.0); VertexOut vertexOut; vertexOut.position = projectionMatrix * modelViewMatrix * fragmentPosition; vertexOut.occlusion = vertexIn.occlusion; vertexOut.texCoords = vertexIn.texCoords; vertexOut.color = float4(1.0,1.0,1.0,1.0); vertexOut.occlusionEnabled = uniformBuffer.occlusionEnabled; vertexOut.textureEnabled = uniformBuffer.textureEnabled; return vertexOut; }
  8. Shaders fragment float4 fragmentShader(VertexOut interpolated [[stage_in]], texture2d<float> tex2D [[texture(0)]], sampler

    sampler2D [[sampler(0)]]) { float4 finalColor = interpolated.color; if (interpolated.occlusionEnabled) { finalColor *= interpolated.occlusion; } if (interpolated.textureEnabled) { finalColor *= tex2D.sample(sampler2D, interpolated.texCoords); } return finalColor; }
  9. Metal Render Pass Descriptor guard let renderPassDescriptor = view.currentRenderPassDescriptor else

    {return} renderPassDescriptor.colorAttachments[0].loadAction = .Clear renderPassDescriptor.colorAttachments[0].clearColor = MTLClearColor(red: 1.0, green: 0.4, blue: 0.6, alpha: 1.0) renderPassDescriptor.colorAttachments[0].storeAction = MTLStoreAction.Store Frame buffer associated with MTKView
  10. Metal Command queue let commandQ = device.newCommandQueue() command buffer command

    buffer command buffer command buffer command buffer command buffer GPU
  11. Metal Command buffer let commandBuffer = commandQ.commandBuffer() command buffer command

    buffer command buffer command buffer command buffer command buffer GPU
  12. More resources on Metal: • RayWenderlich.com • metalbyexample.com • flexmonkey.blogspot.com

    • WWDC 14 videos Q&A @haawa799 Sample code @ https://github.com/haawa799/ RamOnMetal