Skip to content

Commit

Permalink
Merge pull request #7 from yukiny0811/macro
Browse files Browse the repository at this point in the history
Macros
  • Loading branch information
yukiny0811 authored Jan 12, 2024
2 parents 4cef02e + 6b66671 commit f407bca
Show file tree
Hide file tree
Showing 48 changed files with 1,442 additions and 1,414 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ on:
branches: [ main ]
workflow_dispatch:
env:
DEVELOPER_DIR: /Applications/Xcode.app
DEVELOPER_DIR: /Applications/Xcode_15.2.app
jobs:
build:
runs-on: macOS-latest
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- name: Build macOS
run: xcodebuild -scheme EasyMetalShader build -destination "generic/platform=macOS"
- name: Build iOS
run: xcodebuild -scheme EasyMetalShader build -destination "generic/platform=iOS"
test:
runs-on: macOS-latest
runs-on: macos-13
steps:
- uses: actions/checkout@v3
- name: Test macOS
Expand Down
14 changes: 14 additions & 0 deletions EasyMetalShader.xcworkspace/xcshareddata/swiftpm/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax.git",
"state" : {
"revision" : "43c802fb7f96e090dde015344a94b5e85779eff1",
"version" : "509.1.0"
}
}
],
"version" : 2
}
8 changes: 4 additions & 4 deletions Examples/ExampleMacOS/ExampleMacOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -295,12 +295,12 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = ExampleMacOS/ExampleMacOS.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"ExampleMacOS/Preview Content\"";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XH2JMYC7KB;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand All @@ -323,12 +323,12 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = ExampleMacOS/ExampleMacOS.entitlements;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"ExampleMacOS/Preview Content\"";
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = XH2JMYC7KB;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down
27 changes: 17 additions & 10 deletions Examples/ExampleMacOS/ExampleMacOS/Logic/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,39 @@

import EasyMetalShader

