From ed33e05db17882e00437726d5a96281000d0debf Mon Sep 17 00:00:00 2001 From: fujunwei Date: Wed, 8 Dec 2021 05:46:17 +0000 Subject: [PATCH] Move EnumClassBitmasks from wgpu to dawn namespace The EnumClassBitmasks is used by dawn/api_cpp.h that needs to be common. Define a macro to export the operators from dawn to other various namespace. BUG=dawn:1201 Change-Id: I20badd54e844fead6ecf12546a2c9e0afa2fd83f Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/71900 Reviewed-by: Corentin Wallez Reviewed-by: Austin Eng Commit-Queue: Junwei Fu --- generator/templates/api_cpp.h | 22 ++++++++++++------- src/dawn_native/EnumClassBitmasks.h | 13 ++--------- src/dawn_native/Format.h | 4 ++-- src/dawn_native/Subresource.h | 4 ++-- src/include/dawn/EnumClassBitmasks.h | 16 ++++++++++++-- src/tests/end2end/FirstIndexOffsetTests.cpp | 4 ++-- .../unittests/EnumClassBitmasksTests.cpp | 20 ++++++++--------- src/tests/unittests/EnumMaskIteratorTests.cpp | 4 ++-- src/tests/unittests/MathTests.cpp | 11 ++++++---- 9 files changed, 55 insertions(+), 43 deletions(-) diff --git a/generator/templates/api_cpp.h b/generator/templates/api_cpp.h index e351a0a5b7..c70e82720e 100644 --- a/generator/templates/api_cpp.h +++ b/generator/templates/api_cpp.h @@ -46,14 +46,6 @@ namespace {{metadata.namespace}} { {% endfor %} - {% for type in by_category["bitmask"] %} - template<> - struct IsDawnBitmask<{{as_cppType(type.name)}}> { - static constexpr bool enable = true; - }; - - {% endfor %} - {% for type in by_category["function pointer"] %} using {{as_cppType(type.name)}} = {{as_cType(type.name)}}; {% endfor %} @@ -242,6 +234,20 @@ namespace {{metadata.namespace}} { }; {% endfor %} + + // The operators of EnumClassBitmmasks in the dawn:: namespace need to be imported + // in the {{metadata.namespace}} namespace for Argument Dependent Lookup. + DAWN_IMPORT_BITMASK_OPERATORS } // namespace {{metadata.namespace}} +namespace dawn { + {% for type in by_category["bitmask"] %} + template<> + struct IsDawnBitmask<{{metadata.namespace}}::{{as_cppType(type.name)}}> { + static constexpr bool enable = true; + }; + + {% endfor %} +} // namespace dawn + #endif // {{API}}_CPP_H_ diff --git a/src/dawn_native/EnumClassBitmasks.h b/src/dawn_native/EnumClassBitmasks.h index 3227cd2db0..addaab9ab1 100644 --- a/src/dawn_native/EnumClassBitmasks.h +++ b/src/dawn_native/EnumClassBitmasks.h @@ -19,8 +19,9 @@ namespace dawn_native { - // EnumClassBitmmasks is a WebGPU helper in the wgpu:: namespace. + // EnumClassBitmmasks is a helper in the dawn:: namespace. // Re-export it in the dawn_native namespace. + DAWN_IMPORT_BITMASK_OPERATORS // Specify this for usage with EnumMaskIterator template @@ -28,16 +29,6 @@ namespace dawn_native { static constexpr unsigned value = 0; }; - using wgpu::operator|; - using wgpu::operator&; - using wgpu::operator^; - using wgpu::operator~; - using wgpu::operator&=; - using wgpu::operator|=; - using wgpu::operator^=; - - using wgpu::HasZeroOrOneBits; - template constexpr bool HasOneBit(T value) { return HasZeroOrOneBits(value) && value != T(0); diff --git a/src/dawn_native/Format.h b/src/dawn_native/Format.h index f2e83ff823..2f604d366d 100644 --- a/src/dawn_native/Format.h +++ b/src/dawn_native/Format.h @@ -129,13 +129,13 @@ namespace dawn_native { } // namespace dawn_native -namespace wgpu { +namespace dawn { template <> struct IsDawnBitmask { static constexpr bool enable = true; }; -} // namespace wgpu +} // namespace dawn #endif // DAWNNATIVE_FORMAT_H_ diff --git a/src/dawn_native/Subresource.h b/src/dawn_native/Subresource.h index 643b7bc022..454e17c031 100644 --- a/src/dawn_native/Subresource.h +++ b/src/dawn_native/Subresource.h @@ -100,13 +100,13 @@ namespace dawn_native { } // namespace dawn_native -namespace wgpu { +namespace dawn { template <> struct IsDawnBitmask { static constexpr bool enable = true; }; -} // namespace wgpu +} // namespace dawn #endif // DAWNNATIVE_SUBRESOURCE_H_ diff --git a/src/include/dawn/EnumClassBitmasks.h b/src/include/dawn/EnumClassBitmasks.h index 93d2be4574..3947f007a4 100644 --- a/src/include/dawn/EnumClassBitmasks.h +++ b/src/include/dawn/EnumClassBitmasks.h @@ -17,7 +17,19 @@ #include -namespace wgpu { +// The operators in dawn:: namespace need be introduced into other namespaces with +// using-declarations for C++ Argument Dependent Lookup to work. +#define DAWN_IMPORT_BITMASK_OPERATORS \ + using dawn::operator|; \ + using dawn::operator&; \ + using dawn::operator^; \ + using dawn::operator~; \ + using dawn::operator&=; \ + using dawn::operator|=; \ + using dawn::operator^=; \ + using dawn::HasZeroOrOneBits; + +namespace dawn { template struct IsDawnBitmask { @@ -139,6 +151,6 @@ namespace wgpu { return (static_cast(value) & (static_cast(value) - 1)) == 0; } -} // namespace wgpu +} // namespace dawn #endif // DAWN_ENUM_CLASS_BITMASKS_H_ diff --git a/src/tests/end2end/FirstIndexOffsetTests.cpp b/src/tests/end2end/FirstIndexOffsetTests.cpp index 7da6b28a82..5ab3ca3452 100644 --- a/src/tests/end2end/FirstIndexOffsetTests.cpp +++ b/src/tests/end2end/FirstIndexOffsetTests.cpp @@ -34,12 +34,12 @@ enum class CheckIndex : uint32_t { Instance = 0x0000002, }; -namespace wgpu { +namespace dawn { template <> struct IsDawnBitmask { static constexpr bool enable = true; }; -} // namespace wgpu +} // namespace dawn class FirstIndexOffsetTests : public DawnTest { public: diff --git a/src/tests/unittests/EnumClassBitmasksTests.cpp b/src/tests/unittests/EnumClassBitmasksTests.cpp index c87f09f1e8..26849bd568 100644 --- a/src/tests/unittests/EnumClassBitmasksTests.cpp +++ b/src/tests/unittests/EnumClassBitmasksTests.cpp @@ -16,7 +16,7 @@ #include "dawn/EnumClassBitmasks.h" -namespace wgpu { +namespace dawn { enum class Color : uint32_t { R = 1, @@ -80,14 +80,14 @@ namespace wgpu { TEST(BitmaskTests, ZeroOrOneBits) { Color zero = static_cast(0); - ASSERT_TRUE(wgpu::HasZeroOrOneBits(zero)); - ASSERT_TRUE(wgpu::HasZeroOrOneBits(Color::R)); - ASSERT_TRUE(wgpu::HasZeroOrOneBits(Color::G)); - ASSERT_TRUE(wgpu::HasZeroOrOneBits(Color::B)); - ASSERT_TRUE(wgpu::HasZeroOrOneBits(Color::A)); - ASSERT_FALSE(wgpu::HasZeroOrOneBits(static_cast(Color::R | Color::G))); - ASSERT_FALSE(wgpu::HasZeroOrOneBits(static_cast(Color::G | Color::B))); - ASSERT_FALSE(wgpu::HasZeroOrOneBits(static_cast(Color::B | Color::A))); + ASSERT_TRUE(HasZeroOrOneBits(zero)); + ASSERT_TRUE(HasZeroOrOneBits(Color::R)); + ASSERT_TRUE(HasZeroOrOneBits(Color::G)); + ASSERT_TRUE(HasZeroOrOneBits(Color::B)); + ASSERT_TRUE(HasZeroOrOneBits(Color::A)); + ASSERT_FALSE(HasZeroOrOneBits(static_cast(Color::R | Color::G))); + ASSERT_FALSE(HasZeroOrOneBits(static_cast(Color::G | Color::B))); + ASSERT_FALSE(HasZeroOrOneBits(static_cast(Color::B | Color::A))); } -} // namespace wgpu +} // namespace dawn diff --git a/src/tests/unittests/EnumMaskIteratorTests.cpp b/src/tests/unittests/EnumMaskIteratorTests.cpp index 46c351dbd9..d5e0bb58e2 100644 --- a/src/tests/unittests/EnumMaskIteratorTests.cpp +++ b/src/tests/unittests/EnumMaskIteratorTests.cpp @@ -31,14 +31,14 @@ namespace dawn_native { } // namespace dawn_native -namespace wgpu { +namespace dawn { template <> struct IsDawnBitmask { static constexpr bool enable = true; }; -} // namespace wgpu +} // namespace dawn namespace dawn_native { diff --git a/src/tests/unittests/MathTests.cpp b/src/tests/unittests/MathTests.cpp index cd2a50cf80..031c716108 100644 --- a/src/tests/unittests/MathTests.cpp +++ b/src/tests/unittests/MathTests.cpp @@ -18,6 +18,7 @@ #include "dawn/EnumClassBitmasks.h" #include +#include "dawn/webgpu_cpp.h" namespace wgpu { enum class TestEnum { @@ -27,10 +28,12 @@ namespace wgpu { }; } // namespace wgpu -template <> -struct wgpu::IsDawnBitmask { - static constexpr bool enable = true; -}; +namespace dawn { + template <> + struct IsDawnBitmask { + static constexpr bool enable = true; + }; +} // namespace dawn // Tests for ScanForward TEST(Math, ScanForward) {