diff options
| author | Gangzheng Tong <tonggangzheng@gmail.com> | 2025-07-08 23:44:56 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-09 06:44:56 +0000 |
| commit | 43d0c2100ef1a5df4b54525e50eb29fe7c39ec16 (patch) | |
| tree | 25ec4fb9c726115f90bdaa9878f2f4ca372ad0a6 /tools/gfx-unit-test/resolve-resource-tests.cpp | |
| parent | 00746bf09047cdf01c19dac513a532bcf3ed3ea3 (diff) | |
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 <ellieh+slangbot@nvidia.com>
Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Diffstat (limited to 'tools/gfx-unit-test/resolve-resource-tests.cpp')
| -rw-r--r-- | tools/gfx-unit-test/resolve-resource-tests.cpp | 252 |
1 files changed, 112 insertions, 140 deletions
diff --git a/tools/gfx-unit-test/resolve-resource-tests.cpp b/tools/gfx-unit-test/resolve-resource-tests.cpp index dd318c7c4..eac03c228 100644 --- a/tools/gfx-unit-test/resolve-resource-tests.cpp +++ b/tools/gfx-unit-test/resolve-resource-tests.cpp @@ -1,15 +1,19 @@ +#if 0 +// Disabled: slang-rhi doesn't have resolveResource API. + #include "core/slang-basic.h" #include "gfx-test-util.h" -#include "gfx-util/shader-cursor.h" -#include "slang-gfx.h" #include "unit-test/slang-unit-test.h" +#include <slang-rhi.h> +#include <slang-rhi/shader-cursor.h> + #if SLANG_WINDOWS_FAMILY #include <d3d12.h> #endif using namespace Slang; -using namespace gfx; +using namespace rhi; namespace { @@ -46,17 +50,15 @@ static const Vertex kVertexData[kVertexCount] = { const int kWidth = 256; const int kHeight = 256; -Format format = Format::R32G32B32A32_FLOAT; +Format format = Format::RGBA32Float; -ComPtr<IBufferResource> createVertexBuffer(IDevice* device) +ComPtr<IBuffer> createVertexBuffer(IDevice* device) { - IBufferResource::Desc vertexBufferDesc; - vertexBufferDesc.type = IResource::Type::Buffer; - vertexBufferDesc.sizeInBytes = kVertexCount * sizeof(Vertex); - vertexBufferDesc.defaultState = ResourceState::VertexBuffer; - vertexBufferDesc.allowedStates = ResourceState::VertexBuffer; - ComPtr<IBufferResource> vertexBuffer = - device->createBufferResource(vertexBufferDesc, &kVertexData[0]); + BufferDesc vertexBufferDesc; + vertexBufferDesc.size = kVertexCount * sizeof(Vertex); + vertexBufferDesc.defaultState = ResourceState::VertexBuffer; + vertexBufferDesc.usage = BufferUsage::VertexBuffer; + ComPtr<IBuffer> vertexBuffer = device->createBuffer(vertexBufferDesc, &kVertexData[0]); SLANG_CHECK_ABORT(vertexBuffer != nullptr); return vertexBuffer; } @@ -66,22 +68,19 @@ struct BaseResolveResourceTest IDevice* device; UnitTestContext* context; - ComPtr<ITextureResource> msaaTexture; - ComPtr<ITextureResource> dstTexture; + ComPtr<ITexture> msaaTexture; + ComPtr<ITexture> dstTexture; - ComPtr<ITransientResourceHeap> transientHeap; - ComPtr<IPipelineState> pipelineState; - ComPtr<IRenderPassLayout> renderPass; - ComPtr<IFramebuffer> framebuffer; + ComPtr<IRenderPipeline> pipelineState; - ComPtr<IBufferResource> vertexBuffer; + ComPtr<IBuffer> vertexBuffer; struct TextureInfo { - ITextureResource::Extents extent; + Extent3D extent; int numMipLevels; int arraySize; - ITextureResource::SubresourceData const* initData; + const SubresourceData* initData; }; void init(IDevice* device, UnitTestContext* context) @@ -101,42 +100,36 @@ struct BaseResolveResourceTest InputElementDesc inputElements[] = { // Vertex buffer data - {"POSITION", 0, Format::R32G32B32_FLOAT, offsetof(Vertex, position), 0}, - {"COLOR", 0, Format::R32G32B32_FLOAT, offsetof(Vertex, color), 0}, + {"POSITION", 0, Format::RGB32Float, offsetof(Vertex, position), 0}, + {"COLOR", 0, Format::RGB32Float, offsetof(Vertex, color), 0}, }; - ITextureResource::Desc msaaTexDesc = {}; - msaaTexDesc.type = IResource::Type::Texture2D; - msaaTexDesc.numMipLevels = dstTextureInfo.numMipLevels; - msaaTexDesc.arraySize = dstTextureInfo.arraySize; + TextureDesc msaaTexDesc = {}; + msaaTexDesc.type = TextureType::Texture2D; + msaaTexDesc.mipCount = dstTextureInfo.numMipLevels; + msaaTexDesc.arrayLength = dstTextureInfo.arraySize; msaaTexDesc.size = dstTextureInfo.extent; msaaTexDesc.defaultState = ResourceState::RenderTarget; - msaaTexDesc.allowedStates = - ResourceStateSet(ResourceState::RenderTarget, ResourceState::ResolveSource); + msaaTexDesc.usage = TextureUsage::RenderTarget; msaaTexDesc.format = format; - msaaTexDesc.sampleDesc.numSamples = 4; + msaaTexDesc.sampleCount = 4; - GFX_CHECK_CALL_ABORT(device->createTextureResource( - msaaTexDesc, - msaaTextureInfo.initData, - msaaTexture.writeRef())); + msaaTexture = device->createTexture(msaaTexDesc, msaaTextureInfo.initData); + SLANG_CHECK_ABORT(msaaTexture); - ITextureResource::Desc dstTexDesc = {}; - dstTexDesc.type = IResource::Type::Texture2D; - dstTexDesc.numMipLevels = dstTextureInfo.numMipLevels; - dstTexDesc.arraySize = dstTextureInfo.arraySize; + TextureDesc dstTexDesc = {}; + dstTexDesc.type = TextureType::Texture2D; + dstTexDesc.mipCount = dstTextureInfo.numMipLevels; + dstTexDesc.arrayLength = dstTextureInfo.arraySize; dstTexDesc.size = dstTextureInfo.extent; - dstTexDesc.defaultState = ResourceState::ResolveDestination; - dstTexDesc.allowedStates = - ResourceStateSet(ResourceState::ResolveDestination, ResourceState::CopySource); + dstTexDesc.defaultState = ResourceState::CopyDestination; + dstTexDesc.usage = TextureUsage::CopyDestination | TextureUsage::CopySource; dstTexDesc.format = format; - GFX_CHECK_CALL_ABORT(device->createTextureResource( - dstTexDesc, - dstTextureInfo.initData, - dstTexture.writeRef())); + dstTexture = device->createTexture(dstTexDesc, dstTextureInfo.initData); + SLANG_CHECK_ABORT(dstTexture); - IInputLayout::Desc inputLayoutDesc = {}; + InputLayoutDesc inputLayoutDesc = {}; inputLayoutDesc.inputElementCount = SLANG_COUNT_OF(inputElements); inputLayoutDesc.inputElements = inputElements; inputLayoutDesc.vertexStreamCount = SLANG_COUNT_OF(vertexStreams); @@ -146,11 +139,6 @@ struct BaseResolveResourceTest vertexBuffer = createVertexBuffer(device); - ITransientResourceHeap::Desc transientHeapDesc = {}; - transientHeapDesc.constantBufferSize = 4096; - GFX_CHECK_CALL_ABORT( - device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef())); - ComPtr<IShaderProgram> shaderProgram; slang::ProgramLayout* slangReflection; GFX_CHECK_CALL_ABORT(loadGraphicsProgram( @@ -161,98 +149,84 @@ struct BaseResolveResourceTest "fragmentMain", slangReflection)); - IFramebufferLayout::TargetLayout targetLayout; - targetLayout.format = format; - targetLayout.sampleCount = 4; - - IFramebufferLayout::Desc framebufferLayoutDesc; - framebufferLayoutDesc.renderTargetCount = 1; - framebufferLayoutDesc.renderTargets = &targetLayout; - ComPtr<gfx::IFramebufferLayout> framebufferLayout = - device->createFramebufferLayout(framebufferLayoutDesc); - SLANG_CHECK_ABORT(framebufferLayout != nullptr); - - GraphicsPipelineStateDesc pipelineDesc = {}; + ColorTargetDesc colorTarget = {}; + colorTarget.format = format; + + RenderPipelineDesc pipelineDesc = {}; pipelineDesc.program = shaderProgram.get(); pipelineDesc.inputLayout = inputLayout; - pipelineDesc.framebufferLayout = framebufferLayout; + pipelineDesc.targets = &colorTarget; + pipelineDesc.targetCount = 1; + pipelineDesc.primitiveTopology = PrimitiveTopology::TriangleList; pipelineDesc.depthStencil.depthTestEnable = false; pipelineDesc.depthStencil.depthWriteEnable = false; - GFX_CHECK_CALL_ABORT( - device->createGraphicsPipelineState(pipelineDesc, pipelineState.writeRef())); - - IRenderPassLayout::Desc renderPassDesc = {}; - renderPassDesc.framebufferLayout = framebufferLayout; - renderPassDesc.renderTargetCount = 1; - IRenderPassLayout::TargetAccessDesc renderTargetAccess = {}; - renderTargetAccess.loadOp = IRenderPassLayout::TargetLoadOp::Clear; - renderTargetAccess.storeOp = IRenderPassLayout::TargetStoreOp::Store; - renderTargetAccess.initialState = ResourceState::RenderTarget; - renderTargetAccess.finalState = ResourceState::ResolveSource; - renderPassDesc.renderTargetAccess = &renderTargetAccess; - GFX_CHECK_CALL_ABORT(device->createRenderPassLayout(renderPassDesc, renderPass.writeRef())); - - gfx::IResourceView::Desc colorBufferViewDesc; - memset(&colorBufferViewDesc, 0, sizeof(colorBufferViewDesc)); - colorBufferViewDesc.format = format; - colorBufferViewDesc.renderTarget.shape = gfx::IResource::Type::Texture2D; - colorBufferViewDesc.type = gfx::IResourceView::Type::RenderTarget; - auto rtv = device->createTextureView(msaaTexture, colorBufferViewDesc); - - gfx::IFramebuffer::Desc framebufferDesc; - framebufferDesc.renderTargetCount = 1; - framebufferDesc.depthStencilView = nullptr; - framebufferDesc.renderTargetViews = rtv.readRef(); - framebufferDesc.layout = framebufferLayout; - GFX_CHECK_CALL_ABORT(device->createFramebuffer(framebufferDesc, framebuffer.writeRef())); + pipelineState = device->createRenderPipeline(pipelineDesc); + SLANG_CHECK_ABORT(pipelineState); } void submitGPUWork( SubresourceRange msaaSubresource, SubresourceRange dstSubresource, - ITextureResource::Extents extent) + Extent3D extent) { - Slang::ComPtr<ITransientResourceHeap> transientHeap; - ITransientResourceHeap::Desc transientHeapDesc = {}; - transientHeapDesc.constantBufferSize = 4096; - GFX_CHECK_CALL_ABORT( - device->createTransientResourceHeap(transientHeapDesc, transientHeap.writeRef())); - - ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics}; - auto queue = device->createCommandQueue(queueDesc); - - auto commandBuffer = transientHeap->createCommandBuffer(); - auto renderEncoder = commandBuffer->encodeRenderCommands(renderPass, framebuffer); + auto queue = device->getQueue(QueueType::Graphics); + + ComPtr<ICommandEncoder> encoder = queue->createCommandEncoder(); + + // Create render target view + TextureViewDesc rtvDesc = {}; + rtvDesc.format = format; + auto rtv = device->createTextureView(msaaTexture, rtvDesc); + + RenderPassColorAttachment colorAttachment = {}; + colorAttachment.view = rtv; + colorAttachment.loadOp = LoadOp::Clear; + colorAttachment.storeOp = StoreOp::Store; + float clearColor[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + memcpy(colorAttachment.clearValue, clearColor, sizeof(clearColor)); + + RenderPassDesc passDesc = {}; + passDesc.colorAttachments = &colorAttachment; + passDesc.colorAttachmentCount = 1; + + auto renderEncoder = encoder->beginRenderPass(passDesc); auto rootObject = renderEncoder->bindPipeline(pipelineState); - gfx::Viewport viewport = {}; + Viewport viewport = {}; viewport.maxZ = 1.0f; viewport.extentX = kWidth; viewport.extentY = kHeight; - renderEncoder->setViewportAndScissor(viewport); - - renderEncoder->setVertexBuffer(0, vertexBuffer); - renderEncoder->setPrimitiveTopology(PrimitiveTopology::TriangleList); - renderEncoder->draw(kVertexCount, 0); - renderEncoder->endEncoding(); - - auto resourceEncoder = commandBuffer->encodeResourceCommands(); - - resourceEncoder->resolveResource( + + RenderState state = {}; + state.viewports[0] = viewport; + state.viewportCount = 1; + state.vertexBuffers[0] = BufferOffsetPair(vertexBuffer, 0); + state.vertexBufferCount = 1; + renderEncoder->setRenderState(state); + + DrawArguments drawArgs = {}; + drawArgs.vertexCount = kVertexCount; + drawArgs.startVertexLocation = 0; + renderEncoder->draw(drawArgs); + renderEncoder->end(); + + // Note: slang-rhi doesn't have a direct resolveResource function + // For MSAA resolve, we would typically use a resolve render pass or blit operation + // For this test, we'll use a simple copy operation instead + encoder->copyTexture( + dstTexture, + dstSubresource, + Offset3D{0, 0, 0}, msaaTexture, - ResourceState::ResolveSource, msaaSubresource, - dstTexture, - ResourceState::ResolveDestination, - dstSubresource); - resourceEncoder->textureSubresourceBarrier( + Offset3D{0, 0, 0}, + extent); + encoder->setTextureState( dstTexture, dstSubresource, - ResourceState::ResolveDestination, ResourceState::CopySource); - resourceEncoder->endEncoding(); - commandBuffer->close(); - queue->executeCommandBuffer(commandBuffer); + + queue->submit(encoder->finish()); queue->waitOnHost(); } @@ -269,12 +243,12 @@ struct BaseResolveResourceTest ComPtr<ISlangBlob> resultBlob; size_t rowPitch = 0; size_t pixelSize = 0; - GFX_CHECK_CALL_ABORT(device->readTextureResource( + GFX_CHECK_CALL_ABORT(device->readTexture( dstTexture, - ResourceState::CopySource, + 0, // layer + 0, // mip resultBlob.writeRef(), - &rowPitch, - &pixelSize)); + nullptr)); // SubresourceLayout output is optional auto result = (float*)resultBlob->getBufferPointer(); int cursor = 0; @@ -297,13 +271,11 @@ struct BaseResolveResourceTest } }; -// TODO: Add more tests? - struct ResolveResourceSimple : BaseResolveResourceTest { void run() { - ITextureResource::Extents extent = {}; + Extent3D extent = {}; extent.width = kWidth; extent.height = kHeight; extent.depth = 1; @@ -314,18 +286,16 @@ struct ResolveResourceSimple : BaseResolveResourceTest createRequiredResources(msaaTextureInfo, dstTextureInfo, format); SubresourceRange msaaSubresource = {}; - msaaSubresource.aspectMask = TextureAspect::Color; - msaaSubresource.mipLevel = 0; - msaaSubresource.mipLevelCount = 1; - msaaSubresource.baseArrayLayer = 0; + msaaSubresource.layer = 0; msaaSubresource.layerCount = 1; + msaaSubresource.mip = 0; + msaaSubresource.mipCount = 1; SubresourceRange dstSubresource = {}; - dstSubresource.aspectMask = TextureAspect::Color; - dstSubresource.mipLevel = 0; - dstSubresource.mipLevelCount = 1; - dstSubresource.baseArrayLayer = 0; + dstSubresource.layer = 0; dstSubresource.layerCount = 1; + dstSubresource.mip = 0; + dstSubresource.mipCount = 1; submitGPUWork(msaaSubresource, dstSubresource, extent); @@ -355,7 +325,7 @@ SLANG_UNIT_TEST(resolveResourceSimpleD3D12) runTestImpl( resolveResourceTestImpl<ResolveResourceSimple>, unitTestContext, - Slang::RenderApiFlag::D3D12); + DeviceType::D3D12); } SLANG_UNIT_TEST(resolveResourceSimpleVulkan) @@ -363,6 +333,8 @@ SLANG_UNIT_TEST(resolveResourceSimpleVulkan) runTestImpl( resolveResourceTestImpl<ResolveResourceSimple>, unitTestContext, - Slang::RenderApiFlag::Vulkan); + DeviceType::Vulkan); } } // namespace gfx_test + +#endif |
