diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2020-09-16 17:03:46 +0300 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2020-10-04 22:51:45 +0300 |
commit | 0bc03960daf0d7e28d45513873e3d1feecbedbf6 (patch) | |
tree | c0901ee77cfeb75051b17e6a2f27bdd96d07ca90 /src/qcam/viewfinder_gl.cpp | |
parent | 440028d6c9a2e98234a71fee79c55791e1d2af2f (diff) |
qcam: viewfinder_gl: Support #define in shaders
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 <laurent.pinchart@ideasonboard.com>
Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>
Diffstat (limited to 'src/qcam/viewfinder_gl.cpp')
-rw-r--r-- | src/qcam/viewfinder_gl.cpp | 42 |
1 files changed, 30 insertions, 12 deletions
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 <QByteArray> +#include <QFile> #include <QImage> #include <libcamera/formats.h> @@ -24,7 +26,7 @@ static const QList<libcamera::PixelFormat> 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; } |