Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ f45ecf1f3b911a2606feeb22951cfbc88f526832
de6a066f513b111ba8b968d225dce93a99473a38
3df05b9d0167331dc6860201b5bb6970f63613c9
838fce6928f73a29004870cdd8dbe847b28f26c4
e131f1e775b36d892aa05e367dd0d0bea1007ba5
8 changes: 4 additions & 4 deletions ios/graphics/Model/GraphicsFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
import MapCoreSharedModule

class GraphicsFactory: MCGraphicsObjectFactoryInterface {

func createQuadMask(_ is3d: Bool) -> (any MCQuad2dInterface)? {
let shader = ColorShader(shader: .colorShader)
return Quad2d(shader: shader, metalContext: .current)
Expand All @@ -22,7 +22,7 @@ class GraphicsFactory: MCGraphicsObjectFactoryInterface {
let shader = MaskShader()
return Polygon2d(shader: shader, metalContext: .current)
}

func createPolygonMaskTessellated(_ is3d: Bool) -> (any MCPolygon2dInterface)? {
let shader = MaskShader(shader: .maskTessellatedShader)
return Polygon2dTessellated(shader: shader, metalContext: .current)
Expand All @@ -42,7 +42,7 @@ class GraphicsFactory: MCGraphicsObjectFactoryInterface {
guard let shader else { fatalError("No Shader provided") }
return Quad2d(shader: shader, metalContext: .current)
}

func createQuadTessellated(_ shader: MCShaderProgramInterface?) -> MCQuad2dInterface? {
guard let shader else { fatalError("No Shader provided") }
return Quad2dTessellated(shader: shader, metalContext: .current)
Expand All @@ -67,7 +67,7 @@ class GraphicsFactory: MCGraphicsObjectFactoryInterface {
guard let shader else { fatalError("No Shader provided") }
return Polygon2d(shader: shader, metalContext: .current)
}

func createPolygonTessellated(_ shader: MCShaderProgramInterface?) -> MCPolygon2dInterface? {
guard let shader else { fatalError("No Shader provided") }
return Polygon2dTessellated(shader: shader, metalContext: .current)
Expand Down
2 changes: 1 addition & 1 deletion ios/graphics/Model/Polygon/Polygon2d.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ extension Polygon2d: MCMaskingObjectInterface {

extension Polygon2d: MCPolygon2dInterface {
func setSubdivisionFactor(_ factor: Int32) {}

func setVertices(
_ vertices: MCSharedBytes, indices: MCSharedBytes, origin: MCVec3D, is3d: Bool
) {
Expand Down
80 changes: 40 additions & 40 deletions ios/graphics/Model/Polygon/Polygon2dTessellated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ final class Polygon2dTessellated: BaseGraphicsObject, @unchecked Sendable {
private var verticesBuffer: MTLBuffer?
private var indicesBuffer: MTLBuffer?
private var indicesCount: Int = 0

private var tessellationFactorsBuffer: MTLBuffer?
private var originBuffers: MultiBuffer<simd_float4>

private var is3d = false
private var subdivisionFactor: Int32 = 0

Expand All @@ -38,14 +38,14 @@ final class Polygon2dTessellated: BaseGraphicsObject, @unchecked Sendable {
sampler: metalContext.samplerLibrary.value(
Sampler.magLinear.rawValue)!,
label: "Polygon2dTessellated")

let factorH = Half(pow(2, Float(self.subdivisionFactor))).bits

var tessellationFactors = MTLTriangleTessellationFactorsHalf(
edgeTessellationFactor: (factorH, factorH, factorH),
insideTessellationFactor: factorH
)

self.tessellationFactorsBuffer.copyOrCreate(
bytes: &tessellationFactors,
length: MemoryLayout<MTLTriangleTessellationFactorsHalf>.stride,
Expand Down Expand Up @@ -95,7 +95,7 @@ final class Polygon2dTessellated: BaseGraphicsObject, @unchecked Sendable {
encoder.setDepthStencilState(renderPassStencilState)
encoder.setStencilReferenceValue(0b0000_0000)
}

renderMain(
encoder: encoder,
context: context,
Expand All @@ -104,7 +104,7 @@ final class Polygon2dTessellated: BaseGraphicsObject, @unchecked Sendable {
origin: origin,
isScreenSpaceCoords: isScreenSpaceCoords)
}

private func renderMain(
encoder: MTLRenderCommandEncoder,
context: RenderingContext,
Expand All @@ -117,7 +117,7 @@ final class Polygon2dTessellated: BaseGraphicsObject, @unchecked Sendable {
let indicesBuffer,
let tessellationFactorsBuffer
else { return }

shader.setupProgram(context)
shader.preRender(context, isScreenSpaceCoords: isScreenSpaceCoords)

Expand Down Expand Up @@ -146,7 +146,7 @@ final class Polygon2dTessellated: BaseGraphicsObject, @unchecked Sendable {
bufferPointer.pointee.z = Float(originOffset.z - origin.z)
}
encoder.setVertexBuffer(originOffsetBuffer, offset: 0, index: 3)

let originBuffer = originBuffers.getNextBuffer(context)
if let bufferPointer = originBuffer?.contents()
.assumingMemoryBound(
Expand All @@ -159,35 +159,35 @@ final class Polygon2dTessellated: BaseGraphicsObject, @unchecked Sendable {
fatalError()
}
encoder.setVertexBuffer(originBuffer, offset: 0, index: 4)

encoder.setVertexBytes(&self.is3d, length: MemoryLayout<Bool>.stride, index: 5)

encoder.setTessellationFactorBuffer(tessellationFactorsBuffer, offset: 0, instanceStride: 0)

#if HARDWARE_TESSELLATION_WIREFRAME_METAL
let wireframePipeline = MetalContext.current.pipelineLibrary.value(
Pipeline(
type: .polygonTessellatedWireframeShader,
blendMode: (shader as? BaseShader)?.blendMode ?? .NORMAL)
)
if let wireframePipeline {
context.setRenderPipelineStateIfNeeded(wireframePipeline)
}
encoder.setTriangleFillMode(.lines)
encoder.drawIndexedPatches(
numberOfPatchControlPoints: 3,
patchStart: 0,
patchCount: indicesCount / 3,
patchIndexBuffer: nil,
patchIndexBufferOffset: 0,
controlPointIndexBuffer: indicesBuffer,
controlPointIndexBufferOffset: 0,
instanceCount: 1,
baseInstance: 0)
encoder.setTriangleFillMode(.fill)
shader.preRender(context, isScreenSpaceCoords: isScreenSpaceCoords)
let wireframePipeline = MetalContext.current.pipelineLibrary.value(
Pipeline(
type: .polygonTessellatedWireframeShader,
blendMode: (shader as? BaseShader)?.blendMode ?? .NORMAL)
)
if let wireframePipeline {
context.setRenderPipelineStateIfNeeded(wireframePipeline)
}
encoder.setTriangleFillMode(.lines)
encoder.drawIndexedPatches(
numberOfPatchControlPoints: 3,
patchStart: 0,
patchCount: indicesCount / 3,
patchIndexBuffer: nil,
patchIndexBufferOffset: 0,
controlPointIndexBuffer: indicesBuffer,
controlPointIndexBufferOffset: 0,
instanceCount: 1,
baseInstance: 0)
encoder.setTriangleFillMode(.fill)
shader.preRender(context, isScreenSpaceCoords: isScreenSpaceCoords)
#endif

encoder.drawIndexedPatches(
numberOfPatchControlPoints: 3,
patchStart: 0,
Expand Down Expand Up @@ -266,22 +266,22 @@ extension Polygon2dTessellated: MCPolygon2dInterface {
lock.withCritical {
if self.subdivisionFactor != factor {
self.subdivisionFactor = factor
let factorH = Half(pow(2, Float(self.subdivisionFactor))).bits;

let factorH = Half(pow(2, Float(self.subdivisionFactor))).bits

var tessellationFactors = MTLTriangleTessellationFactorsHalf(
edgeTessellationFactor: (factorH, factorH, factorH),
insideTessellationFactor: factorH
);
)

self.tessellationFactorsBuffer.copyOrCreate(
bytes: &tessellationFactors,
length: MemoryLayout<MTLTriangleTessellationFactorsHalf>.stride,
device: device)
}
}
}

func setVertices(
_ vertices: MCSharedBytes, indices: MCSharedBytes, origin: MCVec3D, is3d: Bool
) {
Expand Down
77 changes: 38 additions & 39 deletions ios/graphics/Model/Quad/Quad2dTessellated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import simd

final class Quad2dTessellated: BaseGraphicsObject, @unchecked Sendable {
private var verticesBuffer: MTLBuffer?

private var tessellationFactorsBuffer: MTLBuffer?
private var originBuffers: MultiBuffer<simd_float4>

private var is3d = false
private var subdivisionFactor: Int32 = 0

Expand Down Expand Up @@ -53,15 +53,14 @@ final class Quad2dTessellated: BaseGraphicsObject, @unchecked Sendable {
sampler: metalContext.samplerLibrary.value(
Sampler.magLinear.rawValue)!,
label: label)


let factorH = Half(pow(2, Float(self.subdivisionFactor))).bits;


let factorH = Half(pow(2, Float(self.subdivisionFactor))).bits

var tessellationFactors = MTLQuadTessellationFactorsHalf(
edgeTessellationFactor: (factorH, factorH, factorH, factorH),
insideTessellationFactor: (factorH, factorH)
);
)

self.tessellationFactorsBuffer.copyOrCreate(
bytes: &tessellationFactors,
length: MemoryLayout<MTLQuadTessellationFactorsHalf>.stride,
Expand Down Expand Up @@ -187,7 +186,7 @@ final class Quad2dTessellated: BaseGraphicsObject, @unchecked Sendable {
if let texture {
encoder.setFragmentTexture(texture, index: 0)
}

let originBuffer = originBuffers.getNextBuffer(context)
if let bufferPointer = originBuffer?.contents()
.assumingMemoryBound(
Expand All @@ -200,33 +199,33 @@ final class Quad2dTessellated: BaseGraphicsObject, @unchecked Sendable {
fatalError()
}
encoder.setVertexBuffer(originBuffer, offset: 0, index: 4)

encoder.setVertexBytes(&self.is3d, length: MemoryLayout<Bool>.stride, index: 5)

encoder.setTessellationFactorBuffer(tessellationFactorsBuffer, offset: 0, instanceStride: 0)

#if HARDWARE_TESSELLATION_WIREFRAME_METAL
let wireframePipeline = MetalContext.current.pipelineLibrary.value(
Pipeline(
type: .quadTessellatedWireframeShader,
blendMode: (shader as? BaseShader)?.blendMode ?? .NORMAL)
)
if let wireframePipeline {
context.setRenderPipelineStateIfNeeded(wireframePipeline)
}
encoder.setTriangleFillMode(.lines)
encoder.drawPatches(
numberOfPatchControlPoints: 4,
patchStart: 0,
patchCount: 1,
patchIndexBuffer: nil,
patchIndexBufferOffset: 0,
instanceCount: 1,
baseInstance: 0)
encoder.setTriangleFillMode(.fill)
shader.preRender(context, isScreenSpaceCoords: isScreenSpaceCoords)
let wireframePipeline = MetalContext.current.pipelineLibrary.value(
Pipeline(
type: .quadTessellatedWireframeShader,
blendMode: (shader as? BaseShader)?.blendMode ?? .NORMAL)
)
if let wireframePipeline {
context.setRenderPipelineStateIfNeeded(wireframePipeline)
}
encoder.setTriangleFillMode(.lines)
encoder.drawPatches(
numberOfPatchControlPoints: 4,
patchStart: 0,
patchCount: 1,
patchIndexBuffer: nil,
patchIndexBufferOffset: 0,
instanceCount: 1,
baseInstance: 0)
encoder.setTriangleFillMode(.fill)
shader.preRender(context, isScreenSpaceCoords: isScreenSpaceCoords)
#endif

encoder.drawPatches(
numberOfPatchControlPoints: 4,
patchStart: 0,
Expand Down Expand Up @@ -284,14 +283,14 @@ extension Quad2dTessellated: MCQuad2dInterface {
lock.withCritical {
if self.subdivisionFactor != factor {
self.subdivisionFactor = factor
let factorH = Half(pow(2, Float(self.subdivisionFactor))).bits;

let factorH = Half(pow(2, Float(self.subdivisionFactor))).bits

var tessellationFactors = MTLQuadTessellationFactorsHalf(
edgeTessellationFactor: (factorH, factorH, factorH, factorH),
insideTessellationFactor: (factorH, factorH)
);
)

self.tessellationFactorsBuffer.copyOrCreate(
bytes: &tessellationFactors,
length: MemoryLayout<MTLQuadTessellationFactorsHalf>.stride,
Expand All @@ -317,7 +316,7 @@ extension Quad2dTessellated: MCQuad2dInterface {
return MCVec3D(x: x, y: y, z: 0)
}
}

/*
The quad is made out of 4 vertices as following
B----C
Expand Down Expand Up @@ -352,7 +351,7 @@ extension Quad2dTessellated: MCQuad2dInterface {
textureU: textureCoordinates.xF + textureCoordinates.widthF,
textureV: textureCoordinates.yF + textureCoordinates.heightF), // D
]

lock.withCritical {
self.is3d = is3d
self.originOffset = origin
Expand Down
6 changes: 3 additions & 3 deletions ios/graphics/Model/Vertex3DTessellated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ struct Vertex3DTessellated: Equatable {
let vertexDescriptor = MTLVertexDescriptor()
let bufferIndex = 0
var offset = 0

// Position
vertexDescriptor.attributes[0].bufferIndex = bufferIndex
vertexDescriptor.attributes[0].format = .float4
vertexDescriptor.attributes[0].offset = offset
offset += MemoryLayout<SIMD4<Float>>.stride

// Frame Coord (2D coord used to transform onto unit sphere)
vertexDescriptor.attributes[1].bufferIndex = bufferIndex
vertexDescriptor.attributes[1].format = .float2
vertexDescriptor.attributes[1].offset = offset
offset += MemoryLayout<SIMD2<Float>>.stride

vertexDescriptor.layouts[0].stride = offset
vertexDescriptor.layouts[0].stepRate = 1
vertexDescriptor.layouts[0].stepFunction = .perPatchControlPoint
Expand Down
4 changes: 2 additions & 2 deletions ios/graphics/Model/Vertex3DTextureTessellated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public struct Vertex3DTextureTessellated: Equatable {
vertexDescriptor.attributes[0].format = .float3
vertexDescriptor.attributes[0].offset = offset
offset += MemoryLayout<SIMD3<Float>>.stride

// Frame Coord (2D coord to project onto unit sphere)
vertexDescriptor.attributes[1].bufferIndex = bufferIndex
vertexDescriptor.attributes[1].format = .float2
Expand All @@ -42,7 +42,7 @@ public struct Vertex3DTextureTessellated: Equatable {
vertexDescriptor.attributes[2].format = .float2
vertexDescriptor.attributes[2].offset = offset
offset += MemoryLayout<SIMD2<Float>>.stride

vertexDescriptor.layouts[0].stride = MemoryLayout<Vertex3DTextureTessellated>.stride
vertexDescriptor.layouts[0].stepRate = 1
vertexDescriptor.layouts[0].stepFunction = .perPatchControlPoint
Expand Down
Loading