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