Shader processing fix 94/311394/5
authorAdam Bialogonski <adam.b@samsung.com>
Mon, 20 May 2024 09:51:44 +0000 (10:51 +0100)
committerAdam Bialogonski <adam.b@samsung.com>
Mon, 20 May 2024 14:01:26 +0000 (15:01 +0100)
Making sure that #version is a very first statement in GLSL shader

Change-Id: I74992af10166127a285e0d2a0cc3e7ec41854237

automated-tests/src/dali-graphics/utc-Dali-GraphicsShader.cpp
dali/internal/graphics/gles-impl/gles-graphics-shader.cpp

index e2ec99e..6b9bad3 100644 (file)
@@ -118,4 +118,42 @@ int UtcDaliGlesStripLegacyCodeIfNeededTest2(void)
   }
 
   END_TEST;
+}
+
+int UtcDaliGlesLegacyCodeTest(void)
+{
+  TestGraphicsApplication application;
+
+  std::string vertexShader =
+    "#version 320 es\n"
+    "some code\n";
+
+  std::string somePrefix =
+    "This is some prefix\n";
+
+  auto newVertexPrefix = Dali::Integration::Test::GenerateTaggedShaderPrefix(somePrefix);
+  {
+    Dali::Graphics::ShaderCreateInfo info;
+    info.SetPipelineStage(Dali::Graphics::PipelineStage::VERTEX_SHADER);
+
+    std::string prefixedVertexShader = newVertexPrefix + vertexShader;
+
+    info.SetShaderVersion(0);
+    info.SetSourceData(prefixedVertexShader.data());
+    info.SetSourceSize(prefixedVertexShader.size());
+    info.SetSourceMode(Dali::Graphics::ShaderSourceMode::TEXT);
+
+    size_t dataSize  = 0;
+    size_t dataIndex = 0;
+    Graphics::GLES::ShaderImpl::StripLegacyCodeIfNeeded(info, dataIndex, dataSize);
+
+    auto index = prefixedVertexShader.find("#version");
+
+    DALI_TEST_EQUALS(dataIndex, index, TEST_LOCATION);
+
+    // should match original shader size
+    DALI_TEST_EQUALS(dataSize, vertexShader.size(), TEST_LOCATION);
+  }
+
+  END_TEST;
 }
\ No newline at end of file
index 897a50d..36ffd27 100644 (file)
@@ -43,7 +43,7 @@ struct ShaderImpl::Impl
 
     source.resize(dataSize);
     std::copy(reinterpret_cast<const uint8_t*>(_createInfo.sourceData) + dataStartIndex,
-              reinterpret_cast<const uint8_t*>(_createInfo.sourceData) + dataSize,
+              reinterpret_cast<const uint8_t*>(_createInfo.sourceData) + dataStartIndex + dataSize,
               source.data());
 
     // Substitute pointer
@@ -220,10 +220,10 @@ void ShaderImpl::StripLegacyCodeIfNeeded(const ShaderCreateInfo& info, size_t& s
 {
   // Make a copy of source code. if code is meant to be used
   // by modern parser, skip the prefix part
+  auto text = reinterpret_cast<const char*>(info.sourceData);
+  auto result = std::string_view(text).find("//@legacy-prefix-end");
   if(info.shaderVersion != 0)
   {
-    auto text = reinterpret_cast<const char*>(info.sourceData);
-    auto result = std::string_view(text).find("//@legacy-prefix-end");
     if(result != 0 && result != std::string::npos)
     {
       DALI_LOG_ERROR("Shader processing: @legacy-prefix-end must be a very first statement!\n");
@@ -234,7 +234,24 @@ void ShaderImpl::StripLegacyCodeIfNeeded(const ShaderCreateInfo& info, size_t& s
       startIndex = std::strtoul(reinterpret_cast<const char*>(info.sourceData) + 21, &end, 10);
     }
   }
-
+  else
+  {
+    // For legacy shaders we need to make sure that the #version is a very first line
+    // so need to strip //@legacy-prefix-end tag
+    if(result != std::string::npos)
+    {
+      auto versionPos = std::string_view(text).find("#version", result);
+      if(versionPos == std::string::npos)
+      {
+        DALI_LOG_ERROR("Shader processing: new-line missing after @legacy-prefix-end!\n");
+        startIndex = 0; // not trimming anything
+      }
+      else
+      {
+        startIndex = versionPos;
+      }
+    }
+  }
   finalDataSize = info.sourceSize - startIndex;
 }