2 * Copyright (c) 2019 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.
18 #include <dali-toolkit/internal/controls/control/control-renderers.h>
27 #define DALI_COMPOSE_SHADER(STR) #STR
29 const char * const BASIC_VERTEX_SOURCE = DALI_COMPOSE_SHADER(
30 precision mediump float;\n
31 attribute mediump vec2 aPosition;\n
32 varying mediump vec2 vTexCoord;\n
33 uniform mediump mat4 uMvpMatrix;\n
34 uniform mediump vec3 uSize;\n
38 mediump vec4 vertexPosition = vec4(aPosition * uSize.xy, 0.0, 1.0);\n
39 vTexCoord = aPosition + vec2(0.5);
40 gl_Position = uMvpMatrix * vertexPosition;\n
44 const char * const BASIC_FRAGMENT_SOURCE = DALI_COMPOSE_SHADER(
45 precision mediump float;\n
46 varying mediump vec2 vTexCoord;\n
47 uniform sampler2D sTexture;\n
48 uniform vec4 uColor;\n
52 gl_FragColor = texture2D(sTexture, vTexCoord);\n
53 gl_FragColor *= uColor;
57 Geometry CreateGridGeometry( Uint16Pair gridSize )
59 uint16_t gridWidth = gridSize.GetWidth();
60 uint16_t gridHeight = gridSize.GetHeight();
63 Vector< Vector2 > vertices;
64 vertices.Reserve( ( gridWidth + 1 ) * ( gridHeight + 1 ) );
66 for( int y = 0; y < gridHeight + 1; ++y )
68 for( int x = 0; x < gridWidth + 1; ++x )
70 vertices.PushBack( Vector2( (float)x/gridWidth - 0.5f, (float)y/gridHeight - 0.5f) );
75 Vector< unsigned short > indices;
76 indices.Reserve( (gridWidth+2)*gridHeight*2 - 2);
78 for( unsigned int row = 0u; row < gridHeight; ++row )
80 unsigned int rowStartIndex = row*(gridWidth+1u);
81 unsigned int nextRowStartIndex = rowStartIndex + gridWidth +1u;
83 if( row != 0u ) // degenerate index on non-first row
85 indices.PushBack( rowStartIndex );
88 for( unsigned int column = 0u; column < gridWidth+1u; column++) // main strip
90 indices.PushBack( rowStartIndex + column);
91 indices.PushBack( nextRowStartIndex + column);
94 if( row != gridHeight-1u ) // degenerate index on non-last row
96 indices.PushBack( nextRowStartIndex + gridWidth );
100 Property::Map vertexFormat;
101 vertexFormat[ "aPosition" ] = Property::VECTOR2;
102 VertexBuffer vertexBuffer = VertexBuffer::New( vertexFormat );
103 if( vertices.Size() > 0 )
105 vertexBuffer.SetData( &vertices[ 0 ], vertices.Size() );
108 // Create the geometry object
109 Geometry geometry = Geometry::New();
110 geometry.AddVertexBuffer( vertexBuffer );
111 if( indices.Size() > 0 )
113 geometry.SetIndexBuffer( &indices[ 0 ], indices.Size() );
116 geometry.SetType( Geometry::TRIANGLE_STRIP );
121 Dali::Renderer CreateRenderer( const char* vertexSrc, const char* fragmentSrc )
123 Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc );
125 Dali::Geometry texturedQuadGeometry = Dali::Geometry::New();
127 struct VertexPosition { Dali::Vector2 position; };
128 struct VertexTexture { Dali::Vector2 texture; };
130 VertexPosition positionArray[] =
132 { Dali::Vector2( -0.5f, -0.5f ) },
133 { Dali::Vector2( 0.5f, -0.5f ) },
134 { Dali::Vector2( -0.5f, 0.5f ) },
135 { Dali::Vector2( 0.5f, 0.5f ) }
137 uint32_t numberOfVertices = sizeof(positionArray)/sizeof(VertexPosition);
139 Dali::Property::Map positionVertexFormat;
140 positionVertexFormat["aPosition"] = Dali::Property::VECTOR2;
141 Dali::VertexBuffer positionVertices = Dali::VertexBuffer::New( positionVertexFormat );
142 positionVertices.SetData( positionArray, numberOfVertices );
143 texturedQuadGeometry.AddVertexBuffer( positionVertices );
145 const uint16_t indices[] = { 0, 3, 1, 0, 2, 3 };
146 texturedQuadGeometry.SetIndexBuffer ( &indices[0], sizeof( indices )/ sizeof( indices[0] ) );
148 Dali::Renderer renderer = Dali::Renderer::New( texturedQuadGeometry, shader );
150 Dali::TextureSet textureSet = Dali::TextureSet::New();
151 renderer.SetTextures( textureSet );
156 Dali::Renderer CreateRenderer( const char* vertexSrc, const char* fragmentSrc, Dali::Shader::Hint::Value hints, Uint16Pair gridSize )
158 Dali::Shader shader = Dali::Shader::New( vertexSrc, fragmentSrc, hints );
160 Dali::Geometry gridGeometry = CreateGridGeometry( gridSize );
162 Dali::Renderer renderer = Dali::Renderer::New( gridGeometry, shader );
164 Dali::TextureSet textureSet = Dali::TextureSet::New();
165 renderer.SetTextures( textureSet );
170 void SetRendererTexture( Dali::Renderer renderer, Dali::Texture texture )
174 Dali::TextureSet textureSet = renderer.GetTextures();
175 textureSet.SetTexture( 0u, texture );
179 void SetRendererTexture( Dali::Renderer renderer, Dali::FrameBuffer frameBuffer )
183 Dali::Texture texture = frameBuffer.GetColorTexture();
184 SetRendererTexture( renderer, texture );
188 } // namespace Internal
189 } // namespace Toolkit