linear shaders modified

This commit is contained in:
Uwe Rathmann 2022-10-18 17:37:31 +02:00
parent 0d49820cad
commit 4ca794f7db
12 changed files with 23 additions and 19 deletions

View File

@ -347,15 +347,15 @@ namespace
QTransform transform( rect.width(), 0, 0, rect.height(), rect.x(), rect.y()); QTransform transform( rect.width(), 0, 0, rect.height(), rect.x(), rect.y());
#endif #endif
const QVector4D gradientRect( const QVector4D vector(
rect.left() + gradient.start().x() * rect.width(), rect.left() + gradient.start().x() * rect.width(),
rect.top() + gradient.start().y() * rect.height(), rect.top() + gradient.start().y() * rect.height(),
gradient.finalStop().x() * rect.width(), gradient.finalStop().x() * rect.width(),
gradient.finalStop().y() * rect.height() ); gradient.finalStop().y() * rect.height() );
if ( m_gradientRect != gradientRect ) if ( m_gradientVector != vector )
{ {
m_gradientRect = gradientRect; m_gradientVector = vector;
changed = true; changed = true;
} }
@ -372,7 +372,7 @@ namespace
{ {
const auto mat = static_cast< const LinearMaterial* >( other ); const auto mat = static_cast< const LinearMaterial* >( other );
if ( m_gradientRect != mat->m_gradientRect ) if ( m_gradientVector != mat->m_gradientVector )
return QSGMaterial::compare( other ); return QSGMaterial::compare( other );
else else
return GradientMaterial::compare( other ); return GradientMaterial::compare( other );
@ -380,7 +380,11 @@ namespace
QSGMaterialShader* createShader() const override; QSGMaterialShader* createShader() const override;
QVector4D m_gradientRect; /*
xy: position
zw: relative to position ( sign matters )
*/
QVector4D m_gradientVector;
}; };
#ifdef SHADER_GL #ifdef SHADER_GL
@ -395,17 +399,17 @@ namespace
void initialize() override void initialize() override
{ {
GradientShaderGL::initialize(); GradientShaderGL::initialize();
m_rectId = program()->uniformLocation( "rect" ); m_vectorId = program()->uniformLocation( "vector" );
} }
void updateUniformValues( const GradientMaterial* newMaterial ) override void updateUniformValues( const GradientMaterial* newMaterial ) override
{ {
auto material = static_cast< const LinearMaterial* >( newMaterial ); auto material = static_cast< const LinearMaterial* >( newMaterial );
program()->setUniformValue( m_rectId, material->m_gradientRect ); program()->setUniformValue( m_vectorId, material->m_gradientVector );
} }
private: private:
int m_rectId = -1; int m_vectorId = -1;
}; };
#endif #endif
@ -437,9 +441,9 @@ namespace
changed = true; changed = true;
} }
if ( matOld == nullptr || matNew->m_gradientRect != matOld->m_gradientRect ) if ( matOld == nullptr || matNew->m_gradientVector != matOld->m_gradientVector )
{ {
memcpy( data + 64, &matNew->m_gradientRect, 16 ); memcpy( data + 64, &matNew->m_gradientVector, 16 );
changed = true; changed = true;
} }

Binary file not shown.

Binary file not shown.

View File

@ -6,7 +6,7 @@ layout( location = 0 ) out vec4 fragColor;
layout( std140, binding = 0 ) uniform buf layout( std140, binding = 0 ) uniform buf
{ {
mat4 matrix; mat4 matrix;
vec4 rect; vec4 vector;
float opacity; float opacity;
} ubuf; } ubuf;

View File

@ -6,7 +6,7 @@ layout( location = 0 ) out float colorIndex;
layout( std140, binding = 0 ) uniform buf layout( std140, binding = 0 ) uniform buf
{ {
mat4 matrix; mat4 matrix;
vec4 rect; vec4 vector;
float opacity; float opacity;
} ubuf; } ubuf;
@ -14,9 +14,9 @@ out gl_PerVertex { vec4 gl_Position; };
void main() void main()
{ {
vec2 l = vertexCoord.xy - ubuf.rect.xy; vec2 pos = vertexCoord.xy - ubuf.vector.xy;
vec2 size = ubuf.rect.zw; vec2 span = ubuf.vector.zw;
colorIndex = dot( l, size ) / dot( size, size ); colorIndex = dot( pos, span ) / dot( span, span );
gl_Position = ubuf.matrix * vertexCoord; gl_Position = ubuf.matrix * vertexCoord;
} }

View File

@ -1,15 +1,15 @@
attribute vec4 vertexCoord; attribute vec4 vertexCoord;
uniform mat4 matrix; uniform mat4 matrix;
uniform vec4 rect; uniform vec4 vector;
varying float colorIndex; varying float colorIndex;
void main() void main()
{ {
highp vec2 l = vertexCoord.xy - rect.xy; highp vec2 pos = vertexCoord.xy - vector.xy;
highp vec2 size = rect.zw; highp vec2 span = vector.zw;
colorIndex = dot( l, size ) / dot( size, size ); colorIndex = dot( pos, span ) / dot( span, span );
gl_Position = matrix * vertexCoord; gl_Position = matrix * vertexCoord;
} }