Skip to content

Commit

Permalink
adds iOS part
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmermannubique committed Feb 1, 2024
1 parent ae51246 commit 0d43778
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
34 changes: 34 additions & 0 deletions ios/graphics/Model/BaseGraphicsObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,38 @@ extension BaseGraphicsObject: MCGraphicsObjectInterface {
isMasked: isMasked,
screenPixelAsRealMeterFactor: screenPixelAsRealMeterFactor)
}

// MARK: - Stencil

func maskStencilState(readMask: UInt32 = 0b1111_1111, writeMask: UInt32 = 0b0000_0000) -> MTLDepthStencilState? {
let s = MTLStencilDescriptor()
s.stencilCompareFunction = .equal
s.stencilFailureOperation = .zero
s.depthFailureOperation = .keep
s.depthStencilPassOperation = .keep
s.readMask = readMask
s.writeMask = writeMask

let desc = MTLDepthStencilDescriptor()
desc.frontFaceStencil = s
desc.backFaceStencil = s

return device.makeDepthStencilState(descriptor: desc)
}

func renderPassMaskStencilState() -> MTLDepthStencilState? {
let s = MTLStencilDescriptor()
s.stencilCompareFunction = .equal
s.stencilFailureOperation = .keep
s.depthFailureOperation = .keep
s.depthStencilPassOperation = .incrementWrap
s.readMask = 0b1111_1111
s.writeMask = 0b0000_0001

let desc = MTLDepthStencilDescriptor()
desc.frontFaceStencil = s
desc.backFaceStencil = s

return device.makeDepthStencilState(descriptor: desc)
}
}
31 changes: 13 additions & 18 deletions ios/graphics/Model/Polygon/PolygonGroup2d.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class PolygonGroup2d: BaseGraphicsObject {
private var indicesCount: Int = 0

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


init(shader: MCShaderProgramInterface, metalContext: MetalContext) {
guard let shader = shader as? PolygonGroupShader else {
Expand All @@ -31,25 +33,9 @@ final class PolygonGroup2d: BaseGraphicsObject {
label: "PolygonGroup2d")
}

private func setupStencilStates() {
let ss2 = MTLStencilDescriptor()
ss2.stencilCompareFunction = .equal
ss2.stencilFailureOperation = .zero
ss2.depthFailureOperation = .keep
ss2.depthStencilPassOperation = .keep
ss2.readMask = 0b1111_1111
ss2.writeMask = 0b0000_0000

let s2 = MTLDepthStencilDescriptor()
s2.frontFaceStencil = ss2
s2.backFaceStencil = ss2

stencilState = device.makeDepthStencilState(descriptor: s2)
}

override func render(encoder: MTLRenderCommandEncoder,
context: RenderingContext,
renderPass _: MCRenderPassConfig,
renderPass pass: MCRenderPassConfig,
mvpMatrix: Int64,
isMasked: Bool,
screenPixelAsRealMeterFactor _: Double) {
Expand All @@ -70,12 +56,21 @@ final class PolygonGroup2d: BaseGraphicsObject {

if isMasked {
if stencilState == nil {
setupStencilStates()
stencilState = self.maskStencilState()
}
encoder.setDepthStencilState(stencilState)
encoder.setStencilReferenceValue(0b1100_0000)
}

if pass.isPassMasked {
if renderPassStencilState == nil {
renderPassStencilState = self.renderPassMaskStencilState()
}

encoder.setDepthStencilState(renderPassStencilState)
encoder.setStencilReferenceValue(0b1100_0000)
}

shader.setupProgram(context)
shader.preRender(context)

Expand Down
2 changes: 1 addition & 1 deletion ios/graphics/RenderingContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public class RenderingContext: NSObject {
guard let encoder else { return }
stencilClearQuad.render(encoder: encoder,
context: self,
renderPass: .init(renderPass: 0),
renderPass: .init(renderPass: 0, isPassMasked: false),
mvpMatrix: 0,
isMasked: false,
screenPixelAsRealMeterFactor: 1)
Expand Down

0 comments on commit 0d43778

Please sign in to comment.