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-2013 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 "OgreFileSystemLayer.h" 00033 #include "OgreRenderObjectListener.h" 00034 #include "OgreSceneManager.h" 00035 #include "OgreShaderRenderState.h" 00036 #include "OgreScriptTranslator.h" 00037 #include "OgreShaderScriptTranslator.h" 00038 00039 00040 namespace Ogre { 00041 namespace RTShader { 00042 00053 class _OgreRTSSExport ShaderGenerator : public Singleton<ShaderGenerator>, public RTShaderSystemAlloc 00054 { 00055 // Interface. 00056 public: 00057 00062 static bool initialize(); 00063 00067 static void destroy(); 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 00220 RenderState* getRenderState(const String& schemeName, const String& materialName, unsigned short passIndex); 00221 00230 RenderState* getRenderState(const String& schemeName, const String& materialName, const String& groupName, unsigned short passIndex); 00231 00238 void addSubRenderStateFactory(SubRenderStateFactory* factory); 00239 00243 size_t getNumSubRenderStateFactories() const; 00244 00249 SubRenderStateFactory* getSubRenderStateFactory(size_t index); 00250 00254 SubRenderStateFactory* getSubRenderStateFactory(const String& type); 00255 00260 void removeSubRenderStateFactory(SubRenderStateFactory* factory); 00261 00266 SubRenderState* createSubRenderState(const String& type); 00267 00268 00273 void destroySubRenderState(SubRenderState* subRenderState); 00274 00275 00283 bool hasShaderBasedTechnique(const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName) const; 00284 00293 bool hasShaderBasedTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName) const; 00294 00304 bool createShaderBasedTechnique(const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName, bool overProgrammable = false); 00305 00316 bool createShaderBasedTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName, bool overProgrammable = false); 00317 00318 00327 bool removeShaderBasedTechnique(const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName); 00328 00338 bool removeShaderBasedTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName); 00339 00340 00347 bool removeAllShaderBasedTechniques(const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00348 00359 bool cloneShaderBasedTechniques(const String& srcMaterialName, 00360 const String& srcGroupName, const String& dstMaterialName, const String& dstGroupName); 00361 00365 void removeAllShaderBasedTechniques(); 00366 00371 void createScheme(const String& schemeName); 00372 00378 void invalidateScheme(const String& schemeName); 00379 00385 bool validateScheme(const String& schemeName); 00386 00394 void invalidateMaterial(const String& schemeName, const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00395 00403 bool validateMaterial(const String& schemeName, const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00404 00405 00417 SGMaterialSerializerListener* getMaterialSerializerListener(); 00418 00419 00421 size_t getVertexShaderCount() const; 00422 00423 00425 size_t getFragmentShaderCount() const; 00426 00427 00428 00433 void setVertexShaderOutputsCompactPolicy(VSOutputCompactPolicy policy) { mVSOutputCompactPolicy = policy; } 00434 00438 VSOutputCompactPolicy getVertexShaderOutputsCompactPolicy() const { return mVSOutputCompactPolicy; } 00439 00440 00446 void setCreateShaderOverProgrammablePass(bool value) { mCreateShaderOverProgrammablePass = value; } 00447 00451 bool getCreateShaderOverProgrammablePass() const { return mCreateShaderOverProgrammablePass; } 00452 00453 00456 size_t getRTShaderSchemeCount() const; 00457 00460 const String& getRTShaderScheme(size_t index) const; 00461 00463 static String DEFAULT_SCHEME_NAME; 00464 00465 // Protected types. 00466 protected: 00467 class SGPass; 00468 class SGTechnique; 00469 class SGMaterial; 00470 class SGScheme; 00471 00472 typedef std::pair<String,String> MatGroupPair; 00473 struct MatGroupPair_less 00474 { 00475 // ensure we arrange the list first by material name then by group name 00476 bool operator()(const MatGroupPair& p1, const MatGroupPair& p2) const 00477 { 00478 int cmpVal = strcmp(p1.first.c_str(),p2.first.c_str()); 00479 return (cmpVal < 0) || ((cmpVal == 0) && (strcmp(p1.second.c_str(),p2.second.c_str()) < 0)); 00480 } 00481 }; 00482 00483 typedef vector<SGPass*>::type SGPassList; 00484 typedef SGPassList::iterator SGPassIterator; 00485 typedef SGPassList::const_iterator SGPassConstIterator; 00486 00487 typedef vector<SGTechnique*>::type SGTechniqueList; 00488 typedef SGTechniqueList::iterator SGTechniqueIterator; 00489 typedef SGTechniqueList::const_iterator SGTechniqueConstIterator; 00490 00491 typedef map<SGTechnique*, SGTechnique*>::type SGTechniqueMap; 00492 typedef SGTechniqueMap::iterator SGTechniqueMapIterator; 00493 00494 typedef map<MatGroupPair, SGMaterial*, MatGroupPair_less>::type SGMaterialMap; 00495 typedef SGMaterialMap::iterator SGMaterialIterator; 00496 typedef SGMaterialMap::const_iterator SGMaterialConstIterator; 00497 00498 typedef map<String, SGScheme*>::type SGSchemeMap; 00499 typedef SGSchemeMap::iterator SGSchemeIterator; 00500 typedef SGSchemeMap::const_iterator SGSchemeConstIterator; 00501 00502 typedef map<String, ScriptTranslator*>::type SGScriptTranslatorMap; 00503 typedef SGScriptTranslatorMap::iterator SGScriptTranslatorIterator; 00504 typedef SGScriptTranslatorMap::const_iterator SGScriptTranslatorConstIterator; 00505 00506 00507 00509 class _OgreRTSSExport SGPass : public RTShaderSystemAlloc 00510 { 00511 public: 00512 SGPass(SGTechnique* parent, Pass* srcPass, Pass* dstPass); 00513 ~SGPass(); 00514 00516 void buildTargetRenderState(); 00517 00519 void acquirePrograms(); 00520 00522 void releasePrograms(); 00523 00524 00526 void notifyRenderSingleObject(Renderable* rend, const AutoParamDataSource* source, const LightList* pLightList, bool suppressRenderStateChanges); 00527 00529 Pass* getSrcPass() { return mSrcPass; } 00530 00532 Pass* getDstPass() { return mDstPass; } 00533 00535 SubRenderState* getCustomFFPSubState(int subStateOrder); 00536 00538 RenderState* getCustomRenderState() { return mCustomRenderState; } 00539 00541 void setCustomRenderState(RenderState* customRenderState) { mCustomRenderState = customRenderState; } 00542 00543 // Key name for associating with a Pass instance. 00544 static String UserKey; 00545 00546 protected: 00547 SubRenderState* getCustomFFPSubState(int subStateOrder, const RenderState* renderState); 00548 00549 protected: 00550 // Parent technique. 00551 SGTechnique* mParent; 00552 // Source pass. 00553 Pass* mSrcPass; 00554 // Destination pass. 00555 Pass* mDstPass; 00556 // Custom render state. 00557 RenderState* mCustomRenderState; 00558 // The compiled render state. 00559 TargetRenderState* mTargetRenderState; 00560 }; 00561 00562 00564 class _OgreRTSSExport SGTechnique : public RTShaderSystemAlloc 00565 { 00566 public: 00567 SGTechnique(SGMaterial* parent, Technique* srcTechnique, const String& dstTechniqueSchemeName); 00568 ~SGTechnique(); 00569 00571 const SGMaterial* getParent() const { return mParent; } 00572 00574 Technique* getSourceTechnique() { return mSrcTechnique; } 00575 00577 Technique* getDestinationTechnique() { return mDstTechnique; } 00578 00580 const String& getDestinationTechniqueSchemeName() const { return mDstTechniqueSchemeName; } 00581 00583 void buildTargetRenderState(); 00584 00586 void acquirePrograms(); 00587 00589 void releasePrograms(); 00590 00592 void setBuildDestinationTechnique(bool buildTechnique) { mBuildDstTechnique = buildTechnique; } 00593 00595 bool getBuildDestinationTechnique() const { return mBuildDstTechnique; } 00596 00600 RenderState* getRenderState(unsigned short passIndex); 00602 bool hasRenderState(unsigned short passIndex); 00603 00604 // Key name for associating with a Technique instance. 00605 static String UserKey; 00606 00607 protected: 00608 00610 void createSGPasses(); 00611 00613 void destroySGPasses(); 00614 00615 protected: 00616 // Parent material. 00617 SGMaterial* mParent; 00618 // Source technique. 00619 Technique* mSrcTechnique; 00620 // Destination technique. 00621 Technique* mDstTechnique; 00622 // All passes entries. 00623 SGPassList mPassEntries; 00624 // The custom render states of all passes. 00625 RenderStateList mCustomRenderStates; 00626 // Flag that tells if destination technique should be build. 00627 bool mBuildDstTechnique; 00628 // Scheme name of destination technique. 00629 String mDstTechniqueSchemeName; 00630 }; 00631 00632 00634 class _OgreRTSSExport SGMaterial : public RTShaderSystemAlloc 00635 { 00636 00637 public: 00639 SGMaterial(const String& materialName, const String& groupName) : mName(materialName), mGroup(groupName) 00640 { 00641 00642 } 00643 00645 const String& getMaterialName() const { return mName; } 00646 00648 const String& getGroupName() const { return mGroup; } 00649 00651 const SGTechniqueList& getTechniqueList() const { return mTechniqueEntries; } 00652 00654 SGTechniqueList& getTechniqueList() { return mTechniqueEntries; } 00655 00656 protected: 00657 // The material name. 00658 String mName; 00659 // The group name. 00660 String mGroup; 00661 // All passes entries. 00662 SGTechniqueList mTechniqueEntries; 00663 }; 00664 00665 00667 class _OgreRTSSExport SGScheme : public RTShaderSystemAlloc 00668 { 00669 public: 00670 SGScheme(const String& schemeName); 00671 ~SGScheme(); 00672 00673 00676 bool empty() const { return mTechniqueEntries.empty(); } 00677 00681 void invalidate(); 00682 00686 void validate(); 00687 00691 void invalidate(const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00692 00696 bool validate(const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00697 00699 void addTechniqueEntry(SGTechnique* techEntry); 00700 00702 void removeTechniqueEntry(SGTechnique* techEntry); 00703 00704 00708 RenderState* getRenderState(); 00709 00713 RenderState* getRenderState(const String& materialName, const String& groupName, unsigned short passIndex); 00714 00715 protected: 00717 void synchronizeWithLightSettings(); 00718 00720 void synchronizeWithFogSettings(); 00721 00722 00723 protected: 00724 // Scheme name. 00725 String mName; 00726 // Technique entries. 00727 SGTechniqueList mTechniqueEntries; 00728 // Tells if this scheme is out of date. 00729 bool mOutOfDate; 00730 // The global render state of this scheme. 00731 RenderState* mRenderState; 00732 // Current fog mode. 00733 FogMode mFogMode; 00734 }; 00735 00736 00737 // Protected types. 00738 protected: 00739 00741 class _OgreRTSSExport SGRenderObjectListener : public RenderObjectListener, public RTShaderSystemAlloc 00742 { 00743 public: 00744 SGRenderObjectListener(ShaderGenerator* owner) 00745 { 00746 mOwner = owner; 00747 } 00748 00752 virtual void notifyRenderSingleObject(Renderable* rend, const Pass* pass, 00753 const AutoParamDataSource* source, 00754 const LightList* pLightList, bool suppressRenderStateChanges) 00755 { 00756 mOwner->notifyRenderSingleObject(rend, pass, source, pLightList, suppressRenderStateChanges); 00757 } 00758 00759 protected: 00760 ShaderGenerator* mOwner; 00761 }; 00762 00764 class _OgreRTSSExport SGSceneManagerListener : public SceneManager::Listener, public RTShaderSystemAlloc 00765 { 00766 public: 00767 SGSceneManagerListener(ShaderGenerator* owner) 00768 { 00769 mOwner = owner; 00770 } 00771 00775 virtual void preFindVisibleObjects(SceneManager* source, 00776 SceneManager::IlluminationRenderStage irs, Viewport* v) 00777 { 00778 mOwner->preFindVisibleObjects(source, irs, v); 00779 } 00780 00781 virtual void postFindVisibleObjects(SceneManager* source, 00782 SceneManager::IlluminationRenderStage irs, Viewport* v) 00783 { 00784 00785 } 00786 00787 virtual void shadowTexturesUpdated(size_t numberOfShadowTextures) 00788 { 00789 00790 } 00791 00792 virtual void shadowTextureCasterPreViewProj(Light* light, 00793 Camera* camera, size_t iteration) 00794 { 00795 00796 } 00797 00798 virtual void shadowTextureReceiverPreViewProj(Light* light, 00799 Frustum* frustum) 00800 { 00801 00802 } 00803 00804 protected: 00805 // The shader generator instance. 00806 ShaderGenerator* mOwner; 00807 }; 00808 00810 class _OgreRTSSExport SGScriptTranslatorManager : public ScriptTranslatorManager 00811 { 00812 public: 00813 SGScriptTranslatorManager(ShaderGenerator* owner) 00814 { 00815 mOwner = owner; 00816 } 00817 00819 virtual size_t getNumTranslators() const 00820 { 00821 return mOwner->getNumTranslators(); 00822 } 00823 00825 virtual ScriptTranslator *getTranslator(const AbstractNodePtr& node) 00826 { 00827 return mOwner->getTranslator(node); 00828 } 00829 00830 protected: 00831 // The shader generator instance. 00832 ShaderGenerator* mOwner; 00833 }; 00834 00835 //----------------------------------------------------------------------------- 00836 typedef map<String, SubRenderStateFactory*>::type SubRenderStateFactoryMap; 00837 typedef SubRenderStateFactoryMap::iterator SubRenderStateFactoryIterator; 00838 typedef SubRenderStateFactoryMap::const_iterator SubRenderStateFactoryConstIterator; 00839 00840 //----------------------------------------------------------------------------- 00841 typedef map<String, SceneManager*>::type SceneManagerMap; 00842 typedef SceneManagerMap::iterator SceneManagerIterator; 00843 typedef SceneManagerMap::const_iterator SceneManagerConstIterator; 00844 00845 protected: 00847 ShaderGenerator(); 00848 00850 ~ShaderGenerator(); 00851 00853 bool _initialize(); 00854 00856 void _destroy(); 00857 00859 Technique* findSourceTechnique(const String& materialName, const String& groupName, const String& srcTechniqueSchemeName, bool allowProgrammable); 00860 00862 bool isProgrammable(Technique* tech) const; 00863 00865 void notifyRenderSingleObject(Renderable* rend, const Pass* pass, const AutoParamDataSource* source, const LightList* pLightList, bool suppressRenderStateChanges); 00866 00868 void preFindVisibleObjects(SceneManager* source, SceneManager::IlluminationRenderStage irs, Viewport* v); 00869 00871 void createSubRenderStateExFactories(); 00872 00874 void destroySubRenderStateExFactories(); 00875 00884 SubRenderState* createSubRenderState(ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator); 00885 00894 SubRenderState* createSubRenderState(ScriptCompiler* compiler, PropertyAbstractNode* prop, TextureUnitState* texState, SGScriptTranslator* translator); 00895 00902 bool addCustomScriptTranslator(const String& key, ScriptTranslator* translator); 00903 00909 bool removeCustomScriptTranslator(const String& key); 00910 00912 size_t getNumTranslators() const; 00913 00915 ScriptTranslator* getTranslator(const AbstractNodePtr& node); 00916 00917 00923 void serializePassAttributes(MaterialSerializer* ser, SGPass* passEntry); 00924 00931 void serializeTextureUnitStateAttributes(MaterialSerializer* ser, SGPass* passEntry, const TextureUnitState* srcTextureUnit); 00932 00937 SGMaterialIterator findMaterialEntryIt(const String& materialName, const String& groupName); 00938 SGMaterialConstIterator findMaterialEntryIt(const String& materialName, const String& groupName) const; 00939 00940 00941 typedef std::pair<SGScheme*, bool> SchemeCreateOrRetrieveResult; 00946 SchemeCreateOrRetrieveResult createOrRetrieveScheme(const String& schemeName); 00947 00949 bool getIsFinalizing() const; 00950 protected: 00951 // Auto mutex. 00952 OGRE_AUTO_MUTEX; 00953 // The active scene manager. 00954 SceneManager* mActiveSceneMgr; 00955 // A map of all scene managers this generator is bound to. 00956 SceneManagerMap mSceneManagerMap; 00957 // Render object listener. 00958 SGRenderObjectListener* mRenderObjectListener; 00959 // Scene manager listener. 00960 SGSceneManagerListener* mSceneManagerListener; 00961 // Script translator manager. 00962 SGScriptTranslatorManager* mScriptTranslatorManager; 00963 // Custom material Serializer listener - allows exporting material that contains shader generated techniques. 00964 SGMaterialSerializerListener* mMaterialSerializerListener; 00965 // A map of the registered custom script translators. 00966 SGScriptTranslatorMap mScriptTranslatorsMap; 00967 // The core translator of the RT Shader System. 00968 SGScriptTranslator mCoreScriptTranslator; 00969 // The target shader language (currently only cg supported). 00970 String mShaderLanguage; 00971 // The target vertex shader profile. Will be used as argument for program compilation. 00972 String mVertexShaderProfiles; 00973 // List of target vertex shader profiles. 00974 StringVector mVertexShaderProfilesList; 00975 // The target fragment shader profile. Will be used as argument for program compilation. 00976 String mFragmentShaderProfiles; 00977 // List of target fragment shader profiles.. 00978 StringVector mFragmentShaderProfilesList; 00979 // Path for caching the generated shaders. 00980 String mShaderCachePath; 00981 // Shader program manager. 00982 ProgramManager* mProgramManager; 00983 // Shader program writer manager. 00984 ProgramWriterManager* mProgramWriterManager; 00985 // File system layer manager. 00986 FileSystemLayer* mFSLayer; 00987 // Fixed Function Render state builder. 00988 FFPRenderStateBuilder* mFFPRenderStateBuilder; 00989 // Material entries map. 00990 SGMaterialMap mMaterialEntriesMap; 00991 // Scheme entries map. 00992 SGSchemeMap mSchemeEntriesMap; 00993 // All technique entries map. 00994 SGTechniqueMap mTechniqueEntriesMap; 00995 // Sub render state registered factories. 00996 SubRenderStateFactoryMap mSubRenderStateFactories; 00997 // Sub render state core extension factories. 00998 SubRenderStateFactoryMap mSubRenderStateExFactories; 00999 // True if active view port use a valid SGScheme. 01000 bool mActiveViewportValid; 01001 // Light count per light type. 01002 int mLightCount[3]; 01003 // Vertex shader outputs compact policy. 01004 VSOutputCompactPolicy mVSOutputCompactPolicy; 01005 // Tells whether shaders are created for passes with shaders 01006 bool mCreateShaderOverProgrammablePass; 01007 // a flag to indicate finalizing 01008 bool mIsFinalizing; 01009 private: 01010 friend class SGPass; 01011 friend class FFPRenderStateBuilder; 01012 friend class SGScriptTranslatorManager; 01013 friend class SGScriptTranslator; 01014 friend class SGMaterialSerializerListener; 01015 01016 }; 01017 01021 } 01022 } 01023 01024 #endif 01025
Copyright © 2012 Torus Knot Software Ltd

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:46