altEngine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
graphics.h
Go to the documentation of this file.
1 #include "include.h"
2 
3 #ifndef GRAPHICS_H
4 #define GRAPHICS_H
5 
6 //#define ERROR_CHECK
7 
8 
9 #define FRONTFACE 0x0404
10 #define BACKFACE 0x0405
11 #define NONE 2
12 
13 #define LESS 0x0201
14 #define EQUAL 0x0202
15 #define LEQUAL 0x0203
16 #define GREATER 0x0204
17 #define NOTEQUAL 0x0205
18 #define GEQUAL 0x0206
19 #define ALWAYS 0x0207
20 
21 #define NEVER 0x0200
22 
23 #define KEEP 0x1E00
24 #define REPLACE 0x1E01
25 #define INCR 0x1E02
26 #define DECR 0x1E03
27 #define INCR_WRAP 0x8507
28 #define DECR_WRAP 0x8508
29 
30 
31 /*
32  You could make this a pure virtual class and switch between OpenGL and Direct3D at runtime...
33  But then you would need to link to both d3d9 dll's and opengl32 dll's at the same time
34  (not to mention the whole vtable lookup ontop of a wrapper function)
35  I think a d3d binary and an opengl binary is the better approach
36  (Although the idea of making a no operation graphics stub is nice for headless server)
37 */
38 class Graphics
39 {
40 public:
41  Graphics();
42  void init(void *param1, void *param2);
43  bool error_check();
44  void clear();
45  void clear_color(vec3 &color);
46  void clear_multi();
47  void cleardepth();
48  void clearstencil();
49  void Depth(bool flag);
50  void Blend(bool flag);
51  void BlendFuncDstColorOne();
52  void BlendFuncDstColorZero();
53  void BlendFuncOneOne();
54  void BlendFuncZeroOne();
55  void BlendFuncOneZero();
62  void BlendFuncOneAlpha();
63  void BlendFuncZeroSrcColor();
65  void BlendFuncZeroSrcAlpha();
67  void BlendFuncOneSrcAlpha();
68  void CullFace(int mode);
69  void Color(bool flag);
70  void DepthFunc(int op);
71  void Stencil(bool flag);
72  void StencilFunc(int op, int ref, unsigned int mask);
73  void StencilOp(int stencil_fail, int zfail, int zpass);
74  void TwoSidedStencilOp(int face, int stencil_fail, int zfail, int zpass);
75  void DrawText(const char *str, float x, float y);
76 
77  void swap();
78  void resize(const int width, const int height);
79  void destroy();
80 
81  void DrawArray(primitive_t primitive, int start_index, int start_vertex, unsigned int num_index, int num_verts);
82  void DrawArrayTri(int start_index, int start_vertex, unsigned int num_index, int num_verts);
83  void DrawArrayTriStrip(int start_index, int start_vertex, unsigned int num_index, int num_verts);
84  void DrawArrayLineStrip(int start_index, int start_vertex, unsigned int num_index, int num_verts);
85  void DrawArrayLine(int start_index, int start_vertex, unsigned int num_index, int num_verts);
86  void DrawArrayPoint(int start_index, int start_vertex, unsigned int num_index, int num_verts);
87 
88  //Need to fix dx9 support cause of these guys
89  void CreateVertexArrayObject(unsigned int &vao);
90  void SelectVertexArrayObject(unsigned int vao);
91  void DeleteVertexArrayObject(unsigned int vao);
92 
93  int CreateIndexBuffer(void *index_buffer, int num_index);
94  void SelectIndexBuffer(int handle);
95  void DeleteIndexBuffer(int handle);
96 
97  int CreateVertexBuffer(void *vertex_buffer, int num_vertex, bool dynamic = false);
98 
99  void SelectVertexBuffer(int handle);
100  void DeleteVertexBuffer(int handle);
101 
102  void SelectTexture(int level, int index);
103  void SelectTextureArray(int level, int texObject);
104  void DeselectTexture(int level);
105  int CreateCubeMap();
106  void SelectCubemap(int texObject);
107  int LoadTexture(int width, int height, int components, int format, void *bytes, bool clamp, int anisotropic);
108  int LoadTextureArray(int width, int height, int components, int format, void **bytes, bool clamp, int num_layer);
109  void DeleteTexture(int handle);
110 
111  void SelectShader(int program);
112 
113  int checkFramebuffer();
114  int CreateFramebuffer(int width, int height, unsigned int &fbo, unsigned int &quad_tex, unsigned int &depth_tex, unsigned int &normal_depth, int multisample, bool twoattach);
115  void DeleteFrameBuffer(int fbo, int quad, int depth);
116  void fbAttachTexture(int texObj);
117  void fbAttachTextureOne(int texObj);
118  void fbAttachDepth(int texObj);
119  void bindFramebuffer(int fbo, int num_attach = 1);
120  void GetDebugLog();
121 
122 
123 
124  ~Graphics();
125 
126 #ifdef VULKAN
127 private:
128  VkInstance CreateInstance();
129  void CreateDeviceAndQueue(VkInstance instance, VkDevice* outputDevice, VkQueue* outputQueue, int* outputQueueIndex, VkPhysicalDevice* outputPhysicalDevice);
130  void FindPhysicalDeviceWithGraphicsQueue(const vector<VkPhysicalDevice>& physicalDevices, VkPhysicalDevice* outputDevice, int* outputGraphicsQueueIndex);
131  void CreateSwapchainImageViews(VkDevice device, VkFormat format, const int count, const VkImage* images, VkImageView* imageViews);
132  void CreateFramebuffers(VkDevice device, VkRenderPass renderPass, const int width, const int height, const int count, const VkImageView* imageViews, VkFramebuffer* framebuffers);
133  void CreateSwapchain(VkPhysicalDevice physicalDevice, VkDevice device, VkSurfaceKHR surface, const int surfaceWidth, const int surfaceHeight, const int backbufferCount, VkFormat* swapchainFormat, VkSwapchainKHR &swapchain);
134  void GetSwapchainFormatAndColorspace(VkPhysicalDevice physicalDevice, VkSurfaceKHR surface, SwapchainFormatColorSpace &result);
135 
136  void AllocateBuffer(VkDevice device, const int size, const VkBufferUsageFlagBits bits, VkBuffer &buffer);
137  int AllocateMemory(VkDeviceMemory &deviceMemory, const vector<MemoryTypeInfo>& memoryInfos, VkDevice device, const int size, const uint32_t memoryBits, unsigned int memoryProperties, bool* isHostCoherent = nullptr);
138 
139  void CreateSurface(VkInstance instance, HWND hwnd, VkSurfaceKHR &surface);
140  void CreateTexture(int width, int height, int components, int format, unsigned char *image_data, bool clamp);
141  void CreateSampler();
142  void CreateDescriptors();
143  void LoadShader(VkDevice device, const void* shaderContents, const size_t size, VkShaderModule &shader);
144 
145  void render_cmdbuffer(VkCommandBuffer commandBuffer, int width, int height);
146 
147  void CreatePipeline(VkDevice device, VkRenderPass renderPass, VkPipelineLayout layout, VkShaderModule vertexShader, VkShaderModule fragmentShader, VkPipeline &pipeline);
148  void CreatePipelineStateObject();
149  void CreateMeshBuffers(VkCommandBuffer uploadCommandBuffer);
150  void CreateRenderPass(VkDevice device, VkFormat swapchainFormat, VkRenderPass &rp);
151  void SetupDebugCallback(VkInstance instance, VkDebugReportCallbackEXT &fp);
152 
153  vector<MemoryTypeInfo> EnumerateHeaps(VkPhysicalDevice device);
154 public:
155  void render();
156 #endif
157 
158 public:
159  int width, height;
161 #ifdef _WIN32
162  HWND hwnd;
163  HDC hdc;
164 #endif
165 #ifndef WIN32
166 #ifndef __OBJC__
167  Display *display;
168  Window window;
169  XFontStruct *font;
170 #endif
171 #endif
172 
173 #ifdef DIRECTX
174 #ifdef D3D11
175  ID3D11Device *device;
176  ID3D11DeviceContext *context;
177  IDXGISwapChain *swapchain;
178  ID3D11Texture2D *back_buffer;
179  ID3D11Texture2D *depth_buffer;
180  ID3D11RenderTargetView *render_target;
181  ID3D11DepthStencilView *depth_view;
182 
183 
184 
185  vector<ID3D11Buffer *> vertex_buffers;
186  vector<ID3D11Buffer *> index_buffers;
187  //vector<ID3D11Texture2D *> texture;
189 
190  ID3D11RasterizerState *render_state;
191  ID3D11RasterizerState *render_state_wireframe;
192 
193 #else
194  IDirect3D9 *d3d;
195  IDirect3DDevice9 *device;
196  D3DPRESENT_PARAMETERS d3dpp;
197  LPDIRECT3DVERTEXDECLARATION9 vertex_decl;
198  vector<IDirect3DVertexBuffer9 *> vertex_buffers;
199  vector<IDirect3DIndexBuffer9 *> index_buffers;
202  unsigned char clear_r;
203  unsigned char clear_g;
204  unsigned char clear_b;
205 // ID3DXFont *font;
206 #endif
207 #endif
208 
209 #ifdef VULKAN
210  static const int QUEUE_SLOT_COUNT = 3;
211 
212  VkInstance vk_instance = VK_NULL_HANDLE;
213  VkDevice vk_device = VK_NULL_HANDLE;
214 
215 
216  VkSemaphore renderingCompleteSemaphore;
217  VkSemaphore imageAcquiredSemaphore;
218  VkDebugReportCallbackEXT callback;
219 
220 
221  VkDeviceMemory deviceMemory_ = VK_NULL_HANDLE;
222  VkBuffer vertexBuffer_ = VK_NULL_HANDLE;
223  VkBuffer indexBuffer_ = VK_NULL_HANDLE;
224 
225  VkDeviceMemory uploadMemory_ = VK_NULL_HANDLE;
226  VkBuffer uploadBuffer_ = VK_NULL_HANDLE;
227 
228  VkShaderModule vertexShader_ = VK_NULL_HANDLE;
229  VkShaderModule fragmentShader_ = VK_NULL_HANDLE;
230 
231  VkPipeline pipeline_ = VK_NULL_HANDLE;
232  VkPipelineLayout pipelineLayout_ = VK_NULL_HANDLE;
233 
234  VkDeviceMemory deviceImageMemory_ = VK_NULL_HANDLE;
235  VkImage Image_ = VK_NULL_HANDLE;
236  VkImageView ImageView_ = VK_NULL_HANDLE;
237 
238  VkDeviceMemory uploadImageMemory_ = VK_NULL_HANDLE;
239  VkBuffer uploadImageBuffer_ = VK_NULL_HANDLE;
240 
241  VkDescriptorPool descriptorPool_ = VK_NULL_HANDLE;
242  VkDescriptorSet descriptorSet_ = VK_NULL_HANDLE;
243  VkDescriptorSetLayout descriptorSetLayout_ = VK_NULL_HANDLE;
244 
245  VkSampler sampler_ = VK_NULL_HANDLE;
246 
247  VkViewport viewport_;
248 
249  VkSwapchainKHR swapchain_ = VK_NULL_HANDLE;
250  VkPhysicalDevice physicalDevice_ = VK_NULL_HANDLE;
251  VkQueue queue_ = VK_NULL_HANDLE;
252 
253  VkSurfaceKHR surface_ = VK_NULL_HANDLE;
254 
255  VkFence frameFences_[QUEUE_SLOT_COUNT];
256  VkImage swapchainImages_[QUEUE_SLOT_COUNT];
257  VkImageView swapChainImageViews_[QUEUE_SLOT_COUNT];
258  VkFramebuffer framebuffer_[QUEUE_SLOT_COUNT];
259 
260  VkRenderPass renderPass_ = VK_NULL_HANDLE;
261 
262  int queueFamilyIndex_ = -1;
263 
264  VkCommandPool commandPool_;
265  VkCommandBuffer commandBuffers_[QUEUE_SLOT_COUNT];
266  VkCommandBuffer setupCommandBuffer_;
267  uint32_t currentBackBuffer_ = 0;
268 #endif
269 };
270 
271 class Shader
272 {
273 public:
274  Shader();
275 
276  int init(Graphics *gfx, char *vertex_file, char *geometry_file, char *fragment_file);
277  virtual void Select();
278  void destroy();
279  virtual ~Shader();
280  virtual void prelink() = 0;
281 protected:
283 #ifdef DIRECTX
284 #ifdef D3D11
285  Graphics *gfx;
286 
287  ID3D11InputLayout *layout;
288  ID3D11VertexShader *vertex_shader;
289  ID3D11PixelShader *fragment_shader;
290 #else
291  Graphics *gfx;
292  IDirect3DVertexShader9 *vertex_shader;
293  IDirect3DPixelShader9 *pixel_shader;
294 // IDirect3DGeometryShader9 *geometry_shader;
295  LPD3DXCONSTANTTABLE uniform_vs;
296  LPD3DXCONSTANTTABLE uniform_ps;
297 #endif
298 #else
303 #endif
304 };
305 
306 
307 
308 
309 #endif