webgpu_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "gpu/command_buffer/tests/webgpu_test.h"
  5. #include <dawn/dawn_proc.h>
  6. #include <dawn/webgpu.h>
  7. #include "base/bind.h"
  8. #include "base/command_line.h"
  9. #include "base/test/test_simple_task_runner.h"
  10. #include "build/build_config.h"
  11. #include "components/viz/test/test_gpu_service_holder.h"
  12. #include "gpu/command_buffer/client/webgpu_cmd_helper.h"
  13. #include "gpu/command_buffer/client/webgpu_implementation.h"
  14. #include "gpu/command_buffer/service/service_utils.h"
  15. #include "gpu/command_buffer/service/webgpu_decoder.h"
  16. #include "gpu/config/gpu_test_config.h"
  17. #include "gpu/ipc/host/gpu_memory_buffer_support.h"
  18. #include "gpu/ipc/in_process_command_buffer.h"
  19. #include "gpu/ipc/webgpu_in_process_context.h"
  20. #include "gpu/webgpu/callback.h"
  21. #include "testing/gmock/include/gmock/gmock-matchers.h"
  22. #include "testing/gtest/include/gtest/gtest.h"
  23. #if BUILDFLAG(IS_MAC)
  24. #include "gpu/command_buffer/tests/gl_manager.h"
  25. #include "ui/gl/gl_context.h"
  26. #endif
  27. namespace gpu {
  28. namespace {
  29. void CountCallback(int* count) {
  30. (*count)++;
  31. }
  32. } // anonymous namespace
  33. WebGPUTest::Options::Options() = default;
  34. WebGPUTest::WebGPUTest() = default;
  35. WebGPUTest::~WebGPUTest() = default;
  36. bool WebGPUTest::WebGPUSupported() const {
  37. // TODO(crbug.com/1172447): Re-enable on AMD when the RX 5500 XT issues are
  38. // resolved.
  39. // Win7 does not support WebGPU
  40. if (GPUTestBotConfig::CurrentConfigMatches("Linux AMD") ||
  41. GPUTestBotConfig::CurrentConfigMatches("Win7")) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool WebGPUTest::WebGPUSharedImageSupported() const {
  47. // Currently WebGPUSharedImage is only implemented on Mac, Linux, Windows
  48. // and ChromeOS.
  49. #if (BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
  50. BUILDFLAG(IS_WIN)) && \
  51. BUILDFLAG(USE_DAWN)
  52. return true;
  53. #else
  54. return false;
  55. #endif
  56. }
  57. void WebGPUTest::SetUp() {
  58. if (!WebGPUSupported()) {
  59. return;
  60. }
  61. }
  62. void WebGPUTest::TearDown() {
  63. adapter_ = nullptr;
  64. instance_ = nullptr;
  65. context_ = nullptr;
  66. }
  67. void WebGPUTest::Initialize(const Options& options) {
  68. if (!WebGPUSupported()) {
  69. return;
  70. }
  71. gpu::GpuPreferences gpu_preferences;
  72. gpu_preferences.enable_webgpu = true;
  73. gpu_preferences.use_passthrough_cmd_decoder =
  74. gles2::UsePassthroughCommandDecoder(
  75. base::CommandLine::ForCurrentProcess());
  76. #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && BUILDFLAG(USE_DAWN)
  77. gpu_preferences.use_vulkan = gpu::VulkanImplementationName::kNative;
  78. gpu_preferences.gr_context_type = gpu::GrContextType::kVulkan;
  79. #endif
  80. gpu_preferences.enable_unsafe_webgpu = options.enable_unsafe_webgpu;
  81. gpu_preferences.texture_target_exception_list =
  82. gpu::CreateBufferUsageAndFormatExceptionList();
  83. gpu_service_holder_ =
  84. std::make_unique<viz::TestGpuServiceHolder>(gpu_preferences);
  85. ContextCreationAttribs attributes;
  86. attributes.bind_generates_resource = false;
  87. attributes.enable_gles2_interface = false;
  88. attributes.context_type = CONTEXT_TYPE_WEBGPU;
  89. #if BUILDFLAG(IS_MAC)
  90. ImageFactory* image_factory = &image_factory_;
  91. #else
  92. static constexpr ImageFactory* image_factory = nullptr;
  93. #endif
  94. context_ = std::make_unique<WebGPUInProcessContext>();
  95. ContextResult result =
  96. context_->Initialize(gpu_service_holder_->task_executor(), attributes,
  97. options.shared_memory_limits, image_factory);
  98. ASSERT_EQ(result, ContextResult::kSuccess);
  99. cmd_helper_ = std::make_unique<webgpu::WebGPUCmdHelper>(
  100. context_->GetCommandBufferForTest());
  101. DawnProcTable procs = webgpu()->GetAPIChannel()->GetProcs();
  102. dawnProcSetProcs(&procs);
  103. instance_ = wgpu::Instance(webgpu()->GetAPIChannel()->GetWGPUInstance());
  104. wgpu::RequestAdapterOptions ra_options = {};
  105. ra_options.forceFallbackAdapter = options.force_fallback_adapter;
  106. bool done = false;
  107. auto* callback = webgpu::BindWGPUOnceCallback(
  108. [](WebGPUTest* test, bool force_fallback_adapter, bool* done,
  109. WGPURequestAdapterStatus status, WGPUAdapter adapter,
  110. const char* message) {
  111. if (!force_fallback_adapter) {
  112. // If we don't force a particular adapter, we should always find
  113. // one.
  114. EXPECT_EQ(status, WGPURequestAdapterStatus_Success);
  115. EXPECT_NE(adapter, nullptr);
  116. }
  117. test->adapter_ = wgpu::Adapter::Acquire(adapter);
  118. *done = true;
  119. },
  120. this, options.force_fallback_adapter, &done);
  121. instance_.RequestAdapter(&ra_options, callback->UnboundCallback(),
  122. callback->AsUserdata());
  123. webgpu()->FlushCommands();
  124. while (!done) {
  125. RunPendingTasks();
  126. }
  127. }
  128. webgpu::WebGPUImplementation* WebGPUTest::webgpu() const {
  129. return context_->GetImplementation();
  130. }
  131. webgpu::WebGPUCmdHelper* WebGPUTest::webgpu_cmds() const {
  132. return cmd_helper_.get();
  133. }
  134. SharedImageInterface* WebGPUTest::GetSharedImageInterface() const {
  135. return context_->GetCommandBufferForTest()->GetSharedImageInterface();
  136. }
  137. webgpu::WebGPUDecoder* WebGPUTest::GetDecoder() const {
  138. return context_->GetCommandBufferForTest()->GetWebGPUDecoderForTest();
  139. }
  140. void WebGPUTest::RunPendingTasks() {
  141. context_->GetTaskRunner()->RunPendingTasks();
  142. gpu_service_holder_->ScheduleGpuTask(base::BindOnce(
  143. [](webgpu::WebGPUDecoder* decoder) {
  144. if (decoder->HasPollingWork()) {
  145. decoder->PerformPollingWork();
  146. }
  147. },
  148. GetDecoder()));
  149. }
  150. void WebGPUTest::WaitForCompletion(wgpu::Device device) {
  151. // Wait for any work submitted to the queue to be finished. The guarantees of
  152. // Dawn are that all previous operations will have been completed and more
  153. // importantly the callbacks will have been called.
  154. wgpu::Queue queue = device.GetQueue();
  155. bool done = false;
  156. queue.OnSubmittedWorkDone(
  157. 0u,
  158. [](WGPUQueueWorkDoneStatus, void* userdata) {
  159. *static_cast<bool*>(userdata) = true;
  160. },
  161. &done);
  162. while (!done) {
  163. device.Tick();
  164. webgpu()->FlushCommands();
  165. RunPendingTasks();
  166. }
  167. }
  168. wgpu::Device WebGPUTest::GetNewDevice() {
  169. wgpu::Device device;
  170. bool done = false;
  171. auto* callback = webgpu::BindWGPUOnceCallback(
  172. [](wgpu::Device* device_out, bool* done, WGPURequestDeviceStatus status,
  173. WGPUDevice device, const char* message) {
  174. *device_out = wgpu::Device::Acquire(device);
  175. *done = true;
  176. },
  177. &device, &done);
  178. DCHECK(adapter_);
  179. wgpu::DeviceDescriptor device_desc = {};
  180. adapter_.RequestDevice(&device_desc, callback->UnboundCallback(),
  181. callback->AsUserdata());
  182. webgpu()->FlushCommands();
  183. while (!done) {
  184. RunPendingTasks();
  185. }
  186. EXPECT_NE(device, nullptr);
  187. return device;
  188. }
  189. TEST_F(WebGPUTest, FlushNoCommands) {
  190. if (!WebGPUSupported()) {
  191. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  192. return;
  193. }
  194. Initialize(WebGPUTest::Options());
  195. webgpu()->FlushCommands();
  196. }
  197. // Referred from GLES2ImplementationTest/ReportLoss
  198. TEST_F(WebGPUTest, ReportLoss) {
  199. if (!WebGPUSupported()) {
  200. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  201. return;
  202. }
  203. Initialize(WebGPUTest::Options());
  204. GpuControlClient* webgpu_as_client = webgpu();
  205. int lost_count = 0;
  206. webgpu()->SetLostContextCallback(base::BindOnce(&CountCallback, &lost_count));
  207. EXPECT_EQ(0, lost_count);
  208. webgpu_as_client->OnGpuControlLostContext();
  209. // The lost context callback should be run when WebGPUImplementation is
  210. // notified of the loss.
  211. EXPECT_EQ(1, lost_count);
  212. }
  213. // Referred from GLES2ImplementationTest/ReportLossReentrant
  214. TEST_F(WebGPUTest, ReportLossReentrant) {
  215. if (!WebGPUSupported()) {
  216. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  217. return;
  218. }
  219. Initialize(WebGPUTest::Options());
  220. GpuControlClient* webgpu_as_client = webgpu();
  221. int lost_count = 0;
  222. webgpu()->SetLostContextCallback(base::BindOnce(&CountCallback, &lost_count));
  223. EXPECT_EQ(0, lost_count);
  224. webgpu_as_client->OnGpuControlLostContextMaybeReentrant();
  225. // The lost context callback should not be run yet to avoid calling back into
  226. // clients re-entrantly, and having them re-enter WebGPUImplementation.
  227. EXPECT_EQ(0, lost_count);
  228. }
  229. TEST_F(WebGPUTest, RequestAdapterAfterContextLost) {
  230. if (!WebGPUSupported()) {
  231. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  232. return;
  233. }
  234. Initialize(WebGPUTest::Options());
  235. webgpu()->OnGpuControlLostContext();
  236. bool called = false;
  237. wgpu::RequestAdapterOptions ra_options = {};
  238. instance_.RequestAdapter(
  239. &ra_options,
  240. [](WGPURequestAdapterStatus status, WGPUAdapter adapter,
  241. const char* message, void* userdata) {
  242. EXPECT_EQ(adapter, nullptr);
  243. *static_cast<bool*>(userdata) = true;
  244. },
  245. &called);
  246. webgpu()->FlushCommands();
  247. RunPendingTasks();
  248. EXPECT_TRUE(called);
  249. }
  250. TEST_F(WebGPUTest, RequestDeviceAfterContextLost) {
  251. if (!WebGPUSupported()) {
  252. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  253. return;
  254. }
  255. Initialize(WebGPUTest::Options());
  256. webgpu()->OnGpuControlLostContext();
  257. bool called = false;
  258. DCHECK(adapter_);
  259. wgpu::DeviceDescriptor device_desc = {};
  260. adapter_.RequestDevice(
  261. &device_desc,
  262. [](WGPURequestDeviceStatus status, WGPUDevice device, const char* message,
  263. void* userdata) {
  264. EXPECT_EQ(device, nullptr);
  265. *static_cast<bool*>(userdata) = true;
  266. },
  267. &called);
  268. webgpu()->FlushCommands();
  269. RunPendingTasks();
  270. EXPECT_TRUE(called);
  271. }
  272. TEST_F(WebGPUTest, RequestDeviceWitUnsupportedFeature) {
  273. if (!WebGPUSupported()) {
  274. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  275. return;
  276. }
  277. #if BUILDFLAG(IS_MAC)
  278. // Crashing on Mac M1. Currently missing stack trace. crbug.com/1271926
  279. // This must be checked before WebGPUTest::Initialize otherwise context
  280. // switched is locked and we cannot temporarily have this GLContext.
  281. GLManager gl_manager;
  282. gl_manager.Initialize(GLManager::Options());
  283. std::string renderer(gl_manager.context()->GetGLRenderer());
  284. if (renderer.find("Apple M1") != std::string::npos) {
  285. gl_manager.Destroy();
  286. return;
  287. }
  288. gl_manager.Destroy();
  289. #endif
  290. Initialize(WebGPUTest::Options());
  291. // Create device with unsupported features, expect to fail to create and
  292. // return nullptr
  293. wgpu::FeatureName invalid_feature = static_cast<wgpu::FeatureName>(-2);
  294. wgpu::Device device;
  295. bool done = false;
  296. auto* callback = webgpu::BindWGPUOnceCallback(
  297. [](wgpu::Device* device_out, bool* done, WGPURequestDeviceStatus status,
  298. WGPUDevice device, const char* message) {
  299. *device_out = wgpu::Device::Acquire(device);
  300. *done = true;
  301. },
  302. &device, &done);
  303. DCHECK(adapter_);
  304. wgpu::DeviceDescriptor device_desc = {};
  305. device_desc.requiredFeaturesCount = 1;
  306. device_desc.requiredFeatures = &invalid_feature;
  307. adapter_.RequestDevice(&device_desc, callback->UnboundCallback(),
  308. callback->AsUserdata());
  309. webgpu()->FlushCommands();
  310. while (!done) {
  311. RunPendingTasks();
  312. }
  313. EXPECT_EQ(device, nullptr);
  314. // Create device again with supported features, expect success and not
  315. // blocked by the last failure
  316. GetNewDevice();
  317. }
  318. TEST_F(WebGPUTest, SPIRVIsDisallowed) {
  319. if (!WebGPUSupported()) {
  320. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  321. return;
  322. }
  323. auto ExpectSPIRVDisallowedError = [](WGPUErrorType type, const char* message,
  324. void* userdata) {
  325. // We match on this string to make sure the shader module creation fails
  326. // because SPIR-V is disallowed and not because codeSize=0.
  327. EXPECT_THAT(message, testing::HasSubstr("SPIR-V is disallowed"));
  328. EXPECT_EQ(type, WGPUErrorType_Validation);
  329. *static_cast<bool*>(userdata) = true;
  330. };
  331. auto options = WebGPUTest::Options();
  332. options.enable_unsafe_webgpu = false;
  333. Initialize(options);
  334. wgpu::Device device = GetNewDevice();
  335. // Make a invalid ShaderModuleDescriptor because it contains SPIR-V.
  336. wgpu::ShaderModuleSPIRVDescriptor spirvDesc;
  337. spirvDesc.codeSize = 0;
  338. spirvDesc.code = nullptr;
  339. wgpu::ShaderModuleDescriptor desc;
  340. desc.nextInChain = &spirvDesc;
  341. // Make sure creation fails, and for the correct reason.
  342. device.PushErrorScope(wgpu::ErrorFilter::Validation);
  343. device.CreateShaderModule(&desc);
  344. bool got_error = false;
  345. device.PopErrorScope(ExpectSPIRVDisallowedError, &got_error);
  346. WaitForCompletion(device);
  347. EXPECT_TRUE(got_error);
  348. }
  349. TEST_F(WebGPUTest, ExplicitFallbackAdapterIsDisallowed) {
  350. if (!WebGPUSupported()) {
  351. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  352. return;
  353. }
  354. auto options = WebGPUTest::Options();
  355. options.force_fallback_adapter = true;
  356. options.enable_unsafe_webgpu = false;
  357. // Initialize attempts to create an adapter.
  358. Initialize(options);
  359. // No fallback adapter should be available.
  360. EXPECT_EQ(adapter_, nullptr);
  361. }
  362. TEST_F(WebGPUTest, ImplicitFallbackAdapterIsDisallowed) {
  363. if (!WebGPUSupported()) {
  364. LOG(ERROR) << "Test skipped because WebGPU isn't supported";
  365. return;
  366. }
  367. auto options = WebGPUTest::Options();
  368. options.enable_unsafe_webgpu = false;
  369. // Initialize attempts to create an adapter.
  370. Initialize(options);
  371. if (adapter_) {
  372. wgpu::AdapterProperties properties;
  373. adapter_.GetProperties(&properties);
  374. // If we got an Adapter, it must not be a CPU adapter.
  375. EXPECT_NE(properties.adapterType, wgpu::AdapterType::CPU);
  376. }
  377. }
  378. } // namespace gpu