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_PROGRAM_H_
00026 #define _SO_SHADER_PROGRAM_H_
00027
00028 #include <Inventor/SbBasic.h>
00029 #include <Inventor/nodes/SoNode.h>
00030 #include <Inventor/nodes/SoNode.h>
00031 #include <Inventor/fields/SoMFNode.h>
00032 #include <Inventor/nodes/SoShaderParameter.h>
00033 #include <Inventor/nodes/SoFragmentShader.h>
00034 #include <Inventor/nodes/SoVertexShader.h>
00035 #include <Inventor/nodes/SoTessellationControlShader.h>
00036 #include <Inventor/nodes/SoTessellationEvaluationShader.h>
00037 #include <Inventor/nodes/SoTexture.h>
00038 #include <Inventor/nodes/SoGeometryShader.h>
00039 #include <Inventor/STL/vector>
00040 #include <Inventor/STL/cassert>
00041 #include <Inventor/helpers/SbConstCharMap.h>
00042 #include <Inventor/elements/SoEnvironmentElement.h>
00043
00044 #ifdef _WIN32
00045 #pragma warning(push)
00046 #pragma warning(disable:4251)
00047 #endif
00048
00049 class SoGLRenderAction;
00050 class SoGLShaderProgram ;
00051 class SoGLShaderObject;
00052 class SoFieldSensor ;
00053 class SoLight;
00054 class SoCache;
00055
00056
00057
00218 class SoShaderProgram : public SoNode
00219 {
00220
00221 SO_NODE_HEADER( SoShaderProgram );
00222
00223 public:
00225 enum GeometryInputType
00226 {
00233 POINTS_INPUT = GL_POINTS,
00234
00243 LINES_INPUT = GL_LINES,
00244
00253 TRIANGLES_INPUT = GL_TRIANGLES
00254 };
00255
00259 enum GeometryOutputType
00260 {
00261 POINTS_OUTPUT = GL_POINTS,
00262 LINE_STRIP_OUTPUT = GL_LINE_STRIP,
00266 TRIANGLE_STRIP_OUTPUT = GL_TRIANGLE_STRIP
00267 };
00268
00275 SoMFNode shaderObject;
00276
00283 SoSFEnum geometryInputType;
00284
00291 SoSFEnum geometryOutputType;
00292
00299 SoSFBool vertexProgramTwoSide;
00300
00318 SoSFBool shadowShader;
00319
00326 SoSFInt32 maxGeometryOutputVertices;
00327
00328
00339 SoSFBool generateTransparency;
00340
00347 SoSFInt32 patchLength;
00348
00354 SoMFNode bufferObjects;
00355
00359 SoShaderProgram();
00360
00364 inline SoFragmentShader* getFragmentShader(int pos) const;
00365
00369 inline SoVertexShader* getVertexShader(int pos) const;
00370
00375 inline SoGeometryShader* getGeometryShader(int pos) const;
00376
00381 virtual SoFragmentShader* setFragmentShader(int pos, const SbString& filenameOrSource,
00382 SoShaderObject::SourceType sourceType = SoShaderObject::FILENAME);
00383
00388 virtual SoVertexShader* setVertexShader(int pos, const SbString& filenameOrSource,
00389 SoShaderObject::SourceType sourceType = SoShaderObject::FILENAME);
00390
00396 virtual SoGeometryShader* setGeometryShader(int pos, const SbString& filenameOrSource,
00397 SoShaderObject::SourceType sourceType = SoShaderObject::FILENAME);
00398
00403 static unsigned int getNumReservedTextures();
00404
00408 inline SoTessellationControlShader* getTessellationControlShader(int pos) const;
00409
00413 inline SoTessellationEvaluationShader* getTessellationEvaluationShader(int pos) const;
00414
00419 virtual SoTessellationControlShader* setTessellationControlShader(int pos, const SbString& filenameOrSource,
00420 SoShaderObject::SourceType sourceType = SoShaderObject::FILENAME);
00421
00426 virtual SoTessellationEvaluationShader* setTessellationEvaluationShader(int pos, const SbString& filenameOrSource,
00427 SoShaderObject::SourceType sourceType = SoShaderObject::FILENAME);
00428
00429
00430
00431 private:
00432
00433
00434 virtual void doAction(SoAction *action);
00435 virtual void GLRender( SoGLRenderAction *action );
00436 virtual void getBoundingBox( SoGetBoundingBoxAction *action );
00437 virtual void pick( SoPickAction *action );
00438
00439 private:
00441 enum LightType
00442 {
00443 LIGHT_TYPE_NONE,
00444 LIGHT_TYPE_POINT,
00445 LIGHT_TYPE_SPOT,
00446 LIGHT_TYPE_DIRECTIONAL
00447 };
00448
00450 enum EnvMode
00451 {
00452 ENV_MODE_MODULATE,
00453 ENV_MODE_REPLACE,
00454 ENV_MODE_DECAL,
00455 ENV_MODE_BLEND,
00456 ENV_MODE_ADD,
00457 ENV_MODE_NONE
00458 };
00459
00461 enum TexGenMode
00462 {
00463 TEXGEN_TYPE_NONE,
00464 TEXGEN_TYPE_OBJECT_PLANE,
00465 TEXGEN_TYPE_REFLECTION_MAP,
00466 TEXGEN_TYPE_SPHERE_MAP,
00467 TEXGEN_TYPE_NORMAL_MAP,
00468 TEXGEN_TYPE_EYE_PLANE
00469 };
00470
00472 enum GPUVendorType
00473 {
00474 GPU_TYPE_NOT_INITIALIZED = -1,
00475 GPU_NVIDIA = 0,
00476 GPU_ATI = 1,
00477 GPU_INTEL = 2,
00478 GPU_FIREPRO_MAC = 3
00479 };
00480
00481
00482 static bool isOivReservedName(const std::string ¶mName);
00483
00485 virtual void notify(SoNotList *list);
00486
00488 void setDefine(const char* name, const char* value);
00489 void setDefine(const char* name, const SbString& value)
00490 { setDefine(name,value.toLatin1()); }
00491
00495 bool getDefine(const char* name, SbString& value) const;
00496
00498 void removeDefine(const char* name);
00499
00501 void setHeader(const SbString& filename);
00502
00504 void removeHeader(const SbString& name);
00505
00509 static GLint offsetToGLTextureUnit(SoGLRenderAction* action, const char* offset);
00510
00514 static int offsetToTextureUnit(SoGLRenderAction* action, const char* offset);
00515
00516
00517 static void initClass();
00518 static void exitClass();
00519
00520
00521 void updateStateMatrixParameters( SoState *state );
00522
00523
00524
00525 SbBool containStateMatrixParameters() const;
00526
00527 void getAttribLocation(SoGLRenderAction* action, const char* paramName, int32_t identifier, int& firstInd);
00528 void getAttribSize(SoGLRenderAction* action, SoVertexShaderParameter * param, int& size);
00529
00530 SoGLShaderProgram* getGLShaderProgram() const;
00531
00533 void invalidate();
00534
00536 void setModelMatrix(SoGLRenderAction* actio, const SbMatrix& modelMatrix);
00537
00539 void setTwoSidedLighting(SoGLRenderAction* action);
00540
00542 void setCounterClockWise(SoGLRenderAction* action);
00543
00545 void setTexturing(SoGLRenderAction* action);
00546
00548 void setViewMatrix(SoGLRenderAction* action, const SbMatrix& viewMatrix);
00549
00551 void setProjectionMatrix(SoGLRenderAction* action, const SbMatrix& projectionMatrix);
00552
00554 void setModelViewProjectionMatrix(SoGLRenderAction* action, const SbMatrix& mvpMatrix);
00555
00557 void setNormalMatrix(SoGLRenderAction* action, const SbMatrix& normalMatrix);
00558
00560 void setInstanceModelMatrix(SoGLRenderAction* action, const SbMatrix& instanceModelMatrix);
00561
00563 void setInstanceUseMaterialColor(SoGLRenderAction* action, bool instanceUseMaterialColor);
00564
00566 void setViewportSize(SoGLRenderAction* action, const SbVec2f& viewportSize);
00567
00569 void setLight(SoGLRenderAction* action, int num, LightType type);
00570
00572 void setClipPlaneParameters(SoGLRenderAction* action);
00573
00575 void setShadowParameters(SoGLRenderAction* action);
00576
00578 void setLights(SoGLRenderAction* action);
00579
00581 void setFog(SoState* state);
00582
00584 void sendLazyElementUniforms(SoState* state);
00585
00587 void setBuiltInUniforms( SoGLRenderAction* action );
00588
00590 SoGLShaderObject* getFirstGLShaderObject(SoState* state) const;
00591
00593 void setShaderParameter1i(SoGLRenderAction* action, const char* paramName,
00594 int value);
00596 void setShaderParameter2i(SoGLRenderAction* action, const char* paramName,
00597 const SbVec2i32& value);
00599 void setShaderParameter3i(SoGLRenderAction* action, const char* paramName,
00600 const SbVec3i32& value);
00602 void setShaderParameter4i(SoGLRenderAction* action, const char* paramName,
00603 const SbVec4i32& value);
00605 void setShaderParameterArray1i(SoGLRenderAction* action, const char* paramName,
00606 size_t numElem, const int* values);
00608 void setShaderParameterArray2i(SoGLRenderAction* action, const char* paramName,
00609 size_t numElem, const SbVec2i32* values);
00611 void setShaderParameterArray3i(SoGLRenderAction* action, const char* paramName,
00612 size_t numElem, const SbVec3i32* values);
00614 void setShaderParameterArray4i(SoGLRenderAction* action, const char* paramName,
00615 size_t numElem, const SbVec4i32* values);
00617 void setShaderParameterArray1f(SoGLRenderAction* action, const char* paramName,
00618 size_t numElem, const float* values);
00620 void setShaderParameterArray2f(SoGLRenderAction* action, const char* paramName,
00621 size_t numElem, const SbVec2f* values);
00623 void setShaderParameterArray3f(SoGLRenderAction* action, const char* paramName,
00624 size_t numElem, const SbVec3f* values);
00626 void setShaderParameterArray4f(SoGLRenderAction* action, const char* paramName,
00627 size_t numElem, const SbVec4f* values);
00629 void setShaderParameter1f(SoGLRenderAction* action, const char* paramName,
00630 float value);
00632 void setShaderParameter2f(SoGLRenderAction* action, const char* paramName,
00633 const SbVec2f& value);
00635 void setShaderParameter3f(SoGLRenderAction* action, const char* paramName,
00636 const SbVec3f& value);
00638 void setShaderParameter4f(SoGLRenderAction* action, const char* paramName,
00639 const SbVec4f& value);
00642 void setShaderParameterMatrix(SoGLRenderAction* action, const char* paramName,
00643 const SbMatrix& matrix);
00644
00646 void setShaderParameterMatrix4(SoGLRenderAction* action, const char* paramName,
00647 const SbMatrix& matrix);
00648
00650 void disableLight(SoGLRenderAction* action, int num);
00651
00653 void setLight(SoState* state, SoLight* light);
00654
00656 void enableLighting(SoState* state, bool flag);
00657
00659 void enableColorMaterial(SoState* state, bool flag);
00660
00662 void enableTexturing(SoState* state, const std::vector<bool>& flag);
00663
00665 void enableTexturing(SoState* state);
00666
00668 void enableTexGen(SoState* state, TexGenMode mode);
00669
00671 void setTexEnv(SoState* state, SoTexture::Model model);
00672
00674 bool isGlslProgram() const;
00675
00677 bool isShadowShader() const;
00678
00680 SoShaderProgram* getShadowPassShader() const;
00681
00683 void setShadowShader(bool flag);
00684
00687 void setHiddenShaderObject(const char* objName, SoShaderObject* obj);
00688
00690 void removeHiddenShaderObject(const char* objName);
00691
00693 SoShaderObject* getHiddenShaderObject(const char* objName);
00694
00696 const SbConstCharMap<SoShaderObject*>& getHiddenShaderObjects() const;
00697
00699 static GPUVendorType getGPUVendorType();
00700
00701 private:
00702 typedef std::vector<SoShaderObject*> ShaderObjectVector;
00703 typedef SbConstCharMap<SoShaderObject*> ShaderObjectMap;
00705 typedef SoShaderObject::DefineMap DefineMap;
00706 typedef SoShaderObject::HeaderSet HeaderSet;
00707
00709 struct Members
00710 {
00711 Members();
00712 ~Members();
00713 void unref();
00714
00715 ShaderObjectVector& getShadowShaderLib();
00716
00717 SoGLShaderProgram *m_glShaderProgram;
00719 bool m_isShadowShader;
00720 std::vector<LightType> m_lights;
00721
00723 ShaderObjectMap m_hiddenShaderObjects;
00724
00725
00726
00727
00728 std::vector<SoShaderObject*> m_prevShaderObject;
00729 std::vector< SoNode * > m_toUnrefShaderObjects;
00730
00731
00732 bool m_geomParametersChanged;
00733
00735 SoShaderProgram* m_shadowPassShader;
00736
00738 SoCache* m_cache;
00739
00741 DefineMap m_defineMap;
00742
00744 HeaderSet m_headerMap;
00745
00747 bool m_isValid;
00748
00750 ShaderObjectVector m_shadowShaderLib;
00751
00752 private:
00754 void createShadowShaderLib();
00755
00756 };
00757 SbThreadStorage<Members*> m_members;
00758
00760 void shadowMapGLRender(SoGLRenderAction* action);
00761
00763 void shadowMapDoAction(SoAction* action);
00764
00766 void initShadowMapShader(SoAction* action);
00767
00769 virtual SoShaderProgram* generateShadowShader() const;
00770
00771
00772 virtual ~SoShaderProgram();
00773
00774
00775 SbBool isValidShaderObjects() const;
00776
00777
00778
00779 SbBool isOneShaderObjectActive();
00780
00782 static SoShaderProgram::Members* getMembers(const SoShaderProgram* prog);
00783
00784 private:
00785
00786 SoMFNode prevShaderObject;
00787
00788 enum
00789 {
00790 SHADOW_LIBRARY_FRAG,
00791 GENERATE_SHADOWMAP_FRAG,
00792 SHADOW_LIBRARY_VERT,
00793 SHADER_LAST
00794 };
00795
00797 void addDefines(SoShaderObject* obj);
00798
00800 void addHeaders(SoShaderObject* obj);
00801
00803 void setShadowLibraryDefines(const ShaderObjectVector& shaderObjectsList);
00804
00806 void updateCache(SoState *state);
00807
00809 void addShadowLibrary(SoState* state, ShaderObjectVector& shaderObjectsList);
00810
00812 void enableShadowLibrary(bool flag);
00813
00815 void addHiddenShaderObjects(ShaderObjectVector& shaderObjectsList);
00816
00818 static LightType lightToLightType(SoLight* light);
00819
00821 static EnvMode modelToEnvMode(SoTexture::Model model);
00822
00824 void updateLightState(SoLight* light);
00825
00827 static bool lessVersion(SoShaderObject* obj1, SoShaderObject* obj2);
00828
00830 static SbString getMaxVersion(const ShaderObjectVector& objList);
00831
00833 static bool lessProfile(SoShaderObject* obj1, SoShaderObject* obj2);
00834
00837 static SbString getLoosestProfile(const ShaderObjectVector& objList);
00838
00840 bool isUsingUniform(SoGLRenderAction* action, const char* uniformName);
00841
00843 bool needOivViewMatrixUniforms(SoGLRenderAction* action);
00844
00846 bool needOivProjectionMatrixUniforms(SoGLRenderAction* action);
00847
00849 bool needOivModelMatrixUniforms(SoGLRenderAction* action);
00850
00852 bool needOivModelViewProjMatrixUniforms(SoGLRenderAction* action);
00853
00855 bool needOivNormalMatrixUniforms(SoGLRenderAction* action);
00856
00858 bool needOivInstanceModelMatrixUniforms(SoGLRenderAction* action);
00859
00861 bool needOivInstanceUseMaterialColor(SoGLRenderAction* action);
00862
00864 bool needOivViewportSizeUniforms(SoGLRenderAction* action);
00865
00867 bool hasShaderObjectsChanged() const;
00868
00870 void updatePrevShaderObject();
00871
00872
00873 void shaderObjectSensorCB( void *data, SoSensor *sensor);
00874
00876 static void invalidate(ShaderObjectMap::value_type& p);
00877
00879 void addGPUVendorDefine();
00880
00882 bool hasFragmentShader();
00883
00885 void setShaderBufferObject(SoGLRenderAction* action);
00886
00888 private:
00890 SoCache* m_cache;
00891
00893 int m_previousTransparencyType;
00894
00896 static GPUVendorType s_gpuVendorType;
00897
00899 static int s_firstUsedTextureUnit;
00900
00902 static bool s_debugCache;
00903 };
00904
00905
00906
00907 SoFragmentShader*
00908 SoShaderProgram::getFragmentShader(int pos) const
00909 {
00910 assert(pos >= 0 && pos < shaderObject.getNum());
00911 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
00912
00913 if ( !obj )
00914 return NULL;
00915
00916 assert(obj->getTypeId() == SoFragmentShader::getClassTypeId());
00917
00918 return static_cast<SoFragmentShader*>(obj);
00919 }
00920
00921
00922 SoVertexShader*
00923 SoShaderProgram::getVertexShader(int pos) const
00924 {
00925 assert(pos >= 0 && pos < shaderObject.getNum());
00926 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
00927
00928 if ( !obj )
00929 return NULL;
00930
00931 assert(obj->getTypeId() == SoVertexShader::getClassTypeId());
00932
00933 return static_cast<SoVertexShader*>(obj);
00934 }
00935
00936
00937 SoGeometryShader*
00938 SoShaderProgram::getGeometryShader(int pos) const
00939 {
00940 assert(pos >= 0 && pos < shaderObject.getNum());
00941 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
00942
00943 if ( !obj )
00944 return NULL;
00945
00946 assert(obj->getTypeId() == SoGeometryShader::getClassTypeId());
00947
00948 return static_cast<SoGeometryShader*>(obj);
00949 }
00950
00951
00952 SoTessellationControlShader*
00953 SoShaderProgram::getTessellationControlShader(int pos) const
00954 {
00955 assert(pos >= 0 && pos < shaderObject.getNum());
00956 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
00957
00958 if ( !obj )
00959 return NULL;
00960
00961 assert(obj->getTypeId() == SoTessellationControlShader::getClassTypeId());
00962
00963 return static_cast<SoTessellationControlShader*>(obj);
00964 }
00965
00966
00967 SoTessellationEvaluationShader*
00968 SoShaderProgram::getTessellationEvaluationShader(int pos) const
00969 {
00970 assert(pos >= 0 && pos < shaderObject.getNum());
00971 SoShaderObject* obj = static_cast<SoShaderObject*>(shaderObject[pos]);
00972
00973 if ( !obj )
00974 return NULL;
00975
00976 assert(obj->getTypeId() == SoTessellationEvaluationShader::getClassTypeId());
00977
00978 return static_cast<SoTessellationEvaluationShader*>(obj);
00979 }
00980
00981 #ifdef _WIN32
00982 #pragma warning(pop)
00983 #endif
00984
00985 #endif
00986
00987
00988