2 * Copyright (c) 2015 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.
17 #include "render-geometry.h"
19 #include <dali/internal/common/buffer-index.h>
20 #include <dali/internal/update/geometry/scene-graph-geometry.h>
21 #include <dali/internal/update/common/scene-graph-property-buffer.h>
22 #include <dali/internal/render/data-providers/render-data-provider.h>
23 #include <dali/internal/render/gl-resources/context.h>
24 #include <dali/internal/render/gl-resources/gpu-buffer.h>
25 #include <dali/internal/render/shaders/program.h>
34 RenderGeometry::RenderGeometry()
35 : mDataNeedsUploading( true )
39 RenderGeometry::~RenderGeometry()
43 void RenderGeometry::GlContextCreated( Context& context )
45 mDataNeedsUploading = true;
48 void RenderGeometry::GlContextDestroyed()
50 for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
52 GpuBuffer* gpuBuffer = *iter;
55 gpuBuffer->GlContextDestroyed();
61 mIndexBuffer->GlContextDestroyed();
65 void RenderGeometry::UploadAndDraw(
68 BufferIndex bufferIndex,
69 const RenderDataProvider* dataProviders )
71 UploadVertexData( context, bufferIndex, dataProviders );
73 EnableVertexAttributes( context, program );
74 Draw( context, bufferIndex, dataProviders );
75 DisableVertexAttributes( context, program );
78 void RenderGeometry::GeometryUpdated()
80 mDataNeedsUploading = true;
83 void RenderGeometry::UploadVertexData(
85 BufferIndex bufferIndex,
86 const RenderDataProvider* dataProviders )
88 if( mDataNeedsUploading ) // @todo Or if any of the property buffers are dirty
90 DoUpload( context, bufferIndex, dataProviders );
92 mDataNeedsUploading = false;
96 void RenderGeometry::DoUpload(
98 BufferIndex bufferIndex,
99 const RenderDataProvider* dataProvider )
102 RenderDataProvider::VertexBuffers vertexBuffers = dataProvider->GetVertexBuffers();
104 DALI_ASSERT_DEBUG( vertexBuffers.Count() > 0 && "Need vertex buffers to upload" );
106 for( unsigned int i=0; i<vertexBuffers.Count(); ++i)
108 const PropertyBuffer* vertexBuffer = vertexBuffers[i];
110 // @todo MESH_REWORK STATIC_DRAW or DYNAMIC_DRAW depends on property buffer type (static / animated)
111 GpuBuffer* vertexGpuBuffer = new GpuBuffer( context, GpuBuffer::ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
113 std::size_t dataSize = vertexBuffer->GetDataSize( bufferIndex );
114 vertexGpuBuffer->UpdateDataBuffer( dataSize, vertexBuffer->GetData( bufferIndex ) );
115 vertexGpuBuffer->SetStride( vertexBuffer->GetElementSize( bufferIndex ) );
117 mVertexBuffers.PushBack( vertexGpuBuffer );
121 const PropertyBuffer* indexBuffer = dataProvider->GetIndexBuffer();
124 GpuBuffer* indexGpuBuffer = new GpuBuffer( context, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
126 unsigned int dataSize = indexBuffer->GetDataSize( bufferIndex );
127 indexGpuBuffer->UpdateDataBuffer( dataSize, indexBuffer->GetData( bufferIndex ) );
129 mIndexBuffer.Reset();
130 mIndexBuffer = indexGpuBuffer;
134 void RenderGeometry::BindBuffers()
136 for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
143 mIndexBuffer->Bind();
147 void RenderGeometry::EnableVertexAttributes( Context& context, Program& program )
149 // @todo Loop thru the array of vertex buffers
150 // @todo Use AttributeDataProvider to get the attrs and enable them
151 // Need mapping from gpu buffers index to a particular attributes
154 unsigned int gpuBufferIndex = 0;
156 GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION );
157 context.VertexAttribPointer( positionLoc,
160 GL_FALSE, // Not normalized
161 mVertexBuffers[gpuBufferIndex]->GetStride(),
164 context.EnableVertexAttributeArray( positionLoc );
166 GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD );
167 context.VertexAttribPointer( textureCoordsLoc,
168 2, // Texture Coords = U, V
171 mVertexBuffers[gpuBufferIndex]->GetStride(),
173 context.EnableVertexAttributeArray( textureCoordsLoc );
176 void RenderGeometry::DisableVertexAttributes( Context& context, Program& program )
178 // @todo Loop thru the array of vertex buffers
179 // @todo Use AttributeDataProvider to get the attrs and disable them
180 GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION );
181 GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD );
182 context.DisableVertexAttributeArray( positionLoc );
183 context.DisableVertexAttributeArray( textureCoordsLoc );
186 void RenderGeometry::Draw( Context& context, BufferIndex bufferIndex, const RenderDataProvider* dataProvider )
188 const GeometryDataProvider& geometry = dataProvider->GetGeometry();
189 const PropertyBuffer* indexBuffer = dataProvider->GetIndexBuffer();
191 GeometryDataProvider::GeometryType type = geometry.GetGeometryType( bufferIndex );
193 unsigned int numIndices = 0;
196 numIndices = /* TODO: MESH_REWORK remove this 2, should implement unsigned short properties */ 2 * indexBuffer->GetDataSize(bufferIndex) / indexBuffer->GetElementSize(bufferIndex);
201 case Dali::Geometry::TRIANGLES:
203 context.DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
206 case Dali::Geometry::LINES:
208 context.DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
211 case Dali::Geometry::POINTS:
213 GpuBuffer* firstVertexBuffer = mVertexBuffers[0];
215 unsigned int numVertices = 0;
216 GLuint stride = firstVertexBuffer->GetStride();
219 numVertices = firstVertexBuffer->GetBufferSize() / stride;
222 context.DrawArrays(GL_POINTS, 0, numVertices );
227 DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
233 } // namespace SceneGraph
234 } // namespace Internal