This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TDA4VM: [vision_apps] Sampler External OES support in Compute Shader

Part Number: TDA4VM

Hello TI-san,

I'm trying a copmute shader program by modifying the sample app "vx_app_arm_opengl_mosaic.out".

I'd like to access textures using samplerExternalOES in the Compute Shader program.

However, the modified program crashed and did not work.

I have confirmed that it is possible to render an external texture to FBO with Vertex / Fragment Shader and refer to FBO as a texture with Compute Shader.

But, I'd like to reference the external textures directly in the Compute Shader program because it adds extra memory access.

Is it possible to reference external textures in the ComputeShader program?

If yes, please tell me how to reference external textures in the Compute Shader program?

Attach the modified files and the log file below:

psdk_rtos_auto_j7_07_00_00_11/vision_apps/utils/opengl/include/app_gl_egl_utils.h

psdk_rtos_auto_j7_07_00_00_11/vision_apps/kernels/sample/a72/gpu_render1x1.c (includes Compute Shader program)

log_CompShaderNG.txt

Best Regards,

Yudai ISHIBASHI

app_gl_egl_utils.h

/*
Copyright (c) [2012 - 2017] Texas Instruments Incorporated

All rights reserved not granted herein.

Limited License.

 Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
 license under copyrights and patents it now or hereafter owns or controls to
 make,  have made, use, import, offer to sell and sell ("Utilize") this software
 subject to the terms herein.  With respect to the foregoing patent license,
 such license is granted  solely to the extent that any such patent is necessary
 to Utilize the software alone.  The patent license shall not apply to any
 combinations which include this software, other than combinations with devices
 manufactured by or for TI ("TI Devices").  No hardware patent is licensed
 hereunder.

 Redistributions must preserve existing copyright notices and reproduce this
 license (including the above copyright notice and the disclaimer and
 (if applicable) source code license limitations below) in the documentation
 and/or other materials provided with the distribution

 Redistribution and use in binary form, without modification, are permitted
 provided that the following conditions are met:

 * No reverse engineering, decompilation, or disassembly of this software
   is permitted with respect to any software provided in binary form.

 * Any redistribution and use are licensed by TI for use only with TI Devices.

 * Nothing shall obligate TI to provide you with source code for the software
   licensed and provided to you in object code.

 If software source code is provided to you, modification and redistribution of
 the source code are permitted provided that the following conditions are met:

 * Any redistribution and use of the source code, including any resulting
   derivative works, are licensed by TI for use only with TI Devices.

 * Any redistribution and use of any object code compiled from the source code
   and any resulting derivative works, are licensed by TI for use only with TI
   Devices.

 Neither the name of Texas Instruments Incorporated nor the names of its
 suppliers may be used to endorse or promote products derived from this software
 without specific prior written permission.

 DISCLAIMER.

 THIS SOFTWARE IS PROVIDED BY TI AND TI�S LICENSORS "AS IS" AND ANY EXPRESS OR
 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 IN NO EVENT SHALL TI AND TI�S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "gpu_render1x1.h"

static const char gGpuRender1x1_vertexShader[] =
    "attribute vec4 vPosition;\n"
    "attribute vec2 texCoords;\n"
    "varying vec2 yuvTexCoords;\n"
    "void main() {\n"
    "  yuvTexCoords = texCoords;\n"
    "  gl_Position = vPosition;\n"
    "}\n";

static const char gGpuRender1x1_fragmentShader[] =
#ifdef x86_64
#else
   "#extension GL_OES_EGL_image_external : require\n"
#endif
   "precision mediump float;\n"
//#ifdef x86_64
" uniform sampler2D yuvTexSampler;\n "
//#else
//   "uniform samplerExternalOES yuvTexSampler;\n"
//#endif
   "varying vec2 yuvTexCoords;\n"
   "void main() {\n"
   "  gl_FragColor = texture2D(yuvTexSampler, yuvTexCoords);\n"
   "}\n"
   ;

/// add for compute shader by isibasi 201113
static const char gGpuRender1x1_computeShader[] =
   "#version 310 es\n"
#ifdef x86_64
#else
   "#extension GL_OES_EGL_image_external_essl3 : enable\n"
#endif
   "precision mediump float;\n"
   "uniform samplerExternalOES dataIn;\n"
   "layout (binding = 0, rgba8) writeonly uniform lowp image2D dataOut;\n"
   "layout (local_size_x = 8, local_size_y = 8) in;\n"
   "void main(void)\n"
   "{\n"
   "    ivec2 pos = ivec2(gl_GlobalInvocationID.xy);\n"
   "    vec3 px = texelFetch(dataIn, pos, 0).rgb;\n"
//   "    vec3 px = vec3( float(pos.x)/640.0, float(pos.y)/480.0, 0.0);\n"
   "    imageStore(\n"
   "        dataOut,\n"
   "        pos,\n"
   "        vec4(px, 1.0)\n"
   "        );\n"
   "}\n"
   ;
/// end of add for compute shader by isibasi 201113

static const GLfloat gGpuRender1x1_triangleVertices_fullscreen[] = {
        -1.0f, 1.0f, 0.0f,
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        1.0f, 1.0f, 0.0f
 };

static const GLfloat gGpuRender1x1_texCoords[] = {
        0.0f, 0.0f,
        0.0f, 1.0f,
        1.0f, 1.0f,
        1.0f, 0.0f
};

/// add for compute shader by isibasi 201113
static GLuint gTexWork = 0;
static GLuint gCompProgram = 0;
/// end of add for compute shader by isibasi 201113

GLuint gpuRender1x1_loadShader(GLenum shaderType, const char* pSource) {
   GLuint shader = glCreateShader(shaderType);
   if (shader) {
       glShaderSource(shader, 1, &pSource, NULL);
       glCompileShader(shader);
       GLint compiled = 0;
       glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
       if (!compiled) {
           GLint infoLen = 0;
           glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
           if (infoLen) {
               char* buf = (char*) malloc(infoLen);
               if (buf) {
                   glGetShaderInfoLog(shader, infoLen, NULL, buf);
                   fprintf(stderr, " GL: Could not compile shader %d:\n%s\n",
                       shaderType, buf);
                   free(buf);
               }
           } else {
               fprintf(stderr, " GL: Guessing at GL_INFO_LOG_LENGTH size\n");
               char* buf = (char*) malloc(0x1000);
               if (buf) {
                   glGetShaderInfoLog(shader, 0x1000, NULL, buf);
                   fprintf(stderr, " GL: Could not compile shader %d:\n%s\n",
                   shaderType, buf);
                   free(buf);
               }
           }
           glDeleteShader(shader);
           shader = 0;
       }
   }
   return shader;
}


GLuint gpuRender1x1_createProgram(const char* pVertexSource, const char* pFragmentSource) {
   GLuint vertexShader = gpuRender1x1_loadShader(GL_VERTEX_SHADER, pVertexSource);
   if (!vertexShader) {
       return 0;
   }

   GLuint pixelShader = gpuRender1x1_loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
   if (!pixelShader) {
       return 0;
   }

   GLuint program = glCreateProgram();
   if (program) {
       glAttachShader(program, vertexShader);
       appEglCheckGlError("glAttachShader");
       glAttachShader(program, pixelShader);
       appEglCheckGlError("glAttachShader");
       glLinkProgram(program);
       GLint linkStatus = GL_FALSE;
       glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
       if (linkStatus != GL_TRUE) {
           GLint bufLength = 0;
           glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
           if (bufLength) {
               char* buf = (char*) malloc(bufLength);
               if (buf) {
                   glGetProgramInfoLog(program, bufLength, NULL, buf);
                   fprintf(stderr, " GL: Could not link program:\n%s\n", buf);
                   free(buf);
               }
           }
           glDeleteProgram(program);
           program = 0;
       }
   }
   return program;
}

/// add for compute shader by isibasi 201113
GLuint gpuRender1x1_createComputeProgram(const char* pComputeSource) {
   GLuint computeShader = gpuRender1x1_loadShader(GL_COMPUTE_SHADER, pComputeSource);
   if (!computeShader) {
       return 0;
   }

   GLuint program = glCreateProgram();
   if (program) {
       glAttachShader(program, computeShader);
       appEglCheckGlError("glAttachShader");
       glLinkProgram(program);
       GLint linkStatus = GL_FALSE;
       glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
       if (linkStatus != GL_TRUE) {
           GLint bufLength = 0;
           glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
           if (bufLength) {
               char* buf = (char*) malloc(bufLength);
               if (buf) {
                   glGetProgramInfoLog(program, bufLength, NULL, buf);
                   fprintf(stderr, " GL: Could not link compute program:\n%s\n", buf);
                   free(buf);
               }
           }
           glDeleteProgram(program);
           program = 0;
       }
   }
   return program;
}
/// end of add for compute shader by isibasi 201113

int gpuRender1x1_setup(GpuRender1x1_Obj *pObj)
{
    pObj->program = gpuRender1x1_createProgram(
                        gGpuRender1x1_vertexShader,
                        gGpuRender1x1_fragmentShader
                     );
    if (pObj->program==0)
    {
       return -1;
    }

    pObj->vPositionHandle = glGetAttribLocation(pObj->program, "vPosition");
    appEglCheckGlError("glGetAttribLocation");

    pObj->yuvTexSamplerHandle = glGetUniformLocation(pObj->program, "yuvTexSampler");
    appEglCheckGlError("glGetUniformLocation");

    pObj->vTexHandle = glGetAttribLocation(pObj->program, "texCoords");
    appEglCheckGlError("glGetAttribLocation");

    glUseProgram(pObj->program);
    appEglCheckGlError("glUseProgram");

/// add for compute shader by isibasi 201113
    gCompProgram = gpuRender1x1_createComputeProgram(gGpuRender1x1_computeShader);
    
    if (gCompProgram==0)
    {
       return -1;
    }

   /* STORAGE */
   glGenTextures(1, &gTexWork);
   appEglCheckGlError("glGenTextures");
   
   glBindTexture(GL_TEXTURE_2D, gTexWork);
   appEglCheckGlError("glBindTexture");
   
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   appEglCheckGlError("glTexParameteri");
   
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   appEglCheckGlError("glTexParameteri");
   
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   appEglCheckGlError("glTexParameteri");
   
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   appEglCheckGlError("glTexParameteri");
   
   glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, 640, 480);
   appEglCheckGlError("glTexStorage2D");
   
   glBindTexture(GL_TEXTURE_2D, 0);
   appEglCheckGlError("glBindTexture");
