graphic_context.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 "../api_display.h"
35 #include "../2D/color.h"
36 #include "../Image/texture_format.h"
37 #include <memory>
38 #include "../../Core/Math/mat4.h"
39 #include "../../Core/Math/rect.h"
40 #include "../../Core/Signals/signal_v0.h"
41 #include "primitives_array.h"
42 #include "frame_buffer.h"
43 #include "element_array_vector.h"
44 
45 namespace clan
46 {
49 
50 class Size;
51 class Texture;
52 class FrameBuffer;
53 class PixelBuffer;
54 class PrimitivesArray;
55 class Font;
56 class FontMetrics;
57 class GraphicContextProvider;
58 class GraphicContext_Impl;
59 class ProgramObject;
60 class Angle;
61 class RenderBatcher;
62 class FontProvider_Freetype;
63 class UniformBuffer;
64 class StorageBuffer;
65 class RasterizerState;
66 class BlendState;
67 class DepthStencilState;
68 
71 {
75 };
76 
79 {
83 };
84 
87 {
90 };
91 
94 {
103 };
104 
107 {
116 };
117 
120 {
131 };
132 
135 {
152 };
153 
156 {
159 
162 
165 
168 
171 
174 
177 
180 
183 
186 
189 
192 
195 
198 
201 };
202 
205 {
211 };
212 
215 {
218 };
219 
222 {
230 };
231 
234 {
235  y_axis_bottom_up, // OpenGL, origin is lower left with Y going upwards
236  y_axis_top_down // Direct3D, origin is upper left with Y going downwards
237 };
238 
241 {
245 };
246 
249 {
254 };
255 
257 class CL_API_DISPLAY GraphicContext
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 
284  ClipZRange get_clip_z_range() const;
285 
287  TextureImageYAxis get_texture_image_y_axis() const;
288 
290  ShaderLanguage get_shader_language() const;
291 
296  int get_major_version() const;
297 
302  int get_minor_version() const;
303 
308  bool has_compute_shader_support() const;
309 
314  Texture get_texture(int index) const;
315 
319  std::vector<Texture> get_textures() const;
320 
324  FrameBuffer get_write_frame_buffer() const;
325 
329  FrameBuffer get_read_frame_buffer() const;
330 
332  int get_width() const;
333 
335  int get_height() const;
336 
338  Size get_size() const;
339 
341 
343  Size get_max_texture_size() const;
344 
346  GraphicContextProvider *get_provider();
347 
348  const GraphicContextProvider * const get_provider() const;
349 
353 public:
355  GraphicContext create() const;
356 
358  GraphicContext create(FrameBuffer &buffer) const;
359 
361  GraphicContext clone() const;
362 
364  PixelBuffer get_pixeldata(const Rect& rect, TextureFormat texture_format = tf_rgba8, bool clamp = true);
365 
367  PixelBuffer get_pixeldata(TextureFormat texture_format = tf_rgba8, bool clamp = true);
368 
373  bool is_frame_buffer_owner(const FrameBuffer &fb);
374 
376  void set_frame_buffer(const FrameBuffer &write_buffer);
377  void set_frame_buffer(const FrameBuffer &write_buffer, const FrameBuffer &read_buffer);
378 
380  void reset_frame_buffer();
381 
383  void set_uniform_buffer(int index, const UniformBuffer &buffer);
384 
386  void reset_uniform_buffer(int index);
387 
389  void set_storage_buffer(int index, const StorageBuffer &buffer);
390 
392  void reset_storage_buffer(int index);
393 
398  void set_texture(int unit_index, const Texture &texture);
399 
405  void set_textures(std::vector<Texture> &textures);
406 
410  void reset_texture(int unit_index);
411 
413  void reset_textures();
414 
419  void set_image_texture(int unit_index, const Texture &texture);
420 
426  void set_image_texture(std::vector<Texture> &textures);
427 
431  void reset_image_texture(int unit_index);
432 
434  void reset_image_textures();
435 
437  void set_rasterizer_state(const RasterizerState &state);
438 
440  void set_blend_state(const BlendState &state, const Colorf &blend_color = Colorf::white, unsigned int sample_mask = 0xffffffff);
441 
443  void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref = 0);
444 
446  void reset_rasterizer_state();
447 
449  void reset_blend_state();
450 
452  void reset_depth_stencil_state();
453 
455  void set_program_object(StandardProgram standard_program);
456 
460  void set_program_object(const ProgramObject &program);
461 
463  void reset_program_object();
464 
469  bool is_primitives_array_owner(const PrimitivesArray &primitives_array);
470 
472  void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArray &array);
473 
475  void set_primitives_array(const PrimitivesArray &array);
476 
478  void draw_primitives_array(PrimitivesType type, int num_vertices);
479 
485  void draw_primitives_array(PrimitivesType type, int offset, int num_vertices);
486 
493  void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count);
494 
496  void set_primitives_elements(ElementArrayBuffer &element_array);
497 
499  template<typename Type>
501  {
502  set_primitives_elements((ElementArrayBuffer&)element_array);
503  }
504 
511  void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset = 0);
512 
520  void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count);
521 
523  void reset_primitives_elements();
524 
532  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset = 0);
533 
541  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned int> &element_array, size_t offset = 0)
542  {
543  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_int, offset * sizeof(unsigned int));
544  }
545 
553  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned short> &element_array, size_t offset = 0)
554  {
555  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_short, offset * sizeof(unsigned short));
556  }
557 
565  void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector<unsigned char> &element_array, size_t offset = 0)
566  {
567  draw_primitives_elements(type, count, (ElementArrayBuffer&)element_array, type_unsigned_byte, offset * sizeof(unsigned char));
568  }
569 
578  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset, int instance_count);
579 
588  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned int> &element_array, size_t offset, int instance_count)
589  {
590  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_int, offset * sizeof(unsigned int), instance_count);
591  }
592 
601  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned short> &element_array, size_t offset, int instance_count)
602  {
603  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_short, offset * sizeof(unsigned short), instance_count);
604  }
605 
614  void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector<unsigned char> &element_array, size_t offset, int instance_count)
615  {
616  draw_primitives_elements_instanced(type, count, (ElementArrayBuffer&)element_array, type_unsigned_byte, offset * sizeof(unsigned char), instance_count);
617  }
618 
620  void reset_primitives_array();
621 
623  void dispatch(int x = 1, int y = 1, int z = 1);
624 
626  void clear(const Colorf &color = Colorf::black);
627 
631  void clear_stencil(int value = 0);
632 
636  void clear_depth(float value = 0);
637 
639  void set_scissor(const Rect &rect, TextureImageYAxis y_axis);
640 
642  void reset_scissor();
643 
647  void set_viewport(const Rectf &viewport);
648 
653  void set_viewport(int index, const Rectf &viewport);
654 
656  void set_depth_range(float n, float f);
657 
659  void set_depth_range(int viewport, float n, float f);
660 
662  void set_draw_buffer(DrawBuffer buffer);
663 
667 public:
668 
672 public:
673  bool operator ==(const GraphicContext &other) const { return impl == other.impl; }
674  bool operator !=(const GraphicContext &other) const { return impl != other.impl; }
675 
679 private:
680  std::shared_ptr<GraphicContext_Impl> impl;
681 
682  friend class OpenGL;
684 };
685 
686 const float pixelcenter_constant = 0.375f;
687 
688 }
689 
Definition: graphic_context.h:126
PointSpriteOrigin
Point Sprite Origin.
Definition: graphic_context.h:214
Primitives array description.
Definition: primitives_array.h:69
Definition: graphic_context.h:140
Floating point color description class (for float).
Definition: color.h:661
Rasterizer state setup.
Definition: rasterizer_state.h:45
Definition: graphic_context.h:144
source or destination (1, 1, 1, 1) - (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:185
source or destination (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:197
Definition: graphic_context.h:123
static Colorf white
Definition: color.h:1245
destination (1, 1, 1, 1) - (Rs, Gs, Bs, As)
Definition: graphic_context.h:173
Frame-buffer object class.
Definition: frame_buffer.h:73
source (f, f, f, 1) - f = min(As, 1 - Ad)
Definition: graphic_context.h:188
Definition: graphic_context.h:252
Definition: graphic_context.h:206
Definition: graphic_context.h:217
Definition: graphic_context.h:142
FaceSide
Front face modes.
Definition: graphic_context.h:86
Definition: graphic_context.h:138
Definition: graphic_context.h:82
Definition: graphic_context.h:143
Interface for implementing a GraphicContext target.
Definition: graphic_context_provider.h:87
Texture object class.
Definition: texture.h:104
Definition: graphic_context.h:141
Definition: graphic_context.h:110
Definition: graphic_context.h:151
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned int > &element_array, size_t offset, int instance_count)
Draw primitives elements instanced.
Definition: graphic_context.h:588
Pixel data container.
Definition: pixel_buffer.h:69
Definition: graphic_context.h:108
Blend state setup.
Definition: blend_state.h:45
Definition: graphic_context.h:128
Element Array Vector.
Definition: element_array_vector.h:43
Definition: graphic_context.h:223
BlendEquation
Blending equations.
Definition: graphic_context.h:204
Definition: graphic_context.h:102
OpenGL utility class.
Definition: opengl.h:64
Definition: graphic_context.h:235
Definition: graphic_context.h:226
source (1, 1, 1, 1) - (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:170
DrawBuffer
Drawing buffers.
Definition: graphic_context.h:119
Definition: graphic_context.h:208
void set_primitives_elements(ElementArrayVector< Type > &element_array)
Sets current elements array buffer.
Definition: graphic_context.h:500
Definition: graphic_context.h:236
ShaderLanguage
Shader language used.
Definition: graphic_context.h:248
Definition: graphic_context.h:89
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned char > &element_array, size_t offset=0)
Draw primitives elements.
Definition: graphic_context.h:565
Definition: graphic_context.h:88
Definition: graphic_context.h:148
CullMode
Polygon culling modes.
Definition: graphic_context.h:70
ClipZRange
Definition: mat4.h:52
Definition: graphic_context.h:72
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned short > &element_array, size_t offset, int instance_count)
Draw primitives elements instanced.
Definition: graphic_context.h:601
Definition: graphic_context.h:95
Definition: graphic_context.h:109
Definition: graphic_context.h:97
StandardProgram
Standard Program.
Definition: graphic_context.h:240
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:453
StencilOp
Stencil operations.
Definition: graphic_context.h:106
source or destination (1, 1, 1, 1) - (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:200
Definition: graphic_context.h:244
Definition: graphic_context.h:137
Definition: graphic_context.h:98
Definition: graphic_context.h:224
Definition: graphic_context.h:114
Definition: graphic_context.h:253
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:467
Definition: primitives_array.h:59
Definition: graphic_context.h:99
FillMode
Polygon filling modes.
Definition: graphic_context.h:78
Definition: graphic_context.h:73
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned short > &element_array, size_t offset=0)
Draw primitives elements.
Definition: graphic_context.h:553
bool is_null() const
Returns true if this object is invalid.
Definition: graphic_context.h:278
Definition: graphic_context.h:250
source or destination (1, 1, 1, 1)
Definition: graphic_context.h:161
Definition: graphic_context.h:125
Definition: graphic_context.h:207
Definition: graphic_context.h:96
Definition: graphic_context.h:229
source or destination (1, 1, 1, 1) - (As, As, As, As)
Definition: graphic_context.h:179
Definition: graphic_context.h:145
TextureFormat
Texture format.
Definition: texture_format.h:41
TextureImageYAxis
Y axis direction for viewports, clipping rects, textures and render targets.
Definition: graphic_context.h:233
const float pixelcenter_constant
Definition: graphic_context.h:686
Definition: primitives_array.h:60
Definition: graphic_context.h:228
Definition: graphic_context.h:127
Definition: graphic_context.h:227
Definition: graphic_context.h:112
Definition: graphic_context.h:146
Definition: graphic_context.h:216
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayVector< unsigned char > &element_array, size_t offset, int instance_count)
Draw primitives elements instanced.
Definition: graphic_context.h:614
source or destination (As, As, As, As)
Definition: graphic_context.h:176
Definition: primitives_array.h:61
Element Array Buffer.
Definition: element_array_buffer.h:47
Uniform Buffer.
Definition: uniform_buffer.h:48
Definition: graphic_context.h:150
Interface to drawing graphics.
Definition: graphic_context.h:257
BlendFunc
Blending functions.
Definition: graphic_context.h:155
Definition: graphic_context.h:149
VertexAttributeDataType
Primitives array description.
Definition: primitives_array.h:57
Definition: graphic_context.h:209
source (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:164
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned int > &element_array, size_t offset=0)
Draw primitives elements.
Definition: graphic_context.h:541
Definition: graphic_context.h:210
Program Object.
Definition: program_object.h:70
CompareFunction
Compare functions.
Definition: graphic_context.h:93
source or destination (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:191
Definition: graphic_context.h:80
Definition: graphic_context.h:74
source or destination (0, 0, 0, 0)
Definition: graphic_context.h:158
Definition: graphic_context.h:147
2D (width,height) size structure - Integer
Definition: size.h:157
DepthStencil state setup.
Definition: depth_stencil_state.h:45
static Colorf black
Definition: color.h:837
Definition: graphic_context.h:122
Definition: graphic_context.h:81
Storage Buffer.
Definition: storage_buffer.h:48
Definition: graphic_context.h:225
destination (Rs, Gs, Bs, As)
Definition: graphic_context.h:167
LogicOp
Logic Op.
Definition: graphic_context.h:134
PrimitivesType
Primitive types.
Definition: graphic_context.h:221
source or destination (1, 1, 1, 1) - (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:194
Definition: graphic_context.h:101
Definition: texture_format.h:44
Definition: graphic_context.h:139
Definition: graphic_context.h:100
Definition: graphic_context.h:115
Definition: graphic_context.h:243
Definition: graphic_context.h:251
Definition: graphic_context.h:121
Definition: graphic_context.h:124
Definition: graphic_context.h:130
Definition: graphic_context.h:111
source or destination (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:182
Definition: graphic_context.h:129
C clamp(A val, B minval, C maxval)
Definition: cl_math.h:91
Definition: graphic_context.h:242
Definition: graphic_context.h:113
Definition: graphic_context.h:136