Skip to content

Commit

Permalink
Merge pull request #10 from MetalBuilder/compileOptions_helpers
Browse files Browse the repository at this point in the history
added helpers
  • Loading branch information
gadirom authored Aug 19, 2022
2 parents b87dd96 + b2c2e0e commit a7716e8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
25 changes: 16 additions & 9 deletions Example/Example/Example/ComputeBlock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ struct Vertex: MetalStruct
var color: simd_float4 = [0, 0, 0, 0]
}

let sincos2 = """
float2 sincos2(float a){
float cosa;
float sina = sincos(a, cosa);
return float2(sina, cosa);
}
"""

struct ComputeBlock<Particle, Vertex>: MetalBuildingBlock{

let compileOptions: MetalBuilderCompileOptions? = nil //MetalBuilderCompileOptions(mtlCompileOptions: nil, libraryPrefix: .default)
Expand All @@ -42,6 +50,8 @@ struct ComputeBlock<Particle, Vertex>: MetalBuildingBlock{
.uniforms(u)
.threadsFromBuffer(0)
}

let helpers: String = sincos2

let librarySource = """
Expand All @@ -61,16 +71,13 @@ struct ComputeBlock<Particle, Vertex>: MetalBuildingBlock{
float pi = 3.14;
float sinA = sin(angle);
float sinA23 = sin(angle+pi*2/3);
float sinA43 = sin(angle+pi*4/3);
float cosA = cos(angle);
float cosA23 = cos(angle+pi*2/3);
float cosA43 = cos(angle+pi*4/3);
float2 scA = sincos2(angle);
float2 scA23 = sincos2(angle+pi*2/3);
float2 scA43 = sincos2(angle+pi*4/3);
vertices[j].position = position + float2(size*sinA, size*cosA);
vertices[j+1].position = position + float2(size*sinA23, size*cosA23);
vertices[j+2].position = position + float2(size*sinA43, size*cosA43);
vertices[j].position = position + size*scA;
vertices[j+1].position = position + size*scA23;
vertices[j+2].position = position + size*scA43;
float2 viewport = float2(viewportSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public struct MetalBuilderCompileOptions{

public static let `default` = MetalBuilderCompileOptions(mtlCompileOptions: nil, libraryPrefix: .default)


public init(mtlCompileOptions: MTLCompileOptions?, libraryPrefix: MetalBuilderLibraryPrefix) {
self.mtlCompileOptions = mtlCompileOptions
self.libraryPrefix = libraryPrefix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public extension MetalBuilderRenderer{
convenience init(device: MTLDevice,
pixelFormat: MTLPixelFormat,
librarySource: String,
helpers: String,
options: MetalBuilderCompileOptions = .default,
renderingContent: MetalRenderingContent) throws{

Expand All @@ -54,11 +55,12 @@ public extension MetalBuilderRenderer{
do{

renderData = try RenderData(from: renderingContent,
librarySource: librarySource,
options: options,
context: context,
device: device,
pixelFormat: pixelFormat)
librarySource: librarySource,
helpers: helpers,
options: options,
context: context,
device: device,
pixelFormat: pixelFormat)

}catch{
print(error)
Expand Down
21 changes: 16 additions & 5 deletions Sources/MetalBuilder/MetalBuilderRenderer/RenderData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ struct RenderData{

var functionsAndArgumentsToAddToMetal: [FunctionAndArguments] = []

//var libraryBindings: [LibraryContainer] = []
// var helpers = ""

var pixelFormat: MTLPixelFormat?
//var device: MTLDevice!

var context: MetalBuilderRenderingContext!

init(){}

init(from renderingContent: MetalRenderingContent,
librarySource: String,
helpers: String,
options: MetalBuilderCompileOptions,
context: MetalBuilderRenderingContext,
device: MTLDevice,
Expand All @@ -43,6 +43,7 @@ struct RenderData{
pixelFormat: pixelFormat,
content: content,
librarySource: librarySource,
helpers: helpers,
options: options,
context: context)
self.append(data)
Expand All @@ -55,16 +56,19 @@ struct RenderData{
pixelFormat: MTLPixelFormat,
content: MetalContent,
librarySource: String,
helpers: String,
options: MetalBuilderCompileOptions,
context: MetalBuilderRenderingContext) throws -> Self{

var librarySource = librarySource
var helpers = helpers
let libraryContainer = LibraryContainer()

return try compile(device: device,
pixelFormat: pixelFormat,
content: content,
librarySource: &librarySource,
helpers: &helpers,
libraryContainer: libraryContainer,
options: options,
context: context,
Expand All @@ -75,6 +79,7 @@ struct RenderData{
pixelFormat: MTLPixelFormat,
content: MetalContent,
librarySource: inout String,
helpers: inout String,
libraryContainer: LibraryContainer,
options: MetalBuilderCompileOptions,
context: MetalBuilderRenderingContext,
Expand Down Expand Up @@ -162,6 +167,7 @@ struct RenderData{
pixelFormat: pixelFormat,
content: encodeGroupComponent.metalContent,
librarySource: &librarySource,
helpers: &helpers,
libraryContainer: libraryContainer,
options: options,
context: context,
Expand All @@ -180,19 +186,22 @@ struct RenderData{
pixelFormat: pixelFormat,
content: buildingBlockComponent.metalContent,
librarySource: buildingBlockComponent.librarySource,
helpers: buildingBlockComponent.helpers,
options: options,
context: context
)
data.append(blockData)
}else{
//compile to shared library
librarySource = buildingBlockComponent.librarySource + librarySource
helpers += buildingBlockComponent.helpers

let blockData = try compile(
device: device,
pixelFormat: pixelFormat,
content: buildingBlockComponent.metalContent,
librarySource: &librarySource,
helpers: &helpers,
libraryContainer: libraryContainer,
options: options,
context: context,
Expand All @@ -213,10 +222,12 @@ struct RenderData{
try parse(library: &librarySource,
funcArguments: data.functionsAndArgumentsToAddToMetal)

let libraryPrefix: String
switch options.libraryPrefix{
case .`default`: librarySource = kMetalBuilderDefaultLibraryPrefix + librarySource
case .custom(let prefix): librarySource = prefix + librarySource
case .`default`: libraryPrefix = kMetalBuilderDefaultLibraryPrefix
case .custom(let prefix): libraryPrefix = prefix
}
librarySource = libraryPrefix + helpers + librarySource
libraryContainer.library = try device.makeLibrary(source: librarySource, options: options.mtlCompileOptions)
}
}
Expand Down
10 changes: 9 additions & 1 deletion Sources/MetalBuilder/MetalBuilderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@ import SwiftUI
public struct MetalBuilderView: UIViewRepresentable {

public let librarySource: String
public let helpers: String
@Binding public var isDrawing: Bool
@MetalResultBuilder public let metalContent: MetalRenderingContent
let onResizeCode: ((CGSize)->())?

public init(librarySource: String,
helpers: String = "",
isDrawing: Binding<Bool>,
@MetalResultBuilder metalContent: @escaping MetalRenderingContent){
self.init(librarySource: librarySource,
helpers: helpers,
isDrawing: isDrawing,
metalContent: metalContent,
onResizeCode: nil)
}
init(librarySource: String,
helpers: String,
isDrawing: Binding<Bool>,
metalContent: @escaping MetalRenderingContent,
onResizeCode: ((CGSize)->())?) {
self.librarySource = librarySource
self.helpers = helpers
self._isDrawing = isDrawing
self.metalContent = metalContent
self.onResizeCode = onResizeCode
}

public func onResize(perform: @escaping (CGSize)->())->MetalBuilderView{
MetalBuilderView(librarySource: librarySource,
helpers: helpers,
isDrawing: $isDrawing,
metalContent: metalContent,
onResizeCode: perform)
Expand All @@ -54,6 +60,7 @@ public struct MetalBuilderView: UIViewRepresentable {
mtkView.isPaused = false

context.coordinator.setupRenderer(librarySource: librarySource,
helpers: helpers,
pixelFormat: mtkView.colorPixelFormat,
metalContent: metalContent)

Expand All @@ -78,12 +85,13 @@ public struct MetalBuilderView: UIViewRepresentable {
}
}

func setupRenderer(librarySource: String, pixelFormat: MTLPixelFormat, metalContent: MetalRenderingContent){
func setupRenderer(librarySource: String, helpers: String, pixelFormat: MTLPixelFormat, metalContent: MetalRenderingContent){
do{
renderer =
try MetalBuilderRenderer(device: device,
pixelFormat: pixelFormat,
librarySource: librarySource,
helpers: helpers,
renderingContent: metalContent)
}catch{ print(error) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import SwiftUI
/// if compileOptions are not nil the source will compile into separate library
public protocol MetalBuildingBlock: MetalBuilderComponent{
var context: MetalBuilderRenderingContext { get set }
var helpers: String { get }
var librarySource: String { get }
var compileOptions: MetalBuilderCompileOptions? { get }
@MetalResultBuilder var metalContent: MetalContent{ get }
Expand Down

0 comments on commit a7716e8

Please sign in to comment.