Fixed uniform mappings to handle actor properties
authorDavid Steele <david.steele@samsung.com>
Fri, 8 Jun 2018 15:05:19 +0000 (16:05 +0100)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Mon, 11 Jun 2018 12:52:45 +0000 (12:52 +0000)
Uniform mappings weren't taking account of actor properties
(Removed in render-renderer.cpp deletion).

Updated scene-graph-renderer to ensure that all uniform maps are
properly collected into a single collectedUniformMap in the right
priority. This only happens when a property is registered on the
associated shader, renderer or node.

Only parses the current buffer of the collected uniform map now,
instead of both buffers.

Change-Id: I744f2c4494eb788b49117186d209e3b440db3fe9
Signed-off-by: David Steele <david.steele@samsung.com>
dali/internal/update/common/uniform-map.h
dali/internal/update/nodes/node.cpp
dali/internal/update/nodes/node.h
dali/internal/update/rendering/scene-graph-renderer.cpp
dali/internal/update/rendering/scene-graph-renderer.h

index 9b5bb4c..664a8cd 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_SCENE_GRAPH_UNIFORM_MAP_H
 
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
@@ -64,7 +64,7 @@ public:
 /**
  * The UniformMap class is used to map uniform names to property values. It is available
  * in all of the classes responsible for rendering:
- * Actor, Renderer, Geometry, TextureSet, Shader.
+ * Actor, Renderer, Shader.
  *
  * It can be observed for changes to the mapping table.
  */
index aa7f777..088c3c8 100644 (file)
@@ -110,13 +110,9 @@ Node::Node()
   mIsLayer( false ),
   mPositionUsesAnchorPoint( true )
 {
-  mUniformMapChanged[0] = 0u;
-  mUniformMapChanged[1] = 0u;
-
 #ifdef DEBUG_ENABLED
   gNodeCount++;
 #endif
-
 }
 
 Node::~Node()
@@ -180,35 +176,22 @@ void Node::RemoveUniformMapping( const std::string& uniformName )
   mRegenerateUniformMap = 2;
 }
 
+bool Node::GetUniformMapChanged( BufferIndex bufferIndex ) const
+{
+  return mRegenerateUniformMap > 0;
+}
+
 void Node::PrepareRender( BufferIndex bufferIndex )
 {
-  if(mRegenerateUniformMap != 0 )
+  // Each renderer will query back to find out if the node's map has been updated
+  for(auto renderer : mRenderer )
   {
-    if( mRegenerateUniformMap == 2 )
-    {
-      CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
-      localMap.Resize(0);
-
-      for( unsigned int i=0, count=mUniformMaps.Count(); i<count; ++i )
-      {
-        localMap.PushBack( &mUniformMaps[i] );
-      }
-    }
-    else if( mRegenerateUniformMap == 1 )
-    {
-      CollectedUniformMap& localMap = mCollectedUniformMap[ bufferIndex ];
-      CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-bufferIndex ];
-
-      localMap.Resize( oldMap.Count() );
+    renderer->UpdateUniformMap( bufferIndex, *this );
+  }
 
-      unsigned int index=0;
-      for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
-      {
-        localMap[index] = *iter;
-      }
-    }
-    --mRegenerateUniformMap;
-    mUniformMapChanged[bufferIndex] = 1u;
+  if( mRegenerateUniformMap > 0 )
+  {
+    mRegenerateUniformMap--;
   }
 }
 
index 76a3fd5..4482777 100644 (file)
@@ -96,7 +96,7 @@ static const int RenderableUpdateFlags = TransformFlag | SortModifierFlag | Chil
  * thread to modify node data, without interferring with another
  * thread reading the values from the previous update traversal.
  */
