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,int32_t,const std::vector<std::string> &,const std::vector<std::string> &,const std::vector<std::string> &,bool,bool,bool,std::unique_ptr<amber::EngineConfig> * config)51 amber::Result ConfigHelperDawn::CreateConfig(
52 uint32_t,
53 uint32_t,
54 int32_t,
55 const std::vector<std::string>&,
56 const std::vector<std::string>&,
57 const std::vector<std::string>&,
58 bool,
59 bool,
60 bool,
61 std::unique_ptr<amber::EngineConfig>* config) {
62 // Set procedure table and error callback.
63 DawnProcTable backendProcs = dawn_native::GetProcs();
64 dawnSetProcs(&backendProcs);
65 dawn_instance_.DiscoverDefaultAdapters();
66
67 for (dawn_native::Adapter& adapter : dawn_instance_.GetAdapters()) {
68 #if AMBER_DAWN_METAL
69 ::dawn_native::BackendType backendType = ::dawn_native::BackendType::Metal;
70 #else // assuming VULKAN
71 ::dawn_native::BackendType backendType = ::dawn_native::BackendType::Vulkan;
72 #endif
73
74 if (adapter.GetBackendType() == backendType) {
75 dawn_device_ = ::dawn::Device::Acquire(adapter.CreateDevice());
76 }
77 }
78
79 if (!dawn_device_)
80 return amber::Result("could not find Vulkan or Metal backend for Dawn");
81
82 backendProcs.deviceSetUncapturedErrorCallback(dawn_device_.Get(),
83 PrintDeviceError, nullptr);
84 auto* dawn_config = new amber::DawnEngineConfig;
85 dawn_config->device = &dawn_device_;
86 config->reset(dawn_config);
87
88 return {};
89 }
90
91 } // namespace sample
92