6b9bad3060b759d5c1a8a2b78a255eec2dee30a0
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-graphics / utc-Dali-GraphicsShader.cpp
1 /*
2 * Copyright (c) 2024 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18 #include <dali-test-suite-utils.h>
19 #include <dali/dali.h>
20 #include <dali/integration-api/testing.h>
21 #include <dali/internal/graphics/gles-impl/gles-graphics-shader.h>
22 #include <test-graphics-application.h>
23
24 int UtcDaliGlesStripLegacyCodeIfNeededTest1(void)
25 {
26   TestGraphicsApplication application;
27
28   {
29     Dali::Graphics::ShaderCreateInfo info;
30     info.SetPipelineStage(Dali::Graphics::PipelineStage::VERTEX_SHADER);
31     std::string vertexShader =
32       "//@version 100\n"
33       "some code\n";
34
35     info.SetShaderVersion(100);
36     info.SetSourceData(vertexShader.data());
37     info.SetSourceSize(vertexShader.size());
38     info.SetSourceMode(Dali::Graphics::ShaderSourceMode::TEXT);
39
40     size_t dataSize  = 0;
41     size_t dataIndex = 0;
42     Graphics::GLES::ShaderImpl::StripLegacyCodeIfNeeded(info, dataIndex, dataSize);
43
44     DALI_TEST_EQUALS(dataIndex, 0, TEST_LOCATION);
45     DALI_TEST_EQUALS(dataSize, vertexShader.size(), TEST_LOCATION);
46   }
47
48   END_TEST;
49 }
50
51 int UtcDaliGlesStripLegacyCodeTestDifferentPrefix(void)
52 {
53   TestGraphicsApplication application;
54
55   std::string vertexShader =
56     "//@version 100\n"
57     "some code\n";
58
59   std::string somePrefix =
60     "This is some prefix\n";
61
62   auto newVertexPrefix = Dali::Integration::Test::GenerateTaggedShaderPrefix(somePrefix);
63   {
64     Dali::Graphics::ShaderCreateInfo info;
65     info.SetPipelineStage(Dali::Graphics::PipelineStage::VERTEX_SHADER);
66
67     std::string prefixedVertexShader = newVertexPrefix + vertexShader;
68
69     info.SetShaderVersion(100);
70     info.SetSourceData(prefixedVertexShader.data());
71     info.SetSourceSize(prefixedVertexShader.size());
72     info.SetSourceMode(Dali::Graphics::ShaderSourceMode::TEXT);
73
74     size_t dataSize  = 0;
75     size_t dataIndex = 0;
76     Graphics::GLES::ShaderImpl::StripLegacyCodeIfNeeded(info, dataIndex, dataSize);
77
78     auto index = prefixedVertexShader.find("//@version");
79
80     DALI_TEST_EQUALS(dataIndex, index, TEST_LOCATION);
81
82     // should match original shader size
83     DALI_TEST_EQUALS(dataSize, vertexShader.size(), TEST_LOCATION);
84   }
85
86   END_TEST;
87 }
88
89 int UtcDaliGlesStripLegacyCodeIfNeededTest2(void)
90 {
91   TestGraphicsApplication application;
92
93   std::string vertexShader =
94     "//@version 100\n"
95     "some code\n";
96
97   auto vertexPrefix = Dali::Shader::GetVertexShaderPrefix();
98
99   {
100     Dali::Graphics::ShaderCreateInfo info;
101     info.SetPipelineStage(Dali::Graphics::PipelineStage::VERTEX_SHADER);
102
103     std::string prefixedVertexShader = Dali::Shader::GetVertexShaderPrefix() + vertexShader;
104
105     info.SetShaderVersion(100);
106     info.SetSourceData(prefixedVertexShader.data());
107     info.SetSourceSize(prefixedVertexShader.size());
108     info.SetSourceMode(Dali::Graphics::ShaderSourceMode::TEXT);
109
110     size_t dataSize  = 0;
111     size_t dataIndex = 0;
112     Graphics::GLES::ShaderImpl::StripLegacyCodeIfNeeded(info, dataIndex, dataSize);
113
114     DALI_TEST_EQUALS(dataIndex, vertexPrefix.length(), TEST_LOCATION);
115
116     // should match original shader size
117     DALI_TEST_EQUALS(dataSize, vertexShader.size(), TEST_LOCATION);
118   }
119
120   END_TEST;
121 }
122
123 int UtcDaliGlesLegacyCodeTest(void)
124 {
125   TestGraphicsApplication application;
126
127   std::string vertexShader =
128     "#version 320 es\n"
129     "some code\n";
130
131   std::string somePrefix =
132     "This is some prefix\n";
133
134   auto newVertexPrefix = Dali::Integration::Test::GenerateTaggedShaderPrefix(somePrefix);
135   {
136     Dali::Graphics::ShaderCreateInfo info;
137     info.SetPipelineStage(Dali::Graphics::PipelineStage::VERTEX_SHADER);
138
139     std::string prefixedVertexShader = newVertexPrefix + vertexShader;
140
141     info.SetShaderVersion(0);
142     info.SetSourceData(prefixedVertexShader.data());
143     info.SetSourceSize(prefixedVertexShader.size());
144     info.SetSourceMode(Dali::Graphics::ShaderSourceMode::TEXT);
145
146     size_t dataSize  = 0;
147     size_t dataIndex = 0;
148     Graphics::GLES::ShaderImpl::StripLegacyCodeIfNeeded(info, dataIndex, dataSize);
149
150     auto index = prefixedVertexShader.find("#version");
151
152     DALI_TEST_EQUALS(dataIndex, index, TEST_LOCATION);
153
154     // should match original shader size
155     DALI_TEST_EQUALS(dataSize, vertexShader.size(), TEST_LOCATION);
156   }
157
158   END_TEST;
159 }