Skip to content

Commit

Permalink
Migrate 3 more examples to slang-unit-test (shader-slang#4927)
Browse files Browse the repository at this point in the history
* Convert 'ray-tracing-pipeline' example into slang-test

* Convert model-view and 'autodiff-texture' examples to slang-test

* Add more error message in RecordReplay test

* Fix a shader issue in autodiff-texture
  • Loading branch information
kaizhangNV authored Aug 29, 2024
1 parent d3a5a47 commit efda04f
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 32 deletions.
42 changes: 34 additions & 8 deletions examples/autodiff-texture/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ struct AutoDiffTexture : public WindowedAppBase
diagnoseIfNeeded(diagnosticsBlob);
SLANG_RETURN_ON_FAIL(result);

if (isTestMode())
{
printEntrypointHashes(componentTypes.getCount() - 1, 1, linkedProgram);
}

gfx::IShaderProgram::Desc programDesc = {};
programDesc.slangGlobalScope = linkedProgram;
SLANG_RETURN_ON_FAIL(device->createProgram(programDesc, outProgram));
Expand Down Expand Up @@ -114,6 +119,11 @@ struct AutoDiffTexture : public WindowedAppBase
diagnoseIfNeeded(diagnosticsBlob);
SLANG_RETURN_ON_FAIL(result);

if (isTestMode())
{
printEntrypointHashes(componentTypes.getCount() - 1, 1, linkedProgram);
}

gfx::IShaderProgram::Desc programDesc = {};
programDesc.slangGlobalScope = linkedProgram;
SLANG_RETURN_ON_FAIL(device->createProgram(programDesc, outProgram));
Expand Down Expand Up @@ -266,18 +276,31 @@ struct AutoDiffTexture : public WindowedAppBase
initializeBase("autodiff-texture", 1024, 768);
srand(20421);

gWindow->events.keyPress = [this](platform::KeyEventArgs& e)
if (!isTestMode())
{
if (e.keyChar == 'R' || e.keyChar == 'r')
resetLearntTexture = true;
};
gWindow->events.keyPress = [this](platform::KeyEventArgs& e)
{
if (e.keyChar == 'R' || e.keyChar == 'r')
resetLearntTexture = true;
};
}

kClearValue.color.floatValues[0] = 0.3f;
kClearValue.color.floatValues[1] = 0.5f;
kClearValue.color.floatValues[2] = 0.7f;
kClearValue.color.floatValues[3] = 1.0f;

auto clientRect = gWindow->getClientRect();
platform::Rect clientRect{};
if (isTestMode())
{
clientRect.width = 1024;
clientRect.height = 768;
}
else
{
clientRect = getWindow()->getClientRect();
}

windowWidth = clientRect.width;
windowHeight = clientRect.height;

Expand Down Expand Up @@ -321,7 +344,7 @@ struct AutoDiffTexture : public WindowedAppBase
}
{
ComPtr<IShaderProgram> shaderProgram;
SLANG_RETURN_ON_FAIL(loadComputeProgram(gDevice, "convert", shaderProgram.writeRef()));
SLANG_RETURN_ON_FAIL(loadComputeProgram(gDevice, "convert.slang", shaderProgram.writeRef()));
gConvertPipelineState = createComputePipelineState(shaderProgram);
}
{
Expand Down Expand Up @@ -619,7 +642,7 @@ struct AutoDiffTexture : public WindowedAppBase
commandBuffer->close();
gQueue->executeCommandBuffer(commandBuffer);
}

// Draw currently learnt texture.
{
ComPtr<ICommandBuffer> commandBuffer =
Expand All @@ -635,7 +658,10 @@ struct AutoDiffTexture : public WindowedAppBase
gQueue->executeCommandBuffer(commandBuffer);
}

gSwapchain->present();
if (!isTestMode())
{
gSwapchain->present();
}
}

void drawTexturedQuad(IRenderCommandEncoder* renderEncoder, int x, int y, int w, int h, IResourceView* srv)
Expand Down
4 changes: 2 additions & 2 deletions examples/autodiff-texture/train.slang
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ struct DifferentiableTexture
float minLOD;

[BackwardDerivative(bwd_LoadTexel)]
float4 LoadTexel(int3 location, int2 offset, uint dLayerW, uint dMipOffset)
float4 LoadTexel(int3 location, constexpr int2 offset, uint dLayerW, uint dMipOffset)
{
return texture.Load(location, offset);
}

