From 0bc03960daf0d7e28d45513873e3d1feecbedbf6 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Wed, 16 Sep 2020 17:03:46 +0300 Subject: qcam: viewfinder_gl: Support #define in shaders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prepare the infrastructure to support defining preprocessor macros in shaders: - Rename the fragmentShaderSrc_ member to fragmentShaderFile_ to reflect better that it contains a file name, not shader source code - Add a new fragmentShaderDefines_ member to store preprocessor macros - Prepend the macros to the shader source before compiling it Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/qcam/viewfinder_gl.cpp | 42 ++++++++++++++++++++++++++++++------------ src/qcam/viewfinder_gl.h | 11 +++++------ 2 files changed, 35 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/qcam/viewfinder_gl.cpp b/src/qcam/viewfinder_gl.cpp index 0fa06a29..7cb5fb63 100644 --- a/src/qcam/viewfinder_gl.cpp +++ b/src/qcam/viewfinder_gl.cpp @@ -7,6 +7,8 @@ #include "viewfinder_gl.h" +#include +#include #include #include @@ -24,7 +26,7 @@ static const QList supportedFormats{ ViewFinderGL::ViewFinderGL(QWidget *parent) : QOpenGLWidget(parent), buffer_(nullptr), yuvData_(nullptr), - fragmentShader_(nullptr), vertexShader_(nullptr), + vertexShader_(nullptr), fragmentShader_(nullptr), vertexBuffer_(QOpenGLBuffer::VertexBuffer), textureU_(QOpenGLTexture::Target2D), textureV_(QOpenGLTexture::Target2D), @@ -97,46 +99,49 @@ void ViewFinderGL::render(libcamera::FrameBuffer *buffer, MappedBuffer *map) bool ViewFinderGL::selectFormat(const libcamera::PixelFormat &format) { bool ret = true; + + fragmentShaderDefines_.clear(); + switch (format) { case libcamera::formats::NV12: horzSubSample_ = 2; vertSubSample_ = 2; - fragmentShaderSrc_ = ":YUV_2_planes_UV.frag"; + fragmentShaderFile_ = ":YUV_2_planes_UV.frag"; break; case libcamera::formats::NV21: horzSubSample_ = 2; vertSubSample_ = 2; - fragmentShaderSrc_ = ":YUV_2_planes_VU.frag"; + fragmentShaderFile_ = ":YUV_2_planes_VU.frag"; break; case libcamera::formats::NV16: horzSubSample_ = 2; vertSubSample_ = 1; - fragmentShaderSrc_ = ":YUV_2_planes_UV.frag"; + fragmentShaderFile_ = ":YUV_2_planes_UV.frag"; break; case libcamera::formats::NV61: horzSubSample_ = 2; vertSubSample_ = 1; - fragmentShaderSrc_ = ":YUV_2_planes_VU.frag"; + fragmentShaderFile_ = ":YUV_2_planes_VU.frag"; break; case libcamera::formats::NV24: horzSubSample_ = 1; vertSubSample_ = 1; - fragmentShaderSrc_ = ":YUV_2_planes_UV.frag"; + fragmentShaderFile_ = ":YUV_2_planes_UV.frag"; break; case libcamera::formats::NV42: horzSubSample_ = 1; vertSubSample_ = 1; - fragmentShaderSrc_ = ":YUV_2_planes_VU.frag"; + fragmentShaderFile_ = ":YUV_2_planes_VU.frag"; break; case libcamera::formats::YUV420: horzSubSample_ = 2; vertSubSample_ = 2; - fragmentShaderSrc_ = ":YUV_3_planes.frag"; + fragmentShaderFile_ = ":YUV_3_planes.frag"; break; case libcamera::formats::YVU420: horzSubSample_ = 2; vertSubSample_ = 2; - fragmentShaderSrc_ = ":YUV_3_planes.frag"; + fragmentShaderFile_ = ":YUV_3_planes.frag"; break; default: ret = false; @@ -168,11 +173,24 @@ bool ViewFinderGL::createFragmentShader() int attributeVertex; int attributeTexture; - /* Create Fragment Shader */ + /* + * Create the fragment shader, compile it, and add it to the shader + * program. The #define macros stored in fragmentShaderDefines_, if + * any, are prepended to the source code. + */ fragmentShader_ = new QOpenGLShader(QOpenGLShader::Fragment, this); - /* Compile the fragment shader */ - if (!fragmentShader_->compileSourceFile(fragmentShaderSrc_)) { + QFile file(fragmentShaderFile_); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { + qWarning() << "Shader" << fragmentShaderFile_ << "not found"; + return false; + } + + QString defines = fragmentShaderDefines_.join('\n') + "\n"; + QByteArray src = file.readAll(); + src.prepend(defines.toUtf8()); + + if (!fragmentShader_->compileSourceCode(src)) { qWarning() << "[ViewFinderGL]:" << fragmentShader_->log(); return false; } diff --git a/src/qcam/viewfinder_gl.h b/src/qcam/viewfinder_gl.h index 7675d0a0..53424dc1 100644 --- a/src/qcam/viewfinder_gl.h +++ b/src/qcam/viewfinder_gl.h @@ -65,17 +65,16 @@ private: QSize size_; unsigned char *yuvData_; - /* OpenGL components for rendering */ - QOpenGLShader *fragmentShader_; - QOpenGLShader *vertexShader_; + /* Shaders */ QOpenGLShaderProgram shaderProgram_; + QOpenGLShader *vertexShader_; + QOpenGLShader *fragmentShader_; + QString fragmentShaderFile_; + QStringList fragmentShaderDefines_; /* Vertex buffer */ QOpenGLBuffer vertexBuffer_; - /* Fragment shader file name */ - QString fragmentShaderSrc_; - /* YUV texture planars and parameters */ GLuint textureUniformU_; GLuint textureUniformV_; -- cgit v1.2.1