|
| 1 | +#include "core/slang-blob.h" |
| 2 | +#include "gfx-test-util.h" |
| 3 | +#include "slang-gfx.h" |
| 4 | +#include "unit-test/slang-unit-test.h" |
| 5 | + |
| 6 | +using namespace gfx; |
| 7 | + |
| 8 | +namespace gfx_test |
| 9 | +{ |
| 10 | + |
| 11 | +static void diagnoseIfNeeded(Slang::ComPtr<slang::IBlob>& diagnosticsBlob) |
| 12 | +{ |
| 13 | + if (diagnosticsBlob && diagnosticsBlob->getBufferSize() > 0) |
| 14 | + { |
| 15 | + fprintf(stderr, "%s\n", (const char*)diagnosticsBlob->getBufferPointer()); |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +static Slang::Result loadSpirvProgram( |
| 20 | + gfx::IDevice* device, |
| 21 | + Slang::ComPtr<gfx::IShaderProgram>& outShaderProgram, |
| 22 | + slang::ProgramLayout*& slangReflection) |
| 23 | +{ |
| 24 | + // main.slang: declares the interface and extern struct S, and the vertex shader. |
| 25 | + const char* mainSrc = R"( |
| 26 | + public interface IFoo |
| 27 | + { |
| 28 | + public float4 getFoo(); |
| 29 | + }; |
| 30 | + public extern struct S : IFoo; |
| 31 | +
|
| 32 | + [shader("vertex")] |
| 33 | + float4 vertexMain(S params) : SV_Position |
| 34 | + { |
| 35 | + return params.getFoo(); |
| 36 | + } |
| 37 | + )"; |
| 38 | + |
| 39 | + // foo.slang: defines S with its field layout and its implementation of getFoo(). |
| 40 | + const char* fooSrc = R"( |
| 41 | + import main; |
| 42 | +
|
| 43 | + export public struct S : IFoo |
| 44 | + { |
| 45 | + public float4 getFoo() { return this.foo; } |
| 46 | + float4 foo; |
| 47 | + } |
| 48 | + )"; |
| 49 | + |
| 50 | + Slang::ComPtr<slang::ISession> slangSession; |
| 51 | + SLANG_RETURN_ON_FAIL(device->getSlangSession(slangSession.writeRef())); |
| 52 | + Slang::ComPtr<slang::IBlob> diagnosticsBlob; |
| 53 | + |
| 54 | + // Create blobs for the two modules. |
| 55 | + auto mainBlob = Slang::UnownedRawBlob::create(mainSrc, strlen(mainSrc)); |
| 56 | + auto fooBlob = Slang::UnownedRawBlob::create(fooSrc, strlen(fooSrc)); |
| 57 | + |
| 58 | + // Load modules from source. |
| 59 | + slang::IModule* mainModule = slangSession->loadModuleFromSource("main", "main.slang", mainBlob); |
| 60 | + slang::IModule* fooModule = slangSession->loadModuleFromSource("foo", "foo.slang", fooBlob); |
| 61 | + |
| 62 | + // Find the entry point from main.slang |
| 63 | + Slang::ComPtr<slang::IEntryPoint> vsEntryPoint; |
| 64 | + SLANG_RETURN_ON_FAIL(mainModule->findEntryPointByName("vertexMain", vsEntryPoint.writeRef())); |
| 65 | + |
| 66 | + // Compose the program from both modules and the entry point. |
| 67 | + Slang::List<slang::IComponentType*> componentTypes; |
| 68 | + componentTypes.add(mainModule); |
| 69 | + componentTypes.add(fooModule); |
| 70 | + componentTypes.add(vsEntryPoint); |
| 71 | + |
| 72 | + Slang::ComPtr<slang::IComponentType> composedProgram; |
| 73 | + SLANG_RETURN_ON_FAIL(slangSession->createCompositeComponentType( |
| 74 | + componentTypes.getBuffer(), |
| 75 | + componentTypes.getCount(), |
| 76 | + composedProgram.writeRef(), |
| 77 | + diagnosticsBlob.writeRef())); |
| 78 | + diagnoseIfNeeded(diagnosticsBlob); |
| 79 | + |
| 80 | + // Link the composite program. |
| 81 | + Slang::ComPtr<slang::IComponentType> linkedProgram; |
| 82 | + SLANG_RETURN_ON_FAIL( |
| 83 | + composedProgram->link(linkedProgram.writeRef(), diagnosticsBlob.writeRef())); |
| 84 | + diagnoseIfNeeded(diagnosticsBlob); |
| 85 | + |
| 86 | + // Retrieve the reflection information. |
| 87 | + composedProgram = linkedProgram; |
| 88 | + slangReflection = composedProgram->getLayout(); |
| 89 | + |
| 90 | + // Create a shader program that will generate SPIRV code. |
| 91 | + gfx::IShaderProgram::Desc programDesc = {}; |
| 92 | + programDesc.slangGlobalScope = composedProgram.get(); |
| 93 | + auto shaderProgram = device->createProgram(programDesc); |
| 94 | + outShaderProgram = shaderProgram; |
| 95 | + |
| 96 | + // Force SPIRV generation by explicitly requesting it |
| 97 | + Slang::ComPtr<slang::IBlob> spirvBlob; |
| 98 | + Slang::ComPtr<slang::IBlob> spirvDiagnostics; |
| 99 | + |
| 100 | + // Request SPIRV code generation for the vertex shader entry point |
| 101 | + auto targetIndex = 0; // Assuming this is the first/only target |
| 102 | + auto entryPointIndex = 0; // Assuming this is the first/only entry point |
| 103 | + |
| 104 | + auto result = composedProgram->getEntryPointCode( |
| 105 | + entryPointIndex, |
| 106 | + targetIndex, |
| 107 | + spirvBlob.writeRef(), |
| 108 | + spirvDiagnostics.writeRef()); |
| 109 | + |
| 110 | + if (SLANG_FAILED(result)) |
| 111 | + { |
| 112 | + if (spirvDiagnostics && spirvDiagnostics->getBufferSize() > 0) |
| 113 | + { |
| 114 | + fprintf( |
| 115 | + stderr, |
| 116 | + "SPIRV generation failed: %s\n", |
| 117 | + (const char*)spirvDiagnostics->getBufferPointer()); |
| 118 | + } |
| 119 | + return result; |
| 120 | + } |
| 121 | + |
| 122 | + // Verify we actually got SPIRV code |
| 123 | + if (!spirvBlob || spirvBlob->getBufferSize() == 0) |
| 124 | + { |
| 125 | + return SLANG_FAIL; |
| 126 | + } |
| 127 | + |
| 128 | + return SLANG_OK; |
| 129 | +} |
| 130 | + |
| 131 | +// Function to validate the type layout of struct S |
| 132 | +static void validateStructSLayout(UnitTestContext* context, slang::ProgramLayout* slangReflection) |
| 133 | +{ |
| 134 | + // Check reflection is available |
| 135 | + SLANG_CHECK(slangReflection != nullptr); |
| 136 | + |
| 137 | + // Get the entry point layout for vertexMain |
| 138 | + auto entryPointCount = slangReflection->getEntryPointCount(); |
| 139 | + slang::EntryPointLayout* entryPointLayout = nullptr; |
| 140 | + |
| 141 | + for (unsigned int i = 0; i < entryPointCount; i++) |
| 142 | + { |
| 143 | + auto currentEntryPoint = slangReflection->getEntryPointByIndex(i); |
| 144 | + const char* name = currentEntryPoint->getName(); |
| 145 | + |
| 146 | + if (strcmp(name, "vertexMain") == 0) |
| 147 | + { |
| 148 | + entryPointLayout = currentEntryPoint; |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + SLANG_CHECK_MSG(entryPointLayout != nullptr, "Could not find vertexMain entry point"); |
| 154 | + |
| 155 | + // Get the parameter count for the entry point |
| 156 | + auto paramCount = entryPointLayout->getParameterCount(); |
| 157 | + SLANG_CHECK_MSG(paramCount >= 1, "Entry point has no parameters"); |
| 158 | + |
| 159 | + // Get the first parameter, which should be of type S |
| 160 | + auto paramLayout = entryPointLayout->getParameterByIndex(0); |
| 161 | + SLANG_CHECK_MSG(paramLayout != nullptr, "Could not get first parameter layout"); |
| 162 | + |
| 163 | + // Get the type layout of the parameter |
| 164 | + auto typeLayout = paramLayout->getTypeLayout(); |
| 165 | + SLANG_CHECK_MSG(typeLayout != nullptr, "Parameter has no type layout"); |
| 166 | + |
| 167 | + // Check if it's a struct type |
| 168 | + auto kind = typeLayout->getKind(); |
| 169 | + SLANG_CHECK_MSG(kind == slang::TypeReflection::Kind::Struct, "Parameter is not a struct type"); |
| 170 | + |
| 171 | + // Get the field count |
| 172 | + auto fieldCount = typeLayout->getFieldCount(); |
| 173 | + SLANG_CHECK_MSG(fieldCount >= 1, "Struct has no fields"); |
| 174 | + |
| 175 | + // Check for the 'foo' field |
| 176 | + bool foundFooField = false; |
| 177 | + for (unsigned int i = 0; i < fieldCount; i++) |
| 178 | + { |
| 179 | + auto fieldLayout = typeLayout->getFieldByIndex(i); |
| 180 | + const char* fieldName = fieldLayout->getName(); |
| 181 | + |
| 182 | + if (fieldName && strcmp(fieldName, "foo") == 0) |
| 183 | + { |
| 184 | + foundFooField = true; |
| 185 | + |
| 186 | + // Check that it's a float4 type |
| 187 | + auto fieldTypeLayout = fieldLayout->getTypeLayout(); |
| 188 | + auto fieldTypeKind = fieldTypeLayout->getKind(); |
| 189 | + |
| 190 | + SLANG_CHECK_MSG( |
| 191 | + fieldTypeKind == slang::TypeReflection::Kind::Vector, |
| 192 | + "Field 'foo' is not a vector type"); |
| 193 | + |
| 194 | + auto elementCount = fieldTypeLayout->getElementCount(); |
| 195 | + SLANG_CHECK_MSG(elementCount == 4, "Field 'foo' is not a 4-element vector"); |
| 196 | + |
| 197 | + break; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + SLANG_CHECK_MSG(foundFooField, "Could not find field 'foo' in struct S"); |
| 202 | +} |
| 203 | + |
| 204 | +void linkTimeTypeLayoutImpl(gfx::IDevice* device, UnitTestContext* context) |
| 205 | +{ |
| 206 | + Slang::ComPtr<gfx::IShaderProgram> shaderProgram; |
| 207 | + slang::ProgramLayout* slangReflection = nullptr; |
| 208 | + |
| 209 | + auto result = loadSpirvProgram(device, shaderProgram, slangReflection); |
| 210 | + SLANG_CHECK(SLANG_SUCCEEDED(result)); |
| 211 | + |
| 212 | + // Validate the struct S layout |
| 213 | + validateStructSLayout(context, slangReflection); |
| 214 | + |
| 215 | + // Create a graphics pipeline to verify SPIRV code generation works |
| 216 | + GraphicsPipelineStateDesc pipelineDesc = {}; |
| 217 | + pipelineDesc.program = shaderProgram.get(); |
| 218 | + |
| 219 | + // We need to set up a minimal pipeline state for a vertex shader |
| 220 | + pipelineDesc.primitiveType = PrimitiveType::Triangle; |
| 221 | + |
| 222 | + ComPtr<gfx::IPipelineState> pipelineState; |
| 223 | + auto pipelineResult = |
| 224 | + device->createGraphicsPipelineState(pipelineDesc, pipelineState.writeRef()); |
| 225 | + SLANG_CHECK(SLANG_SUCCEEDED(pipelineResult)); |
| 226 | +} |
| 227 | + |
| 228 | +// |
| 229 | +// This test verifies that type layout information correctly propagates through |
| 230 | +// the Slang compilation pipeline when types are defined in modules other than where they are used. |
| 231 | +// Specifically, it tests |
| 232 | +// that when using an extern struct that's defined in a separate module: |
| 233 | +// |
| 234 | +// 1. The struct definition is properly linked across module boundaries |
| 235 | +// 2. The complete type layout information is available in the reflection data |
| 236 | +// 3. SPIRV code generation succeeds with the linked type information (this |
| 237 | +// failed before when layout information was required during code generation) |
| 238 | +// |
| 239 | + |
| 240 | +SLANG_UNIT_TEST(linkTimeTypeLayout) |
| 241 | +{ |
| 242 | + runTestImpl(linkTimeTypeLayoutImpl, unitTestContext, Slang::RenderApiFlag::Vulkan); |
| 243 | +} |
| 244 | + |
| 245 | +} // namespace gfx_test |
0 commit comments