void bwd_LoadTexel(int3 location, int2 offset, uint dLayerW, uint dMipOffset, float4 val)
void bwd_LoadTexel(int3 location, constexpr int2 offset, uint dLayerW, uint dMipOffset, float4 val)
{
// Ignore alpha dimension for this example..
int4 uval = int4(int3(val.xyz * 65536), 1);
Expand Down
45 changes: 36 additions & 9 deletions examples/model-viewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ struct RendererContext
slang::TypeReflection* perViewShaderType;
slang::TypeReflection* perModelShaderType;

Result init(IDevice* inDevice)
TestBase *pTestBase;

Result init(IDevice* inDevice, TestBase* inTestBase)
{
device = inDevice;
ComPtr<ISlangBlob> diagnostic;
pTestBase = inTestBase;

Slang::String path = resourceBase.resolveResource("shaders.slang").getBuffer();
shaderModule = device->getSlangSession()->loadModule(
path.getBuffer(),
Expand Down Expand Up @@ -108,6 +112,12 @@ struct RendererContext
diagnosticsBlob.writeRef());
diagnoseIfNeeded(diagnosticsBlob);
SLANG_RETURN_ON_FAIL(result);

if (pTestBase && pTestBase->isTestMode())
{
pTestBase->printEntrypointHashes(componentTypes.getCount() - 1, 1, composedProgram);
}

slangReflection = composedProgram->getLayout();

// At this point, `composedProgram` represents the shader program
Expand Down Expand Up @@ -740,14 +750,18 @@ void onMouseUp(platform::MouseEventArgs args)
Result initialize()
{
initializeBase("Model Viewer", 1024, 768);
gWindow->events.mouseMove = [this](const platform::MouseEventArgs& e) { onMouseMove(e); };
gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { onMouseUp(e); };
gWindow->events.mouseDown = [this](const platform::MouseEventArgs& e) { onMouseDown(e); };
gWindow->events.keyDown = [this](const platform::KeyEventArgs& e) { onKeyDown(e); };
gWindow->events.keyUp = [this](const platform::KeyEventArgs& e) { onKeyUp(e); };
if (!isTestMode())
{
gWindow->events.mouseMove = [this](const platform::MouseEventArgs& e) { onMouseMove(e); };
gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { onMouseUp(e); };
gWindow->events.mouseDown = [this](const platform::MouseEventArgs& e) { onMouseDown(e); };
gWindow->events.keyDown = [this](const platform::KeyEventArgs& e) { onKeyDown(e); };
gWindow->events.keyUp = [this](const platform::KeyEventArgs& e) { onKeyUp(e); };
}

// Initialize `RendererContext`, which loads the shader module from file.
SLANG_RETURN_ON_FAIL(context.init(gDevice));
SLANG_RETURN_ON_FAIL(context.init(gDevice, this));


InputElementDesc inputElements[] = {
{"POSITION", 0, Format::R32G32B32_FLOAT, offsetof(Model::Vertex, position) },
Expand Down Expand Up @@ -817,7 +831,17 @@ void renderFrame(int frameIndex) override
// to set up our various transformation matrices.
//
glm::mat4x4 identity = glm::mat4x4(1.0f);
auto clientRect = getWindow()->getClientRect();

platform::Rect clientRect{};
if (isTestMode())
{
clientRect.width = 1024;
clientRect.height = 768;
}
else
{
clientRect = getWindow()->getClientRect();
}
if (clientRect.height == 0)
return;
glm::mat4x4 projection = glm::perspectiveRH_ZO(
Expand Down Expand Up @@ -933,7 +957,10 @@ void renderFrame(int frameIndex) override
drawCommandBuffer->close();
gQueue->executeCommandBuffer(drawCommandBuffer);

gSwapchain->present();
if (!isTestMode())
{
gSwapchain->present();
}
}

};
Expand Down
28 changes: 20 additions & 8 deletions examples/ray-tracing-pipeline/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ gfx::Result loadShaderProgram(
diagnoseIfNeeded(diagnosticsBlob);
SLANG_RETURN_ON_FAIL(result);

if (isTestMode())
{
printEntrypointHashes(componentTypes.getCount() - 1, 1, linkedProgram);
}

gfx::IShaderProgram::Desc programDesc = {};
programDesc.slangGlobalScope = linkedProgram;
SLANG_RETURN_ON_FAIL(device->createProgram(programDesc, outProgram));
Expand Down Expand Up @@ -297,11 +302,14 @@ void onMouseUp(platform::MouseEventArgs args) { isMouseDown = false; }
Slang::Result initialize()
{
initializeBase("Ray Tracing Pipeline", 1024, 768);
gWindow->events.mouseMove = [this](const platform::MouseEventArgs& e) { onMouseMove(e); };
gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { onMouseUp(e); };
gWindow->events.mouseDown = [this](const platform::MouseEventArgs& e) { onMouseDown(e); };
gWindow->events.keyDown = [this](const platform::KeyEventArgs& e) { onKeyDown(e); };
gWindow->events.keyUp = [this](const platform::KeyEventArgs& e) { onKeyUp(e); };
if (!isTestMode())
{
gWindow->events.mouseMove = [this](const platform::MouseEventArgs& e) { onMouseMove(e); };
gWindow->events.mouseUp = [this](const platform::MouseEventArgs& e) { onMouseUp(e); };
gWindow->events.mouseDown = [this](const platform::MouseEventArgs& e) { onMouseDown(e); };
gWindow->events.keyDown = [this](const platform::KeyEventArgs& e) { onKeyDown(e); };
gWindow->events.keyUp = [this](const platform::KeyEventArgs& e) { onKeyUp(e); };
}

IBufferResource::Desc vertexBufferDesc;
vertexBufferDesc.type = IResource::Type::Buffer;
Expand Down Expand Up @@ -670,9 +678,13 @@ virtual void renderFrame(int frameBufferIndex) override
presentCommandBuffer->close();
gQueue->executeCommandBuffer(presentCommandBuffer);
}
// With that, we are done drawing for one frame, and ready for the next.
//
gSwapchain->present();

if (!isTestMode())
{
// With that, we are done drawing for one frame, and ready for the next.
//
gSwapchain->present();
}
}

};
Expand Down
4 changes: 2 additions & 2 deletions source/slang-record-replay/replay/json-consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,8 @@ namespace SlangRecord
_writePair(builder, indent, "this", Slang::StringUtil::makeStringWithFormat("0x%X", objectId));
_writePair(builder, indent, "type", Slang::StringUtil::makeStringWithFormat("0x%X", typeId));
_writePair(builder, indent, "rules", LayoutRulesToString(rules));
_writePair(builder, indent, "outDiagnostics", outDiagnosticsId);
_writePairNoComma(builder, indent, "retTypeReflectionId", outTypeLayoutReflectionId);
_writePair(builder, indent, "outDiagnostics", Slang::StringUtil::makeStringWithFormat("0x%X", outDiagnosticsId));
_writePairNoComma(builder, indent, "retTypeReflectionId", Slang::StringUtil::makeStringWithFormat("0x%X", outTypeLayoutReflectionId));
}
}

