1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
using Ryujinx.Graphics.Shader.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using static Ryujinx.Graphics.Shader.IntermediateRepresentation.OperandHelper;
namespace Ryujinx.Graphics.Shader.Translation
{
class HelperFunctionManager
{
private readonly List<Function> _functionList;
private readonly Dictionary<HelperFunctionName, int> _functionIds;
private readonly ShaderStage _stage;
public HelperFunctionManager(List<Function> functionList, ShaderStage stage)
{
_functionList = functionList;
_functionIds = new Dictionary<HelperFunctionName, int>();
_stage = stage;
}
public int GetOrCreateFunctionId(HelperFunctionName functionName)
{
if (_functionIds.TryGetValue(functionName, out int functionId))
{
return functionId;
}
Function function = GenerateFunction(functionName);
functionId = _functionList.Count;
_functionList.Add(function);
_functionIds.Add(functionName, functionId);
return functionId;
}
private Function GenerateFunction(HelperFunctionName functionName)
{
return functionName switch
{
HelperFunctionName.TexelFetchScale => GenerateTexelFetchScaleFunction(),
HelperFunctionName.TextureSizeUnscale => GenerateTextureSizeUnscaleFunction(),
_ => throw new ArgumentException($"Invalid function name {functionName}")
};
}
private Function GenerateTexelFetchScaleFunction()
{
EmitterContext context = new EmitterContext();
Operand input = Argument(0);
Operand samplerIndex = Argument(1);
Operand index = GetScaleIndex(context, samplerIndex);
Operand scale = context.Load(StorageKind.ConstantBuffer, 0, Const((int)SupportBufferField.RenderScale), index);
Operand scaleIsOne = context.FPCompareEqual(scale, ConstF(1f));
Operand lblScaleNotOne = Label();
context.BranchIfFalse(lblScaleNotOne, scaleIsOne);
context.Return(input);
context.MarkLabel(lblScaleNotOne);
int inArgumentsCount;
if (_stage == ShaderStage.Fragment)
{
Operand scaleIsLessThanZero = context.FPCompareLess(scale, ConstF(0f));
Operand lblScaleGreaterOrEqualZero = Label();
context.BranchIfFalse(lblScaleGreaterOrEqualZero, scaleIsLessThanZero);
Operand negScale = context.FPNegate(scale);
Operand inputScaled = context.FPMultiply(context.IConvertS32ToFP32(input), negScale);
Operand fragCoordX = context.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(0));
Operand fragCoordY = context.Load(StorageKind.Input, IoVariable.FragmentCoord, null, Const(1));
Operand fragCoord = context.ConditionalSelect(Argument(2), fragCoordY, fragCoordX);
Operand inputBias = context.FPModulo(fragCoord, negScale);
Operand inputWithBias = context.FPAdd(inputScaled, inputBias);
context.Return(context.FP32ConvertToS32(inputWithBias));
context.MarkLabel(lblScaleGreaterOrEqualZero);
inArgumentsCount = 3;
}
else
{
inArgumentsCount = 2;
}
Operand inputScaled2 = context.FPMultiply(context.IConvertS32ToFP32(input), scale);
context.Return(context.FP32ConvertToS32(inputScaled2));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "TexelFetchScale", true, inArgumentsCount, 0);
}
private Function GenerateTextureSizeUnscaleFunction()
{
EmitterContext context = new EmitterContext();
Operand input = Argument(0);
Operand samplerIndex = Argument(1);
Operand index = GetScaleIndex(context, samplerIndex);
Operand scale = context.FPAbsolute(context.Load(StorageKind.ConstantBuffer, 0, Const((int)SupportBufferField.RenderScale), index));
Operand scaleIsOne = context.FPCompareEqual(scale, ConstF(1f));
Operand lblScaleNotOne = Label();
context.BranchIfFalse(lblScaleNotOne, scaleIsOne);
context.Return(input);
context.MarkLabel(lblScaleNotOne);
Operand inputUnscaled = context.FPDivide(context.IConvertS32ToFP32(input), scale);
context.Return(context.FP32ConvertToS32(inputUnscaled));
return new Function(ControlFlowGraph.Create(context.GetOperations()).Blocks, "TextureSizeUnscale", true, 2, 0);
}
private Operand GetScaleIndex(EmitterContext context, Operand index)
{
switch (_stage)
{
case ShaderStage.Vertex:
Operand fragScaleCount = context.Load(StorageKind.ConstantBuffer, 0, Const((int)SupportBufferField.FragmentRenderScaleCount));
return context.IAdd(Const(1), context.IAdd(index, fragScaleCount));
default:
return context.IAdd(Const(1), index);
}
}
}
}
|