graphic_context.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore 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 UICore 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 "../../Core/Math/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 #include "shader_object.h"
43 #include "program_object.h"
44 
45 namespace uicore
46 {
47  class Size;
48  class Texture;
49  class FrameBuffer;
50  class PixelBuffer;
51  class PrimitivesArray;
52  class Font;
53  class FontMetrics;
54  class GraphicContextImpl;
55  class GraphicContext_Impl;
56  class ProgramObject;
57  class FontProvider_Freetype;
58  class UniformBuffer;
59  class StorageBuffer;
60  class RasterizerStateDescription;
61  class BlendStateDescription;
63  typedef std::shared_ptr<ProgramObject> ProgramObjectPtr;
64  typedef std::shared_ptr<FrameBuffer> FrameBufferPtr;
65  typedef std::shared_ptr<StorageBuffer> StorageBufferPtr;
66  typedef std::shared_ptr<UniformBuffer> UniformBufferPtr;
67  typedef std::shared_ptr<PrimitivesArray> PrimitivesArrayPtr;
68  typedef std::shared_ptr<Texture> TexturePtr;
69  typedef std::shared_ptr<PixelBuffer> PixelBufferPtr;
70 
72  enum CullMode
73  {
77  };
78 
80  enum FillMode
81  {
85  };
86 
88  enum FaceSide
89  {
92  };
93 
96  {
105  };
106 
109  {
118  };
119 
122  {
133  };
134 
136  enum LogicOp
137  {
154  };
155 
158  {
161 
164 
167 
170 
173 
176 
179 
182 
185 
188 
191 
194 
197 
200 
203  };
204 
207  {
213  };
214 
217  {
220  };
221 
224  {
232  };
233 
236  {
239  };
240 
243  {
248  };
249 
252  {
257  };
258 
259  class RasterizerState { };
260  typedef std::shared_ptr<RasterizerState> RasterizerStatePtr;
261 
262  class BlendState { };
263  typedef std::shared_ptr<BlendState> BlendStatePtr;
264 
265  class DepthStencilState { };
266  typedef std::shared_ptr<DepthStencilState> DepthStencilStatePtr;
267 
270  {
271  public:
273  virtual ClipZRange clip_z_range() const = 0;
274 
276  virtual TextureImageYAxis texture_image_y_axis() const = 0;
277 
279  virtual ShaderLanguage shader_language() const = 0;
280 
285  virtual int major_version() const = 0;
286 
291  virtual int minor_version() const = 0;
292 
298  virtual bool has_compute_shader_support() const = 0;
299 
303  virtual FrameBufferPtr write_frame_buffer() const = 0;
304 
308  virtual FrameBufferPtr read_frame_buffer() const = 0;
309 
311  virtual ProgramObjectPtr program_object() const = 0;
312 
314  int width() const { return size().width; }
315 
317  int height() const { return size().height; }
318 
320  virtual Size size() const = 0;
321 
324  virtual float pixel_ratio() const = 0;
325 
328  float dip_width() const { return width() / pixel_ratio(); }
329 
332  float dip_height() const { return height() / pixel_ratio(); }
333 
336  Sizef dip_size() const { return Sizef{ dip_width(), dip_height() }; }
337 
342  virtual Size max_texture_size() const = 0;
343 
345  virtual PixelBufferPtr pixeldata(const Rect& rect, TextureFormat texture_format = tf_rgba8, bool clamp = true) const = 0;
346 
348  PixelBufferPtr pixeldata(TextureFormat texture_format = tf_rgba8, bool clamp = true) { return pixeldata(Rect(Point(), size()), texture_format, clamp); }
349 
357  virtual bool is_frame_buffer_owner(const FrameBufferPtr &fb) = 0;
358 
360  virtual std::shared_ptr<RasterizerState> create_rasterizer_state(const RasterizerStateDescription &desc) = 0;
361 
363  virtual std::shared_ptr<BlendState> create_blend_state(const BlendStateDescription &desc) = 0;
364 
366  virtual std::shared_ptr<DepthStencilState> create_depth_stencil_state(const DepthStencilStateDescription &desc) = 0;
367 
369  void set_frame_buffer(const FrameBufferPtr &write_buffer) { set_frame_buffer(write_buffer, write_buffer); }
370  virtual void set_frame_buffer(const FrameBufferPtr &write_buffer, const FrameBufferPtr &read_buffer) = 0;
371 
374 
376  virtual void set_uniform_buffer(int index, const UniformBufferPtr &buffer) = 0;
377 
379  void reset_uniform_buffer(int index) { set_uniform_buffer(index, nullptr); }
380 
382  virtual void set_storage_buffer(int index, const StorageBufferPtr &buffer) = 0;
383 
385  void reset_storage_buffer(int index) { set_storage_buffer(index, nullptr); }
386 
391  virtual void set_texture(int unit_index, const TexturePtr &texture) = 0;
392 
397  void set_textures(const std::vector<TexturePtr> &textures) { set_textures(0, textures); }
398  void set_textures(int start_index, const std::vector<TexturePtr> &textures) { for (int i = 0; i < (int)textures.size(); i++) set_texture(start_index + i, textures[i]); }
399 
403  void reset_texture(int unit_index) { set_texture(unit_index, nullptr); }
404 
406  void reset_textures(int start_index, size_t count) { for (int i = 0; i < (int)count; i++) set_texture(start_index + i, nullptr); }
407 
412  virtual void set_image_texture(int unit_index, const TexturePtr &texture) = 0;
413 
420  void set_image_textures(const std::vector<TexturePtr> &textures) { set_image_textures(0, textures); }
421  void set_image_textures(int start_index, const std::vector<TexturePtr> &textures) { for (int i = 0; i < (int)textures.size(); i++) set_image_texture(start_index + i, textures[i]); }
422 
426  void reset_image_texture(int unit_index) { set_image_texture(unit_index, nullptr); }
427 
429  void reset_image_textures(int start_index, size_t count) { for (int i = 0; i < (int)count; i++) set_image_texture(start_index + i, nullptr); }
430 
432  virtual void set_rasterizer_state(const RasterizerStatePtr &state) = 0;
433 
435  virtual void set_blend_state(const BlendStatePtr &state, const Colorf &blend_color = Colorf::white, unsigned int sample_mask = 0xffffffff) = 0;
436 
438  virtual void set_depth_stencil_state(const DepthStencilStatePtr &state, int stencil_ref = 0) = 0;
439 
442 
444  void reset_blend_state() { set_blend_state(nullptr); }
445 
448 
450  virtual void set_program_object(StandardProgram standard_program) = 0;
451 
453  virtual void set_program_object(const ProgramObjectPtr &program) = 0;
454 
457 
462  virtual bool is_primitives_array_owner(const PrimitivesArrayPtr &primitives_array) = 0;
463 
465  virtual void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArrayPtr &array) = 0;
466 
468  virtual void set_primitives_array(const PrimitivesArrayPtr &array) = 0;
469 
471  void draw_primitives_array(PrimitivesType type, int num_vertices) { draw_primitives_array(type, 0, num_vertices); }
472 
478  virtual void draw_primitives_array(PrimitivesType type, int offset, int num_vertices) = 0;
479 
486  virtual void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count) = 0;
487 
489  virtual void set_primitives_elements(const ElementArrayBufferPtr &element_array) = 0;
490 
497  virtual void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset = 0) = 0;
498 
506  virtual void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count) = 0;
507 
510 
518  virtual void draw_primitives_elements(PrimitivesType type, int count, const ElementArrayBufferPtr &element_array, VertexAttributeDataType indices_type, size_t offset = 0) = 0;
519 
528  virtual void draw_primitives_elements_instanced(PrimitivesType type, int count, const ElementArrayBufferPtr &element_array, VertexAttributeDataType indices_type, size_t offset, int instance_count) = 0;
529 
532 
534  virtual void dispatch(int x = 1, int y = 1, int z = 1) = 0;
535 
537  virtual void clear(const Colorf &color = Colorf::black) = 0;
538 
542  virtual void clear_stencil(int value = 0) = 0;
543 
547  virtual void clear_depth(float value = 0) = 0;
548 
550  virtual void set_scissor(const Rect &rect, TextureImageYAxis y_axis) = 0;
551 
553  virtual void reset_scissor() = 0;
554 
558  virtual void set_viewport(const Rectf &viewport, TextureImageYAxis y_axis) = 0;
559 
564  virtual void set_viewport(int index, const Rectf &viewport, TextureImageYAxis y_axis) = 0;
565 
567  void set_depth_range(float n, float f) { set_depth_range(-1, n, f); }
568 
570  virtual void set_depth_range(int viewport, float n, float f) = 0;
571 
573  virtual void set_draw_buffer(DrawBuffer buffer) = 0;
574 
576  virtual void flush() = 0;
577  };
578 
579  typedef std::shared_ptr<GraphicContext> GraphicContextPtr;
580 }
virtual void set_depth_stencil_state(const DepthStencilStatePtr &state, int stencil_ref=0)=0
Set active depth stencil state.
source (1, 1, 1, 1) - (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:172
virtual bool has_compute_shader_support() const =0
PrimitivesType
Primitive types.
Definition: graphic_context.h:223
Definition: graphic_context.h:143
virtual void set_primitives_elements(const ElementArrayBufferPtr &element_array)=0
Sets current elements array buffer.
Direct3D, origin is upper left with Y going downwards.
Definition: graphic_context.h:238
void reset_image_textures(int start_index, size_t count)
Remove all selected textures.
Definition: graphic_context.h:429
source or destination (1, 1, 1, 1)
Definition: graphic_context.h:163
void set_textures(const std::vector< TexturePtr > &textures)
Definition: graphic_context.h:397
float dip_width() const
Definition: graphic_context.h:328
source (Rd, Gd, Bd, Ad)
Definition: graphic_context.h:166
PixelBufferPtr pixeldata(TextureFormat texture_format=tf_rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
Definition: graphic_context.h:348
Definition: graphic_context.h:75
virtual bool is_frame_buffer_owner(const FrameBufferPtr &fb)=0
Definition: graphic_context.h:139
Rasterizer state description.
Definition: rasterizer_state_description.h:38
2D (width,height) size structure - Integer
Definition: size.h:167
void reset_depth_stencil_state()
Set active depth stencil state.
Definition: graphic_context.h:447
Definition: graphic_context.h:245
void set_image_textures(const std::vector< TexturePtr > &textures)
Definition: graphic_context.h:420
Definition: graphic_context.h:219
Definition: graphic_context.h:130
Definition: graphic_context.h:116
std::shared_ptr< Texture > TexturePtr
Definition: d3d_target.h:37
Definition: graphic_context.h:117
Definition: graphic_context.h:227
void reset_blend_state()
Set active blend state.
Definition: graphic_context.h:444
virtual void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset=0)=0
Sizef dip_size() const
Definition: graphic_context.h:336
virtual FrameBufferPtr write_frame_buffer() const =0
Definition: graphic_context.h:208
static Colorf white
Definition: color.h:1207
Definition: graphic_context.h:125
virtual std::shared_ptr< BlendState > create_blend_state(const BlendStateDescription &desc)=0
Create blend state object.
void set_textures(int start_index, const std::vector< TexturePtr > &textures)
Definition: graphic_context.h:398
BlendFunc
Blending functions.
Definition: graphic_context.h:157
Definition: graphic_context.h:127
virtual std::shared_ptr< DepthStencilState > create_depth_stencil_state(const DepthStencilStateDescription &desc)=0
Create depth-stencil state object.
Definition: graphic_context.h:99
Blend state description.
Definition: blend_state_description.h:38
virtual ClipZRange clip_z_range() const =0
Returns in what range clip space z values are clipped.
int width() const
Returns the current actual width of the context.
Definition: graphic_context.h:314
std::shared_ptr< StorageBuffer > StorageBufferPtr
Definition: graphic_context.h:65
Definition: graphic_context.h:228
std::shared_ptr< BlendState > BlendStatePtr
Definition: canvas.h:45
FaceSide
Front face modes.
Definition: graphic_context.h:88
void reset_texture(int unit_index)
Definition: graphic_context.h:403
Definition: graphic_context.h:141
FillMode
Polygon filling modes.
Definition: graphic_context.h:80
Definition: graphic_context.h:265
Definition: graphic_context.h:225
Floating point color description class (for float).
Definition: color.h:630
Definition: graphic_context.h:114
virtual void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count)=0
ShaderLanguage
Shader language used.
Definition: graphic_context.h:251
virtual float pixel_ratio() const =0
Definition: graphic_context.h:145
Definition: graphic_context.h:259
virtual void clear(const Colorf &color=Colorf::black)=0
Clears the whole context using the specified color.
Definition: graphic_context.h:111
CompareFunction
Compare functions.
Definition: graphic_context.h:95
virtual void clear_depth(float value=0)=0
virtual void set_program_object(StandardProgram standard_program)=0
Set active program object to the standard program specified.
Definition: graphic_context.h:212
Definition: graphic_context.h:144
Definition: graphic_context.h:246
static Colorf black
Definition: color.h:799
virtual void flush()=0
Flush the command buffer.
ClipZRange
Definition: mat4.h:47
virtual void set_uniform_buffer(int index, const UniformBufferPtr &buffer)=0
Select uniform buffer into index.
virtual FrameBufferPtr read_frame_buffer() const =0
TextureFormat
Texture format.
Definition: texture_format.h:35
virtual void set_texture(int unit_index, const TexturePtr &texture)=0
Definition: texture_format.h:38
virtual void set_primitives_array(const PrimitivesArrayPtr &array)=0
Set the primitives array on the gc.
void reset_frame_buffer()
Resets the current frame buffer to be the initial frame buffer.
Definition: graphic_context.h:373
std::shared_ptr< RasterizerState > RasterizerStatePtr
Definition: graphic_context.h:260
source or destination (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:184
float dip_height() const
Definition: graphic_context.h:332
Definition: graphic_context.h:104
Definition: graphic_context.h:126
std::shared_ptr< ElementArrayBuffer > ElementArrayBufferPtr
Definition: d3d_target.h:43
Definition: graphic_context.h:98
virtual ProgramObjectPtr program_object() const =0
Returns the currently selected program object.
void reset_textures(int start_index, size_t count)
Remove all selected textures.
Definition: graphic_context.h:406
Definition: graphic_context.h:151
virtual ShaderLanguage shader_language() const =0
Returns the shader language used.
2D (x,y) point structure - Integer
Definition: point.h:58
Definition: graphic_context.h:131
source or destination (1, 1, 1, 1) - (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:202
Definition: graphic_context.h:147
TextureImageYAxis
Y axis direction for viewports, clipping rects, textures and render targets.
Definition: graphic_context.h:235
destination (Rs, Gs, Bs, As)
Definition: graphic_context.h:169
Definition: graphic_context.h:253
Definition: graphic_context.h:218
virtual TextureImageYAxis texture_image_y_axis() const =0
Returns the Y axis direction for viewports, clipping rects, textures and render targets.
destination (1, 1, 1, 1) - (Rs, Gs, Bs, As)
Definition: graphic_context.h:175
Definition: graphic_context.h:124
virtual int major_version() const =0
Definition: graphic_context.h:83
2D (width,height) size structure - Float
Definition: size.h:180
virtual void set_viewport(const Rectf &viewport, TextureImageYAxis y_axis)=0
Type height
Size height.
Definition: size.h:74
Definition: graphic_context.h:132
virtual void set_scissor(const Rect &rect, TextureImageYAxis y_axis)=0
Set the current clipping rectangle.
virtual void reset_scissor()=0
Removes the set clipping rectangle.
Definition: graphic_context.h:254
Definition: graphic_context.h:230
void reset_primitives_array()
Reset the primitives arrays.
Definition: graphic_context.h:531
2D (left,top,right,bottom) rectangle structure - Float
Definition: rect.h:505
void reset_primitives_elements()
Resets current elements array buffer.
Definition: graphic_context.h:509
virtual void set_storage_buffer(int index, const StorageBufferPtr &buffer)=0
Select storage buffer into index.
virtual void set_rasterizer_state(const RasterizerStatePtr &state)=0
Set active rasterizer state.
Definition: graphic_context.h:91
Definition: graphic_context.h:103
std::shared_ptr< UniformBuffer > UniformBufferPtr
Definition: d3d_target.h:47
std::shared_ptr< FrameBuffer > FrameBufferPtr
Definition: frame_buffer.h:132
Definition: graphic_context.h:97
Definition: graphic_context.h:244
LogicOp
Logic Op.
Definition: graphic_context.h:136
2D (left,top,right,bottom) rectangle structure - Integer
Definition: rect.h:491
Definition: graphic_context.h:101
source or destination (1, 1, 1, 1) - (As, As, As, As)
Definition: graphic_context.h:181
Type width
Size width.
Definition: size.h:71
source (f, f, f, 1) - f = min(As, 1 - Ad)
Definition: graphic_context.h:190
virtual void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArrayPtr &array)=0
Draw primitives on gc.
Definition: graphic_context.h:74
Definition: graphic_context.h:84
source or destination (1, 1, 1, 1) - (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:196
virtual PixelBufferPtr pixeldata(const Rect &rect, TextureFormat texture_format=tf_rgba8, bool clamp=true) const =0
Return the content of the read buffer into a pixel buffer.
virtual void set_draw_buffer(DrawBuffer buffer)=0
Set used draw buffer.
std::shared_ptr< ProgramObject > ProgramObjectPtr
Definition: graphic_context.h:62
Definition: graphic_context.h:140
void reset_rasterizer_state()
Set active rasterizer state.
Definition: graphic_context.h:441
CullMode
Polygon culling modes.
Definition: graphic_context.h:72
Definition: graphic_context.h:90
Definition: graphic_context.h:128
void reset_image_texture(int unit_index)
Definition: graphic_context.h:426
Definition: graphic_context.h:100
Definition: graphic_context.h:211
Definition: graphic_context.h:153
Definition: graphic_context.h:148
void set_frame_buffer(const FrameBufferPtr &write_buffer)
Sets the current frame buffer.
Definition: graphic_context.h:369
void set_depth_range(float n, float f)
Specifies the depth range for all viewports.
Definition: graphic_context.h:567
std::shared_ptr< DepthStencilState > DepthStencilStatePtr
Definition: graphic_context.h:266
DepthStencil state description.
Definition: depth_stencil_state_description.h:38
source or destination (1, 1, 1, 1) - (Ad, Ad, Ad, Ad)
Definition: graphic_context.h:187
Definition: graphic_context.h:112
std::shared_ptr< PixelBuffer > PixelBufferPtr
Definition: d3d_target.h:39
std::shared_ptr< GraphicContext > GraphicContextPtr
Definition: d3d_target.h:49
source or destination (Ac, Ac, Ac, Ac)
Definition: graphic_context.h:199
Definition: graphic_context.h:247
void draw_primitives_array(PrimitivesType type, int num_vertices)
Draws primitives from the current assigned primitives array.
Definition: graphic_context.h:471
Definition: graphic_context.h:102
Definition: graphic_context.h:226
source or destination (0, 0, 0, 0)
Definition: graphic_context.h:160
Definition: graphic_context.h:255
int height() const
Returns the current actual height of the context.
Definition: graphic_context.h:317
std::shared_ptr< PrimitivesArray > PrimitivesArrayPtr
Definition: graphic_context.h:67
Definition: graphic_context.h:82
Definition: graphic_context.h:229
VertexAttributeDataType
Primitives array description.
Definition: primitives_array.h:48
Definition: graphic_context.h:146
Interface to drawing graphics.
Definition: graphic_context.h:269
void reset_program_object()
Remove active program object.
Definition: graphic_context.h:456
Definition: graphic_context.h:110
Definition: graphic_context.h:150
void reset_storage_buffer(int index)
Remove storage buffer from index.
Definition: graphic_context.h:385
Definition: graphic_context.h:231
source or destination (Rc, Gc, Bc, Ac)
Definition: graphic_context.h:193
Definition: graphic_context.h:76
PointSpriteOrigin
Point Sprite Origin.
Definition: graphic_context.h:216
Definition: graphic_context.h:115
virtual void set_image_texture(int unit_index, const TexturePtr &texture)=0
virtual bool is_primitives_array_owner(const PrimitivesArrayPtr &primitives_array)=0
Definition: graphic_context.h:210
Definition: graphic_context.h:152
OpenGL, origin is lower left with Y going upwards.
Definition: graphic_context.h:237
virtual int minor_version() const =0
virtual void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count)=0
Definition: graphic_context.h:209
source or destination (As, As, As, As)
Definition: graphic_context.h:178
virtual void dispatch(int x=1, int y=1, int z=1)=0
Execute a compute shader.
Definition: Application/application.h:35
void reset_uniform_buffer(int index)
Remove uniform buffer from index.
Definition: graphic_context.h:379
virtual void clear_stencil(int value=0)=0
StencilOp
Stencil operations.
Definition: graphic_context.h:108
virtual Size max_texture_size() const =0
virtual Size size() const =0
Returns the current actual size of the context.
Definition: graphic_context.h:149
Definition: graphic_context.h:142
BlendEquation
Blending equations.
Definition: graphic_context.h:206
Definition: graphic_context.h:113
Definition: graphic_context.h:123
Definition: graphic_context.h:256
Definition: graphic_context.h:129
Definition: graphic_context.h:138
StandardProgram
Standard Program.
Definition: graphic_context.h:242
void set_image_textures(int start_index, const std::vector< TexturePtr > &textures)
Definition: graphic_context.h:421
DrawBuffer
Drawing buffers.
Definition: graphic_context.h:121
virtual void set_blend_state(const BlendStatePtr &state, const Colorf &blend_color=Colorf::white, unsigned int sample_mask=0xffffffff)=0
Set active blend state.
virtual std::shared_ptr< RasterizerState > create_rasterizer_state(const RasterizerStateDescription &desc)=0
Create rasterizer state object.
Definition: graphic_context.h:262