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/gl-resources/context.h>
23 #include <dali/internal/render/gl-resources/gpu-buffer.h>
24 #include <dali/internal/render/shaders/program.h>
33 RenderGeometry::RenderGeometry()
34 : mDataNeedsUploading( true )
38 RenderGeometry::~RenderGeometry()
42 void RenderGeometry::GlContextCreated( Context& context )
44 mDataNeedsUploading = true;
47 void RenderGeometry::GlContextDestroyed()
49 for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
51 GpuBuffer* gpuBuffer = *iter;
54 gpuBuffer->GlContextDestroyed();
60 mIndexBuffer->GlContextDestroyed();
64 void RenderGeometry::UploadAndDraw(
67 BufferIndex bufferIndex,
68 const GeometryDataProvider& geometryDataProvider )
70 UploadVertexData( context, bufferIndex, geometryDataProvider );
72 EnableVertexAttributes( context, program );
73 Draw( context, bufferIndex, geometryDataProvider );
74 DisableVertexAttributes( context, program );
77 void RenderGeometry::GeometryUpdated()
79 mDataNeedsUploading = true;
82 void RenderGeometry::UploadVertexData(
84 BufferIndex bufferIndex,
85 const GeometryDataProvider& geometry )
87 if( mDataNeedsUploading ) // @todo Or if any of the property buffers are dirty
89 DoUpload( context, bufferIndex, geometry );
91 mDataNeedsUploading = false;
95 void RenderGeometry::DoUpload(
97 BufferIndex bufferIndex,
98 const GeometryDataProvider& geometry)
101 const Geometry::VertexBuffers& vertexBuffers = geometry.GetVertexBuffers();
102 DALI_ASSERT_DEBUG( vertexBuffers.Count() > 0 && "Need vertex buffers to upload" );
104 for( unsigned int i=0; i<vertexBuffers.Count(); ++i)
106 const PropertyBuffer* vertexBuffer = vertexBuffers[i];
108 // @todo MESH_REWORK STATIC_DRAW or DYNAMIC_DRAW depends on property buffer type (static / animated)
109 GpuBuffer* vertexGpuBuffer = new GpuBuffer( *context, GpuBuffer::ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
111 std::size_t dataSize = vertexBuffer->GetDataSize( bufferIndex );
112 vertexGpuBuffer->UpdateDataBuffer( dataSize, vertexBuffer->GetData( bufferIndex ) );
113 vertexGpuBuffer->SetStride( vertexBuffer->GetElementSize( bufferIndex ) );
115 mVertexBuffers.PushBack( vertexGpuBuffer );
119 const PropertyBuffer* indexBuffer = geometry.GetIndexBuffer();
122 GpuBuffer* indexGpuBuffer = new GpuBuffer( *context, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW );
124 unsigned int dataSize = indexBuffer->GetDataSize( bufferIndex );
125 indexGpuBuffer->UpdateDataBuffer( dataSize, indexBuffer->GetData( bufferIndex ) );
127 mIndexBuffer.Reset();
128 mIndexBuffer = indexGpuBuffer;
132 void RenderGeometry::BindBuffers()
134 for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter )
141 mIndexBuffer->Bind();
145 void RenderGeometry::EnableVertexAttributes( Context* context, Program& program )
147 // @todo Loop thru the array of vertex buffers
148 // @todo Use AttributeDataProvider to get the attrs and enable them
149 // Need mapping from gpu buffers index to a particular attributes
152 unsigned int gpuBufferIndex = 0;
154 GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION );
155 context->VertexAttribPointer( positionLoc,
158 GL_FALSE, // Not normalized
159 mVertexBuffers[gpuBufferIndex]->GetStride(),
162 context->EnableVertexAttributeArray( positionLoc );
164 GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD );
165 context->VertexAttribPointer( textureCoordsLoc,
166 2, // Texture Coords = U, V
169 mVertexBuffers[gpuBufferIndex]->GetStride(),
171 context->EnableVertexAttributeArray( textureCoordsLoc );
174 void RenderGeometry::DisableVertexAttributes( Context* context, Program& program )
176 // @todo Loop thru the array of vertex buffers
177 // @todo Use AttributeDataProvider to get the attrs and disable them
178 GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION );
179 GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD );
180 context->DisableVertexAttributeArray( positionLoc );
181 context->DisableVertexAttributeArray( textureCoordsLoc );
184 void RenderGeometry::Draw( Context* context, BufferIndex bufferIndex, const GeometryDataProvider& geometry )
186 GeometryDataProvider::GeometryType type = geometry.GetGeometryType( bufferIndex );
188 unsigned int numIndices = 0;
189 const PropertyBuffer* indexBuffer = geometry.GetIndexBuffer();
193 numIndices = indexBuffer->GetDataSize(bufferIndex) / indexBuffer->GetElementSize(bufferIndex);
198 case Dali::Geometry::TRIANGLES:
200 context->DrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, 0);
203 case Dali::Geometry::LINES:
205 context->DrawElements(GL_LINES, numIndices, GL_UNSIGNED_SHORT, 0);
208 case Dali::Geometry::POINTS:
210 GpuBuffer* firstVertexBuffer = mVertexBuffers[0];
212 unsigned int numVertices = 0;
213 GLuint stride = firstVertexBuffer->GetStride();
216 numVertices = firstVertexBuffer->GetBufferSize() / stride;
219 context->DrawArrays(GL_POINTS, 0, numVertices );
224 DALI_ASSERT_ALWAYS( 0 && "Geometry type not supported (yet)" );
230 } // namespace SceneGraph
231 } // namespace Internal