Skip to content

Commit f55bf33

Browse files
automagical shared memory sizing done
1 parent 4038a27 commit f55bf33

File tree

5 files changed

+32
-18
lines changed

5 files changed

+32
-18
lines changed

include/irr/builtin/glsl/ext/LumaMeter/common.glsl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,9 @@ uint irr_glsl_workgroupExclusiveAdd2(uint val)
287287
barrier();
288288
memoryBarrierShared();
289289
const bool propagateReduction = (gl_LocalInvocationIndex&loMask)==loMask;
290-
const uint subgroupSizeLog2 = uint(findLSB(irr_glsl_SubgroupSize));
291290
//uint firstLevelScan = INVCONV(FIRST_SUBGROUP_OP(false,VALUE));
292291
//uint lastInvocationInLevel = lastInvocation;
293-
const uint lowerIndex = gl_LocalInvocationIndex>>subgroupSizeLog2;
292+
const uint lowerIndex = gl_LocalInvocationIndex>>irr_glsl_SubgroupSizeLog2;
294293

295294
if (propagateReduction)
296295
_IRR_GLSL_SCRATCH_SHARED_DEFINED_[lowerIndex] = firstLevelScan;

include/irr/builtin/glsl/subgroup/basic_portability.glsl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,28 @@
88
#endif
99

1010

11-
#define irr_glsl_MaxWorkgroupSize 2048
11+
#define irr_glsl_MaxWorkgroupSizeLog2 11
12+
#define irr_glsl_MaxWorkgroupSize (0x1<<irr_glsl_MaxWorkgroupSizeLog2)
1213

1314

14-
#define irr_glsl_MinSubgroupSize 4
15-
#define irr_glsl_MaxSubgroupSize 128
15+
#define irr_glsl_MinSubgroupSizeLog2 2
16+
#define irr_glsl_MinSubgroupSize (0x1<<irr_glsl_MinSubgroupSizeLog2)
17+
18+
#define irr_glsl_MaxSubgroupSizeLog2 7
19+
#define irr_glsl_MaxSubgroupSize (0x1<<irr_glsl_MaxSubgroupSizeLog2)
1620

1721

1822
// TODO: define this properly from gl_SubgroupSize and available extensions
1923
#define IRR_GLSL_SUBGROUP_SIZE_IS_CONSTEXPR
20-
#define irr_glsl_SubgroupSize 4
24+
#define irr_glsl_SubgroupSizeLog2 2
25+
#define irr_glsl_SubgroupSize (0x1<<irr_glsl_SubgroupSizeLog2)
2126

2227

23-
#if IRR_GLSL_EVAL(irr_glsl_SubgroupSize)<IRR_GLSL_EVAL(irr_glsl_MinSubgroupSize)
28+
#if IRR_GLSL_EVAL(irr_glsl_SubgroupSizeLog2)<IRR_GLSL_EVAL(irr_glsl_MinSubgroupSizeLog2)
2429
#error "Something went very wrong when figuring out irr_glsl_SubgroupSize!"
2530
#endif
26-
#define irr_glsl_HalfSubgroupSize (irr_glsl_SubgroupSize>>1)
31+
#define irr_glsl_HalfSubgroupSizeLog2 (irr_glsl_SubgroupSizeLog2-1)
32+
#define irr_glsl_HalfSubgroupSize (0x1<<irr_glsl_HalfSubgroupSizeLog2)
2733

2834

2935
#endif

include/irr/builtin/glsl/subgroup/shared_arithmetic_portability.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
#if defined(IRR_GLSL_SUBGROUP_SIZE_IS_CONSTEXPR)
21-
#define IRR_GLSL_SUBGROUP_EMULATION_SCRATCH_BOUND(LAST_ITEM) ((((IRR_GLSL_EVAL(LAST_ITEM)&(-irr_glsl_SubgroupSize))<<1)|(IRR_GLSL_EVAL(LAST_ITEM)&irr_glsl_SubgroupSize))+(irr_glsl_SubgroupSize>>1)+1)
21+
#define IRR_GLSL_SUBGROUP_EMULATION_SCRATCH_BOUND(LAST_ITEM) ((((IRR_GLSL_EVAL(LAST_ITEM)&(-irr_glsl_SubgroupSize))<<1)|(IRR_GLSL_EVAL(LAST_ITEM)&irr_glsl_SubgroupSize))+irr_glsl_HalfSubgroupSize+1)
2222
#else
2323
#define IRR_GLSL_SUBGROUP_EMULATION_SCRATCH_BOUND(LAST_ITEM) ((IRR_GLSL_EVAL(LAST_ITEM)<<1)+(irr_glsl_MaxSubgroupSize>>1)+1)
2424
#endif

