From e85fc9e1ac53b98b345f7e26099c21a7fd17f89d Mon Sep 17 00:00:00 2001 From: fatherdougalmaguire <144415904+fatherdougalmaguire@users.noreply.github.com> Date: Sun, 2 Feb 2025 13:08:51 +1100 Subject: [PATCH] v0.25 Arbitrary core and core cell size in shader Shader will now draw a core with arbitrary grid size and x and y dimensions --- LifeOnMars.xcodeproj/project.pbxproj | 4 +- .../xcdebugger/Breakpoints_v2.xcbkptlist | 84 ++++------ LifeOnMars/ContentView.swift | 72 ++++++--- LifeOnMars/EmulatorCore.swift | 31 +++- LifeOnMars/buffer.metal | 147 ++++++++++++++++-- 5 files changed, 247 insertions(+), 91 deletions(-) diff --git a/LifeOnMars.xcodeproj/project.pbxproj b/LifeOnMars.xcodeproj/project.pbxproj index 4b56148..4de8f35 100644 --- a/LifeOnMars.xcodeproj/project.pbxproj +++ b/LifeOnMars.xcodeproj/project.pbxproj @@ -302,7 +302,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 14.6; - MARKETING_VERSION = 0.241; + MARKETING_VERSION = 0.25; PRODUCT_BUNDLE_IDENTIFIER = com.fatherdougalmaguire.LifeOnMars; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; @@ -333,7 +333,7 @@ "@executable_path/../Frameworks", ); MACOSX_DEPLOYMENT_TARGET = 14.6; - MARKETING_VERSION = 0.241; + MARKETING_VERSION = 0.25; PRODUCT_BUNDLE_IDENTIFIER = com.fatherdougalmaguire.LifeOnMars; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; diff --git a/LifeOnMars.xcodeproj/xcuserdata/tony.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/LifeOnMars.xcodeproj/xcuserdata/tony.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 2b23c9e..ed016d9 100644 --- a/LifeOnMars.xcodeproj/xcuserdata/tony.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/LifeOnMars.xcodeproj/xcuserdata/tony.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -14,8 +14,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "322" - endingLineNumber = "322" + startingLineNumber = "341" + endingLineNumber = "341" landmarkName = "LoadCore()" landmarkType = "7"> @@ -30,8 +30,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "945" - endingLineNumber = "945" + startingLineNumber = "964" + endingLineNumber = "964" landmarkName = "CoreStepExecute()" landmarkType = "7"> @@ -46,8 +46,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "942" - endingLineNumber = "942" + startingLineNumber = "961" + endingLineNumber = "961" landmarkName = "CoreStepExecute()" landmarkType = "7"> @@ -62,8 +62,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "402" - endingLineNumber = "402" + startingLineNumber = "421" + endingLineNumber = "421" landmarkName = "LoadCore()" landmarkType = "7"> @@ -78,8 +78,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "403" - endingLineNumber = "403" + startingLineNumber = "422" + endingLineNumber = "422" landmarkName = "LoadCore()" landmarkType = "7"> @@ -94,8 +94,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "363" - endingLineNumber = "363" + startingLineNumber = "382" + endingLineNumber = "382" landmarkName = "LoadCore()" landmarkType = "7"> @@ -126,8 +126,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "212" - endingLineNumber = "212" + startingLineNumber = "231" + endingLineNumber = "231" landmarkName = "FormatCoreOutput(_:)" landmarkType = "7"> @@ -142,8 +142,8 @@ filePath = "LifeOnMars/EmulatorCore.swift" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "533" - endingLineNumber = "533" + startingLineNumber = "552" + endingLineNumber = "552" landmarkName = "CoreStepExecute()" landmarkType = "7"> @@ -151,47 +151,31 @@ - - - - @@ -199,15 +183,15 @@ @@ -215,32 +199,32 @@ diff --git a/LifeOnMars/ContentView.swift b/LifeOnMars/ContentView.swift index b5b08da..12a2cd6 100644 --- a/LifeOnMars/ContentView.swift +++ b/LifeOnMars/ContentView.swift @@ -9,19 +9,20 @@ import SwiftUI struct CoreDisplayView: View { - + @EnvironmentObject var ThisEmulatorCore : EmulatorCore - + var body: some View { - + VStack { TimelineView(.animation) { context in Rectangle() - .fill(.red) - .frame(width: 100.0, height: 80.0) - .colorEffect(ShaderLibrary.DrawCore(.floatArray(ThisEmulatorCore.CoreBuffer))) - .scaleEffect(x: 7.0, y:7.0) + .frame(width: CGFloat(ThisEmulatorCore.CoreSizeInCols*(ThisEmulatorCore.CoreCellSize+1)), height: CGFloat(ThisEmulatorCore.CoreSizeInRows*(ThisEmulatorCore.CoreCellSize+1))) + .colorEffect(ShaderLibrary.DrawCore1(.floatArray(ThisEmulatorCore.CoreBuffer),.float(Float(ThisEmulatorCore.CoreSizeInCols)),.float(Float(ThisEmulatorCore.CoreSizeInRows)),.float(Float(ThisEmulatorCore.CoreCellSize)))) +// .frame(width: 100.0, height: 80.0) +// .colorEffect(ShaderLibrary.DrawCore(.floatArray(ThisEmulatorCore.CoreBuffer))) +// .scaleEffect(x: 7.0, y:7.0) .onChange(of: context.date) { ThisEmulatorCore.CoreExecute() @@ -31,6 +32,31 @@ struct CoreDisplayView: View { } //End body } // End CoreDisplayView +struct oldthing: View { + + @EnvironmentObject var ThisEmulatorCore : EmulatorCore + + var body: some View { + + HStack { + Grid(horizontalSpacing: 1.0, verticalSpacing: 1.0) { + ForEach(0..<10) { MyIndexRow in + GridRow { + ForEach(0..<10) { MyIndexCol in + Rectangle().fill(ThisEmulatorCore.Core[(MyIndexRow*10)+MyIndexCol].InstructionColour) + } // End ForEach + } // End GridRow + .frame(width: 10.0, height: 10.0) + } // End ForEach + } // End Grid + Rectangle() + .frame(width: CGFloat(ThisEmulatorCore.CoreSizeInCols*(ThisEmulatorCore.CoreCellSize+1)), height: CGFloat(ThisEmulatorCore.CoreSizeInRows*(ThisEmulatorCore.CoreCellSize+1))) + .colorEffect(ShaderLibrary.DrawCore1(.floatArray(ThisEmulatorCore.CoreBuffer),.float(Float(ThisEmulatorCore.CoreSizeInCols)),.float(Float(ThisEmulatorCore.CoreSizeInRows)),.float(Float(ThisEmulatorCore.CoreCellSize)))) + } // End VStack + //.background(.blue) + } //End body +} // End CoreDisplayView + #Preview { CoreDisplayView() } // End Preview @@ -129,21 +155,23 @@ struct ContentView: View { @StateObject var ThisEmulatorCore = EmulatorCore() var body: some View { -// ZStack -// { -// Color.white.ignoresSafeArea() - CoreDisplayView().environmentObject(ThisEmulatorCore).frame(width:1000,height:560,alignment: .top) -// Divider() -// HStack -// { - CoreMemoryView().environmentObject(ThisEmulatorCore).frame(width:1000,height:120) - // Divider() - WarriorDisplayView().environmentObject(ThisEmulatorCore).frame(width:1000,height:80) - // Divider() - // } // End HStack -// Spacer() - WarriorControlView().environmentObject(ThisEmulatorCore).frame(width:1000,height:50,alignment: .bottom) - // } // End ZStack + + //oldthing().environmentObject(ThisEmulatorCore).frame(width:1000,height:560,alignment: .top) + // ZStack + // { + // Color.white.ignoresSafeArea() + CoreDisplayView().environmentObject(ThisEmulatorCore).frame(width:1000,height:560,alignment: .top) + // Divider() + // HStack + // { + CoreMemoryView().environmentObject(ThisEmulatorCore).frame(width:1000,height:120) + // Divider() + WarriorDisplayView().environmentObject(ThisEmulatorCore).frame(width:1000,height:80) + // Divider() + // } // End HStack + // Spacer() + WarriorControlView().environmentObject(ThisEmulatorCore).frame(width:1000,height:50,alignment: .bottom) + // } // End ZStack } // End body } // End ContentView diff --git a/LifeOnMars/EmulatorCore.swift b/LifeOnMars/EmulatorCore.swift index e408b3c..c7134ca 100644 --- a/LifeOnMars/EmulatorCore.swift +++ b/LifeOnMars/EmulatorCore.swift @@ -93,7 +93,7 @@ class EmulatorCore : ObservableObject { var CoreSize: Int = 8000 var CoreSizeInRows: Int var CoreSizeInCols: Int - var CoreCellSize: Double + var CoreCellSize: Int var CoreDrawSize : Int var CoreUpdateFreq : Double = 0.1 @@ -117,22 +117,41 @@ class EmulatorCore : ObservableObject { else if self.CoreSize <= 1000 { self.CoreSizeInRows = 20 - self.CoreSizeInCols = 50 - self.CoreCellSize = 10 + self.CoreSizeInCols = 50 +// self.CoreSizeInRows = 20 +// self.CoreSizeInCols = 20 + self.CoreCellSize = 15 CoreDrawSize = self.CoreSizeInRows*self.CoreSizeInCols } else { - self.CoreSizeInRows = 70 - self.CoreSizeInCols = 120 - self.CoreCellSize = 10 + self.CoreSizeInRows = 64 + self.CoreSizeInCols = 125 + self.CoreCellSize = 7 CoreDrawSize = self.CoreSizeInRows*self.CoreSizeInCols } self.Core = Array(repeating: RedCodeInstruction(),count:CoreSize) self.CoreBuffer = Array(repeating: 0.0,count:CoreSize) for MyIndex in 0.. using namespace metal; +[[ stitchable ]] half4 DrawCore1(float2 position, half4 color, device const float *CoreBuffer1, int CoreSize1, float GridCol, float GridRow, float GridSize) +{ + half4 PixelColour; + int CoreValue; + // int ColourMatch = 0; + //int MyGridRow = int(GridRow); + int MyGridCol = int(GridCol); + int MyGridSize = int(GridSize); + + // if (int(position.x) % 11 == 0 || int(position.y) % 11 == 0 ) + if (int(position.x) % (MyGridSize+1) == 0 || int(position.y) % (MyGridSize+1) == 0 ) + { + PixelColour = half4(255,255,255,1); + // ColourMatch = 1; + } + else + { + // CoreValue = int(CoreBuffer1[(int(position.y)/11*10) + int(position.x)/11]); + CoreValue = int(CoreBuffer1[(int(position.y)/(MyGridSize+1)*MyGridCol) + int(position.x)/(MyGridSize+1)]); + switch (CoreValue) + { + case 0 : PixelColour = half4(0,0,0,1);break; + case 1 : PixelColour = half4(255,0,0,1);break; + case 2 : PixelColour = half4(0,255,0,1); + } + // ColourMatch = 1; + } + +// // if (int(position.x) >= 1 && int(position.y) >= 1 && int(position.x) <= 10 && int(position.y) <= 10) +// // { +// // thingy = half4(0,255,0,1); +// // ColourMatch = 1; +// // } // Corebuffer[0] +// +// if (int(position.x) >= 12 && int(position.y) >= 1 && int(position.x) <= 21 && int(position.y) <= 10) +// { +// thingy = half4(0,255,0,1); +// ColourMatch = 1; +// } // Corebuffer[1] +// +// if (int(position.x) >= 23 && int(position.y) >= 1 && int(position.x) <= 32 && int(position.y) <= 10) +// { +// thingy = half4(0,255,0,1); +// ColourMatch = 1; +// } // Corebuffer[2] +// +// if (int(position.x) >= 34 && int(position.y) >= 1 && int(position.x) <= 43 && int(position.y) <= 10) +// { +// thingy = half4(0,255,0,1); +// ColourMatch = 1; +// } // Corebuffer[3] +// +// if (int(position.x) >= 45 && int(position.y) >= 1 && int(position.x) <= 54 && int(position.y) <= 10) +// { +// thingy = half4(0,255,0,1); +// ColourMatch = 1; +// } // Corebuffer[4] +// +// if (int(position.x) >= 56 && int(position.y) >= 1 && int(position.x) <= 65 && int(position.y) <= 10) +// { +// thingy = half4(0,255,0,1); +// ColourMatch = 1; +// } // Corebuffer[5] +// +// if (int(position.x) >= 67 && int(position.y) >= 1 && int(position.x) <= 76 && int(position.y) <= 10) +// { +// thingy = half4(0,0,255,1); +// ColourMatch = 1; +// } // Corebuffer[6] +// +// if (int(position.x) >= 78 && int(position.y) >= 1 && int(position.x) <= 87 && int(position.y) <= 10) +// { +// thingy = half4(0,0,255,1); +// ColourMatch = 1; +// } // Corebuffer[7] +// +// if (int(position.x) >= 89 && int(position.y) >= 1 && int(position.x) <= 98 && int(position.y) <= 10) +// { +// thingy = half4(0,0,255,1); +// ColourMatch = 1; +// } // Corebuffer[8] +// +// if (int(position.x) >= 100 && int(position.y) >= 1 && int(position.x) <= 109 && int(position.y) <= 10) +// { +// thingy = half4(0,0,255,1); +// ColourMatch = 1; +// } // Corebuffer[9] +// +// if (int(position.x) >= 1 && int(position.y) >= 34 && int(position.x) <= 10 && int(position.y) <= 43) +// { +// thingy = half4(255,0,0,1); +// ColourMatch = 1; +// } // Corebuffer[30] +// +// if (int(position.x) >= 12 && int(position.y) >= 34 && int(position.x) <= 21 && int(position.y) <= 43) +// { +// thingy = half4(255,0,0,1); +// ColourMatch = 1; +// } // Corebuffer[31] +// +// if (int(position.x) >= 23 && int(position.y) >= 34 && int(position.x) <= 32 && int(position.y) <= 43) +// { +// thingy = half4(255,0,0,1); +// ColourMatch = 1; +// } // Corebuffer[32] +// +// if (int(position.x) >= 34 && int(position.y) >= 34 && int(position.x) <= 43 && int(position.y) <= 43) +// { +// thingy = half4(255,0,0,1); +// ColourMatch = 1; +// } // Corebuffer[33] +// +// // if (int(position.x) >= 45 && int(position.y) >= 1 && int(position.x) <= 54 && int(position.y) <= 10) +// // { +// // thingy = half4(255,0,0,1); +// // ColourMatch = 1; +// // } // Corebuffer[34] + +// if (ColourMatch == 0) +// +// { +// PixelColour = half4(0,0,0,1); } + + return PixelColour; +} + [[ stitchable ]] half4 DrawCore(float2 position, half4 color, device const float *CoreBuffer1, int CoreSize1) { half4 thingy; int CoreValue; - + CoreValue = int(CoreBuffer1[int(position.y) * 100 + int(position.x)]); - + switch (CoreValue) { - case 0: // empty - thingy = half4(0,0,0,1); - break; - case 1: // red + case 0: // empty + thingy = half4(0,0,0,1); + break; + case 1: // red thingy = half4(255, 0, 0,1); - break; - case 2: //green - thingy = half4(0,255,0,1); - break; + break; + case 2: //green + thingy = half4(0,255,0,1); + break; } return thingy; } -