diff --git a/21-metal-performance-shaders/README.md b/21-metal-performance-shaders/README.md new file mode 100644 index 0000000..083710c --- /dev/null +++ b/21-metal-performance-shaders/README.md @@ -0,0 +1,55 @@ +# Chapter 21: Metal Performance Shaders + +As I read this book, I summarize what I think is wrong. If you think my comments are wrong then please let me know. We can dicuss more and update your opinion. + +## 3. Secondary rays + +In Renderer, in draw(in:), the code for secondary rays is like this: + +``` +for _ in 0..<3 { + // MARK: generate intersections between rays and model triangles + intersector.intersectionDataType = .distancePrimitiveIndexCoordinates + intersector.encodeIntersection(commandBuffer: commandBuffer, + intersectionType: .nearest, + rayBuffer: rayBuffer, rayBufferOffset: 0, + intersectionBuffer: intersectionBuffer, intersectionBufferOffset: 0, + rayCount: width * height, + accelerationStructure: accelerationStructure) + + // MARK: shading + computeEncoder = commandBuffer.makeComputeCommandEncoder() + computeEncoder?.label = "Shading" + computeEncoder?.setBuffer(uniformBuffer, offset: uniformBufferOffset, index: 0) + computeEncoder?.setBuffer(rayBuffer, offset: 0, index: 1) + computeEncoder?.setBuffer(shadowRayBuffer, offset: 0, index: 2) + computeEncoder?.setBuffer(intersectionBuffer, offset: 0, index: 3) + computeEncoder?.setBuffer(vertexColorBuffer, offset: 0, index: 4) + computeEncoder?.setBuffer(vertexNormalBuffer, offset: 0, index: 5) + computeEncoder?.setBuffer(randomBuffer, offset: randomBufferOffset, index: 6) + computeEncoder?.setTexture(renderTarget, index: 0) + computeEncoder?.setComputePipelineState(shadePipelineState!) + computeEncoder?.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadsPerGroup) + computeEncoder?.endEncoding() + + // MARK: shadows + intersector.label = "Shadow Intersector" + intersector.intersectionDataType = .distance + intersector.encodeIntersection(commandBuffer: commandBuffer, + intersectionType: .any, + rayBuffer: shadowRayBuffer, rayBufferOffset: 0, + intersectionBuffer: intersectionBuffer, intersectionBufferOffset: 0, + rayCount: width * height, + accelerationStructure: accelerationStructure) + + computeEncoder = commandBuffer.makeComputeCommandEncoder() + computeEncoder?.label = "Shadows" + computeEncoder?.setBuffer(uniformBuffer, offset: uniformBufferOffset, index: 0) + computeEncoder?.setBuffer(shadowRayBuffer, offset: 0, index: 1) + computeEncoder?.setBuffer(intersectionBuffer, offset: 0, index: 2) + computeEncoder?.setTexture(renderTarget, index: 0) + computeEncoder?.setComputePipelineState(shadowPipelineState) + computeEncoder?.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadsPerGroup) + computeEncoder?.endEncoding() +} +``` \ No newline at end of file diff --git a/README.md b/README.md index 9d5c65e..d492018 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ You can see color images in this book at [here](https://www.raywenderlich.com/bo * [Chapter 18: Rendering with Rays](https://github.com/daemyung/metal-by-tutorials/tree/main/18-rendering-with-rays) * [Chapter 19: Advanced Shadows](https://github.com/daemyung/metal-by-tutorials/tree/main/19-shadows) * [Chapter 20: Advanced Lighting](https://github.com/daemyung/metal-by-tutorials/tree/main/20-advanced-lighting) + * [Chapter 21: Metal Performance Shaders](https://github.com/daemyung/metal-by-tutorials/tree/main/21-metal-performance-shaders) ## Copyright