#define TEST_GL_ABSTRACTION_H
/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
if(it2 == uniformIDs.end())
{
// Uniform not found, so add it...
- uniformIDs[name] = mLastUniformIdUsed++;
- return mLastUniformIdUsed;
+ uniformIDs[name] = ++mLastUniformIdUsed;
+ return uniformIDs[name];
}
return it2->second;
for(const auto& uniform : mCustomUniformData)
{
- GetUniformLocation(program, uniform.name.c_str());
+ auto iter = uniform.name.find("[");
+ auto name = uniform.name;
+ if(iter != std::string::npos)
+ {
+ name = uniform.name.substr(0, iter);
+ auto arrayCount = std::stoi(uniform.name.substr(iter + 1));
+ for(int i = 0; i < arrayCount; ++i)
+ {
+ std::stringstream nss;
+ nss << name << "[" << i << "]";
+ GetUniformLocation(program, nss.str().c_str()); // Generate a GL loc per element
+ }
+ }
+ else
+ {
+ GetUniformLocation(program, name.c_str());
+ }
}
}
void TestGraphicsBuffer::BindAsUniformBuffer(const TestGraphicsProgram* program, const Dali::UniformBufferBindingDescriptor& uboBinding) const
{
- auto* reflection = static_cast<const TestGraphicsReflection*>(&program->GetReflection());
-
- Graphics::UniformBlockInfo uboInfo{};
- reflection->GetUniformBlock(0, uboInfo);
+ auto* reflection = static_cast<const TestGraphicsReflection*>(&program->GetReflection());
+ const auto& uboInfo = reflection->GetTestUniformBlock(0u);
auto offset = uboBinding.offset;
auto* data = memory.data() + offset;
for(const auto& member : uboInfo.members)
{
- auto type = reflection->GetMemberType(0, member.location);
- switch(type)
+ uint32_t numElements = member.numElements > 0 ? member.numElements : 1;
+
+ for(uint32_t i = 0; i < numElements; ++i)
{
- case Property::VECTOR4:
- {
- auto value = *reinterpret_cast<const Dali::Vector4*>(data + member.offset);
- mGl.Uniform4f(member.location, value.x, value.y, value.z, value.w);
- break;
- }
- case Property::VECTOR3:
- {
- auto value = *reinterpret_cast<const Dali::Vector3*>(data + member.offset);
- mGl.Uniform3f(member.location, value.x, value.y, value.z);
- break;
- }
- case Property::VECTOR2:
- {
- auto value = *reinterpret_cast<const Dali::Vector2*>(data + member.offset);
- mGl.Uniform2f(member.location, value.x, value.y);
- break;
- }
- case Property::FLOAT:
- {
- auto value = *reinterpret_cast<const float*>(data + member.offset);
- mGl.Uniform1f(member.location, value);
- break;
- }
- case Property::INTEGER:
- {
- auto ptr = reinterpret_cast<const GLint*>(data + member.offset);
- auto value = *ptr;
- mGl.Uniform1i(member.location, value);
- break;
- }
- case Property::MATRIX:
- {
- auto value = reinterpret_cast<const float*>(data + member.offset);
- mGl.UniformMatrix4fv(member.location, 1, GL_FALSE, value);
- break;
- }
- case Property::MATRIX3:
- {
- auto value = reinterpret_cast<const float*>(data + member.offset);
- mGl.UniformMatrix3fv(member.location, 1, GL_FALSE, value);
- break;
- }
- default:
+ switch(member.type)
{
- fprintf(stderr, "\n%s type not found\n", member.name.c_str());
+ case Property::VECTOR4:
+ {
+ auto value = *reinterpret_cast<const Dali::Vector4*>(data + member.offsets[i]);
+ mGl.Uniform4f(member.locations[i], value.x, value.y, value.z, value.w);
+ break;
+ }
+ case Property::VECTOR3:
+ {
+ auto value = *reinterpret_cast<const Dali::Vector3*>(data + member.offsets[i]);
+ mGl.Uniform3f(member.locations[i], value.x, value.y, value.z);
+ break;
+ }
+ case Property::VECTOR2:
+ {
+ auto value = *reinterpret_cast<const Dali::Vector2*>(data + member.offsets[i]);
+ mGl.Uniform2f(member.locations[i], value.x, value.y);
+ break;
+ }
+ case Property::FLOAT:
+ {
+ auto value = *reinterpret_cast<const float*>(data + member.offsets[i]);
+ mGl.Uniform1f(member.locations[i], value);
+ break;
+ }
+ case Property::INTEGER:
+ {
+ auto ptr = reinterpret_cast<const GLint*>(data + member.offsets[i]);
+ auto value = *ptr;
+ mGl.Uniform1i(member.locations[i], value);
+ break;
+ }
+ case Property::MATRIX:
+ {
+ auto value = reinterpret_cast<const float*>(data + member.offsets[i]);
+ mGl.UniformMatrix4fv(member.locations[i], 1, GL_FALSE, value);
+ break;
+ }
+ case Property::MATRIX3:
+ {
+ auto value = reinterpret_cast<const float*>(data + member.offsets[i]);
+ mGl.UniformMatrix3fv(member.locations[i], 1, GL_FALSE, value);
+ break;
+ }
+ default:
+ {
+ fprintf(stderr, "\n%s type not found\n", member.name.c_str());
+ }
}
}
}
/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
{
TestGraphicsProgramImpl::TestGraphicsProgramImpl(TestGlAbstraction& gl, const Graphics::ProgramCreateInfo& createInfo, Property::Array& vertexFormats, std::vector<UniformData>& customUniforms)
: mGl(gl),
+ mId(gl.CreateProgram()),
mCreateInfo(createInfo),
- mReflection(gl, vertexFormats, createInfo, customUniforms)
+ mReflection(gl, mId, vertexFormats, createInfo, customUniforms)
{
- mId = mGl.CreateProgram();
-
// Ensure active sampler uniforms are set
mGl.SetCustomUniforms(customUniforms);
- mGl.LinkProgram(mId);
+
+ // Don't need to re-register uniforms in GL side - now done in creation of mReflection.
+ // Was previously done in mGl.LinkProgram(mId);
}
bool TestGraphicsProgramImpl::GetParameter(uint32_t parameterId, void* outData)
// WARNING: IF YOU CHANGE THIS LIST, ALSO CHANGE mActiveUniforms IN test-gl-abstraction, Initialize
};
+
+/**
+ * Helper function that returns size of uniform datatypes based
+ * on property type.
+ */
+constexpr int GetSizeForType(Property::Type type)
+{
+ switch(type)
+ {
+ case Property::Type::BOOLEAN:
+ {
+ return sizeof(bool);
+ }
+ case Property::Type::FLOAT:
+ {
+ return sizeof(float);
+ }
+ case Property::Type::INTEGER:
+ {
+ return sizeof(int);
+ }
+ case Property::Type::VECTOR2:
+ {
+ return sizeof(Vector2);
+ }
+ case Property::Type::VECTOR3:
+ {
+ return sizeof(Vector3);
+ }
+ case Property::Type::VECTOR4:
+ {
+ return sizeof(Vector4);
+ }
+ case Property::Type::MATRIX3:
+ {
+ return sizeof(Matrix3);
+ }
+ case Property::Type::MATRIX:
+ {
+ return sizeof(Matrix);
+ }
+ default:
+ {
+ return 0;
+ }
+ };
}
-TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, Property::Array& vfs, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms)
+} // namespace
+
+TestGraphicsReflection::TestGraphicsReflection(TestGlAbstraction& gl, uint32_t programId, Property::Array& vfs, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms)
: mGl(gl),
mCustomUniforms(customUniforms)
{
mDefaultUniformBlock.name = "";
mDefaultUniformBlock.members = {};
mDefaultUniformBlock.binding = 0;
- mDefaultUniformBlock.size = 64 * (UNIFORMS.size() + mCustomUniforms.size());
mDefaultUniformBlock.descriptorSet = 0;
mDefaultUniformBlock.members.clear();
- int loc = 0;
+
+ int offset = 0;
for(const auto& data : UNIFORMS)
{
mDefaultUniformBlock.members.emplace_back();
- auto& item = mDefaultUniformBlock.members.back();
- item.name = data.name;
- item.binding = 0;
- item.offset = loc * 64;
- item.location = loc++;
+ auto& item = mDefaultUniformBlock.members.back();
+ item.name = data.name;
+ item.binding = 0;
+ item.offsets.push_back(offset);
+ item.locations.push_back(gl.GetUniformLocation(programId, data.name.c_str()));
item.bufferIndex = 0;
item.uniformClass = Graphics::UniformClass::UNIFORM;
+ item.type = data.type;
+ offset += GetSizeForType(data.type);
}
for(const auto& data : mCustomUniforms)
{
fprintf(stderr, "\ncustom uniforms: %s\n", data.name.c_str());
mDefaultUniformBlock.members.emplace_back();
- auto& item = mDefaultUniformBlock.members.back();
- item.name = data.name;
- item.binding = 0;
- item.offset = loc * 64;
- item.location = loc++;
- item.bufferIndex = 0;
- item.uniformClass = Graphics::UniformClass::UNIFORM;
+ auto& item = mDefaultUniformBlock.members.back();
+
+ auto iter = data.name.find("[", 0);
+ int numElements = 1;
+ if(iter != std::string::npos)
+ {
+ auto baseName = data.name.substr(0, iter);
+ iter++;
+ numElements = std::stoi(data.name.substr(iter));
+ if(numElements == 0)
+ {
+ numElements = 1;
+ }
+
+ item.name = baseName;
+ item.binding = 0;
+ item.bufferIndex = 0;
+ item.uniformClass = Graphics::UniformClass::UNIFORM;
+ item.type = data.type;
+ item.numElements = numElements;
+
+ for(int i = 0; i < numElements; ++i)
+ {
+ std::stringstream elementNameStream;
+ elementNameStream << baseName << "[" << i << "]";
+
+ item.locations.push_back(gl.GetUniformLocation(programId, elementNameStream.str().c_str()));
+ item.offsets.push_back(offset);
+ offset += GetSizeForType(data.type);
+ }
+ }
+ else
+ {
+ item.name = data.name;
+ item.binding = 0;
+ item.offsets.push_back(offset);
+ item.locations.push_back(gl.GetUniformLocation(programId, item.name.c_str()));
+ item.bufferIndex = 0;
+ item.uniformClass = Graphics::UniformClass::UNIFORM;
+ item.type = data.type;
+ offset += GetSizeForType(data.type);
+ }
}
+ mDefaultUniformBlock.size = offset;
mUniformBlocks.push_back(mDefaultUniformBlock);
}
uint32_t TestGraphicsReflection::GetUniformBlockSize(uint32_t index) const
{
- // 64 bytes per uniform (64 = 4x4 matrix)
- // TODO: fix if array will be used
- return 64 * (UNIFORMS.size() + mCustomUniforms.size());
+ if(index >= mUniformBlocks.size())
+ {
+ return 0;
+ }
+
+ const auto& block = mUniformBlocks[index];
+ return block.size;
}
bool TestGraphicsReflection::GetUniformBlock(uint32_t index, Dali::Graphics::UniformBlockInfo& out) const
out.members[i].name = memberUniform.name;
out.members[i].binding = block.binding;
out.members[i].uniformClass = Graphics::UniformClass::UNIFORM;
- out.members[i].offset = memberUniform.offset;
- out.members[i].location = memberUniform.location;
+ out.members[i].offset = memberUniform.offsets[0];
+ out.members[i].location = memberUniform.locations[0];
}
return true;
{
if(blockIndex < mUniformBlocks.size() && memberLocation < mUniformBlocks[blockIndex].members.size())
{
- return mUniformBlocks[blockIndex].members[memberLocation].offset;
+ return mUniformBlocks[blockIndex].members[memberLocation].offsets[0];
}
else
{
return Graphics::ShaderLanguage::GLSL_3_1;
}
-Dali::Property::Type TestGraphicsReflection::GetMemberType(int blockIndex, int location) const
-{
- return location < static_cast<int>(UNIFORMS.size()) ? UNIFORMS[location].type : mCustomUniforms[location - UNIFORMS.size()].type;
-}
-
} // namespace Dali
#define DALI_TEST_GRAPHICS_REFLECTION_H
/*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
class TestGraphicsReflection : public Graphics::Reflection
{
public:
- TestGraphicsReflection(TestGlAbstraction& gl, Property::Array& vertexFormats, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms);
+ TestGraphicsReflection(TestGlAbstraction& gl, uint32_t program_id, Property::Array& vertexFormats, const Graphics::ProgramCreateInfo& createInfo, std::vector<UniformData>& customUniforms);
uint32_t GetVertexAttributeLocation(const std::string& name) const override;
Dali::Graphics::VertexInputAttributeFormat GetVertexAttributeFormat(uint32_t location) const override;
Graphics::ShaderLanguage GetLanguage() const override;
public: // Test methods
+ struct TestUniformInfo
+ {
+ std::string name{""}; // baseName in the case of arrays
+ Graphics::UniformClass uniformClass{Graphics::UniformClass::UNDEFINED};
+ uint32_t binding{0u};
+ uint32_t bufferIndex{0u};
+ std::vector<uint32_t> offsets{};
+ std::vector<uint32_t> locations{};
+ uint32_t numElements{0u}; // 0 elements means this isn't an array; 1 element means this is an array of size 1
+ Property::Type type;
+ };
+
+ struct TestUniformBlockInfo
+ {
+ std::string name{""};
+ uint32_t descriptorSet{0u};
+ uint32_t binding{0u};
+ uint32_t size{0u};
+ std::vector<TestUniformInfo> members{};
+ };
+
void SetAttributes(std::vector<std::string> locations)
{
mAttributes.clear();
}
}
- Dali::Property::Type GetMemberType(int blockIndex, int location) const;
+ const TestUniformBlockInfo& GetTestUniformBlock(uint32_t index) const
+ {
+ return mUniformBlocks[index];
+ }
TestGlAbstraction& mGl;
mutable std::vector<std::string> mAttributes;
std::vector<UniformData> mCustomUniforms;
- Graphics::UniformBlockInfo mDefaultUniformBlock{}; ///< The emulated UBO containing all the standalone uniforms
- std::vector<Graphics::UniformBlockInfo> mUniformBlocks{}; ///< List of uniform blocks
+ TestUniformBlockInfo mDefaultUniformBlock{}; ///< The emulated UBO containing all the standalone uniforms
+ std::vector<TestUniformBlockInfo> mUniformBlocks{}; ///< List of uniform blocks
};
} // namespace Dali