From 639a513d21ff2c58cfb7192df27b327d4b4ee37d Mon Sep 17 00:00:00 2001 From: Julien Jorge Date: Tue, 12 Nov 2024 20:20:43 +0100 Subject: [PATCH 1/4] Fix typo in pkg_check_modules for GTK3. --- core/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index deca2de071b..a7e70a40ef5 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -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}) From d82e4e19ca6d595a59698e386a02125b40f1da90 Mon Sep 17 00:00:00 2001 From: Julien Jorge Date: Wed, 13 Nov 2024 20:23:15 +0100 Subject: [PATCH 2/4] Handle gray+alpha PNG images as gray+alpha sprites. --- core/2d/Sprite.cpp | 9 ++++++++- core/renderer/Shaders.cpp | 1 + core/renderer/Shaders.h | 1 + core/renderer/backend/Enums.h | 1 + core/renderer/backend/PixelFormatUtils.cpp | 2 +- core/renderer/backend/ProgramManager.cpp | 2 ++ .../shaders/positionTextureGrayAlpha.frag | 16 ++++++++++++++++ 7 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 core/renderer/shaders/positionTextureGrayAlpha.frag diff --git a/core/2d/Sprite.cpp b/core/2d/Sprite.cpp index 62b67e9d78b..f2970934ee1 100644 --- a/core/2d/Sprite.cpp +++ b/core/2d/Sprite.cpp @@ -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); } diff --git a/core/renderer/Shaders.cpp b/core/renderer/Shaders.cpp index ed097d29d92..207634d78fd 100644 --- a/core/renderer/Shaders.cpp +++ b/core/renderer/Shaders.cpp @@ -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; diff --git a/core/renderer/Shaders.h b/core/renderer/Shaders.h index cb99f492a63..8d9b83e4b6c 100644 --- a/core/renderer/Shaders.h +++ b/core/renderer/Shaders.h @@ -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; diff --git a/core/renderer/backend/Enums.h b/core/renderer/backend/Enums.h index 7cfea6b0948..73ab624113e 100644 --- a/core/renderer/backend/Enums.h +++ b/core/renderer/backend/Enums.h @@ -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 diff --git a/core/renderer/backend/PixelFormatUtils.cpp b/core/renderer/backend/PixelFormatUtils.cpp index 2113197b64f..e0a5ead9769 100644 --- a/core/renderer/backend/PixelFormatUtils.cpp +++ b/core/renderer/backend/PixelFormatUtils.cpp @@ -145,7 +145,7 @@ static void convertRG8ToRGBA8(const unsigned char* data, size_t dataLen, unsigne for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2) { *outData++ = data[i]; // R - *outData++ = data[i + 1]; // G + *outData++ = data[i]; // G *outData++ = data[i]; // B *outData++ = data[i + 1]; // A } diff --git a/core/renderer/backend/ProgramManager.cpp b/core/renderer/backend/ProgramManager.cpp index a7abce33cf1..12fe99a385c 100644 --- a/core/renderer/backend/ProgramManager.cpp +++ b/core/renderer/backend/ProgramManager.cpp @@ -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); diff --git a/core/renderer/shaders/positionTextureGrayAlpha.frag b/core/renderer/shaders/positionTextureGrayAlpha.frag new file mode 100644 index 00000000000..9abd1fc8aeb --- /dev/null +++ b/core/renderer/shaders/positionTextureGrayAlpha.frag @@ -0,0 +1,16 @@ +#version 310 es +precision highp float; +precision highp int; + +layout(location = COLOR0) in vec4 v_color; +layout(location = TEXCOORD0) in vec2 v_texCoord; + +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); +} From a994e52d38cdfac72897cd195cb5bc151c116639 Mon Sep 17 00:00:00 2001 From: Julien Jorge Date: Wed, 13 Nov 2024 20:51:12 +0100 Subject: [PATCH 3/4] Fix incorrect premultiplied alpha formula for gray+alpha images. --- core/platform/Image.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/platform/Image.cpp b/core/platform/Image.cpp index a6fcb36c09a..befa09e7099 100644 --- a/core/platform/Image.cpp +++ b/core/platform/Image.cpp @@ -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); } } From 5a192ef23fe0676454940d1b914802f979b37cb3 Mon Sep 17 00:00:00 2001 From: Julien Jorge Date: Thu, 14 Nov 2024 07:10:52 +0100 Subject: [PATCH 4/4] wip code review --- core/renderer/backend/PixelFormatUtils.cpp | 2 +- tests/cpp-tests/Source/Texture2dTest/Texture2dTest.cpp | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/core/renderer/backend/PixelFormatUtils.cpp b/core/renderer/backend/PixelFormatUtils.cpp index e0a5ead9769..2113197b64f 100644 --- a/core/renderer/backend/PixelFormatUtils.cpp +++ b/core/renderer/backend/PixelFormatUtils.cpp @@ -145,7 +145,7 @@ static void convertRG8ToRGBA8(const unsigned char* data, size_t dataLen, unsigne for (ssize_t i = 0, l = dataLen - 1; i < l; i += 2) { *outData++ = data[i]; // R - *outData++ = data[i]; // G + *outData++ = data[i + 1]; // G *outData++ = data[i]; // B *outData++ = data[i + 1]; // A } diff --git a/tests/cpp-tests/Source/Texture2dTest/Texture2dTest.cpp b/tests/cpp-tests/Source/Texture2dTest/Texture2dTest.cpp index f1a9f5044cb..03a2f150825 100644 --- a/tests/cpp-tests/Source/Texture2dTest/Texture2dTest.cpp +++ b/tests/cpp-tests/Source/Texture2dTest/Texture2dTest.cpp @@ -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); @@ -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"; } //------------------------------------------------------------------ @@ -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);