summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/gpu-printing/main.cpp22
-rw-r--r--examples/hello-world/main.cpp37
-rw-r--r--examples/shader-toy/main.cpp82
3 files changed, 48 insertions, 93 deletions
diff --git a/examples/gpu-printing/main.cpp b/examples/gpu-printing/main.cpp
index 63eb31a82..e95233571 100644
--- a/examples/gpu-printing/main.cpp
+++ b/examples/gpu-printing/main.cpp
@@ -6,7 +6,7 @@
using Slang::ComPtr;
#include "slang-gfx.h"
-#include "tools/graphics-app-framework/window.h"
+#include "tools/platform/window.h"
#include "source/core/slang-basic.h"
using namespace gfx;
@@ -44,7 +44,7 @@ ComPtr<slang::IModule> compileShaderModuleFromFile(slang::ISession* slangSession
const SlangResult compileRes = spCompile(slangRequest);
if(auto diagnostics = spGetDiagnosticOutput(slangRequest))
{
- reportError("%s", diagnostics);
+ printf("%s", diagnostics);
}
if(SLANG_FAILED(compileRes))
@@ -63,7 +63,6 @@ struct ExampleProgram
int gWindowWidth = 640;
int gWindowHeight = 480;
-gfx::ApplicationContext* gAppContext;
ComPtr<gfx::IRenderer> gRenderer;
ComPtr<slang::ISession> gSlangSession;
@@ -151,8 +150,6 @@ Result execute()
auto descriptorSet = gRenderer->createDescriptorSet(descriptorSetLayout, IDescriptorSet::Flag::Transient);
if(!descriptorSet) return SLANG_FAIL;
-// descriptorSet->setConstantBuffer(0, 0, gConstantBuffer);
-
gDescriptorSet = descriptorSet;
ComputePipelineStateDesc desc;
@@ -201,21 +198,12 @@ Result execute()
};
-// This "inner" main function is used by the platform abstraction
-// layer to deal with differences in how an entry point needs
-// to be defined for different platforms.
-//
-void innerMain(ApplicationContext* context)
+int main()
{
ExampleProgram app;
-
if (SLANG_FAILED(app.execute()))
{
- return exitApplication(context, 1);
+ return -1;
}
+ return 0;
}
-
-// This macro instantiates an appropriate main function to
-// invoke the `innerMain` above.
-//
-GFX_CONSOLE_MAIN(innerMain)
diff --git a/examples/hello-world/main.cpp b/examples/hello-world/main.cpp
index 47016b48d..330b52a59 100644
--- a/examples/hello-world/main.cpp
+++ b/examples/hello-world/main.cpp
@@ -34,7 +34,7 @@
//
#include "slang-gfx.h"
#include "gfx-util/shader-cursor.h"
-#include "tools/graphics-app-framework/window.h"
+#include "tools/platform/window.h"
#include "slang-com-ptr.h"
#include "source/core/slang-basic.h"
@@ -76,7 +76,7 @@ void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob)
{
if( diagnosticsBlob != nullptr )
{
- reportError("%s", (const char*) diagnosticsBlob->getBufferPointer());
+ printf("%s", (const char*) diagnosticsBlob->getBufferPointer());
}
}
@@ -210,8 +210,7 @@ const uint32_t kSwapchainImageCount = 2;
// of them come from the utility library we are using to simplify
// building an example program.
//
-gfx::ApplicationContext* gAppContext;
-gfx::Window* gWindow;
+RefPtr<platform::Window> gWindow;
Slang::ComPtr<gfx::IRenderer> gRenderer;
ComPtr<gfx::IPipelineState> gPipelineState;
@@ -230,12 +229,12 @@ Slang::Result initialize()
{
// Create a window for our application to render into.
//
- WindowDesc windowDesc;
+ platform::WindowDesc windowDesc;
windowDesc.title = "Hello, World!";
windowDesc.width = gWindowWidth;
windowDesc.height = gWindowHeight;
- gWindow = createWindow(windowDesc);
-
+ gWindow = platform::Application::createWindow(windowDesc);
+ gWindow->events.mainLoop = [this]() { renderFrame(); };
// Initialize the rendering layer.
//
// Note: for now we are hard-coding logic to use the
@@ -323,8 +322,9 @@ Slang::Result initialize()
swapchainDesc.height = gWindowHeight;
swapchainDesc.imageCount = kSwapchainImageCount;
swapchainDesc.queue = gQueue;
- gSwapchain = gRenderer->createSwapchain(
- swapchainDesc, gfx::WindowHandle::FromHwnd(getPlatformWindowHandle(gWindow)));
+ gfx::WindowHandle windowHandle;
+ memcpy(&windowHandle, &gWindow->getNativeHandle(), sizeof(windowHandle));
+ gSwapchain = gRenderer->createSwapchain(swapchainDesc, windowHandle);
IFramebufferLayout::AttachmentLayout renderTargetLayout = {gSwapchain->getDesc().format, 1};
IFramebufferLayout::AttachmentLayout depthLayout = {gfx::Format::D_Float32, 1};
@@ -405,11 +405,6 @@ Slang::Result initialize()
renderPassDesc.depthStencilAccess = &depthStencilAccess;
gRenderPass = gRenderer->createRenderPassLayout(renderPassDesc);
- // Once we've initialized all the graphics API objects,
- // it is time to show our application window and start rendering.
- //
- showWindow(gWindow);
-
return SLANG_OK;
}
@@ -526,7 +521,6 @@ void finalize()
{
gQueue->wait();
gSwapchain = nullptr;
- destroyWindow(gWindow);
}
};
@@ -535,7 +529,7 @@ void finalize()
// layer to deal with differences in how an entry point needs
// to be defined for different platforms.
//
-void innerMain(ApplicationContext* context)
+int innerMain()
{
// We construct an instance of our example application
// `struct` type, and then walk through the lifecyle
@@ -545,18 +539,17 @@ void innerMain(ApplicationContext* context)
if (SLANG_FAILED(app.initialize()))
{
- return exitApplication(context, 1);
+ return -1;
}
- while(dispatchEvents(context))
- {
- app.renderFrame();
- }
+ platform::Application::run(app.gWindow);
app.finalize();
+
+ return 0;
}
// This macro instantiates an appropriate main function to
// invoke the `innerMain` above.
//
-GFX_UI_MAIN(innerMain)
+PLATFORM_UI_MAIN(innerMain)
diff --git a/examples/shader-toy/main.cpp b/examples/shader-toy/main.cpp
index 0d058fa2c..7c22954c0 100644
--- a/examples/shader-toy/main.cpp
+++ b/examples/shader-toy/main.cpp
@@ -20,9 +20,12 @@ using Slang::ComPtr;
// compiler, and API.
//
#include "slang-gfx.h"
-#include "tools/graphics-app-framework/window.h"
+#include "tools/platform/window.h"
+#include "tools/platform/performance-counter.h"
#include "source/core/slang-basic.h"
+#include <chrono>
+
using namespace gfx;
// In order to display a shader toy effect using rasterization-based shader
@@ -82,7 +85,7 @@ void diagnoseIfNeeded(slang::IBlob* diagnosticsBlob)
{
if( diagnosticsBlob != nullptr )
{
- reportError("%s", (const char*) diagnosticsBlob->getBufferPointer());
+ printf("%s", (const char*) diagnosticsBlob->getBufferPointer());
}
}
@@ -329,8 +332,7 @@ int gWindowWidth = 1024;
int gWindowHeight = 768;
const uint32_t kSwapchainImageCount = 2;
-gfx::ApplicationContext* gAppContext;
-gfx::Window* gWindow;
+Slang::RefPtr<platform::Window> gWindow;
Slang::ComPtr<gfx::IRenderer> gRenderer;
ComPtr<gfx::IBufferResource> gConstantBuffer;
ComPtr<gfx::IPipelineLayout> gPipelineLayout;
@@ -344,13 +346,15 @@ ComPtr<gfx::ICommandQueue> gQueue;
Result initialize()
{
- WindowDesc windowDesc;
+ platform::WindowDesc windowDesc;
windowDesc.title = "Slang Shader Toy";
windowDesc.width = gWindowWidth;
windowDesc.height = gWindowHeight;
- windowDesc.eventHandler = &_handleEvent;
- windowDesc.userData = this;
- gWindow = createWindow(windowDesc);
+ gWindow = platform::Application::createWindow(windowDesc);
+ gWindow->events.mainLoop = [this]() { renderFrame(); };
+ gWindow->events.mouseMove = [this](const platform::MouseEventArgs& e) { handleEvent(e); };
+ gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { handleEvent(e); };
+ gWindow->events.mouseDown = [this](const platform::MouseEventArgs& e) { handleEvent(e); };
IRenderer::Desc rendererDesc;
rendererDesc.rendererType = RendererType::DirectX11;
@@ -430,8 +434,9 @@ Result initialize()
swapchainDesc.height = gWindowHeight;
swapchainDesc.imageCount = kSwapchainImageCount;
swapchainDesc.queue = gQueue;
- gSwapchain = gRenderer->createSwapchain(
- swapchainDesc, gfx::WindowHandle::FromHwnd(getPlatformWindowHandle(gWindow)));
+ gfx::WindowHandle windowHandle;
+ memcpy(&windowHandle, &gWindow->getNativeHandle(), sizeof(windowHandle));
+ gSwapchain = gRenderer->createSwapchain(swapchainDesc, windowHandle);
IFramebufferLayout::AttachmentLayout renderTargetLayout = {gSwapchain->getDesc().format, 1};
IFramebufferLayout::AttachmentLayout depthLayout = {gfx::Format::D_Float32, 1};
@@ -513,9 +518,6 @@ Result initialize()
renderPassDesc.renderTargetAccess = &renderTargetAccess;
renderPassDesc.depthStencilAccess = &depthStencilAccess;
gRenderPass = gRenderer->createRenderPassLayout(renderPassDesc);
-
- showWindow(gWindow);
-
return SLANG_OK;
}
@@ -527,7 +529,7 @@ float clickMouseX = 0.0f;
float clickMouseY = 0.0f;
bool firstTime = true;
-uint64_t startTime = 0;
+platform::TimePoint startTime;
void renderFrame()
{
@@ -535,7 +537,7 @@ void renderFrame()
auto commandBuffer = gQueue->createCommandBuffer();
if( firstTime )
{
- startTime = getCurrentTime();
+ startTime = platform::PerformanceCounter::now();
firstTime = false;
}
@@ -557,7 +559,7 @@ void renderFrame()
uniforms.iMouse[1] = lastMouseY;
uniforms.iMouse[2] = isMouseDown ? clickMouseX : -clickMouseX;
uniforms.iMouse[3] = isMouseClick ? clickMouseY : -clickMouseY;
- uniforms.iTime = float( double(getCurrentTime() - startTime) / double(getTimerFrequency()) );
+ uniforms.iTime = platform::PerformanceCounter::getElapsedTimeInSeconds(startTime);
uniforms.iResolution[0] = float(gWindowWidth);
uniforms.iResolution[1] = float(gWindowHeight);
@@ -588,59 +590,31 @@ void renderFrame()
void finalize()
{
gQueue->wait();
- destroyWindow(gWindow);
-}
-
-void handleEvent(Event const& event)
-{
- switch( event.code )
- {
- case EventCode::MouseDown:
- isMouseDown = true;
- lastMouseX = event.u.mouse.x;
- lastMouseY = event.u.mouse.y;
- break;
-
- case EventCode::MouseMoved:
- lastMouseX = event.u.mouse.x;
- lastMouseY = event.u.mouse.y;
- break;
-
- case EventCode::MouseUp:
- isMouseDown = false;
- lastMouseX = event.u.mouse.x;
- lastMouseY = event.u.mouse.y;
- break;
-
- default:
- break;
- }
}
-static void _handleEvent(Event const& event)
+void handleEvent(const platform::MouseEventArgs& event)
{
- ShaderToyApp* app = (ShaderToyApp*) getUserData(event.window);
- app->handleEvent(event);
+ isMouseDown = ((int)event.buttons & (int)platform::ButtonState::Enum::LeftButton) != 0;
+ lastMouseX = (float)event.x;
+ lastMouseY = (float)event.y;
}
-
};
-void innerMain(ApplicationContext* context)
+int innerMain()
{
ShaderToyApp app;
if (SLANG_FAILED(app.initialize()))
{
- return exitApplication(context, 1);
+ return -1;
}
- while(dispatchEvents(context))
- {
- app.renderFrame();
- }
+ platform::Application::run(app.gWindow);
app.finalize();
+
+ return 0;
}
-GFX_UI_MAIN(innerMain)
+PLATFORM_UI_MAIN(innerMain)