Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-vertex-buffer.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
18 #include <dali/internal/render/renderers/render-vertex-buffer.h>
19 #include <dali/internal/event/rendering/vertex-buffer-impl.h>  // Dali::Internal::VertexBuffer
20
21 namespace
22 {
23
24 using Dali::Property;
25 using Dali::Internal::PropertyImplementationType;
26
27 Dali::GLenum GetPropertyImplementationGlType( Property::Type propertyType )
28 {
29   Dali::GLenum type = GL_BYTE;
30
31   switch( propertyType )
32   {
33     case Property::NONE:
34     case Property::STRING:
35     case Property::ARRAY:
36     case Property::MAP:
37     case Property::EXTENTS:
38     case Property::RECTANGLE:
39     case Property::ROTATION:
40     {
41       // types not supported by gl
42       break;
43     }
44     case Property::BOOLEAN:
45     {
46       type = GL_BYTE;
47       break;
48     }
49     case Property::INTEGER:
50     {
51       type = GL_SHORT;
52       break;
53     }
54     case Property::FLOAT:
55     case Property::VECTOR2:
56     case Property::VECTOR3:
57     case Property::VECTOR4:
58     case Property::MATRIX3:
59     case Property::MATRIX:
60     {
61       type = GL_FLOAT;
62       break;
63     }
64   }
65
66   return type;
67 }
68
69 Dali::GLint GetPropertyImplementationGlSize( Property::Type propertyType )
70 {
71   Dali::GLint size = 1u;
72
73   switch( propertyType )
74   {
75     case Property::NONE:
76     case Property::STRING:
77     case Property::ARRAY:
78     case Property::MAP:
79     case Property::EXTENTS:
80     case Property::RECTANGLE:
81     case Property::ROTATION:
82     {
83       // types not supported by gl
84       break;
85     }
86     case Property::BOOLEAN:
87     {
88       size = 1u;
89       break;
90     }
91     case Property::INTEGER:
92     {
93       size = 2u;
94       break;
95     }
96     case Property::FLOAT:
97     case Property::VECTOR2:
98     case Property::VECTOR3:
99     case Property::VECTOR4:
100     case Property::MATRIX3:
101     case Property::MATRIX:
102     {
103       size = 4u;
104       break;
105     }
106   }
107
108   return size;
109 }
110 } //Unnamed namespace
111
112 namespace Dali
113 {
114 namespace Internal
115 {
116 namespace Render
117 {
118
119 VertexBuffer::VertexBuffer()
120 :mFormat(nullptr),
121  mData(nullptr),
122  mGpuBuffer(nullptr),
123  mSize(0),
124  mDataChanged(true)
125 {
126 }
127
128 VertexBuffer::~VertexBuffer() = default;
129
130 void VertexBuffer::SetFormat( VertexBuffer::Format* format )
131 {
132   mFormat = format;
133   mDataChanged = true;
134 }
135
136 void VertexBuffer::SetData( Dali::Vector<uint8_t>* data, uint32_t size )
137 {
138   mData = data;
139   mSize = size;
140   mDataChanged = true;
141 }
142
143 bool VertexBuffer::Update( Context& context )
144 {
145   if( !mData || !mFormat || !mSize )
146   {
147     return false;
148   }
149
150   if( !mGpuBuffer || mDataChanged )
151   {
152     if ( ! mGpuBuffer )
153     {
154       mGpuBuffer = new GpuBuffer( context );
155     }
156
157     // Update the GpuBuffer
158     if ( mGpuBuffer )
159     {
160       DALI_ASSERT_DEBUG( mSize && "No data in the property buffer!" );
161       mGpuBuffer->UpdateDataBuffer( context, GetDataSize(), &((*mData)[0]), GpuBuffer::STATIC_DRAW, GpuBuffer::ARRAY_BUFFER );
162     }
163
164     mDataChanged = false;
165   }
166
167   return true;
168 }
169
170 void VertexBuffer::BindBuffer( Context& context, GpuBuffer::Target target )
171 {
172   if(mGpuBuffer)
173   {
174     mGpuBuffer->Bind(context, target);
175   }
176 }
177
178 uint32_t VertexBuffer::EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, uint32_t locationBase )
179 {
180   const uint32_t attributeCount = static_cast<uint32_t>( mFormat->components.size() );
181
182   GLsizei elementSize = mFormat->size;
183
184   for( uint32_t i = 0; i < attributeCount; ++i )
185   {
186     GLint attributeLocation = vAttributeLocation[i+locationBase];
187     if( attributeLocation != -1 )
188     {
189       context.EnableVertexAttributeArray( attributeLocation );
190
191       const GLint attributeSize = mFormat->components[i].size;
192       uint32_t attributeOffset = mFormat->components[i].offset;
193       const Property::Type attributeType = mFormat->components[i].type;
194
195       context.VertexAttribPointer( attributeLocation,
196                                    attributeSize  / GetPropertyImplementationGlSize(attributeType),
197                                    GetPropertyImplementationGlType(attributeType),
198                                    GL_FALSE,  // Not normalized
199                                    elementSize,
200                                    reinterpret_cast< void* >( attributeOffset ) );
201     }
202   }
203
204   return attributeCount;
205 }
206
207 } //Render
208 } //Internal
209 } //Dali