[Tizen] Using Depth and Stencil buffer & texture
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-texture.cpp
index 7e0af70..d6783a1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2020 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -517,6 +517,28 @@ void PixelFormatToGl( Pixel::Format pixelFormat, GLenum& glFormat, GLint& glInte
       break;
     }
 
+    // GLES 3.0 depth and stencil formats
+    case Pixel::DEPTH_UNSIGNED_INT:
+    {
+      glFormat = GL_DEPTH_COMPONENT;
+      pixelDataType = GL_UNSIGNED_INT;
+      break;
+    }
+
+    case Pixel::DEPTH_FLOAT:
+    {
+      glFormat = GL_DEPTH_COMPONENT;
+      pixelDataType = GL_FLOAT;
+      break;
+    }
+
+    case Pixel::DEPTH_STENCIL:
+    {
+      glFormat = GL_DEPTH_STENCIL;
+      pixelDataType = GL_UNSIGNED_INT_24_8;
+      break;
+    }
+
     case Pixel::INVALID:
     {
       DALI_LOG_ERROR( "Invalid pixel format for bitmap\n" );
@@ -533,6 +555,16 @@ void PixelFormatToGl( Pixel::Format pixelFormat, GLenum& glFormat, GLint& glInte
       glInternalFormat = GL_R11F_G11F_B10F;
       break;
     }
+    case Pixel::DEPTH_FLOAT:
+    {
+      glInternalFormat = GL_DEPTH_COMPONENT32F;
+      break;
+    }
+    case Pixel::DEPTH_STENCIL:
+    {
+      glInternalFormat = GL_DEPTH24_STENCIL8;
+      break;
+    }
     default:
     {
       glInternalFormat = glFormat;
@@ -568,6 +600,9 @@ bool IsCompressedFormat(Pixel::Format pixelFormat)
     case Pixel::BGRA8888:
     case Pixel::RGB16F:
     case Pixel::RGB32F:
+    case Pixel::DEPTH_UNSIGNED_INT:
+    case Pixel::DEPTH_FLOAT:
+    case Pixel::DEPTH_STENCIL:
     case Pixel::INVALID:
     {
       return false;
@@ -653,8 +688,8 @@ Texture::Texture( NativeImageInterfacePtr nativeImageInterface )
   mGlInternalFormat( GL_RGB ),
   mGlFormat( GL_RGB ),
   mPixelDataType( GL_UNSIGNED_BYTE ),
-  mWidth( nativeImageInterface->GetWidth() ),
-  mHeight( nativeImageInterface->GetHeight() ),
+  mWidth( static_cast<uint16_t >( nativeImageInterface->GetWidth() ) ), // ignoring overflow, not happening in practice
+  mHeight( static_cast<uint16_t >( nativeImageInterface->GetHeight() ) ), // ignoring overflow, not happening in practice
   mMaxMipMapLevel( 0 ),
   mType( TextureType::TEXTURE_2D ),
   mHasAlpha( nativeImageInterface->RequiresBlending() ),
@@ -742,14 +777,14 @@ void Texture::Initialize(Context& context)
     {
       if( !mIsCompressed )
       {
-        for( unsigned int i(0); i<6; ++i )
+        for( uint32_t i(0); i<6; ++i )
         {
           context.TexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, mWidth, mHeight, 0, mGlFormat, mPixelDataType, 0 );
         }
       }
       else
       {
-        for( unsigned int i(0); i<6; ++i )
+        for( uint32_t i(0); i<6; ++i )
         {
           context.CompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, mGlInternalFormat, mWidth, mHeight, 0, 0, 0 );
         }
@@ -764,10 +799,10 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
   DALI_ASSERT_ALWAYS( mNativeImage == NULL );
 
   //Get pointer to the data of the PixelData object
-  unsigned char* buffer( pixelData->GetBuffer() );
+  uint8_t* buffer( pixelData->GetBuffer() );
 
   //This buffer is only used if manually converting from RGB to RGBA
-  unsigned char* tempBuffer(0);
+  std::vector< uint8_t > tempBuffer;
 
   //Retrieves the pixel data element type, the gl format and gl internal format of the data contained in the PixelData object.
   GLenum glFormat;
@@ -784,17 +819,12 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
                             ( params.width  != ( mWidth  / ( 1 << params.mipmap ) ) ) ||
                             ( params.height != ( mHeight / ( 1 << params.mipmap ) ) ) );
 
-  bool convert = ( ( glFormat == GL_RGB ) && ( mGlFormat == GL_RGBA ) );
-#if DALI_GLES_VERSION >= 30
-  // Don't convert manually from RGB to RGBA if GLES >= 3.0 and a sub-image is uploaded.
-  convert = convert && !isSubImage;
-#endif
-
-  if( convert )
+  if( context.TextureRequiresConverting( glFormat, mGlFormat, isSubImage ) )
   {
-    size_t dataSize = static_cast< size_t >( params.width ) * params.height;
-    tempBuffer = new unsigned char[dataSize*4u];
-    for( size_t i(0u); i<dataSize; i++ )
+    uint32_t dataSize = static_cast< uint32_t >( params.width ) * params.height;
+    //reserve() does not allocate the memory on some systems so can crash if not populated using push_back
+    tempBuffer.resize( dataSize * 4u );
+    for( uint32_t i = 0u; i < dataSize; ++i )
     {
       tempBuffer[i*4u]   = buffer[i*3u];
       tempBuffer[i*4u+1] = buffer[i*3u+1];
@@ -802,7 +832,7 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
       tempBuffer[i*4u+3] = 0xFF;
     }
 
-    buffer = tempBuffer;
+    buffer = &tempBuffer[0];
     glFormat = mGlFormat; // Set the glFormat to GL_RGBA
   }
 
@@ -826,7 +856,7 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
     }
     else
     {
-      context.CompressedTexImage2D( target, params.mipmap, mGlInternalFormat, params.width, params.height, 0, pixelData->GetBufferSize(), buffer );
+      context.CompressedTexImage2D( target, params.mipmap, mGlInternalFormat, params.width, params.height, 0, static_cast<GLsizei>( pixelData->GetBufferSize() ), buffer );
     }
   }
   else
