|
| 1 | +// StructureKit - A collection of extension utilities for Structure SDK |
| 2 | +// Copyright 2022 XRPro, LLC. All rights reserved. |
| 3 | +// http://structure.io |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// modification, are permitted provided that the following conditions are met: |
| 7 | +// |
| 8 | +// * Redistributions of source code must retain the above copyright notice, |
| 9 | +// this list of conditions and the following disclaimer. |
| 10 | +// * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +// this list of conditions and the following disclaimer in the documentation |
| 12 | +// and/or other materials provided with the distribution. |
| 13 | +// * Neither the name of XRPro, LLC nor the names of its contributors may be |
| 14 | +// used to endorse or promote products derived from this software without |
| 15 | +// specific prior written permission. |
| 16 | +// |
| 17 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 22 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | +// POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +import Metal |
| 30 | +import MetalKit |
| 31 | +import StructureKitCTypes |
| 32 | + |
| 33 | +public class STKDepthBandOverlayRenderer { |
| 34 | + private var mtkView: MTKView |
| 35 | + private var renderDepthState: MTLRenderPipelineState |
| 36 | + private var textureDepth: MTLTexture? |
| 37 | + private var vertexBuffer: MTLBuffer |
| 38 | + private var samplerState: MTLSamplerState |
| 39 | + private var device: MTLDevice |
| 40 | + private var intr: STKIntrinsics? |
| 41 | + |
| 42 | + private var validRangeMinMM: Float = 200.0 |
| 43 | + private var validRangeMaxMM: Float = 400.0 |
| 44 | + private var validRangeColor: simd_float4 = simd_float4(0,1,0,0.5) // Green |
| 45 | + private var outOfRangeColor: simd_float4 = simd_float4(1,0,0,0.5) // Red |
| 46 | + private var feather: Float = 40.0 // in mm |
| 47 | + |
| 48 | + public init(view: MTKView, device: MTLDevice) { |
| 49 | + mtkView = view |
| 50 | + self.device = device |
| 51 | + |
| 52 | + let library = STKMetalLibLoader.load(device: device) |
| 53 | + let pipelineDepthDescriptor = MTLRenderPipelineDescriptor() |
| 54 | + pipelineDepthDescriptor.sampleCount = 1 |
| 55 | + pipelineDepthDescriptor.colorAttachments[0].pixelFormat = mtkView.colorPixelFormat |
| 56 | + pipelineDepthDescriptor.depthAttachmentPixelFormat = mtkView.depthStencilPixelFormat |
| 57 | + pipelineDepthDescriptor.vertexFunction = library.makeFunction(name: "vertexDepthBandOverlay") |
| 58 | + pipelineDepthDescriptor.fragmentFunction = library.makeFunction(name: "fragmentDepthBandOverlay") |
| 59 | + // blending |
| 60 | + pipelineDepthDescriptor.colorAttachments[0].isBlendingEnabled = true |
| 61 | + pipelineDepthDescriptor.colorAttachments[0].rgbBlendOperation = .add |
| 62 | + pipelineDepthDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha |
| 63 | + pipelineDepthDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha |
| 64 | + |
| 65 | + renderDepthState = try! device.makeRenderPipelineState(descriptor: pipelineDepthDescriptor) |
| 66 | + |
| 67 | + let vertexData = makeRectangularVertices() |
| 68 | + let dataSize = vertexData.count * MemoryLayout.size(ofValue: vertexData[0]) |
| 69 | + vertexBuffer = device.makeBuffer(bytes: vertexData, length: dataSize, options: [])! |
| 70 | + |
| 71 | + samplerState = makeDefaultSampler(device) |
| 72 | + } |
| 73 | + |
| 74 | + public func uploadColorTextureFromDepth(_ depthFrame: STKDepthFrame) { |
| 75 | + if let texture = textureDepth { |
| 76 | + if texture.width != depthFrame.width || texture.height != depthFrame.height { |
| 77 | + textureDepth = nil // invalidate texture |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if textureDepth == nil { |
| 82 | + let textureDescriptor = MTLTextureDescriptor.texture2DDescriptor( |
| 83 | + pixelFormat: .r32Float, width: Int(depthFrame.width), height: Int(depthFrame.height), mipmapped: false) |
| 84 | + textureDescriptor.usage = MTLTextureUsage( |
| 85 | + rawValue: MTLTextureUsage.renderTarget.rawValue | MTLTextureUsage.shaderRead.rawValue) |
| 86 | + textureDepth = device.makeTexture(descriptor: textureDescriptor) |
| 87 | + } |
| 88 | + |
| 89 | + intr = depthFrame.intrinsics() |
| 90 | + let depthMap = depthFrame.depthInMillimeters |
| 91 | + assert(MemoryLayout<Float32>.size == MemoryLayout<Float>.size) |
| 92 | + |
| 93 | + let bytesPerRow: Int = Int(depthFrame.width) * MemoryLayout<Float32>.stride |
| 94 | + let region = MTLRegionMake2D(0, 0, Int(depthFrame.width), Int(depthFrame.height)) |
| 95 | + textureDepth?.replace(region: region, mipmapLevel: 0, withBytes: depthMap!, bytesPerRow: bytesPerRow) |
| 96 | + } |
| 97 | + |
| 98 | + public func renderDepthOverlay( |
| 99 | + _ commandEncoder: MTLRenderCommandEncoder, |
| 100 | + volumeSizeInMeters: simd_float3, |
| 101 | + cameraPosition: float4x4, |
| 102 | + textureOrientation: float4x4, |
| 103 | + alpha: Float |
| 104 | + ) { |
| 105 | + guard let texture = textureDepth, |
| 106 | + let intr = intr |
| 107 | + else { return } |
| 108 | + |
| 109 | + commandEncoder.pushDebugGroup("RenderDepthRangeOverlay") |
| 110 | + commandEncoder.setRenderPipelineState(renderDepthState) |
| 111 | + |
| 112 | + // rotate and mirror: |
| 113 | + // move to [-0.5, 0.5] coordinates |
| 114 | + // rotate |
| 115 | + // mirror and apply scale to fix the ratio |
| 116 | + // move back to [0, 1] coordinates |
| 117 | + let projection = |
| 118 | + float4x4.makeTranslation(0.5, 0.5, 0) * textureOrientation * float4x4.makeTranslation(-0.5, -0.5, 0) |
| 119 | + |
| 120 | + let intrinsics = STKIntrinsicsMetal( |
| 121 | + cx: intr.cx, cy: intr.cy, fx: intr.fx, fy: intr.fy, width: UInt32(texture.width), height: UInt32(texture.height)) |
| 122 | + |
| 123 | + var uniforms = STKUniformsDepthBandOverlay( |
| 124 | + projection: projection, |
| 125 | + cameraPose: cameraPosition, |
| 126 | + cameraIntrinsics: intrinsics, |
| 127 | + cubeModelInv: float4x4.makeScale(volumeSizeInMeters.x, volumeSizeInMeters.y, volumeSizeInMeters.z).inverse, |
| 128 | + alpha: alpha, |
| 129 | + validRangeMinMM: validRangeMinMM, |
| 130 | + validRangeMaxMM: validRangeMaxMM, |
| 131 | + validRangeColor: validRangeColor, |
| 132 | + outOfRangeColor: outOfRangeColor, |
| 133 | + feather: feather |
| 134 | + ) |
| 135 | + |
| 136 | + commandEncoder.setVertexBuffer(vertexBuffer, offset: 0, index: 0) |
| 137 | + commandEncoder.setVertexBytes(&uniforms, length: MemoryLayout<STKUniformsDepthBandOverlay>.stride, index: 1) |
| 138 | + |
| 139 | + commandEncoder.setFragmentBytes(&uniforms, length: MemoryLayout<STKUniformsDepthBandOverlay>.stride, index: 0) |
| 140 | + commandEncoder.setFragmentTexture(texture, index: 0) // [[ texture(0) ]], |
| 141 | + commandEncoder.setFragmentSamplerState(samplerState, index: 0) // [[ sampler(0) ]] |
| 142 | + commandEncoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6, instanceCount: 1) |
| 143 | + commandEncoder.popDebugGroup() |
| 144 | + } |
| 145 | + |
| 146 | + public func configure( |
| 147 | + validRangeMinMM: Float, validRangeMaxMM: Float, |
| 148 | + validRangeColor: simd_float4 = simd_float4(0,1,0,0.5), |
| 149 | + outOfRangeColor: simd_float4 = simd_float4(1,0,0,0.5), |
| 150 | + feather: Float = 40.0 |
| 151 | + ) { |
| 152 | + self.validRangeMinMM = validRangeMinMM |
| 153 | + self.validRangeMaxMM = validRangeMaxMM |
| 154 | + self.validRangeColor = validRangeColor |
| 155 | + self.outOfRangeColor = outOfRangeColor |
| 156 | + self.feather = feather |
| 157 | + } |
| 158 | +} |
0 commit comments