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 static String UserKey; // Key name for associating with a Pass instance. 00509 00510 protected: 00511 SubRenderState* getCustomFFPSubState (int subStateOrder, const RenderState* renderState); 00512 00513 protected: 00514 SGTechnique* mParent; // Parent technique. 00515 Pass* mSrcPass; // Source pass. 00516 Pass* mDstPass; // Destination pass. 00517 RenderState* mCustomRenderState; // Custom render state. 00518 TargetRenderState* mTargetRenderState; // The compiled render state. 00519 }; 00520 00521 00523 class _OgreRTSSExport SGTechnique : public RTShaderSystemAlloc 00524 { 00525 public: 00526 SGTechnique (SGMaterial* parent, Technique* srcTechnique, const String& dstTechniqueSchemeName); 00527 ~SGTechnique (); 00528 00530 const SGMaterial* getParent () const { return mParent; } 00531 00533 Technique* getSourceTechnique () { return mSrcTechnique; } 00534 00536 Technique* getDestinationTechnique () { return mDstTechnique; } 00537 00539 const String& getDestinationTechniqueSchemeName() const { return mDstTechniqueSchemeName; } 00540 00542 void buildTargetRenderState (); 00543 00545 void acquirePrograms (); 00546 00548 void releasePrograms (); 00549 00551 void setBuildDestinationTechnique (bool buildTechnique) { mBuildDstTechnique = buildTechnique; } 00552 00554 bool getBuildDestinationTechnique () const { return mBuildDstTechnique; } 00555 00559 RenderState* getRenderState (unsigned short passIndex); 00561 bool hasRenderState (unsigned short passIndex); 00562 00563 static String UserKey; // Key name for associating with a Technique instance. 00564 00565 protected: 00566 00568 void createSGPasses (); 00569 00571 void destroySGPasses (); 00572 00573 protected: 00574 SGMaterial* mParent; // Parent material. 00575 Technique* mSrcTechnique; // Source technique. 00576 Technique* mDstTechnique; // Destination technique. 00577 SGPassList mPassEntries; // All passes entries. 00578 RenderStateList mCustomRenderStates; // The custom render states of all passes. 00579 bool mBuildDstTechnique; // Flag that tells if destination technique should be build. 00580 String mDstTechniqueSchemeName; // Scheme name of destination technique. 00581 }; 00582 00583 00585 class _OgreRTSSExport SGMaterial : public RTShaderSystemAlloc 00586 { 00587 00588 public: 00590 SGMaterial(const String& materialName, const String& groupName) : mName(materialName), mGroup(groupName) 00591 { 00592 00593 } 00594 00596 const String& getMaterialName () const { return mName; } 00597 00599 const String& getGroupName () const { return mGroup; } 00600 00602 const SGTechniqueList& getTechniqueList () const { return mTechniqueEntries; } 00603 00605 SGTechniqueList& getTechniqueList () { return mTechniqueEntries; } 00606 00607 protected: 00608 String mName; // The material name. 00609 String mGroup; // The group name. 00610 SGTechniqueList mTechniqueEntries; // All passes entries. 00611 }; 00612 00613 00615 class _OgreRTSSExport SGScheme : public RTShaderSystemAlloc 00616 { 00617 public: 00618 SGScheme (const String& schemeName); 00619 ~SGScheme (); 00620 00621 00624 bool empty () const { return mTechniqueEntries.empty(); } 00625 00629 void invalidate (); 00630 00634 void validate (); 00635 00639 void invalidate (const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00640 00644 bool validate (const String& materialName, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); 00645 00647 void addTechniqueEntry (SGTechnique* techEntry); 00648 00650 void removeTechniqueEntry (SGTechnique* techEntry); 00651 00652 00656 RenderState* getRenderState (); 00657 00661 RenderState* getRenderState (const String& materialName, const String& groupName, unsigned short passIndex); 00662 00663 protected: 00665 void synchronizeWithLightSettings (); 00666 00668 void synchronizeWithFogSettings (); 00669 00670 00671 protected: 00672 String mName; // Scheme name. 00673 SGTechniqueList mTechniqueEntries; // Technique entries. 00674 bool mOutOfDate; // Tells if this scheme is out of date. 00675 RenderState* mRenderState; // The global render state of this scheme. 00676 FogMode mFogMode; // Current fog mode. 00677 }; 00678 00679 00680 // Protected types. 00681 protected: 00682 00684 class _OgreRTSSExport SGRenderObjectListener : public RenderObjectListener, public RTShaderSystemAlloc 00685 { 00686 public: 00687 SGRenderObjectListener(ShaderGenerator* owner) 00688 { 00689 mOwner = owner; 00690 } 00691 00695 virtual void notifyRenderSingleObject(Renderable* rend, const Pass* pass, 00696 const AutoParamDataSource* source, 00697 const LightList* pLightList, bool suppressRenderStateChanges) 00698 { 00699 mOwner->notifyRenderSingleObject(rend, pass, source, pLightList, suppressRenderStateChanges); 00700 } 00701 00702 protected: 00703 ShaderGenerator* mOwner; 00704 }; 00705 00707 class _OgreRTSSExport SGSceneManagerListener : public SceneManager::Listener, public RTShaderSystemAlloc 00708 { 00709 public: 00710 SGSceneManagerListener(ShaderGenerator* owner) 00711 { 00712 mOwner = owner; 00713 } 00714 00718 virtual void preFindVisibleObjects(SceneManager* source, 00719 SceneManager::IlluminationRenderStage irs, Viewport* v) 00720 { 00721 mOwner->preFindVisibleObjects(source, irs, v); 00722 } 00723 00724 virtual void postFindVisibleObjects(SceneManager* source, 00725 SceneManager::IlluminationRenderStage irs, Viewport* v) 00726 { 00727 00728 } 00729 00730 virtual void shadowTexturesUpdated(size_t numberOfShadowTextures) 00731 { 00732 00733 } 00734 00735 virtual void shadowTextureCasterPreViewProj(Light* light, 00736 Camera* camera, size_t iteration) 00737 { 00738 00739 } 00740 00741 virtual void shadowTextureReceiverPreViewProj(Light* light, 00742 Frustum* frustum) 00743 { 00744 00745 } 00746 00747 protected: 00748 ShaderGenerator* mOwner; // The shader generator instance. 00749 }; 00750 00752 class _OgreRTSSExport SGScriptTranslatorManager : public ScriptTranslatorManager 00753 { 00754 public: 00755 SGScriptTranslatorManager(ShaderGenerator* owner) 00756 { 00757 mOwner = owner; 00758 } 00759 00761 virtual size_t getNumTranslators() const 00762 { 00763 return mOwner->getNumTranslators(); 00764 } 00765 00767 virtual ScriptTranslator *getTranslator(const AbstractNodePtr& node) 00768 { 00769 return mOwner->getTranslator(node); 00770 } 00771 00772 protected: 00773 ShaderGenerator* mOwner; // The shader generator instance. 00774 }; 00775 00776 //----------------------------------------------------------------------------- 00777 typedef map<String, SubRenderStateFactory*>::type SubRenderStateFactoryMap; 00778 typedef SubRenderStateFactoryMap::iterator SubRenderStateFactoryIterator; 00779 typedef SubRenderStateFactoryMap::const_iterator SubRenderStateFactoryConstIterator; 00780 00781 //----------------------------------------------------------------------------- 00782 typedef map<String, SceneManager*>::type SceneManagerMap; 00783 typedef SceneManagerMap::iterator SceneManagerIterator; 00784 typedef SceneManagerMap::const_iterator SceneManagerConstIterator; 00785 00786 protected: 00788 ShaderGenerator (); 00789 00791 ~ShaderGenerator (); 00792 00794 bool _initialize (); 00795 00797 void _finalize (); 00798 00800 Technique* findSourceTechnique (const String& materialName, const String& groupName, const String& srcTechniqueSchemeName); 00801 00803 bool isProgrammable (Technique* tech) const; 00804 00806 void notifyRenderSingleObject (Renderable* rend, const Pass* pass, const AutoParamDataSource* source, const LightList* pLightList, bool suppressRenderStateChanges); 00807 00809 void preFindVisibleObjects (SceneManager* source, SceneManager::IlluminationRenderStage irs, Viewport* v); 00810 00812 void createSubRenderStateExFactories (); 00813 00815 void destroySubRenderStateExFactories (); 00816 00825 SubRenderState* createSubRenderState (ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass, SGScriptTranslator* translator); 00826 00835 SubRenderState* createSubRenderState (ScriptCompiler* compiler, PropertyAbstractNode* prop, TextureUnitState* texState, SGScriptTranslator* translator); 00836 00843 bool addCustomScriptTranslator (const String& key, ScriptTranslator* translator); 00844 00850 bool removeCustomScriptTranslator (const String& key); 00851 00853 size_t getNumTranslators () const; 00854 00856 ScriptTranslator* getTranslator (const AbstractNodePtr& node); 00857 00858 00864 void serializePassAttributes (MaterialSerializer* ser, SGPass* passEntry); 00865 00872 void serializeTextureUnitStateAttributes(MaterialSerializer* ser, SGPass* passEntry, const TextureUnitState* srcTextureUnit); 00873 00878 SGMaterialIterator findMaterialEntryIt(const String& materialName, const String& groupName); 00879 SGMaterialConstIterator findMaterialEntryIt(const String& materialName, const String& groupName) const; 00880 00881 00882 typedef std::pair<SGScheme*, bool> SchemeCreateOrRetrieveResult; 00887 SchemeCreateOrRetrieveResult createOrRetrieveScheme(const String& schemeName); 00888 00890 bool getIsFinalizing() const; 00891 protected: 00892 OGRE_AUTO_MUTEX // Auto mutex. 00893 SceneManager* mActiveSceneMgr; // The active scene manager. 00894 SceneManagerMap mSceneManagerMap; // A map of all scene managers this generator is bound to. 00895 SGRenderObjectListener* mRenderObjectListener; // Render object listener. 00896 SGSceneManagerListener* mSceneManagerListener; // Scene manager listener. 00897 SGScriptTranslatorManager* mScriptTranslatorManager; // Script translator manager. 00898 SGMaterialSerializerListener* mMaterialSerializerListener; // Custom material Serializer listener - allows exporting material that contains shader generated techniques. 00899 SGScriptTranslatorMap mScriptTranslatorsMap; // A map of the registered custom script translators. 00900 SGScriptTranslator mCoreScriptTranslator; // The core translator of the RT Shader System. 00901 String mShaderLanguage; // The target shader language (currently only cg supported). 00902 String mVertexShaderProfiles; // The target vertex shader profile. Will be used as argument for program compilation. 00903 StringVector mVertexShaderProfilesList; // List of target vertex shader profiles. 00904 String mFragmentShaderProfiles; // The target fragment shader profile. Will be used as argument for program compilation. 00905 StringVector mFragmentShaderProfilesList; // List of target fragment shader profiles.. 00906 String mShaderCachePath; // Path for caching the generated shaders. 00907 ProgramManager* mProgramManager; // Shader program manager. 00908 ProgramWriterManager* mProgramWriterManager; // Shader program writer manager. 00909 FFPRenderStateBuilder* mFFPRenderStateBuilder; // Fixed Function Render state builder. 00910 SGMaterialMap mMaterialEntriesMap; // Material entries map. 00911 SGSchemeMap mSchemeEntriesMap; // Scheme entries map. 00912 SGTechniqueMap mTechniqueEntriesMap; // All technique entries map. 00913 SubRenderStateFactoryMap mSubRenderStateFactories; // Sub render state registered factories. 00914 SubRenderStateFactoryMap mSubRenderStateExFactories; // Sub render state core extension factories. 00915 bool mActiveViewportValid; // True if active view port use a valid SGScheme. 00916 int mLightCount[3]; // Light count per light type. 00917 VSOutputCompactPolicy mVSOutputCompactPolicy; // Vertex shader outputs compact policy. 00918 bool mCreateShaderOverProgrammablePass; // Tells whether shaders are created for passes with shaders 00919 bool mIsFinalizing; // a flag to indicate finalizing 00920 private: 00921 friend class SGPass; 00922 friend class FFPRenderStateBuilder; 00923 friend class SGScriptTranslatorManager; 00924 friend class SGScriptTranslator; 00925 friend class SGMaterialSerializerListener; 00926 00927 }; 00928 00932 } 00933 } 00934 00935 #endif 00936
Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:26 2012