2
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
3
* Copyright 2008-2010 Pelican Mapping
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.
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.
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/>
19
#ifndef OSGEARTH_SHADER_COMPOSITION_H
20
#define OSGEARTH_SHADER_COMPOSITION_H 1
22
#include <osgEarth/Common>
23
#include <osgEarth/Revisioning>
24
#include <osgEarth/ThreadingUtils>
28
#include <osg/Program>
29
#include <osg/StateAttribute>
35
// User function injection points.
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
46
// set of user functions, ordered by priority.
47
typedef std::multimap<float, std::string> OrderedFunctionMap; // duplicate keys allowed
49
// user function sets, categorized by function location.
50
typedef std::map<FunctionLocation, OrderedFunctionMap> FunctionLocationMap;
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.
58
class OSGEARTH_EXPORT ShaderFactory : public osg::Referenced
61
/** Creates a vertex shader main(). */
62
virtual osg::Shader* createVertexShaderMain(
63
const ShaderComp::FunctionLocationMap& functions =ShaderComp::FunctionLocationMap() ) const;
65
/** Creates a fragment shader main(). */
66
virtual osg::Shader* createFragmentShaderMain(
67
const ShaderComp::FunctionLocationMap& functions =ShaderComp::FunctionLocationMap() ) const;
70
* Creates the function that sets up default texcoords in the vertex shader.
71
* The name/prototype is:
72
* void osgearth_vert_setupTexturing();
74
virtual osg::Shader* createDefaultTextureVertexShader( int numTexCoordSets ) const;
77
* Creates the function that applies texture data in the fragment shader.
78
* The name/prototype is:
79
* osgearth_frag_applyTexturing( inout vec4 color );
81
virtual osg::Shader* createDefaultTextureFragmentShader( int numTexCoordSets ) const;
84
* Creates the function that applies lighting calculations in the vertex shader.
85
* The name/prototype is:
86
* void osgearth_vert_setupLighting();
88
virtual osg::Shader* createDefaultLightingVertexShader() const;
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 );
95
virtual osg::Shader* createDefaultLightingFragmentShader() const;
99
* VirtualProgram enables basic GLSL shader composition within osgEarth.
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.
105
class OSGEARTH_EXPORT VirtualProgram : public osg::Program
109
* Adds a shader function to the program.
110
* Call this method (rather than setShader directly) to inject "user" functions into the
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.
119
const std::string& name,
120
const std::string& source,
121
ShaderComp::FunctionLocation loc,
122
float priority =1.0f );
125
VirtualProgram( unsigned int mask = 0xFFFFFFFFUL );
127
VirtualProgram( const VirtualProgram& VirtualProgram,
128
const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY );
130
META_StateAttribute( osgCandidate, VirtualProgram, Type( PROGRAM ) )
132
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
133
virtual int compare(const StateAttribute& sa) const
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)
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.
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;
149
osg::Shader* getShader( const std::string& shaderSemantic, osg::Shader::Type type );
151
osg::Shader* setShader( const std::string& shaderSemantic, osg::Shader* shader );
153
void removeShader( const std::string& shaderSemantic, osg::Shader::Type type );
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;
162
mutable ProgramMap _programMap;
163
ShaderMap _shaderMap;
166
ShaderComp::FunctionLocationMap _functions;
167
ShaderComp::FunctionLocationMap _accumulatedFunctions;
169
Threading::Mutex _functionsMutex;
171
bool hasLocalFunctions() const;
172
void refreshAccumulatedFunctions( const osg::State& state );
175
void getFunctions( ShaderComp::FunctionLocationMap& out ) const;
178
} // namespace osgEarth
180
#endif // OSGEARTH_SHADER_COMPOSITION_H