SHARE
TWEET

Texture.hpp

a guest Jan 29th, 2020 97 in 198 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * A VCOS shared memory render target with one RGBA buffer (read-write)
  3.  * VCOS only support POT textures, so only a part will be used
  4.  */
  5. class VCSMRenderTarget : public RenderTarget
  6. {
  7.     public:
  8.     int bufferWidth, bufferHeight;
  9.     VCSMRenderTarget (int Width, int Height, EGLDisplay Display);
  10.     ~VCSMRenderTarget (void);
  11.     void setTarget (void) override;
  12.     void setSource (ShaderProgram *shader, int slot) override;
  13.     uint8_t* lock(void);
  14.     void unlock(void);
  15.  
  16.     private:
  17.     GLuint TEX_ID;
  18.     struct egl_image_brcm_vcsm_info VCSM;
  19.     EGLImageKHR eglImg;
  20.     uint8_t *vcsmBuffer;
  21.     EGLDisplay eglDisplay;
  22. };
  23.  
  24.  
  25. static int nextPOT (int NPOT)
  26. {
  27.     NPOT--;
  28.     NPOT |= NPOT >> 1;
  29.     NPOT |= NPOT >> 2;
  30.     NPOT |= NPOT >> 4;
  31.     NPOT |= NPOT >> 8;
  32.     NPOT++;
  33.     return std::max(64, NPOT);
  34. }
  35.  
  36. VCSMRenderTarget::VCSMRenderTarget (int Width, int Height, EGLDisplay Display)
  37. {
  38.     width = Width;
  39.     height = Height;
  40.     bufferWidth = nextPOT(width);
  41.     bufferHeight = nextPOT(height);
  42.     std::cout << "Texture Size " << width << "x" << height << std::endl;
  43.     std::cout << "Buffer Size " << bufferWidth << "x" << bufferHeight << std::endl;
  44.     eglDisplay = Display;
  45.     // Framebuffer
  46.     glGenFramebuffers(1, &FBO_ID);
  47.     glBindFramebuffer(GL_FRAMEBUFFER, FBO_ID);
  48.     // Create texture handle
  49.     glGenTextures(1, &TEX_ID);
  50.     glBindTexture(GL_TEXTURE_2D, TEX_ID);
  51.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR / GL_NEAREST
  52.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR / GL_NEAREST
  53.     // Allocate VCOS Shared Memory and assign to texture
  54.     VCSM.width = bufferWidth;
  55.     VCSM.height = bufferHeight;
  56.     eglImg = eglCreateImageKHR(eglDisplay, EGL_NO_CONTEXT, EGL_IMAGE_BRCM_VCSM, &VCSM, NULL);
  57.     CHECK_GL();
  58.     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, eglImg);
  59.     CHECK_GL();
  60.     // Bind texture to framebuffer
  61.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, TEX_ID, 0);
  62.     if (eglImg == EGL_NO_IMAGE_KHR || VCSM.vcsm_handle == 0)
  63.     {
  64.         std::cout << VCOS_FUNCTION << ": Failed to create EGL VCSM image\n";
  65.         return;
  66.     }
  67.     CHECK_GL();
  68.     // Check
  69.     if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  70.     {
  71.         std::cout << "Error: Framebuffer not complete: " << glCheckFramebufferStatus(GL_FRAMEBUFFER) << "\n";
  72.     }
  73.     glBindFramebuffer(GL_FRAMEBUFFER, 0);
  74. }
  75. VCSMRenderTarget::~VCSMRenderTarget (void)
  76. {
  77.     glDeleteTextures(1, &TEX_ID);
  78.     glDeleteFramebuffers(1, &FBO_ID);
  79.     eglDestroyImageKHR(eglDisplay, eglImg);
  80. }
  81. void VCSMRenderTarget::setTarget (void)
  82. {
  83.     glViewport(0, 0, width, height); // Important: only the actual texture, not full buffer
  84.     glBindFramebuffer(GL_FRAMEBUFFER, FBO_ID);
  85. }
  86. void VCSMRenderTarget::setSource (ShaderProgram *shader, int slot)
  87. {
  88.     glUniform1i(shader->uWidthAdr, bufferWidth);
  89.     glUniform1i(shader->uHeightAdr, bufferHeight);
  90.     glUniform1i(shader->uImageAdr, slot);
  91.     glActiveTexture(GL_TEXTURE0 + slot);
  92.     glBindTexture(GL_TEXTURE_2D, TEX_ID);
  93. }
  94. uint8_t* VCSMRenderTarget::lock (void)
  95. {
  96.     VCSM_CACHE_TYPE_T cacheType;
  97.     vcsmBuffer = (uint8_t*)vcsm_lock_cache(VCSM.vcsm_handle, VCSM_CACHE_TYPE_HOST, &cacheType);
  98.     if (!vcsmBuffer) std::cout << "Failed to lock VCSM buffer!\n";
  99.     return vcsmBuffer;
  100. }
  101. void VCSMRenderTarget::unlock (void)
  102. {
  103.     vcsm_unlock_ptr(vcsmBuffer);
  104. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top