Commit c4dd00fe authored by Frédéric Larue's avatar Frédéric Larue

Preparing architecture in order to integrate compute shaders.

parent 8bebbd2e
This diff is collapsed.
/*
* (c) LSIIT, UMR CNRS/UdS
* Authors: F. Larue.
*
* See licence.txt for additional information.
*/
#ifndef __GPU__COMPUTESHADER_H__
#define __GPU__COMPUTESHADER_H__
#include "GPUdefs.h"
#include "InstantiatedObject.h"
#include "Shader.h"
#include <string>
#include <map>
/*
*
* CLASS DECLARATION.
*
*/
namespace GPU
{
class VBO;
class VBOAttribBuffer;
class ComputeShader : public GenericShader
{
/******************\
| Internal type(s) |
\******************/
public:
struct Uniform
{
GLint location;
GLint size;
GLenum type;
};
class ComputePg : public PgObject
{
public:
inline bool CompileSrcFile( const std::string &filename,
std::string *logs = NULL ) { return PgObject::CompileSrcFile( filename, GL_COMPUTE_SHADER, logs ); }
inline bool CompileSrcString( const char *sourceString,
std::string *logs = NULL ) { return PgObject::CompileSrcString( sourceString, GL_COMPUTE_SHADER, logs ); }
};
static ComputePg ComputePgFromFile( const std::string &filename,
std::string *logs = NULL ) { ComputePg pg; return (pg.CompileSrcFile(filename,logs)? pg : ComputePg()); }
static ComputePg ComputePgFromString( const char *sourceString,
std::string *logs = NULL ) { ComputePg pg; return (pg.CompileSrcString(sourceString,logs)? pg : ComputePg()); }
private:
typedef std::map<std::string,Uniform> UniformMap;
typedef std::map<std::string,GLint> AttribMap;
typedef std::map<GLint,Texture*> SamplerBindingMap;
typedef std::map<GLint,VBOAttribBuffer*> AttribBindingMap;
/********************\
| Member variable(s) |
\********************/
private:
ComputePg m_Pg;
UniformMap m_Uniforms;
UniformMap m_Samplers;
AttribMap m_Attributes;
SamplerBindingMap m_SamplerBinding;
/*****************************\
| Constructor(s) / destructor |
\*****************************/
public:
inline ComputeShader() : GenericShader() {}
inline ~ComputeShader() { Release(); }
/********************\
| Member function(s) |
\********************/
protected:
void RecoverActiveUniforms();
void RecoverActiveAttributes();
public:
bool Link( std::string *logs = NULL );
ComputeShader& Attach( const ComputePg& pg );
inline bool AttachAndLink( const ComputePg& pg,
std::string *logs = NULL ) { Attach(pg); return Link(logs); }
ComputeShader& DetachComputePg();
void Release();
inline const ComputePg& ComputeProgram() const { return m_Pg; }
inline void Bind() { gpuAssert( glUseProgramObjectARB( Id() ) ); BindSamplers(); }
inline void Unbind() { UnbindSamplers(); gpuAssert( glUseProgramObjectARB( 0 ) ); }
void SetUniform( const std::string& name,
const void *value );
inline void SetSampler( const std::string& name,
const GLint texUnit ) { SetSamplers( name, &texUnit ); }
void SetSamplers( const std::string& name,
const GLint* texUnit );
void SetSamplers( const std::string& name,
const GLint firstTexUnit );
void SetAttrib( const std::string& name,
VBOAttribBuffer *attrib );
void UniformNames( std::vector<std::string> &names ) const;
void SamplerNames( std::vector<std::string> &names ) const;
void AttribNames( std::vector<std::string> &names ) const;
inline const Uniform* UniformLoc( const std::string& name ) const
{
UniformMap::const_iterator u = m_Uniforms.find( name );
return (u!=m_Uniforms.end())? &u->second : NULL;
}
inline GLint SamplerLoc( const std::string& name ) const
{
UniformMap::const_iterator s = m_Samplers.find( name );
return (s!=m_Samplers.end())? s->second.location : -1;
}
inline GLint AttribLoc( const std::string& name ) const
{
AttribMap::const_iterator a = m_Attributes.find( name );
return (a!=m_Attributes.end())? a->second : -1;
}
inline void SetSamplerBinding( const std::string& name, Texture &sampler ) { SetSamplerBinding( name, &sampler ); }
inline void SetSamplerBinding( const std::string& name, Texture *sampler )
{
GLint loc = SamplerLoc( name );
if( loc >= 0 )
m_SamplerBinding[loc] = sampler;
}
inline void RemoveSamplerBinding( const std::string& name )
{
auto sb = m_SamplerBinding.find( SamplerLoc(name) );
if( sb != m_SamplerBinding.end() )
m_SamplerBinding.erase( sb );
}
inline void RemoveAllSamplerBindings()
{
m_SamplerBinding.clear();
}
private:
inline void BindSamplers()
{
GLint texUnit = 0;
for( auto sb=m_SamplerBinding.begin(); sb!=m_SamplerBinding.end(); ++sb )
{
sb->second->Bind( texUnit );
gpuAssert( glUniform1iARB( sb->first, texUnit++ ) );
}
}
inline void UnbindSamplers()
{
for( auto sb=m_SamplerBinding.begin(); sb!=m_SamplerBinding.end(); ++sb )
sb->second->Unbind();
}
};
bool CreateShaderFromSources( GPU::ComputeShader &shader,
const char *pgSrc,
std::string *logs = NULL );
inline bool CreateShaderFromSources( GPU::ComputeShader &shader,
const std::string &pgSrc,
std::string *logs = NULL ) { return CreateShaderFromSources( shader, pgSrc.c_str(), logs ); }
};
#endif //__GPU__COMPUTESHADER_H__
This diff is collapsed.
......@@ -31,19 +31,12 @@ namespace GPU
class VBOAttribBuffer;
class Shader
class GenericShader
{
/******************\
| Internal type(s) |
\******************/
public:
struct Uniform
{
GLint location;
GLint size;
GLenum type;
};
class PgObject : public InstantiatedObject
{
/********************\
......@@ -78,6 +71,43 @@ namespace GPU
GLenum Type() const { return m_PgType; }
};
/*******************\
| Class function(s) |
\*******************/
public:
static std::string GetLogs( GLuint pgId );
/*****************************\
| Constructor(s) / destructor |
\*****************************/
public:
inline GenericShader() : m_Id(0) {}
virtual ~GenericShader() { Unallocate(); }
/********************\
| Member variable(s) |
\********************/
private:
GLhandleARB m_Id;
/********************\
| Member function(s) |
\********************/
public:
inline bool IsCreated() const { return m_Id != 0; }
inline GLhandleARB Id() const { return m_Id; }
protected:
bool Allocate();
bool Unallocate();
};
class Shader : public GenericShader
{
/******************\
| Internal type(s) |
\******************/
public:
class VertPg : public PgObject
{
public:
......@@ -120,6 +150,13 @@ namespace GPU
static GeomPg GeomPgFromString( const char *sourceString,
std::string *logs = NULL ) { GeomPg pg; return (pg.CompileSrcString(sourceString,logs)? pg : GeomPg()); }
struct Uniform
{
GLint location;
GLint size;
GLenum type;
};
private:
typedef std::map<std::string,Uniform> UniformMap;
typedef std::map<std::string,GLint> AttribMap;
......@@ -130,8 +167,6 @@ namespace GPU
| Member variable(s) |
\********************/
private:
GLhandleARB m_Id;
VertPg m_VertPg;
FragPg m_FragPg;
GeomPg m_GeomPg;
......@@ -145,14 +180,8 @@ namespace GPU
| Constructor(s) / destructor |
\*****************************/
public:
inline Shader() : m_Id(0) {}
inline ~Shader() { Release(); }
/*******************\
| Class function(s) |
\*******************/
public:
static std::string GetLogs( GLuint pgId );
inline Shader() : GenericShader() {}
inline ~Shader() { Release(); }
/********************\
| Member function(s) |
......@@ -172,15 +201,13 @@ namespace GPU
Shader& DetachGeomPg();
void Release();
inline bool IsCreated() const { return m_Id != 0; }
inline GLhandleARB Id() const { return m_Id; }
inline const VertPg& VertProgram() const { return m_VertPg; }
inline const FragPg& FragProgram() const { return m_FragPg; }
inline const GeomPg& GeomProgram() const { return m_GeomPg; }
inline GeomPg& GeomProgram() { return m_GeomPg; }
inline void Bind() { gpuAssert( glUseProgramObjectARB( m_Id ) ); BindSamplers(); }
inline void Bind() { gpuAssert( glUseProgramObjectARB( Id() ) ); BindSamplers(); }
inline void Unbind() { UnbindSamplers(); gpuAssert( glUseProgramObjectARB( 0 ) ); }
void SetUniform( const std::string& name,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment