[dali_2.3.31] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / internal / common / shader-data.h
index a7ffb1f..f52accd 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTERNAL_SHADER_DATA_H
 
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 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.
@@ -20,6 +20,7 @@
 
 // EXTERNAL INCLUDES
 #include <string>
+#include <string_view>
 
 // INTERNAL INCLUDES
 #include <dali/graphics-api/graphics-types.h>
@@ -36,7 +37,8 @@ using ShaderDataPtr = IntrusivePtr<ShaderData>;
 
 namespace
 {
-inline std::vector<char> StringToVector(const std::string& str)
+template<typename StdStringType>
+inline std::vector<char> StringToVector(const StdStringType& str)
 {
   auto retval = std::vector<char>{};
   retval.insert(retval.begin(), str.begin(), str.end());
@@ -58,15 +60,41 @@ public:
    * @param[in] vertexSource   Source code for vertex program
    * @param[in] fragmentSource Source code for fragment program
    * @param[in] hints          Hints for rendering
+   * @param[in] renderPassTag  RenderPassTag to match shader data and render task.
+   * @param[in] name           Shader name for debug.
    */
-  ShaderData(std::string vertexSource, std::string fragmentSource, const Dali::Shader::Hint::Value hints, uint32_t renderPass)
+  ShaderData(std::string vertexSource, std::string fragmentSource, const Dali::Shader::Hint::Value hints, uint32_t renderPassTag, std::string_view name)
   : mShaderHash(-1),
     mVertexShader(StringToVector(vertexSource)),
     mFragmentShader(StringToVector(fragmentSource)),
     mHints(hints),
     mSourceMode(Graphics::ShaderSourceMode::TEXT),
-    mRenderPass(renderPass)
+    mRenderPassTag(renderPassTag),
+    mName(name)
   {
+    UpdateShaderVersion(mVertexShader, mVertexShaderVersion);
+    UpdateShaderVersion(mFragmentShader, mFragmentShaderVersion);
+  }
+
+  /**
+   * Constructor
+   * @param[in] vertexSource   Source code for vertex program
+   * @param[in] fragmentSource Source code for fragment program
+   * @param[in] hints          Hints for rendering
+   * @param[in] renderPassTag  RenderPassTag to match shader data and render task.
+   * @param[in] name           Shader name for debug.
+   */
+  ShaderData(std::string_view vertexSource, std::string_view fragmentSource, const Dali::Shader::Hint::Value hints, uint32_t renderPassTag, std::string_view name)
+  : mShaderHash(-1),
+    mVertexShader(StringToVector(vertexSource)),
+    mFragmentShader(StringToVector(fragmentSource)),
+    mHints(hints),
+    mSourceMode(Graphics::ShaderSourceMode::TEXT),
+    mRenderPassTag(renderPassTag),
+    mName(name)
+  {
+    UpdateShaderVersion(mVertexShader, mVertexShaderVersion);
+    UpdateShaderVersion(mFragmentShader, mFragmentShaderVersion);
   }
 
   /**
@@ -74,15 +102,20 @@ public:
    * @param[in] vertexSource   Source code for vertex program
    * @param[in] fragmentSource Source code for fragment program
    * @param[in] hints          Hints for rendering
+   * @param[in] renderPassTag  RenderPassTag to match shader data and render task.
+   * @param[in] name Shader name for debug.
    */
-  ShaderData(std::vector<char>& vertexSource, std::vector<char>& fragmentSource, const Dali::Shader::Hint::Value hints, uint32_t renderPass)
+  ShaderData(std::vector<char>& vertexSource, std::vector<char>& fragmentSource, const Dali::Shader::Hint::Value hints, uint32_t renderPassTag, std::string_view name)
   : mShaderHash(-1),
     mVertexShader(vertexSource),
     mFragmentShader(fragmentSource),
     mHints(hints),
     mSourceMode(Graphics::ShaderSourceMode::BINARY),
-    mRenderPass(renderPass)
+    mRenderPassTag(renderPassTag),
+    mName(name)
   {
+    UpdateShaderVersion(mVertexShader, mVertexShaderVersion);
+    UpdateShaderVersion(mFragmentShader, mFragmentShaderVersion);
   }
 
   /**
@@ -233,25 +266,82 @@ public: // API
 
   /**
    * Get Render Pass of shader data
-   * @return Render Pass of this shader data, Default value is 0.
+   * @return RenderPassTag for Render Pass of this shader data, Default value is 0.
+   */
+  uint32_t GetRenderPassTag() const
+  {
+    return mRenderPassTag;
+  }
+
+  /**
+   * Get Name of shader data
+   * @return Name for this shader data, Default value is empty string.
    */
-  uint32_t GetRenderPass() const
+  const std::string& GetName() const
   {
-    return mRenderPass;
+    return mName;
+  }
+
+  /**
+   * Returns DALi specific vertex shader version
+   * @return valid version number
+   */
+  uint32_t GetVertexShaderVersion() const
+  {
+    return mVertexShaderVersion;
+  }
+
+  /**
+   * Returns DALi specific fragment shader version
+   * @return valid version number
+   */
+  uint32_t GetFragmentShaderVersion() const
+  {
+    return mFragmentShaderVersion;
   }
 
 private:                                        // Not implemented
   ShaderData(const ShaderData& other);          ///< no copying of this object
   ShaderData& operator=(const ShaderData& rhs); ///< no copying of this object
 
-private:                                        // Data
-  std::size_t                mShaderHash;       ///< hash key created with vertex and fragment shader code
-  std::vector<char>          mVertexShader;     ///< source code for vertex program
-  std::vector<char>          mFragmentShader;   ///< source code for fragment program
-  Dali::Shader::Hint::Value  mHints;            ///< take a hint
-  Dali::Vector<uint8_t>      mBuffer;           ///< buffer containing compiled binary bytecode
-  Graphics::ShaderSourceMode mSourceMode;       ///< Source mode of shader data ( text or binary )
-  uint32_t                   mRenderPass{0u};   ///< Render Pass for this shader
+private:
+  /**
+   * Updates shader version.
+   */
+  void UpdateShaderVersion(std::vector<char>& code, uint32_t& outVersion)
+  {
+    // The version may be updated only for GLSL language.
+    // If we support direct SPIRV this will be skipped
+    std::string_view strView = code.data();
+
+    // find first occurence of 'version' tag
+    // the tag is expected at the start of line
+    static const std::string VERSION_TAG = "//@version";
+
+    auto pos = strView.find(VERSION_TAG);
+    if(pos != std::string_view::npos && (pos == 0 || strView[pos - 1] == '\n'))
+    {
+      char* end;
+      // Update version
+      outVersion = std::strtoul(strView.data() + pos + VERSION_TAG.length(), &end, 10);
+    }
+    else
+    {
+      outVersion = 0;
+    }
+  }
+
+private:                                             // Data
+  std::size_t                mShaderHash;            ///< hash key created with vertex and fragment shader code
+  std::vector<char>          mVertexShader;          ///< source code for vertex program
+  std::vector<char>          mFragmentShader;        ///< source code for fragment program
+  Dali::Shader::Hint::Value  mHints;                 ///< take a hint
+  Dali::Vector<uint8_t>      mBuffer;                ///< buffer containing compiled binary bytecode
+  Graphics::ShaderSourceMode mSourceMode;            ///< Source mode of shader data ( text or binary )
+  uint32_t                   mRenderPassTag{0u};     ///< Render Pass Tag for this shader
+  std::string                mName{""};              ///< Name for this shader
+  uint32_t                   mVertexShaderVersion;   ///< Vertex shader version
+  uint32_t                   mFragmentShaderVersion; ///< Fragment shader version
 };
 
 } // namespace Internal