aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOve Brynestad (ovebryne) <ovebryne@cisco.com>2016-07-08 11:33:08 +0200
committerHans Verkuil <hans.verkuil@cisco.com>2016-07-08 12:34:02 +0200
commitc9c9f31aea8f77c6651ed07b3e25a00808c241ae (patch)
treefbbfb22cfe4d5d16c33e97b6fc2a4eb49ff3260c
parenta776a949c4700ec07c02d9d64c784d48024f9bd1 (diff)
0001-qdisp-refactor-to-support-GLES-2.0.patchqdisp
From 3b92141e78190fa1f5ec9f200355552b3d6a3651 Mon Sep 17 00:00:00 2001 From: Ove Brynestad <ovebryne@cisco.com> Date: Wed, 6 Jul 2016 15:29:36 +0200 Subject: [PATCH] qdisp: refactor to support GLES 2.0 Signed-off-by: Ove Brynestad <ovebryne@cisco.com> Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
-rw-r--r--utils/qdisp/capture-win-gl.cpp97
-rw-r--r--utils/qdisp/qdisp.cpp2
-rw-r--r--utils/qdisp/v4l2-convert.glsl8
3 files changed, 78 insertions, 29 deletions
diff --git a/utils/qdisp/capture-win-gl.cpp b/utils/qdisp/capture-win-gl.cpp
index c9244253..7ae16b72 100644
--- a/utils/qdisp/capture-win-gl.cpp
+++ b/utils/qdisp/capture-win-gl.cpp
@@ -343,8 +343,8 @@ void CaptureGLWin::configureTexture(size_t idx)
glBindTexture(GL_TEXTURE_2D, m_screenTexture[idx]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // use new GLES 2.0 clamp definition
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // use new GLES 2.0 clamp definition
}
void CaptureGLWin::setFd(cv4l_fd *fd)
@@ -988,8 +988,8 @@ void CaptureGLWin::tpgUpdateFrame()
void CaptureGLWin::initializeGL()
{
initializeOpenGLFunctions();
- glShadeModel(GL_FLAT);
- glEnable(GL_TEXTURE_2D);
+ //glShadeModel(GL_FLAT); // depreciated GLES 2.0
+ //glEnable(GL_TEXTURE_2D); // depreciated GLES 2.0
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
@@ -997,10 +997,11 @@ void CaptureGLWin::initializeGL()
// If it is, then the GPU can perform the SRGB transfer function
// for us.
GLint res = 0;
- glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &res);
+ // glGetIntegerv(GL_FRAMEBUFFER_SRGB_CAPABLE_EXT, &res); // Fails, Non GLES extension
m_haveFramebufferSRGB = res;
- if (m_haveFramebufferSRGB)
- glEnable(GL_FRAMEBUFFER_SRGB);
+ // if (m_haveFramebufferSRGB) {
+ // glEnable(GL_FRAMEBUFFER_SRGB); // Fails, not available with GLES
+ // }
if (m_verbose) {
printf("OpenGL %sdoes%s support GL_FRAMEBUFFER_SRGB\n",
context()->isOpenGLES() ? "ES " : "",
@@ -1044,16 +1045,13 @@ void CaptureGLWin::paintGL()
}
if (m_curData[0] == NULL) {
+
+ // No data, just clear display
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
- glBindTexture(GL_TEXTURE_2D, 0);
-
- glBegin(GL_QUADS);
- glColor3f(0.0f, 0.0f, 0.0f);
- glVertex2f(0, 0);
- glVertex2f(m_v4l_fmt.g_width(), 0);
- glVertex2f(m_v4l_fmt.g_width(), m_v4l_fmt.g_height());
- glVertex2f(0, m_v4l_fmt.g_height());
- glEnd();
+
+ checkError("paintGL - no data");
+
return;
}
@@ -1161,12 +1159,38 @@ void CaptureGLWin::paintGL()
glBeginQuery(GL_TIME_ELAPSED, query);
}
- glBegin(GL_QUADS);
- glTexCoord2f(0, 0); glVertex2f(0, 0);
- glTexCoord2f(1.0f, 0); glVertex2f(m_v4l_fmt.g_width(), 0);
- glTexCoord2f(1.0f, 1.0f); glVertex2f(m_v4l_fmt.g_width(), m_v4l_fmt.g_height());
- glTexCoord2f(0, 1.0f); glVertex2f(0, m_v4l_fmt.g_height());
- glEnd();
+ // Define Quad surface to draw to, vertices sequenced for GL_TRIANGLE_FAN
+ // Adjusting these will make the screen to draw to smaller or larger
+ const GLfloat quadVertex[] = {
+ -1.0f, -1.0f, // Left Bottom
+ +1.0f, -1.0f, // Right Bottom
+ +1.0f, +1.0f, // Rigth Top
+ -1.0f, +1.0f // Left Top
+ };
+
+ // Normalized texture coords to be aligned to draw quad corners, same sequence but 0,0 is Left Top
+ GLuint texCoords[] = {
+ 0, 1,
+ 1, 1,
+ 1, 0,
+ 0, 0
+ };
+
+ // Attach Quad positions to vertex shader "position" attribute (location = 0)
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, quadVertex);
+ glEnableVertexAttribArray(0);
+ // Attach texture corner positions to vertex shader "texCoord" attribute (location = 1)
+ glVertexAttribPointer(1, 2, GL_UNSIGNED_INT, GL_FALSE, 0, texCoords);
+ glEnableVertexAttribArray(1);
+
+ // Draw quad with texture
+ glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+ // Disable attrib arrays
+ glDisableVertexAttribArray(0);
+ glDisableVertexAttribArray(1);
+
+ checkError("paintGL - drawquad");
if (m_reportTimings) {
glEndQuery(GL_TIME_ELAPSED);
@@ -1312,9 +1336,11 @@ void CaptureGLWin::changeShader()
glDeleteTextures(m_screenTextureCount, m_screenTexture);
m_program->removeAllShaders();
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, m_v4l_fmt.g_width(), m_v4l_fmt.g_height(), 0, 0, 1);
+ // These were depreciated with the programmable function pipeline (GL 3.2 & GLES 2.0 onwards)
+ // This is now normally done in the (mandatory) vertex shader
+ // glMatrixMode(GL_PROJECTION);
+ // glLoadIdentity();
+ // glOrtho(0, m_v4l_fmt.g_width(), m_v4l_fmt.g_height(), 0, 0, 1);
glViewport(0, 0, m_v4l_fmt.g_width(), m_v4l_fmt.g_height());
checkError("Render settings.\n");
@@ -1356,7 +1382,26 @@ void CaptureGLWin::changeShader()
QOpenGLShader::Fragment, code);
if (!src_ok) {
- fprintf(stderr, "OpenGL Error: shader compilation failed.\n");
+ fprintf(stderr, "OpenGL Error: fragment shader compilation failed.\n");
+ exit(1);
+ }
+
+ // Mandatory vertex shader replaces fixed pipeline in GLES 2.0. In this case just a feedthrough shader.
+ const char vertexShaderSrc[] =
+ "#version 300 es\n"
+ "layout(location = 0) in vec2 position;\n"
+ "layout(location = 1) in vec2 texCoord;\n"
+ "out vec2 vs_TexCoord;\n"
+ "void main() {\n"
+ " gl_Position = vec4(position, 0.0, 1.0);\n"
+ " vs_TexCoord = texCoord;\n"
+ "}\n";
+
+ src_ok = m_program->addShaderFromSourceCode(
+ QOpenGLShader::Vertex, vertexShaderSrc);
+
+ if (!src_ok) {
+ fprintf(stderr, "OpenGL Error: vertex shader compilation failed.\n");
exit(1);
}
diff --git a/utils/qdisp/qdisp.cpp b/utils/qdisp/qdisp.cpp
index aef82c36..a35c34f9 100644
--- a/utils/qdisp/qdisp.cpp
+++ b/utils/qdisp/qdisp.cpp
@@ -640,7 +640,7 @@ int main(int argc, char **argv)
}
format.setDepthBufferSize(24);
- //format.setRenderableType(QSurfaceFormat::OpenGLES);
+ format.setRenderableType(QSurfaceFormat::OpenGLES);
//format.setRenderableType(QSurfaceFormat::OpenGL);
QSurfaceFormat::setDefaultFormat(format);
CaptureGLWin win;
diff --git a/utils/qdisp/v4l2-convert.glsl b/utils/qdisp/v4l2-convert.glsl
index a0ad9cce..0b835cd9 100644
--- a/utils/qdisp/v4l2-convert.glsl
+++ b/utils/qdisp/v4l2-convert.glsl
@@ -5,6 +5,10 @@ uniform sampler2D uvtex;
uniform sampler2D utex;
uniform sampler2D vtex;
+in vec2 vs_TexCoord;
+
+out vec4 fs_FragColor;
+
// YUV (aka Y'CbCr) to R'G'B' matrices
const mat3 yuv2rgb = mat3(
@@ -79,7 +83,7 @@ void main()
const float texl_w = 1.0 / tex_w;
const float texl_h = 1.0 / tex_h;
float alpha = 0.0;
- vec2 xy = vec2(gl_FragCoord.x / tex_w, (tex_h - gl_FragCoord.y) / tex_h);
+ vec2 xy = vs_TexCoord;
float xcoord = floor(xy.x * tex_w);
float ycoord = floor(xy.y * tex_h);
bool xeven = mod(xcoord, 2.0) == 0.0;
@@ -319,5 +323,5 @@ void main()
rgb = vec3(XFER_SRGB(rgb.r), XFER_SRGB(rgb.g), XFER_SRGB(rgb.b));
#endif
- gl_FragColor = vec4(rgb, alpha);
+ fs_FragColor = vec4(rgb, alpha);
}

Privacy Policy