Use vector transform feedback outputs with fragment shaders (#4708)

* Use vector transform feedback outputs with fragment shaders

* Shader cache version bump

* Fix missing outputs when vector transform feedback outputs are used
This commit is contained in:
gdkchan 2023-04-24 03:34:38 -03:00 committed by GitHub
parent 3f98369a17
commit 4dd77316f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 38 additions and 33 deletions

View file

@ -22,7 +22,7 @@ namespace Ryujinx.Graphics.Gpu.Shader.DiskCache
private const ushort FileFormatVersionMajor = 1; private const ushort FileFormatVersionMajor = 1;
private const ushort FileFormatVersionMinor = 2; private const ushort FileFormatVersionMinor = 2;
private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor; private const uint FileFormatVersionPacked = ((uint)FileFormatVersionMajor << 16) | FileFormatVersionMinor;
private const uint CodeGenVersion = 4703; private const uint CodeGenVersion = 4707;
private const string SharedTocFileName = "shared.toc"; private const string SharedTocFileName = "shared.toc";
private const string SharedDataFileName = "shared.data"; private const string SharedDataFileName = "shared.data";

View file

@ -449,7 +449,7 @@ namespace Ryujinx.Graphics.Gpu.Shader
if (translatorContexts[i] != null) if (translatorContexts[i] != null)
{ {
translatorContexts[i].SetGeometryShaderLayerInputAttribute(info.GpLayerInputAttribute); translatorContexts[i].SetGeometryShaderLayerInputAttribute(info.GpLayerInputAttribute);
translatorContexts[i].SetLastInVertexPipeline(translatorContexts[5] != null); translatorContexts[i].SetLastInVertexPipeline();
break; break;
} }
} }

View file

