From eb0abe25a4e0752810eb32ae7b0c9c687e48dc79 Mon Sep 17 00:00:00 2001 From: MeltyPlayer Date: Wed, 4 Dec 2024 14:04:00 -0600 Subject: [PATCH] Switched to OpenGL ES compatible fragment shaders so we can use the Angle renderer soon (which can run at 120 FPS!) --- .../glsl/ColorShaderSourceGlslTests.cs | 61 +++++---- .../FixedFunctionShaderSourceGlslTests.cs | 84 ++++++------ .../shaders/glsl/NullShaderSourceGlslTests.cs | 74 +++++----- .../glsl/StandardShaderSourceGlslTests.cs | 127 +++++++++--------- .../glsl/TextureShaderSourceGlslTests.cs | 69 +++++----- .../Fin.Ui/src/rendering/gl/ubo/LightsUbo.cs | 4 +- .../Fin.Ui/src/rendering/gl/ubo/UboUtil.cs | 7 + .../FixedFunctionEquationsGlslPrinter.cs | 32 ++--- .../src/shaders/glsl/ColorShaderSourceGlsl.cs | 5 +- .../Fin/Fin/src/shaders/glsl/GlslConstants.cs | 5 +- .../Fin/Fin/src/shaders/glsl/GlslUtil.cs | 46 ++++--- .../shaders/glsl/HiddenShaderSourceGlsl.cs | 7 +- .../src/shaders/glsl/NullShaderSourceGlsl.cs | 3 +- .../shaders/glsl/StandardShaderSourceGlsl.cs | 3 +- .../shaders/glsl/TextureShaderSourceGlsl.cs | 5 +- 15 files changed, 287 insertions(+), 245 deletions(-) diff --git a/FinModelUtility/Fin/Fin Tests/shaders/glsl/ColorShaderSourceGlslTests.cs b/FinModelUtility/Fin/Fin Tests/shaders/glsl/ColorShaderSourceGlslTests.cs index 4248f9568..b4c754cc0 100644 --- a/FinModelUtility/Fin/Fin Tests/shaders/glsl/ColorShaderSourceGlslTests.cs +++ b/FinModelUtility/Fin/Fin Tests/shaders/glsl/ColorShaderSourceGlslTests.cs @@ -16,42 +16,44 @@ public void TestWithoutNormalsNotMasked() => AssertGlsl_( false, false, - """ - #version 430 + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + uniform vec4 diffuseColor; + + out vec4 fragColor; - uniform vec4 diffuseColor; - - out vec4 fragColor; - - in vec4 vertexColor0; + in vec4 vertexColor0; - void main() { - fragColor = diffuseColor * vertexColor0; - } - """); + void main() { + fragColor = diffuseColor * vertexColor0; + } + """); [Test] public void TestWithoutNormalsMasked() => AssertGlsl_( false, true, - """ - #version 430 + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} - uniform vec4 diffuseColor; + uniform vec4 diffuseColor; - out vec4 fragColor; + out vec4 fragColor; - in vec4 vertexColor0; + in vec4 vertexColor0; - void main() { - fragColor = diffuseColor * vertexColor0; - - if (fragColor.a < .95) { - discard; + void main() { + fragColor = diffuseColor * vertexColor0; + + if (fragColor.a < .95) { + discard; + } } - } - """); + """); [Test] public void TestWithNormals() @@ -59,23 +61,24 @@ public void TestWithNormals() true, true, $$""" - #version 430 + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} {{GlslUtil.GetLightHeader(true)}} - + uniform vec4 diffuseColor; uniform float shininess; - + out vec4 fragColor; in vec4 vertexColor0; in vec3 vertexPosition; in vec3 vertexNormal; - + {{GlslUtil.GetGetIndividualLightColorsFunction()}} - + {{GlslUtil.GetGetMergedLightColorsFunction()}} - + {{GlslUtil.GetApplyMergedLightColorsFunction(false)}} void main() { diff --git a/FinModelUtility/Fin/Fin Tests/shaders/glsl/FixedFunctionShaderSourceGlslTests.cs b/FinModelUtility/Fin/Fin Tests/shaders/glsl/FixedFunctionShaderSourceGlslTests.cs index 35dfb6443..5aeac8c63 100644 --- a/FinModelUtility/Fin/Fin Tests/shaders/glsl/FixedFunctionShaderSourceGlslTests.cs +++ b/FinModelUtility/Fin/Fin Tests/shaders/glsl/FixedFunctionShaderSourceGlslTests.cs @@ -21,19 +21,20 @@ public void TestWithNothing() false, false, (m, t) => CreateFixedFunctionMaterial_(m, t, false, false, false), - """ - #version 430 + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} - out vec4 fragColor; + out vec4 fragColor; - void main() { - vec3 colorComponent = vec3(1); - - float alphaComponent = 1; - - fragColor = vec4(colorComponent, alphaComponent); - } - """); + void main() { + vec3 colorComponent = vec3(1.0); + + float alphaComponent = 1.0; + + fragColor = vec4(colorComponent, alphaComponent); + } + """); [Test] public void TestWithVertexColorOnly() @@ -41,21 +42,22 @@ public void TestWithVertexColorOnly() false, false, (m, t) => CreateFixedFunctionMaterial_(m, t, false, false, true), - """ - #version 430 + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} - in vec4 vertexColor0; + in vec4 vertexColor0; - out vec4 fragColor; + out vec4 fragColor; - void main() { - vec3 colorComponent = vertexColor0.rgb; - - float alphaComponent = vertexColor0.a; - - fragColor = vec4(colorComponent, alphaComponent); - } - """); + void main() { + vec3 colorComponent = vertexColor0.rgb; + + float alphaComponent = vertexColor0.a; + + fragColor = vec4(colorComponent, alphaComponent); + } + """); [Test] public void TestWithTextureOnly() @@ -63,27 +65,28 @@ public void TestWithTextureOnly() false, true, (m, t) => CreateFixedFunctionMaterial_(m, t, false, true, false), - """ - #version 430 + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} - uniform sampler2D texture0; + uniform sampler2D texture0; - in vec2 uv0; + in vec2 uv0; - out vec4 fragColor; + out vec4 fragColor; - void main() { - vec3 colorComponent = texture(texture0, uv0).rgb; - - float alphaComponent = texture(texture0, uv0).a; - - fragColor = vec4(colorComponent, alphaComponent); - - if (!(alphaComponent > 0.95)) { - discard; + void main() { + vec3 colorComponent = texture(texture0, uv0).rgb; + + float alphaComponent = texture(texture0, uv0).a; + + fragColor = vec4(colorComponent, alphaComponent); + + if (!(alphaComponent > 0.95)) { + discard; + } } - } - """); + """); [Test] public void TestWithLightingAndTextureAndVertexColor() @@ -92,7 +95,8 @@ public void TestWithLightingAndTextureAndVertexColor() true, (m, t) => CreateFixedFunctionMaterial_(m, t, true, true, true), $$""" - #version 430 + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} {{GlslUtil.GetLightHeader(true)}} uniform float shininess; diff --git a/FinModelUtility/Fin/Fin Tests/shaders/glsl/NullShaderSourceGlslTests.cs b/FinModelUtility/Fin/Fin Tests/shaders/glsl/NullShaderSourceGlslTests.cs index dae967456..fa24f7ab6 100644 --- a/FinModelUtility/Fin/Fin Tests/shaders/glsl/NullShaderSourceGlslTests.cs +++ b/FinModelUtility/Fin/Fin Tests/shaders/glsl/NullShaderSourceGlslTests.cs @@ -1,7 +1,4 @@ -using System.Drawing; - -using fin.image; -using fin.model.impl; +using fin.model.impl; using fin.model.util; using fin.util.image; @@ -17,51 +14,54 @@ public void TestWithoutNormalsNotMasked() => AssertGlsl_( false, false, - """ - #version 430 - - out vec4 fragColor; - - in vec4 vertexColor0; - - void main() { - fragColor = vertexColor0; - } - """); + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + out vec4 fragColor; + + in vec4 vertexColor0; + + void main() { + fragColor = vertexColor0; + } + """); [Test] public void TestWithoutNormalsMasked() => AssertGlsl_( false, true, - """ - #version 430 - - out vec4 fragColor; - - in vec4 vertexColor0; - - void main() { - fragColor = vertexColor0; - } - """); + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + out vec4 fragColor; + + in vec4 vertexColor0; + + void main() { + fragColor = vertexColor0; + } + """); [Test] public void TestWithNormals() => AssertGlsl_( true, true, - """ - #version 430 - - out vec4 fragColor; - - in vec4 vertexColor0; - - void main() { - fragColor = vertexColor0; - } - """); + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + out vec4 fragColor; + + in vec4 vertexColor0; + + void main() { + fragColor = vertexColor0; + } + """); private static void AssertGlsl_( bool withNormals, diff --git a/FinModelUtility/Fin/Fin Tests/shaders/glsl/StandardShaderSourceGlslTests.cs b/FinModelUtility/Fin/Fin Tests/shaders/glsl/StandardShaderSourceGlslTests.cs index 35f96357d..8704a6f81 100644 --- a/FinModelUtility/Fin/Fin Tests/shaders/glsl/StandardShaderSourceGlslTests.cs +++ b/FinModelUtility/Fin/Fin Tests/shaders/glsl/StandardShaderSourceGlslTests.cs @@ -20,17 +20,18 @@ public void TestWithoutNormalsNoMask() false, false, (m, t) => { }, - """ - #version 430 - - out vec4 fragColor; + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + out vec4 fragColor; - in vec4 vertexColor0; + in vec4 vertexColor0; - void main() { - fragColor = vertexColor0; - } - """); + void main() { + fragColor = vertexColor0; + } + """); [Test] public void TestWithoutNormalsNoTextures() @@ -38,21 +39,22 @@ public void TestWithoutNormalsNoTextures() false, true, (m, t) => { }, - """ - #version 430 - - out vec4 fragColor; + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + out vec4 fragColor; - in vec4 vertexColor0; + in vec4 vertexColor0; - void main() { - fragColor = vertexColor0; - - if (fragColor.a < .95) { - discard; + void main() { + fragColor = vertexColor0; + + if (fragColor.a < .95) { + discard; + } } - } - """); + """); [Test] public void TestWithoutNormalsDiffuseOnly() @@ -60,25 +62,26 @@ public void TestWithoutNormalsDiffuseOnly() false, true, (m, t) => m.DiffuseTexture = t, - """ - #version 430 - - uniform sampler2D diffuseTexture; + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + uniform sampler2D diffuseTexture; - out vec4 fragColor; + out vec4 fragColor; - in vec4 vertexColor0; - in vec2 uv0; + in vec4 vertexColor0; + in vec2 uv0; - void main() { - vec4 diffuseColor = texture(diffuseTexture, uv0); - fragColor = diffuseColor * vertexColor0; - - if (fragColor.a < .95) { - discard; + void main() { + vec4 diffuseColor = texture(diffuseTexture, uv0); + fragColor = diffuseColor * vertexColor0; + + if (fragColor.a < .95) { + discard; + } } - } - """); + """); [Test] public void TestWithoutNormalsEmissiveOnly() @@ -86,28 +89,29 @@ public void TestWithoutNormalsEmissiveOnly() false, true, (m, t) => m.EmissiveTexture = t, - """ - #version 430 - - uniform sampler2D emissiveTexture; + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + uniform sampler2D emissiveTexture; - out vec4 fragColor; + out vec4 fragColor; - in vec4 vertexColor0; - in vec2 uv0; + in vec4 vertexColor0; + in vec2 uv0; - void main() { - fragColor = vertexColor0; - - vec4 emissiveColor = texture(emissiveTexture, uv0); - fragColor.rgb += emissiveColor.rgb; - fragColor.rgb = min(fragColor.rgb, 1); - - if (fragColor.a < .95) { - discard; + void main() { + fragColor = vertexColor0; + + vec4 emissiveColor = texture(emissiveTexture, uv0); + fragColor.rgb += emissiveColor.rgb; + fragColor.rgb = min(fragColor.rgb, 1); + + if (fragColor.a < .95) { + discard; + } } - } - """); + """); [Test] public void TestWithNormalsNoTextures() @@ -116,8 +120,9 @@ public void TestWithNormalsNoTextures() true, (m, t) => { }, $$""" - #version 430 - + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + {{GlslUtil.GetLightHeader(true)}} uniform float shininess; @@ -156,8 +161,9 @@ public void TestWithNormalsDiffuseOnly() true, (m, t) => m.DiffuseTexture = t, $$""" - #version 430 - + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + {{GlslUtil.GetLightHeader(true)}} uniform sampler2D diffuseTexture; @@ -199,8 +205,9 @@ public void TestWithNormalsEmissiveOnly() true, (m, t) => m.EmissiveTexture = t, $$""" - #version 430 - + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + {{GlslUtil.GetLightHeader(true)}} uniform sampler2D emissiveTexture; diff --git a/FinModelUtility/Fin/Fin Tests/shaders/glsl/TextureShaderSourceGlslTests.cs b/FinModelUtility/Fin/Fin Tests/shaders/glsl/TextureShaderSourceGlslTests.cs index 813e85b5a..6e1828502 100644 --- a/FinModelUtility/Fin/Fin Tests/shaders/glsl/TextureShaderSourceGlslTests.cs +++ b/FinModelUtility/Fin/Fin Tests/shaders/glsl/TextureShaderSourceGlslTests.cs @@ -17,44 +17,46 @@ public void TestWithoutNormalsNotMasked() => AssertGlsl_( false, false, - """ - #version 430 - - uniform sampler2D diffuseTexture; - - out vec4 fragColor; - - in vec4 vertexColor0; - in vec2 uv0; - - void main() { - fragColor = texture(diffuseTexture, uv0) * vertexColor0; - } - """); + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + uniform sampler2D diffuseTexture; + + out vec4 fragColor; + + in vec4 vertexColor0; + in vec2 uv0; + + void main() { + fragColor = texture(diffuseTexture, uv0) * vertexColor0; + } + """); [Test] public void TestWithoutNormalsMasked() => AssertGlsl_( false, true, - """ - #version 430 - - uniform sampler2D diffuseTexture; - - out vec4 fragColor; - - in vec4 vertexColor0; - in vec2 uv0; - - void main() { - fragColor = texture(diffuseTexture, uv0) * vertexColor0; - - if (fragColor.a < .95) { - discard; + $$""" + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + + uniform sampler2D diffuseTexture; + + out vec4 fragColor; + + in vec4 vertexColor0; + in vec2 uv0; + + void main() { + fragColor = texture(diffuseTexture, uv0) * vertexColor0; + + if (fragColor.a < .95) { + discard; + } } - } - """); + """); [Test] public void TestWithNormals() @@ -62,8 +64,9 @@ public void TestWithNormals() true, true, $$""" - #version 430 - + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + {{GlslUtil.GetLightHeader(true)}} uniform sampler2D diffuseTexture; diff --git a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/LightsUbo.cs b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/LightsUbo.cs index 00aa6c0b9..4ab99a824 100644 --- a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/LightsUbo.cs +++ b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/LightsUbo.cs @@ -38,7 +38,7 @@ public void UpdateData(bool useLighting, IReadOnlyLighting? lighting) { if (!useLighting || lighting == null) { offset += SIZE_OF_LIGHT * MaterialConstants.MAX_LIGHTS + UboUtil.SIZE_OF_VECTOR4; - UboUtil.AppendBool(buffer, ref offset, false); + UboUtil.AppendFloat(buffer, ref offset, 0); } else { var lights = lighting.Lights; for (var i = 0; i < MaterialConstants.MAX_LIGHTS; ++i) { @@ -53,7 +53,7 @@ public void UpdateData(bool useLighting, IReadOnlyLighting? lighting) { lighting.AmbientLightColor.Af) * lighting.AmbientLightStrength; UboUtil.AppendVector4(buffer, ref offset, ambientLightColor); - UboUtil.AppendBool(buffer, ref offset, true); + UboUtil.AppendFloat(buffer, ref offset, 1); } this.impl_.UpdateDataIfChanged(buffer); diff --git a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/UboUtil.cs b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/UboUtil.cs index 6c8cd6a05..611414ec3 100644 --- a/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/UboUtil.cs +++ b/FinModelUtility/Fin/Fin.Ui/src/rendering/gl/ubo/UboUtil.cs @@ -23,6 +23,13 @@ public static void AppendInt(Span buffer, offset += 4; } + public static void AppendFloat(Span buffer, + ref int offset, + float value) { + buffer.Slice(offset, 4).Cast()[0] = value; + offset += 4; + } + public static void AppendVector3(Span buffer, ref int offset, Vector3? vector3) { diff --git a/FinModelUtility/Fin/Fin/src/language/equations/fixedFunction/FixedFunctionEquationsGlslPrinter.cs b/FinModelUtility/Fin/Fin/src/language/equations/fixedFunction/FixedFunctionEquationsGlslPrinter.cs index c079e46fb..8b999c34a 100644 --- a/FinModelUtility/Fin/Fin/src/language/equations/fixedFunction/FixedFunctionEquationsGlslPrinter.cs +++ b/FinModelUtility/Fin/Fin/src/language/equations/fixedFunction/FixedFunctionEquationsGlslPrinter.cs @@ -29,7 +29,8 @@ public void Print( var registers = material.Registers; var textures = material.TextureSources; - sb.AppendLine($"#version {GlslConstants.SHADER_VERSION}"); + sb.AppendLine($"#version {GlslConstants.FRAGMENT_SHADER_VERSION}"); + sb.AppendLine(GlslConstants.FLOAT_PRECISION); sb.AppendLine(); var hasIndividualLights = @@ -322,12 +323,12 @@ private string GetAlphaCompareText_( float reference) => alphaCompareType switch { AlphaCompareType.Never => "false", - AlphaCompareType.Less => $"{alphaAccessorText} < {reference}", - AlphaCompareType.Equal => $"{alphaAccessorText} == {reference}", - AlphaCompareType.LEqual => $"{alphaAccessorText} <= {reference}", - AlphaCompareType.Greater => $"{alphaAccessorText} > {reference}", - AlphaCompareType.NEqual => $"{alphaAccessorText} != {reference}", - AlphaCompareType.GEqual => $"{alphaAccessorText} >= {reference}", + AlphaCompareType.Less => $"{alphaAccessorText} < {reference:0.0###########}", + AlphaCompareType.Equal => $"{alphaAccessorText} == {reference:0.0###########}", + AlphaCompareType.LEqual => $"{alphaAccessorText} <= {reference:0.0###########}", + AlphaCompareType.Greater => $"{alphaAccessorText} > {reference:0.0###########}", + AlphaCompareType.NEqual => $"{alphaAccessorText} != {reference:0.0###########}", + AlphaCompareType.GEqual => $"{alphaAccessorText} >= {reference:0.0###########}", AlphaCompareType.Always => "true", _ => throw new ArgumentOutOfRangeException( nameof(alphaCompareType), @@ -480,7 +481,7 @@ private void PrintScalarTerm_( this.PrintScalarValue_(sb, numerator, textures, true); } } else { - sb.Append(1); + PrintDouble_(sb, 1); } if (denominators != null) { @@ -576,10 +577,11 @@ private string GetScalarIdentifiedValue_( }; } - private void PrintScalarConstant_( - StringBuilder sb, - IScalarConstant constant) - => sb.Append(constant.Value); + private void PrintScalarConstant_(StringBuilder sb, IScalarConstant constant) + => PrintDouble_(sb, constant.Value); + + private static void PrintDouble_(StringBuilder sb, double value) + => sb.Append($"{value:0.0###########}"); private enum WrapType { NEVER, @@ -636,7 +638,7 @@ private void PrintColorValue_( } if (clamp) { - sb.Append(", 0, 1)"); + sb.Append(", 0.0, 1.0)"); } } @@ -675,7 +677,7 @@ private void PrintColorTerm_( this.PrintColorValue_(sb, numerator, textures, WrapType.EXPRESSIONS); } } else { - sb.Append(1); + PrintDouble_(sb, 1); } if (denominators != null) { @@ -860,7 +862,7 @@ private void PrintColorTernaryOperator_( this.PrintScalarValue_(sb, ternaryOperator.Rhs, textures); sb.Append(")"); sb.Append(" < "); - sb.Append("(1.0 / 255)"); + sb.Append("(1.0 / 255.0)"); break; } case BoolComparisonType.GREATER_THAN: { diff --git a/FinModelUtility/Fin/Fin/src/shaders/glsl/ColorShaderSourceGlsl.cs b/FinModelUtility/Fin/Fin/src/shaders/glsl/ColorShaderSourceGlsl.cs index 9c278bd87..822e9c390 100644 --- a/FinModelUtility/Fin/Fin/src/shaders/glsl/ColorShaderSourceGlsl.cs +++ b/FinModelUtility/Fin/Fin/src/shaders/glsl/ColorShaderSourceGlsl.cs @@ -17,7 +17,8 @@ public ColorShaderSourceGlsl(IReadOnlyModel model, var hasNormals = model.Skin.HasNormalsForMaterial(material); var fragmentSrc = new StringBuilder(); - fragmentSrc.AppendLine($"#version {GlslConstants.SHADER_VERSION}"); + fragmentSrc.AppendLine($"#version {GlslConstants.FRAGMENT_SHADER_VERSION}"); + fragmentSrc.AppendLine(GlslConstants.FLOAT_PRECISION); fragmentSrc.AppendLine(); if (hasNormals) { @@ -82,7 +83,7 @@ void main() { fragmentSrc.AppendLine( $$""" - if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT}}) { + if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT:0.0###########}}) { discard; } """); diff --git a/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslConstants.cs b/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslConstants.cs index 02c2ca700..796900a5e 100644 --- a/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslConstants.cs +++ b/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslConstants.cs @@ -1,7 +1,10 @@ namespace fin.shaders.glsl; public static class GlslConstants { - public const string SHADER_VERSION = "430"; + public const string VERTEX_SHADER_VERSION = "430"; + public const string FRAGMENT_SHADER_VERSION = "310 es"; + + public const string FLOAT_PRECISION = "precision mediump float;"; public const int UBO_MATRICES_BINDING_INDEX = 1; public const string UBO_MATRICES_NAME = "Matrices"; diff --git a/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslUtil.cs b/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslUtil.cs index e3bc98a90..bd2e70d9e 100644 --- a/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslUtil.cs +++ b/FinModelUtility/Fin/Fin/src/shaders/glsl/GlslUtil.cs @@ -79,7 +79,7 @@ public static string GetVertexSrc(IReadOnlyModel model, var vertexSrc = new StringBuilder(); vertexSrc.Append($$""" - #version {{GlslConstants.SHADER_VERSION}} + #version {{GlslConstants.VERTEX_SHADER_VERSION}} layout (std140, binding = {{GlslConstants.UBO_MATRICES_BINDING_INDEX}}) uniform {{GlslConstants.UBO_MATRICES_NAME}} { mat4 {{GlslConstants.UNIFORM_MODEL_MATRIX_NAME}}; @@ -109,9 +109,17 @@ public static string GetVertexSrc(IReadOnlyModel model, """); } - vertexSrc.AppendLine(@$" -layout(location = {UseThenAdd(ref location, MaterialConstants.MAX_UVS)}) in vec2 in_Uvs[{MaterialConstants.MAX_UVS}]; -layout(location = {UseThenAdd(ref location, MaterialConstants.MAX_COLORS)}) in vec4 in_Colors[{MaterialConstants.MAX_COLORS}]; + for (var i = 0; i < MaterialConstants.MAX_UVS; ++i) { + vertexSrc.AppendLine( + $"layout(location = {location++}) in vec2 in_Uv{i};"); + } + + for (var i = 0; i < MaterialConstants.MAX_COLORS; ++i) { + vertexSrc.AppendLine( + $"layout(location = {location++}) in vec4 in_Color{i};"); + } + + vertexSrc.AppendLine(@" out vec3 vertexPosition; out vec3 vertexNormal; @@ -146,7 +154,7 @@ public static string GetVertexSrc(IReadOnlyModel model, void main() { mat4 mvMatrix = {{GlslConstants.UNIFORM_VIEW_MATRIX_NAME}} * {{GlslConstants.UNIFORM_MODEL_MATRIX_NAME}}; mat4 mvpMatrix = {{GlslConstants.UNIFORM_PROJECTION_MATRIX_NAME}} * mvMatrix; - + """); if (useBoneMatrices) { @@ -225,14 +233,14 @@ void main() { for (var i = 0; i < usedUvs.Length; ++i) { if (usedUvs[i]) { - vertexSrc.AppendLine($" {GlslConstants.IN_UV_NAME}{i} = in_Uvs[{i}];"); + vertexSrc.AppendLine($" {GlslConstants.IN_UV_NAME}{i} = in_Uv{i};"); } } for (var i = 0; i < usedColors.Length; ++i) { if (usedColors[i]) { vertexSrc.AppendLine( - $" {GlslConstants.IN_VERTEX_COLOR_NAME}{i} = in_Colors[{i}];"); + $" {GlslConstants.IN_VERTEX_COLOR_NAME}{i} = in_Color{i};"); } } @@ -268,7 +276,7 @@ struct Light { layout (std140, binding = {{GlslConstants.UBO_LIGHTS_BINDING_INDEX}}) uniform {{GlslConstants.UBO_LIGHTS_NAME}} { Light lights[{{MaterialConstants.MAX_LIGHTS}}]; vec4 ambientLightColor; - int {{GlslConstants.UNIFORM_USE_LIGHTING_NAME}}; + float {{GlslConstants.UNIFORM_USE_LIGHTING_NAME}}; }; uniform vec3 {{GlslConstants.UNIFORM_CAMERA_POSITION_NAME}}; @@ -287,7 +295,7 @@ void getSurfaceToLightNormalAndAttenuation(Light light, vec3 position, vec3 norm ? -light.normal : normalize(surfaceToLight); if (light.attenuationFunction == {{(int) AttenuationFunction.NONE}}) { - attenuation = 1; + attenuation = 1.0; return; } @@ -301,15 +309,15 @@ void getSurfaceToLightNormalAndAttenuation(Light light, vec3 position, vec3 norm float attn = dot(attnDotLhs, light.normal); vec3 attnPowers = vec3(1, attn, attn*attn); - float attenuationNumerator = max(0, dot(cosAttn, attnPowers)); + float attenuationNumerator = max(0.0, dot(cosAttn, attnPowers)); // Denominator (Distance attenuation) - float attenuationDenominator = 1; + float attenuationDenominator = 1.0; if (light.sourceType != {{(int) LightSourceType.LINE}}) { vec3 distAttn = light.distanceAttenuation; if (light.attenuationFunction == {{(int) AttenuationFunction.SPECULAR}}) { - float attn = max(0, -dot(normal, light.normal)); + float attn = max(0.0, -dot(normal, light.normal)); if (light.diffuseFunction != {{(int) DiffuseFunction.NONE}}) { distAttn = normalize(distAttn); } @@ -332,18 +340,18 @@ void getIndividualLightColors(Light light, vec3 position, vec3 normal, float shi } vec3 surfaceToLightNormal = vec3(0); - float attenuation = 0; + float attenuation = 0.0; getSurfaceToLightNormalAndAttenuation(light, position, normal, surfaceToLightNormal, attenuation); - float diffuseLightAmount = 1; + float diffuseLightAmount = 1.0; if (light.diffuseFunction == {{(int) DiffuseFunction.SIGNED}} || light.diffuseFunction == {{(int) DiffuseFunction.CLAMP}}) { - diffuseLightAmount = max(0, dot(normal, surfaceToLightNormal)); + diffuseLightAmount = max(0.0, dot(normal, surfaceToLightNormal)); } diffuseColor = light.color * diffuseLightAmount * attenuation; - if (dot(normal, surfaceToLightNormal) >= 0) { + if (dot(normal, surfaceToLightNormal) >= 0.0) { vec3 surfaceToCameraNormal = normalize(cameraPosition - position); - float specularLightAmount = pow(max(0, dot(reflect(-surfaceToLightNormal, normal), surfaceToCameraNormal)), {{GlslConstants.UNIFORM_SHININESS_NAME}}); + float specularLightAmount = pow(max(0.0, dot(reflect(-surfaceToLightNormal, normal), surfaceToCameraNormal)), {{GlslConstants.UNIFORM_SHININESS_NAME}}); specularColor = light.color * specularLightAmount * attenuation; } } @@ -377,10 +385,10 @@ vec4 applyMergedLightingColors(vec3 position, vec3 normal, float shininess, vec4 getMergedLightColors(position, normal, shininess, mergedDiffuseLightColor, mergedSpecularLightColor); // We double it because all the other kids do. (Other fixed-function games.) - vec4 diffuseComponent = 2 * diffuseSurfaceColor * ({{(withAmbientOcclusion ? "ambientOcclusionAmount * " : "")}}ambientLightColor + mergedDiffuseLightColor); + vec4 diffuseComponent = 2.0 * diffuseSurfaceColor * ({{(withAmbientOcclusion ? "ambientOcclusionAmount * " : "")}}ambientLightColor + mergedDiffuseLightColor); vec4 specularComponent = specularSurfaceColor * mergedSpecularLightColor; - return clamp(diffuseComponent + specularComponent, 0, 1); + return clamp(diffuseComponent + specularComponent, 0.0, 1.0); } """; } diff --git a/FinModelUtility/Fin/Fin/src/shaders/glsl/HiddenShaderSourceGlsl.cs b/FinModelUtility/Fin/Fin/src/shaders/glsl/HiddenShaderSourceGlsl.cs index bb9ef1883..c7b8c6007 100644 --- a/FinModelUtility/Fin/Fin/src/shaders/glsl/HiddenShaderSourceGlsl.cs +++ b/FinModelUtility/Fin/Fin/src/shaders/glsl/HiddenShaderSourceGlsl.cs @@ -4,15 +4,16 @@ public class HiddenShaderSourceGlsl : IShaderSourceGlsl { public string VertexShaderSource => $$""" - #version {{GlslConstants.SHADER_VERSION}} + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} void main() {} """; public string FragmentShaderSource => $$""" - #version {{GlslConstants.SHADER_VERSION}} - + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} + void main() { discard; } diff --git a/FinModelUtility/Fin/Fin/src/shaders/glsl/NullShaderSourceGlsl.cs b/FinModelUtility/Fin/Fin/src/shaders/glsl/NullShaderSourceGlsl.cs index 0861b5c0d..113693245 100644 --- a/FinModelUtility/Fin/Fin/src/shaders/glsl/NullShaderSourceGlsl.cs +++ b/FinModelUtility/Fin/Fin/src/shaders/glsl/NullShaderSourceGlsl.cs @@ -12,7 +12,8 @@ public class NullShaderSourceGlsl( public string FragmentShaderSource => $$""" - #version {{GlslConstants.SHADER_VERSION}} + #version {{GlslConstants.FRAGMENT_SHADER_VERSION}} + {{GlslConstants.FLOAT_PRECISION}} out vec4 fragColor; diff --git a/FinModelUtility/Fin/Fin/src/shaders/glsl/StandardShaderSourceGlsl.cs b/FinModelUtility/Fin/Fin/src/shaders/glsl/StandardShaderSourceGlsl.cs index 099b82054..05a90cb69 100644 --- a/FinModelUtility/Fin/Fin/src/shaders/glsl/StandardShaderSourceGlsl.cs +++ b/FinModelUtility/Fin/Fin/src/shaders/glsl/StandardShaderSourceGlsl.cs @@ -18,7 +18,8 @@ public StandardShaderSourceGlsl( var animations = model.AnimationManager.Animations; var fragmentShaderSrc = new StringBuilder(); - fragmentShaderSrc.AppendLine($"#version {GlslConstants.SHADER_VERSION}"); + fragmentShaderSrc.AppendLine($"#version {GlslConstants.FRAGMENT_SHADER_VERSION}"); + fragmentShaderSrc.AppendLine(GlslConstants.FLOAT_PRECISION); fragmentShaderSrc.AppendLine(); var diffuseTexture = material.DiffuseTexture; diff --git a/FinModelUtility/Fin/Fin/src/shaders/glsl/TextureShaderSourceGlsl.cs b/FinModelUtility/Fin/Fin/src/shaders/glsl/TextureShaderSourceGlsl.cs index caf33b06c..d3ead1dfc 100644 --- a/FinModelUtility/Fin/Fin/src/shaders/glsl/TextureShaderSourceGlsl.cs +++ b/FinModelUtility/Fin/Fin/src/shaders/glsl/TextureShaderSourceGlsl.cs @@ -22,7 +22,8 @@ public TextureShaderSourceGlsl(IReadOnlyModel model, var hasNormals = model.Skin.HasNormalsForMaterial(material); var fragmentSrc = new StringBuilder(); - fragmentSrc.AppendLine($"#version {GlslConstants.SHADER_VERSION}"); + fragmentSrc.AppendLine($"#version {GlslConstants.FRAGMENT_SHADER_VERSION}"); + fragmentSrc.AppendLine(GlslConstants.FLOAT_PRECISION); if (hasNormals) { fragmentSrc.AppendLine( @@ -98,7 +99,7 @@ void main() { fragmentSrc.AppendLine( $$""" - if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT}}) { + if (fragColor.a < {{GlslConstants.MIN_ALPHA_BEFORE_DISCARD_TEXT:0.0###########}}) { discard; } """);