mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-03 05:10:57 +00:00
extract shader types to module
This commit is contained in:
parent
bc9289474b
commit
67e6dd0507
@ -4,7 +4,9 @@ add_executable(Voxelotl MACOSX_BUNDLE
|
|||||||
FPSCalculator.swift
|
FPSCalculator.swift
|
||||||
Application.swift
|
Application.swift
|
||||||
main.swift
|
main.swift
|
||||||
shader.metal)
|
shader.metal
|
||||||
|
shadertypes.h
|
||||||
|
module.modulemap)
|
||||||
|
|
||||||
set_source_files_properties(
|
set_source_files_properties(
|
||||||
shader.metal PROPERTIES
|
shader.metal PROPERTIES
|
||||||
@ -12,6 +14,7 @@ set_source_files_properties(
|
|||||||
COMPILE_OPTIONS "-I${PROJECT_SOURCE_DIR}"
|
COMPILE_OPTIONS "-I${PROJECT_SOURCE_DIR}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
target_include_directories(Voxelotl PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
|
||||||
target_link_libraries(Voxelotl PRIVATE SDLSwift)
|
target_link_libraries(Voxelotl PRIVATE SDLSwift)
|
||||||
set_target_properties(Voxelotl PROPERTIES
|
set_target_properties(Voxelotl PROPERTIES
|
||||||
XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME YES
|
XCODE_ATTRIBUTE_ENABLE_HARDENED_RUNTIME YES
|
||||||
@ -33,6 +36,7 @@ set_target_properties(Voxelotl PROPERTIES
|
|||||||
XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS "${CMAKE_CURRENT_SOURCE_DIR}/Voxelotl.entitlements"
|
XCODE_ATTRIBUTE_CODE_SIGN_ENTITLEMENTS "${CMAKE_CURRENT_SOURCE_DIR}/Voxelotl.entitlements"
|
||||||
MACOSX_BUNDLE_COPYRIGHT "© 2024 Gay Pizza Specifications")
|
MACOSX_BUNDLE_COPYRIGHT "© 2024 Gay Pizza Specifications")
|
||||||
set_source_files_properties(Assets.xcassets PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
|
set_source_files_properties(Assets.xcassets PROPERTIES MACOSX_PACKAGE_LOCATION "Resources")
|
||||||
|
set_source_files_properties(module.modulemap PROPERTIES MACOSX_PACKAGE_LOCATION Modules)
|
||||||
source_group("Resources" FILES Assets.xcassets)
|
source_group("Resources" FILES Assets.xcassets)
|
||||||
|
|
||||||
source_group("Source Files" REGULAR_EXPRESSION "\\.(swift|metal)$")
|
source_group("Source Files" REGULAR_EXPRESSION "\\.(swift|metal)$")
|
||||||
|
@ -2,65 +2,18 @@ import Foundation
|
|||||||
import Metal
|
import Metal
|
||||||
import QuartzCore.CAMetalLayer
|
import QuartzCore.CAMetalLayer
|
||||||
import simd
|
import simd
|
||||||
|
import ShaderTypes
|
||||||
|
|
||||||
// Temp:
|
//// Temp:
|
||||||
@objc fileprivate enum ShaderInputIdx: NSInteger {
|
//@objc fileprivate enum ShaderInputIdx: NSInteger {
|
||||||
case ShaderInputIdxVertices = 0
|
// case ShaderInputIdxVertices = 0
|
||||||
}
|
//}
|
||||||
fileprivate struct ShaderVertex {
|
//fileprivate struct ShaderVertex {
|
||||||
let position: SIMD4<Float>
|
// let position: SIMD4<Float>
|
||||||
let color: SIMD4<Float>
|
// let color: SIMD4<Float>
|
||||||
}
|
//}
|
||||||
|
|
||||||
class Renderer {
|
class Renderer {
|
||||||
fileprivate static let shaderSource = """
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
|
|
||||||
fileprivate static let vertices = [
|
fileprivate static let vertices = [
|
||||||
ShaderVertex(position: SIMD4<Float>(-0.5, -0.5, 0.0, 1.0), color: SIMD4<Float>(1.0, 0.0, 0.0, 1.0)),
|
ShaderVertex(position: SIMD4<Float>(-0.5, -0.5, 0.0, 1.0), color: SIMD4<Float>(1.0, 0.0, 0.0, 1.0)),
|
||||||
ShaderVertex(position: SIMD4<Float>( 0.0, 0.5, 0.0, 1.0), color: SIMD4<Float>(0.0, 1.0, 0.0, 1.0)),
|
ShaderVertex(position: SIMD4<Float>( 0.0, 0.5, 0.0, 1.0), color: SIMD4<Float>(0.0, 1.0, 0.0, 1.0)),
|
||||||
@ -170,7 +123,7 @@ class Renderer {
|
|||||||
encoder.setCullMode(MTLCullMode.none)
|
encoder.setCullMode(MTLCullMode.none)
|
||||||
encoder.setRenderPipelineState(pso)
|
encoder.setRenderPipelineState(pso)
|
||||||
|
|
||||||
encoder.setVertexBuffer(vtxBuffer, offset: 0, index: ShaderInputIdx.ShaderInputIdxVertices.rawValue)
|
encoder.setVertexBuffer(vtxBuffer, offset: 0, index: ShaderInputIdx.vertices.rawValue)
|
||||||
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)
|
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3)
|
||||||
|
|
||||||
encoder.endEncoding()
|
encoder.endEncoding()
|
||||||
|
3
Sources/Voxelotl/module.modulemap
Normal file
3
Sources/Voxelotl/module.modulemap
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module ShaderTypes {
|
||||||
|
header "shadertypes.h"
|
||||||
|
}
|
@ -1,25 +1,4 @@
|
|||||||
#ifndef SHADERTYPES_H
|
#include "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>
|
#include <metal_stdlib>
|
||||||
|
|
||||||
|
22
Sources/Voxelotl/shadertypes.h
Normal file
22
Sources/Voxelotl/shadertypes.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#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
|
Loading…
Reference in New Issue
Block a user