 		do {
 			try pipeline = device.makeRenderPipelineState(descriptor: pipelineDescriptor)
 			self.device = device
+			self.renderList = RenderList()	// SelectionRenderer does not need to triple-buffer
 		} catch let error {
 			fatalError(error.localizedDescription)
 		}

import Metal
import simd

struct SelectionUniforms {
	let mvpMatrix: simd_float4x4
	let width: simd_float1
	let color: simd_float4
}

class SelectionRenderer {
	let device: MTLDevice
	let pipeline: MTLRenderPipelineState
	
	var outlinePrimitive: OutlineRenderPrimitive?
	var frameSelectSemaphore = DispatchSemaphore(value: 1)
	
	var outlineWidth: Float

		let countourVertices = tessellation.contours.map({$0.vertices})
		let outlineGeometry: RegionContours = countourVertices.map(thinOutline)
		
		outlinePrimitive = OutlineRenderPrimitive(contours: outlineGeometry,
																							device: device,
																							ownerHash: selectedRegionHash,
																							debugName: "Selection contours")
	}
	
	func clear() {
		outlinePrimitive = nil
	}
	
	func prepareFrame(zoomLevel: Float) {
		
		if let selectionHash = outlinePrimitive?.ownerHash, lodLevel != GeometryStreamer.shared.actualLodLevel {
			updatePrimitive(selectedRegionHash: selectionHash)
			lodLevel = GeometryStreamer.shared.actualLodLevel
		}
		
		frameSelectSemaphore.wait()
			self.outlineWidth = 1.0 / zoomLevel
			self.renderList = outlinePrimitive != nil ? [outlinePrimitive!] : []
		frameSelectSemaphore.signal()
	}
	
	func renderSelection(inProjection projection: simd_float4x4, inEncoder encoder: MTLRenderCommandEncoder) {
		guard !renderList.isEmpty else { return }
	
		encoder.pushDebugGroup("Render outlines")
		encoder.setRenderPipelineState(pipeline)
	
		frameSelectSemaphore.wait()
			var uniforms = FrameUniforms(mvpMatrix: projection, width: self.outlineWidth, color: Color(r: 0.0, g: 0.0, b: 0.0, a: 1.0).vector)
		frameSelectSemaphore.signal()
		
		encoder.setVertexBytes(&uniforms, length: MemoryLayout.stride(ofValue: uniforms), index: 1)
		
		for primitive in renderList {
			render(primitive: primitive, into: encoder)
		}
	
		encoder.popDebugGroup()
	}
