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 // Note - this is currently called from UpdateThread, PrepareRenderInstructions,
59 // as an optimisation.
60 // @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
61 bool NewRenderer::RequiresDepthTest() const
66 bool NewRenderer::CheckResources()
68 // Query material to check it has texture pointers & image has size
69 // Query geometry to check it has vertex buffers
71 // General point though - why would we have a render item in RenderThread with no ready
72 // resources in UpdateThread?
76 void NewRenderer::ResolveGeometryTypes( BufferIndex bufferIndex, GeometryType& outType, ShaderSubTypes& outSubType )
78 // @todo MESH_REWORK Remove after merge
83 bool NewRenderer::IsOutsideClipSpace( Context& context, const Matrix& modelMatrix, const Matrix& modelViewProjectionMatrix )
85 // @todo MESH_REWORK Add clipping
89 void NewRenderer::DoSetUniforms( Context& context, BufferIndex bufferIndex, Shader* shader, Program* program, unsigned int programIndex, ShaderSubTypes subType )
91 // Do nothing, we're going to set up the uniforms with our own code instead
95 void NewRenderer::DoRender( Context& context, TextureCache& textureCache, BufferIndex bufferIndex, Program& program, const Matrix& modelViewMatrix, const Matrix& viewMatrix )
97 BindTextures( textureCache, bufferIndex, program, mRenderDataProvider->GetSamplers() );
99 SetUniforms( bufferIndex, program );
101 mRenderGeometry.UploadAndDraw( context, program, bufferIndex, mRenderDataProvider.Get() );
104 void NewRenderer::GlContextDestroyed()
106 mRenderGeometry.GlContextDestroyed();
109 void NewRenderer::GlCleanup()
113 void NewRenderer::SetUniforms( BufferIndex bufferIndex, Program& program )
115 // Check if the map has changed
116 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
118 const UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
120 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) )
122 const CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
124 unsigned int numberOfMaps = uniformMap.Count();
125 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
126 mUniformIndexMap.Resize( numberOfMaps );
128 // Remap uniform indexes to property value addresses
129 for( unsigned int mapIndex = 0 ; mapIndex < numberOfMaps ; ++mapIndex )
131 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
132 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
136 // Set uniforms in local map
137 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
138 end = mUniformIndexMap.End() ;
142 SetUniformFromProperty( bufferIndex, program, *iter );
145 // @todo MESH_REWORK On merge, copy code from renderer to setup standard matrices and color
147 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
150 Vector3 size = mDataProvider.GetRenderSize( bufferIndex );
151 program.SetUniform3f( sizeLoc, size.x, size.y, size.z );
155 void NewRenderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
157 GLint location = program.GetUniformLocation(map.uniformIndex);
158 if( Program::UNIFORM_UNKNOWN != location )
160 // switch based on property type to use correct GL uniform setter
161 switch ( map.propertyValue->GetType() )
163 case Property::INTEGER:
165 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
168 case Property::FLOAT:
170 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
173 case Property::VECTOR2:
175 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
176 program.SetUniform2f( location, value.x, value.y );
180 case Property::VECTOR3:
182 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
183 program.SetUniform3f( location, value.x, value.y, value.z );
187 case Property::VECTOR4:
189 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
190 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
194 case Property::ROTATION:
196 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
197 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
201 case Property::MATRIX:
203 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
204 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
208 case Property::MATRIX3:
210 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
211 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
217 // Other property types are ignored
224 void NewRenderer::BindTextures(
225 TextureCache& textureCache,
226 BufferIndex bufferIndex,
228 const RenderDataProvider::Samplers& samplers )
230 // @todo MESH_REWORK Write a cache of texture units to commonly used sampler textures
231 unsigned int textureUnit = 0;
233 for( RenderDataProvider::Samplers::Iterator iter = samplers.Begin();
234 iter != samplers.End();
237 const SamplerDataProvider* sampler = *iter;
238 ResourceId textureId = sampler->GetTextureId(bufferIndex);
239 Texture* texture = textureCache.GetTexture( textureId );
240 if( texture != NULL )
242 unsigned int textureUnitUniformIndex = GetTextureUnitUniformIndex( program, *sampler );
243 TextureUnit theTextureUnit = static_cast<TextureUnit>(textureUnit);
244 BindTexture( textureCache, program, textureId, texture, theTextureUnit, textureUnitUniformIndex );
245 ApplySampler( bufferIndex, texture, theTextureUnit, *sampler );
252 void NewRenderer::BindTexture(
253 TextureCache& textureCache,
257 TextureUnit textureUnit,
258 unsigned int textureUnitUniformIndex )
260 if( texture != NULL )
262 textureCache.BindTexture( texture, id, GL_TEXTURE_2D, textureUnit );
264 // Set sampler uniform location for the texture
265 GLint textureUnitLoc = program.GetUniformLocation( textureUnitUniformIndex );
266 if( Program::UNIFORM_UNKNOWN != textureUnitLoc )
268 program.SetUniform1i( textureUnitLoc, textureUnit );
273 void NewRenderer::ApplySampler(
274 BufferIndex bufferIndex,
276 TextureUnit textureUnit,
277 const SamplerDataProvider& sampler )
279 unsigned int samplerBitfield = ImageSampler::PackBitfield(
280 static_cast< FilterMode::Type >(sampler.GetMinifyFilterMode(bufferIndex)),
281 static_cast< FilterMode::Type >(sampler.GetMagnifyFilterMode(bufferIndex)) );
283 texture->ApplySampler( textureUnit, samplerBitfield );
285 // @todo MESH_REWORK add support for wrap modes
288 unsigned int NewRenderer::GetTextureUnitUniformIndex(
290 const SamplerDataProvider& sampler )
292 // Find sampler in mSamplerNameCache
293 // If it doesn't exist,
294 // get the index by calling program.RegisterUniform and store it
295 // If it exists, it's index should be set.
296 // @todo Cache should be reset on scene change
298 unsigned int uniformIndex = 0;
301 for( unsigned int i=0; i< mTextureUnitUniforms.Count(); ++i )
303 if( mTextureUnitUniforms[i].sampler == &sampler )
305 uniformIndex = mTextureUnitUniforms[i].index;
312 TextureUnitUniformIndex textureUnitUniformIndex;
313 textureUnitUniformIndex.sampler = &sampler;
314 textureUnitUniformIndex.index = program.RegisterUniform( sampler.GetTextureUnitUniformName() );
315 mTextureUnitUniforms.PushBack( textureUnitUniformIndex );
316 uniformIndex = textureUnitUniformIndex.index;