mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 10:59:39 +00:00 
			
		
		
		
	compile metal in cmake
This commit is contained in:
		@ -25,10 +25,10 @@ public class Application {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Create SDL window
 | 
					    // Create SDL window
 | 
				
			||||||
    var windowFlags = SDL_WindowFlags(0)
 | 
					    var windowFlags = SDL_WindowFlags(0)
 | 
				
			||||||
    if (cfg.flags.contains(.resizable)) {
 | 
					    if cfg.flags.contains(.resizable) {
 | 
				
			||||||
      windowFlags |= SDL_WindowFlags(SDL_WINDOW_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)
 | 
					      windowFlags |= SDL_WindowFlags(SDL_WINDOW_HIGH_PIXEL_DENSITY)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    window = SDL_CreateWindow(cfg.title, cfg.width, cfg.height, windowFlags)
 | 
					    window = SDL_CreateWindow(cfg.title, cfg.width, cfg.height, windowFlags)
 | 
				
			||||||
@ -174,7 +174,7 @@ fileprivate enum ApplicationExecutionState {
 | 
				
			|||||||
  case running
 | 
					  case running
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extension FileHandle: TextOutputStream {
 | 
					extension FileHandle: @retroactive TextOutputStream {
 | 
				
			||||||
  public func write(_ string: String) {
 | 
					  public func write(_ string: String) {
 | 
				
			||||||
    self.write(Data(string.utf8))
 | 
					    self.write(Data(string.utf8))
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,14 @@ add_executable(Voxelotl MACOSX_BUNDLE
 | 
				
			|||||||
  Renderer.swift
 | 
					  Renderer.swift
 | 
				
			||||||
  FPSCalculator.swift
 | 
					  FPSCalculator.swift
 | 
				
			||||||
  Application.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)
 | 
					target_link_libraries(Voxelotl PRIVATE SDLSwift)
 | 
				
			||||||
set_target_properties(Voxelotl PROPERTIES
 | 
					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")
 | 
					set_source_files_properties(Assets.xcassets PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
 | 
				
			||||||
source_group("Resources" FILES Assets.xcassets)
 | 
					source_group("Resources" FILES Assets.xcassets)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
source_group("Source Files" REGULAR_EXPRESSION "\\.(swift)$")
 | 
					source_group("Source Files" REGULAR_EXPRESSION "\\.(swift|metal)$")
 | 
				
			||||||
 | 
				
			|||||||
@ -8,7 +8,7 @@ public struct FPSCalculator {
 | 
				
			|||||||
    _framesCount += 1
 | 
					    _framesCount += 1
 | 
				
			||||||
    _accumulator += deltaTime
 | 
					    _accumulator += deltaTime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (_accumulator >= 1.0) {
 | 
					    if _accumulator >= 1.0 {
 | 
				
			||||||
      result(_framesCount)
 | 
					      result(_framesCount)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      _framesCount = 0
 | 
					      _framesCount = 0
 | 
				
			||||||
 | 
				
			|||||||
@ -109,12 +109,8 @@ class Renderer {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    // Create shader library & grab functions
 | 
					    // Create shader library & grab functions
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
      //self.lib = try device.makeDefaultLibrary(bundle: Bundle.main)
 | 
					      self.lib = try device.makeDefaultLibrary(bundle: Bundle.main)
 | 
				
			||||||
      let options = MTLCompileOptions()
 | 
					    } catch {
 | 
				
			||||||
      options.fastMathEnabled = true
 | 
					 | 
				
			||||||
      self.lib = try device.makeLibrary(source: Self.shaderSource, options: options)
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    catch {
 | 
					 | 
				
			||||||
      throw RendererError.initFailure("Metal shader compilation failed:\n\(error.localizedDescription)")
 | 
					      throw RendererError.initFailure("Metal shader compilation failed:\n\(error.localizedDescription)")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    let vertexProgram   = lib.makeFunction(name: "vertexMain")
 | 
					    let vertexProgram   = lib.makeFunction(name: "vertexMain")
 | 
				
			||||||
@ -127,8 +123,7 @@ class Renderer {
 | 
				
			|||||||
    pipeDescription.colorAttachments[0].pixelFormat = layer.pixelFormat
 | 
					    pipeDescription.colorAttachments[0].pixelFormat = layer.pixelFormat
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
      self.pso = try device.makeRenderPipelineState(descriptor: pipeDescription)
 | 
					      self.pso = try device.makeRenderPipelineState(descriptor: pipeDescription)
 | 
				
			||||||
    }
 | 
					    } catch {
 | 
				
			||||||
    catch {
 | 
					 | 
				
			||||||
      throw RendererError.initFailure("Failed to create pipeline state: \(error.localizedDescription)")
 | 
					      throw RendererError.initFailure("Failed to create pipeline state: \(error.localizedDescription)")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -6,6 +6,6 @@ let app = Application(
 | 
				
			|||||||
  height: 720,
 | 
					  height: 720,
 | 
				
			||||||
  title: "Voxelotl Demo",
 | 
					  title: "Voxelotl Demo",
 | 
				
			||||||
  flags: [ .resizable, .highDPI ],
 | 
					  flags: [ .resizable, .highDPI ],
 | 
				
			||||||
  vsyncMode: .on(interval: 1)))
 | 
					  vsyncMode: .off))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
exit(app.run())
 | 
					exit(app.run())
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										45
									
								
								Sources/Voxelotl/shader.metal
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								Sources/Voxelotl/shader.metal
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user