summaryrefslogtreecommitdiff
path: root/vcl/inc/opengl/RenderState.hxx
blob: 3a89344e2e0e124f17646bad06afb0636d5d899a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#ifndef INCLUDED_VCL_INC_OPENGL_RENDER_STATE_H
#define INCLUDED_VCL_INC_OPENGL_RENDER_STATE_H

#include <opengl/TextureState.hxx>

template<GLenum ENUM_TYPE, typename TYPE>
class GenericCapabilityState
{
protected:
    bool mbTest;

    GenericCapabilityState()
        : mbTest(false)
    {
    }

    static bool readState()
    {
        return glIsEnabled(ENUM_TYPE);
    }

public:
    void sync()
    {
        mbTest = readState();
    }

    void enable()
    {
        if (!mbTest)
        {
            glEnable(ENUM_TYPE);
            CHECK_GL_ERROR();
            mbTest = true;
        }
        else
        {
            VCL_GL_INFO(TYPE::className() << ": enable called but already enabled");
        }
#ifdef DBG_UTIL
        checkState();
#endif
    }

    void disable()
    {
        if (mbTest)
        {
            glDisable(ENUM_TYPE);
            CHECK_GL_ERROR();
            mbTest = false;
        }
        else
        {
            VCL_GL_INFO(TYPE::className() << ": disable called but already disabled");
        }
#ifdef DBG_UTIL
        checkState();
#endif
    }

#ifdef DBG_UTIL
    void checkState()
    {
        bool bRealState = readState();
        if (mbTest != bRealState)
        {
            VCL_GL_INFO(TYPE::className() << " mismatch! "
                            << "Expected: " << (mbTest ? "enabled" : "disabled")
                            << " but is: "        << (bRealState ? "enabled" : "disabled"));
        }
    }
#endif
};

class ScissorState : public GenericCapabilityState<GL_SCISSOR_TEST, ScissorState>
{
private:
    int mX;
    int mY;
    int mWidth;
    int mHeight;

public:
    static std::string className() { return std::string("ScissorState"); }

    ScissorState()
        : mX(0)
        , mY(0)
        , mWidth(0)
        , mHeight(0)
    {}

    void set(int x, int y, int width, int height)
    {
        if (x != mX || y != mY || width != mWidth || height != mHeight)
        {
            glScissor(x, y, width, height);
            CHECK_GL_ERROR();

            mX = x;
            mY = y;
            mWidth = width;
            mHeight = height;
        }
    }
};

class StencilState : public GenericCapabilityState<GL_STENCIL_TEST, StencilState>
{
public:
    static std::string className() { return std::string("StencilState"); }
};

class BlendState : public GenericCapabilityState<GL_BLEND, BlendState>
{
    GLenum mnSourceMode;
    GLenum mnDestinationMode;
public:
    BlendState()
        : mnSourceMode(GL_ZERO)
        , mnDestinationMode(GL_ZERO)
    {}

    static std::string className() { return std::string("BlendState"); }

    void func(GLenum nSource, GLenum nDestination)
    {
        if (mnSourceMode != nSource || mnDestinationMode != nDestination)
        {
            glBlendFunc(nSource, nDestination);
            CHECK_GL_ERROR();
            mnSourceMode = nSource;
            mnDestinationMode = nDestination;
        }
    }
};

class RenderState
{
    TextureState maTexture;
    ScissorState maScissor;
    StencilState maStencil;
    BlendState   maBlend;

    tools::Rectangle maCurrentViewport;

public:
    RenderState()
    {}

    void viewport(tools::Rectangle aViewPort)
    {
        if (aViewPort != maCurrentViewport)
        {
            glViewport(aViewPort.Left(), aViewPort.Top(), aViewPort.GetWidth(), aViewPort.GetHeight());
            CHECK_GL_ERROR();
            maCurrentViewport = aViewPort;
        }
    }

    TextureState& texture() { return maTexture; }
    ScissorState& scissor() { return maScissor; }
    StencilState& stencil() { return maStencil; }
    BlendState&   blend()   { return maBlend; }

    void sync()
    {
        VCL_GL_INFO("RenderState::sync");
        maScissor.sync();
        maStencil.sync();
        maBlend.sync();
    }
};

#endif // INCLUDED_VCL_INC_OPENGL_RENDER_STATE_H

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */