Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-geometry.cpp
1 /*
2  * Copyright (c) 2020 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 // CLASS HEADER
18 #include <dali/internal/render/renderers/render-geometry.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/internal/common/buffer-index.h>
22 #include <dali/internal/render/gl-resources/context.h>
23 #include <dali/internal/render/gl-resources/gpu-buffer.h>
24 #include <dali/internal/render/renderers/render-vertex-buffer.h>
25 #include <dali/internal/render/shaders/program.h>
26
27 namespace Dali
28 {
29 namespace Internal
30 {
31 namespace Render
32 {
33
34 Geometry::Geometry()
35 : mIndices(),
36   mIndexBuffer(nullptr),
37   mGeometryType( Dali::Geometry::TRIANGLES ),
38   mIndicesChanged(false),
39   mHasBeenUpdated(false),
40   mAttributesChanged(true)
41 {
42 }
43
44 Geometry::~Geometry() = default;
45
46 void Geometry::GlContextCreated( Context& context )
47 {
48 }
49
50 void Geometry::GlContextDestroyed()
51 {
52 }
53
54 void Geometry::AddVertexBuffer( Render::VertexBuffer* vertexBuffer )
55 {
56   mVertexBuffers.PushBack( vertexBuffer );
57   mAttributesChanged = true;
58 }
59
60 void Geometry::SetIndexBuffer( Dali::Vector<uint16_t>& indices )
61 {
62   mIndices.Swap( indices );
63   mIndicesChanged = true;
64 }
65
66 void Geometry::RemoveVertexBuffer( const Render::VertexBuffer* vertexBuffer )
67 {
68   const auto&& end = mVertexBuffers.End();
69   for( auto&& iter = mVertexBuffers.Begin(); iter != end; ++iter )
70   {
71     if( *iter == vertexBuffer )
72     {
73       //This will delete the gpu buffer associated to the RenderVertexBuffer if there is one
74       mVertexBuffers.Remove( iter );
75       mAttributesChanged = true;
76       break;
77     }
78   }
79 }
80
81 void Geometry::GetAttributeLocationFromProgram( Vector<GLint>& attributeLocation, Program& program, BufferIndex bufferIndex ) const
82 {
83   attributeLocation.Clear();
84
85   for( auto&& vertexBuffer : mVertexBuffers )
86   {
87     const uint32_t attributeCount = vertexBuffer->GetAttributeCount();
88     for( uint32_t j = 0; j < attributeCount; ++j )
89     {
90       const std::string& attributeName = vertexBuffer->GetAttributeName( j );
91       uint32_t index = program.RegisterCustomAttribute( attributeName );
92       GLint location = program.GetCustomAttributeLocation( index );
93
94       if( -1 == location )
95       {
96         DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() );
97       }
98
99       attributeLocation.PushBack( location );
100     }
101   }
102 }
103
104 void Geometry::OnRenderFinished()
105 {
106   mHasBeenUpdated = false;
107   mAttributesChanged = false;
108 }
109
110 void Geometry::Upload( Context& context )
111 {
112   if( !mHasBeenUpdated )
113   {
114     // Update buffers
115     if( mIndicesChanged )
116     {
117       if( mIndices.Empty() )
118       {
119         mIndexBuffer = nullptr;
120       }
121       else
122       {
123         if ( mIndexBuffer == nullptr )
124         {
125           mIndexBuffer = new GpuBuffer( context );
126         }
127
128         uint32_t bufferSize = static_cast<uint32_t>( sizeof( uint16_t ) * mIndices.Size() );
129         mIndexBuffer->UpdateDataBuffer( context, bufferSize, &mIndices[0], GpuBuffer::STATIC_DRAW, GpuBuffer::ELEMENT_ARRAY_BUFFER );
130       }
131
132       mIndicesChanged = false;
133     }
134
135     for( auto&& buffer : mVertexBuffers )
136     {
137       if( !buffer->Update( context ) )
138       {
139         //Vertex buffer is not ready ( Size, data or format has not been specified yet )
140         return;
141       }
142     }
143
144     mHasBeenUpdated = true;
145   }
146 }
147
148 void Geometry::Draw(
149     Context& context,
150     BufferIndex bufferIndex,
151     Vector<GLint>& attributeLocation,
152     uint32_t elementBufferOffset,
153     uint32_t elementBufferCount )
154 {
155   //Bind buffers to attribute locations
156   uint32_t base = 0u;
157   const uint32_t vertexBufferCount = static_cast<uint32_t>( mVertexBuffers.Count() );
158   for( uint32_t i = 0; i < vertexBufferCount; ++i )
159   {
160     mVertexBuffers[i]->BindBuffer( context, GpuBuffer::ARRAY_BUFFER );
161     base += mVertexBuffers[i]->EnableVertexAttributes( context, attributeLocation, base );
162   }
163
164   uint32_t numIndices(0u);
165   intptr_t firstIndexOffset(0u);
166   if( mIndexBuffer )
167   {
168     numIndices = static_cast<uint32_t>( mIndices.Size() );
169
170     if( elementBufferOffset != 0u )
171     {
172       elementBufferOffset = (elementBufferOffset >= numIndices ) ? numIndices - 1 : elementBufferOffset;
173       firstIndexOffset = elementBufferOffset * sizeof(GLushort);
174       numIndices -= elementBufferOffset;
175     }
176
177     if( elementBufferCount != 0u )
178     {
179       numIndices = std::min( elementBufferCount, numIndices );
180     }
181   }
182
183   GLenum geometryGLType(GL_NONE);
184   switch(mGeometryType)
185   {
186     case Dali::Geometry::TRIANGLES:
187     {
188       geometryGLType = GL_TRIANGLES;
189       break;
190     }
191     case Dali::Geometry::LINES:
192     {
193       geometryGLType = GL_LINES;
194       break;
195     }
196     case Dali::Geometry::POINTS:
197     {
198       geometryGLType = GL_POINTS;
199       break;
200     }
201     case Dali::Geometry::TRIANGLE_STRIP:
202     {
203       geometryGLType = GL_TRIANGLE_STRIP;
204       break;
205     }
206     case Dali::Geometry::TRIANGLE_FAN:
207     {
208       geometryGLType = GL_TRIANGLE_FAN;
209       break;
210     }
211     case Dali::Geometry::LINE_LOOP:
212     {
213       geometryGLType = GL_LINE_LOOP;
214       break;
215     }
216     case Dali::Geometry::LINE_STRIP:
217     {
218       geometryGLType = GL_LINE_STRIP;
219       break;
220     }
221   }
222
223   //Draw call
224   if( mIndexBuffer && geometryGLType != GL_POINTS )
225   {
226     //Indexed draw call
227     mIndexBuffer->Bind( context, GpuBuffer::ELEMENT_ARRAY_BUFFER );
228     // numIndices truncated, no value loss happening in practice
229     context.DrawElements( geometryGLType, static_cast<GLsizei>( numIndices ), GL_UNSIGNED_SHORT, reinterpret_cast<void*>( firstIndexOffset ) );
230   }
231   else
232   {
233     //Unindex draw call
234     GLsizei numVertices(0u);
235     if( vertexBufferCount > 0 )
236     {
237       // truncated, no value loss happening in practice
238       numVertices = static_cast<GLsizei>( mVertexBuffers[0]->GetElementCount() );
239     }
240
241     context.DrawArrays( geometryGLType, 0, numVertices );
242   }
243
244   //Disable attributes
245   for( auto&& attribute : attributeLocation )
246   {
247     if( attribute != -1 )
248     {
249       context.DisableVertexAttributeArray( static_cast<GLuint>( attribute ) );
250     }
251   }
252 }
253
254 } // namespace SceneGraph
255 } // namespace Internal
256 } // namespace Dali