]> git.tdb.fi Git - libs/gl.git/blob - source/core/pipelinestate.h
Unify handling of shader resources in PipelineState
[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 "blend.h"
7 #include "cullface.h"
8 #include "depthtest.h"
9 #include "pipelinestate_backend.h"
10 #include "primitivetype.h"
11 #include "rect.h"
12 #include "stenciltest.h"
13
14 namespace Msp {
15 namespace GL {
16
17 class Buffer;
18 class Framebuffer;
19 class Program;
20 class Sampler;
21 class Texture;
22 class UniformBlock;
23 class VertexSetup;
24
25 /**
26 Stores state for the entire GPU pipeline.
27
28 Applications normally use the higher-level Renderer class rather than this.
29 */
30 class PipelineState: public PipelineStateBackend
31 {
32         friend PipelineStateBackend;
33
34 private:
35         enum ResourceType: std::uint8_t
36         {
37                 NO_RESOURCE,
38                 UNIFORM_BLOCK,
39                 TEXTURE
40         };
41
42         struct BoundResource
43         {
44                 int binding = 0;
45                 ResourceType type = NO_RESOURCE;
46                 mutable bool changed = false;
47                 mutable bool used = false;
48                 union
49                 {
50                         const UniformBlock *block;
51                         const Texture *texture;
52                 };
53                 union
54                 {
55                         const Buffer *buffer;
56                         const Sampler *sampler;
57                 };
58                 int mip_level = -1;
59
60                 BoundResource(int b): binding(b), texture(0), sampler(0) { }
61         };
62
63         enum ChangeMask
64         {
65                 FRAMEBUFFER = 1,
66                 VIEWPORT = 2,
67                 SCISSOR = 4,
68                 SHPROG = 8,
69                 RESOURCES = 16,
70                 VERTEX_SETUP = 64,
71                 FACE_CULL = 128,
72                 DEPTH_TEST = 256,
73                 STENCIL_TEST = 512,
74                 BLEND = 1024,
75                 PRIMITIVE_TYPE = 2048
76         };
77
78         const Framebuffer *framebuffer = 0;
79         Rect viewport = Rect::max();
80         Rect scissor = Rect::max();
81         const Program *shprog = 0;
82         std::vector<BoundResource> resources;
83         const VertexSetup *vertex_setup = 0;
84         PrimitiveType primitive_type = TRIANGLES;
85         FaceWinding front_face = COUNTERCLOCKWISE;
86         CullMode face_cull = NO_CULL;
87         DepthTest depth_test;
88         StencilTest stencil_test;
89         Blend blend;
90
91         template<typename T>
92         void set(T &, const T &, unsigned);
93 public:
94         void set_framebuffer(const Framebuffer *);
95         void set_viewport(const Rect &);
96         void set_scissor(const Rect &);
97         void set_shader_program(const Program *);
98         void set_uniform_block(int, const UniformBlock *);
99         void set_texture(unsigned, const Texture *, const Sampler *);
100         void set_texture(unsigned, const Texture *, int, const Sampler *);
101         void set_vertex_setup(const VertexSetup *);
102         void set_primitive_type(PrimitiveType);
103         void set_front_face(FaceWinding);
104         void set_face_cull(CullMode);
105         void set_depth_test(const DepthTest &);
106         void set_stencil_test(const StencilTest &);
107         void set_blend(const Blend &);
108
109         const Framebuffer *get_framebuffer() const { return framebuffer; }
110         const Rect &get_viewport() const { return viewport; }
111         const Program *get_shader_program() const { return shprog; }
112         const VertexSetup *get_vertex_setup() const { return vertex_setup; }
113         FaceWinding get_front_face() const { return front_face; }
114         CullMode get_face_cull() const { return face_cull; }
115 };
116
117 } // namespace GL
118 } // namespace Msp
119
120 #endif