graphic_context.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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
43namespace 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 class CullMode
69 {
70 front,
71 back,
73 };
74
76 enum class FillMode
77 {
78 point,
79 line,
81 };
82
84 enum class FaceSide
85 {
88 };
89
91 enum class CompareFunction
92 {
93 lequal,
94 gequal,
95 less,
96 greater,
97 equal,
99 always,
100 never
101 };
102
104 enum class StencilOp
105 {
106 keep,
107 zero,
108 replace,
109 incr,
110 decr,
111 invert,
112 incr_wrap,
114 };
115
117 enum class DrawBuffer
118 {
119 none,
122 back_left,
124 front,
125 back,
126 left,
127 right,
129 };
130
132 enum class LogicOp
133 {
134 _clear,
135 _and,
137 _copy,
139 _noop,
140 _xor,
141 _or,
142 _nor,
143 _equiv,
144 _invert,
148 _nand,
149 _set
150 };
151
153 enum class BlendFunc
154 {
156 zero,
157
159 one,
160
163
165 src_color,
166
169
172
174 src_alpha,
175
178
181
184
187
190
193
196
199 };
200
202 enum class BlendEquation
203 {
204 add,
205 subtract,
207 min,
208 max
209 };
210
213 {
216 };
217
219 enum class PrimitivesType
220 {
221 points,
223 line_loop,
224 lines,
228 };
229
232 {
235 };
236
239 {
242 sprite,
243 path
244 };
245
247 enum class ShaderLanguage
248 {
249 glsl,
250 hlsl,
253 };
254
257 {
258 public:
261
266
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
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
360
363
365
368
371
374
376 PixelBuffer get_pixeldata(const Rect& rect, TextureFormat texture_format = TextureFormat::rgba8, bool clamp = true);
377
380
389
391 void set_frame_buffer(const FrameBuffer &write_buffer);
392 void set_frame_buffer(const FrameBuffer &write_buffer, const FrameBuffer &read_buffer);
393
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
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
450
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
462
465
468
470 void set_program_object(StandardProgram standard_program);
471
475 void set_program_object(const ProgramObject &program);
476
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
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, VertexAttributeDataType::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, VertexAttributeDataType::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, VertexAttributeDataType::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, VertexAttributeDataType::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, VertexAttributeDataType::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, VertexAttributeDataType::type_unsigned_byte, offset * sizeof(unsigned char), instance_count);
632 }
633
636
638 void dispatch(int x = 1, int y = 1, int z = 1);
639
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
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
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}
Blend state setup.
Definition blend_state.h:44
Floating point color description class (for float).
Definition color.h:799
DepthStencil state setup.
Definition depth_stencil_state.h:44
Element Array Buffer.
Definition element_array_buffer.h:46
Element Array Vector.
Definition element_array_vector.h:43
Frame-buffer object class.
Definition frame_buffer.h:72
Interface for implementing a GraphicContext target.
Definition graphic_context_provider.h:86
Interface to drawing graphics.
Definition graphic_context.h:257
PixelBuffer get_pixeldata(TextureFormat texture_format=TextureFormat::rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
std::vector< Texture > get_textures() const
void clear(const Colorf &color=StandardColorf::black())
Clears the whole context using the specified color.
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
GraphicContext create(FrameBuffer &buffer) const
Create a new default graphic context with a frame buffer selected.
void draw_primitives_elements_instanced(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset, int instance_count)
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayBuffer &element_array, VertexAttributeDataType indices_type, size_t offset=0)
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
PixelBuffer get_pixeldata(const Rect &rect, TextureFormat texture_format=TextureFormat::rgba8, bool clamp=true)
Return the content of the read buffer into a pixel buffer.
void set_blend_state(const BlendState &state, const Colorf &blend_color=StandardColorf::white(), unsigned int sample_mask=0xffffffff)
Set active blend state.
void set_scissor(const Rect &rect, TextureImageYAxis y_axis)
Set the current clipping rectangle.
void reset_blend_state()
Set active blend state.
void set_image_texture(std::vector< Texture > &textures)
ProgramObject get_program_object() const
Returns the currently selected program object.
bool has_compute_shader_support() const
void draw_primitives_array(PrimitivesType type, int offset, int num_vertices)
bool is_primitives_array_owner(const PrimitivesArray &primitives_array)
GraphicContextProvider * get_provider()
Returns the provider for this graphic context.
void reset_image_texture(int unit_index)
GraphicContext create() const
Create a new default graphic context compatible with this one.
bool operator!=(const GraphicContext &other) const
Definition graphic_context.h:683
void clear_stencil(int value=0)
void set_storage_buffer(int index, const StorageBuffer &buffer)
Select storage buffer into index.
const GraphicContextProvider * get_provider() const
void set_primitives_array(const PrimitivesArray &array)
Set the primitives array on the gc.
Sizef get_dip_size() const
Definition graphic_context.h:353
void set_frame_buffer(const FrameBuffer &write_buffer, const FrameBuffer &read_buffer)
ClipZRange get_clip_z_range() const
Returns in what range clip space z values are clipped.
void set_rasterizer_state(const RasterizerState &state)
Set active rasterizer state.
void clear_depth(float value=0)
void set_primitives_elements(ElementArrayVector< Type > &element_array)
Sets current elements array buffer.
Definition graphic_context.h:515
void set_image_texture(int unit_index, const Texture &texture)
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned char > &element_array, size_t offset=0)
Definition graphic_context.h:580
float get_dip_height() const
Definition graphic_context.h:349
void reset_image_textures()
Remove all selected textures.
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
void reset_primitives_elements()
Resets current elements array buffer.
void reset_depth_stencil_state()
Set active depth stencil state.
Size get_max_texture_size() const
Size get_size() const
Returns the current actual size of the context.
void draw_primitives(PrimitivesType type, int num_vertices, const PrimitivesArray &array)
Draw primitives on gc.
FrameBuffer get_read_frame_buffer() const
void draw_primitives_array_instanced(PrimitivesType type, int offset, int num_vertices, int instance_count)
void reset_uniform_buffer(int index)
Remove uniform buffer from index.
void draw_primitives_elements(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset=0)
void reset_texture(int unit_index)
float get_dip_width() const
Definition graphic_context.h:345
GraphicContext clone() const
Create a new default graphic context cloned with this one.
FrameBuffer get_write_frame_buffer() const
void set_frame_buffer(const FrameBuffer &write_buffer)
Sets the current frame buffer.
void set_viewport(int index, const Rectf &viewport)
void set_draw_buffer(DrawBuffer buffer)
Set used draw buffer.
bool operator==(const GraphicContext &other) const
Definition graphic_context.h:682
void reset_textures()
Remove all selected textures.
bool is_null() const
Returns true if this object is invalid.
Definition graphic_context.h:270
ShaderLanguage get_shader_language() const
Returns the shader language used.
void set_depth_stencil_state(const DepthStencilState &state, int stencil_ref=0)
Set active depth stencil state.
Texture get_texture(int index) const
GraphicContext()
Constructs a null instance.
void reset_program_object()
Remove active program object.
int get_minor_version() const
bool is_frame_buffer_owner(const FrameBuffer &fb)
void reset_frame_buffer()
Resets the current frame buffer to be the initial frame buffer.
void set_program_object(const ProgramObject &program)
TextureImageYAxis get_texture_image_y_axis() const
Returns the Y axis direction for viewports, clipping rects, textures and render targets.
void flush()
Flush the command buffer.
void set_program_object(StandardProgram standard_program)
Set active program object to the standard program specified.
void reset_rasterizer_state()
Set active rasterizer state.
GraphicContext(GraphicContextProvider *provider)
void dispatch(int x=1, int y=1, int z=1)
Execute a compute shader.
void reset_primitives_array()
Reset the primitives arrays.
void set_primitives_elements(ElementArrayBuffer &element_array)
Sets current elements array buffer.
void set_depth_range(float n, float f)
Specifies the depth range for all viewports.
int get_height() const
Returns the current actual height of the context.
void draw_primitives_elements_instanced(PrimitivesType type, int count, VertexAttributeDataType indices_type, size_t offset, int instance_count)
void draw_primitives_array(PrimitivesType type, int num_vertices)
Draws primitives from the current assigned primitives array.
int get_major_version() const
void set_uniform_buffer(int index, const UniformBuffer &buffer)
Select uniform buffer into index.
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned int > &element_array, size_t offset=0)
Definition graphic_context.h:556
void set_textures(std::vector< Texture > &textures)
void reset_scissor()
Removes the set clipping rectangle.
void reset_storage_buffer(int index)
Remove storage buffer from index.
void draw_primitives_elements(PrimitivesType type, int count, ElementArrayVector< unsigned short > &element_array, size_t offset=0)
Definition graphic_context.h:568
void throw_if_null() const
Throw an exception if this object is invalid.
float get_pixel_ratio() const
void set_depth_range(int viewport, float n, float f)
Specifies the depth range for the specified viewport.
void set_texture(int unit_index, const Texture &texture)
void set_viewport(const Rectf &viewport)
int get_width() const
Returns the current actual width of the context.
OpenGL utility class.
Definition opengl.h:74
Pixel data container.
Definition pixel_buffer.h:68
Primitives array description.
Definition primitives_array.h:68
Program Object.
Definition program_object.h:69
Rasterizer state setup.
Definition rasterizer_state.h:44
2D (left,top,right,bottom) rectangle structure - Integer
Definition rect.h:489
2D (left,top,right,bottom) rectangle structure - Float
Definition rect.h:503
2D (width,height) size structure - Integer
Definition size.h:171
2D (width,height) size structure - Float
Definition size.h:184
static Colorf black()
Definition color.h:1747
static Colorf white()
Definition color.h:1884
Storage Buffer.
Definition storage_buffer.h:47
Texture object class.
Definition texture.h:103
Uniform Buffer.
Definition uniform_buffer.h:47
ClipZRange
Definition mat4.h:51
BlendFunc
Blending functions.
Definition graphic_context.h:154
FaceSide
Front face modes.
Definition graphic_context.h:85
CompareFunction
Compare functions.
Definition graphic_context.h:92
TextureFormat
Texture format.
Definition texture_format.h:39
PrimitivesType
Primitive types.
Definition graphic_context.h:220
ShaderLanguage
Shader language used.
Definition graphic_context.h:248
FillMode
Polygon filling modes.
Definition graphic_context.h:77
const float pixelcenter_constant
Definition graphic_context.h:691
PointSpriteOrigin
Point Sprite Origin.
Definition graphic_context.h:213
LogicOp
Logic Op.
Definition graphic_context.h:133
StencilOp
Stencil operations.
Definition graphic_context.h:105
TextureImageYAxis
Y axis direction for viewports, clipping rects, textures and render targets.
Definition graphic_context.h:232
DrawBuffer
Drawing buffers.
Definition graphic_context.h:118
StandardProgram
Standard Program.
Definition graphic_context.h:239
VertexAttributeDataType
Primitives array description.
Definition primitives_array.h:56
CullMode
Polygon culling modes.
Definition graphic_context.h:69
BlendEquation
Blending equations.
Definition graphic_context.h:203
@ one_minus_dest_alpha
source or destination (1, 1, 1, 1) - (Ad, Ad, Ad, Ad)
@ one_minus_dest_color
source (1, 1, 1, 1) - (Rd, Gd, Bd, Ad)
@ src_color
destination (Rs, Gs, Bs, As)
@ src_alpha_saturate
source (f, f, f, 1) - f = min(As, 1 - Ad)
@ one_minus_constant_color
source or destination (1, 1, 1, 1) - (Rc, Gc, Bc, Ac)
@ dest_color
source (Rd, Gd, Bd, Ad)
@ constant_alpha
source or destination (Ac, Ac, Ac, Ac)
@ one_minus_src_alpha
source or destination (1, 1, 1, 1) - (As, As, As, As)
@ one_minus_src_color
destination (1, 1, 1, 1) - (Rs, Gs, Bs, As)
@ one_minus_constant_alpha
source or destination (1, 1, 1, 1) - (Ac, Ac, Ac, Ac)
@ constant_color
source or destination (Rc, Gc, Bc, Ac)
@ one
source or destination (1, 1, 1, 1)
@ dest_alpha
source or destination (Ad, Ad, Ad, Ad)
@ src_alpha
source or destination (As, As, As, As)
@ y_bottom_up
OpenGL, origin is lower left with Y going upwards.
@ y_top_down
Direct3D, origin is upper left with Y going downwards.
Definition clanapp.h:36
@ color
value is an url