aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Shader/CodeGen/Spirv
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2024-04-07 18:25:55 -0300
committerGitHub <noreply@github.com>2024-04-07 18:25:55 -0300
commit3e6e0e4afaa3c3ffb118cb17b61feb16966a7eeb (patch)
treea4652499c089b0853e39c382cad82a9db4d6ad08 /src/Ryujinx.Graphics.Shader/CodeGen/Spirv
parent808803d97a0c06809bf000687c252f960048fcf0 (diff)
Add support for large sampler arrays on Vulkan (#6489)
* Add support for large sampler arrays on Vulkan * Shader cache version bump * Format whitespace * Move DescriptorSetManager to PipelineLayoutCacheEntry to allow different pool sizes per layout * Handle array textures with different types on the same buffer * Somewhat better caching system * Avoid useless buffer data modification checks * Move redundant bindings update checking to the backend * Fix an issue where texture arrays would get the same bindings across stages on Vulkan * Backport some fixes from part 2 * Fix typo * PR feedback * Format whitespace * Add some missing XML docs
Diffstat (limited to 'src/Ryujinx.Graphics.Shader/CodeGen/Spirv')
-rw-r--r--src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs4
-rw-r--r--src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs39
-rw-r--r--src/Ryujinx.Graphics.Shader/CodeGen/Spirv/ImageDeclaration.cs20
-rw-r--r--src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs220
-rw-r--r--src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SamplerDeclaration.cs27
5 files changed, 153 insertions, 157 deletions
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs
index 17c3eefe..2b1fdf44 100644
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/CodeGenContext.cs
@@ -34,8 +34,8 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
public Dictionary<int, Instruction> SharedMemories { get; } = new();
public Dictionary<int, SamplerType> SamplersTypes { get; } = new();
- public Dictionary<int, (Instruction, Instruction, Instruction)> Samplers { get; } = new();
- public Dictionary<int, (Instruction, Instruction)> Images { get; } = new();
+ public Dictionary<int, SamplerDeclaration> Samplers { get; } = new();
+ public Dictionary<int, ImageDeclaration> Images { get; } = new();
public Dictionary<IoDefinition, Instruction> Inputs { get; } = new();
public Dictionary<IoDefinition, Instruction> Outputs { get; } = new();
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs
index b7482425..9633c522 100644
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Declarations.cs
@@ -181,9 +181,27 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
var sampledImageType = context.TypeSampledImage(imageType);
var sampledImagePointerType = context.TypePointer(StorageClass.UniformConstant, sampledImageType);
- var sampledImageVariable = context.Variable(sampledImagePointerType, StorageClass.UniformConstant);
+ var sampledImageArrayPointerType = sampledImagePointerType;
- context.Samplers.Add(sampler.Binding, (imageType, sampledImageType, sampledImageVariable));
+ if (sampler.ArrayLength == 0)
+ {
+ var sampledImageArrayType = context.TypeRuntimeArray(sampledImageType);
+ sampledImageArrayPointerType = context.TypePointer(StorageClass.UniformConstant, sampledImageArrayType);
+ }
+ else if (sampler.ArrayLength != 1)
+ {
+ var sampledImageArrayType = context.TypeArray(sampledImageType, context.Constant(context.TypeU32(), sampler.ArrayLength));
+ sampledImageArrayPointerType = context.TypePointer(StorageClass.UniformConstant, sampledImageArrayType);
+ }
+
+ var sampledImageVariable = context.Variable(sampledImageArrayPointerType, StorageClass.UniformConstant);
+
+ context.Samplers.Add(sampler.Binding, new SamplerDeclaration(
+ imageType,
+ sampledImageType,
+ sampledImagePointerType,
+ sampledImageVariable,
+ sampler.ArrayLength != 1));
context.SamplersTypes.Add(sampler.Binding, sampler.Type);
context.Name(sampledImageVariable, sampler.Name);
@@ -211,9 +229,22 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
GetImageFormat(image.Format));
var imagePointerType = context.TypePointer(StorageClass.UniformConstant, imageType);
- var imageVariable = context.Variable(imagePointerType, StorageClass.UniformConstant);
+ var imageArrayPointerType = imagePointerType;
+
+ if (image.ArrayLength == 0)
+ {
+ var imageArrayType = context.TypeRuntimeArray(imageType);
+ imageArrayPointerType = context.TypePointer(StorageClass.UniformConstant, imageArrayType);
+ }
+ else if (image.ArrayLength != 1)
+ {
+ var imageArrayType = context.TypeArray(imageType, context.Constant(context.TypeU32(), image.ArrayLength));
+ imageArrayPointerType = context.TypePointer(StorageClass.UniformConstant, imageArrayType);
+ }
+
+ var imageVariable = context.Variable(imageArrayPointerType, StorageClass.UniformConstant);
- context.Images.Add(image.Binding, (imageType, imageVariable));
+ context.Images.Add(image.Binding, new ImageDeclaration(imageType, imagePointerType, imageVariable, image.ArrayLength != 1));
context.Name(imageVariable, image.Name);
context.Decorate(imageVariable, Decoration.DescriptorSet, (LiteralInteger)setIndex);
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/ImageDeclaration.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/ImageDeclaration.cs
new file mode 100644
index 00000000..1e0aee73
--- /dev/null
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/ImageDeclaration.cs
@@ -0,0 +1,20 @@
+using Spv.Generator;
+
+namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
+{
+ readonly struct ImageDeclaration
+ {
+ public readonly Instruction ImageType;
+ public readonly Instruction ImagePointerType;
+ public readonly Instruction Image;
+ public readonly bool IsIndexed;
+
+ public ImageDeclaration(Instruction imageType, Instruction imagePointerType, Instruction image, bool isIndexed)
+ {
+ ImageType = imageType;
+ ImagePointerType = imagePointerType;
+ Image = image;
+ IsIndexed = isIndexed;
+ }
+ }
+}
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs
index 601753cb..409e466c 100644
--- a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/Instructions.cs
@@ -591,34 +591,28 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
AstTextureOperation texOp = (AstTextureOperation)operation;
- bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
-
var componentType = texOp.Format.GetComponentType();
- // TODO: Bindless texture support. For now we just return 0/do nothing.
- if (isBindless)
- {
- return new OperationResult(componentType, componentType switch
- {
- AggregateType.S32 => context.Constant(context.TypeS32(), 0),
- AggregateType.U32 => context.Constant(context.TypeU32(), 0u),
- _ => context.Constant(context.TypeFP32(), 0f),
- });
- }
-
bool isArray = (texOp.Type & SamplerType.Array) != 0;
- bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
- int srcIndex = isBindless ? 1 : 0;
+ int srcIndex = 0;
SpvInstruction Src(AggregateType type)
{
return context.Get(type, texOp.GetSource(srcIndex++));
}
- if (isIndexed)
+ ImageDeclaration declaration = context.Images[texOp.Binding];
+ SpvInstruction image = declaration.Image;
+
+ SpvInstruction resultType = context.GetType(componentType);
+ SpvInstruction imagePointerType = context.TypePointer(StorageClass.Image, resultType);
+
+ if (declaration.IsIndexed)
{
- Src(AggregateType.S32);
+ SpvInstruction textureIndex = Src(AggregateType.S32);
+
+ image = context.AccessChain(imagePointerType, image, textureIndex);
}
int coordsCount = texOp.Type.GetDimensions();
@@ -646,14 +640,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
SpvInstruction value = Src(componentType);
- (var imageType, var imageVariable) = context.Images[texOp.Binding];
-
- context.Load(imageType, imageVariable);
-
- SpvInstruction resultType = context.GetType(componentType);
- SpvInstruction imagePointerType = context.TypePointer(StorageClass.Image, resultType);
-
- var pointer = context.ImageTexelPointer(imagePointerType, imageVariable, pCoords, context.Constant(context.TypeU32(), 0));
+ var pointer = context.ImageTexelPointer(imagePointerType, image, pCoords, context.Constant(context.TypeU32(), 0));
var one = context.Constant(context.TypeU32(), 1);
var zero = context.Constant(context.TypeU32(), 0);
@@ -683,31 +670,29 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
AstTextureOperation texOp = (AstTextureOperation)operation;
- bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
-
var componentType = texOp.Format.GetComponentType();
- // TODO: Bindless texture support. For now we just return 0/do nothing.
- if (isBindless)
- {
- return GetZeroOperationResult(context, texOp, componentType, isVector: true);
- }
-
bool isArray = (texOp.Type & SamplerType.Array) != 0;
- bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
- int srcIndex = isBindless ? 1 : 0;
+ int srcIndex = 0;
SpvInstruction Src(AggregateType type)
{
return context.Get(type, texOp.GetSource(srcIndex++));
}
- if (isIndexed)
+ ImageDeclaration declaration = context.Images[texOp.Binding];
+ SpvInstruction image = declaration.Image;
+
+ if (declaration.IsIndexed)
{
- Src(AggregateType.S32);
+ SpvInstruction textureIndex = Src(AggregateType.S32);
+
+ image = context.AccessChain(declaration.ImagePointerType, image, textureIndex);
}
+ image = context.Load(declaration.ImageType, image);
+
int coordsCount = texOp.Type.GetDimensions();
int pCount = coordsCount + (isArray ? 1 : 0);
@@ -731,9 +716,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
pCoords = Src(AggregateType.S32);
}
- (var imageType, var imageVariable) = context.Images[texOp.Binding];
-
- var image = context.Load(imageType, imageVariable);
var imageComponentType = context.GetType(componentType);
var swizzledResultType = texOp.GetVectorType(componentType);
@@ -747,29 +729,27 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
AstTextureOperation texOp = (AstTextureOperation)operation;
- bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
-
- // TODO: Bindless texture support. For now we just return 0/do nothing.
- if (isBindless)
- {
- return OperationResult.Invalid;
- }
-
bool isArray = (texOp.Type & SamplerType.Array) != 0;
- bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
- int srcIndex = isBindless ? 1 : 0;
+ int srcIndex = 0;
SpvInstruction Src(AggregateType type)
{
return context.Get(type, texOp.GetSource(srcIndex++));
}
- if (isIndexed)
+ ImageDeclaration declaration = context.Images[texOp.Binding];
+ SpvInstruction image = declaration.Image;
+
+ if (declaration.IsIndexed)
{
- Src(AggregateType.S32);
+ SpvInstruction textureIndex = Src(AggregateType.S32);
+
+ image = context.AccessChain(declaration.ImagePointerType, image, textureIndex);
}
+ image = context.Load(declaration.ImageType, image);
+
int coordsCount = texOp.Type.GetDimensions();
int pCount = coordsCount + (isArray ? 1 : 0);
@@ -818,10 +798,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
var texel = context.CompositeConstruct(context.TypeVector(context.GetType(componentType), ComponentsCount), cElems);
- (var imageType, var imageVariable) = context.Images[texOp.Binding];
-
- var image = context.Load(imageType, imageVariable);
-
context.ImageWrite(image, pCoords, texel, ImageOperandsMask.MaskNone);
return OperationResult.Invalid;
@@ -854,16 +830,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
AstTextureOperation texOp = (AstTextureOperation)operation;
- bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
-
- bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
-
- // TODO: Bindless texture support. For now we just return 0.
- if (isBindless)
- {
- return new OperationResult(AggregateType.S32, context.Constant(context.TypeS32(), 0));
- }
-
int srcIndex = 0;
SpvInstruction Src(AggregateType type)
@@ -871,11 +837,18 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
return context.Get(type, texOp.GetSource(srcIndex++));
}
- if (isIndexed)
+ SamplerDeclaration declaration = context.Samplers[texOp.Binding];
+ SpvInstruction image = declaration.Image;
+
+ if (declaration.IsIndexed)
{
- Src(AggregateType.S32);
+ SpvInstruction textureIndex = Src(AggregateType.S32);
+
+ image = context.AccessChain(declaration.SampledImagePointerType, image, textureIndex);
}
+ image = context.Load(declaration.SampledImageType, image);
+
int pCount = texOp.Type.GetDimensions();
SpvInstruction pCoords;
@@ -897,10 +870,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
pCoords = Src(AggregateType.FP32);
}
- (_, var sampledImageType, var sampledImageVariable) = context.Samplers[texOp.Binding];
-
- var image = context.Load(sampledImageType, sampledImageVariable);
-
var resultType = context.TypeVector(context.TypeFP32(), 2);
var packed = context.ImageQueryLod(resultType, image, pCoords);
var result = context.CompositeExtract(context.TypeFP32(), packed, (SpvLiteralInteger)texOp.Index);
@@ -1182,7 +1151,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
AstTextureOperation texOp = (AstTextureOperation)operation;
- bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
bool isGather = (texOp.Flags & TextureFlags.Gather) != 0;
bool hasDerivatives = (texOp.Flags & TextureFlags.Derivatives) != 0;
bool intCoords = (texOp.Flags & TextureFlags.IntCoords) != 0;
@@ -1192,30 +1160,28 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
bool hasOffsets = (texOp.Flags & TextureFlags.Offsets) != 0;
bool isArray = (texOp.Type & SamplerType.Array) != 0;
- bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
bool isMultisample = (texOp.Type & SamplerType.Multisample) != 0;
bool isShadow = (texOp.Type & SamplerType.Shadow) != 0;
- bool colorIsVector = isGather || !isShadow;
-
- // TODO: Bindless texture support. For now we just return 0.
- if (isBindless)
- {
- return GetZeroOperationResult(context, texOp, AggregateType.FP32, colorIsVector);
- }
-
- int srcIndex = isBindless ? 1 : 0;
+ int srcIndex = 0;
SpvInstruction Src(AggregateType type)
{
return context.Get(type, texOp.GetSource(srcIndex++));
}
- if (isIndexed)
+ SamplerDeclaration declaration = context.Samplers[texOp.Binding];
+ SpvInstruction image = declaration.Image;
+
+ if (declaration.IsIndexed)
{
- Src(AggregateType.S32);
+ SpvInstruction textureIndex = Src(AggregateType.S32);
+
+ image = context.AccessChain(declaration.SampledImagePointerType, image, textureIndex);
}
+ image = context.Load(declaration.SampledImageType, image);
+
int coordsCount = texOp.Type.GetDimensions();
int pCount = coordsCount;
@@ -1419,15 +1385,13 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
operandsList.Add(sample);
}
- var resultType = colorIsVector ? context.TypeVector(context.TypeFP32(), 4) : context.TypeFP32();
-
- (var imageType, var sampledImageType, var sampledImageVariable) = context.Samplers[texOp.Binding];
+ bool colorIsVector = isGather || !isShadow;
- var image = context.Load(sampledImageType, sampledImageVariable);
+ var resultType = colorIsVector ? context.TypeVector(context.TypeFP32(), 4) : context.TypeFP32();
if (intCoords)
{
- image = context.Image(imageType, image);
+ image = context.Image(declaration.ImageType, image);
}
var operands = operandsList.ToArray();
@@ -1485,25 +1449,18 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
AstTextureOperation texOp = (AstTextureOperation)operation;
- bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
+ SamplerDeclaration declaration = context.Samplers[texOp.Binding];
+ SpvInstruction image = declaration.Image;
- // TODO: Bindless texture support. For now we just return 0.
- if (isBindless)
+ if (declaration.IsIndexed)
{
- return new OperationResult(AggregateType.S32, context.Constant(context.TypeS32(), 0));
- }
-
- bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
+ SpvInstruction textureIndex = context.GetS32(texOp.GetSource(0));
- if (isIndexed)
- {
- context.GetS32(texOp.GetSource(0));
+ image = context.AccessChain(declaration.SampledImagePointerType, image, textureIndex);
}
- (var imageType, var sampledImageType, var sampledImageVariable) = context.Samplers[texOp.Binding];
-
- var image = context.Load(sampledImageType, sampledImageVariable);
- image = context.Image(imageType, image);
+ image = context.Load(declaration.SampledImageType, image);
+ image = context.Image(declaration.ImageType, image);
SpvInstruction result = context.ImageQuerySamples(context.TypeS32(), image);
@@ -1514,25 +1471,18 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
{
AstTextureOperation texOp = (AstTextureOperation)operation;
- bool isBindless = (texOp.Flags & TextureFlags.Bindless) != 0;
+ SamplerDeclaration declaration = context.Samplers[texOp.Binding];
+ SpvInstruction image = declaration.Image;
- // TODO: Bindless texture support. For now we just return 0.
- if (isBindless)
+ if (declaration.IsIndexed)
{
- return new OperationResult(AggregateType.S32, context.Constant(context.TypeS32(), 0));
- }
-
- bool isIndexed = (texOp.Type & SamplerType.Indexed) != 0;
+ SpvInstruction textureIndex = context.GetS32(texOp.GetSource(0));
- if (isIndexed)
- {
- context.GetS32(texOp.GetSource(0));
+ image = context.AccessChain(declaration.SampledImagePointerType, image, textureIndex);
}
- (var imageType, var sampledImageType, var sampledImageVariable) = context.Samplers[texOp.Binding];
-
- var image = context.Load(sampledImageType, sampledImageVariable);
- image = context.Image(imageType, image);
+ image = context.Load(declaration.SampledImageType, image);
+ image = context.Image(declaration.ImageType, image);
if (texOp.Index == 3)
{
@@ -1556,7 +1506,7 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
if (hasLod)
{
- int lodSrcIndex = isBindless || isIndexed ? 1 : 0;
+ int lodSrcIndex = declaration.IsIndexed ? 1 : 0;
var lod = context.GetS32(operation.GetSource(lodSrcIndex));
result = context.ImageQuerySizeLod(resultType, image, lod);
}
@@ -1929,38 +1879,6 @@ namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
return context.Load(context.GetType(varType), context.Inputs[ioDefinition]);
}
- private static OperationResult GetZeroOperationResult(
- CodeGenContext context,
- AstTextureOperation texOp,
- AggregateType scalarType,
- bool isVector)
- {
- var zero = scalarType switch
- {
- AggregateType.S32 => context.Constant(context.TypeS32(), 0),
- AggregateType.U32 => context.Constant(context.TypeU32(), 0u),
- _ => context.Constant(context.TypeFP32(), 0f),
- };
-
- if (isVector)
- {
- AggregateType outputType = texOp.GetVectorType(scalarType);
-
- if ((outputType & AggregateType.ElementCountMask) != 0)
- {
- int componentsCount = BitOperations.PopCount((uint)texOp.Index);
-
- SpvInstruction[] values = new SpvInstruction[componentsCount];
-
- values.AsSpan().Fill(zero);
-
- return new OperationResult(outputType, context.ConstantComposite(context.GetType(outputType), values));
- }
- }
-
- return new OperationResult(scalarType, zero);
- }
-
private static SpvInstruction GetSwizzledResult(CodeGenContext context, SpvInstruction vector, AggregateType swizzledResultType, int mask)
{
if ((swizzledResultType & AggregateType.ElementCountMask) != 0)
diff --git a/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SamplerDeclaration.cs b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SamplerDeclaration.cs
new file mode 100644
index 00000000..9e0ecd79
--- /dev/null
+++ b/src/Ryujinx.Graphics.Shader/CodeGen/Spirv/SamplerDeclaration.cs
@@ -0,0 +1,27 @@
+using Spv.Generator;
+
+namespace Ryujinx.Graphics.Shader.CodeGen.Spirv
+{
+ readonly struct SamplerDeclaration
+ {
+ public readonly Instruction ImageType;
+ public readonly Instruction SampledImageType;
+ public readonly Instruction SampledImagePointerType;
+ public readonly Instruction Image;
+ public readonly bool IsIndexed;
+
+ public SamplerDeclaration(
+ Instruction imageType,
+ Instruction sampledImageType,
+ Instruction sampledImagePointerType,
+ Instruction image,
+ bool isIndexed)
+ {
+ ImageType = imageType;
+ SampledImageType = sampledImageType;
+ SampledImagePointerType = sampledImagePointerType;
+ Image = image;
+ IsIndexed = isIndexed;
+ }
+ }
+}