Add partial support to the TEX.B shader instruction (#342)

* Add partial support to the TEX.B shader instruction, fix for mixed indexed and non-indexed drawing

* Better exception
This commit is contained in:
gdkchan 2018-08-13 18:22:09 -03:00 committed by GitHub
parent 4518c52c65
commit 09593ae6d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 153 additions and 42 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Ryujinx.Graphics.Gal.Shader namespace Ryujinx.Graphics.Gal.Shader
@ -19,7 +20,7 @@ namespace Ryujinx.Graphics.Gal.Shader
public const int PositionOutAttrLocation = 15; public const int PositionOutAttrLocation = 15;
private const int AttrStartIndex = 8; private const int AttrStartIndex = 8;
private const int TexStartIndex = 8; private const int TexStartIndex = 8;
public const string PositionOutAttrName = "position"; public const string PositionOutAttrName = "position";
@ -46,6 +47,8 @@ namespace Ryujinx.Graphics.Gal.Shader
private string StagePrefix; private string StagePrefix;
private Dictionary<ShaderIrOp, ShaderDeclInfo> m_CbTextures;
private Dictionary<int, ShaderDeclInfo> m_Textures; private Dictionary<int, ShaderDeclInfo> m_Textures;
private Dictionary<int, ShaderDeclInfo> m_Uniforms; private Dictionary<int, ShaderDeclInfo> m_Uniforms;
@ -56,6 +59,8 @@ namespace Ryujinx.Graphics.Gal.Shader
private Dictionary<int, ShaderDeclInfo> m_Gprs; private Dictionary<int, ShaderDeclInfo> m_Gprs;
private Dictionary<int, ShaderDeclInfo> m_Preds; private Dictionary<int, ShaderDeclInfo> m_Preds;
public IReadOnlyDictionary<ShaderIrOp, ShaderDeclInfo> CbTextures => m_CbTextures;
public IReadOnlyDictionary<int, ShaderDeclInfo> Textures => m_Textures; public IReadOnlyDictionary<int, ShaderDeclInfo> Textures => m_Textures;
public IReadOnlyDictionary<int, ShaderDeclInfo> Uniforms => m_Uniforms; public IReadOnlyDictionary<int, ShaderDeclInfo> Uniforms => m_Uniforms;
@ -72,8 +77,10 @@ namespace Ryujinx.Graphics.Gal.Shader
{ {
this.ShaderType = ShaderType; this.ShaderType = ShaderType;
m_Uniforms = new Dictionary<int, ShaderDeclInfo>(); m_CbTextures = new Dictionary<ShaderIrOp, ShaderDeclInfo>();
m_Textures = new Dictionary<int, ShaderDeclInfo>(); m_Textures = new Dictionary<int, ShaderDeclInfo>();
m_Uniforms = new Dictionary<int, ShaderDeclInfo>();
m_Attributes = new Dictionary<int, ShaderDeclInfo>(); m_Attributes = new Dictionary<int, ShaderDeclInfo>();
m_InAttributes = new Dictionary<int, ShaderDeclInfo>(); m_InAttributes = new Dictionary<int, ShaderDeclInfo>();
@ -89,14 +96,16 @@ namespace Ryujinx.Graphics.Gal.Shader
if (ShaderType == GalShaderType.Fragment) if (ShaderType == GalShaderType.Fragment)
{ {
m_Gprs.Add(0, new ShaderDeclInfo(FragmentOutputName, 0, 0, 4)); m_Gprs.Add(0, new ShaderDeclInfo(FragmentOutputName, 0, false, 0, 4));
} }
foreach (ShaderIrBlock Block in Blocks) foreach (ShaderIrBlock Block in Blocks)
{ {
foreach (ShaderIrNode Node in Block.GetNodes()) ShaderIrNode[] Nodes = Block.GetNodes();
foreach (ShaderIrNode Node in Nodes)
{ {
Traverse(null, Node); Traverse(Nodes, null, Node);
} }
} }
} }
@ -152,31 +161,31 @@ namespace Ryujinx.Graphics.Gal.Shader
} }
} }
private void Traverse(ShaderIrNode Parent, ShaderIrNode Node) private void Traverse(ShaderIrNode[] Nodes, ShaderIrNode Parent, ShaderIrNode Node)
{ {
switch (Node) switch (Node)
{ {
case ShaderIrAsg Asg: case ShaderIrAsg Asg:
{ {
Traverse(Asg, Asg.Dst); Traverse(Nodes, Asg, Asg.Dst);
Traverse(Asg, Asg.Src); Traverse(Nodes, Asg, Asg.Src);
break; break;
} }
case ShaderIrCond Cond: case ShaderIrCond Cond:
{ {
Traverse(Cond, Cond.Pred); Traverse(Nodes, Cond, Cond.Pred);
Traverse(Cond, Cond.Child); Traverse(Nodes, Cond, Cond.Child);
break; break;
} }
case ShaderIrOp Op: case ShaderIrOp Op:
{ {
Traverse(Op, Op.OperandA); Traverse(Nodes, Op, Op.OperandA);
Traverse(Op, Op.OperandB); Traverse(Nodes, Op, Op.OperandB);
Traverse(Op, Op.OperandC); Traverse(Nodes, Op, Op.OperandC);
if (Op.Inst == ShaderIrInst.Texq || if (Op.Inst == ShaderIrInst.Texq ||
Op.Inst == ShaderIrInst.Texs || Op.Inst == ShaderIrInst.Texs ||
@ -190,6 +199,38 @@ namespace Ryujinx.Graphics.Gal.Shader
m_Textures.TryAdd(Handle, new ShaderDeclInfo(Name, Handle)); m_Textures.TryAdd(Handle, new ShaderDeclInfo(Name, Handle));
} }
else if (Op.Inst == ShaderIrInst.Texb)
{
ShaderIrNode HandleSrc = null;
int Index = Array.IndexOf(Nodes, Parent) - 1;
for (; Index >= 0; Index--)
{
ShaderIrNode Curr = Nodes[Index];
if (Curr is ShaderIrAsg Asg && Asg.Dst is ShaderIrOperGpr Gpr)
{
if (Gpr.Index == ((ShaderIrOperGpr)Op.OperandC).Index)
{
HandleSrc = Asg.Src;
break;
}
}
}
if (HandleSrc != null && HandleSrc is ShaderIrOperCbuf Cbuf)
{
string Name = StagePrefix + TextureName + "_cb" + Cbuf.Index + "_" + Cbuf.Pos;
m_CbTextures.Add(Op, new ShaderDeclInfo(Name, Cbuf.Pos, true, Cbuf.Index));
}
else
{
throw new NotImplementedException("Shader TEX.B instruction is not fully supported!");
}
}
break; break;
} }
@ -199,7 +240,7 @@ namespace Ryujinx.Graphics.Gal.Shader
{ {
string Name = StagePrefix + UniformName + Cbuf.Index; string Name = StagePrefix + UniformName + Cbuf.Index;
ShaderDeclInfo DeclInfo = new ShaderDeclInfo(Name, Cbuf.Pos, Cbuf.Index); ShaderDeclInfo DeclInfo = new ShaderDeclInfo(Name, Cbuf.Pos, true, Cbuf.Index);
m_Uniforms.Add(Cbuf.Index, DeclInfo); m_Uniforms.Add(Cbuf.Index, DeclInfo);
} }
@ -252,12 +293,13 @@ namespace Ryujinx.Graphics.Gal.Shader
if (!m_Attributes.ContainsKey(Index)) if (!m_Attributes.ContainsKey(Index))
{ {
DeclInfo = new ShaderDeclInfo(AttrName + GlslIndex, GlslIndex, 0, 4); DeclInfo = new ShaderDeclInfo(AttrName + GlslIndex, GlslIndex, false, 0, 4);
m_Attributes.Add(Index, DeclInfo); m_Attributes.Add(Index, DeclInfo);
} }
Traverse(Abuf, Abuf.Vertex); Traverse(Nodes, Abuf, Abuf.Vertex);
break; break;
} }

View file

@ -98,6 +98,7 @@ namespace Ryujinx.Graphics.Gal.Shader
{ ShaderIrInst.Or, GetOrExpr }, { ShaderIrInst.Or, GetOrExpr },
{ ShaderIrInst.Stof, GetStofExpr }, { ShaderIrInst.Stof, GetStofExpr },
{ ShaderIrInst.Sub, GetSubExpr }, { ShaderIrInst.Sub, GetSubExpr },
{ ShaderIrInst.Texb, GetTexbExpr },
{ ShaderIrInst.Texq, GetTexqExpr }, { ShaderIrInst.Texq, GetTexqExpr },
{ ShaderIrInst.Texs, GetTexsExpr }, { ShaderIrInst.Texs, GetTexsExpr },
{ ShaderIrInst.Trunc, GetTruncExpr }, { ShaderIrInst.Trunc, GetTruncExpr },
@ -174,10 +175,12 @@ namespace Ryujinx.Graphics.Gal.Shader
string GlslCode = SB.ToString(); string GlslCode = SB.ToString();
return new GlslProgram( List<ShaderDeclInfo> TextureInfo = new List<ShaderDeclInfo>();
GlslCode,
Decl.Textures.Values, TextureInfo.AddRange(Decl.Textures.Values);
Decl.Uniforms.Values); TextureInfo.AddRange(IterateCbTextures());
return new GlslProgram(GlslCode, TextureInfo, Decl.Uniforms.Values);
} }
private void PrintDeclHeader() private void PrintDeclHeader()
@ -213,9 +216,27 @@ namespace Ryujinx.Graphics.Gal.Shader
private void PrintDeclTextures() private void PrintDeclTextures()
{ {
foreach (ShaderDeclInfo DeclInfo in IterateCbTextures())
{
SB.AppendLine("uniform sampler2D " + DeclInfo.Name + ";");
}
PrintDecls(Decl.Textures, "uniform sampler2D"); PrintDecls(Decl.Textures, "uniform sampler2D");
} }
private IEnumerable<ShaderDeclInfo> IterateCbTextures()
{
HashSet<string> Names = new HashSet<string>();
foreach (ShaderDeclInfo DeclInfo in Decl.CbTextures.Values.OrderBy(DeclKeySelector))
{
if (Names.Add(DeclInfo.Name))
{
yield return DeclInfo;
}
}
}
private void PrintDeclUniforms() private void PrintDeclUniforms()
{ {
if (Decl.ShaderType == GalShaderType.Vertex) if (Decl.ShaderType == GalShaderType.Vertex)
@ -994,6 +1015,22 @@ namespace Ryujinx.Graphics.Gal.Shader
private string GetSubExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "-"); private string GetSubExpr(ShaderIrOp Op) => GetBinaryExpr(Op, "-");
private string GetTexbExpr(ShaderIrOp Op)
{
ShaderIrMetaTex Meta = (ShaderIrMetaTex)Op.MetaData;
if (!Decl.CbTextures.TryGetValue(Op, out ShaderDeclInfo DeclInfo))
{
throw new InvalidOperationException();
}
string Coords = GetTexSamplerCoords(Op);
string Ch = "rgba".Substring(Meta.Elem, 1);
return "texture(" + DeclInfo.Name + ", " + Coords + ")." + Ch;
}
private string GetTexqExpr(ShaderIrOp Op) private string GetTexqExpr(ShaderIrOp Op)
{ {
ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData; ShaderIrMetaTexq Meta = (ShaderIrMetaTexq)Op.MetaData;

View file

@ -121,6 +121,16 @@ namespace Ryujinx.Graphics.Gal.Shader
} }
public static void Tex(ShaderIrBlock Block, long OpCode) public static void Tex(ShaderIrBlock Block, long OpCode)
{
EmitTex(Block, OpCode, GprHandle: false);
}
public static void Tex_B(ShaderIrBlock Block, long OpCode)
{
EmitTex(Block, OpCode, GprHandle: true);
}
private static void EmitTex(ShaderIrBlock Block, long OpCode, bool GprHandle)
{ {
//TODO: Support other formats. //TODO: Support other formats.
ShaderIrOperGpr[] Coords = new ShaderIrOperGpr[2]; ShaderIrOperGpr[] Coords = new ShaderIrOperGpr[2];
@ -139,7 +149,11 @@ namespace Ryujinx.Graphics.Gal.Shader
int ChMask = (int)(OpCode >> 31) & 0xf; int ChMask = (int)(OpCode >> 31) & 0xf;
ShaderIrNode OperC = GetOperImm13_36(OpCode); ShaderIrNode OperC = GprHandle
? (ShaderIrNode)GetOperGpr20 (OpCode)
: (ShaderIrNode)GetOperImm13_36(OpCode);
ShaderIrInst Inst = GprHandle ? ShaderIrInst.Texb : ShaderIrInst.Texs;
for (int Ch = 0; Ch < 4; Ch++) for (int Ch = 0; Ch < 4; Ch++)
{ {
@ -147,7 +161,7 @@ namespace Ryujinx.Graphics.Gal.Shader
ShaderIrMetaTex Meta = new ShaderIrMetaTex(Ch); ShaderIrMetaTex Meta = new ShaderIrMetaTex(Ch);
ShaderIrOp Op = new ShaderIrOp(ShaderIrInst.Texs, Coords[0], Coords[1], OperC, Meta); ShaderIrOp Op = new ShaderIrOp(Inst, Coords[0], Coords[1], OperC, Meta);
Block.AddNode(GetPredNode(new ShaderIrAsg(Dst, Op), OpCode)); Block.AddNode(GetPredNode(new ShaderIrAsg(Dst, Op), OpCode));
} }
@ -178,15 +192,15 @@ namespace Ryujinx.Graphics.Gal.Shader
public static void Texs(ShaderIrBlock Block, long OpCode) public static void Texs(ShaderIrBlock Block, long OpCode)
{ {
EmitTex(Block, OpCode, ShaderIrInst.Texs); EmitTexs(Block, OpCode, ShaderIrInst.Texs);
} }
public static void Tlds(ShaderIrBlock Block, long OpCode) public static void Tlds(ShaderIrBlock Block, long OpCode)
{ {
EmitTex(Block, OpCode, ShaderIrInst.Txlf); EmitTexs(Block, OpCode, ShaderIrInst.Txlf);
} }
private static void EmitTex(ShaderIrBlock Block, long OpCode, ShaderIrInst Inst) private static void EmitTexs(ShaderIrBlock Block, long OpCode, ShaderIrInst Inst)
{ {
//TODO: Support other formats. //TODO: Support other formats.
ShaderIrNode OperA = GetOperGpr8 (OpCode); ShaderIrNode OperA = GetOperGpr8 (OpCode);
@ -195,7 +209,7 @@ namespace Ryujinx.Graphics.Gal.Shader
int LutIndex; int LutIndex;
LutIndex = GetOperGpr0(OpCode).Index != ShaderIrOperGpr.ZRIndex ? 1 : 0; LutIndex = GetOperGpr0(OpCode).Index != ShaderIrOperGpr.ZRIndex ? 1 : 0;
LutIndex |= GetOperGpr28(OpCode).Index != ShaderIrOperGpr.ZRIndex ? 2 : 0; LutIndex |= GetOperGpr28(OpCode).Index != ShaderIrOperGpr.ZRIndex ? 2 : 0;
int ChMask = MaskLut[LutIndex, (OpCode >> 50) & 7]; int ChMask = MaskLut[LutIndex, (OpCode >> 50) & 7];

View file

@ -47,6 +47,7 @@ namespace Ryujinx.Graphics.Gal.Shader
Ftos, Ftos,
Ftou, Ftou,
Ipa, Ipa,
Texb,
Texs, Texs,
Trunc, Trunc,
F_End, F_End,
@ -83,7 +84,7 @@ namespace Ryujinx.Graphics.Gal.Shader
Bra, Bra,
Exit, Exit,
Kil, Kil,
Emit, Emit,
Cut Cut
} }

View file

@ -114,6 +114,7 @@ namespace Ryujinx.Graphics.Gal.Shader
Set("0101110000101x", ShaderDecode.Shr_R); Set("0101110000101x", ShaderDecode.Shr_R);
Set("1110111111110x", ShaderDecode.St_A); Set("1110111111110x", ShaderDecode.St_A);
Set("110000xxxx111x", ShaderDecode.Tex); Set("110000xxxx111x", ShaderDecode.Tex);
Set("1101111010111x", ShaderDecode.Tex_B);
Set("1101111101001x", ShaderDecode.Texq); Set("1101111101001x", ShaderDecode.Texq);
Set("1101100xxxxxxx", ShaderDecode.Texs); Set("1101100xxxxxxx", ShaderDecode.Texs);
Set("1101101xxxxxxx", ShaderDecode.Tlds); Set("1101101xxxxxxx", ShaderDecode.Tlds);

View file

@ -4,14 +4,21 @@ namespace Ryujinx.Graphics.Gal
{ {
public string Name { get; private set; } public string Name { get; private set; }
public int Index { get; private set; } public int Index { get; private set; }
public int Cbuf { get; private set; } public bool IsCb { get; private set; }
public int Size { get; private set; } public int Cbuf { get; private set; }
public int Size { get; private set; }
public ShaderDeclInfo(string Name, int Index, int Cbuf = 0, int Size = 1) public ShaderDeclInfo(
string Name,
int Index,
bool IsCb = false,
int Cbuf = 0,
int Size = 1)
{ {
this.Name = Name; this.Name = Name;
this.Index = Index; this.Index = Index;
this.IsCb = IsCb;
this.Cbuf = Cbuf; this.Cbuf = Cbuf;
this.Size = Size; this.Size = Size;
} }

View file

@ -309,7 +309,7 @@ namespace Ryujinx.HLE.Gpu.Engines
private void SetStencil(GalPipelineState State) private void SetStencil(GalPipelineState State)
{ {
State.StencilTestEnabled = (ReadRegister(NvGpuEngine3dReg.StencilEnable) & 1) != 0; State.StencilTestEnabled = (ReadRegister(NvGpuEngine3dReg.StencilEnable) & 1) != 0;
if (State.StencilTestEnabled) if (State.StencilTestEnabled)
{ {
State.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc); State.StencilBackFuncFunc = (GalComparisonOp)ReadRegister(NvGpuEngine3dReg.StencilBackFuncFunc);
@ -364,17 +364,26 @@ namespace Ryujinx.HLE.Gpu.Engines
int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex); int TextureCbIndex = ReadRegister(NvGpuEngine3dReg.TextureCbIndex);
//Note: On the emulator renderer, Texture Unit 0 is int TexIndex = 0;
//reserved for drawing the frame buffer.
int TexIndex = 1;
for (int Index = 0; Index < Keys.Length; Index++) for (int Index = 0; Index < Keys.Length; Index++)
{ {
foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index])) foreach (ShaderDeclInfo DeclInfo in Gpu.Renderer.Shader.GetTextureUsage(Keys[Index]))
{ {
long Position = ConstBuffers[Index][TextureCbIndex].Position; long Position;
UploadTexture(Vmm, Position, TexIndex, DeclInfo.Index); if (DeclInfo.IsCb)
{
Position = ConstBuffers[Index][DeclInfo.Cbuf].Position;
}
else
{
Position = ConstBuffers[Index][TextureCbIndex].Position;
}
int TextureHandle = Vmm.ReadInt32(Position + DeclInfo.Index * 4);
UploadTexture(Vmm, TexIndex, TextureHandle);
Gpu.Renderer.Shader.EnsureTextureBinding(DeclInfo.Name, TexIndex); Gpu.Renderer.Shader.EnsureTextureBinding(DeclInfo.Name, TexIndex);
@ -383,12 +392,8 @@ namespace Ryujinx.HLE.Gpu.Engines
} }
} }
private void UploadTexture(NvGpuVmm Vmm, long BasePosition, int TexIndex, int HndIndex) private void UploadTexture(NvGpuVmm Vmm, int TexIndex, int TextureHandle)
{ {
long Position = BasePosition + HndIndex * 4;
int TextureHandle = Vmm.ReadInt32(Position);
if (TextureHandle == 0) if (TextureHandle == 0)
{ {
//TODO: Is this correct? //TODO: Is this correct?
@ -601,6 +606,10 @@ namespace Ryujinx.HLE.Gpu.Engines
Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType); Gpu.Renderer.Rasterizer.DrawArrays(VertexFirst, VertexCount, PrimType);
} }
//Is the GPU really clearing those registers after draw?
WriteRegister(NvGpuEngine3dReg.IndexBatchFirst, 0);
WriteRegister(NvGpuEngine3dReg.IndexBatchCount, 0);
} }
private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry) private void QueryControl(NvGpuVmm Vmm, NvGpuPBEntry PBEntry)