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