-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathMetalStates.swift
132 lines (104 loc) · 4.83 KB
/
MetalStates.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// MetalStates.swift
// ShaderMania
//
// Created by Markus Moenig on 30/8/20.
//
import MetalKit
class MetalStates {
enum States : Int {
case DrawDisc, CopyTexture, DrawTexture, DrawBox, DrawBoxExt, DrawTextChar, DrawBackPattern, DrawLine
}
enum ComputeStates : Int {
case MakeCGIImage
}
var defaultLibrary : MTLLibrary!
let pipelineStateDescriptor : MTLRenderPipelineDescriptor
var states : [Int:MTLRenderPipelineState] = [:]
var computeStates : [Int:MTLComputePipelineState] = [:]
var core : Core
init(_ core: Core)
{
self.core = core
if let frameworkId = core.frameworkId {
for b in Bundle.allFrameworks {
if b.bundleIdentifier == frameworkId {
defaultLibrary = try? core.device.makeDefaultLibrary(bundle: b)
break
}
}
} else {
defaultLibrary = core.device.makeDefaultLibrary()
}
let vertexFunction = defaultLibrary!.makeFunction( name: "m4mQuadVertexShader" )
pipelineStateDescriptor = MTLRenderPipelineDescriptor()
pipelineStateDescriptor.vertexFunction = vertexFunction
// pipelineStateDescriptor.fragmentFunction = fragmentFunction
pipelineStateDescriptor.colorAttachments[0].pixelFormat = MTLPixelFormat.bgra8Unorm;
pipelineStateDescriptor.colorAttachments[0].isBlendingEnabled = true
pipelineStateDescriptor.colorAttachments[0].rgbBlendOperation = .add
pipelineStateDescriptor.colorAttachments[0].alphaBlendOperation = .add
pipelineStateDescriptor.colorAttachments[0].sourceRGBBlendFactor = .sourceAlpha
pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = .sourceAlpha
pipelineStateDescriptor.colorAttachments[0].destinationRGBBlendFactor = .oneMinusSourceAlpha
pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = .oneMinusSourceAlpha
states[States.DrawDisc.rawValue] = createQuadState(name: "m4mDiscDrawable")
states[States.CopyTexture.rawValue] = createQuadState(name: "m4mCopyTextureDrawable")
states[States.DrawTexture.rawValue] = createQuadState(name: "m4mTextureDrawable")
states[States.DrawBox.rawValue] = createQuadState(name: "m4mBoxDrawable")
states[States.DrawBoxExt.rawValue] = createQuadState(name: "m4mBoxDrawableExt")
states[States.DrawTextChar.rawValue] = createQuadState(name: "m4mTextDrawable")
states[States.DrawBackPattern.rawValue] = createQuadState(name: "m4mBoxPatternDrawable")
states[States.DrawLine.rawValue] = createQuadState(name: "m4mLineDrawable")
computeStates[ComputeStates.MakeCGIImage.rawValue] = createComputeState(name: "makeCGIImage")
}
/// Creates a quad state from an optional library and the function name
func createQuadState( library: MTLLibrary? = nil, name: String ) -> MTLRenderPipelineState?
{
let function : MTLFunction?
if library != nil {
function = library!.makeFunction( name: name )
} else {
function = defaultLibrary!.makeFunction( name: name )
}
var renderPipelineState : MTLRenderPipelineState?
do {
//renderPipelineState = try device.makeComputePipelineState( function: function! )
pipelineStateDescriptor.fragmentFunction = function
renderPipelineState = try core.device.makeRenderPipelineState( descriptor: pipelineStateDescriptor )
} catch {
print( "computePipelineState failed" )
return nil
}
return renderPipelineState
}
/// Creates a compute state from an optional library and the function name
func createComputeState( library: MTLLibrary? = nil, name: String ) -> MTLComputePipelineState?
{
let function : MTLFunction?
if library != nil {
function = library!.makeFunction( name: name )
} else {
function = defaultLibrary!.makeFunction( name: name )
}
var computePipelineState : MTLComputePipelineState?
if function == nil {
return nil
}
do {
computePipelineState = try core.device.makeComputePipelineState( function: function! )
} catch {
print( "computePipelineState failed" )
return nil
}
return computePipelineState
}
func getState(state: States) -> MTLRenderPipelineState
{
return states[state.rawValue]!
}
func getComputeState(state: ComputeStates) -> MTLComputePipelineState
{
return computeStates[state.rawValue]!
}
}