Revert "[Tizen] Revert "Support for recognizing legacy shaders."
authorWonsik Jung <sidein@samsung.com>
Wed, 26 Jun 2024 07:31:21 +0000 (16:31 +0900)
committerWonsik Jung <sidein@samsung.com>
Wed, 26 Jun 2024 07:31:21 +0000 (16:31 +0900)
This reverts commit 85c6d4287e77b74228339d8aa35926822a898b06.

automated-tests/src/dali-internal/CMakeLists.txt
automated-tests/src/dali-internal/utc-Dali-Internal-Shader.cpp [new file with mode: 0644]
dali/graphics-api/graphics-shader-create-info.h
dali/integration-api/file.list
dali/integration-api/testing.cpp [new file with mode: 0644]
dali/integration-api/testing.h [new file with mode: 0644]
dali/internal/common/shader-data.h
dali/internal/event/rendering/shader-impl.cpp
dali/internal/event/rendering/shader-impl.h
dali/internal/render/renderers/render-renderer.cpp

index 1a85735..8247542 100644 (file)
@@ -28,6 +28,7 @@ SET(TC_SOURCES
         utc-Dali-Internal-PinchGestureProcessor.cpp
         utc-Dali-Internal-PipelineCache.cpp
         utc-Dali-Internal-RotationGesture.cpp
+        utc-Dali-Internal-Shader.cpp
         utc-Dali-Internal-TapGesture.cpp
         utc-Dali-Internal-TapGestureProcessor.cpp
         utc-Dali-Internal-Texture.cpp
diff --git a/automated-tests/src/dali-internal/utc-Dali-Internal-Shader.cpp b/automated-tests/src/dali-internal/utc-Dali-Internal-Shader.cpp
new file mode 100644 (file)
index 0000000..ec9cfa9
--- /dev/null
@@ -0,0 +1,98 @@
+/*
+* 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.
+* 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 <dali-test-suite-utils.h>
+#include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
+#include <dali/internal/event/common/thread-local-storage.h>
+#include <dali/internal/event/effects/shader-factory.h>
+#include <dali/internal/event/rendering/shader-impl.h>
+#include <dali/internal/update/manager/update-manager.h>
+#include <dali/public-api/dali-core.h>
+#include <dali/public-api/object/type-registry.h>
+
+using namespace Dali;
+
+int UtcDaliShaderTestVersion(void)
+{
+  TestApplication application;
+
+  std::string vertexShader =
+    "//@version 100\n"
+    "some code\n";
+  std::string fragmentShader =
+    "//@version 101\n"
+    "some code\n";
+
+  Dali::Shader shader = Dali::Shader::New(vertexShader, fragmentShader);
+  {
+    auto vertexPrefix   = Dali::Shader::GetVertexShaderPrefix();
+    auto fragmentPrefix = Dali::Shader::GetFragmentShaderPrefix();
+
+    DALI_TEST_EQUALS(vertexPrefix.substr(0, 20), "//@legacy-prefix-end", TEST_LOCATION);
+    DALI_TEST_EQUALS(fragmentPrefix.substr(0, 20), "//@legacy-prefix-end", TEST_LOCATION);
+  }
+
+  // Test version number in the shader data
+  Dali::Internal::ThreadLocalStorage& tls           = Dali::Internal::ThreadLocalStorage::Get();
+  Dali::Internal::ShaderFactory&      shaderFactory = tls.GetShaderFactory();
+  size_t                              shaderHash;
+  Internal::ShaderDataPtr             shaderData = shaderFactory.Load(vertexShader, fragmentShader, {}, {}, "", shaderHash);
+
+  bool dataValid = (shaderData != nullptr);
+  DALI_TEST_EQUALS(dataValid, true, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(shaderData->GetVertexShaderVersion(), 100, TEST_LOCATION);
+  DALI_TEST_EQUALS(shaderData->GetFragmentShaderVersion(), 101, TEST_LOCATION);
+
+  END_TEST;
+}
+
+int UtcDaliShaderWithPrefixTestVersion(void)
+{
+  TestApplication application;
+
+  std::string vertexShader =
+    "//@version 100\n"
+    "some code\n";
+  std::string fragmentShader =
+    "//@version 101\n"
+    "some code\n";
+
+  auto vertexPrefix   = Dali::Shader::GetVertexShaderPrefix();
+  auto fragmentPrefix = Dali::Shader::GetFragmentShaderPrefix();
+
+  Dali::Shader shader = Dali::Shader::New(
+    vertexPrefix + vertexShader,
+    fragmentPrefix + fragmentShader);
+
+  DALI_TEST_EQUALS(vertexPrefix.substr(0, 20), "//@legacy-prefix-end", TEST_LOCATION);
+  DALI_TEST_EQUALS(fragmentPrefix.substr(0, 20), "//@legacy-prefix-end", TEST_LOCATION);
+
+  // Test version number in the shader data
+  Dali::Internal::ThreadLocalStorage& tls           = Dali::Internal::ThreadLocalStorage::Get();
+  Dali::Internal::ShaderFactory&      shaderFactory = tls.GetShaderFactory();
+  size_t                              shaderHash;
+  Internal::ShaderDataPtr             shaderData = shaderFactory.Load(vertexShader, fragmentShader, {}, {}, "", shaderHash);
+
+  bool dataValid = (shaderData != nullptr);
+  DALI_TEST_EQUALS(dataValid, true, TEST_LOCATION);
+
+  DALI_TEST_EQUALS(shaderData->GetVertexShaderVersion(), 100, TEST_LOCATION);
+  DALI_TEST_EQUALS(shaderData->GetFragmentShaderVersion(), 101, TEST_LOCATION);
+
+  END_TEST;
+}
\ No newline at end of file
index 2476c79..24cbfcf 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_GRAPHICS_SHADER_CREATE_INFO_H
 
 /*
- * Copyright (c) 2021 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.
@@ -117,6 +117,18 @@ struct ShaderCreateInfo
   }
 
   /**
+   * @brief Sets shader code DALi-specific version
+   *
+   * @param[in] value shader version
+   * @return reference to this structure
+   */
+  auto& SetShaderVersion(uint32_t value)
+  {
+    shaderVersion = value;
+    return *this;
+  }
+
+  /**
    * @brief Sets allocation callbacks which will be used when object is created
    * and destroyed.
    *
@@ -148,6 +160,7 @@ struct ShaderCreateInfo
   const void*      sourceData{nullptr};
   uint32_t         sourceSize{0u};
   ShaderSourceMode sourceMode{};
+  uint32_t         shaderVersion{};
 
   const AllocationCallbacks* allocationCallbacks{nullptr};
 };
index 7b6dc9a..bf08a16 100644 (file)
@@ -13,6 +13,7 @@ SET( platform_abstraction_src_files
    ${platform_abstraction_src_dir}/profiling.cpp
    ${platform_abstraction_src_dir}/render-task-list-integ.cpp
    ${platform_abstraction_src_dir}/scene.cpp
+   ${platform_abstraction_src_dir}/testing.cpp
    ${platform_abstraction_src_dir}/texture-integ.cpp
    ${platform_abstraction_src_dir}/trace.cpp
 )
@@ -52,6 +53,7 @@ SET( platform_abstraction_header_files
    ${platform_abstraction_src_dir}/resource-policies.h
    ${platform_abstraction_src_dir}/resource-types.h
    ${platform_abstraction_src_dir}/scene.h
+   ${platform_abstraction_src_dir}/testing.h
    ${platform_abstraction_src_dir}/texture-integ.h
    ${platform_abstraction_src_dir}/trace.h
 )
diff --git a/dali/integration-api/testing.cpp b/dali/integration-api/testing.cpp
new file mode 100644 (file)
index 0000000..5b916b4
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+* 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.
+* 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 <dali/integration-api/testing.h>
+#include <dali/internal/event/rendering/shader-impl.h>
+
+namespace Dali
+{
+namespace Integration
+{
+namespace Test
+{
+std::string GenerateTaggedShaderPrefix(std::string prefix)
+{
+  return Internal::Shader::GenerateTaggedShaderPrefix(prefix);
+}
+} // Namespace Test
+} // Namespace Integration
+} // Namespace Dali
diff --git a/dali/integration-api/testing.h b/dali/integration-api/testing.h
new file mode 100644 (file)
index 0000000..ea2c993
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef DALI_INTEGRATION_TESTING_H
+#define DALI_INTEGRATION_TESTING_H
+
+/*
+ * 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.
+ * 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 <string>
+
+namespace Dali
+{
+namespace Integration
+{
+/**
+ * This functions should be used only within automated tests suit
+ */
+namespace Test
+{
+/**
+ * @brief Generates internal tag for shader prefix
+ * @param[in] prefix Prefix to generate tag for
+ * @return Tagged prefix
+ */
+std::string GenerateTaggedShaderPrefix(std::string prefix);
+
+} // Namespace Test
+} // Namespace Integration
+} // Namespace Dali
+
+#endif
\ No newline at end of file
index 10e58b4..f52accd 100644 (file)
@@ -72,6 +72,8 @@ public:
     mRenderPassTag(renderPassTag),
     mName(name)
   {
+    UpdateShaderVersion(mVertexShader, mVertexShaderVersion);
+    UpdateShaderVersion(mFragmentShader, mFragmentShaderVersion);
   }
 
   /**
@@ -91,6 +93,8 @@ public:
     mRenderPassTag(renderPassTag),
     mName(name)
   {
+    UpdateShaderVersion(mVertexShader, mVertexShaderVersion);
+    UpdateShaderVersion(mFragmentShader, mFragmentShaderVersion);
   }
 
   /**
@@ -110,6 +114,8 @@ public:
     mRenderPassTag(renderPassTag),
     mName(name)
   {
+    UpdateShaderVersion(mVertexShader, mVertexShaderVersion);
+    UpdateShaderVersion(mFragmentShader, mFragmentShaderVersion);
   }
 
   /**
@@ -276,19 +282,66 @@ public: // API
     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                   mRenderPassTag{0u}; ///< Render Pass Tag for this shader
-  std::string                mName{""};          ///< Name 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
index de3c95e..76e1e04 100644 (file)
@@ -343,13 +343,33 @@ std::string Shader::GetShaderVersionPrefix()
 std::string Shader::GetVertexShaderPrefix()
 {
   Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
-  return tls.GetVertexShaderPrefix();
+  return GenerateTaggedShaderPrefix(tls.GetVertexShaderPrefix());
 }
 
 std::string Shader::GetFragmentShaderPrefix()
 {
   Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
-  return tls.GetFragmentShaderPrefix();
+  return GenerateTaggedShaderPrefix(tls.GetFragmentShaderPrefix());
+}
+
+std::string Shader::GenerateTaggedShaderPrefix(const std::string& shaderPrefix)
+{
+  // The tag is added at the top of vertex/fragment shader and
+  // contains an offset where the source code starts after
+  // the prefix.
+  // This offset is used later by the graphics backend to ignore
+  // the legacy prefix if provided with new versioned shader.
+  // When shader contains tagged prefix, then it starts with:
+  // "//@legacy-prefix-end <offset>" tag.
+  static const std::string TAG           = "//@legacy-prefix-end ";
+  const uint32_t           OFFSET_DIGITS = 5; // offset allocates 5 digits
+
+  auto prefix       = std::string(TAG + "00000\n") + shaderPrefix;
+  auto prefixLength = prefix.length();
+  char tmp          = *(prefix.data() + TAG.length() + OFFSET_DIGITS);
+  std::snprintf(prefix.data() + TAG.size(), OFFSET_DIGITS + 1, "%05d", int(prefixLength));
+  *(prefix.data() + TAG.length() + OFFSET_DIGITS) = tmp;
+  return prefix;
 }
 
 } // namespace Internal
index ccde312..0f597d1 100644 (file)
@@ -141,6 +141,14 @@ public:
    * @copydoc Dali::Shader::GetFragmentShaderPrefix()
    */
   static std::string GetFragmentShaderPrefix();
