~ubuntu-branches/debian/sid/osgearth/sid

« back to all changes in this revision

Viewing changes to src/osgEarth/ShaderComposition

  • Committer: Bazaar Package Importer
  • Author(s): Pirmin Kalberer
  • Date: 2011-07-14 22:13:36 UTC
  • Revision ID: james.westby@ubuntu.com-20110714221336-94igk9rskxveh794
Tags: upstream-2.0+dfsg
ImportĀ upstreamĀ versionĀ 2.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*-c++-*- */
 
2
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 
3
* Copyright 2008-2010 Pelican Mapping
 
4
* http://osgearth.org
 
5
*
 
6
* osgEarth is free software; you can redistribute it and/or modify
 
7
* it under the terms of the GNU Lesser General Public License as published by
 
8
* the Free Software Foundation; either version 2 of the License, or
 
9
* (at your option) any later version.
 
10
*
 
11
* This program is distributed in the hope that it will be useful,
 
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
* GNU Lesser General Public License for more details.
 
15
*
 
16
* You should have received a copy of the GNU Lesser General Public License
 
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>
 
18
*/
 
19
#ifndef OSGEARTH_SHADER_COMPOSITION_H
 
20
#define OSGEARTH_SHADER_COMPOSITION_H 1
 
21
 
 
22
#include <osgEarth/Common>
 
23
#include <osgEarth/Revisioning>
 
24
#include <osgEarth/ThreadingUtils>
 
25
#include <string>
 
26
#include <map>
 
27
#include <osg/Shader>
 
28
#include <osg/Program>
 
29
#include <osg/StateAttribute>
 
30
 
 
31
namespace osgEarth
 
