From 920b85054f3607e64f7c060090399c8b86c8bfd0 Mon Sep 17 00:00:00 2001 From: Francisco Santos Date: Wed, 8 Apr 2015 17:38:06 +0100 Subject: [PATCH] PropertyBuffer Attributes. Change-Id: I6e2766d3f1726a04c49b463c0f7c2ed62fcbdf38 --- dali/internal/file.list | 1 + .../data-providers/attribute-data-provider.h | 56 -------- .../render/data-providers/geometry-data-provider.h | 3 +- .../data-providers/property-buffer-data-provider.h | 137 ++++++++++++++++++ .../render/data-providers/render-data-provider.cpp | 6 +- .../render/data-providers/render-data-provider.h | 11 +- dali/internal/render/renderers/render-geometry.cpp | 129 +++++++---------- dali/internal/render/renderers/render-geometry.h | 24 +++- .../renderers/render-renderer-property-buffer.cpp | 153 +++++++++++++++++++++ .../renderers/render-renderer-property-buffer.h | 112 +++++++++++++++ dali/internal/render/renderers/render-renderer.h | 6 +- .../update/common/scene-graph-property-buffer.cpp | 103 +++++++++++--- .../update/common/scene-graph-property-buffer.h | 84 +++++++---- .../update/geometry/scene-graph-geometry.cpp | 22 ++- .../scene-graph-renderer-attachment.cpp | 3 +- 15 files changed, 638 insertions(+), 212 deletions(-) delete mode 100644 dali/internal/render/data-providers/attribute-data-provider.h create mode 100644 dali/internal/render/data-providers/property-buffer-data-provider.h create mode 100644 dali/internal/render/renderers/render-renderer-property-buffer.cpp create mode 100644 dali/internal/render/renderers/render-renderer-property-buffer.h diff --git a/dali/internal/file.list b/dali/internal/file.list index d53acc7..f0c1d7d 100644 --- a/dali/internal/file.list +++ b/dali/internal/file.list @@ -148,6 +148,7 @@ internal_src_files = \ $(internal_src_dir)/render/queue/render-queue.cpp \ $(internal_src_dir)/render/renderers/render-geometry.cpp \ $(internal_src_dir)/render/renderers/render-renderer.cpp \ + $(internal_src_dir)/render/renderers/render-renderer-property-buffer.cpp \ $(internal_src_dir)/render/renderers/scene-graph-image-renderer.cpp \ $(internal_src_dir)/render/renderers/scene-graph-renderer.cpp \ $(internal_src_dir)/render/renderers/scene-graph-renderer-debug.cpp \ diff --git a/dali/internal/render/data-providers/attribute-data-provider.h b/dali/internal/render/data-providers/attribute-data-provider.h deleted file mode 100644 index cb3f680..0000000 --- a/dali/internal/render/data-providers/attribute-data-provider.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef DALI_INTERNAL_SCENE_GRAPH_ATTRIBUTE_DATA_PROVIDER_H -#define DALI_INTERNAL_SCENE_GRAPH_ATTRIBUTE_DATA_PROVIDER_H - -/* - * Copyright (c) 2015 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -namespace Dali -{ -namespace Internal -{ -namespace SceneGraph -{ - -class AttributeDataProvider -{ -public: - /** - * Constructor - */ - AttributeDataProvider() - { - } - - virtual const AttributeList& GetAttributeList() = 0; - -protected: - /** - * No deletion through this interface - */ - virtual ~AttributeDataProvider() - { - } -}; - -} // namespace SceneGraph -} // namespace Internal -} // namespace Dali - -#endif // DALI_INTERNAL_SCENE_GRAPH_ATTRIBUTE_DATA_PROVIDER_H diff --git a/dali/internal/render/data-providers/geometry-data-provider.h b/dali/internal/render/data-providers/geometry-data-provider.h index 3063738..60abb72 100644 --- a/dali/internal/render/data-providers/geometry-data-provider.h +++ b/dali/internal/render/data-providers/geometry-data-provider.h @@ -20,6 +20,7 @@ #include #include #include +#include namespace Dali { @@ -39,7 +40,7 @@ class GeometryDataProvider { public: typedef Dali::Geometry::GeometryType GeometryType; - typedef Dali::Vector< const PropertyBuffer* > VertexBuffers; + typedef Dali::Vector< const PropertyBufferDataProvider* > VertexBuffers; /** * Constructor. Nothing to do as a pure interface. diff --git a/dali/internal/render/data-providers/property-buffer-data-provider.h b/dali/internal/render/data-providers/property-buffer-data-provider.h new file mode 100644 index 0000000..2ae1862 --- /dev/null +++ b/dali/internal/render/data-providers/property-buffer-data-provider.h @@ -0,0 +1,137 @@ +#ifndef DALI_INTERNAL_SCENE_GRAPH_PROPERTY_BUFFER_DATA_PROVIDER_H +#define DALI_INTERNAL_SCENE_GRAPH_PROPERTY_BUFFER_DATA_PROVIDER_H + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// EXTERNAL INCLUDES +#include + +// INTERNAL INCLUDES +#include +#include + +namespace Dali +{ +namespace Internal +{ +namespace SceneGraph +{ + +class PropertyBufferDataProvider +{ +public: + + /** + * Type for the data contained in the buffer + */ + typedef Dali::Vector< char > BufferType; + +public: + /** + * Constructor + */ + PropertyBufferDataProvider() + { + } + + /** + * Check if the data has changed + * @param[in] bufferIndex Index to access double buffered values + * @return true if the data has changed + */ + virtual bool HasDataChanged( BufferIndex bufferIndex ) const = 0; + + /** + * Get the number of attributes + * @param[in] bufferIndex Index to access double buffered values + * @return the number of attributes + */ + virtual unsigned int GetAttributeCount( BufferIndex bufferIndex ) const = 0; + + /** + * Get the name of an attribute + * @param[in] bufferIndex Index to access double buffered values + * @param[in] index Index of the attribute + * @return the name of the attribute + */ + virtual const std::string& GetAttributeName( BufferIndex bufferIndex, unsigned int index ) const = 0; + + /** + * Get the byte size of an attribute + * @param[in] bufferIndex Index to access double buffered values + * @param[in] index Index of the attribute + * @return the byte size of the attribute + */ + virtual size_t GetAttributeSize( BufferIndex bufferIndex, unsigned int index ) const = 0; + + /** + * Get the byte offset of an attribute + * @param[in] bufferIndex Index to access double buffered values + * @param[in] index Index of the attribute + * @return the byte offset of the attribute + */ + virtual size_t GetAttributeOffset( BufferIndex bufferIndex, unsigned int index ) const = 0; + + /** + * Get the property buffer data + * @param[in] bufferIndex Index to access double buffered values + * @return the property buffer's data array + */ + virtual const BufferType& GetData( BufferIndex bufferIndex ) const = 0; + + /** + * Get the size of the property buffer in bytes + * @param[in] bufferIndex Index to access double buffered values + * @return the size in bytes + */ + virtual size_t GetDataSize( BufferIndex bufferIndex ) const = 0; + + /** + * Get the size of an element of the buffer in bytes + * @param[in] bufferIndex Index to access double buffered values + * @return the element size in bytes + */ + virtual size_t GetElementSize( BufferIndex bufferIndex ) const = 0; + + /** + * Get the number of elements + * @param[in] bufferIndex Index to access double buffered values + * @return the number of elements + */ + virtual unsigned int GetElementCount( BufferIndex bufferIndex ) const = 0; + + /** + * Get if of the GPU buffer associated with this propertyBuffer + * @param[in] bufferIndex Index to access double buffered values + * @return the element size in bytes + */ + virtual unsigned int GetGpuBufferId( BufferIndex bufferIndex ) const = 0; + +protected: + /** + * No deletion through this interface + */ + virtual ~PropertyBufferDataProvider() + { + } +}; + +} // namespace SceneGraph +} // namespace Internal +} // namespace Dali + +#endif // DALI_INTERNAL_SCENE_GRAPH_PROPERTY_BUFFER_DATA_PROVIDER_H diff --git a/dali/internal/render/data-providers/render-data-provider.cpp b/dali/internal/render/data-providers/render-data-provider.cpp index 0fa8f33..eb4d462 100644 --- a/dali/internal/render/data-providers/render-data-provider.cpp +++ b/dali/internal/render/data-providers/render-data-provider.cpp @@ -29,7 +29,7 @@ RenderDataProvider::RenderDataProvider( const MaterialDataProvider& materialDataProvider, const UniformMapDataProvider& uniformMapDataProvider, Shader& shader, - const PropertyBuffer* indexBuffer, + const PropertyBufferDataProvider* indexBuffer, const VertexBuffers& vertexBuffers, const Samplers& samplers ) @@ -87,12 +87,12 @@ Shader& RenderDataProvider::GetShader() const return *mShader; } -void RenderDataProvider::SetIndexBuffer( const PropertyBuffer* indexBuffer ) +void RenderDataProvider::SetIndexBuffer( const PropertyBufferDataProvider* indexBuffer ) { mIndexBuffer = indexBuffer; } -const PropertyBuffer* RenderDataProvider::GetIndexBuffer() const +const PropertyBufferDataProvider* RenderDataProvider::GetIndexBuffer() const { return mIndexBuffer; } diff --git a/dali/internal/render/data-providers/render-data-provider.h b/dali/internal/render/data-providers/render-data-provider.h index 47170e3..a068a57 100644 --- a/dali/internal/render/data-providers/render-data-provider.h +++ b/dali/internal/render/data-providers/render-data-provider.h @@ -22,6 +22,7 @@ #include #include #include +#include #include namespace Dali @@ -45,7 +46,7 @@ class Shader; class RenderDataProvider { public: - typedef Dali::Vector< const PropertyBuffer* > VertexBuffers; + typedef Dali::Vector< const PropertyBufferDataProvider* > VertexBuffers; typedef Dali::Vector< const SamplerDataProvider* > Samplers; /** @@ -55,7 +56,7 @@ public: const MaterialDataProvider& mMaterialDataProvider, const UniformMapDataProvider& mUniformMapDataProvider, Shader& mShader, - const PropertyBuffer* mIndexBuffer, + const PropertyBufferDataProvider* mIndexBuffer, const VertexBuffers& mVertexBuffers, const Samplers& mSamplers ); @@ -116,13 +117,13 @@ public: * Set the index buffer * @param[in] indexBuffer the index buffer to set */ - void SetIndexBuffer( const PropertyBuffer* indexBuffer ); + void SetIndexBuffer( const PropertyBufferDataProvider* indexBuffer ); /** * Get the index buffer of the geometry * @return A const reference to the index buffer */ - const PropertyBuffer* GetIndexBuffer() const; + const PropertyBufferDataProvider* GetIndexBuffer() const; /** * Set the vertex buffers @@ -153,7 +154,7 @@ private: const MaterialDataProvider* mMaterialDataProvider; const UniformMapDataProvider* mUniformMapDataProvider; Shader* mShader; - const PropertyBuffer* mIndexBuffer; + const PropertyBufferDataProvider* mIndexBuffer; VertexBuffers mVertexBuffers; Samplers mSamplers; }; diff --git a/dali/internal/render/renderers/render-geometry.cpp b/dali/internal/render/renderers/render-geometry.cpp index c7b07a8..c5b7ec6 100644 --- a/dali/internal/render/renderers/render-geometry.cpp +++ b/dali/internal/render/renderers/render-geometry.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "render-geometry.h" +#include #include #include @@ -32,7 +32,8 @@ namespace SceneGraph { RenderGeometry::RenderGeometry() -: mDataNeedsUploading( true ) +: mDataNeedsUploading( true ), + mShaderChanged( true ) { } @@ -47,19 +48,6 @@ void RenderGeometry::GlContextCreated( Context& context ) void RenderGeometry::GlContextDestroyed() { - for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter ) - { - GpuBuffer* gpuBuffer = *iter; - if( gpuBuffer ) - { - gpuBuffer->GlContextDestroyed(); - } - } - - if( mIndexBuffer ) - { - mIndexBuffer->GlContextDestroyed(); - } } void RenderGeometry::UploadAndDraw( @@ -69,10 +57,27 @@ void RenderGeometry::UploadAndDraw( const RenderDataProvider* dataProviders ) { UploadVertexData( context, bufferIndex, dataProviders ); - BindBuffers(); - EnableVertexAttributes( context, program ); + + for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i ) + { + mVertexBuffers[i]->BindBuffer( context, program ); + mVertexBuffers[i]->EnableVertexAttributes( context, bufferIndex, program ); + } + + if( mIndexBuffer ) + { + mIndexBuffer->BindBuffer( context, program ); + } + +// BindBuffers( context, bufferIndex, program ); +// EnableVertexAttributes( context, bufferIndex, program ); Draw( context, bufferIndex, dataProviders ); - DisableVertexAttributes( context, program ); + + for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i ) + { + mVertexBuffers[i]->DisableVertexAttributes( context, bufferIndex, program ); + } + //DisableVertexAttributes( context, bufferIndex, program ); } void RenderGeometry::GeometryUpdated() @@ -105,88 +110,62 @@ void RenderGeometry::DoUpload( for( unsigned int i=0; iGetDataSize( bufferIndex ); - vertexGpuBuffer->UpdateDataBuffer( dataSize, vertexBuffer->GetData( bufferIndex ) ); - vertexGpuBuffer->SetStride( vertexBuffer->GetElementSize( bufferIndex ) ); + propertyBuffer->DoUpload( context, bufferIndex ); - mVertexBuffers.PushBack( vertexGpuBuffer ); + mVertexBuffers.PushBack( propertyBuffer ); } // Index buffer - const PropertyBuffer* indexBuffer = dataProvider->GetIndexBuffer(); + const PropertyBufferDataProvider* indexBuffer = dataProvider->GetIndexBuffer(); if( indexBuffer ) { - GpuBuffer* indexGpuBuffer = new GpuBuffer( context, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW ); + mIndexBuffer = new RenderPropertyBuffer( *indexBuffer, true ); - unsigned int dataSize = indexBuffer->GetDataSize( bufferIndex ); - indexGpuBuffer->UpdateDataBuffer( dataSize, indexBuffer->GetData( bufferIndex ) ); - - mIndexBuffer.Reset(); - mIndexBuffer = indexGpuBuffer; + mIndexBuffer->DoUpload( context, bufferIndex ); } } -void RenderGeometry::BindBuffers() +void RenderGeometry::BindBuffers( Context& context, BufferIndex bufferIndex, Program& program ) { - for( GpuBuffers::Iterator iter=mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter ) + for( unsigned int i = 0; i < mVertexBuffers.Count(); ++i ) { - (*iter)->Bind(); + mVertexBuffers[i]->BindBuffer( context, program ); } if( mIndexBuffer ) { - mIndexBuffer->Bind(); + mIndexBuffer->BindBuffer( context, program ); } } -void RenderGeometry::EnableVertexAttributes( Context& context, Program& program ) +void RenderGeometry::EnableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program ) { - // @todo Loop thru the array of vertex buffers - // @todo Use AttributeDataProvider to get the attrs and enable them - // Need mapping from gpu buffers index to a particular attributes - Vector4 *vertex=0; - - unsigned int gpuBufferIndex = 0; - - GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION ); - context.VertexAttribPointer( positionLoc, - 2, // 2D position - GL_FLOAT, - GL_FALSE, // Not normalized - mVertexBuffers[gpuBufferIndex]->GetStride(), - &vertex->x ); - - context.EnableVertexAttributeArray( positionLoc ); - - GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD ); - context.VertexAttribPointer( textureCoordsLoc, - 2, // Texture Coords = U, V - GL_FLOAT, - GL_FALSE, - mVertexBuffers[gpuBufferIndex]->GetStride(), - &vertex->z ); - context.EnableVertexAttributeArray( textureCoordsLoc ); + OwnerContainer< RenderPropertyBuffer* >::Iterator it = mVertexBuffers.Begin(); + OwnerContainer< RenderPropertyBuffer* >::ConstIterator end = mVertexBuffers.End(); + for( ; it != end; ++it ) + { + (*it)->EnableVertexAttributes( context, bufferIndex, program ); + } } -void RenderGeometry::DisableVertexAttributes( Context& context, Program& program ) +void RenderGeometry::DisableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program ) { - // @todo Loop thru the array of vertex buffers - // @todo Use AttributeDataProvider to get the attrs and disable them - GLint positionLoc = program.GetAttribLocation( Program::ATTRIB_POSITION ); - GLint textureCoordsLoc = program.GetAttribLocation( Program::ATTRIB_TEXCOORD ); - context.DisableVertexAttributeArray( positionLoc ); - context.DisableVertexAttributeArray( textureCoordsLoc ); + OwnerContainer< RenderPropertyBuffer* >::Iterator it = mVertexBuffers.Begin(); + OwnerContainer< RenderPropertyBuffer* >::ConstIterator end = mVertexBuffers.End(); + for( ; it != end; ++it ) + { + (*it)->DisableVertexAttributes( context, bufferIndex, program ); + } } void RenderGeometry::Draw( Context& context, BufferIndex bufferIndex, const RenderDataProvider* dataProvider ) { const GeometryDataProvider& geometry = dataProvider->GetGeometry(); - const PropertyBuffer* indexBuffer = dataProvider->GetIndexBuffer(); + const PropertyBufferDataProvider* indexBuffer = dataProvider->GetIndexBuffer(); GeometryDataProvider::GeometryType type = geometry.GetGeometryType( bufferIndex ); @@ -210,15 +189,9 @@ void RenderGeometry::Draw( Context& context, BufferIndex bufferIndex, const Rend } case Dali::Geometry::POINTS: { - GpuBuffer* firstVertexBuffer = mVertexBuffers[0]; - - unsigned int numVertices = 0; - GLuint stride = firstVertexBuffer->GetStride(); - if( stride != 0 ) - { - numVertices = firstVertexBuffer->GetBufferSize() / stride; - } + const PropertyBufferDataProvider* firstVertexBuffer = dataProvider->GetVertexBuffers()[0]; + unsigned int numVertices = firstVertexBuffer->GetElementCount( bufferIndex ); context.DrawArrays(GL_POINTS, 0, numVertices ); break; } diff --git a/dali/internal/render/renderers/render-geometry.h b/dali/internal/render/renderers/render-geometry.h index eada45f..f4c7a19 100644 --- a/dali/internal/render/renderers/render-geometry.h +++ b/dali/internal/render/renderers/render-geometry.h @@ -17,10 +17,13 @@ * limitations under the License. */ +#include +#include #include #include #include #include +#include namespace Dali { @@ -43,7 +46,6 @@ class GeometryDataProvider; class RenderGeometry { public: - typedef OwnerContainer< GpuBuffer* > GpuBuffers; /** * Constructor. Creates a render geometry object with no GPU buffers. @@ -107,24 +109,31 @@ private: /** * Bind the geometry buffers + * @param[in] context The GL context + * @param[in] bufferIndex The current buffer index + * @param[in] program The shader program to query for attribute locations */ - void BindBuffers(); + void BindBuffers( Context& context, + BufferIndex bufferIndex, + Program& program ); /** * Enable the vertex attributes for each vertex buffer from the corresponding * shader program. * @param[in] context The GL context + * @param[in] bufferIndex The current buffer index * @param[in] program The shader program to query for attribute locations */ - void EnableVertexAttributes( Context& context, Program& progam ); + void EnableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& progam ); /** * Disable the vertex attributes for each vertex buffer from the corresponding * shader program. * @param[in] context The GL context + * @param[in] bufferIndex The current buffer index * @param[in] program The shader program to query for attribute locations */ - void DisableVertexAttributes( Context& context, Program& program ); + void DisableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program ); /** * Perform the correct draw call corresponding to the geometry type @@ -137,10 +146,13 @@ private: const RenderDataProvider* dataProviders ); private: - GpuBuffers mVertexBuffers; - OwnerPointer< GpuBuffer > mIndexBuffer; + // PropertyBuffers + OwnerPointer< RenderPropertyBuffer > mIndexBuffer; + OwnerContainer< RenderPropertyBuffer* > mVertexBuffers; + // Booleans bool mDataNeedsUploading; + bool mShaderChanged; }; } // namespace SceneGraph diff --git a/dali/internal/render/renderers/render-renderer-property-buffer.cpp b/dali/internal/render/renderers/render-renderer-property-buffer.cpp new file mode 100644 index 0000000..9be59e2 --- /dev/null +++ b/dali/internal/render/renderers/render-renderer-property-buffer.cpp @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include +#include + +namespace Dali +{ +namespace Internal +{ +namespace SceneGraph +{ + +RenderPropertyBuffer::RenderPropertyBuffer( const PropertyBufferDataProvider& propertyBufferDataProvider, bool isIndexBuffer ) +: mDataProvider( propertyBufferDataProvider ), + mGpuBuffer( NULL ), + mIsIndexBuffer( isIndexBuffer ) +{ +} + +RenderPropertyBuffer::~RenderPropertyBuffer() +{ +} + +void RenderPropertyBuffer::DoUpload( Context& context, BufferIndex bufferIndex ) +{ + bool hasGpuBuffer = NULL != mGpuBuffer; + + // Check if we have a gpu-buffer + unsigned int gpuBufferId = 0; // TODO: MESH_REWORK FIX THIS mDataProvider.GetGpuBufferId( bufferIndex ); + if ( ! hasGpuBuffer ) + { + // TODO: MESH_REWORK +// mGpuBuffer /*= gpuBufferCache.GetGpuBuffer( gpuBufferId ) */; + (void )gpuBufferId; + + if( mIsIndexBuffer ) + { + mGpuBuffer = new GpuBuffer( context, GpuBuffer::ELEMENT_ARRAY_BUFFER, GpuBuffer::STATIC_DRAW ); + } + else + { + mGpuBuffer = new GpuBuffer( context, GpuBuffer::ARRAY_BUFFER, GpuBuffer::STATIC_DRAW ); + } + } + + // Update the GpuBuffer + if ( ! hasGpuBuffer || mDataProvider.HasDataChanged( bufferIndex ) ) + { + if( mIsIndexBuffer ) + { + unsigned int dataSize = mDataProvider.GetDataSize( bufferIndex ); + mGpuBuffer->UpdateDataBuffer( dataSize, &(mDataProvider.GetData( bufferIndex )[0]) ); + } + else + { + std::size_t dataSize = mDataProvider.GetDataSize( bufferIndex ); + mGpuBuffer->UpdateDataBuffer( dataSize, &(mDataProvider.GetData( bufferIndex )[0]) ); + mGpuBuffer->SetStride( mDataProvider.GetElementSize( bufferIndex ) ); + } + } +} + +void RenderPropertyBuffer::BindBuffer( Context& context, Program& progam ) +{ + mGpuBuffer->Bind(); +} + +void RenderPropertyBuffer::EnableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program ) +{ + + // Check if attribute locations are cached already + if( mAttributesLocation.Size() == 0 ) + { + UpdateAttributeLocations( context, bufferIndex, program ); + } + + unsigned int attributeCount = mDataProvider.GetAttributeCount( bufferIndex ); + DALI_ASSERT_DEBUG( attributeCount == mAttributesLocation.Size() && "Incorrect number of attributes!" ); + + GLsizei elementSize = mDataProvider.GetElementSize( bufferIndex ); + + for( unsigned int i = 0; i < attributeCount; ++i ) + { + GLint attributeLocation = mAttributesLocation[i]; + if( attributeLocation != -1 ) + { + context.EnableVertexAttributeArray( attributeLocation ); + + GLint attributeSize = mDataProvider.GetAttributeSize( bufferIndex, i ); + size_t attributeOffset = mDataProvider.GetAttributeOffset( bufferIndex, i ); + + // TODO: MESH_REWORK Matrices need multiple calls to this function + context.VertexAttribPointer( attributeLocation, + attributeSize / sizeof(float), // TODO: MESH_REWORK get the correct type + GL_FLOAT, // TODO: MESH_REWORK get the correct type + GL_FALSE, // Not normalized + elementSize, + (void*)attributeOffset ); + } + } +} + +void RenderPropertyBuffer::DisableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program ) +{ + unsigned int attributeCount = mDataProvider.GetAttributeCount( bufferIndex ); + for( unsigned int i = 0; i < attributeCount; ++i ) + { + GLint attributeLocation = mAttributesLocation[i]; + if( attributeLocation != -1 ) + { + context.DisableVertexAttributeArray( attributeLocation ); + } + } +} + +void RenderPropertyBuffer::UpdateAttributeLocations( Context& context, BufferIndex bufferIndex, Program& program ) +{ + unsigned int attributeCount = mDataProvider.GetAttributeCount( bufferIndex ); + mAttributesLocation.Resize( attributeCount ); + + for( unsigned int i = 0; i < attributeCount; ++i ) + { + const std::string& attributeName = mDataProvider.GetAttributeName( bufferIndex, i ); + unsigned int index = program.RegisterCustomAttribute( attributeName ); + GLint attributeLocation = program.GetCustomAttributeLocation( index ); + + if( -1 == attributeLocation ) + { + DALI_LOG_WARNING( "Attribute not found in the shader: %s\n", attributeName.c_str() ); + } + mAttributesLocation[i] = attributeLocation; + } +} + +} // namespace SceneGraph +} // namespace Internal +} // namespace Dali diff --git a/dali/internal/render/renderers/render-renderer-property-buffer.h b/dali/internal/render/renderers/render-renderer-property-buffer.h new file mode 100644 index 0000000..b4d170d --- /dev/null +++ b/dali/internal/render/renderers/render-renderer-property-buffer.h @@ -0,0 +1,112 @@ +#ifndef DALI_INTERNAL_SCENE_GRAPH_RENDER_RENDERER_PROPERTY_BUFFER_H +#define DALI_INTERNAL_SCENE_GRAPH_RENDER_RENDERER_PROPERTY_BUFFER_H + +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +namespace Dali +{ +namespace Internal +{ +class Context; +class Program; +class GpuBuffer; + +namespace SceneGraph +{ +class NewRenderer; +/** + * This class encapsulates the GPU buffers. It is used to upload vertex data + * to it's GPU buffers, to bind all the buffers and to setup/teardown vertex attribute + * bindings + */ +class RenderPropertyBuffer +{ +public: + typedef OwnerContainer< GpuBuffer* > GpuBuffers; + + /** + * Constructor. Creates a render geometry object with no GPU buffers. + * @param[in] propertyBufferDataProvider The property-buffer data provider (to fetch geometry from) + */ + RenderPropertyBuffer( const PropertyBufferDataProvider& propertyBufferDataProvider, bool isIndexBuffer ); + + /** + * Destructor + */ + ~RenderPropertyBuffer(); + + /** + * Perform the upload of the buffer + * @param[in] context The GL context + * @param[in] bufferIndex The current buffer index + */ + void DoUpload( Context& context, BufferIndex bufferIndex ); + + /** + * Bind the geometry buffers + * @param[in] context The GL context + * @param[in] program The shader program to query for attribute locations + */ + void BindBuffer( Context& context, Program& progam ); + + /** + * Enable the vertex attributes for each vertex buffer from the corresponding + * shader program. + * @param[in] context The GL context + * @param[in] bufferIndex The current buffer index + * @param[in] program The shader program to query for attribute locations + */ + void EnableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program ); + + /** + * Disable the vertex attributes for each vertex buffer from the corresponding + * shader program. + * @param[in] context The GL context + * @param[in] bufferIndex The current buffer index + * @param[in] program The shader program to query for attribute locations + */ + void DisableVertexAttributes( Context& context, BufferIndex bufferIndex, Program& program ); + +private: // implementation + + /** + * Update attribute locations + * @param[in] context The GL context + * @param[in] bufferIndex The current buffer index + * @param[in] program The shader program to query for attribute locations + */ + void UpdateAttributeLocations( Context& context, BufferIndex bufferIndex, Program& program ); + +private: + const PropertyBufferDataProvider& mDataProvider; ///< Data provider used by this property buffer + Vector mAttributesLocation; ///< Location of the attributes for the property buffer in this renderer. + GpuBuffer* mGpuBuffer; ///< Pointer to the GpuBuffer associated with this RenderPropertyBuffer + + bool mIsIndexBuffer; +}; + +} // namespace SceneGraph +} // namespace Internal +} // namespace Dali + +#endif // DALI_INTERNAL_SCENE_GRAPH_RENDER_RENDERER_PROPERTY_BUFFER_H diff --git a/dali/internal/render/renderers/render-renderer.h b/dali/internal/render/renderers/render-renderer.h index d17b3e0..107c003 100644 --- a/dali/internal/render/renderers/render-renderer.h +++ b/dali/internal/render/renderers/render-renderer.h @@ -41,12 +41,10 @@ namespace SceneGraph */ class NewRenderer : public Renderer { -private: - struct UniformIndexMap; - public: typedef Integration::ResourceId ResourceId; +public: /** * Create a new renderer instance * @param[in] nodeDataProvider The node data provider @@ -121,6 +119,8 @@ public: // Implementation of GlResourceOwner virtual void GlCleanup(); private: + struct UniformIndexMap; + /** * Set the uniforms from properties according to the uniform map * @param[in] program The shader program on which to set the uniforms. diff --git a/dali/internal/update/common/scene-graph-property-buffer.cpp b/dali/internal/update/common/scene-graph-property-buffer.cpp index f405202..4f22dec 100644 --- a/dali/internal/update/common/scene-graph-property-buffer.cpp +++ b/dali/internal/update/common/scene-graph-property-buffer.cpp @@ -51,52 +51,115 @@ PropertyBuffer::~PropertyBuffer() { } -std::size_t PropertyBuffer::GetDataSize( BufferIndex bufferIndex ) const +void PropertyBuffer::SetFormat( PropertyBufferMetadata::Format* format ) { - DALI_ASSERT_DEBUG( mFormat && "Format must have been set!" ); + mFormat = format; +} - // @todo MESH_REWORK mData should be double buffered? - return mFormat->components.back().accumulatedSize * mSize; +//TODO:: MESH_REWORK Remove this, should be a property +void PropertyBuffer::SetSize( unsigned int size ) +{ + mSize = size; } -std::size_t PropertyBuffer::GetElementSize( BufferIndex bufferIndex ) const +void PropertyBuffer::SetData( PropertyBufferDataProvider::BufferType* data ) { - DALI_ASSERT_DEBUG( mFormat && "Format must have been set!" ); + mBufferData = data; - // @todo MESH_REWORK mData should be double buffered? - return mFormat->GetElementSize(); + // TODO: MESH_REWORK FIX THIS, should go through aging or message + mRenderBufferData = data; } -void PropertyBuffer::SetFormat( PropertyBufferMetadata::Format* format ) +void PropertyBuffer::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex ) { - mFormat = format; } -//TODO:: MESH_REWORK Remove this, should be a property -void PropertyBuffer::SetSize( unsigned int size ) +void PropertyBuffer::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex ) { - mSize = size; } -void PropertyBuffer::SetData( BufferType* data ) +bool PropertyBuffer::HasDataChanged( BufferIndex bufferIndex ) const { - mBufferData = data; + //TODO: MESH_REWORK fix this + DALI_ASSERT_DEBUG(false && "FIX THIS"); + return true; } -const void* PropertyBuffer::GetData( BufferIndex bufferIndex ) const +unsigned int PropertyBuffer::GetAttributeCount( BufferIndex bufferIndex ) const { - // @todo MESH_REWORK mData should be double buffered - return reinterpret_cast< const void* >(&(*mBufferData.Get())[0]); + DALI_ASSERT_DEBUG( mFormat && "Format should be set "); + return mFormat->components.size(); } -void PropertyBuffer::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex ) +const std::string& PropertyBuffer::GetAttributeName( BufferIndex bufferIndex, unsigned int index ) const { + DALI_ASSERT_DEBUG( mFormat && "Format should be set "); + return mFormat->components[index].name; } -void PropertyBuffer::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex ) +size_t PropertyBuffer::GetAttributeSize( BufferIndex bufferIndex, unsigned int index ) const +{ + DALI_ASSERT_DEBUG( mFormat && "Format should be set "); + + size_t size = mFormat->components[index].accumulatedSize; + + if ( index > 0 ) + { + size -= mFormat->components[index - 1].accumulatedSize; + } + + return size; +} + +size_t PropertyBuffer::GetAttributeOffset( BufferIndex bufferIndex, unsigned int index ) const +{ + DALI_ASSERT_DEBUG( mFormat && "Format should be set "); + + size_t offset = 0; + + if ( index > 0 ) + { + offset = mFormat->components[index - 1].accumulatedSize; + } + + return offset; +} + +const PropertyBufferDataProvider::BufferType& PropertyBuffer::GetData( BufferIndex bufferIndex ) const +{ + DALI_ASSERT_DEBUG( mRenderBufferData && "Should have some data."); + + //TODO: MESH_REWORK Should check the correct buffer index + return *mRenderBufferData; +} + +std::size_t PropertyBuffer::GetDataSize( BufferIndex bufferIndex ) const +{ + DALI_ASSERT_DEBUG( mFormat && "Format should be set "); + + //TODO: MESH_REWORK mSize should be double buffered + return mFormat->GetElementSize() * mSize; +} + +std::size_t PropertyBuffer::GetElementSize( BufferIndex bufferIndex ) const { + return mFormat->GetElementSize(); } +unsigned int PropertyBuffer::GetElementCount( BufferIndex bufferIndex ) const +{ + return mSize; +} + +unsigned int PropertyBuffer::GetGpuBufferId( BufferIndex bufferIndex ) const +{ + //TODO: MESH_REWORK fix this + DALI_ASSERT_DEBUG(false && "FIX THIS"); + + return 0; +} + + } // namespace SceneGraph } // namespace Internal } // namespace Dali diff --git a/dali/internal/update/common/scene-graph-property-buffer.h b/dali/internal/update/common/scene-graph-property-buffer.h index 06e0781..96d8466 100644 --- a/dali/internal/update/common/scene-graph-property-buffer.h +++ b/dali/internal/update/common/scene-graph-property-buffer.h @@ -20,6 +20,7 @@ #include #include #include +#include namespace Dali { @@ -66,16 +67,11 @@ struct Format } // PropertyBufferMetadata -class PropertyBuffer : public PropertyOwner +class PropertyBuffer : public PropertyOwner, public PropertyBufferDataProvider { public: /** - * Type for the data contained in the buffer - */ - typedef Dali::Vector< char > BufferType; - - /** * Constructor */ PropertyBuffer(); @@ -86,18 +82,6 @@ public: virtual ~PropertyBuffer(); /** - * Get the size of the property buffer in bytes - * @return the size in bytes - */ - std::size_t GetDataSize( BufferIndex bufferIndex ) const; - - /** - * Get the size of an element of the buffer in bytes - * @return the element size in bytes - */ - std::size_t GetElementSize( BufferIndex bufferIndex ) const; - - /** * Set the format of the buffer * @param[in] format The format for the PropertyBuffer */ @@ -107,18 +91,12 @@ public: * Set the data of the PropertyBuffer * @param[in] data The new data of the PropertyBuffer */ - void SetData( BufferType* data ); + void SetData( PropertyBufferDataProvider::BufferType* data ); //TODO:: MESH_REWORK Remove this, should be a property void SetSize( unsigned int size ); /** - * Get the property buffer data - * @return the property buffer's data array - */ - const void* GetData( BufferIndex bufferIndex ) const; - - /** * Connect the object to the scene graph * * @param[in] sceneController The scene controller - used for sending messages to render thread @@ -131,10 +109,62 @@ public: */ void DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex ); +public: // PropertyBufferDataProvider + + /** + * @copydoc PropertyBufferDataProvider::HasDataChanged( BufferIndex bufferIndex ) + */ + virtual bool HasDataChanged( BufferIndex bufferIndex ) const; + + /** + * @copydoc PropertyBufferDataProvider::HasDataChanged( BufferIndex bufferIndex ) + */ + virtual unsigned int GetAttributeCount( BufferIndex bufferIndex ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetAttributeName( BufferIndex bufferIndex, unsigned int index ) + */ + virtual const std::string& GetAttributeName( BufferIndex bufferIndex, unsigned int index ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetAttributeSize( BufferIndex bufferIndex, unsigned int index ) + */ + virtual size_t GetAttributeSize( BufferIndex bufferIndex, unsigned int index ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetAttributeOffset( BufferIndex bufferIndex, unsigned int index ) + */ + virtual size_t GetAttributeOffset( BufferIndex bufferIndex, unsigned int index ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetData( BufferIndex bufferIndex ) + */ + virtual const PropertyBufferDataProvider::BufferType& GetData( BufferIndex bufferIndex ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetDataSize( BufferIndex bufferIndex ) + */ + virtual size_t GetDataSize( BufferIndex bufferIndex ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetElementSize( BufferIndex bufferIndex ) + */ + virtual size_t GetElementSize( BufferIndex bufferIndex ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetElementCount( BufferIndex bufferIndex ) + */ + virtual unsigned int GetElementCount( BufferIndex bufferIndex ) const; + + /** + * @copydoc PropertyBufferDataProvider::GetGpuBufferId + */ + virtual unsigned int GetGpuBufferId( BufferIndex bufferIndex ) const; + private: OwnerPointer mFormat; ///< Format of the buffer - OwnerPointer mBufferData; ///< Data - BufferType* mRenderBufferData; + OwnerPointer mBufferData; ///< Data + PropertyBufferDataProvider::BufferType* mRenderBufferData; //TODO: MESH_REWORK should be double buffered property unsigned int mSize; ///< Size of the buffer diff --git a/dali/internal/update/geometry/scene-graph-geometry.cpp b/dali/internal/update/geometry/scene-graph-geometry.cpp index c951fb7..e4cf29d 100644 --- a/dali/internal/update/geometry/scene-graph-geometry.cpp +++ b/dali/internal/update/geometry/scene-graph-geometry.cpp @@ -54,20 +54,18 @@ void Geometry::RemoveVertexBuffer( const PropertyBuffer* vertexBuffer ) DALI_ASSERT_DEBUG( NULL != vertexBuffer ); // Find the object and destroy it - for ( VertexBuffers::Iterator iter = mVertexBuffers.Begin(); iter != mVertexBuffers.End(); ++iter ) + VertexBuffers::Iterator match = std::find( mVertexBuffers.Begin(), + mVertexBuffers.End(), + vertexBuffer ); + + DALI_ASSERT_DEBUG( mVertexBuffers.End() != match ); + if( mVertexBuffers.End() != match ) { - const PropertyBuffer* current = *iter; - if ( current == vertexBuffer ) - { - PropertyBuffer* theVertexBuffer = const_cast(vertexBuffer); - theVertexBuffer->RemoveUniformMapObserver(*this); - mVertexBuffers.Erase( iter ); - mConnectionObservers.ConnectionsChanged(*this); - return; - } + PropertyBuffer* theVertexBuffer = const_cast(vertexBuffer); + theVertexBuffer->RemoveUniformMapObserver(*this); + mVertexBuffers.Erase( match ); + mConnectionObservers.ConnectionsChanged(*this); } - - DALI_ASSERT_DEBUG(false); } void Geometry::SetIndexBuffer( const PropertyBuffer* indexBuffer ) diff --git a/dali/internal/update/node-attachments/scene-graph-renderer-attachment.cpp b/dali/internal/update/node-attachments/scene-graph-renderer-attachment.cpp index 841fcf9..ec07650 100644 --- a/dali/internal/update/node-attachments/scene-graph-renderer-attachment.cpp +++ b/dali/internal/update/node-attachments/scene-graph-renderer-attachment.cpp @@ -217,7 +217,8 @@ void RendererAttachment::DoPrepareRender( BufferIndex updateBufferIndex ) iter != end ; ++iter ) { - AddMappings( localMap, (*iter)->GetUniformMap() ); + const SceneGraph::PropertyBuffer* vertexBuffer = static_cast( *iter ); + AddMappings( localMap, vertexBuffer->GetUniformMap() ); } mUniformMapChanged[updateBufferIndex] = true; -- 2.7.4