2019-10-31 04:29:22 +01:00
|
|
|
using Ryujinx.Common;
|
2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
|
|
|
|
using Ryujinx.Graphics.Shader.StructuredIr;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
|
|
|
|
{
|
|
|
|
static class Declarations
|
|
|
|
{
|
|
|
|
// At least 16 attributes are guaranteed by the spec.
|
|
|
|
public const int MaxAttributes = 16;
|
|
|
|
|
|
|
|
public static void Declare(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
2020-07-15 05:01:10 +02:00
|
|
|
context.AppendLine("#version 440 core");
|
2019-11-08 21:29:41 +01:00
|
|
|
context.AppendLine("#extension GL_ARB_gpu_shader_int64 : enable");
|
2019-10-31 04:29:22 +01:00
|
|
|
context.AppendLine("#extension GL_ARB_shader_ballot : enable");
|
2019-11-08 21:29:41 +01:00
|
|
|
context.AppendLine("#extension GL_ARB_shader_group_vote : enable");
|
2020-05-27 11:00:21 +02:00
|
|
|
context.AppendLine("#extension GL_EXT_shader_image_load_formatted : enable");
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (context.Config.Stage == ShaderStage.Compute)
|
|
|
|
{
|
|
|
|
context.AppendLine("#extension GL_ARB_compute_shader : enable");
|
|
|
|
}
|
|
|
|
|
2019-12-07 05:54:28 +01:00
|
|
|
context.AppendLine("#pragma optionNV(fastmath off)");
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
context.AppendLine();
|
|
|
|
|
|
|
|
context.AppendLine($"const int {DefaultNames.UndefinedName} = 0;");
|
|
|
|
context.AppendLine();
|
|
|
|
|
|
|
|
if (context.Config.Stage == ShaderStage.Geometry)
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
string inPrimitive = context.Config.GpuAccessor.QueryPrimitiveTopology().ToGlslString();
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
context.AppendLine($"layout ({inPrimitive}) in;");
|
|
|
|
|
2019-12-16 05:59:46 +01:00
|
|
|
string outPrimitive = context.Config.OutputTopology.ToGlslString();
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
int maxOutputVertices = context.Config.MaxOutputVertices;
|
|
|
|
|
|
|
|
context.AppendLine($"layout ({outPrimitive}, max_vertices = {maxOutputVertices}) out;");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2020-02-02 04:25:52 +01:00
|
|
|
if (context.Config.Stage == ShaderStage.Compute)
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
int localMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeLocalMemorySize(), 4);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-02-02 04:25:52 +01:00
|
|
|
if (localMemorySize != 0)
|
|
|
|
{
|
|
|
|
string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-02-02 04:25:52 +01:00
|
|
|
context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-05-06 03:02:28 +02:00
|
|
|
int sharedMemorySize = BitUtils.DivRoundUp(context.Config.GpuAccessor.QueryComputeSharedMemorySize(), 4);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-02-02 04:25:52 +01:00
|
|
|
if (sharedMemorySize != 0)
|
|
|
|
{
|
|
|
|
string sharedMemorySizeStr = NumberFormatter.FormatInt(sharedMemorySize);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-02-02 04:25:52 +01:00
|
|
|
context.AppendLine($"shared uint {DefaultNames.SharedMemoryName}[{sharedMemorySizeStr}];");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (context.Config.LocalMemorySize != 0)
|
2019-11-08 21:29:41 +01:00
|
|
|
{
|
2020-02-02 04:25:52 +01:00
|
|
|
int localMemorySize = BitUtils.DivRoundUp(context.Config.LocalMemorySize, 4);
|
|
|
|
|
|
|
|
string localMemorySizeStr = NumberFormatter.FormatInt(localMemorySize);
|
2019-12-09 03:55:22 +01:00
|
|
|
|
2020-02-02 04:25:52 +01:00
|
|
|
context.AppendLine($"uint {DefaultNames.LocalMemoryName}[{localMemorySizeStr}];");
|
2019-11-08 21:29:41 +01:00
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
if (info.CBuffers.Count != 0)
|
|
|
|
{
|
|
|
|
DeclareUniforms(context, info);
|
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.SBuffers.Count != 0)
|
|
|
|
{
|
2019-12-01 03:53:09 +01:00
|
|
|
DeclareStorages(context, info);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.Samplers.Count != 0)
|
|
|
|
{
|
|
|
|
DeclareSamplers(context, info);
|
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
if (info.Images.Count != 0)
|
|
|
|
{
|
|
|
|
DeclareImages(context, info);
|
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
if (context.Config.Stage != ShaderStage.Compute)
|
|
|
|
{
|
|
|
|
if (info.IAttributes.Count != 0)
|
|
|
|
{
|
|
|
|
DeclareInputAttributes(context, info);
|
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info.OAttributes.Count != 0 || context.Config.Stage != ShaderStage.Fragment)
|
|
|
|
{
|
|
|
|
DeclareOutputAttributes(context, info);
|
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-06 03:02:28 +02:00
|
|
|
string localSizeX = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeX());
|
|
|
|
string localSizeY = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeY());
|
|
|
|
string localSizeZ = NumberFormatter.FormatInt(context.Config.GpuAccessor.QueryComputeLocalSizeZ());
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
context.AppendLine(
|
2019-12-16 05:59:46 +01:00
|
|
|
"layout (" +
|
2019-10-13 08:02:07 +02:00
|
|
|
$"local_size_x = {localSizeX}, " +
|
|
|
|
$"local_size_y = {localSizeY}, " +
|
|
|
|
$"local_size_z = {localSizeZ}) in;");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
2019-10-31 04:29:22 +01:00
|
|
|
|
2020-07-07 04:41:07 +02:00
|
|
|
if (context.Config.Stage == ShaderStage.Fragment || context.Config.Stage == ShaderStage.Compute)
|
|
|
|
{
|
2020-07-26 05:03:40 +02:00
|
|
|
if (context.Config.Stage == ShaderStage.Fragment)
|
|
|
|
{
|
2020-12-02 00:13:27 +01:00
|
|
|
if (context.Config.GpuAccessor.QueryEarlyZForce())
|
|
|
|
{
|
|
|
|
context.AppendLine("layout(early_fragment_tests) in;");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2020-07-26 05:03:40 +02:00
|
|
|
context.AppendLine($"uniform bool {DefaultNames.IsBgraName}[8];");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2020-07-07 04:41:07 +02:00
|
|
|
if (DeclareRenderScale(context))
|
|
|
|
{
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 23:37:00 +01:00
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighS32) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighS32.glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.MultiplyHighU32) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/MultiplyHighU32.glsl");
|
|
|
|
}
|
|
|
|
|
2019-10-31 04:29:22 +01:00
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.Shuffle) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/Shuffle.glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleDown) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleDown.glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleUp) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleUp.glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.ShuffleXor) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/ShuffleXor.glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.SwizzleAdd) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/SwizzleAdd.glsl");
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-10-25 21:00:44 +01:00
|
|
|
public static void DeclareLocals(CodeGenContext context, StructuredFunction function)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-10-25 21:00:44 +01:00
|
|
|
foreach (AstOperand decl in function.Locals)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
string name = context.OperandManager.DeclareLocal(decl);
|
|
|
|
|
|
|
|
context.AppendLine(GetVarTypeName(decl.VarType) + " " + name + ";");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-25 21:00:44 +01:00
|
|
|
public static string GetVarTypeName(VariableType type)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case VariableType.Bool: return "bool";
|
|
|
|
case VariableType.F32: return "precise float";
|
2020-03-03 15:02:08 +01:00
|
|
|
case VariableType.F64: return "double";
|
2020-10-25 21:00:44 +01:00
|
|
|
case VariableType.None: return "void";
|
2019-10-13 08:02:07 +02:00
|
|
|
case VariableType.S32: return "int";
|
|
|
|
case VariableType.U32: return "uint";
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new ArgumentException($"Invalid variable type \"{type}\".");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareUniforms(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
2020-10-13 02:40:50 +02:00
|
|
|
string ubSize = "[" + NumberFormatter.FormatInt(Constants.ConstantBufferSize / 16) + "]";
|
|
|
|
|
|
|
|
if (info.UsesCbIndexing)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
int count = info.CBuffers.Max() + 1;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
int[] bindings = new int[count];
|
2020-10-13 02:40:50 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
bindings[i] = context.Config.Counts.IncrementUniformBuffersCount();
|
|
|
|
}
|
2020-10-13 02:40:50 +02:00
|
|
|
|
|
|
|
foreach (int cbufSlot in info.CBuffers.OrderBy(x => x))
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
context.CBufferDescriptors.Add(new BufferDescriptor(bindings[cbufSlot], cbufSlot));
|
2020-10-13 02:40:50 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
ubName += "_" + DefaultNames.UniformNamePrefix;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
string blockName = $"{ubName}_{DefaultNames.BlockSuffix}";
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
context.AppendLine($"layout (binding = {bindings[0]}, std140) uniform {blockName}");
|
|
|
|
context.EnterScope();
|
|
|
|
context.AppendLine("vec4 " + DefaultNames.DataName + ubSize + ";");
|
|
|
|
context.LeaveScope($" {ubName}[{NumberFormatter.FormatInt(count)}];");
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2020-10-13 02:40:50 +02:00
|
|
|
else
|
2020-07-07 04:41:07 +02:00
|
|
|
{
|
2020-10-13 02:40:50 +02:00
|
|
|
foreach (int cbufSlot in info.CBuffers.OrderBy(x => x))
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
int binding = context.Config.Counts.IncrementUniformBuffersCount();
|
2020-07-07 04:41:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
context.CBufferDescriptors.Add(new BufferDescriptor(binding, cbufSlot));
|
2020-07-15 05:01:10 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
string ubName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
|
2020-07-07 04:41:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
ubName += "_" + DefaultNames.UniformNamePrefix + cbufSlot;
|
2020-07-07 04:41:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
context.AppendLine($"layout (binding = {binding}, std140) uniform {ubName}");
|
2020-10-13 02:40:50 +02:00
|
|
|
context.EnterScope();
|
|
|
|
context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, cbufSlot, false) + ubSize + ";");
|
|
|
|
context.LeaveScope(";");
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 04:41:07 +02:00
|
|
|
}
|
|
|
|
|
2019-12-01 03:53:09 +01:00
|
|
|
private static void DeclareStorages(CodeGenContext context, StructuredProgramInfo info)
|
2019-11-08 21:29:41 +01:00
|
|
|
{
|
|
|
|
string sbName = OperandManager.GetShaderStagePrefix(context.Config.Stage);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-08 21:29:41 +01:00
|
|
|
sbName += "_" + DefaultNames.StorageNamePrefix;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-08 21:29:41 +01:00
|
|
|
string blockName = $"{sbName}_{DefaultNames.BlockSuffix}";
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
int count = info.SBuffers.Max() + 1;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
int[] bindings = new int[count];
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
for (int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
bindings[i] = context.Config.Counts.IncrementStorageBuffersCount();
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2019-11-08 21:29:41 +01:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
foreach (int sbufSlot in info.SBuffers)
|
|
|
|
{
|
|
|
|
context.SBufferDescriptors.Add(new BufferDescriptor(bindings[sbufSlot], sbufSlot));
|
|
|
|
}
|
2019-11-08 21:29:41 +01:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
context.AppendLine($"layout (binding = {bindings[0]}, std430) buffer {blockName}");
|
2019-11-08 21:29:41 +01:00
|
|
|
context.EnterScope();
|
|
|
|
context.AppendLine("uint " + DefaultNames.DataName + "[];");
|
2020-11-08 12:10:00 +01:00
|
|
|
context.LeaveScope($" {sbName}[{NumberFormatter.FormatInt(count)}];");
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareSamplers(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
HashSet<string> samplers = new HashSet<string>();
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-03-15 01:24:45 +01:00
|
|
|
// Texture instructions other than TextureSample (like TextureSize)
|
|
|
|
// may have incomplete sampler type information. In those cases,
|
|
|
|
// we prefer instead the more accurate information from the
|
|
|
|
// TextureSample instruction, if both are available.
|
|
|
|
foreach (AstTextureOperation texOp in info.Samplers.OrderBy(x => x.Handle * 2 + (x.Inst == Instruction.TextureSample ? 0 : 1)))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2019-11-03 03:07:21 +01:00
|
|
|
string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize);
|
|
|
|
|
|
|
|
string samplerName = OperandManager.GetSamplerName(context.Config.Stage, texOp, indexExpr);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-09 23:35:04 +01:00
|
|
|
if ((texOp.Flags & TextureFlags.Bindless) != 0 || !samplers.Add(samplerName))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
int firstBinding = -1;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-11-09 23:35:04 +01:00
|
|
|
if ((texOp.Type & SamplerType.Indexed) != 0)
|
2019-11-03 03:07:21 +01:00
|
|
|
{
|
|
|
|
for (int index = 0; index < texOp.ArraySize; index++)
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
int binding = context.Config.Counts.IncrementTexturesCount();
|
2019-11-03 03:07:21 +01:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
if (firstBinding < 0)
|
|
|
|
{
|
|
|
|
firstBinding = binding;
|
|
|
|
}
|
2019-11-03 03:07:21 +01:00
|
|
|
|
2020-11-09 23:35:04 +01:00
|
|
|
var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle + index * 2);
|
2019-11-03 03:07:21 +01:00
|
|
|
|
|
|
|
context.TextureDescriptors.Add(desc);
|
|
|
|
}
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
else
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
firstBinding = context.Config.Counts.IncrementTexturesCount();
|
|
|
|
|
2020-11-09 23:35:04 +01:00
|
|
|
var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2019-11-03 03:07:21 +01:00
|
|
|
context.TextureDescriptors.Add(desc);
|
|
|
|
}
|
2020-11-08 12:10:00 +01:00
|
|
|
|
|
|
|
string samplerTypeName = texOp.Type.ToGlslSamplerType();
|
|
|
|
|
|
|
|
context.AppendLine($"layout (binding = {firstBinding}) uniform {samplerTypeName} {samplerName};");
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-18 04:41:18 +02:00
|
|
|
private static void DeclareImages(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
HashSet<string> images = new HashSet<string>();
|
2019-10-18 04:41:18 +02:00
|
|
|
|
|
|
|
foreach (AstTextureOperation texOp in info.Images.OrderBy(x => x.Handle))
|
|
|
|
{
|
2019-11-03 03:07:21 +01:00
|
|
|
string indexExpr = NumberFormatter.FormatInt(texOp.ArraySize);
|
|
|
|
|
|
|
|
string imageName = OperandManager.GetImageName(context.Config.Stage, texOp, indexExpr);
|
2019-10-18 04:41:18 +02:00
|
|
|
|
2020-11-09 23:35:04 +01:00
|
|
|
if ((texOp.Flags & TextureFlags.Bindless) != 0 || !images.Add(imageName))
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
int firstBinding = -1;
|
2019-10-18 04:41:18 +02:00
|
|
|
|
2019-11-03 03:07:21 +01:00
|
|
|
if ((texOp.Type & SamplerType.Indexed) != 0)
|
|
|
|
{
|
|
|
|
for (int index = 0; index < texOp.ArraySize; index++)
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
int binding = context.Config.Counts.IncrementImagesCount();
|
2019-11-03 03:07:21 +01:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
if (firstBinding < 0)
|
|
|
|
{
|
|
|
|
firstBinding = binding;
|
|
|
|
}
|
2019-11-03 03:07:21 +01:00
|
|
|
|
2020-11-09 23:35:04 +01:00
|
|
|
var desc = new TextureDescriptor(binding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle + index * 2);
|
2019-11-03 03:07:21 +01:00
|
|
|
|
2020-11-08 12:10:00 +01:00
|
|
|
context.ImageDescriptors.Add(desc);
|
2019-11-03 03:07:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-08 12:10:00 +01:00
|
|
|
firstBinding = context.Config.Counts.IncrementImagesCount();
|
|
|
|
|
2020-11-09 23:35:04 +01:00
|
|
|
var desc = new TextureDescriptor(firstBinding, texOp.Type, texOp.Format, texOp.CbufSlot, texOp.Handle);
|
2019-10-18 04:41:18 +02:00
|
|
|
|
2019-11-03 03:07:21 +01:00
|
|
|
context.ImageDescriptors.Add(desc);
|
|
|
|
}
|
2020-11-08 12:10:00 +01:00
|
|
|
|
|
|
|
string layout = texOp.Format.ToGlslFormat();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(layout))
|
|
|
|
{
|
|
|
|
layout = ", " + layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
string imageTypeName = texOp.Type.ToGlslImageType(texOp.Format.GetComponentType());
|
|
|
|
|
|
|
|
context.AppendLine($"layout (binding = {firstBinding}{layout}) uniform {imageTypeName} {imageName};");
|
2019-10-18 04:41:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
private static void DeclareInputAttributes(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
|
|
|
string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
|
|
|
|
|
|
|
|
foreach (int attr in info.IAttributes.OrderBy(x => x))
|
|
|
|
{
|
2020-04-03 02:20:47 +02:00
|
|
|
string iq = string.Empty;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2020-04-03 02:20:47 +02:00
|
|
|
if (context.Config.Stage == ShaderStage.Fragment)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-04-03 02:20:47 +02:00
|
|
|
iq = context.Config.ImapTypes[attr].GetFirstUsedType() switch
|
|
|
|
{
|
|
|
|
PixelImap.Constant => "flat ",
|
|
|
|
PixelImap.ScreenLinear => "noperspective ",
|
|
|
|
_ => string.Empty
|
|
|
|
};
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-07-29 00:01:11 +02:00
|
|
|
string name = $"{DefaultNames.IAttributePrefix}{attr}";
|
|
|
|
|
|
|
|
if ((context.Config.Flags & TranslationFlags.Feedback) != 0)
|
2020-07-15 05:01:10 +02:00
|
|
|
{
|
2020-07-29 00:01:11 +02:00
|
|
|
for (int c = 0; c < 4; c++)
|
|
|
|
{
|
|
|
|
char swzMask = "xyzw"[c];
|
2020-07-15 05:01:10 +02:00
|
|
|
|
2020-07-29 00:01:11 +02:00
|
|
|
context.AppendLine($"layout (location = {attr}, component = {c}) {iq}in float {name}_{swzMask}{suffix};");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.AppendLine($"layout (location = {attr}) {iq}in vec4 {name}{suffix};");
|
2020-07-15 05:01:10 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
|
|
|
if (context.Config.Stage == ShaderStage.Fragment)
|
|
|
|
{
|
|
|
|
DeclareUsedOutputAttributes(context, info);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DeclareAllOutputAttributes(context, info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareUsedOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
|
|
|
foreach (int attr in info.OAttributes.OrderBy(x => x))
|
|
|
|
{
|
|
|
|
context.AppendLine($"layout (location = {attr}) out vec4 {DefaultNames.OAttributePrefix}{attr};");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareAllOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
|
|
|
for (int attr = 0; attr < MaxAttributes; attr++)
|
|
|
|
{
|
2020-07-29 00:01:11 +02:00
|
|
|
DeclareOutputAttribute(context, attr);
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (int attr in info.OAttributes.OrderBy(x => x).Where(x => x >= MaxAttributes))
|
2020-07-29 00:01:11 +02:00
|
|
|
{
|
|
|
|
DeclareOutputAttribute(context, attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareOutputAttribute(CodeGenContext context, int attr)
|
|
|
|
{
|
|
|
|
string name = $"{DefaultNames.OAttributePrefix}{attr}";
|
|
|
|
|
|
|
|
if ((context.Config.Flags & TranslationFlags.Feedback) != 0)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-07-15 05:01:10 +02:00
|
|
|
for (int c = 0; c < 4; c++)
|
|
|
|
{
|
|
|
|
char swzMask = "xyzw"[c];
|
|
|
|
|
2020-07-29 00:01:11 +02:00
|
|
|
context.AppendLine($"layout (location = {attr}, component = {c}) out float {name}_{swzMask};");
|
2020-07-15 05:01:10 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2020-07-29 00:01:11 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
context.AppendLine($"layout (location = {attr}) out vec4 {name};");
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-10-13 02:40:50 +02:00
|
|
|
private static bool DeclareRenderScale(CodeGenContext context)
|
|
|
|
{
|
|
|
|
if ((context.Config.UsedFeatures & (FeatureFlags.FragCoordXY | FeatureFlags.IntegerSampling)) != 0)
|
|
|
|
{
|
|
|
|
string stage = OperandManager.GetShaderStagePrefix(context.Config.Stage);
|
|
|
|
|
2020-11-02 20:53:23 +01:00
|
|
|
int scaleElements = context.TextureDescriptors.Count + context.ImageDescriptors.Count;
|
2020-10-13 02:40:50 +02:00
|
|
|
|
|
|
|
if (context.Config.Stage == ShaderStage.Fragment)
|
|
|
|
{
|
|
|
|
scaleElements++; // Also includes render target scale, for gl_FragCoord.
|
|
|
|
}
|
|
|
|
|
|
|
|
context.AppendLine($"uniform float {stage}_renderScale[{scaleElements}];");
|
|
|
|
|
|
|
|
if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IntegerSampling))
|
|
|
|
{
|
|
|
|
context.AppendLine();
|
|
|
|
AppendHelperFunction(context, $"Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_{stage}.glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-31 04:29:22 +01:00
|
|
|
private static void AppendHelperFunction(CodeGenContext context, string filename)
|
|
|
|
{
|
|
|
|
string code = EmbeddedResources.ReadAllText(filename);
|
|
|
|
|
|
|
|
context.AppendLine(code.Replace("\t", CodeGenContext.Tab));
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|