1 // Created on: 2015-06-10
2 // Created by: Kirill Gavrilov
3 // Copyright (c) 2015 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
18 #include <D3DHost_View.hxx>
20 #include <D3DHost_GraphicDriver.hxx>
21 #include <TCollection_ExtendedString.hxx>
23 #include <Standard_WarningDisableFunctionCast.hxx>
25 IMPLEMENT_STANDARD_RTTIEXT(D3DHost_View,OpenGl_View)
31 D3DHost_VendorId_AMD = 0x1002,
32 D3DHost_VendorId_NVIDIA = 0x10DE,
33 D3DHost_VendorId_Intel = 0x8086,
37 // =======================================================================
38 // function : d3dFormatError
40 // =======================================================================
41 TCollection_AsciiString D3DHost_View::d3dFormatError (const long theErrCode)
45 case D3D_OK: return "OK";
46 case D3DERR_DEVICELOST: return "Device lost";
47 case D3DERR_DEVICEREMOVED: return "Device removed";
48 case D3DERR_DRIVERINTERNALERROR: return "Driver internal error";
49 case D3DERR_OUTOFVIDEOMEMORY: return "Out of video memory";
50 case D3DERR_INVALIDCALL: return "Invalid call";
51 default: return TCollection_AsciiString ("Error #") + int(theErrCode) + ")";
55 // =======================================================================
56 // function : D3DHost_View
58 // =======================================================================
59 D3DHost_View::D3DHost_View (const Handle(Graphic3d_StructureManager)& theMgr,
60 const Handle(D3DHost_GraphicDriver)& theDriver,
61 const Handle(OpenGl_Caps)& theCaps,
62 OpenGl_StateCounter* theCounter)
63 : OpenGl_View (theMgr, theDriver, theCaps, theCounter),
66 myD3dParams (new D3DPRESENT_PARAMETERS()),
67 myRefreshRate (D3DPRESENT_RATE_DEFAULT),
70 memset(myD3dParams.operator->(), 0, sizeof(D3DPRESENT_PARAMETERS));
72 myD3dParams->Windowed = TRUE;
73 myD3dParams->SwapEffect = D3DSWAPEFFECT_DISCARD;
74 myD3dParams->BackBufferFormat = D3DFMT_X8R8G8B8;
75 myD3dParams->BackBufferCount = 1;
76 myD3dParams->BackBufferHeight = 2;
77 myD3dParams->BackBufferWidth = 2;
78 myD3dParams->EnableAutoDepthStencil = FALSE;
79 myD3dParams->AutoDepthStencilFormat = D3DFMT_D16_LOCKABLE;
80 myD3dParams->FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
81 myD3dParams->PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
84 // =======================================================================
85 // function : ~D3DHost_View
87 // =======================================================================
88 D3DHost_View::~D3DHost_View()
90 if (!myD3dWglFbo.IsNull())
92 myD3dWglFbo->Release (myWorkspace->GetGlContext().operator->());
93 myD3dWglFbo.Nullify();
95 if (myD3dDevice != NULL)
97 myD3dDevice->Release();
100 if (myD3dLib != NULL)
107 // =======================================================================
108 // function : D3dColorSurface
110 // =======================================================================
111 IDirect3DSurface9* D3DHost_View::D3dColorSurface() const
113 return myD3dWglFbo->D3dColorSurface();
116 // =======================================================================
117 // function : SetWindow
119 // =======================================================================
120 void D3DHost_View::SetWindow (const Handle(Aspect_Window)& theWindow,
121 const Aspect_RenderingContext theContext)
123 if (!myD3dWglFbo.IsNull())
125 myD3dWglFbo->Release (myWorkspace->GetGlContext().operator->());
126 myD3dWglFbo.Nullify();
128 if (myD3dDevice != NULL)
130 myD3dDevice->Release();
134 OpenGl_View::SetWindow (theWindow, theContext);
136 if (!myWindow.IsNull())
139 d3dCreateRenderTarget();
143 // =======================================================================
144 // function : DiagnosticInformation
146 // =======================================================================
147 void D3DHost_View::DiagnosticInformation (TColStd_IndexedDataMapOfStringString& theDict,
148 Graphic3d_DiagnosticInfo theFlags) const
150 base_type::DiagnosticInformation (theDict, theFlags);
151 if (myD3dDevice == NULL)
157 memset (&aDevCaps, 0, sizeof(aDevCaps));
158 if (myD3dDevice->GetDeviceCaps (&aDevCaps) < 0)
163 const UINT anAdapter = aDevCaps.AdapterOrdinal;
164 D3DADAPTER_IDENTIFIER9 aDevId;
165 memset (&aDevId, 0, sizeof(aDevId));
166 if (myD3dLib->GetAdapterIdentifier (anAdapter, 0, &aDevId) < 0)
171 TCollection_AsciiString aVendorId ((int )aDevId.VendorId);
172 switch (aDevId.VendorId)
174 case D3DHost_VendorId_AMD: aVendorId = "AMD"; break;
175 case D3DHost_VendorId_NVIDIA: aVendorId = "NVIDIA"; break;
176 case D3DHost_VendorId_Intel: aVendorId = "Intel"; break;
178 theDict.Add ("D3Dvendor", aVendorId);
179 theDict.Add ("D3Ddescription", aDevId.Description);
180 theDict.Add ("D3DdeviceName", aDevId.DeviceName);
181 theDict.Add ("D3Ddriver", aDevId.Driver);
182 theDict.Add ("D3DdeviceId", TCollection_AsciiString((int )aDevId.DeviceId));
183 theDict.Add ("D3Dinterop", myD3dWglFbo.IsNull() || myD3dWglFbo->D3dFallback()
184 ? "Software Fallback"
185 : "WGL_NV_DX_interop");
188 // =======================================================================
189 // function : d3dInitLib
191 // =======================================================================
192 bool D3DHost_View::d3dInitLib()
194 if (myD3dLib == NULL)
196 IDirect3D9Ex* aD3dLibEx = NULL;
197 // we link against d3d (using Direct3DCreate9 symbol), thus it should be already loaded
198 HMODULE aLib = GetModuleHandleW (L"d3d9");
201 // retrieve D3D9Ex function dynamically (available only since Vista+)
202 typedef HRESULT (WINAPI* Direct3DCreate9Ex_t)(UINT , IDirect3D9Ex** );
203 Direct3DCreate9Ex_t Direct3DCreate9ExProc = (Direct3DCreate9Ex_t )GetProcAddress (aLib, "Direct3DCreate9Ex");
204 if (Direct3DCreate9ExProc != NULL)
206 Direct3DCreate9ExProc(D3D_SDK_VERSION, &aD3dLibEx);
209 myD3dLib = aD3dLibEx;
210 myIsD3dEx = aD3dLibEx != NULL;
211 if (myD3dLib == NULL)
213 myD3dLib = Direct3DCreate9 (D3D_SDK_VERSION);
216 return myD3dLib != NULL;
219 // =======================================================================
220 // function : d3dInit
222 // =======================================================================
223 bool D3DHost_View::d3dInit()
227 myWorkspace->GetGlContext()->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH, "Direct3DCreate9 failed!");
231 UINT anAdapterId = D3DADAPTER_DEFAULT;
233 // setup the present parameters
234 D3DDISPLAYMODE aCurrMode;
235 memset(&aCurrMode, 0, sizeof(aCurrMode));
236 if (myD3dLib->GetAdapterDisplayMode (anAdapterId, &aCurrMode) == D3D_OK)
238 myD3dParams->BackBufferFormat = aCurrMode.Format;
239 myRefreshRate = aCurrMode.RefreshRate;
241 myD3dParams->Windowed = TRUE;
242 myD3dParams->BackBufferWidth = myWindow->Width();
243 myD3dParams->BackBufferHeight = myWindow->Height();
244 myD3dParams->hDeviceWindow = (HWND )myWindow->PlatformWindow()->NativeHandle();
246 // create the Video Device
247 HRESULT isOK = myD3dLib->CreateDevice (anAdapterId, D3DDEVTYPE_HAL,
248 (HWND )myWindow->PlatformWindow()->NativeHandle(),
249 D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
250 myD3dParams.get(), &myD3dDevice);
256 return myD3dDevice != NULL;
259 // =======================================================================
260 // function : d3dReset
262 // =======================================================================
263 bool D3DHost_View::d3dReset()
265 if (myD3dDevice == NULL)
270 myD3dParams->Windowed = TRUE;
271 myD3dParams->BackBufferWidth = myWindow->Width();
272 myD3dParams->BackBufferHeight = myWindow->Height();
273 myD3dParams->hDeviceWindow = (HWND )myWindow->PlatformWindow()->NativeHandle();
274 myD3dParams->FullScreen_RefreshRateInHz = !myD3dParams->Windowed ? myRefreshRate : 0;
276 HRESULT isOK = myD3dDevice->Reset(myD3dParams.get());
277 return isOK == D3D_OK;
280 // =======================================================================
281 // function : d3dCreateRenderTarget
283 // =======================================================================
284 bool D3DHost_View::d3dCreateRenderTarget()
286 bool toD3dFallback = false;
287 if (myD3dWglFbo.IsNull())
289 myD3dWglFbo = new D3DHost_FrameBuffer();
293 toD3dFallback = myD3dWglFbo->D3dFallback();
298 toD3dFallback = !myD3dWglFbo->InitD3dInterop (myWorkspace->GetGlContext(),
303 0); // do not request depth-stencil attachment since buffer will be flipped using addition FBO (myToFlipOutput)
307 if (!myD3dWglFbo->InitD3dFallback (myWorkspace->GetGlContext(),
312 GL_DEPTH24_STENCIL8))
318 myD3dDevice->SetRenderTarget (0, myD3dWglFbo->D3dColorSurface());
322 // =======================================================================
323 // function : d3dBeginRender
325 // =======================================================================
326 void D3DHost_View::d3dBeginRender()
328 if (myD3dDevice == NULL)
333 // clear the back buffer
334 myD3dDevice->Clear (0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
335 myD3dDevice->BeginScene();
338 // =======================================================================
339 // function : d3dEndRender
341 // =======================================================================
342 void D3DHost_View::d3dEndRender()
344 if (myD3dDevice != NULL)
346 myD3dDevice->EndScene();
350 // =======================================================================
351 // function : d3dSwap
353 // =======================================================================
354 bool D3DHost_View::d3dSwap()
356 if (myD3dDevice == NULL)
361 const HRESULT isOK = myD3dDevice->Present (NULL, NULL, NULL, NULL);
364 myWorkspace->GetGlContext()->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_ERROR, 0, GL_DEBUG_SEVERITY_HIGH,
365 TCollection_AsciiString("Direct3D9, Present device failed, ") + d3dFormatError (isOK));
367 return isOK == D3D_OK;
370 // =======================================================================
373 // =======================================================================
374 void D3DHost_View::Redraw()
376 if (!myWorkspace->Activate()
377 || myD3dDevice == NULL)
381 else if (!myFBO.IsNull())
383 OpenGl_View::Redraw();
387 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
388 if (myWindow->PlatformWindow()->IsVirtual()
389 && aCtx->arbFBO == NULL)
391 // do a dirty hack in extreme fallback mode with OpenGL driver not supporting FBO,
392 // the back buffer of hidden window is used for rendering as offscreen buffer
393 myTransientDrawToFront = false;
394 int aWinSizeX = 0, aWinSizeY = 0;
395 myWindow->PlatformWindow()->Size (aWinSizeX, aWinSizeY);
396 WINDOWPLACEMENT aPlace;
397 GetWindowPlacement ((HWND )myWindow->PlatformWindow()->NativeHandle(), &aPlace);
398 if (aPlace.rcNormalPosition.right - aPlace.rcNormalPosition.left != aWinSizeX
399 || aPlace.rcNormalPosition.bottom - aPlace.rcNormalPosition.top != aWinSizeY)
401 aPlace.rcNormalPosition.right = aPlace.rcNormalPosition.left + aWinSizeX;
402 aPlace.rcNormalPosition.bottom = aPlace.rcNormalPosition.top + aWinSizeY;
403 aPlace.showCmd = SW_HIDE;
404 SetWindowPlacement ((HWND )myWindow->PlatformWindow()->NativeHandle(), &aPlace);
408 myD3dWglFbo->LockSurface (aCtx);
409 if (myD3dWglFbo->IsValid())
411 myToFlipOutput = Standard_True;
414 OpenGl_View::Redraw();
416 myD3dWglFbo->UnlockSurface (aCtx);
417 myToFlipOutput = Standard_False;
418 if (aCtx->caps->buffersNoSwap)
423 // blit result to the D3D back buffer and swap
426 IDirect3DSurface9* aBackbuffer = NULL;
427 myD3dDevice->GetBackBuffer (0, 0, D3DBACKBUFFER_TYPE_MONO, &aBackbuffer);
428 myD3dDevice->StretchRect (myD3dWglFbo->D3dColorSurface(), NULL, aBackbuffer, NULL, D3DTEXF_LINEAR);
429 aBackbuffer->Release();
435 // =======================================================================
436 // function : RedrawImmediate
438 // =======================================================================
439 void D3DHost_View::RedrawImmediate()
441 Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext();
442 if (!myTransientDrawToFront
443 || !myBackBufferRestored
444 || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid()))
449 else if (!myWorkspace->Activate()
450 || myD3dDevice == NULL)
454 else if (!myFBO.IsNull())
456 OpenGl_View::Redraw();
460 myD3dWglFbo->LockSurface (aCtx);
461 if (myD3dWglFbo->IsValid())
463 myToFlipOutput = Standard_True;
466 OpenGl_View::RedrawImmediate();
468 myD3dWglFbo->UnlockSurface (aCtx);
469 myToFlipOutput = Standard_False;
470 if (aCtx->caps->buffersNoSwap)
475 // blit result to the D3D back buffer and swap
478 IDirect3DSurface9* aBackbuffer = NULL;
479 myD3dDevice->GetBackBuffer (0, 0, D3DBACKBUFFER_TYPE_MONO, &aBackbuffer);
480 myD3dDevice->StretchRect (myD3dWglFbo->D3dColorSurface(), NULL, aBackbuffer, NULL, D3DTEXF_LINEAR);
481 aBackbuffer->Release();
487 // =======================================================================
490 // =======================================================================
491 void D3DHost_View::Resized()
493 const Standard_Integer aWidthOld = myWindow->Width();
494 const Standard_Integer aHeightOld = myWindow->Height();
495 OpenGl_View::Resized();
496 if (aWidthOld == myWindow->Width()
497 && aHeightOld == myWindow->Height())
502 if (!myD3dWglFbo.IsNull())
504 myD3dWglFbo->Release (myWorkspace->GetGlContext().operator->());
506 if (!myWorkspace->GetGlContext()->caps->buffersNoSwap)
510 d3dCreateRenderTarget();