2 * Copyright (c) 2022 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(false);
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(), GLsizeiptr(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());
86 const auto& uboInfo = reflection->GetTestUniformBlock(0u);
88 auto offset = uboBinding.offset;
89 auto* data = memory.data() + offset;
91 for(const auto& member : uboInfo.members)
93 uint32_t numElements = member.numElements > 0 ? member.numElements : 1;
95 for(uint32_t i = 0; i < numElements; ++i)
99 case Property::VECTOR4:
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);
105 case Property::VECTOR3:
107 auto value = *reinterpret_cast<const Dali::Vector3*>(data + member.offsets[i]);
108 mGl.Uniform3f(member.locations[i], value.x, value.y, value.z);
111 case Property::VECTOR2:
113 auto value = *reinterpret_cast<const Dali::Vector2*>(data + member.offsets[i]);
114 mGl.Uniform2f(member.locations[i], value.x, value.y);
117 case Property::FLOAT:
119 auto value = *reinterpret_cast<const float*>(data + member.offsets[i]);
120 mGl.Uniform1f(member.locations[i], value);
123 case Property::INTEGER:
125 auto ptr = reinterpret_cast<const GLint*>(data + member.offsets[i]);
127 mGl.Uniform1i(member.locations[i], value);
130 case Property::MATRIX:
132 auto value = reinterpret_cast<const float*>(data + member.offsets[i]);
133 mGl.UniformMatrix4fv(member.locations[i], 1, GL_FALSE, value);
136 case Property::MATRIX3:
138 auto value = reinterpret_cast<const float*>(data + member.offsets[i]);
139 mGl.UniformMatrix3fv(member.locations[i], 1, GL_FALSE, value);
144 fprintf(stderr, "\n%s type not found\n", member.name.c_str());