test3d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include <Fenster.h>
  2. #include <Globals.h>
  3. #include <MausEreignis.h>
  4. #include <TastaturEreignis.h>
  5. #include <Bildschirm.h>
  6. #include <RenderThread.h>
  7. #include <d3d11.h>
  8. #include <Bild.h>
  9. #include <iostream>
  10. #include <D3DX11async.h>
  11. using namespace Framework;
  12. Bildschirm *bs;
  13. void FClose( void *p, void *zF )
  14. {
  15. StopNachrichtenSchleife( ( (WFenster*)zF )->getFensterHandle() );
  16. }
  17. struct XMFLOAT3
  18. {
  19. float x;
  20. float y;
  21. float z;
  22. };
  23. struct Vertex
  24. {
  25. XMFLOAT3 pos;
  26. };
  27. class Kam3D : public Zeichnung
  28. {
  29. private:
  30. ID3D11Texture2D *d3d11RenderTarget;
  31. ID3D11Texture2D *d3d11Staging;
  32. ID3D11RenderTargetView *d3d11RenderTargetView;
  33. ID3D11DepthStencilView *d3d11DepthStencilView;
  34. ID3D11ShaderResourceView *d3d11ShaderResourceView;
  35. ID3D11DeviceContext *context;
  36. ID3D11Device *device;
  37. ID3D11Buffer *pVBuffer; // global
  38. ID3D11VertexShader *pVS; // the vertex shader
  39. ID3D11PixelShader *pPS; // the pixel shader
  40. ID3D11InputLayout *pLayout; // global
  41. public:
  42. Kam3D()
  43. : Zeichnung()
  44. {
  45. d3d11RenderTarget = 0;
  46. }
  47. ~Kam3D()
  48. {
  49. d3d11RenderTarget->Release();
  50. d3d11Staging->Release();
  51. d3d11RenderTargetView->Release();
  52. d3d11ShaderResourceView->Release();
  53. pVBuffer->Release();
  54. pVS->Release();
  55. pPS->Release();
  56. pLayout->Release();
  57. //d3d11DepthStencilView->Release();
  58. }
  59. bool init( ID3D11Device *device, ID3D11DeviceContext *context )
  60. {
  61. this->context = context;
  62. this->device = device;
  63. D3D11_TEXTURE2D_DESC textureDesc;
  64. HRESULT result;
  65. D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
  66. D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
  67. D3D11_SHADER_RESOURCE_VIEW_DESC shaderResourceViewDesc;
  68. // Initialize the render target texture description.
  69. ZeroMemory( &textureDesc, sizeof( textureDesc ) );
  70. // Setup the render target texture description.
  71. textureDesc.Width = getBreite();
  72. textureDesc.Height = getHöhe();
  73. textureDesc.MipLevels = 1;
  74. textureDesc.ArraySize = 1;
  75. textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
  76. textureDesc.SampleDesc.Count = 1;
  77. textureDesc.Usage = D3D11_USAGE_DEFAULT;
  78. textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
  79. textureDesc.CPUAccessFlags = 0;
  80. textureDesc.MiscFlags = 0;
  81. // Create the render target texture.
  82. result = device->CreateTexture2D( &textureDesc, NULL, &d3d11RenderTarget );
  83. if( FAILED( result ) )
  84. {
  85. return false;
  86. }
  87. // Initialize the render target texture description.
  88. ZeroMemory( &textureDesc, sizeof( textureDesc ) );
  89. // Setup the render target texture description.
  90. textureDesc.Width = getBreite();
  91. textureDesc.Height = getHöhe();
  92. textureDesc.MipLevels = 1;
  93. textureDesc.ArraySize = 1;
  94. textureDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
  95. textureDesc.SampleDesc.Count = 1;
  96. textureDesc.Usage = D3D11_USAGE_STAGING;
  97. textureDesc.BindFlags = 0;
  98. textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  99. textureDesc.MiscFlags = 0;
  100. // Create the render target texture.
  101. result = device->CreateTexture2D( &textureDesc, NULL, &d3d11Staging );
  102. if( FAILED( result ) )
  103. {
  104. return false;
  105. }
  106. // Setup the description of the render target view.
  107. renderTargetViewDesc.Format = textureDesc.Format;
  108. renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
  109. renderTargetViewDesc.Texture2D.MipSlice = 0;
  110. // Create the render target view.
  111. result = device->CreateRenderTargetView( d3d11RenderTarget, &renderTargetViewDesc, &d3d11RenderTargetView );
  112. if( FAILED( result ) )
  113. {
  114. return false;
  115. }
  116. // Setup the description of the shader resource view.
  117. shaderResourceViewDesc.Format = textureDesc.Format;
  118. shaderResourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
  119. shaderResourceViewDesc.Texture2D.MostDetailedMip = 0;
  120. shaderResourceViewDesc.Texture2D.MipLevels = 1;
  121. // Create the shader resource view.
  122. result = device->CreateShaderResourceView( d3d11RenderTarget, &shaderResourceViewDesc, &d3d11ShaderResourceView );
  123. if( FAILED( result ) )
  124. {
  125. return false;
  126. }
  127. /* ZeroMemory( &depthStencilViewDesc, sizeof( D3D11_DEPTH_STENCIL_VIEW_DESC ) );
  128. depthStencilViewDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
  129. depthStencilViewDesc.Flags = D3D11_DSV_FLAG::D3D11_DSV_READ_ONLY_DEPTH | D3D11_DSV_FLAG::D3D11_DSV_READ_ONLY_STENCIL;
  130. result = device->CreateDepthStencilView( d3d11RenderTarget, &depthStencilViewDesc, &d3d11DepthStencilView );
  131. if( FAILED( result ) )
  132. {
  133. return false;
  134. }*/
  135. InitGraphics();
  136. InitPipeline();
  137. return true;
  138. }
  139. void InitGraphics()
  140. {
  141. // create a triangle using the VERTEX struct
  142. Vertex vertexData[ 3 ];
  143. vertexData[ 0 ].pos = { 0.0f, 0.5f, 0.5f };
  144. vertexData[ 1 ].pos = { 0.5f, -0.5f, 0.5f };
  145. vertexData[ 2 ].pos = { -0.5f, -0.5f, 0.5f };
  146. // create the vertex buffer
  147. D3D11_BUFFER_DESC bd;
  148. ZeroMemory( &bd, sizeof( bd ) );
  149. bd.Usage = D3D11_USAGE_DYNAMIC; // write access access by CPU and GPU
  150. bd.ByteWidth = sizeof( Vertex ) * 3; // size is the VERTEX struct * 3
  151. bd.BindFlags = D3D11_BIND_VERTEX_BUFFER; // use as a vertex buffer
  152. bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; // allow CPU to write in buffer
  153. device->CreateBuffer( &bd, NULL, &pVBuffer ); // create the buffer
  154. // copy the vertices into the buffer
  155. D3D11_MAPPED_SUBRESOURCE ms;
  156. context->Map( pVBuffer, NULL, D3D11_MAP_WRITE_DISCARD, NULL, &ms ); // map the buffer
  157. memcpy( ms.pData, vertexData, sizeof( vertexData ) ); // copy the data
  158. context->Unmap( pVBuffer, NULL ); // unmap the buffer
  159. }
  160. void InitPipeline()
  161. {
  162. // load and compile the two shaders
  163. ID3D10Blob *VS, *PS;
  164. D3DX11CompileFromFile( "shader.hlsl", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS, 0, 0 );
  165. D3DX11CompileFromFile( "shader.hlsl", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS, 0, 0 );
  166. // encapsulate both shaders into shader objects
  167. device->CreateVertexShader( VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &pVS );
  168. device->CreatePixelShader( PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pPS );
  169. // set the shader objects
  170. context->VSSetShader( pVS, 0, 0 );
  171. context->PSSetShader( pPS, 0, 0 );
  172. // create the input layout object
  173. D3D11_INPUT_ELEMENT_DESC ied[] =
  174. {
  175. { "POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0, D3D11_INPUT_PER_VERTEX_DATA,0 }
  176. };
  177. device->CreateInputLayout( ied, 1, VS->GetBufferPointer(), VS->GetBufferSize(), &pLayout );
  178. UINT stride = sizeof( Vertex );
  179. UINT offset = 0;
  180. context->IASetVertexBuffers( 0, 1, &pVBuffer, &stride, &offset );
  181. context->IASetInputLayout( pLayout );
  182. context->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
  183. context->OMSetRenderTargets( 1, &d3d11RenderTargetView, 0 );
  184. context->PSSetShaderResources( 1, 1, &d3d11ShaderResourceView );
  185. }
  186. void render( Bild &rObj )
  187. {
  188. float color[ 4 ];
  189. // Setup the color to clear the buffer to.
  190. color[ 0 ] = 0;
  191. color[ 1 ] = 0;
  192. color[ 2 ] = 0;
  193. color[ 3 ] = 0;
  194. // Clear the back buffer.
  195. context->ClearRenderTargetView( d3d11RenderTargetView, color );
  196. //context->ClearDepthStencilView( d3d11DepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0 );
  197. context->Draw( 3, 0 ); // draw 3 vertices, starting from vertex 0
  198. //--------------------------------------------------------------------------------
  199. // Draw to Framework
  200. context->CopyResource( d3d11Staging, d3d11RenderTarget );
  201. D3D11_MAPPED_SUBRESOURCE buffer;
  202. context->Map( d3d11Staging, 0, D3D11_MAP::D3D11_MAP_READ, 0, &buffer );
  203. int *pixel = rObj.getBuffer();
  204. for( int y = 0, pitch = 0, bry = pos.x + pos.y * rObj.getBreite(); y < gr.y; ++y, pitch += buffer.RowPitch, bry += rObj.getBreite() )
  205. memcpy( &pixel[ bry ], &( (BYTE *)buffer.pData )[ pitch ], buffer.RowPitch );
  206. context->Unmap( d3d11Staging, 0 );
  207. std::cout << bs->getRenderZeit() << "\n";
  208. }
  209. };
  210. int main()
  211. {
  212. initFramework();
  213. WFenster *f = new WFenster();
  214. WNDCLASS fc = F_Normal( 0 );
  215. fc.lpszClassName = "Test";
  216. f->erstellen( WS_OVERLAPPEDWINDOW, fc );
  217. f->setGröße( 1600, 900 );
  218. f->setVSchließAktion( FClose );
  219. f->setMausAktion( _ret1ME );
  220. f->setTastaturAktion( _ret1TE );
  221. f->setAnzeigeModus( 1 );
  222. bs = new Bildschirm( f->getThis() );
  223. f->setBildschirm( bs->getThis() );
  224. bs->setTestRend( 0 );
  225. bs->setFüll( 0 );
  226. RenderTh *r = new RenderTh();
  227. r->setBildschirm( bs->getThis() );
  228. r->setMaxFps( 60 );
  229. r->beginn();
  230. Kam3D *obj = new Kam3D();
  231. obj->setPosition( 100, 100 );
  232. obj->setGröße( 1000, 600 );
  233. bs->addMember( obj );
  234. ID3D11Device *device;
  235. ID3D11DeviceContext *context;
  236. D3D_FEATURE_LEVEL levels[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0 };
  237. D3D_FEATURE_LEVEL levelsOk[ 3 ];
  238. HRESULT hr = D3D11CreateDevice( 0, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_DEBUG, levels, 3, D3D11_SDK_VERSION, &device, levelsOk, &context );
  239. if( hr == S_OK )
  240. {
  241. obj->init( device, context );
  242. }
  243. StartNachrichtenSchleife();
  244. r->beenden();
  245. r->release();
  246. f->setBildschirm( 0 );
  247. bs->release();
  248. f->release();
  249. delete obj;
  250. context->Release();
  251. device->Release();
  252. releaseFramework();
  253. return 0;
  254. }