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
|
#include "slang-ir-vk-invert-y.h"
#include "slang-ir-insts.h"
#include "slang-ir-util.h"
#include "slang-ir.h"
namespace Slang
{
static IRInst* _invertYOfVector(IRBuilder& builder, IRInst* originalVector)
{
auto vectorType = as<IRVectorType>(originalVector->getDataType());
SLANG_ASSERT(vectorType);
UInt elementIndexY = 1;
auto originalY =
builder.emitSwizzle(vectorType->getElementType(), originalVector, 1, &elementIndexY);
auto negY = builder.emitNeg(originalY->getDataType(), originalY);
auto newVal =
builder
.emitSwizzleSet(originalVector->getDataType(), originalVector, negY, 1, &elementIndexY);
return newVal;
}
// Find outputs to SV_Position and invert the y coordinates of it right before the write.
void invertYOfPositionOutput(IRModule* module)
{
for (auto globalInst : module->getGlobalInsts())
{
if (globalInst->findDecoration<IRGLPositionOutputDecoration>())
{
// Find all loads and stores to it.
IRBuilder builder(module);
List<IRUse*> useWorkList;
auto processUse = [&](IRUse* use)
{
if (auto store = as<IRStore>(use->getUser()))
{
if (getRootAddr(store->getPtr()) != globalInst)
return;
builder.setInsertBefore(store);
auto originalVal = store->getVal();
auto invertedVal = _invertYOfVector(builder, originalVal);
builder.replaceOperand(&store->val, invertedVal);
}
else if (auto load = as<IRLoad>(use->getUser()))
{
// Since we negate the y coordinate before writing
// to gl_Position, we also need to negate the value after reading from it.
builder.setInsertAfter(load);
// Store existing uses of the load that we are going to replace with
// inverted val later.
List<IRUse*> oldUses;
for (auto loadUse = load->firstUse; loadUse; loadUse = loadUse->nextUse)
oldUses.add(loadUse);
// Get the inverted vector.
auto invertedVal = _invertYOfVector(builder, load);
// Replace original uses with the invertex vector.
for (auto loadUse : oldUses)
builder.replaceOperand(loadUse, invertedVal);
}
else if (auto getElementPtr = as<IRGetElementPtr>(use->getUser()))
{
traverseUses(getElementPtr, [&](IRUse* use) { useWorkList.add(use); });
}
};
traverseUses(globalInst, processUse);
for (Index i = 0; i < useWorkList.getCount(); i++)
{
processUse(useWorkList[i]);
}
}
}
}
static IRInst* _invertWOfVector(IRBuilder& builder, IRInst* originalVector)
{
auto vectorType = as<IRVectorType>(originalVector->getDataType());
SLANG_ASSERT(vectorType);
UInt elementIndexW = 3;
auto originalW =
builder.emitSwizzle(vectorType->getElementType(), originalVector, 1, &elementIndexW);
auto rcpW = builder.emitDiv(
originalW->getDataType(),
builder.getFloatValue(originalW->getDataType(), 1.0),
originalW);
auto newVal =
builder
.emitSwizzleSet(originalVector->getDataType(), originalVector, rcpW, 1, &elementIndexW);
return newVal;
}
// Find inputs of SV_Position and rcp the w coordinates of it right after the read.
void rcpWOfPositionInput(IRModule* module)
{
for (auto globalInst : module->getGlobalInsts())
{
if (globalInst->findDecoration<IRGLPositionInputDecoration>())
{
// Find all loads and replace them with reciprocals.
IRBuilder builder(module);
traverseUses(
globalInst,
[&](IRUse* use)
{
// Get the inverted vector.
auto user = use->getUser();
if (user->getOp() == kIROp_Load)
{
builder.setInsertBefore(user);
auto val = builder.emitLoad(globalInst);
auto invertedVal = _invertWOfVector(builder, val);
user->replaceUsesWith(invertedVal);
user->removeAndDeallocate();
}
});
}
}
}
} // namespace Slang
|