e9f1a5aa64ff8babb5f985c2175f3fe38f8a6a18
[platform/core/uifw/dali-core.git] / dali / internal / update / rendering / scene-graph-shader.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/update/rendering/scene-graph-shader.h>
20
21 // INTERNAL INCLUDES
22 #define DEBUG_OVERRIDE_VULKAN_SHADER
23 #ifdef DEBUG_OVERRIDE_VULKAN_SHADER
24 #include <dali/graphics/vulkan/generated/spv-shaders-gen.h>
25 #endif
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace SceneGraph
34 {
35
36 Shader::Shader( Dali::Shader::Hint::Value& hints )
37 : mGraphics( nullptr ),
38   mGraphicsShader( nullptr ),
39   mShaderCache( nullptr ),
40   mHints( hints ),
41   mConnectionObservers()
42 {
43   AddUniformMapObserver( *this );
44 }
45
46 Shader::~Shader()
47 {
48   mConnectionObservers.Destroy( *this );
49 }
50
51 void Shader::Initialize( Integration::Graphics::Graphics& graphics, ShaderCache& shaderCache )
52 {
53   mGraphics = &graphics;
54   mShaderCache = &shaderCache;
55 }
56
57 Graphics::API::Accessor<Graphics::API::Shader>& Shader::GetGfxObject()
58 {
59   return mGraphicsShader;
60 }
61
62 void Shader::AddConnectionObserver( ConnectionChangePropagator::Observer& observer )
63 {
64   mConnectionObservers.Add(observer);
65 }
66
67 void Shader::RemoveConnectionObserver( ConnectionChangePropagator::Observer& observer )
68 {
69   mConnectionObservers.Remove(observer);
70 }
71
72 void Shader::UniformMappingsChanged( const UniformMap& mappings )
73 {
74   // Our uniform map, or that of one of the watched children has changed.
75   // Inform connected observers.
76   mConnectionObservers.ConnectedUniformMapChanged();
77 }
78
79 void Shader::SetShaderProgram( Internal::ShaderDataPtr shaderData, bool modifiesGeometry )
80 {
81   // TODO: for now we will use hardcoded binary SPIRV shaders which will replace anything
82   // that is passed by the caller
83 #ifdef DEBUG_OVERRIDE_VULKAN_SHADER
84
85   mGraphicsShader = mShaderCache->GetShader( Graphics::API::ShaderDetails::ShaderSource( VSH_IMAGE_VISUAL_CODE ),
86                                              Graphics::API::ShaderDetails::ShaderSource( FSH_IMAGE_VISUAL_CODE ));
87
88 #else
89   auto& controller = mGraphics->GetController();
90
91   mGraphicsShader = controller.CreateShader(
92     controller.GetShaderFactory()
93     .SetShaderModule( Graphics::API::ShaderDetails::PipelineStage::VERTEX,
94                       Graphics::API::ShaderDetails::Language::SPIRV_1_0,
95                       Graphics::API::ShaderDetails::ShaderSource( shaderData->GetVertexShader() ))
96     .SetShaderModule( Graphics::API::ShaderDetails::PipelineStage::FRAGMENT,
97                       Graphics::API::ShaderDetails::Language::SPIRV_1_0,
98                       Graphics::API::ShaderDetails::ShaderSource( shaderData->GetFragmentShader() )) );
99 #endif
100 }
101
102
103 } // namespace SceneGraph
104
105 } // namespace Internal
106
107 } // namespace Dali