FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
D3d10DeviceContextImpl.cpp
1 /**********************************************************\
2 Original Author: Gil Gonen
3 
4 Created: Jan 28, 2013
5 License: Dual license model; choose one of two:
6 New BSD License
7 http://www.opensource.org/licenses/bsd-license.php
8 - or -
9 GNU Lesser General Public License, version 2.1
10 http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 Copyright 2013 Gil Gonen and the Firebreath development team
13 \**********************************************************/
14 
15 #include "D3d10DeviceContextImpl.h"
16 #include "logging.h"
17 #include <iomanip>
18 
19 using namespace FB;
20 
21 typedef HRESULT (WINAPI *CreateDXGIFactory1Func)(
22  REFIID riid,
23  void **ppFactory
24  );
25 
27 
28 static void fblog_hr(const char* what, HRESULT hr)
29 {
30  std::stringstream ss;
31  ss << what << " failed: 0x" << std::setfill('0') << std::setw(8) << std::hex << hr;
32  FBLOG_ERROR("getD3D10Device", ss);
33 }
34 
35 D3d10DeviceContextImpl::D3d10DeviceContextImpl()
36  :_d3d10(LoadLibraryA("d3d10_1.dll"))
37  ,_dxgi(LoadLibraryA("dxgi.dll"))
38 {
39  if (!_d3d10 || !_dxgi) {
40  FBLOG_ERROR("D3d10DeviceContextImpl", "missing dlls");
41  return;
42  }
43 
44  D3D10CreateDevice1Func createD3DDevice = (D3D10CreateDevice1Func) GetProcAddress(_d3d10, "D3D10CreateDevice1");
45  CreateDXGIFactory1Func createDXGIFactory1 = (CreateDXGIFactory1Func) GetProcAddress(_dxgi, "CreateDXGIFactory1");
46 
47  if (!createD3DDevice || !createDXGIFactory1) {
48  FBLOG_ERROR("D3d10DeviceContextImpl", "missing procs");
49  return;
50  }
51 
52  // Try to use a DXGI 1.1 adapter in order to share resources across processes.
53  CComPtr<IDXGIFactory1> factory1;
54  HRESULT hr = createDXGIFactory1(__uuidof(IDXGIFactory1), (void**) &factory1);
55 
56  if (FAILED(hr) || !factory1) {
57  fblog_hr("CreateDXGIFactory1", hr);
58  return;
59  }
60 
61  CComPtr<IDXGIAdapter1> adapter1;
62  hr = factory1->EnumAdapters1(0, &adapter1);
63  if (FAILED(hr) || !adapter1) {
64  fblog_hr("EnumAdapters1", hr);
65  }
66 
67  hr = createDevice(createD3DDevice, adapter1);
68  if (FAILED(hr)) {
69  fblog_hr("D3D10CreateDevice1", hr);
70  }
71 }
72 
73 HRESULT D3d10DeviceContextImpl::createDevice(D3D10CreateDevice1Func func, CComPtr<IDXGIAdapter1> adapter)
74 {
75  return func(
76  adapter,
77  D3D10_DRIVER_TYPE_HARDWARE,
78  NULL,
79  D3D10_CREATE_DEVICE_BGRA_SUPPORT |
80  D3D10_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS,
81  D3D10_FEATURE_LEVEL_10_0,
82  D3D10_1_SDK_VERSION,
83  &_dev);
84 }
85 
86 D3d10DeviceContextImpl::~D3d10DeviceContextImpl()
87 {
88  _dev.Release();
89  if (_dxgi) FreeLibrary(_dxgi);
90  if (_d3d10) FreeLibrary(_d3d10);
91 }
92 
93 CComPtr<ID3D10Device1> D3d10DeviceContextImpl::device()
94 {
95  return _dev;
96 }
97