2018-11-28 15:09:44 -08:00
|
|
|
using Ryujinx.Graphics.Gal.OpenGL;
|
2018-08-13 14:22:09 -07:00
|
|
|
using System;
|
2018-04-08 12:17:35 -07:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.Shader
|
|
|
|
{
|
|
|
|
class GlslDecl
|
|
|
|
{
|
2018-07-18 22:33:27 -07:00
|
|
|
public const int LayerAttr = 0x064;
|
2018-08-15 22:26:03 -07:00
|
|
|
public const int PointSizeAttr = 0x06c;
|
|
|
|
public const int PointCoordAttrX = 0x2e0;
|
|
|
|
public const int PointCoordAttrY = 0x2e4;
|
2018-05-29 16:37:10 -07:00
|
|
|
public const int TessCoordAttrX = 0x2f0;
|
|
|
|
public const int TessCoordAttrY = 0x2f4;
|
|
|
|
public const int TessCoordAttrZ = 0x2f8;
|
|
|
|
public const int InstanceIdAttr = 0x2f8;
|
2018-04-08 12:17:35 -07:00
|
|
|
public const int VertexIdAttr = 0x2fc;
|
2018-07-03 16:06:13 -07:00
|
|
|
public const int FaceAttr = 0x3fc;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
public const int GlPositionVec4Index = 7;
|
|
|
|
|
2018-06-28 18:01:58 -07:00
|
|
|
public const int PositionOutAttrLocation = 15;
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
private const int AttrStartIndex = 8;
|
2018-08-13 14:22:09 -07:00
|
|
|
private const int TexStartIndex = 8;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-04-10 12:50:32 -07:00
|
|
|
public const string PositionOutAttrName = "position";
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
private const string TextureName = "tex";
|
2018-04-08 12:17:35 -07:00
|
|
|
private const string UniformName = "c";
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
private const string AttrName = "attr";
|
|
|
|
private const string InAttrName = "in_" + AttrName;
|
|
|
|
private const string OutAttrName = "out_" + AttrName;
|
|
|
|
|
|
|
|
private const string GprName = "gpr";
|
|
|
|
private const string PredName = "pred";
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
public const string FragmentOutputName = "FragColor";
|
|
|
|
|
2018-08-09 21:09:40 -07:00
|
|
|
public const string ExtraUniformBlockName = "Extra";
|
2018-06-23 17:39:25 -07:00
|
|
|
public const string FlipUniformName = "flip";
|
2018-08-24 21:16:58 -07:00
|
|
|
public const string InstanceUniformName = "instance";
|
2018-06-23 17:39:25 -07:00
|
|
|
|
2018-08-31 09:14:04 -07:00
|
|
|
public const string BasicBlockName = "bb";
|
|
|
|
public const string BasicBlockAName = BasicBlockName + "_a";
|
|
|
|
public const string BasicBlockBName = BasicBlockName + "_b";
|
|
|
|
|
|
|
|
public const int SsyStackSize = 16;
|
|
|
|
public const string SsyStackName = "ssy_stack";
|
|
|
|
public const string SsyCursorName = "ssy_cursor";
|
2018-06-27 19:55:08 -07:00
|
|
|
|
2018-11-28 15:09:44 -08:00
|
|
|
public static int MaxUboSize => OGLLimit.MaxUboSize / 16;
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
private string[] StagePrefixes = new string[] { "vp", "tcp", "tep", "gp", "fp" };
|
|
|
|
|
|
|
|
private string StagePrefix;
|
|
|
|
|
2018-08-13 14:22:09 -07:00
|
|
|
private Dictionary<ShaderIrOp, ShaderDeclInfo> m_CbTextures;
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
private Dictionary<int, ShaderDeclInfo> m_Textures;
|
2018-05-17 11:25:42 -07:00
|
|
|
private Dictionary<int, ShaderDeclInfo> m_Uniforms;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
private Dictionary<int, ShaderDeclInfo> m_Attributes;
|
2018-04-08 12:17:35 -07:00
|
|
|
private Dictionary<int, ShaderDeclInfo> m_InAttributes;
|
|
|
|
private Dictionary<int, ShaderDeclInfo> m_OutAttributes;
|
|
|
|
|
|
|
|
private Dictionary<int, ShaderDeclInfo> m_Gprs;
|
|
|
|
private Dictionary<int, ShaderDeclInfo> m_Preds;
|
|
|
|
|
2018-08-13 14:22:09 -07:00
|
|
|
public IReadOnlyDictionary<ShaderIrOp, ShaderDeclInfo> CbTextures => m_CbTextures;
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
public IReadOnlyDictionary<int, ShaderDeclInfo> Textures => m_Textures;
|
2018-05-17 11:25:42 -07:00
|
|
|
public IReadOnlyDictionary<int, ShaderDeclInfo> Uniforms => m_Uniforms;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
public IReadOnlyDictionary<int, ShaderDeclInfo> Attributes => m_Attributes;
|
2018-04-08 12:17:35 -07:00
|
|
|
public IReadOnlyDictionary<int, ShaderDeclInfo> InAttributes => m_InAttributes;
|
|
|
|
public IReadOnlyDictionary<int, ShaderDeclInfo> OutAttributes => m_OutAttributes;
|
|
|
|
|
|
|
|
public IReadOnlyDictionary<int, ShaderDeclInfo> Gprs => m_Gprs;
|
|
|
|
public IReadOnlyDictionary<int, ShaderDeclInfo> Preds => m_Preds;
|
|
|
|
|
|
|
|
public GalShaderType ShaderType { get; private set; }
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
private GlslDecl(GalShaderType ShaderType)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
this.ShaderType = ShaderType;
|
|
|
|
|
2018-08-13 14:22:09 -07:00
|
|
|
m_CbTextures = new Dictionary<ShaderIrOp, ShaderDeclInfo>();
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
m_Textures = new Dictionary<int, ShaderDeclInfo>();
|
2018-08-13 14:22:09 -07:00
|
|
|
m_Uniforms = new Dictionary<int, ShaderDeclInfo>();
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
m_Attributes = new Dictionary<int, ShaderDeclInfo>();
|
2018-04-08 12:17:35 -07:00
|
|
|
m_InAttributes = new Dictionary<int, ShaderDeclInfo>();
|
|
|
|
m_OutAttributes = new Dictionary<int, ShaderDeclInfo>();
|
|
|
|
|
|
|
|
m_Gprs = new Dictionary<int, ShaderDeclInfo>();
|
|
|
|
m_Preds = new Dictionary<int, ShaderDeclInfo>();
|
2018-06-27 19:55:08 -07:00
|
|
|
}
|
|
|
|
|
2018-11-28 15:09:44 -08:00
|
|
|
public GlslDecl(ShaderIrBlock[] Blocks, GalShaderType ShaderType, ShaderHeader Header) : this(ShaderType)
|
2018-06-27 19:55:08 -07:00
|
|
|
{
|
|
|
|
StagePrefix = StagePrefixes[(int)ShaderType] + "_";
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
if (ShaderType == GalShaderType.Fragment)
|
|
|
|
{
|
2018-08-22 22:07:23 -07:00
|
|
|
int Index = 0;
|
|
|
|
|
|
|
|
for (int Attachment = 0; Attachment < 8; Attachment++)
|
2018-08-19 18:25:26 -07:00
|
|
|
{
|
2018-08-22 22:07:23 -07:00
|
|
|
for (int Component = 0; Component < 4; Component++)
|
|
|
|
{
|
|
|
|
if (Header.OmapTargets[Attachment].ComponentEnabled(Component))
|
|
|
|
{
|
|
|
|
m_Gprs.TryAdd(Index, new ShaderDeclInfo(GetGprName(Index), Index));
|
|
|
|
|
|
|
|
Index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Header.OmapDepth)
|
|
|
|
{
|
|
|
|
Index = Header.DepthRegister;
|
|
|
|
|
|
|
|
m_Gprs.TryAdd(Index, new ShaderDeclInfo(GetGprName(Index), Index));
|
2018-08-19 18:25:26 -07:00
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 16:37:10 -07:00
|
|
|
foreach (ShaderIrBlock Block in Blocks)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-08-13 14:22:09 -07:00
|
|
|
ShaderIrNode[] Nodes = Block.GetNodes();
|
|
|
|
|
|
|
|
foreach (ShaderIrNode Node in Nodes)
|
2018-05-29 16:37:10 -07:00
|
|
|
{
|
2018-08-13 14:22:09 -07:00
|
|
|
Traverse(Nodes, null, Node);
|
2018-05-29 16:37:10 -07:00
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
public static GlslDecl Merge(GlslDecl VpA, GlslDecl VpB)
|
|
|
|
{
|
|
|
|
GlslDecl Combined = new GlslDecl(GalShaderType.Vertex);
|
|
|
|
|
|
|
|
Merge(Combined.m_Textures, VpA.m_Textures, VpB.m_Textures);
|
|
|
|
Merge(Combined.m_Uniforms, VpA.m_Uniforms, VpB.m_Uniforms);
|
|
|
|
|
|
|
|
Merge(Combined.m_Attributes, VpA.m_Attributes, VpB.m_Attributes);
|
|
|
|
Merge(Combined.m_OutAttributes, VpA.m_OutAttributes, VpB.m_OutAttributes);
|
|
|
|
|
|
|
|
Merge(Combined.m_Gprs, VpA.m_Gprs, VpB.m_Gprs);
|
|
|
|
Merge(Combined.m_Preds, VpA.m_Preds, VpB.m_Preds);
|
|
|
|
|
|
|
|
//Merge input attributes.
|
|
|
|
foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpA.m_InAttributes)
|
|
|
|
{
|
|
|
|
Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (KeyValuePair<int, ShaderDeclInfo> KV in VpB.m_InAttributes)
|
|
|
|
{
|
|
|
|
//If Vertex Program A already writes to this attribute,
|
|
|
|
//then we don't need to add it as an input attribute since
|
|
|
|
//Vertex Program A will already have written to it anyway,
|
|
|
|
//and there's no guarantee that there is an input attribute
|
|
|
|
//for this slot.
|
|
|
|
if (!VpA.m_OutAttributes.ContainsKey(KV.Key))
|
|
|
|
{
|
|
|
|
Combined.m_InAttributes.TryAdd(KV.Key, KV.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Combined;
|
|
|
|
}
|
|
|
|
|
2018-08-22 22:07:23 -07:00
|
|
|
public static string GetGprName(int Index)
|
|
|
|
{
|
|
|
|
return GprName + Index;
|
|
|
|
}
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
private static void Merge(
|
|
|
|
Dictionary<int, ShaderDeclInfo> C,
|
|
|
|
Dictionary<int, ShaderDeclInfo> A,
|
|
|
|
Dictionary<int, ShaderDeclInfo> B)
|
|
|
|
{
|
|
|
|
foreach (KeyValuePair<int, ShaderDeclInfo> KV in A)
|
|
|
|
{
|
|
|
|
C.TryAdd(KV.Key, KV.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (KeyValuePair<int, ShaderDeclInfo> KV in B)
|
|
|
|
{
|
|
|
|
C.TryAdd(KV.Key, KV.Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-13 14:22:09 -07:00
|
|
|
private void Traverse(ShaderIrNode[] Nodes, ShaderIrNode Parent, ShaderIrNode Node)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
switch (Node)
|
|
|
|
{
|
|
|
|
case ShaderIrAsg Asg:
|
|
|
|
{
|
2018-08-13 14:22:09 -07:00
|
|
|
Traverse(Nodes, Asg, Asg.Dst);
|
|
|
|
Traverse(Nodes, Asg, Asg.Src);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderIrCond Cond:
|
|
|
|
{
|
2018-08-13 14:22:09 -07:00
|
|
|
Traverse(Nodes, Cond, Cond.Pred);
|
|
|
|
Traverse(Nodes, Cond, Cond.Child);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderIrOp Op:
|
|
|
|
{
|
2018-08-13 14:22:09 -07:00
|
|
|
Traverse(Nodes, Op, Op.OperandA);
|
|
|
|
Traverse(Nodes, Op, Op.OperandB);
|
|
|
|
Traverse(Nodes, Op, Op.OperandC);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-04-10 12:50:32 -07:00
|
|
|
if (Op.Inst == ShaderIrInst.Texq ||
|
|
|
|
Op.Inst == ShaderIrInst.Texs ||
|
|
|
|
Op.Inst == ShaderIrInst.Txlf)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
int Handle = ((ShaderIrOperImm)Op.OperandC).Value;
|
|
|
|
|
|
|
|
int Index = Handle - TexStartIndex;
|
|
|
|
|
|
|
|
string Name = StagePrefix + TextureName + Index;
|
|
|
|
|
|
|
|
m_Textures.TryAdd(Handle, new ShaderDeclInfo(Name, Handle));
|
|
|
|
}
|
2018-08-13 14:22:09 -07:00
|
|
|
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!");
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderIrOperCbuf Cbuf:
|
|
|
|
{
|
2018-06-27 19:55:08 -07:00
|
|
|
if (!m_Uniforms.ContainsKey(Cbuf.Index))
|
2018-05-17 11:25:42 -07:00
|
|
|
{
|
|
|
|
string Name = StagePrefix + UniformName + Cbuf.Index;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-08-13 14:22:09 -07:00
|
|
|
ShaderDeclInfo DeclInfo = new ShaderDeclInfo(Name, Cbuf.Pos, true, Cbuf.Index);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
2018-05-17 11:25:42 -07:00
|
|
|
m_Uniforms.Add(Cbuf.Index, DeclInfo);
|
|
|
|
}
|
2018-04-08 12:17:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderIrOperAbuf Abuf:
|
|
|
|
{
|
2018-08-15 22:26:03 -07:00
|
|
|
//This is a built-in variable.
|
|
|
|
if (Abuf.Offs == LayerAttr ||
|
|
|
|
Abuf.Offs == PointSizeAttr ||
|
|
|
|
Abuf.Offs == PointCoordAttrX ||
|
|
|
|
Abuf.Offs == PointCoordAttrY ||
|
|
|
|
Abuf.Offs == VertexIdAttr ||
|
|
|
|
Abuf.Offs == InstanceIdAttr ||
|
|
|
|
Abuf.Offs == FaceAttr)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Index = Abuf.Offs >> 4;
|
|
|
|
int Elem = (Abuf.Offs >> 2) & 3;
|
|
|
|
|
|
|
|
int GlslIndex = Index - AttrStartIndex;
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
if (GlslIndex < 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
ShaderDeclInfo DeclInfo;
|
|
|
|
|
|
|
|
if (Parent is ShaderIrAsg Asg && Asg.Dst == Node)
|
|
|
|
{
|
|
|
|
if (!m_OutAttributes.TryGetValue(Index, out DeclInfo))
|
|
|
|
{
|
|
|
|
DeclInfo = new ShaderDeclInfo(OutAttrName + GlslIndex, GlslIndex);
|
|
|
|
|
|
|
|
m_OutAttributes.Add(Index, DeclInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!m_InAttributes.TryGetValue(Index, out DeclInfo))
|
|
|
|
{
|
|
|
|
DeclInfo = new ShaderDeclInfo(InAttrName + GlslIndex, GlslIndex);
|
|
|
|
|
|
|
|
m_InAttributes.Add(Index, DeclInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeclInfo.Enlarge(Elem + 1);
|
|
|
|
|
2018-06-27 19:55:08 -07:00
|
|
|
if (!m_Attributes.ContainsKey(Index))
|
|
|
|
{
|
2018-08-13 14:22:09 -07:00
|
|
|
DeclInfo = new ShaderDeclInfo(AttrName + GlslIndex, GlslIndex, false, 0, 4);
|
2018-06-27 19:55:08 -07:00
|
|
|
|
|
|
|
m_Attributes.Add(Index, DeclInfo);
|
|
|
|
}
|
2018-07-18 22:33:27 -07:00
|
|
|
|
2018-08-13 14:22:09 -07:00
|
|
|
Traverse(Nodes, Abuf, Abuf.Vertex);
|
|
|
|
|
2018-04-08 12:17:35 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderIrOperGpr Gpr:
|
|
|
|
{
|
2018-08-22 22:07:23 -07:00
|
|
|
if (!Gpr.IsConst)
|
2018-04-08 12:17:35 -07:00
|
|
|
{
|
2018-08-22 22:07:23 -07:00
|
|
|
string Name = GetGprName(Gpr.Index);
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
m_Gprs.TryAdd(Gpr.Index, new ShaderDeclInfo(Name, Gpr.Index));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ShaderIrOperPred Pred:
|
|
|
|
{
|
|
|
|
if (!Pred.IsConst && !HasName(m_Preds, Pred.Index))
|
|
|
|
{
|
|
|
|
string Name = PredName + Pred.Index;
|
|
|
|
|
|
|
|
m_Preds.TryAdd(Pred.Index, new ShaderDeclInfo(Name, Pred.Index));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool HasName(Dictionary<int, ShaderDeclInfo> Decls, int Index)
|
|
|
|
{
|
2018-06-27 19:55:08 -07:00
|
|
|
//This is used to check if the dictionary already contains
|
|
|
|
//a entry for a vector at a given index position.
|
|
|
|
//Used to enable turning gprs into vectors.
|
|
|
|
int VecIndex = Index & ~3;
|
2018-04-08 12:17:35 -07:00
|
|
|
|
|
|
|
if (Decls.TryGetValue(VecIndex, out ShaderDeclInfo DeclInfo))
|
|
|
|
{
|
|
|
|
if (DeclInfo.Size > 1 && Index < VecIndex + DeclInfo.Size)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Decls.ContainsKey(Index);
|
|
|
|
}
|
|
|
|
}
|
2018-08-15 22:26:03 -07:00
|
|
|
}
|