# OgreShaderGenerator.h

Go to the documentation of this file.

    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-2009 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
    00204     RenderState*    getRenderState              (const String& schemeName, const String& materialName, unsigned short passIndex);
    00205
    00212     void            addSubRenderStateFactory    (SubRenderStateFactory* factory);
    00213
    00218     void            removeSubRenderStateFactory (SubRenderStateFactory* factory);
    00219
    00224     SubRenderState* createSubRenderState        (const String& type);
    00225
    00226
    00231     void            destroySubRenderState       (SubRenderState* subRenderState);
    00232
    00233
    00242     bool            createShaderBasedTechnique  (const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName);
    00243
    00244
    00253     bool            removeShaderBasedTechnique  (const String& materialName, const String& srcTechniqueSchemeName, const String& dstTechniqueSchemeName);
    00254
    00255
    00261     bool            removeAllShaderBasedTechniques  (const String& materialName);
    00262
    00266     void            removeAllShaderBasedTechniques  ();
    00267
    00272     void            createScheme                (const String& schemeName);
    00273
    00279     void            invalidateScheme            (const String& schemeName);
    00280
    00286     bool            validateScheme              (const String& schemeName);
    00287
    00294     void            invalidateMaterial          (const String& schemeName, const String& materialName);
    00295
    00302     bool            validateMaterial            (const String& schemeName, const String& materialName);
    00303
    00304
    00316     SGMaterialSerializerListener*   getMaterialSerializerListener();
    00317
    00318
    00320     size_t                          getVertexShaderCount            () const;
    00321
    00322
    00324     size_t                          getFragmentShaderCount          () const;
    00325
    00326
    00327
    00332     void                            setVertexShaderOutputsCompactPolicy     (VSOutputCompactPolicy policy)  { mVSOutputCompactPolicy = policy; }
    00333
    00337     VSOutputCompactPolicy           getVertexShaderOutputsCompactPolicy     () const { return mVSOutputCompactPolicy; }
    00338
    00340     static String DEFAULT_SCHEME_NAME;
    00341
    00342 // Protected types.
    00343 protected:
    00344     class SGPass;
    00345     class SGTechnique;
    00346     class SGMaterial;
    00347     class SGScheme;
    00348
    00349     typedef vector<SGPass*>::type                   SGPassList;
    00350     typedef SGPassList::iterator                    SGPassIterator;
    00351     typedef SGPassList::const_iterator              SGPassConstIterator;
    00352
    00353     typedef vector<SGTechnique*>::type              SGTechniqueList;
    00354     typedef SGTechniqueList::iterator               SGTechniqueIterator;
    00355     typedef SGTechniqueList::const_iterator         SGTechniqueConstIterator;
    00356
    00357     typedef map<SGTechnique*, SGTechnique*>::type   SGTechniqueMap;
    00358     typedef SGTechniqueMap::iterator                SGTechniqueMapIterator;
    00359
    00360     typedef map<String, SGMaterial*>::type          SGMaterialMap;
    00361     typedef SGMaterialMap::iterator                 SGMaterialIterator;
    00362     typedef SGMaterialMap::const_iterator           SGMaterialConstIterator;
    00363
    00364     typedef map<String, SGScheme*>::type            SGSchemeMap;
    00365     typedef SGSchemeMap::iterator                   SGSchemeIterator;
    00366     typedef SGMaterialMap::const_iterator           SGSchemeConstIterator;
    00367
    00368     typedef map<String, ScriptTranslator*>::type    SGScriptTranslatorMap;
    00369     typedef SGScriptTranslatorMap::iterator         SGScriptTranslatorIterator;
    00370     typedef SGScriptTranslatorMap::const_iterator   SGScriptTranslatorConstIterator;
    00371
    00372
    00373
    00375     class _OgreRTSSExport SGPass : public RTShaderSystemAlloc
    00376     {
    00377     public:
    00378         SGPass          (SGTechnique* parent, Pass* srcPass, Pass* dstPass);
    00379         ~SGPass         ();
    00380
    00382         void            buildTargetRenderState          ();
    00383
    00385         void            acquirePrograms         ();
    00386
    00388         void            releasePrograms         ();
    00389
    00390
    00392         void            notifyRenderSingleObject    (Renderable* rend, const AutoParamDataSource* source, const LightList* pLightList, bool suppressRenderStateChanges);
    00393
    00395         Pass*           getSrcPass                  () { return mSrcPass; }
    00396
    00398         Pass*           getDstPass                  () { return mDstPass; }
    00399
    00401         SubRenderState* getCustomFFPSubState        (int subStateOrder);
    00402
    00404         RenderState*    getCustomRenderState        () { return mCustomRenderState; }
    00405
    00407         void            setCustomRenderState        (RenderState* customRenderState) { mCustomRenderState = customRenderState; }
    00408
    00409         static String   UserKey;                    // Key name for associating with a Pass instance.
    00410
    00411     protected:
    00412         SubRenderState* getCustomFFPSubState        (int subStateOrder, const RenderState* renderState);
    00413
    00414     protected:
    00415         SGTechnique*            mParent;                // Parent technique.
    00416         Pass*                   mSrcPass;               // Source pass.
    00417         Pass*                   mDstPass;               // Destination pass.
    00418         RenderState*            mCustomRenderState;     // Custom render state.
    00419         TargetRenderState*      mTargetRenderState; // The compiled render state.
    00420     };
    00421
    00422
    00424     class _OgreRTSSExport SGTechnique : public RTShaderSystemAlloc
    00425     {
    00426     public:
    00427         SGTechnique         (SGMaterial* parent, Technique* srcTechnique, const String& dstTechniqueSchemeName);
    00428         ~SGTechnique        ();
    00429
    00431         const SGMaterial*   getParent                       () const { return mParent; }
    00432
    00434         Technique*          getSourceTechnique              () { return mSrcTechnique; }
    00435
    00437         Technique*          getDestinationTechnique         () { return mDstTechnique; }
    00438
    00440         const String&       getDestinationTechniqueSchemeName() const { return mDstTechniqueSchemeName; }
    00441
    00443         void                buildTargetRenderState              ();
    00444
    00446         void                acquirePrograms             ();
    00447
    00449         void                releasePrograms             ();
    00450
    00452         void                setBuildDestinationTechnique    (bool buildTechnique)   { mBuildDstTechnique = buildTechnique; }
    00453
    00455         bool                getBuildDestinationTechnique    () const                { return mBuildDstTechnique; }
    00456
    00460         RenderState*        getRenderState                  (unsigned short passIndex);
    00461
    00462         static String   UserKey;                    // Key name for associating with a Technique instance.
    00463
    00464     protected:
    00465
    00467         void                createSGPasses          ();
    00468
    00470         void                destroySGPasses         ();
    00471
    00472
    00473     protected:
    00474         SGMaterial*             mParent;                    // Parent material.
    00475         Technique*              mSrcTechnique;              // Source technique.
    00476         Technique*              mDstTechnique;              // Destination technique.
    00477         SGPassList              mPassEntries;               // All passes entries.
    00478         RenderStateList         mCustomRenderStates;        // The custom render states of all passes.
    00479         bool                    mBuildDstTechnique;         // Flag that tells if destination technique should be build.
    00480         String                  mDstTechniqueSchemeName;    // Scheme name of destination technique.
    00481     };
    00482
    00483
    00485     class _OgreRTSSExport SGMaterial : public RTShaderSystemAlloc
    00486     {
    00487
    00488     public:
    00490         SGMaterial(const String& materialName)                  { mName = materialName; }
    00491
    00493         const String& getMaterialName               () const    { return mName; }
    00494
    00496         const SGTechniqueList&  getTechniqueList    () const     { return mTechniqueEntires; }
    00497
    00499         SGTechniqueList&    getTechniqueList        ()           { return mTechniqueEntires; }
    00500
    00501     protected:
    00502         String              mName;                  // The material name.
    00503         SGTechniqueList     mTechniqueEntires;      // All passes entries.
    00504     };
    00505
    00506
    00508     class _OgreRTSSExport SGScheme : public RTShaderSystemAlloc
    00509     {
    00510     public:
    00511         SGScheme        (const String& schemeName);
    00512         ~SGScheme       ();
    00513
    00514
    00517         bool                    empty               () const  { return mTechniqueEntires.empty(); }
    00518
    00522         void                    invalidate              ();
    00523
    00527         void                    validate                ();
    00528
    00532         void                    invalidate              (const String& materialName);
    00533
    00537         bool                    validate                (const String& materialName);
    00538
    00540         void                    addTechniqueEntry       (SGTechnique* techEntry);
    00541
    00543         void                    removeTechniqueEntry    (SGTechnique* techEntry);
    00544
    00545
    00549         RenderState*            getRenderState          ();
    00550
    00554         RenderState*            getRenderState          (const String& materialName, unsigned short passIndex);
    00555
    00556     protected:
    00558         void                    synchronizeWithLightSettings    ();
    00559
    00561         void                    synchronizeWithFogSettings      ();
    00562
    00563
    00564     protected:
    00565         String                  mName;                  // Scheme name.
    00566         SGTechniqueList         mTechniqueEntires;      // Technique entries.
    00567         bool                    mOutOfDate;             // Tells if this scheme is out of date.
    00568         RenderState*            mRenderState;           // The global render state of this scheme.
    00569         FogMode                 mFogMode;               // Current fog mode.
    00570     };
    00571
    00572
    00573 // Protected types.
    00574 protected:
    00575
    00577     class _OgreRTSSExport SGRenderObjectListener : public RenderObjectListener, public RTShaderSystemAlloc
    00578     {
    00579     public:
    00580         SGRenderObjectListener(ShaderGenerator* owner)
    00581         {
    00582             mOwner = owner;
    00583         }
    00584
    00588         virtual void notifyRenderSingleObject(Renderable* rend, const Pass* pass,
    00589             const AutoParamDataSource* source,
    00590             const LightList* pLightList, bool suppressRenderStateChanges)
    00591         {
    00592             mOwner->notifyRenderSingleObject(rend, pass, source, pLightList, suppressRenderStateChanges);
    00593         }
    00594
    00595     protected:
    00596         ShaderGenerator* mOwner;
    00597     };
    00598
    00600     class _OgreRTSSExport SGSceneManagerListener : public SceneManager::Listener, public RTShaderSystemAlloc
    00601     {
    00602     public:
    00603         SGSceneManagerListener(ShaderGenerator* owner)
    00604         {
    00605             mOwner = owner;
    00606         }
    00607
    00611         virtual void preFindVisibleObjects(SceneManager* source,
    00612             SceneManager::IlluminationRenderStage irs, Viewport* v)
    00613         {
    00614             mOwner->preFindVisibleObjects(source, irs, v);
    00615         }
    00616
    00617         virtual void postFindVisibleObjects(SceneManager* source,
    00618             SceneManager::IlluminationRenderStage irs, Viewport* v)
    00619         {
    00620
    00621         }
    00622
    00623         virtual void shadowTexturesUpdated(size_t numberOfShadowTextures)
    00624         {
    00625
    00626         }
    00627
    00628         virtual void shadowTextureCasterPreViewProj(Light* light,
    00629             Camera* camera, size_t iteration)
    00630         {
    00631
    00632         }
    00633
    00634         virtual void shadowTextureReceiverPreViewProj(Light* light,
    00635             Frustum* frustum)
    00636         {
    00637
    00638         }
    00639
    00640     protected:
    00641         ShaderGenerator* mOwner;            // The shader generator instance.
    00642     };
    00643
    00645     class _OgreRTSSExport SGScriptTranslatorManager : public ScriptTranslatorManager
    00646     {
    00647     public:
    00648         SGScriptTranslatorManager(ShaderGenerator* owner)
    00649         {
    00650             mOwner = owner;
    00651         }
    00652
    00654         virtual size_t getNumTranslators() const
    00655         {
    00656             return mOwner->getNumTranslators();
    00657         }
    00658
    00660         virtual ScriptTranslator *getTranslator(const AbstractNodePtr& node)
    00661         {
    00662             return mOwner->getTranslator(node);
    00663         }
    00664
    00665     protected:
    00666         ShaderGenerator* mOwner;        // The shader generator instance.
    00667     };
    00668
    00669     //-----------------------------------------------------------------------------
    00670     typedef map<String, SubRenderStateFactory*>::type       SubRenderStateFactoryMap;
    00671     typedef SubRenderStateFactoryMap::iterator              SubRenderStateFactoryIterator;
    00672     typedef SubRenderStateFactoryMap::const_iterator        SubRenderStateFactoryConstIterator;
    00673
    00674     //-----------------------------------------------------------------------------
    00675     typedef map<String, SceneManager*>::type                SceneManagerMap;
    00676     typedef SceneManagerMap::iterator                       SceneManagerIterator;
    00677     typedef SceneManagerMap::const_iterator                 SceneManagerConstIterator;
    00678
    00679 protected:
    00681     ShaderGenerator     ();
    00682
    00684     ~ShaderGenerator    ();
    00685
    00687     bool                _initialize         ();
    00688
    00690     void                _finalize           ();
    00691
    00693     Technique*          findSourceTechnique             (const String& materialName, const String& srcTechniqueSchemeName);
    00694
    00696     void                notifyRenderSingleObject        (Renderable* rend, const Pass* pass,  const AutoParamDataSource* source, const LightList* pLightList, bool suppressRenderStateChanges);
    00697
    00699     void                preFindVisibleObjects           (SceneManager* source, SceneManager::IlluminationRenderStage irs, Viewport* v);
    00700
    00702     void                createSubRenderStateExFactories         ();
    00703
    00705     void                destroySubRenderStateExFactories        ();
    00706
    00714     SubRenderState*     createSubRenderState                (ScriptCompiler* compiler, PropertyAbstractNode* prop, Pass* pass);
    00715
    00722     bool                addCustomScriptTranslator           (const String& key, ScriptTranslator* translator);
    00723
    00729     bool                removeCustomScriptTranslator        (const String& key);
    00730
    00732     size_t              getNumTranslators                   () const;
    00733
    00735     ScriptTranslator*   getTranslator                       (const AbstractNodePtr& node);
    00736
    00737
    00743     void                serializePassAttributes             (MaterialSerializer* ser, SGPass* passEntry);
    00744
    00745
    00746
    00747 protected:
    00748     OGRE_AUTO_MUTEX                                                 // Auto mutex.
    00749     SceneManager*                   mActiveSceneMgr;                // The active scene manager.
    00750     SceneManagerMap                 mSceneManagerMap;               // A map of all scene managers this generator is bound to.
    00751     SGRenderObjectListener*         mRenderObjectListener;          // Render object listener.
    00752     SGSceneManagerListener*         mSceneManagerListener;          // Scene manager listener.
    00753     SGScriptTranslatorManager*      mScriptTranslatorManager;       // Script translator manager.
    00754     SGMaterialSerializerListener*   mMaterialSerializerListener;    // Custom material Serializer listener - allows exporting material that contains shader generated techniques.
    00755     SGScriptTranslatorMap           mScriptTranslatorsMap;          // A map of the registered custom script translators.
    00756     SGScriptTranslator              mCoreScriptTranslator;          // The core translator of the RT Shader System.
    00757     String                          mShaderLanguage;                // The target shader language (currently only cg supported).
    00758     String                          mVertexShaderProfiles;          // The target vertex shader profile. Will be used as argument for program compilation.
    00759     StringVector                    mVertexShaderProfilesList;      // List of target vertex shader profiles.
    00760     String                          mFragmentShaderProfiles;        // The target fragment shader profile. Will be used as argument for program compilation.
    00761     StringVector                    mFragmentShaderProfilesList;    // List of target fragment shader profiles..
    00762     String                          mShaderCachePath;               // Path for caching the generated shaders.
    00763     ProgramManager*                 mProgramManager;                // Shader program manager.
    00764     ProgramWriterManager*           mProgramWriterManager;          // Shader program writer manager.
    00765     FFPRenderStateBuilder*          mFFPRenderStateBuilder;         // Fixed Function Render state builder.
    00766     SGMaterialMap                   mMaterialEntriesMap;            // Material entries map.
    00767     SGSchemeMap                     mSchemeEntriesMap;              // Scheme entries map.
    00768     SGTechniqueMap                  mTechniqueEntriesMap;           // All technique entries map.
    00769     SubRenderStateFactoryMap        mSubRenderStateFactories;       // Sub render state registered factories.
    00770     SubRenderStateFactoryMap        mSubRenderStateExFactories;     // Sub render state core extension factories.
    00771     bool                            mActiveViewportValid;           // True if active view port use a valid SGScheme.
    00772     int                             mLightCount[3];                 // Light count per light type.
    00773     VSOutputCompactPolicy           mVSOutputCompactPolicy;         // Vertex shader outputs compact policy.
    00774
    00775 private:
    00776     friend class SGPass;
    00777     friend class FFPRenderStateBuilder;
    00778     friend class SGScriptTranslatorManager;
    00779     friend class SGScriptTranslator;
    00780     friend class SGMaterialSerializerListener;
    00781
    00782 };
    00783
    00787 }
    00788 }
    00789
    00790 #endif
    00791

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun May 8 2011 20:28:09