Skip to content

Commit fae71ae

Browse files
PatriceJiangminggo
authored and
minggo
committed
[texture2d] impl texture format support (cocos2d#175)
* texture update * update * update texture * commit * compile on windows * ddd * rename * rename methods * no crash * save gl * save * save * rename * move out pixel format convert functions * metal crash * update * update android * support gles compressed texture format * support more compress format * add more conversion methods * ss * save * update conversion methods * add PVRTC format support * reformat * add marco linux * fix GL marcro * pvrtc supported only by ios 8.0+ * remove unused cmake * revert change * refactor Texture2D::initWithData * fix conversion log * refactor Texture2D::initWithData * remove some OpenGL constants for PVRTC * add todo * fix typo
1 parent 113ed67 commit fae71ae

37 files changed

+1706
-896
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,5 @@ cmake-build-*/
170170
cmake_build*
171171
metal-support*.zip
172172
**/.project
173+
174+
desktop.ini

cocos/2d/CCAutoPolygon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ AutoPolygon::AutoPolygon(const std::string &filename)
187187
_filename = filename;
188188
_image = new (std::nothrow) Image();
189189
_image->initWithImageFile(filename);
190-
CCASSERT(_image->getRenderFormat()==Texture2D::PixelFormat::RGBA8888, "unsupported format, currently only supports rgba8888");
190+
CCASSERT(_image->getPixelFormat()==Texture2D::PixelFormat::RGBA8888, "unsupported format, currently only supports rgba8888");
191191
_data = _image->getData();
192192
_width = _image->getWidth();
193193
_height = _image->getHeight();

cocos/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ include(storage/CMakeLists.txt)
5050

5151
# default value for cocos2dx extensions modules build
5252
option(BUILD_EDITOR_COCOSTUDIO "Build editor support for cocostudio" ON)
53-
option(BUILD_EDITOR_SPINE "Build editor support for spine" ON)
53+
option(BUILD_EDITOR_SPINE "Build editor support for spine" OFF)
5454
option(BUILD_EXTENSIONS "Build extension library" ON)
5555

5656
if(BUILD_EDITOR_COCOSTUDIO)

cocos/base/CCConfiguration.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,18 @@ std::string Configuration::getInfo() const
111111
return forDump.getDescription();
112112
}
113113

114+
#ifdef CC_USE_METAL
115+
void Configuration::gatherGPUInfo()
116+
{
117+
//support PVRTC/EAC/ETC2/ASTC/BC/YUV
118+
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
119+
_supportsPVRTC = true;
120+
#else
121+
_supportsPVRTC = false;
122+
#endif
123+
_supportsETC1 = false; //support etc2;
124+
}
125+
#else
114126
void Configuration::gatherGPUInfo()
115127
{
116128
_valueDict["gl.vendor"] = Value((const char*)glGetString(GL_VENDOR));
@@ -172,6 +184,8 @@ void Configuration::gatherGPUInfo()
172184
CHECK_GL_ERROR_DEBUG();
173185
}
174186

