]> git.tdb.fi Git - libs/gl.git/blob - source/core/pipelinestate.h
Simplify pipeline state management
[libs/gl.git] / source / core / pipelinestate.h
1 #ifndef MSP_GL_PIPELINESTATE_H_
2 #define MSP_GL_PIPELINESTATE_H_
3
4 #include <vector>
5 #include <msp/core/noncopyable.h>
6 #include "cullface.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Blend;
12 class BufferBackedUniformBlock;
13 class DefaultUniformBlock;
14 class DepthTest;
15 class Framebuffer;
16 class Program;
17 class Rect;
18 class Sampler;
19 class StencilTest;
20 class Texture;
21 class UniformBlock;
22 class VertexSetup;
23
24 class PipelineState: public NonCopyable
25 {
26 private:
27         struct BoundTexture
28         {
29                 unsigned binding;
30                 mutable bool changed;
31                 const Texture *texture;
32                 const Sampler *sampler;
33
34                 BoundTexture(unsigned);
35         };
36
37         struct BoundUniformBlock
38         {
39                 int binding;
40                 mutable bool changed;
41                 const UniformBlock *block;
42
43                 BoundUniformBlock(int);
44         };
45
46         enum ChangeMask
47         {
48                 SHPROG = 1,
49                 VERTEX_SETUP = 2,
50                 FACE_CULL = 4,
51                 CLIP_PLANES = 8,
52                 TEXTURES = 16,
53                 UNIFORMS = 32,
54                 DEPTH_TEST = 64,
55                 STENCIL_TEST = 128,
56                 BLEND = 256,
57                 FRAMEBUFFER = 512,
58                 VIEWPORT = 1024,
59                 SCISSOR = 2048
60         };
61
62         const Framebuffer *framebuffer;
63         const Rect *viewport;
64         const Rect *scissor;
65         const Program *shprog;
66         const VertexSetup *vertex_setup;
67         FaceWinding front_face;
68         CullMode face_cull;
69         unsigned enabled_clip_planes;
70         std::vector<BoundTexture> textures;
71         std::vector<BoundUniformBlock> uniform_blocks;
72         const DepthTest *depth_test;
73         const StencilTest *stencil_test;
74         const Blend *blend;
75         mutable unsigned changes;
76
77         static const PipelineState *last_applied;
78         static std::vector<int> bound_tex_targets;
79         static std::vector<char> bound_uniform_blocks;
80         static unsigned restart_index;
81
82 public:
83         PipelineState();
84         ~PipelineState();
85
86 private:
87         template<typename T>
88         void set(T &, T, unsigned);
89 public:
90         void set_framebuffer(const Framebuffer *);
91         void set_viewport(const Rect *);
92         void set_scissor(const Rect *);
93         void set_shader_program(const Program *);
94         void set_vertex_setup(const VertexSetup *);
95         void set_front_face(FaceWinding);
96         void set_face_cull(CullMode);
97         void set_enabled_clip_planes(unsigned);
98         void set_texture(unsigned, const Texture *, const Sampler *);
99         void set_uniforms(const DefaultUniformBlock *);
100         void set_uniform_block(unsigned, const BufferBackedUniformBlock *);
101 private:
102         void set_uniform_block_(int, const UniformBlock *);
103 public:
104         void set_depth_test(const DepthTest *);
105         void set_stencil_test(const StencilTest *);
106         void set_blend(const Blend *);
107
108         const Framebuffer *get_framebuffer() const { return framebuffer; }
109         const Program *get_shader_program() const { return shprog; }
110         const VertexSetup *get_vertex_setup() const { return vertex_setup; }
111         FaceWinding get_front_face() const { return front_face; }
112         CullMode get_face_cull() const { return face_cull; }
113
114         void apply() const;
115 private:
116         void apply(unsigned) const;
117 public:
118         static void clear();
119 };
120
121 } // namespace GL
122 } // namespace Msp
123
124 #endif