Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rendergraph: add rendergraph library #14007

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4572,6 +4572,9 @@ if(VINYLCONTROL)
target_link_libraries(mixxx-lib PRIVATE mixxx-xwax)
endif()

# rendergraph
add_subdirectory(src/rendergraph/opengl)

# WavPack audio file support
find_package(wavpack)
default_option(WAVPACK "WavPack audio file support" "wavpack_FOUND")
Expand Down
43 changes: 43 additions & 0 deletions src/rendergraph/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core)
message(STATUS "Qt version ${QT_VERSION_MAJOR}.${QT_VERSION_MINOR}")

if(QT_VERSION_MINOR GREATER_EQUAL 6)
set(USE_QSHADER_FOR_GL ON)
endif()

set(
COMMON_RENDERGRAPH_FILES
../common/rendergraph/attributeinit.h
../common/rendergraph/attributeset.h
../common/rendergraph/geometry.h
../common/rendergraph/geometrynode.h
../common/rendergraph/material.h
../common/rendergraph/material/endoftrackmaterial.cpp
../common/rendergraph/material/endoftrackmaterial.h
../common/rendergraph/material/patternmaterial.cpp
../common/rendergraph/material/patternmaterial.h
../common/rendergraph/material/rgbamaterial.cpp
../common/rendergraph/material/rgbamaterial.h
../common/rendergraph/material/rgbmaterial.cpp
../common/rendergraph/material/rgbmaterial.h
../common/rendergraph/material/texturematerial.cpp
../common/rendergraph/material/texturematerial.h
../common/rendergraph/material/unicolormaterial.cpp
../common/rendergraph/material/unicolormaterial.h
../common/rendergraph/materialshader.h
../common/rendergraph/materialtype.h
../common/rendergraph/node.h
../common/rendergraph/opacitynode.h
../common/rendergraph/texture.h
../common/rendergraph/types.h
../common/rendergraph/uniform.h
../common/rendergraph/uniformscache.cpp
../common/rendergraph/uniformscache.h
../common/rendergraph/uniformset.cpp
../common/rendergraph/uniformset.h
../common/types.cpp
)

add_subdirectory(opengl)
add_subdirectory(scenegraph)
add_subdirectory(shaders)
43 changes: 43 additions & 0 deletions src/rendergraph/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
rendergraph is an abstraction layer that can be compiled with one of two backends:

- scenegraph (QtQuick scene graph classes)
- opengl (custom classes using QOpenGL)

This abstraction layer follows the design of the QtQuick scene graph, with classes such as Material, Geometry, Node and GeometryNode, but it only gives access to and only uses a subset of its functionality.

The scenegraph backend class the underlying QtQuick scene graph classes directly or almost directly. The opengl layer implements classes that mimic the behaviour of the
Qt scene graph classes.

The objective of rendergraph is to be able to write code that can be used verbatim within the context of both QWidgets applications and QML applications. This includes using the same Vulkan-style GLSL shader code for both. (The QSGMaterialShader uses the qsb files, QOpenGLShader uses the OpenGL shader code generated by qsb)

Primarily, rendering with rendergraph uses a graph of Node's, typically of type GeometryNode. A GeometryNode uses a Geometry, which contains the vertex data of what the node renders (positional, texture coordinates, color information...) and a Material, which is the interface to the underlying shader. Uniform data is set through the Material. The QtQuick scene graph documentation is a good started point to understand the design.

The code is organized as follows:

common/rendergraph
Public header files. This is the primary API, identical for both backends, and the only files that should be included to write code compatible with both backends.

common
Implementation of the functionality declared in the common/rendergraph headers that is identical for both backends.
The general guideline is "code duplicated between the backends should be in the common layer".

common/rendergraph/material
Several basic 'materials' for common rendering operations (e.g. uniform or variying color, texture)

scenegraph
Implementation of the functionality declared in the common/rendergraph headers that is specific to call the scenegraph backend

scenegraph/rendergraph
Public header files specific for the opengl backend. This is the layer between QtQuick application code and the common rendergraph API.

scenegraph/backend
The scenegraph backend implementation, the layer between the common/rendergraph API and the underlying QtQuick scene graph classes.

opengl
Implementation of the functionality declared in the common/rendergraph headers that is specific to call the opengl backend

