|
| 1 | +package io.github.refux.slang; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.nio.ByteBuffer; |
| 8 | +import java.nio.ByteOrder; |
| 9 | +import java.nio.charset.StandardCharsets; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.Set; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +/** |
| 15 | + * Covers the slang-java 0.0.4 addition: {@link Session#getDynamicObjectRTTIBytes}, the CPU's source of |
| 16 | + * truth for the 16-byte header written at the start of each element of a |
| 17 | + * {@code StructuredBuffer<Interface>} in dynamic-dispatch shaders. |
| 18 | + */ |
| 19 | +class Slang0_0_4Test { |
| 20 | + |
| 21 | + private static final String MODIFIERS = """ |
| 22 | + public interface IModifier { float apply(float x); } |
| 23 | + public struct Doubler : IModifier { public float apply(float x) { return x * 2.0; } } |
| 24 | + public struct Negator : IModifier { public float apply(float x) { return -x; } } |
| 25 | + """; |
| 26 | + |
| 27 | + private static final String DISPATCH_SHADER = """ |
| 28 | + import modifiers; |
| 29 | + RWStructuredBuffer<float> result; |
| 30 | + StructuredBuffer<IModifier> gModifiers; |
| 31 | + [shader("compute")] [numthreads(1,1,1)] |
| 32 | + void main(uint3 tid : SV_DispatchThreadID) { |
| 33 | + float v = 1.0; |
| 34 | + for (uint i = 0; i < 2; ++i) { v = gModifiers[i].apply(v); } |
| 35 | + result[tid.x] = v; |
| 36 | + } |
| 37 | + """; |
| 38 | + |
| 39 | + /** |
| 40 | + * The header is a fixed 16 bytes: byte 0-3 is Slang's valid-type marker (1) and byte 8-11 is the |
| 41 | + * conformance id. {@link Session#getDynamicObjectRTTIBytes} returns the **registration-order** |
| 42 | + * (sequential) id, while the shader's dispatch {@code OpSwitch} branches on the |
| 43 | + * {@code conformanceIdOverride} — so they agree only when the override equals the registration |
| 44 | + * order, which is how a caller must register (id 0 first, id 1 next, …). Here the ids match the |
| 45 | + * order, so the header id is an OpSwitch case and a CPU writing this header dispatches correctly. |
| 46 | + */ |
| 47 | + @Test |
| 48 | + void headerCarriesTheMarkerAndTheIdTheDispatchSwitchUses() { |
| 49 | + try (GlobalSession global = Slang.createGlobalSession(); |
| 50 | + Session session = global.newSession() |
| 51 | + .target(CompileTarget.SPIRV_ASM, t -> t.profile("spirv_1_5")) |
| 52 | + .create()) { |
| 53 | + Module modifiers = session.loadModuleFromSource("modifiers", MODIFIERS); |
| 54 | + Module shader = session.loadModuleFromSource("shader", DISPATCH_SHADER); |
| 55 | + ShaderReflection refl = modifiers.layout(0); |
| 56 | + TypeReflection iface = refl.findTypeByName("IModifier"); |
| 57 | + TypeReflection doubler = refl.findTypeByName("Doubler"); |
| 58 | + TypeReflection negator = refl.findTypeByName("Negator"); |
| 59 | + assertNotNull(iface); |
| 60 | + assertNotNull(doubler); |
| 61 | + assertNotNull(negator); |
| 62 | + |
| 63 | + // Register in id order (0 then 1), the only regime in which the header id matches the switch. |
| 64 | + try (TypeConformance c0 = session.createTypeConformance(doubler, iface, 0); |
| 65 | + TypeConformance c1 = session.createTypeConformance(negator, iface, 1); |
| 66 | + ComponentType linked = session.composite(shader, shader.entryPoint("main"), c0, c1) |
| 67 | + .link()) { |
| 68 | + byte[] doublerHeader = session.getDynamicObjectRTTIBytes(doubler, iface); |
| 69 | + byte[] negatorHeader = session.getDynamicObjectRTTIBytes(negator, iface); |
| 70 | + |
| 71 | + assertEquals(16, doublerHeader.length, "RTTI header is 16 bytes"); |
| 72 | + assertEquals(1, intAt(doublerHeader, 0), "byte 0-3 is the valid-type marker"); |
| 73 | + assertEquals(0, intAt(doublerHeader, 8), "Doubler's id (0) sits at offset 8"); |
| 74 | + assertEquals(1, intAt(negatorHeader, 0), "byte 0-3 is the valid-type marker"); |
| 75 | + assertEquals(1, intAt(negatorHeader, 8), "Negator's id (1) sits at offset 8"); |
| 76 | + |
| 77 | + String asm = new String(linked.entryPointCode(0, 0), StandardCharsets.UTF_8); |
| 78 | + Set<Integer> switchCases = opSwitchCaseLiterals(asm); |
| 79 | + assertTrue(switchCases.contains(0), "header id 0 is an OpSwitch case; cases=" + switchCases); |
| 80 | + assertTrue(switchCases.contains(1), "header id 1 is an OpSwitch case; cases=" + switchCases); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Mimics the sprite composer: two stages on different interfaces, compiled separately in one |
| 87 | + * session, each registering its own modifiers from id 0. If the sequential id were session-global |
| 88 | + * rather than per-interface, stage B's header id (from getDynamicObjectRTTIBytes) would drift off |
| 89 | + * the per-stage id its OpSwitch branches on. This proves it does not. |
| 90 | + */ |
| 91 | + @Test |
| 92 | + void headerIdIsPerInterfaceAcrossSeparateCompilesInOneSession() { |
| 93 | + String stageA = """ |
| 94 | + public interface IA { float apply(float x); } |
| 95 | + public struct A0 : IA { public float apply(float x) { return x + 1.0; } } |
| 96 | + public struct A1 : IA { public float apply(float x) { return x + 2.0; } } |
| 97 | + """; |
| 98 | + String stageB = """ |
| 99 | + public interface IB { float apply(float x); } |
| 100 | + public struct B0 : IB { public float apply(float x) { return x * 3.0; } } |
| 101 | + public struct B1 : IB { public float apply(float x) { return x * 4.0; } } |
| 102 | + """; |
| 103 | + String shaderA = """ |
| 104 | + import a; |
| 105 | + RWStructuredBuffer<float> r; |
| 106 | + StructuredBuffer<IA> g; |
| 107 | + [shader("compute")] [numthreads(1,1,1)] |
| 108 | + void main(uint3 t : SV_DispatchThreadID) { float v = 1.0; for (uint i = 0; i < 2; ++i) v = g[i].apply(v); r[t.x] = v; } |
| 109 | + """; |
| 110 | + String shaderB = """ |
| 111 | + import b; |
| 112 | + RWStructuredBuffer<float> r; |
| 113 | + StructuredBuffer<IB> g; |
| 114 | + [shader("compute")] [numthreads(1,1,1)] |
| 115 | + void main(uint3 t : SV_DispatchThreadID) { float v = 1.0; for (uint i = 0; i < 2; ++i) v = g[i].apply(v); r[t.x] = v; } |
| 116 | + """; |
| 117 | + try (GlobalSession global = Slang.createGlobalSession(); |
| 118 | + Session session = global.newSession() |
| 119 | + .target(CompileTarget.SPIRV_ASM, t -> t.profile("spirv_1_5")) |
| 120 | + .create()) { |
| 121 | + Module a = session.loadModuleFromSource("a", stageA); |
| 122 | + Module b = session.loadModuleFromSource("b", stageB); |
| 123 | + Module sa = session.loadModuleFromSource("sa", shaderA); |
| 124 | + Module sb = session.loadModuleFromSource("sb", shaderB); |
| 125 | + ShaderReflection ra = a.layout(0); |
| 126 | + ShaderReflection rb = b.layout(0); |
| 127 | + TypeReflection ia = ra.findTypeByName("IA"); |
| 128 | + TypeReflection ib = rb.findTypeByName("IB"); |
| 129 | + |
| 130 | + // Stage A first: two conformances (ids 0, 1) — this advances any session-global counter. |
| 131 | + try (TypeConformance ca0 = session.createTypeConformance(ra.findTypeByName("A0"), ia, 0); |
| 132 | + TypeConformance ca1 = session.createTypeConformance(ra.findTypeByName("A1"), ia, 1); |
| 133 | + ComponentType la = session.composite(sa, sa.entryPoint("main"), ca0, ca1) |
| 134 | + .link()) { |
| 135 | + la.entryPointCode(0, 0); |
| 136 | + } |
| 137 | + |
| 138 | + // Stage B in the same session: its modifiers registered from id 0 again (per-interface). |
| 139 | + try (TypeConformance cb0 = session.createTypeConformance(rb.findTypeByName("B0"), ib, 0); |
| 140 | + TypeConformance cb1 = session.createTypeConformance(rb.findTypeByName("B1"), ib, 1); |
| 141 | + ComponentType lb = session.composite(sb, sb.entryPoint("main"), cb0, cb1) |
| 142 | + .link()) { |
| 143 | + int b0Id = intAt(session.getDynamicObjectRTTIBytes(rb.findTypeByName("B0"), ib), 8); |
| 144 | + Set<Integer> switchCasesB = |
| 145 | + opSwitchCaseLiterals(new String(lb.entryPointCode(0, 0), StandardCharsets.UTF_8)); |
| 146 | + assertEquals(0, b0Id, "stage B's first modifier is id 0 (per-interface, not session-global)"); |
| 147 | + assertTrue( |
| 148 | + switchCasesB.contains(b0Id), |
| 149 | + "B0 header id " + b0Id + " is a stage-B OpSwitch case; cases=" + switchCasesB); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private static int intAt(byte[] bytes, int offset) { |
| 155 | + return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getInt(offset); |
| 156 | + } |
| 157 | + |
| 158 | + // The integer case labels of every OpSwitch in the SPIR-V assembly. Format: |
| 159 | + // OpSwitch %selector %default <lit> %target <lit> %target ... |
| 160 | + private static Set<Integer> opSwitchCaseLiterals(String asm) { |
| 161 | + Set<Integer> cases = new HashSet<>(); |
| 162 | + for (String line : asm.split("\n")) { |
| 163 | + int at = line.indexOf("OpSwitch"); |
| 164 | + if (at < 0) { |
| 165 | + continue; |
| 166 | + } |
| 167 | + String[] tokens = line.substring(at).trim().split("\\s+"); |
| 168 | + for (int i = 3; i + 1 < tokens.length; i += 2) { |
| 169 | + try { |
| 170 | + cases.add(Integer.parseInt(tokens[i])); |
| 171 | + } catch (NumberFormatException notALiteral) { |
| 172 | + // A comment token or non-integer operand — skip. |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + return cases; |
| 177 | + } |
| 178 | +} |
0 commit comments