yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
13dd01489
master
1#include "core/slang-basic.h" 2#include "examples/example-base/example-base.h" 3#include "platform/window.h" 4#include "slang-com-ptr.h" 5#include "slang-rhi.h" 6#include "slang.h" 7 8#include <cstdio> 9 10using namespace rhi ; 11using namespace Slang ; 12 13struct PlatformTest :public WindowedAppBase 14{ 15 16void onSizeChanged () 17 { 18printf ("onSizeChanged\n" ); 19fflush (stdout ); 20 } 21 22void onFocus () 23 { 24printf ("onFocus\n" ); 25fflush (stdout ); 26 } 27 28void onLostFocus () 29 { 30printf ("onLostFocus\n" ); 31fflush (stdout ); 32 } 33 34void onKeyDown (platform::KeyEventArgs args ) 35 { 36printf ("onKeyDown(key=0x%02x, buttons=0x%02x)\n" , (uint32_t )args .key ,args .buttons ); 37fflush (stdout ); 38 } 39 40void onKeyUp (platform::KeyEventArgs args ) 41 { 42printf ("onKeyUp(key=0x%02x, buttons=0x%02x)\n" , (uint32_t )args .key ,args .buttons ); 43fflush (stdout ); 44 } 45 46void onKeyPress (platform::KeyEventArgs args ) 47 { 48printf ("onKeyPress(keyChar=0x%02x)\n" ,args .keyChar ); 49fflush (stdout ); 50 } 51 52void onMouseMove (platform::MouseEventArgs args ) 53 { 54// Throttle mouse move events using a simple counter 55static int mouseMoveCounter = 0 ; 56mouseMoveCounter ++ ; 57if (mouseMoveCounter %50 == 0 )// Only print every 50th mouse move event 58 { 59printf ( 60"onMouseMove(x=%d, y=%d, delta=%d, buttons=0x%02x)\n" , 61args .x , 62args .y , 63args .delta , 64args .buttons ); 65fflush (stdout ); 66 } 67 } 68 69void onMouseDown (platform::MouseEventArgs args ) 70 { 71printf ( 72"onMouseDown(x=%d, y=%d, delta=%d, buttons=0x%02x)\n" , 73args .x , 74args .y , 75args .delta , 76args .buttons ); 77fflush (stdout ); 78 } 79 80void onMouseUp (platform::MouseEventArgs args ) 81 { 82printf ( 83"onMouseUp(x=%d, y=%d, delta=%d, buttons=0x%02x)\n" , 84args .x , 85args .y , 86args .delta , 87args .buttons ); 88fflush (stdout ); 89 } 90 91void onMouseWheel (platform::MouseEventArgs args ) 92 { 93printf ( 94"onMouseWheel(x=%d, y=%d, delta=%d, buttons=0x%02x\n" , 95args .x , 96args .y , 97args .delta , 98args .buttons ); 99fflush (stdout ); 100 } 101 102Slang ::Result initialize () 103 { 104SLANG_RETURN_ON_FAIL (initializeBase ("platform-test" ,1024 ,768 ,getDeviceType ())); 105 106// We may not have a window if we're running in test mode 107SLANG_ASSERT (isTestMode ()|| gWindow ); 108if (gWindow ) 109 { 110printf ("Setting up event handlers...\n" ); 111fflush (stdout ); 112 113gWindow -> events .sizeChanged = [this ]() {onSizeChanged (); }; 114gWindow -> events .focus = [this ]() {onFocus (); }; 115gWindow -> events .lostFocus = [this ]() {onLostFocus (); }; 116gWindow -> events .keyDown = [this ](const platform::KeyEventArgs & e ) {onKeyDown (e ); }; 117gWindow -> events .keyUp = [this ](const platform::KeyEventArgs & e ) {onKeyUp (e ); }; 118gWindow -> events .keyPress = [this ](const platform::KeyEventArgs & e ) {onKeyPress (e ); }; 119gWindow -> events .mouseMove = [this ](const platform::MouseEventArgs & e ) 120 {onMouseMove (e ); }; 121gWindow -> events .mouseDown = [this ](const platform::MouseEventArgs & e ) 122 {onMouseDown (e ); }; 123gWindow -> events .mouseUp = [this ](const platform::MouseEventArgs & e ) {onMouseUp (e ); }; 124gWindow -> events .mouseWheel = [this ](const platform::MouseEventArgs & e ) 125 {onMouseWheel (e ); }; 126printf ("Event handlers set up successfully.\n" ); 127 } 128else 129 { 130printf ("No window available for event setup.\n" ); 131fflush (stdout ); 132 } 133 134return SLANG_OK ; 135 } 136 137virtual void renderFrame (ITexture * texture )override 138 { 139auto commandEncoder = gQueue -> createCommandEncoder (); 140 141ComPtr < ITextureView > textureView = gDevice -> createTextureView (texture , {}); 142RenderPassColorAttachment colorAttachment = {}; 143colorAttachment .view = textureView ; 144colorAttachment .loadOp = LoadOp ::Clear ; 145 146RenderPassDesc renderPass = {}; 147renderPass .colorAttachments = & colorAttachment ; 148renderPass .colorAttachmentCount = 1 ; 149 150auto renderEncoder = commandEncoder -> beginRenderPass (renderPass ); 151 152RenderState renderState = {}; 153renderState .viewports [0 ]= Viewport ::fromSize (windowWidth ,windowHeight ); 154renderState .viewportCount = 1 ; 155renderState .scissorRects [0 ]= ScissorRect ::fromSize (windowWidth ,windowHeight ); 156renderState .scissorRectCount = 1 ; 157 158renderEncoder -> setRenderState (renderState ); 159 160renderEncoder -> end (); 161gQueue -> submit (commandEncoder -> finish ()); 162 163if (!isTestMode ()) 164 { 165gSurface -> present (); 166 } 167 } 168}; 169 170// This macro instantiates an appropriate main function to 171// run the application defined above. 172EXAMPLE_MAIN (innerMain < PlatformTest > );