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 __CompositorManager_H__ |
---|
29 | #define __CompositorManager_H__ |
---|
30 | |
---|
31 | #include "OgrePrerequisites.h" |
---|
32 | #include "OgreResourceManager.h" |
---|
33 | #include "OgreCompositor.h" |
---|
34 | #include "OgreRectangle2D.h" |
---|
35 | #include "OgreRenderSystem.h" |
---|
36 | #include "OgreCompositionTechnique.h" |
---|
37 | #include "OgreHeaderPrefix.h" |
---|
38 | |
---|
39 | namespace Ogre { |
---|
40 | /** \addtogroup Core |
---|
41 | * @{ |
---|
42 | */ |
---|
43 | /** \addtogroup Effects |
---|
44 | * @{ |
---|
45 | */ |
---|
46 | /** Class for managing Compositor settings for Ogre. Compositors provide the means |
---|
47 | to flexibly "composite" the final rendering result from multiple scene renders |
---|
48 | and intermediate operations like rendering fullscreen quads. This makes |
---|
49 | it possible to apply postfilter effects, HDRI postprocessing, and shadow |
---|
50 | effects to a Viewport. |
---|
51 | @par |
---|
52 | When loaded from a script, a Compositor is in an 'unloaded' state and only stores the settings |
---|
53 | required. It does not at that stage load any textures. This is because the material settings may be |
---|
54 | loaded 'en masse' from bulk material script files, but only a subset will actually be required. |
---|
55 | @par |
---|
56 | Because this is a subclass of ResourceManager, any files loaded will be searched for in any path or |
---|
57 | archive added to the resource paths/archives. See ResourceManager for details. |
---|
58 | */ |
---|
59 | class _OgreExport CompositorManager : public ResourceManager, public Singleton<CompositorManager> |
---|
60 | { |
---|
61 | public: |
---|
62 | CompositorManager(); |
---|
63 | virtual ~CompositorManager(); |
---|
64 | |
---|
65 | /// Overridden from ResourceManager |
---|
66 | Resource* createImpl(const String& name, ResourceHandle handle, |
---|
67 | const String& group, bool isManual, ManualResourceLoader* loader, |
---|
68 | const NameValuePairList* params); |
---|
69 | |
---|
70 | /** Initialises the Compositor manager, which also triggers it to |
---|
71 | parse all available .compositor scripts. */ |
---|
72 | void initialise(void); |
---|
73 | |
---|
74 | /** |
---|
75 | * Create a new compositor |
---|
76 | * @see ResourceManager::createResource |
---|
77 | */ |
---|
78 | CompositorPtr create (const String& name, const String& group, |
---|
79 | bool isManual = false, ManualResourceLoader* loader = 0, |
---|
80 | const NameValuePairList* createParams = 0); |
---|
81 | |
---|
82 | /// Get a resource by name |
---|
83 | /// @see ResourceManager::getResourceByName |
---|
84 | CompositorPtr getByName(const String& name, const String& groupName = ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME); |
---|
85 | |
---|
86 | |
---|
87 | /** @see ScriptLoader::parseScript |
---|
88 | */ |
---|
89 | void parseScript(DataStreamPtr& stream, const String& groupName); |
---|
90 | |
---|
91 | /** Get the compositor chain for a Viewport. If there is none yet, a new |
---|
92 | compositor chain is registered. |
---|
93 | XXX We need a _notifyViewportRemoved to find out when this viewport disappears, |
---|
94 | so we can destroy its chain as well. |
---|
95 | */ |
---|
96 | CompositorChain *getCompositorChain(Viewport *vp); |
---|
97 | |
---|
98 | /** Returns whether exists compositor chain for a viewport. |
---|
99 | */ |
---|
100 | bool hasCompositorChain(Viewport *vp) const; |
---|
101 | |
---|
102 | /** Remove the compositor chain from a viewport if exists. |
---|
103 | */ |
---|
104 | void removeCompositorChain(Viewport *vp); |
---|
105 | |
---|
106 | /** Add a compositor to a viewport. By default, it is added to end of the chain, |
---|
107 | after the other compositors. |
---|
108 | @param vp Viewport to modify |
---|
109 | @param compositor The name of the compositor to apply |
---|
110 | @param addPosition At which position to add, defaults to the end (-1). |
---|
111 | @return pointer to instance, or 0 if it failed. |
---|
112 | */ |
---|
113 | CompositorInstance *addCompositor(Viewport *vp, const String &compositor, int addPosition=-1); |
---|
114 | |
---|
115 | /** Remove a compositor from a viewport |
---|
116 | */ |
---|
117 | void removeCompositor(Viewport *vp, const String &compositor); |
---|
118 | |
---|
119 | /** Set the state of a compositor on a viewport to enabled or disabled. |
---|
120 | Disabling a compositor stops it from rendering but does not free any resources. |
---|
121 | This can be more efficient than using removeCompositor and addCompositor in cases |
---|
122 | the filter is switched on and off a lot. |
---|
123 | */ |
---|
124 | void setCompositorEnabled(Viewport *vp, const String &compositor, bool value); |
---|
125 | |
---|
126 | /** Get a textured fullscreen 2D rectangle, for internal use. |
---|
127 | */ |
---|
128 | Renderable *_getTexturedRectangle2D(); |
---|
129 | |
---|
130 | /** Overridden from ResourceManager since we have to clean up chains too. */ |
---|
131 | void removeAll(void); |
---|
132 | |
---|
133 | /** Internal method for forcing all active compositors to recreate their resources. */ |
---|
134 | void _reconstructAllCompositorResources(); |
---|
135 | |
---|
136 | typedef set<Texture*>::type UniqueTextureSet; |
---|
137 | |
---|
138 | /** Utility function to get an existing pooled texture matching a given |
---|
139 | definition, or creating one if one doesn't exist. It also takes into |
---|
140 | account whether a pooled texture has already been supplied to this |
---|
141 | same requester already, in which case it won't give the same texture |
---|
142 | twice (this is important for example if you request 2 ping-pong textures, |
---|
143 | you don't want to get the same texture for both requests! |
---|
144 | */ |
---|
145 | TexturePtr getPooledTexture(const String& name, const String& localName, |
---|
146 | size_t w, size_t h, |
---|
147 | PixelFormat f, uint aa, const String& aaHint, bool srgb, UniqueTextureSet& texturesAlreadyAssigned, |
---|
148 | CompositorInstance* inst, CompositionTechnique::TextureScope scope); |
---|
149 | |
---|
150 | /** Free pooled textures from the shared pool (compositor instances still |
---|
151 | using them will keep them in memory though). |
---|
152 | */ |
---|
153 | void freePooledTextures(bool onlyIfUnreferenced = true); |
---|
154 | |
---|
155 | /** Register a compositor logic for listening in to expecting composition |
---|
156 | techniques. |
---|
157 | */ |
---|
158 | void registerCompositorLogic(const String& name, CompositorLogic* logic); |
---|
159 | |
---|
160 | /** Removes a listener for compositor logic registered with registerCompositorLogic |
---|
161 | */ |
---|
162 | void unregisterCompositorLogic(const String& name); |
---|
163 | |
---|
164 | /** Get a compositor logic by its name |
---|
165 | */ |
---|
166 | CompositorLogic* getCompositorLogic(const String& name); |
---|
167 | |
---|
168 | /** Register a custom composition pass. |
---|
169 | */ |
---|
170 | void registerCustomCompositionPass(const String& name, CustomCompositionPass* customPass); |
---|
171 | |
---|
172 | /** Get a custom composition pass by its name |
---|
173 | */ |
---|
174 | CustomCompositionPass* getCustomCompositionPass(const String& name); |
---|
175 | |
---|
176 | /** Override standard Singleton retrieval. |
---|
177 | @remarks |
---|
178 | Why do we do this? Well, it's because the Singleton |
---|
179 | implementation is in a .h file, which means it gets compiled |
---|
180 | into anybody who includes it. This is needed for the |
---|
181 | Singleton template to work, but we actually only want it |
---|
182 | compiled into the implementation of the class based on the |
---|
183 | Singleton, not all of them. If we don't change this, we get |
---|
184 | link errors when trying to use the Singleton-based class from |
---|
185 | an outside dll. |
---|
186 | @par |
---|
187 | This method just delegates to the template version anyway, |
---|
188 | but the implementation stays in this single compilation unit, |
---|
189 | preventing link errors. |
---|
190 | */ |
---|
191 | static CompositorManager& getSingleton(void); |
---|
192 | /** Override standard Singleton retrieval. |
---|
193 | @remarks |
---|
194 | Why do we do this? Well, it's because the Singleton |
---|
195 | implementation is in a .h file, which means it gets compiled |
---|
196 | into anybody who includes it. This is needed for the |
---|
197 | Singleton template to work, but we actually only want it |
---|
198 | compiled into the implementation of the class based on the |
---|
199 | Singleton, not all of them. If we don't change this, we get |
---|
200 | link errors when trying to use the Singleton-based class from |
---|
201 | an outside dll. |
---|
202 | @par |
---|
203 | This method just delegates to the template version anyway, |
---|
204 | but the implementation stays in this single compilation unit, |
---|
205 | preventing link errors. |
---|
206 | */ |
---|
207 | static CompositorManager* getSingletonPtr(void); |
---|
208 | |
---|
209 | |
---|
210 | private: |
---|
211 | typedef map<Viewport*, CompositorChain*>::type Chains; |
---|
212 | Chains mChains; |
---|
213 | |
---|
214 | /** Clear composition chains for all viewports |
---|
215 | */ |
---|
216 | void freeChains(); |
---|
217 | |
---|
218 | Rectangle2D *mRectangle; |
---|
219 | |
---|
220 | /// List of instances |
---|
221 | typedef vector<CompositorInstance *>::type Instances; |
---|
222 | Instances mInstances; |
---|
223 | |
---|
224 | /// Map of registered compositor logics |
---|
225 | typedef map<String, CompositorLogic*>::type CompositorLogicMap; |
---|
226 | CompositorLogicMap mCompositorLogics; |
---|
227 | |
---|
228 | /// Map of registered custom composition passes |
---|
229 | typedef map<String, CustomCompositionPass*>::type CustomCompositionPassMap; |
---|
230 | CustomCompositionPassMap mCustomCompositionPasses; |
---|
231 | |
---|
232 | typedef vector<TexturePtr>::type TextureList; |
---|
233 | typedef VectorIterator<TextureList> TextureIterator; |
---|
234 | |
---|
235 | struct TextureDef |
---|
236 | { |
---|
237 | size_t width, height; |
---|
238 | PixelFormat format; |
---|
239 | uint fsaa; |
---|
240 | String fsaaHint; |
---|
241 | bool sRGBwrite; |
---|
242 | |
---|
243 | TextureDef(size_t w, size_t h, PixelFormat f, uint aa, const String& aaHint, bool srgb) |
---|
244 | : width(w), height(h), format(f), fsaa(aa), fsaaHint(aaHint), sRGBwrite(srgb) |
---|
245 | { |
---|
246 | |
---|
247 | } |
---|
248 | }; |
---|
249 | struct TextureDefLess |
---|
250 | { |
---|
251 | bool _OgreExport operator()(const TextureDef& x, const TextureDef& y) const |
---|
252 | { |
---|
253 | if (x.format < y.format) |
---|
254 | return true; |
---|
255 | else if (x.format == y.format) |
---|
256 | { |
---|
257 | if (x.width < y.width) |
---|
258 | return true; |
---|
259 | else if (x.width == y.width) |
---|
260 | { |
---|
261 | if (x.height < y.height) |
---|
262 | return true; |
---|
263 | else if (x.height == y.height) |
---|
264 | { |
---|
265 | if (x.fsaa < y.fsaa) |
---|
266 | return true; |
---|
267 | else if (x.fsaa == y.fsaa) |
---|
268 | { |
---|
269 | if (x.fsaaHint < y.fsaaHint) |
---|
270 | return true; |
---|
271 | else if (x.fsaaHint == y.fsaaHint) |
---|
272 | { |
---|
273 | if (!x.sRGBwrite && y.sRGBwrite) |
---|
274 | return true; |
---|
275 | } |
---|
276 | |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | } |
---|
281 | return false; |
---|
282 | } |
---|
283 | virtual ~TextureDefLess() {} |
---|
284 | }; |
---|
285 | typedef map<TextureDef, TextureList*, TextureDefLess>::type TexturesByDef; |
---|
286 | TexturesByDef mTexturesByDef; |
---|
287 | |
---|
288 | typedef std::pair<String, String> StringPair; |
---|
289 | typedef map<TextureDef, TexturePtr, TextureDefLess>::type TextureDefMap; |
---|
290 | typedef map<StringPair, TextureDefMap>::type ChainTexturesByDef; |
---|
291 | |
---|
292 | ChainTexturesByDef mChainTexturesByDef; |
---|
293 | |
---|
294 | bool isInputPreviousTarget(CompositorInstance* inst, const Ogre::String& localName); |
---|
295 | bool isInputPreviousTarget(CompositorInstance* inst, TexturePtr tex); |
---|
296 | bool isInputToOutputTarget(CompositorInstance* inst, const Ogre::String& localName); |
---|
297 | bool isInputToOutputTarget(CompositorInstance* inst, TexturePtr tex); |
---|
298 | |
---|
299 | }; |
---|
300 | /** @} */ |
---|
301 | /** @} */ |
---|
302 | |
---|
303 | } |
---|
304 | |
---|
305 | #include "OgreHeaderSuffix.h" |
---|
306 | |
---|
307 | #endif |
---|