2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FUiEffects_RendererGraphicsEngineRender.cpp
20 * @brief Implementation of class for rendering scene tree
24 #include <renderer/engine-model/FUiEffects_RendererEngineModelDrawable.h>
25 #include <renderer/engine-model/FUiEffects_RendererEngineModelNode.h>
26 #include <renderer/engine-model/FUiEffects_RendererEngineModelGroup.h>
27 #include <renderer/engine-model/FUiEffects_RendererEngineModelCamera.h>
28 #include <renderer/engine-model/FUiEffects_RendererEngineModelBuffer.h>
29 #include <renderer/engine-model/FUiEffects_RendererEngineModelTexture2DProperty.h>
30 #include <renderer/engine-model/FUiEffects_RendererEngineModelStringShaderProperty.h>
31 #include <renderer/engine-model/FUiEffects_RendererEngineModelShaderHolderProgramProperty.h>
33 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineOpenGLImplementation.h>
34 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRender.h>
35 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRenderDrawableCache.h>
36 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRenderBufferCache.h>
37 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRenderTextureCache.h>
38 #include <renderer/graphics-engine/FUiEffects_RendererGraphicsEngineRenderShaderCache.h>
40 namespace Tizen { namespace Ui { namespace Effects { namespace _Renderer { namespace GraphicsEngine
44 : __worldViewProjection()
45 , __externalRenderTargets()
49 , __findCameraPass(false)
52 , __alphaBlendSource(GL_SRC_ALPHA)
53 , __alphaBlendDestination(GL_ONE_MINUS_SRC_ALPHA)
54 , __depthTestState(false)
55 , __currentFrameBuffer(0)
61 const CacheListSizeType len = __caches.size();
62 for (CacheListSizeType i = 0; len > i; ++i)
64 __caches[i]->InternalDestroyCache();
69 Render::SetDefaultDepthTestState(bool aIsEnable)
71 __depthTestState = aIsEnable;
76 Render::Draw(Tizen::Ui::Effects::_Renderer::EngineModel::Node& root, EngineModel::Camera& camera)
78 InternalKillDeadCaches();
81 if (null == __pCamera)
83 ThrowJmp("Render::draw: Can`t render, no active camera");
88 InternalCollectInfo(root);
97 Render::Draw(Tizen::Ui::Effects::_Renderer::EngineModel::Node& root)
99 InternalFindCameras(root);
101 Draw(root, *__pCamera);
106 Render::OnGroup(Tizen::Ui::Effects::_Renderer::EngineModel::Group& prop)
108 const size_t len = prop.ChildrenCount();
109 for (size_t i = 0; len > i; ++i)
111 prop.GetChild(i)->Visit(*this);
117 Render::OnDrawable(Tizen::Ui::Effects::_Renderer::EngineModel::Drawable& prop)
119 EngineModel::Cache* pCache = System::GetImpl(prop.GetCache());
124 pCache = System::GetImpl(prop.GetCache());
125 if (!pCache) { return; }
128 RenderDrawableCache* renderCache = dynamic_cast<RenderDrawableCache*>(pCache);
129 if (renderCache == null)
134 __drawables.push_back(renderCache);
137 const RenderDrawableCache::ExternalTexturesListSizeType numExternalTextures = renderCache->NumExternalTextures();
139 for (RenderDrawableCache::ExternalTexturesListSizeType i = 0; numExternalTextures > i; ++i)
142 EngineModel::TextureAttachment* attachment = renderCache->GetExternalTexture(i);
143 if (attachment == null)
148 const EngineModel::TextureAttachment::RenderTargetListSizeType numRenderTargets = attachment->GetNumRenderTargets();
150 for (Tizen::Ui::Effects::_Renderer::EngineModel::TextureAttachment::RenderTargetListSizeType rt = 0; numRenderTargets > rt; ++rt)
152 EngineModel::RenderTargetExternalRender* pRenderTarget = static_cast<Tizen::Ui::Effects::_Renderer::EngineModel::RenderTargetExternalRender*>(attachment->GetRenderTarget(rt));
153 if (pRenderTarget == null)
158 RenderTargetExternalRenderCache* cache = static_cast<RenderTargetExternalRenderCache*>(System::GetImpl(pRenderTarget->GetCache()));
161 BuildCache(*pRenderTarget);
163 cache = static_cast<RenderTargetExternalRenderCache*>(System::GetImpl(pRenderTarget->GetCache()));
165 __externalRenderTargets[cache] = cache;
173 Render::ManageCache(RenderCacheImplPtr pCache)
175 __caches.push_back(pCache);
176 pCache->__cacheIndex = __caches.size() - 1;
181 Render::KillCache(RenderCacheImpl* pCache)
183 __deadCaches.push_back(pCache);
188 Render::InternalKillDeadCaches(void)
190 const DeadCacheListSizeType len = __deadCaches.size();
191 for (DeadCacheListSizeType i = 0; len > i; ++i)
193 __deadCaches[i]->InternalDestroyCache();
194 const size_t cacheIndex = __deadCaches[i]->__cacheIndex;
195 const CacheListSizeType cachesSize = __caches.size();
196 __caches[cacheIndex] = __caches[cachesSize - 1];
197 __caches[cacheIndex]->__cacheIndex = cacheIndex;
198 __caches.resize(cachesSize - 1);
200 __deadCaches.clear();
205 Render::ContextLost(void)
207 const CacheListSizeType len = __caches.size();
208 for (CacheListSizeType i = 0; len > i; ++i)
210 __caches[i]->InternalDropCache();
217 struct CameraFinder: public EngineModel::Visitor
219 EngineModel::Camera* pCamera;
220 CameraFinder(): pCamera(null) {}
223 OnGroup(Tizen::Ui::Effects::_Renderer::EngineModel::Group& prop)
225 const size_t len = prop.ChildrenCount();
226 for (size_t i = 0; len > i; ++i)
228 prop.GetChild(i)->Visit(*this);
234 OnCamera(Tizen::Ui::Effects::_Renderer::EngineModel::Camera& prop)
243 Render::InternalFindCameras(Tizen::Ui::Effects::_Renderer::EngineModel::Node& prop)
246 CameraFinder cameraFinder;
247 prop.Visit(cameraFinder);
248 __pCamera = cameraFinder.pCamera;
253 Render::InternalPreDraw(void)
256 __blendDrawables.clear();
257 __externalRenderTargets.clear();
262 Render::InternalCollectInfo(Tizen::Ui::Effects::_Renderer::EngineModel::Node& prop)
269 Render::InternalDraw(void)
271 Math::Vector2f minPos = __pCamera->GetViewportMin();
272 Math::Vector2f maxPos = __pCamera->GetViewportMax();
273 glViewport(static_cast<GLint>(minPos.X()), static_cast<GLint>(minPos.Y()), static_cast<GLint>(maxPos.X()), static_cast<GLint>(maxPos.Y()));
275 Math::Matrix4f cameraWorld(__pCamera->GetWorld());
276 Math::Matrix4f cameraView(cameraWorld.GetInversed()); //__pCamera world matrix is inverted view matrix
277 Math::Matrix4f projection(__pCamera->GetProj());
278 Math::Matrix4f viewProjection = projection * cameraView;
279 if (__depthTestState)
281 glEnable(GL_DEPTH_TEST);
285 glDisable(GL_DEPTH_TEST);
289 const size_t len = __drawables.size();
290 for (size_t i = 0; len > i; ++i)
292 __worldViewProjection = viewProjection * __drawables[i]->GetWorld();
293 __worldViewProjection.Transpose();
294 if (static_cast<RenderDrawableCache*>(__drawables[i])->HasAlphaBlend(this))
303 static_cast<RenderDrawableCache*>(__drawables[i])->Draw(this);
310 Render::InternalPostDraw(void)
316 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::Drawable& prop)
318 RenderCachePtr cachePtr(new (std::nothrow) RenderDrawableCache(this, &prop));
319 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "cache = null");
321 prop.SetCache(cachePtr);
323 cachePtr->Validate(this);
325 ManageCache(cachePtr);
330 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::VertexBuffer& prop)
332 RenderCachePtr cachePtr(new (std::nothrow) SolidVertexBufferCache(this, &prop));
333 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "cache = null");
335 prop.SetCache(cachePtr);
336 cachePtr->Validate(this);
337 ManageCache(cachePtr);
342 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::IndexBuffer& prop)
344 RenderCachePtr cachePtr(new (std::nothrow) IndexBufferCache(this, &prop));
345 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "cache = null");
347 prop.SetCache(cachePtr);
348 cachePtr->Validate(this);
349 ManageCache(cachePtr);
354 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::CompositeVertexBuffer& prop)
356 RenderCachePtr cachePtr(new (std::nothrow) CompositeVertexBufferCache(this, &prop));
357 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
359 prop.SetCache(cachePtr);
360 cachePtr->Validate(this);
361 ManageCache(cachePtr);
366 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::PartVertexBuffer& prop)
368 RenderCachePtr cachePtr(new (std::nothrow) PartVertexBufferCache(this, &prop));
369 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
371 prop.SetCache(cachePtr);
372 cachePtr->Validate(this);
373 ManageCache(cachePtr);
378 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::Texture2dProperty& prop, GLuint stage)
380 RenderCachePtr cachePtr(new (std::nothrow) RenderTexture2dCache(this, &prop));
381 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
383 prop.SetCache(cachePtr);
384 cachePtr.staticCast<RenderTexture2dCache>()->Validate(this, stage);
385 ManageCache(cachePtr);
390 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::ShaderProperty& prop)
392 RenderCachePtr cachePtr(new (std::nothrow) RenderShaderCache(this, &prop));
393 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
395 prop.SetCache(cachePtr);
396 cachePtr->Validate(this);
397 ManageCache(cachePtr);
402 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::ProgramProperty& prop)
404 RenderCachePtr cachePtr(new (std::nothrow) RenderProgramCache(this, &prop));
405 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
407 prop.SetCache(cachePtr);
408 cachePtr->Validate(this);
409 ManageCache(cachePtr);
414 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::RenderTargetExternalRender& prop)
416 RenderCachePtr cachePtr(new (std::nothrow) RenderTargetExternalRenderCache(this, &prop));
417 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
419 prop.SetCache(cachePtr);
420 cachePtr->Validate(this);
421 ManageCache(cachePtr);
426 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::Texture2dAttachmentProperty& prop, GLuint stage)
428 RenderCachePtr cachePtr(new (std::nothrow) Texture2dAttachmentCache(this, &prop));
429 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
431 prop.SetCache(cachePtr);
432 cachePtr.staticCast<Texture2dAttachmentCache>()->Validate(this, stage);
433 ManageCache(cachePtr);
438 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::StubAttachment& o)
440 RenderCachePtr cachePtr(new (std::nothrow) StubAttachmentCache(this, &o));
441 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
443 o.SetCache(cachePtr);
444 cachePtr->Validate(this);
445 ManageCache(cachePtr);
450 Render::BuildCache(Tizen::Ui::Effects::_Renderer::EngineModel::AlphaBlendProperty& prop)
452 RenderCachePtr cachePtr(new (std::nothrow) RenderAlphaBlendCache(this, &prop));
453 _TryJmp(GetImpl(cachePtr) != null, E_OUT_OF_MEMORY, "pCache = null");
455 prop.SetCache(cachePtr);
456 cachePtr->Validate(this);
457 ManageCache(cachePtr);
462 Render::GetDefaultRenderCommand(const RenderProgramCache::UniformListValueType* pInfo)
464 if (pInfo->second == "WorldViewProjection")
466 RenderCommandPtr result = RenderCommandPtr(new (std::nothrow) SetWorldViewProjection(pInfo->first.location));
467 _TryJmp(System::GetImpl(result) != null, E_OUT_OF_MEMORY, "GetDefaultRenderCommand = null");
472 return RenderCommandPtr();
475 SetWorldViewProjection::SetWorldViewProjection(GLuint location):
481 SetWorldViewProjection::Apply(Render* pRender)
483 glUniformMatrix4fv(__location, 1, GL_FALSE, pRender->GetWorldViewProjection().data[0].data);
487 const Math::Matrix4f&
488 Render::GetWorldViewProjection(void)
490 return __worldViewProjection;
493 }}}}} //Tizen::Ui::Effects::_Renderer::GraphicsEngine