@ -569,7 +569,23 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
if (context.Config.TransformFeedbackEnabled && context.Config.Stage == ShaderStage.Fragment) if (context.Config.TransformFeedbackEnabled && context.Config.Stage == ShaderStage.Fragment)
{ {
for (int c = 0; c < 4; c++) int attrOffset = AttributeConsts.UserAttributeBase + attr * 16;
int components = context.Info.GetTransformFeedbackOutputComponents(attrOffset);
if (components > 1)
{
string type = components switch
{
2 => "vec2",
3 => "vec3",
4 => "vec4",
_ => "float"
};
context.AppendLine($"layout (location = {attr}) in {type} {name};");
}
for (int c = components > 1 ? components : 0; c < 4; c++)
{ {
char swzMask = "xyzw"[c]; char swzMask = "xyzw"[c];
@ -642,7 +658,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
if (context.Config.TransformFeedbackEnabled && context.Config.LastInVertexPipeline) if (context.Config.TransformFeedbackEnabled && context.Config.LastInVertexPipeline)
{ {
int attrOffset = AttributeConsts.UserAttributeBase + attr * 16; int attrOffset = AttributeConsts.UserAttributeBase + attr * 16;
int components = context.Config.LastInPipeline ? context.Info.GetTransformFeedbackOutputComponents(attrOffset) : 1; int components = context.Info.GetTransformFeedbackOutputComponents(attrOffset);
if (components > 1) if (components > 1)
{ {
@ -664,22 +680,20 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
context.AppendLine($"layout (location = {attr}{xfb}) out {type} {name};"); context.AppendLine($"layout (location = {attr}{xfb}) out {type} {name};");
} }
else
for (int c = components > 1 ? components : 0; c < 4; c++)
{ {
for (int c = 0; c < 4; c++) char swzMask = "xyzw"[c];
string xfb = string.Empty;
var tfOutput = context.Info.GetTransformFeedbackOutput(attrOffset + c * 4);
if (tfOutput.Valid)
{ {
char swzMask = "xyzw"[c]; xfb = $", xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}";
string xfb = string.Empty;
var tfOutput = context.Info.GetTransformFeedbackOutput(attrOffset + c * 4);
if (tfOutput.Valid)
{
xfb = $", xfb_buffer = {tfOutput.Buffer}, xfb_offset = {tfOutput.Offset}, xfb_stride = {tfOutput.Stride}";
}
context.AppendLine($"layout (location = {attr}, component = {c}{xfb}) out float {name}_{swzMask};");
} }
context.AppendLine($"layout (location = {attr}, component = {c}{xfb}) out float {name}_{swzMask};");
} }
} }
else else

View file

@ -220,7 +220,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Glsl
((config.LastInVertexPipeline && isOutAttr) || ((config.LastInVertexPipeline && isOutAttr) ||
(config.Stage == ShaderStage.Fragment && !isOutAttr))) (config.Stage == ShaderStage.Fragment && !isOutAttr)))
{ {
int components = config.LastInPipeline ? context.Info.GetTransformFeedbackOutputComponents(attrOffset) : 1; int components = context.Info.GetTransformFeedbackOutputComponents(attrOffset);
string name = components > 1 ? $"{prefix}{(value >> 4)}" : $"{prefix}{(value >> 4)}_{swzMask}"; string name = components > 1 ? $"{prefix}{(value >> 4)}" : $"{prefix}{(value >> 4)}_{swzMask}";
if (AttributeInfo.IsArrayAttributeGlsl(config.Stage, isOutAttr)) if (AttributeInfo.IsArrayAttributeGlsl(config.Stage, isOutAttr))

View file

@ -341,7 +341,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
attrOffset = attr; attrOffset = attr;
type = elemType; type = elemType;
if (Config.LastInPipeline && isOutAttr) if (isOutAttr)
{ {
int components = Info.GetTransformFeedbackOutputComponents(attr); int components = Info.GetTransformFeedbackOutputComponents(attr);

View file

@ -673,7 +673,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
int components = 1; int components = 1;
var type = attrInfo.Type & AggregateType.ElementTypeMask; var type = attrInfo.Type & AggregateType.ElementTypeMask;
if (context.Config.LastInPipeline && isOutAttr) if (isOutAttr)
{ {
components = context.Info.GetTransformFeedbackOutputComponents(attr); components = context.Info.GetTransformFeedbackOutputComponents(attr);

View file

@ -65,7 +65,7 @@ namespace Ryujinx.Graphics.Shader.StructuredIr
context.LeaveFunction(); context.LeaveFunction();
} }
if (config.TransformFeedbackEnabled && config.LastInVertexPipeline) if (config.TransformFeedbackEnabled && (config.LastInVertexPipeline || config.Stage == ShaderStage.Fragment))
{ {
for (int tfbIndex = 0; tfbIndex < 4; tfbIndex++) for (int tfbIndex = 0; tfbIndex < 4; tfbIndex++)
{ {

View file

@ -17,7 +17,6 @@ namespace Ryujinx.Graphics.Shader.Translation
public ShaderStage Stage { get; } public ShaderStage Stage { get; }
public bool GpPassthrough { get; } public bool GpPassthrough { get; }
public bool LastInPipeline { get; private set; }
public bool LastInVertexPipeline { get; private set; } public bool LastInVertexPipeline { get; private set; }
public bool HasLayerInputAttribute { get; private set; } public bool HasLayerInputAttribute { get; private set; }
@ -145,7 +144,6 @@ namespace Ryujinx.Graphics.Shader.Translation
OmapSampleMask = header.OmapSampleMask; OmapSampleMask = header.OmapSampleMask;
OmapDepth = header.OmapDepth; OmapDepth = header.OmapDepth;
TransformFeedbackEnabled = gpuAccessor.QueryTransformFeedbackEnabled(); TransformFeedbackEnabled = gpuAccessor.QueryTransformFeedbackEnabled();
LastInPipeline = true;
LastInVertexPipeline = header.Stage < ShaderStage.Fragment; LastInVertexPipeline = header.Stage < ShaderStage.Fragment;
} }
@ -253,13 +251,8 @@ namespace Ryujinx.Graphics.Shader.Translation
GpLayerInputAttribute = attr; GpLayerInputAttribute = attr;
} }
public void SetLastInVertexPipeline(bool hasFragment) public void SetLastInVertexPipeline()
{ {
if (!hasFragment)
{
LastInPipeline = true;
}
LastInVertexPipeline = true; LastInVertexPipeline = true;
} }
@ -331,8 +324,6 @@ namespace Ryujinx.Graphics.Shader.Translation
config._perPatchAttributeLocations = locationsMap; config._perPatchAttributeLocations = locationsMap;
} }
LastInPipeline = false;
// We don't consider geometry shaders using the geometry shader passthrough feature // We don't consider geometry shaders using the geometry shader passthrough feature
// as being the last because when this feature is used, it can't actually modify any of the outputs, // as being the last because when this feature is used, it can't actually modify any of the outputs,
// so the stage that comes before it is the last one that can do modifications. // so the stage that comes before it is the last one that can do modifications.

View file

@ -143,9 +143,9 @@ namespace Ryujinx.Graphics.Shader.Translation
_config.SetGeometryShaderLayerInputAttribute(attr); _config.SetGeometryShaderLayerInputAttribute(attr);
} }
public void SetLastInVertexPipeline(bool hasFragment) public void SetLastInVertexPipeline()
{ {
_config.SetLastInVertexPipeline(hasFragment); _config.SetLastInVertexPipeline();
} }
public ShaderProgram Translate(TranslatorContext other = null) public ShaderProgram Translate(TranslatorContext other = null)