-class Node : public PropertyOwner, public NodeDataProvider
+class Node : public PropertyOwner
 {
 public:
 
@@ -760,6 +760,11 @@ public:
   void RemoveUniformMapping( const std::string& uniformName );
 
   /**
+   * Return true if the uniform map has changed this frame
+   */
+  bool GetUniformMapChanged( BufferIndex bufferIndex ) const;
+
+  /**
    * Prepare the node for rendering.
    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
    * @param[in] updateBufferIndex The current update buffer index.
@@ -818,23 +823,6 @@ private: // from NodeDataProvider
     return GetWorldColor( bufferId );
   }
 
-public: // From UniformMapDataProvider
-  /**
-   * @copydoc UniformMapDataProvider::GetUniformMapChanged
-   */
-  virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const
-  {
-    return mUniformMapChanged[bufferIndex];
-  }
-
-  /**
-   * @copydoc UniformMapDataProvider::GetUniformMap
-   */
-  virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const
-  {
-    return mCollectedUniformMap[bufferIndex];
-  }
-
 private:
 
   // Undefined
@@ -883,8 +871,7 @@ protected:
 
   NodeContainer                      mChildren;               ///< Container of children; not owned
 
-  CollectedUniformMap                mCollectedUniformMap[2]; ///< Uniform maps of the node
-  unsigned int                       mUniformMapChanged[2];   ///< Records if the uniform map has been altered this frame
+
   uint32_t                           mClippingDepth;          ///< The number of stencil clipping nodes deep this node is
   uint32_t                           mScissorDepth;           ///< The number of scissor clipping nodes deep this node is
 
index 9c61452..a9787a5 100644 (file)
@@ -60,14 +60,13 @@ void AddMappings( Dali::Internal::SceneGraph::CollectedUniformMap& localMap, con
   //   Any maps that aren't in localMap should be added in a single step
   Dali::Internal::SceneGraph::CollectedUniformMap newUniformMappings;
 
-  for( unsigned int i=0, count=uniformMap.Count(); i<count; ++i )
+  for( unsigned int i=0, count = uniformMap.Count(); i<count; ++i )
   {
     Dali::Internal::SceneGraph::UniformPropertyMapping::Hash nameHash = uniformMap[i].uniformNameHash;
     bool found = false;
 
-    for( Dali::Internal::SceneGraph::CollectedUniformMap::Iterator iter = localMap.Begin() ; iter != localMap.End() ; ++iter )
+    for( auto map : localMap )
     {
-      const Dali::Internal::SceneGraph::UniformPropertyMapping* map = (*iter);
       if( map->uniformNameHash == nameHash )
       {
         if( map->uniformName == uniformMap[i].uniformName )
@@ -88,12 +87,8 @@ void AddMappings( Dali::Internal::SceneGraph::CollectedUniformMap& localMap, con
   {
     localMap.Reserve( localMap.Count() + newUniformMappings.Count() );
 
-    for( Dali::Internal::SceneGraph::CollectedUniformMap::Iterator iter = newUniformMappings.Begin(),
-           end = newUniformMappings.End() ;
-         iter != end ;
-         ++iter )
+    for( auto map : newUniformMappings )
     {
-      const Dali::Internal::SceneGraph::UniformPropertyMapping* map = (*iter);
       localMap.PushBack( map );
     }
   }
@@ -134,9 +129,6 @@ Renderer::Renderer()
   mOpacity( 1.0f ),
   mDepthIndex( 0 )
 {
-  mUniformMapChanged[0] = false;
-  mUniformMapChanged[1] = false;
-
   // Observe our own PropertyOwner's uniform map
   AddUniformMapObserver( *this );
 }
@@ -174,40 +166,44 @@ void* AllocateUniformBufferMemory( size_t size )
 }
 
 
-void Renderer::UpdateUniformMap( BufferIndex updateBufferIndex )
+void Renderer::UpdateUniformMap( BufferIndex updateBufferIndex, Node& node )
 {
+  // Called every frame.
+  CollectedUniformMap& localMap = mCollectedUniformMap[ updateBufferIndex ];
 
-  if( mRegenerateUniformMap > UNIFORM_MAP_READY )
+  // See if we need to update the collected map:
+  if( mRegenerateUniformMap == REGENERATE_UNIFORM_MAP || // renderer or shader's uniform map has changed
+      node.GetUniformMapChanged( updateBufferIndex ) ) // node's uniform map has change
   {
-    if( mRegenerateUniformMap == REGENERATE_UNIFORM_MAP)
-    {
-      CollectedUniformMap& localMap = mCollectedUniformMap[ updateBufferIndex ];
-      localMap.Resize(0);
+    localMap.Resize(0);
 
-      const UniformMap& rendererUniformMap = PropertyOwner::GetUniformMap();
-      AddMappings( localMap, rendererUniformMap );
+    // Priority is: High: Node, Renderer, Shader, to Low
+    AddMappings( localMap, node.GetUniformMap() );
 
-      if( mShader )
-      {
-        AddMappings( localMap, mShader->GetUniformMap() );
-      }
-    }
-    else if( mRegenerateUniformMap == COPY_UNIFORM_MAP )
+    const UniformMap& rendererUniformMap = PropertyOwner::GetUniformMap();
+    AddMappings( localMap, rendererUniformMap );
+
+    if( mShader )
     {
-      // Copy old map into current map
-      CollectedUniformMap& localMap = mCollectedUniformMap[ updateBufferIndex ];
-      CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-updateBufferIndex ];
+      AddMappings( localMap, mShader->GetUniformMap() );
+    }
+  }
+  else if( mRegenerateUniformMap == COPY_UNIFORM_MAP )
+  {
+    // Copy old map into current map
+    CollectedUniformMap& oldMap = mCollectedUniformMap[ 1-updateBufferIndex ];
 
-      localMap.Resize( oldMap.Count() );
+    localMap.Resize( oldMap.Count() );
 
-      unsigned int index=0;
-      for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
-      {
-        localMap[index] = *iter;
-      }
+    unsigned int index=0;
+    for( CollectedUniformMap::Iterator iter = oldMap.Begin(), end = oldMap.End() ; iter != end ; ++iter, ++index )
+    {
+      localMap[index] = *iter;
     }
+  }
 
-    mUniformMapChanged[updateBufferIndex] = true;
+  if( mRegenerateUniformMap > 0 )
+  {
     mRegenerateUniformMap--;
   }
 }
@@ -266,8 +262,6 @@ void Renderer::PrepareRender( BufferIndex updateBufferIndex )
              .size() != vertexAttributeBindings.size())
     return;
 
-  UpdateUniformMap(updateBufferIndex);
-
   auto &shader = mShader->GetGfxObject().Get();
   auto uboCount = shader.GetUniformBlockCount();
 
@@ -306,97 +300,87 @@ void Renderer::PrepareRender( BufferIndex updateBufferIndex )
   // add built-in uniforms
 
   // write to memory
-  for (auto &&i : mCollectedUniformMap)
+  for (auto&& uniformMap : mCollectedUniformMap[updateBufferIndex])
   {
-    for (auto &&j : i)
+    std::string uniformName(uniformMap->uniformName);
+    int         arrayIndex       = 0;
+    auto        arrayLeftBracket = uniformMap->uniformName .find('[');
+    if (arrayLeftBracket != std::string::npos)
     {
-      std::string uniformName(j->uniformName);
-      int         arrayIndex       = 0;
-      auto        arrayLeftBracket = j->uniformName
-                                      .find('[');
-      if (arrayLeftBracket != std::string::npos)
-      {
-        auto arrayRightBracket = j->uniformName
-                                  .find(']');
-        arrayIndex = std::atoi(&uniformName.c_str()[arrayLeftBracket + 1]);
-        DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  "UNIFORM NAME: " << j->uniformName << ", index: " << arrayIndex );
-        uniformName = uniformName.substr(0, arrayLeftBracket);
-      }
+      auto arrayRightBracket = uniformMap->uniformName.find(']');
+      arrayIndex = std::atoi(&uniformName.c_str()[arrayLeftBracket + 1]);
+      DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  "UNIFORM NAME: " << uniformMap->uniformName << ", index: " << arrayIndex );
+      uniformName = uniformName.substr(0, arrayLeftBracket);
+    }
 
-      auto uniformInfo = Graphics::API::ShaderDetails::UniformInfo{};
-      if (shader.GetNamedUniform(uniformName, uniformInfo))
+    auto uniformInfo = Graphics::API::ShaderDetails::UniformInfo{};
+    if (shader.GetNamedUniform(uniformName, uniformInfo))
+    {
+      // write into correct uniform buffer
+      auto dst = (mUboMemory[uniformInfo.bufferIndex].data() + uniformInfo.offset);
+
+      switch (uniformMap->propertyPtr->GetType())
       {
-        // write into correct uniform buffer
-        auto dst = (mUboMemory[uniformInfo.bufferIndex].data() + uniformInfo.offset);
-        switch (j->propertyPtr
-                 ->GetType())
+        case Property::Type::FLOAT:
+        case Property::Type::INTEGER:
+        case Property::Type::BOOLEAN:
+        {
+          DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing 32bit offset: "
+                           << uniformInfo.offset << ", size: " << sizeof(float) );
+
+          dst += sizeof(float) * arrayIndex;
+          memcpy(dst, &uniformMap->propertyPtr->GetFloat(updateBufferIndex), sizeof(float));
+          break;
+        }
+        case Property::Type::VECTOR2:
+        {
+          DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing vec2 offset: "
+                           << uniformInfo.offset << ", size: " << sizeof(Vector2) ) ;
+          dst += /* sizeof(Vector2) * */arrayIndex * 16; // todo: use array stride from spirv
+          memcpy(dst, &uniformMap->propertyPtr->GetVector2(updateBufferIndex), sizeof(Vector2));
+          break;
+        }
+        case Property::Type::VECTOR3:
+        {
+          DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing vec3 offset: "
+                  << uniformInfo.offset << ", size: " << sizeof(Vector3) );
+          dst += sizeof(Vector3) * arrayIndex;
+          memcpy(dst, &uniformMap->propertyPtr->GetVector3(updateBufferIndex), sizeof(Vector3));
+          break;
+        }
+        case Property::Type::VECTOR4:
+        {
+          DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing vec4 offset: "
+                           << uniformInfo.offset << ", size: " << sizeof(Vector4) );
+
+          dst += sizeof(float) * arrayIndex;
+          memcpy(dst, &uniformMap->propertyPtr->GetVector4(updateBufferIndex), sizeof(Vector4));
+          break;
+        }
+        case Property::Type::MATRIX:
+        {
+          DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing mat4 offset: "
+                           << uniformInfo.offset << ", size: " << sizeof(Matrix)  );
+          dst += sizeof(Matrix) * arrayIndex;
+          memcpy(dst, &uniformMap->propertyPtr->GetMatrix(updateBufferIndex), sizeof(Matrix));
+          break;
+        }
+        case Property::Type::MATRIX3:
+        {
+          DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing mat3 offset: "
+                           << uniformInfo.offset << ", size: " << sizeof(Matrix3) );
+          dst += sizeof(Matrix3) * arrayIndex;
+          memcpy(dst, &uniformMap->propertyPtr->GetMatrix3(updateBufferIndex), sizeof(Matrix3));
+          break;
+        }
+        default:
         {
-          case Property::Type::FLOAT:
-          case Property::Type::INTEGER:
-          case Property::Type::BOOLEAN:
-          {
-            DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing 32bit offset: "
-                   << uniformInfo.offset << ", size: " << sizeof(float) );
-
-            dst += sizeof(float) * arrayIndex;
-            memcpy(dst, &j->propertyPtr
-                          ->GetFloat(updateBufferIndex), sizeof(float));
-            break;
-          }
-          case Property::Type::VECTOR2:
-          {
-            DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing vec2 offset: "
-                   << uniformInfo.offset << ", size: " << sizeof(Vector2) ) ;
-            dst += /* sizeof(Vector2) * */arrayIndex * 16; // todo: use array stride from spirv
-            memcpy(dst, &j->propertyPtr
-                          ->GetVector2(updateBufferIndex), sizeof(Vector2));
-            break;
-          }
-          case Property::Type::VECTOR3:
-          {
-            DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing vec3 offset: "
-                   << uniformInfo.offset << ", size: " << sizeof(Vector3) );
-            dst += sizeof(Vector3) * arrayIndex;
-            memcpy(dst, &j->propertyPtr
-                          ->GetVector3(updateBufferIndex), sizeof(Vector3));
-            break;
-          }
-          case Property::Type::VECTOR4:
-          {
-            DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing vec4 offset: "
-                   << uniformInfo.offset << ", size: " << sizeof(Vector4) );
-
-            dst += sizeof(float) * arrayIndex;
-            memcpy(dst, &j->propertyPtr
-                          ->GetVector4(updateBufferIndex), sizeof(Vector4));
-            break;
-          }
-          case Property::Type::MATRIX:
-          {
-            DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing mat4 offset: "
-                   << uniformInfo.offset << ", size: " << sizeof(Matrix)  );
-            dst += sizeof(Matrix) * arrayIndex;
-            memcpy(dst, &j->propertyPtr
-                          ->GetMatrix(updateBufferIndex), sizeof(Matrix));
-            break;
-          }
-          case Property::Type::MATRIX3:
-          {
-            DALI_LOG_STREAM( gVulkanFilter, Debug::Verbose,  uniformInfo.name << ":[" << uniformInfo.bufferIndex << "]: " << "Writing mat3 offset: "
-                   << uniformInfo.offset << ", size: " << sizeof(Matrix3) );
-            dst += sizeof(Matrix3) * arrayIndex;
-            memcpy(dst, &j->propertyPtr
-                          ->GetMatrix3(updateBufferIndex), sizeof(Matrix3));
-            break;
-          }
-          default:
-          {
-          }
         }
       }
     }
   }
 
