2019-10-31 04:29:22 +01:00
|
|
|
using Ryujinx.Common;
|
2019-10-13 08:02:07 +02:00
|
|
|
using Ryujinx.Graphics.Shader.StructuredIr;
|
|
|
|
using Ryujinx.Graphics.Shader.Translation;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
2021-08-11 22:27:00 +02:00
|
|
|
using System.Numerics;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
|
|
|
|
{
|
|
|
|
static class Declarations
|
|
|
|
{
|
|
|
|
public static void Declare(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
2021-04-02 12:50:35 +02:00
|
|
|
context.AppendLine("#version 450 core");
|
2019-11-08 21:29:41 +01:00
|
|
|
context.AppendLine("#extension GL_ARB_gpu_shader_int64 : enable");
|
2021-09-19 14:38:39 +02:00
|
|
|
|
|
|
|
if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot())
|
|
|
|
{
|
|
|
|
context.AppendLine("#extension GL_ARB_shader_ballot : enable");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.AppendLine("#extension GL_KHR_shader_subgroup_basic : enable");
|
|
|
|
context.AppendLine("#extension GL_KHR_shader_subgroup_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");
|
2021-06-25 00:54:50 +02:00
|
|
|
context.AppendLine("#extension GL_EXT_texture_shadow_lod : enable");
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
if (context.Config.Stage == ShaderStage.Compute)
|
|
|
|
{
|
|
|
|
context.AppendLine("#extension GL_ARB_compute_shader : enable");
|
|
|
|
}
|
|
|
|
|
2021-01-29 04:38:51 +01:00
|
|
|
if (context.Config.GpPassthrough)
|
|
|
|
{
|
|
|
|
context.AppendLine("#extension GL_NV_geometry_shader_passthrough : 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();
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
var cBufferDescriptors = context.Config.GetConstantBufferDescriptors();
|
|
|
|
if (cBufferDescriptors.Length != 0)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
DeclareUniforms(context, cBufferDescriptors);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
var sBufferDescriptors = context.Config.GetStorageBufferDescriptors();
|
|
|
|
if (sBufferDescriptors.Length != 0)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
DeclareStorages(context, sBufferDescriptors);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
var textureDescriptors = context.Config.GetTextureDescriptors();
|
|
|
|
if (textureDescriptors.Length != 0)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
DeclareSamplers(context, textureDescriptors);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
var imageDescriptors = context.Config.GetImageDescriptors();
|
|
|
|
if (imageDescriptors.Length != 0)
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
DeclareImages(context, imageDescriptors);
|
2019-10-18 04:41:18 +02:00
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
if (context.Config.Stage != ShaderStage.Compute)
|
|
|
|
{
|
2021-01-29 04:38:51 +01:00
|
|
|
if (context.Config.Stage == ShaderStage.Geometry)
|
|
|
|
{
|
|
|
|
string inPrimitive = context.Config.GpuAccessor.QueryPrimitiveTopology().ToGlslString();
|
|
|
|
|
|
|
|
context.AppendLine($"layout ({inPrimitive}) in;");
|
|
|
|
|
|
|
|
if (context.Config.GpPassthrough)
|
|
|
|
{
|
|
|
|
context.AppendLine($"layout (passthrough) in gl_PerVertex");
|
|
|
|
context.EnterScope();
|
|
|
|
context.AppendLine("vec4 gl_Position;");
|
|
|
|
context.AppendLine("float gl_PointSize;");
|
|
|
|
context.AppendLine("float gl_ClipDistance[];");
|
|
|
|
context.LeaveScope(";");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string outPrimitive = context.Config.OutputTopology.ToGlslString();
|
|
|
|
|
|
|
|
int maxOutputVertices = context.Config.MaxOutputVertices;
|
|
|
|
|
|
|
|
context.AppendLine($"layout ({outPrimitive}, max_vertices = {maxOutputVertices}) out;");
|
|
|
|
}
|
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (context.Config.UsedInputAttributes != 0 || context.Config.GpPassthrough)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
DeclareInputAttributes(context, info);
|
|
|
|
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
if (context.Config.UsedOutputAttributes != 0 || context.Config.Stage != ShaderStage.Fragment)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
bool isFragment = context.Config.Stage == ShaderStage.Fragment;
|
|
|
|
|
|
|
|
if (isFragment || context.Config.Stage == ShaderStage.Compute)
|
2020-07-07 04:41:07 +02:00
|
|
|
{
|
2021-08-11 21:33:43 +02:00
|
|
|
if (isFragment && context.Config.GpuAccessor.QueryEarlyZForce())
|
|
|
|
{
|
|
|
|
context.AppendLine("layout(early_fragment_tests) in;");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((context.Config.UsedFeatures & (FeatureFlags.FragCoordXY | FeatureFlags.IntegerSampling)) != 0)
|
2020-07-26 05:03:40 +02:00
|
|
|
{
|
2021-08-11 21:33:43 +02:00
|
|
|
string stage = OperandManager.GetShaderStagePrefix(context.Config.Stage);
|
|
|
|
|
|
|
|
int scaleElements = context.Config.GetTextureDescriptors().Length + context.Config.GetImageDescriptors().Length;
|
|
|
|
|
|
|
|
if (isFragment)
|
2020-12-02 00:13:27 +01:00
|
|
|
{
|
2021-08-11 21:33:43 +02:00
|
|
|
scaleElements++; // Also includes render target scale, for gl_FragCoord.
|
2020-12-02 00:13:27 +01:00
|
|
|
}
|
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
DeclareSupportUniformBlock(context, isFragment, scaleElements);
|
2020-07-26 05:03:40 +02:00
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IntegerSampling))
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, $"Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/TexelFetchScale_{stage}.glsl");
|
|
|
|
context.AppendLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (isFragment)
|
2020-07-07 04:41:07 +02:00
|
|
|
{
|
2021-08-11 21:33:43 +02:00
|
|
|
DeclareSupportUniformBlock(context, true, 0);
|
2020-07-07 04:41:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:38:33 +01:00
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.AtomicMinMaxS32Shared) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/AtomicMinMaxS32Shared.glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((info.HelperFunctionsMask & HelperFunctionsMask.AtomicMinMaxS32Storage) != 0)
|
|
|
|
{
|
|
|
|
AppendHelperFunction(context, "Ryujinx.Graphics.Shader/CodeGen/Glsl/HelperFunctions/AtomicMinMaxS32Storage.glsl");
|
|
|
|
}
|
|
|
|
|
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}\".");
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
private static void DeclareUniforms(CodeGenContext context, BufferDescriptor[] descriptors)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2020-10-13 02:40:50 +02:00
|
|
|
string ubSize = "[" + NumberFormatter.FormatInt(Constants.ConstantBufferSize / 16) + "]";
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
if (context.Config.UsedFeatures.HasFlag(FeatureFlags.CbIndexing))
|
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
|
|
|
|
2021-05-20 20:12:15 +02:00
|
|
|
context.AppendLine($"layout (binding = {context.Config.FirstConstantBufferBinding}, std140) uniform {blockName}");
|
2020-11-08 12:10:00 +01:00
|
|
|
context.EnterScope();
|
|
|
|
context.AppendLine("vec4 " + DefaultNames.DataName + ubSize + ";");
|
2021-05-20 20:12:15 +02:00
|
|
|
context.LeaveScope($" {ubName}[{NumberFormatter.FormatInt(descriptors.Max(x => x.Slot) + 1)}];");
|
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
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
foreach (var descriptor in descriptors)
|
2020-10-13 02:40:50 +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
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
ubName += "_" + DefaultNames.UniformNamePrefix + descriptor.Slot;
|
2020-07-07 04:41:07 +02:00
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
context.AppendLine($"layout (binding = {descriptor.Binding}, std140) uniform {ubName}");
|
2020-10-13 02:40:50 +02:00
|
|
|
context.EnterScope();
|
2021-05-19 23:15:26 +02:00
|
|
|
context.AppendLine("vec4 " + OperandManager.GetUbName(context.Config.Stage, descriptor.Slot, false) + ubSize + ";");
|
2020-10-13 02:40:50 +02:00
|
|
|
context.LeaveScope(";");
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 04:41:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
private static void DeclareStorages(CodeGenContext context, BufferDescriptor[] descriptors)
|
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
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
string layout = context.Config.Options.TargetApi == TargetApi.Vulkan ? ", set = 1" : string.Empty;
|
|
|
|
|
|
|
|
context.AppendLine($"layout (binding = {context.Config.FirstStorageBufferBinding}{layout}, std430) buffer {blockName}");
|
2019-11-08 21:29:41 +01:00
|
|
|
context.EnterScope();
|
|
|
|
context.AppendLine("uint " + DefaultNames.DataName + "[];");
|
2021-05-20 20:12:15 +02:00
|
|
|
context.LeaveScope($" {sbName}[{NumberFormatter.FormatInt(descriptors.Max(x => x.Slot) + 1)}];");
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
private static void DeclareSamplers(CodeGenContext context, TextureDescriptor[] descriptors)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
int arraySize = 0;
|
|
|
|
foreach (var descriptor in descriptors)
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
if (descriptor.Type.HasFlag(SamplerType.Indexed))
|
2019-11-03 03:07:21 +01:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
if (arraySize == 0)
|
2019-11-03 03:07:21 +01:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
arraySize = ShaderConfig.SamplerArraySize;
|
|
|
|
}
|
|
|
|
else if (--arraySize != 0)
|
|
|
|
{
|
|
|
|
continue;
|
2019-11-03 03:07:21 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-08 12:10:00 +01:00
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
string indexExpr = NumberFormatter.FormatInt(arraySize);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
string samplerName = OperandManager.GetSamplerName(
|
|
|
|
context.Config.Stage,
|
|
|
|
descriptor.CbufSlot,
|
|
|
|
descriptor.HandleIndex,
|
|
|
|
descriptor.Type.HasFlag(SamplerType.Indexed),
|
|
|
|
indexExpr);
|
2020-11-08 12:10:00 +01:00
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
string samplerTypeName = descriptor.Type.ToGlslSamplerType();
|
2020-11-08 12:10:00 +01:00
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
string layout = string.Empty;
|
|
|
|
|
|
|
|
if (context.Config.Options.TargetApi == TargetApi.Vulkan)
|
|
|
|
{
|
|
|
|
bool isBuffer = (descriptor.Type & SamplerType.Mask) == SamplerType.TextureBuffer;
|
|
|
|
int setIndex = isBuffer ? 4 : 2;
|
|
|
|
|
|
|
|
layout = $", set = {setIndex}";
|
|
|
|
}
|
|
|
|
|
|
|
|
context.AppendLine($"layout (binding = {descriptor.Binding}{layout}) uniform {samplerTypeName} {samplerName};");
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
private static void DeclareImages(CodeGenContext context, TextureDescriptor[] descriptors)
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
int arraySize = 0;
|
|
|
|
foreach (var descriptor in descriptors)
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
if (descriptor.Type.HasFlag(SamplerType.Indexed))
|
2019-10-18 04:41:18 +02:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
if (arraySize == 0)
|
2019-11-03 03:07:21 +01:00
|
|
|
{
|
2021-05-19 23:15:26 +02:00
|
|
|
arraySize = ShaderConfig.SamplerArraySize;
|
|
|
|
}
|
|
|
|
else if (--arraySize != 0)
|
|
|
|
{
|
|
|
|
continue;
|
2019-11-03 03:07:21 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-08 12:10:00 +01:00
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
string indexExpr = NumberFormatter.FormatInt(arraySize);
|
2019-10-18 04:41:18 +02:00
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
string imageName = OperandManager.GetImageName(
|
|
|
|
context.Config.Stage,
|
|
|
|
descriptor.CbufSlot,
|
|
|
|
descriptor.HandleIndex,
|
|
|
|
descriptor.Format,
|
|
|
|
descriptor.Type.HasFlag(SamplerType.Indexed),
|
|
|
|
indexExpr);
|
2020-11-08 12:10:00 +01:00
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
string imageTypeName = descriptor.Type.ToGlslImageType(descriptor.Format.GetComponentType());
|
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
string layout = descriptor.Format.ToGlslFormat();
|
2020-11-08 12:10:00 +01:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(layout))
|
|
|
|
{
|
|
|
|
layout = ", " + layout;
|
|
|
|
}
|
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
if (context.Config.Options.TargetApi == TargetApi.Vulkan)
|
|
|
|
{
|
|
|
|
bool isBuffer = (descriptor.Type & SamplerType.Mask) == SamplerType.TextureBuffer;
|
|
|
|
int setIndex = isBuffer ? 5 : 3;
|
|
|
|
|
|
|
|
layout = $", set = {setIndex}{layout}";
|
|
|
|
}
|
2020-11-08 12:10:00 +01:00
|
|
|
|
2021-05-19 23:15:26 +02:00
|
|
|
context.AppendLine($"layout (binding = {descriptor.Binding}{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)
|
|
|
|
{
|
2021-08-27 01:44:47 +02:00
|
|
|
if (context.Config.UsedFeatures.HasFlag(FeatureFlags.IaIndexing))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-08-27 01:44:47 +02:00
|
|
|
string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-08-27 01:44:47 +02:00
|
|
|
context.AppendLine($"layout (location = 0) in vec4 {DefaultNames.IAttributePrefix}{suffix}[{Constants.MaxAttributes}];");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int usedAttributes = context.Config.UsedInputAttributes;
|
|
|
|
while (usedAttributes != 0)
|
|
|
|
{
|
|
|
|
int index = BitOperations.TrailingZeroCount(usedAttributes);
|
|
|
|
|
|
|
|
DeclareInputAttribute(context, info, index);
|
2021-08-11 22:27:00 +02:00
|
|
|
|
2021-08-27 01:44:47 +02:00
|
|
|
usedAttributes &= ~(1 << index);
|
|
|
|
}
|
2021-06-23 23:04:59 +02:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 04:38:51 +01:00
|
|
|
|
2021-06-23 23:04:59 +02:00
|
|
|
private static void DeclareInputAttribute(CodeGenContext context, StructuredProgramInfo info, int attr)
|
|
|
|
{
|
|
|
|
string suffix = context.Config.Stage == ShaderStage.Geometry ? "[]" : string.Empty;
|
|
|
|
string iq = string.Empty;
|
2020-07-29 00:01:11 +02:00
|
|
|
|
2021-06-23 23:04:59 +02:00
|
|
|
if (context.Config.Stage == ShaderStage.Fragment)
|
|
|
|
{
|
|
|
|
iq = context.Config.ImapTypes[attr].GetFirstUsedType() switch
|
2020-07-15 05:01:10 +02:00
|
|
|
{
|
2021-06-23 23:04:59 +02:00
|
|
|
PixelImap.Constant => "flat ",
|
|
|
|
PixelImap.ScreenLinear => "noperspective ",
|
|
|
|
_ => string.Empty
|
|
|
|
};
|
|
|
|
}
|
2020-07-15 05:01:10 +02:00
|
|
|
|
2021-08-11 22:27:00 +02:00
|
|
|
string pass = (context.Config.PassthroughAttributes & (1 << attr)) != 0 ? "passthrough, " : string.Empty;
|
2021-06-23 23:04:59 +02:00
|
|
|
string name = $"{DefaultNames.IAttributePrefix}{attr}";
|
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
if ((context.Config.Options.Flags & TranslationFlags.Feedback) != 0)
|
2021-06-23 23:04:59 +02:00
|
|
|
{
|
|
|
|
for (int c = 0; c < 4; c++)
|
2020-07-29 00:01:11 +02:00
|
|
|
{
|
2021-06-23 23:04:59 +02:00
|
|
|
char swzMask = "xyzw"[c];
|
|
|
|
|
|
|
|
context.AppendLine($"layout ({pass}location = {attr}, component = {c}) {iq}in float {name}_{swzMask}{suffix};");
|
2020-07-15 05:01:10 +02:00
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
2021-06-23 23:04:59 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
context.AppendLine($"layout ({pass}location = {attr}) {iq}in vec4 {name}{suffix};");
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareOutputAttributes(CodeGenContext context, StructuredProgramInfo info)
|
|
|
|
{
|
2021-08-27 01:44:47 +02:00
|
|
|
if (context.Config.UsedFeatures.HasFlag(FeatureFlags.OaIndexing))
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
2021-08-27 01:44:47 +02:00
|
|
|
context.AppendLine($"layout (location = 0) out vec4 {DefaultNames.OAttributePrefix}[{Constants.MaxAttributes}];");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int usedAttributes = context.Config.UsedOutputAttributes;
|
|
|
|
while (usedAttributes != 0)
|
|
|
|
{
|
|
|
|
int index = BitOperations.TrailingZeroCount(usedAttributes);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-08-27 01:44:47 +02:00
|
|
|
DeclareOutputAttribute(context, index);
|
2019-10-13 08:02:07 +02:00
|
|
|
|
2021-08-27 01:44:47 +02:00
|
|
|
usedAttributes &= ~(1 << index);
|
|
|
|
}
|
2020-07-29 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void DeclareOutputAttribute(CodeGenContext context, int attr)
|
|
|
|
{
|
|
|
|
string name = $"{DefaultNames.OAttributePrefix}{attr}";
|
|
|
|
|
2021-07-06 21:20:06 +02:00
|
|
|
if ((context.Config.Options.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
|
|
|
}
|
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
private static void DeclareSupportUniformBlock(CodeGenContext context, bool isFragment, int scaleElements)
|
2020-10-13 02:40:50 +02:00
|
|
|
{
|
2021-08-11 21:33:43 +02:00
|
|
|
if (!isFragment && scaleElements == 0)
|
2020-10-13 02:40:50 +02:00
|
|
|
{
|
2021-08-11 21:33:43 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-10-13 02:40:50 +02:00
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
context.AppendLine($"layout (binding = 0, std140) uniform {DefaultNames.SupportBlockName}");
|
|
|
|
context.EnterScope();
|
2020-10-13 02:40:50 +02:00
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
if (isFragment)
|
|
|
|
{
|
|
|
|
context.AppendLine($"uint {DefaultNames.SupportBlockAlphaTestName};");
|
|
|
|
context.AppendLine($"bool {DefaultNames.SupportBlockIsBgraName}[{SupportBuffer.FragmentIsBgraCount}];");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context.AppendLine($"uint s_reserved[{SupportBuffer.ComputeRenderScaleOffset / SupportBuffer.FieldSize}];");
|
|
|
|
}
|
2020-10-13 02:40:50 +02:00
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
if (scaleElements != 0)
|
|
|
|
{
|
|
|
|
context.AppendLine($"float {DefaultNames.SupportBlockRenderScaleName}[{scaleElements}];");
|
2020-10-13 02:40:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 21:33:43 +02:00
|
|
|
context.LeaveScope(";");
|
|
|
|
context.AppendLine();
|
2020-10-13 02:40:50 +02:00
|
|
|
}
|
|
|
|
|
2019-10-31 04:29:22 +01:00
|
|
|
private static void AppendHelperFunction(CodeGenContext context, string filename)
|
|
|
|
{
|
|
|
|
string code = EmbeddedResources.ReadAllText(filename);
|
|
|
|
|
2021-01-26 07:38:33 +01:00
|
|
|
code = code.Replace("\t", CodeGenContext.Tab);
|
|
|
|
code = code.Replace("$SHARED_MEM$", DefaultNames.SharedMemoryName);
|
|
|
|
code = code.Replace("$STORAGE_MEM$", OperandManager.GetShaderStagePrefix(context.Config.Stage) + "_" + DefaultNames.StorageNamePrefix);
|
|
|
|
|
2021-09-19 14:38:39 +02:00
|
|
|
if (context.Config.GpuAccessor.QueryHostSupportsShaderBallot())
|
|
|
|
{
|
|
|
|
code = code.Replace("$SUBGROUP_INVOCATION$", "gl_SubGroupInvocationARB");
|
|
|
|
code = code.Replace("$SUBGROUP_BROADCAST$", "readInvocationARB");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
code = code.Replace("$SUBGROUP_INVOCATION$", "gl_SubgroupInvocationID");
|
|
|
|
code = code.Replace("$SUBGROUP_BROADCAST$", "subgroupBroadcast");
|
|
|
|
}
|
|
|
|
|
2021-01-26 07:38:33 +01:00
|
|
|
context.AppendLine(code);
|
2019-10-31 04:29:22 +01:00
|
|
|
context.AppendLine();
|
|
|
|
}
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
}
|