1 // Copyright 2017 The Dawn 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 #ifndef DAWNNATIVE_TOBACKEND_H_ 16 #define DAWNNATIVE_TOBACKEND_H_ 17 18 #include "dawn_native/Forward.h" 19 20 namespace dawn_native { 21 22 // ToBackendTraits implements the mapping from base type to member type of BackendTraits 23 template <typename T, typename BackendTraits> 24 struct ToBackendTraits; 25 26 template <typename BackendTraits> 27 struct ToBackendTraits<AdapterBase, BackendTraits> { 28 using BackendType = typename BackendTraits::AdapterType; 29 }; 30 31 template <typename BackendTraits> 32 struct ToBackendTraits<BindGroupBase, BackendTraits> { 33 using BackendType = typename BackendTraits::BindGroupType; 34 }; 35 36 template <typename BackendTraits> 37 struct ToBackendTraits<BindGroupLayoutBase, BackendTraits> { 38 using BackendType = typename BackendTraits::BindGroupLayoutType; 39 }; 40 41 template <typename BackendTraits> 42 struct ToBackendTraits<BufferBase, BackendTraits> { 43 using BackendType = typename BackendTraits::BufferType; 44 }; 45 46 template <typename BackendTraits> 47 struct ToBackendTraits<CommandBufferBase, BackendTraits> { 48 using BackendType = typename BackendTraits::CommandBufferType; 49 }; 50 51 template <typename BackendTraits> 52 struct ToBackendTraits<ComputePipelineBase, BackendTraits> { 53 using BackendType = typename BackendTraits::ComputePipelineType; 54 }; 55 56 template <typename BackendTraits> 57 struct ToBackendTraits<DeviceBase, BackendTraits> { 58 using BackendType = typename BackendTraits::DeviceType; 59 }; 60 61 template <typename BackendTraits> 62 struct ToBackendTraits<PipelineLayoutBase, BackendTraits> { 63 using BackendType = typename BackendTraits::PipelineLayoutType; 64 }; 65 66 template <typename BackendTraits> 67 struct ToBackendTraits<QuerySetBase, BackendTraits> { 68 using BackendType = typename BackendTraits::QuerySetType; 69 }; 70 71 template <typename BackendTraits> 72 struct ToBackendTraits<QueueBase, BackendTraits> { 73 using BackendType = typename BackendTraits::QueueType; 74 }; 75 76 template <typename BackendTraits> 77 struct ToBackendTraits<RenderPipelineBase, BackendTraits> { 78 using BackendType = typename BackendTraits::RenderPipelineType; 79 }; 80 81 template <typename BackendTraits> 82 struct ToBackendTraits<ResourceHeapBase, BackendTraits> { 83 using BackendType = typename BackendTraits::ResourceHeapType; 84 }; 85 86 template <typename BackendTraits> 87 struct ToBackendTraits<SamplerBase, BackendTraits> { 88 using BackendType = typename BackendTraits::SamplerType; 89 }; 90 91 template <typename BackendTraits> 92 struct ToBackendTraits<ShaderModuleBase, BackendTraits> { 93 using BackendType = typename BackendTraits::ShaderModuleType; 94 }; 95 96 template <typename BackendTraits> 97 struct ToBackendTraits<StagingBufferBase, BackendTraits> { 98 using BackendType = typename BackendTraits::StagingBufferType; 99 }; 100 101 template <typename BackendTraits> 102 struct ToBackendTraits<TextureBase, BackendTraits> { 103 using BackendType = typename BackendTraits::TextureType; 104 }; 105 106 template <typename BackendTraits> 107 struct ToBackendTraits<SwapChainBase, BackendTraits> { 108 using BackendType = typename BackendTraits::SwapChainType; 109 }; 110 111 template <typename BackendTraits> 112 struct ToBackendTraits<TextureViewBase, BackendTraits> { 113 using BackendType = typename BackendTraits::TextureViewType; 114 }; 115 116 // ToBackendBase implements conversion to the given BackendTraits 117 // To use it in a backend, use the following: 118 // template<typename T> 119 // auto ToBackend(T&& common) -> decltype(ToBackendBase<MyBackendTraits>(common)) { 120 // return ToBackendBase<MyBackendTraits>(common); 121 // } 122 123 template <typename BackendTraits, typename T> 124 Ref<typename ToBackendTraits<T, BackendTraits>::BackendType>& ToBackendBase(Ref<T>& common) { 125 return reinterpret_cast<Ref<typename ToBackendTraits<T, BackendTraits>::BackendType>&>( 126 common); 127 } 128 129 template <typename BackendTraits, typename T> 130 Ref<typename ToBackendTraits<T, BackendTraits>::BackendType>&& ToBackendBase(Ref<T>&& common) { 131 return reinterpret_cast<Ref<typename ToBackendTraits<T, BackendTraits>::BackendType>&&>( 132 common); 133 } 134 135 template <typename BackendTraits, typename T> 136 const Ref<typename ToBackendTraits<T, BackendTraits>::BackendType>& ToBackendBase( 137 const Ref<T>& common) { 138 return reinterpret_cast< 139 const Ref<typename ToBackendTraits<T, BackendTraits>::BackendType>&>(common); 140 } 141 142 template <typename BackendTraits, typename T> 143 typename ToBackendTraits<T, BackendTraits>::BackendType* ToBackendBase(T* common) { 144 return reinterpret_cast<typename ToBackendTraits<T, BackendTraits>::BackendType*>(common); 145 } 146 147 template <typename BackendTraits, typename T> 148 const typename ToBackendTraits<T, BackendTraits>::BackendType* ToBackendBase(const T* common) { 149 return reinterpret_cast<const typename ToBackendTraits<T, BackendTraits>::BackendType*>( 150 common); 151 } 152 153 } // namespace dawn_native 154 155 #endif // DAWNNATIVE_TOBACKEND_H_ 156