graphic_context.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Harry Storbacka
28 ** Kenneth Gangstoe
29 */
30 
31 #pragma once
32 
33 #include "../2D/color.h"
34 #include "../Image/texture_format.h"
35 #include <memory>
36 #include "../../Core/Math/mat4.h"
37 #include "../../Core/Math/rect.h"
38 #include "../../Core/Signals/signal.h"
39 #include "primitives_array.h"
40 #include "frame_buffer.h"
41 #include "element_array_vector.h"
42 
43 namespace clan
44 {
47 
48  class Size;
49  class Texture;
50  class FrameBuffer;
51  class PixelBuffer;
52  class PrimitivesArray;
53  class Font;
54  class FontMetrics;
55  class GraphicContextProvider;
56  class GraphicContext_Impl;
57  class ProgramObject;
58  class Angle;
59  class RenderBatcher;
60  class FontProvider_Freetype;
61  class UniformBuffer;
62  class StorageBuffer;
63  class RasterizerState;
64  class BlendState;
65  class DepthStencilState;
66 
68  enum CullMode
69  {
73  };
74 
76  enum FillMode
77  {
81  };
82 
84  enum FaceSide
85  {
88  };
89 
92  {
101  };
102 
105  {
114  };
115 
118  {
129  };
130 
132  enum LogicOp
133  {
150  };
151 
154  {
157 
160 
163 
166 
169 
172 
175 
178 
181 
184 
187 
190 
193 
196 
199  };
200 
203  {
209  };
210 
213  {
216  };
217 
220  {
228  };
229 
232  {
235  };
236 
239  {
244  };
245 
248  {
253  };
254 
257  {
258  public:
260  GraphicContext();
261 
266 
267  ~GraphicContext();
268 
270  bool is_null() const { return !impl; }
271  explicit operator bool() const { return bool(impl); }
272 
274  void throw_if_null() const;
275 
278 
281 
284 
289  int get_major_version() const;
290 
295  int get_minor_version() const;
296 
302  bool has_compute_shader_support() const;
303 
309  Texture get_texture(int index) const;
310 
315  std::vector<Texture> get_textures() const;
316 
321 
326 
329 
331  int get_width() const;
332 
334  int get_height() const;
335 
337  Size get_size() const;
338 
341  float get_pixel_ratio() const;
342 
345  float get_dip_width() const { return get_width() / get_pixel_ratio(); }
346 
349  float get_dip_height() const { return get_height() / get_pixel_ratio(); }
350 
354 
359  Size get_max_texture_size() const;
360 
363 
364  const GraphicContextProvider * get_provider() const;
365 
367  GraphicContext create() const;
368 
370  GraphicContext create(FrameBuffer &buffer) const;
371 
373  GraphicContext clone() const;
374 
376  PixelBuffer get_pixeldata(const Rect& rect, TextureFormat texture_format = tf_rgba8, bool clamp = true);
377 
379  PixelBuffer get_pixeldata(TextureFormat texture_format = tf_rgba8, bool clamp = true);
380 
388  bool is_frame_buffer_owner(const FrameBuffer &fb);
389 
391  void set_frame_buffer(const FrameBuffer &write_buffer);
392  void set_frame_buffer(const FrameBuffer &write_buffer, const FrameBuffer &read_buffer);
393 
395  void reset_frame_buffer();
396 
398  void set_uniform_buffer(int index, const UniformBuffer &buffer);
399 
401  void reset_uniform_buffer(int index);
402 
404  void set_storage_buffer(int index, const StorageBuffer &buffer);
405 
407  void reset_storage_buffer(int index);
408 
413  void set_texture(int unit_index, const Texture &texture);
414 
420  void set_textures(std::vector<Texture> &textures);
421 
425  void reset_texture(int unit_index);
426 
428  void reset_textures();
429 
434  void set_image_texture(int unit_index, const Texture &texture);
435 
441  void set_image_texture(std::vector<Texture> &textures);
442 
446  void reset_image_texture(int unit_index);
447 
449  void reset_image_textures();
450 
452  void set_rasterizer_state(const RasterizerState &state);
453 
455  void set_blend_state(const BlendState &state, const Colorf &blend_color = StandardColorf::white(), unsigned int sample_mask = 0xffffffff);
456 
458  void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref = 0);
459 
461  void reset_rasterizer_state();
462 
464  void reset_blend_state();
465 
468 
470  void set_program_object(StandardProgram standard_program);
471 
475  void set_program_object(const ProgramObject &program);
476 
478  void reset_program_object();
479 
484  bool is_primitives_array_owner(const PrimitivesArray &primitives_array);
485 
487  void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArray &array);
488 
491 
493  void draw_primitives_array(PrimitivesType type, int num_vertices);
494 
500  void draw_primitives_array(PrimitivesType type, int offset, int num_vertices);
501 
508  void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count);
509 
511  void set_primitives_elements(ElementArrayBuffer &element_array);
512 
514  template<typename Type>
516  {
518  }
519 
526  void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset = 0);
527 
535  void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count);
536 
539 
547  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset = 0);
548 
556  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned int> &element_array, size_t offset = 0)
557  {
558  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_int, offset * sizeof(unsigned int));
559  }
560 
568  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned short> &element_array, size_t offset = 0)
569  {
570  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_short, offset * sizeof(unsigned short));
571  }
572 
580  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned char> &element_array, size_t offset = 0)
581  {
582  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_byte, offset * sizeof(unsigned char));
583  }
584 
593  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset, int instance_count);
594 
603  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned int> &element_array, size_t offset, int instance_count)
604  {
605  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_int, offset * sizeof(unsigned int), instance_count);
606  }
607 
616  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned short> &element_array, size_t offset, int instance_count)
617  {
618  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_short, offset * sizeof(unsigned short), instance_count);
619  }
620 
629  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned char> &element_array, size_t offset, int instance_count)
630  {
631  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_byte, offset * sizeof(unsigned char), instance_count);
632  }
633 
635  void reset_primitives_array();
636 
638  void dispatch(int x = 1, int y = 1, int z = 1);
639 
641  void clear(const Colorf &color = StandardColorf::black());
642 
646  void clear_stencil(int value = 0);
647 
651  void clear_depth(float value = 0);
652 
654  void set_scissor(const Rect &rect, TextureImageYAxis y_axis);
655 
657  void reset_scissor();
658 
662  void set_viewport(const Rectf &viewport);
663 
668  void set_viewport(int index, const Rectf &viewport);
669 
671  void set_depth_range(float n, float f);
672 
674  void set_depth_range(int viewport, float n, float f);
675 
677  void set_draw_buffer(DrawBuffer buffer);
678 
680  void flush();
681 
682  bool operator ==(const GraphicContext &other) const { return impl == other.impl; }
683  bool operator !=(const GraphicContext &other) const { return impl != other.impl; }
684 
685  private:
686  std::shared_ptr<GraphicContext_Impl> impl;
687 
688  friend class OpenGL;
689  };
690 
691  const float pixelcenter_constant = 0.375f;
692 
694 }
Definition: graphic_context.h:124
PointSpriteOrigin
Point Sprite Origin.
Definition: graphic_context.h:212
Primitives array description.
Definition: primitives_array.h:67
Definition: graphic_context.h:138
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned int > &element_array, size_t offset=0)
Definition: graphic_context.h:556
void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset=0)
void reset_textures()
Remove all selected textures.
Definition: clanapp.h:35
Floating point color description class (for float).
Definition: color.h:798
Rasterizer state setup.
Definition: rasterizer_state.h:43
Definition: graphic_context.h:142
Size get_max_texture_size() const
void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count)
void reset_frame_buffer()
Resets the current frame buffer to be the initial frame buffer.
source or destination (1, 1, 1, 1) - (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:183
source or destination (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:195
Definition: graphic_context.h:121
Size get_size() const
Returns the current actual size of the context.
void reset_texture(int unit_index)
destination (1, 1, 1, 1) - (Rs, Gs, Bs, As)
Definition: graphic_context.h:171
void clear_stencil(int value=0)
void reset_rasterizer_state()
Set active rasterizer state.
Frame-buffer object class.
Definition: frame_buffer.h:71
source (f, f, f, 1) - f = min(As, 1 - Ad)
Definition: graphic_context.h:186
Definition: graphic_context.h:251
Definition: graphic_context.h:204
Definition: graphic_context.h:215
Definition: graphic_context.h:140
FaceSide
Front face modes.
Definition: graphic_context.h:84
Definition: graphic_context.h:136
Definition: graphic_context.h:80
Definition: graphic_context.h:141
Interface for implementing a GraphicContext target.
Definition: graphic_context_provider.h:85
Texture object class.
Definition: texture.h:102
Definition: graphic_context.h:139
float get_pixel_ratio() const
Definition: graphic_context.h:108
Definition: graphic_context.h:149
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned char > &element_array, size_t offset=0)
Definition: graphic_context.h:580
void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count)
Pixel data container.
Definition: pixel_buffer.h:67
Texture get_texture(int index) const
void reset_uniform_buffer(int index)
Remove uniform buffer from index.
Definition: graphic_context.h:106
Blend state setup.
Definition: blend_state.h:43
void set_scissor(const Rect &rect, TextureImageYAxis y_axis)
Set the current clipping rectangle.
Definition: graphic_context.h:126
Element Array Vector.
Definition: element_array_vector.h:42
Definition: graphic_context.h:221
BlendEquation
Blending equations.
Definition: graphic_context.h:202
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned short > &element_array, size_t offset=0)
Definition: graphic_context.h:568
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned char > &element_array, size_t offset, int instance_count)
Definition: graphic_context.h:629
ProgramObject get_program_object() const
Returns the currently selected program object.
Definition: graphic_context.h:100
OpenGL utility class.
Definition: opengl.h:73
FrameBuffer get_write_frame_buffer() const
OpenGL, origin is lower left with Y going upwards.
Definition: graphic_context.h:233
void set_primitives_elements(ElementArrayBuffer &element_array)
Sets current elements array buffer.
void set_texture(int unit_index, const Texture &texture)
Definition: graphic_context.h:224
source (1, 1, 1, 1) - (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:168
DrawBuffer
Drawing buffers.
Definition: graphic_context.h:117
Definition: graphic_context.h:206
GraphicContext()
Constructs a null instance.
Direct3D, origin is upper left with Y going downwards.
Definition: graphic_context.h:234
void set_rasterizer_state(const RasterizerState &state)
Set active rasterizer state.
ShaderLanguage
Shader language used.
Definition: graphic_context.h:247
Definition: graphic_context.h:87
Definition: graphic_context.h:86
Definition: graphic_context.h:146
CullMode
Polygon culling modes.
Definition: graphic_context.h:68
ClipZRange
Definition: mat4.h:50
void set_draw_buffer(DrawBuffer buffer)
Set used draw buffer.
Definition: graphic_context.h:70
ShaderLanguage get_shader_language() const
Returns the shader language used.
bool is_frame_buffer_owner(const FrameBuffer &fb)
PixelBuffer get_pixeldata(const Rect &rect, TextureFormat texture_format=tf_rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
void reset_scissor()
Removes the set clipping rectangle.
void reset_primitives_elements()
Resets current elements array buffer.
void set_depth_range(float n, float f)
Specifies the depth range for all viewports.
bool operator!=(const GraphicContext &other) const
Definition: graphic_context.h:683
void set_primitives_array(const PrimitivesArray &array)
Set the primitives array on the gc.
Definition: graphic_context.h:93
std::vector< Texture > get_textures() const
Definition: graphic_context.h:107
Definition: graphic_context.h:95
StandardProgram
Standard Program.
Definition: graphic_context.h:238
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:488
void throw_if_null() const
Throw an exception if this object is invalid.
void set_primitives_elements(ElementArrayVector< Type > &element_array)
Sets current elements array buffer.
Definition: graphic_context.h:515
StencilOp
Stencil operations.
Definition: graphic_context.h:104
void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArray &array)
Draw primitives on gc.
source or destination (1, 1, 1, 1) - (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:198
GraphicContext create() const
Create a new default graphic context compatible with this one.
Definition: graphic_context.h:242
TextureImageYAxis get_texture_image_y_axis() const
Returns the Y axis direction for viewports, clipping rects, textures and render targets.
Definition: graphic_context.h:135
int get_minor_version() const
Definition: graphic_context.h:96
void reset_depth_stencil_state()
Set active depth stencil state.
int get_major_version() const
Definition: graphic_context.h:222
GraphicContext clone() const
Create a new default graphic context cloned with this one.
Definition: graphic_context.h:112
Definition: graphic_context.h:252
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:502
bool is_null() const
Returns true if this object is invalid.
Definition: graphic_context.h:270
Definition: primitives_array.h:57
Definition: graphic_context.h:97
FillMode
Polygon filling modes.
Definition: graphic_context.h:76
void reset_blend_state()
Set active blend state.
void set_frame_buffer(const FrameBuffer &write_buffer)
Sets the current frame buffer.
void set_viewport(const Rectf &viewport)
Definition: graphic_context.h:71
int get_height() const
Returns the current actual height of the context.
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned short > &element_array, size_t offset, int instance_count)
Definition: graphic_context.h:616
Definition: graphic_context.h:249
float get_dip_height() const
Definition: graphic_context.h:349
void reset_storage_buffer(int index)
Remove storage buffer from index.
source or destination (1, 1, 1, 1)
Definition: graphic_context.h:159
Definition: graphic_context.h:123
int get_width() const
Returns the current actual width of the context.
void set_program_object(StandardProgram standard_program)
Set active program object to the standard program specified.
Definition: graphic_context.h:205
Definition: graphic_context.h:94
Definition: graphic_context.h:227
source or destination (1, 1, 1, 1) - (As, As, As, As)
Definition: graphic_context.h:177
Definition: graphic_context.h:143
TextureFormat
Texture format.
Definition: texture_format.h:38
TextureImageYAxis
Y axis direction for viewports, clipping rects, textures and render targets.
Definition: graphic_context.h:231
const float pixelcenter_constant
Definition: graphic_context.h:691
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned int > &element_array, size_t offset, int instance_count)
Definition: graphic_context.h:603
Definition: primitives_array.h:58
void set_uniform_buffer(int index, const UniformBuffer &buffer)
Select uniform buffer into index.
Definition: graphic_context.h:226
bool has_compute_shader_support() const
Definition: graphic_context.h:125
float get_dip_width() const
Definition: graphic_context.h:345
Definition: graphic_context.h:225
Definition: graphic_context.h:110
void dispatch(int x=1, int y=1, int z=1)
Execute a compute shader.
Definition: graphic_context.h:144
Definition: graphic_context.h:214
Definition: graphic_context.h:243
void clear_depth(float value=0)
static Colorf black()
Definition: color.h:1747
source or destination (As, As, As, As)
Definition: graphic_context.h:174
void set_image_texture(int unit_index, const Texture &texture)
void draw_primitives_array(PrimitivesType type, int num_vertices)
Draws primitives from the current assigned primitives array.
bool operator==(const GraphicContext &other) const
Definition: graphic_context.h:682
Definition: primitives_array.h:59
void reset_program_object()
Remove active program object.
Element Array Buffer.
Definition: element_array_buffer.h:45
Uniform Buffer.
Definition: uniform_buffer.h:46
Definition: graphic_context.h:148
Interface to drawing graphics.
Definition: graphic_context.h:256
BlendFunc
Blending functions.
Definition: graphic_context.h:153
Definition: graphic_context.h:147
VertexAttributeDataType
Primitives array description.
Definition: primitives_array.h:55
Definition: graphic_context.h:207
source (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:162
void clear(const Colorf &color=StandardColorf::black())
Clears the whole context using the specified color.
static Colorf white()
Definition: color.h:1884
Definition: graphic_context.h:208
Program Object.
Definition: program_object.h:68
CompareFunction
Compare functions.
Definition: graphic_context.h:91
source or destination (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:189
Definition: graphic_context.h:78
Definition: graphic_context.h:72
Sizef get_dip_size() const
Definition: graphic_context.h:353
source or destination (0, 0, 0, 0)
Definition: graphic_context.h:156
Definition: graphic_context.h:145
2D (width,height) size structure - Integer
Definition: size.h:170
void reset_image_texture(int unit_index)
void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref=0)
Set active depth stencil state.
DepthStencil state setup.
Definition: depth_stencil_state.h:43
Definition: graphic_context.h:120
Definition: graphic_context.h:79
Storage Buffer.
Definition: storage_buffer.h:46
void set_textures(std::vector< Texture > &textures)
void reset_primitives_array()
Reset the primitives arrays.
void reset_image_textures()
Remove all selected textures.
Definition: graphic_context.h:223
destination (Rs, Gs, Bs, As)
Definition: graphic_context.h:165
LogicOp
Logic Op.
Definition: graphic_context.h:132
PrimitivesType
Primitive types.
Definition: graphic_context.h:219
source or destination (1, 1, 1, 1) - (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:192
Definition: graphic_context.h:99
FrameBuffer get_read_frame_buffer() const
bool is_primitives_array_owner(const PrimitivesArray &primitives_array)
Definition: texture_format.h:41
void set_blend_state(const BlendState &state, const Colorf &blend_color=StandardColorf::white(), unsigned int sample_mask=0xffffffff)
Set active blend state.
Definition: graphic_context.h:137
void flush()
Flush the command buffer.
Definition: graphic_context.h:98
Definition: graphic_context.h:113
ClipZRange get_clip_z_range() const
Returns in what range clip space z values are clipped.
Definition: graphic_context.h:241
void set_storage_buffer(int index, const StorageBuffer &buffer)
Select storage buffer into index.
Definition: graphic_context.h:250
Definition: graphic_context.h:119
Definition: graphic_context.h:122
Definition: graphic_context.h:128
GraphicContextProvider * get_provider()
Returns the provider for this graphic context.
Definition: graphic_context.h:109
source or destination (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:180
Definition: graphic_context.h:127
2D (width,height) size structure - Float
Definition: size.h:183
Definition: graphic_context.h:240
Definition: graphic_context.h:111
Definition: graphic_context.h:134