| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of OGRE |
|---|
| 4 | (Object-oriented Graphics Rendering Engine) |
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ |
|---|
| 6 | |
|---|
| 7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
|---|
| 8 | |
|---|
| 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 10 | of this software and associated documentation files (the "Software"), to deal |
|---|
| 11 | in the Software without restriction, including without limitation the rights |
|---|
| 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 13 | copies of the Software, and to permit persons to whom the Software is |
|---|
| 14 | furnished to do so, subject to the following conditions: |
|---|
| 15 | |
|---|
| 16 | The above copyright notice and this permission notice shall be included in |
|---|
| 17 | all copies or substantial portions of the Software. |
|---|
| 18 | |
|---|
| 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 25 | THE SOFTWARE. |
|---|
| 26 | ----------------------------------------------------------------------------- |
|---|
| 27 | */ |
|---|
| 28 | #ifndef __CompositorInstance_H__ |
|---|
| 29 | #define __CompositorInstance_H__ |
|---|
| 30 | |
|---|
| 31 | #include "OgrePrerequisites.h" |
|---|
| 32 | #include "OgreMaterialManager.h" |
|---|
| 33 | #include "OgreTexture.h" |
|---|
| 34 | #include "OgreRenderQueue.h" |
|---|
| 35 | #include "OgreCompositionTechnique.h" |
|---|
| 36 | #include "OgreHeaderPrefix.h" |
|---|
| 37 | |
|---|
| 38 | namespace Ogre { |
|---|
| 39 | /** \addtogroup Core |
|---|
| 40 | * @{ |
|---|
| 41 | */ |
|---|
| 42 | /** \addtogroup Effects |
|---|
| 43 | * @{ |
|---|
| 44 | */ |
|---|
| 45 | const size_t RENDER_QUEUE_COUNT = RENDER_QUEUE_MAX+1; |
|---|
| 46 | |
|---|
| 47 | /** An instance of a Compositor object for one Viewport. It is part of the CompositorChain |
|---|
| 48 | for a Viewport. |
|---|
| 49 | */ |
|---|
| 50 | class _OgreExport CompositorInstance : public CompositorInstAlloc |
|---|
| 51 | { |
|---|
| 52 | public: |
|---|
| 53 | CompositorInstance(CompositionTechnique *technique, CompositorChain *chain); |
|---|
| 54 | virtual ~CompositorInstance(); |
|---|
| 55 | /** Provides an interface to "listen in" to to render system operations executed by this |
|---|
| 56 | CompositorInstance. |
|---|
| 57 | */ |
|---|
| 58 | class _OgreExport Listener |
|---|
| 59 | { |
|---|
| 60 | public: |
|---|
| 61 | virtual ~Listener(); |
|---|
| 62 | |
|---|
| 63 | /** Notification of when a render target operation involving a material (like |
|---|
| 64 | rendering a quad) is compiled, so that miscellaneous parameters that are different |
|---|
| 65 | per Compositor instance can be set up. |
|---|
| 66 | @param pass_id |
|---|
| 67 | Pass identifier within Compositor instance, this is specified |
|---|
| 68 | by the user by CompositionPass::setIdentifier(). |
|---|
| 69 | @param mat |
|---|
| 70 | Material, this may be changed at will and will only affect |
|---|
| 71 | the current instance of the Compositor, not the global material |
|---|
| 72 | it was cloned from. |
|---|
| 73 | */ |
|---|
| 74 | virtual void notifyMaterialSetup(uint32 pass_id, MaterialPtr &mat); |
|---|
| 75 | |
|---|
| 76 | /** Notification before a render target operation involving a material (like |
|---|
| 77 | rendering a quad), so that material parameters can be varied. |
|---|
| 78 | @param pass_id |
|---|
| 79 | Pass identifier within Compositor instance, this is specified |
|---|
| 80 | by the user by CompositionPass::setIdentifier(). |
|---|
| 81 | @param mat |
|---|
| 82 | Material, this may be changed at will and will only affect |
|---|
| 83 | the current instance of the Compositor, not the global material |
|---|
| 84 | it was cloned from. |
|---|
| 85 | */ |
|---|
| 86 | virtual void notifyMaterialRender(uint32 pass_id, MaterialPtr &mat); |
|---|
| 87 | |
|---|
| 88 | /** Notification after resources have been created (or recreated). |
|---|
| 89 | @param forResizeOnly |
|---|
| 90 | Was the creation because the viewport was resized? |
|---|
| 91 | */ |
|---|
| 92 | virtual void notifyResourcesCreated(bool forResizeOnly); |
|---|
| 93 | |
|---|
| 94 | }; |
|---|
| 95 | /** Specific render system operation. A render target operation does special operations |
|---|
| 96 | between render queues like rendering a quad, clearing the frame buffer or |
|---|
| 97 | setting stencil state. |
|---|
| 98 | */ |
|---|
| 99 | class _OgreExport RenderSystemOperation : public CompositorInstAlloc |
|---|
| 100 | { |
|---|
| 101 | public: |
|---|
| 102 | virtual ~RenderSystemOperation(); |
|---|
| 103 | /// Set state to SceneManager and RenderSystem |
|---|
| 104 | virtual void execute(SceneManager *sm, RenderSystem *rs) = 0; |
|---|
| 105 | }; |
|---|
| 106 | typedef map<int, MaterialPtr>::type QuadMaterialMap; |
|---|
| 107 | typedef std::pair<int, RenderSystemOperation*> RenderSystemOpPair; |
|---|
| 108 | typedef vector<RenderSystemOpPair>::type RenderSystemOpPairs; |
|---|
| 109 | /** Operation setup for a RenderTarget (collected). |
|---|
| 110 | */ |
|---|
| 111 | class TargetOperation |
|---|
| 112 | { |
|---|
| 113 | public: |
|---|
| 114 | TargetOperation() |
|---|
| 115 | { |
|---|
| 116 | } |
|---|
| 117 | TargetOperation(RenderTarget *inTarget): |
|---|
| 118 | target(inTarget), currentQueueGroupID(0), visibilityMask(0xFFFFFFFF), |
|---|
| 119 | lodBias(1.0f), |
|---|
| 120 | onlyInitial(false), hasBeenRendered(false), findVisibleObjects(false), |
|---|
| 121 | materialScheme(MaterialManager::DEFAULT_SCHEME_NAME), shadowsEnabled(true) |
|---|
| 122 | { |
|---|
| 123 | } |
|---|
| 124 | /// Target |
|---|
| 125 | RenderTarget *target; |
|---|
| 126 | |
|---|
| 127 | /// Current group ID |
|---|
| 128 | int currentQueueGroupID; |
|---|
| 129 | |
|---|
| 130 | /// RenderSystem operations to queue into the scene manager, by |
|---|
| 131 | /// uint8 |
|---|
| 132 | RenderSystemOpPairs renderSystemOperations; |
|---|
| 133 | |
|---|
| 134 | /// Scene visibility mask |
|---|
| 135 | /// If this is 0, the scene is not rendered at all |
|---|
| 136 | uint32 visibilityMask; |
|---|
| 137 | |
|---|
| 138 | /// LOD offset. This is multiplied with the camera LOD offset |
|---|
| 139 | /// 1.0 is default, lower means lower detail, higher means higher detail |
|---|
| 140 | float lodBias; |
|---|
| 141 | |
|---|
| 142 | /** A set of render queues to either include or exclude certain render queues. |
|---|
| 143 | */ |
|---|
| 144 | typedef std::bitset<RENDER_QUEUE_COUNT> RenderQueueBitSet; |
|---|
| 145 | |
|---|
| 146 | /// Which renderqueues to render from scene |
|---|
| 147 | RenderQueueBitSet renderQueues; |
|---|
| 148 | |
|---|
| 149 | /** @see CompositionTargetPass::mOnlyInitial |
|---|
| 150 | */ |
|---|
| 151 | bool onlyInitial; |
|---|
| 152 | /** "Has been rendered" flag; used in combination with |
|---|
| 153 | onlyInitial to determine whether to skip this target operation. |
|---|
| 154 | */ |
|---|
| 155 | bool hasBeenRendered; |
|---|
| 156 | /** Whether this op needs to find visible scene objects or not |
|---|
| 157 | */ |
|---|
| 158 | bool findVisibleObjects; |
|---|
| 159 | /** Which material scheme this op will use */ |
|---|
| 160 | String materialScheme; |
|---|
| 161 | /** Whether shadows will be enabled */ |
|---|
| 162 | bool shadowsEnabled; |
|---|
| 163 | }; |
|---|
| 164 | typedef vector<TargetOperation>::type CompiledState; |
|---|
| 165 | |
|---|
| 166 | /** Set enabled flag. The compositor instance will only render if it is |
|---|
| 167 | enabled, otherwise it is pass-through. Resources are only created if |
|---|
| 168 | they weren't alive when enabling. |
|---|
| 169 | */ |
|---|
| 170 | void setEnabled(bool value); |
|---|
| 171 | |
|---|
| 172 | /** Get enabled flag. |
|---|
| 173 | */ |
|---|
| 174 | bool getEnabled() const { return mEnabled; } |
|---|
| 175 | |
|---|
| 176 | /** Set alive/active flag. The compositor instance will create resources when alive, |
|---|
| 177 | and destroy them when inactive. |
|---|
| 178 | @remarks |
|---|
| 179 | Killing an instance means also disabling it: setAlive(false) implies |
|---|
| 180 | setEnabled(false) |
|---|
| 181 | */ |
|---|
| 182 | void setAlive(bool value); |
|---|
| 183 | |
|---|
| 184 | /** Get alive flag. |
|---|
| 185 | */ |
|---|
| 186 | bool getAlive() const { return mAlive; } |
|---|
| 187 | |
|---|
| 188 | /** Get the instance name for a local texture. |
|---|
| 189 | @note It is only valid to call this when local textures have been loaded, |
|---|
| 190 | which in practice means that the compositor instance is active. Calling |
|---|
| 191 | it at other times will cause an exception. Note that since textures |
|---|
| 192 | are cleaned up aggressively, this name is not guaranteed to stay the |
|---|
| 193 | same if you disable and re-enable the compositor instance. |
|---|
| 194 | @param name |
|---|
| 195 | The name of the texture in the original compositor definition. |
|---|
| 196 | @param mrtIndex |
|---|
| 197 | If name identifies a MRT, which texture attachment to retrieve. |
|---|
| 198 | @return |
|---|
| 199 | The instance name for the texture, corresponds to a real texture. |
|---|
| 200 | */ |
|---|
| 201 | const String& getTextureInstanceName(const String& name, size_t mrtIndex); |
|---|
| 202 | |
|---|
| 203 | /** Get the instance of a local texture. |
|---|
| 204 | @note Textures are only valid when local textures have been loaded, |
|---|
| 205 | which in practice means that the compositor instance is active. Calling |
|---|
| 206 | this method at other times will return null pointers. Note that since textures |
|---|
| 207 | are cleaned up aggressively, this pointer is not guaranteed to stay the |
|---|
| 208 | same if you disable and re-enable the compositor instance. |
|---|
| 209 | @param name |
|---|
| 210 | The name of the texture in the original compositor definition. |
|---|
| 211 | @param mrtIndex |
|---|
| 212 | If name identifies a MRT, which texture attachment to retrieve. |
|---|
| 213 | @return |
|---|
| 214 | The texture pointer, corresponds to a real texture. |
|---|
| 215 | */ |
|---|
| 216 | TexturePtr getTextureInstance(const String& name, size_t mrtIndex); |
|---|
| 217 | |
|---|
| 218 | /** Get the render target for a given render texture name. |
|---|
| 219 | @remarks |
|---|
| 220 | You can use this to add listeners etc, but do not use it to update the |
|---|
| 221 | targets manually or any other modifications, the compositor instance |
|---|
| 222 | is in charge of this. |
|---|
| 223 | */ |
|---|
| 224 | RenderTarget* getRenderTarget(const String& name); |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | /** Recursively collect target states (except for final Pass). |
|---|
| 228 | @param compiledState |
|---|
| 229 | This vector will contain a list of TargetOperation objects. |
|---|
| 230 | */ |
|---|
| 231 | virtual void _compileTargetOperations(CompiledState &compiledState); |
|---|
| 232 | |
|---|
| 233 | /** Compile the final (output) operation. This is done separately because this |
|---|
| 234 | is combined with the input in chained filters. |
|---|
| 235 | */ |
|---|
| 236 | virtual void _compileOutputOperation(TargetOperation &finalState); |
|---|
| 237 | |
|---|
| 238 | /** Get Compositor of which this is an instance |
|---|
| 239 | */ |
|---|
| 240 | Compositor *getCompositor(); |
|---|
| 241 | |
|---|
| 242 | /** Get CompositionTechnique used by this instance |
|---|
| 243 | */ |
|---|
| 244 | CompositionTechnique *getTechnique(); |
|---|
| 245 | |
|---|
| 246 | /** Change the technique we're using to render this compositor. |
|---|
| 247 | @param tech |
|---|
| 248 | The technique to use (must be supported and from the same Compositor) |
|---|
| 249 | @param reuseTextures |
|---|
| 250 | If textures have already been created for the current |
|---|
| 251 | technique, whether to try to re-use them if sizes & formats match. |
|---|
| 252 | */ |
|---|
| 253 | void setTechnique(CompositionTechnique* tech, bool reuseTextures = true); |
|---|
| 254 | |
|---|
| 255 | /** Pick a technique to use to render this compositor based on a scheme. |
|---|
| 256 | @remarks |
|---|
| 257 | If there is no specific supported technique with this scheme name, |
|---|
| 258 | then the first supported technique with no specific scheme will be used. |
|---|
| 259 | @see CompositionTechnique::setSchemeName |
|---|
| 260 | @param schemeName |
|---|
| 261 | The scheme to use |
|---|
| 262 | @param reuseTextures |
|---|
| 263 | If textures have already been created for the current |
|---|
| 264 | technique, whether to try to re-use them if sizes & formats match. |
|---|
| 265 | Note that for this feature to be of benefit, the textures must have been created |
|---|
| 266 | with the 'pooled' option enabled. |
|---|
| 267 | */ |
|---|
| 268 | void setScheme(const String& schemeName, bool reuseTextures = true); |
|---|
| 269 | |
|---|
| 270 | /// Returns the name of the scheme this compositor is using. |
|---|
| 271 | const String& getScheme() const { return mTechnique ? mTechnique->getSchemeName() : StringUtil::BLANK; } |
|---|
| 272 | |
|---|
| 273 | /** Notify this instance that the primary surface has been resized. |
|---|
| 274 | @remarks |
|---|
| 275 | This will allow the instance to recreate its resources that |
|---|
| 276 | are dependent on the size. |
|---|
| 277 | */ |
|---|
| 278 | void notifyResized(); |
|---|
| 279 | |
|---|
| 280 | /** Get Chain that this instance is part of |
|---|
| 281 | */ |
|---|
| 282 | CompositorChain *getChain(); |
|---|
| 283 | |
|---|
| 284 | /** Add a listener. Listeners provide an interface to "listen in" to to render system |
|---|
| 285 | operations executed by this CompositorInstance so that materials can be |
|---|
| 286 | programmatically set up. |
|---|
| 287 | @see CompositorInstance::Listener |
|---|
| 288 | */ |
|---|
| 289 | void addListener(Listener *l); |
|---|
| 290 | |
|---|
| 291 | /** Remove a listener. |
|---|
| 292 | @see CompositorInstance::Listener |
|---|
| 293 | */ |
|---|
| 294 | void removeListener(Listener *l); |
|---|
| 295 | |
|---|
| 296 | /** Notify listeners of a material compilation. |
|---|
| 297 | */ |
|---|
| 298 | void _fireNotifyMaterialSetup(uint32 pass_id, MaterialPtr &mat); |
|---|
| 299 | |
|---|
| 300 | /** Notify listeners of a material render. |
|---|
| 301 | */ |
|---|
| 302 | void _fireNotifyMaterialRender(uint32 pass_id, MaterialPtr &mat); |
|---|
| 303 | |
|---|
| 304 | /** Notify listeners of a material render. |
|---|
| 305 | */ |
|---|
| 306 | void _fireNotifyResourcesCreated(bool forResizeOnly); |
|---|
| 307 | private: |
|---|
| 308 | /// Compositor of which this is an instance. |
|---|
| 309 | Compositor *mCompositor; |
|---|
| 310 | /// Composition technique used by this instance. |
|---|
| 311 | CompositionTechnique *mTechnique; |
|---|
| 312 | /// Composition chain of which this instance is part. |
|---|
| 313 | CompositorChain *mChain; |
|---|
| 314 | /// Is this instance enabled? |
|---|
| 315 | bool mEnabled; |
|---|
| 316 | /// Is this instance allocating resources? |
|---|
| 317 | bool mAlive; |
|---|
| 318 | /// Map from name->local texture. |
|---|
| 319 | typedef map<String,TexturePtr>::type LocalTextureMap; |
|---|
| 320 | LocalTextureMap mLocalTextures; |
|---|
| 321 | /// Store a list of MRTs we've created. |
|---|
| 322 | typedef map<String,MultiRenderTarget*>::type LocalMRTMap; |
|---|
| 323 | LocalMRTMap mLocalMRTs; |
|---|
| 324 | typedef map<CompositionTechnique::TextureDefinition*, TexturePtr>::type ReserveTextureMap; |
|---|
| 325 | /** Textures that are not currently in use, but that we want to keep for now, |
|---|
| 326 | for example if we switch techniques but want to keep all textures available |
|---|
| 327 | in case we switch back. |
|---|
| 328 | */ |
|---|
| 329 | ReserveTextureMap mReserveTextures; |
|---|
| 330 | |
|---|
| 331 | /// Vector of listeners. |
|---|
| 332 | typedef vector<Listener*>::type Listeners; |
|---|
| 333 | Listeners mListeners; |
|---|
| 334 | |
|---|
| 335 | /// Previous instance (set by chain). |
|---|
| 336 | CompositorInstance *mPreviousInstance; |
|---|
| 337 | |
|---|
| 338 | /** Collect rendering passes. Here, passes are converted into render target operations |
|---|
| 339 | and queued with queueRenderSystemOp. |
|---|
| 340 | */ |
|---|
| 341 | virtual void collectPasses(TargetOperation &finalState, CompositionTargetPass *target); |
|---|
| 342 | |
|---|
| 343 | /** Create a local dummy material with one technique but no passes. |
|---|
| 344 | The material is detached from the Material Manager to make sure it is destroyed |
|---|
| 345 | when going out of scope. |
|---|
| 346 | */ |
|---|
| 347 | MaterialPtr createLocalMaterial(const String& srcName); |
|---|
| 348 | |
|---|
| 349 | /** Create local rendertextures and other resources. Builds mLocalTextures. |
|---|
| 350 | */ |
|---|
| 351 | void createResources(bool forResizeOnly); |
|---|
| 352 | |
|---|
| 353 | /** Destroy local rendertextures and other resources. |
|---|
| 354 | */ |
|---|
| 355 | void freeResources(bool forResizeOnly, bool clearReserveTextures); |
|---|
| 356 | |
|---|
| 357 | /** Get RenderTarget for a named local texture. |
|---|
| 358 | */ |
|---|
| 359 | RenderTarget *getTargetForTex(const String &name); |
|---|
| 360 | |
|---|
| 361 | /** Get source texture name for a named local texture. |
|---|
| 362 | @param name |
|---|
| 363 | The local name of the texture as given to it in the compositor. |
|---|
| 364 | @param mrtIndex |
|---|
| 365 | For MRTs, which attached surface to retrieve. |
|---|
| 366 | */ |
|---|
| 367 | const String &getSourceForTex(const String &name, size_t mrtIndex = 0); |
|---|
| 368 | |
|---|
| 369 | /** Queue a render system operation. |
|---|
| 370 | */ |
|---|
| 371 | void queueRenderSystemOp(TargetOperation &finalState, RenderSystemOperation *op); |
|---|
| 372 | |
|---|
| 373 | /// Util method for assigning a local texture name to a MRT attachment |
|---|
| 374 | String getMRTTexLocalName(const String& baseName, size_t attachment); |
|---|
| 375 | |
|---|
| 376 | /** Search for options like AA and hardware gamma which we may want to |
|---|
| 377 | inherit from the main render target to which we're attached. |
|---|
| 378 | */ |
|---|
| 379 | void deriveTextureRenderTargetOptions(const String& texname, |
|---|
| 380 | bool *hwGammaWrite, uint *fsaa, String* fsaaHint); |
|---|
| 381 | |
|---|
| 382 | /// Notify this instance that the primary viewport's camera has changed. |
|---|
| 383 | void notifyCameraChanged(Camera* camera); |
|---|
| 384 | |
|---|
| 385 | friend class CompositorChain; |
|---|
| 386 | }; |
|---|
| 387 | /** @} */ |
|---|
| 388 | /** @} */ |
|---|
| 389 | |
|---|
| 390 | } // namespace Ogre |
|---|
| 391 | |
|---|
| 392 | #include "OgreHeaderSuffix.h" |
|---|
| 393 | |
|---|
| 394 | #endif // __CompositorInstance_H__ |
|---|