12bool SHdrHandler::compositorCreated =
false;
20 compositorCreated =
false;
23SHdrHandler::SHdrHandler()
27SHdrHandler::SHdrHandler(
SViewPort* targetViewportInstance)
30 inputCompositorInstance = 0;
32 targetViewport = targetViewportInstance;
36 ogreCompositorManager = Ogre::CompositorManager::getSingletonPtr();
40 isSupported = caps && caps->hasCapability(Ogre::RSC_VERTEX_PROGRAM) &&
41 caps->hasCapability(Ogre::RSC_FRAGMENT_PROGRAM) &&
42 caps->hasCapability(Ogre::RSC_NON_POWER_OF_2_TEXTURES) &&
43 !caps->hasCapability(Ogre::RSC_MRT_SAME_BIT_DEPTHS) &&
44 (caps->isShaderProfileSupported(
"ps_3_0") || caps->isShaderProfileSupported(
"ps_4_0") || (caps->isShaderProfileSupported(
"arbfp1")) || (caps->isShaderProfileSupported(
"glsles")) || (caps->isShaderProfileSupported(
"glsl300es")));
51 inputCompositorInstance = ogreCompositorManager->addCompositor(targetViewport->
GetOgreViewPortPointer(), std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr");
54 if(inputCompositorInstance)
56 inputCompositorInstance->getTechnique()->getTargetPass(1)->getPasses()[0]->setAutomaticColour(
true);
69 if (inputCompositorInstance)
71 ogreCompositorManager->removeCompositor(targetViewport->
GetOgreViewPortPointer(), std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr");
72 inputCompositorInstance = 0;
75 Ogre::CompositorChain* compositorChain = ogreCompositorManager->getCompositorChain(targetViewport->
GetOgreViewPortPointer());
77 if(compositorChain->getCompositorInstances().empty())
80 ogreCompositorManager = 0;
93 if(enable != isEnable)
98 ogreCompositorManager->setCompositorEnabled(targetViewport->
GetOgreViewPortPointer(), std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr", isEnable);
119void SHdrHandler::CreateCompositors()
122 if(!compositorCreated)
124 Ogre::StringVector resourceGroups = Ogre::ResourceGroupManager::getSingletonPtr()->getResourceGroups();
125 if(std::find(resourceGroups.begin(), resourceGroups.end(), SO3_INTERNAL_RESOURCE_GROUP) == resourceGroups.end())
126 Ogre::ResourceGroupManager::getSingletonPtr()->createResourceGroup(SO3_INTERNAL_RESOURCE_GROUP);
129 GenerateHdrCompositor();
132 compositorCreated =
true;
136void SHdrHandler::GenerateHdrCompositor()
139 Ogre::CompositorPtr hdrCompositor = ogreCompositorManager->create(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr", SO3_INTERNAL_RESOURCE_GROUP);
142 Ogre::CompositionTechnique* hdrCompositionTechnique = hdrCompositor->createTechnique();
143 hdrCompositionTechnique->setCompositorLogicName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Logic");
145 Ogre::PixelFormat rttformat = Ogre::PF_BYTE_RGB;
148 Ogre::PixelFormatList pixelFormatList;
149 pixelFormatList.push_back(rttformat);
152 Ogre::CompositionTechnique::TextureDefinition* previousTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/Previous");
153 previousTexture->width = 0;
154 previousTexture->height = 0;
155 previousTexture->formatList = pixelFormatList;
157 Ogre::CompositionTargetPass* compositionTargetPrevious = hdrCompositionTechnique->createTargetPass();
158 compositionTargetPrevious->setShadowsEnabled(
false);
159 compositionTargetPrevious->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/Previous");
160 compositionTargetPrevious->setInputMode(Ogre::CompositionTargetPass::IM_PREVIOUS);
163 pixelFormatList.clear();
164 pixelFormatList.push_back(rttformat);
167 Ogre::CompositionTechnique::TextureDefinition* lum4Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum4");
168 lum4Texture->width = 128;
169 lum4Texture->height = 128;
170 lum4Texture->formatList = pixelFormatList;
172 Ogre::CompositionTechnique::TextureDefinition* lum3Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum3");
173 lum3Texture->width = 64;
174 lum3Texture->height = 64;
175 lum3Texture->formatList = pixelFormatList;
177 Ogre::CompositionTechnique::TextureDefinition* lum2Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum2");
178 lum2Texture->width = 16;
179 lum2Texture->height = 16;
180 lum2Texture->formatList = pixelFormatList;
182 Ogre::CompositionTechnique::TextureDefinition* lum1Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum1");
183 lum1Texture->width = 4;
184 lum1Texture->height = 4;
185 lum1Texture->formatList = pixelFormatList;
187 Ogre::CompositionTechnique::TextureDefinition* lum0Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum0");
188 lum0Texture->width = 1;
189 lum0Texture->height = 1;
190 lum0Texture->formatList = pixelFormatList;
193 Ogre::CompositionTargetPass* compositionTargetLum4 = hdrCompositionTechnique->createTargetPass();
194 compositionTargetLum4->setShadowsEnabled(
false);
195 compositionTargetLum4->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum4");
196 compositionTargetLum4->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
198 Ogre::CompositionPass* compositionTargetLum4QuadPass = compositionTargetLum4->createPass();
199 compositionTargetLum4QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
200 compositionTargetLum4QuadPass->setMaterialName(
"Ogre/Compositor/HDR/Downsample2x2Luminence");
201 compositionTargetLum4QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/Previous");
202 compositionTargetLum4QuadPass->setIdentifier(994);
205 Ogre::CompositionTargetPass* compositionTargetLum3 = hdrCompositionTechnique->createTargetPass();
206 compositionTargetLum3->setShadowsEnabled(
false);
207 compositionTargetLum3->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum3");
208 compositionTargetLum3->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
210 Ogre::CompositionPass* compositionTargetLum3QuadPass = compositionTargetLum3->createPass();
211 compositionTargetLum3QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
212 compositionTargetLum3QuadPass->setMaterialName(
"Ogre/Compositor/HDR/Downsample3x3");
213 compositionTargetLum3QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum4");
214 compositionTargetLum3QuadPass->setIdentifier(993);
217 Ogre::CompositionTargetPass* compositionTargetLum2 = hdrCompositionTechnique->createTargetPass();
218 compositionTargetLum2->setShadowsEnabled(
false);
219 compositionTargetLum2->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum2");
220 compositionTargetLum2->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
222 Ogre::CompositionPass* compositionTargetLum2QuadPass = compositionTargetLum2->createPass();
223 compositionTargetLum2QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
224 compositionTargetLum2QuadPass->setMaterialName(
"Ogre/Compositor/HDR/Downsample3x3");
225 compositionTargetLum2QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum3");
226 compositionTargetLum2QuadPass->setIdentifier(992);
229 Ogre::CompositionTargetPass* compositionTargetLum1 = hdrCompositionTechnique->createTargetPass();
230 compositionTargetLum1->setShadowsEnabled(
false);
231 compositionTargetLum1->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum1");
232 compositionTargetLum1->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
234 Ogre::CompositionPass* compositionTargetLum1QuadPass = compositionTargetLum1->createPass();
235 compositionTargetLum1QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
236 compositionTargetLum1QuadPass->setMaterialName(
"Ogre/Compositor/HDR/Downsample3x3");
237 compositionTargetLum1QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum2");
238 compositionTargetLum1QuadPass->setIdentifier(991);
241 Ogre::CompositionTargetPass* compositionTargetLum0 = hdrCompositionTechnique->createTargetPass();
242 compositionTargetLum0->setShadowsEnabled(
false);
243 compositionTargetLum0->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum0");
244 compositionTargetLum0->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
246 Ogre::CompositionPass* compositionTargetLum0QuadPass = compositionTargetLum0->createPass();
247 compositionTargetLum0QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
248 compositionTargetLum0QuadPass->setMaterialName(
"Ogre/Compositor/HDR/FinalLuminance");
249 compositionTargetLum0QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/lum1");
250 compositionTargetLum1QuadPass->setIdentifier(990);
253 Ogre::CompositionTechnique::TextureDefinition* adaptLastlumTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/adaptLastLum");
254 adaptLastlumTexture->width = 1;
255 adaptLastlumTexture->height = 1;
256 adaptLastlumTexture->formatList = pixelFormatList;
258 Ogre::CompositionTechnique::TextureDefinition* adaptedlumTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/adaptedLum");
259 adaptedlumTexture->width = 1;
260 adaptedlumTexture->height = 1;
261 adaptedlumTexture->formatList = pixelFormatList;
263 Ogre::CompositionTargetPass* compositionTargetAdaptLastLum = hdrCompositionTechnique->createTargetPass();
264 compositionTargetAdaptLastLum->setShadowsEnabled(
false);
265 compositionTargetAdaptLastLum->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/adaptLastLum");
266 compositionTargetAdaptLastLum->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
268 Ogre::CompositionPass* compositionTargetAdaptLastLumQuadPass = compositionTargetAdaptLastLum->createPass();
269 compositionTargetAdaptLastLumQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
270 compositionTargetAdaptLastLumQuadPass->setMaterialName(
"Ogre/Compositor/HDR/Copy");
271 compositionTargetAdaptLastLumQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/adaptedLum");
273 Ogre::CompositionTargetPass* compositionTargetAdaptedLum = hdrCompositionTechnique->createTargetPass();
274 compositionTargetAdaptedLum->setShadowsEnabled(
false);
275 compositionTargetAdaptedLum->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/adaptedLum");
276 compositionTargetAdaptedLum->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
278 Ogre::CompositionPass* compositionTargetAdaptedLumQuadPass = compositionTargetAdaptedLum->createPass();
279 compositionTargetAdaptedLumQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
280 compositionTargetAdaptedLumQuadPass->setMaterialName(
"Ogre/Compositor/HDR/AdaptLuminance");
281 compositionTargetAdaptedLumQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/lum0");
282 compositionTargetAdaptedLumQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/adaptLastLum");
285 Ogre::CompositionTechnique::TextureDefinition* keyTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/key");
286 keyTexture->width = 1;
287 keyTexture->height = 1;
288 keyTexture->formatList = pixelFormatList;
290 Ogre::CompositionTargetPass* compositionTargetKey = hdrCompositionTechnique->createTargetPass();
291 compositionTargetKey->setShadowsEnabled(
false);
292 compositionTargetKey->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/key");
293 compositionTargetKey->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
295 Ogre::CompositionPass* compositionTargetKeyQuadPass = compositionTargetKey->createPass();
296 compositionTargetKeyQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
297 compositionTargetKeyQuadPass->setMaterialName(
"Ogre/Compositor/HDR/AutoKey");
298 compositionTargetKeyQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/lum0");
300 pixelFormatList.clear();
301 pixelFormatList.push_back(rttformat);
304 Ogre::CompositionTechnique::TextureDefinition* brightPassTexture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/brightpass");
305 brightPassTexture->width = 128;
306 brightPassTexture->height = 128;
307 brightPassTexture->formatList = pixelFormatList;
309 Ogre::CompositionTargetPass* compositionTargetBrightPass = hdrCompositionTechnique->createTargetPass();
310 compositionTargetBrightPass->setShadowsEnabled(
false);
311 compositionTargetBrightPass->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/brightpass");
312 compositionTargetBrightPass->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
314 Ogre::CompositionPass* compositionTargetBrightQuadPass = compositionTargetBrightPass->createPass();
315 compositionTargetBrightQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
316 compositionTargetBrightQuadPass->setMaterialName(
"Ogre/Compositor/HDR/Brightpass");
317 compositionTargetBrightQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/Previous");
318 compositionTargetBrightQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/adaptedLum");
319 compositionTargetBrightQuadPass->setInput(2, std::string(SO3_INTERNAL_RESOURCE_GROUP) +
"/Hdr/Input/Texture/key");
320 compositionTargetBrightQuadPass->setIdentifier(800);
323 Ogre::CompositionTechnique::TextureDefinition* bloom0Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/bloom0");
324 bloom0Texture->width = 128;
325 bloom0Texture->height = 128;
326 bloom0Texture->formatList = pixelFormatList;
328 Ogre::CompositionTechnique::TextureDefinition* bloom1Texture = hdrCompositionTechnique->createTextureDefinition(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/bloom1");
329 bloom1Texture->width = 128;
330 bloom1Texture->height = 128;
331 bloom1Texture->formatList = pixelFormatList;
334 Ogre::CompositionTargetPass* compositionTargetBloom1 = hdrCompositionTechnique->createTargetPass();
335 compositionTargetBloom1->setShadowsEnabled(
false);
336 compositionTargetBloom1->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/bloom1");
337 compositionTargetBloom1->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
339 Ogre::CompositionPass* compositionTargetBloom1QuadPass = compositionTargetBloom1->createPass();
340 compositionTargetBloom1QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
341 compositionTargetBloom1QuadPass->setMaterialName(
"Ogre/Compositor/HDR/GaussianBloom");
342 compositionTargetBloom1QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/brightpass");
343 compositionTargetBloom1QuadPass->setIdentifier(701);
346 Ogre::CompositionTargetPass* compositionTargetBloom0 = hdrCompositionTechnique->createTargetPass();
347 compositionTargetBloom0->setShadowsEnabled(
false);
348 compositionTargetBloom0->setOutputName(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/bloom0");
349 compositionTargetBloom0->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
351 Ogre::CompositionPass* compositionTargetBloom0QuadPass = compositionTargetBloom0->createPass();
352 compositionTargetBloom0QuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
353 compositionTargetBloom0QuadPass->setMaterialName(
"Ogre/Compositor/HDR/GaussianBloom");
354 compositionTargetBloom0QuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/bloom1");
355 compositionTargetBloom0QuadPass->setIdentifier(700);
358 Ogre::CompositionTargetPass* compositionOutTarget = hdrCompositionTechnique->getOutputTargetPass();
359 compositionOutTarget->setShadowsEnabled(
false);
360 compositionOutTarget->setInputMode(Ogre::CompositionTargetPass::IM_NONE);
362 Ogre::CompositionPass* compositionOutTargetQuadPass = compositionOutTarget->createPass();
363 compositionOutTargetQuadPass->setType(Ogre::CompositionPass::PT_RENDERQUAD);
365 compositionOutTargetQuadPass->setIdentifier(1000);
366 compositionOutTargetQuadPass->setMaterialName(
"Ogre/Compositor/HDR/ToneMapping");
367 compositionOutTargetQuadPass->setInput(0, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/Previous");
368 compositionOutTargetQuadPass->setInput(1, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/bloom0");
369 compositionOutTargetQuadPass->setInput(2, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/adaptedLum");
370 compositionOutTargetQuadPass->setInput(3, std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/key");
378 Ogre::CompositorManager& compMgr = Ogre::CompositorManager::getSingleton();
379 compMgr.registerCompositorLogic(std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Logic",
new SHdrLogic);
402 Ogre::CompositionTechnique::TextureDefinitions defList = instance->getTechnique()->getTextureDefinitions();
403 Ogre::CompositionTechnique::TextureDefinitions::const_iterator defIter;
404 for (defIter = defList.begin(); defIter != defList.end(); ++defIter)
406 Ogre::CompositionTechnique::TextureDefinition* def = *defIter;
408 if(def->name == (std::string(SO3_INTERNAL_RESOURCE_GROUP)+
"/Hdr/Input/Texture/bloom0"))
412 float deviation = 5.0f;
424 for(
int i = 1; i < 8; ++i)
434 for(
int i = 8; i < 15; ++i)
464 Ogre::GpuProgramParametersSharedPtr fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
474 Ogre::GpuProgramParametersSharedPtr fparams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
487 if (pass_id == 1000 && mat->getSupportedTechniques().size() > 0)
489 Ogre::GpuProgramParametersSharedPtr toneShaderParams = mat->getTechnique(0)->getPass(0)->getFragmentProgramParameters();
490 if (toneShaderParams)
492 if (toneShaderParams->_findNamedConstantDefinition(
"blurAmount"))
495 if (toneShaderParams->_findNamedConstantDefinition(
"effectAmount"))
498 if (toneShaderParams->_findNamedConstantDefinition(
"gamma"))
void SetEnable(bool enable)
void SetEffectAmount(float amount)
void SetBlurAmount(float amount)
static float mEffectAmount
void SetGamma(float gamma)
void notifyCompositor(Ogre::CompositorInstance *instance)
virtual void notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
float mBloomTexOffsetsVert[15][4]
virtual void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
float mBloomTexOffsetsHorz[15][4]
float mBloomTexWeights[15][4]
virtual Ogre::CompositorInstance::Listener * createListener(Ogre::CompositorInstance *instance)
bool GetRttPixelFormat(Ogre::PixelFormat &format, bool alpha=false, bool floattex=false)
Ogre::RenderSystem * GetOgreRenderSystem()
static SRoot & getSingleton()
Ogre::Viewport * GetOgreViewPortPointer()
bool UpdateStereoCompositorState(const Ogre::String compname, bool state)