• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "samples/config_helper_dawn.h"
16 
17 #include <iostream>
18 
19 namespace sample {
20 
21 ConfigHelperDawn::ConfigHelperDawn() = default;
22 ConfigHelperDawn::~ConfigHelperDawn() = default;
23 
24 namespace {
25 
26 // Callback which prints a message from a Dawn device operation.
PrintDeviceError(DawnErrorType errorType,const char * message,void *)27 void PrintDeviceError(DawnErrorType errorType, const char* message, void*) {
28   switch (errorType) {
29     case DAWN_ERROR_TYPE_VALIDATION:
30       std::cout << "Validation ";
31       break;
32     case DAWN_ERROR_TYPE_OUT_OF_MEMORY:
33       std::cout << "Out of memory ";
34       break;
35     case DAWN_ERROR_TYPE_UNKNOWN:
36     case DAWN_ERROR_TYPE_FORCE32:
37       std::cout << "Unknown ";
38       break;
39     case DAWN_ERROR_TYPE_DEVICE_LOST:
40       std::cout << "Device lost ";
41       break;
42     default:
43       std::cout << "Unreachable";
44       return;
45   }
46   std::cout << "error: " << message << std::endl;
47 }
48 
49 }  // namespace
50 
CreateConfig(uint32_t,uint32_t,const std::vector<std::string> &,const std::vector<std::string> &,const std::vector<std::string> &,bool,bool,std::unique_ptr<amber::EngineConfig> * config)51 amber::Result ConfigHelperDawn::CreateConfig(
52     uint32_t,
53     uint32_t,
54     const std::vector<std::string>&,
55     const std::vector<std::string>&,
56     const std::vector<std::string>&,
57     bool,
58     bool,
59     std::unique_ptr<amber::EngineConfig>* config) {
60   // Set procedure table and error callback.
61   DawnProcTable backendProcs = dawn_native::GetProcs();
62   dawnSetProcs(&backendProcs);
63   dawn_instance_.DiscoverDefaultAdapters();
64 
65   for (dawn_native::Adapter& adapter : dawn_instance_.GetAdapters()) {
66 #if AMBER_DAWN_METAL
67     ::dawn_native::BackendType backendType = ::dawn_native::BackendType::Metal;
68 #else  // assuming VULKAN
69     ::dawn_native::BackendType backendType = ::dawn_native::BackendType::Vulkan;
70 #endif
71 
72     if (adapter.GetBackendType() == backendType) {
73       dawn_device_ = ::dawn::Device::Acquire(adapter.CreateDevice());
74     }
75   }
76 
77   if (!dawn_device_)
78     return amber::Result("could not find Vulkan or Metal backend for Dawn");
79 
80   backendProcs.deviceSetUncapturedErrorCallback(dawn_device_.Get(),
81                                                 PrintDeviceError, nullptr);
82   auto* dawn_config = new amber::DawnEngineConfig;
83   dawn_config->device = &dawn_device_;
84   config->reset(dawn_config);
85 
86   return {};
87 }
88 
89 }  // namespace sample
90