DALi Version 2.0.37
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-graphics-buffer.cpp
1 /*
2  * Copyright (c) 2021 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(true);
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(), 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
87   Graphics::UniformBlockInfo uboInfo{};
88   reflection->GetUniformBlock(0, uboInfo);
89
90   auto  offset = uboBinding.offset;
91   auto* data   = memory.data() + offset;
92
93   for(const auto& member : uboInfo.members)
94   {
95     auto type = reflection->GetMemberType(0, member.location);
96     switch(type)
97     {
98       case Property::VECTOR4:
99       {
100         auto value = *reinterpret_cast<const Dali::Vector4*>(data + member.offset);
101         mGl.Uniform4f(member.location, value.x, value.y, value.z, value.w);
102         break;
103       }
104       case Property::VECTOR3:
105       {
106         auto value = *reinterpret_cast<const Dali::Vector3*>(data + member.offset);
107         mGl.Uniform3f(member.location, value.x, value.y, value.z);
108         break;
109       }
110       case Property::VECTOR2:
111       {
112         auto value = *reinterpret_cast<const Dali::Vector2*>(data + member.offset);
113         mGl.Uniform2f(member.location, value.x, value.y);
114         break;
115       }
116       case Property::FLOAT:
117       {
118         auto value = *reinterpret_cast<const float*>(data + member.offset);
119         mGl.Uniform1f(member.location, value);
120         break;
121       }
122       case Property::INTEGER:
123       {
124         auto ptr   = reinterpret_cast<const GLint*>(data + member.offset);
125         auto value = *ptr;
126         mGl.Uniform1i(member.location, value);
127         break;
128       }
129       case Property::MATRIX:
130       {
131         auto value = reinterpret_cast<const float*>(data + member.offset);
132         mGl.UniformMatrix4fv(member.location, 1, GL_FALSE, value);
133         break;
134       }
135       case Property::MATRIX3:
136       {
137         auto value = reinterpret_cast<const float*>(data + member.offset);
138         mGl.UniformMatrix3fv(member.location, 1, GL_FALSE, value);
139         break;
140       }
141       default:
142       {
143         fprintf(stderr, "\n%s type not found\n", member.name.c_str());
144       }
145     }
146   }
147 }
148
149 } // namespace Dali