GraphicsApi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. //! creates a StructuredBuffer that is stored in the GPU memory and can
  119. //! be used as a shader resource
  120. //! \param eSize the size of one element of the buffer in bytes
  121. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) = 0;
  122. };
  123. class DirectX9 : public GraphicsApi
  124. {
  125. private:
  126. IDirect3D9* pDirect3D;
  127. IDirect3DDevice9* pDevice;
  128. IDirect3DSurface9* pBackBuffer;
  129. _D3DLOCKED_RECT* backRect;
  130. Bild* uiBild;
  131. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  132. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  133. public:
  134. DLLEXPORT DirectX9();
  135. DLLEXPORT ~DirectX9();
  136. DLLEXPORT void initialize(WFenster* fenster,
  137. Vec2<int> backBufferSize,
  138. bool fullScreen) override;
  139. DLLEXPORT void update() override;
  140. DLLEXPORT void beginFrame(
  141. bool fill2D, bool fill3D, int fillColor) override;
  142. DLLEXPORT void renderKamera(Kam3D* zKamera) override;
  143. DLLEXPORT void presentFrame() override;
  144. DLLEXPORT Textur* createOrGetTextur(const char* name, Bild* b) override;
  145. DLLEXPORT Bild* zUIRenderBild() const override;
  146. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  147. };
  148. class DirectX11 : public GraphicsApi
  149. {
  150. private:
  151. ID3D11Device* d3d11Device;
  152. ID3D11DeviceContext* d3d11Context;
  153. IDXGISwapChain* d3d11SpawChain;
  154. Textur* uiTextur;
  155. DX11VertexShader* vertexShader;
  156. DX11PixelShader* pixelShader;
  157. ID3D11SamplerState* sampleState;
  158. ID3D11RenderTargetView* rtview;
  159. ID3D11DepthStencilView* dsView;
  160. ID3D11Texture2D* depthStencilBuffer;
  161. ID3D11DepthStencilState* depthStencilState;
  162. ID3D11DepthStencilState* depthDisabledStencilState;
  163. ID3D11BlendState* blendStateAlphaBlend;
  164. D3D11_VIEWPORT* vp;
  165. TexturModel* texturModel;
  166. TexturList* texturRegister;
  167. Textur* defaultTextur;
  168. DX11StructuredBuffer* diffuseLights;
  169. DX11StructuredBuffer* pointLights;
  170. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  171. Mat4<float> viewAndProj[2];
  172. Vec3<float> kamPos;
  173. Ebene3D<float> frustrum[6];
  174. int lastModelId = -1;
  175. void renderObject(Model3D* zObj);
  176. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  177. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  178. protected:
  179. ID3D11RasterizerState* texturRS;
  180. ID3D11RasterizerState* meshRS;
  181. DLLEXPORT virtual DX11VertexShader* initializeVertexShader(
  182. unsigned char* byteCode, int size);
  183. DLLEXPORT virtual DX11PixelShader* initializePixelShader(
  184. unsigned char* byteCode, int size);
  185. DLLEXPORT ID3D11DeviceContext* zContext();
  186. public:
  187. DLLEXPORT DirectX11();
  188. DLLEXPORT ~DirectX11();
  189. DLLEXPORT void initialize(WFenster* fenster,
  190. Vec2<int> backBufferSize,
  191. bool fullScreen) override;
  192. DLLEXPORT void update() override;
  193. DLLEXPORT void beginFrame(
  194. bool fill2D, bool fill3D, int fillColor) override;
  195. DLLEXPORT void renderKamera(Kam3D* zKamera) override;
  196. DLLEXPORT void renderKamera(Kam3D* zKamera, Textur* zTarget) override;
  197. DLLEXPORT void presentFrame() override;
  198. DLLEXPORT Textur* createOrGetTextur(const char* name, Bild* b) override;
  199. DLLEXPORT Bild* zUIRenderBild() const override;
  200. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  201. //! Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und
  202. //! gezeichnet werden muss \param pos Der Mittelpunkt der Kugel \param
  203. //! radius Der Radius der Kugel \param dist Einen Zeiger auf einen
  204. //! float, in dem das quadrat des Abstands zur Kammeraposition
  205. //! gespeichert wird, falls diese Funktion true zurückgiebt und der
  206. //! Zeiger nicht 0 ist
  207. DLLEXPORT bool isInFrustrum(
  208. const Vec3<float>& pos, float radius, float* dist = 0) const;
  209. DLLEXPORT bool isInFrustrum(
  210. const Vec3<float>& pos, Vec3<float> radius, float* dist = 0) const;
  211. DLLEXPORT static bool isAvailable();
  212. };
  213. class DirectX12 : public GraphicsApi
  214. {
  215. private:
  216. ID3D12Debug* debug;
  217. ID3D12Device* device;
  218. ID3D12InfoQueue* infoQueue;
  219. DX12DirectCommandQueue* directCommandQueue;
  220. DX12CopyCommandQueue* copyCommandQueue;
  221. DX12ComputeCommandQueue* computeCommandQueue;
  222. IDXGISwapChain4* swapChain;
  223. ID3D12DescriptorHeap* rtvHeap;
  224. ID3D12DescriptorHeap* dsvHeap;
  225. ID3D12DescriptorHeap* shaderBufferHeap;
  226. ID3D12Resource* depthBuffer;
  227. ID3D12Resource* backBuffer[2];
  228. int backBufferIndex;
  229. int tearing;
  230. D3D12_VIEWPORT* viewPort;
  231. tagRECT* allowedRenderArea;
  232. D3D12_VERTEX_BUFFER_VIEW* vertexBufferView;
  233. D3D12_INDEX_BUFFER_VIEW* indexBufferView;
  234. ID3D12RootSignature* signature;
  235. ID3D12PipelineState* pipeline;
  236. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  237. Mat4<float> viewAndProj[2];
  238. Vec3<float> kamPos;
  239. Ebene3D<float> frustrum[6];
  240. TexturModel* texturModel;
  241. Textur* uiTextur;
  242. TexturList* texturRegister;
  243. DX12VertexShader* vertexShader;
  244. DX12PixelShader* pixelShader;
  245. void renderObject(Model3D* zObj);
  246. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  247. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  248. public:
  249. DLLEXPORT DirectX12();
  250. DLLEXPORT ~DirectX12();
  251. DLLEXPORT void initialize(WFenster* fenster,
  252. Vec2<int> backBufferSize,
  253. bool fullScreen) override;
  254. DLLEXPORT void update() override;
  255. DLLEXPORT void beginFrame(
  256. bool fill2D, bool fill3D, int fillColor) override;
  257. DLLEXPORT void renderKamera(Kam3D* zKamera) override;
  258. //! TODO: DLLEXPORT void renderKamera( Kam3D* zKamera, Textur* zTarget )
  259. //! override;
  260. DLLEXPORT void presentFrame() override;
  261. DLLEXPORT Textur* createOrGetTextur(const char* name, Bild* b) override;
  262. DLLEXPORT Bild* zUIRenderBild() const override;
  263. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  264. //! Überprüft, ob eine Kugel in dem Sichtbaren Raum der Welt liegt und
  265. //! gezeichnet werden muss \param pos Der Mittelpunkt der Kugel \param
  266. //! radius Der Radius der Kugel \param dist Einen Zeiger auf einen
  267. //! float, in dem das quadrat des Abstands zur Kammeraposition
  268. //! gespeichert wird, falls diese Funktion true zurückgiebt und der
  269. //! Zeiger nicht 0 ist
  270. DLLEXPORT bool isInFrustrum(
  271. const Vec3<float>& pos, float radius, float* dist = 0) const;
  272. DLLEXPORT static bool isAvailable();
  273. };
  274. } // namespace Framework