Expand Down
27 changes: 24 additions & 3 deletions tools/slang-unit-test/unit-test-record-replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,16 @@ static SlangResult launchProcessAndReadStdout(UnitTestContext* context, const Li
return res;
}

if (exeRes.resultCode != 0)
{
msgBuilder << "'" << exampleName << "' exits with failure\n";
msgBuilder << "Process ret code: " << exeRes.resultCode << "\n";
msgBuilder << "Standard output:\n" << exeRes.standardOutput;
msgBuilder << "Standard error:\n" << exeRes.standardError;
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}

if (exeRes.standardOutput.getLength() == 0)
{
msgBuilder << "No stdout found in '" << exampleName << "'\n";
Expand Down Expand Up @@ -300,12 +310,20 @@ static SlangResult replayExample(UnitTestContext* context, List<entryHashInfo>&

static SlangResult resultCompare(List<entryHashInfo> const& expectHashes, List<entryHashInfo> const& resultHashes)
{
if (expectHashes.getCount() != resultHashes.getCount())
if (expectHashes.getCount() == 0)
{
getTestReporter()->message(TestMessageType::TestFailure, "No hash found\n");
return SLANG_FAIL;
}

StringBuilder msgBuilder;
if (expectHashes.getCount() != resultHashes.getCount())
{
msgBuilder << "The number of hashes doesn't match, expect: " << expectHashes.getCount() << ", actual: " << resultHashes.getCount() << "\n";
getTestReporter()->message(TestMessageType::TestFailure, msgBuilder.toString().getBuffer());
return SLANG_FAIL;
}

for (Index i = 0; i < expectHashes.getCount(); i++)
{
if (expectHashes[i].targetIndex != resultHashes[i].targetIndex)
Expand Down Expand Up @@ -419,7 +437,10 @@ static SlangResult runTests(UnitTestContext* context)
"cpu-hello-world",
"triangle",
"shader-object",
"ray-tracing"
"ray-tracing",
"ray-tracing-pipeline",
"model-viewer",
"autodiff-texture"
};

SlangResult finalRes = SLANG_OK;
Expand All @@ -435,7 +456,7 @@ static SlangResult runTests(UnitTestContext* context)
}
}

return SLANG_OK;
return finalRes;
}

// Those examples all depend on the Vulkan, so we only run them on non-Apple platforms.
Expand Down

0 comments on commit efda04f

Please sign in to comment.