]> git.tdb.fi Git - libs/gl.git/blob - source/core/pipelinestate.cpp
076eb333d29b127a0d08ef38ec158223fb957179
[libs/gl.git] / source / core / pipelinestate.cpp
1 #include <stdexcept>
2 #include <msp/core/algorithm.h>
3 #include "pipelinestate.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 template<typename T>
11 void PipelineState::set(T &target, T value, unsigned flag)
12 {
13         if(value!=target)
14         {
15                 target = value;
16                 changes |= flag;
17         }
18 }
19
20 void PipelineState::set_framebuffer(const Framebuffer *f)
21 {
22         set(framebuffer, f, FRAMEBUFFER);
23 }
24
25 void PipelineState::set_viewport(const Rect *v)
26 {
27         set(viewport, v, VIEWPORT);
28 }
29
30 void PipelineState::set_scissor(const Rect *s)
31 {
32         set(scissor, s, SCISSOR);
33 }
34
35 void PipelineState::set_shader_program(const Program *p)
36 {
37         set(shprog, p, SHPROG);
38 }
39
40 void PipelineState::set_uniform_block(int binding, const UniformBlock *block)
41 {
42         auto i = lower_bound_member(uniform_blocks, binding, &BoundUniformBlock::binding);
43         if(i==uniform_blocks.end() || i->binding!=binding)
44                 i = uniform_blocks.insert(i, BoundUniformBlock(binding));
45         i->used = block;
46         if(block!=i->block || binding<0)
47         {
48                 i->block = block;
49                 i->changed = true;
50                 changes |= UNIFORMS;
51         }
52 }
53
54 void PipelineState::set_texture(unsigned binding, const Texture *tex, const Sampler *samp)
55 {
56         if((tex!=0)!=(samp!=0))
57                 throw invalid_argument("PipelineState::set_texture");
58
59         auto i = lower_bound_member(textures, binding, &BoundTexture::binding);
60         if(i==textures.end() || i->binding!=binding)
61                 i = textures.insert(i, BoundTexture(binding));
62         i->used = (tex && samp);
63         if(tex!=i->texture || samp!=i->sampler)
64         {
65                 i->texture = tex;
66                 i->sampler = samp;
67                 i->changed = true;
68                 changes |= TEXTURES;
69         }
70 }
71
72 void PipelineState::set_vertex_setup(const VertexSetup *s)
73 {
74         set(vertex_setup, s, VERTEX_SETUP);
75 }
76
77 void PipelineState::set_primitive_type(PrimitiveType t)
78 {
79         set(primitive_type, t, PRIMITIVE_TYPE);
80 }
81
82 void PipelineState::set_front_face(FaceWinding w)
83 {
84         set(front_face, w, FACE_CULL);
85 }
86
87 void PipelineState::set_face_cull(CullMode c)
88 {
89         set(face_cull, c, FACE_CULL);
90 }
91
92 void PipelineState::set_depth_test(const DepthTest *dt)
93 {
94         set(depth_test, dt, DEPTH_TEST);
95 }
96
97 void PipelineState::set_stencil_test(const StencilTest *st)
98 {
99         set(stencil_test, st, STENCIL_TEST);
100 }
101
102 void PipelineState::set_blend(const Blend *b)
103 {
104         set(blend, b, BLEND);
105 }
106
107 } // namespace GL
108 } // namespace Msp