mirror of
https://github.com/GayPizzaSpecifications/voxelotl-engine.git
synced 2025-08-02 21:00:57 +00:00
restore halfs in shader
This commit is contained in:
parent
4209a925c4
commit
42244456c9
@ -167,7 +167,7 @@ public class Renderer {
|
|||||||
|
|
||||||
let color = Color<Float>.white
|
let color = Color<Float>.white
|
||||||
let vertices = mesh.vertices.map {
|
let vertices = mesh.vertices.map {
|
||||||
ShaderVertex(position: $0.position, normal: $0.normal, color: color.values, 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,
|
||||||
@ -348,14 +348,14 @@ 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.values,
|
ambientColor: SIMD4(material.ambient),
|
||||||
diffuseColor: material.diffuse.values,
|
diffuseColor: SIMD4(material.diffuse),
|
||||||
specularColor: material.specular.values,
|
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.values)
|
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.values,
|
ambientColor: SIMD4(material.ambient),
|
||||||
diffuseColor: material.diffuse.values,
|
diffuseColor: SIMD4(material.diffuse),
|
||||||
specularColor: material.specular.values,
|
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.values)
|
color: SIMD4(instance.color))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
instanceBuffer.didModifyRange(0..<instancesBytes)
|
instanceBuffer.didModifyRange(0..<instancesBytes)
|
||||||
@ -468,17 +468,6 @@ fileprivate extension MTLCullMode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 == Float {
|
|
||||||
var reinterpretUShort: SIMD4<UInt32> {
|
|
||||||
.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)
|
||||||
|
@ -7,7 +7,7 @@ struct FragmentInput {
|
|||||||
float3 world;
|
float3 world;
|
||||||
float3 normal;
|
float3 normal;
|
||||||
float2 texCoord;
|
float2 texCoord;
|
||||||
float4 color;
|
half4 color;
|
||||||
};
|
};
|
||||||
|
|
||||||
vertex FragmentInput vertexMain(
|
vertex FragmentInput vertexMain(
|
||||||
@ -23,13 +23,13 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
fragment float4 fragmentMain(
|
fragment half4 fragmentMain(
|
||||||
FragmentInput in [[stage_in]],
|
FragmentInput in [[stage_in]],
|
||||||
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
|
metal::texture2d<half, metal::access::sample> texture [[texture(0)]],
|
||||||
constant FragmentShaderUniforms& u [[buffer(FragmentShaderInputIdxUniforms)]]
|
constant FragmentShaderUniforms& u [[buffer(FragmentShaderInputIdxUniforms)]]
|
||||||
@ -45,18 +45,18 @@ fragment float4 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);
|
||||||
float4 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);
|
||||||
float4 specular = u.specularColor * specularAmount;
|
half4 specular = half4(u.specularColor) * specularAmount;
|
||||||
|
|
||||||
// Sample texture & vertex color to get albedo
|
// Sample texture & vertex color to get albedo
|
||||||
float4 albedo = float4(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;
|
||||||
}
|
}
|
||||||
|
@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
#include <simd/simd.h>
|
#include <simd/simd.h>
|
||||||
|
|
||||||
typedef simd_float4 color_float4;
|
|
||||||
|
|
||||||
typedef NS_ENUM(NSInteger, VertexShaderInputIdx) {
|
typedef NS_ENUM(NSInteger, VertexShaderInputIdx) {
|
||||||
VertexShaderInputIdxVertices = 0,
|
VertexShaderInputIdxVertices = 0,
|
||||||
VertexShaderInputIdxInstance = 1,
|
VertexShaderInputIdxInstance = 1,
|
||||||
@ -21,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_float4 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_float4 color;
|
vector_float4 color;
|
||||||
} VertexShaderInstance;
|
} VertexShaderInstance;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -41,7 +39,7 @@ typedef NS_ENUM(NSInteger, FragmentShaderInputIdx) {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
vector_float3 cameraPosition, directionalLight;
|
vector_float3 cameraPosition, directionalLight;
|
||||||
color_float4 ambientColor, diffuseColor, specularColor;
|
vector_float4 ambientColor, diffuseColor, specularColor;
|
||||||
float specularIntensity;
|
float specularIntensity;
|
||||||
} FragmentShaderUniforms;
|
} FragmentShaderUniforms;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user