From 0542bd25ec7dc049b87a90f3472fe3e946637422 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 16 Aug 2019 14:01:27 +0200 Subject: [PATCH] - convert texturefactor to uniform --- Source/Core/Rendering/RenderDevice.cs | 10 ++--- Source/Core/Rendering/Renderer2D.cs | 56 +++++++++++++-------------- Source/Core/Rendering/Renderer3D.cs | 26 ++++++------- Source/Native/RenderDevice.cpp | 5 ++- Source/Native/RenderDevice.h | 3 +- Source/Native/Shader.cpp | 3 +- Source/Native/ShaderDisplay2D.h | 7 ++++ Source/Native/ShaderThings2D.h | 6 +++ 8 files changed, 65 insertions(+), 51 deletions(-) diff --git a/Source/Core/Rendering/RenderDevice.cs b/Source/Core/Rendering/RenderDevice.cs index 52a3c498..cc5bb5c9 100755 --- a/Source/Core/Rendering/RenderDevice.cs +++ b/Source/Core/Rendering/RenderDevice.cs @@ -172,11 +172,6 @@ namespace CodeImp.DoomBuilder.Rendering RenderDevice_SetMultisampleAntialias(Handle, value); } - public void SetTextureFactor(int factor) - { - // To do: is this even applied to fragment shaders? if not, delete - if it is, convert to uniform - } - public void SetZEnable(bool value) { RenderDevice_SetZEnable(Handle, value); @@ -351,7 +346,7 @@ namespace CodeImp.DoomBuilder.Rendering SetFogEnable(false); SetMultisampleAntialias((General.Settings.AntiAliasingSamples > 0)); SetSourceBlend(Blend.SourceAlpha); - SetTextureFactor(-1); + SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); SetZEnable(false); SetZWriteEnable(false); @@ -577,7 +572,8 @@ namespace CodeImp.DoomBuilder.Rendering lightColor, ignoreNormals, spotLight, - campos + campos, + texturefactor } public enum VertexFormat : int { Flat, World } diff --git a/Source/Core/Rendering/Renderer2D.cs b/Source/Core/Rendering/Renderer2D.cs index 876d87e4..4c87f02d 100755 --- a/Source/Core/Rendering/Renderer2D.cs +++ b/Source/Core/Rendering/Renderer2D.cs @@ -202,21 +202,21 @@ namespace CodeImp.DoomBuilder.Rendering case BlendingMode.None: graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - break; + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + break; case BlendingMode.Mask: graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(true); - graphics.SetTextureFactor(-1); - break; + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + break; case BlendingMode.Alpha: graphics.SetAlphaBlendEnable(true); graphics.SetAlphaTestEnable(false); graphics.SetSourceBlend(Blend.SourceAlpha); graphics.SetDestinationBlend(Blend.InverseSourceAlpha); - graphics.SetTextureFactor((new Color4(1f, 1f, 1f, layer.alpha)).ToArgb()); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, layer.alpha)); break; case BlendingMode.Additive: @@ -224,7 +224,7 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetAlphaTestEnable(false); graphics.SetSourceBlend(Blend.SourceAlpha); graphics.SetDestinationBlend(Blend.One); - graphics.SetTextureFactor((new Color4(1f, 1f, 1f, layer.alpha)).ToArgb()); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, layer.alpha)); break; } @@ -1229,7 +1229,7 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetDestinationBlend(Blend.InverseSourceAlpha); graphics.SetAlphaTestEnable(false); graphics.SetFogEnable(false); - graphics.SetTextureFactor(alphacolor.ToArgb()); + graphics.SetUniform(UniformName.texturefactor, alphacolor); graphics.SetVertexBuffer(thingsvertices); // Set things texture @@ -1487,7 +1487,7 @@ namespace CodeImp.DoomBuilder.Rendering { // Set renderstates for rendering graphics.SetAlphaBlendEnable(false); - graphics.SetTextureFactor(-1); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); graphics.SetFillMode(FillMode.Wireframe); graphics.SetShader(ShaderName.things2d_fill); @@ -1583,8 +1583,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(true); SetDisplay2DSettings(1f, 1f, 0f, 1f, General.Settings.ClassicBilinear); @@ -1641,8 +1641,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, t); SetWorldTransformation(transformcoords); @@ -1663,8 +1663,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(true); graphics.SetUniform(UniformName.FillColor, new Color4(color)); @@ -1691,8 +1691,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(true); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, label.Texture); SetWorldTransformation(false); @@ -1722,8 +1722,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(true); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(false); graphics.SetShader(ShaderName.display2d_normal); SetDisplay2DSettings(1f, 1f, 0f, 1f, false); @@ -1789,8 +1789,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(false); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); @@ -1824,8 +1824,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(false); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); @@ -1856,8 +1856,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(false); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, texture.Texture); @@ -1955,8 +1955,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(false); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); @@ -2007,8 +2007,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZEnable(false); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); - graphics.SetFogEnable(false); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetFogEnable(false); SetWorldTransformation(false); graphics.SetShader(ShaderName.display2d_normal); graphics.SetTexture(0, General.Map.Data.WhiteTexture.Texture); diff --git a/Source/Core/Rendering/Renderer3D.cs b/Source/Core/Rendering/Renderer3D.cs index 8e5afea5..ace29f32 100755 --- a/Source/Core/Rendering/Renderer3D.cs +++ b/Source/Core/Rendering/Renderer3D.cs @@ -316,8 +316,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetFogColor(General.Colors.Background.ToInt()); graphics.SetFogStart(General.Settings.ViewDistance * FOG_RANGE); graphics.SetFogEnd(General.Settings.ViewDistance); - graphics.SetTextureFactor(-1); - graphics.SetUniform(UniformName.highlightcolor, new Color4()); //mxd + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + graphics.SetUniform(UniformName.highlightcolor, new Color4()); //mxd // Texture addressing graphics.SetSamplerState(0, TextureAddress.Wrap); @@ -384,10 +384,10 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetZWriteEnable(true); graphics.SetAlphaBlendEnable(false); graphics.SetAlphaTestEnable(false); - graphics.SetTextureFactor(-1); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); - //mxd. SKY PASS - if(skygeo.Count > 0) + //mxd. SKY PASS + if (skygeo.Count > 0) { world = Matrix.Identity; ApplyMatrices3D(); @@ -611,8 +611,8 @@ namespace CodeImp.DoomBuilder.Rendering } // Done - graphics.SetTextureFactor(-1); - } + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + } //mxd private void RenderVertices() @@ -651,8 +651,8 @@ namespace CodeImp.DoomBuilder.Rendering } // Done - graphics.SetTextureFactor(-1); - } + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + } //mxd private void RenderArrows(ICollection lines) @@ -729,8 +729,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.Draw(PrimitiveType.LineList, 0, pointscount / 2); // Done - graphics.SetTextureFactor(-1); - vb.Dispose(); + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + vb.Dispose(); } // This performs a single render pass @@ -2013,8 +2013,8 @@ namespace CodeImp.DoomBuilder.Rendering graphics.SetAlphaTestEnable(false); graphics.SetSourceBlend(Blend.SourceAlpha); graphics.SetDestinationBlend(Blend.InverseSourceAlpha); - graphics.SetTextureFactor(-1); - worldmatrix = Matrix.Identity; + graphics.SetUniform(UniformName.texturefactor, new Color4(1f, 1f, 1f, 1f)); + worldmatrix = Matrix.Identity; ApplyMatrices2D(); graphics.SetShader(ShaderName.display2d_normal); diff --git a/Source/Native/RenderDevice.cpp b/Source/Native/RenderDevice.cpp index 7db17a55..9f0d8a12 100644 --- a/Source/Native/RenderDevice.cpp +++ b/Source/Native/RenderDevice.cpp @@ -469,7 +469,8 @@ static const int uniformLocations[(int)UniformName::NumUniforms] = { 104, // lightColor 109, // ignoreNormals 110, // spotLight - 76, // campos + 76, // campos, + 112, // texturefactor }; void RenderDevice::SetUniform(UniformName name, const void* values, int count) @@ -504,6 +505,8 @@ void RenderDevice::ApplyUniforms() glUniform1fv(locations[(int)UniformName::ignoreNormals], 1, &mUniforms[109].valuef); glUniform1fv(locations[(int)UniformName::spotLight], 1, &mUniforms[110].valuef); + glUniform4fv(locations[(int)UniformName::texturefactor], 1, &mUniforms[112].valuef); + for (int i = 0; i < Shader::MaxSamplers; i++) glUniform1i(shader->SamplerLocations[i], i); } diff --git a/Source/Native/RenderDevice.h b/Source/Native/RenderDevice.h index 860d9216..220a09bc 100644 --- a/Source/Native/RenderDevice.h +++ b/Source/Native/RenderDevice.h @@ -67,6 +67,7 @@ enum class UniformName : int ignoreNormals, spotLight, campos, + texturefactor, NumUniforms }; @@ -158,7 +159,7 @@ public: int32_t valuei; }; - UniformEntry mUniforms[4 * 16 + 12 * 4]; + UniformEntry mUniforms[4 * 16 + 13 * 4]; GLuint mStreamVertexBuffer = 0; GLuint mStreamVAO = 0; diff --git a/Source/Native/Shader.cpp b/Source/Native/Shader.cpp index e591cecb..a3e792de 100644 --- a/Source/Native/Shader.cpp +++ b/Source/Native/Shader.cpp @@ -84,7 +84,8 @@ bool Shader::Compile(const std::string& vertexShader, const std::string& fragmen "lightColor", "ignoreNormals", "spotLight", - "campos" + "campos", + "texturefactor" }; for (int i = 0; i < (int)UniformName::NumUniforms; i++) diff --git a/Source/Native/ShaderDisplay2D.h b/Source/Native/ShaderDisplay2D.h index 8c1b6653..4041d851 100644 --- a/Source/Native/ShaderDisplay2D.h +++ b/Source/Native/ShaderDisplay2D.h @@ -33,6 +33,7 @@ static const char* display2D_ps_fsaa = R"( uniform float desaturation; uniform sampler2D texture1; + uniform vec4 texturefactor; // This blends the max of 2 pixels vec4 addcolor(vec4 c1, vec4 c2) @@ -71,6 +72,8 @@ static const char* display2D_ps_fsaa = R"( FragColor = vec4(desaturate(c.rgb), c.a * rendersettings.w) * Color; } + FragColor *= texturefactor; + #if defined(ALPHA_TEST) if (FragColor.a < 0.5) discard; #endif @@ -92,6 +95,7 @@ const char* display2D_ps_normal = R"( uniform float desaturation; uniform sampler2D texture1; + uniform vec4 texturefactor; vec3 desaturate(vec3 texel) { @@ -103,6 +107,7 @@ const char* display2D_ps_normal = R"( { vec4 c = texture(texture1, UV); FragColor = vec4(desaturate(c.rgb), c.a * rendersettings.w) * Color; + FragColor *= texturefactor; #if defined(ALPHA_TEST) if (FragColor.a < 0.5) discard; @@ -124,11 +129,13 @@ const char* display2D_ps_fullbright = R"( uniform vec4 rendersettings; uniform sampler2D texture1; + uniform vec4 texturefactor; void main() { vec4 c = texture(texture1, UV); FragColor = vec4(c.rgb, c.a * rendersettings.w); + FragColor *= texturefactor; #if defined(ALPHA_TEST) if (FragColor.a < 0.5) discard; diff --git a/Source/Native/ShaderThings2D.h b/Source/Native/ShaderThings2D.h index 490a73b0..8f81521b 100644 --- a/Source/Native/ShaderThings2D.h +++ b/Source/Native/ShaderThings2D.h @@ -30,6 +30,7 @@ static const char* things2D_ps_sprite = R"( uniform float desaturation; uniform sampler2D texture1; + uniform vec4 texturefactor; vec3 desaturate(vec3 texel) { @@ -54,6 +55,8 @@ static const char* things2D_ps_sprite = R"( FragColor = vec4(desaturate(c.rgb), c.a * rendersettings.w); } + FragColor *= texturefactor; + #if defined(ALPHA_TEST) if (FragColor.a < 0.5) discard; #endif @@ -72,6 +75,7 @@ static const char* things2D_ps_thing = R"( uniform float desaturation; uniform sampler2D texture1; + uniform vec4 texturefactor; vec3 desaturate(vec3 texel) { @@ -84,6 +88,8 @@ static const char* things2D_ps_thing = R"( vec4 c = texture(texture1, UV); FragColor = vec4(desaturate(c.rgb), c.a * rendersettings.w) * Color; + FragColor *= texturefactor; + #if defined(ALPHA_TEST) if (FragColor.a < 0.5) discard; #endif