Skip to content

Commit

Permalink
adds striped polygons for iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmermannubique committed Feb 5, 2024
1 parent 7263867 commit 1844d06
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
31 changes: 29 additions & 2 deletions ios/graphics/Model/Polygon/PolygonGroup2d.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class PolygonGroup2d: BaseGraphicsObject {

private var stencilState: MTLDepthStencilState?
private var renderPassStencilState: MTLDepthStencilState?

private var posOffset = SIMD2<Float>([0.0, 0.0])

init(shader: MCShaderProgramInterface, metalContext: MetalContext) {
guard let shader = shader as? PolygonGroupShader else {
Expand All @@ -38,7 +38,7 @@ final class PolygonGroup2d: BaseGraphicsObject {
renderPass pass: MCRenderPassConfig,
mvpMatrix: Int64,
isMasked: Bool,
screenPixelAsRealMeterFactor _: Double) {
screenPixelAsRealMeterFactor: Double) {
lock.lock()
defer {
lock.unlock()
Expand Down Expand Up @@ -79,6 +79,14 @@ final class PolygonGroup2d: BaseGraphicsObject {
encoder.setVertexBytes(matrixPointer, length: 64, index: 1)
}

if self.shader.isStriped {
encoder.setVertexBytes(&posOffset, length: MemoryLayout<SIMD2<Float>>.stride, index: 2)

let p : Float = Float(screenPixelAsRealMeterFactor)
var scaleFactors = SIMD2<Float>([p, pow(2.0, ceil(log2(p)))])
encoder.setFragmentBytes(&scaleFactors, length: MemoryLayout<SIMD2<Float>>.stride, index: 2)
}

encoder.drawIndexedPrimitives(type: .triangle,
indexCount: indicesCount,
indexType: .uint16,
Expand Down Expand Up @@ -107,6 +115,25 @@ extension PolygonGroup2d: MCPolygonGroup2dInterface {
self.indicesCount = Int(indices.elementCount)
self.verticesBuffer = verticesBuffer
self.indicesBuffer = indicesBuffer

if shader.isStriped {
if let p = UnsafeRawPointer(bitPattern: Int(vertices.address)) {
var minX = Float.greatestFiniteMagnitude
var minY = Float.greatestFiniteMagnitude

for i in 0..<vertices.elementCount {
if i % 3 == 0 {
let x = (p + 4 * Int(i)).load(as: Float.self)
let y = (p + 4 * (Int(i) + 1)).load(as: Float.self)
minX = min(x, minX)
minY = min(y, minY)
}
}

self.posOffset.x = minX;
self.posOffset.y = minY;
}
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion ios/graphics/Pipelines/PipelineLibrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public enum PipelineType: String, CaseIterable, Codable {
case alphaInstancedShader
case lineGroupShader
case polygonGroupShader
case polygonStripedGroupShader
case polygonPatternGroupShader
case polygonPatternFadeInGroupShader
case colorShader
Expand All @@ -137,6 +138,7 @@ public enum PipelineType: String, CaseIterable, Codable {
case .alphaInstancedShader: return "Alpha instanced shader with texture"
case .lineGroupShader: return "Line Group shader"
case .polygonGroupShader: return "Polygon Group shader"
case .polygonStripedGroupShader: return "Polygon Group (striped) shader"
case .polygonPatternGroupShader: return "Polygon Group Pattern shader"
case .polygonPatternFadeInGroupShader: return "Polygon Group Pattern (fade in) shader"
case .colorShader: return "Color shader"
Expand All @@ -156,6 +158,7 @@ public enum PipelineType: String, CaseIterable, Codable {
case .alphaInstancedShader: return "alphaInstancedVertexShader"
case .lineGroupShader: return "lineGroupVertexShader"
case .polygonGroupShader: return "polygonGroupVertexShader"
case .polygonStripedGroupShader: return "polygonStripedGroupVertexShader"
case .polygonPatternGroupShader: return "polygonPatternGroupVertexShader"
case .polygonPatternFadeInGroupShader: return "polygonPatternGroupVertexShader"
case .colorShader: return "colorVertexShader"
Expand All @@ -175,6 +178,7 @@ public enum PipelineType: String, CaseIterable, Codable {
case .alphaInstancedShader: return "alphaInstancedFragmentShader"
case .lineGroupShader: return "lineGroupFragmentShader"
case .polygonGroupShader: return "polygonGroupFragmentShader"
case .polygonStripedGroupShader: return "polygonGroupStripedFragmentShader"
case .polygonPatternGroupShader: return "polygonPatternGroupFragmentShader"
case .polygonPatternFadeInGroupShader: return "polygonPatternGroupFadeInFragmentShader"
case .colorShader: return "colorFragmentShader"
Expand All @@ -192,7 +196,7 @@ public enum PipelineType: String, CaseIterable, Codable {
switch self {
case .lineGroupShader:
return LineVertex.descriptor
case .polygonGroupShader, .polygonPatternGroupShader, .polygonPatternFadeInGroupShader:
case .polygonGroupShader, .polygonPatternGroupShader, .polygonPatternFadeInGroupShader, .polygonStripedGroupShader:
return PolygonVertex.descriptor
default:
return Vertex.descriptor
Expand Down
39 changes: 39 additions & 0 deletions ios/graphics/Shader/Metal/PolygonGroupShader.metal
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct PolygonGroupVertexIn {

struct PolygonGroupVertexOut {
float4 position [[ position ]];
float2 uv;
float stylingIndex;
};

Expand All @@ -26,13 +27,20 @@ struct PolygonGroupStyling {
float opacity;
};

struct PolygonGroupStripeStyling {
float color[4];
float opacity;
float stripeInfoX;
float stripeInfoY;
};

vertex PolygonGroupVertexOut
polygonGroupVertexShader(const PolygonGroupVertexIn vertexIn [[stage_in]],
constant float4x4 &mvpMatrix [[buffer(1)]])
{
PolygonGroupVertexOut out {
.position = mvpMatrix * float4(vertexIn.position.xy, 0.0, 1.0),
.uv = float2(0.0, 0.0),
.stylingIndex = vertexIn.stylingIndex,
};

Expand All @@ -53,6 +61,37 @@ struct PolygonPatternGroupVertexOut {
float2 pixelPosition;
};

vertex PolygonGroupVertexOut
polygonStripedGroupVertexShader(const PolygonGroupVertexIn vertexIn [[stage_in]],
constant float4x4 &mvpMatrix [[buffer(1)]],
constant float2 &posOffset [[buffer(2)]])
{
PolygonGroupVertexOut out {
.position = mvpMatrix * float4(vertexIn.position.xy, 0.0, 1.0),
.uv = vertexIn.position.xy - posOffset,
.stylingIndex = vertexIn.stylingIndex,
};

return out;
}


fragment float4
polygonGroupStripedFragmentShader(PolygonGroupVertexOut in [[stage_in]],
constant PolygonGroupStripeStyling *styling [[buffer(1)]],
constant float2 &scaleFactors [[buffer(2)]])
{
PolygonGroupStripeStyling s = styling[int(in.stylingIndex)];

float disPx = ((in.uv.x - 1100000.0) + (in.uv.y + 6000000.0)) / scaleFactors.y;
float totalPx = s.stripeInfoX + s.stripeInfoY;
float adjLineWPx = s.stripeInfoX / scaleFactors.y * scaleFactors.x;
if (fmod(disPx, totalPx) > adjLineWPx) {
return float4(0.0, 1.0, 0.0, 0.0);
}

return float4(s.color[0], s.color[1], s.color[2], 1.0) * s.opacity * s.color[3];
}

vertex PolygonPatternGroupVertexOut
polygonPatternGroupVertexShader(const PolygonGroupVertexIn vertexIn [[stage_in]],
Expand Down
10 changes: 7 additions & 3 deletions ios/graphics/Shader/PolygonGroupShader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ import UIKit
class PolygonGroupShader: BaseShader {
var polygonStyleBuffer: MTLBuffer?

override init() {
let isStriped : Bool

init(isStriped: Bool) {
self.isStriped = isStriped
super.init()
}

override func setupProgram(_: MCRenderingContextInterface?) {
if pipeline == nil {
pipeline = MetalContext.current.pipelineLibrary.value(Pipeline(type: .polygonGroupShader, blendMode: blendMode).json)
let t : PipelineType = isStriped ? .polygonStripedGroupShader : .polygonGroupShader
pipeline = MetalContext.current.pipelineLibrary.value(Pipeline(type: t, blendMode: blendMode).json)
}
}

Expand All @@ -30,7 +35,6 @@ class PolygonGroupShader: BaseShader {
let pipeline else { return }

context.setRenderPipelineStateIfNeeded(pipeline)

encoder.setFragmentBuffer(polygonStyleBuffer, offset: 0, index: 1)
}
}
Expand Down
5 changes: 3 additions & 2 deletions ios/graphics/Shader/ShaderFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import Foundation
import MapCoreSharedModule

class ShaderFactory: MCShaderFactoryInterface {

func createTextShader() -> MCTextShaderInterface? {
TextShader()
}

func createPolygonGroupShader() -> MCPolygonGroupShaderInterface? {
PolygonGroupShader()
func createPolygonGroupShader(_ isStriped: Bool) -> MCPolygonGroupShaderInterface? {
PolygonGroupShader(isStriped: isStriped)
}

func createPolygonPatternGroupShader(_ fadeInPattern: Bool) -> MCPolygonPatternGroupShaderInterface? {
Expand Down

0 comments on commit 1844d06

Please sign in to comment.