glTexImage2D在某些PC上需要很长时间

时间:2017-09-11 08:00:43

标签: c++ opengl hardware-acceleration

背景

  1. 使用DXVA2解码4K 30FPS视频,然后将解码后的帧直接传递给opengl进行渲染。
  2. 框架是GPU解码(DXVA2),然后是GPU渲染(Opengl),无需复制回CPU。
  3. 问题:

    1. 我在一些电脑上发现glTexImage2D耗时太长(140ms)。一台PC的GPU是intel HD 5500 + Nivida GeFoce 940M。
    2. 但是在某些PC上它只需要16ms。一台PC的GPU是intel HD 5500 + AMD R7 M260。
    3. 所有PC都更新了最新的gpu驱动程序。
    4. 源码:

      以下是DXVA2解码源代码。

      static int dxva2_retrieve_data(AVCodecContext *s, AVFrame *frame)
      {
          LPDIRECT3DSURFACE9 surface =  (LPDIRECT3DSURFACE9)frame->data[3];
          InputStream        *ist = (InputStream *)s->opaque;
          DXVA2Context       *ctx = (DXVA2Context*)ist->hwaccel_ctx;
          D3DSURFACE_DESC    surfaceDesc;
          D3DLOCKED_RECT     LockedRect;
          HRESULT            hr;
          int                ret;
      
          IDirect3DSurface9_GetDesc(surface, &surfaceDesc);
      
          hr = IDirect3DSurface9_LockRect(surface, &LockedRect, NULL, D3DLOCK_READONLY);
          if (FAILED(hr)) {
              av_log(NULL, AV_LOG_ERROR, "Unable to lock DXVA2 surface\n");
              return AVERROR_UNKNOWN;
          }
      
          picture->data[0] = (uint8_t*)LockedRect.pBits;
          picture->data[1] = (uint8_t*)LockedRect.pBits + LockedRect.Pitch * surfaceDesc.Height;
          frameWidth = frame->width;
          frameHeight = frame->height;
          //glFinish() <---- commentee suggestion
          DWORD start = GetTickCount();//test time
          Draw();//opengl draw        
          //glFinish() <---- commentee suggestion
          DWORD end = GetTickCount();//test time
          printf("draw time:%d\n", end - start);//test time
          start = end - start;
          picture->data[0] = nullptr;
          picture->data[1] = nullptr;
      
          IDirect3DSurface9_UnlockRect(surface);
          return 0;
      fail:
          return ret;
      }
      

      下面是Draw函数的代码。

      void Draw()
      {
          glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
          glClearDepth(1.0f);
          glClear(GL_COLOR_BUFFER_BIT);
          glShadeModel(GL_SMOOTH);
      
          glClearColor(0.0, 255, 0.0, 0.0);
          glClear(GL_COLOR_BUFFER_BIT);
      
          glUseProgram(g_program);
          glActiveTexture(GL_TEXTURE0);
          glBindTexture(GL_TEXTURE_2D, g_texture[0]);
          glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, frameWidth, frameHeight, 0, GL_RED, GL_UNSIGNED_BYTE, picture->data[0]);
          glUniform1i(y, 0);
      
          glActiveTexture(GL_TEXTURE1);
          glBindTexture(GL_TEXTURE_2D, g_texture[1]);
          glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE_ALPHA, frameWidth / 2, frameHeight / 2, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, picture->data[1]);
          glUniform1i(uv, 1);
      
          DrawHorizontal();
      
          glBindVertexArray(0);
          glUseProgram(0);
          glBindTexture(GL_TEXTURE_2D, 0);
      }
      

      问题:

      谁能告诉我为什么相同的代码在不同的PC上有不同的结果? 或者给我一些建议?

0 个答案:

没有答案