From 43d0c2100ef1a5df4b54525e50eb29fe7c39ec16 Mon Sep 17 00:00:00 2001 From: Gangzheng Tong Date: Tue, 8 Jul 2025 23:44:56 -0700 Subject: Convert gfx unit tests and examples to use slang-rhi (#7577) * Port first gfx unit test to slang-rhi * port triangle example to use slang-rhi * port platform-test to slang-rhi * Update platform-test to throttle mouse move events * port gpu-printing example to use slang-rhi * port model-viewer example to use slang-rhi * port ray-tracing example to use slang-rhi * port ray-tracing pipeline example to use slang-rhi * port reflection parameter blocks example to use slang-rhi * port shader-object example to use slang-rhi * port shader-toy example to use slang-rhi * Port most of tests to slang-rhi * port link-time-constant-array-size to use slang-rhi * Fix tests and find matching tests in slang-rhi * port autodiff-texture * remove gfx target; port nv-aftermath-example * update include path for shader-cursor.h * Disabled 2 more ported tests * fix build error * remove gfx test * put slang-rhi (static-lib) before slang (shared) * format code (#7621) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> * add debug callback * format code (#7649) Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> * Address review comments; revert back to use SLANG_CHECK_MSG --------- Co-authored-by: slangbot Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> --- examples/platform-test/main.cpp | 115 ++++++++++++++++++++++++++++------------ 1 file changed, 81 insertions(+), 34 deletions(-) (limited to 'examples/platform-test') diff --git a/examples/platform-test/main.cpp b/examples/platform-test/main.cpp index 865e4eab7..7949d90c1 100644 --- a/examples/platform-test/main.cpp +++ b/examples/platform-test/main.cpp @@ -1,62 +1,91 @@ +#include "core/slang-basic.h" #include "examples/example-base/example-base.h" #include "platform/window.h" +#include "slang-com-ptr.h" +#include "slang-rhi.h" #include "slang.h" -using namespace gfx; +#include + +using namespace rhi; using namespace Slang; struct PlatformTest : public WindowedAppBase { - void onSizeChanged() { printf("onSizeChanged\n"); } + void onSizeChanged() + { + printf("onSizeChanged\n"); + fflush(stdout); + } - void onFocus() { printf("onFocus\n"); } + void onFocus() + { + printf("onFocus\n"); + fflush(stdout); + } - void onLostFocus() { printf("onLostFocus\n"); } + void onLostFocus() + { + printf("onLostFocus\n"); + fflush(stdout); + } void onKeyDown(platform::KeyEventArgs args) { printf("onKeyDown(key=0x%02x, buttons=0x%02x)\n", (uint32_t)args.key, args.buttons); + fflush(stdout); } void onKeyUp(platform::KeyEventArgs args) { - printf("okKeyUp(key=0x%02x, buttons=0x%02x)\n", (uint32_t)args.key, args.buttons); + printf("onKeyUp(key=0x%02x, buttons=0x%02x)\n", (uint32_t)args.key, args.buttons); + fflush(stdout); } void onKeyPress(platform::KeyEventArgs args) { printf("onKeyPress(keyChar=0x%02x)\n", args.keyChar); + fflush(stdout); } void onMouseMove(platform::MouseEventArgs args) { - printf( - "onMouseMove(x=%d, y=%d, delta=%d, buttons=0x%02x\n", - args.x, - args.y, - args.delta, - args.buttons); + // Throttle mouse move events using a simple counter + static int mouseMoveCounter = 0; + mouseMoveCounter++; + if (mouseMoveCounter % 50 == 0) // Only print every 50th mouse move event + { + printf( + "onMouseMove(x=%d, y=%d, delta=%d, buttons=0x%02x)\n", + args.x, + args.y, + args.delta, + args.buttons); + fflush(stdout); + } } void onMouseDown(platform::MouseEventArgs args) { printf( - "onMouseDown(x=%d, y=%d, delta=%d, buttons=0x%02x\n", + "onMouseDown(x=%d, y=%d, delta=%d, buttons=0x%02x)\n", args.x, args.y, args.delta, args.buttons); + fflush(stdout); } void onMouseUp(platform::MouseEventArgs args) { printf( - "onMouseUp(x=%d, y=%d, delta=%d, buttons=0x%02x\n", + "onMouseUp(x=%d, y=%d, delta=%d, buttons=0x%02x)\n", args.x, args.y, args.delta, args.buttons); + fflush(stdout); } void onMouseWheel(platform::MouseEventArgs args) @@ -67,6 +96,7 @@ struct PlatformTest : public WindowedAppBase args.y, args.delta, args.buttons); + fflush(stdout); } Slang::Result initialize() @@ -77,6 +107,9 @@ struct PlatformTest : public WindowedAppBase SLANG_ASSERT(isTestMode() || gWindow); if (gWindow) { + printf("Setting up event handlers...\n"); + fflush(stdout); + gWindow->events.sizeChanged = [this]() { onSizeChanged(); }; gWindow->events.focus = [this]() { onFocus(); }; gWindow->events.lostFocus = [this]() { onLostFocus(); }; @@ -90,33 +123,47 @@ struct PlatformTest : public WindowedAppBase gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { onMouseUp(e); }; gWindow->events.mouseWheel = [this](const platform::MouseEventArgs& e) { onMouseWheel(e); }; + printf("Event handlers set up successfully.\n"); + } + else + { + printf("No window available for event setup.\n"); + fflush(stdout); } return SLANG_OK; } - virtual void renderFrame(int frameBufferIndex) override + virtual void renderFrame(ITexture* texture) override { - ComPtr commandBuffer = - gTransientHeaps[frameBufferIndex]->createCommandBuffer(); - - auto renderEncoder = - commandBuffer->encodeRenderCommands(gRenderPass, gFramebuffers[frameBufferIndex]); - - gfx::Viewport viewport = {}; - viewport.maxZ = 1.0f; - viewport.extentX = (float)windowWidth; - viewport.extentY = (float)windowHeight; - renderEncoder->setViewportAndScissor(viewport); - - renderEncoder->endEncoding(); - commandBuffer->close(); - gQueue->executeCommandBuffer(commandBuffer); - - // We may not have a swapchain if we're running in test mode - SLANG_ASSERT(isTestMode() || gSwapchain); - if (gSwapchain) - gSwapchain->present(); + auto commandEncoder = gQueue->createCommandEncoder(); + + ComPtr textureView = gDevice->createTextureView(texture, {}); + RenderPassColorAttachment colorAttachment = {}; + colorAttachment.view = textureView; + colorAttachment.loadOp = LoadOp::Clear; + + RenderPassDesc renderPass = {}; + renderPass.colorAttachments = &colorAttachment; + renderPass.colorAttachmentCount = 1; + + auto renderEncoder = commandEncoder->beginRenderPass(renderPass); + + RenderState renderState = {}; + renderState.viewports[0] = Viewport::fromSize(windowWidth, windowHeight); + renderState.viewportCount = 1; + renderState.scissorRects[0] = ScissorRect::fromSize(windowWidth, windowHeight); + renderState.scissorRectCount = 1; + + renderEncoder->setRenderState(renderState); + + renderEncoder->end(); + gQueue->submit(commandEncoder->finish()); + + if (!isTestMode()) + { + gSurface->present(); + } } }; -- cgit v1.2.3