From 6e24f7e13aec55f10f830233aaa2112efae31a9c Mon Sep 17 00:00:00 2001 From: Larpon Date: Thu, 1 Dec 2022 16:54:37 +0100 Subject: [PATCH] gg: always use 4 channels in init_sokol_image (#16564) --- vlib/gg/image.c.v | 15 ++++++++++++++- vlib/stbi/stbi.c.v | 9 ++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/vlib/gg/image.c.v b/vlib/gg/image.c.v index ac6cc2dd0..3c3f53c00 100644 --- a/vlib/gg/image.c.v +++ b/vlib/gg/image.c.v @@ -84,9 +84,22 @@ pub fn (mut img Image) init_sokol_image() &Image { label: img.path.str d3d11_texture: 0 } + + // NOTE the following code, sometimes, result in hard-to-detect visual errors/bugs: + // img_size := usize(img.nr_channels * img.width * img.height) + // As an example see https://github.com/vlang/vab/issues/239 + // The image will come out blank for some reason and no SOKOL_ASSERT + // nor any CI check will/can currently catch this. + // Since all of gg currently runs with more or less *defaults* from sokol_gfx/sokol_gl + // we should currently just use the sum of each of the RGB and A channels (= 4) here instead. + // Optimized PNG images that have no alpha channel is often optimized to only have + // 3 (or less) channels which stbi will correctly detect and set as `img.nr_channels` + // but the current sokol_gl context setup expects 4. It *should* be the same with + // all other stbi supported formats. + img_size := usize(4 * img.width * img.height) img_desc.data.subimage[0][0] = gfx.Range{ ptr: img.data - size: usize(img.nr_channels * img.width * img.height) + size: img_size } img.simg = gfx.make_image(&img_desc) img.simg_ok = true diff --git a/vlib/stbi/stbi.c.v b/vlib/stbi/stbi.c.v index 2c662d737..4d5609c77 100644 --- a/vlib/stbi/stbi.c.v +++ b/vlib/stbi/stbi.c.v @@ -117,14 +117,9 @@ pub fn load(path string) !Image { ext: ext data: 0 } - // flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 } - desired_channels := if ext in ['png', 'jpg', 'jpeg'] { 4 } else { 0 } res.data = C.stbi_load(&char(path.str), &res.width, &res.height, &res.nr_channels, - desired_channels) - if desired_channels == 4 && res.nr_channels == 3 { - // Fix an alpha png bug - res.nr_channels = 4 - } + C.STBI_rgb_alpha) + if isnil(res.data) { return error('stbi_image failed to load from "${path}"') } -- 2.30.2