Merge "Removed unused macro invocation from shader" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-gl-abstraction.cpp
1 /*
2  * Copyright (c) 2023 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 "test-gl-abstraction.h"
19 #include "test-trace-call-stack.h"
20
21 static const bool TRACE{false};
22
23 uint32_t GetGLDataTypeSize(GLenum type)
24 {
25   // There are many more types than what are covered here, but
26   // they are not supported in dali.
27   switch(type)
28   {
29     case GL_FLOAT: // "float", 1 float, 4 bytes
30       return 4;
31     case GL_FLOAT_VEC2: // "vec2", 2 floats, 8 bytes
32       return 8;
33     case GL_FLOAT_VEC3: // "vec3", 3 floats, 12 bytes
34       return 12;
35     case GL_FLOAT_VEC4: // "vec4", 4 floats, 16 bytes
36       return 16;
37     case GL_INT: // "int", 1 integer, 4 bytes
38       return 4;
39     case GL_FLOAT_MAT2: // "mat2", 4 floats, 16 bytes
40       return 16;
41     case GL_FLOAT_MAT3: // "mat3", 3 vec3, 36 bytes
42       return 36;
43     case GL_FLOAT_MAT4: // "mat4", 4 vec4, 64 bytes
44       return 64;
45     default:
46       return 0;
47   }
48 }
49
50 namespace Dali
51 {
52 TestGlAbstraction::TestGlAbstraction()
53 : mBufferTrace(TRACE, std::string("gl")),
54   mCullFaceTrace(TRACE, "gl"),
55   mEnableDisableTrace(TRACE, "gl"),
56   mShaderTrace(TRACE, "gl"),
57   mTextureTrace(TRACE, std::string("gl")),
58   mTexParameterTrace(TRACE, "gl"),
59   mDrawTrace(TRACE, "gl"),
60   mDepthFunctionTrace(TRACE, "gl"),
61   mStencilFunctionTrace(TRACE, "gl"),
62   mScissorTrace(TRACE, "gl"),
63   mSetUniformTrace(TRACE, "Uniform "),
64   mViewportTrace(TRACE, "gl")
65 {
66   Initialize();
67 }
68
69 TestGlAbstraction::~TestGlAbstraction()
70 {
71 }
72
73 void TestGlAbstraction::Initialize()
74 {
75   mCurrentProgram                         = 0;
76   mCompileStatus                          = GL_TRUE;
77   mLinkStatus                             = GL_TRUE;
78   mGetErrorResult                         = 0;
79   mGetStringResult                        = NULL;
80   mIsBufferResult                         = 0;
81   mIsEnabledResult                        = 0;
82   mIsFramebufferResult                    = 0;
83   mIsProgramResult                        = 0;
84   mIsRenderbufferResult                   = 0;
85   mIsShaderResult                         = 0;
86   mIsTextureResult                        = 0;
87   mActiveTextureUnit                      = 0;
88   mCheckFramebufferStatusResult           = 0;
89   mFramebufferStatus                      = 0;
90   mFramebufferDepthAttached               = 0;
91   mFramebufferStencilAttached             = 0;
92   mFramebufferDepthStencilAttached        = 0;
93   mFramebufferColorAttachmentCount        = 0;
94   mFrameBufferColorStatus                 = 0;
95   mFramebufferDepthAttachmentCount        = 0;
96   mFramebufferStencilAttachmentCount      = 0;
97   mFramebufferDepthStencilAttachmentCount = 0;
98   mNumBinaryFormats                       = 0;
99   mBinaryFormats                          = 0;
100   mProgramBinaryLength                    = 0;
101
102   mVertexAttribArrayChanged = false;
103   mGetProgramBinaryCalled   = false;
104
105   mLastShaderCompiled = 0;
106   mLastClearBitMask   = 0;
107   mLastClearColor     = Color::TRANSPARENT;
108   mClearCount         = 0;
109
110   mLastBlendEquationRgb   = 0;
111   mLastBlendEquationAlpha = 0;
112   mLastBlendFuncSrcRgb    = 0;
113   mLastBlendFuncDstRgb    = 0;
114   mLastBlendFuncSrcAlpha  = 0;
115   mLastBlendFuncDstAlpha  = 0;
116   mLastAutoTextureIdUsed  = 0;
117   mNumGeneratedTextures   = 0;
118   mLastShaderIdUsed       = 0;
119   mLastProgramIdUsed      = 0;
120   mLastUniformIdUsed      = 0;
121   mLastDepthMask          = false;
122
123   mUniforms.clear();
124   mProgramUniforms1i.clear();
125   mProgramUniforms1f.clear();
126   mProgramUniforms2f.clear();
127   mProgramUniforms3f.clear();
128   mProgramUniforms4f.clear();
129
130   mAttribLocs  = {"aPosition", "aTexCoord"};
131   mAttribTypes = {GL_FLOAT, GL_FLOAT};
132
133   mCullFaceTrace.Reset();
134   mDepthFunctionTrace.Reset();
135   mEnableDisableTrace.Reset();
136   mShaderTrace.Reset();
137   mStencilFunctionTrace.Reset();
138   mScissorTrace.Reset();
139   mTextureTrace.Reset();
140   mTexParameterTrace.Reset();
141   mDrawTrace.Reset();
142
143   for(unsigned int i = 0; i < MAX_ATTRIBUTE_CACHE_SIZE; ++i)
144   {
145     mVertexAttribArrayState[i] = false;
146   }
147
148   mActiveUniforms = std::vector<ActiveUniform>{
149     {"uRendererColor", GL_FLOAT, 1},
150     {"uCustom", GL_FLOAT_VEC3, 1},
151     {"uCustom3", GL_FLOAT_VEC3, 1},
152     {"uFadeColor", GL_FLOAT_VEC4, 1},
153     {"uUniform1", GL_FLOAT_VEC4, 1},
154     {"uUniform2", GL_FLOAT_VEC4, 1},
155     {"uUniform3", GL_FLOAT_VEC4, 1},
156     {"uFadeProgress", GL_FLOAT, 1},
157     {"uANormalMatrix", GL_FLOAT_MAT3, 1},
158     {"sEffect", GL_SAMPLER_2D, 1},
159     {"sTexture", GL_SAMPLER_2D, 1},
160     {"sTextureRect", GL_SAMPLER_2D, 1},
161     {"sGloss", GL_SAMPLER_2D, 1},
162     {"uColor", GL_FLOAT_VEC4, 1},
163     {"uActorColor", GL_FLOAT_VEC4, 1},
164     {"uModelMatrix", GL_FLOAT_MAT4, 1},
165     {"uModelView", GL_FLOAT_MAT4, 1},
166     {"uMvpMatrix", GL_FLOAT_MAT4, 1},
167     {"uNormalMatrix", GL_FLOAT_MAT4, 1},
168     {"uProjection", GL_FLOAT_MAT4, 1},
169     {"uScale", GL_FLOAT_VEC3, 1},
170     {"uSize", GL_FLOAT_VEC3, 1},
171     {"uViewMatrix", GL_FLOAT_MAT4, 1},
172     {"uLightCameraProjectionMatrix", GL_FLOAT_MAT4, 1},
173     {"uLightCameraViewMatrix", GL_FLOAT_MAT4, 1}};
174
175   int offset = 0;
176   for(uint32_t i = 0; i < mActiveUniforms.size(); ++i)
177   {
178     mActiveUniforms[i].offset = offset;
179     offset += mActiveUniforms[i].size * GetGLDataTypeSize(mActiveUniforms[i].type);
180   }
181   // WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE UNIFORMS IN test-graphics-reflection.cpp
182 }
183
184 void TestGlAbstraction::PreRender()
185 {
186 }
187
188 void TestGlAbstraction::PostRender()
189 {
190 }
191
192 bool TestGlAbstraction::IsSurfacelessContextSupported() const
193 {
194   return true;
195 }
196
197 bool TestGlAbstraction::IsAdvancedBlendEquationSupported()
198 {
199   return true;
200 }
201
202 bool TestGlAbstraction::IsMultisampledRenderToTextureSupported()
203 {
204   return true;
205 }
206
207 bool TestGlAbstraction::IsBlendEquationSupported(DevelBlendEquation::Type blendEquation)
208 {
209   return true;
210 }
211
212 std::string TestGlAbstraction::GetShaderVersionPrefix()
213 {
214   return std::string("");
215 }
216
217 std::string TestGlAbstraction::GetVertexShaderPrefix()
218 {
219   return std::string("");
220 }
221
222 std::string TestGlAbstraction::GetFragmentShaderPrefix()
223 {
224   return std::string("");
225 }
226
227 bool TestGlAbstraction::TextureRequiresConverting(const GLenum imageGlFormat, const GLenum textureGlFormat, const bool isSubImage) const
228 {
229   return ((imageGlFormat == GL_RGB) && (textureGlFormat == GL_RGBA));
230 }
231
232 void TestGlAbstraction::SetActiveUniforms(const std::vector<ActiveUniform>& uniforms)
233 {
234   mActiveUniforms = uniforms;
235   int offset      = 0;
236   for(uint32_t i = 0; i < uniforms.size(); ++i)
237   {
238     mActiveUniforms[i].offset = offset;
239     offset += mActiveUniforms[i].size * GetGLDataTypeSize(mActiveUniforms[i].type);
240   }
241 }
242
243 } // namespace Dali
244
245 bool BlendEnabled(const Dali::TraceCallStack& callStack)
246 {
247   std::stringstream out;
248   out << GL_BLEND;
249   bool blendEnabled = callStack.FindMethodAndParams("Enable", out.str());
250   return blendEnabled;
251 }
252
253 bool BlendDisabled(const Dali::TraceCallStack& callStack)
254 {
255   std::stringstream out;
256   out << GL_BLEND;
257   bool blendEnabled = callStack.FindMethodAndParams("Disable", out.str());
258   return blendEnabled;
259 }