Re-instating test cases from old graphics backend
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-buffer.cpp
1 /*
2  * Copyright (c) 2022 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 #include "test-graphics-buffer.h"
18 #include <sstream>
19 #include "dali-test-suite-utils.h"
20 #include "test-graphics-program.h"
21 #include "test-graphics-reflection.h"
22
23 namespace Dali
24 {
25 TestGraphicsBuffer::TestGraphicsBuffer(TraceCallStack& callStack, TestGlAbstraction& glAbstraction, uint32_t size, Graphics::BufferUsageFlags usage)
26 : mCallStack(callStack),
27   mGl(glAbstraction),
28   mUsage(usage)
29 {
30   memory.resize(size);
31   mGl.GetBufferTrace().EnableLogging(false);
32 }
33
34 void TestGraphicsBuffer::Bind()
35 {
36   mCallStack.PushCall("Buffer::Bind", "");
37   if(!mId)
38   {
39     mGl.GenBuffers(1, &mId);
40   }
41   mGl.BindBuffer(GetTarget(), mId);
42 }
43
44 void TestGraphicsBuffer::Unbind()
45 {
46   mCallStack.PushCall("Buffer::Unbind", "");
47   if(mId)
48   {
49     mGl.BindBuffer(GetTarget(), 0);
50   }
51 }
52
53 void TestGraphicsBuffer::Upload(uint32_t offset, uint32_t size)
54 {
55   std::ostringstream o;
56   o << offset << ", " << size;
57   TraceCallStack::NamedParams namedParams;
58   namedParams["offset"] << offset;
59   namedParams["size"] << size;
60   mCallStack.PushCall("Buffer::Upload", o.str(), namedParams);
61   if(size <= memory.size() && mCreated)
62   {
63     // Use subData to avoid re-allocation
64     mGl.BufferSubData(GetTarget(), offset, size, &memory[offset]);
65   }
66   else
67   {
68     mGl.BufferData(GetTarget(), GLsizeiptr(size), &memory[0], GL_STATIC_DRAW); //@todo Query - do we need other usages?
69     mCreated = true;
70   }
71 }
72
73 GLenum TestGraphicsBuffer::GetTarget()
74 {
75   GLenum target = GL_ARRAY_BUFFER;
76   if((mUsage & static_cast<Graphics::BufferUsageFlags>(Graphics::BufferUsage::INDEX_BUFFER)) != 0)
77   {
78     target = GL_ELEMENT_ARRAY_BUFFER;
79   }
80   return target;
81 }
82
83 void TestGraphicsBuffer::BindAsUniformBuffer(const TestGraphicsProgram* program, const Dali::UniformBufferBindingDescriptor& uboBinding) const
84 {
85   auto*       reflection = static_cast<const TestGraphicsReflection*>(&program->GetReflection());
86   const auto& uboInfo    = reflection->GetTestUniformBlock(0u);
87
88   auto  offset = uboBinding.offset;
89   auto* data   = memory.data() + offset;
90
91   for(const auto& member : uboInfo.members)
92   {
93     uint32_t numElements = member.numElements > 0 ? member.numElements : 1;
94
95     for(uint32_t i = 0; i < numElements; ++i)
96     {
97       switch(member.type)
98       {
99         case Property::VECTOR4:
100         {
101           auto value = *reinterpret_cast<const Dali::Vector4*>(data + member.offsets[i]);
102           mGl.Uniform4f(member.locations[i], value.x, value.y, value.z, value.w);
103           break;
104         }
105         case Property::VECTOR3:
106         {
107           auto value = *reinterpret_cast<const Dali::Vector3*>(data + member.offsets[i]);
108           mGl.Uniform3f(member.locations[i], value.x, value.y, value.z);
109           break;
110         }
111         case Property::VECTOR2:
112         {
113           auto value = *reinterpret_cast<const Dali::Vector2*>(data + member.offsets[i]);
114           mGl.Uniform2f(member.locations[i], value.x, value.y);
115           break;
116         }
117         case Property::FLOAT:
118         {
119           auto value = *reinterpret_cast<const float*>(data + member.offsets[i]);
120           mGl.Uniform1f(member.locations[i], value);
121           break;
122         }
123         case Property::INTEGER:
124         {
125           auto ptr   = reinterpret_cast<const GLint*>(data + member.offsets[i]);
126           auto value = *ptr;
127           mGl.Uniform1i(member.locations[i], value);
128           break;
129         }
130         case Property::MATRIX:
131         {
132           auto value = reinterpret_cast<const float*>(data + member.offsets[i]);
133           mGl.UniformMatrix4fv(member.locations[i], 1, GL_FALSE, value);
134           break;
135         }
136         case Property::MATRIX3:
137         {
138           auto value = reinterpret_cast<const float*>(data + member.offsets[i]);
139           mGl.UniformMatrix3fv(member.locations[i], 1, GL_FALSE, value);
140           break;
141         }
142         default:
143         {
144           fprintf(stderr, "\n%s type not found\n", member.name.c_str());
145         }
146       }
147     }
148   }
149 }
150
151 } // namespace Dali