+
   /**
    * Prepare textures
    */
@@ -506,7 +490,6 @@ void Renderer::SetTextures( TextureSet* textureSet )
 
   mTextureSet = textureSet;
   mTextureSet->AddObserver( this );
-  mRegenerateUniformMap = REGENERATE_UNIFORM_MAP;
 }
 
 void Renderer::SetShader( Shader* shader )
@@ -710,11 +693,6 @@ float Renderer::GetOpacity( BufferIndex updateBufferIndex ) const
   return mOpacity[updateBufferIndex];
 }
 
-const CollectedUniformMap& Renderer::GetUniformMap( BufferIndex bufferIndex ) const
-{
-  return mCollectedUniformMap[bufferIndex];
-}
-
 Renderer::OpacityType Renderer::GetOpacityType( BufferIndex updateBufferIndex, const Node& node ) const
 {
   Renderer::OpacityType opacityType = Renderer::OPAQUE;
@@ -776,19 +754,20 @@ void Renderer::TextureSetDeleted()
 
 void Renderer::ConnectionsChanged( PropertyOwner& object )
 {
-  // One of our child objects has changed it's connections. Ensure the uniform
-  // map gets regenerated during PrepareRender
+  // One of our child objects has changed it's connections. Ensure the collected uniform
+  // map gets regenerated during UpdateUniformMap
   mRegenerateUniformMap = REGENERATE_UNIFORM_MAP;
 }
 
 void Renderer::ConnectedUniformMapChanged()
 {
+  // Called if the shader's uniform map has changed
   mRegenerateUniformMap = REGENERATE_UNIFORM_MAP;
 }
 
 void Renderer::UniformMappingsChanged( const UniformMap& mappings )
 {
-  // The mappings are either from PropertyOwner base class, or the Actor
+  // The mappings are from PropertyOwner base class
   mRegenerateUniformMap = REGENERATE_UNIFORM_MAP;
 }
 
index 77179fe..e1f1010 100644 (file)
@@ -52,7 +52,6 @@ typedef RendererContainer::ConstIterator RendererConstIter;
 
 
 class Renderer : public PropertyOwner,
-                 public UniformMapDataProvider,
                  public UniformMap::Observer,
                  public ConnectionChangePropagator::Observer
 {
@@ -337,6 +336,13 @@ public:
   float GetOpacity( BufferIndex updateBufferIndex ) const;
 
   /**
+   * Helper function to update the uniform map.
+   * @param[in] bufferIndex The buffer to read from.
+   * @param[in] node NodeDataProvider to get uniform map
+   */
+  void UpdateUniformMap( BufferIndex bufferIndex, Node& node );
+
+  /**
    * Prepare the object for rendering.
    * This is called by the UpdateManager when an object is due to be rendered in the current frame.
    * @param[in] updateBufferIndex The current update buffer index.
@@ -421,18 +427,6 @@ public: // PropertyOwner implementation
    */
   virtual void ResetDefaultProperties( BufferIndex updateBufferIndex ){};
 
-public: // From UniformMapDataProvider
-
-  /**
-   * @copydoc UniformMapDataProvider::GetUniformMapChanged
-   */
-  virtual bool GetUniformMapChanged( BufferIndex bufferIndex ) const{ return mUniformMapChanged[bufferIndex];}
-
-  /**
-   * @copydoc UniformMapDataProvider::GetUniformMap
-   */
-  virtual const CollectedUniformMap& GetUniformMap( BufferIndex bufferIndex ) const;
-
 private:
 
   /**
@@ -440,10 +434,6 @@ private:
    */
   Renderer();
 
-  /**
-   * Helper function to update the uniform map.
-   */
-  void UpdateUniformMap( BufferIndex updateBufferIndex );
 
 private:
   Integration::Graphics::Graphics* mGraphics; ///< Graphics interface object
@@ -468,7 +458,6 @@ private:
   DepthWriteMode::Type         mDepthWriteMode:2;                 ///< Local copy of the depth write mode
   DepthTestMode::Type          mDepthTestMode:2;                  ///< Local copy of the depth test mode
 
-  bool                         mUniformMapChanged[2];             ///< Records if the uniform map has been altered this frame
   bool                         mPremultipledAlphaEnabled:1;       ///< Flag indicating whether the Pre-multiplied Alpha Blending is required
 
   std::vector<std::vector<char>> mUboMemory;                      ///< Transient memory allocated for each UBO