include/irr/builtin/glsl/workgroup/ballot.glsl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ uint irr_glsl_workgroupBallotFindMSB();
161161
uint firstLevelScan = INVCONV(INCLUSIVE_SUBGROUP_OP(false,VALUE)); \
162162
uint scan = firstLevelScan; \
163163
const bool possibleProp = pseudoSubgroupInvocation==loMask; \
164-
const uint subgroupSizeLog2 = findLSB(irr_glsl_SubgroupSize); \
165-
const uint pseudoSubgroupID = (gl_LocalInvocationIndex>>subgroupSizeLog2); \
164+
const uint pseudoSubgroupID = gl_LocalInvocationIndex>>irr_glsl_SubgroupSizeLog2; \
166165
const uint nextStoreIndex = irr_glsl_subgroup_getSubgroupEmulationMemoryStoreOffset(loMask,pseudoSubgroupID); \
167166
uint scanStoreIndex = irr_glsl_subgroup_getSubgroupEmulationMemoryStoreOffset(loMask,lastInvocation)+gl_LocalInvocationIndex+1u; \
168167
bool participate = gl_LocalInvocationIndex<=lastInvocationInLevel; \
@@ -175,7 +174,7 @@ uint irr_glsl_workgroupBallotFindMSB();
175174
_IRR_GLSL_SCRATCH_SHARED_DEFINED_[nextStoreIndex] = scan; \
176175
} \
177176
barrier(); \
178-
participate = gl_LocalInvocationIndex<=(lastInvocationInLevel>>=subgroupSizeLog2); \
177+
participate = gl_LocalInvocationIndex<=(lastInvocationInLevel>>=irr_glsl_SubgroupSizeLog2); \
179178
if (participate) \
180179
{ \
181180
const uint prevLevelScan = _IRR_GLSL_SCRATCH_SHARED_DEFINED_[subgroupScanStoreOffset]; \
@@ -193,7 +192,7 @@ uint irr_glsl_workgroupBallotFindMSB();
193192
_IRR_GLSL_SCRATCH_SHARED_DEFINED_[nextStoreIndex] = scan; \
194193
} \
195194
barrier(); \
196-
participate = gl_LocalInvocationIndex<=(lastInvocationInLevel>>=subgroupSizeLog2); \
195+
participate = gl_LocalInvocationIndex<=(lastInvocationInLevel>>=irr_glsl_SubgroupSizeLog2); \
197196
if (participate) \
198197
{ \
199198
const uint prevLevelScan = _IRR_GLSL_SCRATCH_SHARED_DEFINED_[subgroupScanStoreOffset]; \
@@ -208,7 +207,7 @@ uint irr_glsl_workgroupBallotFindMSB();
208207
uint scanLoadIndex = scanStoreIndex+irr_glsl_SubgroupSize; \
209208
const uint shiftedInvocationIndex = gl_LocalInvocationIndex+irr_glsl_SubgroupSize; \
210209
const uint currentToHighLevel = pseudoSubgroupID-shiftedInvocationIndex; \
211-
for (uint logShift=(findMSB(lastInvocation)/subgroupSizeLog2-1u)*subgroupSizeLog2; logShift>0u; logShift-=subgroupSizeLog2) \
210+
for (uint logShift=(findMSB(lastInvocation)/irr_glsl_SubgroupSizeLog2-1u)*irr_glsl_SubgroupSizeLog2; logShift>0u; logShift-=irr_glsl_SubgroupSizeLog2) \
212211
{ \
213212
lastInvocationInLevel = lastInvocation>>logShift; \
214213
barrier(); \

include/irr/builtin/glsl/workgroup/shared_ballot.glsl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@
1515

1616
#define IRR_GLSL_WORKGROUP_REDUCTION_SCRATCH_BOUND(LAST_ITEM) IRR_GLSL_EVAL(IRR_GLSL_SUBGROUP_EMULATION_SCRATCH_BOUND(LAST_ITEM))
1717

18-
// TODO: fuck!
18+
19+
#define IRR_GLSL_WORKGROUP_SCAN_SCRATCH_BOUND_IMPL(LAST_ITEM,SUBGROUP_SIZE_LOG2) (IRR_GLSL_WORKGROUP_REDUCTION_SCRATCH_BOUND(LAST_ITEM)+ \
20+
(LAST_ITEM>>(SUBGROUP_SIZE_LOG2))+ \
21+
(LAST_ITEM>>(SUBGROUP_SIZE_LOG2*2))+ \
22+
(LAST_ITEM>>(SUBGROUP_SIZE_LOG2*3))+ \
23+
(LAST_ITEM>>(SUBGROUP_SIZE_LOG2*4))+ \
24+
(LAST_ITEM>>(SUBGROUP_SIZE_LOG2*5))+ \
25+
5)
26+
1927
#if defined(IRR_GLSL_SUBGROUP_SIZE_IS_CONSTEXPR)
20-
#define IRR_GLSL_WORKGROUP_SCAN_SCRATCH_BOUND(LAST_ITEM) 2548
21-
//IRR_GLSL_WORKGROUP_REDUCTION_SCRATCH_BOUND(LAST_ITEM)+
28+
#if (irr_glsl_MaxWorkgroupSizeLog2-irr_glsl_SubgroupSizeLog2*6)>0
29+
#error "Someone updated irr_glsl_MaxWorkgroupSizeLog2 without letting us know"
30+
#endif
31+
#define IRR_GLSL_WORKGROUP_SCAN_SCRATCH_BOUND(LAST_ITEM) IRR_GLSL_WORKGROUP_SCAN_SCRATCH_BOUND_IMPL(LAST_ITEM,irr_glsl_SubgroupSizeLog2)
2232
#else
23-
#define IRR_GLSL_WORKGROUP_SCAN_SCRATCH_BOUND(LAST_ITEM) (IRR_GLSL_MAX(IRR_GLSL_WORKGROUP_REDUCTION_SCRATCH_BOUND(LAST_ITEM)+)
33+
#define IRR_GLSL_WORKGROUP_SCAN_SCRATCH_BOUND(LAST_ITEM) IRR_GLSL_WORKGROUP_SCAN_SCRATCH_BOUND_IMPL(LAST_ITEM,irr_glsl_MinSubgroupSizeLog2)
2434
#endif
2535

2636

0 commit comments

Comments
 (0)