[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / rendering / dx / DirectXHelper.h
blobbb51ca6fd784bc6e155853187254747dc5d9db08
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include "commons/Exception.h"
12 #include "dxerr.h"
14 #include "platform/win32/CharsetConverter.h"
16 #include <d3d11_4.h>
17 #include <ppltasks.h> // For create_task
19 enum PCI_Vendors
21 PCIV_AMD = 0x1002,
22 PCIV_NVIDIA = 0x10DE,
23 PCIV_Intel = 0x8086,
26 namespace DX
28 #define RATIONAL_TO_FLOAT(rational) ((rational.Denominator != 0) ? \
29 static_cast<float>(rational.Numerator) / static_cast<float>(rational.Denominator) : 0.0f)
31 namespace DisplayMetrics
33 // High resolution displays can require a lot of GPU and battery power to render.
34 // High resolution phones, for example, may suffer from poor battery life if
35 // games attempt to render at 60 frames per second at full fidelity.
36 // The decision to render at full fidelity across all platforms and form factors
37 // should be deliberate.
38 static const bool SupportHighResolutions = true;
40 // The default thresholds that define a "high resolution" display. If the thresholds
41 // are exceeded and SupportHighResolutions is false, the dimensions will be scaled
42 // by 50%.
43 static const float Dpi100 = 96.0f; // 100% of standard desktop display.
44 static const float DpiThreshold = 192.0f; // 200% of standard desktop display.
45 static const float WidthThreshold = 1920.0f; // 1080p width.
46 static const float HeightThreshold = 1080.0f; // 1080p height.
49 inline void BreakIfFailed(HRESULT hr)
51 if (FAILED(hr))
53 // Set a breakpoint on this line to catch Win32 API errors.
54 #if _DEBUG && !defined(TARGET_WINDOWS_STORE)
55 DebugBreak();
56 #endif
57 throw new XbmcCommons::UncheckedException(__FUNCTION__, "Unhandled error");
61 // Converts a length in device-independent pixels (DIPs) to a length in physical pixels.
62 inline float ConvertDipsToPixels(float dips, float dpi)
64 static const float dipsPerInch = DisplayMetrics::Dpi100;
65 return floorf(dips * dpi / dipsPerInch + 0.5f); // Round to nearest integer.
68 inline float ConvertPixelsToDips(float pixels, float dpi)
70 static const float dipsPerInch = DisplayMetrics::Dpi100;
71 return floorf(pixels / (dpi / dipsPerInch) + 0.5f); // Round to nearest integer.
74 inline float RationalToFloat(DXGI_RATIONAL rational)
76 return RATIONAL_TO_FLOAT(rational);
79 inline void GetRefreshRatio(uint32_t refresh, uint32_t *num, uint32_t *den)
81 int i = (((refresh + 1) % 24) == 0 || ((refresh + 1) % 30) == 0) ? 1 : 0;
82 *num = (refresh + i) * 1000;
83 *den = 1000 + i;
86 inline std::string GetErrorDescription(HRESULT hr)
88 using namespace KODI::PLATFORM::WINDOWS;
90 WCHAR buff[2048];
91 DXGetErrorDescriptionW(hr, buff, 2048);
93 return FromW(StringUtils::Format(L"{:X} - {} ({})", hr, DXGetErrorStringW(hr), buff));
96 inline std::string GetFeatureLevelDescription(D3D_FEATURE_LEVEL featureLevel)
98 uint32_t fl_major = (featureLevel & 0xF000u) >> 12;
99 uint32_t fl_minor = (featureLevel & 0x0F00u) >> 8;
101 return StringUtils::Format("D3D_FEATURE_LEVEL_{}_{}", fl_major, fl_minor);
104 inline std::string GetGFXProviderName(UINT vendorId)
106 std::string name;
107 switch (vendorId)
109 case PCIV_AMD:
110 name = "AMD";
111 break;
112 case PCIV_Intel:
113 name = "Intel";
114 break;
115 case PCIV_NVIDIA:
116 name = "NVIDIA";
117 break;
120 return name;
123 template <typename T> struct SizeGen
125 SizeGen<T>() { Width = Height = 0; }
126 SizeGen<T>(T width, T height) { Width = width; Height = height; }
128 bool operator !=(const SizeGen<T> &size) const
130 return Width != size.Width || Height != size.Height;
133 const SizeGen<T> &operator -=(const SizeGen<T> &size)
135 Width -= size.Width;
136 Height -= size.Height;
137 return *this;
140 const SizeGen<T> &operator +=(const SizeGen<T> &size)
142 Width += size.Width;
143 Height += size.Height;
144 return *this;
147 const SizeGen<T> &operator -=(const T &size)
149 Width -= size;
150 Height -= size;
151 return *this;
154 const SizeGen<T> &operator +=(const T &size)
156 Width += size;
157 Height += size;
158 return *this;
161 T Width, Height;
164 #if defined(_DEBUG)
165 // Check for SDK Layer support.
166 inline bool SdkLayersAvailable()
168 HRESULT hr = D3D11CreateDevice(
169 nullptr,
170 D3D_DRIVER_TYPE_NULL, // There is no need to create a real hardware device.
171 nullptr,
172 D3D11_CREATE_DEVICE_DEBUG, // Check for the SDK layers.
173 nullptr, // Any feature level will do.
175 D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows Store apps.
176 nullptr, // No need to keep the D3D device reference.
177 nullptr, // No need to know the feature level.
178 nullptr // No need to keep the D3D device context reference.
181 return SUCCEEDED(hr);
183 #endif
186 #ifdef TARGET_WINDOWS_DESKTOP
187 namespace winrt
189 namespace Windows
191 namespace Foundation
193 typedef DX::SizeGen<float> Size;
194 typedef DX::SizeGen<int> SizeInt;
198 #endif