00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org 00006 00007 Copyright (c) 2000-2012 Torus Knot Software Ltd 00008 Permission is hereby granted, free of charge, to any person obtaining a copy 00009 of this software and associated documentation files (the "Software"), to deal 00010 in the Software without restriction, including without limitation the rights 00011 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00012 copies of the Software, and to permit persons to whom the Software is 00013 furnished to do so, subject to the following conditions: 00014 00015 The above copyright notice and this permission notice shall be included in 00016 all copies or substantial portions of the Software. 00017 00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00024 THE SOFTWARE. 00025 ----------------------------------------------------------------------------- 00026 */ 00027 #ifndef _ShaderGenerator_ 00028 #define _ShaderGenerator_ 00029 00030 #include "OgreShaderPrerequisites.h" 00031 #include "OgreSingleton.h" 00032 #include "OgreRenderObjectListener.h" 00033 #include "OgreSceneManager.h" 00034 #include "OgreShaderRenderState.h" 00035 #include "OgreScriptTranslator.h" 00036 #include "OgreShaderScriptTranslator.h" 00037 00038 00039 namespace Ogre { 00040 namespace RTShader { 00041 00052 class _OgreRTSSExport ShaderGenerator : public Singleton<ShaderGenerator>, public RTShaderSystemAlloc 00053 { 00054 // Interface. 00055 public: 00056 00062 static bool initialize(); 00063 00067 static void finalize(); 00068 00069 00085 static ShaderGenerator& getSingleton(); 00086 00087 00103 static ShaderGenerator* getSingletonPtr(); 00104 00109 void addSceneManager(SceneManager* sceneMgr); 00110 00115 void removeSceneManager(SceneManager* sceneMgr); 00116 00121 SceneManager* getActiveSceneManager(); 00122 00128 void setTargetLanguage(const String& shaderLanguage); 00129 00133 const String& getTargetLanguage() const { return mShaderLanguage; } 00134 00139 void setVertexShaderProfiles(const String& vertexShaderProfiles); 00140 00144 const String& getVertexShaderProfiles() const { return mVertexShaderProfiles; } 00145 00149 const StringVector& getVertexShaderProfilesList() const { return mVertexShaderProfilesList; } 00150 00151 00156 void setFragmentShaderProfiles(const String& fragmentShaderProfiles); 00157 00161 const String& getFragmentShaderProfiles() const { return mFragmentShaderProfiles; } 00162 00166 const StringVector& getFragmentShaderProfilesList() const { return mFragmentShaderProfilesList; } 00167 00174 void setShaderCachePath(const String& cachePath); 00175 00179 const String& getShaderCachePath() const { return mShaderCachePath; } 00180 00185 void flushShaderCache(); 00186 00195 RenderState* getRenderState(const String& schemeName); 00196 00197 00198 typedef std::pair<RenderState*, bool> RenderStateCreateOrRetrieveResult; 00203 RenderStateCreateOrRetrieveResult createOrRetrieveRenderState(const String& schemeName); 00204 00205 00210 bool hasRenderState(const String& schemeName) const; 00211 00212 00221 RenderState* getRenderState(const String& schemeName, const String& materialName, unsigned short passIndex); 00222 RenderState* getRenderState(const String& schemeName, const String& materialName, const String& groupName, unsigned short passIndex); 00223 00230 void addSubRenderStateFactory(SubRenderStateFactory* factory); 00231 00235 size_t getNumSubRenderStateFactories() const; 00236 00241 SubRenderStateFactory* getSubRenderStateFactory(size_t index); 00242 00246 SubRenderStateFactory* getSubRenderStateFactory(const String& type); 00247 00252 void removeSubRenderStateFactory(SubRenderStateFactory* factory); 00253 00258 SubRenderState* createSubRenderState(const String& type); 00259 00260 00265 void destroySubRenderState(SubRenderState* subRenderState); 00266 00267 00276 bool hasShaderBasedTechnique(const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName) const; 00277 bool hasShaderBasedTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName) const; 00278 00289 bool createShaderBasedTechnique(const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName, bool overProgrammable = false); 00290 bool createShaderBasedTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName, bool overProgrammable = false); 00291 00292 00302 bool removeShaderBasedTechnique(const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName); 00303 bool removeShaderBasedTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName); 00304 00305 00312 bool removeAllShaderBasedTechniques(const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00313 00324 bool cloneShaderBasedTechniques(const String& srcMaterialName, 00325 const String& srcGroupName, const String& dstMaterialName, const String& dstGroupName); 00326 00330 void removeAllShaderBasedTechniques(); 00331 00336 void createScheme(const String& schemeName); 00337 00343 void invalidateScheme(const String& schemeName); 00344 00350 bool validateScheme(const String& schemeName); 00351 00359 void invalidateMaterial(const String& schemeName, const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00360 00368 bool validateMaterial(const String& schemeName, const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00369 00370 00382 SGMaterialSerializerListener* getMaterialSerializerListener(); 00383 00384 00386 size_t getVertexShaderCount() const; 00387 00388 00390 size_t getFragmentShaderCount() const; 00391 00392 00393 00398 void setVertexShaderOutputsCompactPolicy(VSOutputCompactPolicy policy) { mVSOutputCompactPolicy = policy; } 00399 00403 VSOutputCompactPolicy getVertexShaderOutputsCompactPolicy() const { return mVSOutputCompactPolicy; } 00404 00405 00411 void setCreateShaderOverProgrammablePass(bool value) { mCreateShaderOverProgrammablePass = value; } 00412 00416 bool getCreateShaderOverProgrammablePass() const { return mCreateShaderOverProgrammablePass; } 00417 00418 00421 size_t getRTShaderSchemeCount() const; 00422 00425 const String& getRTShaderScheme(size_t index) const; 00426 00428 static String DEFAULT_SCHEME_NAME; 00429 00430 // Protected types. 00431 protected: 00432 class SGPass; 00433 class SGTechnique; 00434 class SGMaterial; 00435 class SGScheme; 00436 00437 typedef std::pair<String,String> MatGroupPair; 00438 struct MatGroupPair_less 00439 { 00440 // ensure we arrange the list first by material name then by group name 00441 bool operator()(const MatGroupPair& p1, const MatGroupPair& p2) const 00442 { 00443 int cmpVal = strcmp(p1.first.c_str(),p2.first.c_str()); 00444 return (cmpVal < 0) || ((cmpVal == 0) && (strcmp(p1.second.c_str(),p2.second.c_str()) < 0)); 00445 } 00446 }; 00447 00448 typedef vector<SGPass*>::type SGPassList; 00449 typedef SGPassList::iterator SGPassIterator; 00450 typedef SGPassList::const_iterator SGPassConstIterator; 00451 00452 typedef vector<SGTechnique*>::type SGTechniqueList; 00453 typedef SGTechniqueList::iterator SGTechniqueIterator; 00454 typedef SGTechniqueList::const_iterator SGTechniqueConstIterator; 00455 00456 typedef map<SGTechnique*, SGTechnique*>::type SGTechniqueMap; 00457 typedef SGTechniqueMap::iterator SGTechniqueMapIterator; 00458 00459 typedef map<MatGroupPair, SGMaterial*, MatGroupPair_less>::type SGMaterialMap; 00460 typedef SGMaterialMap::iterator SGMaterialIterator; 00461 typedef SGMaterialMap::const_iterator SGMaterialConstIterator; 00462 00463 typedef map<String, SGScheme*>::type SGSchemeMap; 00464 typedef SGSchemeMap::iterator SGSchemeIterator; 00465 typedef SGSchemeMap::const_iterator SGSchemeConstIterator; 00466 00467 typedef map<String, ScriptTranslator*>::type SGScriptTranslatorMap; 00468 typedef SGScriptTranslatorMap::iterator SGScriptTranslatorIterator; 00469 typedef SGScriptTranslatorMap::const_iterator SGScriptTranslatorConstIterator; 00470 00471 00472 00474 class _OgreRTSSExport SGPass : public RTShaderSystemAlloc 00475 { 00476 public: 00477 SGPass(SGTechnique* parent, Pass* srcPass, Pass* dstPass); 00478 ~SGPass(); 00479 00481 void buildTargetRenderState(); 00482 00484 void acquirePrograms(); 00485 00487 void releasePrograms(); 00488 00489 00491 void notifyRenderSingleObject(Renderable* rend, const AutoParamDataSource* source, const LightList* pLightList, bool suppressRenderStateChanges); 00492 00494 Pass* getSrcPass() { return mSrcPass; } 00495 00497 Pass* getDstPass() { return mDstPass; } 00498 00500 SubRenderState* getCustomFFPSubState(int subStateOrder); 00501 00503 RenderState* getCustomRenderState() { return mCustomRenderState; } 00504 00506 void setCustomRenderState(RenderState* customRenderState) { mCustomRenderState = customRenderState; } 00507 00508 // Key name for associating with a Pass instance. 00509 static String UserKey; 00510 00511 protected: 00512 SubRenderState* getCustomFFPSubState(int subStateOrder, const RenderState* renderState); 00513 00514 protected: 00515 // Parent technique. 00516 SGTechnique* mParent; 00517 // Source pass. 00518 Pass* mSrcPass; 00519 // Destination pass. 00520 Pass* mDstPass; 00521 // Custom render state. 00522 RenderState* mCustomRenderState; 00523 // The compiled render state. 00524 TargetRenderState* mTargetRenderState; 00525 }; 00526 00527 00529 class _OgreRTSSExport SGTechnique : public RTShaderSystemAlloc 00530 { 00531 public: 00532 SGTechnique(SGMaterial* parent, Technique* srcTechnique, const String& dstTechniqueSchemeName); 00533 ~SGTechnique(); 00534 00536 const SGMaterial* getParent() const { return mParent; } 00537 00539 Technique* getSourceTechnique() { return mSrcTechnique; } 00540 00542 Technique* getDestinationTechnique() { return mDstTechnique; } 00543 00545 const String& getDestinationTechniqueSchemeName() const { return mDstTechniqueSchemeName; } 00546 00548 void buildTargetRenderState(); 00549 00551 void acquirePrograms(); 00552 00554 void releasePrograms(); 00555 00557 void setBuildDestinationTechnique(bool buildTechnique) { mBuildDstTechnique = buildTechnique; } 00558 00560 bool getBuildDestinationTechnique() const { return mBuildDstTechnique; } 00561 00565 RenderState* getRenderState(unsigned short passIndex); 00567 bool hasRenderState(unsigned short passIndex); 00568 00569 // Key name for associating with a Technique instance. 00570 static String UserKey; 00571 00572 protected: 00573 00575 void createSGPasses(); 00576 00578 void destroySGPasses(); 00579 00580 protected: 00581 // Parent material. 00582 SGMaterial* mParent; 00583 // Source technique. 00584 Technique* mSrcTechnique; 00585 // Destination technique. 00586 Technique* mDstTechnique; 00587 // All passes entries. 00588 SGPassList mPassEntries; 00589 // The custom render states of all passes. 00590 RenderStateList mCustomRenderStates; 00591 // Flag that tells if destination technique should be build. 00592 bool mBuildDstTechnique; 00593 // Scheme name of destination technique. 00594 String mDstTechniqueSchemeName; 00595 }; 00596 00597 00599 class _OgreRTSSExport SGMaterial : public RTShaderSystemAlloc 00600 { 00601 00602 public: 00604 SGMaterial(const String& materialName, const String& groupName) : mName(materialName), mGroup(groupName) 00605 { 00606 00607 } 00608 00610 const String& getMaterialName() const { return mName; } 00611 00613 const String& getGroupName() const { return mGroup; } 00614 00616 const SGTechniqueList& getTechniqueList() const { return mTechniqueEntries; } 00617 00619 SGTechniqueList& getTechniqueList() { return mTechniqueEntries; } 00620 00621 protected: 00622 // The material name. 00623 String mName; 00624 // The group name. 00625 String mGroup; 00626 // All passes entries. 00627 SGTechniqueList mTechniqueEntries; 00628 }; 00629 00630 00632 class _OgreRTSSExport SGScheme : public RTShaderSystemAlloc 00633 { 00634 public: 00635 SGScheme(const String& schemeName); 00636 ~SGScheme(); 00637 00638 00641 bool empty() const { return mTechniqueEntries.empty(); } 00642 00646 void invalidate(); 00647 00651 void validate(); 00652 00656 void invalidate(const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00657 00661 bool validate(const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00662 00664 void addTechniqueEntry(SGTechnique* techEntry); 00665 00667 void removeTechniqueEntry(SGTechnique* techEntry); 00668 00669 00673 RenderState* getRenderState(); 00674 00678 RenderState* getRenderState(const String& materialName, const String& groupName, unsigned short passIndex); 00679 00680 protected: 00682 void synchronizeWithLightSettings(); 00683 00685 void synchronizeWithFogSettings(); 00686 00687 00688 protected: 00689 // Scheme name. 00690 String mName; 00691 // Technique entries. 00692 SGTechniqueList mTechniqueEntries; 00693 // Tells if this scheme is out of date. 00694 bool mOutOfDate; 00695 // The global render state of this scheme. 00696 RenderState* mRenderState; 00697 // Current fog mode. 00698 FogMode mFogMode; 00699 }; 00700 00701 00702 // Protected types. 00703 protected: 00704 00706 class _OgreRTSSExport SGRenderObjectListener : public RenderObjectListener, public RTShaderSystemAlloc 00707 { 00708 public: 00709 SGRenderObjectListener(ShaderGenerator* owner) 00710 { 00711 mOwner = owner; 00712 } 00713 00717 virtual void notifyRenderSingleObject(Renderable* rend, const Pass* pass, 00718 const AutoParamDataSource* source, 00719 const LightList* pLightList, bool suppressRenderStateChanges) 00720 { 00721 mOwner->notifyRenderSingleObject(rend, pass, source, pLightList, suppressRenderStateChanges); 00722 } 00723 00724 protected: 00725 ShaderGenerator* mOwner; 00726 }; 00727 00729 class _OgreRTSSExport SGSceneManagerListener : public SceneManager::Listener, public RTShaderSystemAlloc 00730 { 00731 public: 00732 SGSceneManagerListener(ShaderGenerator* owner) 00733 { 00734 mOwner = owner; 00735 } 00736 00740 virtual void preFindVisibleObjects(SceneManager* source, 00741 SceneManager::IlluminationRenderStage irs, Viewport* v) 00742 { 00743 mOwner->preFindVisibleObjects(source, irs, v); 00744 } 00745 00746 virtual void postFindVisibleObjects(SceneManager* source, 00747 SceneManager::IlluminationRenderStage irs, Viewport* v) 00748 { 00749 00750 } 00751 00752 virtual void shadowTexturesUpdated(size_t numberOfShadowTextures) 00753 { 00754 00755 } 00756 00757 virtual void shadowTextureCasterPreViewProj(Light* light, 00758 Camera* camera, size_t iteration) 00759 { 00760 00761 } 00762 00763 virtual void shadowTextureReceiverPreViewProj(Light* light, 00764 Frustum* frustum) 00765 { 00766 00767 } 00768 00769 protected: 00770 // The shader generator instance. 00771 ShaderGenerator* mOwner; 00772 }; 00773 00775 class _OgreRTSSExport SGScriptTranslatorManager : public ScriptTranslatorManager 00776 { 00777 public: 00778 SGScriptTranslatorManager(ShaderGenerator* owner) 00779 { 00780 mOwner = owner; 00781 } 00782 00784 virtual size_t getNumTranslators() const 00785 { 00786 return mOwner->getNumTranslators(); 00787 } 00788 00790 virtual ScriptTranslator *getTranslator(const AbstractNodePtr& node) 00791 { 00792 return mOwner->getTranslator(node); 00793 } 00794 00795 protected: 00796 // The shader generator instance. 00797 ShaderGenerator* mOwner; 00798 }; 00799 00800 //----------------------------------------------------------------------------- 00801 typedef map<String, SubRenderStateFactory*>::type SubRenderStateFactoryMap; 00802 typedef SubRenderStateFactoryMap::iterator SubRenderStateFactoryIterator; 00803 typedef SubRenderStateFactoryMap::const_iterator SubRenderStateFactoryConstIterator; 00804 00805 //----------------------------------------------------------------------------- 00806 typedef map<String, SceneManager*>::type SceneManagerMap; 00807 typedef SceneManagerMap::iterator SceneManagerIterator; 00808 typedef SceneManagerMap::const_iterator SceneManagerConstIterator; 00809 00810 protected: 00812 ShaderGenerator(); 00813 00815 ~ShaderGenerator(); 00816 00818 bool _initialize(); 00819 00821 void _finalize(); 00822 00824 Technique* findSourceTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName); 00825 00827 bool isProgrammable(Technique* tech) const; 00828 00830 void notifyRenderSingleObject(Renderable* rend, const Pass* pass, const AutoParamDataSource* source, const LightList* pLightList, bool suppressRenderStateChanges); 00831 00833 void preFindVisibleObjects(SceneManager* source, SceneManager::IlluminationRenderStage irs, Viewport* v); 00834 00836 void createSubRenderStateExFactories(); 00837 00839 void destroySubRenderStateExFactories(); 00840 00849 SubRenderState* createSubRenderState(ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator); 00850 00859 SubRenderState* createSubRenderState(ScriptCompiler* compiler, PropertyAbstractNode* prop, TextureUnitState* texState, SGScriptTranslator* translator); 00860 00867 bool addCustomScriptTranslator(const String& key, ScriptTranslator* translator); 00868 00874 bool removeCustomScriptTranslator(const String& key); 00875 00877 size_t getNumTranslators() const; 00878 00880 ScriptTranslator* getTranslator(const AbstractNodePtr& node); 00881 00882 00888 void serializePassAttributes(MaterialSerializer* ser, SGPass* passEntry); 00889 00896 void serializeTextureUnitStateAttributes(MaterialSerializer* ser, SGPass* passEntry, const TextureUnitState* srcTextureUnit); 00897 00902 SGMaterialIterator findMaterialEntryIt(const String& materialName, const String& groupName); 00903 SGMaterialConstIterator findMaterialEntryIt(const String& materialName, const String& groupName) const; 00904 00905 00906 typedef std::pair<SGScheme*, bool> SchemeCreateOrRetrieveResult; 00911 SchemeCreateOrRetrieveResult createOrRetrieveScheme(const String& schemeName); 00912 00914 bool getIsFinalizing() const; 00915 protected: 00916 OGRE_AUTO_MUTEX // Auto mutex. 00917 // The active scene manager. 00918 SceneManager* mActiveSceneMgr; 00919 // A map of all scene managers this generator is bound to. 00920 SceneManagerMap mSceneManagerMap; 00921 // Render object listener. 00922 SGRenderObjectListener* mRenderObjectListener; 00923 // Scene manager listener. 00924 SGSceneManagerListener* mSceneManagerListener; 00925 // Script translator manager. 00926 SGScriptTranslatorManager* mScriptTranslatorManager; 00927 // Custom material Serializer listener - allows exporting material that contains shader generated techniques. 00928 SGMaterialSerializerListener* mMaterialSerializerListener; 00929 // A map of the registered custom script translators. 00930 SGScriptTranslatorMap mScriptTranslatorsMap; 00931 // The core translator of the RT Shader System. 00932 SGScriptTranslator mCoreScriptTranslator; 00933 // The target shader language (currently only cg supported). 00934 String mShaderLanguage; 00935 // The target vertex shader profile. Will be used as argument for program compilation. 00936 String mVertexShaderProfiles; 00937 // List of target vertex shader profiles. 00938 StringVector mVertexShaderProfilesList; 00939 // The target fragment shader profile. Will be used as argument for program compilation. 00940 String mFragmentShaderProfiles; 00941 // List of target fragment shader profiles.. 00942 StringVector mFragmentShaderProfilesList; 00943 // Path for caching the generated shaders. 00944 String mShaderCachePath; 00945 // Shader program manager. 00946 ProgramManager* mProgramManager; 00947 // Shader program writer manager. 00948 ProgramWriterManager* mProgramWriterManager; 00949 // Fixed Function Render state builder. 00950 FFPRenderStateBuilder* mFFPRenderStateBuilder; 00951 // Material entries map. 00952 SGMaterialMap mMaterialEntriesMap; 00953 // Scheme entries map. 00954 SGSchemeMap mSchemeEntriesMap; 00955 // All technique entries map. 00956 SGTechniqueMap mTechniqueEntriesMap; 00957 // Sub render state registered factories. 00958 SubRenderStateFactoryMap mSubRenderStateFactories; 00959 // Sub render state core extension factories. 00960 SubRenderStateFactoryMap mSubRenderStateExFactories; 00961 // True if active view port use a valid SGScheme. 00962 bool mActiveViewportValid; 00963 // Light count per light type. 00964 int mLightCount[3]; 00965 // Vertex shader outputs compact policy. 00966 VSOutputCompactPolicy mVSOutputCompactPolicy; 00967 // Tells whether shaders are created for passes with shaders 00968 bool mCreateShaderOverProgrammablePass; 00969 // a flag to indicate finalizing 00970 bool mIsFinalizing; 00971 private: 00972 friend class SGPass; 00973 friend class FFPRenderStateBuilder; 00974 friend class SGScriptTranslatorManager; 00975 friend class SGScriptTranslator; 00976 friend class SGMaterialSerializerListener; 00977 00978 }; 00979 00983 } 00984 } 00985 00986 #endif 00987
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:23