2005-02-24 21:33:35 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield
|
|
|
|
* Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
|
2005-03-24 17:37:45 +08:00
|
|
|
* Copyright (C) 2004-2005 Nathan Cournia
|
2005-02-24 21:33:35 +08:00
|
|
|
*
|
|
|
|
* This application is open source and may be redistributed and/or modified
|
|
|
|
* freely and without restriction, both in commericial and non commericial
|
|
|
|
* applications, as long as this copyright notice is maintained.
|
|
|
|
*
|
|
|
|
* This application is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
/* file: include/osg/Shader
|
2005-04-08 04:23:58 +08:00
|
|
|
* author: Mike Weiblen 2005-04-06
|
2005-02-24 21:33:35 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSG_SHADER
|
|
|
|
#define OSG_SHADER 1
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
#include <osg/Program>
|
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/** osg::Shader is an application-level abstraction of an OpenGL glShader.
|
|
|
|
* It is a container to load the shader source code text and manage its
|
|
|
|
* compilation.
|
2005-03-24 17:37:45 +08:00
|
|
|
* An osg::Shader may be attached to more than one osg::Program.
|
2005-02-24 21:33:35 +08:00
|
|
|
* Shader will automatically manage per-context instancing of the
|
|
|
|
* internal objects, if that is necessary for a particular display
|
|
|
|
* configuration.
|
|
|
|
*/
|
|
|
|
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT Shader : public osg::Object
|
2005-02-24 21:33:35 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum Type {
|
|
|
|
VERTEX = GL_VERTEX_SHADER,
|
2005-04-08 04:23:58 +08:00
|
|
|
FRAGMENT = GL_FRAGMENT_SHADER,
|
|
|
|
UNDEFINED = -1
|
2005-02-24 21:33:35 +08:00
|
|
|
};
|
|
|
|
|
2005-04-25 21:37:12 +08:00
|
|
|
Shader( Type type = UNDEFINED);
|
|
|
|
Shader( Type type, const std::string& source );
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
|
|
|
Shader(const Shader& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
|
|
|
|
|
2005-04-08 04:23:58 +08:00
|
|
|
META_Object(osg, Shader);
|
2005-03-24 17:37:45 +08:00
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
int compare(const Shader& rhs) const;
|
|
|
|
|
2005-04-08 04:23:58 +08:00
|
|
|
bool setType( Type t );
|
2005-04-25 21:37:12 +08:00
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
/** Load the Shader's source code text from a string. */
|
2005-04-25 21:37:12 +08:00
|
|
|
void setShaderSource( const std::string& sourceText );
|
|
|
|
|
|
|
|
/** Read shader source from file and then constructor shader of specified type.
|
|
|
|
* Return the resulting Shader or 0 if no valid shader source code be read.*/
|
|
|
|
static Shader* readShaderFile( Type type, const std::string& fileName );
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
/** Load the Shader's source code text from a file. */
|
2005-04-25 21:37:12 +08:00
|
|
|
bool loadShaderSourceFromFile( const std::string& fileName );
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
/** Query the shader's source code text */
|
|
|
|
inline const std::string& getShaderSource() const { return _shaderSource; }
|
|
|
|
|
|
|
|
/** Get the Shader type as an enum. */
|
|
|
|
inline Type getType() const { return _type; }
|
|
|
|
|
|
|
|
/** Get the Shader type as a descriptive string. */
|
|
|
|
const char* getTypename() const;
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
/** Mark our PCSs as needing recompilation.
|
|
|
|
* Also mark Programs that depend on us as needing relink */
|
2005-02-24 21:33:35 +08:00
|
|
|
void dirtyShader();
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
/** If needed, compile the PCS's glShader */
|
2005-02-24 21:33:35 +08:00
|
|
|
void compileShader(unsigned int contextID) const;
|
|
|
|
|
|
|
|
/** For a given GL context, attach a glShader to a glProgram */
|
|
|
|
void attachShader(unsigned int contextID, GLuint program) const;
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
/** Query InfoLog from a glShader */
|
|
|
|
void getGlShaderInfoLog(unsigned int contextID, std::string& log) const;
|
|
|
|
|
|
|
|
/** A name for use by the application */
|
|
|
|
void setName( const std::string& name ) { _name = name; }
|
|
|
|
void setName( const char* name ) { _name = name; }
|
|
|
|
const std::string& getName() const { return _name; };
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
/** Mark internal glShader for deletion.
|
|
|
|
* Deletion requests are queued tuntil they can be executed
|
|
|
|
* in the proper GL context. */
|
|
|
|
static void deleteGlShader(unsigned int contextID, GLuint shader);
|
|
|
|
|
|
|
|
/** flush all the cached glShaders which need to be deleted
|
|
|
|
* in the OpenGL context related to contextID.*/
|
|
|
|
static void flushDeletedGlShaders(unsigned int contextID,double currentTime, double& availableTime);
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-04-08 04:23:58 +08:00
|
|
|
static Shader::Type getTypeId( const std::string& tname );
|
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
protected:
|
2005-03-24 17:37:45 +08:00
|
|
|
/** PerContextShader (PCS) is an OSG-internal encapsulation of glShader per-GL context. */
|
|
|
|
class PerContextShader : public osg::Referenced
|
2005-02-24 21:33:35 +08:00
|
|
|
{
|
|
|
|
public:
|
2005-03-24 17:37:45 +08:00
|
|
|
PerContextShader(const Shader* shader, unsigned int contextID);
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
GLuint getHandle() const {return _glShaderHandle;}
|
2005-02-24 21:33:35 +08:00
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
void requestCompile();
|
2005-02-24 21:33:35 +08:00
|
|
|
void compileShader();
|
2005-03-24 17:37:45 +08:00
|
|
|
void getInfoLog( std::string& infoLog ) const;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
/** Attach our glShader to a glProgram */
|
|
|
|
void attachShader(GLuint program) const;
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
/** Detach our glShader from a glProgram */
|
|
|
|
void detachShader(GLuint program) const;
|
|
|
|
|
2005-02-24 21:33:35 +08:00
|
|
|
protected: /*methods*/
|
2005-03-24 17:37:45 +08:00
|
|
|
~PerContextShader();
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
protected: /*data*/
|
2005-03-24 17:37:45 +08:00
|
|
|
/** Pointer to our parent osg::Shader */
|
2005-02-24 21:33:35 +08:00
|
|
|
const Shader* _shader;
|
|
|
|
/** Pointer to this context's extension functions. */
|
2005-03-24 17:37:45 +08:00
|
|
|
osg::ref_ptr<osg::GL2Extensions> _extensions;
|
2005-02-24 21:33:35 +08:00
|
|
|
/** Handle to the actual glShader. */
|
|
|
|
GLuint _glShaderHandle;
|
2005-03-24 17:37:45 +08:00
|
|
|
/** Does our glShader need to be recompiled? */
|
|
|
|
bool _needsCompile;
|
|
|
|
/** Is our glShader successfully compiled? */
|
|
|
|
bool _isCompiled;
|
2005-02-24 21:33:35 +08:00
|
|
|
const unsigned int _contextID;
|
2005-03-24 17:37:45 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
PerContextShader(); // disallowed
|
|
|
|
PerContextShader(const PerContextShader&); // disallowed
|
|
|
|
PerContextShader& operator=(const PerContextShader&); // disallowed
|
2005-02-24 21:33:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
protected: /*methods*/
|
|
|
|
virtual ~Shader();
|
|
|
|
|
2005-03-24 17:37:45 +08:00
|
|
|
PerContextShader* getPCS(unsigned int contextID) const;
|
|
|
|
|
|
|
|
friend class Program;
|
|
|
|
bool addProgramRef( Program* program );
|
|
|
|
bool removeProgramRef( Program* program );
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
protected: /*data*/
|
2005-04-08 04:23:58 +08:00
|
|
|
Type _type;
|
2005-03-24 17:37:45 +08:00
|
|
|
std::string _name;
|
2005-02-24 21:33:35 +08:00
|
|
|
std::string _shaderSource;
|
2005-03-24 17:37:45 +08:00
|
|
|
/** osg::Programs that this osg::Shader is attached to */
|
|
|
|
typedef std::set< Program* > ProgramSet;
|
|
|
|
ProgramSet _programSet;
|
|
|
|
mutable osg::buffered_value< osg::ref_ptr<PerContextShader> > _pcsList;
|
2005-02-24 21:33:35 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Shader& operator=(const Shader&); // disallowed
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*EOF*/
|