GraphicsApi.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #pragma once
  2. #include "Vec2.h"
  3. #include "Mat4.h"
  4. #include "Ebene3D.h"
  5. #include "Bildschirm.h"
  6. //! DirectX 12 Types
  7. struct ID3D12Debug;
  8. struct ID3D12Device;
  9. struct ID3D12InfoQueue;
  10. struct ID3D12CommandQueue;
  11. struct IDXGISwapChain4;
  12. struct ID3D12DescriptorHeap;
  13. struct ID3D12Resource;
  14. struct ID3D12CommandAllocator;
  15. struct ID3D12GraphicsCommandList;
  16. struct ID3D12Fence;
  17. struct D3D12_VIEWPORT;
  18. struct D3D12_VERTEX_BUFFER_VIEW;
  19. struct D3D12_INDEX_BUFFER_VIEW;
  20. struct ID3D12RootSignature;
  21. struct ID3D12PipelineState;
  22. //! DirectX 11 Types
  23. struct ID3D11Device;
  24. struct ID3D11DeviceContext;
  25. struct IDXGISwapChain;
  26. struct ID3D11Texture2D;
  27. struct ID3D11SamplerState;
  28. struct ID3D11ShaderResourceView;
  29. struct ID3D11RenderTargetView;
  30. struct ID3D11DepthStencilView;
  31. struct ID3D11DepthStencilState;
  32. struct ID3D11RasterizerState;
  33. struct ID3D11BlendState;
  34. struct D3D11_VIEWPORT;
  35. //! DirectX 9 Types
  36. struct IDirect3D9;
  37. struct IDirect3DDevice9;
  38. struct IDirect3DSurface9;
  39. struct _D3DLOCKED_RECT;
  40. struct tagRECT;
  41. namespace Framework
  42. {
  43. class WFenster;
  44. class Bild;
  45. class Textur;
  46. class DX11PixelShader;
  47. class DX11VertexShader;
  48. class TexturModel;
  49. class Render3D;
  50. class TexturList;
  51. class Kam3D;
  52. class Model3D;
  53. class DX11Buffer;
  54. class DX11StructuredBuffer;
  55. class DX12Buffer;
  56. class DX12DirectCommandQueue;
  57. class DX12CopyCommandQueue;
  58. class DX12ComputeCommandQueue;
  59. class DX12PixelShader;
  60. class DX12VertexShader;
  61. class DX12VertexBuffer;
  62. class DX12IndexBuffer;
  63. class Model3DList;
  64. class DXBuffer;
  65. class Model3DData;
  66. enum GraphicApiType;
  67. struct DiffuseLight
  68. {
  69. Vec3<float> direction;
  70. Vec3<float> color;
  71. };
  72. struct PointLight
  73. {
  74. Vec3<float> position;
  75. Vec3<float> color;
  76. float radius;
  77. };
  78. class GraphicsApi : public virtual ReferenceCounter
  79. {
  80. protected:
  81. GraphicApiType typ;
  82. WFenster* fenster;
  83. Vec2<int> backBufferSize;
  84. Model3DList* modelList;
  85. int nextModelId;
  86. Critical cs;
  87. bool fullScreen;
  88. DLLEXPORT virtual DXBuffer* createIndexBuffer() = 0;
  89. DLLEXPORT virtual DXBuffer* createVertexBuffer() = 0;
  90. public:
  91. DLLEXPORT GraphicsApi( GraphicApiType typ );
  92. DLLEXPORT virtual ~GraphicsApi();
  93. DLLEXPORT virtual void initialize( WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen );
  94. DLLEXPORT virtual void setBackBufferSize( Vec2< int > size );
  95. DLLEXPORT virtual void setFullScreen( bool fullScreen );
  96. DLLEXPORT virtual void update() = 0;
  97. DLLEXPORT virtual void beginFrame( bool fill2D = 0, bool fill3D = 0, int fillColor = 0 );
  98. DLLEXPORT virtual void renderKamera( Kam3D* zKamera );
  99. DLLEXPORT virtual void renderKamera( Kam3D* zKamera, Textur* zTarget );
  100. DLLEXPORT virtual void presentFrame() = 0;
  101. DLLEXPORT virtual Textur* createOrGetTextur( const char* name, Bild* b = 0 );
  102. DLLEXPORT GraphicApiType getTyp() const;
  103. DLLEXPORT Vec2< int > getBackBufferSize() const;
  104. DLLEXPORT bool isFullScreen() const;
  105. DLLEXPORT virtual Bild* zUIRenderBild() const = 0;
  106. // returns the specified model without increased reference counter
  107. DLLEXPORT virtual Model3DData* zModel( const char* name );
  108. // returns the specified model with increased reference counter
  109. DLLEXPORT virtual Model3DData* getModel( const char* name );
  110. // creates a new empty Model3DData object if the model does not exist yet
  111. DLLEXPORT virtual Model3DData* createModel( const char* name );
  112. // check if a model exists
  113. DLLEXPORT virtual bool hasModel( const char* name );
  114. };
  115. class DirectX9 : public GraphicsApi
  116. {
  117. private:
  118. IDirect3D9* pDirect3D;
  119. IDirect3DDevice9* pDevice;
  120. IDirect3DSurface9* pBackBuffer;
  121. _D3DLOCKED_RECT* backRect;
  122. Bild* uiBild;
  123. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  124. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  125. public:
  126. DLLEXPORT DirectX9();
  127. DLLEXPORT ~DirectX9();
  128. DLLEXPORT void initialize( WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen ) override;
  129. DLLEXPORT void update() override;
  130. DLLEXPORT void beginFrame( bool fill2D, bool fill3D, int fillColor ) override;
  131. DLLEXPORT void renderKamera( Kam3D* zKamera ) override;
  132. DLLEXPORT void presentFrame() override;
  133. DLLEXPORT Textur* createOrGetTextur( const char* name, Bild* b ) override;
  134. DLLEXPORT Bild* zUIRenderBild() const override;
  135. };
  136. class DirectX11 : public GraphicsApi
  137. {
  138. private:
  139. ID3D11Device* d3d11Device;
  140. ID3D11DeviceContext* d3d11Context;
  141. IDXGISwapChain* d3d11SpawChain;
  142. Textur* uiTextur;
  143. DX11VertexShader* vertexShader;
  144. DX11PixelShader* pixelShader;
  145. ID3D11SamplerState* sampleState;
  146. ID3D11RenderTargetView* rtview;
  147. ID3D11DepthStencilView* dsView;
  148. ID3D11Texture2D* depthStencilBuffer;
  149. ID3D11DepthStencilState* depthStencilState;
  150. ID3D11DepthStencilState* depthDisabledStencilState;
  151. ID3D11BlendState* blendStateAlphaBlend;
  152. D3D11_VIEWPORT* vp;
  153. TexturModel* texturModel;
  154. TexturList* texturRegister;
  155. ID3D11RasterizerState* texturRS;
  156. ID3D11RasterizerState* meshRS;
  157. Textur* defaultTextur;
  158. DX11StructuredBuffer* diffuseLights;
  159. DX11StructuredBuffer* pointLights;
  160. Mat4< float > matrixBuffer[ MAX_KNOCHEN_ANZ ];
  161. Mat4< float > viewAndProj[ 2 ];
  162. Vec3< float > kamPos;
  163. Ebene3D< float > frustrum[ 6 ];
  164. int lastModelId = -1;
  165. void renderObject( Model3D* zObj );
  166. //! Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet werden muss
  167. //! \param pos Der Mittelpunkt der Kugel
  168. //! \param radius Der Radius der Kugel
  169. //! \param dist Einen Zeiger auf einen float, in dem das quadrat des Abstands zur Kammeraposition gespeichert wird, falls diese Funktion true zurückgiebt und der Zeiger nicht 0 ist
  170. bool isInFrustrum( const Vec3< float >& pos, float radius, float* dist = 0 ) const;
  171. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  172. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  173. public:
  174. DLLEXPORT DirectX11();
  175. DLLEXPORT ~DirectX11();
  176. DLLEXPORT void initialize( WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen ) override;
  177. DLLEXPORT void update() override;
  178. DLLEXPORT void beginFrame( bool fill2D, bool fill3D, int fillColor ) override;
  179. DLLEXPORT void renderKamera( Kam3D* zKamera ) override;
  180. DLLEXPORT void renderKamera( Kam3D* zKamera, Textur* zTarget ) override;
  181. DLLEXPORT void presentFrame() override;
  182. DLLEXPORT Textur* createOrGetTextur( const char* name, Bild* b ) override;
  183. DLLEXPORT Bild* zUIRenderBild() const override;
  184. DLLEXPORT static bool isAvailable();
  185. };
  186. class DirectX12 : public GraphicsApi
  187. {
  188. private:
  189. ID3D12Debug* debug;
  190. ID3D12Device* device;
  191. ID3D12InfoQueue* infoQueue;
  192. DX12DirectCommandQueue* directCommandQueue;
  193. DX12CopyCommandQueue* copyCommandQueue;
  194. DX12ComputeCommandQueue* computeCommandQueue;
  195. IDXGISwapChain4* swapChain;
  196. ID3D12DescriptorHeap* rtvHeap;
  197. ID3D12DescriptorHeap* dsvHeap;
  198. ID3D12DescriptorHeap* shaderBufferHeap;
  199. ID3D12Resource* depthBuffer;
  200. ID3D12Resource* backBuffer[ 2 ];
  201. int backBufferIndex;
  202. int tearing;
  203. D3D12_VIEWPORT* viewPort;
  204. tagRECT* allowedRenderArea;
  205. D3D12_VERTEX_BUFFER_VIEW* vertexBufferView;
  206. D3D12_INDEX_BUFFER_VIEW* indexBufferView;
  207. ID3D12RootSignature* signature;
  208. ID3D12PipelineState* pipeline;
  209. Mat4< float > matrixBuffer[ MAX_KNOCHEN_ANZ ];
  210. Mat4< float > viewAndProj[ 2 ];
  211. Vec3< float > kamPos;
  212. Ebene3D< float > frustrum[ 6 ];
  213. TexturModel* texturModel;
  214. Textur* uiTextur;
  215. TexturList* texturRegister;
  216. DX12VertexShader* vertexShader;
  217. DX12PixelShader* pixelShader;
  218. void renderObject( Model3D* zObj );
  219. //! Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und gezeichnet werden muss
  220. //! \param pos Der Mittelpunkt der Kugel
  221. //! \param radius Der Radius der Kugel
  222. //! \param dist Einen Zeiger auf einen float, in dem das quadrat des Abstands zur Kammeraposition gespeichert wird, falls diese Funktion true zurückgiebt und der Zeiger nicht 0 ist
  223. bool isInFrustrum( const Vec3< float >& pos, float radius, float* dist = 0 ) const;
  224. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  225. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  226. public:
  227. DLLEXPORT DirectX12();
  228. DLLEXPORT ~DirectX12();
  229. DLLEXPORT void initialize( WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen ) override;
  230. DLLEXPORT void update() override;
  231. DLLEXPORT void beginFrame( bool fill2D, bool fill3D, int fillColor ) override;
  232. DLLEXPORT void renderKamera( Kam3D* zKamera ) override;
  233. // TODO: DLLEXPORT void renderKamera( Kam3D* zKamera, Textur* zTarget ) override;
  234. DLLEXPORT void presentFrame() override;
  235. DLLEXPORT Textur* createOrGetTextur( const char* name, Bild* b ) override;
  236. DLLEXPORT Bild* zUIRenderBild() const override;
  237. DLLEXPORT static bool isAvailable();
  238. };
  239. }