Skip to content

Commit 8a699f4

Browse files
j-jorgehalx99
authored andcommitted
Remove the default pixel format, fix rendering of grayscale PNGs (#2573)
* Add AXMOL_AUTOTEST_CAPTURE_DIR for cpp-tests. In auto test mode, cpp-tests will save a capture of the scene in the directory whose path is given by environment variable AXMOL_AUTOTEST_CAPTURE_DIR, if such a path is provided. * Check the value of AXMOL_START_AUTOTEST, ignore it if zero. * Remove the default pixel format from Texture2D, use the source format. * Handle gray+alpha PNG images as gray+alpha sprites.
1 parent bde204d commit 8a699f4

File tree

25 files changed

+3641
-3754
lines changed

25 files changed

+3641
-3754
lines changed

core/2d/AnchoredSprite.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ AnchoredSprite* AnchoredSprite::createWithTexture(Texture2D* texture, const Rect
7171

7272
AnchoredSprite* AnchoredSprite::create(std::string_view filename)
7373
{
74-
return AnchoredSprite::create(filename, Texture2D::getDefaultAlphaPixelFormat());
74+
return AnchoredSprite::create(filename, PixelFormat::NONE);
7575
}
7676

7777
AnchoredSprite* AnchoredSprite::create(std::string_view filename, PixelFormat format)

core/2d/Sprite.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Sprite* Sprite::createWithTexture(Texture2D* texture, const Rect& rect, bool rot
7575

7676
Sprite* Sprite::create(std::string_view filename)
7777
{
78-
return Sprite::create(filename, Texture2D::getDefaultAlphaPixelFormat());
78+
return Sprite::create(filename, PixelFormat::NONE);
7979
}
8080

8181
Sprite* Sprite::create(std::string_view filename, PixelFormat format)
@@ -187,7 +187,7 @@ bool Sprite::initWithTexture(Texture2D* texture, const Rect& rect)
187187

188188
bool Sprite::initWithFile(std::string_view filename)
189189
{
190-
return initWithFile(filename, Texture2D::getDefaultAlphaPixelFormat());
190+
return initWithFile(filename, PixelFormat::NONE);
191191
}
192192

193193
bool Sprite::initWithFile(std::string_view filename, PixelFormat format)
@@ -434,7 +434,25 @@ void Sprite::setTexture(Texture2D* texture)
434434
}
435435

436436
if (needsUpdatePS)
437-
setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
437+
{
438+
const PixelFormat pixelFormat = _texture->getPixelFormat();
439+
440+
switch(pixelFormat)
441+
{
442+
case PixelFormat::R8:
443+
setProgramState(backend::ProgramType::POSITION_TEXTURE_GRAY);
444+
break;
445+
case PixelFormat::RG8:
446+
setProgramState(backend::ProgramType::POSITION_TEXTURE_GRAY_ALPHA);
447+
break;
448+
case PixelFormat::RGBA8:
449+
setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
450+
break;
451+
default:
452+
AXLOGW("Warning: Sprite::setTexture() unhandled pixel format {}", (int)pixelFormat);
453+
setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
454+
}
455+
}
438456
else
439457
updateProgramStateTexture(_texture);
440458
}

core/base/Director.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,6 @@ void Director::setDefaultValues()
229229
else
230230
AXASSERT(false, "Invalid projection value");
231231

232-
// Default pixel format for PNG images with alpha
233-
std::string pixel_format = conf->getValue("axmol.texture.pixel_format_for_png", Value("rgba8888")).asString();
234-
if (pixel_format == "rgba8888")
235-
Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat::RGBA8);
236-
else if (pixel_format == "rgba4444")
237-
Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat::RGBA4);
238-
else if (pixel_format == "rgba5551")
239-
Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat::RGB5A1);
240-
241232
/* !!!Notes
242233
** All compressed image should do PMA at texture convert tools(such as astcenc-2.2+ with -pp-premultiply)
243234
** or GPU fragment shader
@@ -1318,7 +1309,7 @@ void Director::createStatsLabel()
13181309
return;
13191310
}
13201311

1321-
texture = _textureCache->addImage(image, "/ax_fps_images", PixelFormat::RGBA4);
1312+
texture = _textureCache->addImage(image, "/ax_fps_images");
13221313
AX_SAFE_RELEASE(image);
13231314

13241315
/*

core/platform/Image.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,8 @@ bool Image::initWithPngData(uint8_t* data, ssize_t dataLen)
13141314
png_read_end(png_ptr, nullptr);
13151315

13161316
// premultiplied alpha for RGBA8888
1317-
if ((color_type == PNG_COLOR_TYPE_RGB_ALPHA) || (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
1317+
if ((color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1318+
|| (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
13181319
{
13191320
if (PNG_PREMULTIPLIED_ALPHA_ENABLED)
13201321
{
@@ -2635,7 +2636,7 @@ void Image::premultiplyAlpha()
26352636
for (int i = 0; i < _width * _height; i++)
26362637
{
26372638
uint8_t* p = _data + i * 2;
2638-
twoBytes[i] = ((p[0] * p[1] + 1) >> 8) | (p[1] << 8);
2639+
twoBytes[i] = ((p[0] * (p[1] + 1)) >> 8) | (p[1] << 8);
26392640
}
26402641
}
26412642

core/renderer/Shaders.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ AX_DLL const std::string_view positionTexture_frag = "positionTe
3939
AX_DLL const std::string_view positionTextureColor_vert = "positionTextureColor_vs"sv;
4040
AX_DLL const std::string_view positionTextureColor_frag = "positionTextureColor_fs"sv;
4141
AX_DLL const std::string_view positionTextureColorAlphaTest_frag = "positionTextureColorAlphaTest_fs"sv;
42+
AX_DLL const std::string_view positionTextureGray_frag = "positionTextureGray_fs"sv;
43+
AX_DLL const std::string_view positionTextureGrayAlpha_frag = "positionTextureGrayAlpha_fs"sv;
4244
AX_DLL const std::string_view label_normal_frag = "label_normal_fs"sv;
4345
AX_DLL const std::string_view label_outline_frag = "label_outline_fs"sv;
4446
AX_DLL const std::string_view label_distanceNormal_frag = "label_distanceNormal_fs"sv;

core/renderer/Shaders.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ extern AX_DLL const std::string_view positionTexture_frag;
4747
extern AX_DLL const std::string_view positionTextureColor_vert;
4848
extern AX_DLL const std::string_view positionTextureColor_frag;
4949
extern AX_DLL const std::string_view positionTextureColorAlphaTest_frag;
50+
extern AX_DLL const std::string_view positionTextureGray_frag;
51+
extern AX_DLL const std::string_view positionTextureGrayAlpha_frag;
5052
extern AX_DLL const std::string_view label_normal_frag;
5153
extern AX_DLL const std::string_view label_outline_frag;
5254
extern AX_DLL const std::string_view label_distanceNormal_frag;

core/renderer/Texture2D.cpp

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,6 @@ THE SOFTWARE.
5656
namespace ax
5757
{
5858

59-
// CLASS IMPLEMENTATIONS:
60-
61-
// If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
62-
// Default is: RGBA8888 (32-bit textures)
63-
static backend::PixelFormat g_defaultAlphaPixelFormat = backend::PixelFormat::RGBA8;
64-
6559
Texture2D::Texture2D()
6660
: _pixelFormat(backend::PixelFormat::NONE)
6761
, _pixelsWide(0)
@@ -188,6 +182,11 @@ bool Texture2D::initWithMipmaps(MipmapInfo* mipmaps,
188182
return true;
189183
}
190184

185+
bool Texture2D::updateWithImage(Image* image, int index)
186+
{
187+
return updateWithImage(image, image->getPixelFormat(), index);
188+
}
189+
191190
bool Texture2D::updateWithImage(Image* image, backend::PixelFormat format, int index)
192191
{
193192
if (image == nullptr)
@@ -237,34 +236,24 @@ bool Texture2D::updateWithImage(Image* image, backend::PixelFormat format, int i
237236
default:
238237
break;
239238
}
240-
#elif !AX_GLES_PROFILE
241-
// Non-GLES doesn't support follow render formats, needs convert PixelFormat::RGBA8
242-
// Note: axmol-1.1 deprecated A8, L8, LA8 as renderFormat, preferred R8, RG8
243-
switch (renderFormat)
244-
{
245-
case PixelFormat::R8:
246-
case PixelFormat::RG8:
247-
// Note: conversion to RGBA8 will happends
248-
renderFormat = PixelFormat::RGBA8;
249-
}
250239
#endif
251240

252241
if (image->getNumberOfMipmaps() > 1)
253242
{
254-
if (renderFormat != image->getPixelFormat())
243+
if (renderFormat != imagePixelFormat)
255244
{
256245
AXLOGW("WARNING: This image has more than 1 mipmaps and we will not convert the data format");
257246
}
258247

259248
// pixel format of data is not converted, renderFormat can be different from pixelFormat
260249
// it will be done later
261-
updateWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), image->getPixelFormat(), renderFormat, imageHeight, imageWidth, image->hasPremultipliedAlpha(), index);
250+
updateWithMipmaps(image->getMipmaps(), image->getNumberOfMipmaps(), imagePixelFormat, renderFormat, imageHeight, imageWidth, image->hasPremultipliedAlpha(), index);
262251
}
263252
else if (image->isCompressed())
264253
{ // !Only hardware support texture will be compression PixelFormat, otherwise, will convert to RGBA8 duraing image
265254
// load
266255
renderFormat = imagePixelFormat;
267-
updateWithData(tempData, tempDataLen, image->getPixelFormat(), image->getPixelFormat(), imageWidth, imageHeight, image->hasPremultipliedAlpha(), index);
256+
updateWithData(tempData, tempDataLen, imagePixelFormat, image->getPixelFormat(), imageWidth, imageHeight, image->hasPremultipliedAlpha(), index);
268257
}
269258
else
270259
{
@@ -448,7 +437,13 @@ bool Texture2D::updateWithSubData(void* data, int offsetX, int offsetY, int widt
448437
// implementation Texture2D (Image)
449438
bool Texture2D::initWithImage(Image* image)
450439
{
451-
return initWithImage(image, g_defaultAlphaPixelFormat);
440+
if (image == nullptr)
441+
{
442+
AXLOGW("Texture2D. Can't create Texture. UIImage is nil");
443+
return false;
444+
}
445+
446+
return initWithImage(image, image->getPixelFormat());
452447
}
453448

454449
bool Texture2D::initWithImage(Image* image, backend::PixelFormat format)
@@ -532,10 +527,6 @@ bool Texture2D::initWithString(std::string_view text, const FontDefinition& text
532527
AXASSERT(textDefinition._stroke._strokeEnabled == false, "Currently stroke only supported on iOS and Android!");
533528
#endif
534529

535-
PixelFormat pixelFormat = g_defaultAlphaPixelFormat;
536-
unsigned char* outTempData = nullptr;
537-
size_t outTempDataLen = 0;
538-
539530
int imageWidth;
540531
int imageHeight;
541532
auto textDef = textDefinition;
@@ -560,16 +551,10 @@ bool Texture2D::initWithString(std::string_view text, const FontDefinition& text
560551
}
561552

562553
Vec2 imageSize = Vec2((float)imageWidth, (float)imageHeight);
563-
pixelFormat =
564-
backend::PixelFormatUtils::convertDataToFormat(outData.getBytes(), imageWidth * imageHeight * 4,
565-
PixelFormat::RGBA8, pixelFormat, &outTempData, &outTempDataLen);
554+
const PixelFormat pixelFormat = PixelFormat::RGBA8;
566555

567-
ret = initWithData(outTempData, outTempDataLen, pixelFormat, imageWidth, imageHeight);
556+
ret = initWithData(outData.getBytes(), imageWidth * imageHeight * 4, pixelFormat, imageWidth, imageHeight);
568557

569-
if (outTempData != nullptr && outTempData != outData.getBytes())
570-
{
571-
free(outTempData);
572-
}
573558
setPremultipliedAlpha(hasPremultipliedAlpha);
574559

575560
return ret;
@@ -647,21 +632,6 @@ const char* Texture2D::getStringForFormat() const
647632
return backend::PixelFormatUtils::getFormatDescriptor(_pixelFormat).name;
648633
}
649634

650-
//
651-
// Texture options for images that contains alpha
652-
//
653-
// implementation Texture2D (PixelFormat)
654-
655-
void Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat format)
656-
{
657-
g_defaultAlphaPixelFormat = format;
658-
}
659-
660-
backend::PixelFormat Texture2D::getDefaultAlphaPixelFormat()
661-
{
662-
return g_defaultAlphaPixelFormat;
663-
}
664-
665635
unsigned int Texture2D::getBitsPerPixelForFormat(backend::PixelFormat format) const
666636
{
667637
return backend::PixelFormatUtils::getFormatDescriptor(format).bpp;

core/renderer/Texture2D.h

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -91,36 +91,6 @@ class AX_DLL Texture2D : public Object
9191
*/
9292
using TexParams = backend::SamplerDescriptor;
9393

