Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7801601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:00:05+00:00 2026-06-02T01:00:05+00:00

I have a code where I receive YUV channels and I’m drawing them using

  • 0

I have a code where I receive YUV channels and I’m drawing them using OpenGLES. Basically, I have a shader that combines them together.

I would like to add a sharpen filter to the result (using the following example: http://igortrindade.wordpress.com/2010/04/23/fun-with-opengl-and-shaders/)

I’m not sure how to run another shader on the actual result (since I would like to run it after my previous shader combined all channels to a single frame).

My current code looks like that:

            glUniform1i(texLum, 0);
            glUniform1i(texU, 1);
            glUniform1i(texV, 2);

            glEnableVertexAttribArray(positionLoc);
            glVertexAttribPointer(positionLoc, 
                                  4, 
                                  GL_FLOAT, 
                                  GL_FALSE, 
                                  0, 
                                  &vertices[0]);

            glEnableVertexAttribArray(texCoordLoc);
            glVertexAttribPointer(texCoordLoc, 
                                  2, 
                                  GL_FLOAT, 
                                  GL_FALSE, 
                                  0, 
                                  &texCoords[0]);


            glDrawElements(GL_TRIANGLES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_BYTE, &indices[0]);

I guess I need to add the new shader right before the last line (glDrawElements), but I’m not sure how to call it.

My shader looks like this:

    static char const *frag = 
    "uniform lowp sampler2D texLum; \n"
    "uniform lowp sampler2D texU; \n"
    "uniform lowp sampler2D texV; \n"
    "varying mediump vec2 texCoordAtFrag; \n"
    "void main() { \n"
    "   lowp float Y = texture2D(texLum, texCoordAtFrag).r; \n"
    "   lowp float U = texture2D(texU, texCoordAtFrag).r; \n"
    "   lowp float V = texture2D(texV, texCoordAtFrag).r; \n"
    "   lowp float R = 1.164 * (Y - 16.0 / 256.0) + 1.596 * (V - 0.5); \n"
    "   lowp float G = 1.164 * (Y - 16.0 / 256.0) - 0.813 * (V - 0.5) - 0.391 * (U - 0.5); \n"
    "   lowp float B = 1.164 * (Y - 16.0 / 256.0) + 2.018 * (U - 0.5); \n"
    "   gl_FragColor = vec4(R,G,B,1); \n"
    "}\r\n";

    static char const *vert = 
    "varying mediump vec2 texCoordAtFrag; \n"
    "attribute vec4 Position; \n"
    "attribute vec2 TexCoord; \n"
    "void main() { \n"
    "   texCoordAtFrag = TexCoord; \n"
    "   gl_Position = Position; \n"
    "}\r\n";

Where texLum,texU,texV are the textures holding the channels.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:00:09+00:00Added an answer on June 2, 2026 at 1:00 am

    Sharpen is a convolution filter, so it reads nine input values to produce one output value. So if you have another shader that is supposed to occur before it and operates one pixel at a time, there’d be a decent argument for running them in two steps (YUV transformation first, sharpen second) so as to eliminate repeated calculations, even if it weren’t also the easiest way to combine shaders as closed boxes.

    If you want to combine them live, break the YUV transformation into a separate function and have the sharpen filter call that function instead of texture2D. The GL shader compiler doesn’t have any sort of restriction on the number of source files you can link together to make a compiled program precisely so that you can follow the usual programming routes for function reuse.

    If you’d prefer to run one then run the other then use an intermediate render-to-texture stage. Do the YUV transform, then switch buffers and use the output of that as the input to the sharpen.

    In practice the former may actually be faster than the latter since a YUV transform is probably a fast operation (if it’s YUV to RGB then it’s one matrix multiplication, for example) whereas memory speed and the need to do quite a drastic change state can be quite expensive. You’d probably need to profile if performance is a concern.

    EDIT: so, from your current main you could just adapt that to (typed here, as I go, please forgive errors):

    "uniform lowp sampler2D texLum; \n"
    "uniform lowp sampler2D texU; \n"
    "uniform lowp sampler2D texV; \n"
    "varying mediump vec2 texCoordAtFrag; \n"
    "lowp vec4 yuvTexture2D(mediump vec2 coord) { \n"
    "   lowp float Y = texture2D(texLum, coord).r; \n"
    "   lowp float U = texture2D(texU, coord).r; \n"
    "   lowp float V = texture2D(texV, coord).r; \n"
    "   lowp float R = 1.164 * (Y - 16.0 / 256.0) + 1.596 * (V - 0.5); \n"
    "   lowp float G = 1.164 * (Y - 16.0 / 256.0) - 0.813 * (V - 0.5) - 0.391 * (U - 0.5); \n"
    "   lowp float B = 1.164 * (Y - 16.0 / 256.0) + 2.018 * (U - 0.5); \n"
    "   return vec4(R,G,B,1.0); \n"
    "}\r\n
    

    And then in your sharpen filter you’d substitute the call to texture2D(<whatever>, coord) with a call to yuvTexture2D(coord), having either included the fragment in the source listing for the sharpen shader or linked it into the program. With respect to switching to using a matrix approach, I guess you’d want (I’m going to format it other than as a string constant, for ease of typing):

    uniform lowp sampler2D texLum;
    uniform lowp sampler2D texU;
    uniform lowp sampler2D texV;
    varying mediump vec2 texCoordAtFrag;
    
    const mediump mat4 yuvToRgb = 
    mat4(   1.164,  1.164,  1.164,  -0.07884, 
            2.018, -0.391,    0.0,  1.153216,
            0.0, -0.813,    1.596,  0.53866, 
            0.0,      0.0,    0.0,  1.0);
    
    lowp vec4 yuvTexture2D(mediump vec2 coord) {
       lowp vec4 yuv = 
             vec4(
                 texture2D(texLum, coord).r,
                 texture2D(texU, coord).r,
                 texture2D(texV, coord).r,
                 1.0)
       return yuvToRgb * yuv;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to code a Java program that will receive messages from network and
I have this code, the point is that I want to receive no more
I have code that looks like this: template<class T> class list { public: class
I have code that I want to look like this: List<Type> Os; ... foreach
I have code that uses Win API function RegSaveKeyEx to save registry entries to
I have code that uses jquery.slideup and jquery.slidedown How can i know that div
I have code that calls an ENTRY in a SUBROUTINE before the SUBROUTINE .
I have code in global.asax that sets some things in cache with CacheItemRemovedCallback. When
I have this code, that when swapping the order of UsingAs and UsingCast, their
I have this code to receive the caller number and do query in side

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.