Improve batching by binding up to 8 simultaneous textures.
This commit is contained in:
@@ -20,9 +20,11 @@ namespace OpenRA.Graphics
|
||||
readonly IShader shader;
|
||||
|
||||
readonly Vertex[] vertices;
|
||||
Sheet currentSheet;
|
||||
readonly Sheet[] sheets = new Sheet[8];
|
||||
|
||||
BlendMode currentBlend = BlendMode.Alpha;
|
||||
int nv = 0;
|
||||
int ns = 0;
|
||||
|
||||
public SpriteRenderer(Renderer renderer, IShader shader)
|
||||
{
|
||||
@@ -35,8 +37,11 @@ namespace OpenRA.Graphics
|
||||
{
|
||||
if (nv > 0)
|
||||
{
|
||||
if (currentSheet != null)
|
||||
shader.SetTexture("DiffuseTexture", currentSheet.GetTexture());
|
||||
for (var i = 0; i < ns; i++)
|
||||
{
|
||||
shader.SetTexture("Texture{0}".F(i), sheets[i].GetTexture());
|
||||
sheets[i] = null;
|
||||
}
|
||||
|
||||
renderer.Device.SetBlendMode(currentBlend);
|
||||
shader.PrepareRender();
|
||||
@@ -44,25 +49,45 @@ namespace OpenRA.Graphics
|
||||
renderer.Device.SetBlendMode(BlendMode.None);
|
||||
|
||||
nv = 0;
|
||||
currentSheet = null;
|
||||
ns = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void SetRenderStateForSprite(Sprite s)
|
||||
int2 SetRenderStateForSprite(Sprite s)
|
||||
{
|
||||
renderer.CurrentBatchRenderer = this;
|
||||
|
||||
if (s.BlendMode != currentBlend || s.Sheet != currentSheet || nv + 6 > renderer.TempBufferSize)
|
||||
if (s.BlendMode != currentBlend || nv + 6 > renderer.TempBufferSize)
|
||||
Flush();
|
||||
|
||||
currentBlend = s.BlendMode;
|
||||
currentSheet = s.Sheet;
|
||||
|
||||
var sheet = s.Sheet;
|
||||
var sheetIndex = 0;
|
||||
for (; sheetIndex < ns; sheetIndex++)
|
||||
if (sheets[sheetIndex] == sheet)
|
||||
break;
|
||||
|
||||
if (sheetIndex == ns)
|
||||
{
|
||||
if (sheetIndex == sheets.Length)
|
||||
{
|
||||
Flush();
|
||||
sheetIndex = 0;
|
||||
}
|
||||
|
||||
sheets[sheetIndex] = sheet;
|
||||
ns += 1;
|
||||
}
|
||||
|
||||
// TODO: Add support for secondary channels on different sheets
|
||||
return new int2(sheetIndex, sheetIndex);
|
||||
}
|
||||
|
||||
internal void DrawSprite(Sprite s, float3 location, float paletteTextureIndex, float3 size)
|
||||
{
|
||||
SetRenderStateForSprite(s);
|
||||
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, paletteTextureIndex, nv, size);
|
||||
var samplers = SetRenderStateForSprite(s);
|
||||
Util.FastCreateQuad(vertices, location + s.FractionalOffset * size, s, samplers, paletteTextureIndex, nv, size);
|
||||
nv += 6;
|
||||
}
|
||||
|
||||
@@ -78,14 +103,14 @@ namespace OpenRA.Graphics
|
||||
|
||||
public void DrawSprite(Sprite s, float3 a, float3 b, float3 c, float3 d)
|
||||
{
|
||||
SetRenderStateForSprite(s);
|
||||
Util.FastCreateQuad(vertices, a, b, c, d, s, 0, nv);
|
||||
var samplers = SetRenderStateForSprite(s);
|
||||
Util.FastCreateQuad(vertices, a, b, c, d, s, samplers, 0, nv);
|
||||
nv += 6;
|
||||
}
|
||||
|
||||
public void DrawVertexBuffer(IVertexBuffer<Vertex> buffer, int start, int length, PrimitiveType type, Sheet sheet, BlendMode blendMode)
|
||||
{
|
||||
shader.SetTexture("DiffuseTexture", sheet.GetTexture());
|
||||
shader.SetTexture("Texture0", sheet.GetTexture());
|
||||
renderer.Device.SetBlendMode(blendMode);
|
||||
shader.PrepareRender();
|
||||
renderer.DrawBatch(buffer, start, length, type);
|
||||
|
||||
@@ -92,7 +92,7 @@ namespace OpenRA.Graphics
|
||||
return;
|
||||
|
||||
var offset = rowStride * uv.V + 6 * uv.U;
|
||||
Util.FastCreateQuad(vertices, pos, sprite, palette.TextureIndex, offset, sprite.Size);
|
||||
Util.FastCreateQuad(vertices, pos, sprite, int2.Zero, palette.TextureIndex, offset, sprite.Size);
|
||||
|
||||
dirtyRows.Add(uv.V);
|
||||
}
|
||||
|
||||
@@ -20,15 +20,15 @@ namespace OpenRA.Graphics
|
||||
// yes, our channel order is nuts.
|
||||
static readonly int[] ChannelMasks = { 2, 1, 0, 3 };
|
||||
|
||||
public static void FastCreateQuad(Vertex[] vertices, float3 o, Sprite r, float paletteTextureIndex, int nv, float3 size)
|
||||
public static void FastCreateQuad(Vertex[] vertices, float3 o, Sprite r, int2 samplers, float paletteTextureIndex, int nv, float3 size)
|
||||
{
|
||||
var b = new float3(o.X + size.X, o.Y, o.Z);
|
||||
var c = new float3(o.X + size.X, o.Y + size.Y, o.Z + size.Z);
|
||||
var d = new float3(o.X, o.Y + size.Y, o.Z + size.Z);
|
||||
FastCreateQuad(vertices, o, b, c, d, r, paletteTextureIndex, nv);
|
||||
FastCreateQuad(vertices, o, b, c, d, r, samplers, paletteTextureIndex, nv);
|
||||
}
|
||||
|
||||
public static void FastCreateQuad(Vertex[] vertices, float3 a, float3 b, float3 c, float3 d, Sprite r, float paletteTextureIndex, int nv)
|
||||
public static void FastCreateQuad(Vertex[] vertices, float3 a, float3 b, float3 c, float3 d, Sprite r, int2 samplers, float paletteTextureIndex, int nv)
|
||||
{
|
||||
float sl = 0;
|
||||
float st = 0;
|
||||
@@ -37,7 +37,7 @@ namespace OpenRA.Graphics
|
||||
|
||||
// See shp.vert for documentation on the channel attribute format
|
||||
var attribC = r.Channel == TextureChannel.RGBA ? 0x02 : ((byte)r.Channel) << 1 | 0x01;
|
||||
|
||||
attribC |= samplers.X << 6;
|
||||
var ss = r as SpriteWithSecondaryData;
|
||||
if (ss != null)
|
||||
{
|
||||
@@ -47,6 +47,7 @@ namespace OpenRA.Graphics
|
||||
sb = ss.SecondaryBottom;
|
||||
|
||||
attribC |= ((byte)ss.SecondaryChannel) << 4 | 0x08;
|
||||
attribC |= samplers.Y << 9;
|
||||
}
|
||||
|
||||
var fAttribC = (float)attribC;
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
uniform sampler2D DiffuseTexture, Palette;
|
||||
uniform sampler2D Texture0;
|
||||
uniform sampler2D Texture1;
|
||||
uniform sampler2D Texture2;
|
||||
uniform sampler2D Texture3;
|
||||
uniform sampler2D Texture4;
|
||||
uniform sampler2D Texture5;
|
||||
uniform sampler2D Texture6;
|
||||
uniform sampler2D Texture7;
|
||||
uniform sampler2D Palette;
|
||||
|
||||
uniform bool EnableDepthPreview;
|
||||
uniform float DepthTextureScale;
|
||||
@@ -7,6 +15,7 @@ varying vec4 vTexCoord;
|
||||
varying vec2 vTexMetadata;
|
||||
varying vec4 vChannelMask;
|
||||
varying vec4 vDepthMask;
|
||||
varying vec2 vTexSampler;
|
||||
|
||||
varying vec4 vColorFraction;
|
||||
varying vec4 vRGBAFraction;
|
||||
@@ -27,9 +36,29 @@ float jet_b(float x)
|
||||
return x < 0.3 ? 4.0 * x + 0.5 : -4.0 * x + 2.5;
|
||||
}
|
||||
|
||||
vec4 Sample(float samplerIndex, vec2 pos)
|
||||
{
|
||||
if (samplerIndex < 1.0)
|
||||
return texture2D(Texture0, pos);
|
||||
else if (samplerIndex < 2.0)
|
||||
return texture2D(Texture1, pos);
|
||||
else if (samplerIndex < 3.0)
|
||||
return texture2D(Texture2, pos);
|
||||
else if (samplerIndex < 4.0)
|
||||
return texture2D(Texture3, pos);
|
||||
else if (samplerIndex < 5.0)
|
||||
return texture2D(Texture4, pos);
|
||||
else if (samplerIndex < 6.0)
|
||||
return texture2D(Texture5, pos);
|
||||
else if (samplerIndex < 7.0)
|
||||
return texture2D(Texture6, pos);
|
||||
|
||||
return texture2D(Texture7, pos);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 x = texture2D(DiffuseTexture, vTexCoord.st);
|
||||
vec4 x = Sample(vTexSampler.s, vTexCoord.st);
|
||||
vec2 p = vec2(dot(x, vChannelMask), vTexMetadata.s);
|
||||
vec4 c = vPalettedFraction * texture2D(Palette, p) + vRGBAFraction * x + vColorFraction * vTexCoord;
|
||||
|
||||
@@ -40,7 +69,7 @@ void main()
|
||||
float depth = gl_FragCoord.z;
|
||||
if (length(vDepthMask) > 0.0)
|
||||
{
|
||||
vec4 y = texture2D(DiffuseTexture, vTexCoord.pq);
|
||||
vec4 y = Sample(vTexSampler.t, vTexCoord.pq);
|
||||
depth = depth + DepthTextureScale * dot(y, vDepthMask);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,15 +9,18 @@ varying vec4 vTexCoord;
|
||||
varying vec2 vTexMetadata;
|
||||
varying vec4 vChannelMask;
|
||||
varying vec4 vDepthMask;
|
||||
varying vec2 vTexSampler;
|
||||
|
||||
varying vec4 vColorFraction;
|
||||
varying vec4 vRGBAFraction;
|
||||
varying vec4 vPalettedFraction;
|
||||
|
||||
vec2 UnpackChannelAttributes(float x)
|
||||
vec4 UnpackChannelAttributes(float x)
|
||||
{
|
||||
// The channel attributes float encodes a set of attributes
|
||||
// stored as flags in the mantissa of the unnormalized float value.
|
||||
// Bits 9-11 define the sampler index (0-7) that the secondary texture is bound to
|
||||
// Bits 6-8 define the sampler index (0-7) that the primary texture is bound to
|
||||
// Bits 3-5 define the behaviour of the secondary texture channel:
|
||||
// 000: Channel is not used
|
||||
// 001, 011, 101, 111: Sample depth sprite from channel R,G,B,A
|
||||
@@ -26,6 +29,16 @@ vec2 UnpackChannelAttributes(float x)
|
||||
// 010: Sample RGBA sprite from all four channels
|
||||
// 001, 011, 101, 111: Sample paletted sprite from channel R,G,B,A
|
||||
|
||||
float secondarySampler = 0.0;
|
||||
if (x >= 2048.0) { x -= 2048.0; secondarySampler += 4.0; }
|
||||
if (x >= 1024.0) { x -= 1024.0; secondarySampler += 2.0; }
|
||||
if (x >= 512.0) { x -= 512.0; secondarySampler += 1.0; }
|
||||
|
||||
float primarySampler = 0.0;
|
||||
if (x >= 256.0) { x -= 256.0; primarySampler += 4.0; }
|
||||
if (x >= 128.0) { x -= 128.0; primarySampler += 2.0; }
|
||||
if (x >= 64.0) { x -= 64.0; primarySampler += 1.0; }
|
||||
|
||||
float secondaryChannel = 0.0;
|
||||
if (x >= 32.0) { x -= 32.0; secondaryChannel += 4.0; }
|
||||
if (x >= 16.0) { x -= 16.0; secondaryChannel += 2.0; }
|
||||
@@ -36,7 +49,7 @@ vec2 UnpackChannelAttributes(float x)
|
||||
if (x >= 2.0) { x -= 2.0; primaryChannel += 2.0; }
|
||||
if (x >= 1.0) { x -= 1.0; primaryChannel += 1.0; }
|
||||
|
||||
return vec2(primaryChannel, secondaryChannel);
|
||||
return vec4(primaryChannel, secondaryChannel, primarySampler, secondarySampler);
|
||||
}
|
||||
|
||||
vec4 SelectChannelMask(float x)
|
||||
@@ -85,10 +98,11 @@ void main()
|
||||
vTexCoord = aVertexTexCoord;
|
||||
vTexMetadata = aVertexTexMetadata;
|
||||
|
||||
vec2 attrib = UnpackChannelAttributes(aVertexTexMetadata.t);
|
||||
vec4 attrib = UnpackChannelAttributes(aVertexTexMetadata.t);
|
||||
vChannelMask = SelectChannelMask(attrib.s);
|
||||
vColorFraction = SelectColorFraction(attrib.s);
|
||||
vRGBAFraction = SelectRGBAFraction(attrib.s);
|
||||
vPalettedFraction = SelectPalettedFraction(attrib.s);
|
||||
vDepthMask = SelectChannelMask(attrib.t);
|
||||
vTexSampler = attrib.pq;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user