Skip to content

Commit f407bca

Browse files
authored
Merge pull request #7 from yukiny0811/macro
Macros
2 parents 4cef02e + 6b66671 commit f407bca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1442
-1414
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ on:
66
branches: [ main ]
77
workflow_dispatch:
88
env:
9-
DEVELOPER_DIR: /Applications/Xcode.app
9+
DEVELOPER_DIR: /Applications/Xcode_15.2.app
1010
jobs:
1111
build:
12-
runs-on: macOS-latest
12+
runs-on: macos-13
1313
steps:
1414
- uses: actions/checkout@v3
1515
- name: Build macOS
1616
run: xcodebuild -scheme EasyMetalShader build -destination "generic/platform=macOS"
1717
- name: Build iOS
1818
run: xcodebuild -scheme EasyMetalShader build -destination "generic/platform=iOS"
1919
test:
20-
runs-on: macOS-latest
20+
runs-on: macos-13
2121
steps:
2222
- uses: actions/checkout@v3
2323
- name: Test macOS

EasyMetalShader.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Examples/ExampleMacOS/ExampleMacOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,12 @@
295295
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
296296
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
297297
CODE_SIGN_ENTITLEMENTS = ExampleMacOS/ExampleMacOS.entitlements;
298-
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
298+
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
299299
CODE_SIGN_STYLE = Automatic;
300300
COMBINE_HIDPI_IMAGES = YES;
301301
CURRENT_PROJECT_VERSION = 1;
302302
DEVELOPMENT_ASSET_PATHS = "\"ExampleMacOS/Preview Content\"";
303-
DEVELOPMENT_TEAM = "";
303+
DEVELOPMENT_TEAM = XH2JMYC7KB;
304304
ENABLE_HARDENED_RUNTIME = YES;
305305
ENABLE_PREVIEWS = YES;
306306
GENERATE_INFOPLIST_FILE = YES;
@@ -323,12 +323,12 @@
323323
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
324324
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
325325
CODE_SIGN_ENTITLEMENTS = ExampleMacOS/ExampleMacOS.entitlements;
326-
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
326+
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
327327
CODE_SIGN_STYLE = Automatic;
328328
COMBINE_HIDPI_IMAGES = YES;
329329
CURRENT_PROJECT_VERSION = 1;
330330
DEVELOPMENT_ASSET_PATHS = "\"ExampleMacOS/Preview Content\"";
331-
DEVELOPMENT_TEAM = "";
331+
DEVELOPMENT_TEAM = XH2JMYC7KB;
332332
ENABLE_HARDENED_RUNTIME = YES;
333333
ENABLE_PREVIEWS = YES;
334334
GENERATE_INFOPLIST_FILE = YES;

Examples/ExampleMacOS/ExampleMacOS/Logic/Functions.swift

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,39 @@
77

88
import EasyMetalShader
99