187+
#endif
188+
175189
Configuration* Configuration::getInstance()
176190
{
177191
if (! s_sharedConfiguration)

cocos/base/CCDirector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ void Director::setOpenGLView(GLView *openGLView)
371371
// Configuration. Gather GPU info
372372
Configuration *conf = Configuration::getInstance();
373373
//TODO: minggo
374-
// conf->gatherGPUInfo();
374+
conf->gatherGPUInfo();
375375
CCLOG("%s\n",conf->getInfo().c_str());
376376

377377
if(_openGLView)

cocos/base/CCNinePatchImageParser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ NinePatchImageParser::NinePatchImageParser(Image* image)
4848
,_isRotated(false)
4949
{
5050
this->_imageFrame = Rect(0,0,image->getWidth(), image->getHeight());
51-
CCASSERT(image->getRenderFormat()==Texture2D::PixelFormat::RGBA8888,
51+
CCASSERT(image->getPixelFormat()==Texture2D::PixelFormat::RGBA8888,
5252
"unsupported format, currently only supports rgba8888");
5353
}
5454

@@ -57,7 +57,7 @@ NinePatchImageParser::NinePatchImageParser(Image* image, const Rect& frame, bool
5757
,_imageFrame(frame)
5858
,_isRotated(rotated)
5959
{
60-
CCASSERT(image->getRenderFormat()==Texture2D::PixelFormat::RGBA8888,
60+
CCASSERT(image->getPixelFormat()==Texture2D::PixelFormat::RGBA8888,
6161
"unsupported format, currently only supports rgba8888");
6262
}
6363

@@ -192,7 +192,7 @@ Rect NinePatchImageParser::parseCapInset() const
192192
void NinePatchImageParser::setSpriteFrameInfo(Image* image, const cocos2d::Rect& frameRect, bool rotated )
193193
{
194194
this->_image = image;
195-
CCASSERT(image->getRenderFormat()==Texture2D::PixelFormat::RGBA8888,
195+
CCASSERT(image->getPixelFormat()==Texture2D::PixelFormat::RGBA8888,
196196
"unsupported format, currently only supports rgba8888");
197197
this->_imageFrame = frameRect;
198198
this->_isRotated = rotated;

cocos/platform/CCImage.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ Image::Image()
488488
, _height(0)
489489
, _unpack(false)
490490
, _fileType(Format::UNKNOWN)
491-
, _renderFormat(Texture2D::PixelFormat::NONE)
491+
, _pixelFormat(Texture2D::PixelFormat::NONE)
492492
, _numberOfMipmaps(0)
493493
, _hasPremultipliedAlpha(false)
494494
{
@@ -755,17 +755,17 @@ Image::Format Image::detectFormat(const unsigned char * data, ssize_t dataLen)
755755

756756
int Image::getBitPerPixel()
757757
{
758-
return Texture2D::getPixelFormatInfoMap().at(_renderFormat).bpp;
758+
return Texture2D::getPixelFormatInfoMap().at(_pixelFormat).bpp;
759759
}
760760

761761
bool Image::hasAlpha()
762762
{
763-
return Texture2D::getPixelFormatInfoMap().at(_renderFormat).alpha;
763+
return Texture2D::getPixelFormatInfoMap().at(_pixelFormat).alpha;
764764
}
765765

766766
bool Image::isCompressed()
767767
{
768-
return Texture2D::getPixelFormatInfoMap().at(_renderFormat).compressed;
768+
return Texture2D::getPixelFormatInfoMap().at(_pixelFormat).compressed;
769769
}
770770

771771
namespace
@@ -875,11 +875,11 @@ bool Image::initWithJpgData(const unsigned char * data, ssize_t dataLen)
875875
// we only support RGB or grayscale
876876
if (cinfo.jpeg_color_space == JCS_GRAYSCALE)
877877
{
878-
_renderFormat = Texture2D::PixelFormat::I8;
878+
_pixelFormat = Texture2D::PixelFormat::I8;
879879
}else
880880
{
881881
cinfo.out_color_space = JCS_RGB;
882-
_renderFormat = Texture2D::PixelFormat::RGB888;
882+
_pixelFormat = Texture2D::PixelFormat::RGB888;
883883
}
884884

885885
/* Start decompression jpeg here */
@@ -1003,16 +1003,16 @@ bool Image::initWithPngData(const unsigned char * data, ssize_t dataLen)
10031003
switch (color_type)
10041004
{
10051005
case PNG_COLOR_TYPE_GRAY:
1006-
_renderFormat = Texture2D::PixelFormat::I8;
1006+
_pixelFormat = Texture2D::PixelFormat::I8;
10071007
break;
10081008
case PNG_COLOR_TYPE_GRAY_ALPHA:
1009-
_renderFormat = Texture2D::PixelFormat::AI88;
1009+
_pixelFormat = Texture2D::PixelFormat::AI88;
10101010
break;
10111011
case PNG_COLOR_TYPE_RGB:
1012-
_renderFormat = Texture2D::PixelFormat::RGB888;
1012+
_pixelFormat = Texture2D::PixelFormat::RGB888;
10131013
break;
10141014
case PNG_COLOR_TYPE_RGB_ALPHA:
1015-
_renderFormat = Texture2D::PixelFormat::RGBA8888;
1015+
_pixelFormat = Texture2D::PixelFormat::RGBA8888;
10161016
break;
10171017
default:
10181018
break;
@@ -1202,7 +1202,7 @@ bool Image::initWithTiffData(const unsigned char * data, ssize_t dataLen)
12021202

12031203
npixels = w * h;
12041204

1205-
_renderFormat = Texture2D::PixelFormat::RGBA8888;
1205+
_pixelFormat = Texture2D::PixelFormat::RGBA8888;
12061206
_width = w;
12071207
_height = h;
12081208

@@ -1331,7 +1331,7 @@ bool Image::initWithPVRv2Data(const unsigned char * data, ssize_t dataLen)
13311331
return false;
13321332
}
13331333

1334-
_renderFormat = it->first;
1334+
_pixelFormat = it->first;
13351335
int bpp = it->second.bpp;
13361336

13371337
//Reset num of mipmaps
@@ -1475,7 +1475,7 @@ bool Image::initWithPVRv3Data(const unsigned char * data, ssize_t dataLen)
14751475
return false;
14761476
}
14771477

1478-
_renderFormat = it->first;
1478+
_pixelFormat = it->first;
14791479
int bpp = it->second.bpp;
14801480

14811481
// flags
@@ -1626,7 +1626,7 @@ bool Image::initWithETCData(const unsigned char * data, ssize_t dataLen)
16261626
{
16271627
//old opengl version has no define for GL_ETC1_RGB8_OES, add macro to make compiler happy.
16281628
#ifdef GL_ETC1_RGB8_OES
1629-
_renderFormat = Texture2D::PixelFormat::ETC;
1629+
_pixelFormat = Texture2D::PixelFormat::ETC;
16301630
_dataLen = dataLen - ETC_PKM_HEADER_SIZE;
16311631
_data = static_cast<unsigned char*>(malloc(_dataLen * sizeof(unsigned char)));
16321632
memcpy(_data, static_cast<const unsigned char*>(data) + ETC_PKM_HEADER_SIZE, _dataLen);
@@ -1642,7 +1642,7 @@ bool Image::initWithETCData(const unsigned char * data, ssize_t dataLen)
16421642
//if it is not gles or device do not support ETC, decode texture by software
16431643
int bytePerPixel = 3;
16441644
unsigned int stride = _width * bytePerPixel;
1645-
_renderFormat = Texture2D::PixelFormat::RGB888;
1645+
_pixelFormat = Texture2D::PixelFormat::RGB888;
16461646

16471647
_dataLen = _width * _height * bytePerPixel;
16481648
_data = static_cast<unsigned char*>(malloc(_dataLen * sizeof(unsigned char)));
@@ -1677,15 +1677,15 @@ bool Image::initWithTGAData(tImageTGA* tgaData)
16771677
// unsupported RGB555
16781678
if (tgaData->pixelDepth == 16)
16791679
{
1680-
_renderFormat = Texture2D::PixelFormat::RGB5A1;
1680+
_pixelFormat = Texture2D::PixelFormat::RGB5A1;
16811681
}
16821682
else if(tgaData->pixelDepth == 24)
16831683
{
1684-
_renderFormat = Texture2D::PixelFormat::RGB888;
1684+
_pixelFormat = Texture2D::PixelFormat::RGB888;
16851685
}
16861686
else if(tgaData->pixelDepth == 32)
16871687
{
1688-
_renderFormat = Texture2D::PixelFormat::RGBA8888;
1688+
_pixelFormat = Texture2D::PixelFormat::RGBA8888;
16891689
}
16901690
else
16911691
{
@@ -1698,7 +1698,7 @@ bool Image::initWithTGAData(tImageTGA* tgaData)
16981698
// gray
16991699
if (8 == tgaData->pixelDepth)
17001700
{
1701-
_renderFormat = Texture2D::PixelFormat::I8;
1701+
_pixelFormat = Texture2D::PixelFormat::I8;
17021702
}
17031703
else
17041704
{
@@ -1796,18 +1796,18 @@ bool Image::initWithS3TCData(const unsigned char * data, ssize_t dataLen)
17961796

17971797
if (FOURCC_DXT1 == header->ddsd.DUMMYUNIONNAMEN4.ddpfPixelFormat.fourCC)
17981798
{
1799-
_renderFormat = Texture2D::PixelFormat::S3TC_DXT1;
1799+
_pixelFormat = Texture2D::PixelFormat::S3TC_DXT1;
18001800
}
18011801
else if (FOURCC_DXT3 == header->ddsd.DUMMYUNIONNAMEN4.ddpfPixelFormat.fourCC)
18021802
{
1803-
_renderFormat = Texture2D::PixelFormat::S3TC_DXT3;
1803+
_pixelFormat = Texture2D::PixelFormat::S3TC_DXT3;
18041804
}
18051805
else if (FOURCC_DXT5 == header->ddsd.DUMMYUNIONNAMEN4.ddpfPixelFormat.fourCC)
18061806
{
1807-
_renderFormat = Texture2D::PixelFormat::S3TC_DXT5;
1807+
_pixelFormat = Texture2D::PixelFormat::S3TC_DXT5;
18081808
}
18091809
} else { //will software decode
1810-
_renderFormat = Texture2D::PixelFormat::RGBA8888;
1810+
_pixelFormat = Texture2D::PixelFormat::RGBA8888;
18111811
}
18121812

18131813
/* load the mipmaps */
@@ -1945,13 +1945,13 @@ bool Image::initWithATITCData(const unsigned char *data, ssize_t dataLen)
19451945
switch (header->glInternalFormat)
19461946
{
19471947
case CC_GL_ATC_RGB_AMD:
1948-
_renderFormat = Texture2D::PixelFormat::ATC_RGB;
1948+
_pixelFormat = Texture2D::PixelFormat::ATC_RGB;
19491949
break;
19501950
case CC_GL_ATC_RGBA_EXPLICIT_ALPHA_AMD:
1951-
_renderFormat = Texture2D::PixelFormat::ATC_EXPLICIT_ALPHA;
1951+
_pixelFormat = Texture2D::PixelFormat::ATC_EXPLICIT_ALPHA;
19521952
break;
19531953
case CC_GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD:
1954-
_renderFormat = Texture2D::PixelFormat::ATC_INTERPOLATED_ALPHA;
1954+
_pixelFormat = Texture2D::PixelFormat::ATC_INTERPOLATED_ALPHA;
19551955
break;
19561956
default:
19571957
break;
@@ -1968,7 +1968,7 @@ bool Image::initWithATITCData(const unsigned char *data, ssize_t dataLen)
19681968

19691969
int bytePerPixel = 4;
19701970
unsigned int stride = width * bytePerPixel;
1971-
_renderFormat = Texture2D::PixelFormat::RGBA8888;
1971+
_pixelFormat = Texture2D::PixelFormat::RGBA8888;
19721972

19731973
std::vector<unsigned char> decodeImageData(stride * height);
19741974
switch (header->glInternalFormat)
@@ -2019,7 +2019,7 @@ bool Image::initWithWebpData(const unsigned char * data, ssize_t dataLen)
20192019
if (config.input.width == 0 || config.input.height == 0) break;
20202020

20212021
config.output.colorspace = config.input.has_alpha?MODE_rgbA:MODE_RGB;
2022-
_renderFormat = config.input.has_alpha?Texture2D::PixelFormat::RGBA8888:Texture2D::PixelFormat::RGB888;
2022+
_pixelFormat = config.input.has_alpha?Texture2D::PixelFormat::RGBA8888:Texture2D::PixelFormat::RGB888;
20232023
_width = config.input.width;
20242024
_height = config.input.height;
20252025

@@ -2061,7 +2061,7 @@ bool Image::initWithRawData(const unsigned char * data, ssize_t /*dataLen*/, int
20612061
_height = height;
20622062
_width = width;
20632063
_hasPremultipliedAlpha = preMulti;
2064-
_renderFormat = Texture2D::PixelFormat::RGBA8888;
2064+
_pixelFormat = Texture2D::PixelFormat::RGBA8888;
20652065

20662066
// only RGBA8888 supported
20672067
int bytesPerComponent = 4;
@@ -2081,7 +2081,7 @@ bool Image::initWithRawData(const unsigned char * data, ssize_t /*dataLen*/, int
20812081
bool Image::saveToFile(const std::string& filename, bool isToRGB)
20822082
{
20832083
//only support for Texture2D::PixelFormat::RGB888 or Texture2D::PixelFormat::RGBA8888 uncompressed data
2084-
if (isCompressed() || (_renderFormat != Texture2D::PixelFormat::RGB888 && _renderFormat != Texture2D::PixelFormat::RGBA8888))
2084+
if (isCompressed() || (_pixelFormat != Texture2D::PixelFormat::RGB888 && _pixelFormat != Texture2D::PixelFormat::RGBA8888))
20852085
{
20862086
CCLOG("cocos2d: Image: saveToFile is only support for Texture2D::PixelFormat::RGB888 or Texture2D::PixelFormat::RGBA8888 uncompressed data for now");
20872087
return false;
@@ -2338,7 +2338,7 @@ void Image::premultipliedAlpha()
23382338
_hasPremultipliedAlpha = false;
23392339
return;
23402340
#else
2341-
CCASSERT(_renderFormat == Texture2D::PixelFormat::RGBA8888, "The pixel format should be RGBA8888!");
2341+
CCASSERT(_pixelFormat == Texture2D::PixelFormat::RGBA8888, "The pixel format should be RGBA8888!");
23422342

23432343
unsigned int* fourBytes = (unsigned int*)_data;
23442344
for(int i = 0; i < _width * _height; i++)

cocos/platform/CCImage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class CC_DLL Image : public Ref
136136
unsigned char * getData() { return _data; }
137137
ssize_t getDataLen() { return _dataLen; }
138138
Format getFileType() { return _fileType; }
139-
Texture2D::PixelFormat getRenderFormat() { return _renderFormat; }
139+
Texture2D::PixelFormat getPixelFormat() { return _pixelFormat; }
140140
int getWidth() { return _width; }
141141
int getHeight() { return _height; }
142142
int getNumberOfMipmaps() { return _numberOfMipmaps; }
@@ -191,7 +191,7 @@ class CC_DLL Image : public Ref
191191
int _height;
192192
bool _unpack;
193193
Format _fileType;
194-
Texture2D::PixelFormat _renderFormat;
194+
Texture2D::PixelFormat _pixelFormat;
195195
MipmapInfo _mipmaps[MIPMAP_MAX]; // pointer to mipmap images
196196
int _numberOfMipmaps;
197197
// false if we can't auto detect the image is premultiplied or not.

cocos/platform/ios/CCImage-ios.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal
4141
bool cocos2d::Image::saveToFile(const std::string& filename, bool isToRGB)
4242
{
4343
//only support for Texture2D::PixelFormat::RGB888 or Texture2D::PixelFormat::RGBA8888 uncompressed data
44-
if (isCompressed() || (_renderFormat != Texture2D::PixelFormat::RGB888 && _renderFormat != Texture2D::PixelFormat::RGBA8888))
44+
if (isCompressed() || (_pixelFormat != Texture2D::PixelFormat::RGB888 && _pixelFormat != Texture2D::PixelFormat::RGBA8888))
4545
{
4646
CCLOG("cocos2d: Image: saveToFile is only support for Texture2D::PixelFormat::RGB888 or Texture2D::PixelFormat::RGBA8888 uncompressed data for now");
4747
return false;

cocos/renderer/CCMaterial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ bool Material::parseSampler(GLProgramState* glProgramState, Properties* samplerP
285285
CCLOG("Invalid magFilter: %s", magFilter);
286286

287287
// TODO coulsonwang
288-
// texture->setTexParameters(texParams);
288+
// texture->setSamplerDescriptor(texParams);
289289
}
290290

291291
glProgramState->setUniformTexture(samplerProperties->getId(), texture);

0 commit comments

Comments
 (0)