mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 10:59:39 +00:00 
			
		
		
		
	avoid unnecessary conversion of colours between half4 to float4 and back again
This commit is contained in:
		@ -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)
 | 
			
		||||
 | 
			
		||||
@ -45,18 +45,18 @@ fragment half4 fragmentMain(
 | 
			
		||||
  // Compute diffuse component
 | 
			
		||||
  float lambert = metal::dot(normal, lightVec);
 | 
			
		||||
  float diffuseAmount = metal::max(0.0, lambert);
 | 
			
		||||
  half4 diffuse = half4(u.diffuseColor) * diffuseAmount;
 | 
			
		||||
  half4 diffuse = u.diffuseColor * diffuseAmount;
 | 
			
		||||
 | 
			
		||||
  // Compute specular component (blinn-phong)
 | 
			
		||||
  float specularAngle = metal::max(0.0, metal::dot(halfDir, normal));
 | 
			
		||||
  float specularTerm = metal::pow(specularAngle, u.specularIntensity);
 | 
			
		||||
  // smoothstep hack to ensure highlight tapers gracefully at grazing angles
 | 
			
		||||
  float specularAmount = specularTerm * metal::smoothstep(0, 2, lambert * u.specularIntensity);
 | 
			
		||||
  half4 specular = half4(u.specularColor) * specularAmount;
 | 
			
		||||
  half4 specular = u.specularColor * specularAmount;
 | 
			
		||||
 | 
			
		||||
  // Sample texture & vertex color to get albedo
 | 
			
		||||
  half4 albedo = texture.sample(sampler, in.texCoord);
 | 
			
		||||
  albedo *= in.color;
 | 
			
		||||
 | 
			
		||||
  return albedo * (half4(u.ambientColor) + diffuse) + specular;
 | 
			
		||||
  return albedo * (u.ambientColor + diffuse) + specular;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -10,6 +10,13 @@
 | 
			
		||||
 | 
			
		||||
#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) {
 | 
			
		||||
  VertexShaderInputIdxVertices = 0,
 | 
			
		||||
  VertexShaderInputIdxInstance = 1,
 | 
			
		||||
@ -25,7 +32,7 @@ typedef struct {
 | 
			
		||||
typedef struct {
 | 
			
		||||
  matrix_float4x4 model;
 | 
			
		||||
  matrix_float4x4 normalModel;
 | 
			
		||||
  vector_float4 color;
 | 
			
		||||
  color_half4 color;
 | 
			
		||||
} VertexShaderInstance;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
@ -38,7 +45,7 @@ typedef NS_ENUM(NSInteger, FragmentShaderInputIdx) {
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
  vector_float3 cameraPosition, directionalLight;
 | 
			
		||||
  vector_float4 ambientColor, diffuseColor, specularColor;
 | 
			
		||||
  color_half4 ambientColor, diffuseColor, specularColor;
 | 
			
		||||
  float specularIntensity;
 | 
			
		||||
} FragmentShaderUniforms;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user