00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _SO_SHADER_OBJECT_H_
00026 #define _SO_SHADER_OBJECT_H_
00027
00028 #include <Inventor/SbBasic.h>
00029 #include <Inventor/SbString.h>
00030 #include <Inventor/nodes/SoNode.h>
00031 #include <Inventor/fields/SoSFFilePathString.h>
00032 #include <Inventor/fields/SoSFEnum.h>
00033 #include <Inventor/fields/SoSFBool.h>
00034 #include <Inventor/fields/SoMFUniformShaderParameter.h>
00035 #include <Inventor/SbEnums.h>
00036
00037 #include <Inventor/STL/cassert>
00038 #include <Inventor/STL/map>
00039 #include <Inventor/helpers/SbConstCharMap.h>
00040
00041 class SoGLShaderContainer ;
00042 class SoGLShaderObject ;
00043 class SoFieldSensor ;
00044 class SoNodeSensor ;
00045 class SoFileSensor ;
00046 class SoShaderParser;
00047
00048 #ifdef _MSC_VER
00049 #pragma warning( push )
00050 #pragma warning(disable:4251)
00051 #endif
00052
00288 class SoShaderObject : public SoNode
00289 {
00290
00291 SO_NODE_ABSTRACT_HEADER(SoShaderObject);
00292
00293 public:
00294
00296 enum SourceType
00297 {SoDEPRECATED
00302 ARB_PROGRAM,
00303 SoDEPRECATED
00308 CG_PROGRAM,
00309
00313 GLSL_PROGRAM,
00314
00318 FILENAME
00319 };
00320
00324 SoSFBool isActive;
00325
00336 SoSFEnum sourceType;
00337
00352 SoSFFilePathString sourceProgram;
00353
00358 SoMFUniformShaderParameter parameter;
00359
00376 template<typename UniformParamType, typename ParamValueType>
00377 UniformParamType* addShaderParameter(const SbString& name, ParamValueType val);
00378
00380 enum ShaderType
00381 {
00383 VERTEX_SHADER = SbEnums::SHADER_TYPE_VERTEX,
00385 GEOMETRY_SHADER = SbEnums::SHADER_TYPE_GEOMETRY,
00387 FRAGMENT_SHADER = SbEnums::SHADER_TYPE_FRAGMENT,
00389 TESSELLATION_CONTROL_SHADER = SbEnums::SHADER_TYPE_TESS_CTRL,
00391 TESSELLATION_EVALUATION_SHADER = SbEnums::SHADER_TYPE_TESS_EVAL,
00393 COMPUTE_SHADER = SbEnums::SHADER_TYPE_COMPUTE,
00394 };
00395
00397 virtual ShaderType getShaderType() const = 0;
00398
00413 template<typename UniformParamType, typename ParamValueType>
00414 void setShaderParameter(const SbString& name, ParamValueType val);
00415
00416 #ifndef HIDDEN_FROM_DOC
00417
00418 #define MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(ParamType)\
00419 \
00420 template<typename ParamValueType>\
00421 void\
00422 setShaderParameter ## ParamType(const SbString& name, ParamValueType val)\
00423 {\
00424 setShaderParameter<SoShaderParameter ## ParamType>(name, val);\
00425 }
00426 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(1i)
00427 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(2i)
00428 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(3i)
00429 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(4i)
00430 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(1f)
00431 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(2f)
00432 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(3f)
00433 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(4f)
00434 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array1f)
00435 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array2f)
00436 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array3f)
00437 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array4f)
00438 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array1i)
00439 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array2i)
00440 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array3i)
00441 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array4i)
00442 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Matrix)
00443 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Matrix3)
00444 #undef MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER
00445
00446
00447 #define MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(ParamType)\
00448 \
00449 template<typename ParamValueType>\
00450 SoShaderParameter ## ParamType *\
00451 addShaderParameter ## ParamType(const SbString& name, ParamValueType val)\
00452 {\
00453 return addShaderParameter<SoShaderParameter ## ParamType>(name, val);\
00454 }
00455 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(1i)
00456 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(2i)
00457 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(3i)
00458 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(4i)
00459 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(1f)
00460 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(2f)
00461 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(3f)
00462 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(4f)
00463 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array1f)
00464 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array2f)
00465 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array3f)
00466 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array4f)
00467 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array1i)
00468 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array2i)
00469 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array3i)
00470 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array4i)
00471 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Matrix)
00472 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Matrix3)
00473 #undef MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER
00474 #endif //HIDDEN_FROM_DOC
00475
00476
00477
00478 private:
00479
00481 void setShaderObjectInfos(SoState* state, const SbString& shaderVersion, const SbString& shaderProfile);
00482
00484 SourceType getSourceType();
00485
00486 static void initClass();
00487 static void exitClass() ;
00488
00493 SbBool isInternal() const;
00494
00495
00496 SbBool containStateMatrixParameters() const
00497 { return (m_stateMatrixParameters.getLength() != 0) ; }
00498
00499 const SbString& getSourceProgram() const
00500 { return m_sourceProgram;}
00501
00505 inline bool isFileFound() const;
00506
00508 inline bool needUpdate() const { return m_needUpdate; }
00509
00511 void invalidate();
00512
00514 void setDefine(const char* name, const char* value);
00515 void setDefine(const char* name, const SbString& value)
00516 { setDefine(name,value.toLatin1()); }
00517
00519 void removeDefine(const char* name);
00520
00522 void checkValidity();
00523
00525 inline void setVersion(const SbString& version) { m_version = version; }
00526
00528 const SbString& getVersion();
00529
00531 const SbString& getProfile();
00532
00534 inline void setProfile(const SbString& profile) { m_profile = profile; }
00535
00537 static bool isShaderObjectAvailable(SoState* state);
00538
00540 static bool isGLSLLanguageAvailable(SoState* state);
00541
00543 void invalidateSource();
00544
00546 void buildSourceToLoad(SbString &builtSource);
00547
00549 SbString getFileAbsolutePath(const SbString& filename);
00550
00552 virtual void notify(SoNotList *list);
00553
00555 void getHeaderFilenames( std::vector<SbString>& filenames );
00556
00558 void setIsFixedPipeline(bool isFixedPipelineShader)
00559 {
00560 m_isFixedPipelineShader = isFixedPipelineShader;
00561 }
00562
00564 bool isFixedPipeline() const
00565 {
00566 return m_isFixedPipelineShader;
00567 }
00568
00569 private:
00570
00571 friend class SoShaderProgram;
00573 typedef SbConstCharMap<SbString> DefineMap;
00574 typedef DefineMap::iterator DefineMapIt;
00575
00577 typedef std::vector<SbString> SbStringVector;
00578
00580 void setGlobalDefine(const DefineMap& map);
00581
00583 void setDefine(DefineMap::value_type& p);
00584
00586 bool hasDefine(const char* name) const;
00587
00589 typedef std::set<SbString> HeaderSet;
00590 typedef HeaderSet::iterator HeaderSetIt;
00591
00593 void setGlobalHeader(const HeaderSet& map);
00594
00595
00596 void readSource();
00597
00601 SoShaderObject();
00602
00603
00604 virtual ~SoShaderObject();
00605
00606 private:
00607 SoShaderParser* m_parser;
00608 SbString m_sourceProgram;
00609
00611 SbString m_version;
00612 SbString m_profile;
00614 typedef std::map<std::string, std::string> ExtensionMap;
00615 ExtensionMap m_extensionList;
00616
00617 typedef std::map<SbString, SbString> StrStrMap;
00618
00626 void extractInfosFromSource(SbString& version, SbString& source,SbString& profile, ExtensionMap& extensionList);
00627
00629 bool isEmptyLine(const std::string& line) const;
00630
00632 static SbString definesExtension(const SbString& source, const ExtensionMap::value_type& p);
00633
00635 static SbString definesAcc(const SbString& source, const DefineMap::value_type& p);
00636
00639 void getSourceFilename(std::vector<SbString>& filenames) const;
00640
00642 DefineMap m_defineMap;
00643
00645 DefineMap m_defineGlobalMap;
00646
00648 HeaderSet m_headerGlobalSet;
00649
00651 SbStringVector m_includeSourceVector;
00652
00654 SbStringVector m_includeFilenameVector;
00655
00657 SbString m_directoryFound;
00658
00659
00660 void updateStateMatrixList() ;
00661
00662
00663 SoNodeList m_stateMatrixParameters ;
00664
00665
00666 bool m_fileFound;
00667
00669 bool m_needUpdate;
00670
00672 bool m_needRead;
00673
00674 bool m_isFixedPipelineShader;
00675
00677 static StrStrMap s_shadersPaths;
00678 static SbThreadRWMutex s_shadersPathsMutex;
00679 static bool s_debugCache;
00680 };
00681
00682
00683
00684 template<typename UniformParamType, typename ParamValueType>
00685 UniformParamType*
00686 SoShaderObject::addShaderParameter(const SbString& name, ParamValueType val)
00687 {
00688 UniformParamType *param = dynamic_cast<UniformParamType *>(parameter.findUniformParameterByName(name));
00689 if (param)
00690 {
00691
00692 param->value.setValue(val);
00693 }
00694 else
00695 {
00696 param = new UniformParamType;
00697 param->name = name;
00698 param->value.setValue(val);
00699 parameter.addShaderParameter(param);
00700 }
00701
00702 return param;
00703 }
00704
00705
00706 template<typename UniformParamType, typename ParamValueType>
00707 void
00708 SoShaderObject::setShaderParameter(const SbString& name, ParamValueType val)
00709 {
00710 SoUniformShaderParameter* param = parameter.findUniformParameterByName(name);
00711 assert(param);
00712 assert(param->isOfType(UniformParamType::getClassTypeId()) == TRUE);
00713 UniformParamType* i = static_cast<UniformParamType*>(param);
00714 i->value = val;
00715 }
00716
00717
00718 bool
00719 SoShaderObject::isFileFound() const
00720 {
00721 return m_fileFound;
00722 }
00723
00724 #ifdef _MSC_VER
00725 #pragma warning( pop )
00726 #endif
00727
00728 #endif
00729
00730
00731