opengl/rendergraph
Public header files specific for the opengl backend. This is the layer between QWidgets/QOpenGL application code and the common rendergraph API.

opengl/backend
The opengl backend implementation, the layer between the common/rendergraph API and custom implemented classes that mimic the behaviour QtQuick scene graph classes.
4 changes: 4 additions & 0 deletions src/rendergraph/common/rendergraph/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Note: in principal, rendergraph is a module independent of Mixxx, but we
// do want to include util/assert.h from Mixxx here, using a relative path
// so we avoid adding the entire Mixxx src/ dir to the include paths.
#include "../../../util/assert.h"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of the relative import from the parent dir. We don't do that anywhere else. Not sure if there is a better way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s a bit ugly, but I didn’t want to add the entire src/ dir to the include paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we could get CMake to copy assert.h into the rendergraph include? CMake noob so I have no idea if it could work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really wouldn't go down that path. This is isolated and commented.

18 changes: 18 additions & 0 deletions src/rendergraph/common/rendergraph/attributeinit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "rendergraph/types.h"

namespace rendergraph {
struct AttributeInit;
}

/// Helper to create an AttributeSet using an initializer_list.
struct rendergraph::AttributeInit {
int m_tupleSize;
PrimitiveType m_primitiveType;

template<typename T>
static AttributeInit create() {
return AttributeInit{tupleSizeOf<T>(), primitiveTypeOf<T>()};
}
};
20 changes: 20 additions & 0 deletions src/rendergraph/common/rendergraph/attributeset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include "backend/baseattributeset.h"
#include "rendergraph/attributeinit.h"

namespace rendergraph {

class AttributeSet : public BaseAttributeSet {
public:
AttributeSet(std::initializer_list<AttributeInit> list, const std::vector<QString>& names);
};

template<typename... Ts, int N>
AttributeSet makeAttributeSet(const QString (&names)[N]) {
static_assert(sizeof...(Ts) == N, "Mismatch between number of attribute types and names");
return AttributeSet({(AttributeInit::create<Ts>())...},
std::vector<QString>(std::cbegin(names), std::cend(names)));
}

} // namespace rendergraph
67 changes: 67 additions & 0 deletions src/rendergraph/common/rendergraph/geometry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <QVector2D>
#include <QVector3D>

#include "backend/basegeometry.h"
#include "rendergraph/attributeset.h"

namespace rendergraph {
class Geometry;
} // namespace rendergraph

class rendergraph::Geometry : public rendergraph::BaseGeometry {
public:
using DrawingMode = rendergraph::DrawingMode;

struct Point2D {
QVector2D position2D;
};

struct TexturedPoint2D {
QVector2D position2D;
QVector2D texcoord2D;
};

struct RGBColoredPoint2D {
QVector2D position2D;
QVector3D color3D;
};

struct RGBAColoredPoint2D {
QVector2D position2D;
QVector4D color4D;
};

Geometry(const rendergraph::AttributeSet& attributeSet, int vertexCount);

const Attribute* attributes() const {
return BaseGeometry::attributes();
}

void setAttributeValues(int attributePosition, const float* data, int numTuples);

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<typename T>
T* vertexDataAs() {
return reinterpret_cast<T*>(vertexData());
}

DrawingMode drawingMode() const;

void setDrawingMode(DrawingMode mode);
};
39 changes: 39 additions & 0 deletions src/rendergraph/common/rendergraph/geometrynode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include "backend/basegeometrynode.h"
#include "rendergraph/geometry.h"
#include "rendergraph/material.h"
#include "rendergraph/nodeinterface.h"

namespace rendergraph {
class GeometryNode;
} // namespace rendergraph

class rendergraph::GeometryNode : public rendergraph::NodeInterface<rendergraph::BaseGeometryNode> {
public:
GeometryNode();
virtual ~GeometryNode() = default;

template<class T_Material>
void initForRectangles(int numRectangles) {
const int verticesPerRectangle = 6; // 2 triangles
setGeometry(std::make_unique<Geometry>(T_Material::attributes(),
numRectangles * verticesPerRectangle));
setMaterial(std::make_unique<T_Material>());
geometry().setDrawingMode(DrawingMode::Triangles);
}

void setUsePreprocess(bool value);
void setMaterial(std::unique_ptr<Material> material);
void setGeometry(std::unique_ptr<Geometry> geometry);

Geometry& geometry() const;
Material& material() const;

void markDirtyGeometry();
void markDirtyMaterial();

private:
std::unique_ptr<Material> m_pMaterial;
std::unique_ptr<Geometry> m_pGeometry;
};
71 changes: 71 additions & 0 deletions src/rendergraph/common/rendergraph/material.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <memory>

