diff options
| author | Yong He <yonghe@outlook.com> | 2021-01-27 10:02:44 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-01-27 10:02:44 -0800 |
| commit | 615dfba810964bed1caad8ecb87850c8eb1544b4 (patch) | |
| tree | 3fefe8c801a15213ca16eb947ce305a5ed69c6fa /tools/gfx | |
| parent | a90c850735664e2928e4cc961442a126c6859b97 (diff) | |
Make own a slang session. (#1678)
Diffstat (limited to 'tools/gfx')
| -rw-r--r-- | tools/gfx/cuda/render-cuda.cpp | 11 | ||||
| -rw-r--r-- | tools/gfx/d3d11/render-d3d11.cpp | 2 | ||||
| -rw-r--r-- | tools/gfx/d3d12/render-d3d12.cpp | 3 | ||||
| -rw-r--r-- | tools/gfx/open-gl/render-gl.cpp | 2 | ||||
| -rw-r--r-- | tools/gfx/render-graphics-common.cpp | 7 | ||||
| -rw-r--r-- | tools/gfx/render-graphics-common.h | 5 | ||||
| -rw-r--r-- | tools/gfx/render.h | 38 | ||||
| -rw-r--r-- | tools/gfx/slang-context.h | 43 | ||||
| -rw-r--r-- | tools/gfx/vulkan/render-vk.cpp | 2 |
9 files changed, 107 insertions, 6 deletions
diff --git a/tools/gfx/cuda/render-cuda.cpp b/tools/gfx/cuda/render-cuda.cpp index d1e320224..057674550 100644 --- a/tools/gfx/cuda/render-cuda.cpp +++ b/tools/gfx/cuda/render-cuda.cpp @@ -6,6 +6,7 @@ #include "../renderer-shared.h" #include "../render-graphics-common.h" +#include "../slang-context.h" #ifdef GFX_ENABLE_CUDA #include <cuda.h> @@ -782,6 +783,7 @@ private: CUcontext m_context = nullptr; CUDAPipelineState* currentPipeline = nullptr; CUDARootShaderObject* currentRootObject = nullptr; + SlangContext slangContext; public: ~CUDARenderer() { @@ -792,6 +794,8 @@ private: } virtual SLANG_NO_THROW SlangResult SLANG_MCALL initialize(const Desc& desc, void* inWindowHandle) override { + SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_PTX, "sm_5_1")); + SLANG_RETURN_ON_FAIL(_initCuda(reportType)); SLANG_RETURN_ON_FAIL(_findMaxFlopsDeviceIndex(&m_deviceIndex)); @@ -809,6 +813,13 @@ private: return SLANG_OK; } + virtual SLANG_NO_THROW Result SLANG_MCALL getSlangSession(slang::ISession** outSlangSession) override + { + *outSlangSession = slangContext.session.get(); + slangContext.session->addRef(); + return SLANG_OK; + } + virtual SLANG_NO_THROW Result SLANG_MCALL createTextureResource( IResource::Usage initialUsage, const ITextureResource::Desc& desc, diff --git a/tools/gfx/d3d11/render-d3d11.cpp b/tools/gfx/d3d11/render-d3d11.cpp index 40e617a1f..3df5e25bc 100644 --- a/tools/gfx/d3d11/render-d3d11.cpp +++ b/tools/gfx/d3d11/render-d3d11.cpp @@ -664,6 +664,8 @@ static bool _isSupportedNVAPIOp(IUnknown* dev, uint32_t op) SlangResult D3D11Renderer::initialize(const Desc& desc, void* inWindowHandle) { + SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_DXBC, "sm_5_0")); + auto windowHandle = (HWND)inWindowHandle; m_desc = desc; diff --git a/tools/gfx/d3d12/render-d3d12.cpp b/tools/gfx/d3d12/render-d3d12.cpp index 648fc3052..407f0dbf2 100644 --- a/tools/gfx/d3d12/render-d3d12.cpp +++ b/tools/gfx/d3d12/render-d3d12.cpp @@ -1348,7 +1348,10 @@ static bool _isSupportedNVAPIOp(ID3D12Device* dev, uint32_t op) Result D3D12Renderer::initialize(const Desc& desc, void* inWindowHandle) { + SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_DXBC, "sm_5_1")); + m_hwnd = (HWND)inWindowHandle; + // Rather than statically link against D3D, we load it dynamically. HMODULE d3dModule = LoadLibraryA("d3d12.dll"); diff --git a/tools/gfx/open-gl/render-gl.cpp b/tools/gfx/open-gl/render-gl.cpp index 49fd6b005..80edbdceb 100644 --- a/tools/gfx/open-gl/render-gl.cpp +++ b/tools/gfx/open-gl/render-gl.cpp @@ -785,6 +785,8 @@ void GLRenderer::destroyBindingEntries(const BindingState::Desc& desc, const Bin SLANG_NO_THROW Result SLANG_MCALL GLRenderer::initialize(const Desc& desc, void* inWindowHandle) { + SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_GLSL, "sm_5_0")); + auto windowHandle = (HWND)inWindowHandle; m_desc = desc; diff --git a/tools/gfx/render-graphics-common.cpp b/tools/gfx/render-graphics-common.cpp index 593d4708c..adc943d1d 100644 --- a/tools/gfx/render-graphics-common.cpp +++ b/tools/gfx/render-graphics-common.cpp @@ -1372,6 +1372,13 @@ SLANG_NO_THROW bool SLANG_MCALL gfx::GraphicsAPIRenderer::hasFeature(const char* return m_features.findFirstIndex([&](Slang::String x) { return x == featureName; }) != -1; } +SLANG_NO_THROW Result SLANG_MCALL gfx::GraphicsAPIRenderer::getSlangSession(slang::ISession** outSlangSession) +{ + *outSlangSession = slangContext.session.get(); + slangContext.session->addRef(); + return SLANG_OK; +} + GraphicsCommonShaderProgram::~GraphicsCommonShaderProgram() { // Note: It might not seem like this destructor is needed at all, since diff --git a/tools/gfx/render-graphics-common.h b/tools/gfx/render-graphics-common.h index 3a07f2993..f4d9567e3 100644 --- a/tools/gfx/render-graphics-common.h +++ b/tools/gfx/render-graphics-common.h @@ -2,9 +2,11 @@ #include "tools/gfx/render.h" #include "core/slang-basic.h" +#include "tools/gfx/slang-context.h" namespace gfx { + class GraphicsCommonProgramLayout; class GraphicsCommonShaderProgram : public IShaderProgram, public Slang::RefObject @@ -32,6 +34,8 @@ public: virtual SLANG_NO_THROW Result SLANG_MCALL getFeatures( const char** outFeatures, UInt bufferSize, UInt* outFeatureCount) SLANG_OVERRIDE; virtual SLANG_NO_THROW bool SLANG_MCALL hasFeature(const char* featureName) SLANG_OVERRIDE; + virtual SLANG_NO_THROW Result SLANG_MCALL getSlangSession(slang::ISession** outSlangSession) SLANG_OVERRIDE; + virtual SLANG_NO_THROW Result SLANG_MCALL createShaderObjectLayout( slang::TypeLayoutReflection* typeLayout, IShaderObjectLayout** outLayout) SLANG_OVERRIDE; virtual SLANG_NO_THROW Result SLANG_MCALL @@ -51,6 +55,7 @@ public: protected: Slang::List<Slang::String> m_features; + SlangContext slangContext; }; struct GfxGUID diff --git a/tools/gfx/render.h b/tools/gfx/render.h index 4c3b71645..325bab4ee 100644 --- a/tools/gfx/render.h +++ b/tools/gfx/render.h @@ -1085,15 +1085,32 @@ struct Viewport class IRenderer: public ISlangUnknown { public: + struct SlangDesc + { + slang::IGlobalSession* slangGlobalSession = nullptr; // (optional) A slang global session object. If null will create automatically. + + SlangMatrixLayoutMode defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_ROW_MAJOR; + + char const* const* searchPaths = nullptr; + SlangInt searchPathCount = 0; + + slang::PreprocessorMacroDesc const* preprocessorMacros = nullptr; + SlangInt preprocessorMacroCount = 0; + + const char* targetProfile = nullptr; // (optional) Target shader profile. If null this will be set to platform dependent default. + SlangFloatingPointMode floatingPointMode = SLANG_FLOATING_POINT_MODE_DEFAULT; + SlangOptimizationLevel optimizationLevel = SLANG_OPTIMIZATION_LEVEL_DEFAULT; + }; struct Desc { - int width = 0; ///< Width in pixels - int height = 0; ///< height in pixels - const char* adapter = nullptr; ///< Name to identify the adapter to use - int requiredFeatureCount = 0; ///< Number of required features. - const char** requiredFeatures = nullptr; ///< Array of required feature names, whose size is `requiredFeatureCount`. - int nvapiExtnSlot = -1; ///< The slot (typically UAV) used to identify NVAPI intrinsics. If >=0 NVAPI is required. + int width = 0; // Width in pixels + int height = 0; // height in pixels + const char* adapter = nullptr; // Name to identify the adapter to use + int requiredFeatureCount = 0; // Number of required features. + const char** requiredFeatures = nullptr; // Array of required feature names, whose size is `requiredFeatureCount`. + int nvapiExtnSlot = -1; // The slot (typically UAV) used to identify NVAPI intrinsics. If >=0 NVAPI is required. + SlangDesc slang = {}; // Configurations for Slang. }; // Will return with SLANG_E_NOT_AVAILABLE if NVAPI can't be initialized and nvapiExtnSlot >= 0 @@ -1104,6 +1121,15 @@ public: /// Returns a list of features supported by the renderer. virtual SLANG_NO_THROW Result SLANG_MCALL getFeatures(const char** outFeatures, UInt bufferSize, UInt* outFeatureCount) = 0; + virtual SLANG_NO_THROW Result SLANG_MCALL getSlangSession(slang::ISession** outSlangSession) = 0; + + inline ComPtr<slang::ISession> getSlangSession() + { + ComPtr<slang::ISession> result; + getSlangSession(result.writeRef()); + return result; + } + virtual SLANG_NO_THROW void SLANG_MCALL setClearColor(const float color[4]) = 0; virtual SLANG_NO_THROW void SLANG_MCALL clearFrame() = 0; diff --git a/tools/gfx/slang-context.h b/tools/gfx/slang-context.h new file mode 100644 index 000000000..e6168deed --- /dev/null +++ b/tools/gfx/slang-context.h @@ -0,0 +1,43 @@ +#pragma once + +#include "tools/gfx/render.h" + +namespace gfx +{ + class SlangContext + { + public: + Slang::ComPtr<slang::IGlobalSession> globalSession; + Slang::ComPtr<slang::ISession> session; + Result initialize(const gfx::IRenderer::SlangDesc& desc, SlangCompileTarget compileTarget, const char* defaultProfileName) + { + if (desc.slangGlobalSession) + { + globalSession = desc.slangGlobalSession; + } + else + { + SLANG_RETURN_ON_FAIL(slang::createGlobalSession(globalSession.writeRef())); + } + + slang::SessionDesc slangSessionDesc = {}; + slangSessionDesc.defaultMatrixLayoutMode = desc.defaultMatrixLayoutMode; + slangSessionDesc.searchPathCount = desc.searchPathCount; + slangSessionDesc.searchPaths = desc.searchPaths; + slangSessionDesc.preprocessorMacroCount = desc.preprocessorMacroCount; + slangSessionDesc.preprocessorMacros = desc.preprocessorMacros; + slang::TargetDesc targetDesc = {}; + targetDesc.format = compileTarget; + auto targetProfile = desc.targetProfile; + if (targetProfile == nullptr) + targetProfile = defaultProfileName; + targetDesc.profile = globalSession->findProfile(targetProfile); + targetDesc.optimizationLevel = desc.optimizationLevel; + targetDesc.floatingPointMode = desc.floatingPointMode; + slangSessionDesc.targetCount = 1; + slangSessionDesc.targets = &targetDesc; + SLANG_RETURN_ON_FAIL(globalSession->createSession(slangSessionDesc, session.writeRef())); + return SLANG_OK; + } + }; +} diff --git a/tools/gfx/vulkan/render-vk.cpp b/tools/gfx/vulkan/render-vk.cpp index 5afe3e3b4..21034d167 100644 --- a/tools/gfx/vulkan/render-vk.cpp +++ b/tools/gfx/vulkan/render-vk.cpp @@ -1033,6 +1033,8 @@ VkPipelineShaderStageCreateInfo VKRenderer::compileEntryPoint( SlangResult VKRenderer::initialize(const Desc& desc, void* inWindowHandle) { + SLANG_RETURN_ON_FAIL(slangContext.initialize(desc.slang, SLANG_SPIRV, "sm_5_1")); + SLANG_RETURN_ON_FAIL(m_module.init()); SLANG_RETURN_ON_FAIL(m_api.initGlobalProcs(m_module)); |
