diff --git a/src/common/BUILD.gn b/src/common/BUILD.gn index ee6c218fb4..a401a72de3 100644 --- a/src/common/BUILD.gn +++ b/src/common/BUILD.gn @@ -198,7 +198,6 @@ if (is_win || is_linux || is_chromeos || is_mac || is_fuchsia || is_android) { "ityp_stack_vec.h", "ityp_vector.h", "vulkan_platform.h", - "windows_with_undefs.h", "xlib_with_undefs.h", ] @@ -207,6 +206,14 @@ if (is_win || is_linux || is_chromeos || is_mac || is_fuchsia || is_android) { "${dawn_root}/src/dawn:dawn_headers", "${dawn_root}/src/dawn:dawncpp_headers", ] + + if (is_win) { + sources += [ + "WindowsUtils.cpp", + "WindowsUtils.h", + "windows_with_undefs.h", + ] + } if (dawn_enable_vulkan) { public_deps = [ "${dawn_root}/third_party/khronos:vulkan_headers" ] } diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 7d8b7719ba..a49e6827f8 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -61,9 +61,17 @@ target_sources(dawn_common PRIVATE "ityp_stack_vec.h" "ityp_vector.h" "vulkan_platform.h" - "windows_with_undefs.h" "xlib_with_undefs.h" ) + +if (WIN32) + target_sources(dawn_common PRIVATE + "WindowsUtils.cpp" + "WindowsUtils.h" + "windows_with_undefs.h" + ) +endif() + target_link_libraries(dawn_common PRIVATE dawn_internal_config) # TODO Android Log support diff --git a/src/common/WindowsUtils.cpp b/src/common/WindowsUtils.cpp new file mode 100644 index 0000000000..0f9b9852fc --- /dev/null +++ b/src/common/WindowsUtils.cpp @@ -0,0 +1,32 @@ +// Copyright 2021 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "common/WindowsUtils.h" + +#include "common/windows_with_undefs.h" + +#include + +std::string WCharToUTF8(const wchar_t* input) { + // The -1 argument asks WideCharToMultiByte to use the null terminator to know the size of + // input. It will return a size that includes the null terminator. + int requiredSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, nullptr, 0, nullptr, nullptr); + + // When we can use C++17 this can be changed to use string.data() instead. + std::unique_ptr result = std::make_unique(requiredSize); + WideCharToMultiByte(CP_UTF8, 0, input, -1, result.get(), requiredSize, nullptr, nullptr); + + // This will allocate the returned std::string and then destroy result. + return std::string(result.get(), result.get() + (requiredSize - 1)); +} diff --git a/src/common/WindowsUtils.h b/src/common/WindowsUtils.h new file mode 100644 index 0000000000..0c43d08aa4 --- /dev/null +++ b/src/common/WindowsUtils.h @@ -0,0 +1,22 @@ +// Copyright 2021 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef COMMON_WINDOWSUTILS_H_ +#define COMMON_WINDOWSUTILS_H_ + +#include + +std::string WCharToUTF8(const wchar_t* input); + +#endif // COMMON_WINDOWSUTILS_H_ diff --git a/src/dawn_native/d3d12/AdapterD3D12.cpp b/src/dawn_native/d3d12/AdapterD3D12.cpp index 775e32da18..0fe3714fd7 100644 --- a/src/dawn_native/d3d12/AdapterD3D12.cpp +++ b/src/dawn_native/d3d12/AdapterD3D12.cpp @@ -15,28 +15,17 @@ #include "dawn_native/d3d12/AdapterD3D12.h" #include "common/Constants.h" +#include "common/WindowsUtils.h" #include "dawn_native/Instance.h" #include "dawn_native/d3d12/BackendD3D12.h" #include "dawn_native/d3d12/D3D12Error.h" #include "dawn_native/d3d12/DeviceD3D12.h" #include "dawn_native/d3d12/PlatformFunctions.h" -#include #include namespace dawn_native { namespace d3d12 { - // utility wrapper to adapt locale-bound facets for wstring/wbuffer convert - template - struct DeletableFacet : Facet { - template - DeletableFacet(Args&&... args) : Facet(std::forward(args)...) { - } - - ~DeletableFacet() { - } - }; - Adapter::Adapter(Backend* backend, ComPtr hardwareAdapter) : AdapterBase(backend->GetInstance(), wgpu::BackendType::D3D12), mHardwareAdapter(hardwareAdapter), @@ -80,6 +69,7 @@ namespace dawn_native { namespace d3d12 { mPCIInfo.deviceId = adapterDesc.DeviceId; mPCIInfo.vendorId = adapterDesc.VendorId; + mPCIInfo.name = WCharToUTF8(adapterDesc.Description); DAWN_TRY_ASSIGN(mDeviceInfo, GatherDeviceInfo(*this)); @@ -90,11 +80,6 @@ namespace dawn_native { namespace d3d12 { : wgpu::AdapterType::DiscreteGPU; } - // Get the adapter's name as a UTF8 string. - std::wstring_convert>> converter( - "Error converting"); - mPCIInfo.name = converter.to_bytes(adapterDesc.Description); - // Convert the adapter's D3D12 driver version to a readable string like "24.21.13.9793". LARGE_INTEGER umdVersion; if (mHardwareAdapter->CheckInterfaceSupport(__uuidof(IDXGIDevice), &umdVersion) != diff --git a/src/tests/BUILD.gn b/src/tests/BUILD.gn index dd1d74f6b1..f7924e992a 100644 --- a/src/tests/BUILD.gn +++ b/src/tests/BUILD.gn @@ -238,6 +238,10 @@ test("dawn_unittests") { "unittests/wire/WireWGPUDevicePropertiesTests.cpp", ] + if (is_win) { + sources += [ "unittests/WindowsUtilsTests.cpp" ] + } + if (dawn_enable_d3d12) { sources += [ "unittests/d3d12/CopySplitTests.cpp" ] } diff --git a/src/tests/unittests/WindowsUtilsTests.cpp b/src/tests/unittests/WindowsUtilsTests.cpp new file mode 100644 index 0000000000..7bc4cd75b9 --- /dev/null +++ b/src/tests/unittests/WindowsUtilsTests.cpp @@ -0,0 +1,34 @@ +// Copyright 2021 The Dawn Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include "common/WindowsUtils.h" + +TEST(WindowsUtilsTests, WCharToUTF8) { + // Test the empty string + ASSERT_EQ("", WCharToUTF8(L"")); + + // Test ASCII characters + ASSERT_EQ("abc", WCharToUTF8(L"abc")); + + // Test ASCII characters + ASSERT_EQ("abc", WCharToUTF8(L"abc")); + + // Test two-byte utf8 character + ASSERT_EQ("\xd1\x90", WCharToUTF8(L"\x450")); + + // Test three-byte utf8 codepoint + ASSERT_EQ("\xe1\x81\x90", WCharToUTF8(L"\x1050")); +} \ No newline at end of file