94-
public:
95-
/** sets the default pixel format for UIImagescontains alpha channel.
96-
97-
@param format
98-
If the UIImage contains alpha channel, then the options are:
99-
- generate 32-bit textures: backend::PixelFormat::RGBA8 (default one)
100-
- generate 24-bit textures: backend::PixelFormat::RGB8
101-
- generate 16-bit textures: backend::PixelFormat::RGBA4
102-
- generate 16-bit textures: backend::PixelFormat::RGB5A1
103-
- generate 16-bit textures: backend::PixelFormat::RGB565
104-
- generate 8-bit textures: backend::PixelFormat::R8 (only use it if you use just 1 color)
105-
- generate 16-bit textures: backend::PixelFormat::RG8 (only use it if you use just 2 color)
106-
107-
How does it work ?
108-
- If the image is an RGBA (with Alpha) then the default pixel format will be used (it can be a 8-bit, 16-bit or
109-
32-bit texture)
110-
- If the image is an RGB (without Alpha) then: If the default pixel format is RGBA8888 then a RGBA8888 (32-bit)
111-
will be used. Otherwise a RGB565 (16-bit texture) will be used.
112-
113-
This parameter is not valid for PVR / PVR.CCZ images.
114-
115-
@since v0.8
116-
*/
117-
static void setDefaultAlphaPixelFormat(backend::PixelFormat format);
118-
119-
/** Returns the alpha pixel format.
120-
@since v0.8
121-
*/
122-
static backend::PixelFormat getDefaultAlphaPixelFormat();
123-
12494
public:
12595
/**
12696
*/
@@ -197,6 +167,7 @@ class AX_DLL Texture2D : public Object
197167
@param width Specifies the width of the texture subimage.
198168
@param height Specifies the height of the texture subimage.
199169
*/
170+
bool updateWithImage(Image* image, int index = 0);
200171
bool updateWithImage(Image* image, backend::PixelFormat format, int index = 0);
201172
bool updateWithData(const void* data,
202173
ssize_t dataLen,
@@ -240,21 +211,19 @@ class AX_DLL Texture2D : public Object
240211
/**
241212
Initializes a texture from a UIImage object.
242213
243-
We will use the format you specified with setDefaultAlphaPixelFormat to convert the image for texture.
214+
We will use the pixel format of the image.
244215
NOTE: It will not convert the pvr image file.
245216
@param image An UIImage object.
246217
*/
247218
bool initWithImage(Image* image);
248219

249220
/**
250-
Initializes a texture from a UIImage object.
221+
Initializes a texture from an Image object and convert it to the given
222+
format if necessary.
251223
252-
We will use the format you passed to the function to convert the image format to the texture format.
253-
If you pass PixelFormat::NONE, we will auto detect the image render type and use that type for texture to render.
254-
@param image An UIImage object.
255-
@param format Texture pixel formats.
256-
**/
257-
bool initWithImage(Image* image, backend::PixelFormat format);
224+
NOTE: It will not convert the pvr image file.
225+
*/
226+
bool initWithImage(Image* image, PixelFormat format);
258227

259228
/** Initializes a texture from a string with dimensions, alignment, font name and font size.
260229

core/renderer/TextureCache.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ struct TextureCache::AsyncStruct
8787
: filename(fn)
8888
, callback(f)
8989
, callbackKey(key)
90-
, pixelFormat(Texture2D::getDefaultAlphaPixelFormat())
90+
, pixelFormat(PixelFormat::NONE)
9191
, loadSuccess(false)
9292
{}
9393

@@ -433,7 +433,7 @@ Texture2D* TextureCache::getDummyTexture()
433433

434434
Texture2D* TextureCache::addImage(std::string_view path)
435435
{
436-
return addImage(path, Texture2D::getDefaultAlphaPixelFormat());
436+
return addImage(path, PixelFormat::NONE);
437437
}
438438

439439
Texture2D* TextureCache::addImage(std::string_view path, PixelFormat format)
@@ -516,7 +516,7 @@ void TextureCache::parseNinePatchImage(ax::Image* image, ax::Texture2D* texture,
516516

517517
Texture2D* TextureCache::addImage(Image* image, std::string_view key)
518518
{
519-
return addImage(image, key, Texture2D::getDefaultAlphaPixelFormat());
519+
return addImage(image, key, PixelFormat::NONE);
520520
}
521521

522522
Texture2D* TextureCache::addImage(Image* image, std::string_view key, PixelFormat format)

core/renderer/TextureCache.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ class AX_DLL TextureCache : public Object
112112
@param filepath The file path.
113113
@param callback A callback function would be invoked after the image is loaded.
114114
@since v0.8
115-
116-
@remark Please don't invoke Texture2D::setDefaultAlphaPixelFormat in main GL thread before invoke this API.
117115
*/
118116
virtual void addImageAsync(std::string_view filepath, const std::function<void(Texture2D*)>& callback);
119117

0 commit comments

Comments
 (0)