From 9abe4409cba944399969ca0e4b1e2c7bab14e033 Mon Sep 17 00:00:00 2001 From: m0dB Date: Tue, 24 Sep 2024 18:55:08 +0200 Subject: [PATCH] removed class Attribute and use BaseGeometry::Attribute to reduce delta between opengl and scenegraph --- src/rendergraph/common/attributeset.cpp | 12 ----- .../common/rendergraph/attribute.h | 26 ---------- .../common/rendergraph/attributeinit.h | 17 +++++++ .../common/rendergraph/attributeset.h | 6 +-- src/rendergraph/common/rendergraph/geometry.h | 21 ++++++-- src/rendergraph/opengl/CMakeLists.txt | 4 +- src/rendergraph/opengl/attributeset.cpp | 8 +++ .../opengl/backend/baseattributeset.cpp | 8 ++- .../opengl/backend/baseattributeset.h | 18 +++++-- .../opengl/backend/basegeometry.cpp | 46 +++++------------ src/rendergraph/opengl/backend/basegeometry.h | 49 ++++++++++++++----- .../opengl/backend/basegeometrynode.cpp | 10 ++-- src/rendergraph/opengl/geometry.cpp | 34 +++++-------- src/rendergraph/scenegraph/CMakeLists.txt | 4 +- src/rendergraph/scenegraph/attributeset.cpp | 10 ++++ .../scenegraph/backend/baseattributeset.cpp | 16 ++---- .../scenegraph/backend/baseattributeset.h | 29 +++++------ .../scenegraph/backend/basegeometry.h | 12 +---- src/rendergraph/scenegraph/geometry.cpp | 26 ++++------ 19 files changed, 177 insertions(+), 179 deletions(-) delete mode 100644 src/rendergraph/common/attributeset.cpp delete mode 100644 src/rendergraph/common/rendergraph/attribute.h create mode 100644 src/rendergraph/common/rendergraph/attributeinit.h create mode 100644 src/rendergraph/opengl/attributeset.cpp create mode 100644 src/rendergraph/scenegraph/attributeset.cpp diff --git a/src/rendergraph/common/attributeset.cpp b/src/rendergraph/common/attributeset.cpp deleted file mode 100644 index a2fef348d9c0..000000000000 --- a/src/rendergraph/common/attributeset.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "rendergraph/attributeset.h" - -using namespace rendergraph; - -AttributeSet::AttributeSet(std::initializer_list list, - const std::vector& names) - : BaseAttributeSet(list, names) { -} - -const std::vector& AttributeSet::attributes() const { - return m_attributes; -} diff --git a/src/rendergraph/common/rendergraph/attribute.h b/src/rendergraph/common/rendergraph/attribute.h deleted file mode 100644 index 150ad00c3aaf..000000000000 --- a/src/rendergraph/common/rendergraph/attribute.h +++ /dev/null @@ -1,26 +0,0 @@ -#pragma once - -#include - -#include "rendergraph/types.h" - -namespace rendergraph { -struct Attribute; -} - -struct rendergraph::Attribute { - const int m_tupleSize; - const PrimitiveType m_primitiveType; - const QString m_name; - - Attribute(int tupleSize, PrimitiveType primitiveType, QString name = {}) - : m_tupleSize{tupleSize}, - m_primitiveType{primitiveType}, - m_name{std::move(name)} { - } - - template - static Attribute create() { - return Attribute(tupleSizeOf(), primitiveTypeOf()); - } -}; diff --git a/src/rendergraph/common/rendergraph/attributeinit.h b/src/rendergraph/common/rendergraph/attributeinit.h new file mode 100644 index 000000000000..db1a27970906 --- /dev/null +++ b/src/rendergraph/common/rendergraph/attributeinit.h @@ -0,0 +1,17 @@ +#pragma once + +#include "rendergraph/types.h" + +namespace rendergraph { +struct AttributeInit; +} + +struct rendergraph::AttributeInit { + int m_tupleSize; + PrimitiveType m_primitiveType; + + template + static AttributeInit create() { + return AttributeInit{tupleSizeOf(), primitiveTypeOf()}; + } +}; diff --git a/src/rendergraph/common/rendergraph/attributeset.h b/src/rendergraph/common/rendergraph/attributeset.h index 589a6e51bfc3..b79833934fe2 100644 --- a/src/rendergraph/common/rendergraph/attributeset.h +++ b/src/rendergraph/common/rendergraph/attributeset.h @@ -1,6 +1,7 @@ #pragma once #include "backend/baseattributeset.h" +#include "rendergraph/attributeinit.h" namespace rendergraph { class AttributeSet; @@ -8,13 +9,12 @@ class AttributeSet; class rendergraph::AttributeSet : public rendergraph::BaseAttributeSet { public: - AttributeSet(std::initializer_list list, const std::vector& names); - const std::vector& attributes() const; + AttributeSet(std::initializer_list list, const std::vector& names); }; namespace rendergraph { template AttributeSet makeAttributeSet(const std::vector& names) { - return AttributeSet({(Attribute::create())...}, names); + return AttributeSet({(AttributeInit::create())...}, names); } } // namespace rendergraph diff --git a/src/rendergraph/common/rendergraph/geometry.h b/src/rendergraph/common/rendergraph/geometry.h index c44572c3b9a2..398c94937150 100644 --- a/src/rendergraph/common/rendergraph/geometry.h +++ b/src/rendergraph/common/rendergraph/geometry.h @@ -52,13 +52,27 @@ class rendergraph::Geometry : public rendergraph::BaseGeometry { }; Geometry(const rendergraph::AttributeSet& attributeSet, int vertexCount); - ~Geometry(); - void allocate(int vertexCount); + const Attribute* attributes() const { + return BaseGeometry::attributes(); + } void setAttributeValues(int attributePosition, const float* data, int numTuples); - float* vertexData(); + int attributeCount() const { + return BaseGeometry::attributeCount(); + } + + void allocate(int vertexCount) { + BaseGeometry::allocate(vertexCount); + } + + int sizeOfVertex() const { + return BaseGeometry::sizeOfVertex(); + } + int vertexCount() const { + return BaseGeometry::vertexCount(); + } template T* vertexDataAs() { @@ -66,5 +80,6 @@ class rendergraph::Geometry : public rendergraph::BaseGeometry { } DrawingMode drawingMode() const; + void setDrawingMode(DrawingMode mode); }; diff --git a/src/rendergraph/opengl/CMakeLists.txt b/src/rendergraph/opengl/CMakeLists.txt index 968b690e3daf..a6f1dce0a429 100644 --- a/src/rendergraph/opengl/CMakeLists.txt +++ b/src/rendergraph/opengl/CMakeLists.txt @@ -1,9 +1,8 @@ add_library(rendergraph_gl -../common/attributeset.cpp ../common/treenode.cpp ../common/node.cpp ../common/opacitynode.cpp -../common/rendergraph/attribute.h +../common/rendergraph/attributeinit.h ../common/rendergraph/attributeset.h ../common/rendergraph/treenode.h ../common/rendergraph/geometry.h @@ -49,6 +48,7 @@ backend/baseopenglnode.cpp backend/baseopenglnode.h backend/shadercache.h backend/basetexture.h +attributeset.cpp treenode.cpp engine.cpp geometry.cpp diff --git a/src/rendergraph/opengl/attributeset.cpp b/src/rendergraph/opengl/attributeset.cpp new file mode 100644 index 000000000000..302c2ad5c9c4 --- /dev/null +++ b/src/rendergraph/opengl/attributeset.cpp @@ -0,0 +1,8 @@ +#include "rendergraph/attributeset.h" + +using namespace rendergraph; + +AttributeSet::AttributeSet(std::initializer_list list, + const std::vector& names) + : BaseAttributeSet(list, names) { +} diff --git a/src/rendergraph/opengl/backend/baseattributeset.cpp b/src/rendergraph/opengl/backend/baseattributeset.cpp index 9ddec2306765..87e1ea333d0d 100644 --- a/src/rendergraph/opengl/backend/baseattributeset.cpp +++ b/src/rendergraph/opengl/backend/baseattributeset.cpp @@ -3,12 +3,16 @@ using namespace rendergraph; -BaseAttributeSet::BaseAttributeSet(std::initializer_list list, +BaseAttributeSet::BaseAttributeSet(std::initializer_list list, const std::vector& names) { DEBUG_ASSERT(list.size() == names.size()); int i = 0; + int offset = 0; m_attributes.reserve(list.size()); for (auto item : list) { - m_attributes.push_back(Attribute{item.m_tupleSize, item.m_primitiveType, names[i++]}); + m_attributes.push_back(BaseGeometry::Attribute{ + offset, item.m_tupleSize, item.m_primitiveType, names[i++]}); + offset += item.m_tupleSize * sizeOf(item.m_primitiveType); } + m_sizeOfVertex = offset; } diff --git a/src/rendergraph/opengl/backend/baseattributeset.h b/src/rendergraph/opengl/backend/baseattributeset.h index 57b7fcf76ec3..c2c1c2cda2a2 100644 --- a/src/rendergraph/opengl/backend/baseattributeset.h +++ b/src/rendergraph/opengl/backend/baseattributeset.h @@ -3,7 +3,8 @@ #include #include -#include "rendergraph/attribute.h" +#include "backend/basegeometry.h" +#include "rendergraph/attributeinit.h" namespace rendergraph { class BaseAttributeSet; @@ -11,6 +12,17 @@ class BaseAttributeSet; class rendergraph::BaseAttributeSet { protected: - BaseAttributeSet(std::initializer_list list, const std::vector& names); - std::vector m_attributes; + BaseAttributeSet(std::initializer_list list, const std::vector& names); + + public: + const std::vector& attributes() const { + return m_attributes; + } + int sizeOfVertex() const { + return m_sizeOfVertex; + } + + protected: + std::vector m_attributes; + int m_sizeOfVertex; }; diff --git a/src/rendergraph/opengl/backend/basegeometry.cpp b/src/rendergraph/opengl/backend/basegeometry.cpp index 03ffd7ac9882..81471400cb64 100644 --- a/src/rendergraph/opengl/backend/basegeometry.cpp +++ b/src/rendergraph/opengl/backend/basegeometry.cpp @@ -1,41 +1,21 @@ #include "backend/basegeometry.h" +#include "backend/baseattributeset.h" #include "rendergraph/geometry.h" using namespace rendergraph; -BaseGeometry::BaseGeometry( - const AttributeSet& attributeSet, int vertexCount) - : m_drawingMode(static_cast(Geometry::DrawingMode:: - TriangleStrip)) // to mimic sg default - , - m_vertexCount(vertexCount) { - int offset = 0; - for (const auto& attribute : attributeSet.attributes()) { - m_offsets.push_back(offset); - offset += attribute.m_tupleSize; - m_tupleSizes.push_back(attribute.m_tupleSize); - } - m_stride = offset * sizeof(float); - m_data.resize(offset * vertexCount); -} - -int BaseGeometry::attributeCount() const { - return m_tupleSizes.size(); -} - -int BaseGeometry::vertexCount() const { - return m_vertexCount; -} +namespace { +// to mimic sg default +constexpr int defaultDrawingMode = static_cast(Geometry::DrawingMode::TriangleStrip); +} // namespace -int BaseGeometry::offset(int attributeIndex) const { - return m_offsets[attributeIndex]; -} - -int BaseGeometry::tupleSize(int attributeIndex) const { - return m_tupleSizes[attributeIndex]; -} - -int BaseGeometry::stride() const { - return m_stride; +BaseGeometry::BaseGeometry( + const BaseAttributeSet& attributeSet, int vertexCount) + : m_pAttributes(attributeSet.attributes().data()), + m_attributeCount(attributeSet.attributes().size()), + m_sizeOfVertex(attributeSet.sizeOfVertex()), + m_drawingMode(defaultDrawingMode), + m_vertexCount(vertexCount), + m_vertexData(m_vertexCount * m_sizeOfVertex / sizeof(float)) { } diff --git a/src/rendergraph/opengl/backend/basegeometry.h b/src/rendergraph/opengl/backend/basegeometry.h index 60281f6cb4b6..5b17f518494a 100644 --- a/src/rendergraph/opengl/backend/basegeometry.h +++ b/src/rendergraph/opengl/backend/basegeometry.h @@ -1,29 +1,56 @@ #pragma once +#include #include -#include "rendergraph/attributeset.h" +#include "rendergraph/types.h" namespace rendergraph { +class BaseAttributeSet; // fwd decl to avoid circular dependency class BaseGeometry; } +// TODO this assumes all vertices consist of floats class rendergraph::BaseGeometry { protected: - BaseGeometry(const AttributeSet& attributeSet, int vertexCount); + BaseGeometry(const BaseAttributeSet& attributeSet, int vertexCount); public: - int attributeCount() const; - int vertexCount() const; - int offset(int attributeIndex) const; - int tupleSize(int attributeIndex) const; - int stride() const; + struct Attribute { + const int m_offset; + const int m_tupleSize; + const PrimitiveType m_primitiveType; + const QString m_name; + }; + + float* vertexData() { + return m_vertexData.data(); + } + const float* vertexData() const { + return m_vertexData.data(); + } + const Attribute* attributes() const { + return m_pAttributes; + } + int attributeCount() const { + return m_attributeCount; + } + int vertexCount() const { + return m_vertexCount; + } + int sizeOfVertex() const { // in bytes + return m_sizeOfVertex; + } + void allocate(int vertexCount) { + m_vertexCount = vertexCount; + m_vertexData.resize(m_vertexCount * sizeOfVertex() / sizeof(float)); + } protected: + const Attribute* m_pAttributes; + const int m_attributeCount; + const int m_sizeOfVertex; int m_drawingMode; int m_vertexCount; - std::vector m_tupleSizes; - std::vector m_offsets; - int m_stride; - std::vector m_data; + std::vector m_vertexData; }; diff --git a/src/rendergraph/opengl/backend/basegeometrynode.cpp b/src/rendergraph/opengl/backend/basegeometrynode.cpp index 889b654deca1..14dcc4fc2e48 100644 --- a/src/rendergraph/opengl/backend/basegeometrynode.cpp +++ b/src/rendergraph/opengl/backend/basegeometrynode.cpp @@ -70,13 +70,17 @@ void BaseGeometryNode::renderBackend() { } } + // TODO this code assumes all vertices are floats + int vertexOffset = 0; for (int i = 0; i < geometry.attributeCount(); i++) { + const Geometry::Attribute& attribute = geometry.attributes()[i]; int location = material.attributeLocation(i); shader.enableAttributeArray(location); shader.setAttributeArray(location, - geometry.vertexData() + geometry.offset(i), - geometry.tupleSize(i), - geometry.stride()); + geometry.vertexDataAs() + vertexOffset, + attribute.m_tupleSize, + geometry.sizeOfVertex()); + vertexOffset += attribute.m_tupleSize; } // TODO multiple textures diff --git a/src/rendergraph/opengl/geometry.cpp b/src/rendergraph/opengl/geometry.cpp index 0c4c7434ed67..6d4a9d04c494 100644 --- a/src/rendergraph/opengl/geometry.cpp +++ b/src/rendergraph/opengl/geometry.cpp @@ -9,41 +9,33 @@ Geometry::Geometry(const AttributeSet& attributeSet, int vertexCount) : BaseGeometry(attributeSet, vertexCount) { } -Geometry::~Geometry() = default; - void Geometry::setAttributeValues(int attributePosition, const float* from, int numTuples) { - const int offset = m_offsets[attributePosition]; - const int tupleSize = m_tupleSizes[attributePosition]; - const int strideNumberOfFloats = m_stride / sizeof(float); - const int skip = strideNumberOfFloats - tupleSize; + // TODO this code assumes all vertices are floats + VERIFY_OR_DEBUG_ASSERT(attributePosition < attributeCount()) { + return; + } + const int vertexOffset = attributes()[attributePosition].m_offset / sizeof(float); + const int tupleSize = attributes()[attributePosition].m_tupleSize; + const int vertexStride = sizeOfVertex() / sizeof(float); + const int vertexSkip = vertexStride - tupleSize; - VERIFY_OR_DEBUG_ASSERT(offset + numTuples * strideNumberOfFloats - skip <= - static_cast(m_data.size())) { + VERIFY_OR_DEBUG_ASSERT(vertexOffset + numTuples * vertexStride - vertexSkip <= + static_cast(m_vertexData.size())) { return; } - float* to = m_data.data(); - to += offset; + float* to = m_vertexData.data(); + to += vertexOffset; while (numTuples--) { int k = tupleSize; while (k--) { *to++ = *from++; } - to += skip; + to += vertexSkip; } } -float* Geometry::vertexData() { - return m_data.data(); -} - -void Geometry::allocate(int vertexCount) { - const int strideNumberOfFloats = m_stride / sizeof(float); - m_vertexCount = vertexCount; - m_data.resize(strideNumberOfFloats * m_vertexCount); -} - void Geometry::setDrawingMode(Geometry::DrawingMode mode) { m_drawingMode = static_cast(mode); } diff --git a/src/rendergraph/scenegraph/CMakeLists.txt b/src/rendergraph/scenegraph/CMakeLists.txt index 3fe8fd57bd9d..497c127bfead 100644 --- a/src/rendergraph/scenegraph/CMakeLists.txt +++ b/src/rendergraph/scenegraph/CMakeLists.txt @@ -1,9 +1,8 @@ add_library(rendergraph_sg -../common/attributeset.cpp ../common/treenode.cpp ../common/node.cpp ../common/opacitynode.cpp -../common/rendergraph/attribute.h +../common/rendergraph/attributeinit.h ../common/rendergraph/attributeset.h ../common/rendergraph/treenode.h ../common/rendergraph/geometry.h @@ -44,6 +43,7 @@ backend/basematerialtype.h backend/basenode.h backend/baseopacitynode.h backend/basetexture.h +attributeset.cpp treenode.cpp context.cpp geometry.cpp diff --git a/src/rendergraph/scenegraph/attributeset.cpp b/src/rendergraph/scenegraph/attributeset.cpp new file mode 100644 index 000000000000..04d55fcae8f6 --- /dev/null +++ b/src/rendergraph/scenegraph/attributeset.cpp @@ -0,0 +1,10 @@ +#include "rendergraph/attributeset.h" + +#include "backend/baseattributeset.h" + +using namespace rendergraph; + +AttributeSet::AttributeSet(std::initializer_list list, const std::vector&) + : BaseAttributeSet(list) { + // names are not used in scenegraph +} diff --git a/src/rendergraph/scenegraph/backend/baseattributeset.cpp b/src/rendergraph/scenegraph/backend/baseattributeset.cpp index 23c173285dbb..0f0bf0b8e7a7 100644 --- a/src/rendergraph/scenegraph/backend/baseattributeset.cpp +++ b/src/rendergraph/scenegraph/backend/baseattributeset.cpp @@ -1,7 +1,5 @@ #include "backend/baseattributeset.h" -#include "rendergraph/assert.h" - using namespace rendergraph; namespace { @@ -15,23 +13,15 @@ int toQSGGeometryType(const PrimitiveType& t) { } } // namespace -BaseAttributeSetHelper::BaseAttributeSetHelper(std::initializer_list list, - const std::vector& names) { +BaseAttributeSetHelper::BaseAttributeSetHelper(std::initializer_list list) { int i = 0; - DEBUG_ASSERT(list.size() == names.size()); - m_attributes.reserve(list.size()); m_sgAttributes.reserve(list.size()); for (auto item : list) { - m_attributes.push_back(Attribute{item.m_tupleSize, item.m_primitiveType, names[i++]}); - - const auto& attribute = m_attributes.back(); - const int count = static_cast(m_sgAttributes.size()); const bool isPosition = count == 0; m_sgAttributes.push_back(QSGGeometry::Attribute::create(count, - attribute.m_tupleSize, - toQSGGeometryType(attribute.m_primitiveType), + item.m_tupleSize, + toQSGGeometryType(item.m_primitiveType), isPosition)); - m_stride += attribute.m_tupleSize * sizeOf(attribute.m_primitiveType); } } diff --git a/src/rendergraph/scenegraph/backend/baseattributeset.h b/src/rendergraph/scenegraph/backend/baseattributeset.h index 1d6d35638c67..7c33e25192aa 100644 --- a/src/rendergraph/scenegraph/backend/baseattributeset.h +++ b/src/rendergraph/scenegraph/backend/baseattributeset.h @@ -4,7 +4,7 @@ #include #include -#include "rendergraph/attribute.h" +#include "rendergraph/attributeinit.h" namespace rendergraph { class BaseAttributeSet; @@ -13,18 +13,7 @@ class BaseAttributeSetHelper; class rendergraph::BaseAttributeSetHelper { protected: - // helper base class for BaseAttributeSet to populate m_attributes, and m_sgAttributes - // needed to construct BaseAttributeSet's other base class, QSGGeometry::AttributeSet - int m_stride{}; - - BaseAttributeSetHelper(std::initializer_list list, - const std::vector& names); - - const std::vector& attributes() const { - return m_attributes; - } - - std::vector m_attributes; + BaseAttributeSetHelper(std::initializer_list list); std::vector m_sgAttributes; }; @@ -32,11 +21,17 @@ class rendergraph::BaseAttributeSet : protected rendergraph::BaseAttributeSetHelper, public QSGGeometry::AttributeSet { protected: - BaseAttributeSet(std::initializer_list list, - const std::vector& names) - : BaseAttributeSetHelper(list, names), + BaseAttributeSet(std::initializer_list list) + : BaseAttributeSetHelper(list), QSGGeometry::AttributeSet{static_cast(m_sgAttributes.size()), - m_stride, + calculateSizeOfVertex(list), m_sgAttributes.data()} { } + static int calculateSizeOfVertex(std::initializer_list list) { + int numBytes = 0; + for (auto item : list) { + numBytes += item.m_tupleSize * sizeOf(item.m_primitiveType); + } + return numBytes; + } }; diff --git a/src/rendergraph/scenegraph/backend/basegeometry.h b/src/rendergraph/scenegraph/backend/basegeometry.h index 4eb75b7be10a..45cf8780309b 100644 --- a/src/rendergraph/scenegraph/backend/basegeometry.h +++ b/src/rendergraph/scenegraph/backend/basegeometry.h @@ -5,15 +5,5 @@ #include "rendergraph/attributeset.h" namespace rendergraph { -class BaseGeometry; +using BaseGeometry = QSGGeometry; } - -class rendergraph::BaseGeometry : public QSGGeometry { - protected: - BaseGeometry(const AttributeSet& attributeSet, int vertexCount) - : QSGGeometry(attributeSet, vertexCount), - m_stride(attributeSet.stride) { - QSGGeometry::setDrawingMode(QSGGeometry::DrawTriangleStrip); - } - const int m_stride; -}; diff --git a/src/rendergraph/scenegraph/geometry.cpp b/src/rendergraph/scenegraph/geometry.cpp index 6fff53e37890..6f1ccd55c548 100644 --- a/src/rendergraph/scenegraph/geometry.cpp +++ b/src/rendergraph/scenegraph/geometry.cpp @@ -30,41 +30,33 @@ Geometry::Geometry(const rendergraph::AttributeSet& attributeSet, int vertexCoun : BaseGeometry(attributeSet, vertexCount) { } -Geometry::~Geometry() = default; - void Geometry::setAttributeValues(int attributePosition, const float* from, int numTuples) { + // TODO this code assumes all vertices are floats const auto attributeArray = QSGGeometry::attributes(); - int offset = 0; + int vertexOffset = 0; for (int i = 0; i < attributePosition; i++) { - offset += attributeArray[i].tupleSize; + vertexOffset += attributeArray[i].tupleSize; } const int tupleSize = attributeArray[attributePosition].tupleSize; - const int strideNumberOfFloats = m_stride / sizeof(float); - const int skip = strideNumberOfFloats - tupleSize; + const int vertexStride = sizeOfVertex() / sizeof(float); + const int vertexSkip = vertexStride - tupleSize; - VERIFY_OR_DEBUG_ASSERT(offset + numTuples * strideNumberOfFloats - skip <= - vertexCount() * strideNumberOfFloats) { + VERIFY_OR_DEBUG_ASSERT(vertexOffset + numTuples * vertexStride - vertexSkip <= + vertexCount() * vertexStride) { return; } float* to = static_cast(QSGGeometry::vertexData()); - to += offset; + to += vertexOffset; while (numTuples--) { int k = tupleSize; while (k--) { *to++ = *from++; } - to += skip; + to += vertexSkip; } } -float* Geometry::vertexData() { - return static_cast(QSGGeometry::vertexData()); -} - -void Geometry::allocate(int vertexCount) { - QSGGeometry::allocate(vertexCount); -} void Geometry::setDrawingMode(Geometry::DrawingMode mode) { QSGGeometry::setDrawingMode(toSgDrawingMode(mode));