axmol/cocos/renderer/backend/opengl/TextureGL.cpp

443 lines
15 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2018-2019 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "TextureGL.h"
#include "base/ccMacros.h"
#include "base/CCEventListenerCustom.h"
#include "base/CCEventDispatcher.h"
#include "base/CCEventType.h"
#include "base/CCDirector.h"
#include "platform/CCPlatformConfig.h"
#include "renderer/backend/opengl/UtilsGL.h"
CC_BACKEND_BEGIN
#define ISPOW2(n) (((n) & (n-1)) == 0)
namespace {
bool isMipmapEnabled(GLint filter)
{
switch(filter)
{
case GL_LINEAR_MIPMAP_LINEAR:
case GL_LINEAR_MIPMAP_NEAREST:
case GL_NEAREST_MIPMAP_NEAREST:
case GL_NEAREST_MIPMAP_LINEAR:
return true;
default:
break;
}
return false;
}
}
2020-02-15 02:36:02 +08:00
void TextureInfoGL::applySampler(const SamplerDescriptor& descriptor, bool isPow2, bool hasMipmaps, GLenum target)
2019-11-23 20:27:39 +08:00
{
if (descriptor.magFilter != SamplerFilter::DONT_CARE)
{
magFilterGL = UtilsGL::toGLMagFilter(descriptor.magFilter);
}
if (descriptor.minFilter != SamplerFilter::DONT_CARE)
{
minFilterGL = UtilsGL::toGLMinFilter(descriptor.minFilter, hasMipmaps, isPow2);
}
if (descriptor.sAddressMode != SamplerAddressMode::DONT_CARE)
{
sAddressModeGL = UtilsGL::toGLAddressMode(descriptor.sAddressMode, isPow2);
}
if (descriptor.tAddressMode != SamplerAddressMode::DONT_CARE)
{
tAddressModeGL = UtilsGL::toGLAddressMode(descriptor.tAddressMode, isPow2);
}
2020-02-15 02:36:02 +08:00
// apply sampler for all internal textures
foreach([=](GLuint texID, int index) {
apply(index, target);
setCurrentTexParameters(target);
glBindTexture(target, 0); // unbind
});
2019-11-23 20:27:39 +08:00
}
2020-02-15 02:36:02 +08:00
void TextureInfoGL::setCurrentTexParameters(GLenum target)
2019-11-23 20:27:39 +08:00
{
2020-02-15 02:36:02 +08:00
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, minFilterGL);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, magFilterGL);
glTexParameteri(target, GL_TEXTURE_WRAP_S, sAddressModeGL);
glTexParameteri(target, GL_TEXTURE_WRAP_T, tAddressModeGL);
}
void TextureInfoGL::apply(int index, GLenum target) const
{
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(target, index < CC_META_TEXTURES ? textures[index] : textures[0]);
}
GLuint TextureInfoGL::ensure(int index, GLenum target)
{
if (index >= CC_META_TEXTURES) return 0;
glActiveTexture(GL_TEXTURE0 + index);
auto& texID = this->textures[index];
if (!texID)
glGenTextures(1, &texID);
glBindTexture(target, texID);
setCurrentTexParameters(target); // set once
if (this->maxIdx < index) this->maxIdx = index;
2019-11-23 20:27:39 +08:00
2020-02-15 02:36:02 +08:00
return texID;
}
2020-02-15 02:58:41 +08:00
Texture2DGL::Texture2DGL(const TextureDescriptor& descriptor)
2020-02-15 02:36:02 +08:00
{
2019-11-23 20:27:39 +08:00
updateTextureDescriptor(descriptor);
#if CC_ENABLE_CACHE_TEXTURE_DATA
// Listen this event to restored texture id after coming to foreground on Android.
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*){
2020-02-15 03:10:01 +08:00
glGenTextures(1, &(_textureInfo.textures[0]));
_textureInfo.apply(0);
_textureInfo.setCurrentTexParameters(GL_TEXTURE_2D);
2020-02-14 15:18:56 +08:00
// this->initWithZeros();
2019-11-23 20:27:39 +08:00
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
void Texture2DGL::initWithZeros()
{
auto size = _width * _height * _bitsPerElement / 8;
uint8_t* data = (uint8_t*)malloc(size);
memset(data, 0, size);
updateData(data, _width, _height, 0);
free(data);
}
void Texture2DGL::updateTextureDescriptor(const cocos2d::backend::TextureDescriptor &descriptor, int index)
{
TextureBackend::updateTextureDescriptor(descriptor, index);
2020-02-15 02:58:41 +08:00
2019-11-23 20:27:39 +08:00
UtilsGL::toGLTypes(descriptor.textureFormat, _textureInfo.internalFormat, _textureInfo.format, _textureInfo.type, _isCompressed);
bool isPow2 = ISPOW2(_width) && ISPOW2(_height);
_textureInfo.magFilterGL = UtilsGL::toGLMagFilter(descriptor.samplerDescriptor.magFilter);
_textureInfo.minFilterGL = UtilsGL::toGLMinFilter(descriptor.samplerDescriptor.minFilter, _hasMipmaps, isPow2);
_textureInfo.sAddressModeGL = UtilsGL::toGLAddressMode(descriptor.samplerDescriptor.sAddressMode, isPow2);
_textureInfo.tAddressModeGL = UtilsGL::toGLAddressMode(descriptor.samplerDescriptor.tAddressMode, isPow2);
2020-02-15 02:36:02 +08:00
updateSamplerDescriptor(descriptor.samplerDescriptor);
2019-11-23 20:27:39 +08:00
// Update data here because `updateData()` may not be invoked later.
// For example, a texture used as depth buffer will not invoke updateData().
2020-02-14 15:18:56 +08:00
// FIXME, Don't call, now it's unused, when the texture is compressed, initWithZeros will cause GL Error: 0x501
// initWithZeros();
2019-11-23 20:27:39 +08:00
}
Texture2DGL::~Texture2DGL()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
2020-02-15 02:36:02 +08:00
void Texture2DGL::updateSamplerDescriptor(const SamplerDescriptor &sampler) {
2019-11-23 20:27:39 +08:00
bool isPow2 = ISPOW2(_width) && ISPOW2(_height);
2020-02-15 02:36:02 +08:00
_textureInfo.applySampler(sampler, isPow2, _hasMipmaps, GL_TEXTURE_2D);
2019-11-23 20:27:39 +08:00
}
void Texture2DGL::updateData(uint8_t* data, std::size_t width , std::size_t height, std::size_t level, int index)
{
2020-02-15 02:36:02 +08:00
if (!_textureInfo.ensure(index)) return;
2020-02-14 15:18:56 +08:00
2019-11-23 20:27:39 +08:00
//Set the row align only when mipmapsNum == 1 and the data is uncompressed
auto mipmapEnalbed = isMipmapEnabled(_textureInfo.minFilterGL) || isMipmapEnabled(_textureInfo.magFilterGL);
if(!mipmapEnalbed)
{
unsigned int bytesPerRow = width * _bitsPerElement / 8;
if(bytesPerRow % 8 == 0)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 8);
}
else if(bytesPerRow % 4 == 0)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
else if(bytesPerRow % 2 == 0)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
}
else
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
}
else
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
2020-02-14 15:18:56 +08:00
CHECK_GL_ERROR_DEBUG();
2019-11-23 20:27:39 +08:00
glTexImage2D(GL_TEXTURE_2D,
level,
_textureInfo.internalFormat,
width,
height,
0,
_textureInfo.format,
_textureInfo.type,
data);
CHECK_GL_ERROR_DEBUG();
if(!_hasMipmaps && level > 0)
_hasMipmaps = true;
}
void Texture2DGL::updateCompressedData(uint8_t *data, std::size_t width, std::size_t height,
std::size_t dataLen, std::size_t level, int index)
2019-11-23 20:27:39 +08:00
{
2020-02-15 02:36:02 +08:00
if (!_textureInfo.ensure(index)) return;
2019-11-23 20:27:39 +08:00
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glCompressedTexImage2D(GL_TEXTURE_2D,
level,
_textureInfo.internalFormat,
(GLsizei)width,
(GLsizei)height,
0,
dataLen,
data);
CHECK_GL_ERROR_DEBUG();
if(!_hasMipmaps && level > 0)
_hasMipmaps = true;
}
void Texture2DGL::updateSubData(std::size_t xoffset, std::size_t yoffset, std::size_t width, std::size_t height, std::size_t level, uint8_t* data, int index)
{
2020-02-15 02:36:02 +08:00
if (!_textureInfo.ensure(index)) return;
2019-11-23 20:27:39 +08:00
glTexSubImage2D(GL_TEXTURE_2D,
level,
xoffset,
yoffset,
width,
height,
_textureInfo.format,
_textureInfo.type,
data);
CHECK_GL_ERROR_DEBUG();
if(!_hasMipmaps && level > 0)
_hasMipmaps = true;
}
void Texture2DGL::updateCompressedSubData(std::size_t xoffset, std::size_t yoffset, std::size_t width,
std::size_t height, std::size_t dataLen, std::size_t level,
uint8_t *data, int index)
2019-11-23 20:27:39 +08:00
{
2020-02-15 02:36:02 +08:00
if (!_textureInfo.ensure(index)) return;
2019-11-23 20:27:39 +08:00
glCompressedTexSubImage2D(GL_TEXTURE_2D,
level,
xoffset,
yoffset,
width,
height,
_textureInfo.format,
dataLen,
data);
CHECK_GL_ERROR_DEBUG();
if(!_hasMipmaps && level > 0)
_hasMipmaps = true;
}
void Texture2DGL::generateMipmaps()
{
if (TextureUsage::RENDER_TARGET == _textureUsage)
return;
if(!_hasMipmaps)
{
_hasMipmaps = true;
2020-02-15 02:36:02 +08:00
glBindTexture(GL_TEXTURE_2D, this->getHandler());
2019-11-23 20:27:39 +08:00
glGenerateMipmap(GL_TEXTURE_2D);
}
}
void Texture2DGL::getBytes(std::size_t x, std::size_t y, std::size_t width, std::size_t height, bool flipImage, std::function<void(const unsigned char*, std::size_t, std::size_t)> callback)
{
GLint defaultFBO = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
GLuint frameBuffer = 0;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
2020-02-15 02:36:02 +08:00
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->getHandler(), 0);
2019-11-23 20:27:39 +08:00
glPixelStorei(GL_PACK_ALIGNMENT, 1);
auto bytePerRow = width * _bitsPerElement / 8;
unsigned char* image = new unsigned char[bytePerRow * height];
glReadPixels(x,y,width, height,GL_RGBA,GL_UNSIGNED_BYTE, image);
if(flipImage)
{
unsigned char* flippedImage = new unsigned char[bytePerRow * height];
for (int i = 0; i < height; ++i)
{
memcpy(&flippedImage[i * bytePerRow],
&image[(height - i - 1) * bytePerRow],
bytePerRow);
}
callback(flippedImage, width, height);
CC_SAFE_DELETE_ARRAY(flippedImage);
} else
{
callback(image, width, height);
CC_SAFE_DELETE_ARRAY(image);
}
glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
glDeleteFramebuffers(1, &frameBuffer);
}
TextureCubeGL::TextureCubeGL(const TextureDescriptor& descriptor)
{
assert(_width == _height);
_textureType = TextureType::TEXTURE_CUBE;
2020-02-15 02:58:41 +08:00
updateTextureDescriptor(descriptor);
2019-11-23 20:27:39 +08:00
#if CC_ENABLE_CACHE_TEXTURE_DATA
// Listen this event to restored texture id after coming to foreground on Android.
_backToForegroundListener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom*){
2020-02-15 03:10:01 +08:00
glGenTextures(1, &(_textureInfo.textures[0]));
_textureInfo.apply(0);
_textureInfo.setCurrentTexParameters(GL_TEXTURE_CUBE_MAP);
2019-11-23 20:27:39 +08:00
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
CHECK_GL_ERROR_DEBUG();
}
void TextureCubeGL::updateTextureDescriptor(const cocos2d::backend::TextureDescriptor &descriptor, int index)
{
2020-02-15 02:58:41 +08:00
backend::TextureCubemapBackend::updateTextureDescriptor(descriptor, index);
2019-11-23 20:27:39 +08:00
UtilsGL::toGLTypes(descriptor.textureFormat, _textureInfo.internalFormat, _textureInfo.format, _textureInfo.type, _isCompressed);
2020-02-15 02:58:41 +08:00
2020-02-15 02:36:02 +08:00
updateSamplerDescriptor(descriptor.samplerDescriptor);
2019-11-23 20:27:39 +08:00
}
TextureCubeGL::~TextureCubeGL()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
2020-02-15 02:36:02 +08:00
void TextureCubeGL::updateSamplerDescriptor(const SamplerDescriptor &sampler)
2019-11-23 20:27:39 +08:00
{
2020-02-15 02:36:02 +08:00
_textureInfo.applySampler(sampler, true, _hasMipmaps, GL_TEXTURE_CUBE_MAP);
2019-11-23 20:27:39 +08:00
}
2020-02-15 02:36:02 +08:00
void TextureCubeGL::updateFaceData(TextureCubeFace side, void *data, int index)
{
if (!_textureInfo.ensure(index, GL_TEXTURE_CUBE_MAP)) return;
2019-11-23 20:27:39 +08:00
CHECK_GL_ERROR_DEBUG();
int i = static_cast<int>(side);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, // level
GL_RGBA, // internal format
_width, // width
_height, // height
0, // border
_textureInfo.internalFormat, // format
_textureInfo.type, // type
data); // pixel data
CHECK_GL_ERROR_DEBUG();
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
void TextureCubeGL::getBytes(std::size_t x, std::size_t y, std::size_t width, std::size_t height, bool flipImage, std::function<void(const unsigned char*, std::size_t, std::size_t)> callback)
{
GLint defaultFBO = 0;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
GLuint frameBuffer = 0;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
2020-02-15 02:36:02 +08:00
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP, this->getHandler(), 0);
2019-11-23 20:27:39 +08:00
glPixelStorei(GL_PACK_ALIGNMENT, 1);
auto bytePerRow = width * _bitsPerElement / 8;
unsigned char* image = new unsigned char[bytePerRow * height];
glReadPixels(x,y,width, height,GL_RGBA,GL_UNSIGNED_BYTE, image);
if(flipImage)
{
unsigned char* flippedImage = new unsigned char[bytePerRow * height];
for (int i = 0; i < height; ++i)
{
memcpy(&flippedImage[i * bytePerRow],
&image[(height - i - 1) * bytePerRow],
bytePerRow);
}
callback(flippedImage, width, height);
CC_SAFE_DELETE_ARRAY(flippedImage);
} else
{
callback(image, width, height);
CC_SAFE_DELETE_ARRAY(image);
}
glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
glDeleteFramebuffers(1, &frameBuffer);
}
void TextureCubeGL::generateMipmaps()
{
if (TextureUsage::RENDER_TARGET == _textureUsage)
return;
if(!_hasMipmaps)
{
_hasMipmaps = true;
2020-02-15 02:36:02 +08:00
glBindTexture(GL_TEXTURE_CUBE_MAP, this->getHandler());
2019-11-23 20:27:39 +08:00
glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
}
}
CC_BACKEND_END