compile metal in cmake

This commit is contained in:
Alex Zenla 2024-08-05 00:08:16 -07:00
parent 1fe9578b3a
commit bc9289474b
Signed by: alex
GPG Key ID: C0780728420EBFE5
6 changed files with 62 additions and 15 deletions

View File

@ -25,10 +25,10 @@ public class Application {
// Create SDL window
var windowFlags = SDL_WindowFlags(0)
if (cfg.flags.contains(.resizable)) {
if cfg.flags.contains(.resizable) {
windowFlags |= SDL_WindowFlags(SDL_WINDOW_RESIZABLE)
}
if (cfg.flags.contains(.highDPI)) {
if cfg.flags.contains(.highDPI) {
windowFlags |= SDL_WindowFlags(SDL_WINDOW_HIGH_PIXEL_DENSITY)
}
window = SDL_CreateWindow(cfg.title, cfg.width, cfg.height, windowFlags)
@ -174,7 +174,7 @@ fileprivate enum ApplicationExecutionState {
case running
}
extension FileHandle: TextOutputStream {
extension FileHandle: @retroactive TextOutputStream {
public func write(_ string: String) {
self.write(Data(string.utf8))
}

View File

@ -3,7 +3,14 @@ add_executable(Voxelotl MACOSX_BUNDLE
Renderer.swift
FPSCalculator.swift
Application.swift
main.swift)
main.swift
shader.metal)
set_source_files_properties(
shader.metal PROPERTIES
LANGUAGE METAL
COMPILE_OPTIONS "-I${PROJECT_SOURCE_DIR}"
)
target_link_libraries(Voxelotl PRIVATE SDLSwift)
set_target_properties(Voxelotl PROPERTIES
@ -28,4 +35,4 @@ set_target_properties(Voxelotl PROPERTIES
set_source_files_properties(Assets.xcassets PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
source_group("Resources" FILES Assets.xcassets)
source_group("Source Files" REGULAR_EXPRESSION "\\.(swift)$")
source_group("Source Files" REGULAR_EXPRESSION "\\.(swift|metal)$")

View File

@ -8,7 +8,7 @@ public struct FPSCalculator {
_framesCount += 1
_accumulator += deltaTime
if (_accumulator >= 1.0) {
if _accumulator >= 1.0 {
result(_framesCount)
_framesCount = 0

View File

@ -109,12 +109,8 @@ class Renderer {
// Create shader library & grab functions
do {
//self.lib = try device.makeDefaultLibrary(bundle: Bundle.main)
let options = MTLCompileOptions()
options.fastMathEnabled = true
self.lib = try device.makeLibrary(source: Self.shaderSource, options: options)
}
catch {
self.lib = try device.makeDefaultLibrary(bundle: Bundle.main)
} catch {
throw RendererError.initFailure("Metal shader compilation failed:\n\(error.localizedDescription)")
}
let vertexProgram = lib.makeFunction(name: "vertexMain")
@ -127,8 +123,7 @@ class Renderer {
pipeDescription.colorAttachments[0].pixelFormat = layer.pixelFormat
do {
self.pso = try device.makeRenderPipelineState(descriptor: pipeDescription)
}
catch {
} catch {
throw RendererError.initFailure("Failed to create pipeline state: \(error.localizedDescription)")
}

View File

@ -6,6 +6,6 @@ let app = Application(
height: 720,
title: "Voxelotl Demo",
flags: [ .resizable, .highDPI ],
vsyncMode: .on(interval: 1)))
vsyncMode: .off))
exit(app.run())

View File

@ -0,0 +1,45 @@
#ifndef SHADERTYPES_H
#define SHADERTYPES_H
#ifdef __METAL_VERSION__
# define NS_ENUM(TYPE, NAME) enum NAME : TYPE NAME; enum NAME : TYPE
# define NSInteger metal::int32_t
#else
# import <Foundation/Foundation.h>
#endif
#include <simd/simd.h>
typedef NS_ENUM(NSInteger, ShaderInputIdx) {
ShaderInputIdxVertices = 0
};
typedef struct {
vector_float4 position;
vector_float4 color;
} ShaderVertex;
#endif//SHADERTYPES_H
#include <metal_stdlib>
using namespace metal;
struct FragmentInput {
float4 position [[position]];
float4 color;
};
vertex FragmentInput vertexMain(
uint vertexID [[vertex_id]],
device const ShaderVertex* vtx [[buffer(ShaderInputIdxVertices)]]
) {
FragmentInput out;
out.position = vtx[vertexID].position;
out.color = vtx[vertexID].color;
return out;
}
fragment float4 fragmentMain(FragmentInput in [[stage_in]]) {
return in.color;
}