GraphicsApi.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #pragma once
  2. #include "Bildschirm.h"
  3. #include "Ebene3D.h"
  4. #include "Mat4.h"
  5. #include "Vec2.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(
  94. WFenster* fenster, Vec2<int> backBufferSize, bool fullScreen);
  95. DLLEXPORT virtual void setBackBufferSize(Vec2<int> size);
  96. DLLEXPORT virtual void setFullScreen(bool fullScreen);
  97. DLLEXPORT virtual void update() = 0;
  98. DLLEXPORT virtual void beginFrame(
  99. bool fill2D = 0, bool fill3D = 0, int fillColor = 0);
  100. DLLEXPORT virtual void renderKamera(Kam3D* zKamera);
  101. DLLEXPORT virtual void renderKamera(Kam3D* zKamera, Textur* zTarget);
  102. DLLEXPORT virtual void presentFrame() = 0;
  103. DLLEXPORT virtual Textur* createOrGetTextur(
  104. const char* name, Bild* b = 0);
  105. DLLEXPORT GraphicApiType getTyp() const;
  106. DLLEXPORT Vec2<int> getBackBufferSize() const;
  107. DLLEXPORT bool isFullScreen() const;
  108. DLLEXPORT virtual Bild* zUIRenderBild() const = 0;
  109. // returns the specified model without increased reference counter
  110. DLLEXPORT virtual Model3DData* zModel(const char* name);
  111. // returns the specified model with increased reference counter
  112. DLLEXPORT virtual Model3DData* getModel(const char* name);
  113. // creates a new empty Model3DData object if the model does not exist
  114. // yet
  115. DLLEXPORT virtual Model3DData* createModel(const char* name);
  116. // check if a model exists
  117. DLLEXPORT virtual bool hasModel(const char* name);
  118. };
  119. class DirectX9 : public GraphicsApi
  120. {
  121. private:
  122. IDirect3D9* pDirect3D;
  123. IDirect3DDevice9* pDevice;
  124. IDirect3DSurface9* pBackBuffer;
  125. _D3DLOCKED_RECT* backRect;
  126. Bild* uiBild;
  127. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  128. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  129. public:
  130. DLLEXPORT DirectX9();
  131. DLLEXPORT ~DirectX9();
  132. DLLEXPORT void initialize(WFenster* fenster,
  133. Vec2<int> backBufferSize,
  134. bool fullScreen) override;
  135. DLLEXPORT void update() override;
  136. DLLEXPORT void beginFrame(
  137. bool fill2D, bool fill3D, int fillColor) override;
  138. DLLEXPORT void renderKamera(Kam3D* zKamera) override;
  139. DLLEXPORT void presentFrame() override;
  140. DLLEXPORT Textur* createOrGetTextur(const char* name, Bild* b) override;
  141. DLLEXPORT Bild* zUIRenderBild() const override;
  142. };
  143. class DirectX11 : public GraphicsApi
  144. {
  145. private:
  146. ID3D11Device* d3d11Device;
  147. ID3D11DeviceContext* d3d11Context;
  148. IDXGISwapChain* d3d11SpawChain;
  149. Textur* uiTextur;
  150. DX11VertexShader* vertexShader;
  151. DX11PixelShader* pixelShader;
  152. ID3D11SamplerState* sampleState;
  153. ID3D11RenderTargetView* rtview;
  154. ID3D11DepthStencilView* dsView;
  155. ID3D11Texture2D* depthStencilBuffer;
  156. ID3D11DepthStencilState* depthStencilState;
  157. ID3D11DepthStencilState* depthDisabledStencilState;
  158. ID3D11BlendState* blendStateAlphaBlend;
  159. D3D11_VIEWPORT* vp;
  160. TexturModel* texturModel;
  161. TexturList* texturRegister;
  162. ID3D11RasterizerState* texturRS;
  163. ID3D11RasterizerState* meshRS;
  164. Textur* defaultTextur;
  165. DX11StructuredBuffer* diffuseLights;
  166. DX11StructuredBuffer* pointLights;
  167. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  168. Mat4<float> viewAndProj[2];
  169. Vec3<float> kamPos;
  170. Ebene3D<float> frustrum[6];
  171. int lastModelId = -1;
  172. void renderObject(Model3D* zObj);
  173. //! Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und
  174. //! gezeichnet werden muss \param pos Der Mittelpunkt der Kugel \param
  175. //! radius Der Radius der Kugel \param dist Einen Zeiger auf einen
  176. //! float, in dem das quadrat des Abstands zur Kammeraposition
  177. //! gespeichert wird, falls diese Funktion true zurückgiebt und der
  178. //! Zeiger nicht 0 ist
  179. bool isInFrustrum(
  180. const Vec3<float>& pos, float radius, float* dist = 0) const;
  181. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  182. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  183. protected:
  184. DLLEXPORT virtual DX11VertexShader* initializeVertexShader(
  185. unsigned char* byteCode, int size);
  186. DLLEXPORT virtual DX11PixelShader* initializePixelShader(
  187. unsigned char* byteCode, int size);
  188. public:
  189. DLLEXPORT DirectX11();
  190. DLLEXPORT ~DirectX11();
  191. DLLEXPORT void initialize(WFenster* fenster,
  192. Vec2<int> backBufferSize,
  193. bool fullScreen) override;
  194. DLLEXPORT void update() override;
  195. DLLEXPORT void beginFrame(
  196. bool fill2D, bool fill3D, int fillColor) override;
  197. DLLEXPORT void renderKamera(Kam3D* zKamera) override;
  198. DLLEXPORT void renderKamera(Kam3D* zKamera, Textur* zTarget) override;
  199. DLLEXPORT void presentFrame() override;
  200. DLLEXPORT Textur* createOrGetTextur(const char* name, Bild* b) override;
  201. DLLEXPORT Bild* zUIRenderBild() const override;
  202. DLLEXPORT static bool isAvailable();
  203. };
  204. class DirectX12 : public GraphicsApi
  205. {
  206. private:
  207. ID3D12Debug* debug;
  208. ID3D12Device* device;
  209. ID3D12InfoQueue* infoQueue;
  210. DX12DirectCommandQueue* directCommandQueue;
  211. DX12CopyCommandQueue* copyCommandQueue;
  212. DX12ComputeCommandQueue* computeCommandQueue;
  213. IDXGISwapChain4* swapChain;
  214. ID3D12DescriptorHeap* rtvHeap;
  215. ID3D12DescriptorHeap* dsvHeap;
  216. ID3D12DescriptorHeap* shaderBufferHeap;
  217. ID3D12Resource* depthBuffer;
  218. ID3D12Resource* backBuffer[2];
  219. int backBufferIndex;
  220. int tearing;
  221. D3D12_VIEWPORT* viewPort;
  222. tagRECT* allowedRenderArea;
  223. D3D12_VERTEX_BUFFER_VIEW* vertexBufferView;
  224. D3D12_INDEX_BUFFER_VIEW* indexBufferView;
  225. ID3D12RootSignature* signature;
  226. ID3D12PipelineState* pipeline;
  227. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  228. Mat4<float> viewAndProj[2];
  229. Vec3<float> kamPos;
  230. Ebene3D<float> frustrum[6];
  231. TexturModel* texturModel;
  232. Textur* uiTextur;
  233. TexturList* texturRegister;
  234. DX12VertexShader* vertexShader;
  235. DX12PixelShader* pixelShader;
  236. void renderObject(Model3D* zObj);
  237. //! Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und
  238. //! gezeichnet werden muss \param pos Der Mittelpunkt der Kugel \param
  239. //! radius Der Radius der Kugel \param dist Einen Zeiger auf einen
  240. //! float, in dem das quadrat des Abstands zur Kammeraposition
  241. //! gespeichert wird, falls diese Funktion true zurückgiebt und der
  242. //! Zeiger nicht 0 ist
  243. bool isInFrustrum(
  244. const Vec3<float>& pos, float radius, float* dist = 0) const;
  245. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  246. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  247. public:
  248. DLLEXPORT DirectX12();
  249. DLLEXPORT ~DirectX12();
  250. DLLEXPORT void initialize(WFenster* fenster,
  251. Vec2<int> backBufferSize,
  252. bool fullScreen) override;
  253. DLLEXPORT void update() override;
  254. DLLEXPORT void beginFrame(
  255. bool fill2D, bool fill3D, int fillColor) override;
  256. DLLEXPORT void renderKamera(Kam3D* zKamera) override;
  257. // TODO: DLLEXPORT void renderKamera( Kam3D* zKamera, Textur* zTarget )
  258. // override;
  259. DLLEXPORT void presentFrame() override;
  260. DLLEXPORT Textur* createOrGetTextur(const char* name, Bild* b) override;
  261. DLLEXPORT Bild* zUIRenderBild() const override;
  262. DLLEXPORT static bool isAvailable();
  263. };
  264. } // namespace Framework