aboutsummaryrefslogtreecommitdiffstats
path: root/support/hololens/ServoApp/Common/DirectXHelper.h
blob: 5ab0dae02c77b9a20cf9ac754519d642d4dc7eb4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#pragma once

namespace DX {
// Function that reads from a binary file asynchronously.
inline std::future<std::vector<byte>>
ReadDataAsync(const std::wstring_view &filename) {
  using namespace winrt::Windows::Storage;
  using namespace winrt::Windows::Storage::Streams;

  IBuffer fileBuffer = co_await PathIO::ReadBufferAsync(filename);

  std::vector<byte> returnBuffer;
  returnBuffer.resize(fileBuffer.Length());
  DataReader::FromBuffer(fileBuffer)
      .ReadBytes(winrt::array_view<uint8_t>(returnBuffer));
  return returnBuffer;
}

// Converts a length in device-independent pixels (DIPs) to a length in physical
// pixels.
inline float ConvertDipsToPixels(float dips, float dpi) {
  constexpr float dipsPerInch = 96.0f;
  return floorf(dips * dpi / dipsPerInch + 0.5f); // Round to nearest integer.
}

inline winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DSurface
CreateDepthTextureInteropObject(
    const Microsoft::WRL::ComPtr<ID3D11Texture2D> spTexture2D) {
  // Direct3D interop APIs are used to provide the buffer to the WinRT API.
  Microsoft::WRL::ComPtr<IDXGIResource1> depthStencilResource;
  winrt::check_hresult(spTexture2D.As(&depthStencilResource));
  Microsoft::WRL::ComPtr<IDXGISurface2> depthDxgiSurface;
  winrt::check_hresult(
      depthStencilResource->CreateSubresourceSurface(0, &depthDxgiSurface));
  winrt::com_ptr<::IInspectable> inspectableSurface;
  winrt::check_hresult(CreateDirect3D11SurfaceFromDXGISurface(
      depthDxgiSurface.Get(),
      reinterpret_cast<IInspectable **>(winrt::put_abi(inspectableSurface))));

  return inspectableSurface
      .as<winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DSurface>();
}

#if defined(_DEBUG)
// Check for SDK Layer support.
inline bool SdkLayersAvailable() {
  HRESULT hr = D3D11CreateDevice(
      nullptr,
      D3D_DRIVER_TYPE_NULL, // There is no need to create a real hardware
                            // device.
      0,
      D3D11_CREATE_DEVICE_DEBUG, // Check for the SDK layers.
      nullptr,                   // Any feature level will do.
      0,
      D3D11_SDK_VERSION, // Always set this to D3D11_SDK_VERSION for Windows
                         // Runtime apps.
      nullptr,           // No need to keep the D3D device reference.
      nullptr,           // No need to know the feature level.
      nullptr            // No need to keep the D3D device context reference.
  );

  return SUCCEEDED(hr);
}
#endif
} // namespace DX