10-
class MyCompute: EMMetalComputeFunction {
10+
@EMComputeShader
11+
class MyCompute {
1112

12-
@EMArgument("tex") var tex: EMMetalTexture = .init(texture: nil, usage: .read_write)
13-
@EMArgument("intensity") var intensity: Float = 0
13+
var intensity: Float = 3
14+
var tex: MTLTexture?
1415

15-
@ShaderStringBuilder
16-
override var impl: String {
16+
var impl: String {
1717
"float2 floatGid = float2(gid.x, gid.y);"
1818
"float2 center = float2(tex.get_width() / 2, tex.get_height() / 2);"
1919
"float dist = distance(center, floatGid);"
2020
"float color = intensity / dist;"
2121
"tex.write(float4(color, color, color, 1), gid);"
2222
}
23+
24+
var customMetalCode: String {
25+
""
26+
}
2327
}
2428

25-
class MyRender: EMMetalRenderFunction {
29+
@EMRenderShader
30+
class MyRender {
2631

27-
@ShaderStringBuilder
28-
override var vertImpl: String {
32+
var vertImpl: String {
2933
"rd.size = 10;"
3034
"rd.position = vertexInput.input0;"
3135
"rd.color = vertexInput.input1;"
3236
}
3337

34-
@ShaderStringBuilder
35-
override var fragImpl: String {
38+
var fragImpl: String {
3639
"return rd.color + c0;"
3740
}
41+
42+
var customMetalCode: String {
43+
""
44+
}
3845
}

Examples/ExampleMacOS/ExampleMacOS/Logic/MyRenderer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class MyRenderer: ShaderRenderer {
2626
override func draw(view: MTKView, drawable: CAMetalDrawable) {
2727
let dispatch = EMMetalDispatch()
2828
dispatch.compute { [self] encoder in
29-
compute.tex = drawable.texture.emTexture
3029
compute.intensity = abs(sin(Float(Date().timeIntervalSince(date)))) * 100
30+
compute.tex = drawable.texture
3131
compute.dispatch(encoder, textureSizeReference: drawable.texture)
3232
}
3333
dispatch.render(renderTargetTexture: drawable.texture, needsClear: false) { [self] encoder in

Examples/ExampleiOS/ExampleiOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
5627863C2B515AB900355E44 /* EasyMetalShader in Frameworks */ = {isa = PBXBuildFile; productRef = 5627863B2B515AB900355E44 /* EasyMetalShader */; };
1011
56C0156A2B4D748600E51B4A /* ExampleiOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C015692B4D748600E51B4A /* ExampleiOSApp.swift */; };
1112
56C0156C2B4D748600E51B4A /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0156B2B4D748600E51B4A /* ContentView.swift */; };
1213
56C0156E2B4D748700E51B4A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 56C0156D2B4D748700E51B4A /* Assets.xcassets */; };
1314
56C015712B4D748700E51B4A /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 56C015702B4D748700E51B4A /* Preview Assets.xcassets */; };
1415
56C0157C2B4D74D700E51B4A /* Functions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0157B2B4D74D700E51B4A /* Functions.swift */; };
1516
56C0157E2B4D74E300E51B4A /* MyRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56C0157D2B4D74E300E51B4A /* MyRenderer.swift */; };
16-
56C015872B4D7CC100E51B4A /* EasyMetalShader in Frameworks */ = {isa = PBXBuildFile; productRef = 56C015862B4D7CC100E51B4A /* EasyMetalShader */; };
1717
/* End PBXBuildFile section */
1818

1919
/* Begin PBXFileReference section */
@@ -31,7 +31,7 @@
3131
isa = PBXFrameworksBuildPhase;
3232
buildActionMask = 2147483647;
3333
files = (
34-
56C015872B4D7CC100E51B4A /* EasyMetalShader in Frameworks */,
34+
5627863C2B515AB900355E44 /* EasyMetalShader in Frameworks */,
3535
);
3636
runOnlyForDeploymentPostprocessing = 0;
3737
};
@@ -108,7 +108,7 @@
108108
);
109109
name = ExampleiOS;
110110
packageProductDependencies = (
111-
56C015862B4D7CC100E51B4A /* EasyMetalShader */,
111+
5627863B2B515AB900355E44 /* EasyMetalShader */,
112112
);
113113
productName = ExampleiOS;
114114
productReference = 56C015662B4D748600E51B4A /* ExampleiOS.app */;
@@ -387,7 +387,7 @@
387387
/* End XCLocalSwiftPackageReference section */
388388

