graphic_context.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 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 
32 #pragma once
33 
34 #include "../2D/color.h"
35 #include "../Image/texture_format.h"
36 #include <memory>
37 #include "../../Core/Math/mat4.h"
38 #include "../../Core/Math/rect.h"
39 #include "../../Core/Signals/signal.h"
40 #include "primitives_array.h"
41 #include "frame_buffer.h"
42 #include "element_array_vector.h"
43 
44 namespace clan
45 {
48 
49 class Size;
50 class Texture;
51 class FrameBuffer;
52 class PixelBuffer;
53 class PrimitivesArray;
54 class Font;
55 class FontMetrics;
56 class GraphicContextProvider;
57 class GraphicContext_Impl;
58 class ProgramObject;
59 class Angle;
60 class RenderBatcher;
61 class FontProvider_Freetype;
62 class UniformBuffer;
63 class StorageBuffer;
64 class RasterizerState;
65 class BlendState;
66 class DepthStencilState;
67 
70 {
74 };
75 
78 {
82 };
83 
86 {
89 };
90 
93 {
102 };
103 
106 {
115 };
116 
119 {
130 };
131 
134 {
151 };
152 
155 {
158 
161 
164 
167 
170 
173 
176 
179 
182 
185 
188 
191 
194 
197 
200 };
201 
204 {
210 };
211 
214 {
217 };
218 
221 {
229 };
230 
233 {
236 };
237 
240 {
245 };
246 
249 {
254 };
255 
258 {
261 
262 public:
264  GraphicContext();
265 
270 
271  ~GraphicContext();
272 
276 public:
278  bool is_null() const { return !impl; }
279 
281  void throw_if_null() const;
282 
285 
288 
291 
296  int get_major_version() const;
297 
302  int get_minor_version() const;
303 
309  bool has_compute_shader_support() const;
310 
316  Texture get_texture(int index) const;
317 
322  std::vector<Texture> get_textures() const;
323 
328 
333 
336 
338  int get_width() const;
339 
341  int get_height() const;
342 
344  Size get_size() const;
345 
348  float get_pixel_ratio() const;
349 
352  float get_dip_width() const { return get_width() / get_pixel_ratio(); }
353 
356  float get_dip_height() const { return get_height() / get_pixel_ratio(); }
357 
361 
366  Size get_max_texture_size() const;
367 
370 
371  const GraphicContextProvider * get_provider() const;
372 
376 public:
378  GraphicContext create() const;
379 
381  GraphicContext create(FrameBuffer &buffer) const;
382 
384  GraphicContext clone() const;
385 
387  PixelBuffer get_pixeldata(const Rect& rect, TextureFormat texture_format = tf_rgba8, bool clamp = true);
388 
390  PixelBuffer get_pixeldata(TextureFormat texture_format = tf_rgba8, bool clamp = true);
391 
399  bool is_frame_buffer_owner(const FrameBuffer &fb);
400 
402  void set_frame_buffer(const FrameBuffer &write_buffer);
403  void set_frame_buffer(const FrameBuffer &write_buffer, const FrameBuffer &read_buffer);
404 
406  void reset_frame_buffer();
407 
409  void set_uniform_buffer(int index, const UniformBuffer &buffer);
410 
412  void reset_uniform_buffer(int index);
413 
415  void set_storage_buffer(int index, const StorageBuffer &buffer);
416 
418  void reset_storage_buffer(int index);
419 
424  void set_texture(int unit_index, const Texture &texture);
425 
431  void set_textures(std::vector<Texture> &textures);
432 
436  void reset_texture(int unit_index);
437 
439  void reset_textures();
440 
445  void set_image_texture(int unit_index, const Texture &texture);
446 
452  void set_image_texture(std::vector<Texture> &textures);
453 
457  void reset_image_texture(int unit_index);
458 
460  void reset_image_textures();
461 
463  void set_rasterizer_state(const RasterizerState &state);
464 
466  void set_blend_state(const BlendState &state, const Colorf &blend_color = Colorf::white, unsigned int sample_mask = 0xffffffff);
467 
469  void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref = 0);
470 
472  void reset_rasterizer_state();
473 
475  void reset_blend_state();
476 
479 
481  void set_program_object(StandardProgram standard_program);
482 
486  void set_program_object(const ProgramObject &program);
487 
489  void reset_program_object();
490 
495  bool is_primitives_array_owner(const PrimitivesArray &primitives_array);
496 
498  void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArray &array);
499 
501  void set_primitives_array(const PrimitivesArray &array);
502 
504  void draw_primitives_array(PrimitivesType type, int num_vertices);
505 
511  void draw_primitives_array(PrimitivesType type, int offset, int num_vertices);
512 
519  void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count);
520 
522  void set_primitives_elements(ElementArrayBuffer &element_array);
523 
525  template<typename Type>
527  {
529  }
530 
537  void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset = 0);
538 
546  void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count);
547 
550 
558  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset = 0);
559 
567  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned int> &element_array, size_t offset = 0)
568  {
569  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_int, offset * sizeof(unsigned int));
570  }
571 
579  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned short> &element_array, size_t offset = 0)
580  {
581  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_short, offset * sizeof(unsigned short));
582  }
583 
591  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned char> &element_array, size_t offset = 0)
592  {
593  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_byte, offset * sizeof(unsigned char));
594  }
595 
604  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset, int instance_count);
605 
614  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned int> &element_array, size_t offset, int instance_count)
615  {
616  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_int, offset * sizeof(unsigned int), instance_count);
617  }
618 
627  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned short> &element_array, size_t offset, int instance_count)
628  {
629  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_short, offset * sizeof(unsigned short), instance_count);
630  }
631 
640  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned char> &element_array, size_t offset, int instance_count)
641  {
642  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_byte, offset * sizeof(unsigned char), instance_count);
643  }
644 
646  void reset_primitives_array();
647 
649  void dispatch(int x = 1, int y = 1, int z = 1);
650 
652  void clear(const Colorf &color = Colorf::black);
653 
657  void clear_stencil(int value = 0);
658 
662  void clear_depth(float value = 0);
663 
665  void set_scissor(const Rect &rect, TextureImageYAxis y_axis);
666 
668  void reset_scissor();
669 
673  void set_viewport(const Rectf &viewport);
674 
679  void set_viewport(int index, const Rectf &viewport);
680 
682  void set_depth_range(float n, float f);
683 
685  void set_depth_range(int viewport, float n, float f);
686 
688  void set_draw_buffer(DrawBuffer buffer);
689 
691  void flush();
692 
696 public:
697 
701 public:
702  bool operator ==(const GraphicContext &other) const { return impl == other.impl; }
703  bool operator !=(const GraphicContext &other) const { return impl != other.impl; }
704 
708 private:
709  std::shared_ptr<GraphicContext_Impl> impl;
710 
711  friend class OpenGL;
713 };
714 
715 const float pixelcenter_constant = 0.375f;
716 
717 }
718 
Definition: graphic_context.h:125
PointSpriteOrigin
Point Sprite Origin.
Definition: graphic_context.h:213
Primitives array description.
Definition: primitives_array.h:68
Definition: graphic_context.h:139
bool operator!=(const GraphicContext &other) const
Definition: graphic_context.h:703
void reset_textures()
Remove all selected textures.
bool is_primitives_array_owner(const PrimitivesArray &primitives_array)
void set_frame_buffer(const FrameBuffer &write_buffer)
Sets the current frame buffer.
void set_viewport(const Rectf &viewport)
void set_scissor(const Rect &rect, TextureImageYAxis y_axis)
Set the current clipping rectangle.
Definition: clanapp.h:35
void reset_storage_buffer(int index)
Remove storage buffer from index.
Floating point color description class (for float).
Definition: color.h:660
void reset_depth_stencil_state()
Set active depth stencil state.
void set_image_texture(int unit_index, const Texture &texture)
Rasterizer state setup.
Definition: rasterizer_state.h:44
Definition: graphic_context.h:143
source or destination (1, 1, 1, 1) - (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:184
source or destination (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:196
Definition: graphic_context.h:122
static Colorf white
Definition: color.h:1251
destination (1, 1, 1, 1) - (Rs, Gs, Bs, As)
Definition: graphic_context.h:172
void set_primitives_elements(ElementArrayBuffer &element_array)
Sets current elements array buffer.
void clear_depth(float value=0)
void reset_image_texture(int unit_index)
Frame-buffer object class.
Definition: frame_buffer.h:72
source (f, f, f, 1) - f = min(As, 1 - Ad)
Definition: graphic_context.h:187
Definition: graphic_context.h:252
Definition: graphic_context.h:205
void throw_if_null() const
Throw an exception if this object is invalid.
ProgramObject get_program_object() const
Returns the currently selected program object.
Definition: graphic_context.h:216
Definition: graphic_context.h:141
FaceSide
Front face modes.
Definition: graphic_context.h:85
Definition: graphic_context.h:137
Definition: graphic_context.h:81
Definition: graphic_context.h:142
Interface for implementing a GraphicContext target.
Definition: graphic_context_provider.h:86
Texture object class.
Definition: texture.h:103
Definition: graphic_context.h:140
void clear_stencil(int value=0)
Definition: graphic_context.h:109
Definition: graphic_context.h:150
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned int > &element_array, size_t offset, int instance_count)
Definition: graphic_context.h:614
Pixel data container.
Definition: pixel_buffer.h:68
Definition: graphic_context.h:107
float get_dip_width() const
Definition: graphic_context.h:352
Blend state setup.
Definition: blend_state.h:44
Definition: graphic_context.h:127
Element Array Vector.
Definition: element_array_vector.h:43
Definition: graphic_context.h:222
BlendEquation
Blending equations.
Definition: graphic_context.h:203
void dispatch(int x=1, int y=1, int z=1)
Execute a compute shader.
Definition: graphic_context.h:101
OpenGL utility class.
Definition: opengl.h:73
void flush()
Flush the command buffer.
OpenGL, origin is lower left with Y going upwards.
Definition: graphic_context.h:234
Definition: graphic_context.h:225
source (1, 1, 1, 1) - (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:169
DrawBuffer
Drawing buffers.
Definition: graphic_context.h:118
Definition: graphic_context.h:207
void set_primitives_elements(ElementArrayVector< Type > &element_array)
Sets current elements array buffer.
Definition: graphic_context.h:526
Direct3D, origin is upper left with Y going downwards.
Definition: graphic_context.h:235
void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArray &array)
Draw primitives on gc.
ShaderLanguage
Shader language used.
Definition: graphic_context.h:248
Definition: graphic_context.h:88
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned char > &element_array, size_t offset=0)
Definition: graphic_context.h:591
void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref=0)
Set active depth stencil state.
Definition: graphic_context.h:87
Definition: graphic_context.h:147
CullMode
Polygon culling modes.
Definition: graphic_context.h:69
void set_textures(std::vector< Texture > &textures)
ClipZRange
Definition: mat4.h:51
Sizef get_dip_size() const
Definition: graphic_context.h:360
Definition: graphic_context.h:71
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned short > &element_array, size_t offset, int instance_count)
Definition: graphic_context.h:627
void reset_rasterizer_state()
Set active rasterizer state.
int get_height() const
Returns the current actual height of the context.
Definition: graphic_context.h:94
Definition: graphic_context.h:108
void draw_primitives_array(PrimitivesType type, int num_vertices)
Draws primitives from the current assigned primitives array.
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.
GraphicContext create() const
Create a new default graphic context compatible with this one.
void reset_scissor()
Removes the set clipping rectangle.
Definition: graphic_context.h:96
void set_storage_buffer(int index, const StorageBuffer &buffer)
Select storage buffer into index.
void reset_texture(int unit_index)
ClipZRange get_clip_z_range() const
Returns in what range clip space z values are clipped.
StandardProgram
Standard Program.
Definition: graphic_context.h:239
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:470
bool is_frame_buffer_owner(const FrameBuffer &fb)
StencilOp
Stencil operations.
Definition: graphic_context.h:105
source or destination (1, 1, 1, 1) - (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:199
void set_draw_buffer(DrawBuffer buffer)
Set used draw buffer.
Definition: graphic_context.h:243
void set_primitives_array(const PrimitivesArray &array)
Set the primitives array on the gc.
void reset_primitives_array()
Reset the primitives arrays.
Definition: graphic_context.h:136
Size get_max_texture_size() const
Definition: graphic_context.h:97
Definition: graphic_context.h:223
void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count)
float get_pixel_ratio() const
Definition: graphic_context.h:113
Definition: graphic_context.h:253
GraphicContext clone() const
Create a new default graphic context cloned with this one.
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:484
Definition: primitives_array.h:58
Definition: graphic_context.h:98
void reset_uniform_buffer(int index)
Remove uniform buffer from index.
FillMode
Polygon filling modes.
Definition: graphic_context.h:77
Definition: graphic_context.h:72
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned short > &element_array, size_t offset=0)
Definition: graphic_context.h:579
Definition: graphic_context.h:250
source or destination (1, 1, 1, 1)
Definition: graphic_context.h:160
Definition: graphic_context.h:124
Definition: graphic_context.h:206
Definition: graphic_context.h:95
Definition: graphic_context.h:228
source or destination (1, 1, 1, 1) - (As, As, As, As)
Definition: graphic_context.h:178
Definition: graphic_context.h:144
TextureFormat
Texture format.
Definition: texture_format.h:40
TextureImageYAxis
Y axis direction for viewports, clipping rects, textures and render targets.
Definition: graphic_context.h:232
const float pixelcenter_constant
Definition: graphic_context.h:715
Definition: primitives_array.h:59
Definition: graphic_context.h:227
GraphicContextProvider * get_provider()
Returns the provider for this graphic context.
Definition: graphic_context.h:126
void set_rasterizer_state(const RasterizerState &state)
Set active rasterizer state.
Definition: graphic_context.h:226
Definition: graphic_context.h:111
Definition: graphic_context.h:145
Definition: graphic_context.h:215
Definition: graphic_context.h:244
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned char > &element_array, size_t offset, int instance_count)
Definition: graphic_context.h:640
source or destination (As, As, As, As)
Definition: graphic_context.h:175
Definition: primitives_array.h:60
void set_program_object(StandardProgram standard_program)
Set active program object to the standard program specified.
Element Array Buffer.
Definition: element_array_buffer.h:46
void reset_image_textures()
Remove all selected textures.
Uniform Buffer.
Definition: uniform_buffer.h:47
Definition: graphic_context.h:149
Interface to drawing graphics.
Definition: graphic_context.h:257
BlendFunc
Blending functions.
Definition: graphic_context.h:154
Definition: graphic_context.h:148
VertexAttributeDataType
Primitives array description.
Definition: primitives_array.h:56
GraphicContext()
Constructs a null instance.
Definition: graphic_context.h:208
source (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:163
int get_width() const
Returns the current actual width of the context.
void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset=0)
void reset_frame_buffer()
Resets the current frame buffer to be the initial frame buffer.
void set_blend_state(const BlendState &state, const Colorf &blend_color=Colorf::white, unsigned int sample_mask=0xffffffff)
Set active blend state.
void reset_program_object()
Remove active program object.
float get_dip_height() const
Definition: graphic_context.h:356
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned int > &element_array, size_t offset=0)
Definition: graphic_context.h:567
Definition: graphic_context.h:209
Program Object.
Definition: program_object.h:69
CompareFunction
Compare functions.
Definition: graphic_context.h:92
source or destination (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:190
ShaderLanguage get_shader_language() const
Returns the shader language used.
Definition: graphic_context.h:79
Definition: graphic_context.h:73
int get_major_version() const
void set_uniform_buffer(int index, const UniformBuffer &buffer)
Select uniform buffer into index.
source or destination (0, 0, 0, 0)
Definition: graphic_context.h:157
void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count)
bool is_null() const
Returns true if this object is invalid.
Definition: graphic_context.h:278
TextureImageYAxis get_texture_image_y_axis() const
Returns the Y axis direction for viewports, clipping rects, textures and render targets.
Definition: graphic_context.h:146
2D (width,height) size structure - Integer
Definition: size.h:156
DepthStencil state setup.
Definition: depth_stencil_state.h:44
static Colorf black
Definition: color.h:843
Definition: graphic_context.h:121
Definition: graphic_context.h:80
Storage Buffer.
Definition: storage_buffer.h:47
void clear(const Colorf &color=Colorf::black)
Clears the whole context using the specified color.
int get_minor_version() const
void reset_blend_state()
Set active blend state.
Definition: graphic_context.h:224
destination (Rs, Gs, Bs, As)
Definition: graphic_context.h:166
LogicOp
Logic Op.
Definition: graphic_context.h:133
void reset_primitives_elements()
Resets current elements array buffer.
PrimitivesType
Primitive types.
Definition: graphic_context.h:220
source or destination (1, 1, 1, 1) - (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:193
Definition: graphic_context.h:100
bool operator==(const GraphicContext &other) const
Definition: graphic_context.h:702
FrameBuffer get_write_frame_buffer() const
Definition: texture_format.h:43
Size get_size() const
Returns the current actual size of the context.
Definition: graphic_context.h:138
Texture get_texture(int index) const
Definition: graphic_context.h:99
Definition: graphic_context.h:114
Definition: graphic_context.h:242
Definition: graphic_context.h:251
Definition: graphic_context.h:120
Definition: graphic_context.h:123
Definition: graphic_context.h:129
void set_texture(int unit_index, const Texture &texture)
Definition: graphic_context.h:110
source or destination (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:181
Definition: graphic_context.h:128
bool has_compute_shader_support() const
std::vector< Texture > get_textures() const
2D (width,height) size structure - Float
Definition: size.h:169
FrameBuffer get_read_frame_buffer() const
Definition: graphic_context.h:241
Definition: graphic_context.h:112
void set_depth_range(float n, float f)
Specifies the depth range for all viewports.
Definition: graphic_context.h:135