123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459 |
- // Copyright 2019 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "gpu/command_buffer/tests/webgpu_test.h"
- #include <dawn/dawn_proc.h>
- #include <dawn/webgpu.h>
- #include "base/bind.h"
- #include "base/command_line.h"
- #include "base/test/test_simple_task_runner.h"
- #include "build/build_config.h"
- #include "components/viz/test/test_gpu_service_holder.h"
- #include "gpu/command_buffer/client/webgpu_cmd_helper.h"
- #include "gpu/command_buffer/client/webgpu_implementation.h"
- #include "gpu/command_buffer/service/service_utils.h"
- #include "gpu/command_buffer/service/webgpu_decoder.h"
- #include "gpu/config/gpu_test_config.h"
- #include "gpu/ipc/host/gpu_memory_buffer_support.h"
- #include "gpu/ipc/in_process_command_buffer.h"
- #include "gpu/ipc/webgpu_in_process_context.h"
- #include "gpu/webgpu/callback.h"
- #include "testing/gmock/include/gmock/gmock-matchers.h"
- #include "testing/gtest/include/gtest/gtest.h"
- #if BUILDFLAG(IS_MAC)
- #include "gpu/command_buffer/tests/gl_manager.h"
- #include "ui/gl/gl_context.h"
- #endif
- namespace gpu {
- namespace {
- void CountCallback(int* count) {
- (*count)++;
- }
- } // anonymous namespace
- WebGPUTest::Options::Options() = default;
- WebGPUTest::WebGPUTest() = default;
- WebGPUTest::~WebGPUTest() = default;
- bool WebGPUTest::WebGPUSupported() const {
- // TODO(crbug.com/1172447): Re-enable on AMD when the RX 5500 XT issues are
- // resolved.
- // Win7 does not support WebGPU
- if (GPUTestBotConfig::CurrentConfigMatches("Linux AMD") ||
- GPUTestBotConfig::CurrentConfigMatches("Win7")) {
- return false;
- }
- return true;
- }
- bool WebGPUTest::WebGPUSharedImageSupported() const {
- // Currently WebGPUSharedImage is only implemented on Mac, Linux, Windows
- // and ChromeOS.
- #if (BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
- BUILDFLAG(IS_WIN)) && \
- BUILDFLAG(USE_DAWN)
- return true;
- #else
- return false;
- #endif
- }
- void WebGPUTest::SetUp() {
- if (!WebGPUSupported()) {
- return;
- }
- }
- void WebGPUTest::TearDown() {
- adapter_ = nullptr;
- instance_ = nullptr;
- context_ = nullptr;
- }
- void WebGPUTest::Initialize(const Options& options) {
- if (!WebGPUSupported()) {
- return;
- }
- gpu::GpuPreferences gpu_preferences;
- gpu_preferences.enable_webgpu = true;
- gpu_preferences.use_passthrough_cmd_decoder =
- gles2::UsePassthroughCommandDecoder(
- base::CommandLine::ForCurrentProcess());
- #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && BUILDFLAG(USE_DAWN)
- gpu_preferences.use_vulkan = gpu::VulkanImplementationName::kNative;
- gpu_preferences.gr_context_type = gpu::GrContextType::kVulkan;
- #endif
- gpu_preferences.enable_unsafe_webgpu = options.enable_unsafe_webgpu;
- gpu_preferences.texture_target_exception_list =
- gpu::CreateBufferUsageAndFormatExceptionList();
- gpu_service_holder_ =
- std::make_unique<viz::TestGpuServiceHolder>(gpu_preferences);
- ContextCreationAttribs attributes;
- attributes.bind_generates_resource = false;
- attributes.enable_gles2_interface = false;
- attributes.context_type = CONTEXT_TYPE_WEBGPU;
- #if BUILDFLAG(IS_MAC)
- ImageFactory* image_factory = &image_factory_;
- #else
- static constexpr ImageFactory* image_factory = nullptr;
- #endif
- context_ = std::make_unique<WebGPUInProcessContext>();
- ContextResult result =
- context_->Initialize(gpu_service_holder_->task_executor(), attributes,
- options.shared_memory_limits, image_factory);
- ASSERT_EQ(result, ContextResult::kSuccess);
- cmd_helper_ = std::make_unique<webgpu::WebGPUCmdHelper>(
- context_->GetCommandBufferForTest());
- DawnProcTable procs = webgpu()->GetAPIChannel()->GetProcs();
- dawnProcSetProcs(&procs);
- instance_ = wgpu::Instance(webgpu()->GetAPIChannel()->GetWGPUInstance());
- wgpu::RequestAdapterOptions ra_options = {};
- ra_options.forceFallbackAdapter = options.force_fallback_adapter;
- bool done = false;
- auto* callback = webgpu::BindWGPUOnceCallback(
- [](WebGPUTest* test, bool force_fallback_adapter, bool* done,
- WGPURequestAdapterStatus status, WGPUAdapter adapter,
- const char* message) {
- if (!force_fallback_adapter) {
- // If we don't force a particular adapter, we should always find
- // one.
- EXPECT_EQ(status, WGPURequestAdapterStatus_Success);
- EXPECT_NE(adapter, nullptr);
- }
- test->adapter_ = wgpu::Adapter::Acquire(adapter);
- *done = true;
- },
- this, options.force_fallback_adapter, &done);
- instance_.RequestAdapter(&ra_options, callback->UnboundCallback(),
- callback->AsUserdata());
- webgpu()->FlushCommands();
- while (!done) {
- RunPendingTasks();
- }
- }
- webgpu::WebGPUImplementation* WebGPUTest::webgpu() const {
- return context_->GetImplementation();
- }
- webgpu::WebGPUCmdHelper* WebGPUTest::webgpu_cmds() const {
- return cmd_helper_.get();
- }
- SharedImageInterface* WebGPUTest::GetSharedImageInterface() const {
- return context_->GetCommandBufferForTest()->GetSharedImageInterface();
- }
- webgpu::WebGPUDecoder* WebGPUTest::GetDecoder() const {
- return context_->GetCommandBufferForTest()->GetWebGPUDecoderForTest();
- }
- void WebGPUTest::RunPendingTasks() {
- context_->GetTaskRunner()->RunPendingTasks();
- gpu_service_holder_->ScheduleGpuTask(base::BindOnce(
- [](webgpu::WebGPUDecoder* decoder) {
- if (decoder->HasPollingWork()) {
- decoder->PerformPollingWork();
- }
- },
- GetDecoder()));
- }
- void WebGPUTest::WaitForCompletion(wgpu::Device device) {
- // Wait for any work submitted to the queue to be finished. The guarantees of
- // Dawn are that all previous operations will have been completed and more
- // importantly the callbacks will have been called.
- wgpu::Queue queue = device.GetQueue();
- bool done = false;
- queue.OnSubmittedWorkDone(
- 0u,
- [](WGPUQueueWorkDoneStatus, void* userdata) {
- *static_cast<bool*>(userdata) = true;
- },
- &done);
- while (!done) {
- device.Tick();
- webgpu()->FlushCommands();
- RunPendingTasks();
- }
- }
- wgpu::Device WebGPUTest::GetNewDevice() {
- wgpu::Device device;
- bool done = false;
- auto* callback = webgpu::BindWGPUOnceCallback(
- [](wgpu::Device* device_out, bool* done, WGPURequestDeviceStatus status,
- WGPUDevice device, const char* message) {
- *device_out = wgpu::Device::Acquire(device);
- *done = true;
- },
- &device, &done);
- DCHECK(adapter_);
- wgpu::DeviceDescriptor device_desc = {};
- adapter_.RequestDevice(&device_desc, callback->UnboundCallback(),
- callback->AsUserdata());
- webgpu()->FlushCommands();
- while (!done) {
- RunPendingTasks();
- }
- EXPECT_NE(device, nullptr);
- return device;
- }
- TEST_F(WebGPUTest, FlushNoCommands) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- Initialize(WebGPUTest::Options());
- webgpu()->FlushCommands();
- }
- // Referred from GLES2ImplementationTest/ReportLoss
- TEST_F(WebGPUTest, ReportLoss) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- Initialize(WebGPUTest::Options());
- GpuControlClient* webgpu_as_client = webgpu();
- int lost_count = 0;
- webgpu()->SetLostContextCallback(base::BindOnce(&CountCallback, &lost_count));
- EXPECT_EQ(0, lost_count);
- webgpu_as_client->OnGpuControlLostContext();
- // The lost context callback should be run when WebGPUImplementation is
- // notified of the loss.
- EXPECT_EQ(1, lost_count);
- }
- // Referred from GLES2ImplementationTest/ReportLossReentrant
- TEST_F(WebGPUTest, ReportLossReentrant) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- Initialize(WebGPUTest::Options());
- GpuControlClient* webgpu_as_client = webgpu();
- int lost_count = 0;
- webgpu()->SetLostContextCallback(base::BindOnce(&CountCallback, &lost_count));
- EXPECT_EQ(0, lost_count);
- webgpu_as_client->OnGpuControlLostContextMaybeReentrant();
- // The lost context callback should not be run yet to avoid calling back into
- // clients re-entrantly, and having them re-enter WebGPUImplementation.
- EXPECT_EQ(0, lost_count);
- }
- TEST_F(WebGPUTest, RequestAdapterAfterContextLost) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- Initialize(WebGPUTest::Options());
- webgpu()->OnGpuControlLostContext();
- bool called = false;
- wgpu::RequestAdapterOptions ra_options = {};
- instance_.RequestAdapter(
- &ra_options,
- [](WGPURequestAdapterStatus status, WGPUAdapter adapter,
- const char* message, void* userdata) {
- EXPECT_EQ(adapter, nullptr);
- *static_cast<bool*>(userdata) = true;
- },
- &called);
- webgpu()->FlushCommands();
- RunPendingTasks();
- EXPECT_TRUE(called);
- }
- TEST_F(WebGPUTest, RequestDeviceAfterContextLost) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- Initialize(WebGPUTest::Options());
- webgpu()->OnGpuControlLostContext();
- bool called = false;
- DCHECK(adapter_);
- wgpu::DeviceDescriptor device_desc = {};
- adapter_.RequestDevice(
- &device_desc,
- [](WGPURequestDeviceStatus status, WGPUDevice device, const char* message,
- void* userdata) {
- EXPECT_EQ(device, nullptr);
- *static_cast<bool*>(userdata) = true;
- },
- &called);
- webgpu()->FlushCommands();
- RunPendingTasks();
- EXPECT_TRUE(called);
- }
- TEST_F(WebGPUTest, RequestDeviceWitUnsupportedFeature) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- #if BUILDFLAG(IS_MAC)
- // Crashing on Mac M1. Currently missing stack trace. crbug.com/1271926
- // This must be checked before WebGPUTest::Initialize otherwise context
- // switched is locked and we cannot temporarily have this GLContext.
- GLManager gl_manager;
- gl_manager.Initialize(GLManager::Options());
- std::string renderer(gl_manager.context()->GetGLRenderer());
- if (renderer.find("Apple M1") != std::string::npos) {
- gl_manager.Destroy();
- return;
- }
- gl_manager.Destroy();
- #endif
- Initialize(WebGPUTest::Options());
- // Create device with unsupported features, expect to fail to create and
- // return nullptr
- wgpu::FeatureName invalid_feature = static_cast<wgpu::FeatureName>(-2);
- wgpu::Device device;
- bool done = false;
- auto* callback = webgpu::BindWGPUOnceCallback(
- [](wgpu::Device* device_out, bool* done, WGPURequestDeviceStatus status,
- WGPUDevice device, const char* message) {
- *device_out = wgpu::Device::Acquire(device);
- *done = true;
- },
- &device, &done);
- DCHECK(adapter_);
- wgpu::DeviceDescriptor device_desc = {};
- device_desc.requiredFeaturesCount = 1;
- device_desc.requiredFeatures = &invalid_feature;
- adapter_.RequestDevice(&device_desc, callback->UnboundCallback(),
- callback->AsUserdata());
- webgpu()->FlushCommands();
- while (!done) {
- RunPendingTasks();
- }
- EXPECT_EQ(device, nullptr);
- // Create device again with supported features, expect success and not
- // blocked by the last failure
- GetNewDevice();
- }
- TEST_F(WebGPUTest, SPIRVIsDisallowed) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- auto ExpectSPIRVDisallowedError = [](WGPUErrorType type, const char* message,
- void* userdata) {
- // We match on this string to make sure the shader module creation fails
- // because SPIR-V is disallowed and not because codeSize=0.
- EXPECT_THAT(message, testing::HasSubstr("SPIR-V is disallowed"));
- EXPECT_EQ(type, WGPUErrorType_Validation);
- *static_cast<bool*>(userdata) = true;
- };
- auto options = WebGPUTest::Options();
- options.enable_unsafe_webgpu = false;
- Initialize(options);
- wgpu::Device device = GetNewDevice();
- // Make a invalid ShaderModuleDescriptor because it contains SPIR-V.
- wgpu::ShaderModuleSPIRVDescriptor spirvDesc;
- spirvDesc.codeSize = 0;
- spirvDesc.code = nullptr;
- wgpu::ShaderModuleDescriptor desc;
- desc.nextInChain = &spirvDesc;
- // Make sure creation fails, and for the correct reason.
- device.PushErrorScope(wgpu::ErrorFilter::Validation);
- device.CreateShaderModule(&desc);
- bool got_error = false;
- device.PopErrorScope(ExpectSPIRVDisallowedError, &got_error);
- WaitForCompletion(device);
- EXPECT_TRUE(got_error);
- }
- TEST_F(WebGPUTest, ExplicitFallbackAdapterIsDisallowed) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- auto options = WebGPUTest::Options();
- options.force_fallback_adapter = true;
- options.enable_unsafe_webgpu = false;
- // Initialize attempts to create an adapter.
- Initialize(options);
- // No fallback adapter should be available.
- EXPECT_EQ(adapter_, nullptr);
- }
- TEST_F(WebGPUTest, ImplicitFallbackAdapterIsDisallowed) {
- if (!WebGPUSupported()) {
- LOG(ERROR) << "Test skipped because WebGPU isn't supported";
- return;
- }
- auto options = WebGPUTest::Options();
- options.enable_unsafe_webgpu = false;
- // Initialize attempts to create an adapter.
- Initialize(options);
- if (adapter_) {
- wgpu::AdapterProperties properties;
- adapter_.GetProperties(&properties);
- // If we got an Adapter, it must not be a CPU adapter.
- EXPECT_NE(properties.adapterType, wgpu::AdapterType::CPU);
- }
- }
- } // namespace gpu
|