Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gray alpha png #2234

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion core/2d/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,14 @@ void Sprite::setTexture(Texture2D* texture)
}

if (needsUpdatePS)
setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
{
const PixelFormat pixelFormat = _texture->getPixelFormat();

if (pixelFormat == PixelFormat::RG8)
setProgramState(backend::ProgramType::POSITION_TEXTURE_GRAY_ALPHA);
else
setProgramState(backend::ProgramType::POSITION_TEXTURE_COLOR);
}
else
updateProgramStateTexture(_texture);
}
Expand Down
2 changes: 1 addition & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ if(LINUX)

# including GTK3.0 and WebKit2Gtk4.x
find_package(PkgConfig REQUIRED)
pkg_check_modules(GKT3 REQUIRED gtk+-3.0)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)

set(_webkit2gtk_vers "4.1;4.0")
foreach(_ver ${_webkit2gtk_vers})
Expand Down
2 changes: 1 addition & 1 deletion core/platform/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2606,7 +2606,7 @@ void Image::premultiplyAlpha()
for (int i = 0; i < _width * _height; i++)
{
uint8_t* p = _data + i * 2;
twoBytes[i] = ((p[0] * p[1] + 1) >> 8) | (p[1] << 8);
twoBytes[i] = ((p[0] * (p[1] + 1)) >> 8) | (p[1] << 8);
}
}

Expand Down
1 change: 1 addition & 0 deletions core/renderer/Shaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ AX_DLL const std::string_view positionTexture_frag = "positionTe
AX_DLL const std::string_view positionTextureColor_vert = "positionTextureColor_vs"sv;
AX_DLL const std::string_view positionTextureColor_frag = "positionTextureColor_fs"sv;
AX_DLL const std::string_view positionTextureColorAlphaTest_frag = "positionTextureColorAlphaTest_fs"sv;
AX_DLL const std::string_view positionTextureGrayAlpha_frag = "positionTextureGrayAlpha_fs"sv;
AX_DLL const std::string_view label_normal_frag = "label_normal_fs"sv;
AX_DLL const std::string_view label_outline_frag = "label_outline_fs"sv;
AX_DLL const std::string_view label_distanceNormal_frag = "label_distanceNormal_fs"sv;
Expand Down
1 change: 1 addition & 0 deletions core/renderer/Shaders.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern AX_DLL const std::string_view positionTexture_frag;
extern AX_DLL const std::string_view positionTextureColor_vert;
extern AX_DLL const std::string_view positionTextureColor_frag;
extern AX_DLL const std::string_view positionTextureColorAlphaTest_frag;
extern AX_DLL const std::string_view positionTextureGrayAlpha_frag;
extern AX_DLL const std::string_view label_normal_frag;
extern AX_DLL const std::string_view label_outline_frag;
extern AX_DLL const std::string_view label_distanceNormal_frag;
Expand Down
1 change: 1 addition & 0 deletions core/renderer/backend/Enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ struct ProgramType
POSITION_TEXTURE, // positionTexture_vert, positionTexture_frag
POSITION_TEXTURE_COLOR, // positionTextureColor_vert, positionTextureColor_frag
POSITION_TEXTURE_COLOR_ALPHA_TEST, // positionTextureColor_vert, positionTextureColorAlphaTest_frag
POSITION_TEXTURE_GRAY_ALPHA, // positionTextureColor_vert, positionTextureGrayAlpha_frag
LABEL_NORMAL, // positionTextureColor_vert, label_normal_frag
LABLE_OUTLINE, // positionTextureColor_vert, labelOutline_frag
LABEL_DISTANCE_NORMAL, // positionTextureColor_vert, label_distanceNormal_frag
Expand Down
2 changes: 2 additions & 0 deletions core/renderer/backend/ProgramManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ bool ProgramManager::init()
VertexLayoutType::Texture);
registerProgram(ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST, positionTextureColor_vert,
positionTextureColorAlphaTest_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::POSITION_TEXTURE_GRAY_ALPHA, positionTextureColor_vert,
positionTextureGrayAlpha_frag, VertexLayoutType::Sprite);
registerProgram(ProgramType::POSITION_UCOLOR, positionUColor_vert, positionColor_frag, VertexLayoutType::Pos);
registerProgram(ProgramType::DUAL_SAMPLER_GRAY, positionTextureColor_vert, dualSampler_gray_frag,
VertexLayoutType::Sprite);
Expand Down
16 changes: 16 additions & 0 deletions core/renderer/shaders/positionTextureGrayAlpha.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#version 310 es
precision highp float;
precision highp int;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use lowp precision as default for both float and int. That's enough for simple color processing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shader was copied from positionTextureColor.frag, with the only section changed being what is in the main() function. The existing shaders use the same settings for precision etc..


layout(location = COLOR0) in vec4 v_color;
layout(location = TEXCOORD0) in vec2 v_texCoord;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use highp precision for texture coordinates.


layout(binding = 0) uniform sampler2D u_tex0;

layout(location = SV_Target0) out vec4 FragColor;

void main()
{
vec4 c = texture(u_tex0, v_texCoord);
FragColor = v_color * vec4(c.r, c.r, c.r, c.g);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail on GLES2, because for two channel textures it uses GL_LUMINANCE_ALPHA pixel format which loads data into shader's rgba channels as LLLA. So for GLES2 you need to use (c.r, c.r, c.r, c.a). So you either need an #if defined(GLES2), or use compatibility macro RG8_CHANNEL defined in base.glsl. Check videoTextureNV12.frag shader for example.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would (c.r, c.r, c.r, c.a) work for both cases? For RG8, it's being loaded as RGRG into the RGBA channels, so instead of using R and G, why not just use R and A, since A has the same value as G?

}
9 changes: 7 additions & 2 deletions tests/cpp-tests/Source/Texture2dTest/Texture2dTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ void TexturePNG::onEnter()
ai88->setPosition(s.width / 4.0f, s.height * 3.0f / 4.0f);
addChild(ai88);

// grayscale with alpha, gray+alpha shader.
auto ai88s = Sprite::create("Images/test_image_ai88.png", PixelFormat::RG8);
ai88s->setPosition(s.width / 4.0f, s.height / 2.0f);
addChild(ai88s);

// rgb without alpha
auto rgb888 = Sprite::create("Images/test_image_rgb888.png");
rgb888->setPosition(s.width * 3.0f / 4.0f, s.height / 4.0f);
Expand All @@ -455,7 +460,7 @@ std::string TexturePNG::title() const

std::string TexturePNG::subtitle() const
{
return "LB:I8, LT:AI8\nRB:RGB888, RT: RGBA8888";
return "LB:I8, LM: AI8/RG8 pixel format, LT:AI8\nRB:RGB888, RT: RGBA8888";
}

//------------------------------------------------------------------
Expand Down Expand Up @@ -1634,7 +1639,7 @@ void TexturePixelFormat::onEnter()
Director::getInstance()->getTextureCache()->removeTexture(sprite5->getTexture());

// A8 image (8-bit)
auto sprite6 = Sprite::create("Images/test-rgba1.png", PixelFormat::R8); //
auto sprite6 = Sprite::create("Images/test-rgba1.png", PixelFormat::R8); //
sprite6->setPosition(Vec2(6 * s.width / 7, s.height / 2 - 32));
addChild(sprite6, 0);

Expand Down