Nxna2
 All Classes Namespaces Functions Enumerations Enumerator Pages
GraphicsDevice.h
1 #ifndef NXNA_GRAPHICS_GRAPHICSDEVICE_H
2 #define NXNA_GRAPHICS_GRAPHICSDEVICE_H
3 
4 #ifdef NXNA_ENABLE_DIRECT3D11
5 #include <d3d11.h>
6 #endif
7 
8 #ifdef NXNA_ENABLE_MATH
9 #include "../Vector3.h"
10 #include "../Matrix.h"
11 #endif
12 
13 #include "PipelineState.h"
14 
15 
17 namespace Nxna
18 {
20  namespace Graphics
21  {
23  enum class GraphicsDeviceType
24  {
26  None,
27 
29  OpenGl41,
30 
32  OpenGLES3,
33 
35  Direct3D11,
36 
37 #ifndef NDEBUG
38  LAST
39 #endif
40  };
41 
43  {
44  struct
45  {
46  bool SupportsS3tcTextureCompression;
47  } OpenGLCaps;
48  };
49 
50  enum class Usage
51  {
53  Default,
54 
56  Immutable,
57 
59  Dynamic
60  };
61 
62  enum class PrimitiveType
63  {
66 
69  };
70 
71  enum class IndexElementSize
72  {
74  SixteenBits = 2,
75 
77  ThirtyTwoBits = 4
78  };
79 
80  struct IndexBuffer
81  {
82  union
83  {
84  struct
85  {
86  unsigned int ByteLength;
87  unsigned int Buffer;
88  } OpenGL;
89 #ifdef NXNA_ENABLE_DIRECT3D11
90  struct
91  {
92  ID3D11Buffer* Buffer;
93  } Direct3D11;
94 #endif
95  };
96 
97  IndexElementSize ElementSize;
98 
99 #ifndef NXNA_DISABLE_VALIDATION
100  unsigned int ByteLength;
101  Usage BufferUsage;
102 #endif
103  };
104 
106  {
107  union
108  {
109  struct
110  {
111  unsigned int ByteLength;
112  unsigned int Buffer;
113  unsigned int VAO;
114  } OpenGL;
115 #ifdef NXNA_ENABLE_DIRECT3D11
116  struct
117  {
118  ID3D11Buffer* Buffer;
119  } Direct3D11;
120 #endif
121  };
122 
123 #ifndef NXNA_DISABLE_VALIDATION
124  unsigned int ByteLength;
125  Usage BufferUsage;
126 #endif
127  };
128 
130  {
131  union
132  {
133  struct
134  {
135  unsigned int ByteLength;
136  unsigned int UniformBuffer;
137  } OpenGL;
138 #ifdef NXNA_ENABLE_DIRECT3D11
139  struct
140  {
141  ID3D11Buffer* Buffer;
142  } Direct3D11;
143 #endif
144  };
145 
146 #ifndef NXNA_DISABLE_VALIDATION
147  Usage BufferUsage;
148  unsigned int ByteLength;
149 #endif
150  };
151 
152  enum class BufferType
153  {
155  Index,
156 
158  Vertex,
159 
161  Constant
162  };
163 
164  struct Buffer
165  {
166  union
167  {
168  struct
169  {
170  unsigned int BufferHandle;
171  } OpenGL;
172 #ifdef NXNA_ENABLE_DIRECT3D11
173  struct
174  {
175  ID3D11Buffer* BufferPtr;
176  } Direct3D11;
177 #endif
178  };
179 
180 #ifndef NXNA_DISABLE_VALIDATION
181  BufferType Type;
182  Usage BufferUsage;
183  unsigned int ByteLength;
184 #endif
185  };
186 
187 
188  struct Viewport
189  {
190  Viewport()
191  {
192  X = 0;
193  Y = 0;
194  Width = 0;
195  Height = 0;
196  }
197 
198  Viewport(int x, int y, int width, int height)
199  {
200  X = x;
201  Y = y;
202  Width = width;
203  Height = height;
204  }
205 
206  float GetAspectRatio() const { return Width / (float)Height; }
207 
208 #ifdef NXNA_ENABLE_MATH
209  Vector3 Project(const Vector3& source,
210  const Matrix& project,
211  const Matrix& view,
212  const Matrix& world);
213 #endif
214  void Project(const float* source3f,
215  const float* projectMatrix16f,
216  const float* viewMatrix16f,
217  const float* worldMatrix16f,
218  float* result3f);
219 
220 #ifdef NXNA_ENABLE_MATH
221  Vector3 Unproject(const Vector3& source,
222  const Matrix& projection,
223  const Matrix& view,
224  const Matrix& world);
225 #endif
226 
227 #ifdef NXNA_ENABLE_MATH
228  Rectangle GetBounds()
229  {
230  return Rectangle(X, Y, Width, Height);
231  }
232 #endif
233 
234  int X, Y, Width, Height;
235  };
236 
238  {
239  GraphicsDeviceType DeviceType;
240  const char* Message;
241  };
242 
243  enum class ShaderType
244  {
245  Vertex,
246  Pixel
247  };
248 
249  typedef void (*GraphicsDeviceMessageCallback)(GraphicsDeviceDebugMessage);
250 
259  {
260  GraphicsDeviceType Type;
261  int ScreenWidth;
262  int ScreenHeight;
263 
264  union
265  {
266  struct
267  {
268  // nothing
269  } OpenGL;
270 #ifdef NXNA_ENABLE_DIRECT3D11
271  struct
272  {
273  ID3D11Device* Device;
274  ID3D11DeviceContext* DeviceContext;
275  ID3D11RenderTargetView* RenderTargetView;
276  IDXGISwapChain* SwapChain;
277  } Direct3D11;
278 #endif
279  };
280  };
281 
282 #ifdef NXNA_ENABLE_DIRECT3D11
283  struct D3D11DeviceState
284  {
285  ID3D11Device* Device;
286  ID3D11DeviceContext* Context;
287  ID3D11RenderTargetView* RenderTargetView;
288  IDXGISwapChain* SwapChain;
289  };
290 #endif
291 
292  enum class SurfaceFormat
293  {
294  Color,
295  Bgr565,
296  Bgra5551,
297  Bgra4444,
298  Dxt1,
299  Dxt3,
300  Dxt5,
301 
302  // the following are not supported by XNA. These are our own
303  // extensions so that iOS devices can have compressed textures too.
304  Pvrtc4
305  };
306 
308  {
309  int Width;
310  int Height;
311  int MipLevels;
312  Usage TextureUsage;
313  SurfaceFormat Format;
314 
315  void* InitialData;
316  unsigned int InitialDataByteCount;
317  };
318 
319  struct Texture2D
320  {
321  union
322  {
323  struct
324  {
325  unsigned int Handle;
326  } OpenGL;
327 #ifdef NXNA_ENABLE_DIRECT3D11
328  struct
329  {
330  ID3D11ShaderResourceView* m_shaderResourceView;
331  ID3D11Texture2D* m_texture;
332  } Direct3D11;
333 #endif
334  };
335  };
336 
338  {
339  Texture2D Texture;
340  int Width;
341  int Height;
342  };
343 
345  {
346  IndexElementSize ElementSize;
347  int NumElements;
348  Usage BufferUsage;
349 
350  void* InitialData;
351  unsigned int InitialDataByteCount;
352  };
353 
355  {
356  int NumVertices;
357  Usage BufferUsage;
358  InputElement* InputElements;
359  int NumInputElements;
360  int StrideBytes;
361 
362  void* InitialData;
363  unsigned int InitialDataByteCount;
364  };
365 
367  {
368  Usage BufferUsage;
369  unsigned int ByteCount;
370 
371  void* InitialData;
372  };
373 
374  enum class MapType
375  {
376  Write,
377  WriteDiscard,
378  WriteNoOverwrite
379  };
380 
382  {
383  GraphicsDeviceType m_type;
384  Capabilities m_caps;
385  int m_screenWidth, m_screenHeight;
386 
387  IndexBuffer m_indices;
388  VertexBuffer m_vertices;
389  ShaderPipeline* m_shaderPipeline;
390  BlendState* m_blendState;
391  RasterizerState* m_rasterizerState;
392 
393  NxnaResultDetails m_errorDetails;
394 
395 #ifdef NXNA_ENABLE_DIRECT3D11
396  D3D11DeviceState m_d3d11State;
397 #endif
398 
399  public:
404  static NxnaResult CreateGraphicsDevice(const GraphicsDeviceDesc* params, GraphicsDevice* result);
405  static void DestroyGraphicsDevice(GraphicsDevice* device);
406 
407  void SetMessageCallback(GraphicsDeviceMessageCallback callback);
408 
409 #ifndef NXNA_ENABLE_DIRECT3D11
410  constexpr GraphicsDeviceType GetType() { return m_type; }
411 #else
412  GraphicsDeviceType GetType() { return m_type; }
413 #endif
414  const NxnaResultDetails* GetErrorDetails() { return &m_errorDetails; }
415 
416  Capabilities* GetCaps() { return &m_caps; }
417 
419  void SetViewport(int x, int y, int width, int height);
421  void SetViewport(Viewport viewport);
422 
429  NxnaResult CreateShader(ShaderType type, const void* bytecode, int bytecodeLength, Shader* result);
430 
436  void DestroyShader(Shader* shader);
437 
443  void BindTexture(Texture2D* texture, int textureUnit);
444 
447  void DestroyTexture2D(Texture2D* texture);
448 
454  void SetShaderPipeline(ShaderPipeline* pipeline);
455 
458  void DestroyShaderPipeline(ShaderPipeline* pipeline);
459 
465  void SetBlendState(BlendState* state);
466 
469  void DestroyBlendState(BlendState* state);
470 
471  NxnaResult CreateRasterizerState(const RasterizerStateDesc* desc, RasterizerState* result);
472  void SetRasterizerState(RasterizerState* state);
473  void DestroyRasterizerState(RasterizerState* state);
474 
482  void DestroyIndexBuffer(IndexBuffer buffer);
486  void UpdateIndexBuffer(IndexBuffer buffer, unsigned int startOffset, void* data, unsigned int dataLengthInBytes);
487  void SetIndices(IndexBuffer indices);
488 
496  void DestroyVertexBuffer(VertexBuffer buffer);
500  void UpdateVertexBuffer(VertexBuffer buffer, unsigned int startOffset, void* data, unsigned int dataLengthInBytes);
501  void SetVertexBuffer(VertexBuffer vertexBuffer, unsigned int offset, unsigned int stride);
502 
511  void UpdateConstantBuffer(ConstantBuffer buffer, void* data, unsigned int dataLengthInBytes);
512  void SetConstantBuffer(ConstantBuffer buffer, int index);
516 
523  void* MapBuffer(IndexBuffer buffer, MapType type);
530  void* MapBuffer(VertexBuffer buffer, MapType type);
537  void* MapBuffer(ConstantBuffer buffer, MapType type);
538 
541  void UnmapBuffer(IndexBuffer buffer);
544  void UnmapBuffer(VertexBuffer buffer);
547  void UnmapBuffer(ConstantBuffer buffer);
548 
549  void DrawIndexedPrimitives(PrimitiveType primitiveType, int baseVertex, int minVertexIndex, int numVertices, int startIndex, int primitiveCount);
550  void DrawPrimitives(PrimitiveType primitiveType, int startVertex, int primitiveCount);
551 
552  void Clear(float r, float g, float b, float a);
553  void Present();
554 
555 #ifdef NXNA_ENABLE_DIRECT3D11
556  const D3D11DeviceState* GetD3D11DeviceState() { return &m_d3d11State; }
560 #endif
561 
562  private:
563  void applyDirtyStates();
564  };
565  }
566 }
567 
568 #endif // NXNA_GRAPHICS_GRAPHICSDEVICE_H
NxnaResult CreateVertexBuffer(const VertexBufferDesc *desc, VertexBuffer *result)
Definition: GraphicsDevice.cpp:1097
NxnaResult CreateTexture2D(const TextureCreationDesc *desc, Texture2D *result)
Definition: GraphicsDevice.cpp:280
void * MapBuffer(IndexBuffer buffer, MapType type)
Definition: GraphicsDevice.cpp:1459
void UpdateConstantBuffer(ConstantBuffer buffer, void *data, unsigned int dataLengthInBytes)
Definition: GraphicsDevice.cpp:1372
Definition: PipelineState.h:191
NxnaResult CreateShader(ShaderType type, const void *bytecode, int bytecodeLength, Shader *result)
Definition: GraphicsDevice.cpp:183
Definition: PipelineState.h:142
void DestroyTexture2D(Texture2D *texture)
Definition: GraphicsDevice.cpp:397
Definition: Nxna2.h:112
Definition: GraphicsDevice.h:258
Default write-only usage.
Definition: PipelineState.h:183
Definition: PipelineState.h:51
Definition: GraphicsDevice.h:237
Usage
Definition: GraphicsDevice.h:50
void SetViewport(int x, int y, int width, int height)
Sets the current viewport.
Definition: GraphicsDevice.cpp:121
Definition: PipelineState.h:58
Definition: GraphicsDevice.h:105
Definition: GraphicsDevice.h:164
void DestroyConstantBuffer(ConstantBuffer *buffer)
Definition: GraphicsDevice.cpp:1415
Definition: NxnaCommon.h:24
Definition: GraphicsDevice.h:80
NxnaResult
Definition: NxnaCommon.h:12
Definition: GraphicsDevice.h:354
void UpdateIndexBuffer(IndexBuffer buffer, unsigned int startOffset, void *data, unsigned int dataLengthInBytes)
Definition: GraphicsDevice.cpp:1025
Write lots of times.
Definition: GraphicsDevice.h:344
Definition: GraphicsDevice.h:42
void DestroyBlendState(BlendState *state)
Definition: GraphicsDevice.cpp:780
PrimitiveType
Definition: GraphicsDevice.h:62
Definition: GraphicsDevice.h:337
Definition: PipelineState.h:101
Definition: GraphicsDevice.h:129
Definition: GraphicsDevice.h:307
Definition: Vector3.h:11
void DestroyIndexBuffer(IndexBuffer buffer)
Definition: GraphicsDevice.cpp:1006
void UnmapBuffer(IndexBuffer buffer)
Definition: GraphicsDevice.cpp:1608
void DestroyShaderPipeline(ShaderPipeline *pipeline)
Definition: GraphicsDevice.cpp:536
Definition: GraphicsDevice.h:188
GraphicsDeviceType
The type of graphics device.
Definition: GraphicsDevice.h:23
BufferType
Definition: GraphicsDevice.h:152
Definition: GraphicsDevice.h:366
NxnaResult CreateIndexBuffer(const IndexBufferDesc *desc, IndexBuffer *result)
Definition: GraphicsDevice.cpp:931
A constant (or uniform) buffer.
Definition: Color.h:6
Definition: GraphicsDevice.h:381
NxnaResult CreateShaderPipeline(const ShaderPipelineDesc *desc, ShaderPipeline *result)
Definition: GraphicsDevice.cpp:402
Definition: GraphicsDevice.h:319
void DestroyShader(Shader *shader)
Definition: GraphicsDevice.cpp:258
NxnaResult CreateBlendState(const BlendStateDesc *desc, BlendState *result)
Definition: GraphicsDevice.cpp:604
NxnaResult CreateConstantBuffer(const ConstantBufferDesc *desc, ConstantBuffer *result)
Definition: GraphicsDevice.cpp:1285
static NxnaResult CreateGraphicsDevice(const GraphicsDeviceDesc *params, GraphicsDevice *result)
Definition: GraphicsDevice.cpp:42
void UpdateVertexBuffer(VertexBuffer buffer, unsigned int startOffset, void *data, unsigned int dataLengthInBytes)
Definition: GraphicsDevice.cpp:1227
Definition: PipelineState.h:133
Definition: Matrix.h:8
IndexElementSize
Definition: GraphicsDevice.h:71
Definition: PipelineState.h:110
void DestroyVertexBuffer(VertexBuffer buffer)
Definition: GraphicsDevice.cpp:1207