class MyCompute: EMMetalComputeFunction {
@EMComputeShader
class MyCompute {

@EMArgument("tex") var tex: EMMetalTexture = .init(texture: nil, usage: .read_write)
@EMArgument("intensity") var intensity: Float = 0
var intensity: Float = 3
var tex: MTLTexture?

@ShaderStringBuilder
override var impl: String {
var impl: String {
"float2 floatGid = float2(gid.x, gid.y);"
"float2 center = float2(tex.get_width() / 2, tex.get_height() / 2);"
"float dist = distance(center, floatGid);"
"float color = intensity / dist;"
"tex.write(float4(color, color, color, 1), gid);"
}

var customMetalCode: String {
""
}
}

class MyRender: EMMetalRenderFunction {
@EMRenderShader
class MyRender {

@ShaderStringBuilder
override var vertImpl: String {
var vertImpl: String {
"rd.size = 10;"
"rd.position = vertexInput.input0;"
"rd.color = vertexInput.input1;"
}

@ShaderStringBuilder
override var fragImpl: String {
var fragImpl: String {
"return rd.color + c0;"
}

var customMetalCode: String {
""
}
}
2 changes: 1 addition & 1 deletion Examples/ExampleMacOS/ExampleMacOS/Logic/MyRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class MyRenderer: ShaderRenderer {
override func draw(view: MTKView, drawable: CAMetalDrawable) {
let dispatch = EMMetalDispatch()
dispatch.compute { [self] encoder in
compute.tex = drawable.texture.emTexture
compute.intensity = abs(sin(Float(Date().timeIntervalSince(date)))) * 100
compute.tex = drawable.texture
compute.dispatch(encoder, textureSizeReference: drawable.texture)
}
dispatch.render(renderTargetTexture: drawable.texture, needsClear: false) { [self] encoder in
Expand Down
8 changes: 4 additions & 4 deletions Examples/ExampleiOS/ExampleiOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
5627863C2B515AB900355E44 /* EasyMetalShader in Frameworks */ = {isa = PBXBuildFile; productRef = 5627863B2B515AB900355E44 /* EasyMetalShader */; };
56C0156A2B4D748600E51B4A /* ExampleiOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C015692B4D748600E51B4A /* ExampleiOSApp.swift */; };
56C0156C2B4D748600E51B4A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0156B2B4D748600E51B4A /* ContentView.swift */; };
56C0156E2B4D748700E51B4A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 56C0156D2B4D748700E51B4A /* Assets.xcassets */; };
56C015712B4D748700E51B4A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 56C015702B4D748700E51B4A /* Preview Assets.xcassets */; };
56C0157C2B4D74D700E51B4A /* Functions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0157B2B4D74D700E51B4A /* Functions.swift */; };
56C0157E2B4D74E300E51B4A /* MyRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0157D2B4D74E300E51B4A /* MyRenderer.swift */; };
56C015872B4D7CC100E51B4A /* EasyMetalShader in Frameworks */ = {isa = PBXBuildFile; productRef = 56C015862B4D7CC100E51B4A /* EasyMetalShader */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -31,7 +31,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
56C015872B4D7CC100E51B4A /* EasyMetalShader in Frameworks */,
5627863C2B515AB900355E44 /* EasyMetalShader in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -108,7 +108,7 @@
);
name = ExampleiOS;
packageProductDependencies = (
56C015862B4D7CC100E51B4A /* EasyMetalShader */,
5627863B2B515AB900355E44 /* EasyMetalShader */,
);
productName = ExampleiOS;
productReference = 56C015662B4D748600E51B4A /* ExampleiOS.app */;
Expand Down Expand Up @@ -387,7 +387,7 @@
/* End XCLocalSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
56C015862B4D7CC100E51B4A /* EasyMetalShader */ = {
5627863B2B515AB900355E44 /* EasyMetalShader */ = {
isa = XCSwiftPackageProductDependency;
productName = EasyMetalShader;
};
Expand Down
30 changes: 19 additions & 11 deletions Examples/ExampleiOS/ExampleiOS/Logic/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,41 @@
//

import EasyMetalShader
import simd

class MyCompute: EMMetalComputeFunction {
@EMComputeShader
class MyCompute {

@EMArgument("tex") var tex: EMMetalTexture = .init(texture: nil, usage: .read_write)
@EMArgument("intensity") var intensity: Float = 0
var intensity: Float = 3
var tex: MTLTexture?

@ShaderStringBuilder
override var impl: String {
var impl: String {
"float2 floatGid = float2(gid.x, gid.y);"
"float2 center = float2(tex.get_width() / 2, tex.get_height() / 2);"
"float dist = distance(center, floatGid);"
"float color = intensity / dist;"
"tex.write(float4(color, color, color, 1), gid);"
}

var customMetalCode: String {
""
}
}

class MyRender: EMMetalRenderFunction {
@EMRenderShader
class MyRender {

@ShaderStringBuilder
override var vertImpl: String {
var vertImpl: String {
"rd.size = 10;"
"rd.position = vertexInput.input0;"
"rd.color = float4(1, 0.6, 0.8, 1);"
"rd.color = vertexInput.input1;"
}

@ShaderStringBuilder
override var fragImpl: String {
var fragImpl: String {
"return rd.color + c0;"
}

var customMetalCode: String {
""
}
}
15 changes: 7 additions & 8 deletions Examples/ExampleiOS/ExampleiOS/Logic/MyRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import EasyMetalShader
class MyRenderer: ShaderRenderer {

var particles: [VertexInput] = {
var ps: [simd_float4] = []
var inputs: [VertexInput] = []
for _ in 0...1000 {
ps.append(.init(Float.random(in: -1...1), Float.random(in: -1...1), 0, 1))
}
return ps.map {
var vertexInput = VertexInput()
vertexInput.input0 = $0
return vertexInput
var input = VertexInput()
input.input0 = .init(Float.random(in: -1...1), Float.random(in: -1...1), 0, 1)
input.input1 = .init(Float.random(in: 0.3...1), 0.3, 0.3, 1)
inputs.append(input)
}
return inputs
}()

let compute = MyCompute()
Expand All @@ -27,8 +26,8 @@ class MyRenderer: ShaderRenderer {
override func draw(view: MTKView, drawable: CAMetalDrawable) {
let dispatch = EMMetalDispatch()
dispatch.compute { [self] encoder in
compute.tex = drawable.texture.emTexture
compute.intensity = abs(sin(Float(Date().timeIntervalSince(date)))) * 100
compute.tex = drawable.texture
compute.dispatch(encoder, textureSizeReference: drawable.texture)
}
dispatch.render(renderTargetTexture: drawable.texture, needsClear: false) { [self] encoder in
Expand Down
18 changes: 16 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import CompilerPluginSupport

let package = Package(
name: "EasyMetalShader",
Expand All @@ -15,9 +16,22 @@ let package = Package(
targets: ["EasyMetalShader"]
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
],
targets: [
.macro(
name: "EasyMetalShaderMacro",
dependencies: [
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
]
),
.target(
name: "EasyMetalShader"
name: "EasyMetalShader",
dependencies: [
"EasyMetalShaderMacro"
]
),
.testTarget(
name: "EasyMetalShaderTests",
Expand Down
Loading

0 comments on commit f407bca

Please sign in to comment.