PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
nvGPUutil.cpp
Go to the documentation of this file.
1 /***************************************************************************\
2 |* *|
3 |* Copyright 2007 NVIDIA Corporation. All rights reserved. *|
4 |* *|
5 |* NOTICE TO USER: *|
6 |* *|
7 |* This source code is subject to NVIDIA ownership rights under U.S. *|
8 |* and international Copyright laws. Users and possessors of this *|
9 |* source code are hereby granted a nonexclusive, royalty-free *|
10 |* license to use this code in individual and commercial software. *|
11 |* *|
12 |* NVIDIA MAKES NO REPRESENTATION ABOUT THE SUITABILITY OF THIS SOURCE *|
13 |* CODE FOR ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR *|
14 |* IMPLIED WARRANTY OF ANY KIND. NVIDIA DISCLAIMS ALL WARRANTIES WITH *|
15 |* REGARD TO THIS SOURCE CODE, INCLUDING ALL IMPLIED WARRANTIES OF *|
16 |* MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR *|
17 |* PURPOSE. IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, *|
18 |* INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES *|
19 |* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN *|
20 |* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING *|
21 |* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE *|
22 |* CODE. *|
23 |* *|
24 |* U.S. Government End Users. This source code is a "commercial item" *|
25 |* as that term is defined at 48 C.F.R. 2.101 (OCT 1995), consisting *|
26 |* of "commercial computer software" and "commercial computer software *|
27 |* documentation" as such terms are used in 48 C.F.R. 12.212 (SEPT 1995) *|
28 |* and is provided to the U.S. Government only as a commercial end item. *|
29 |* Consistent with 48 C.F.R.12.212 and 48 C.F.R. 227.7202-1 through *|
30 |* 227.7202-4 (JUNE 1995), all U.S. Government End Users acquire the *|
31 |* source code with only those rights set forth herein. *|
32 |* *|
33 |* Any use of this source code in individual and commercial software must *|
34 |* include, in the user documentation and internal comments to the code, *|
35 |* the above Disclaimer and U.S. Government End Users Notice. *|
36 |* *|
37 |* *|
38 \***************************************************************************/
39 
40 #include "PlusCommon.h"
41 #include "nvGPUutil.h"
42 #include "glExtensions.h"
43 
44 #if WIN32
45 //----------------------------------------------------------------------------
46 bool CreateDummyGLWindowWin32( HWND* hWnd, HGLRC* hGLRC )
47 {
48  HINSTANCE hInstance = GetModuleHandle( NULL ); // Need a handle to this process instance
49  // Create the window class
50  WNDCLASSEX wc; // Windows Class Structure
51  wc.cbSize = sizeof( WNDCLASSEX );
52  wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
53  wc.lpfnWndProc = DefWindowProc; // WndProc Handles Messages, we'll pass a static version, which will receive a 'this' pointer
54  wc.cbClsExtra = 0; // No Extra Window Data
55  wc.cbWndExtra = 0; // No Extra Window Data
56  wc.hInstance = hInstance; // Set The Instance
57  wc.hIcon = NULL; // Load The Default Icon
58  wc.hIconSm = NULL;
59  wc.hCursor = LoadCursor( hInstance, IDC_ARROW ); // Load The Arrow Pointer
60  wc.hbrBackground = NULL; // No Background Required For GL
61  wc.lpszMenuName = NULL; // We Don't Want A Menu
62  wc.lpszClassName = "Dummy"; // Set The Class Name
63 
64  // register the window class
65  RegisterClassEx( &wc );
66 
67  // Call the windows function to create the window. The
68  *hWnd = CreateWindowEx( NULL, "Dummy", NULL, NULL, 0, 0, 1, 1, NULL, NULL, NULL, NULL );
69 
70  // Get the windows device context
71  HDC hDC = GetDC( *hWnd );
72 
74 
75  // We need a pixel format descriptor. A PFD tells how OpenGL draws
76  static PIXELFORMATDESCRIPTOR pfd =
77  {
78  sizeof( PIXELFORMATDESCRIPTOR ), // Size Of This Pixel Format Descriptor
79  1, // Version Number
80  PFD_DRAW_TO_WINDOW | // Format Must Support Window
81  PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
82  PFD_DOUBLEBUFFER, // Must Support Double Buffering
83  PFD_TYPE_RGBA, // Request An RGBA Format
84  8, // Select Our Color Depth
85  0, 0, 0, 0, 0, 0, // Color Bits Ignored
86  0, // No Alpha Buffer
87  0, // Shift Bit Ignored
88  0, // No Accumulation Buffer
89  0, 0, 0, 0, // Accumulation Bits Ignored
90  16, // 16Bit Z-Buffer (Depth Buffer)
91  0, // No Stencil Buffer
92  0, // No Auxiliary Buffer
93  PFD_MAIN_PLANE, // Main Drawing Layer
94  0, // Reserved
95  0, 0, 0 // Layer Masks Ignored
96  };
97 
98  int pixelformat;
99  // create the pixel pixel format descriptor
100  if ( ( pixelformat = ChoosePixelFormat( hDC, &pfd ) ) == 0 )
101  {
102  return false;
103  }
104  // set the pixel format descriptor
105  if ( SetPixelFormat( hDC, pixelformat, &pfd ) == FALSE )
106  {
107  return false;
108  }
109  // Create a wGL rendering context
110  *hGLRC = wglCreateContext( hDC );
111  // Activate the rendering context
112  wglMakeCurrent( hDC, *hGLRC );
113 
114  return true;
115 }
116 
117 //----------------------------------------------------------------------------
118 bool DestroyGLWindowWin32( HWND* hWnd, HGLRC* hGLRC )
119 {
120  HDC hDC = GetDC( *hWnd );
121  wglMakeCurrent( NULL, NULL );
122  wglDeleteContext( *hGLRC );
123  if( ReleaseDC( *hWnd, hDC ) == 0 )
124  {
125  return false;
126  }
127  if( DestroyWindow( *hWnd ) == FALSE )
128  {
129  return false;
130  }
131  return true;
132 }
133 
134 #endif
135 
136 //----------------------------------------------------------------------------
138 {
139  m_bInitialized = false;
140  m_nGpu = 0;
141  if( Init() )
142  {
143  m_bInitialized = true;
144  }
145 }
146 
147 //----------------------------------------------------------------------------
149 {
150  if( !m_bInitialized )
151  {
152  return;
153  }
154  for( int i = 0; i < m_nGpu; i++ )
155  {
156  delete m_lGpu[i];
157  m_lGpu[i] = NULL;
158  }
159 }
160 
161 //----------------------------------------------------------------------------
163 {
164  if( m_bInitialized )
165  {
166  return true;
167  }
168 
169 #if WIN32
170  HWND hWnd;
171  HGLRC hGLRC;
172 
173  if( CreateDummyGLWindowWin32( &hWnd, &hGLRC ) == false )
174  {
175  return false;
176  }
177 #elif __linux__
178 
179 #endif
180 
181  if( !loadAffinityExtension() )
182  {
183  LOG_ERROR( "Could not load OpenGL Affinity extension" );
184  return false;
185  }
186 
187  LOG_INFO( "Listing GPUs available for OpenGL GPU Affinity" );
188 
189  unsigned int GPUIdx = 0;
190 
191  GPU_DEVICE gpuDevice;
192  gpuDevice.cb = sizeof( gpuDevice );
193  HGPUNV hGPU;
194  bool bDisplay;
195  bool bPrimary;
196  while( wglEnumGpusNV( GPUIdx, &hGPU ) ) // First call this function to get a handle to the gpu
197  {
198  // wglEnumPGUsNV will fails if DeviceIdx > the available devices
199  LOG_DEBUG( "GPU# " << GPUIdx << ":" );
200  bDisplay = false;
201  bPrimary = false;
202 
203  // Now get the detailed information about this device:
204  // How many displays it's attached to and whether any of them
205  int DisplayDeviceIdx = 0;
206  while( wglEnumGpuDevicesNV( hGPU, DisplayDeviceIdx, &gpuDevice ) )
207  {
208  LOG_DEBUG( " Display# " << DisplayDeviceIdx );
209  LOG_DEBUG( " Name: " << gpuDevice.DeviceName );
210  LOG_DEBUG( " String: " << gpuDevice.DeviceString );
211 
212  bDisplay = true;
213 
214  if( gpuDevice.Flags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP )
215  {
216  LOG_DEBUG( " Attached to the desktop: LEFT=" << gpuDevice.rcVirtualScreen.left << ", RIGHT=" << gpuDevice.rcVirtualScreen.right
217  << ", TOP=" << gpuDevice.rcVirtualScreen.top << ", BOTTOM=" << gpuDevice.rcVirtualScreen.bottom );
218  }
219  else
220  {
221  LOG_DEBUG( " Not attached to the desktop." );
222  }
223 
224  // See if it's the primary GPU
225  if( gpuDevice.Flags & DISPLAY_DEVICE_PRIMARY_DEVICE )
226  {
227  LOG_DEBUG( " This is the PRIMARY Display Device." );
228  bPrimary = true;
229  }
230  DisplayDeviceIdx++;
231 
232  }
233  CNvGpu* gpu = new CNvGpu();
234  gpu->Init( hGPU, bPrimary, bDisplay );
235 
236  m_lGpu[GPUIdx] = gpu;
237 
238  GPUIdx++;
239  }
240 
241  m_nGpu = GPUIdx;
242  m_bInitialized = true;
243 
244 #if WIN32
245  // We can kill the dummy window now
246  if( DestroyGLWindowWin32( &hWnd, &hGLRC ) == false )
247  {
248  return false;
249  }
250 #elif __linux__
251 
252 #endif
253 
254  return true;
255 }
256 
257 //----------------------------------------------------------------------------
259 {
260  static CNvGpuTopology instance;
261  return instance;
262 }
263 
264 //----------------------------------------------------------------------------
266 {
267  return m_nGpu;
268 }
269 
270 //----------------------------------------------------------------------------
272 {
273  if( index >= 0 && index < m_nGpu )
274  {
275  return m_lGpu[index];
276  }
277  return NULL;
278 }
279 
280 //----------------------------------------------------------------------------
282 {
283  for( int i = 0; i < m_nGpu; i++ )
284  {
285  if( m_lGpu[i]->isPrimary() )
286  {
287  return i;
288  }
289  }
290  return 0;
291 }
292 
293 //----------------------------------------------------------------------------
295 {
296  for( int i = 0; i < m_nGpu; i++ )
297  {
298  if( m_lGpu[i]->isPrimary() )
299  {
300  return m_lGpu[i];
301  }
302  }
303  return NULL;
304 }
305 
306 //----------------------------------------------------------------------------
308 {
309 
310 }
311 
312 //----------------------------------------------------------------------------
314 {
315 
316 }
317 
318 //----------------------------------------------------------------------------
319 bool CNvGpu::Init( HGPUNV gpuAffinityHandle, bool bPrimary, bool bDisplay )
320 {
321  m_hGpu = gpuAffinityHandle;
322  m_bPrimary = bPrimary;
323  m_bDisplay = bDisplay;
324  return true;
325 }
326 
327 //----------------------------------------------------------------------------
329 {
330  return m_bPrimary;
331 }
332 
333 //----------------------------------------------------------------------------
335 {
336  return m_bDisplay;
337 }
338 
339 //----------------------------------------------------------------------------
341 {
342  return m_hGpu;
343 }
PFNWGLENUMGPUSNVPROC wglEnumGpusNV
virtual CNvGpu * GetGpu(int index)
Definition: nvGPUutil.cpp:271
virtual CNvGpu * GetPrimaryGpu()
Definition: nvGPUutil.cpp:294
virtual ~CNvGpu()
Definition: nvGPUutil.cpp:313
bool isDisplay()
Definition: nvGPUutil.cpp:334
for i
#define FALSE
Definition: ATC3DGm.h:220
CNvGpu * m_lGpu[MAX_GPUS]
Definition: nvGPUutil.h:91
bool m_bInitialized
Definition: nvGPUutil.h:93
HGPUNV getAffinityHandle()
Definition: nvGPUutil.cpp:340
bool Init(HGPUNV gpuAffinityHandle, bool bPrimary, bool bDisplay)
Definition: nvGPUutil.cpp:319
bool isPrimary()
Definition: nvGPUutil.cpp:328
PFNWGLENUMGPUDEVICESNVPROC wglEnumGpuDevicesNV
bool m_bDisplay
Definition: nvGPUutil.h:72
virtual ~CNvGpuTopology()
Definition: nvGPUutil.cpp:148
bool m_bPrimary
Definition: nvGPUutil.h:71
bool loadAffinityExtension()
HGPUNV m_hGpu
Definition: nvGPUutil.h:70
int GetPrimaryGpuIndex()
Definition: nvGPUutil.cpp:281
static CNvGpuTopology & Instance()
Definition: nvGPUutil.cpp:258