-bgfx: Switched fs_blit_palette16 to expand bitmap_ind16 contents to R8. Fixes vertical off-by-one shift in games with an odd pixel count along X. [Ryan Holtz] (#8528)

This commit is contained in:
MooglyGuy 2021-09-04 15:02:10 +02:00 committed by GitHub
parent b790bb64c9
commit cbc2489dfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 75 additions and 43 deletions

View File

@ -465,13 +465,14 @@ uint32_t chain_manager::update_screen_textures(uint32_t view, render_primitive *
bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
uint16_t pitch = prim.m_rowpixels;
int convert_stride = 1;
int width_div_factor = 1;
int width_mul_factor = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, prim.m_flags & PRIMFLAG_TEXFORMAT_MASK,
prim.m_rowpixels, tex_height, prim.m_prim->texture.palette, prim.m_prim->texture.base, pitch, convert_stride);
prim.m_rowpixels, tex_height, prim.m_prim->texture.palette, prim.m_prim->texture.base, pitch, width_div_factor, width_mul_factor);
if (texture == nullptr)
{
bgfx_texture *texture = new bgfx_texture(full_name, dst_format, tex_width, tex_height, mem, BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_MIP_POINT, pitch, prim.m_rowpixels, convert_stride);
bgfx_texture *texture = new bgfx_texture(full_name, dst_format, tex_width, tex_height, mem, BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP | BGFX_SAMPLER_MIN_POINT | BGFX_SAMPLER_MAG_POINT | BGFX_SAMPLER_MIP_POINT, pitch, prim.m_rowpixels, width_div_factor, width_mul_factor);
m_textures.add_provider(full_name, texture);
if (prim.m_prim->texture.palette)

View File

@ -15,14 +15,23 @@ uniform vec4 u_inv_tex_size1;
void main()
{
vec2 original_uv = v_texcoord0.xy * u_tex_size0.xy;
float mod_val = mod(original_uv.x, 2.0);
vec2 rounded_uv = vec2(original_uv.x - mod_val, original_uv.y);
vec4 srcpix = texture2D(s_tex, rounded_uv * u_inv_tex_size0.xy + vec2(u_inv_tex_size0.x, 0.0));
vec2 original_uv = v_texcoord0.xy * u_tex_size0.xy * vec2(2.0, 1.0);
float mod_val = mod(original_uv.x, 4.0);
vec2 rounded_uv = vec2(original_uv.x - mod_val, original_uv.y) * vec2(0.5, 1.0);
vec2 palette_uv = (srcpix.ra * vec2(256.0, 256.0)) * u_inv_tex_size1.xy;
if (mod_val < 1.0)
palette_uv = (srcpix.bg * vec2(256.0, 256.0)) * u_inv_tex_size1.xy;
float inv_width = u_inv_tex_size0.x * 0.5;
gl_FragColor = vec4(texture2D(s_pal, palette_uv).rgb, 1.0) * v_color0;
vec2 palette_uv = vec2(0.0, 0.0);
if (mod_val < 2.0)
{
palette_uv.x = texture2D(s_tex, rounded_uv * u_inv_tex_size0.xy + vec2(inv_width * 0.5, 0.0)).r;
palette_uv.y = texture2D(s_tex, rounded_uv * u_inv_tex_size0.xy + vec2(inv_width * 1.5, 0.0)).r;
}
else
{
palette_uv.x = texture2D(s_tex, rounded_uv * u_inv_tex_size0.xy + vec2(inv_width * 2.5, 0.0)).r;
palette_uv.y = texture2D(s_tex, rounded_uv * u_inv_tex_size0.xy + vec2(inv_width * 3.5, 0.0)).r;
}
gl_FragColor = vec4(texture2D(s_pal, palette_uv * vec2(256.0, 256.0) * u_inv_tex_size1.xy).rgb, 1.0) * v_color0;
}

View File

@ -49,7 +49,8 @@ public:
virtual uint16_t width() const override { return m_width; }
virtual uint16_t height() const override { return m_height; }
virtual uint16_t rowpixels() const override { return m_width; }
virtual int convert_stride() const override { return 1; }
virtual int width_div_factor() const override { return 1; }
virtual int width_mul_factor() const override { return 1; }
private:
std::string m_name;

View File

@ -16,7 +16,8 @@ bgfx_texture::bgfx_texture(std::string name, bgfx::TextureFormat::Enum format, u
, m_width(width)
, m_height(height)
, m_rowpixels(width)
, m_convert_stride(1)
, m_width_div_factor(1)
, m_width_mul_factor(1)
{
bgfx::TextureInfo info;
bgfx::calcTextureSize(info, width, height, 1, false, false, 1, format);
@ -34,18 +35,20 @@ bgfx_texture::bgfx_texture(std::string name, bgfx::TextureFormat::Enum format, u
}
}
bgfx_texture::bgfx_texture(std::string name, bgfx::TextureFormat::Enum format, uint16_t width, uint16_t height, const bgfx::Memory* data, uint32_t flags, uint16_t pitch, uint16_t rowpixels, int convert_stride)
bgfx_texture::bgfx_texture(std::string name, bgfx::TextureFormat::Enum format, uint16_t width, uint16_t height, const bgfx::Memory* data, uint32_t flags, uint16_t pitch, uint16_t rowpixels, int width_div_factor, int width_mul_factor)
: m_name(name)
, m_format(format)
, m_width(width)
, m_height(height)
, m_rowpixels(rowpixels ? rowpixels : width)
, m_convert_stride(convert_stride)
, m_width_div_factor(width_div_factor)
, m_width_mul_factor(width_mul_factor)
{
int adjusted_width = (m_rowpixels * m_width_mul_factor) / m_width_div_factor;
bgfx::TextureInfo info;
bgfx::calcTextureSize(info, m_rowpixels / m_convert_stride, height, 1, false, false, 1, format);
m_texture = bgfx::createTexture2D(m_rowpixels / m_convert_stride, height, false, 1, format, flags, nullptr);
bgfx::updateTexture2D(m_texture, 0, 0, 0, 0, m_rowpixels / m_convert_stride, height, data, pitch);
bgfx::calcTextureSize(info, adjusted_width, height, 1, false, false, 1, format);
m_texture = bgfx::createTexture2D(adjusted_width, height, false, 1, format, flags, nullptr);
bgfx::updateTexture2D(m_texture, 0, 0, 0, 0, adjusted_width, height, data, pitch);
}
bgfx_texture::~bgfx_texture()
@ -55,5 +58,5 @@ bgfx_texture::~bgfx_texture()
void bgfx_texture::update(const bgfx::Memory *data, uint16_t pitch)
{
bgfx::updateTexture2D(m_texture, 0, 0, 0, 0, m_rowpixels / m_convert_stride, m_height, data, pitch);
bgfx::updateTexture2D(m_texture, 0, 0, 0, 0, (m_rowpixels * m_width_mul_factor) / m_width_div_factor, m_height, data, pitch);
}

View File

@ -21,7 +21,7 @@ class bgfx_texture : public bgfx_texture_handle_provider
{
public:
bgfx_texture(std::string name, bgfx::TextureFormat::Enum format, uint16_t width, uint16_t height, uint32_t flags, void* data);
bgfx_texture(std::string name, bgfx::TextureFormat::Enum format, uint16_t width, uint16_t height, const bgfx::Memory* data, uint32_t flags = BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP, uint16_t pitch = UINT16_MAX, uint16_t rowpixels = 0, int convert_stride = 1);
bgfx_texture(std::string name, bgfx::TextureFormat::Enum format, uint16_t width, uint16_t height, const bgfx::Memory* data, uint32_t flags = BGFX_SAMPLER_U_CLAMP | BGFX_SAMPLER_V_CLAMP, uint16_t pitch = UINT16_MAX, uint16_t rowpixels = 0, int width_div_factor = 1, int width_mul_factor = 1);
virtual ~bgfx_texture();
// Getters
@ -34,7 +34,8 @@ public:
virtual uint16_t width() const override { return m_width; }
virtual uint16_t height() const override { return m_height; }
virtual uint16_t rowpixels() const override { return m_rowpixels; }
virtual int convert_stride() const override { return m_convert_stride; }
virtual int width_div_factor() const override { return m_width_div_factor; }
virtual int width_mul_factor() const override { return m_width_mul_factor; }
void update(const bgfx::Memory *data, uint16_t pitch = UINT16_MAX);
@ -44,7 +45,8 @@ protected:
uint16_t m_width;
uint16_t m_height;
uint16_t m_rowpixels;
int m_convert_stride;
int m_width_div_factor;
int m_width_mul_factor;
bgfx::TextureHandle m_texture;
};

View File

@ -25,7 +25,8 @@ public:
virtual uint16_t width() const = 0;
virtual uint16_t height() const = 0;
virtual uint16_t rowpixels() const = 0;
virtual int convert_stride() const = 0;
virtual int width_div_factor() const = 0;
virtual int width_mul_factor() const = 0;
};
#endif // __DRAWBGFX_TEXTURE_HANDLE_PROVIDER__

View File

@ -122,9 +122,10 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form
{
bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
uint16_t pitch = width;
int convert_stride = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, pitch, convert_stride);
bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)(rowpixels / convert_stride), (uint16_t)height, mem, pitch);
int width_div_factor = 1;
int width_mul_factor = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, pitch, width_div_factor, width_mul_factor);
bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)((rowpixels * width_mul_factor) / width_div_factor), (uint16_t)height, mem, pitch);
return handle;
}
}
@ -151,9 +152,10 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form
{
bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
uint16_t pitch = width;
int convert_stride = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, pitch, convert_stride);
bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)(rowpixels / convert_stride), (uint16_t)height, mem, pitch);
int width_div_factor = 1;
int width_mul_factor = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, pitch, width_div_factor, width_mul_factor);
bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)((rowpixels * width_mul_factor) / width_div_factor), (uint16_t)height, mem, pitch);
return handle;
}
}
@ -163,10 +165,12 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form
bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
uint16_t pitch = width;
int convert_stride = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, pitch, convert_stride);
handle = bgfx::createTexture2D(width, height, false, 1, dst_format, flags, nullptr);
bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)(rowpixels / convert_stride), (uint16_t)height, mem, pitch);
int width_div_factor = 1;
int width_mul_factor = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, pitch, width_div_factor, width_mul_factor);
const uint16_t adjusted_width = (uint16_t)((rowpixels * width_mul_factor) / width_div_factor);
handle = bgfx::createTexture2D(adjusted_width, height, false, 1, dst_format, flags, nullptr);
bgfx::updateTexture2D(handle, 0, 0, 0, 0, adjusted_width, (uint16_t)height, mem, pitch);
m_mame_textures[key] = { handle, seqid, width, height };
return handle;

