yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
aefd1e3e0
master
1// slang-support.h 2#pragma once 3 4#include "core/slang-std-writers.h" 5#include "options.h" 6#include "shader-input-layout.h" 7#include "slang.h" 8 9#include <slang-rhi.h> 10 11namespace renderer_test 12{ 13 14/// Bridge from core debug callback to RHI debug callback 15/// This allows core callbacks to receive messages from RHI systems 16/// TODO: We should replace rhi::IDebugCallback with Slang::IDebugCallback. 17class CoreToRHIDebugBridge :public rhi ::IDebugCallback 18{ 19public : 20void setCoreCallback (Slang ::IDebugCallback * coreCallback ) {m_coreCallback = coreCallback ; } 21 22virtual SLANG_NO_THROW void SLANG_MCALL handleMessage ( 23rhi ::DebugMessageType type , 24rhi ::DebugMessageSource source , 25const char * message )override 26 { 27if (m_coreCallback ) 28 { 29// Convert RHI types to core types 30Slang ::DebugMessageType coreType = static_cast < Slang ::DebugMessageType > (type ); 31Slang ::DebugMessageSource coreSource = static_cast < Slang ::DebugMessageSource > (source ); 32m_coreCallback -> handleMessage (coreType ,coreSource ,message ); 33 } 34 } 35 36private : 37Slang ::IDebugCallback * m_coreCallback = nullptr ; 38}; 39 40/// Core debug callback that captures debug messages in a string buffer 41class CoreDebugCallback :public Slang ::IDebugCallback 42{ 43public : 44virtual SLANG_NO_THROW void SLANG_MCALL handleMessage ( 45Slang ::DebugMessageType type , 46Slang ::DebugMessageSource source , 47const char * message )override 48 { 49SLANG_UNUSED (source ); 50 51// Only capture error messages 52if (type == Slang ::DebugMessageType ::Error ) 53 { 54m_buf <<message ; 55if (message [strlen (message )- 1 ]!= '\n' ) 56 { 57m_buf <<'\n' ; 58 } 59 } 60 } 61 62void clear () {m_buf .clear (); } 63Slang ::String getString () {return m_buf .toString (); } 64 65private : 66Slang ::StringBuilder m_buf ; 67}; 68 69struct ShaderCompileRequest 70{ 71struct SourceInfo 72 { 73char const * path ; 74 75// The data may either be source text (in which 76// case it can be assumed to be nul-terminated with 77// `dataEnd` pointing at the terminator), or 78// raw binary data (in which case `dataEnd` points 79// at the end of the buffer). 80char const * dataBegin ; 81char const * dataEnd ; 82 }; 83 84struct EntryPoint 85 { 86char const * name = nullptr; 87SlangStage slangStage ; 88 }; 89 90struct TypeConformance 91 { 92public : 93Slang ::String derivedTypeName ; 94Slang ::String baseTypeName ; 95Slang ::Int idOverride ; 96 }; 97 98SourceInfo source ; 99Slang ::List < EntryPoint > entryPoints ; 100 101Slang ::List < Slang ::String > globalSpecializationArgs ; 102Slang ::List < Slang ::String > entryPointSpecializationArgs ; 103Slang ::List < TypeConformance > typeConformances ; 104}; 105 106 107struct ShaderCompilerUtil 108{ 109struct Input 110 { 111SlangCompileTarget target ; 112SlangSourceLanguage sourceLanguage ; 113SlangPassThrough passThrough ; 114Slang ::String profile ; 115 }; 116 117struct Output 118 { 119void set (slang ::IComponentType * slangProgram ); 120void reset (); 121 ~Output () {reset (); } 122 123ComPtr < slang ::IComponentType > slangProgram ; 124ShaderProgramDesc desc = {}; 125 126ComPtr < slang ::ISession > m_session = nullptr ; 127 128slang :: IGlobalSession * globalSession = nullptr ; 129}; 130 131struct OutputAndLayout 132{ 133Output output ; 134ShaderInputLayout layout ; 135Slang :: String sourcePath ; 136}; 137 138// Wrapper for compileProgram 139static SlangResult compileWithLayout ( 140slang :: IGlobalSession * globalSession , 141const Options & options , 142const ShaderCompilerUtil :: Input & input , 143OutputAndLayout & output ); 144}; 145 146 147} // namespace renderer_test