This commit is contained in:
Mat Cegiela 2024-01-03 17:24:03 -06:00 committed by GitHub
commit a827d8a7de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 42 additions and 24 deletions

View File

@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate {
}
func registerShaders() {
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return }
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return }
do {
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@ -29,7 +29,7 @@ public class MetalView: NSObject, MTKViewDelegate {
}
func registerShaders() {
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
let input: String?
let library: MTLLibrary
let vert_func: MTLFunction

View File

@ -43,7 +43,7 @@ public class MetalView: NSObject, MTKViewDelegate {
}
func createPipeline() {
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
let input: String?
let library: MTLLibrary
let vert_func: MTLFunction

View File

@ -15,7 +15,7 @@ public class MetalView: NSObject, MTKViewDelegate {
func registerShaders() {
device = MTLCreateSystemDefaultDevice()!
queue = device.makeCommandQueue()
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)

View File

@ -15,7 +15,7 @@ public class MetalView: NSObject, MTKViewDelegate {
func registerShaders() {
device = MTLCreateSystemDefaultDevice()!
queue = device.makeCommandQueue()
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)

View File

@ -56,7 +56,7 @@ public class MetalView: MTKView, NSWindowDelegate {
func registerShaders() {
queue = device!.makeCommandQueue()
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device!.makeLibrary(source: input, options: nil)

View File

@ -55,7 +55,7 @@ public class MetalView: MTKView, NSWindowDelegate {
func registerShaders() {
queue = device!.makeCommandQueue()
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device!.makeLibrary(source: input, options: nil)

View File

@ -43,7 +43,7 @@ public class MetalView: MTKView, NSWindowDelegate {
func registerShaders() {
queue = device!.makeCommandQueue()
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device!.makeLibrary(source: input, options: nil)

View File

@ -25,7 +25,7 @@ public class MetalView: NSObject, MTKViewDelegate {
}
func registerShaders() {
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device!.makeLibrary(source: input, options: nil)

View File

@ -42,7 +42,7 @@ public class Render : NSObject, MTKViewDelegate {
}
class func buildRenderPipelineWithDevice(device: MTLDevice, view: MTKView) throws -> MTLRenderPipelineState {
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { fatalError() }
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { fatalError() }
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)
let vertexFunction = library.makeFunction(name: "vertex_transform")
@ -54,7 +54,7 @@ public class Render : NSObject, MTKViewDelegate {
pipelineDescriptor.colorAttachments[0].pixelFormat = .bgra8Unorm
return try device.makeRenderPipelineState(descriptor: pipelineDescriptor)
}
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {}
public func draw(in view: MTKView) {

View File

@ -0,0 +1,16 @@
#include <metal_stdlib>
using namespace metal;
kernel void k(texture2d<float,access::write> o[[texture(0)]],
uint2 gid[[thread_position_in_grid]]) {
int width = o.get_width();
int height = o.get_height();
float2 uv = float2(gid) / float2(width, height);
float3 color = mix(float3(1.0, 0.6, 0.1), float3(0.5, 0.8, 1.0), sqrt(1 - uv.y));
float2 q = uv - float2(0.67, 0.25);
float r = 0.2 + 0.1 * cos(atan2(q.x, q.y) * 9.0 + 20.0 * q.x);
color *= smoothstep(r, r + 0.01, length(q));
r = 0.03 + 0.002 * cos(120.0 * q.y) + exp(-50.0 * (1.0 - uv.y));
color *= 1.0 - (1.0 - smoothstep(r, r + 0.002, abs(q.x - 0.25 * sin(2.0 * q.y)))) * smoothstep(0.0, 0.1, q.y);
o.write(float4(color, 1.0), gid);
}

View File

@ -13,7 +13,9 @@ public class MetalView: NSObject, MTKViewDelegate {
view.colorPixelFormat = .bgra8Unorm
device = MTLCreateSystemDefaultDevice()!
commandQueue = device.makeCommandQueue()
let library = try! device.makeLibrary(source: shader, options: nil)
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { fatalError() }
let input = try! String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try! device.makeLibrary(source: input, options: nil)
let function = library.makeFunction(name:"k")!
cps = try! device.makeComputePipelineState(function: function)

View File

@ -18,7 +18,7 @@ public class Renderer: NSObject, MTKViewDelegate {
queue = device.makeCommandQueue()
let textureLoader = MTKTextureLoader(device: device)
let url = Bundle.main.url(forResource: "nature", withExtension: "jpg")!
guard let file = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return }
guard let file = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return }
do {
let source = try String(contentsOfFile: file, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: source, options: nil)

View File

@ -1,2 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='macos' executeOnSourceChanges='false'/>
<playground version='6.0' target-platform='macos'/>

View File

@ -11,7 +11,7 @@ guard let outBuffer = device.makeBuffer(bytes: myVector, length: length, options
for (index, _) in myVector.enumerated() { myVector[index] = Float(index) }
var inBuffer = device.makeBuffer(bytes: myVector, length: length, options: [])
let path = Bundle.main.path(forResource: "memory", ofType: "metal")
let path = Bundle.main.path(forResource: "memory", ofType: "txt")
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)
let function = library.makeFunction(name: "compute")!

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' executeOnSourceChanges='false'>
<playground version='5.0' target-platform='macos'>
<timeline fileName='timeline.xctimeline'/>
</playground>

View File

@ -3,7 +3,7 @@
version = "3.0">
<TimelineItems>
<LoggerValueHistoryTimelineItem
documentLocation = "file:///Users/mhorga/Desktop/metal/memory/memory_1.playground#CharacterRangeLen=2&amp;CharacterRangeLoc=1554&amp;EndingColumnNumber=14&amp;EndingLineNumber=32&amp;StartingColumnNumber=12&amp;StartingLineNumber=32&amp;Timestamp=528665184.878259"
documentLocation = "file:///Users/matcegiela/Code/Metal/metal/memory/memory_1.playground#CharacterRangeLen=2&amp;CharacterRangeLoc=1552&amp;EndingColumnNumber=14&amp;EndingLineNumber=32&amp;StartingColumnNumber=12&amp;StartingLineNumber=32&amp;Timestamp=683824809.957611"
selectedRepresentationIndex = "0"
shouldTrackSuperviewWidth = "NO">
</LoggerValueHistoryTimelineItem>

View File

@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate {
}
func registerShaders() {
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return }
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return }
do {
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)

View File

@ -43,7 +43,7 @@ public class MetalViewDelegate: NSObject, MTKViewDelegate {
initializeBuffers()
let library: MTLLibrary
do {
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
let source = try String(contentsOfFile: path!, encoding: .utf8)
library = try device.makeLibrary(source: source, options: nil)
let descriptor = MTLRenderPipelineDescriptor()

View File

@ -35,7 +35,7 @@ public class MetalView: NSObject, MTKViewDelegate {
func initializeMetal() {
device = MTLCreateSystemDefaultDevice()
queue = device.makeCommandQueue()
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return }
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return }
do {
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)

View File

@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate {
func registerShaders() {
device = MTLCreateSystemDefaultDevice()!
queue = device.makeCommandQueue()
let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
let path = Bundle.main.path(forResource: "Shaders", ofType: "txt")
do {
let input = try String(contentsOfFile: path!, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)

View File

@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate {
}
func registerShaders() {
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return }
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return }
do {
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)

View File

@ -17,7 +17,7 @@ public class MetalView: NSObject, MTKViewDelegate {
}
func registerShaders() {
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "metal") else { return }
guard let path = Bundle.main.path(forResource: "Shaders", ofType: "txt") else { return }
do {
let input = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
let library = try device.makeLibrary(source: input, options: nil)