yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Gangzheng TongConvert gfx unit tests and examples to use slang-rhi (#7577)43d0c2100

master
4.3 KiB131 linesraw
1// Duplicated: This test is identical to slang-rhi\tests\test-sampler-array.cpp
2
3#include "core/slang-basic.h"
4#include "gfx-test-util.h"
5#include "slang-rhi.h"
6#include "slang-rhi/shader-cursor.h"
7#include "unit-test/slang-unit-test.h"
8
9using namespace rhi;
10
11namespace gfx_test
12{
13static ComPtr<IBuffer> createBuffer(IDevice* device, uint32_t content)
14{
15    ComPtr<IBuffer> buffer;
16    BufferDesc bufferDesc = {};
17    bufferDesc.size = sizeof(uint32_t);
18    bufferDesc.format = rhi::Format::Undefined;
19    bufferDesc.elementSize = sizeof(float);
20    bufferDesc.usage = BufferUsage::ShaderResource | BufferUsage::UnorderedAccess |
21                       BufferUsage::CopyDestination | BufferUsage::CopySource;
22    bufferDesc.defaultState = ResourceState::UnorderedAccess;
23    bufferDesc.memoryType = MemoryType::DeviceLocal;
24
25    GFX_CHECK_CALL_ABORT(device->createBuffer(bufferDesc, (void*)&content, buffer.writeRef()));
26
27    return buffer;
28}
29void samplerArrayTestImpl(IDevice* device, UnitTestContext* context)
30{
31    ComPtr<IShaderProgram> shaderProgram;
32    slang::ProgramLayout* slangReflection;
33    GFX_CHECK_CALL_ABORT(
34        loadComputeProgram(device, shaderProgram, "sampler-array", "computeMain", slangReflection));
35
36    ComputePipelineDesc pipelineDesc = {};
37    pipelineDesc.program = shaderProgram.get();
38    ComPtr<IComputePipeline> pipeline;
39    GFX_CHECK_CALL_ABORT(device->createComputePipeline(pipelineDesc, pipeline.writeRef()));
40
41    Slang::List<ComPtr<ISampler>> samplers;
42    ComPtr<ITexture> texture;
43    ComPtr<IBuffer> buffer = createBuffer(device, 0);
44
45    {
46        TextureDesc textureDesc = {};
47        textureDesc.type = TextureType::Texture2D;
48        textureDesc.format = Format::RGBA8Unorm;
49        textureDesc.size.width = 2;
50        textureDesc.size.height = 2;
51        textureDesc.size.depth = 1;
52        textureDesc.mipCount = 2;
53        textureDesc.memoryType = MemoryType::DeviceLocal;
54        textureDesc.usage = TextureUsage::ShaderResource | TextureUsage::CopyDestination;
55        textureDesc.defaultState = ResourceState::ShaderResource;
56        uint32_t data[] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
57        SubresourceData subResourceData[2] = {{data, 8, 16}, {data, 8, 16}};
58        GFX_CHECK_CALL_ABORT(
59            device->createTexture(textureDesc, subResourceData, texture.writeRef()));
60    }
61
62    for (uint32_t i = 0; i < 32; i++)
63    {
64        SamplerDesc desc = {};
65        ComPtr<ISampler> sampler;
66        GFX_CHECK_CALL_ABORT(device->createSampler(desc, sampler.writeRef()));
67        samplers.add(sampler);
68    }
69
70    ComPtr<IShaderObject> rootObject;
71    device->createRootShaderObject(shaderProgram, rootObject.writeRef());
72
73    ComPtr<IShaderObject> g;
74    device->createShaderObject(
75        slangReflection->findTypeByName("S0"),
76        ShaderObjectContainerType::None,
77        g.writeRef());
78
79    ComPtr<IShaderObject> s1;
80    device->createShaderObject(
81        slangReflection->findTypeByName("S1"),
82        ShaderObjectContainerType::None,
83        s1.writeRef());
84
85    {
86        auto cursor = ShaderCursor(s1);
87        for (uint32_t i = 0; i < 32; i++)
88        {
89            cursor["samplers"][i].setBinding(Binding(samplers[i]));
90            cursor["tex"][i].setBinding(Binding(texture));
91        }
92        cursor["data"].setData(1.0f);
93    }
94    s1->finalize();
95
96    {
97        auto cursor = ShaderCursor(g);
98        cursor["s"].setObject(s1);
99        cursor["data"].setData(2.0f);
100    }
101
102    {
103        auto cursor = ShaderCursor(rootObject);
104        cursor["g"].setObject(g);
105        cursor["buffer"].setBinding(Binding(buffer));
106    }
107    g->finalize();
108
109    {
110        auto queue = device->getQueue(QueueType::Graphics);
111        auto commandEncoder = queue->createCommandEncoder();
112        auto passEncoder = commandEncoder->beginComputePass();
113        auto rootObject = passEncoder->bindPipeline(pipeline);
114        auto cursor = ShaderCursor(rootObject);
115        cursor["g"].setObject(g);
116        cursor["buffer"].setBinding(buffer);
117        passEncoder->dispatchCompute(1, 1, 1);
118        passEncoder->end();
119
120        queue->submit(commandEncoder->finish());
121        queue->waitOnHost();
122    }
123
124    compareComputeResult(device, buffer, std::array{4.0f});
125}
126
127SLANG_UNIT_TEST(samplerArrayVulkan)
128{
129    runTestImpl(samplerArrayTestImpl, unitTestContext, DeviceType::Vulkan);
130}
131} // namespace gfx_test