2 * Copyright (c) 2014 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.
19 #include <dali/internal/render/shaders/program-controller.h>
22 #include <dali/integration-api/gl-defines.h>
23 #include <dali/internal/common/shader-saver.h>
24 #include <dali/internal/update/resources/resource-manager-declarations.h>
25 #include <dali/internal/render/shaders/program.h>
26 #include <dali/internal/render/common/post-process-resource-dispatcher.h>
27 #include <dali/internal/render/gl-resources/gl-call-debug.h>
35 ProgramController::ProgramController( Integration::GlAbstraction& glAbstraction )
37 mGlAbstraction( glAbstraction ),
38 mCurrentProgram( NULL ),
39 mProgramBinaryFormat( 0 ),
40 mNumberOfProgramBinaryFormats( 0 )
42 // we have 17 default programs so make room for those and a few custom ones as well
43 mProgramCache.Reserve( 32 );
46 ProgramController::~ProgramController()
50 void ProgramController::ResetProgramMatrices()
52 const ProgramIterator end = mProgramCache.End();
53 for ( ProgramIterator iter = mProgramCache.Begin(); iter != end; ++iter )
55 Program* program = (*iter)->GetProgram();
56 program->SetProjectionMatrix( NULL );
57 program->SetViewMatrix( NULL );
61 void ProgramController::GlContextCreated()
63 // reset any potential previous errors
64 LOG_GL( "GetError()\n" );
65 CHECK_GL( mGlAbstraction, mGlAbstraction.GetError() );
67 // find out if program binaries are supported and the format enum as well
68 Dali::Vector<GLint> programBinaryFormats;
70 CHECK_GL( mGlAbstraction, mGlAbstraction.GetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES, &mNumberOfProgramBinaryFormats ) );
71 LOG_GL("GetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS_OES) = %d\n", mNumberOfProgramBinaryFormats );
73 if( GL_NO_ERROR == mGlAbstraction.GetError() && 0 < mNumberOfProgramBinaryFormats )
75 programBinaryFormats.Resize( mNumberOfProgramBinaryFormats );
76 CHECK_GL( mGlAbstraction, mGlAbstraction.GetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES, &programBinaryFormats[0] ) );
77 LOG_GL("GetIntegerv(GL_PROGRAM_BINARY_FORMATS_OES) = %d\n", programBinaryFormats[0] );
78 mProgramBinaryFormat = programBinaryFormats[0];
82 void ProgramController::GlContextDestroyed()
84 mNumberOfProgramBinaryFormats = 0;
85 mProgramBinaryFormat = 0;
87 SetCurrentProgram( NULL );
88 // Inform programs they are no longer valid
89 const ProgramIterator end = mProgramCache.End();
90 for ( ProgramIterator iter = mProgramCache.Begin(); iter != end; ++iter )
92 (*iter)->GetProgram()->GlContextDestroyed();
96 Integration::GlAbstraction& ProgramController::GetGlAbstraction()
98 return mGlAbstraction;
101 Program* ProgramController::GetProgram( size_t shaderHash )
103 Program* program = NULL;
104 const ProgramIterator end = mProgramCache.End();
105 for ( ProgramIterator iter = mProgramCache.Begin(); iter != end; ++iter )
107 size_t hash = (*iter)->GetHash();
108 if( shaderHash == hash )
110 program = (*iter)->GetProgram();
117 void ProgramController::AddProgram( size_t shaderHash, Program* program )
119 // we expect unique hash values so its event thread sides job to guarantee that
120 // AddProgram is only called after program checks that GetProgram returns NULL
121 mProgramCache.PushBack( new ProgramPair( program, shaderHash ) );
124 Program* ProgramController::GetCurrentProgram()
126 return mCurrentProgram;
129 void ProgramController::SetCurrentProgram( Program* program )
131 mCurrentProgram = program;
134 bool ProgramController::IsBinarySupported()
136 return mNumberOfProgramBinaryFormats > 0;
139 unsigned int ProgramController::ProgramBinaryFormat()
141 return mProgramBinaryFormat;
144 void ProgramController::StoreBinary( Integration::ShaderDataPtr programData )
146 DALI_ASSERT_DEBUG( programData->GetBufferSize() > 0 );
147 DALI_ASSERT_DEBUG( programData->GetHashValue() != 0 );
148 DALI_ASSERT_DEBUG( mShaderSaver && "SetShaderSaver() should have been called during startup." );
150 if( mShaderSaver != NULL )
152 mShaderSaver->SaveBinary( programData );
156 void ProgramController::SetShaderSaver( ShaderSaver& shaderSaver )
158 mShaderSaver = &shaderSaver;
161 } // namespace Internal