2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "test-graphics-buffer.h"
19 #include "dali-test-suite-utils.h"
20 #include "test-graphics-program.h"
21 #include "test-graphics-reflection.h"
25 TestGraphicsBuffer::TestGraphicsBuffer(TraceCallStack& callStack, TestGlAbstraction& glAbstraction, uint32_t size, Graphics::BufferUsageFlags usage)
26 : mCallStack(callStack),
31 mGl.GetBufferTrace().EnableLogging(true);
34 void TestGraphicsBuffer::Bind()
36 mCallStack.PushCall("Buffer::Bind", "");
39 mGl.GenBuffers(1, &mId);
41 mGl.BindBuffer(GetTarget(), mId);
44 void TestGraphicsBuffer::Unbind()
46 mCallStack.PushCall("Buffer::Unbind", "");
49 mGl.BindBuffer(GetTarget(), 0);
53 void TestGraphicsBuffer::Upload(uint32_t offset, uint32_t size)
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)
63 // Use subData to avoid re-allocation
64 mGl.BufferSubData(GetTarget(), offset, size, &memory[offset]);
68 mGl.BufferData(GetTarget(), size, &memory[0], GL_STATIC_DRAW); //@todo Query - do we need other usages?
73 GLenum TestGraphicsBuffer::GetTarget()
75 GLenum target = GL_ARRAY_BUFFER;
76 if((mUsage & static_cast<Graphics::BufferUsageFlags>(Graphics::BufferUsage::INDEX_BUFFER)) != 0)
78 target = GL_ELEMENT_ARRAY_BUFFER;
83 void TestGraphicsBuffer::BindAsUniformBuffer(const TestGraphicsProgram* program, const Dali::UniformBufferBindingDescriptor& uboBinding) const
85 auto* reflection = static_cast<const TestGraphicsReflection*>(&program->GetReflection());
87 Graphics::UniformBlockInfo uboInfo{};
88 reflection->GetUniformBlock(0, uboInfo);
90 auto offset = uboBinding.offset;
91 auto* data = memory.data() + offset;
93 for(const auto& member : uboInfo.members)
95 auto type = reflection->GetMemberType(0, member.location);
98 case Property::VECTOR4:
100 auto value = *reinterpret_cast<const Dali::Vector4*>(data + member.offset);
101 mGl.Uniform4f(member.location, value.x, value.y, value.z, value.w);
104 case Property::VECTOR3:
106 auto value = *reinterpret_cast<const Dali::Vector3*>(data + member.offset);
107 mGl.Uniform3f(member.location, value.x, value.y, value.z);
110 case Property::VECTOR2:
112 auto value = *reinterpret_cast<const Dali::Vector2*>(data + member.offset);
113 mGl.Uniform2f(member.location, value.x, value.y);
116 case Property::FLOAT:
118 auto value = *reinterpret_cast<const float*>(data + member.offset);
119 mGl.Uniform1f(member.location, value);
122 case Property::INTEGER:
124 auto ptr = reinterpret_cast<const GLint*>(data + member.offset);
126 mGl.Uniform1i(member.location, value);
129 case Property::MATRIX:
131 auto value = reinterpret_cast<const float*>(data + member.offset);
132 mGl.UniformMatrix4fv(member.location, 1, GL_FALSE, value);
135 case Property::MATRIX3:
137 auto value = reinterpret_cast<const float*>(data + member.offset);
138 mGl.UniformMatrix3fv(member.location, 1, GL_FALSE, value);
143 fprintf(stderr, "\n%s type not found\n", member.name.c_str());