2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "render-renderer.h"
19 #include <dali/internal/common/image-sampler.h>
20 #include <dali/internal/event/common/property-input-impl.h>
21 #include <dali/internal/update/common/uniform-map.h>
22 #include <dali/internal/render/data-providers/render-data-provider.h>
23 #include <dali/internal/render/gl-resources/texture.h>
24 #include <dali/internal/render/gl-resources/texture-cache.h>
25 #include <dali/internal/render/shaders/program.h>
34 NewRenderer* NewRenderer::New( NodeDataProvider& nodeDataProvider,
35 RenderDataProvider* dataProvider )
37 return new NewRenderer(nodeDataProvider, dataProvider);
41 NewRenderer::NewRenderer( NodeDataProvider& nodeDataProvider,
42 RenderDataProvider* dataProvider )
43 : Renderer( nodeDataProvider ),
44 mRenderDataProvider( dataProvider )
48 NewRenderer::~NewRenderer()
52 void NewRenderer::SetRenderDataProvider( RenderDataProvider* dataProvider )
54 mRenderDataProvider = dataProvider;
55 mRenderGeometry.GeometryUpdated();
58 void NewRenderer::SetGeometryUpdated( )
60 mRenderGeometry.GeometryUpdated();
63 // Note - this is currently called from UpdateThread, PrepareRenderInstructions,
64 // as an optimisation.
65 // @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
66 bool NewRenderer::RequiresDepthTest() const
71 bool NewRenderer::CheckResources()
73 // Query material to check it has texture pointers & image has size
74 // Query geometry to check it has vertex buffers
76 // General point though - why would we have a render item in RenderThread with no ready
77 // resources in UpdateThread?
81 bool NewRenderer::IsOutsideClipSpace( Context& context, const Matrix& modelMatrix, const Matrix& modelViewProjectionMatrix )
83 // @todo MESH_REWORK Add clipping
87 void NewRenderer::DoSetUniforms( Context& context, BufferIndex bufferIndex, Shader* shader, Program* program, unsigned int programIndex )
89 // Do nothing, we're going to set up the uniforms with our own code instead
92 void NewRenderer::DoSetCullFaceMode( Context& context, BufferIndex bufferIndex )
96 void NewRenderer::DoSetBlending( Context& context, BufferIndex bufferIndex )
98 context.SetBlend(mUseBlend); // @todo MESH_REWORK Should use a RendererDataProvider
102 const MaterialDataProvider& material = mRenderDataProvider->GetMaterial();
104 context.SetCustomBlendColor( material.GetBlendColor( bufferIndex ) );
106 // Set blend source & destination factors
107 context.BlendFuncSeparate( material.GetBlendSrcFactorRgb( bufferIndex ),
108 material.GetBlendDestFactorRgb( bufferIndex ),
109 material.GetBlendSrcFactorAlpha( bufferIndex ),
110 material.GetBlendDestFactorAlpha( bufferIndex ) );
112 // Set blend equations
113 context.BlendEquationSeparate( material.GetBlendEquationRgb( bufferIndex ),
114 material.GetBlendEquationAlpha( bufferIndex ) );
118 void NewRenderer::DoRender( Context& context, TextureCache& textureCache, BufferIndex bufferIndex, Program& program, const Matrix& modelViewMatrix, const Matrix& viewMatrix )
120 BindTextures( textureCache, bufferIndex, program, mRenderDataProvider->GetSamplers() );
122 SetUniforms( bufferIndex, program );
124 mRenderGeometry.UploadAndDraw( context, program, bufferIndex, mRenderDataProvider.Get() );
127 void NewRenderer::GlContextDestroyed()
129 mRenderGeometry.GlContextDestroyed();
132 void NewRenderer::GlCleanup()
136 void NewRenderer::SetUniforms( BufferIndex bufferIndex, Program& program )
138 // Check if the map has changed
139 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
141 const UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
143 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) )
145 const CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
147 unsigned int numberOfMaps = uniformMap.Count();
148 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
149 mUniformIndexMap.Resize( numberOfMaps );
151 // Remap uniform indexes to property value addresses
152 for( unsigned int mapIndex = 0 ; mapIndex < numberOfMaps ; ++mapIndex )
154 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
155 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
159 // Set uniforms in local map
160 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
161 end = mUniformIndexMap.End() ;
165 SetUniformFromProperty( bufferIndex, program, *iter );
168 // @todo MESH_REWORK On merge, copy code from renderer to setup standard matrices and color
170 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
173 Vector3 size = mDataProvider.GetRenderSize( bufferIndex );
174 program.SetUniform3f( sizeLoc, size.x, size.y, size.z );
178 void NewRenderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
180 GLint location = program.GetUniformLocation(map.uniformIndex);
181 if( Program::UNIFORM_UNKNOWN != location )
183 // switch based on property type to use correct GL uniform setter
184 switch ( map.propertyValue->GetType() )
186 case Property::INTEGER:
188 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
191 case Property::FLOAT:
193 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
196 case Property::VECTOR2:
198 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
199 program.SetUniform2f( location, value.x, value.y );
203 case Property::VECTOR3:
205 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
206 program.SetUniform3f( location, value.x, value.y, value.z );
210 case Property::VECTOR4:
212 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
213 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
217 case Property::ROTATION:
219 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
220 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
224 case Property::MATRIX:
226 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
227 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
231 case Property::MATRIX3:
233 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
234 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
240 // Other property types are ignored
247 void NewRenderer::BindTextures(
248 TextureCache& textureCache,
249 BufferIndex bufferIndex,
251 const RenderDataProvider::Samplers& samplers )
253 // @todo MESH_REWORK Write a cache of texture units to commonly used sampler textures
254 unsigned int textureUnit = 0;
256 for( RenderDataProvider::Samplers::Iterator iter = samplers.Begin();
257 iter != samplers.End();
260 const SamplerDataProvider* sampler = *iter;
261 ResourceId textureId = sampler->GetTextureId(bufferIndex);
262 Texture* texture = textureCache.GetTexture( textureId );
263 if( texture != NULL )
265 unsigned int textureUnitUniformIndex = GetTextureUnitUniformIndex( program, *sampler );
266 TextureUnit theTextureUnit = static_cast<TextureUnit>(textureUnit);
267 BindTexture( textureCache, program, textureId, texture, theTextureUnit, textureUnitUniformIndex );
268 ApplySampler( bufferIndex, texture, theTextureUnit, *sampler );
275 void NewRenderer::BindTexture(
276 TextureCache& textureCache,
280 TextureUnit textureUnit,
281 unsigned int textureUnitUniformIndex )
283 if( texture != NULL )
285 textureCache.BindTexture( texture, id, GL_TEXTURE_2D, textureUnit );
287 // Set sampler uniform location for the texture
288 GLint textureUnitLoc = program.GetUniformLocation( textureUnitUniformIndex );
289 if( Program::UNIFORM_UNKNOWN != textureUnitLoc )
291 program.SetUniform1i( textureUnitLoc, textureUnit );
296 void NewRenderer::ApplySampler(
297 BufferIndex bufferIndex,
299 TextureUnit textureUnit,
300 const SamplerDataProvider& sampler )
302 unsigned int samplerBitfield = ImageSampler::PackBitfield(
303 static_cast< FilterMode::Type >(sampler.GetMinifyFilterMode(bufferIndex)),
304 static_cast< FilterMode::Type >(sampler.GetMagnifyFilterMode(bufferIndex)) );
306 texture->ApplySampler( textureUnit, samplerBitfield );
308 // @todo MESH_REWORK add support for wrap modes
311 unsigned int NewRenderer::GetTextureUnitUniformIndex(
313 const SamplerDataProvider& sampler )
315 // Find sampler in mSamplerNameCache
316 // If it doesn't exist,
317 // get the index by calling program.RegisterUniform and store it
318 // If it exists, it's index should be set.
319 // @todo Cache should be reset on scene change
321 unsigned int uniformIndex = 0;
324 for( unsigned int i=0; i< mTextureUnitUniforms.Count(); ++i )
326 if( mTextureUnitUniforms[i].sampler == &sampler )
328 uniformIndex = mTextureUnitUniforms[i].index;
335 TextureUnitUniformIndex textureUnitUniformIndex;
336 textureUnitUniformIndex.sampler = &sampler;
337 textureUnitUniformIndex.index = program.RegisterUniform( sampler.GetTextureUnitUniformName() );
338 mTextureUnitUniforms.PushBack( textureUnitUniformIndex );
339 uniformIndex = textureUnitUniformIndex.index;