avoid unnecessary conversion of colours between half4 to float4 and back again

This commit is contained in:
2024-08-25 15:14:00 +10:00
parent 428b142bf2
commit 8de398ce13
3 changed files with 22 additions and 9 deletions

View File

@ -353,9 +353,9 @@ public class Renderer {
var fragUniforms = FragmentShaderUniforms(
cameraPosition: camera.position,
directionalLight: normalize(environment.lightDirection),
ambientColor: SIMD4(Color<Float>(material.ambient)),
diffuseColor: SIMD4(Color<Float>(material.diffuse)),
specularColor: SIMD4(Color<Float>(material.specular)),
ambientColor: material.ambient.reinterpretUShort,
diffuseColor: material.diffuse.reinterpretUShort,
specularColor: material.specular.reinterpretUShort,
specularIntensity: material.gloss)
let numInstances = instances.count
@ -384,7 +384,7 @@ public class Renderer {
.scale(instance.scale)
data[i] = VertexShaderInstance(
model: model, normalModel: model.inverse.transpose,
color: SIMD4(Color<Float>(instance.color)))
color: instance.color.reinterpretUShort)
}
}
instanceBuffer.didModifyRange(0..<instancesBytes)
@ -428,6 +428,12 @@ 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)
}
}
enum RendererError: Error {
case initFailure(_ message: String)
case loadFailure(_ message: String)