1 // Copyright 2018 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 #include "dawn_native/DawnNative.h" 16 17 #include "dawn_native/BindGroupLayout.h" 18 #include "dawn_native/Buffer.h" 19 #include "dawn_native/Device.h" 20 #include "dawn_native/Instance.h" 21 #include "dawn_native/Texture.h" 22 #include "dawn_platform/DawnPlatform.h" 23 24 // Contains the entry-points into dawn_native 25 26 namespace dawn_native { 27 28 const DawnProcTable& GetProcsAutogen(); 29 GetProcs()30 const DawnProcTable& GetProcs() { 31 return GetProcsAutogen(); 32 } 33 GetTogglesUsed(WGPUDevice device)34 std::vector<const char*> GetTogglesUsed(WGPUDevice device) { 35 return FromAPI(device)->GetTogglesUsed(); 36 } 37 38 // Adapter 39 40 Adapter::Adapter() = default; 41 Adapter(AdapterBase * impl)42 Adapter::Adapter(AdapterBase* impl) : mImpl(impl) { 43 } 44 ~Adapter()45 Adapter::~Adapter() { 46 mImpl = nullptr; 47 } 48 49 Adapter::Adapter(const Adapter& other) = default; 50 Adapter& Adapter::operator=(const Adapter& other) = default; 51 GetProperties(wgpu::AdapterProperties * properties) const52 void Adapter::GetProperties(wgpu::AdapterProperties* properties) const { 53 properties->backendType = mImpl->GetBackendType(); 54 properties->adapterType = mImpl->GetAdapterType(); 55 properties->driverDescription = mImpl->GetDriverDescription().c_str(); 56 properties->deviceID = mImpl->GetPCIInfo().deviceId; 57 properties->vendorID = mImpl->GetPCIInfo().vendorId; 58 properties->name = mImpl->GetPCIInfo().name.c_str(); 59 } 60 GetBackendType() const61 BackendType Adapter::GetBackendType() const { 62 switch (mImpl->GetBackendType()) { 63 case wgpu::BackendType::D3D12: 64 return BackendType::D3D12; 65 case wgpu::BackendType::Metal: 66 return BackendType::Metal; 67 case wgpu::BackendType::Null: 68 return BackendType::Null; 69 case wgpu::BackendType::OpenGL: 70 return BackendType::OpenGL; 71 case wgpu::BackendType::Vulkan: 72 return BackendType::Vulkan; 73 case wgpu::BackendType::OpenGLES: 74 return BackendType::OpenGLES; 75 76 case wgpu::BackendType::D3D11: 77 case wgpu::BackendType::WebGPU: 78 break; 79 } 80 UNREACHABLE(); 81 } 82 GetDeviceType() const83 DeviceType Adapter::GetDeviceType() const { 84 switch (mImpl->GetAdapterType()) { 85 case wgpu::AdapterType::DiscreteGPU: 86 return DeviceType::DiscreteGPU; 87 case wgpu::AdapterType::IntegratedGPU: 88 return DeviceType::IntegratedGPU; 89 case wgpu::AdapterType::CPU: 90 return DeviceType::CPU; 91 case wgpu::AdapterType::Unknown: 92 return DeviceType::Unknown; 93 } 94 UNREACHABLE(); 95 } 96 GetPCIInfo() const97 const PCIInfo& Adapter::GetPCIInfo() const { 98 return mImpl->GetPCIInfo(); 99 } 100 GetSupportedFeatures() const101 std::vector<const char*> Adapter::GetSupportedFeatures() const { 102 FeaturesSet supportedFeaturesSet = mImpl->GetSupportedFeatures(); 103 return supportedFeaturesSet.GetEnabledFeatureNames(); 104 } 105 GetAdapterProperties() const106 WGPUDeviceProperties Adapter::GetAdapterProperties() const { 107 return mImpl->GetAdapterProperties(); 108 } 109 GetLimits(WGPUSupportedLimits * limits) const110 bool Adapter::GetLimits(WGPUSupportedLimits* limits) const { 111 return mImpl->GetLimits(FromAPI(limits)); 112 } 113 SetUseTieredLimits(bool useTieredLimits)114 void Adapter::SetUseTieredLimits(bool useTieredLimits) { 115 mImpl->SetUseTieredLimits(useTieredLimits); 116 } 117 SupportsExternalImages() const118 bool Adapter::SupportsExternalImages() const { 119 return mImpl->SupportsExternalImages(); 120 } 121 operator bool() const122 Adapter::operator bool() const { 123 return mImpl != nullptr; 124 } 125 CreateDevice(const DawnDeviceDescriptor * deviceDescriptor)126 WGPUDevice Adapter::CreateDevice(const DawnDeviceDescriptor* deviceDescriptor) { 127 return ToAPI(mImpl->CreateDevice(deviceDescriptor)); 128 } 129 RequestDevice(const DawnDeviceDescriptor * descriptor,WGPURequestDeviceCallback callback,void * userdata)130 void Adapter::RequestDevice(const DawnDeviceDescriptor* descriptor, 131 WGPURequestDeviceCallback callback, 132 void* userdata) { 133 mImpl->RequestDevice(descriptor, callback, userdata); 134 } 135 ResetInternalDeviceForTesting()136 void Adapter::ResetInternalDeviceForTesting() { 137 mImpl->ResetInternalDeviceForTesting(); 138 } 139 140 // AdapterDiscoverOptionsBase 141 AdapterDiscoveryOptionsBase(WGPUBackendType type)142 AdapterDiscoveryOptionsBase::AdapterDiscoveryOptionsBase(WGPUBackendType type) 143 : backendType(type) { 144 } 145 146 // Instance 147 Instance()148 Instance::Instance() : mImpl(InstanceBase::Create()) { 149 } 150 ~Instance()151 Instance::~Instance() { 152 if (mImpl != nullptr) { 153 mImpl->Release(); 154 mImpl = nullptr; 155 } 156 } 157 DiscoverDefaultAdapters()158 void Instance::DiscoverDefaultAdapters() { 159 mImpl->DiscoverDefaultAdapters(); 160 } 161 DiscoverAdapters(const AdapterDiscoveryOptionsBase * options)162 bool Instance::DiscoverAdapters(const AdapterDiscoveryOptionsBase* options) { 163 return mImpl->DiscoverAdapters(options); 164 } 165 GetAdapters() const166 std::vector<Adapter> Instance::GetAdapters() const { 167 // Adapters are owned by mImpl so it is safe to return non RAII pointers to them 168 std::vector<Adapter> adapters; 169 for (const std::unique_ptr<AdapterBase>& adapter : mImpl->GetAdapters()) { 170 adapters.push_back({adapter.get()}); 171 } 172 return adapters; 173 } 174 GetToggleInfo(const char * toggleName)175 const ToggleInfo* Instance::GetToggleInfo(const char* toggleName) { 176 return mImpl->GetToggleInfo(toggleName); 177 } 178 EnableBackendValidation(bool enableBackendValidation)179 void Instance::EnableBackendValidation(bool enableBackendValidation) { 180 if (enableBackendValidation) { 181 mImpl->SetBackendValidationLevel(BackendValidationLevel::Full); 182 } 183 } 184 SetBackendValidationLevel(BackendValidationLevel level)185 void Instance::SetBackendValidationLevel(BackendValidationLevel level) { 186 mImpl->SetBackendValidationLevel(level); 187 } 188 EnableBeginCaptureOnStartup(bool beginCaptureOnStartup)189 void Instance::EnableBeginCaptureOnStartup(bool beginCaptureOnStartup) { 190 mImpl->EnableBeginCaptureOnStartup(beginCaptureOnStartup); 191 } 192 SetPlatform(dawn_platform::Platform * platform)193 void Instance::SetPlatform(dawn_platform::Platform* platform) { 194 mImpl->SetPlatform(platform); 195 } 196 Get() const197 WGPUInstance Instance::Get() const { 198 return ToAPI(mImpl); 199 } 200 GetLazyClearCountForTesting(WGPUDevice device)201 size_t GetLazyClearCountForTesting(WGPUDevice device) { 202 return FromAPI(device)->GetLazyClearCountForTesting(); 203 } 204 GetDeprecationWarningCountForTesting(WGPUDevice device)205 size_t GetDeprecationWarningCountForTesting(WGPUDevice device) { 206 return FromAPI(device)->GetDeprecationWarningCountForTesting(); 207 } 208 IsTextureSubresourceInitialized(WGPUTexture texture,uint32_t baseMipLevel,uint32_t levelCount,uint32_t baseArrayLayer,uint32_t layerCount,WGPUTextureAspect cAspect)209 bool IsTextureSubresourceInitialized(WGPUTexture texture, 210 uint32_t baseMipLevel, 211 uint32_t levelCount, 212 uint32_t baseArrayLayer, 213 uint32_t layerCount, 214 WGPUTextureAspect cAspect) { 215 TextureBase* textureBase = FromAPI(texture); 216 217 Aspect aspect = 218 ConvertAspect(textureBase->GetFormat(), static_cast<wgpu::TextureAspect>(cAspect)); 219 SubresourceRange range(aspect, {baseArrayLayer, layerCount}, {baseMipLevel, levelCount}); 220 return textureBase->IsSubresourceContentInitialized(range); 221 } 222 223 std::vector<const char*> GetProcMapNamesForTestingInternal(); 224 GetProcMapNamesForTesting()225 std::vector<const char*> GetProcMapNamesForTesting() { 226 return GetProcMapNamesForTestingInternal(); 227 } 228 DeviceTick(WGPUDevice device)229 DAWN_NATIVE_EXPORT bool DeviceTick(WGPUDevice device) { 230 return FromAPI(device)->APITick(); 231 } 232 233 // ExternalImageDescriptor 234 ExternalImageDescriptor(ExternalImageType type)235 ExternalImageDescriptor::ExternalImageDescriptor(ExternalImageType type) : type(type) { 236 } 237 238 // ExternalImageExportInfo 239 ExternalImageExportInfo(ExternalImageType type)240 ExternalImageExportInfo::ExternalImageExportInfo(ExternalImageType type) : type(type) { 241 } 242 GetObjectLabelForTesting(void * objectHandle)243 const char* GetObjectLabelForTesting(void* objectHandle) { 244 ApiObjectBase* object = reinterpret_cast<ApiObjectBase*>(objectHandle); 245 return object->GetLabel().c_str(); 246 } 247 GetAllocatedSizeForTesting(WGPUBuffer buffer)248 uint64_t GetAllocatedSizeForTesting(WGPUBuffer buffer) { 249 return FromAPI(buffer)->GetAllocatedSize(); 250 } 251 BindGroupLayoutBindingsEqualForTesting(WGPUBindGroupLayout a,WGPUBindGroupLayout b)252 bool BindGroupLayoutBindingsEqualForTesting(WGPUBindGroupLayout a, WGPUBindGroupLayout b) { 253 bool excludePipelineCompatibiltyToken = true; 254 return FromAPI(a)->IsLayoutEqual(FromAPI(b), excludePipelineCompatibiltyToken); 255 } 256 257 } // namespace dawn_native 258