aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/OpenGLShaders.hh
blob: ee4a6b2599c9856a887bbef4dc77b097cde60169 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/** OpenGLShaders.hh file for the fluxbox compositor. */

// Copyright (c) 2011 Gediminas Liktaras (gliktaras at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#ifndef FBCOMPOSITOR_OPENGLSHADERS_HH
#define FBCOMPOSITOR_OPENGLSHADERS_HH


#include "FbTk/Command.hh"
#include "FbTk/RefCount.hh"

#include <GL/glew.h>
#include <GL/gl.h>

#include <vector>


namespace FbCompositor {

    class BasePlugin;


    //--- SHADER INITIALIZERS --------------------------------------------------

    /** Rendering job initialization functor.  */
    typedef FbTk::Command<void> OpenGLShaderInitializer;

    /**
     * Null initialization action.
     */
    class NullInitializer : public OpenGLShaderInitializer {
    public :
        virtual ~NullInitializer() { }
        void execute() { }
    };


    //--- SHADER DEINITIALIZERS ------------------------------------------------

    /** Rendering job cleanup functor. */
    typedef FbTk::Command<void> OpenGLShaderDeinitializer;

    /** 
     * Null cleanup action.
     */
    class NullDeinitializer : public OpenGLShaderDeinitializer {
    public :
        virtual ~NullDeinitializer() { }
        void execute() { }
    };


    //--- SHADER PROGRAM WRAPPER -----------------------------------------------

    /**
     * OpenGL shader program wrapper.
     */
    class OpenGLShaderProgram {
    public :
        //--- CONSTRUCTORS AND DESTRUCTORS -------------------------------------
        
        /** Constructor. */
        OpenGLShaderProgram(const std::vector<BasePlugin*> &plugins);

        /** Destructor. */
        ~OpenGLShaderProgram();


        //--- ACCESSORS --------------------------------------------------------

        /** \returns handle to the shader program. */
        GLuint programHandle() const;


        /** \returns location of the given attribute. */
        GLuint getAttributeLocation(const char *attrib_name);

        /** \returns location of the fb_InitMainTexCoord attribute. */
        GLuint mainTexCoordAttrib() const;

        /** \returns location of the fb_InitPrimPos attribute. */
        GLuint primPosAttrib() const;

        /** \returns location of the fb_InitShapeTexCoord attribute. */
        GLuint shapeTexCoordAttrib() const;


        /** \returns location of the given uniform. */
        GLuint getUniformLocation(const char *uniform_name);

        /** \returns location of the fb_Alpha uniform. */
        GLuint alphaUniform() const;
        
        /** \returns location of the fb_MainTexture uniform. */
        GLuint mainTexUniform() const;

        /** \returns location of the fb_ShapeTexture uniform. */
        GLuint shapeTexUniform() const;


        //--- SHADER MANIPULATION ----------------------------------------------

        /** Uses the current shader program. */
        void use();


    private :
        //--- INITIALIZATION FUNCTIONS -----------------------------------------

        /** Creates a shader. */
        GLuint createShader(GLenum shader_type, GLint source_length, const GLchar *source);

        /** Creates a shader program. */
        GLuint createShaderProgram(GLuint vertex_shader, GLuint fragment_shader);


        //--- MAIN VARIABLES ---------------------------------------------------

        /** The vertex shader. */
        GLuint m_vertex_shader;

        /** The fragment shader. */
        GLuint m_fragment_shader;
        
        /** The shader program. */
        GLuint m_shader_program;


        /** Location of the fb_InitMainTexCoord attribute. */
        GLuint m_main_tex_coord_attrib;

        /** Location of the fb_InitPrimPos attribute. */
        GLuint m_prim_pos_attrib;

        /** Location of the fb_InitShapeTexCoord attribute. */
        GLuint m_shape_tex_coord_attrib;


        /** Location of the fb_Alpha uniform. */
        GLuint m_alpha_uniform;
        
        /** Location of the fb_MainTexture uniform. */
        GLuint m_main_tex_uniform;

        /** Location of the fb_ShapeTexture uniform. */
        GLuint m_shape_tex_uniform;
    };


    //--- INLINE FUNCTIONS -------------------------------------------------

    // Returns location of the fb_Alpha uniform.
    inline GLuint OpenGLShaderProgram::alphaUniform() const {
        return m_alpha_uniform;
    }

    // Returns location of the given attribute.
    inline GLuint OpenGLShaderProgram::getAttributeLocation(const char *attrib_name) {
        return glGetAttribLocation(m_shader_program, attrib_name);
    }

    // Returns location of the given uniform.
    inline GLuint OpenGLShaderProgram::getUniformLocation(const char *uniform_name) {
        return glGetUniformLocation(m_shader_program, uniform_name);
    }

    // Returns location of the fb_InitMainTexCoord attribute.
    inline GLuint OpenGLShaderProgram::mainTexCoordAttrib() const {
        return m_main_tex_coord_attrib;
    }

    // Returns location of the fb_MainTexture uniform.
    inline GLuint OpenGLShaderProgram::mainTexUniform() const {
        return m_main_tex_uniform;
    }

    // Returns location of the fb_InitPrimPos attribute.
    inline GLuint OpenGLShaderProgram::primPosAttrib() const {
        return m_prim_pos_attrib;
    }

    // Returns the handle to the shader program.
    inline GLuint OpenGLShaderProgram::programHandle() const {
        return m_shader_program;
    }

    // Returns location of the fb_InitShapeTexCoord attribute.
    inline GLuint OpenGLShaderProgram::shapeTexCoordAttrib() const {
        return m_shape_tex_coord_attrib;
    }

    // Returns location of the fb_ShapeTexture uniform.
    inline GLuint OpenGLShaderProgram::shapeTexUniform() const {
        return m_shape_tex_uniform;
    }

    // Uses the current shader program.
    inline void OpenGLShaderProgram::use() {
        glUseProgram(m_shader_program);
    }


    //--- TYPEDEFS ---------------------------------------------------------

    typedef FbTk::RefCount<OpenGLShaderProgram> OpenGLShaderProgramPtr;
}

#endif  // FBCOMPOSITOR_OPENGLSHADERS_HH