@@ -842,16 +872,12 @@ void Texture::Upload( Context& context, PixelDataPtr pixelData, const Internal::
     {
       context.CompressedTexSubImage2D( target, params.mipmap,
                                        params.xOffset, params.yOffset, params.width, params.height,
-                                       glFormat, pixelData->GetBufferSize(), buffer );
+                                       glFormat, static_cast<GLsizei>( pixelData->GetBufferSize() ), buffer );
     }
   }
-
-
-  //Destroy temp buffer used for conversion RGB->RGBA
-  delete[] tempBuffer;
 }
 
-bool Texture::Bind( Context& context, unsigned int textureUnit, Render::Sampler* sampler )
+bool Texture::Bind( Context& context, uint32_t textureUnit, Render::Sampler* sampler )
 {
   if( mNativeImage && mId == 0 )
   {
@@ -860,8 +886,7 @@ bool Texture::Bind( Context& context, unsigned int textureUnit, Render::Sampler*
 
   if( mId != 0 )
   {
-    context.ActiveTexture( static_cast<TextureUnit>(textureUnit) );
-    context.BindTexture( mTarget, mId );
+    context.BindTextureForUnit( static_cast<TextureUnit>( textureUnit ), mTarget, mId );
     ApplySampler( context, sampler );
 
     if( mNativeImage )
@@ -915,18 +940,14 @@ void Texture::ApplySampler( Context& context, Render::Sampler* sampler )
       }
     }
 
-#if DALI_GLES_VERSION >= 30
-    //In GLES 3.0 we do not need to upload all of the mipmap levels, but GL_TEXTURE_MAX_LEVEL must be set
     if(mMaxMipMapLevel)
     {
       context.TexParameteri( mTarget, GL_TEXTURE_MAX_LEVEL, mMaxMipMapLevel );
     }
-#endif
-
   }
 }
 
-bool Texture::HasAlphaChannel()
+bool Texture::HasAlphaChannel() const
 {
   return mHasAlpha;
 }