GraphicsApi.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #pragma once
  2. #include "Mat4.h"
  3. #include "Plane3D.h"
  4. #include "Screen.h"
  5. #include "Vec2.h"
  6. //! DirectX 12 Types
  7. struct ID3D12Debug;
  8. struct ID3D12Device5;
  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 NativeWindow;
  44. class Image;
  45. class Texture;
  46. class DX11PixelShader;
  47. class DX11VertexShader;
  48. class TextureModel;
  49. class Render3D;
  50. class TextureList;
  51. class Cam3D;
  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. NativeWindow* fenster;
  83. Vec2<int> backBufferSize;
  84. Model3DList* modelList;
  85. int nextModelId;
  86. ReadWriteLock rwLock;
  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. NativeWindow* 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(Cam3D* zKamera);
  101. DLLEXPORT virtual void renderKamera(Cam3D* zKamera, Texture* zTarget);
  102. DLLEXPORT virtual void presentFrame() = 0;
  103. DLLEXPORT virtual Texture* createOrGetTexture(
  104. const char* name, Image* b = 0);
  105. DLLEXPORT GraphicApiType getTyp() const;
  106. DLLEXPORT Vec2<int> getBackBufferSize() const;
  107. DLLEXPORT bool isFullScreen() const;
  108. DLLEXPORT virtual Image* zUIRenderImage() 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. Image* uiImage;
  131. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  132. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  133. public:
  134. DLLEXPORT DirectX9();
  135. DLLEXPORT ~DirectX9();
  136. DLLEXPORT void initialize(NativeWindow* 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(Cam3D* zKamera) override;
  143. DLLEXPORT void presentFrame() override;
  144. DLLEXPORT Texture* createOrGetTexture(
  145. const char* name, Image* b) override;
  146. DLLEXPORT Image* zUIRenderImage() const override;
  147. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  148. };
  149. class DirectX11 : public GraphicsApi
  150. {
  151. private:
  152. ID3D11Device* d3d11Device;
  153. Critical deviceLock;
  154. ID3D11DeviceContext* d3d11Context;
  155. IDXGISwapChain* d3d11SpawChain;
  156. Texture* uiTexture;
  157. DX11VertexShader* vertexShader;
  158. DX11PixelShader* pixelShader;
  159. ID3D11SamplerState* sampleState;
  160. ID3D11RenderTargetView* rtview;
  161. ID3D11DepthStencilView* dsView;
  162. ID3D11Texture2D* depthStencilBuffer;
  163. ID3D11DepthStencilState* depthStencilState;
  164. ID3D11DepthStencilState* depthDisabledStencilState;
  165. ID3D11BlendState* blendStateAlphaBlend;
  166. D3D11_VIEWPORT* vp;
  167. TextureModel* texturModel;
  168. TextureList* texturRegister;
  169. Texture* defaultTexture;
  170. DX11StructuredBuffer* diffuseLights;
  171. DX11StructuredBuffer* pointLights;
  172. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  173. Mat4<float> viewAndProj[2];
  174. Vec3<float> kamPos;
  175. Plane3D<float> frustrum[6];
  176. int lastModelId = -1;
  177. void renderObject(Model3D* zObj);
  178. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  179. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  180. protected:
  181. ID3D11RasterizerState* texturRS;
  182. ID3D11RasterizerState* meshRS;
  183. DLLEXPORT virtual DX11VertexShader* initializeVertexShader(
  184. unsigned char* byteCode, int size);
  185. DLLEXPORT virtual DX11PixelShader* initializePixelShader(
  186. unsigned char* byteCode, int size);
  187. DLLEXPORT ID3D11DeviceContext* zContext();
  188. public:
  189. DLLEXPORT DirectX11();
  190. DLLEXPORT ~DirectX11();
  191. DLLEXPORT void initialize(NativeWindow* 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(Cam3D* zKamera) override;
  198. DLLEXPORT void renderKamera(Cam3D* zKamera, Texture* zTarget) override;
  199. DLLEXPORT void presentFrame() override;
  200. DLLEXPORT Texture* createOrGetTexture(
  201. const char* name, Image* b) override;
  202. DLLEXPORT Image* zUIRenderImage() const override;
  203. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  204. //! Checks whether a sphere is in the visible space of the world and
  205. //! needs to be drawn \param pos The center of the sphere \param
  206. //! radius The radius of the sphere \param dist A pointer to a
  207. //! float where the square of the distance to the camera position
  208. //! is stored if this function returns true and the pointer is not 0
  209. DLLEXPORT bool isInFrustrum(
  210. const Vec3<float>& pos, float radius, float* dist = 0) const;
  211. DLLEXPORT bool isInFrustrum(
  212. const Vec3<float>& pos, Vec3<float> radius, float* dist = 0) const;
  213. DLLEXPORT static bool isAvailable();
  214. };
  215. class DirectX12 : public GraphicsApi
  216. {
  217. private:
  218. ID3D12Debug* debug;
  219. ID3D12Device5* device;
  220. ID3D12InfoQueue* infoQueue;
  221. DX12DirectCommandQueue* directCommandQueue;
  222. DX12CopyCommandQueue* copyCommandQueue;
  223. DX12ComputeCommandQueue* computeCommandQueue;
  224. IDXGISwapChain4* swapChain;
  225. ID3D12DescriptorHeap* rtvHeap;
  226. ID3D12DescriptorHeap* dsvHeap;
  227. ID3D12DescriptorHeap* shaderBufferHeap;
  228. ID3D12Resource* depthBuffer;
  229. ID3D12Resource* backBuffer[2];
  230. int backBufferIndex;
  231. int tearing;
  232. D3D12_VIEWPORT* viewPort;
  233. tagRECT* allowedRenderArea;
  234. D3D12_VERTEX_BUFFER_VIEW* vertexBufferView;
  235. D3D12_INDEX_BUFFER_VIEW* indexBufferView;
  236. ID3D12RootSignature* signature;
  237. ID3D12PipelineState* pipeline;
  238. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  239. Mat4<float> viewAndProj[2];
  240. Vec3<float> kamPos;
  241. Plane3D<float> frustrum[6];
  242. TextureModel* texturModel;
  243. Texture* uiTexture;
  244. TextureList* texturRegister;
  245. DX12VertexShader* vertexShader;
  246. DX12PixelShader* pixelShader;
  247. void renderObject(Model3D* zObj);
  248. DLLEXPORT virtual DXBuffer* createIndexBuffer() override;
  249. DLLEXPORT virtual DXBuffer* createVertexBuffer() override;
  250. public:
  251. DLLEXPORT DirectX12();
  252. DLLEXPORT ~DirectX12();
  253. DLLEXPORT void initialize(NativeWindow* fenster,
  254. Vec2<int> backBufferSize,
  255. bool fullScreen) override;
  256. DLLEXPORT void update() override;
  257. DLLEXPORT void beginFrame(
  258. bool fill2D, bool fill3D, int fillColor) override;
  259. DLLEXPORT void renderKamera(Cam3D* zKamera) override;
  260. //! TODO: DLLEXPORT void renderKamera( Cam3D* zKamera, Texture* zTarget
  261. //! ) override;
  262. DLLEXPORT void presentFrame() override;
  263. DLLEXPORT Texture* createOrGetTexture(
  264. const char* name, Image* b) override;
  265. DLLEXPORT Image* zUIRenderImage() const override;
  266. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  267. //! Checks whether a sphere is in the visible space of the world and
  268. //! needs to be drawn \param pos The center of the sphere \param
  269. //! radius The radius of the sphere \param dist A pointer to a
  270. //! float where the square of the distance to the camera position
  271. //! is stored if this function returns true and the pointer is not 0
  272. DLLEXPORT bool isInFrustrum(
  273. const Vec3<float>& pos, float radius, float* dist = 0) const;
  274. DLLEXPORT static bool isAvailable();
  275. };
  276. } // namespace Framework