2019-02-28 01:12:24 +00:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
using Ryujinx.Graphics.Texture;
|
2018-04-08 19:17:35 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Gal.Shader
|
|
|
|
{
|
2018-05-17 18:25:42 +00:00
|
|
|
public class GlslDecompiler
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
private delegate string GetInstExpr(ShaderIrOp op);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private Dictionary<ShaderIrInst, GetInstExpr> _instsExpr;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
|
|
|
private enum OperType
|
|
|
|
{
|
|
|
|
Bool,
|
|
|
|
F32,
|
|
|
|
I32
|
|
|
|
}
|
|
|
|
|
|
|
|
private const string IdentationStr = " ";
|
|
|
|
|
2018-07-19 05:33:27 +00:00
|
|
|
private const int MaxVertexInput = 3;
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private GlslDecl _decl;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private ShaderHeader _header, _headerB;
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private ShaderIrBlock[] _blocks, _blocksB;
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private StringBuilder _sb;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-12-18 00:32:12 +00:00
|
|
|
public int MaxUboSize { get; }
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private bool _isNvidiaDriver;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
public GlslDecompiler(int maxUboSize, bool isNvidiaDriver)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_instsExpr = new Dictionary<ShaderIrInst, GetInstExpr>()
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-05-17 18:25:42 +00:00
|
|
|
{ ShaderIrInst.Abs, GetAbsExpr },
|
|
|
|
{ ShaderIrInst.Add, GetAddExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.And, GetAndExpr },
|
|
|
|
{ ShaderIrInst.Asr, GetAsrExpr },
|
|
|
|
{ ShaderIrInst.Band, GetBandExpr },
|
|
|
|
{ ShaderIrInst.Bnot, GetBnotExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Bor, GetBorExpr },
|
|
|
|
{ ShaderIrInst.Bxor, GetBxorExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Ceil, GetCeilExpr },
|
|
|
|
{ ShaderIrInst.Ceq, GetCeqExpr },
|
|
|
|
{ ShaderIrInst.Cge, GetCgeExpr },
|
|
|
|
{ ShaderIrInst.Cgt, GetCgtExpr },
|
|
|
|
{ ShaderIrInst.Clamps, GetClampsExpr },
|
|
|
|
{ ShaderIrInst.Clampu, GetClampuExpr },
|
|
|
|
{ ShaderIrInst.Cle, GetCleExpr },
|
|
|
|
{ ShaderIrInst.Clt, GetCltExpr },
|
|
|
|
{ ShaderIrInst.Cne, GetCneExpr },
|
2018-07-19 05:33:27 +00:00
|
|
|
{ ShaderIrInst.Cut, GetCutExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Exit, GetExitExpr },
|
2018-05-17 18:25:42 +00:00
|
|
|
{ ShaderIrInst.Fabs, GetAbsExpr },
|
|
|
|
{ ShaderIrInst.Fadd, GetAddExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Fceq, GetCeqExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Fcequ, GetCequExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Fcge, GetCgeExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Fcgeu, GetCgeuExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Fcgt, GetCgtExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Fcgtu, GetCgtuExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Fclamp, GetFclampExpr },
|
|
|
|
{ ShaderIrInst.Fcle, GetCleExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Fcleu, GetCleuExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Fclt, GetCltExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Fcltu, GetCltuExpr },
|
|
|
|
{ ShaderIrInst.Fcnan, GetCnanExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Fcne, GetCneExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Fcneu, GetCneuExpr },
|
|
|
|
{ ShaderIrInst.Fcnum, GetCnumExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Fcos, GetFcosExpr },
|
|
|
|
{ ShaderIrInst.Fex2, GetFex2Expr },
|
|
|
|
{ ShaderIrInst.Ffma, GetFfmaExpr },
|
|
|
|
{ ShaderIrInst.Flg2, GetFlg2Expr },
|
|
|
|
{ ShaderIrInst.Floor, GetFloorExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Fmax, GetMaxExpr },
|
|
|
|
{ ShaderIrInst.Fmin, GetMinExpr },
|
2018-05-17 18:25:42 +00:00
|
|
|
{ ShaderIrInst.Fmul, GetMulExpr },
|
|
|
|
{ ShaderIrInst.Fneg, GetNegExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Frcp, GetFrcpExpr },
|
|
|
|
{ ShaderIrInst.Frsq, GetFrsqExpr },
|
|
|
|
{ ShaderIrInst.Fsin, GetFsinExpr },
|
2018-06-23 05:00:44 +00:00
|
|
|
{ ShaderIrInst.Fsqrt, GetFsqrtExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Ftos, GetFtosExpr },
|
|
|
|
{ ShaderIrInst.Ftou, GetFtouExpr },
|
|
|
|
{ ShaderIrInst.Ipa, GetIpaExpr },
|
|
|
|
{ ShaderIrInst.Kil, GetKilExpr },
|
2018-05-17 18:25:42 +00:00
|
|
|
{ ShaderIrInst.Lsl, GetLslExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Lsr, GetLsrExpr },
|
2018-05-29 23:37:10 +00:00
|
|
|
{ ShaderIrInst.Max, GetMaxExpr },
|
|
|
|
{ ShaderIrInst.Min, GetMinExpr },
|
2018-05-17 18:25:42 +00:00
|
|
|
{ ShaderIrInst.Mul, GetMulExpr },
|
|
|
|
{ ShaderIrInst.Neg, GetNegExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Not, GetNotExpr },
|
|
|
|
{ ShaderIrInst.Or, GetOrExpr },
|
|
|
|
{ ShaderIrInst.Stof, GetStofExpr },
|
2018-05-17 18:25:42 +00:00
|
|
|
{ ShaderIrInst.Sub, GetSubExpr },
|
2018-08-13 21:22:09 +00:00
|
|
|
{ ShaderIrInst.Texb, GetTexbExpr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Texq, GetTexqExpr },
|
|
|
|
{ ShaderIrInst.Texs, GetTexsExpr },
|
2019-02-28 01:12:24 +00:00
|
|
|
{ ShaderIrInst.Tld4, GetTld4Expr },
|
2018-04-26 02:11:26 +00:00
|
|
|
{ ShaderIrInst.Trunc, GetTruncExpr },
|
|
|
|
{ ShaderIrInst.Txlf, GetTxlfExpr },
|
|
|
|
{ ShaderIrInst.Utof, GetUtofExpr },
|
|
|
|
{ ShaderIrInst.Xor, GetXorExpr }
|
2018-04-08 19:17:35 +00:00
|
|
|
};
|
2018-12-18 00:32:12 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
MaxUboSize = maxUboSize / 16;
|
|
|
|
_isNvidiaDriver = isNvidiaDriver;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 02:55:08 +00:00
|
|
|
public GlslProgram Decompile(
|
2019-03-04 01:45:25 +00:00
|
|
|
IGalMemory memory,
|
|
|
|
long vpAPosition,
|
|
|
|
long vpBPosition,
|
|
|
|
GalShaderType shaderType)
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_header = new ShaderHeader(memory, vpAPosition);
|
|
|
|
_headerB = new ShaderHeader(memory, vpBPosition);
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_blocks = ShaderDecoder.Decode(memory, vpAPosition);
|
|
|
|
_blocksB = ShaderDecoder.Decode(memory, vpBPosition);
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
GlslDecl declVpA = new GlslDecl(_blocks, shaderType, _header);
|
|
|
|
GlslDecl declVpB = new GlslDecl(_blocksB, shaderType, _headerB);
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_decl = GlslDecl.Merge(declVpA, declVpB);
|
2018-06-28 02:55:08 +00:00
|
|
|
|
|
|
|
return Decompile();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
public GlslProgram Decompile(IGalMemory memory, long position, GalShaderType shaderType)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_header = new ShaderHeader(memory, position);
|
|
|
|
_headerB = null;
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_blocks = ShaderDecoder.Decode(memory, position);
|
|
|
|
_blocksB = null;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_decl = new GlslDecl(_blocks, shaderType, _header);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-06-28 02:55:08 +00:00
|
|
|
return Decompile();
|
|
|
|
}
|
|
|
|
|
|
|
|
private GlslProgram Decompile()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb = new StringBuilder();
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("#version 410 core");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2018-07-19 05:33:27 +00:00
|
|
|
PrintDeclHeader();
|
2018-04-08 19:17:35 +00:00
|
|
|
PrintDeclTextures();
|
|
|
|
PrintDeclUniforms();
|
2018-06-28 02:55:08 +00:00
|
|
|
PrintDeclAttributes();
|
2018-04-08 19:17:35 +00:00
|
|
|
PrintDeclInAttributes();
|
|
|
|
PrintDeclOutAttributes();
|
|
|
|
PrintDeclGprs();
|
|
|
|
PrintDeclPreds();
|
2018-08-31 16:14:04 +00:00
|
|
|
PrintDeclSsy();
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_blocksB != null)
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintBlockScope(_blocks, GlslDecl.BasicBlockAName);
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintBlockScope(_blocksB, GlslDecl.BasicBlockBName);
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintBlockScope(_blocks, GlslDecl.BasicBlockName);
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-06-28 02:55:08 +00:00
|
|
|
|
|
|
|
PrintMain();
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string glslCode = _sb.ToString();
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
List<ShaderDeclInfo> textureInfo = new List<ShaderDeclInfo>();
|
2018-08-13 21:22:09 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
textureInfo.AddRange(_decl.Textures.Values);
|
|
|
|
textureInfo.AddRange(IterateCbTextures());
|
2018-08-13 21:22:09 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return new GlslProgram(glslCode, textureInfo, _decl.Uniforms.Values);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 05:33:27 +00:00
|
|
|
private void PrintDeclHeader()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Geometry)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int maxVertices = _header.MaxOutputVertexCount;
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string outputTopology;
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (_header.OutputTopology)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
case ShaderHeader.PointList: outputTopology = "points"; break;
|
|
|
|
case ShaderHeader.LineStrip: outputTopology = "line_strip"; break;
|
|
|
|
case ShaderHeader.TriangleStrip: outputTopology = "triangle_strip"; break;
|
2018-07-19 05:33:27 +00:00
|
|
|
|
|
|
|
default: throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("#extension GL_ARB_enhanced_layouts : require");
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("// Stubbed. Maxwell geometry shaders don't inform input geometry type");
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("layout(triangles) in;" + Environment.NewLine);
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine($"layout({outputTopology}, max_vertices = {maxVertices}) out;");
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetSamplerType(TextureTarget textureTarget, bool hasShadow)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string result;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (textureTarget)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
|
|
|
case TextureTarget.Texture1D:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler1D";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.Texture2D:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler2D";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.Texture3D:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler3D";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.TextureCubeMap:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "samplerCube";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.TextureRectangle:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler2DRect";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.Texture1DArray:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler1DArray";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.Texture2DArray:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler2DArray";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.TextureCubeMapArray:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "samplerCubeArray";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.TextureBuffer:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "samplerBuffer";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.Texture2DMultisample:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler2DMS";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
case TextureTarget.Texture2DMultisampleArray:
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "sampler2DMSArray";
|
2019-02-28 01:12:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new NotSupportedException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (hasShadow)
|
|
|
|
result += "Shadow";
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return result;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
private void PrintDeclTextures()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderDeclInfo declInfo in IterateCbTextures())
|
2018-08-13 21:22:09 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
TextureTarget target = ImageUtils.GetTextureTarget(declInfo.TextureTarget);
|
|
|
|
_sb.AppendLine($"// {declInfo.TextureSuffix}");
|
|
|
|
_sb.AppendLine("uniform " + GetSamplerType(target, (declInfo.TextureSuffix & TextureInstructionSuffix.Dc) != 0) + " " + declInfo.Name + ";");
|
2018-08-13 21:22:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderDeclInfo declInfo in _decl.Textures.Values.OrderBy(DeclKeySelector))
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
TextureTarget target = ImageUtils.GetTextureTarget(declInfo.TextureTarget);
|
|
|
|
_sb.AppendLine($"// {declInfo.TextureSuffix}");
|
|
|
|
_sb.AppendLine("uniform " + GetSamplerType(target, (declInfo.TextureSuffix & TextureInstructionSuffix.Dc) != 0) + " " + declInfo.Name + ";");
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 21:22:09 +00:00
|
|
|
private IEnumerable<ShaderDeclInfo> IterateCbTextures()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
HashSet<string> names = new HashSet<string>();
|
2018-08-13 21:22:09 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderDeclInfo declInfo in _decl.CbTextures.Values.OrderBy(DeclKeySelector))
|
2018-08-13 21:22:09 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (names.Add(declInfo.Name))
|
2018-08-13 21:22:09 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
yield return declInfo;
|
2018-08-13 21:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
private void PrintDeclUniforms()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Vertex)
|
2018-04-14 03:39:24 +00:00
|
|
|
{
|
2018-08-25 04:16:58 +00:00
|
|
|
//Memory layout here is [flip_x, flip_y, instance, unused]
|
|
|
|
//It's using 4 bytes, not 8
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("layout (std140) uniform " + GlslDecl.ExtraUniformBlockName + " {");
|
2018-08-10 04:09:40 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "vec2 " + GlslDecl.FlipUniformName + ";");
|
2018-08-10 04:09:40 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "int " + GlslDecl.InstanceUniformName + ";");
|
2018-08-25 04:16:58 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("};");
|
|
|
|
_sb.AppendLine();
|
2018-04-14 03:39:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderDeclInfo declInfo in _decl.Uniforms.Values.OrderBy(DeclKeySelector))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine($"layout (std140) uniform {declInfo.Name} {{");
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine($"{IdentationStr}vec4 {declInfo.Name}_data[{MaxUboSize}];");
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("};");
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.Uniforms.Count > 0)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-28 02:55:08 +00:00
|
|
|
private void PrintDeclAttributes()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string geometryArray = (_decl.ShaderType == GalShaderType.Geometry) ? "[" + MaxVertexInput + "]" : "";
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintDecls(_decl.Attributes, suffix: geometryArray);
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
private void PrintDeclInAttributes()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Fragment)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") in vec4 " + GlslDecl.PositionOutAttrName + ";");
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Geometry)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.InAttributes.Count > 0)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("in Vertex {");
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderDeclInfo declInfo in _decl.InAttributes.Values.OrderBy(DeclKeySelector))
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (declInfo.Index >= 0)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "layout (location = " + declInfo.Index + ") vec4 " + declInfo.Name + "; ");
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("} block_in[];" + Environment.NewLine);
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintDeclAttributes(_decl.InAttributes.Values, "in");
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void PrintDeclOutAttributes()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Fragment)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int count = 0;
|
2018-10-23 20:59:52 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
for (int attachment = 0; attachment < 8; attachment++)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_header.OmapTargets[attachment].Enabled)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("layout (location = " + attachment + ") out vec4 " + GlslDecl.FragmentOutputName + attachment + ";");
|
2018-10-23 20:59:52 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
count++;
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-23 20:59:52 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (count > 0)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-10-23 20:59:52 +00:00
|
|
|
}
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|
|
|
|
else
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("layout (location = " + GlslDecl.PositionOutAttrLocation + ") out vec4 " + GlslDecl.PositionOutAttrName + ";");
|
|
|
|
_sb.AppendLine();
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintDeclAttributes(_decl.OutAttributes.Values, "out");
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private void PrintDeclAttributes(IEnumerable<ShaderDeclInfo> decls, string inOut)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int count = 0;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderDeclInfo declInfo in decls.OrderBy(DeclKeySelector))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (declInfo.Index >= 0)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("layout (location = " + declInfo.Index + ") " + inOut + " vec4 " + declInfo.Name + ";");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
count++;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (count > 0)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PrintDeclGprs()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintDecls(_decl.Gprs);
|
|
|
|
PrintDecls(_decl.GprsHalf);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void PrintDeclPreds()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintDecls(_decl.Preds, "bool");
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
private void PrintDeclSsy()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("uint " + GlslDecl.SsyCursorName + " = 0;");
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("uint " + GlslDecl.SsyStackName + "[" + GlslDecl.SsyStackSize + "];" + Environment.NewLine);
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private void PrintDecls(IReadOnlyDictionary<int, ShaderDeclInfo> dict, string customType = null, string suffix = "")
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderDeclInfo declInfo in dict.Values.OrderBy(DeclKeySelector))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string name;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (customType != null)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
name = customType + " " + declInfo.Name + suffix + ";";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (declInfo.Name.Contains(GlslDecl.FragmentOutputName))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
name = "layout (location = " + declInfo.Index / 4 + ") out vec4 " + declInfo.Name + suffix + ";";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
name = GetDecl(declInfo) + suffix + ";";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(name);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (dict.Count > 0)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine();
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private int DeclKeySelector(ShaderDeclInfo declInfo)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return declInfo.Cbuf << 24 | declInfo.Index;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetDecl(ShaderDeclInfo declInfo)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (declInfo.Size == 4)
|
2018-08-07 01:26:19 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "vec4 " + declInfo.Name;
|
2018-08-07 01:26:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "float " + declInfo.Name;
|
2018-08-07 01:26:19 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2018-06-28 02:55:08 +00:00
|
|
|
private void PrintMain()
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("void main() {");
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (KeyValuePair<int, ShaderDeclInfo> kv in _decl.InAttributes)
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!_decl.Attributes.TryGetValue(kv.Key, out ShaderDeclInfo attr))
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderDeclInfo declInfo = kv.Value;
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Geometry)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
for (int vertex = 0; vertex < MaxVertexInput; vertex++)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string dst = attr.Name + "[" + vertex + "]";
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string src = "block_in[" + vertex + "]." + declInfo.Name;
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + dst + " = " + src + ";");
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + attr.Name + " = " + declInfo.Name + ";");
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "uint pc;");
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_blocksB != null)
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintProgram(_blocks, GlslDecl.BasicBlockAName);
|
|
|
|
PrintProgram(_blocksB, GlslDecl.BasicBlockBName);
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintProgram(_blocks, GlslDecl.BasicBlockName);
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType != GalShaderType.Geometry)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
|
|
|
PrintAttrToOutput();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Fragment)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_header.OmapDepth)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "gl_FragDepth = " + GlslDecl.GetGprName(_header.DepthRegister) + ";");
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
int gprIndex = 0;
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
for (int attachment = 0; attachment < 8; attachment++)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string output = GlslDecl.FragmentOutputName + attachment;
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
OmapTarget target = _header.OmapTargets[attachment];
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
for (int component = 0; component < 4; component++)
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (target.ComponentEnabled(component))
|
2018-08-23 05:07:23 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + output + "[" + component + "] = " + GlslDecl.GetGprName(gprIndex) + ";");
|
2018-08-23 05:07:23 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
gprIndex++;
|
2018-08-23 05:07:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("}");
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private void PrintProgram(ShaderIrBlock[] blocks, string name)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
const string ident1 = IdentationStr;
|
|
|
|
const string ident2 = ident1 + IdentationStr;
|
|
|
|
const string ident3 = ident2 + IdentationStr;
|
|
|
|
const string ident4 = ident3 + IdentationStr;
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(ident1 + "pc = " + GetBlockPosition(blocks[0]) + ";");
|
|
|
|
_sb.AppendLine(ident1 + "do {");
|
|
|
|
_sb.AppendLine(ident2 + "switch (pc) {");
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderIrBlock block in blocks)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string functionName = block.Position.ToString("x8");
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(ident3 + "case 0x" + functionName + ": pc = " + name + "_" + functionName + "(); break;");
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(ident3 + "default:");
|
|
|
|
_sb.AppendLine(ident4 + "pc = 0;");
|
|
|
|
_sb.AppendLine(ident4 + "break;");
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(ident2 + "}");
|
|
|
|
_sb.AppendLine(ident1 + "} while (pc != 0);");
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private void PrintAttrToOutput(string identation = IdentationStr)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (KeyValuePair<int, ShaderDeclInfo> kv in _decl.OutAttributes)
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!_decl.Attributes.TryGetValue(kv.Key, out ShaderDeclInfo attr))
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderDeclInfo declInfo = kv.Value;
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string name = attr.Name;
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Geometry)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
name += "[0]";
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + declInfo.Name + " = " + name + ";");
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Vertex)
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + "gl_Position.xy *= " + GlslDecl.FlipUniformName + ";");
|
2018-07-14 16:08:39 +00:00
|
|
|
}
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType != GalShaderType.Fragment)
|
2018-07-14 16:08:39 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + GlslDecl.PositionOutAttrName + " = gl_Position;");
|
|
|
|
_sb.AppendLine(identation + GlslDecl.PositionOutAttrName + ".w = 1;");
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private void PrintBlockScope(ShaderIrBlock[] blocks, string name)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderIrBlock block in blocks)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("uint " + name + "_" + block.Position.ToString("x8") + "() {");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintNodes(block, block.GetNodes());
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine("}" + Environment.NewLine);
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private void PrintNodes(ShaderIrBlock block, ShaderIrNode[] nodes)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
foreach (ShaderIrNode node in nodes)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintNode(block, node, IdentationStr);
|
2018-10-23 20:59:52 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (nodes.Length == 0)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "return 0u;");
|
2018-10-23 20:59:52 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrNode last = nodes[nodes.Length - 1];
|
2018-10-23 20:59:52 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
bool unconditionalFlowChange = false;
|
2018-10-23 20:59:52 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (last is ShaderIrOp op)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (op.Inst)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
|
|
|
case ShaderIrInst.Bra:
|
|
|
|
case ShaderIrInst.Exit:
|
|
|
|
case ShaderIrInst.Sync:
|
2019-03-04 01:45:25 +00:00
|
|
|
unconditionalFlowChange = true;
|
2018-10-23 20:59:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!unconditionalFlowChange)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (block.Next != null)
|
2018-10-23 20:59:52 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "return " + GetBlockPosition(block.Next) + ";");
|
2018-10-23 20:59:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(IdentationStr + "return 0u;");
|
2018-10-23 20:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private void PrintNode(ShaderIrBlock block, ShaderIrNode node, string identation)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (node is ShaderIrCond cond)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string ifExpr = GetSrcExpr(cond.Pred, true);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (cond.Not)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ifExpr = "!(" + ifExpr + ")";
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + "if (" + ifExpr + ") {");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintNode(block, cond.Child, identation + IdentationStr);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + "}");
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (node is ShaderIrAsg asg)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (IsValidOutOper(asg.Dst))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string expr = GetSrcExpr(asg.Src, true);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
expr = GetExprWithCast(asg.Dst, asg.Src, expr);
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + GetDstOperName(asg.Dst) + " = " + expr + ";");
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (node is ShaderIrOp op)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (op.Inst)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
|
|
|
case ShaderIrInst.Bra:
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + "return " + GetBlockPosition(block.Branch) + ";");
|
2018-08-31 16:14:04 +00:00
|
|
|
|
|
|
|
break;
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
case ShaderIrInst.Emit:
|
2018-05-17 18:25:42 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
PrintAttrToOutput(identation);
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + "EmitVertex();");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
case ShaderIrInst.Ssy:
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string stackIndex = GlslDecl.SsyStackName + "[" + GlslDecl.SsyCursorName + "]";
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
int targetPosition = (op.OperandA as ShaderIrOperImm).Value;
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string target = "0x" + targetPosition.ToString("x8") + "u";
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + stackIndex + " = " + target + ";");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + GlslDecl.SsyCursorName + "++;");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
break;
|
2018-05-17 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
case ShaderIrInst.Sync:
|
2018-04-29 20:58:38 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + GlslDecl.SsyCursorName + "--;");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string target = GlslDecl.SsyStackName + "[" + GlslDecl.SsyCursorName + "]";
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + "return " + target + ";");
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
break;
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
2018-08-31 16:14:04 +00:00
|
|
|
|
|
|
|
default:
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + GetSrcExpr(op, true) + ";");
|
2018-09-18 04:30:35 +00:00
|
|
|
|
2018-08-31 16:14:04 +00:00
|
|
|
break;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (node is ShaderIrCmnt cmnt)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
_sb.AppendLine(identation + "// " + cmnt.Comment);
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private bool IsValidOutOper(ShaderIrNode node)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (node is ShaderIrOperGpr gpr && gpr.IsConst)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (node is ShaderIrOperPred pred && pred.IsConst)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetDstOperName(ShaderIrNode node)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (node is ShaderIrOperAbuf abuf)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetOutAbufName(abuf);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (node is ShaderIrOperGpr gpr)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetName(gpr);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (node is ShaderIrOperPred pred)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetName(pred);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
throw new ArgumentException(nameof(node));
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetSrcExpr(ShaderIrNode node, bool entry = false)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (node)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
case ShaderIrOperAbuf abuf: return GetName (abuf);
|
|
|
|
case ShaderIrOperCbuf cbuf: return GetName (cbuf);
|
|
|
|
case ShaderIrOperGpr gpr: return GetName (gpr);
|
|
|
|
case ShaderIrOperImm imm: return GetValue(imm);
|
|
|
|
case ShaderIrOperImmf immf: return GetValue(immf);
|
|
|
|
case ShaderIrOperPred pred: return GetName (pred);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
case ShaderIrOp op:
|
|
|
|
string expr;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_instsExpr.TryGetValue(op.Inst, out GetInstExpr getExpr))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
expr = getExpr(op);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
throw new NotImplementedException(op.Inst.ToString());
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!entry && NeedsParentheses(op))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
expr = "(" + expr + ")";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return expr;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
default: throw new ArgumentException(nameof(node));
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static bool NeedsParentheses(ShaderIrOp op)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (op.Inst)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
case ShaderIrInst.Ipa:
|
2018-04-10 19:50:32 +00:00
|
|
|
case ShaderIrInst.Texq:
|
|
|
|
case ShaderIrInst.Texs:
|
2019-02-28 01:12:24 +00:00
|
|
|
case ShaderIrInst.Tld4:
|
2018-04-10 19:50:32 +00:00
|
|
|
case ShaderIrInst.Txlf:
|
2018-04-08 19:17:35 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-14 16:08:39 +00:00
|
|
|
return true;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetName(ShaderIrOperCbuf cbuf)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!_decl.Uniforms.TryGetValue(cbuf.Index, out ShaderDeclInfo declInfo))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (cbuf.Offs != null)
|
2018-05-17 18:25:42 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string offset = "floatBitsToInt(" + GetSrcExpr(cbuf.Offs) + ")";
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string index = "(" + cbuf.Pos * 4 + " + " + offset + ")";
|
2018-06-26 05:10:54 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return $"{declInfo.Name}_data[{index} / 16][({index} / 4) % 4]";
|
2018-05-17 18:25:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return $"{declInfo.Name}_data[{cbuf.Pos / 4}][{cbuf.Pos % 4}]";
|
2018-05-17 18:25:42 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetOutAbufName(ShaderIrOperAbuf abuf)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Geometry)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (abuf.Offs)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
|
|
|
case GlslDecl.LayerAttr: return "gl_Layer";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetAttrTempName(abuf);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetName(ShaderIrOperAbuf abuf)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-06-28 02:55:08 +00:00
|
|
|
//Handle special scalar read-only attributes here.
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Vertex)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (abuf.Offs)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
|
|
|
case GlslDecl.VertexIdAttr: return "gl_VertexID";
|
2018-08-25 04:16:58 +00:00
|
|
|
case GlslDecl.InstanceIdAttr: return GlslDecl.InstanceUniformName;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (_decl.ShaderType == GalShaderType.TessEvaluation)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (abuf.Offs)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
|
|
|
case GlslDecl.TessCoordAttrX: return "gl_TessCoord.x";
|
|
|
|
case GlslDecl.TessCoordAttrY: return "gl_TessCoord.y";
|
|
|
|
case GlslDecl.TessCoordAttrZ: return "gl_TessCoord.z";
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (_decl.ShaderType == GalShaderType.Fragment)
|
2018-07-03 23:06:13 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (abuf.Offs)
|
2018-07-03 23:06:13 +00:00
|
|
|
{
|
2018-08-16 05:26:03 +00:00
|
|
|
case GlslDecl.PointCoordAttrX: return "gl_PointCoord.x";
|
|
|
|
case GlslDecl.PointCoordAttrY: return "gl_PointCoord.y";
|
2019-02-15 03:23:14 +00:00
|
|
|
case GlslDecl.FaceAttr: return "(gl_FrontFacing ? -1 : 0)";
|
2018-07-03 23:06:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetAttrTempName(abuf);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetAttrTempName(ShaderIrOperAbuf abuf)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int index = abuf.Offs >> 4;
|
|
|
|
int elem = (abuf.Offs >> 2) & 3;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string swizzle = "." + GetAttrSwizzle(elem);
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!_decl.Attributes.TryGetValue(index, out ShaderDeclInfo declInfo))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-06-28 02:55:08 +00:00
|
|
|
//Handle special vec4 attributes here
|
2018-08-16 05:26:03 +00:00
|
|
|
//(for example, index 7 is always gl_Position).
|
2019-03-04 01:45:25 +00:00
|
|
|
if (index == GlslDecl.GlPositionVec4Index)
|
2018-06-28 02:55:08 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string name =
|
|
|
|
_decl.ShaderType != GalShaderType.Vertex &&
|
|
|
|
_decl.ShaderType != GalShaderType.Geometry ? GlslDecl.PositionOutAttrName : "gl_Position";
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return name + swizzle;
|
2018-06-28 02:55:08 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (abuf.Offs == GlslDecl.PointSizeAttr)
|
2018-08-16 05:26:03 +00:00
|
|
|
{
|
|
|
|
return "gl_PointSize";
|
|
|
|
}
|
2018-08-20 01:25:26 +00:00
|
|
|
}
|
2018-06-28 02:55:08 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (declInfo.Index >= 32)
|
2018-08-20 01:25:26 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
throw new InvalidOperationException($"Shader attribute offset {abuf.Offs} is invalid.");
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Geometry)
|
2018-07-19 05:33:27 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string vertex = "floatBitsToInt(" + GetSrcExpr(abuf.Vertex) + ")";
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return declInfo.Name + "[" + vertex + "]" + swizzle;
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return declInfo.Name + swizzle;
|
2018-07-19 05:33:27 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetName(ShaderIrOperGpr gpr)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (gpr.IsConst)
|
2019-01-31 12:43:24 +00:00
|
|
|
{
|
|
|
|
return "0";
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (gpr.RegisterSize == ShaderRegisterSize.Single)
|
2019-01-31 12:43:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetNameWithSwizzle(_decl.Gprs, gpr.Index);
|
2019-01-31 12:43:24 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (gpr.RegisterSize == ShaderRegisterSize.Half)
|
2019-01-31 12:43:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetNameWithSwizzle(_decl.GprsHalf, (gpr.Index << 1) | gpr.HalfPart);
|
2019-01-31 12:43:24 +00:00
|
|
|
}
|
|
|
|
else /* if (Gpr.RegisterSize == ShaderRegisterSize.Double) */
|
|
|
|
{
|
|
|
|
throw new NotImplementedException("Double types are not supported.");
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetValue(ShaderIrOperImm imm)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
//Only use hex is the value is too big and would likely be hard to read as int.
|
2019-03-04 01:45:25 +00:00
|
|
|
if (imm.Value > 0xfff ||
|
|
|
|
imm.Value < -0xfff)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "0x" + imm.Value.ToString("x8", CultureInfo.InvariantCulture);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetIntConst(imm.Value);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetValue(ShaderIrOperImmf immf)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetFloatConst(immf.Value);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetName(ShaderIrOperPred pred)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return pred.IsConst ? "true" : GetNameWithSwizzle(_decl.Preds, pred.Index);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetNameWithSwizzle(IReadOnlyDictionary<int, ShaderDeclInfo> dict, int index)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int vecIndex = index & ~3;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (dict.TryGetValue(vecIndex, out ShaderDeclInfo declInfo))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (declInfo.Size > 1 && index < vecIndex + declInfo.Size)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return declInfo.Name + "." + GetAttrSwizzle(index & 3);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!dict.TryGetValue(index, out declInfo))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return declInfo.Name;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetAttrSwizzle(int elem)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "xyzw".Substring(elem, 1);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetAbsExpr(ShaderIrOp op) => GetUnaryCall(op, "abs");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetAddExpr(ShaderIrOp op) => GetBinaryExpr(op, "+");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetAndExpr(ShaderIrOp op) => GetBinaryExpr(op, "&");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetAsrExpr(ShaderIrOp op) => GetBinaryExpr(op, ">>");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetBandExpr(ShaderIrOp op) => GetBinaryExpr(op, "&&");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetBnotExpr(ShaderIrOp op) => GetUnaryExpr(op, "!");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetBorExpr(ShaderIrOp op) => GetBinaryExpr(op, "||");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetBxorExpr(ShaderIrOp op) => GetBinaryExpr(op, "^^");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCeilExpr(ShaderIrOp op) => GetUnaryCall(op, "ceil");
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetClampsExpr(ShaderIrOp op)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "clamp(" + GetOperExpr(op, op.OperandA) + ", " +
|
|
|
|
GetOperExpr(op, op.OperandB) + ", " +
|
|
|
|
GetOperExpr(op, op.OperandC) + ")";
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetClampuExpr(ShaderIrOp op)
|
2018-04-26 02:11:26 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "int(clamp(uint(" + GetOperExpr(op, op.OperandA) + "), " +
|
|
|
|
"uint(" + GetOperExpr(op, op.OperandB) + "), " +
|
|
|
|
"uint(" + GetOperExpr(op, op.OperandC) + ")))";
|
2018-04-26 02:11:26 +00:00
|
|
|
}
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCeqExpr(ShaderIrOp op) => GetBinaryExpr(op, "==");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCequExpr(ShaderIrOp op) => GetBinaryExprWithNaN(op, "==");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCgeExpr(ShaderIrOp op) => GetBinaryExpr(op, ">=");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCgeuExpr(ShaderIrOp op) => GetBinaryExprWithNaN(op, ">=");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCgtExpr(ShaderIrOp op) => GetBinaryExpr(op, ">");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCgtuExpr(ShaderIrOp op) => GetBinaryExprWithNaN(op, ">");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCleExpr(ShaderIrOp op) => GetBinaryExpr(op, "<=");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCleuExpr(ShaderIrOp op) => GetBinaryExprWithNaN(op, "<=");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCltExpr(ShaderIrOp op) => GetBinaryExpr(op, "<");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCltuExpr(ShaderIrOp op) => GetBinaryExprWithNaN(op, "<");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCnanExpr(ShaderIrOp op) => GetUnaryCall(op, "isnan");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCneExpr(ShaderIrOp op) => GetBinaryExpr(op, "!=");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCutExpr(ShaderIrOp op) => "EndPrimitive()";
|
2018-07-19 05:33:27 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCneuExpr(ShaderIrOp op) => GetBinaryExprWithNaN(op, "!=");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetCnumExpr(ShaderIrOp op) => GetUnaryCall(op, "!isnan");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetExitExpr(ShaderIrOp op) => "return 0u";
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFcosExpr(ShaderIrOp op) => GetUnaryCall(op, "cos");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFex2Expr(ShaderIrOp op) => GetUnaryCall(op, "exp2");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFfmaExpr(ShaderIrOp op) => GetTernaryExpr(op, "*", "+");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFclampExpr(ShaderIrOp op) => GetTernaryCall(op, "clamp");
|
2018-04-26 02:11:26 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFlg2Expr(ShaderIrOp op) => GetUnaryCall(op, "log2");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFloorExpr(ShaderIrOp op) => GetUnaryCall(op, "floor");
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFrcpExpr(ShaderIrOp op) => GetUnaryExpr(op, "1 / ");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFrsqExpr(ShaderIrOp op) => GetUnaryCall(op, "inversesqrt");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFsinExpr(ShaderIrOp op) => GetUnaryCall(op, "sin");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFsqrtExpr(ShaderIrOp op) => GetUnaryCall(op, "sqrt");
|
2018-06-23 05:00:44 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFtosExpr(ShaderIrOp op)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "int(" + GetOperExpr(op, op.OperandA) + ")";
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetFtouExpr(ShaderIrOp op)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "int(uint(" + GetOperExpr(op, op.OperandA) + "))";
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetIpaExpr(ShaderIrOp op)
|
2018-09-01 21:44:19 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaIpa meta = (ShaderIrMetaIpa)op.MetaData;
|
2018-09-01 21:44:19 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrOperAbuf abuf = (ShaderIrOperAbuf)op.OperandA;
|
2018-09-01 21:44:19 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (meta.Mode == ShaderIpaMode.Pass)
|
2018-09-01 21:44:19 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
int index = abuf.Offs >> 4;
|
|
|
|
int elem = (abuf.Offs >> 2) & 3;
|
2018-09-01 21:44:19 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (_decl.ShaderType == GalShaderType.Fragment && index == GlslDecl.GlPositionVec4Index)
|
2018-09-01 21:44:19 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (elem)
|
2018-09-01 21:44:19 +00:00
|
|
|
{
|
|
|
|
case 0: return "gl_FragCoord.x";
|
|
|
|
case 1: return "gl_FragCoord.y";
|
|
|
|
case 2: return "gl_FragCoord.z";
|
|
|
|
case 3: return "1";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetSrcExpr(op.OperandA);
|
2018-09-01 21:44:19 +00:00
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetKilExpr(ShaderIrOp op) => "discard";
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetLslExpr(ShaderIrOp op) => GetBinaryExpr(op, "<<");
|
|
|
|
private string GetLsrExpr(ShaderIrOp op)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "int(uint(" + GetOperExpr(op, op.OperandA) + ") >> " +
|
|
|
|
GetOperExpr(op, op.OperandB) + ")";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetMaxExpr(ShaderIrOp op) => GetBinaryCall(op, "max");
|
|
|
|
private string GetMinExpr(ShaderIrOp op) => GetBinaryCall(op, "min");
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetMulExpr(ShaderIrOp op) => GetBinaryExpr(op, "*");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetNegExpr(ShaderIrOp op) => GetUnaryExpr(op, "-");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetNotExpr(ShaderIrOp op) => GetUnaryExpr(op, "~");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetOrExpr(ShaderIrOp op) => GetBinaryExpr(op, "|");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetStofExpr(ShaderIrOp op)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "float(" + GetOperExpr(op, op.OperandA) + ")";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetSubExpr(ShaderIrOp op) => GetBinaryExpr(op, "-");
|
2018-05-17 18:25:42 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTexbExpr(ShaderIrOp op)
|
2018-08-13 21:22:09 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2018-08-13 21:22:09 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!_decl.CbTextures.TryGetValue(op, out ShaderDeclInfo declInfo))
|
2018-08-13 21:22:09 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string coords = GetTexSamplerCoords(op);
|
2018-08-13 21:22:09 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string ch = "rgba".Substring(meta.Elem, 1);
|
2018-08-13 21:22:09 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetTextureOperation(op, declInfo.Name, coords, ch);
|
2018-08-13 21:22:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTexqExpr(ShaderIrOp op)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTexq meta = (ShaderIrMetaTexq)op.MetaData;
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string ch = "xyzw".Substring(meta.Elem, 1);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (meta.Info == ShaderTexqInfo.Dimension)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string sampler = GetTexSamplerName(op);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string lod = GetOperExpr(op, op.OperandA); //???
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureSize(" + sampler + ", " + lod + ")." + ch;
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
throw new NotImplementedException(meta.Info.ToString());
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTexsExpr(ShaderIrOp op)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string sampler = GetTexSamplerName(op);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string coords = GetTexSamplerCoords(op);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string ch = "rgba".Substring(meta.Elem, 1);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetTextureOperation(op, sampler, coords, ch);
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTld4Expr(ShaderIrOp op)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string sampler = GetTexSamplerName(op);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string coords = GetTexSamplerCoords(op);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string ch = "rgba".Substring(meta.Elem, 1);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetTextureGatherOperation(op, sampler, coords, ch);
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 01:12:24 +00:00
|
|
|
// TODO: support AOFFI on non nvidia drivers
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTxlfExpr(ShaderIrOp op)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-02-28 01:12:24 +00:00
|
|
|
// TODO: Support all suffixes
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
TextureInstructionSuffix suffix = meta.TextureInstructionSuffix;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string sampler = GetTexSamplerName(op);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string coords = GetITexSamplerCoords(op);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string ch = "rgba".Substring(meta.Elem, 1);
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string lod = "0";
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (meta.LevelOfDetail != null)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
lod = GetOperExpr(op, meta.LevelOfDetail);
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.AOffI) != 0 && _isNvidiaDriver)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string offset = GetTextureOffset(meta, GetOperExpr(op, meta.Offset));
|
|
|
|
return "texelFetchOffset(" + sampler + ", " + coords + ", " + lod + ", " + offset + ")." + ch;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "texelFetch(" + sampler + ", " + coords + ", " + lod + ")." + ch;
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTruncExpr(ShaderIrOp op) => GetUnaryCall(op, "trunc");
|
2018-04-10 19:50:32 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetUtofExpr(ShaderIrOp op)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "float(uint(" + GetOperExpr(op, op.OperandA) + "))";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetXorExpr(ShaderIrOp op) => GetBinaryExpr(op, "^");
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetUnaryCall(ShaderIrOp op, string funcName)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return funcName + "(" + GetOperExpr(op, op.OperandA) + ")";
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetBinaryCall(ShaderIrOp op, string funcName)
|
2018-05-17 18:25:42 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return funcName + "(" + GetOperExpr(op, op.OperandA) + ", " +
|
|
|
|
GetOperExpr(op, op.OperandB) + ")";
|
2018-05-17 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTernaryCall(ShaderIrOp op, string funcName)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return funcName + "(" + GetOperExpr(op, op.OperandA) + ", " +
|
|
|
|
GetOperExpr(op, op.OperandB) + ", " +
|
|
|
|
GetOperExpr(op, op.OperandC) + ")";
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetUnaryExpr(ShaderIrOp op, string opr)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return opr + GetOperExpr(op, op.OperandA);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetBinaryExpr(ShaderIrOp op, string opr)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetOperExpr(op, op.OperandA) + " " + opr + " " +
|
|
|
|
GetOperExpr(op, op.OperandB);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetBinaryExprWithNaN(ShaderIrOp op, string opr)
|
2018-05-29 23:37:10 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string a = GetOperExpr(op, op.OperandA);
|
|
|
|
string b = GetOperExpr(op, op.OperandB);
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string nanCheck =
|
|
|
|
" || isnan(" + a + ")" +
|
|
|
|
" || isnan(" + b + ")";
|
2018-05-29 23:37:10 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return a + " " + opr + " " + b + nanCheck;
|
2018-05-29 23:37:10 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTernaryExpr(ShaderIrOp op, string opr1, string opr2)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetOperExpr(op, op.OperandA) + " " + opr1 + " " +
|
|
|
|
GetOperExpr(op, op.OperandB) + " " + opr2 + " " +
|
|
|
|
GetOperExpr(op, op.OperandC);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTexSamplerName(ShaderIrOp op)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrOperImm node = (ShaderIrOperImm)op.OperandC;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
int handle = ((ShaderIrOperImm)op.OperandC).Value;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (!_decl.Textures.TryGetValue(handle, out ShaderDeclInfo declInfo))
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return declInfo.Name;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTexSamplerCoords(ShaderIrOp op)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
bool hasDepth = (meta.TextureInstructionSuffix & TextureInstructionSuffix.Dc) != 0;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
int coords = ImageUtils.GetCoordsCountTextureTarget(meta.TextureTarget);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
bool isArray = ImageUtils.IsArray(meta.TextureTarget);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string GetLastArgument(ShaderIrNode node)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string result = GetOperExpr(op, node);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
|
|
|
// array index is actually an integer so we need to pass it correctly
|
2019-03-04 01:45:25 +00:00
|
|
|
if (isArray)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
result = "float(floatBitsToInt(" + result + "))";
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return result;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string lastArgument;
|
|
|
|
string depthArgument = "";
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
int vecSize = coords;
|
|
|
|
if (hasDepth && op.Inst != ShaderIrInst.Tld4)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
vecSize++;
|
|
|
|
depthArgument = $", {GetOperExpr(op, meta.DepthCompare)}";
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (coords)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
|
|
|
case 1:
|
2019-03-04 01:45:25 +00:00
|
|
|
if (hasDepth)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return $"vec3({GetOperExpr(op, meta.Coordinates[0])}, 0.0{depthArgument})";
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetOperExpr(op, meta.Coordinates[0]);
|
2019-02-28 01:12:24 +00:00
|
|
|
case 2:
|
2019-03-04 01:45:25 +00:00
|
|
|
lastArgument = GetLastArgument(meta.Coordinates[1]);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return $"vec{vecSize}({GetOperExpr(op, meta.Coordinates[0])}, {lastArgument}{depthArgument})";
|
2019-02-28 01:12:24 +00:00
|
|
|
case 3:
|
2019-03-04 01:45:25 +00:00
|
|
|
lastArgument = GetLastArgument(meta.Coordinates[2]);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return $"vec{vecSize}({GetOperExpr(op, meta.Coordinates[0])}, {GetOperExpr(op, meta.Coordinates[1])}, {lastArgument}{depthArgument})";
|
2019-02-28 01:12:24 +00:00
|
|
|
case 4:
|
2019-03-04 01:45:25 +00:00
|
|
|
lastArgument = GetLastArgument(meta.Coordinates[3]);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return $"vec4({GetOperExpr(op, meta.Coordinates[0])}, {GetOperExpr(op, meta.Coordinates[1])}, {GetOperExpr(op, meta.Coordinates[2])}, {lastArgument}){depthArgument}";
|
2019-02-28 01:12:24 +00:00
|
|
|
default:
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTextureOffset(ShaderIrMetaTex meta, string oper, int shift = 4, int mask = 0xF)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string GetOffset(string operation, int index)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return $"({operation} >> {index * shift}) & 0x{mask:x}";
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
int coords = ImageUtils.GetCoordsCountTextureTarget(meta.TextureTarget);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (ImageUtils.IsArray(meta.TextureTarget))
|
|
|
|
coords -= 1;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (coords)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
|
|
|
case 1:
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetOffset(oper, 0);
|
2019-02-28 01:12:24 +00:00
|
|
|
case 2:
|
2019-03-04 01:45:25 +00:00
|
|
|
return "ivec2(" + GetOffset(oper, 0) + ", " + GetOffset(oper, 1) + ")";
|
2019-02-28 01:12:24 +00:00
|
|
|
case 3:
|
2019-03-04 01:45:25 +00:00
|
|
|
return "ivec3(" + GetOffset(oper, 0) + ", " + GetOffset(oper, 1) + ", " + GetOffset(oper, 2) + ")";
|
2019-02-28 01:12:24 +00:00
|
|
|
case 4:
|
2019-03-04 01:45:25 +00:00
|
|
|
return "ivec4(" + GetOffset(oper, 0) + ", " + GetOffset(oper, 1) + ", " + GetOffset(oper, 2) + ", " + GetOffset(oper, 3) + ")";
|
2019-02-28 01:12:24 +00:00
|
|
|
default:
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support AOFFI on non nvidia drivers
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTextureGatherOperation(ShaderIrOp op, string sampler, string coords, string ch)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
TextureInstructionSuffix suffix = meta.TextureInstructionSuffix;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string chString = "." + ch;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string comp = meta.Component.ToString();
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.Dc) != 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
comp = GetOperExpr(op, meta.DepthCompare);
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.AOffI) != 0 && _isNvidiaDriver)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string offset = GetTextureOffset(meta, "floatBitsToInt((" + GetOperExpr(op, meta.Offset) + "))", 8, 0x3F);
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.Dc) != 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureGatherOffset(" + sampler + ", " + coords + ", " + comp + ", " + offset + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureGatherOffset(" + sampler + ", " + coords + ", " + offset + ", " + comp + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
// TODO: Support PTP
|
2019-03-04 01:45:25 +00:00
|
|
|
else if ((suffix & TextureInstructionSuffix.Ptp) != 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureGather(" + sampler + ", " + coords + ", " + comp + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: support AOFFI on non nvidia drivers
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetTextureOperation(ShaderIrOp op, string sampler, string coords, string ch)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
TextureInstructionSuffix suffix = meta.TextureInstructionSuffix;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
string chString = "." + ch;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.Dc) != 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
chString = "";
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Support LBA and LLA
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.Lz) != 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.AOffI) != 0 && _isNvidiaDriver)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string offset = GetTextureOffset(meta, "floatBitsToInt((" + GetOperExpr(op, meta.Offset) + "))");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureLodOffset(" + sampler + ", " + coords + ", 0.0, " + offset + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureLod(" + sampler + ", " + coords + ", 0.0)" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if ((suffix & TextureInstructionSuffix.Lb) != 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.AOffI) != 0 && _isNvidiaDriver)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string offset = GetTextureOffset(meta, "floatBitsToInt((" + GetOperExpr(op, meta.Offset) + "))");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureOffset(" + sampler + ", " + coords + ", " + offset + ", " + GetOperExpr(op, meta.LevelOfDetail) + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "texture(" + sampler + ", " + coords + ", " + GetOperExpr(op, meta.LevelOfDetail) + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if ((suffix & TextureInstructionSuffix.Ll) != 0)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if ((suffix & TextureInstructionSuffix.AOffI) != 0 && _isNvidiaDriver)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string offset = GetTextureOffset(meta, "floatBitsToInt((" + GetOperExpr(op, meta.Offset) + "))");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureLodOffset(" + sampler + ", " + coords + ", " + GetOperExpr(op, meta.LevelOfDetail) + ", " + offset + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureLod(" + sampler + ", " + coords + ", " + GetOperExpr(op, meta.LevelOfDetail) + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if ((suffix & TextureInstructionSuffix.AOffI) != 0 && _isNvidiaDriver)
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string offset = GetTextureOffset(meta, "floatBitsToInt((" + GetOperExpr(op, meta.Offset) + "))");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return "textureOffset(" + sampler + ", " + coords + ", " + offset + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "texture(" + sampler + ", " + coords + ")" + chString;
|
2019-02-28 01:12:24 +00:00
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
throw new NotImplementedException($"Texture Suffix {meta.TextureInstructionSuffix} is not implemented");
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetITexSamplerCoords(ShaderIrOp op)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
ShaderIrMetaTex meta = (ShaderIrMetaTex)op.MetaData;
|
2019-02-28 01:12:24 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (ImageUtils.GetCoordsCountTextureTarget(meta.TextureTarget))
|
2019-02-28 01:12:24 +00:00
|
|
|
{
|
|
|
|
case 1:
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetOperExpr(op, meta.Coordinates[0]);
|
2019-02-28 01:12:24 +00:00
|
|
|
case 2:
|
2019-03-04 01:45:25 +00:00
|
|
|
return "ivec2(" + GetOperExpr(op, meta.Coordinates[0]) + ", " + GetOperExpr(op, meta.Coordinates[1]) + ")";
|
2019-02-28 01:12:24 +00:00
|
|
|
case 3:
|
2019-03-04 01:45:25 +00:00
|
|
|
return "ivec3(" + GetOperExpr(op, meta.Coordinates[0]) + ", " + GetOperExpr(op, meta.Coordinates[1]) + ", " + GetOperExpr(op, meta.Coordinates[2]) + ")";
|
2019-02-28 01:12:24 +00:00
|
|
|
default:
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
2018-04-10 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private string GetOperExpr(ShaderIrOp op, ShaderIrNode oper)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetExprWithCast(op, oper, GetSrcExpr(oper));
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static string GetExprWithCast(ShaderIrNode dst, ShaderIrNode src, string expr)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
//Note: The "DstType" (of the cast) is the type that the operation
|
|
|
|
//uses on the source operands, while the "SrcType" is the destination
|
|
|
|
//type of the operand result (if it is a operation) or just the type
|
|
|
|
//of the variable for registers/uniforms/attributes.
|
2019-03-04 01:45:25 +00:00
|
|
|
OperType dstType = GetSrcNodeType(dst);
|
|
|
|
OperType srcType = GetDstNodeType(src);
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
if (dstType != srcType)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
//Check for invalid casts
|
|
|
|
//(like bool to int/float and others).
|
2019-03-04 01:45:25 +00:00
|
|
|
if (srcType != OperType.F32 &&
|
|
|
|
srcType != OperType.I32)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (src)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
case ShaderIrOperGpr gpr:
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
|
|
|
//When the Gpr is ZR, just return the 0 value directly,
|
|
|
|
//since the float encoding for 0 is 0.
|
2019-03-04 01:45:25 +00:00
|
|
|
if (gpr.IsConst)
|
2018-04-10 19:50:32 +00:00
|
|
|
{
|
|
|
|
return "0";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (dstType)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
case OperType.F32: expr = "intBitsToFloat(" + expr + ")"; break;
|
|
|
|
case OperType.I32: expr = "floatBitsToInt(" + expr + ")"; break;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return expr;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static string GetIntConst(int value)
|
2018-07-14 16:08:39 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string expr = value.ToString(CultureInfo.InvariantCulture);
|
2018-07-14 16:08:39 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return value < 0 ? "(" + expr + ")" : expr;
|
2018-07-14 16:08:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static string GetFloatConst(float value)
|
2018-07-14 16:08:39 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
string expr = value.ToString(CultureInfo.InvariantCulture);
|
2018-07-14 16:08:39 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return value < 0 ? "(" + expr + ")" : expr;
|
2018-07-14 16:08:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static OperType GetDstNodeType(ShaderIrNode node)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-04-10 19:50:32 +00:00
|
|
|
//Special case instructions with the result type different
|
|
|
|
//from the input types (like integer <-> float conversion) here.
|
2019-03-04 01:45:25 +00:00
|
|
|
if (node is ShaderIrOp op)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (op.Inst)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2018-04-10 19:50:32 +00:00
|
|
|
case ShaderIrInst.Stof:
|
|
|
|
case ShaderIrInst.Txlf:
|
|
|
|
case ShaderIrInst.Utof:
|
|
|
|
return OperType.F32;
|
|
|
|
|
|
|
|
case ShaderIrInst.Ftos:
|
|
|
|
case ShaderIrInst.Ftou:
|
|
|
|
return OperType.I32;
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
return GetSrcNodeType(node);
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static OperType GetSrcNodeType(ShaderIrNode node)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
switch (node)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
case ShaderIrOperAbuf abuf:
|
|
|
|
return abuf.Offs == GlslDecl.LayerAttr ||
|
|
|
|
abuf.Offs == GlslDecl.InstanceIdAttr ||
|
|
|
|
abuf.Offs == GlslDecl.VertexIdAttr ||
|
|
|
|
abuf.Offs == GlslDecl.FaceAttr
|
2018-04-08 19:17:35 +00:00
|
|
|
? OperType.I32
|
|
|
|
: OperType.F32;
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
case ShaderIrOperCbuf cbuf: return OperType.F32;
|
|
|
|
case ShaderIrOperGpr gpr: return OperType.F32;
|
|
|
|
case ShaderIrOperImm imm: return OperType.I32;
|
|
|
|
case ShaderIrOperImmf immf: return OperType.F32;
|
|
|
|
case ShaderIrOperPred pred: return OperType.Bool;
|
2018-04-08 19:17:35 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
case ShaderIrOp op:
|
|
|
|
if (op.Inst > ShaderIrInst.B_Start &&
|
|
|
|
op.Inst < ShaderIrInst.B_End)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
return OperType.Bool;
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (op.Inst > ShaderIrInst.F_Start &&
|
|
|
|
op.Inst < ShaderIrInst.F_End)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
return OperType.F32;
|
|
|
|
}
|
2019-03-04 01:45:25 +00:00
|
|
|
else if (op.Inst > ShaderIrInst.I_Start &&
|
|
|
|
op.Inst < ShaderIrInst.I_End)
|
2018-04-08 19:17:35 +00:00
|
|
|
{
|
|
|
|
return OperType.I32;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
throw new ArgumentException(nameof(node));
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2018-08-31 16:14:04 +00:00
|
|
|
|
2019-03-04 01:45:25 +00:00
|
|
|
private static string GetBlockPosition(ShaderIrBlock block)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
if (block != null)
|
2018-08-31 16:14:04 +00:00
|
|
|
{
|
2019-03-04 01:45:25 +00:00
|
|
|
return "0x" + block.Position.ToString("x8") + "u";
|
2018-08-31 16:14:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "0u";
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 19:17:35 +00:00
|
|
|
}
|
2018-08-07 01:26:19 +00:00
|
|
|
}
|