389389
/* Begin XCSwiftPackageProductDependency section */
390-
56C015862B4D7CC100E51B4A /* EasyMetalShader */ = {
390+
5627863B2B515AB900355E44 /* EasyMetalShader */ = {
391391
isa = XCSwiftPackageProductDependency;
392392
productName = EasyMetalShader;
393393
};

Examples/ExampleiOS/ExampleiOS/Logic/Functions.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,41 @@
66
//
77

88
import EasyMetalShader
9+
import simd
910

10-
class MyCompute: EMMetalComputeFunction {
11+
@EMComputeShader
12+
class MyCompute {
1113

12-
@EMArgument("tex") var tex: EMMetalTexture = .init(texture: nil, usage: .read_write)
13-
@EMArgument("intensity") var intensity: Float = 0
14+
var intensity: Float = 3
15+
var tex: MTLTexture?
1416

15-
@ShaderStringBuilder
16-
override var impl: String {
17+
var impl: String {
1718
"float2 floatGid = float2(gid.x, gid.y);"
1819
"float2 center = float2(tex.get_width() / 2, tex.get_height() / 2);"
1920
"float dist = distance(center, floatGid);"
2021
"float color = intensity / dist;"
2122
"tex.write(float4(color, color, color, 1), gid);"
2223
}
24+
25+
var customMetalCode: String {
26+
""
27+
}
2328
}
2429

25-
class MyRender: EMMetalRenderFunction {
30+
@EMRenderShader
31+
class MyRender {
2632

27-
@ShaderStringBuilder
28-
override var vertImpl: String {
33+
var vertImpl: String {
2934
"rd.size = 10;"
3035
"rd.position = vertexInput.input0;"
31-
"rd.color = float4(1, 0.6, 0.8, 1);"
36+
"rd.color = vertexInput.input1;"
3237
}
3338

34-
@ShaderStringBuilder
35-
override var fragImpl: String {
39+
var fragImpl: String {
3640
"return rd.color + c0;"
3741
}
42+
43+
var customMetalCode: String {
44+
""
45+
}
3846
}

Examples/ExampleiOS/ExampleiOS/Logic/MyRenderer.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import EasyMetalShader
1010
class MyRenderer: ShaderRenderer {
1111

1212
var particles: [VertexInput] = {
13-
var ps: [simd_float4] = []
13+
var inputs: [VertexInput] = []
1414
for _ in 0...1000 {
15-
ps.append(.init(Float.random(in: -1...1), Float.random(in: -1...1), 0, 1))
16-
}
17-
return ps.map {
18-
var vertexInput = VertexInput()
19-
vertexInput.input0 = $0
20-
return vertexInput
15+
var input = VertexInput()
16+
input.input0 = .init(Float.random(in: -1...1), Float.random(in: -1...1), 0, 1)
17+
input.input1 = .init(Float.random(in: 0.3...1), 0.3, 0.3, 1)
18+
inputs.append(input)
2119
}
20+
return inputs
2221
}()
2322

2423
let compute = MyCompute()
@@ -27,8 +26,8 @@ class MyRenderer: ShaderRenderer {
2726
override func draw(view: MTKView, drawable: CAMetalDrawable) {
2827
let dispatch = EMMetalDispatch()
2928
dispatch.compute { [self] encoder in
30-
compute.tex = drawable.texture.emTexture
3129
compute.intensity = abs(sin(Float(Date().timeIntervalSince(date)))) * 100
30+
compute.tex = drawable.texture
3231
compute.dispatch(encoder, textureSizeReference: drawable.texture)
3332
}
3433
dispatch.render(renderTargetTexture: drawable.texture, needsClear: false) { [self] encoder in

Package.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// swift-tools-version: 5.7
1+
// swift-tools-version: 5.9
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription
5+
import CompilerPluginSupport
56

67
let package = Package(
78
name: "EasyMetalShader",
@@ -15,9 +16,22 @@ let package = Package(
1516
targets: ["EasyMetalShader"]
1617
),
1718
],
19+
dependencies: [
20+
.package(url: "https://github.com/apple/swift-syntax.git", from: "509.0.0"),
21+
],
1822
targets: [
23+
.macro(
24+
name: "EasyMetalShaderMacro",
25+
dependencies: [
26+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
27+
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
28+
]
29+
),
1930
.target(
20-
name: "EasyMetalShader"
31+
name: "EasyMetalShader",
32+
dependencies: [
33+
"EasyMetalShaderMacro"
34+
]
2135
),
2236
.testTarget(
2337
name: "EasyMetalShaderTests",

0 commit comments

Comments
 (0)