1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-06-01 21:54:55 -04:00

Add OpenGL shaders using OpenGL 3.3 syntax for MacOS which doesn't support version 2 syntax wth 4.2 context

This commit is contained in:
Jon Beniston
2022-06-18 23:40:25 +01:00
parent c135affb6a
commit e13e919012
11 changed files with 263 additions and 96 deletions
+97 -39
View File
@@ -38,7 +38,7 @@ GLShaderSpectrogram::GLShaderSpectrogram() :
m_programForLocs(nullptr),
m_textureTransformLoc(0),
m_vertexTransformLoc(0),
m_textureLoc(0),
m_dataTextureLoc(0),
m_limitLoc(0),
m_brightnessLoc(0),
m_colorMapLoc(0),
@@ -75,35 +75,51 @@ GLShaderSpectrogram::~GLShaderSpectrogram()
cleanup();
}
void GLShaderSpectrogram::initializeGL()
void GLShaderSpectrogram::initializeGL(float openGLVersion)
{
initializeOpenGLFunctions();
m_useImmutableStorage = useImmutableStorage();
qDebug() << "GLShaderSpectrogram::initializeGL: m_useImmutableStorage: " << m_useImmutableStorage;
m_programShaded = new QOpenGLShaderProgram;
if (!m_programShaded->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShader)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in vertex shader: " << m_programShaded->log();
}
if (!m_programShaded->addShaderFromSourceCode(QOpenGLShader::Geometry, m_geometryShader)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in geometry shader: " << m_programShaded->log();
}
if (!m_programShaded->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderShaded)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in fragment shader: " << m_programShaded->log();
}
if (!m_programShaded->link()) {
qDebug() << "GLShaderSpectrogram::initializeGL: error linking shader: " << m_programShaded->log();
}
if (openGLVersion >= 3.3)
{
m_programShaded = new QOpenGLShaderProgram;
if (!m_programShaded->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShader)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in vertex shader: " << m_programShaded->log();
}
if (!m_programShaded->addShaderFromSourceCode(QOpenGLShader::Geometry, m_geometryShader)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in geometry shader: " << m_programShaded->log();
}
if (!m_programShaded->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderShaded)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in fragment shader: " << m_programShaded->log();
}
if (!m_programShaded->link()) {
qDebug() << "GLShaderSpectrogram::initializeGL: error linking shader: " << m_programShaded->log();
}
m_programSimple = new QOpenGLShaderProgram;
if (!m_programSimple->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShader)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in vertex shader: " << m_programSimple->log();
m_programSimple = new QOpenGLShaderProgram;
if (!m_programSimple->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShader)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in vertex shader: " << m_programSimple->log();
}
if (!m_programSimple->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSimple)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in fragment shader: " << m_programSimple->log();
}
if (!m_programSimple->link()) {
qDebug() << "GLShaderSpectrogram::initializeGL: error linking shader: " << m_programSimple->log();
}
}
if (!m_programSimple->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSimple)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in fragment shader: " << m_programSimple->log();
}
if (!m_programSimple->link()) {
qDebug() << "GLShaderSpectrogram::initializeGL: error linking shader: " << m_programSimple->log();
else
{
m_programSimple = new QOpenGLShaderProgram;
if (!m_programSimple->addShaderFromSourceCode(QOpenGLShader::Vertex, m_vertexShader2)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in vertex shader: " << m_programSimple->log();
}
if (!m_programSimple->addShaderFromSourceCode(QOpenGLShader::Fragment, m_fragmentShaderSimple2)) {
qDebug() << "GLShaderSpectrogram::initializeGL: error in fragment shader: " << m_programSimple->log();
}
if (!m_programSimple->link()) {
qDebug() << "GLShaderSpectrogram::initializeGL: error linking shader: " << m_programSimple->log();
}
}
}
@@ -324,6 +340,9 @@ void GLShaderSpectrogram::drawSurface(SpectrumSettings::SpectrogramStyle style,
} else {
program = m_programSimple;
}
if (!program) {
return;
}
float rot = invert ? 1.0 : -1.0;
QMatrix4x4 textureTransform(
@@ -339,7 +358,7 @@ void GLShaderSpectrogram::drawSurface(SpectrumSettings::SpectrogramStyle style,
{
m_textureTransformLoc = program->uniformLocation("textureTransform");
m_vertexTransformLoc = program->uniformLocation("vertexTransform");
m_textureLoc = program->uniformLocation("texture");
m_dataTextureLoc = program->uniformLocation("dataTexture");
m_limitLoc = program->uniformLocation("limit");
m_brightnessLoc = program->uniformLocation("brightness");
m_colorMapLoc = program->uniformLocation("colorMap");
@@ -366,7 +385,7 @@ void GLShaderSpectrogram::drawSurface(SpectrumSettings::SpectrogramStyle style,
glActiveTexture(GL_TEXTURE0);
}
program->setUniformValue(m_textureLoc, 0); // set uniform to texture unit?
program->setUniformValue(m_dataTextureLoc, 0); // set uniform to texture unit?
program->setUniformValue(m_colorMapLoc, 1);
program->setUniformValue(m_limitLoc, 1.5f*1.0f/(float)(m_gridElements));
@@ -664,18 +683,36 @@ void GLShaderSpectrogram::applyPerspective(QMatrix4x4 &matrix)
// The clamp is to prevent old data affecting new data (And vice versa),
// which can happen where the texture repeats - might be a better way to do it
const QString GLShaderSpectrogram::m_vertexShader = QString(
const QString GLShaderSpectrogram::m_vertexShader2 = QString(
"attribute vec2 coord2d;\n"
"varying vec4 coord;\n"
"varying float lightDistance;\n"
"uniform mat4 textureTransform;\n"
"uniform mat4 vertexTransform;\n"
"uniform sampler2D texture;\n"
"uniform sampler2D dataTexture;\n"
"uniform float limit;\n"
"uniform vec3 lightPos;\n"
"void main(void) {\n"
" coord = textureTransform * vec4(clamp(coord2d, limit, 1.0-limit), 0, 1);\n"
" coord.z = (texture2D(texture, coord.xy).r);\n"
" coord.z = (texture2D(dataTexture, coord.xy).r);\n"
" gl_Position = vertexTransform * vec4(coord2d, coord.z, 1);\n"
" lightDistance = length(lightPos - gl_Position.xyz);\n"
"}\n"
);
const QString GLShaderSpectrogram::m_vertexShader = QString(
"#version 330\n"
"in vec2 coord2d;\n"
"out vec4 coord;\n"
"out float lightDistance;\n"
"uniform mat4 textureTransform;\n"
"uniform mat4 vertexTransform;\n"
"uniform sampler2D dataTexture;\n"
"uniform float limit;\n"
"uniform vec3 lightPos;\n"
"void main(void) {\n"
" coord = textureTransform * vec4(clamp(coord2d, limit, 1.0-limit), 0, 1);\n"
" coord.z = (texture(dataTexture, coord.xy).r);\n"
" gl_Position = vertexTransform * vec4(coord2d, coord.z, 1);\n"
" lightDistance = length(lightPos - gl_Position.xyz);\n"
"}\n"
@@ -709,9 +746,11 @@ const QString GLShaderSpectrogram::m_geometryShader = QString(
);
const QString GLShaderSpectrogram::m_fragmentShaderShaded = QString(
"varying vec4 coord2;\n"
"varying vec3 normal;\n"
"varying float lightDistance2;\n"
"#version 330\n"
"in vec4 coord2;\n"
"in vec3 normal;\n"
"in float lightDistance2;\n"
"out vec4 fragColor;\n"
"uniform sampler1D colorMap;\n"
"uniform vec3 lightDir;\n"
"void main(void) {\n"
@@ -722,20 +761,20 @@ const QString GLShaderSpectrogram::m_fragmentShaderShaded = QString(
" factor = 0.5;\n"
" float ambient = 0.4;\n"
" vec3 color;\n"
" color.r = texture1D(colorMap, coord2.z).r;\n"
" color.g = texture1D(colorMap, coord2.z).g;\n"
" color.b = texture1D(colorMap, coord2.z).b;\n"
" color.r = texture(colorMap, coord2.z).r;\n"
" color.g = texture(colorMap, coord2.z).g;\n"
" color.b = texture(colorMap, coord2.z).b;\n"
" float cosTheta = max(0.0, dot(normal, lightDir));\n"
" float d2 = max(1.0, lightDistance2*lightDistance2);\n"
" vec3 relection = (ambient * color + color * cosTheta / d2) * factor;\n"
" gl_FragColor[0] = relection.r;\n"
" gl_FragColor[1] = relection.g;\n"
" gl_FragColor[2] = relection.b;\n"
" gl_FragColor[3] = 1.0;\n"
" fragColor[0] = relection.r;\n"
" fragColor[1] = relection.g;\n"
" fragColor[2] = relection.b;\n"
" fragColor[3] = 1.0;\n"
"}\n"
);
const QString GLShaderSpectrogram::m_fragmentShaderSimple = QString(
const QString GLShaderSpectrogram::m_fragmentShaderSimple2 = QString(
"varying vec4 coord;\n"
"uniform float brightness;\n"
"uniform sampler1D colorMap;\n"
@@ -751,3 +790,22 @@ const QString GLShaderSpectrogram::m_fragmentShaderSimple = QString(
" gl_FragColor[3] = 1.0;\n"
"}\n"
);
const QString GLShaderSpectrogram::m_fragmentShaderSimple = QString(
"#version 330\n"
"in vec4 coord;\n"
"out vec4 fragColor;\n"
"uniform float brightness;\n"
"uniform sampler1D colorMap;\n"
"void main(void) {\n"
" float factor;\n"
" if (gl_FrontFacing)\n"
" factor = 1.0;\n"
" else\n"
" factor = 0.5;\n"
" fragColor[0] = texture(colorMap, coord.z).r * brightness * factor;\n"
" fragColor[1] = texture(colorMap, coord.z).g * brightness * factor;\n"
" fragColor[2] = texture(colorMap, coord.z).b * brightness * factor;\n"
" fragColor[3] = 1.0;\n"
"}\n"
);