Improve shader sending method to GAL, use a memory interface instead of reading a fixed array size and sending every time

This commit is contained in:
gdkchan 2018-05-22 22:43:31 -03:00
parent 84996ccd36
commit 79e0070363
12 changed files with 72 additions and 63 deletions

View file

@ -0,0 +1,7 @@
namespace Ryujinx.Graphics.Gal
{
public unsafe interface IGalMemory
{
int ReadInt32(long Position);
}
}

View file

@ -58,7 +58,7 @@ namespace Ryujinx.Graphics.Gal
void DrawElements(int VbIndex, int First, GalPrimitiveType PrimType);
//Shader
void CreateShader(long Tag, GalShaderType Type, byte[] Data);
void CreateShader(IGalMemory Memory, long Tag, GalShaderType Type);
IEnumerable<ShaderDeclInfo> GetTextureUsage(long Tag);

View file

@ -3,7 +3,6 @@ using Ryujinx.Graphics.Gal.Shader;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Ryujinx.Graphics.Gal.OpenGL
@ -85,14 +84,14 @@ namespace Ryujinx.Graphics.Gal.OpenGL
Programs = new Dictionary<ShaderProgram, int>();
}
public void Create(long Tag, GalShaderType Type, byte[] Data)
public void Create(IGalMemory Memory, long Tag, GalShaderType Type)
{
Stages.GetOrAdd(Tag, (Key) => ShaderStageFactory(Type, Tag, Data));
Stages.GetOrAdd(Tag, (Key) => ShaderStageFactory(Memory, Tag, Type));
}
private ShaderStage ShaderStageFactory(GalShaderType Type, long Tag, byte[] Data)
private ShaderStage ShaderStageFactory(IGalMemory Memory, long Position, GalShaderType Type)
{
GlslProgram Program = GetGlslProgram(Data, Type);
GlslProgram Program = GetGlslProgram(Memory, Position, Type);
return new ShaderStage(
Type,
@ -101,25 +100,11 @@ namespace Ryujinx.Graphics.Gal.OpenGL
Program.Uniforms);
}
private GlslProgram GetGlslProgram(byte[] Data, GalShaderType Type)
private GlslProgram GetGlslProgram(IGalMemory Memory, long Position, GalShaderType Type)
{
int[] Code = new int[(Data.Length - 0x50) >> 2];
using (MemoryStream MS = new MemoryStream(Data))
{
MS.Seek(0x50, SeekOrigin.Begin);
BinaryReader Reader = new BinaryReader(MS);
for (int Index = 0; Index < Code.Length; Index++)
{
Code[Index] = Reader.ReadInt32();
}
}
GlslDecompiler Decompiler = new GlslDecompiler();
return Decompiler.Decompile(Code, Type);
return Decompiler.Decompile(Memory, Position + 0x50, Type);
}
public IEnumerable<ShaderDeclInfo> GetTextureUsage(long Tag)

View file

@ -198,14 +198,14 @@ namespace Ryujinx.Graphics.Gal.OpenGL
ActionsQueue.Enqueue(() => Rasterizer.DrawElements(VbIndex, First, PrimType));
}
public void CreateShader(long Tag, GalShaderType Type, byte[] Data)
public void CreateShader(IGalMemory Memory, long Tag, GalShaderType Type)
{
if (Data == null)
if (Memory == null)
{
throw new ArgumentNullException(nameof(Data));
throw new ArgumentNullException(nameof(Memory));
}
Shader.Create(Tag, Type, Data);
Shader.Create(Memory, Tag, Type);
}
public void SetConstBuffer(long Tag, int Cbuf, byte[] Data)

View file

@ -89,9 +89,9 @@ namespace Ryujinx.Graphics.Gal.Shader
};
}
public GlslProgram Decompile(int[] Code, GalShaderType ShaderType)
public GlslProgram Decompile(IGalMemory Memory, long Position, GalShaderType ShaderType)
{
ShaderIrBlock Block = ShaderDecoder.DecodeBasicBlock(Code, 0);
ShaderIrBlock Block = ShaderDecoder.DecodeBasicBlock(Memory, Position);
ShaderIrNode[] Nodes = Block.GetNodes();

View file

@ -15,7 +15,7 @@ namespace Ryujinx.Graphics.Gal.Shader
throw new NotImplementedException();
}
int Target = ((int)(OpCode >> 20) << 8) >> 8;
long Target = ((int)(OpCode >> 20) << 8) >> 8;
Target += Block.Position + 8;

View file

@ -4,28 +4,28 @@ namespace Ryujinx.Graphics.Gal.Shader
{
private const bool AddDbgComments = true;
public static ShaderIrBlock DecodeBasicBlock(int[] Code, int Offset)
public static ShaderIrBlock DecodeBasicBlock(IGalMemory Memory, long Position)
{
ShaderIrBlock Block = new ShaderIrBlock();
while (Offset + 2 <= Code.Length)
while (true)
{
int InstPos = Offset * 4;
Block.Position = Position;
Block.Position = InstPos;
Block.MarkLabel(InstPos);
Block.MarkLabel(Position);
//Ignore scheduling instructions, which are written every 32 bytes.
if ((Offset & 7) == 0)
if ((Position & 0x1f) == 0)
{
Offset += 2;
Position += 8;
continue;
}
uint Word0 = (uint)Code[Offset++];
uint Word1 = (uint)Code[Offset++];
uint Word0 = (uint)Memory.ReadInt32(Position + 0);
uint Word1 = (uint)Memory.ReadInt32(Position + 4);
Position += 8;
long OpCode = Word0 | (long)Word1 << 32;
@ -33,7 +33,7 @@ namespace Ryujinx.Graphics.Gal.Shader
if (AddDbgComments)
{
string DbgOpCode = $"0x{InstPos:x8}: 0x{OpCode:x16} ";
string DbgOpCode = $"0x{Position:x16}: 0x{OpCode:x16} ";
Block.AddNode(new ShaderIrCmnt(DbgOpCode + (Decode?.Method.Name ?? "???")));
}

View file

@ -6,15 +6,15 @@ namespace Ryujinx.Graphics.Gal.Shader
{
private List<ShaderIrNode> Nodes;
private Dictionary<int, ShaderIrLabel> LabelsToInsert;
private Dictionary<long, ShaderIrLabel> LabelsToInsert;
public int Position;
public long Position;
public ShaderIrBlock()
{
Nodes = new List<ShaderIrNode>();
LabelsToInsert = new Dictionary<int, ShaderIrLabel>();
LabelsToInsert = new Dictionary<long, ShaderIrLabel>();
}
public void AddNode(ShaderIrNode Node)
@ -22,7 +22,7 @@ namespace Ryujinx.Graphics.Gal.Shader
Nodes.Add(Node);
}
public ShaderIrLabel GetLabel(int Position)
public ShaderIrLabel GetLabel(long Position)
{
if (LabelsToInsert.TryGetValue(Position, out ShaderIrLabel Label))
{
@ -36,7 +36,7 @@ namespace Ryujinx.Graphics.Gal.Shader
return Label;
}
public void MarkLabel(int Position)
public void MarkLabel(long Position)
{
if (LabelsToInsert.TryGetValue(Position, out ShaderIrLabel Label))
{