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