32
{        
 
33
    namespace ShaderComp
 
34
    {
 
35
        // User function injection points.
 
36
        enum FunctionLocation
 
37
        {
 
38
            LOCATION_VERTEX_PRE_TEXTURING   =0,
 
39
            LOCATION_VERTEX_PRE_LIGHTING    =1,
 
40
            LOCATION_VERTEX_POST_LIGHTING   =2,
 
41
            LOCATION_FRAGMENT_PRE_TEXTURING =3,
 
42
            LOCATION_FRAGMENT_PRE_LIGHTING  =4,
 
43
            LOCATION_FRAGMENT_POST_LIGHTING =5
 
44
        };
 
45
 
 
46
        // set of user functions, ordered by priority.
 
47
        typedef std::multimap<float, std::string> OrderedFunctionMap; // duplicate keys allowed
 
48
 
 
49
        // user function sets, categorized by function location.
 
50
        typedef std::map<FunctionLocation, OrderedFunctionMap> FunctionLocationMap;
 
51
    }
 
52
 
 
53
    /**
 
54
     * A factory class that generates shader functions for the osgEarth engine.
 
55
     * The default ShaderFactory is stored in the osgEarth registry. You can replace it
 
56
     * if you want to replace osgEarth's default shader templates.
 
57
     */
 
58
    class OSGEARTH_EXPORT ShaderFactory : public osg::Referenced
 
59
    {
 
60
    public:
 
61
        /** Creates a vertex shader main(). */
 
62
        virtual osg::Shader* createVertexShaderMain(
 
63
            const ShaderComp::FunctionLocationMap& functions =ShaderComp::FunctionLocationMap() ) const;
 
64
 
 
65
        /** Creates a fragment shader main(). */
 
66
        virtual osg::Shader* createFragmentShaderMain( 
 
67
            const ShaderComp::FunctionLocationMap& functions =ShaderComp::FunctionLocationMap() ) const;
 
68
        
 
69
        /**
 
70
         * Creates the function that sets up default texcoords in the vertex shader.
 
71
         * The name/prototype is:
 
72
         *    void osgearth_vert_setupTexturing(); 
 
73
         */
 
74
        virtual osg::Shader* createDefaultTextureVertexShader( int numTexCoordSets ) const;
 
75
 
 
76
        /**
 
77
         * Creates the function that applies texture data in the fragment shader.
 
78
         * The name/prototype is:
 
79
         *    osgearth_frag_applyTexturing( inout vec4 color );
 
80
         */
 
81
        virtual osg::Shader* createDefaultTextureFragmentShader( int numTexCoordSets ) const;
 
82
 
 
83
        /**
 
84
         * Creates the function that applies lighting calculations in the vertex shader.
 
85
         * The name/prototype is:
 
86
         *    void osgearth_vert_setupLighting();
 
87
         */
 
88
        virtual osg::Shader* createDefaultLightingVertexShader() const;
 
89
 
 
90
        /**
 
91
         * Creates the function that applies lighting coloring in the fragment shader.
 
92
         * The name/prototype is:
 
93
         *    void osgearth_frag_applyLighting( inout vec4 color ); 
 
94
         */
 
95
        virtual osg::Shader* createDefaultLightingFragmentShader() const;
 
96
    };
 
97
 
 
98
    /**
 
99
     * VirtualProgram enables basic GLSL shader composition within osgEarth.
 
100
     *
 
101
     * VirtualProgram has been adapted from the VirtualProgram shader composition work
 
102
     * originally done by Wojciech Lewandowski and found in OSG's osgvirtualprogram
 
103
     * example, and is used by permission.
 
104
     */
 
105
    class OSGEARTH_EXPORT VirtualProgram : public osg::Program
 
106
    {
 
107
    public:
 
108
        /**
 
109
         * Adds a shader function to the program.
 
110
         * Call this method (rather than setShader directly) to inject "user" functions into the
 
111
         * shader program.
 
112
         *
 
113
         * name: name of the function. This should be the actual function name in the shader source.
 
114
         * source: the shader source code.
 
115
         * location: Function location relative to the built-ins.
 
116
         * priority: Lets you control the order of functions that you inject at the same location.
 
117
         */
 
118
        void setFunction( 
 
119
            const std::string& name,
 
120
            const std::string& source, 
 
121
            ShaderComp::FunctionLocation loc,
 
122
            float priority =1.0f );
 
123
 
 
124
    public: 
 
125
        VirtualProgram( unsigned int mask = 0xFFFFFFFFUL );
 
126
 
 
127
        VirtualProgram( const VirtualProgram& VirtualProgram, 
 
128
                        const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY );
 
129
 
 
130
        META_StateAttribute( osgCandidate, VirtualProgram, Type( PROGRAM ) )
 
131
 
 
132
        /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
 
133
        virtual int compare(const StateAttribute& sa) const
 
134
        {
 
135
           // check the types are equal and then create the rhs variable
 
136
           // used by the COMPARE_StateAttribute_Parameter macros below.
 
137
           COMPARE_StateAttribute_Types(VirtualProgram,sa)
 
138
 
 
139
           // compare each parameter in turn against the rhs.
 
140
           COMPARE_StateAttribute_Parameter(_mask)
 
141
           COMPARE_StateAttribute_Parameter(_shaderMap)
 
142
           return 0; // passed all the above comparison macros, must be equal.
 
143
        }
 
144
 
 
145
        /** If enabled, activate our program in the GL pipeline,
 
146
         * performing any rebuild operations that might be pending. */
 
147
        virtual void apply(osg::State& state) const;
 
148
 
 
149
        osg::Shader* getShader( const std::string& shaderSemantic, osg::Shader::Type type );   
 
150
 
 
151
        osg::Shader* setShader( const std::string& shaderSemantic, osg::Shader* shader );
 
152
 
 
153
        void removeShader( const std::string& shaderSemantic, osg::Shader::Type type );
 
154
 
 
155
 
 
156
    protected:
 
157
        typedef std::vector< osg::ref_ptr< osg::Shader > >            ShaderList;
 
158
        typedef std::pair< std::string, osg::Shader::Type >           ShaderSemantic;
 
159
        typedef std::map< ShaderSemantic, osg::ref_ptr<osg::Shader> > ShaderMap;
 
160
        typedef std::map< ShaderList, osg::ref_ptr<osg::Program> >    ProgramMap;
 
161
 
 
162
        mutable ProgramMap                   _programMap;
 
163
        ShaderMap                            _shaderMap;
 
164
        unsigned int                         _mask;
 
165
 
 
166
        ShaderComp::FunctionLocationMap _functions;
 
167
        ShaderComp::FunctionLocationMap _accumulatedFunctions;
 
168
 
 
169
        Threading::Mutex _functionsMutex;
 
170
 
 
171
        bool hasLocalFunctions() const;
 
172
        void refreshAccumulatedFunctions( const osg::State& state );
 
173
 
 
174
    public:
 
175
        void getFunctions( ShaderComp::FunctionLocationMap& out ) const;
 
176
    };
 
177
 
 
178
} // namespace osgEarth
 
179
 
 
180
#endif // OSGEARTH_SHADER_COMPOSITION_H