[shaderz] r18 committed - First pass for the shader parsing. Deals with CG shaders only at the m...

1 view
Skip to first unread message

sha...@googlecode.com

unread,
Mar 2, 2010, 4:02:24 PM3/2/10
to shade...@googlegroups.com
Revision: 18
Author: vijay.michaeljoseph
Date: Tue Mar 2 13:01:51 2010
Log: First pass for the shader parsing. Deals with CG shaders only at the
moment, and has some debug info still in it.
http://code.google.com/p/shaderz/source/detail?r=18

Added:
/trunk/include/shshaderinfo.h
/trunk/src/shshaderinfo.cpp

=======================================
--- /dev/null
+++ /trunk/include/shshaderinfo.h Tue Mar 2 13:01:51 2010
@@ -0,0 +1,102 @@
+#ifndef SHSHADERINFO_H
+#define SHSHADERINFO_H
+
+#include <CG/cg.h>
+#include <CG/cggl.h>
+#include <string>
+#include <vector>
+
+namespace Shaderz
+{
+ typedef enum
+ {
+ SH_PARAMETERCLASS_UNKNOWN = 0,
+ SH_PARAMETERCLASS_SCALAR,
+ SH_PARAMETERCLASS_VECTOR,
+ SH_PARAMETERCLASS_MATRIX,
+ SH_PARAMETERCLASS_STRUCT,
+ SH_PARAMETERCLASS_ARRAY,
+ SH_PARAMETERCLASS_SAMPLER,
+ SH_PARAMETERCLASS_OBJECT,
+
+ SH_PARAMETERCLASS_MAX
+ } shParameterClass;
+
+ typedef enum
+ {
+ SH_PARAMETERTYPE_UNKNOWN = 0,
+ SH_PARAMETERTYPE_HALF,
+ SH_PARAMETERTYPE_INT,
+ SH_PARAMETERTYPE_FLOAT,
+ SH_PARAMETERTYPE_BOOL,
+ SH_PARAMETERTYPE_SAMPLER1D,
+ SH_PARAMETERTYPE_SAMPLER2D,
+ SH_PARAMETERTYPE_SAMPLER3D,
+ SH_PARAMETERTYPE_SAMPLERRECT,
+ SH_PARAMETERTYPE_SAMPLERCUBE,
+
+ SH_PARAMETERTYPE_MAX
+ } shParameterType;
+
+ /**
+ * Base class for shader program information.
+ */
+ class shShaderInfo
+ {
+ public:
+ struct shShaderParameter
+ {
+ std::string name; // name of the parameter
as it exists in the shader
+ shParameterClass paramClass; // the class this
parameter belongs to
+ shParameterType paramType; // the type this
parameter belongs to
+ int rows; // the number of
rows (default=1 for all, >1 for matrix/vectors)
+ int columns; // the number of
columns for this parameter
+ std::vector<shShaderParameter> memberList; // the members
(iff this is a struct)
+ };
+
+ public:
+ shShaderInfo();
+ shShaderInfo(const std::string sShaderFile);
+ virtual ~shShaderInfo(){}
+
+ void setFile(const std::string sShaderFile);
+ virtual bool parseShader(void);
+ virtual void dumpParameterInfo(std::vector<shShaderParameter>*
pInParameterList = 0, int indentLevel=0);
+
+ protected:
+ virtual void initShaderInfo();
+
+ protected:
+ std::string m_sShaderFile;
+ std::vector<shShaderParameter> m_parameterList;
+ };
+
+ /**
+ * ShaderInfo implementation for shaders writen in CG
+ */
+ class shShaderInfoCG : public shShaderInfo
+ {
+ public:
+ shShaderInfoCG();
+ shShaderInfoCG(std::string sShaderFile);
+ virtual ~shShaderInfoCG(){}
+ virtual bool parseShader(void);
+
+ protected:
+ virtual void initShaderInfo();
+ virtual void collectParameterInfo( CGparameter currentParam,
std::vector<shShaderParameter>& paramRoot );
+ virtual bool getParameterInfo( const CGparameter& currentParam,
shShaderParameter& paramInfo ) const;
+ virtual shParameterClass translateCGParamClass( const
CGparameterclass& cgParamClass ) const;
+ virtual shParameterType translateCGParamType( const CGtype&
cgParamType ) const;
+ virtual bool getParamSizeInfo( const CGparameter& cgParam, int&
columns, int& rows ) const;
+
+ protected:
+ CGprogram m_ShaderHandle;
+ CGcontext m_Context;
+ CGprofile m_VertexProfile;
+
+ private:
+ typedef shShaderInfo ParentClass;
+ };
+}
+#endif // SHSHADERINFO_H
=======================================
--- /dev/null
+++ /trunk/src/shshaderinfo.cpp Tue Mar 2 13:01:51 2010
@@ -0,0 +1,201 @@
+#include "shshaderinfo.h"
+#include "shlogger.h"
+
+using namespace Shaderz;
+
+shShaderInfo::shShaderInfo()
+{
+}
+
+shShaderInfo::shShaderInfo(const std::string sShaderFile)
+{
+ setFile(sShaderFile);
+}
+
+void shShaderInfo::setFile(const std::string sShaderFile)
+{
+ m_sShaderFile = sShaderFile;
+}
+
+bool shShaderInfo::parseShader(void)
+{
+ return false;
+}
+
+void shShaderInfo::initShaderInfo()
+{
+ // nothing to be done here
+}
+
+void shShaderInfo::dumpParameterInfo(std::vector<shShaderParameter>*
pInParameterList, int indentLevel)
+{
+ int index;
+ std::vector<shShaderParameter> parameterRoot = pInParameterList ?
*pInParameterList : m_parameterList;
+ for( index=0; index < parameterRoot.size(); ++index )
+ {
+ for( int indentIndex=0; indentIndex<indentLevel; ++indentIndex )
LOG<<"\t";
+ LOG<<parameterRoot[index].name<<" ";
+ LOG<<"Class="<<parameterRoot[index].paramClass<<" ";
+ LOG<<"Type="<<parameterRoot[index].paramType<<" ";
+ LOG<<"Size="<<parameterRoot[index].columns<<"
x "<<parameterRoot[index].rows;
+ if( SH_PARAMETERCLASS_STRUCT == parameterRoot[index].paramClass )
+ {
+ LOG<<"\n";
+ dumpParameterInfo( &parameterRoot[index].memberList,
indentLevel+1 );
+ }
+ LOG<<"\n";
+ }
+}
+
+
+//////
+// shShaderInfoCG
+/////
+shShaderInfoCG::shShaderInfoCG()
+ : ParentClass()
+{
+ initShaderInfo();
+}
+
+shShaderInfoCG::shShaderInfoCG(std::string sShaderFile)
+ : ParentClass(sShaderFile)
+{
+ initShaderInfo();
+}
+
+bool shShaderInfoCG::parseShader(void)
+{
+ ParentClass::parseShader();
+
+ LOG<<"Parsing shader: "<<m_sShaderFile<<"\nContext="<<m_Context<<"\n";
+ m_ShaderHandle = cgCreateProgramFromFile( m_Context, CG_SOURCE,
m_sShaderFile.c_str(), m_VertexProfile, "main", 0 );
+ if( m_ShaderHandle == 0 )
+ {
+ LOG<<"Failed to create program handle.\n";
+ CGerror err = cgGetError();
+ LOG<<"CgErr = "<<cgGetErrorString(err)<<"\n";
+ return false;
+ }
+ // search for Global and Program parameters
+ CGenum query[] = { CG_GLOBAL, CG_PROGRAM };
+ for( unsigned int i=0; i<sizeof(query)/sizeof(query[0]); ++i )
+ {
+ CGparameter pParam = cgGetFirstParameter( m_ShaderHandle,
query[i] );
+ collectParameterInfo( pParam, m_parameterList );
+ }
+
+ dumpParameterInfo();
+
+ return true;
+}
+
+void shShaderInfoCG::collectParameterInfo( CGparameter currentParam,
std::vector<shShaderParameter>& paramRoot )
+{
+ while(currentParam)
+ {
+ struct shShaderParameter paramInfo;
+ getParameterInfo( currentParam, paramInfo );
+ if( SH_PARAMETERCLASS_STRUCT == paramInfo.paramClass )
+ {
+ CGparameter structParam =
cgGetFirstStructParameter(currentParam);
+ collectParameterInfo(structParam, paramInfo.memberList);
+ }
+ paramRoot.push_back(paramInfo);
+ currentParam = cgGetNextParameter( currentParam );
+ }
+}
+
+shParameterClass shShaderInfoCG::translateCGParamClass( const
CGparameterclass& cgParamClass ) const
+{
+ switch(cgParamClass)
+ {
+ case CG_PARAMETERCLASS_SCALAR: return SH_PARAMETERCLASS_SCALAR;
+ case CG_PARAMETERCLASS_VECTOR: return SH_PARAMETERCLASS_VECTOR;
+ case CG_PARAMETERCLASS_MATRIX: return SH_PARAMETERCLASS_MATRIX;
+ case CG_PARAMETERCLASS_STRUCT: return SH_PARAMETERCLASS_STRUCT;
+ case CG_PARAMETERCLASS_ARRAY: return SH_PARAMETERCLASS_ARRAY;
+ case CG_PARAMETERCLASS_SAMPLER: return SH_PARAMETERCLASS_SAMPLER;
+ case CG_PARAMETERCLASS_OBJECT: return SH_PARAMETERCLASS_OBJECT;
+ case CG_PARAMETERCLASS_UNKNOWN:
+ case CG_PARAMETERCLASS_MAX:
+ default: return SH_PARAMETERCLASS_UNKNOWN;
+ }
+
+ return SH_PARAMETERCLASS_UNKNOWN;
+}
+
+shParameterType shShaderInfoCG::translateCGParamType( const CGtype&
cgParamType ) const
+{
+ switch(cgParamType)
+ {
+ case CG_HALF: return SH_PARAMETERTYPE_HALF;
+ case CG_INT: return SH_PARAMETERTYPE_INT;
+ case CG_FLOAT: return SH_PARAMETERTYPE_FLOAT;
+ case CG_BOOL: return SH_PARAMETERTYPE_BOOL;
+ case CG_SAMPLER1D: return SH_PARAMETERTYPE_SAMPLER1D;
+ case CG_SAMPLER2D: return SH_PARAMETERTYPE_SAMPLER2D;
+ case CG_SAMPLER3D: return SH_PARAMETERTYPE_SAMPLER3D;
+ case CG_SAMPLERRECT: return SH_PARAMETERTYPE_SAMPLERRECT;
+ case CG_SAMPLERCUBE: return SH_PARAMETERTYPE_SAMPLERCUBE;
+ default: return SH_PARAMETERTYPE_UNKNOWN;
+ }
+
+ return SH_PARAMETERTYPE_UNKNOWN;
+}
+
+bool shShaderInfoCG::getParamSizeInfo( const CGparameter& cgParam, int&
columns, int& rows ) const
+{
+ // create the initial mapping for type and size of parameters
+ struct cgParamTypeSizeMapping
+ {
+ CGtype _type;
+ int _col;
+ int _rows;
+ };
+
+#define CG_DATATYPE_MACRO(name, compiler_name, enum_name, base_name,
ncols, nrows, pc) \
+ {enum_name, ncols, nrows},
+
+ cgParamTypeSizeMapping map[] = {
+#include <CG/cg_datatypes.h>
+ };
+#undef CG_DATATYPE_MACRO
+
+ CGtype paramType = cgGetParameterType(cgParam);
+ int mapIndex;
+ for( mapIndex=0; mapIndex < sizeof(map)/sizeof(map[0]); ++mapIndex )
+ {
+ if( paramType == map[mapIndex]._type )
+ {
+ columns = map[mapIndex]._col;
+ rows = map[mapIndex]._rows;
+ break;
+ }
+ }
+
+ return true;
+}
+
+void shShaderInfoCG::initShaderInfo()
+{
+ m_Context = cgCreateContext();
+ if( m_Context == 0 )
+ {
+ LOG<<"Failed to create context.\n";
+ }
+
+ m_VertexProfile = cgGLGetLatestProfile( CG_GL_VERTEX );
+ if( m_VertexProfile == CG_PROFILE_UNKNOWN )
+ {
+ LOG<<"Failed to get profile.\n";
+ }
+}
+
+bool shShaderInfoCG::getParameterInfo( const CGparameter& currentParam,
shShaderParameter& paramInfo ) const
+{
+ paramInfo.name = std::string(cgGetParameterName(currentParam));
+ paramInfo.paramClass = translateCGParamClass(
cgGetParameterClass(currentParam) );
+ paramInfo.paramType =
translateCGParamType(cgGetParameterBaseType(currentParam));
+ getParamSizeInfo( currentParam, paramInfo.columns, paramInfo.rows );
+ return true;
+}

Reply all
Reply to author
Forward
0 new messages