View File

@ -14,7 +14,7 @@
#include "render.h"
const bgfx::Memory* bgfx_util::mame_texture_data_to_bgfx_texture_data(bgfx::TextureFormat::Enum &dst_format, uint32_t src_format, int rowpixels, int height, const rgb_t *palette, void *base, uint16_t &out_pitch, int &convert_stride)
const bgfx::Memory* bgfx_util::mame_texture_data_to_bgfx_texture_data(bgfx::TextureFormat::Enum &dst_format, uint32_t src_format, int rowpixels, int height, const rgb_t *palette, void *base, uint16_t &out_pitch, int &width_div_factor, int &width_mul_factor)
{
bgfx::TextureInfo info;
const bgfx::Memory *data = nullptr;
@ -22,20 +22,30 @@ const bgfx::Memory* bgfx_util::mame_texture_data_to_bgfx_texture_data(bgfx::Text
switch (src_format)
{
case PRIMFLAG_TEXFORMAT(TEXFORMAT_YUY16):
case PRIMFLAG_TEXFORMAT(TEXFORMAT_PALETTE16):
dst_format = bgfx::TextureFormat::BGRA8;
convert_stride = 2;
width_div_factor = 2;
width_mul_factor = 1;
out_pitch = rowpixels * 2;
bgfx::calcTextureSize(info, rowpixels / convert_stride, height, 1, false, false, 1, dst_format);
bgfx::calcTextureSize(info, rowpixels / width_div_factor, height, 1, false, false, 1, dst_format);
data = bgfx::copy(base, info.storageSize);
break;
case PRIMFLAG_TEXFORMAT(TEXFORMAT_PALETTE16):
dst_format = bgfx::TextureFormat::R8;
width_div_factor = 1;
width_mul_factor = 2;
out_pitch = rowpixels * 2;
bgfx::calcTextureSize(info, rowpixels * width_mul_factor, height, 1, false, false, 1, dst_format);
data = bgfx::copy(base, info.storageSize);
break;
case PRIMFLAG_TEXFORMAT(TEXFORMAT_ARGB32):
case PRIMFLAG_TEXFORMAT(TEXFORMAT_RGB32):
dst_format = bgfx::TextureFormat::BGRA8;
convert_stride = 1;
width_div_factor = 1;
width_mul_factor = 1;
out_pitch = rowpixels * 4;
bgfx::calcTextureSize(info, rowpixels / convert_stride, height, 1, false, false, 1, dst_format);
bgfx::calcTextureSize(info, rowpixels, height, 1, false, false, 1, dst_format);
data = bgfx::copy(base, info.storageSize);
break;

View File

@ -11,7 +11,7 @@
class bgfx_util
{
public:
static const bgfx::Memory* mame_texture_data_to_bgfx_texture_data(bgfx::TextureFormat::Enum &dst_format, uint32_t format, int rowpixels, int height, const rgb_t *palette, void *base, uint16_t &out_pitch, int &convert_stride);
static const bgfx::Memory* mame_texture_data_to_bgfx_texture_data(bgfx::TextureFormat::Enum &dst_format, uint32_t format, int rowpixels, int height, const rgb_t *palette, void *base, uint16_t &out_pitch, int &width_div_factor, int &width_mul_factor);
static const bgfx::Memory* mame_texture_data_to_bgra32(uint32_t src_format, int width, int height, int rowpixels, const rgb_t *palette, void *base);
static uint64_t get_blend_state(uint32_t blend);
};

View File

@ -1168,9 +1168,10 @@ void renderer_bgfx::process_atlas_packs(std::vector<std::vector<rectangle_packer
m_hash_to_entry[rect.hash()] = rect;
bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8;
uint16_t pitch = rect.width();
int convert_stride = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, rect.format(), rect.rowpixels(), rect.height(), rect.palette(), rect.base(), pitch, convert_stride);
bgfx::updateTexture2D(m_texture_cache->texture(), 0, 0, rect.x(), rect.y(), rect.width() / convert_stride, rect.height(), mem, pitch);
int width_div_factor = 1;
int width_mul_factor = 1;
const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, rect.format(), rect.rowpixels(), rect.height(), rect.palette(), rect.base(), pitch, width_div_factor, width_mul_factor);
bgfx::updateTexture2D(m_texture_cache->texture(), 0, 0, rect.x(), rect.y(), (rect.width() * width_mul_factor) / width_div_factor, rect.height(), mem, pitch);
}
}
}