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/sampler-array.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/sampler-array.cpp')
| -rw-r--r-- | tools/gfx-unit-test/sampler-array.cpp | 127 |
1 files changed, 49 insertions, 78 deletions
diff --git a/tools/gfx-unit-test/sampler-array.cpp b/tools/gfx-unit-test/sampler-array.cpp index 67efe0c32..6f43be08f 100644 --- a/tools/gfx-unit-test/sampler-array.cpp +++ b/tools/gfx-unit-test/sampler-array.cpp @@ -1,112 +1,83 @@ +// Duplicated: This test is identical to slang-rhi\tests\test-sampler-array.cpp + #include "core/slang-basic.h" #include "gfx-test-util.h" -#include "gfx-util/shader-cursor.h" -#include "slang-gfx.h" +#include "slang-rhi.h" +#include "slang-rhi/shader-cursor.h" #include "unit-test/slang-unit-test.h" -using namespace gfx; +using namespace rhi; namespace gfx_test { -static ComPtr<IBufferResource> createBuffer(IDevice* device, uint32_t content) +static ComPtr<IBuffer> createBuffer(IDevice* device, uint32_t content) { - ComPtr<IBufferResource> buffer; - IBufferResource::Desc bufferDesc = {}; - bufferDesc.sizeInBytes = sizeof(uint32_t); - bufferDesc.format = gfx::Format::Unknown; + ComPtr<IBuffer> buffer; + BufferDesc bufferDesc = {}; + bufferDesc.size = sizeof(uint32_t); + bufferDesc.format = rhi::Format::Undefined; bufferDesc.elementSize = sizeof(float); - bufferDesc.allowedStates = ResourceStateSet( - ResourceState::ShaderResource, - ResourceState::UnorderedAccess, - ResourceState::CopyDestination, - ResourceState::CopySource); + bufferDesc.usage = BufferUsage::ShaderResource | BufferUsage::UnorderedAccess | + BufferUsage::CopyDestination | BufferUsage::CopySource; bufferDesc.defaultState = ResourceState::UnorderedAccess; bufferDesc.memoryType = MemoryType::DeviceLocal; - ComPtr<IBufferResource> numbersBuffer; - GFX_CHECK_CALL_ABORT( - device->createBufferResource(bufferDesc, (void*)&content, buffer.writeRef())); + GFX_CHECK_CALL_ABORT(device->createBuffer(bufferDesc, (void*)&content, buffer.writeRef())); return buffer; } void samplerArrayTestImpl(IDevice* device, UnitTestContext* context) { - Slang::ComPtr<ITransientResourceHeap> transientHeap; - 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( loadComputeProgram(device, shaderProgram, "sampler-array", "computeMain", slangReflection)); - ComputePipelineStateDesc pipelineDesc = {}; + ComputePipelineDesc pipelineDesc = {}; pipelineDesc.program = shaderProgram.get(); - ComPtr<gfx::IPipelineState> pipelineState; - GFX_CHECK_CALL_ABORT( - device->createComputePipelineState(pipelineDesc, pipelineState.writeRef())); + ComPtr<IComputePipeline> pipeline; + GFX_CHECK_CALL_ABORT(device->createComputePipeline(pipelineDesc, pipeline.writeRef())); - Slang::List<ComPtr<ISamplerState>> samplers; - Slang::List<ComPtr<IResourceView>> srvs; - ComPtr<IResourceView> uav; - ComPtr<ITextureResource> texture; - ComPtr<IBufferResource> buffer = createBuffer(device, 0); + Slang::List<ComPtr<ISampler>> samplers; + ComPtr<ITexture> texture; + ComPtr<IBuffer> buffer = createBuffer(device, 0); { - IResourceView::Desc viewDesc = {}; - viewDesc.type = IResourceView::Type::UnorderedAccess; - viewDesc.format = Format::Unknown; - GFX_CHECK_CALL_ABORT(device->createBufferView(buffer, nullptr, viewDesc, uav.writeRef())); - } - { - ITextureResource::Desc textureDesc = {}; - textureDesc.type = IResource::Type::Texture2D; - textureDesc.format = Format::R8G8B8A8_UNORM; + TextureDesc textureDesc = {}; + textureDesc.type = TextureType::Texture2D; + textureDesc.format = Format::RGBA8Unorm; textureDesc.size.width = 2; textureDesc.size.height = 2; textureDesc.size.depth = 1; - textureDesc.numMipLevels = 2; + textureDesc.mipCount = 2; textureDesc.memoryType = MemoryType::DeviceLocal; + textureDesc.usage = TextureUsage::ShaderResource | TextureUsage::CopyDestination; textureDesc.defaultState = ResourceState::ShaderResource; - textureDesc.allowedStates.add(ResourceState::CopyDestination); uint32_t data[] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}; - ITextureResource::SubresourceData subResourceData[2] = {{data, 8, 16}, {data, 8, 16}}; + SubresourceData subResourceData[2] = {{data, 8, 16}, {data, 8, 16}}; GFX_CHECK_CALL_ABORT( - device->createTextureResource(textureDesc, subResourceData, texture.writeRef())); - } - for (uint32_t i = 0; i < 32; i++) - { - ComPtr<IResourceView> srv; - IResourceView::Desc viewDesc = {}; - viewDesc.type = IResourceView::Type::ShaderResource; - viewDesc.format = Format::R8G8B8A8_UNORM; - viewDesc.subresourceRange.layerCount = 1; - viewDesc.subresourceRange.mipLevelCount = 1; - GFX_CHECK_CALL_ABORT(device->createTextureView(texture, viewDesc, srv.writeRef())); - srvs.add(srv); + device->createTexture(textureDesc, subResourceData, texture.writeRef())); } for (uint32_t i = 0; i < 32; i++) { - ISamplerState::Desc desc = {}; - ComPtr<ISamplerState> sampler; - GFX_CHECK_CALL_ABORT(device->createSamplerState(desc, sampler.writeRef())); + SamplerDesc desc = {}; + ComPtr<ISampler> sampler; + GFX_CHECK_CALL_ABORT(device->createSampler(desc, sampler.writeRef())); samplers.add(sampler); } ComPtr<IShaderObject> rootObject; - device->createMutableRootShaderObject(shaderProgram, rootObject.writeRef()); + device->createRootShaderObject(shaderProgram, rootObject.writeRef()); ComPtr<IShaderObject> g; - device->createMutableShaderObject( + device->createShaderObject( slangReflection->findTypeByName("S0"), ShaderObjectContainerType::None, g.writeRef()); ComPtr<IShaderObject> s1; - device->createMutableShaderObject( + device->createShaderObject( slangReflection->findTypeByName("S1"), ShaderObjectContainerType::None, s1.writeRef()); @@ -115,11 +86,12 @@ void samplerArrayTestImpl(IDevice* device, UnitTestContext* context) auto cursor = ShaderCursor(s1); for (uint32_t i = 0; i < 32; i++) { - cursor["samplers"][i].setSampler(samplers[i]); - cursor["tex"][i].setResource(srvs[i]); + cursor["samplers"][i].setBinding(Binding(samplers[i])); + cursor["tex"][i].setBinding(Binding(texture)); } cursor["data"].setData(1.0f); } + s1->finalize(); { auto cursor = ShaderCursor(g); @@ -130,31 +102,30 @@ void samplerArrayTestImpl(IDevice* device, UnitTestContext* context) { auto cursor = ShaderCursor(rootObject); cursor["g"].setObject(g); - cursor["buffer"].setResource(uav); + cursor["buffer"].setBinding(Binding(buffer)); } + g->finalize(); { - ICommandQueue::Desc queueDesc = {ICommandQueue::QueueType::Graphics}; - auto queue = device->createCommandQueue(queueDesc); - - auto commandBuffer = transientHeap->createCommandBuffer(); - { - auto encoder = commandBuffer->encodeComputeCommands(); - encoder->bindPipelineWithRootObject(pipelineState, rootObject); - encoder->dispatchCompute(1, 1, 1); - encoder->endEncoding(); - } + auto queue = device->getQueue(QueueType::Graphics); + auto commandEncoder = queue->createCommandEncoder(); + auto passEncoder = commandEncoder->beginComputePass(); + auto rootObject = passEncoder->bindPipeline(pipeline); + auto cursor = ShaderCursor(rootObject); + cursor["g"].setObject(g); + cursor["buffer"].setBinding(buffer); + passEncoder->dispatchCompute(1, 1, 1); + passEncoder->end(); - commandBuffer->close(); - queue->executeCommandBuffer(commandBuffer); + queue->submit(commandEncoder->finish()); queue->waitOnHost(); } - compareComputeResult(device, buffer, Slang::makeArray<float>(4.0f)); + compareComputeResult(device, buffer, std::array{4.0f}); } SLANG_UNIT_TEST(samplerArrayVulkan) { - runTestImpl(samplerArrayTestImpl, unitTestContext, Slang::RenderApiFlag::Vulkan); + runTestImpl(samplerArrayTestImpl, unitTestContext, DeviceType::Vulkan); } } // namespace gfx_test |
