diff --git a/OpenRA.Game/Graphics/RgbaSpriteRenderer.cs b/OpenRA.Game/Graphics/RgbaSpriteRenderer.cs new file mode 100644 index 0000000000..1dc418b88c --- /dev/null +++ b/OpenRA.Game/Graphics/RgbaSpriteRenderer.cs @@ -0,0 +1,52 @@ +#region Copyright & License Information +/* + * Copyright 2007-2018 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; + +namespace OpenRA.Graphics +{ + public class RgbaSpriteRenderer + { + readonly SpriteRenderer parent; + + public RgbaSpriteRenderer(SpriteRenderer parent) + { + this.parent = parent; + } + + public void DrawSprite(Sprite s, float3 location, float3 size) + { + if (s.Channel != TextureChannel.RGBA) + throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite."); + + parent.DrawSprite(s, location, 0, size); + } + + public void DrawSprite(Sprite s, float3 location) + { + if (s.Channel != TextureChannel.RGBA) + throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite."); + + parent.DrawSprite(s, location, 0, s.Size); + } + + public void DrawSprite(Sprite s, float3 a, float3 b, float3 c, float3 d) + { + if (s.Channel != TextureChannel.RGBA) + throw new InvalidOperationException("DrawRGBASprite requires a RGBA sprite."); + + parent.DrawSprite(s, a, b, c, d); + } + } +} diff --git a/OpenRA.Game/Graphics/SpriteRenderer.cs b/OpenRA.Game/Graphics/SpriteRenderer.cs index 07e1c74654..f434c73ad5 100644 --- a/OpenRA.Game/Graphics/SpriteRenderer.cs +++ b/OpenRA.Game/Graphics/SpriteRenderer.cs @@ -59,6 +59,13 @@ namespace OpenRA.Graphics currentSheet = s.Sheet; } + internal void DrawSprite(Sprite s, float3 location, float paletteTextureIndex, float3 size) + { + SetRenderStateForSprite(s); + Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, paletteTextureIndex, nv, size); + nv += 6; + } + public void DrawSprite(Sprite s, float3 location, PaletteReference pal) { DrawSprite(s, location, pal.TextureIndex, s.Size); @@ -69,24 +76,6 @@ namespace OpenRA.Graphics DrawSprite(s, location, pal.TextureIndex, size); } - void DrawSprite(Sprite s, float3 location, float paletteTextureIndex, float3 size) - { - SetRenderStateForSprite(s); - Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, paletteTextureIndex, nv, size); - nv += 6; - } - - // For RGBASpriteRenderer, which doesn't use palettes - public void DrawSprite(Sprite s, float3 location) - { - DrawSprite(s, location, 0, s.Size); - } - - public void DrawSprite(Sprite s, float3 location, float3 size) - { - DrawSprite(s, location, 0, size); - } - public void DrawSprite(Sprite s, float3 a, float3 b, float3 c, float3 d) { SetRenderStateForSprite(s); @@ -94,13 +83,6 @@ namespace OpenRA.Graphics nv += 6; } - public void DrawSprite(Sprite s, Vertex[] sourceVertices, int offset) - { - SetRenderStateForSprite(s); - Array.Copy(sourceVertices, offset, vertices, nv, 6); - nv += 6; - } - public void DrawVertexBuffer(IVertexBuffer buffer, int start, int length, PrimitiveType type, Sheet sheet, BlendMode blendMode) { shader.SetTexture("DiffuseTexture", sheet.GetTexture()); diff --git a/OpenRA.Game/Graphics/Util.cs b/OpenRA.Game/Graphics/Util.cs index 95c3b6242f..366fc35a27 100644 --- a/OpenRA.Game/Graphics/Util.cs +++ b/OpenRA.Game/Graphics/Util.cs @@ -36,7 +36,7 @@ namespace OpenRA.Graphics float sb = 0; // See shp.vert for documentation on the channel attribute format - var attribC = ((byte)r.Channel) << 1 | 0x01; + var attribC = r.Channel == TextureChannel.RGBA ? 0x02 : ((byte)r.Channel) << 1 | 0x01; var ss = r as SpriteWithSecondaryData; if (ss != null) diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index 4698c8589d..7f2cff361d 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -169,7 +169,6 @@ namespace OpenRA.Graphics { lastDepthPreviewEnabled = debugVis.Value.DepthBuffer; Game.Renderer.WorldSpriteRenderer.SetDepthPreviewEnabled(lastDepthPreviewEnabled); - Game.Renderer.WorldRgbaSpriteRenderer.SetDepthPreviewEnabled(lastDepthPreviewEnabled); } RefreshPalette(); diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index 516da4b424..490b2803ed 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -260,6 +260,7 @@ + diff --git a/OpenRA.Game/Renderer.cs b/OpenRA.Game/Renderer.cs index 2d737157fb..7d91afde6e 100644 --- a/OpenRA.Game/Renderer.cs +++ b/OpenRA.Game/Renderer.cs @@ -21,12 +21,12 @@ namespace OpenRA public sealed class Renderer : IDisposable { public SpriteRenderer WorldSpriteRenderer { get; private set; } - public SpriteRenderer WorldRgbaSpriteRenderer { get; private set; } + public RgbaSpriteRenderer WorldRgbaSpriteRenderer { get; private set; } public RgbaColorRenderer WorldRgbaColorRenderer { get; private set; } public ModelRenderer WorldModelRenderer { get; private set; } public RgbaColorRenderer RgbaColorRenderer { get; private set; } - public SpriteRenderer RgbaSpriteRenderer { get; private set; } public SpriteRenderer SpriteRenderer { get; private set; } + public RgbaSpriteRenderer RgbaSpriteRenderer { get; private set; } public IReadOnlyDictionary Fonts; internal IGraphicsDevice Device { get; private set; } @@ -57,11 +57,11 @@ namespace OpenRA SheetSize = graphicSettings.SheetSize; WorldSpriteRenderer = new SpriteRenderer(this, Device.CreateShader("shp")); - WorldRgbaSpriteRenderer = new SpriteRenderer(this, Device.CreateShader("rgba")); + WorldRgbaSpriteRenderer = new RgbaSpriteRenderer(WorldSpriteRenderer); WorldRgbaColorRenderer = new RgbaColorRenderer(WorldSpriteRenderer); WorldModelRenderer = new ModelRenderer(this, Device.CreateShader("model")); SpriteRenderer = new SpriteRenderer(this, Device.CreateShader("shp")); - RgbaSpriteRenderer = new SpriteRenderer(this, Device.CreateShader("rgba")); + RgbaSpriteRenderer = new RgbaSpriteRenderer(SpriteRenderer); RgbaColorRenderer = new RgbaColorRenderer(SpriteRenderer); tempBuffer = Device.CreateVertexBuffer(TempBufferSize); @@ -124,7 +124,6 @@ namespace OpenRA if (resolutionChanged) { lastResolution = Resolution; - RgbaSpriteRenderer.SetViewportParams(lastResolution, 0f, 0f, 1f, int2.Zero); SpriteRenderer.SetViewportParams(lastResolution, 0f, 0f, 1f, int2.Zero); } @@ -133,7 +132,6 @@ namespace OpenRA { lastScroll = scroll; lastZoom = zoom; - WorldRgbaSpriteRenderer.SetViewportParams(lastResolution, depthScale, depthOffset, zoom, scroll); WorldSpriteRenderer.SetViewportParams(lastResolution, depthScale, depthOffset, zoom, scroll); WorldModelRenderer.SetViewportParams(lastResolution, zoom, scroll); } @@ -147,10 +145,8 @@ namespace OpenRA Flush(); currentPaletteTexture = palette.Texture; - RgbaSpriteRenderer.SetPalette(currentPaletteTexture); SpriteRenderer.SetPalette(currentPaletteTexture); WorldSpriteRenderer.SetPalette(currentPaletteTexture); - WorldRgbaSpriteRenderer.SetPalette(currentPaletteTexture); WorldModelRenderer.SetPalette(currentPaletteTexture); } diff --git a/glsl/rgba.frag b/glsl/rgba.frag deleted file mode 100644 index cbe92009f4..0000000000 --- a/glsl/rgba.frag +++ /dev/null @@ -1,43 +0,0 @@ -uniform sampler2D DiffuseTexture; -uniform bool EnableDepthPreview; - -varying vec4 vTexCoord; - -float jet_r(float x) -{ - return x < 0.7 ? 4.0 * x - 1.5 : -4.0 * x + 4.5; -} - -float jet_g(float x) -{ - return x < 0.5 ? 4.0 * x - 0.5 : -4.0 * x + 3.5; -} - -float jet_b(float x) -{ - return x < 0.3 ? 4.0 * x + 0.5 : -4.0 * x + 2.5; -} - -void main() -{ - vec4 c = texture2D(DiffuseTexture, vTexCoord.st); - // Discard any transparent fragments (both color and depth) - if (c.a == 0.0) - discard; - - float depth = gl_FragCoord.z; - - // Convert to window coords - gl_FragDepth = 0.5 * depth + 0.5; - - if (EnableDepthPreview) - { - float x = 1.0 - gl_FragDepth; - float r = clamp(jet_r(x), 0.0, 1.0); - float g = clamp(jet_g(x), 0.0, 1.0); - float b = clamp(jet_b(x), 0.0, 1.0); - gl_FragColor = vec4(r, g, b, 1.0); - } - else - gl_FragColor = c; -} \ No newline at end of file diff --git a/glsl/rgba.vert b/glsl/rgba.vert deleted file mode 100644 index 7b7e5b7958..0000000000 --- a/glsl/rgba.vert +++ /dev/null @@ -1,12 +0,0 @@ -uniform vec3 Scroll; -uniform vec3 r1, r2; - -attribute vec4 aVertexPosition; -attribute vec4 aVertexTexCoord; -varying vec4 vTexCoord; - -void main() -{ - gl_Position = vec4((aVertexPosition.xyz - Scroll.xyz) * r1 + r2, 1); - vTexCoord = aVertexTexCoord; -} diff --git a/glsl/shp.frag b/glsl/shp.frag index c40e312057..49c8b8823a 100644 --- a/glsl/shp.frag +++ b/glsl/shp.frag @@ -7,7 +7,10 @@ varying vec4 vTexCoord; varying vec2 vTexMetadata; varying vec4 vChannelMask; varying vec4 vDepthMask; + varying vec4 vColorFraction; +varying vec4 vRGBAFraction; +varying vec4 vPalettedFraction; float jet_r(float x) { @@ -28,7 +31,7 @@ void main() { vec4 x = texture2D(DiffuseTexture, vTexCoord.st); vec2 p = vec2(dot(x, vChannelMask), vTexMetadata.s); - vec4 c = (vec4(1, 1, 1, 1) - vColorFraction) * texture2D(Palette, p) + vColorFraction * vTexCoord; + vec4 c = vPalettedFraction * texture2D(Palette, p) + vRGBAFraction * x + vColorFraction * vTexCoord; // Discard any transparent fragments (both color and depth) if (c.a == 0.0) diff --git a/glsl/shp.vert b/glsl/shp.vert index 6186401131..4dd0bae885 100644 --- a/glsl/shp.vert +++ b/glsl/shp.vert @@ -4,11 +4,15 @@ uniform vec3 r1, r2; attribute vec4 aVertexPosition; attribute vec4 aVertexTexCoord; attribute vec2 aVertexTexMetadata; + varying vec4 vTexCoord; varying vec2 vTexMetadata; varying vec4 vChannelMask; varying vec4 vDepthMask; + varying vec4 vColorFraction; +varying vec4 vRGBAFraction; +varying vec4 vPalettedFraction; vec2 UnpackChannelAttributes(float x) { @@ -19,6 +23,7 @@ vec2 UnpackChannelAttributes(float x) // 001, 011, 101, 111: Sample depth sprite from channel R,G,B,A // Bits 0-2 define the behaviour of the primary texture channel: // 000: Channel is not used (aVertexTexCoord instead defines a color value) + // 010: Sample RGBA sprite from all four channels // 001, 011, 101, 111: Sample paletted sprite from channel R,G,B,A float secondaryChannel = 0.0; @@ -42,6 +47,8 @@ vec4 SelectChannelMask(float x) return vec4(0,0,1,0); if (x >= 3.0) return vec4(0,1,0,0); + if (x >= 2.0) + return vec4(1,1,1,1); if (x >= 1.0) return vec4(1,0,0,0); @@ -52,8 +59,24 @@ vec4 SelectColorFraction(float x) { if (x > 0.0) return vec4(0, 0, 0, 0); - else + + return vec4(1, 1, 1, 1); +} + +vec4 SelectRGBAFraction(float x) +{ + if (x == 2.0) return vec4(1, 1, 1, 1); + + return vec4(0, 0, 0, 0); +} + +vec4 SelectPalettedFraction(float x) +{ + if (x == 0.0 || x == 2.0) + return vec4(0, 0, 0, 0); + + return vec4(1, 1, 1, 1); } void main() @@ -65,5 +88,7 @@ void main() vec2 attrib = UnpackChannelAttributes(aVertexTexMetadata.t); vChannelMask = SelectChannelMask(attrib.s); vColorFraction = SelectColorFraction(attrib.s); + vRGBAFraction = SelectRGBAFraction(attrib.s); + vPalettedFraction = SelectPalettedFraction(attrib.s); vDepthMask = SelectChannelMask(attrib.t); }