RIP to Float16, it's sad that Intel doesn't support you.

This commit is contained in:
Alex Zenla
2024-09-01 19:27:39 -04:00
committed by a dinosaur
parent b1f2f645f6
commit 4209a925c4
8 changed files with 35 additions and 40 deletions

View File

@ -141,7 +141,7 @@ public class Renderer {
if mesh.vertices.isEmpty || mesh.indices.isEmpty { return nil }
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(
bytes: vertices,
@ -165,9 +165,9 @@ public class Renderer {
func createMesh(_ mesh: Mesh<VertexPositionNormalTexcoord, UInt16>) -> RendererMesh? {
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 {
ShaderVertex(position: $0.position, normal: $0.normal, color: color, texCoord: $0.texCoord)
ShaderVertex(position: $0.position, normal: $0.normal, color: color.values, texCoord: $0.texCoord)
}
guard let vtxBuffer = self.device.makeBuffer(
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")
var vertUniforms = VertexShaderUniforms(projView: camera.viewProjection)
var fragUniforms = FragmentShaderUniforms(
cameraPosition: camera.position,
directionalLight: normalize(environment.lightDirection),
ambientColor: material.ambient.reinterpretUShort,
diffuseColor: material.diffuse.reinterpretUShort,
specularColor: material.specular.reinterpretUShort,
ambientColor: material.ambient.values,
diffuseColor: material.diffuse.values,
specularColor: material.specular.values,
specularIntensity: material.gloss)
var instance = VertexShaderInstance(
model: model,
normalModel: model.inverse.transpose,
color: color.reinterpretUShort)
color: color.values)
self._encoder.setCullMode(.init(environment.cullFace))
@ -386,9 +386,9 @@ public class Renderer {
var fragUniforms = FragmentShaderUniforms(
cameraPosition: camera.position,
directionalLight: normalize(environment.lightDirection),
ambientColor: material.ambient.reinterpretUShort,
diffuseColor: material.diffuse.reinterpretUShort,
specularColor: material.specular.reinterpretUShort,
ambientColor: material.ambient.values,
diffuseColor: material.diffuse.values,
specularColor: material.specular.values,
specularIntensity: material.gloss)
let numInstances = instances.count
@ -417,7 +417,7 @@ public class Renderer {
.scale(instance.scale)
data[i] = VertexShaderInstance(
model: model, normalModel: model.inverse.transpose,
color: instance.color.reinterpretUShort)
color: instance.color.values)
}
}
instanceBuffer.didModifyRange(0..<instancesBytes)
@ -468,13 +468,13 @@ fileprivate extension MTLCullMode {
}
}
fileprivate extension Color where T == Float16 {
var reinterpretUShort: SIMD4<UInt16> {
fileprivate extension Color where T == Float {
var reinterpretUInt: SIMD4<UInt32> {
.init(self.r.bitPattern, self.g.bitPattern, self.b.bitPattern, self.a.bitPattern)
}
}
fileprivate extension SIMD4 where Scalar == Float16 {
var reinterpretUShort: SIMD4<UInt16> {
fileprivate extension SIMD4 where Scalar == Float {
var reinterpretUShort: SIMD4<UInt32> {
.init(self.x.bitPattern, self.y.bitPattern, self.z.bitPattern, self.w.bitPattern)
}
}