#include "backend/basematerial.h"
#include "rendergraph/assert.h"
#include "rendergraph/materialshader.h"
#include "rendergraph/materialtype.h"
#include "rendergraph/texture.h"
#include "rendergraph/uniformscache.h"
#include "rendergraph/uniformset.h"

namespace rendergraph {
class Material;
} // namespace rendergraph

class rendergraph::Material : public rendergraph::BaseMaterial {
public:
Material(const UniformSet& uniformSet);
virtual ~Material();

/// See QSGMaterial::compare.
// Note: QSGMaterial::compare is virtual, so that a concrete Material can
// implement a custom compare function. But in rendergraph we can always
// compare the uniforms cache and texture already, and this is sufficient
// for our purpose.
int compare(const Material* pOther) const {
DEBUG_ASSERT(type() == pOther->type());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to early return here in case this is not the same type? I guess we might want to do that if m_uniformsCache and pOther->m_uniformsCache have different size which could lead to out of bound access, right?

int cacheCompareResult = std::memcmp(m_uniformsCache.data(),
pOther->m_uniformsCache.data(),
m_uniformsCache.size());
if (cacheCompareResult != 0) {
return cacheCompareResult < 0 ? -1 : 1;
}
// Note: we currently support only a single texture per material
if (!texture(0) || !pOther->texture(0)) {
return texture(0) ? 1 : -1;
}

const qint64 diff = texture(0)->comparisonKey() - pOther->texture(0)->comparisonKey();
return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
}

virtual std::unique_ptr<MaterialShader> createShader() const = 0;

template<typename T>
void setUniform(int uniformIndex, const T& value) {
m_uniformsCache.set(uniformIndex, value);
m_uniformsCacheDirty = true;
}

const UniformsCache& uniformsCache() const {
return m_uniformsCache;
}

bool clearUniformsCacheDirty() {
if (m_uniformsCacheDirty) {
m_uniformsCacheDirty = false;
return true;
}
return false;
}

virtual Texture* texture(int) const {
return nullptr;
}

private:
UniformsCache m_uniformsCache;
bool m_uniformsCacheDirty{};
};
33 changes: 33 additions & 0 deletions src/rendergraph/common/rendergraph/material/endoftrackmaterial.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "endoftrackmaterial.h"

#include <QVector2D>

#include "rendergraph/materialshader.h"
#include "rendergraph/materialtype.h"
#include "rendergraph/uniformset.h"

using namespace rendergraph;

EndOfTrackMaterial::EndOfTrackMaterial()
: Material(uniforms()) {
}

/* static */ const AttributeSet& EndOfTrackMaterial::attributes() {
static AttributeSet set = makeAttributeSet<QVector2D, float>({"position", "gradient"});
return set;
}

/* static */ const UniformSet& EndOfTrackMaterial::uniforms() {
static UniformSet set = makeUniformSet<QVector4D>({"ubuf.color"});
return set;
}

MaterialType* EndOfTrackMaterial::type() const {
static MaterialType type;
return &type;
}

std::unique_ptr<MaterialShader> EndOfTrackMaterial::createShader() const {
return std::make_unique<MaterialShader>(
"endoftrack.vert", "endoftrack.frag", uniforms(), attributes());
}
19 changes: 19 additions & 0 deletions src/rendergraph/common/rendergraph/material/endoftrackmaterial.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "rendergraph/attributeset.h"
#include "rendergraph/material.h"

namespace rendergraph {
class EndOfTrackMaterial;
}

class rendergraph::EndOfTrackMaterial : public rendergraph::Material {
public:
EndOfTrackMaterial();

static const AttributeSet& attributes();

static const UniformSet& uniforms();

MaterialType* type() const override;

std::unique_ptr<MaterialShader> createShader() const override;
};
Loading
Loading