Skip to content

Commit bd50f99

Browse files
Make fvk-invert-y work on mesh shader ouptuts. (#5760)
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
1 parent 46aee66 commit bd50f99

File tree

2 files changed

+62
-29
lines changed

2 files changed

+62
-29
lines changed

source/slang/slang-ir-vk-invert-y.cpp

+38-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "slang-ir-vk-invert-y.h"
22

33
#include "slang-ir-insts.h"
4+
#include "slang-ir-util.h"
45
#include "slang-ir.h"
56

67
namespace Slang
@@ -28,37 +29,45 @@ void invertYOfPositionOutput(IRModule* module)
2829
{
2930
// Find all loads and stores to it.
3031
IRBuilder builder(module);
31-
traverseUses(
32-
globalInst,
33-
[&](IRUse* use)
32+
List<IRUse*> useWorkList;
33+
auto processUse = [&](IRUse* use)
34+
{
35+
if (auto store = as<IRStore>(use->getUser()))
3436
{
35-
if (auto store = as<IRStore>(use->getUser()))
36-
{
37-
if (store->getPtr() != globalInst)
38-
return;
37+
if (getRootAddr(store->getPtr()) != globalInst)
38+
return;
3939

40-
builder.setInsertBefore(store);
41-
auto originalVal = store->getVal();
42-
auto invertedVal = _invertYOfVector(builder, originalVal);
43-
builder.replaceOperand(&store->val, invertedVal);
44-
}
45-
else if (auto load = as<IRLoad>(use->getUser()))
46-
{
47-
// Since we negate the y coordinate before writing
48-
// to gl_Position, we also need to negate the value after reading from it.
49-
builder.setInsertAfter(load);
50-
// Store existing uses of the load that we are going to replace with
51-
// inverted val later.
52-
List<IRUse*> oldUses;
53-
for (auto loadUse = load->firstUse; loadUse; loadUse = loadUse->nextUse)
54-
oldUses.add(loadUse);
55-
// Get the inverted vector.
56-
auto invertedVal = _invertYOfVector(builder, load);
57-
// Replace original uses with the invertex vector.
58-
for (auto loadUse : oldUses)
59-
builder.replaceOperand(loadUse, invertedVal);
60-
}
61-
});
40+
builder.setInsertBefore(store);
41+
auto originalVal = store->getVal();
42+
auto invertedVal = _invertYOfVector(builder, originalVal);
43+
builder.replaceOperand(&store->val, invertedVal);
44+
}
45+
else if (auto load = as<IRLoad>(use->getUser()))
46+
{
47+
// Since we negate the y coordinate before writing
48+
// to gl_Position, we also need to negate the value after reading from it.
49+
builder.setInsertAfter(load);
50+
// Store existing uses of the load that we are going to replace with
51+
// inverted val later.
52+
List<IRUse*> oldUses;
53+
for (auto loadUse = load->firstUse; loadUse; loadUse = loadUse->nextUse)
54+
oldUses.add(loadUse);
55+
// Get the inverted vector.
56+
auto invertedVal = _invertYOfVector(builder, load);
57+
// Replace original uses with the invertex vector.
58+
for (auto loadUse : oldUses)
59+
builder.replaceOperand(loadUse, invertedVal);
60+
}
61+
else if (auto getElementPtr = as<IRGetElementPtr>(use->getUser()))
62+
{
63+
traverseUses(getElementPtr, [&](IRUse* use) { useWorkList.add(use); });
64+
}
65+
};
66+
traverseUses(globalInst, processUse);
67+
for (Index i = 0; i < useWorkList.getCount(); i++)
68+
{
69+
processUse(useWorkList[i]);
70+
}
6271
}
6372
}
6473
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//TEST:SIMPLE(filecheck=CHECK): -target spirv -fvk-invert-y
2+
3+
// CHECK: %[[CONST:[0-9]+]] = OpConstantComposite %v4float %float_0 %float_n1 %float_0 %float_0
4+
// CHECK: OpStore {{.*}} %[[CONST]]
5+
6+
struct PS_IN
7+
{
8+
float4 vPositionPs : SV_Position;
9+
};
10+
11+
[ shader("mesh") ]
12+
[ outputtopology( "line" ) ]
13+
[ numthreads( 64, 1, 1 ) ]
14+
void main(
15+
uint nThreadId : SV_DispatchThreadID,
16+
uint nGroupThreadId : SV_GroupThreadID,
17+
uint nGroupId : SV_GroupID,
18+
out vertices PS_IN outputVerts[ 128 ],
19+
out indices uint2 outputIB[ 64 ] )
20+
{
21+
SetMeshOutputCounts( 128, 64 );
22+
23+
outputVerts[ 2 * nGroupThreadId ].vPositionPs = float4( 0, 1, 0, 0 );
24+
}

0 commit comments

Comments
 (0)