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
00036 #include <Inventor/STL/cassert>
00037 #include <Inventor/STL/map>
00038 #include <Inventor/helpers/SbConstCharMap.h>
00039
00040 class SoGLShaderContainer ;
00041 class SoGLShaderObject ;
00042 class SoFieldSensor ;
00043 class SoNodeSensor ;
00044 class SoFileSensor ;
00045 class SoShaderParser;
00046
00047 #ifdef _MSC_VER
00048 #pragma warning( push )
00049 #pragma warning(disable:4251)
00050 #endif
00051
00290 class SoShaderObject : public SoNode
00291 {
00292
00293 SO_NODE_ABSTRACT_HEADER(SoShaderObject);
00294
00295 public:
00296
00298 enum SourceType
00299 {
00303 ARB_PROGRAM,
00304
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,
00385 GEOMETRY_SHADER,
00387 FRAGMENT_SHADER,
00389 TESSELLATION_CONTROL_SHADER,
00391 TESSELLATION_EVALUATION_SHADER
00392 };
00393
00395 virtual ShaderType getShaderType() const = 0;
00396
00411 template<typename UniformParamType, typename ParamValueType>
00412 void setShaderParameter(const SbString& name, ParamValueType val);
00413
00414 #ifndef HIDDEN_FROM_DOC
00415
00416 #define MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(ParamType)\
00417 \
00418 template<typename ParamValueType>\
00419 void\
00420 setShaderParameter ## ParamType(const SbString& name, ParamValueType val)\
00421 {\
00422 setShaderParameter<SoShaderParameter ## ParamType>(name, val);\
00423 }
00424 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(1i)
00425 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(2i)
00426 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(3i)
00427 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(4i)
00428 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(1f)
00429 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(2f)
00430 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(3f)
00431 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(4f)
00432 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array1f)
00433 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array2f)
00434 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array3f)
00435 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array4f)
00436 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array1i)
00437 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array2i)
00438 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array3i)
00439 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Array4i)
00440 MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER(Matrix)
00441 #undef MAKE_HELPHER_FUNC_SET_SHADER_PARAMETER
00442
00443
00444 #define MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(ParamType)\
00445 \
00446 template<typename ParamValueType>\
00447 SoShaderParameter ## ParamType *\
00448 addShaderParameter ## ParamType(const SbString& name, ParamValueType val)\
00449 {\
00450 return addShaderParameter<SoShaderParameter ## ParamType>(name, val);\
00451 }
00452 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(1i)
00453 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(2i)
00454 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(3i)
00455 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(4i)
00456 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(1f)
00457 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(2f)
00458 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(3f)
00459 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(4f)
00460 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array1f)
00461 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array2f)
00462 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array3f)
00463 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array4f)
00464 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array1i)
00465 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array2i)
00466 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array3i)
00467 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Array4i)
00468 MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER(Matrix)
00469 #undef MAKE_HELPHER_FUNC_ADD_SHADER_PARAMETER
00470 #endif //HIDDEN_FROM_DOC
00471
00472
00473
00474 private:
00475
00477 void createGLShaderObject(SoState* state, SoGLShaderContainer *glShaderContainer,
00478 const SbString& shaderVersion, const SbString& shaderProfile);
00479
00480
00481
00483 SoGLShaderObject* getGLShaderObject() const
00484 { return m_glShaderObject; }
00485
00487 SourceType getSourceType();
00488
00489 static void initClass();
00490 static void exitClass() ;
00491
00496 SbBool isInternal() const;
00497
00498
00499 void updateParameters(SoGLRenderAction* action, int start, int num) ;
00500
00501
00502 void updateAllParameters(SoGLRenderAction* action) ;
00503
00504
00505 void updateStateMatrixParameters(SoGLRenderAction* action) ;
00506
00507
00508 SbBool containStateMatrixParameters() const
00509 { return (m_stateMatrixParameters.getLength() != 0) ; }
00510
00511 SbString getSourceProgram() const
00512 { return m_sourceProgram;}
00513
00517 inline bool isFileFound() const;
00518
00520 inline bool needUpdate() const { return m_needUpdate; }
00521
00523 void invalidate();
00524
00526 void setDefine(const char* name, const char* value);
00527 void setDefine(const char* name, const SbString& value)
00528 { setDefine(name,value.toLatin1()); }
00529
00531 void removeDefine(const char* name);
00532
00534 void checkValidity();
00535
00537 inline void setVersion(const SbString& version) { m_version = version; }
00538
00540 const SbString& getVersion();
00541
00543 const SbString& getProfile();
00544
00546 inline void setProfile(const SbString& profile) { m_profile = profile; }
00547
00549 static bool isShaderObjectAvailable(SoState* state);
00550
00552 static bool isGLSLLanguageAvailable(SoState* state);
00553
00555 void invalidateSource();
00556
00558 void buildSourceToLoad(SbString &builtSource);
00559
00561 SbString getFileAbsolutePath(const SbString& filename);
00562
00563 private:
00564
00566 virtual void notify(SoNotList *list);
00567
00568 friend class SoShaderProgram;
00570 typedef SbConstCharMap<SbString> DefineMap;
00571 typedef DefineMap::iterator DefineMapIt;
00572
00574 typedef std::vector<SbString> SbStringVector;
00575
00577 void setGlobalDefine(const DefineMap& map);
00578
00580 void setDefine(DefineMap::value_type& p);
00581
00583 bool hasDefine(const char* name) const;
00584
00586 typedef std::set<SbString> HeaderSet;
00587 typedef HeaderSet::iterator HeaderSetIt;
00588
00590 void setGlobalHeader(const HeaderSet& map);
00591
00592
00593 void readSource();
00594
00598 SoShaderObject();
00599
00600
00601 virtual ~SoShaderObject();
00602
00603 private:
00604 SoShaderParser* m_parser;
00605 SoGLShaderObject *m_glShaderObject;
00606 SourceType m_sourceType ;
00607 SbString m_sourceProgram;
00608
00610 SbString m_version;
00611 SbString m_profile;
00613 typedef std::map<std::string, std::string> ExtensionMap;
00614 ExtensionMap m_extensionList;
00615
00616 typedef std::map<SbString, SbString> StrStrMap;
00617
00625 void extractInfosFromSource(SbString& version, SbString& source,SbString& profile, ExtensionMap& extensionList);
00626
00628 bool isEmptyLine(const std::string& line) const;
00629
00631 static SbString definesExtension(const SbString& source, const ExtensionMap::value_type& p);
00632
00634 static SbString definesAcc(const SbString& source, const DefineMap::value_type& p);
00635
00636
00637 static void fileSensorCB(void *data, SoSensor *) ;
00638 static void parameterSensorCB(void *data, SoSensor *) ;
00639
00641 DefineMap m_defineMap;
00642
00644 DefineMap m_defineGlobalMap;
00645
00647 HeaderSet m_headerGlobalSet;
00648
00650 SbStringVector m_includeSourceVector;
00651
00653 SoFileSensor* m_fileSensor;
00654
00656 SbString m_directoryFound;
00657
00658
00659 SoFieldSensor *m_paramSensor ;
00660
00661
00662 void updateStateMatrixList() ;
00663
00664
00665 SoNodeList m_stateMatrixParameters ;
00666
00667
00668 bool m_fileFound;
00669
00671 bool m_needUpdate;
00672
00674 bool m_needRead;
00675
00676 static int s_shader_objects_extensionID;
00677 static int s_shading_language_100_extensionID;
00678 static int s_gpu_shader4_extensionID;
00679
00681 static StrStrMap s_shadersPaths;
00682 static SbThreadRWMutex s_shadersPathsMutex;
00683 static bool s_debugCache;
00684 };
00685
00686
00687
00688 template<typename UniformParamType, typename ParamValueType>
00689 UniformParamType*
00690 SoShaderObject::addShaderParameter(const SbString& name, ParamValueType val)
00691 {
00692 UniformParamType *param = dynamic_cast<UniformParamType *>(parameter.findUniformParameterByName(name));
00693 if (param)
00694 {
00695
00696 param->value.setValue(val);
00697 }
00698 else
00699 {
00700 param = new UniformParamType;
00701 param->name = name;
00702 param->value.setValue(val);
00703 parameter.addShaderParameter(param);
00704 }
00705
00706 return param;
00707 }
00708
00709
00710 template<typename UniformParamType, typename ParamValueType>
00711 void
00712 SoShaderObject::setShaderParameter(const SbString& name, ParamValueType val)
00713 {
00714 SoUniformShaderParameter* param = parameter.findUniformParameterByName(name);
00715 assert(param);
00716 assert(param->isOfType(UniformParamType::getClassTypeId()) == TRUE);
00717 UniformParamType* i = static_cast<UniformParamType*>(param);
00718 i->value = val;
00719 }
00720
00721
00722 bool
00723 SoShaderObject::isFileFound() const
00724 {
00725 return m_fileFound;
00726 }
00727
00728 #ifdef _MSC_VER
00729 #pragma warning( pop )
00730 #endif
00731
00732 #endif
00733
00734
00735