Guest User

Untitled

a guest
May 25th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.96 KB | None | 0 0
  1. override func buildPipeline() {
  2. //Model
  3. let library = device!.newDefaultLibrary()!
  4. let pipelineDescriptor = MTLRenderPipelineDescriptor()
  5.  
  6. buildPipelinForSky(pipelineDescriptor, library: library)
  7. buildPipelineForModel(pipelineDescriptor, library: library)
  8.  
  9. do {
  10. pipelineSky = try device!.newRenderPipelineStateWithDescriptor(pipelineDescriptor)
  11. } catch {
  12. print("error with device.newRenderPipelineStateWithDescriptor")
  13. }
  14.  
  15.  
  16.  
  17. let depthStencilDescriptor = MTLDepthStencilDescriptor()
  18. depthStencilDescriptor.depthCompareFunction = .Less
  19. depthStencilDescriptor.depthWriteEnabled = true
  20. depthStencilState = device!.newDepthStencilStateWithDescriptor(depthStencilDescriptor)
  21. commandQueue = device!.newCommandQueue()
  22.  
  23. }
  24.  
  25. func buildPipelineForModel(pipeLineDesc:MTLRenderPipelineDescriptor, library: MTLLibrary) -> MTLRenderPipelineDescriptor {
  26.  
  27. let vertexFunctionModel = library.newFunctionWithName("vertex_ply")
  28. let fragmentFunctionModel = library.newFunctionWithName("fragment_ply")
  29.  
  30. let vertexDescriptorModel = MTLVertexDescriptor()
  31. vertexDescriptorModel.attributes[0].offset = 0
  32. vertexDescriptorModel.attributes[0].format = .Float4
  33. vertexDescriptorModel.attributes[0].bufferIndex = 0
  34. vertexDescriptorModel.layouts[0].stepFunction = .PerVertex
  35. vertexDescriptorModel.layouts[0].stride = sizeof(Float) * 4
  36.  
  37. pipeLineDesc.vertexFunction = vertexFunctionModel
  38. pipeLineDesc.vertexDescriptor = vertexDescriptorModel
  39. pipeLineDesc.fragmentFunction = fragmentFunctionModel
  40. pipeLineDesc.colorAttachments[0].pixelFormat = .BGRA8Unorm
  41.  
  42. return pipeLineDesc
  43. }
  44.  
  45.  
  46. func buildPipelinForSky(pipeLineDesc:MTLRenderPipelineDescriptor, library: MTLLibrary ) -> MTLRenderPipelineDescriptor{
  47. let vertexFunctionSky = library.newFunctionWithName("vertex_sky")
  48. let fragmentFunctionSky = library.newFunctionWithName("fragment_sky")
  49.  
  50. let vertexDescriptorSky = MTLVertexDescriptor()
  51. vertexDescriptorSky.attributes[0].offset = 0
  52. vertexDescriptorSky.attributes[0].format = .Float4
  53. vertexDescriptorSky.attributes[0].bufferIndex = 0
  54. vertexDescriptorSky.attributes[1].offset = sizeof(Float32) * 4
  55. vertexDescriptorSky.attributes[1].format = .Float4
  56. vertexDescriptorSky.attributes[1].bufferIndex = 0
  57. vertexDescriptorSky.attributes[2].offset = sizeof(Float32) * 8
  58. vertexDescriptorSky.attributes[2].format = .Float2
  59. vertexDescriptorSky.attributes[2].bufferIndex = 0
  60. vertexDescriptorSky.layouts[0].stepFunction = .PerVertex
  61. vertexDescriptorSky.layouts[0].stride = sizeof(Vertex)
  62.  
  63. pipeLineDesc.vertexFunction = vertexFunctionSky
  64. pipeLineDesc.vertexDescriptor = vertexDescriptorSky
  65. pipeLineDesc.fragmentFunction = fragmentFunctionSky
  66. pipeLineDesc.depthAttachmentPixelFormat = .Depth32Float
  67.  
  68. let samplerDescriptorSky = MTLSamplerDescriptor()
  69. samplerDescriptorSky.minFilter = .Nearest
  70. samplerDescriptorSky.magFilter = .Linear
  71. samplerStateSky = device!.newSamplerStateWithDescriptor(samplerDescriptorSky)
  72.  
  73. return pipeLineDesc
  74.  
  75. }
  76.  
  77. override func buildResources() {
  78. // (vertexBuffer, indexBuffer) = SphereGenerator.sphereWithRadius(1, stacks: 30, slices: 30, device: device!)
  79. //Model
  80. (vertexBufferModel,normalBufferModel,colorBufferModel) = PointCloud.model(device!)
  81. uniformBufferModel = device!.newBufferWithLength(sizeof(M4f) * 2, options: .OptionCPUCacheModeDefault)
  82. //Sky
  83. vertexBufferSky = SkySphere.sphere(device!)
  84. uniformBufferSky = device!.newBufferWithLength(sizeof(M4f) * 2, options: .OptionCPUCacheModeDefault)
  85. diffuseTextureSky = self.textureForImage(UIImage(named: "bluemarble")!, device: device!)
  86.  
  87.  
  88. }
  89.  
  90. override func resize() {
  91. //Model
  92. super.resize()
  93. //Sky
  94. let layerSizeSky = metalLayer.drawableSize
  95. let depthTextureDescriptorSky = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(.Depth32Float,
  96. width: Int(layerSizeSky.width),
  97. height: Int(layerSizeSky.height),
  98. mipmapped: false)
  99. depthTextureSky = device!.newTextureWithDescriptor(depthTextureDescriptorSky)
  100. }
  101.  
  102. override func draw() {
  103. dispatch_semaphore_wait(inflightSemaphore, DISPATCH_TIME_FOREVER)
  104.  
  105. //Sky
  106. if let drawable = metalLayer.nextDrawable()
  107. {
  108. var modelMatrixTransSky = M4f()
  109. var modelMatrixRotSky = M4f()
  110. var modelMatrixScaleSky = M4f()
  111.  
  112. modelMatrixTransSky = translate(0, y: 0, z: 0)
  113. modelMatrixRotSky = rotate(90, r: V3f(1,0,0)) * modelMatrixRotSky
  114. modelMatrixScaleSky = scaling(10, y: 10, z: 10)
  115.  
  116. let modelMatrixSky = modelMatrixTransSky * modelMatrixRotSky * modelMatrixScaleSky
  117. var viewMatrixSky = M4f()
  118. viewMatrixSky = myCamera.setLookAt(viewMatrixSky)
  119.  
  120. let modelViewMatrixSky = viewMatrixSky * modelMatrixSky
  121.  
  122. let aspect = Float32(metalLayer.drawableSize.width) / Float32(metalLayer.drawableSize.height)
  123. let kFOVY:Float = 85.0
  124. let projectionMatrix = perspective_fov(kFOVY, aspect: aspect, near: 0.1, far: 180.0)
  125.  
  126. let matricesSky = [projectionMatrix, modelViewMatrixSky]
  127. memcpy(uniformBufferSky.contents(), matricesSky, Int(sizeof(M4f) * 2))
  128.  
  129. let commandBufferSky = commandQueue.commandBuffer()
  130. commandBufferSky.addCompletedHandler{ [weak self] commandBufferSky in
  131. if let strongSelf = self {
  132. dispatch_semaphore_signal(strongSelf.inflightSemaphore)
  133. }
  134. return
  135. }
  136.  
  137. //Model
  138. var modelMatrixTransModel = M4f()
  139. var modelMatrixRotModel = M4f()
  140. var modelMatrixScaleModel = M4f()
  141.  
  142. modelMatrixTransModel = translate(0, y: 0, z: 0)
  143. modelMatrixRotModel = rotate(0, r: V3f(1,0,0))
  144. modelMatrixScaleModel = scaling(10, y: 10, z: 10)
  145.  
  146. let modelMatrixModel = modelMatrixTransModel * modelMatrixRotModel * modelMatrixScaleModel
  147. var viewMatrixModel = M4f()
  148. viewMatrixModel = myCamera.setLookAt(viewMatrixModel)
  149.  
  150. let modelViewMatrixModel = viewMatrixModel * modelMatrixModel
  151.  
  152. let matricesModel = [projectionMatrix, modelViewMatrixModel]
  153. memcpy(uniformBufferModel.contents(), matricesModel, Int(sizeof(M4f) * 2))
  154.  
  155.  
  156. //Sky
  157. let passDescriptor = MTLRenderPassDescriptor()
  158. passDescrForSky(passDescriptor, drawable: drawable)
  159. passDescrForModel(passDescriptor, drawable: drawable)
  160.  
  161. //Sky
  162. let commandEncoderSky = commandBufferSky.renderCommandEncoderWithDescriptor(passDescriptor)
  163.  
  164. commandEncoderSky.setRenderPipelineState(pipelineSky)
  165. commandEncoderSky.setDepthStencilState(depthStencilState)
  166. commandEncoderSky.setFrontFacingWinding(.CounterClockwise)
  167. commandEncoderSky.setCullMode(.Back)
  168. pointCloudDraw(commandEncoderSky)
  169. skyDraw(commandEncoderSky)
  170. commandEncoderSky.endEncoding()
  171. commandBufferSky.presentDrawable(drawable)
  172.  
  173. // bufferIndex matches the current semaphore controled frame index to ensure writing occurs at the correct region in the vertex buffer
  174. bufferIndex = (bufferIndex + 1) % MaxBuffers
  175. commandBufferSky.commit()
  176. }
  177. }
  178.  
  179. func passDescrForModel(passDescriptor: MTLRenderPassDescriptor, drawable: CAMetalDrawable) -> MTLRenderPassDescriptor{
  180. passDescriptor.colorAttachments[0].texture = drawable.texture
  181. passDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1)
  182. passDescriptor.colorAttachments[0].loadAction = .Clear
  183. passDescriptor.colorAttachments[0].storeAction = .Store
  184. return passDescriptor
  185. }
  186.  
  187. func passDescrForSky(passDescriptor: MTLRenderPassDescriptor, drawable: CAMetalDrawable) -> MTLRenderPassDescriptor{
  188. passDescriptor.colorAttachments[0].texture = drawable.texture
  189. passDescriptor.colorAttachments[0].clearColor = MTLClearColorMake(0.5, 0.5, 0.5, 1)
  190. passDescriptor.colorAttachments[0].loadAction = .Clear
  191. passDescriptor.colorAttachments[0].storeAction = .Store
  192.  
  193. passDescriptor.depthAttachment.texture = depthTextureSky
  194. passDescriptor.depthAttachment.clearDepth = 1
  195. passDescriptor.depthAttachment.loadAction = .Clear
  196. passDescriptor.depthAttachment.storeAction = .DontCare
  197.  
  198. return passDescriptor
  199.  
  200. }
  201.  
  202. func pointCloudDraw(commandencodeModel: MTLRenderCommandEncoder) {
  203.  
  204. commandencodeModel.setVertexBuffer(vertexBufferModel, offset:0, atIndex:0)
  205. commandencodeModel.setVertexBuffer(normalBufferModel, offset:0, atIndex:1)
  206. commandencodeModel.setVertexBuffer(colorBufferModel, offset:0, atIndex:2)
  207. commandencodeModel.setVertexBuffer(uniformBufferModel, offset:0, atIndex:3)
  208. commandencodeModel.setFragmentBuffer(uniformBufferModel, offset: 0, atIndex: 0)
  209. commandencodeModel.drawPrimitives(.Point, vertexStart: 0, vertexCount: vertextCountModel)
  210. }
  211.  
  212. func skyDraw(commandencodeSky: MTLRenderCommandEncoder) {
  213.  
  214. commandencodeSky.setVertexBuffer(vertexBufferSky, offset:0, atIndex:0)
  215. commandencodeSky.setVertexBuffer(uniformBufferSky, offset:0, atIndex:1)
  216. commandencodeSky.setFragmentTexture(diffuseTextureSky, atIndex: 0)
  217. commandencodeSky.setFragmentSamplerState(samplerStateSky, atIndex: 0)
  218. commandencodeSky.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: vertexCountSky)
  219. }
Add Comment
Please, Sign In to add comment