summaryrefslogtreecommitdiffstats
path: root/examples/example-base/example-base.cpp
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2024-08-22 12:18:44 -0500
committerGitHub <noreply@github.com>2024-08-22 10:18:44 -0700
commitb5bb82411cc6101b66283f7d0abca7ceb58aa2f6 (patch)
tree192a522513da30b1a92e6c319693aa541729bea9 /examples/example-base/example-base.cpp
parenta3fbd8f060317332eae951d4a376e830d058469e (diff)
Feature/capture unit test (#4898)
* record/replay: Add tests Modify the hello-world example to generate the hash code for the entry point spirv code, so that we can compare it with replaying the example. Add the test script to run the example and compare the hash code with replaying it. * Check nullptr for out Diagnostics We need to check whether the output Diagnostics is a nullptr, because it's allowed. * Fix the double free pointers * Add triangle example as the new test for record-replay Change the example base to add the offline rendering path because we don't want to display anything when we're in the test mode. This change involves introducing a TestBase that will parse the command line option. It will decide whether we are in the test mode. Disable all the swapchain and windows related creation, instead we will only create one single framebuffer for the render target. * Address comments TODO: In the follow up patches, I will add more tests and integrate the test flow into slang-unit-test.
Diffstat (limited to 'examples/example-base/example-base.cpp')
-rw-r--r--examples/example-base/example-base.cpp120
1 files changed, 81 insertions, 39 deletions
diff --git a/examples/example-base/example-base.cpp b/examples/example-base/example-base.cpp
index 82d474f06..fb11105f5 100644
--- a/examples/example-base/example-base.cpp
+++ b/examples/example-base/example-base.cpp
@@ -17,19 +17,6 @@ Slang::Result WindowedAppBase::initializeBase(
int height,
DeviceType deviceType)
{
- // Create a window for our application to render into.
- //
- platform::WindowDesc windowDesc;
- windowDesc.title = title;
- windowDesc.width = width;
- windowDesc.height = height;
- windowWidth = width;
- windowHeight = height;
- windowDesc.style = platform::WindowStyle::Default;
- gWindow = platform::Application::createWindow(windowDesc);
- gWindow->events.mainLoop = [this]() { mainLoop(); };
- gWindow->events.sizeChanged = Slang::Action<>(this, &WindowedAppBase::windowSizeChanged);
-
// Initialize the rendering layer.
#ifdef _DEBUG
// Enable debug layer in debug config.
@@ -41,26 +28,44 @@ Slang::Result WindowedAppBase::initializeBase(
if (SLANG_FAILED(res))
return res;
- auto deviceInfo = gDevice->getDeviceInfo();
- Slang::StringBuilder titleSb;
- titleSb << title << " (" << deviceInfo.apiName << ": " << deviceInfo.adapterName << ")";
- gWindow->setText(titleSb.getBuffer());
-
ICommandQueue::Desc queueDesc = {};
queueDesc.type = ICommandQueue::QueueType::Graphics;
gQueue = gDevice->createCommandQueue(queueDesc);
- // Create swapchain and framebuffers.
- gfx::ISwapchain::Desc swapchainDesc = {};
- swapchainDesc.format = gfx::Format::R8G8B8A8_UNORM;
- swapchainDesc.width = width;
- swapchainDesc.height = height;
- swapchainDesc.imageCount = kSwapchainImageCount;
- swapchainDesc.queue = gQueue;
- gfx::WindowHandle windowHandle = gWindow->getNativeHandle().convert<gfx::WindowHandle>();
- gSwapchain = gDevice->createSwapchain(swapchainDesc, windowHandle);
-
- IFramebufferLayout::TargetLayout renderTargetLayout = {gSwapchain->getDesc().format, 1};
+ windowWidth = width;
+ windowHeight = height;
+ // Do not create swapchain and windows in test mode, because there won't be any display.
+ if (!isTestMode())
+ {
+ // Create a window for our application to render into.
+ //
+ platform::WindowDesc windowDesc;
+ windowDesc.title = title;
+ windowDesc.width = width;
+ windowDesc.height = height;
+ windowDesc.style = platform::WindowStyle::Default;
+ gWindow = platform::Application::createWindow(windowDesc);
+ gWindow->events.mainLoop = [this]() { mainLoop(); };
+ gWindow->events.sizeChanged = Slang::Action<>(this, &WindowedAppBase::windowSizeChanged);
+
+ auto deviceInfo = gDevice->getDeviceInfo();
+ Slang::StringBuilder titleSb;
+ titleSb << title << " (" << deviceInfo.apiName << ": " << deviceInfo.adapterName << ")";
+ gWindow->setText(titleSb.getBuffer());
+
+ // Create swapchain and framebuffers.
+ gfx::ISwapchain::Desc swapchainDesc = {};
+ swapchainDesc.format = gfx::Format::R8G8B8A8_UNORM;
+ swapchainDesc.width = width;
+ swapchainDesc.height = height;
+ swapchainDesc.imageCount = kSwapchainImageCount;
+ swapchainDesc.queue = gQueue;
+ gfx::WindowHandle windowHandle = gWindow->getNativeHandle().convert<gfx::WindowHandle>();
+ gSwapchain = gDevice->createSwapchain(swapchainDesc, windowHandle);
+ createSwapchainFramebuffers();
+ }
+
+ IFramebufferLayout::TargetLayout renderTargetLayout = {gfx::Format::R8G8B8A8_UNORM, 1};
IFramebufferLayout::TargetLayout depthLayout = {gfx::Format::D32_FLOAT, 1};
IFramebufferLayout::Desc framebufferLayoutDesc;
framebufferLayoutDesc.renderTargetCount = 1;
@@ -69,7 +74,10 @@ Slang::Result WindowedAppBase::initializeBase(
SLANG_RETURN_ON_FAIL(
gDevice->createFramebufferLayout(framebufferLayoutDesc, gFramebufferLayout.writeRef()));
- createSwapchainFramebuffers();
+ if (isTestMode())
+ {
+ createOfflineFramebuffers();
+ }
for (uint32_t i = 0; i < kSwapchainImageCount; i++)
{
@@ -102,23 +110,27 @@ Slang::Result WindowedAppBase::initializeBase(
void WindowedAppBase::mainLoop()
{
int frameBufferIndex = gSwapchain->acquireNextImage();
- if (frameBufferIndex == -1)
- return;
gTransientHeaps[frameBufferIndex]->synchronizeAndReset();
renderFrame(frameBufferIndex);
gTransientHeaps[frameBufferIndex]->finish();
}
-void WindowedAppBase::createSwapchainFramebuffers()
+void WindowedAppBase::offlineRender()
{
- gFramebuffers.clear();
- for (uint32_t i = 0; i < kSwapchainImageCount; i++)
+ gTransientHeaps[0]->synchronizeAndReset();
+ renderFrame(0);
+ gTransientHeaps[0]->finish();
+}
+
+void WindowedAppBase::createFramebuffers(uint32_t width, uint32_t height, gfx::Format colorFormat, uint32_t frameBufferCount)
+{
+ for (uint32_t i = 0; i < frameBufferCount; i++)
{
gfx::ITextureResource::Desc depthBufferDesc;
depthBufferDesc.type = IResource::Type::Texture2D;
- depthBufferDesc.size.width = gSwapchain->getDesc().width;
- depthBufferDesc.size.height = gSwapchain->getDesc().height;
+ depthBufferDesc.size.width = width;
+ depthBufferDesc.size.height = height;
depthBufferDesc.size.depth = 1;
depthBufferDesc.format = gfx::Format::D32_FLOAT;
depthBufferDesc.defaultState = ResourceState::DepthWrite;
@@ -127,12 +139,28 @@ void WindowedAppBase::createSwapchainFramebuffers()
depthBufferDesc.optimalClearValue = &depthClearValue;
ComPtr<gfx::ITextureResource> depthBufferResource =
gDevice->createTextureResource(depthBufferDesc, nullptr);
+
ComPtr<gfx::ITextureResource> colorBuffer;
- gSwapchain->getImage(i, colorBuffer.writeRef());
+ if (isTestMode())
+ {
+ gfx::ITextureResource::Desc colorBufferDesc;
+ colorBufferDesc.type = IResource::Type::Texture2D;
+ colorBufferDesc.size.width = width;
+ colorBufferDesc.size.height = height;
+ colorBufferDesc.size.depth = 1;
+ colorBufferDesc.format = colorFormat;
+ colorBufferDesc.defaultState = ResourceState::RenderTarget;
+ colorBufferDesc.allowedStates = ResourceStateSet(ResourceState::RenderTarget, ResourceState::CopyDestination);
+ colorBuffer = gDevice->createTextureResource(colorBufferDesc, nullptr);
+ }
+ else
+ {
+ gSwapchain->getImage(i, colorBuffer.writeRef());
+ }
gfx::IResourceView::Desc colorBufferViewDesc;
memset(&colorBufferViewDesc, 0, sizeof(colorBufferViewDesc));
- colorBufferViewDesc.format = gSwapchain->getDesc().format;
+ colorBufferViewDesc.format = colorFormat;
colorBufferViewDesc.renderTarget.shape = gfx::IResource::Type::Texture2D;
colorBufferViewDesc.type = gfx::IResourceView::Type::RenderTarget;
ComPtr<gfx::IResourceView> rtv =
@@ -152,10 +180,24 @@ void WindowedAppBase::createSwapchainFramebuffers()
framebufferDesc.renderTargetViews = rtv.readRef();
framebufferDesc.layout = gFramebufferLayout;
ComPtr<gfx::IFramebuffer> frameBuffer = gDevice->createFramebuffer(framebufferDesc);
+
gFramebuffers.add(frameBuffer);
}
}
+void WindowedAppBase::createOfflineFramebuffers()
+{
+ gFramebuffers.clear();
+ createFramebuffers(windowWidth, windowHeight, gfx::Format::R8G8B8A8_UNORM, 1);
+}
+
+void WindowedAppBase::createSwapchainFramebuffers()
+{
+ gFramebuffers.clear();
+ createFramebuffers(gSwapchain->getDesc().width, gSwapchain->getDesc().height,
+ gSwapchain->getDesc().format, kSwapchainImageCount);
+}
+
ComPtr<gfx::IResourceView> WindowedAppBase::createTextureFromFile(String fileName, int& textureWidth, int& textureHeight)
{
int channelsInFile = 0;