GraphicsApi.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. #include "GraphicsApi.h"
  2. #include "Fenster.h"
  3. #include "Bild.h"
  4. #include "TexturModel.h"
  5. #include "Textur.h"
  6. #include "Shader.h"
  7. #include "Globals.h"
  8. #include "UIVertexShader.h"
  9. #include "UIPixelShader.h"
  10. #include "Render3D.h"
  11. #include "TexturList.h"
  12. #include "Kam3D.h"
  13. #include "DLLRegister.h"
  14. #include <d3d11.h>
  15. #include <d3d9.h>
  16. using namespace Framework;
  17. GraphicsApi::GraphicsApi( GraphicApiType typ )
  18. {
  19. this->typ = typ;
  20. fenster = 0;
  21. backBufferSize = Vec2<int>( 0, 0 );
  22. fullScreen = 0;
  23. ref = 1;
  24. }
  25. GraphicsApi::~GraphicsApi()
  26. {
  27. if( fenster )
  28. fenster->release();
  29. }
  30. void GraphicsApi::initialize( WFenster * fenster, Vec2<int> backBufferSize, bool fullScreen )
  31. {
  32. if( this->fenster )
  33. this->fenster->release();
  34. this->fenster = fenster;
  35. this->backBufferSize = backBufferSize;
  36. this->fullScreen = fullScreen;
  37. }
  38. void GraphicsApi::setBackBufferSize( Vec2< int > size )
  39. {
  40. backBufferSize = size;
  41. update();
  42. }
  43. void GraphicsApi::setFullScreen( bool fullScreen )
  44. {
  45. this->fullScreen = fullScreen;
  46. update();
  47. }
  48. void GraphicsApi::beginFrame( bool fill2D, bool fill3D, int fillColor )
  49. {}
  50. void GraphicsApi::renderKamera( Kam3D *zKamera )
  51. {}
  52. GraphicApiType GraphicsApi::getTyp() const
  53. {
  54. return typ;
  55. }
  56. Vec2< int > GraphicsApi::getBackBufferSize() const
  57. {
  58. return backBufferSize;
  59. }
  60. bool GraphicsApi::isFullScreen() const
  61. {
  62. return fullScreen;
  63. }
  64. GraphicsApi *GraphicsApi::getThis()
  65. {
  66. ref++;
  67. return this;
  68. }
  69. GraphicsApi *GraphicsApi::release()
  70. {
  71. if( !--ref )
  72. delete this;
  73. return 0;
  74. }
  75. DirectX9::DirectX9()
  76. : GraphicsApi( DIRECTX9 ),
  77. pDirect3D( 0 ),
  78. pDevice( 0 ),
  79. pBackBuffer( 0 ),
  80. backRect( new D3DLOCKED_RECT() )
  81. {
  82. uiBild = new Bild( 1 );
  83. }
  84. DirectX9::~DirectX9()
  85. {
  86. backRect->pBits = NULL;
  87. if( pBackBuffer )
  88. {
  89. pBackBuffer->Release();
  90. pBackBuffer = NULL;
  91. }
  92. if( pDevice )
  93. {
  94. pDevice->Release();
  95. pDevice = NULL;
  96. }
  97. if( pDirect3D )
  98. {
  99. pDirect3D->Release();
  100. pDirect3D = NULL;
  101. }
  102. uiBild->release();
  103. }
  104. typedef IDirect3D9 *( *D3D9CreateFunction )( UINT );
  105. void DirectX9::initialize( WFenster * fenster, Vec2<int> backBufferSize, bool fullScreen )
  106. {
  107. if( pDirect3D )
  108. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  109. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  110. HINSTANCE dll = getDLLRegister()->ladeDLL( "d3d9.dll", "d3d9.dll" );
  111. D3D9CreateFunction direct3DCreate9 = (D3D9CreateFunction)GetProcAddress( dll, "Direct3DCreate9" );
  112. pDirect3D = direct3DCreate9( D3D_SDK_VERSION );
  113. D3DPRESENT_PARAMETERS d3dpp;
  114. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  115. d3dpp.Windowed = !fullScreen;
  116. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  117. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  118. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  119. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  120. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  121. if( !backBufferSize.x || !backBufferSize.y )
  122. backBufferSize = fenster->getKörperGröße();
  123. d3dpp.BackBufferHeight = backBufferSize.y;
  124. d3dpp.BackBufferWidth = backBufferSize.x;
  125. uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 );
  126. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  127. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  128. if( result != S_OK )
  129. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  130. if( pDevice )
  131. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  132. if( result != S_OK )
  133. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  134. }
  135. void DirectX9::update()
  136. {
  137. if( !pDirect3D )
  138. return;
  139. backRect->pBits = NULL;
  140. if( pBackBuffer )
  141. {
  142. pBackBuffer->Release();
  143. pBackBuffer = NULL;
  144. }
  145. if( pDevice )
  146. {
  147. pDevice->Release();
  148. pDevice = NULL;
  149. }
  150. D3DPRESENT_PARAMETERS d3dpp;
  151. ZeroMemory( &d3dpp, sizeof( d3dpp ) );
  152. d3dpp.Windowed = !fullScreen;
  153. d3dpp.hDeviceWindow = fenster->getFensterHandle();
  154. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  155. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  156. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  157. d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
  158. if( !backBufferSize.x || !backBufferSize.y )
  159. backBufferSize = fenster->getKörperGröße();
  160. d3dpp.BackBufferHeight = backBufferSize.y;
  161. d3dpp.BackBufferWidth = backBufferSize.x;
  162. uiBild->neuBild( backBufferSize.x, backBufferSize.y, 0xFF000000 );
  163. HRESULT result = pDirect3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, fenster->getFensterHandle(),
  164. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE, &d3dpp, &pDevice );
  165. if( result != S_OK )
  166. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  167. if( pDevice )
  168. result = pDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );
  169. if( result != S_OK )
  170. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 9 konnte nicht initialisiert werden." ), MB_ICONERROR );
  171. }
  172. void DirectX9::beginFrame( bool fill2D, bool fill3D, int fillColor )
  173. {
  174. if( fill2D )
  175. uiBild->setFarbe( fillColor );
  176. }
  177. void DirectX9::presentFrame()
  178. {
  179. if( !uiBild->getBuffer() )
  180. return;
  181. HRESULT result;
  182. result = pBackBuffer->LockRect( backRect, 0, 0 );
  183. if( result != S_OK )
  184. {
  185. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  186. update();
  187. }
  188. // kopieren zum Bildschrirm
  189. int *bgBuff = uiBild->getBuffer();
  190. int tmpBr = sizeof( D3DCOLOR ) * uiBild->getBreite();
  191. for( int y = 0, pitch = 0, bry = 0; y < uiBild->getHeight(); ++y, pitch += backRect->Pitch, bry += uiBild->getBreite() )
  192. memcpy( &( (BYTE *)backRect->pBits )[ pitch ], ( void * ) & ( bgBuff[ bry ] ), tmpBr );
  193. // Beende Bild
  194. result = pBackBuffer->UnlockRect();
  195. if( result != S_OK )
  196. {
  197. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  198. update();
  199. }
  200. if( result != S_OK )
  201. {
  202. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  203. update();
  204. }
  205. result = pDevice->Present( 0, 0, 0, 0 );
  206. if( result != S_OK )
  207. {
  208. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  209. update();
  210. }
  211. }
  212. Bild *DirectX9::zUIRenderBild() const
  213. {
  214. return uiBild;
  215. }
  216. DirectX11::DirectX11()
  217. : GraphicsApi( DIRECTX11 ),
  218. d3d11Device( 0 ),
  219. d3d11Context( 0 ),
  220. d3d11SpawChain( 0 ),
  221. uiTextur( 0 ),
  222. vertexShader( 0 ),
  223. pixelShader( 0 ),
  224. sampleState( 0 ),
  225. rtview( 0 ),
  226. dsView( 0 ),
  227. depthStencilBuffer( 0 ),
  228. depthStencilState( 0 ),
  229. depthDisabledStencilState( 0 ),
  230. blendStateAlphaBlend( 0 ),
  231. vp( 0 ),
  232. texturModel( new TexturModel() ),
  233. renderObj( new Render3D() ),
  234. texturRegister( new TexturList() )
  235. {}
  236. DirectX11::~DirectX11()
  237. {
  238. if( renderObj )
  239. {
  240. renderObj->release();
  241. renderObj = 0;
  242. }
  243. texturModel->release();
  244. texturRegister->release();
  245. if( blendStateAlphaBlend )
  246. {
  247. blendStateAlphaBlend->Release();
  248. blendStateAlphaBlend = NULL;
  249. }
  250. if( uiTextur )
  251. {
  252. uiTextur->release();
  253. uiTextur = NULL;
  254. }
  255. if( sampleState )
  256. {
  257. sampleState->Release();
  258. sampleState = NULL;
  259. }
  260. if( pixelShader )
  261. {
  262. pixelShader->release();
  263. pixelShader = NULL;
  264. }
  265. if( vertexShader )
  266. {
  267. vertexShader->release();
  268. vertexShader = NULL;
  269. }
  270. if( depthDisabledStencilState )
  271. {
  272. depthDisabledStencilState->Release();
  273. depthDisabledStencilState = NULL;
  274. }
  275. delete vp;
  276. vp = 0;
  277. if( dsView )
  278. {
  279. dsView->Release();
  280. dsView = NULL;
  281. }
  282. if( depthStencilState )
  283. {
  284. depthStencilState->Release();
  285. depthStencilState = NULL;
  286. }
  287. if( depthStencilBuffer )
  288. {
  289. depthStencilBuffer->Release();
  290. depthStencilBuffer = NULL;
  291. }
  292. if( rtview )
  293. {
  294. rtview->Release();
  295. rtview = NULL;
  296. }
  297. if( d3d11SpawChain )
  298. {
  299. d3d11SpawChain->Release();
  300. d3d11SpawChain = NULL;
  301. }
  302. if( d3d11Device )
  303. {
  304. d3d11Device->Release();
  305. d3d11Device = NULL;
  306. }
  307. if( d3d11Context )
  308. {
  309. d3d11Context->Release();
  310. d3d11Context = NULL;
  311. }
  312. }
  313. void DirectX11::initialize( WFenster *fenster, Vec2<int> backBufferSize, bool fullScreen )
  314. {
  315. if( d3d11Device )
  316. return GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  317. GraphicsApi::initialize( fenster, backBufferSize, fullScreen );
  318. //--------------------------------------------------------------------
  319. // Create Device
  320. // create a struct to hold information about the swap chain
  321. DXGI_SWAP_CHAIN_DESC scd;
  322. // clear out the struct for use
  323. ZeroMemory( &scd, sizeof( DXGI_SWAP_CHAIN_DESC ) );
  324. // fill the swap chain description struct
  325. scd.BufferCount = 1; // one back buffer
  326. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  327. scd.OutputWindow = fenster ? fenster->getFensterHandle() : 0; // the window to be used
  328. scd.SampleDesc.Count = 1;
  329. // Set the scan line ordering and scaling to unspecified.
  330. scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
  331. scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
  332. scd.Windowed = !fullScreen;
  333. if( !backBufferSize.x || !backBufferSize.y )
  334. backBufferSize = fenster ? fenster->getKörperGröße() : Punkt( 0, 0 );
  335. scd.BufferDesc.Width = backBufferSize.x;
  336. scd.BufferDesc.Height = backBufferSize.y; // windowed/full-screen mode
  337. scd.BufferDesc.RefreshRate.Denominator = 1;
  338. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  339. // Discard the back buffer contents after presenting.
  340. scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  341. D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
  342. D3D_FEATURE_LEVEL support = D3D_FEATURE_LEVEL_11_0;
  343. // create a device, device context and swap chain using the information in the scd struct
  344. UINT flag = 0;
  345. #ifdef _DEBUG
  346. flag |= D3D11_CREATE_DEVICE_DEBUG;
  347. #endif
  348. HRESULT result = D3D11CreateDeviceAndSwapChain( NULL,
  349. D3D_DRIVER_TYPE_HARDWARE,
  350. NULL,
  351. flag,
  352. &featureLevel,
  353. 1,
  354. D3D11_SDK_VERSION,
  355. &scd,
  356. &d3d11SpawChain,
  357. &d3d11Device,
  358. &support,
  359. &d3d11Context );
  360. if( result != S_OK )
  361. {
  362. std::cout << "ERROR: D3D11CreateDeviceAndSwapChain returned " << result << "\n";
  363. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  364. return;
  365. }
  366. ID3D11Texture2D *backBufferPtr;
  367. // Get the pointer to the back buffer.
  368. result = d3d11SpawChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), (LPVOID *)& backBufferPtr );
  369. if( result != S_OK )
  370. {
  371. std::cout << "ERROR: d3d11SpawChain->GetBuffer returned " << result << "\n";
  372. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  373. return;
  374. }
  375. // Create the render target view with the back buffer pointer.
  376. result = d3d11Device->CreateRenderTargetView( backBufferPtr, NULL, &rtview );
  377. if( result != S_OK )
  378. {
  379. std::cout << "ERROR: d3d11Device->CreateRenderTargetView returned " << result << "\n";
  380. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  381. return;
  382. }
  383. // Release pointer to the back buffer as we no longer need it.
  384. backBufferPtr->Release();
  385. // Initialize the description of the depth buffer.
  386. D3D11_TEXTURE2D_DESC depthBufferDesc;
  387. ZeroMemory( &depthBufferDesc, sizeof( depthBufferDesc ) );
  388. // Set up the description of the depth buffer.
  389. depthBufferDesc.Width = backBufferSize.x;
  390. depthBufferDesc.Height = backBufferSize.y;
  391. depthBufferDesc.MipLevels = 1;
  392. depthBufferDesc.ArraySize = 1;
  393. depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  394. depthBufferDesc.SampleDesc.Count = 1;
  395. depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
  396. depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
  397. // Create the texture for the depth buffer using the filled out description.
  398. result = d3d11Device->CreateTexture2D( &depthBufferDesc, NULL, &depthStencilBuffer );
  399. if( result != S_OK )
  400. {
  401. std::cout << "ERROR: d3d11Device->CreateTexture2D returned " << result << "\n";
  402. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  403. return;
  404. }
  405. // Initialize the description of the stencil state.
  406. D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
  407. ZeroMemory( &depthStencilDesc, sizeof( depthStencilDesc ) );
  408. // Set up the description of the stencil state.
  409. depthStencilDesc.DepthEnable = true;
  410. depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  411. depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS_EQUAL;
  412. depthStencilDesc.StencilEnable = true;
  413. depthStencilDesc.StencilReadMask = 0xFF;
  414. depthStencilDesc.StencilWriteMask = 0xFF;
  415. // Stencil operations if pixel is front-facing.
  416. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  417. depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  418. depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  419. depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  420. // Stencil operations if pixel is back-facing.
  421. depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  422. depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  423. depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  424. depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  425. // Create the depth stencil state.
  426. result = d3d11Device->CreateDepthStencilState( &depthStencilDesc, &depthStencilState );
  427. if( result != S_OK )
  428. {
  429. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  430. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  431. return;
  432. }
  433. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  434. // Initialize the depth stencil view.
  435. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  436. ZeroMemory( &depthStencilViewDesc, sizeof( depthStencilViewDesc ) );
  437. // Set up the depth stencil view description.
  438. depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
  439. depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
  440. // Create the depth stencil view.
  441. result = d3d11Device->CreateDepthStencilView( depthStencilBuffer, &depthStencilViewDesc, &dsView );
  442. if( result != S_OK )
  443. {
  444. std::cout << "ERROR: d3d11Device->CreateDepthStencilView returned " << result << "\n";
  445. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  446. return;
  447. }
  448. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  449. vp = new D3D11_VIEWPORT();
  450. memset( vp, 0, sizeof( D3D11_VIEWPORT ) );
  451. vp->Width = (float)backBufferSize.x;
  452. vp->Height = (float)backBufferSize.y;
  453. vp->MinDepth = 0.0f;
  454. vp->MaxDepth = 1.0f;
  455. vp->TopLeftX = 0.0f;
  456. vp->TopLeftY = 0.0f;
  457. d3d11Context->RSSetViewports( 1, vp );
  458. D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
  459. // Clear the second depth stencil state before setting the parameters.
  460. ZeroMemory( &depthDisabledStencilDesc, sizeof( depthDisabledStencilDesc ) );
  461. // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is
  462. // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
  463. depthDisabledStencilDesc.DepthEnable = false;
  464. depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
  465. depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
  466. depthDisabledStencilDesc.StencilEnable = true;
  467. depthDisabledStencilDesc.StencilReadMask = 0xFF;
  468. depthDisabledStencilDesc.StencilWriteMask = 0xFF;
  469. depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  470. depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
  471. depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  472. depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  473. depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
  474. depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
  475. depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
  476. depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
  477. // Create the state using the device.
  478. result = d3d11Device->CreateDepthStencilState( &depthDisabledStencilDesc, &depthDisabledStencilState );
  479. if( result != S_OK )
  480. {
  481. std::cout << "ERROR: d3d11Device->CreateDepthStencilState returned " << result << "\n";
  482. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  483. return;
  484. }
  485. //-------------------------------------------------
  486. // Shaders
  487. vertexShader = new VertexShader();
  488. vertexShader->setCompiledByteArray( d3d11Device, (unsigned char *)UIVertexShader, sizeof( UIVertexShader ) );
  489. pixelShader = new PixelShader();
  490. pixelShader->setCompiledByteArray( d3d11Device, (unsigned char *)UIPixelShader, sizeof( UIPixelShader ) );
  491. D3D11_INPUT_ELEMENT_DESC polygonLayout[ 4 ];
  492. // Create the vertex input layout description.
  493. // This setup needs to match the VertexType stucture in the ModelClass and in the shader.
  494. polygonLayout[ 0 ].SemanticName = "POSITION";
  495. polygonLayout[ 0 ].SemanticIndex = 0;
  496. polygonLayout[ 0 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  497. polygonLayout[ 0 ].InputSlot = 0;
  498. polygonLayout[ 0 ].AlignedByteOffset = 0;
  499. polygonLayout[ 0 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  500. polygonLayout[ 0 ].InstanceDataStepRate = 0;
  501. polygonLayout[ 1 ].SemanticName = "TEXCOORD";
  502. polygonLayout[ 1 ].SemanticIndex = 0;
  503. polygonLayout[ 1 ].Format = DXGI_FORMAT_R32G32_FLOAT;
  504. polygonLayout[ 1 ].InputSlot = 0;
  505. polygonLayout[ 1 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  506. polygonLayout[ 1 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  507. polygonLayout[ 1 ].InstanceDataStepRate = 0;
  508. polygonLayout[ 2 ].SemanticName = "NORMAL";
  509. polygonLayout[ 2 ].SemanticIndex = 0;
  510. polygonLayout[ 2 ].Format = DXGI_FORMAT_R32G32B32_FLOAT;
  511. polygonLayout[ 2 ].InputSlot = 0;
  512. polygonLayout[ 2 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  513. polygonLayout[ 2 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  514. polygonLayout[ 2 ].InstanceDataStepRate = 0;
  515. polygonLayout[ 3 ].SemanticName = "KNOCHEN_ID";
  516. polygonLayout[ 3 ].SemanticIndex = 0;
  517. polygonLayout[ 3 ].Format = DXGI_FORMAT_R32_UINT;
  518. polygonLayout[ 3 ].InputSlot = 0;
  519. polygonLayout[ 3 ].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
  520. polygonLayout[ 3 ].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
  521. polygonLayout[ 3 ].InstanceDataStepRate = 0;
  522. vertexShader->erstelleInputLayout( d3d11Device, polygonLayout, 4 );
  523. vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * MAX_KNOCHEN_ANZ, 0 ); // matrizen für skelett annimationen
  524. vertexShader->erstelleConstBuffer( d3d11Device, sizeof( Mat4< float > ) * 2, 1 ); // View and Projection Matrix
  525. pixelShader->erstelleConstBuffer( d3d11Device, sizeof( float ) * 3, 0 ); // Kamera Position
  526. pixelShader->erstelleConstBuffer( d3d11Device, sizeof( float ) * 3, 1 ); // materialkonstanten nach phong model
  527. pixelShader->erstelleConstBuffer( d3d11Device, sizeof( int ) * 2, 2 ); // materialkonstanten nach phong model
  528. // TODO: Remove Following Test Code
  529. int lc[] = { 1, 6 };
  530. pixelShader->füllConstBuffer( d3d11Context, (char *)lc, 2, sizeof( int ) * 2 );
  531. // Create a texture sampler state description.
  532. D3D11_SAMPLER_DESC samplerDesc;
  533. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
  534. samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
  535. samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
  536. samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
  537. samplerDesc.MipLODBias = 0.0f;
  538. samplerDesc.MaxAnisotropy = 1;
  539. samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
  540. samplerDesc.BorderColor[ 0 ] = 0;
  541. samplerDesc.BorderColor[ 1 ] = 0;
  542. samplerDesc.BorderColor[ 2 ] = 0;
  543. samplerDesc.BorderColor[ 3 ] = 0;
  544. samplerDesc.MinLOD = 0;
  545. samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
  546. // Create the texture sampler state.
  547. result = d3d11Device->CreateSamplerState( &samplerDesc, &sampleState );
  548. if( result != S_OK )
  549. {
  550. std::cout << "ERROR: d3d11Device->CreateSamplerState returned " << result << "\n";
  551. WMessageBox( fenster->getFensterHandle(), new Text( "Fehler" ), new Text( "DirectX 11 konnte nicht initialisiert werden." ), MB_ICONERROR );
  552. return;
  553. }
  554. //---------------------------------------------------------------
  555. // Framework Backbuffer Texture
  556. Bild *renderB = new Bild( 1 );
  557. renderB->setAlpha3D( 1 );
  558. renderB->neuBild( backBufferSize.x, backBufferSize.y, 0 );
  559. uiTextur = new Textur();
  560. uiTextur->setBildZ( renderB );
  561. texturRegister->addTextur( uiTextur->getThis(), "f_Render_Bild" );
  562. texturModel->setSize( backBufferSize );
  563. texturModel->setTextur( uiTextur->getThis() );
  564. D3D11_BLEND_DESC blendState;
  565. ZeroMemory( &blendState, sizeof( D3D11_BLEND_DESC ) );
  566. blendState.AlphaToCoverageEnable = false;
  567. blendState.IndependentBlendEnable = false;
  568. blendState.RenderTarget[ 0 ].BlendEnable = true;
  569. blendState.RenderTarget[ 0 ].SrcBlend = D3D11_BLEND_SRC_ALPHA;
  570. blendState.RenderTarget[ 0 ].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
  571. blendState.RenderTarget[ 0 ].BlendOp = D3D11_BLEND_OP_ADD;
  572. blendState.RenderTarget[ 0 ].SrcBlendAlpha = D3D11_BLEND_ZERO;
  573. blendState.RenderTarget[ 0 ].DestBlendAlpha = D3D11_BLEND_ONE;
  574. blendState.RenderTarget[ 0 ].BlendOpAlpha = D3D11_BLEND_OP_ADD;
  575. blendState.RenderTarget[ 0 ].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
  576. d3d11Device->CreateBlendState( &blendState, &blendStateAlphaBlend );
  577. d3d11Context->OMSetBlendState( blendStateAlphaBlend, 0, 0xFFFFFFFF );
  578. // Setup Render Objekt
  579. if( renderObj )
  580. renderObj->release();
  581. renderObj = new Render3D();
  582. d3d11Device->AddRef();
  583. renderObj->setDevice( d3d11Device );
  584. d3d11Context->AddRef();
  585. renderObj->setContext( d3d11Context );
  586. renderObj->benutzeShader( VERTEX, vertexShader->getThis() );
  587. d3d11Context->PSSetSamplers( 0, 1, &sampleState );
  588. renderObj->benutzeShader( PIXEL, pixelShader->getThis() );
  589. }
  590. void DirectX11::update()
  591. {
  592. if( renderObj )
  593. {
  594. renderObj->release();
  595. renderObj = 0;
  596. }
  597. texturModel->release();
  598. texturRegister->leeren();
  599. if( blendStateAlphaBlend )
  600. {
  601. blendStateAlphaBlend->Release();
  602. blendStateAlphaBlend = NULL;
  603. }
  604. if( uiTextur )
  605. {
  606. uiTextur->release();
  607. uiTextur = NULL;
  608. }
  609. if( sampleState )
  610. {
  611. sampleState->Release();
  612. sampleState = NULL;
  613. }
  614. if( pixelShader )
  615. {
  616. pixelShader->release();
  617. pixelShader = NULL;
  618. }
  619. if( vertexShader )
  620. {
  621. vertexShader->release();
  622. vertexShader = NULL;
  623. }
  624. if( depthDisabledStencilState )
  625. {
  626. depthDisabledStencilState->Release();
  627. depthDisabledStencilState = NULL;
  628. }
  629. delete vp;
  630. vp = 0;
  631. if( dsView )
  632. {
  633. dsView->Release();
  634. dsView = NULL;
  635. }
  636. if( depthStencilState )
  637. {
  638. depthStencilState->Release();
  639. depthStencilState = NULL;
  640. }
  641. if( depthStencilBuffer )
  642. {
  643. depthStencilBuffer->Release();
  644. depthStencilBuffer = NULL;
  645. }
  646. if( rtview )
  647. {
  648. rtview->Release();
  649. rtview = NULL;
  650. }
  651. if( d3d11SpawChain )
  652. {
  653. d3d11SpawChain->Release();
  654. d3d11SpawChain = NULL;
  655. }
  656. if( d3d11Device )
  657. {
  658. d3d11Device->Release();
  659. d3d11Device = NULL;
  660. }
  661. if( d3d11Context )
  662. {
  663. d3d11Context->Release();
  664. d3d11Context = NULL;
  665. }
  666. initialize( fenster->getThis(), backBufferSize, fullScreen );
  667. }
  668. void DirectX11::beginFrame( bool fill2D, bool fill3D, int fillColor )
  669. {
  670. if( fill2D )
  671. uiTextur->zBild()->setFarbe( fillColor );
  672. if( fill3D )
  673. {
  674. float color[ 4 ];
  675. // Setup the color to clear the buffer.
  676. color[ 0 ] = ( ( fillColor >> 16 ) & 0xFF ) / 255.f; // R
  677. color[ 1 ] = ( ( fillColor >> 8 ) & 0xFF ) / 255.f; // G
  678. color[ 2 ] = ( fillColor & 0xFF ) / 255.f; // B
  679. color[ 3 ] = ( ( fillColor >> 24 ) & 0xFF ) / 255.f; // A
  680. d3d11Context->ClearRenderTargetView( rtview, color );
  681. // Clear the depth buffer.
  682. d3d11Context->ClearDepthStencilView( dsView, D3D11_CLEAR_DEPTH, 1, 0 );
  683. }
  684. // Bind the render target view and depth stencil buffer to the output render pipeline.
  685. d3d11Context->OMSetRenderTargets( 1, &rtview, dsView );
  686. // Set the depth stencil state.
  687. d3d11Context->OMSetDepthStencilState( depthStencilState, 1 );
  688. }
  689. void DirectX11::renderKamera( Kam3D *zKamera )
  690. {
  691. zKamera->render( renderObj );
  692. }
  693. void DirectX11::presentFrame()
  694. {
  695. // Set the depth stencil state.
  696. d3d11Context->OMSetDepthStencilState( depthDisabledStencilState, 1 );
  697. uiTextur->updateTextur( renderObj );
  698. d3d11Context->RSSetViewports( 1, vp );
  699. float screenAspect = (float)backBufferSize.x / (float)backBufferSize.y;
  700. Mat4< float > view = view.translation( Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f ) );
  701. renderObj->setKameraMatrix( view, view.projektion( (float)PI / 4.0f, screenAspect, 0.1f, 10000.f ), Vec3< float >( 0.f, 0.f, backBufferSize.y * 1.2075f ) );
  702. if( fenster && !IsIconic( fenster->getFensterHandle() ) )
  703. texturModel->render( renderObj );
  704. HRESULT result = d3d11SpawChain->Present( 0, 0 );
  705. if( !SUCCEEDED( result ) )
  706. {
  707. update();
  708. WMessageBox( fenster ? fenster->getFensterHandle() : 0, new Text( "Fehler" ), new Text( "Es ist ein Fehler beim rendern aufgetreten." ), MB_ICONERROR );
  709. }
  710. }
  711. Bild *DirectX11::zUIRenderBild() const
  712. {
  713. return uiTextur->zBild();
  714. }