GraphicsApi.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. class ID3D12GraphicsCommandList4;
  67. enum GraphicApiType;
  68. struct DiffuseLight
  69. {
  70. Vec3<float> direction;
  71. Vec3<float> color;
  72. };
  73. struct PointLight
  74. {
  75. Vec3<float> position;
  76. Vec3<float> color;
  77. float radius;
  78. };
  79. class GraphicsApi : public virtual ReferenceCounter
  80. {
  81. protected:
  82. GraphicApiType typ;
  83. NativeWindow* fenster;
  84. Vec2<int> backBufferSize;
  85. Model3DList* modelList;
  86. int nextModelId;
  87. ReadWriteLock rwLock;
  88. bool fullScreen;
  89. public:
  90. DLLEXPORT GraphicsApi(GraphicApiType typ);
  91. DLLEXPORT virtual ~GraphicsApi();
  92. DLLEXPORT virtual void initialize(
  93. NativeWindow* fenster, Vec2<int> backBufferSize, bool fullScreen);
  94. DLLEXPORT virtual void beginFrame(
  95. bool fill2D = 0, bool fill3D = 0, int fillColor = 0);
  96. DLLEXPORT virtual void renderKamera(Cam3D* zKamera);
  97. DLLEXPORT virtual void renderKamera(Cam3D* zKamera, Texture* zTarget);
  98. DLLEXPORT virtual void presentFrame() = 0;
  99. DLLEXPORT virtual Texture* createOrGetTexture(
  100. const char* name, Image* b = 0);
  101. DLLEXPORT GraphicApiType getTyp() const;
  102. DLLEXPORT Vec2<int> getBackBufferSize() const;
  103. DLLEXPORT bool isFullScreen() const;
  104. DLLEXPORT virtual Image* zUIRenderImage() const = 0;
  105. //! returns the specified model without increased reference counter
  106. DLLEXPORT virtual Model3DData* zModel(const char* name);
  107. //! returns the specified model with increased reference counter
  108. DLLEXPORT virtual Model3DData* getModel(const char* name);
  109. //! creates a new empty Model3DData object if the model does not exist
  110. //! yet
  111. DLLEXPORT virtual Model3DData* createModel(const char* name);
  112. //! check if a model exists
  113. DLLEXPORT virtual bool hasModel(const char* name);
  114. //! creates a StructuredBuffer that is stored in the GPU memory and can
  115. //! be used as a shader resource
  116. //! \param eSize the size of one element of the buffer in bytes
  117. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) = 0;
  118. };
  119. class DirectX9 : public GraphicsApi
  120. {
  121. private:
  122. IDirect3D9* pDirect3D;
  123. IDirect3DDevice9* pDevice;
  124. IDirect3DSurface9* pBackBuffer;
  125. _D3DLOCKED_RECT* backRect;
  126. Image* uiImage;
  127. public:
  128. DLLEXPORT DirectX9();
  129. DLLEXPORT ~DirectX9();
  130. DLLEXPORT void initialize(NativeWindow* fenster,
  131. Vec2<int> backBufferSize,
  132. bool fullScreen) override;
  133. DLLEXPORT void beginFrame(
  134. bool fill2D, bool fill3D, int fillColor) override;
  135. DLLEXPORT void renderKamera(Cam3D* zKamera) override;
  136. DLLEXPORT void presentFrame() override;
  137. DLLEXPORT Texture* createOrGetTexture(
  138. const char* name, Image* b) override;
  139. DLLEXPORT Image* zUIRenderImage() const override;
  140. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  141. };
  142. class DirectX11 : public GraphicsApi
  143. {
  144. private:
  145. ID3D11Device* d3d11Device;
  146. ID3D11DeviceContext* d3d11Context;
  147. IDXGISwapChain* d3d11SpawChain;
  148. Texture* uiTexture;
  149. DX11VertexShader* vertexShader;
  150. DX11PixelShader* pixelShader;
  151. ID3D11SamplerState* sampleState;
  152. ID3D11RenderTargetView* rtview;
  153. ID3D11DepthStencilView* dsView;
  154. ID3D11Texture2D* depthStencilBuffer;
  155. ID3D11DepthStencilState* depthStencilState;
  156. ID3D11DepthStencilState* depthDisabledStencilState;
  157. ID3D11BlendState* blendStateAlphaBlend;
  158. D3D11_VIEWPORT* vp;
  159. TextureModel* texturModel;
  160. TextureList* texturRegister;
  161. Texture* defaultTexture;
  162. DX11StructuredBuffer* diffuseLights;
  163. DX11StructuredBuffer* pointLights;
  164. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  165. Mat4<float> viewAndProj[2];
  166. Vec3<float> kamPos;
  167. Plane3D<float> frustrum[6];
  168. int lastModelId = -1;
  169. DX11Buffer** indexBuffers;
  170. DX11Buffer** vertexBuffers;
  171. void renderObject(Model3D* zObj);
  172. DX11Buffer* createIndexBuffer();
  173. DX11Buffer* createVertexBuffer();
  174. protected:
  175. Critical deviceLock;
  176. ID3D11RasterizerState* texturRS;
  177. ID3D11RasterizerState* meshRS;
  178. DLLEXPORT virtual DX11VertexShader* initializeVertexShader(
  179. unsigned char* byteCode, int size);
  180. DLLEXPORT virtual DX11PixelShader* initializePixelShader(
  181. unsigned char* byteCode, int size);
  182. DLLEXPORT ID3D11DeviceContext* zContext();
  183. public:
  184. DLLEXPORT DirectX11();
  185. DLLEXPORT ~DirectX11();
  186. DLLEXPORT void initialize(NativeWindow* fenster,
  187. Vec2<int> backBufferSize,
  188. bool fullScreen) override;
  189. DLLEXPORT void beginFrame(
  190. bool fill2D, bool fill3D, int fillColor) override;
  191. DLLEXPORT void renderKamera(Cam3D* zKamera) override;
  192. DLLEXPORT void renderKamera(Cam3D* zKamera, Texture* zTarget) override;
  193. DLLEXPORT void presentFrame() override;
  194. DLLEXPORT Texture* createOrGetTexture(
  195. const char* name, Image* b) override;
  196. DLLEXPORT Image* zUIRenderImage() const override;
  197. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  198. DLLEXPORT virtual Model3DData* createModel(const char* name) override;
  199. //! Checks whether a sphere is in the visible space of the world and
  200. //! needs to be drawn \param pos The center of the sphere \param
  201. //! radius The radius of the sphere \param dist A pointer to a
  202. //! float where the square of the distance to the camera position
  203. //! is stored if this function returns true and the pointer is not 0
  204. DLLEXPORT bool isInFrustrum(
  205. const Vec3<float>& pos, float radius, float* dist = 0) const;
  206. DLLEXPORT bool isInFrustrum(
  207. const Vec3<float>& pos, Vec3<float> radius, float* dist = 0) const;
  208. DLLEXPORT static bool isAvailable();
  209. };
  210. class DirectX12 : public GraphicsApi
  211. {
  212. private:
  213. ID3D12Debug* debug;
  214. ID3D12Device5* device;
  215. ID3D12InfoQueue* infoQueue;
  216. DX12DirectCommandQueue* directCommandQueue;
  217. DX12CopyCommandQueue* copyCommandQueue;
  218. DX12ComputeCommandQueue* computeCommandQueue;
  219. IDXGISwapChain4* swapChain;
  220. ID3D12DescriptorHeap* rtvHeap;
  221. ID3D12DescriptorHeap* dsvHeap;
  222. ID3D12DescriptorHeap* shaderBufferHeap;
  223. ID3D12Resource* depthBuffer;
  224. ID3D12Resource* backBuffer[2];
  225. int backBufferIndex;
  226. int tearing;
  227. D3D12_VIEWPORT* viewPort;
  228. tagRECT* allowedRenderArea;
  229. D3D12_VERTEX_BUFFER_VIEW* vertexBufferView;
  230. D3D12_INDEX_BUFFER_VIEW* indexBufferView;
  231. ID3D12RootSignature* signature;
  232. ID3D12PipelineState* pipeline;
  233. Mat4<float> matrixBuffer[MAX_KNOCHEN_ANZ];
  234. Mat4<float> viewAndProj[2];
  235. Vec3<float> kamPos;
  236. Plane3D<float> frustrum[6];
  237. TextureModel* texturModel;
  238. Texture* uiTexture;
  239. TextureList* texturRegister;
  240. DX12VertexShader* vertexShader;
  241. DX12PixelShader* pixelShader;
  242. DLLEXPORT void updateBottomLevelAccelerationStructure();
  243. public:
  244. DLLEXPORT DirectX12();
  245. DLLEXPORT ~DirectX12();
  246. DLLEXPORT void initialize(NativeWindow* fenster,
  247. Vec2<int> backBufferSize,
  248. bool fullScreen) override;
  249. DLLEXPORT void beginFrame(
  250. bool fill2D, bool fill3D, int fillColor) override;
  251. DLLEXPORT void renderKamera(Cam3D* zKamera) override;
  252. DLLEXPORT virtual void renderKamera(
  253. Cam3D* zKamera, Texture* zTarget) override;
  254. //! TODO: DLLEXPORT void renderKamera( Cam3D* zKamera, Texture* zTarget
  255. //! ) override;
  256. DLLEXPORT void presentFrame() override;
  257. DLLEXPORT Texture* createOrGetTexture(
  258. const char* name, Image* b) override;
  259. DLLEXPORT Image* zUIRenderImage() const override;
  260. DLLEXPORT virtual DXBuffer* createStructuredBuffer(int eSize) override;
  261. DLLEXPORT static bool isAvailable();
  262. };
  263. } // namespace Framework