2019-10-15 12:08:48 +00:00
|
|
|
// Copyright 2019 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 <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include "dawn/dawn_proc.h"
|
|
|
|
#include "dawn_native/Instance.h"
|
|
|
|
#include "dawn_native/null/DeviceNull.h"
|
|
|
|
#include "dawn_wire/WireClient.h"
|
|
|
|
#include "utils/TerribleCommandBuffer.h"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-04-07 07:44:00 +00:00
|
|
|
// dawn_wire and dawn_native contain duplicated code for the handling of GetProcAddress
|
2019-10-15 12:08:48 +00:00
|
|
|
// so we run the tests against both implementations. This enum is used as a test parameters to
|
|
|
|
// know which implementation to test.
|
|
|
|
enum class DawnFlavor {
|
|
|
|
Native,
|
|
|
|
Wire,
|
|
|
|
};
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& stream, DawnFlavor flavor) {
|
|
|
|
switch (flavor) {
|
|
|
|
case DawnFlavor::Native:
|
|
|
|
stream << "dawn_native";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DawnFlavor::Wire:
|
|
|
|
stream << "dawn_wire";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
class GetProcAddressTests : public testing::TestWithParam<DawnFlavor> {
|
|
|
|
public:
|
|
|
|
GetProcAddressTests()
|
|
|
|
: testing::TestWithParam<DawnFlavor>(),
|
2020-01-10 13:06:48 +00:00
|
|
|
mNativeInstance(dawn_native::InstanceBase::Create()),
|
|
|
|
mNativeAdapter(mNativeInstance.Get()) {
|
2019-10-15 12:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetUp() override {
|
|
|
|
switch (GetParam()) {
|
|
|
|
case DawnFlavor::Native: {
|
2019-10-23 11:57:41 +00:00
|
|
|
mDevice = wgpu::Device::Acquire(
|
|
|
|
reinterpret_cast<WGPUDevice>(mNativeAdapter.CreateDevice(nullptr)));
|
2019-10-15 12:08:48 +00:00
|
|
|
mProcs = dawn_native::GetProcs();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case DawnFlavor::Wire: {
|
|
|
|
mC2sBuf = std::make_unique<utils::TerribleCommandBuffer>();
|
|
|
|
|
|
|
|
dawn_wire::WireClientDescriptor clientDesc = {};
|
|
|
|
clientDesc.serializer = mC2sBuf.get();
|
|
|
|
mWireClient = std::make_unique<dawn_wire::WireClient>(clientDesc);
|
|
|
|
|
2021-02-05 23:36:30 +00:00
|
|
|
mDevice = wgpu::Device::Acquire(mWireClient->ReserveDevice().device);
|
2020-10-06 16:13:42 +00:00
|
|
|
mProcs = dawn_wire::client::GetProcs();
|
2019-10-15 12:08:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dawnProcSetProcs(&mProcs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TearDown() override {
|
|
|
|
// Destroy the device before freeing the instance or the wire client in the destructor
|
2019-10-23 11:57:41 +00:00
|
|
|
mDevice = wgpu::Device();
|
2019-10-15 12:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-04-20 17:36:22 +00:00
|
|
|
Ref<dawn_native::InstanceBase> mNativeInstance;
|
2019-10-15 12:08:48 +00:00
|
|
|
dawn_native::null::Adapter mNativeAdapter;
|
|
|
|
|
|
|
|
std::unique_ptr<utils::TerribleCommandBuffer> mC2sBuf;
|
|
|
|
std::unique_ptr<dawn_wire::WireClient> mWireClient;
|
|
|
|
|
2019-10-23 11:57:41 +00:00
|
|
|
wgpu::Device mDevice;
|
2019-10-15 12:08:48 +00:00
|
|
|
DawnProcTable mProcs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test GetProcAddress with and without devices on some valid examples
|
|
|
|
TEST_P(GetProcAddressTests, ValidExamples) {
|
2019-10-21 20:04:10 +00:00
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "wgpuDeviceCreateBuffer"),
|
2019-10-23 11:57:41 +00:00
|
|
|
reinterpret_cast<WGPUProc>(mProcs.deviceCreateBuffer));
|
2019-10-21 20:04:10 +00:00
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "wgpuDeviceCreateBuffer"),
|
2019-10-23 11:57:41 +00:00
|
|
|
reinterpret_cast<WGPUProc>(mProcs.deviceCreateBuffer));
|
2019-10-21 20:04:10 +00:00
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "wgpuQueueSubmit"),
|
2019-10-23 11:57:41 +00:00
|
|
|
reinterpret_cast<WGPUProc>(mProcs.queueSubmit));
|
2019-10-21 20:04:10 +00:00
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "wgpuQueueSubmit"),
|
2019-10-23 11:57:41 +00:00
|
|
|
reinterpret_cast<WGPUProc>(mProcs.queueSubmit));
|
2019-10-15 12:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test GetProcAddress with and without devices on nullptr procName
|
|
|
|
TEST_P(GetProcAddressTests, Nullptr) {
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, nullptr), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), nullptr), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test GetProcAddress with and without devices on some invalid
|
|
|
|
TEST_P(GetProcAddressTests, InvalidExamples) {
|
2019-10-21 20:04:10 +00:00
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "wgpuDeviceDoSomething"), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "wgpuDeviceDoSomething"), nullptr);
|
2019-10-15 12:08:48 +00:00
|
|
|
|
|
|
|
// Trigger the condition where lower_bound will return the end of the procMap.
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "zzzzzzz"), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "zzzzzzz"), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "ZZ"), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "ZZ"), nullptr);
|
|
|
|
|
|
|
|
// Some more potential corner cases.
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, ""), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), ""), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "0"), nullptr);
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "0"), nullptr);
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:06:48 +00:00
|
|
|
// Test that GetProcAddress supports freestanding function that are handled specially
|
|
|
|
TEST_P(GetProcAddressTests, FreeStandingFunctions) {
|
2019-10-21 20:04:10 +00:00
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "wgpuGetProcAddress"),
|
2019-10-23 11:57:41 +00:00
|
|
|
reinterpret_cast<WGPUProc>(mProcs.getProcAddress));
|
2019-10-21 20:04:10 +00:00
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "wgpuGetProcAddress"),
|
2019-10-23 11:57:41 +00:00
|
|
|
reinterpret_cast<WGPUProc>(mProcs.getProcAddress));
|
2020-01-10 13:06:48 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(nullptr, "wgpuCreateInstance"),
|
|
|
|
reinterpret_cast<WGPUProc>(mProcs.createInstance));
|
|
|
|
ASSERT_EQ(mProcs.getProcAddress(mDevice.Get(), "wgpuCreateInstance"),
|
|
|
|
reinterpret_cast<WGPUProc>(mProcs.createInstance));
|
2019-10-15 12:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INSTANTIATE_TEST_SUITE_P(,
|
|
|
|
GetProcAddressTests,
|
|
|
|
testing::Values(DawnFlavor::Native, DawnFlavor::Wire),
|
|
|
|
testing::PrintToStringParamName());
|
|
|
|
|
|
|
|
TEST(GetProcAddressInternalTests, CheckDawnNativeProcMapOrder) {
|
|
|
|
std::vector<const char*> names = dawn_native::GetProcMapNamesForTesting();
|
|
|
|
for (size_t i = 1; i < names.size(); i++) {
|
|
|
|
ASSERT_LT(std::string(names[i - 1]), std::string(names[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GetProcAddressInternalTests, CheckDawnWireClientProcMapOrder) {
|
|
|
|
std::vector<const char*> names = dawn_wire::client::GetProcMapNamesForTesting();
|
|
|
|
for (size_t i = 1; i < names.size(); i++) {
|
|
|
|
ASSERT_LT(std::string(names[i - 1]), std::string(names[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|