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/render/data-providers/geometry-data-provider.h>
21 #include <dali/internal/render/data-providers/material-data-provider.h>
22 #include <dali/internal/render/data-providers/node-data-provider.h>
23 #include <dali/internal/render/data-providers/sampler-data-provider.h>
24 #include <dali/internal/render/gl-resources/texture.h>
25 #include <dali/internal/render/gl-resources/texture-cache.h>
26 #include <dali/internal/render/shaders/program.h>
36 NewRenderer* NewRenderer::New( NodeDataProvider& nodeDataProvider,
37 const GeometryDataProvider* geometryDataProvider,
38 const MaterialDataProvider* materialDataProvider)
40 return new NewRenderer(nodeDataProvider, geometryDataProvider, materialDataProvider);
44 NewRenderer::NewRenderer( NodeDataProvider& nodeDataProvider,
45 const GeometryDataProvider* geometryDataProvider,
46 const MaterialDataProvider* materialDataProvider)
47 : Renderer( nodeDataProvider ),
48 //mShaderDataProvider( shaderDataProvider ), //@todo Add in after merge with parent class
49 mMaterialDataProvider( materialDataProvider ),
50 mGeometryDataProvider( geometryDataProvider )
54 NewRenderer::~NewRenderer()
58 // @todo MESH_REWORK Should we consider changing the providers, or should we instead
59 // create a new renderer when these change?
60 void NewRenderer::SetGeometryDataProvider( const GeometryDataProvider* geometryDataProvider )
62 mGeometryDataProvider = geometryDataProvider;
65 void NewRenderer::SetMaterialDataProvider( const MaterialDataProvider* materialDataProvider )
67 mMaterialDataProvider = materialDataProvider;
71 // Note - this is currently called from UpdateThread, PrepareRenderInstructions,
72 // as an optimisation.
73 // @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
74 bool NewRenderer::RequiresDepthTest() const
79 bool NewRenderer::CheckResources()
81 // Query material to check it has texture pointers & image has size
82 // Query geometry to check it has vertex buffers
84 // General point though - why would we have a render item in RenderThread with no ready
85 // resources in UpdateThread?
89 void NewRenderer::ResolveGeometryTypes( BufferIndex bufferIndex, GeometryType& outType, ShaderSubTypes& outSubType )
91 // @todo MESH_REWORK Remove after merge
96 bool NewRenderer::IsOutsideClipSpace( const Matrix& modelMatrix, const Matrix& modelViewProjectionMatrix )
98 // @todo MESH_REWORK Add clipping
102 void NewRenderer::DoRender( BufferIndex bufferIndex, Program& program, const Matrix& modelViewMatrix, const Matrix& viewMatrix )
104 BindTextures( bufferIndex, program, mMaterialDataProvider->GetSamplers() );
106 SetUniforms( bufferIndex, program );
108 mRenderGeometry.UploadAndDraw( mContext, program, bufferIndex, *mGeometryDataProvider );
111 void NewRenderer::GlContextDestroyed()
113 mRenderGeometry.GlContextDestroyed();
116 void NewRenderer::GlCleanup()
120 void NewRenderer::SetUniforms( BufferIndex bufferIndex, Program& program )
122 // @todo MESH_REWORK Implement uniform map
124 // @todo Base class is currently setting MVP, Color etc.
125 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
128 Vector3 size = mDataProvider.GetRenderSize( bufferIndex );
129 program.SetUniform3f( sizeLoc, size.x, size.y, size.z );
133 void NewRenderer::BindTextures(
134 BufferIndex bufferIndex,
136 const MaterialDataProvider::Samplers& samplers )
138 // @todo MESH_REWORK Write a cache of texture units to commonly used sampler textures
139 unsigned int textureUnit = 0;
141 for( MaterialDataProvider::Samplers::Iterator iter = samplers.Begin();
142 iter != samplers.End();
145 const SamplerDataProvider* sampler = *iter;
146 ResourceId textureId = sampler->GetTextureId(bufferIndex);
147 Texture* texture = mTextureCache->GetTexture( textureId );
148 if( texture != NULL )
150 unsigned int textureUnitUniformIndex = GetTextureUnitUniformIndex( program, *sampler );
151 TextureUnit theTextureUnit = static_cast<TextureUnit>(textureUnit);
152 BindTexture( program, textureId, texture, theTextureUnit, textureUnitUniformIndex );
153 ApplySampler( bufferIndex, texture, theTextureUnit, *sampler );
160 void NewRenderer::BindTexture(
164 TextureUnit textureUnit,
165 unsigned int textureUnitUniformIndex )
167 DALI_ASSERT_DEBUG( NULL != mTextureCache );
169 if( texture != NULL )
171 mTextureCache->BindTexture( texture, id, GL_TEXTURE_2D, textureUnit );
173 // Set sampler uniform location for the texture
174 GLint textureUnitLoc = program.GetUniformLocation( textureUnitUniformIndex );
175 if( Program::UNIFORM_UNKNOWN != textureUnitLoc )
177 program.SetUniform1i( textureUnitLoc, textureUnit );
182 void NewRenderer::ApplySampler(
183 BufferIndex bufferIndex,
185 TextureUnit textureUnit,
186 const SamplerDataProvider& sampler )
188 unsigned int samplerBitfield = ImageSampler::PackBitfield(
189 static_cast< FilterMode::Type >(sampler.GetMinifyFilterMode(bufferIndex)),
190 static_cast< FilterMode::Type >(sampler.GetMagnifyFilterMode(bufferIndex)) );
192 texture->ApplySampler( textureUnit, samplerBitfield );
194 // @todo MESH_REWORK add support for wrap modes
197 unsigned int NewRenderer::GetTextureUnitUniformIndex(
199 const SamplerDataProvider& sampler )
201 // Find sampler in mSamplerNameCache
202 // If it doesn't exist,
203 // get the index by calling program.RegisterUniform and store it
204 // If it exists, it's index should be set.
205 // @todo Cache should be reset on scene change
207 unsigned int uniformIndex = 0;
210 for( unsigned int i=0; i< mTextureUnitUniforms.Count(); ++i )
212 if( mTextureUnitUniforms[i].sampler == &sampler )
214 uniformIndex = mTextureUnitUniforms[i].index;
221 TextureUnitUniformIndex textureUnitUniformIndex;
222 textureUnitUniformIndex.sampler = &sampler;
223 textureUnitUniformIndex.index = program.RegisterUniform( sampler.GetUnitName() );
224 mTextureUnitUniforms.PushBack( textureUnitUniformIndex );
225 uniformIndex = textureUnitUniformIndex.index;