Tracing/QmlProfiler: Prepare for porting of shaders to Qt 6

This change touches up the shader handling code in Tracing and
QmlProfiler in order to pave the way for the pending porting of
the shaders to Qt 6.

- Use QSGGeometry::Attribute::createWithAttributeType instead of
  QSGGeometry::Attribute::create
- Undefine some fields that are unused in Qt 6
- Add a couple of comments to document the relation between C++
  variables and shader attributes
- Extract some code into functions

Change-Id: I0e7119484b6190a415a5c2d2a0bbd6465088cf19
Reviewed-by: Henning Gründl <henning.gruendl@qt.io>
This commit is contained in:
Alessandro Portale
2021-07-13 19:23:39 +02:00
parent f95d6c6009
commit 84a0170516
4 changed files with 71 additions and 64 deletions

View File

@@ -357,14 +357,22 @@ static qint64 endTime(const TimelineModel *model, const TimelineRenderState *par
const QSGGeometry::AttributeSet &OpaqueColoredPoint2DWithSize::attributes() const QSGGeometry::AttributeSet &OpaqueColoredPoint2DWithSize::attributes()
{ {
static QSGGeometry::Attribute data[] = { static const QSGGeometry::Attribute data[] = {
QSGGeometry::Attribute::create(0, 2, QSGGeometry::FloatType, true), // vec4 vertexCoord
QSGGeometry::Attribute::create(1, 2, QSGGeometry::FloatType), QSGGeometry::Attribute::createWithAttributeType(0, 2, QSGGeometry::FloatType,
QSGGeometry::Attribute::create(2, 1, QSGGeometry::FloatType), QSGGeometry::PositionAttribute),
QSGGeometry::Attribute::create(3, 4, QSGGeometry::UnsignedByteType) // vec2 rectSize
QSGGeometry::Attribute::createWithAttributeType(1, 2, QSGGeometry::FloatType,
QSGGeometry::UnknownAttribute),
// float selectionId
QSGGeometry::Attribute::createWithAttributeType(2, 1, QSGGeometry::FloatType,
QSGGeometry::UnknownAttribute),
// vec4 vertexColor
QSGGeometry::Attribute::createWithAttributeType(3, 4, QSGGeometry::UnsignedByteType,
QSGGeometry::ColorAttribute),
}; };
static QSGGeometry::AttributeSet attrs = { static const QSGGeometry::AttributeSet attrs = {
4, sizeof(data) / sizeof(data[0]),
sizeof(OpaqueColoredPoint2DWithSize), sizeof(OpaqueColoredPoint2DWithSize),
data data
}; };
@@ -448,13 +456,13 @@ public:
private: private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void initialize() override; void initialize() override;
#endif // < Qt 6
int m_matrix_id; int m_matrix_id;
int m_scale_id; int m_scale_id;
int m_selection_color_id; int m_selection_color_id;
int m_selected_item_id; int m_selected_item_id;
int m_z_range_id; int m_z_range_id;
#endif // < Qt 6
}; };
TimelineItemsMaterialShader::TimelineItemsMaterialShader() TimelineItemsMaterialShader::TimelineItemsMaterialShader()
@@ -556,17 +564,12 @@ QSGMaterialType *TimelineItemsMaterial::type() const
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSGMaterialShader *TimelineItemsMaterial::createShader() const QSGMaterialShader *TimelineItemsMaterial::createShader() const
{
return new TimelineItemsMaterialShader;
}
#else // < Qt 6 #else // < Qt 6
QSGMaterialShader *TimelineItemsMaterial::createShader( QSGMaterialShader *TimelineItemsMaterial::createShader(QSGRendererInterface::RenderMode) const
QSGRendererInterface::RenderMode renderMode) const #endif // < Qt 6
{ {
Q_UNUSED(renderMode);
return new TimelineItemsMaterialShader; return new TimelineItemsMaterialShader;
} }
#endif // < Qt 6
TimelineItemsRenderPassState::TimelineItemsRenderPassState(const TimelineModel *model) : TimelineItemsRenderPassState::TimelineItemsRenderPassState(const TimelineModel *model) :
m_indexFrom(std::numeric_limits<int>::max()), m_indexTo(-1) m_indexFrom(std::numeric_limits<int>::max()), m_indexTo(-1)

View File

@@ -48,7 +48,7 @@ public:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSGMaterialShader *createShader() const override; QSGMaterialShader *createShader() const override;
#else #else
QSGMaterialShader *createShader(QSGRendererInterface::RenderMode renderMode) const override; QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const override;
#endif // < Qt 6 #endif // < Qt 6
private: private:
@@ -83,8 +83,10 @@ public:
static OpaqueColoredPoint2DWithSize *fromVertexData(QSGGeometry *geometry); static OpaqueColoredPoint2DWithSize *fromVertexData(QSGGeometry *geometry);
private: private:
float x, y, w, h, id; float x, y; // vec4 vertexCoord
unsigned char r, g, b, a; float w, h; // vec2 rectSize
float id; // float selectionId
unsigned char r, g, b, a; // vec4 vertexColor
void setCommon(const OpaqueColoredPoint2DWithSize *master); void setCommon(const OpaqueColoredPoint2DWithSize *master);
void setLeft(const OpaqueColoredPoint2DWithSize *master); void setLeft(const OpaqueColoredPoint2DWithSize *master);

View File

@@ -32,7 +32,8 @@
namespace Timeline { namespace Timeline {
struct Point2DWithDistanceFromTop { struct Point2DWithDistanceFromTop {
float x, y, d; float x, y; // vec4 vertexCoord
float d; // float distanceFromTop
void set(float nx, float ny, float nd); void set(float nx, float ny, float nd);
}; };
@@ -43,7 +44,7 @@ public:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSGMaterialShader *createShader() const final; QSGMaterialShader *createShader() const final;
#else // < Qt 6 #else // < Qt 6
QSGMaterialShader *createShader(QSGRendererInterface::RenderMode renderMode) const final; QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const final;
#endif // < Qt 6 #endif // < Qt 6
}; };
@@ -82,12 +83,16 @@ private:
const QSGGeometry::AttributeSet &NotesGeometry::point2DWithDistanceFromTop() const QSGGeometry::AttributeSet &NotesGeometry::point2DWithDistanceFromTop()
{ {
static QSGGeometry::Attribute data[] = { static const QSGGeometry::Attribute data[] = {
QSGGeometry::Attribute::create(0, 2, QSGGeometry::FloatType, true), // vec4 vertexCoord
QSGGeometry::Attribute::create(1, 1, QSGGeometry::FloatType), QSGGeometry::Attribute::createWithAttributeType(0, 2, QSGGeometry::FloatType,
QSGGeometry::PositionAttribute),
// float distanceFromTop
QSGGeometry::Attribute::createWithAttributeType(1, 1, QSGGeometry::FloatType,
QSGGeometry::UnknownAttribute),
}; };
static QSGGeometry::AttributeSet attrs = { static const QSGGeometry::AttributeSet attrs = {
2, sizeof(data) / sizeof(data[0]),
sizeof(Point2DWithDistanceFromTop), sizeof(Point2DWithDistanceFromTop),
data data
}; };
@@ -229,18 +234,17 @@ public:
QSGMaterial *oldEffect) override; QSGMaterial *oldEffect) override;
char const *const *attributeNames() const override; char const *const *attributeNames() const override;
#else // < Qt 6 #else // < Qt 6
bool updateUniformData(RenderState &state, bool updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *) override;
QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override;
#endif // < Qt 6 #endif // < Qt 6
private: private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void initialize() override; void initialize() override;
#endif // < Qt 6
int m_matrix_id; int m_matrix_id;
int m_z_range_id; int m_z_range_id;
int m_color_id; int m_color_id;
#endif // < Qt 6
}; };
NotesMaterialShader::NotesMaterialShader() NotesMaterialShader::NotesMaterialShader()
@@ -255,20 +259,24 @@ NotesMaterialShader::NotesMaterialShader()
#endif // < Qt 6 #endif // < Qt 6
} }
static QColor notesColor()
{
return Utils::creatorTheme()
? Utils::creatorTheme()->color(Utils::Theme::Timeline_HighlightColor)
: QColor(255, 165, 0);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void NotesMaterialShader::updateState(const RenderState &state, QSGMaterial *, QSGMaterial *) void NotesMaterialShader::updateState(const RenderState &state, QSGMaterial *, QSGMaterial *)
{ {
if (state.isMatrixDirty()) { if (state.isMatrixDirty()) {
program()->setUniformValue(m_matrix_id, state.combinedMatrix()); program()->setUniformValue(m_matrix_id, state.combinedMatrix());
program()->setUniformValue(m_z_range_id, GLfloat(1.0)); program()->setUniformValue(m_z_range_id, GLfloat(1.0));
const QColor notesColor = Utils::creatorTheme() program()->setUniformValue(m_color_id, notesColor());
? Utils::creatorTheme()->color(Utils::Theme::Timeline_HighlightColor)
: QColor(255, 165, 0);
program()->setUniformValue(m_color_id, notesColor);
} }
} }
#else // < Qt 6 #else // < Qt 6
bool NotesMaterialShader::updateUniformData(QSGMaterialShader::RenderState &state, QSGMaterial *, QSGMaterial *) bool NotesMaterialShader::updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *)
{ {
if (state.isMatrixDirty()) { if (state.isMatrixDirty()) {
} }
@@ -299,16 +307,12 @@ QSGMaterialType *NotesMaterial::type() const
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSGMaterialShader *NotesMaterial::createShader() const QSGMaterialShader *NotesMaterial::createShader() const
{
return new NotesMaterialShader;
}
#else // < Qt 6 #else // < Qt 6
QSGMaterialShader *NotesMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const QSGMaterialShader *NotesMaterial::createShader(QSGRendererInterface::RenderMode) const
#endif // < Qt 6
{ {
Q_UNUSED(renderMode);
return new NotesMaterialShader; return new NotesMaterialShader;
} }
#endif // < Qt 6
void Point2DWithDistanceFromTop::set(float nx, float ny, float nd) void Point2DWithDistanceFromTop::set(float nx, float ny, float nd)
{ {

View File

@@ -37,7 +37,7 @@ public:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSGMaterialShader *createShader() const override; QSGMaterialShader *createShader() const override;
#else #else
QSGMaterialShader *createShader(QSGRendererInterface::RenderMode renderMode) const override; QSGMaterialShader *createShader(QSGRendererInterface::RenderMode) const override;
#endif // < Qt 6 #endif // < Qt 6
BindingLoopMaterial(); BindingLoopMaterial();
}; };
@@ -66,7 +66,8 @@ private:
}; };
struct Point2DWithOffset { struct Point2DWithOffset {
float x, y, x2, y2; float x, y; // vec4 vertexCoord
float x2, y2; // vec2 postScaleOffset
void set(float nx, float ny, float nx2, float ny2); void set(float nx, float ny, float nx2, float ny2);
}; };
@@ -199,12 +200,16 @@ Timeline::TimelineRenderPass::State *QmlProfilerBindingLoopsRenderPass::update(
const QSGGeometry::AttributeSet &BindlingLoopsGeometry::point2DWithOffset() const QSGGeometry::AttributeSet &BindlingLoopsGeometry::point2DWithOffset()
{ {
static QSGGeometry::Attribute data[] = { static const QSGGeometry::Attribute data[] = {
QSGGeometry::Attribute::create(0, 2, QSGGeometry::FloatType, true), // vec4 vertexCoord
QSGGeometry::Attribute::create(1, 2, QSGGeometry::FloatType), QSGGeometry::Attribute::createWithAttributeType(0, 2, QSGGeometry::FloatType,
QSGGeometry::PositionAttribute),
// vec2 postScaleOffset
QSGGeometry::Attribute::createWithAttributeType(1, 2, QSGGeometry::FloatType,
QSGGeometry::UnknownAttribute),
}; };
static QSGGeometry::AttributeSet attrs = { static const QSGGeometry::AttributeSet attrs = {
2, sizeof(data) / sizeof(data[0]),
sizeof(Point2DWithOffset), sizeof(Point2DWithOffset),
data data
}; };
@@ -299,18 +304,17 @@ public:
QSGMaterial *oldEffect) override; QSGMaterial *oldEffect) override;
char const *const *attributeNames() const override; char const *const *attributeNames() const override;
#else // < Qt 6 #else // < Qt 6
bool updateUniformData(RenderState &state, QSGMaterial *newEffect, bool updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *) override;
QSGMaterial *oldEffect) override;
#endif // < Qt 6 #endif // < Qt 6
private: private:
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void initialize() override; void initialize() override;
#endif // < Qt 6
int m_matrix_id = 0; int m_matrix_id = 0;
int m_z_range_id = 0; int m_z_range_id = 0;
int m_color_id = 0; int m_color_id = 0;
#endif // < Qt 6
}; };
BindingLoopMaterialShader::BindingLoopMaterialShader() BindingLoopMaterialShader::BindingLoopMaterialShader()
@@ -327,25 +331,24 @@ BindingLoopMaterialShader::BindingLoopMaterialShader()
#endif // < Qt 6 #endif // < Qt 6
} }
static QColor bindingLoopsColor()
{
return Utils::creatorTheme()->color(Utils::Theme::Timeline_HighlightColor);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
void BindingLoopMaterialShader::updateState(const RenderState &state, QSGMaterial *, QSGMaterial *) void BindingLoopMaterialShader::updateState(const RenderState &state, QSGMaterial *, QSGMaterial *)
{ {
if (state.isMatrixDirty()) { if (state.isMatrixDirty()) {
program()->setUniformValue(m_matrix_id, state.combinedMatrix()); program()->setUniformValue(m_matrix_id, state.combinedMatrix());
program()->setUniformValue(m_z_range_id, GLfloat(1.0)); program()->setUniformValue(m_z_range_id, GLfloat(1.0));
program()->setUniformValue( program()->setUniformValue(m_color_id, bindingLoopsColor());
m_color_id,
Utils::creatorTheme()->color(Utils::Theme::Timeline_HighlightColor));
} }
} }
#else // < Qt 6 #else // < Qt 6
bool BindingLoopMaterialShader::updateUniformData(RenderState &state, bool BindingLoopMaterialShader::updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *)
QSGMaterial *newMaterial, QSGMaterial *)
{ {
// TODO: Make this work // TODO: Make this work
if (state.isMatrixDirty()) {
BindingLoopMaterial *material = static_cast<BindingLoopMaterial *>(newMaterial);
}
return state.isMatrixDirty(); return state.isMatrixDirty();
} }
#endif // < Qt 6 #endif // < Qt 6
@@ -378,17 +381,12 @@ QSGMaterialType *BindingLoopMaterial::type() const
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QSGMaterialShader *BindingLoopMaterial::createShader() const QSGMaterialShader *BindingLoopMaterial::createShader() const
{
return new BindingLoopMaterialShader;
}
#else // < Qt 6 #else // < Qt 6
QSGMaterialShader *BindingLoopMaterial::createShader( QSGMaterialShader *BindingLoopMaterial::createShader(QSGRendererInterface::RenderMode) const
QSGRendererInterface::RenderMode renderMode) const #endif // < Qt 6
{ {
Q_UNUSED(renderMode);
return new BindingLoopMaterialShader; return new BindingLoopMaterialShader;
} }
#endif // < Qt 6
void Point2DWithOffset::set(float nx, float ny, float nx2, float ny2) void Point2DWithOffset::set(float nx, float ny, float nx2, float ny2)
{ {