diff options
Diffstat (limited to 'tools/slang-unit-test/unit-test-com-host-callable.cpp')
| -rw-r--r-- | tools/slang-unit-test/unit-test-com-host-callable.cpp | 134 |
1 files changed, 82 insertions, 52 deletions
diff --git a/tools/slang-unit-test/unit-test-com-host-callable.cpp b/tools/slang-unit-test/unit-test-com-host-callable.cpp index 29bc91739..7fcc033b4 100644 --- a/tools/slang-unit-test/unit-test-com-host-callable.cpp +++ b/tools/slang-unit-test/unit-test-com-host-callable.cpp @@ -1,27 +1,25 @@ // unit-test-com-host-callable.cpp #include "../../source/core/slang-byte-encode-util.h" - -#include <stdio.h> -#include <stdlib.h> - -#include "tools/unit-test/slang-unit-test.h" - -#include "slang.h" +#include "../../source/core/slang-list.h" #include "slang-com-helper.h" #include "slang-com-ptr.h" +#include "slang.h" +#include "tools/unit-test/slang-unit-test.h" -#include "../../source/core/slang-list.h" +#include <stdio.h> +#include <stdlib.h> -namespace { // anonymous +namespace +{ // anonymous // Slang namespace is used for elements support code (like core) which we use here // for ComPtr<> and TestToolUtil using namespace Slang; -// For the moment we have to explicitly write the Slang COM interface in C++ code. It *MUST* match +// For the moment we have to explicitly write the Slang COM interface in C++ code. It *MUST* match // the interface in the slang source -// As it stands all interfaces need to derive from ISlangUnknown (or IUnknown). +// As it stands all interfaces need to derive from ISlangUnknown (or IUnknown). class IDoThings : public ISlangUnknown { public: @@ -50,20 +48,34 @@ class DoThings : public IDoThings { public: // We don't need queryInterface for this impl, or ref counting - virtual SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE { return SLANG_E_NOT_IMPLEMENTED; } + virtual SLANG_NO_THROW SlangResult SLANG_MCALL + queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE + { + return SLANG_E_NOT_IMPLEMENTED; + } virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; } virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; } // IDoThings - virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) SLANG_OVERRIDE { return a + b + 1; } - virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) SLANG_OVERRIDE { return (int)_calcHash(in); } + virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) SLANG_OVERRIDE + { + return a + b + 1; + } + virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) SLANG_OVERRIDE + { + return (int)_calcHash(in); + } }; class CountGood : public ICountGood { public: // We don't need queryInterface for this impl, or ref counting - virtual SLANG_NO_THROW SlangResult SLANG_MCALL queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE { return SLANG_E_NOT_IMPLEMENTED; } + virtual SLANG_NO_THROW SlangResult SLANG_MCALL + queryInterface(SlangUUID const& uuid, void** outObject) SLANG_OVERRIDE + { + return SLANG_E_NOT_IMPLEMENTED; + } virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() SLANG_OVERRIDE { return 1; } virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; } @@ -75,44 +87,50 @@ public: struct ComTestContext { - ComTestContext(UnitTestContext* context): - m_unitTestContext(context) + ComTestContext(UnitTestContext* context) + : m_unitTestContext(context) { slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession; - m_defaultCppCompiler = slangSession->getDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP); - - m_hostHostCallableCompiler = slangSession->getDownstreamCompilerForTransition(SLANG_CPP_SOURCE, SLANG_HOST_HOST_CALLABLE); - m_shaderHostCallableCompiler = slangSession->getDownstreamCompilerForTransition(SLANG_CPP_SOURCE, SLANG_SHADER_HOST_CALLABLE); + m_defaultCppCompiler = + slangSession->getDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP); + m_hostHostCallableCompiler = slangSession->getDownstreamCompilerForTransition( + SLANG_CPP_SOURCE, + SLANG_HOST_HOST_CALLABLE); + m_shaderHostCallableCompiler = slangSession->getDownstreamCompilerForTransition( + SLANG_CPP_SOURCE, + SLANG_SHADER_HOST_CALLABLE); } SlangResult runTests() { slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession; - // TODO(JS): - // Care is needed around this in normal testing. `slang-llvm` is whatever was asked for for when premake was built - // when the target is specified. Otherwise it is the `default` which is typically 64 bit during development. + // TODO(JS): + // Care is needed around this in normal testing. `slang-llvm` is whatever was asked for for + // when premake was built when the target is specified. Otherwise it is the `default` which + // is typically 64 bit during development. // - // On CI we should be okay, because it should download the correct `slang-llvm` for the build (as it packages up with it). - // But for normal development, that can easily not be the case (for example changing to 32 bit build in VS is a problem). + // On CI we should be okay, because it should download the correct `slang-llvm` for the + // build (as it packages up with it). But for normal development, that can easily not be the + // case (for example changing to 32 bit build in VS is a problem). // - // Make sure to run + // Make sure to run // // ``` // premake --arch=x86 --deps=true // ``` // // for the actual target/arch(!) - - const bool hasLlvm = SLANG_SUCCEEDED(slangSession->checkPassThroughSupport(SLANG_PASS_THROUGH_LLVM)); + + const bool hasLlvm = + SLANG_SUCCEEDED(slangSession->checkPassThroughSupport(SLANG_PASS_THROUGH_LLVM)); SlangPassThrough cppCompiler = SLANG_PASS_THROUGH_NONE; { - const SlangPassThrough cppCompilers[] = - { + const SlangPassThrough cppCompilers[] = { SLANG_PASS_THROUGH_VISUAL_STUDIO, SLANG_PASS_THROUGH_GCC, SLANG_PASS_THROUGH_CLANG, @@ -132,9 +150,15 @@ struct ComTestContext if (cppCompiler != SLANG_PASS_THROUGH_NONE) { slangSession->setDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP, cppCompiler); - - slangSession->setDownstreamCompilerForTransition(SLANG_CPP_SOURCE, SLANG_SHADER_HOST_CALLABLE, cppCompiler); - slangSession->setDownstreamCompilerForTransition(SLANG_CPP_SOURCE, SLANG_HOST_HOST_CALLABLE, cppCompiler); + + slangSession->setDownstreamCompilerForTransition( + SLANG_CPP_SOURCE, + SLANG_SHADER_HOST_CALLABLE, + cppCompiler); + slangSession->setDownstreamCompilerForTransition( + SLANG_CPP_SOURCE, + SLANG_HOST_HOST_CALLABLE, + cppCompiler); SLANG_RETURN_ON_FAIL(_runTest()); } @@ -157,14 +181,17 @@ struct ComTestContext slang::IGlobalSession* slangSession = m_unitTestContext->slangGlobalSession; slangSession->setDefaultDownstreamCompiler(SLANG_SOURCE_LANGUAGE_CPP, m_defaultCppCompiler); - slangSession->setDownstreamCompilerForTransition(SLANG_CPP_SOURCE, SLANG_SHADER_HOST_CALLABLE, m_shaderHostCallableCompiler); - slangSession->setDownstreamCompilerForTransition(SLANG_CPP_SOURCE, SLANG_HOST_HOST_CALLABLE, m_hostHostCallableCompiler); + slangSession->setDownstreamCompilerForTransition( + SLANG_CPP_SOURCE, + SLANG_SHADER_HOST_CALLABLE, + m_shaderHostCallableCompiler); + slangSession->setDownstreamCompilerForTransition( + SLANG_CPP_SOURCE, + SLANG_HOST_HOST_CALLABLE, + m_hostHostCallableCompiler); } - ~ComTestContext() - { - _reset(); - } + ~ComTestContext() { _reset(); } SlangResult _runTest(); @@ -186,9 +213,9 @@ SlangResult ComTestContext::_runTest() SLANG_ALLOW_DEPRECATED_END // We want to compile to 'HOST_CALLABLE' here such that we can execute the Slang code. - // - // Note that it is possible to use HOST_HOST_CALLABLE, but this currently only works with 'regular' C++ compilers - // not with `slang-llvm`. + // + // Note that it is possible to use HOST_HOST_CALLABLE, but this currently only works with + // 'regular' C++ compilers not with `slang-llvm`. const int targetIndex = request->addCodeGenTarget(SLANG_SHADER_HOST_CALLABLE); // Set the target flag to indicate that we want to compile all into a library. @@ -198,10 +225,13 @@ SlangResult ComTestContext::_runTest() request->setDebugInfoLevel(SLANG_DEBUG_INFO_LEVEL_STANDARD); // Add the translation unit - const int translationUnitIndex = request->addTranslationUnit(SLANG_SOURCE_LANGUAGE_SLANG, nullptr); + const int translationUnitIndex = + request->addTranslationUnit(SLANG_SOURCE_LANGUAGE_SLANG, nullptr); // Set the source file for the translation unit - request->addTranslationUnitSourceFile(translationUnitIndex, "tools/slang-unit-test/unit-test-com-host-callable.slang"); + request->addTranslationUnitSourceFile( + translationUnitIndex, + "tools/slang-unit-test/unit-test-com-host-callable.slang"); const SlangResult compileRes = request->compile(); @@ -214,8 +244,8 @@ SlangResult ComTestContext::_runTest() printf("%s", diagnostics); } - // Get the 'shared library' (note that this doesn't necessarily have to be implemented as a shared library - // it's just an interface to executable code). + // Get the 'shared library' (note that this doesn't necessarily have to be implemented as a + // shared library it's just an interface to executable code). ComPtr<ISlangSharedLibrary> sharedLibrary; SLANG_RETURN_ON_FAIL(request->getTargetHostCallable(0, sharedLibrary.writeRef())); @@ -305,7 +335,7 @@ SlangResult ComTestContext::_runTest() for (Index i = 0; i < 10; ++i) { SLANG_CHECK(*counterPtr == &counter); - + const auto v = nextCount(); SLANG_CHECK(v == i); } @@ -314,7 +344,7 @@ SlangResult ComTestContext::_runTest() return SLANG_OK; } -} // anonymous +} // namespace SLANG_UNIT_TEST(comHostCallable) { @@ -322,9 +352,9 @@ SLANG_UNIT_TEST(comHostCallable) // TODO(JS): // We can't currently run this test reliably on targets other than windows // Visual Studio DownstreamCompiler has support for 32 bit builds - // Other targets generally build for the native environment which is almost always 64 bit, + // Other targets generally build for the native environment which is almost always 64 bit, // and it requires other features to build/test 32 bit binaries on such systems. - // + // // So we disable for any 32 bit non MS target for now return; #endif @@ -332,6 +362,6 @@ SLANG_UNIT_TEST(comHostCallable) ComTestContext context(unitTestContext); const auto result = context.runTests(); - + SLANG_CHECK(SLANG_SUCCEEDED(result)); } |
