yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
13dd01489
master
1#pragma once 2 3#include "core/slang-basic.h" 4#include "core/slang-io.h" 5#include "platform/window.h" 6#include "slang-rhi.h" 7#include "test-base.h" 8 9#ifdef _WIN32 10void _Win32OutputDebugString (const char * str ); 11#endif 12 13#define SLANG_STRINGIFY (x ) #x 14#define SLANG_EXPAND_STRINGIFY (x ) SLANG_STRINGIFY(x) 15 16#ifdef _WIN32 17#define EXAMPLE_MAIN (innerMain ) \ 18 extern const char* const g_logFileName = \ 19 "log-" SLANG_EXPAND_STRINGIFY(SLANG_EXAMPLE_NAME) ".txt"; \ 20 PLATFORM_UI_MAIN(innerMain); 21 22#else 23#define EXAMPLE_MAIN (innerMain ) PLATFORM_UI_MAIN(innerMain) 24#endif // _WIN32 25 26static const float kIdentity []= {1 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,0 ,1 }; 27 28struct WindowedAppBase :public TestBase 29{ 30protected : 31static const int kSwapchainImageCount = 2 ; 32 33Slang ::RefPtr < platform ::Window > gWindow ; 34uint32_t windowWidth ; 35uint32_t windowHeight ; 36 37Slang ::ComPtr < rhi ::IDevice > gDevice ; 38Slang ::ComPtr < rhi ::ICommandQueue > gQueue ; 39Slang ::ComPtr < rhi ::ISurface > gSurface ; 40 41Slang ::List < Slang ::ComPtr < rhi ::ITexture >>gOfflineTextures ; 42 43Slang ::Result initializeBase ( 44const char * title , 45int width , 46int height , 47rhi ::DeviceType deviceType = rhi ::DeviceType ::Default ); 48 49void mainLoop (); 50 51Slang ::ComPtr < rhi ::ITextureView > createTextureFromFile ( 52Slang ::String fileName , 53int & textureWidth , 54int & textureHeight ); 55 56void createOfflineTextures (); 57 58virtual void windowSizeChanged (); 59 60protected : 61virtual void renderFrame (rhi ::ITexture * texture )= 0 ; 62 63public : 64platform ::Window * getWindow () {return gWindow .Ptr (); } 65virtual void finalize () {gQueue -> waitOnHost (); } 66void offlineRender (); 67}; 68 69struct ExampleResources 70{ 71Slang ::String baseDir ; 72 73ExampleResources (const Slang ::String & dir ) 74 :baseDir (dir ) 75 { 76 } 77 78Slang ::String resolveResource (const char * fileName )const 79 { 80static const Slang ::List < Slang ::String > directories { 81"examples" , 82 "../examples ", 83 "../../examples ", 84 }; 85 86for (const Slang ::String & dir :directories ) 87 { 88Slang ::StringBuilder pathSb ; 89pathSb <<dir <<"/" <<baseDir <<"/" <<fileName ; 90if (Slang ::File ::exists (pathSb .getBuffer ())) 91return pathSb .toString (); 92 } 93 94return fileName ; 95 } 96}; 97 98int64_t getCurrentTime (); 99int64_t getTimerFrequency (); 100 101template < typename ...TArgs > 102inline void reportError (const char * format ,TArgs ...args ) 103{ 104printf (format ,std ::forward < TArgs > (args )...); 105#ifdef _WIN32 106char buffer [ 4096 ]; 107sprintf_s ( buffer , format , std :: forward < TArgs >( args )...); 108_Win32OutputDebugString ( buffer ); 109#endif 110} 111 112template < typename ... TArgs > 113inline void log ( const char * format , TArgs ... args ) 114{ 115reportError ( format , args ...); 116} 117 118// Many Slang API functions return detailed diagnostic information 119// (error messages, warnings, etc.) as a "blob" of data, or return 120// a null blob pointer instead if there were no issues. 121// 122// For convenience, we define a subroutine that will dump the information 123// in a diagnostic blob if one is produced, and skip it otherwise. 124// 125inline void diagnoseIfNeeded ( slang :: IBlob * diagnosticsBlob ) 126{ 127if ( diagnosticsBlob != nullptr ) 128{ 129reportError ( "%s" , ( const char *) diagnosticsBlob -> getBufferPointer ()); 130} 131} 132 133template < typename TApp > 134int innerMain ( int argc , char ** argv ) 135{ 136TApp app ; 137 138app . parseOption ( argc , argv ); 139 140if ( app . shouldShowHelp ()) 141{ 142app . printUsage ( argc > 0 ? argv [ 0 ] : "example" ); 143return 0 ; 144} 145 146if ( SLANG_FAILED ( app . initialize ())) 147{ 148return -1 ; 149} 150 151if (! app . isTestMode ()) 152{ 153platform :: Application :: run ( app . getWindow ()); 154} 155else 156{ 157app . offlineRender (); 158} 159 160app . finalize (); 161return 0 ; 162}