Merge pull request #2 from GayPizzaSpecifications/bye-bye-float16

RIP to Float16, it's sad that Intel doesn't support you.
This commit is contained in:
a dinosaur 2024-09-02 19:07:11 +10:00 committed by GitHub
commit fc50f0b350
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 30 additions and 48 deletions

View File

@ -123,7 +123,7 @@ public struct Chunk: Hashable {
public enum BlockType: Hashable { public enum BlockType: Hashable {
case air case air
case solid(_ color: Color<Float16>) case solid(_ color: Color<Float>)
} }
public struct Block: Hashable { public struct Block: Hashable {

View File

@ -4,13 +4,13 @@ struct Instance {
let position: SIMD3<Float> let position: SIMD3<Float>
let scale: SIMD3<Float> let scale: SIMD3<Float>
let rotation: simd_quatf let rotation: simd_quatf
let color: Color<Float16> let color: Color<Float>
init( init(
position: SIMD3<Float> = .zero, position: SIMD3<Float> = .zero,
scale: SIMD3<Float> = .one, scale: SIMD3<Float> = .one,
rotation: simd_quatf = .identity, rotation: simd_quatf = .identity,
color: Color<Float16> = .white color: Color<Float> = .white
) { ) {
self.position = position self.position = position
self.scale = scale self.scale = scale

View File

@ -1,7 +1,7 @@
public struct Material { public struct Material {
public var ambient: Color<Float16> public var ambient: Color<Float>
public var diffuse: Color<Float16> public var diffuse: Color<Float>
public var specular: Color<Float16> public var specular: Color<Float>
public var gloss: Float public var gloss: Float
} }

View File

@ -18,6 +18,6 @@ public struct VertexPositionNormalTexcoord: Vertex {
public struct VertexPositionNormalColorTexcoord: Vertex { public struct VertexPositionNormalColorTexcoord: Vertex {
var position: SIMD3<Float> var position: SIMD3<Float>
var normal: SIMD3<Float> var normal: SIMD3<Float>
var color: SIMD4<Float16> var color: SIMD4<Float>
var texCoord: SIMD2<Float> var texCoord: SIMD2<Float>
} }

View File

@ -141,7 +141,7 @@ public class Renderer {
if mesh.vertices.isEmpty || mesh.indices.isEmpty { return nil } if mesh.vertices.isEmpty || mesh.indices.isEmpty { return nil }
let vertices = mesh.vertices.map { let vertices = mesh.vertices.map {
ShaderVertex(position: $0.position, normal: $0.normal, color: $0.color.reinterpretUShort, texCoord: $0.texCoord) ShaderVertex(position: $0.position, normal: $0.normal, color: $0.color, texCoord: $0.texCoord)
} }
guard let vtxBuffer = self.device.makeBuffer( guard let vtxBuffer = self.device.makeBuffer(
bytes: vertices, bytes: vertices,
@ -165,9 +165,9 @@ public class Renderer {
func createMesh(_ mesh: Mesh<VertexPositionNormalTexcoord, UInt16>) -> RendererMesh? { func createMesh(_ mesh: Mesh<VertexPositionNormalTexcoord, UInt16>) -> RendererMesh? {
if mesh.vertices.isEmpty || mesh.indices.isEmpty { return nil } if mesh.vertices.isEmpty || mesh.indices.isEmpty { return nil }
let color = Color<Float16>.white.reinterpretUShort let color = Color<Float>.white
let vertices = mesh.vertices.map { let vertices = mesh.vertices.map {
ShaderVertex(position: $0.position, normal: $0.normal, color: color, texCoord: $0.texCoord) ShaderVertex(position: $0.position, normal: $0.normal, color: SIMD4(color), texCoord: $0.texCoord)
} }
guard let vtxBuffer = self.device.makeBuffer( guard let vtxBuffer = self.device.makeBuffer(
bytes: vertices, bytes: vertices,
@ -341,21 +341,21 @@ public class Renderer {
} }
} }
func draw(model: matrix_float4x4, color: Color<Float16>, mesh: RendererMesh, material: Material, environment: Environment, camera: Camera) { func draw(model: matrix_float4x4, color: Color<Float>, mesh: RendererMesh, material: Material, environment: Environment, camera: Camera) {
assert(self._encoder != nil, "draw can't be called outside of a frame being rendered") assert(self._encoder != nil, "draw can't be called outside of a frame being rendered")
var vertUniforms = VertexShaderUniforms(projView: camera.viewProjection) var vertUniforms = VertexShaderUniforms(projView: camera.viewProjection)
var fragUniforms = FragmentShaderUniforms( var fragUniforms = FragmentShaderUniforms(
cameraPosition: camera.position, cameraPosition: camera.position,
directionalLight: normalize(environment.lightDirection), directionalLight: normalize(environment.lightDirection),
ambientColor: material.ambient.reinterpretUShort, ambientColor: SIMD4(material.ambient),
diffuseColor: material.diffuse.reinterpretUShort, diffuseColor: SIMD4(material.diffuse),
specularColor: material.specular.reinterpretUShort, specularColor: SIMD4(material.specular),
specularIntensity: material.gloss) specularIntensity: material.gloss)
var instance = VertexShaderInstance( var instance = VertexShaderInstance(
model: model, model: model,
normalModel: model.inverse.transpose, normalModel: model.inverse.transpose,
color: color.reinterpretUShort) color: SIMD4(color))
self._encoder.setCullMode(.init(environment.cullFace)) self._encoder.setCullMode(.init(environment.cullFace))
@ -386,9 +386,9 @@ public class Renderer {
var fragUniforms = FragmentShaderUniforms( var fragUniforms = FragmentShaderUniforms(
cameraPosition: camera.position, cameraPosition: camera.position,
directionalLight: normalize(environment.lightDirection), directionalLight: normalize(environment.lightDirection),
ambientColor: material.ambient.reinterpretUShort, ambientColor: SIMD4(material.ambient),
diffuseColor: material.diffuse.reinterpretUShort, diffuseColor: SIMD4(material.diffuse),
specularColor: material.specular.reinterpretUShort, specularColor: SIMD4(material.specular),
specularIntensity: material.gloss) specularIntensity: material.gloss)
let numInstances = instances.count let numInstances = instances.count
@ -417,7 +417,7 @@ public class Renderer {
.scale(instance.scale) .scale(instance.scale)
data[i] = VertexShaderInstance( data[i] = VertexShaderInstance(
model: model, normalModel: model.inverse.transpose, model: model, normalModel: model.inverse.transpose,
color: instance.color.reinterpretUShort) color: SIMD4(instance.color))
} }
} }
instanceBuffer.didModifyRange(0..<instancesBytes) instanceBuffer.didModifyRange(0..<instancesBytes)
@ -468,17 +468,6 @@ fileprivate extension MTLCullMode {
} }
} }
fileprivate extension Color where T == Float16 {
var reinterpretUShort: SIMD4<UInt16> {
.init(self.r.bitPattern, self.g.bitPattern, self.b.bitPattern, self.a.bitPattern)
}
}
fileprivate extension SIMD4 where Scalar == Float16 {
var reinterpretUShort: SIMD4<UInt16> {
.init(self.x.bitPattern, self.y.bitPattern, self.z.bitPattern, self.w.bitPattern)
}
}
enum RendererError: Error { enum RendererError: Error {
case initFailure(_ message: String) case initFailure(_ message: String)
case loadFailure(_ message: String) case loadFailure(_ message: String)

View File

@ -26,9 +26,9 @@ struct WorldGenerator {
+ self.noise.get(fpos * 0.30) * 0.23 + self.noise.get(fpos * 0.30) * 0.23
return if value < threshold { return if value < threshold {
.solid(.init( .solid(.init(
hue: Float16(180 + self.noise2.get(fpos * 0.05) * 180), hue: Float(180 + self.noise2.get(fpos * 0.05) * 180),
saturation: Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 4)) * 0.5), saturation: Float(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 4)) * 0.5),
value: Float16(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 9)) * 0.5).lerp(0.5, 1)).linear) value: Float(0.5 + self.noise2.get(SIMD4(fpos * 0.05, 9)) * 0.5).lerp(0.5, 1)).linear)
} else { } else {
.air .air
} }

View File

@ -23,7 +23,7 @@ vertex FragmentInput vertexMain(
FragmentInput out; FragmentInput out;
out.position = u.projView * world; out.position = u.projView * world;
out.world = world.xyz; out.world = world.xyz;
out.color = vtx[vertexID].color * i[instanceID].color; out.color = half4(vtx[vertexID].color) * half4(i[instanceID].color);
out.normal = (i[instanceID].normalModel * float4(vtx[vertexID].normal, 0)).xyz; out.normal = (i[instanceID].normalModel * float4(vtx[vertexID].normal, 0)).xyz;
out.texCoord = vtx[vertexID].texCoord; out.texCoord = vtx[vertexID].texCoord;
return out; return out;
@ -45,18 +45,18 @@ fragment half4 fragmentMain(
// Compute diffuse component // Compute diffuse component
float lambert = metal::dot(normal, lightVec); float lambert = metal::dot(normal, lightVec);
float diffuseAmount = metal::max(0.0, lambert); float diffuseAmount = metal::max(0.0, lambert);
half4 diffuse = u.diffuseColor * diffuseAmount; half4 diffuse = half4(u.diffuseColor) * diffuseAmount;
// Compute specular component (blinn-phong) // Compute specular component (blinn-phong)
float specularAngle = metal::max(0.0, metal::dot(halfDir, normal)); float specularAngle = metal::max(0.0, metal::dot(halfDir, normal));
float specularTerm = metal::pow(specularAngle, u.specularIntensity); float specularTerm = metal::pow(specularAngle, u.specularIntensity);
// smoothstep hack to ensure highlight tapers gracefully at grazing angles // smoothstep hack to ensure highlight tapers gracefully at grazing angles
float specularAmount = specularTerm * metal::smoothstep(0, 2, lambert * u.specularIntensity); float specularAmount = specularTerm * metal::smoothstep(0, 2, lambert * u.specularIntensity);
half4 specular = u.specularColor * specularAmount; half4 specular = half4(u.specularColor) * specularAmount;
// Sample texture & vertex color to get albedo // Sample texture & vertex color to get albedo
half4 albedo = texture.sample(sampler, in.texCoord); half4 albedo = texture.sample(sampler, in.texCoord);
albedo *= in.color; albedo *= half4(in.color);
return albedo * (u.ambientColor + diffuse) + specular; return albedo * (half4(u.ambientColor) + diffuse) + specular;
} }

View File

@ -10,13 +10,6 @@
#include <simd/simd.h> #include <simd/simd.h>
// HACK: allow passing SIMD4<Float16> to shader while `simd_half4` is beta
#ifdef __METAL_VERSION__
typedef half4 color_half4;
#else
typedef simd_ushort4 color_half4;
#endif
typedef NS_ENUM(NSInteger, VertexShaderInputIdx) { typedef NS_ENUM(NSInteger, VertexShaderInputIdx) {
VertexShaderInputIdxVertices = 0, VertexShaderInputIdxVertices = 0,
VertexShaderInputIdxInstance = 1, VertexShaderInputIdxInstance = 1,
@ -26,14 +19,14 @@ typedef NS_ENUM(NSInteger, VertexShaderInputIdx) {
typedef struct { typedef struct {
vector_float3 position; vector_float3 position;
vector_float3 normal; vector_float3 normal;
color_half4 color; vector_float4 color;
vector_float2 texCoord; vector_float2 texCoord;
} ShaderVertex; } ShaderVertex;
typedef struct { typedef struct {
matrix_float4x4 model; matrix_float4x4 model;
matrix_float4x4 normalModel; matrix_float4x4 normalModel;
color_half4 color; vector_float4 color;
} VertexShaderInstance; } VertexShaderInstance;
typedef struct { typedef struct {
@ -46,7 +39,7 @@ typedef NS_ENUM(NSInteger, FragmentShaderInputIdx) {
typedef struct { typedef struct {
vector_float3 cameraPosition, directionalLight; vector_float3 cameraPosition, directionalLight;
color_half4 ambientColor, diffuseColor, specularColor; vector_float4 ambientColor, diffuseColor, specularColor;
float specularIntensity; float specularIntensity;
} FragmentShaderUniforms; } FragmentShaderUniforms;