| // Copyright 2025 The Dawn & Tint Authors |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // 1. Redistributions of source code must retain the above copyright notice, this |
| // list of conditions and the following disclaimer. |
| // |
| // 2. Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // |
| // 3. Neither the name of the copyright holder nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| #include "dawn/common/Assert.h" |
| #include "dawn/native/ComputePipeline.h" |
| #include "dawn/native/ImmediateConstantsLayout.h" |
| #include "dawn/native/ImmediateConstantsTracker.h" |
| #include "dawn/native/RenderPipeline.h" |
| #include "dawn/tests/DawnNativeTest.h" |
| #include "dawn/utils/WGPUHelpers.h" |
| |
| namespace dawn::native { |
| namespace { |
| class ImmediateConstantsTrackerTest : public DawnNativeTest {}; |
| |
| class RenderImmediateConstantsTrackerTest : public ImmediateConstantsTrackerTest {}; |
| |
| class ComputeImmediateConstantsTrackerTest : public ImmediateConstantsTrackerTest {}; |
| |
| // Test immediate setting update dirty bits and contents correctly. |
| TEST_F(ImmediateConstantsTrackerTest, SetImmediateData) { |
| static constexpr uint32_t rangeOffset = 1u; |
| static constexpr uint32_t dataOffset = 2u; |
| static constexpr uint32_t userImmediateDataCount = 2u; |
| ImmediateConstantMask expected = |
| GetImmediateConstantBlockBits(0u, sizeof(UserImmediateConstants)); |
| |
| size_t userImmediateDataStartByteOffset = 0u; |
| // RenderImmediateConstantsTracker |
| { |
| RenderImmediateConstantsTrackerBase tracker; |
| int32_t userImmediateData[] = {2, 4, -6, 8}; |
| tracker.SetImmediateData(rangeOffset, |
| reinterpret_cast<uint32_t*>(&userImmediateData[dataOffset]), |
| userImmediateDataCount); |
| EXPECT_TRUE(tracker.GetDirtyBits() == expected); |
| |
| uint32_t userImmediateDataRangeOffset = |
| userImmediateDataStartByteOffset + rangeOffset * kImmediateConstantElementByteSize; |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<int32_t>(userImmediateDataRangeOffset), |
| &userImmediateData[dataOffset], |
| sizeof(int32_t) * userImmediateDataCount) == 0); |
| } |
| |
| // ComputeImmediateConstantsTracker |
| { |
| ComputeImmediateConstantsTrackerBase tracker; |
| int32_t userImmediateData[] = {2, 4, -6, 8}; |
| tracker.SetImmediateData(rangeOffset, |
| reinterpret_cast<uint32_t*>(&userImmediateData[dataOffset]), |
| userImmediateDataCount); |
| EXPECT_TRUE(tracker.GetDirtyBits() == expected); |
| |
| uint32_t userImmediateDataRangeOffset = |
| userImmediateDataStartByteOffset + rangeOffset * kImmediateConstantElementByteSize; |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<int32_t>(userImmediateDataRangeOffset), |
| &userImmediateData[dataOffset], |
| sizeof(int32_t) * userImmediateDataCount) == 0); |
| } |
| |
| device.Destroy(); |
| } |
| |
| // Test setting clamp frag depth args with float value updates dirty bits and contents correctly. |
| TEST_F(RenderImmediateConstantsTrackerTest, SetClampFragDepth) { |
| RenderImmediateConstantsTrackerBase tracker; |
| float minClampFragDepth = 0.1; |
| float maxClampFragDepth = 0.95; |
| tracker.SetClampFragDepth(minClampFragDepth, maxClampFragDepth); |
| |
| ImmediateConstantMask expected; |
| // Hard coded to verify dirty bit. |
| expected |= 1u << 4u; |
| expected |= 1u << 5u; |
| EXPECT_TRUE(tracker.GetDirtyBits() == expected); |
| |
| // Compare bits instead of values here to ensure bits level equality. |
| size_t clampFragDepthStartOffsetBytes = offsetof(RenderImmediateConstants, clampFragDepth); |
| size_t minClampFragDepthOffsetBytes = |
| clampFragDepthStartOffsetBytes + offsetof(ClampFragDepthArgs, minClampFragDepth); |
| size_t maxClampFragDepthOffsetBytes = |
| clampFragDepthStartOffsetBytes + offsetof(ClampFragDepthArgs, maxClampFragDepth); |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<float>(minClampFragDepthOffsetBytes), |
| &minClampFragDepth, sizeof(float)) == 0); |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<float>(maxClampFragDepthOffsetBytes), |
| &maxClampFragDepth, sizeof(float)) == 0); |
| |
| device.Destroy(); |
| } |
| |
| // Test setting first index offset args updates dirty bits and contents correctly. |
| TEST_F(RenderImmediateConstantsTrackerTest, SetFirstIndexOffset) { |
| size_t firstVertexByteOffset = offsetof(RenderImmediateConstants, firstVertex); |
| size_t firstInstanceByteOffset = offsetof(RenderImmediateConstants, firstInstance); |
| // SetFirstIndexOffset() |
| { |
| RenderImmediateConstantsTrackerBase tracker; |
| uint32_t firstVertex = 1; |
| uint32_t firstInstance = 2; |
| tracker.SetFirstIndexOffset(firstVertex, firstInstance); |
| |
| ImmediateConstantMask expected; |
| // Hard coded to verify dirty bit. |
| expected |= 1u << 6u; |
| expected |= 1u << 7u; |
| EXPECT_TRUE(tracker.GetDirtyBits() == expected); |
| |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<uint32_t>(firstVertexByteOffset), &firstVertex, |
| sizeof(uint32_t)) == 0); |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<uint32_t>(firstInstanceByteOffset), |
| &firstInstance, sizeof(uint32_t)) == 0); |
| } |
| |
| // SetFirstVertex() |
| { |
| RenderImmediateConstantsTrackerBase tracker; |
| uint32_t firstVertex = 1; |
| tracker.SetFirstVertex(firstVertex); |
| |
| ImmediateConstantMask expected; |
| // Hard coded to verify dirty bit. |
| expected |= 1u << 6u; |
| EXPECT_TRUE(tracker.GetDirtyBits() == expected); |
| |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<uint32_t>(firstVertexByteOffset), &firstVertex, |
| sizeof(uint32_t)) == 0); |
| } |
| |
| // SetFirstInstance() |
| { |
| RenderImmediateConstantsTrackerBase tracker; |
| uint32_t firstInstance = 2; |
| tracker.SetFirstInstance(firstInstance); |
| |
| ImmediateConstantMask expected; |
| // Hard coded to verify dirty bit. |
| expected |= 1u << 7u; |
| EXPECT_TRUE(tracker.GetDirtyBits() == expected); |
| |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<uint32_t>(firstInstanceByteOffset), |
| &firstInstance, sizeof(uint32_t)) == 0); |
| } |
| |
| device.Destroy(); |
| } |
| |
| // Test setting num workgroups dimensions update dirty bits and contents correctly. |
| TEST_F(ComputeImmediateConstantsTrackerTest, SetNumWorkgroupDimensions) { |
| ComputeImmediateConstantsTrackerBase tracker; |
| uint32_t numWorkgroupsX = 256; |
| uint32_t numWorkgroupsY = 128; |
| uint32_t numWorkgroupsZ = 64; |
| tracker.SetNumWorkgroups(256, 128, 64); |
| |
| ImmediateConstantMask expected; |
| // Hard coded to verify dirty bit. |
| expected |= 1u << 4u; |
| expected |= 1u << 5u; |
| expected |= 1u << 6u; |
| EXPECT_TRUE(tracker.GetDirtyBits() == expected); |
| |
| size_t numWorkgroupsStartByteOffset = offsetof(ComputeImmediateConstants, numWorkgroups); |
| size_t numWorkgroupXByteOffset = |
| numWorkgroupsStartByteOffset + offsetof(NumWorkgroupsDimensions, numWorkgroupsX); |
| size_t numWorkgroupYByteOffset = |
| numWorkgroupsStartByteOffset + offsetof(NumWorkgroupsDimensions, numWorkgroupsY); |
| size_t numWorkgroupZByteOffset = |
| numWorkgroupsStartByteOffset + offsetof(NumWorkgroupsDimensions, numWorkgroupsZ); |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<uint32_t>(numWorkgroupXByteOffset), &numWorkgroupsX, |
| sizeof(uint32_t)) == 0); |
| |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<uint32_t>(numWorkgroupYByteOffset), &numWorkgroupsY, |
| sizeof(uint32_t)) == 0); |
| EXPECT_TRUE(memcmp(tracker.GetContent().Get<uint32_t>(numWorkgroupZByteOffset), &numWorkgroupsZ, |
| sizeof(uint32_t)) == 0); |
| device.Destroy(); |
| } |
| |
| } // anonymous namespace |
| } // namespace dawn::native |