+
+public:
+  /**
+   * Generates tag 'legacy-prefix-end' with end position of
+   * prefix text to make shader code parsing easier.
+   * Function is public to be testable
+   */
+  static std::string GenerateTaggedShaderPrefix(const std::string& shaderPrefix);
 };
 
 } // namespace Internal
index 4320a43..975664a 100644 (file)
@@ -421,6 +421,7 @@ Program* Renderer::PrepareProgram(const SceneGraph::RenderInstruction& instructi
     const std::vector<char>& vertexShaderSrc = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::VERTEX_SHADER);
     vertexShaderCreateInfo.SetSourceSize(vertexShaderSrc.size());
     vertexShaderCreateInfo.SetSourceData(static_cast<const void*>(vertexShaderSrc.data()));
+    vertexShaderCreateInfo.SetShaderVersion(shaderData->GetVertexShaderVersion());
     auto vertexShader = mGraphicsController->CreateShader(vertexShaderCreateInfo, nullptr);
 
     Graphics::ShaderCreateInfo fragmentShaderCreateInfo;
@@ -429,6 +430,7 @@ Program* Renderer::PrepareProgram(const SceneGraph::RenderInstruction& instructi
     const std::vector<char>& fragmentShaderSrc = shaderData->GetShaderForPipelineStage(Graphics::PipelineStage::FRAGMENT_SHADER);
     fragmentShaderCreateInfo.SetSourceSize(fragmentShaderSrc.size());
     fragmentShaderCreateInfo.SetSourceData(static_cast<const void*>(fragmentShaderSrc.data()));
+    fragmentShaderCreateInfo.SetShaderVersion(shaderData->GetFragmentShaderVersion());
     auto fragmentShader = mGraphicsController->CreateShader(fragmentShaderCreateInfo, nullptr);
 
     std::vector<Graphics::ShaderState> shaderStates{
@@ -490,9 +492,8 @@ bool Renderer::Render(Graphics::CommandBuffer&                             comma
     mRenderCallbackInput->usingOwnEglContext = isolatedNotDirect;
     // Set storage for the context to be used
     info.glesNativeInfo.eglSharedContextStoragePointer = &mRenderCallbackInput->eglContext;
-    info.executionMode = isolatedNotDirect ?
-                         Graphics::DrawNativeExecutionMode::ISOLATED : Graphics::DrawNativeExecutionMode::DIRECT;
-    info.reserved = nullptr;
+    info.executionMode                                 = isolatedNotDirect ? Graphics::DrawNativeExecutionMode::ISOLATED : Graphics::DrawNativeExecutionMode::DIRECT;
+    info.reserved                                      = nullptr;
 
     auto& textureResources = mRenderCallback->GetTextureResources();