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