/// end of add for compute shader by isibasi 201113

    return 0;
}

void gpuRender1x1_renderFrame1x1(GpuRender1x1_Obj *pObj, void *pEglWindowObj, const GLfloat *vertices, GLuint texYuv)
{
/// add for compute shader by isibasi 201113
    // for compute shader
    glUseProgram(gCompProgram);
    appEglCheckGlError("glUseProgram");
    
    glActiveTexture(GL_TEXTURE0);
    appEglCheckGlError("glActiveTexture");
    
#if defined(x86_64)
    glBindTexture(GL_TEXTURE_2D, texYuv);
#else
    glBindTexture(GL_TEXTURE_EXTERNAL_OES, texYuv);
#endif
    appEglCheckGlError("glBindTexture");
    
    glUniform1i(glGetUniformLocation(gCompProgram, "dataIn"), 0);
    appEglCheckGlError("glUniform1i");
    
    glBindImageTexture(0, gTexWork, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
    appEglCheckGlError("glBindImageTexture");
    
    glDispatchCompute(640 / 8, 480 / 8, 1);
    appEglCheckGlError("glDispatchCompute");
    
    glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
    appEglCheckGlError("glMemoryBarrier");

    // for Render
    glUseProgram(pObj->program);
    appEglCheckGlError("glUseProgram");
/// end of add for compute shader by isibasi 201113

    glVertexAttribPointer(pObj->vPositionHandle, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    appEglCheckGlError("glVertexAttribPointer");
    glEnableVertexAttribArray(pObj->vPositionHandle);
    appEglCheckGlError("glEnableVertexAttribArray");

    glVertexAttribPointer(pObj->vTexHandle, 2, GL_FLOAT, GL_FALSE, 0, gGpuRender1x1_texCoords);
    appEglCheckGlError("glVertexAttribPointer");
    glEnableVertexAttribArray(pObj->vTexHandle);
    appEglCheckGlError("glEnableVertexAttribArray");

    glUniform1i(pObj->yuvTexSamplerHandle, 0);
    appEglCheckGlError("glUniform1i");

    glActiveTexture(GL_TEXTURE0);
    appEglCheckGlError("glActiveTexture");

//#if defined(x86_64)
//    glBindTexture(GL_TEXTURE_2D, texYuv);
//#else
//    glBindTexture(GL_TEXTURE_EXTERNAL_OES, texYuv);
//#endif
    glBindTexture(GL_TEXTURE_2D, gTexWork);
    appEglCheckGlError("glBindTexture");

	glViewport(0, 0,1920,1080);

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    appEglCheckGlError("glDrawArrays");
}

void gpuRender1x1_renderFrame(GpuRender1x1_Obj *pObj, void *pEglWindowObj, GLuint texYuv)
{
    gpuRender1x1_renderFrame1x1(pObj, pEglWindowObj, gGpuRender1x1_triangleVertices_fullscreen, texYuv);
}
root@j7-evm:/opt/vision_apps# ./vx_app_arm_opengl_mosaic.out
APP: Init ... !!!
MEM: Init ... !!!
MEM: Initialized DMA HEAP (fd=4) !!!
MEM: Init ... Done !!!
IPC: Init ... !!!
IPC: Init ... Done !!!
REMOTE_SERVICE: Init ... !!!
REMOTE_SERVICE: Init ... Done !!!
APP: Init ... Done !!!
    41.896600 s:  VX_ZONE_INIT:Enabled
    41.896613 s:  VX_ZONE_ERROR:Enabled
    41.896618 s:  VX_ZONE_WARNING:Enabled
    41.897110 s:  VX_ZONE_INIT:[tivxInit:71] Initialization Done !!!
    41.897247 s:  VX_ZONE_INIT:[tivxHostInit:48] Initialization Done for HOST !!!
EGL: version 1.4
EGL: GL Version = (null)
EGL: GL Vendor = (null)
EGL: GL Renderer = (null)
EGL: GL Extensions = (null)
(1060) PVR:(Fatal): Debug assertion failed! (!bIsCompute) [ drawvarray.c:2651 ]
Aborted (core dumped)
root@j7-evm:/opt/vision_apps#

Best Regards,

Yudai ISHIBASHI