├── .gitignore ├── LICENSE ├── README.md ├── doc └── screenshots │ └── waves-metal.png ├── metal-watersimulation.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── lachlan.xcuserdatad │ └── xcschemes │ ├── metal-watersimulation.xcscheme │ └── xcschememanagement.plist ├── metal-watersimulation ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── ViewController.swift └── waveComputeShader.metal ├── metal-watersimulationTests ├── Info.plist └── metal_watersimulationTests.swift └── metal-watersimulationUITests ├── Info.plist └── metal_watersimulationUITests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | ### Xcode ### 2 | build/ 3 | *.pbxuser 4 | !default.pbxuser 5 | *.mode1v3 6 | !default.mode1v3 7 | *.mode2v3 8 | !default.mode2v3 9 | *.perspectivev3 10 | !default.perspectivev3 11 | xcuserdata 12 | *.xccheckout 13 | *.moved-aside 14 | DerivedData 15 | *.xcuserstate 16 | 17 | # Workspace file - autogenerated by cocoa pods 18 | *.xcworkspace 19 | 20 | 21 | # CocoaPods 22 | Pods/ 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Lachlan Hurst 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Waves in Metal compute shaders 2 | 3 | The algorithm is based on simple Euler integration, you can find a JavaScript implementation [here](http://users.softlab.ntua.gr/~ttsiod/wavePhysics.html). 4 | 5 | The 'working with Metal' parts of this project owe much to FlexMonkey's [Reaction Diffusion using Swift & Metal](https://github.com/FlexMonkey/MetalReactionDiffusion). 6 | 7 | ![Wave simulation](doc/screenshots/waves-metal.png) 8 | 9 | [YouTube video of simlation](https://youtu.be/cy4S7QrLy8Y) 10 | 11 | The black quad seen in the screenshots and video is SceneKit geometry (SCNPlane), the texture of this SCNGeometry is set to be the output texture of the Metal compute shader. SceneKit also handles the hit detection, the hit coordinates are translated into texture coordinates and drawn into the input textures (red square seen in video). 12 | 13 | 14 | # LICENSE 15 | MIT 16 | -------------------------------------------------------------------------------- /doc/screenshots/waves-metal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lachlanhurst/waves-metalcompute/5a3961a8d454372d55d7bc703b57ab2c6f911610/doc/screenshots/waves-metal.png -------------------------------------------------------------------------------- /metal-watersimulation.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 734FBE431BBD5E4000313AD9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 734FBE421BBD5E4000313AD9 /* AppDelegate.swift */; }; 11 | 734FBE451BBD5E4000313AD9 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 734FBE441BBD5E4000313AD9 /* ViewController.swift */; }; 12 | 734FBE481BBD5E4000313AD9 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 734FBE461BBD5E4000313AD9 /* Main.storyboard */; }; 13 | 734FBE4A1BBD5E4000313AD9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 734FBE491BBD5E4000313AD9 /* Assets.xcassets */; }; 14 | 734FBE4D1BBD5E4100313AD9 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 734FBE4B1BBD5E4100313AD9 /* LaunchScreen.storyboard */; }; 15 | 734FBE581BBD5E4100313AD9 /* metal_watersimulationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 734FBE571BBD5E4100313AD9 /* metal_watersimulationTests.swift */; }; 16 | 734FBE631BBD5E4100313AD9 /* metal_watersimulationUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 734FBE621BBD5E4100313AD9 /* metal_watersimulationUITests.swift */; }; 17 | 734FBE711BBD613A00313AD9 /* waveComputeShader.metal in Sources */ = {isa = PBXBuildFile; fileRef = 734FBE701BBD613A00313AD9 /* waveComputeShader.metal */; settings = {ASSET_TAGS = (); }; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXContainerItemProxy section */ 21 | 734FBE541BBD5E4100313AD9 /* PBXContainerItemProxy */ = { 22 | isa = PBXContainerItemProxy; 23 | containerPortal = 734FBE371BBD5E4000313AD9 /* Project object */; 24 | proxyType = 1; 25 | remoteGlobalIDString = 734FBE3E1BBD5E4000313AD9; 26 | remoteInfo = "metal-watersimulation"; 27 | }; 28 | 734FBE5F1BBD5E4100313AD9 /* PBXContainerItemProxy */ = { 29 | isa = PBXContainerItemProxy; 30 | containerPortal = 734FBE371BBD5E4000313AD9 /* Project object */; 31 | proxyType = 1; 32 | remoteGlobalIDString = 734FBE3E1BBD5E4000313AD9; 33 | remoteInfo = "metal-watersimulation"; 34 | }; 35 | /* End PBXContainerItemProxy section */ 36 | 37 | /* Begin PBXFileReference section */ 38 | 734FBE3F1BBD5E4000313AD9 /* metal-watersimulation.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "metal-watersimulation.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 39 | 734FBE421BBD5E4000313AD9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 40 | 734FBE441BBD5E4000313AD9 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 41 | 734FBE471BBD5E4000313AD9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 42 | 734FBE491BBD5E4000313AD9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 43 | 734FBE4C1BBD5E4100313AD9 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 44 | 734FBE4E1BBD5E4100313AD9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 45 | 734FBE531BBD5E4100313AD9 /* metal-watersimulationTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "metal-watersimulationTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 46 | 734FBE571BBD5E4100313AD9 /* metal_watersimulationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metal_watersimulationTests.swift; sourceTree = ""; }; 47 | 734FBE591BBD5E4100313AD9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 48 | 734FBE5E1BBD5E4100313AD9 /* metal-watersimulationUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "metal-watersimulationUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 49 | 734FBE621BBD5E4100313AD9 /* metal_watersimulationUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = metal_watersimulationUITests.swift; sourceTree = ""; }; 50 | 734FBE641BBD5E4100313AD9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 51 | 734FBE701BBD613A00313AD9 /* waveComputeShader.metal */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.metal; path = waveComputeShader.metal; sourceTree = ""; }; 52 | /* End PBXFileReference section */ 53 | 54 | /* Begin PBXFrameworksBuildPhase section */ 55 | 734FBE3C1BBD5E4000313AD9 /* Frameworks */ = { 56 | isa = PBXFrameworksBuildPhase; 57 | buildActionMask = 2147483647; 58 | files = ( 59 | ); 60 | runOnlyForDeploymentPostprocessing = 0; 61 | }; 62 | 734FBE501BBD5E4100313AD9 /* Frameworks */ = { 63 | isa = PBXFrameworksBuildPhase; 64 | buildActionMask = 2147483647; 65 | files = ( 66 | ); 67 | runOnlyForDeploymentPostprocessing = 0; 68 | }; 69 | 734FBE5B1BBD5E4100313AD9 /* Frameworks */ = { 70 | isa = PBXFrameworksBuildPhase; 71 | buildActionMask = 2147483647; 72 | files = ( 73 | ); 74 | runOnlyForDeploymentPostprocessing = 0; 75 | }; 76 | /* End PBXFrameworksBuildPhase section */ 77 | 78 | /* Begin PBXGroup section */ 79 | 734FBE361BBD5E4000313AD9 = { 80 | isa = PBXGroup; 81 | children = ( 82 | 734FBE411BBD5E4000313AD9 /* metal-watersimulation */, 83 | 734FBE561BBD5E4100313AD9 /* metal-watersimulationTests */, 84 | 734FBE611BBD5E4100313AD9 /* metal-watersimulationUITests */, 85 | 734FBE401BBD5E4000313AD9 /* Products */, 86 | ); 87 | sourceTree = ""; 88 | }; 89 | 734FBE401BBD5E4000313AD9 /* Products */ = { 90 | isa = PBXGroup; 91 | children = ( 92 | 734FBE3F1BBD5E4000313AD9 /* metal-watersimulation.app */, 93 | 734FBE531BBD5E4100313AD9 /* metal-watersimulationTests.xctest */, 94 | 734FBE5E1BBD5E4100313AD9 /* metal-watersimulationUITests.xctest */, 95 | ); 96 | name = Products; 97 | sourceTree = ""; 98 | }; 99 | 734FBE411BBD5E4000313AD9 /* metal-watersimulation */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | 734FBE701BBD613A00313AD9 /* waveComputeShader.metal */, 103 | 734FBE421BBD5E4000313AD9 /* AppDelegate.swift */, 104 | 734FBE441BBD5E4000313AD9 /* ViewController.swift */, 105 | 734FBE461BBD5E4000313AD9 /* Main.storyboard */, 106 | 734FBE491BBD5E4000313AD9 /* Assets.xcassets */, 107 | 734FBE4B1BBD5E4100313AD9 /* LaunchScreen.storyboard */, 108 | 734FBE4E1BBD5E4100313AD9 /* Info.plist */, 109 | ); 110 | path = "metal-watersimulation"; 111 | sourceTree = ""; 112 | }; 113 | 734FBE561BBD5E4100313AD9 /* metal-watersimulationTests */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | 734FBE571BBD5E4100313AD9 /* metal_watersimulationTests.swift */, 117 | 734FBE591BBD5E4100313AD9 /* Info.plist */, 118 | ); 119 | path = "metal-watersimulationTests"; 120 | sourceTree = ""; 121 | }; 122 | 734FBE611BBD5E4100313AD9 /* metal-watersimulationUITests */ = { 123 | isa = PBXGroup; 124 | children = ( 125 | 734FBE621BBD5E4100313AD9 /* metal_watersimulationUITests.swift */, 126 | 734FBE641BBD5E4100313AD9 /* Info.plist */, 127 | ); 128 | path = "metal-watersimulationUITests"; 129 | sourceTree = ""; 130 | }; 131 | /* End PBXGroup section */ 132 | 133 | /* Begin PBXNativeTarget section */ 134 | 734FBE3E1BBD5E4000313AD9 /* metal-watersimulation */ = { 135 | isa = PBXNativeTarget; 136 | buildConfigurationList = 734FBE671BBD5E4100313AD9 /* Build configuration list for PBXNativeTarget "metal-watersimulation" */; 137 | buildPhases = ( 138 | 734FBE3B1BBD5E4000313AD9 /* Sources */, 139 | 734FBE3C1BBD5E4000313AD9 /* Frameworks */, 140 | 734FBE3D1BBD5E4000313AD9 /* Resources */, 141 | ); 142 | buildRules = ( 143 | ); 144 | dependencies = ( 145 | ); 146 | name = "metal-watersimulation"; 147 | productName = "metal-watersimulation"; 148 | productReference = 734FBE3F1BBD5E4000313AD9 /* metal-watersimulation.app */; 149 | productType = "com.apple.product-type.application"; 150 | }; 151 | 734FBE521BBD5E4100313AD9 /* metal-watersimulationTests */ = { 152 | isa = PBXNativeTarget; 153 | buildConfigurationList = 734FBE6A1BBD5E4100313AD9 /* Build configuration list for PBXNativeTarget "metal-watersimulationTests" */; 154 | buildPhases = ( 155 | 734FBE4F1BBD5E4100313AD9 /* Sources */, 156 | 734FBE501BBD5E4100313AD9 /* Frameworks */, 157 | 734FBE511BBD5E4100313AD9 /* Resources */, 158 | ); 159 | buildRules = ( 160 | ); 161 | dependencies = ( 162 | 734FBE551BBD5E4100313AD9 /* PBXTargetDependency */, 163 | ); 164 | name = "metal-watersimulationTests"; 165 | productName = "metal-watersimulationTests"; 166 | productReference = 734FBE531BBD5E4100313AD9 /* metal-watersimulationTests.xctest */; 167 | productType = "com.apple.product-type.bundle.unit-test"; 168 | }; 169 | 734FBE5D1BBD5E4100313AD9 /* metal-watersimulationUITests */ = { 170 | isa = PBXNativeTarget; 171 | buildConfigurationList = 734FBE6D1BBD5E4100313AD9 /* Build configuration list for PBXNativeTarget "metal-watersimulationUITests" */; 172 | buildPhases = ( 173 | 734FBE5A1BBD5E4100313AD9 /* Sources */, 174 | 734FBE5B1BBD5E4100313AD9 /* Frameworks */, 175 | 734FBE5C1BBD5E4100313AD9 /* Resources */, 176 | ); 177 | buildRules = ( 178 | ); 179 | dependencies = ( 180 | 734FBE601BBD5E4100313AD9 /* PBXTargetDependency */, 181 | ); 182 | name = "metal-watersimulationUITests"; 183 | productName = "metal-watersimulationUITests"; 184 | productReference = 734FBE5E1BBD5E4100313AD9 /* metal-watersimulationUITests.xctest */; 185 | productType = "com.apple.product-type.bundle.ui-testing"; 186 | }; 187 | /* End PBXNativeTarget section */ 188 | 189 | /* Begin PBXProject section */ 190 | 734FBE371BBD5E4000313AD9 /* Project object */ = { 191 | isa = PBXProject; 192 | attributes = { 193 | LastUpgradeCheck = 0700; 194 | ORGANIZATIONNAME = "Lachlan Hurst"; 195 | TargetAttributes = { 196 | 734FBE3E1BBD5E4000313AD9 = { 197 | CreatedOnToolsVersion = 7.0; 198 | }; 199 | 734FBE521BBD5E4100313AD9 = { 200 | CreatedOnToolsVersion = 7.0; 201 | TestTargetID = 734FBE3E1BBD5E4000313AD9; 202 | }; 203 | 734FBE5D1BBD5E4100313AD9 = { 204 | CreatedOnToolsVersion = 7.0; 205 | TestTargetID = 734FBE3E1BBD5E4000313AD9; 206 | }; 207 | }; 208 | }; 209 | buildConfigurationList = 734FBE3A1BBD5E4000313AD9 /* Build configuration list for PBXProject "metal-watersimulation" */; 210 | compatibilityVersion = "Xcode 3.2"; 211 | developmentRegion = English; 212 | hasScannedForEncodings = 0; 213 | knownRegions = ( 214 | en, 215 | Base, 216 | ); 217 | mainGroup = 734FBE361BBD5E4000313AD9; 218 | productRefGroup = 734FBE401BBD5E4000313AD9 /* Products */; 219 | projectDirPath = ""; 220 | projectRoot = ""; 221 | targets = ( 222 | 734FBE3E1BBD5E4000313AD9 /* metal-watersimulation */, 223 | 734FBE521BBD5E4100313AD9 /* metal-watersimulationTests */, 224 | 734FBE5D1BBD5E4100313AD9 /* metal-watersimulationUITests */, 225 | ); 226 | }; 227 | /* End PBXProject section */ 228 | 229 | /* Begin PBXResourcesBuildPhase section */ 230 | 734FBE3D1BBD5E4000313AD9 /* Resources */ = { 231 | isa = PBXResourcesBuildPhase; 232 | buildActionMask = 2147483647; 233 | files = ( 234 | 734FBE4D1BBD5E4100313AD9 /* LaunchScreen.storyboard in Resources */, 235 | 734FBE4A1BBD5E4000313AD9 /* Assets.xcassets in Resources */, 236 | 734FBE481BBD5E4000313AD9 /* Main.storyboard in Resources */, 237 | ); 238 | runOnlyForDeploymentPostprocessing = 0; 239 | }; 240 | 734FBE511BBD5E4100313AD9 /* Resources */ = { 241 | isa = PBXResourcesBuildPhase; 242 | buildActionMask = 2147483647; 243 | files = ( 244 | ); 245 | runOnlyForDeploymentPostprocessing = 0; 246 | }; 247 | 734FBE5C1BBD5E4100313AD9 /* Resources */ = { 248 | isa = PBXResourcesBuildPhase; 249 | buildActionMask = 2147483647; 250 | files = ( 251 | ); 252 | runOnlyForDeploymentPostprocessing = 0; 253 | }; 254 | /* End PBXResourcesBuildPhase section */ 255 | 256 | /* Begin PBXSourcesBuildPhase section */ 257 | 734FBE3B1BBD5E4000313AD9 /* Sources */ = { 258 | isa = PBXSourcesBuildPhase; 259 | buildActionMask = 2147483647; 260 | files = ( 261 | 734FBE451BBD5E4000313AD9 /* ViewController.swift in Sources */, 262 | 734FBE431BBD5E4000313AD9 /* AppDelegate.swift in Sources */, 263 | 734FBE711BBD613A00313AD9 /* waveComputeShader.metal in Sources */, 264 | ); 265 | runOnlyForDeploymentPostprocessing = 0; 266 | }; 267 | 734FBE4F1BBD5E4100313AD9 /* Sources */ = { 268 | isa = PBXSourcesBuildPhase; 269 | buildActionMask = 2147483647; 270 | files = ( 271 | 734FBE581BBD5E4100313AD9 /* metal_watersimulationTests.swift in Sources */, 272 | ); 273 | runOnlyForDeploymentPostprocessing = 0; 274 | }; 275 | 734FBE5A1BBD5E4100313AD9 /* Sources */ = { 276 | isa = PBXSourcesBuildPhase; 277 | buildActionMask = 2147483647; 278 | files = ( 279 | 734FBE631BBD5E4100313AD9 /* metal_watersimulationUITests.swift in Sources */, 280 | ); 281 | runOnlyForDeploymentPostprocessing = 0; 282 | }; 283 | /* End PBXSourcesBuildPhase section */ 284 | 285 | /* Begin PBXTargetDependency section */ 286 | 734FBE551BBD5E4100313AD9 /* PBXTargetDependency */ = { 287 | isa = PBXTargetDependency; 288 | target = 734FBE3E1BBD5E4000313AD9 /* metal-watersimulation */; 289 | targetProxy = 734FBE541BBD5E4100313AD9 /* PBXContainerItemProxy */; 290 | }; 291 | 734FBE601BBD5E4100313AD9 /* PBXTargetDependency */ = { 292 | isa = PBXTargetDependency; 293 | target = 734FBE3E1BBD5E4000313AD9 /* metal-watersimulation */; 294 | targetProxy = 734FBE5F1BBD5E4100313AD9 /* PBXContainerItemProxy */; 295 | }; 296 | /* End PBXTargetDependency section */ 297 | 298 | /* Begin PBXVariantGroup section */ 299 | 734FBE461BBD5E4000313AD9 /* Main.storyboard */ = { 300 | isa = PBXVariantGroup; 301 | children = ( 302 | 734FBE471BBD5E4000313AD9 /* Base */, 303 | ); 304 | name = Main.storyboard; 305 | sourceTree = ""; 306 | }; 307 | 734FBE4B1BBD5E4100313AD9 /* LaunchScreen.storyboard */ = { 308 | isa = PBXVariantGroup; 309 | children = ( 310 | 734FBE4C1BBD5E4100313AD9 /* Base */, 311 | ); 312 | name = LaunchScreen.storyboard; 313 | sourceTree = ""; 314 | }; 315 | /* End PBXVariantGroup section */ 316 | 317 | /* Begin XCBuildConfiguration section */ 318 | 734FBE651BBD5E4100313AD9 /* Debug */ = { 319 | isa = XCBuildConfiguration; 320 | buildSettings = { 321 | ALWAYS_SEARCH_USER_PATHS = NO; 322 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 323 | CLANG_CXX_LIBRARY = "libc++"; 324 | CLANG_ENABLE_MODULES = YES; 325 | CLANG_ENABLE_OBJC_ARC = YES; 326 | CLANG_WARN_BOOL_CONVERSION = YES; 327 | CLANG_WARN_CONSTANT_CONVERSION = YES; 328 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 329 | CLANG_WARN_EMPTY_BODY = YES; 330 | CLANG_WARN_ENUM_CONVERSION = YES; 331 | CLANG_WARN_INT_CONVERSION = YES; 332 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 333 | CLANG_WARN_UNREACHABLE_CODE = YES; 334 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 335 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 336 | COPY_PHASE_STRIP = NO; 337 | DEBUG_INFORMATION_FORMAT = dwarf; 338 | ENABLE_STRICT_OBJC_MSGSEND = YES; 339 | ENABLE_TESTABILITY = YES; 340 | GCC_C_LANGUAGE_STANDARD = gnu99; 341 | GCC_DYNAMIC_NO_PIC = NO; 342 | GCC_NO_COMMON_BLOCKS = YES; 343 | GCC_OPTIMIZATION_LEVEL = 0; 344 | GCC_PREPROCESSOR_DEFINITIONS = ( 345 | "DEBUG=1", 346 | "$(inherited)", 347 | ); 348 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 349 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 350 | GCC_WARN_UNDECLARED_SELECTOR = YES; 351 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 352 | GCC_WARN_UNUSED_FUNCTION = YES; 353 | GCC_WARN_UNUSED_VARIABLE = YES; 354 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 355 | MTL_ENABLE_DEBUG_INFO = YES; 356 | ONLY_ACTIVE_ARCH = YES; 357 | SDKROOT = iphoneos; 358 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 359 | TARGETED_DEVICE_FAMILY = "1,2"; 360 | }; 361 | name = Debug; 362 | }; 363 | 734FBE661BBD5E4100313AD9 /* Release */ = { 364 | isa = XCBuildConfiguration; 365 | buildSettings = { 366 | ALWAYS_SEARCH_USER_PATHS = NO; 367 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 368 | CLANG_CXX_LIBRARY = "libc++"; 369 | CLANG_ENABLE_MODULES = YES; 370 | CLANG_ENABLE_OBJC_ARC = YES; 371 | CLANG_WARN_BOOL_CONVERSION = YES; 372 | CLANG_WARN_CONSTANT_CONVERSION = YES; 373 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 374 | CLANG_WARN_EMPTY_BODY = YES; 375 | CLANG_WARN_ENUM_CONVERSION = YES; 376 | CLANG_WARN_INT_CONVERSION = YES; 377 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 378 | CLANG_WARN_UNREACHABLE_CODE = YES; 379 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 380 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 381 | COPY_PHASE_STRIP = NO; 382 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 383 | ENABLE_NS_ASSERTIONS = NO; 384 | ENABLE_STRICT_OBJC_MSGSEND = YES; 385 | GCC_C_LANGUAGE_STANDARD = gnu99; 386 | GCC_NO_COMMON_BLOCKS = YES; 387 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 388 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 389 | GCC_WARN_UNDECLARED_SELECTOR = YES; 390 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 391 | GCC_WARN_UNUSED_FUNCTION = YES; 392 | GCC_WARN_UNUSED_VARIABLE = YES; 393 | IPHONEOS_DEPLOYMENT_TARGET = 9.0; 394 | MTL_ENABLE_DEBUG_INFO = NO; 395 | SDKROOT = iphoneos; 396 | TARGETED_DEVICE_FAMILY = "1,2"; 397 | VALIDATE_PRODUCT = YES; 398 | }; 399 | name = Release; 400 | }; 401 | 734FBE681BBD5E4100313AD9 /* Debug */ = { 402 | isa = XCBuildConfiguration; 403 | buildSettings = { 404 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 405 | INFOPLIST_FILE = "metal-watersimulation/Info.plist"; 406 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 407 | PRODUCT_BUNDLE_IDENTIFIER = "com.lachlanhurst.metal-watersimulation"; 408 | PRODUCT_NAME = "$(TARGET_NAME)"; 409 | }; 410 | name = Debug; 411 | }; 412 | 734FBE691BBD5E4100313AD9 /* Release */ = { 413 | isa = XCBuildConfiguration; 414 | buildSettings = { 415 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 416 | INFOPLIST_FILE = "metal-watersimulation/Info.plist"; 417 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 418 | PRODUCT_BUNDLE_IDENTIFIER = "com.lachlanhurst.metal-watersimulation"; 419 | PRODUCT_NAME = "$(TARGET_NAME)"; 420 | }; 421 | name = Release; 422 | }; 423 | 734FBE6B1BBD5E4100313AD9 /* Debug */ = { 424 | isa = XCBuildConfiguration; 425 | buildSettings = { 426 | BUNDLE_LOADER = "$(TEST_HOST)"; 427 | INFOPLIST_FILE = "metal-watersimulationTests/Info.plist"; 428 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 429 | PRODUCT_BUNDLE_IDENTIFIER = "com.lachlanhurst.metal-watersimulationTests"; 430 | PRODUCT_NAME = "$(TARGET_NAME)"; 431 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/metal-watersimulation.app/metal-watersimulation"; 432 | }; 433 | name = Debug; 434 | }; 435 | 734FBE6C1BBD5E4100313AD9 /* Release */ = { 436 | isa = XCBuildConfiguration; 437 | buildSettings = { 438 | BUNDLE_LOADER = "$(TEST_HOST)"; 439 | INFOPLIST_FILE = "metal-watersimulationTests/Info.plist"; 440 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 441 | PRODUCT_BUNDLE_IDENTIFIER = "com.lachlanhurst.metal-watersimulationTests"; 442 | PRODUCT_NAME = "$(TARGET_NAME)"; 443 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/metal-watersimulation.app/metal-watersimulation"; 444 | }; 445 | name = Release; 446 | }; 447 | 734FBE6E1BBD5E4100313AD9 /* Debug */ = { 448 | isa = XCBuildConfiguration; 449 | buildSettings = { 450 | INFOPLIST_FILE = "metal-watersimulationUITests/Info.plist"; 451 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 452 | PRODUCT_BUNDLE_IDENTIFIER = "com.lachlanhurst.metal-watersimulationUITests"; 453 | PRODUCT_NAME = "$(TARGET_NAME)"; 454 | TEST_TARGET_NAME = "metal-watersimulation"; 455 | USES_XCTRUNNER = YES; 456 | }; 457 | name = Debug; 458 | }; 459 | 734FBE6F1BBD5E4100313AD9 /* Release */ = { 460 | isa = XCBuildConfiguration; 461 | buildSettings = { 462 | INFOPLIST_FILE = "metal-watersimulationUITests/Info.plist"; 463 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 464 | PRODUCT_BUNDLE_IDENTIFIER = "com.lachlanhurst.metal-watersimulationUITests"; 465 | PRODUCT_NAME = "$(TARGET_NAME)"; 466 | TEST_TARGET_NAME = "metal-watersimulation"; 467 | USES_XCTRUNNER = YES; 468 | }; 469 | name = Release; 470 | }; 471 | /* End XCBuildConfiguration section */ 472 | 473 | /* Begin XCConfigurationList section */ 474 | 734FBE3A1BBD5E4000313AD9 /* Build configuration list for PBXProject "metal-watersimulation" */ = { 475 | isa = XCConfigurationList; 476 | buildConfigurations = ( 477 | 734FBE651BBD5E4100313AD9 /* Debug */, 478 | 734FBE661BBD5E4100313AD9 /* Release */, 479 | ); 480 | defaultConfigurationIsVisible = 0; 481 | defaultConfigurationName = Release; 482 | }; 483 | 734FBE671BBD5E4100313AD9 /* Build configuration list for PBXNativeTarget "metal-watersimulation" */ = { 484 | isa = XCConfigurationList; 485 | buildConfigurations = ( 486 | 734FBE681BBD5E4100313AD9 /* Debug */, 487 | 734FBE691BBD5E4100313AD9 /* Release */, 488 | ); 489 | defaultConfigurationIsVisible = 0; 490 | }; 491 | 734FBE6A1BBD5E4100313AD9 /* Build configuration list for PBXNativeTarget "metal-watersimulationTests" */ = { 492 | isa = XCConfigurationList; 493 | buildConfigurations = ( 494 | 734FBE6B1BBD5E4100313AD9 /* Debug */, 495 | 734FBE6C1BBD5E4100313AD9 /* Release */, 496 | ); 497 | defaultConfigurationIsVisible = 0; 498 | }; 499 | 734FBE6D1BBD5E4100313AD9 /* Build configuration list for PBXNativeTarget "metal-watersimulationUITests" */ = { 500 | isa = XCConfigurationList; 501 | buildConfigurations = ( 502 | 734FBE6E1BBD5E4100313AD9 /* Debug */, 503 | 734FBE6F1BBD5E4100313AD9 /* Release */, 504 | ); 505 | defaultConfigurationIsVisible = 0; 506 | }; 507 | /* End XCConfigurationList section */ 508 | }; 509 | rootObject = 734FBE371BBD5E4000313AD9 /* Project object */; 510 | } 511 | -------------------------------------------------------------------------------- /metal-watersimulation.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /metal-watersimulation.xcodeproj/xcuserdata/lachlan.xcuserdatad/xcschemes/metal-watersimulation.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 43 | 49 | 50 | 51 | 52 | 53 | 59 | 60 | 61 | 62 | 63 | 64 | 74 | 76 | 82 | 83 | 84 | 85 | 86 | 87 | 93 | 95 | 101 | 102 | 103 | 104 | 106 | 107 | 110 | 111 | 112 | -------------------------------------------------------------------------------- /metal-watersimulation.xcodeproj/xcuserdata/lachlan.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | metal-watersimulation.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 734FBE3E1BBD5E4000313AD9 16 | 17 | primary 18 | 19 | 20 | 734FBE521BBD5E4100313AD9 21 | 22 | primary 23 | 24 | 25 | 734FBE5D1BBD5E4100313AD9 26 | 27 | primary 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /metal-watersimulation/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // metal-watersimulation 4 | // 5 | // Created by Lachlan Hurst on 1/10/2015. 6 | // Copyright © 2015 Lachlan Hurst. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(application: UIApplication) { 33 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /metal-watersimulation/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "ipad", 35 | "size" : "29x29", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "ipad", 40 | "size" : "29x29", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "40x40", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "40x40", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "76x76", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "76x76", 61 | "scale" : "2x" 62 | } 63 | ], 64 | "info" : { 65 | "version" : 1, 66 | "author" : "xcode" 67 | } 68 | } -------------------------------------------------------------------------------- /metal-watersimulation/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /metal-watersimulation/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /metal-watersimulation/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /metal-watersimulation/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // metal-watersimulation 4 | // 5 | // Created by Lachlan Hurst on 1/10/2015. 6 | // Copyright © 2015 Lachlan Hurst. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import SceneKit 11 | 12 | class ViewController: UIViewController, SCNSceneRendererDelegate { 13 | 14 | 15 | @IBOutlet var scenekitView: SCNView! 16 | 17 | var device:MTLDevice! 18 | var threadGroupCount:MTLSize! 19 | var threadGroups: MTLSize! 20 | var pipelineState: MTLComputePipelineState! 21 | var defaultLibrary: MTLLibrary! = nil 22 | var commandQueue: MTLCommandQueue! = nil 23 | 24 | var textures:[MTLTexture] = [] 25 | var quadMaterial:SCNMaterial! 26 | 27 | let quadSizeX:Float = 100 28 | let quadSizeZ:Float = 200 29 | 30 | let textureSizeX:Int = 100 * 2 31 | let textureSizeY:Int = 200 * 2 32 | 33 | 34 | let bytesPerPixel = Int(4) 35 | let bitsPerComponent = Int(8) 36 | let bitsPerPixel:Int = 32 37 | let rgbColorSpace = CGColorSpaceCreateDeviceRGB() 38 | 39 | 40 | func setupScene() { 41 | let scene = SCNScene() 42 | 43 | let quad = SCNPlane(width: CGFloat(quadSizeX), height: CGFloat(quadSizeZ)) 44 | quadMaterial = quad.materials.first 45 | 46 | let quadNode = SCNNode(geometry: quad) 47 | //quadNode.rotation = SCNVector4Make(1, 0, 0, -30 * Float(M_PI) / 180) 48 | scene.rootNode.addChildNode(quadNode) 49 | 50 | scenekitView.scene = scene 51 | } 52 | 53 | 54 | func setupMetal() { 55 | if self.scenekitView.renderingAPI == SCNRenderingAPI.Metal { 56 | 57 | device = scenekitView.device 58 | 59 | defaultLibrary = device.newDefaultLibrary() 60 | commandQueue = device.newCommandQueue() 61 | 62 | let kernelFunction = defaultLibrary.newFunctionWithName("waveShader") 63 | 64 | do { 65 | pipelineState = try! device.newComputePipelineStateWithFunction(kernelFunction!) 66 | } 67 | 68 | threadGroupCount = MTLSizeMake(16, 16, 1) 69 | threadGroups = MTLSizeMake(Int(textureSizeX) / threadGroupCount.width+1, Int(textureSizeY) / threadGroupCount.height + 1, 1) 70 | 71 | self.scenekitView.delegate = self 72 | } 73 | } 74 | 75 | func setupTextures() { 76 | var rawData0 = [UInt8](count: Int(textureSizeX) * Int(textureSizeY) * 4, repeatedValue: 0) 77 | var rawData1 = [UInt8](count: Int(textureSizeX) * Int(textureSizeY) * 4, repeatedValue: 0) 78 | var rawData2 = [UInt8](count: Int(textureSizeX) * Int(textureSizeY) * 4, repeatedValue: 0) 79 | 80 | let bytesPerRow = 4 * Int(textureSizeX) 81 | let bitmapInfo = CGBitmapInfo.ByteOrder32Big.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue 82 | 83 | let context = CGBitmapContextCreate(&rawData0, Int(textureSizeX), Int(textureSizeY), bitsPerComponent, bytesPerRow, rgbColorSpace, bitmapInfo) 84 | CGContextSetFillColorWithColor(context, UIColor.blackColor().CGColor) 85 | CGContextFillRect(context, CGRectMake(0, 0, CGFloat(textureSizeX), CGFloat(textureSizeY))) 86 | 87 | let contextB = CGBitmapContextCreate(&rawData1, Int(textureSizeX), Int(textureSizeY), bitsPerComponent, bytesPerRow, rgbColorSpace, bitmapInfo) 88 | CGContextSetFillColorWithColor(contextB, UIColor.blackColor().CGColor) 89 | CGContextFillRect(contextB, CGRectMake(0, 0, CGFloat(textureSizeX), CGFloat(textureSizeY))) 90 | 91 | let contextC = CGBitmapContextCreate(&rawData2, Int(textureSizeX), Int(textureSizeY), bitsPerComponent, bytesPerRow, rgbColorSpace, bitmapInfo) 92 | CGContextSetFillColorWithColor(contextC, UIColor.blackColor().CGColor) 93 | CGContextFillRect(contextC, CGRectMake(0, 0, CGFloat(textureSizeX), CGFloat(textureSizeY))) 94 | 95 | let textureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(MTLPixelFormat.RGBA8Unorm, width: Int(textureSizeX), height: Int(textureSizeY), mipmapped: false) 96 | 97 | let textureA = device.newTextureWithDescriptor(textureDescriptor) 98 | textureA.label = "A" 99 | 100 | let outTextureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(textureA.pixelFormat, width: textureA.width, height: textureA.height, mipmapped: false) 101 | let textureB = device.newTextureWithDescriptor(outTextureDescriptor) 102 | textureB.label = "B" 103 | let out2TextureDescriptor = MTLTextureDescriptor.texture2DDescriptorWithPixelFormat(textureA.pixelFormat, width: textureA.width, height: textureA.height, mipmapped: false) 104 | let textureC = device.newTextureWithDescriptor(out2TextureDescriptor) 105 | textureC.label = "C" 106 | 107 | let region = MTLRegionMake2D(0, 0, Int(textureSizeX), Int(textureSizeY)) 108 | textureA.replaceRegion(region, mipmapLevel: 0, withBytes: &rawData0, bytesPerRow: Int(bytesPerRow)) 109 | textureC.replaceRegion(region, mipmapLevel: 0, withBytes: &rawData1, bytesPerRow: Int(bytesPerRow)) 110 | textureB.replaceRegion(region, mipmapLevel: 0, withBytes: &rawData2, bytesPerRow: Int(bytesPerRow)) 111 | 112 | self.textures = [textureA,textureB, textureC] 113 | } 114 | 115 | func renderer(renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: NSTimeInterval) { 116 | 117 | let commandBuffer = commandQueue.commandBuffer() 118 | let commandEncoder = commandBuffer.computeCommandEncoder() 119 | 120 | commandEncoder.setComputePipelineState(pipelineState) 121 | 122 | commandQueue = device.newCommandQueue() 123 | 124 | commandEncoder.setTexture(textures[0], atIndex: 0) 125 | commandEncoder.setTexture(textures[1], atIndex: 1) 126 | commandEncoder.setTexture(textures[2], atIndex: 2) 127 | 128 | commandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount) 129 | 130 | commandEncoder.endEncoding() 131 | commandBuffer.commit() 132 | //commandBuffer.waitUntilCompleted() 133 | 134 | quadMaterial.diffuse.contents = textures[1] 135 | 136 | 137 | let first = textures.removeFirst() 138 | textures.append(first) 139 | 140 | } 141 | 142 | func doSplash(x:Int, y:Int) { 143 | //x,y in texture pixel coordinates 144 | 145 | let bytesPerRow = 4 * 2 146 | let bitmapInfo = CGBitmapInfo.ByteOrder32Big.rawValue | CGImageAlphaInfo.PremultipliedLast.rawValue 147 | var rawData = [UInt8](count: 2 * 2 * 4, repeatedValue: 1) 148 | 149 | 150 | let context = CGBitmapContextCreate(&rawData, Int(2), Int(2), bitsPerComponent, bytesPerRow, rgbColorSpace, bitmapInfo) 151 | CGContextSetFillColorWithColor(context, UIColor.redColor().CGColor) 152 | CGContextFillRect(context, CGRectMake(0, 0, 2, 2)) 153 | 154 | let tex0 = textures[0] 155 | let tex1 = textures[1] 156 | let tex2 = textures[2] 157 | 158 | let region = MTLRegionMake2D(x, y, 2, 2) 159 | 160 | tex0.replaceRegion(region, mipmapLevel: 0, withBytes: &rawData, bytesPerRow: bytesPerRow) 161 | tex1.replaceRegion(region, mipmapLevel: 0, withBytes: &rawData, bytesPerRow: bytesPerRow) 162 | tex2.replaceRegion(region, mipmapLevel: 0, withBytes: &rawData, bytesPerRow: bytesPerRow) 163 | } 164 | 165 | func doTouches(touches: Set) { 166 | 167 | for touch in touches { 168 | let pt = touch.locationInView(self.scenekitView) 169 | 170 | let hits = self.scenekitView.hitTest(pt, options: nil) 171 | 172 | for hit in hits { 173 | var x = Int(hit.localCoordinates.x + quadSizeX/2) * textureSizeX / Int(quadSizeX) 174 | if x >= textureSizeX - 4 { 175 | x = textureSizeX - 4 176 | } 177 | 178 | var y = textureSizeY - Int(hit.localCoordinates.y + quadSizeZ/2) * textureSizeY / Int(quadSizeZ) 179 | if y >= textureSizeY - 4 { 180 | y = textureSizeY - 4 181 | } 182 | 183 | doSplash(x, y: y) 184 | } 185 | } 186 | } 187 | 188 | override func touchesBegan(touches: Set, withEvent event: UIEvent?) { 189 | doTouches(touches) 190 | } 191 | 192 | override func touchesMoved(touches: Set, withEvent event: UIEvent?) { 193 | doTouches(touches) 194 | } 195 | 196 | override func viewDidAppear(animated: Bool) { 197 | super.viewDidAppear(animated) 198 | 199 | doSplash(textureSizeX / 2, y: textureSizeY / 2) 200 | } 201 | 202 | 203 | override func viewDidLoad() { 204 | super.viewDidLoad() 205 | 206 | setupScene() 207 | setupMetal() 208 | setupTextures() 209 | 210 | scenekitView.backgroundColor = UIColor.lightGrayColor() 211 | //scenekitView.allowsCameraControl = true 212 | //scenekitView.debugOptions = .ShowWireframe 213 | scenekitView.showsStatistics = true 214 | scenekitView.autoenablesDefaultLighting = true 215 | 216 | scenekitView.playing = true 217 | } 218 | 219 | override func didReceiveMemoryWarning() { 220 | super.didReceiveMemoryWarning() 221 | // Dispose of any resources that can be recreated. 222 | } 223 | 224 | 225 | } 226 | 227 | -------------------------------------------------------------------------------- /metal-watersimulation/waveComputeShader.metal: -------------------------------------------------------------------------------- 1 | // 2 | // waveComputeShader.metal 3 | // water-shader 4 | // 5 | // Created by Lachlan Hurst on 1/10/2015. 6 | // Copyright © 2015 Lachlan Hurst. All rights reserved. 7 | // 8 | 9 | #include 10 | using namespace metal; 11 | 12 | kernel void waveShader(texture2d inPrevTexture [[texture(0)]], 13 | texture2d inTexture [[texture(1)]], 14 | texture2d outTexture [[texture(2)]], 15 | uint2 gid [[thread_position_in_grid]]) 16 | { 17 | const uint2 centerIndex(gid.x, gid.y); 18 | const uint2 northIndex(gid.x, gid.y - 1); 19 | const uint2 eastIndex(gid.x + 1, gid.y); 20 | const uint2 southIndex(gid.x, gid.y + 1); 21 | const uint2 westIndex(gid.x - 1, gid.y); 22 | 23 | const float centerColor = inPrevTexture.read(centerIndex).r; 24 | const float northColor = inTexture.read(northIndex).r; 25 | const float southColor = inTexture.read(southIndex).r; 26 | const float westColor = inTexture.read(westIndex).r; 27 | const float eastColor = inTexture.read(eastIndex).r; 28 | 29 | const float res = ((northColor + southColor + westColor + eastColor) / 2.0 - centerColor) * 0.9999; 30 | 31 | //const float4 outColor(res, step(0.11,res), step(0.13,res), 1); 32 | const float4 outColor(res, res, res, 1); 33 | outTexture.write(outColor, gid); 34 | } -------------------------------------------------------------------------------- /metal-watersimulationTests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /metal-watersimulationTests/metal_watersimulationTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // metal_watersimulationTests.swift 3 | // metal-watersimulationTests 4 | // 5 | // Created by Lachlan Hurst on 1/10/2015. 6 | // Copyright © 2015 Lachlan Hurst. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import metal_watersimulation 11 | 12 | class metal_watersimulationTests: XCTestCase { 13 | 14 | override func setUp() { 15 | super.setUp() 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | } 18 | 19 | override func tearDown() { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | super.tearDown() 22 | } 23 | 24 | func testExample() { 25 | // This is an example of a functional test case. 26 | // Use XCTAssert and related functions to verify your tests produce the correct results. 27 | } 28 | 29 | func testPerformanceExample() { 30 | // This is an example of a performance test case. 31 | self.measureBlock { 32 | // Put the code you want to measure the time of here. 33 | } 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /metal-watersimulationUITests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /metal-watersimulationUITests/metal_watersimulationUITests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // metal_watersimulationUITests.swift 3 | // metal-watersimulationUITests 4 | // 5 | // Created by Lachlan Hurst on 1/10/2015. 6 | // Copyright © 2015 Lachlan Hurst. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | 11 | class metal_watersimulationUITests: XCTestCase { 12 | 13 | override func setUp() { 14 | super.setUp() 15 | 16 | // Put setup code here. This method is called before the invocation of each test method in the class. 17 | 18 | // In UI tests it is usually best to stop immediately when a failure occurs. 19 | continueAfterFailure = false 20 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. 21 | XCUIApplication().launch() 22 | 23 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 24 | } 25 | 26 | override func tearDown() { 27 | // Put teardown code here. This method is called after the invocation of each test method in the class. 28 | super.tearDown() 29 | } 30 | 31 | func testExample() { 32 | // Use recording to get started writing UI tests. 33 | // Use XCTAssert and related functions to verify your tests produce the correct results. 34 | } 35 | 36 | } 37 | --------------------------------------------------------------------------------