From af27de01532904508e6a630c213249e93fdd1c66 Mon Sep 17 00:00:00 2001 From: jarcherNV Date: Fri, 15 Aug 2025 13:16:09 -0700 Subject: Add static functions to create blobs from data (#8179) Add helper functions to create ISlangBlob and load module data from source. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- .../unit-test-load-module-from-source.cpp | 268 +++++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 tools/slang-unit-test/unit-test-load-module-from-source.cpp (limited to 'tools/slang-unit-test/unit-test-load-module-from-source.cpp') diff --git a/tools/slang-unit-test/unit-test-load-module-from-source.cpp b/tools/slang-unit-test/unit-test-load-module-from-source.cpp new file mode 100644 index 000000000..41c32211b --- /dev/null +++ b/tools/slang-unit-test/unit-test-load-module-from-source.cpp @@ -0,0 +1,268 @@ +// unit-test-load-module-from-source.cpp + +#include "slang-com-ptr.h" +#include "slang.h" +#include "unit-test/slang-unit-test.h" + +#include +#include + +using namespace Slang; + +// Test the loadModuleFromSource method and slang_loadModuleFromSource function +SLANG_UNIT_TEST(loadModuleFromSource) +{ + // Test source code with various content + const char* testSource = R"( + [shader("compute")] + [numthreads(1,1,1)] + void computeMain(uint3 workGroup : SV_GroupID) + { + // Simple compute shader + } + )"; + + size_t sourceSize = strlen(testSource); + + ComPtr globalSession; + SLANG_CHECK(slang_createGlobalSession(SLANG_API_VERSION, globalSession.writeRef()) == SLANG_OK); + + slang::SessionDesc sessionDesc = {}; + sessionDesc.targetCount = 1; + slang::TargetDesc targetDesc = {}; + targetDesc.format = SLANG_HLSL; + targetDesc.profile = globalSession->findProfile("sm_5_0"); + sessionDesc.targets = &targetDesc; + + ComPtr session; + SLANG_CHECK(globalSession->createSession(sessionDesc, session.writeRef()) == SLANG_OK); + + // Test 1: Test the method version (existing functionality) + { + ComPtr module; + ComPtr diagnostics; + + // Use loadModuleFromSourceString which takes a string directly + module = session->loadModuleFromSourceString( + "testModule", + "test.slang", + testSource, + diagnostics.writeRef()); + + SLANG_CHECK(module != nullptr); + if (diagnostics) + { + // If there are diagnostics, they should be warnings or errors + SLANG_CHECK(diagnostics->getBufferSize() > 0); + } + } + + // Test 2: Test the new slang_loadModuleFromSource function + { + ComPtr module; + ComPtr diagnostics; + + module = slang_loadModuleFromSource( + session, + "testModule2", + "test2.slang", + testSource, + sourceSize, + diagnostics.writeRef()); + + SLANG_CHECK(module != nullptr); + if (diagnostics) + { + // If there are diagnostics, they should be warnings or errors + SLANG_CHECK(diagnostics->getBufferSize() > 0); + } + } + + // Test 3: Test with invalid parameters + { + ComPtr module; + ComPtr diagnostics; + + // Test with null session + module = slang_loadModuleFromSource( + nullptr, + "testModule", + "test.slang", + testSource, + sourceSize, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + + // Test with null moduleName + module = slang_loadModuleFromSource( + session, + nullptr, + "test.slang", + testSource, + sourceSize, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + + // Test with null path + module = slang_loadModuleFromSource( + session, + "testModule", + nullptr, + testSource, + sourceSize, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + } + + // Test 4: Test with null source and non-zero size (should fail) + { + ComPtr module; + ComPtr diagnostics; + + module = slang_loadModuleFromSource( + session, + "testModule", + "test.slang", + nullptr, + 10, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + } + + // Test 5: Test with complex source code + { + const char* complexSource = R"( + [shader("compute")] + [numthreads(8,8,1)] + void computeMain(uint3 workGroup : SV_GroupID, uint3 localID : SV_GroupThreadID) + { + uint2 pixelPos = workGroup.xy * uint2(8,8) + localID.xy; + + // Simple computation + float result = sin(pixelPos.x * 0.1f) * cos(pixelPos.y * 0.1f); + + // Store result (in a real shader, this would go to a buffer) + // outputBuffer[pixelPos] = result; + } + )"; + + size_t complexSourceSize = strlen(complexSource); + + ComPtr module; + ComPtr diagnostics; + + module = slang_loadModuleFromSource( + session, + "complexModule", + "complex.slang", + complexSource, + complexSourceSize, + diagnostics.writeRef()); + + SLANG_CHECK(module != nullptr); + } + + // Test 6: Test IR blob functions with invalid parameters + { + ComPtr module; + ComPtr diagnostics; + const char* testData = "test data"; + size_t testDataSize = strlen(testData); + + // Test with null session + module = slang_loadModuleFromIRBlob( + nullptr, + "testModule", + "test.slang", + testData, + testDataSize, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + + // Test with null moduleName + module = slang_loadModuleFromIRBlob( + session, + nullptr, + "test.slang", + testData, + testDataSize, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + + // Test with null path + module = slang_loadModuleFromIRBlob( + session, + "testModule", + nullptr, + testData, + testDataSize, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + + // Test with null source + module = slang_loadModuleFromIRBlob( + session, + "testModule", + "test.slang", + nullptr, + testDataSize, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + + // Test with zero size + module = slang_loadModuleFromIRBlob( + session, + "testModule", + "test.slang", + testData, + 0, + diagnostics.writeRef()); + + SLANG_CHECK(module == nullptr); + + // Test loadModuleInfoFromIRBlob with null session + SlangInt moduleVersion; + const char* moduleCompilerVersion; + const char* moduleName; + + SlangResult infoResult = slang_loadModuleInfoFromIRBlob( + nullptr, + testData, + testDataSize, + moduleVersion, + moduleCompilerVersion, + moduleName); + + SLANG_CHECK(infoResult == SLANG_E_INVALID_ARG); + + // Test loadModuleInfoFromIRBlob with null source + infoResult = slang_loadModuleInfoFromIRBlob( + session, + nullptr, + testDataSize, + moduleVersion, + moduleCompilerVersion, + moduleName); + + SLANG_CHECK(infoResult == SLANG_E_INVALID_ARG); + + // Test loadModuleInfoFromIRBlob with zero size + infoResult = slang_loadModuleInfoFromIRBlob( + session, + testData, + 0, + moduleVersion, + moduleCompilerVersion, + moduleName); + + SLANG_CHECK(infoResult == SLANG_E_INVALID_ARG); + } +} -- cgit v1.2.3