From 9cc98f0b6237a3c8287a09310af86462db8d6260 Mon Sep 17 00:00:00 2001 From: Ryan Holtz Date: Sat, 20 Jun 2020 14:56:39 +0200 Subject: -bgfx: Corrected a data overrun in the d3d12 backend from allocating only enough texture data for width*height, not rowpixels*height. [Ryan Holtz] --- scripts/src/osd/modules.lua | 38 ++++++++++++++++++++++ src/osd/modules/render/bgfx/chain.cpp | 2 +- src/osd/modules/render/bgfx/chainentry.cpp | 9 +++-- src/osd/modules/render/bgfx/chainentry.h | 2 +- src/osd/modules/render/bgfx/chainmanager.cpp | 6 ++-- src/osd/modules/render/bgfx/target.h | 1 + src/osd/modules/render/bgfx/texture.cpp | 9 ++--- src/osd/modules/render/bgfx/texture.h | 4 ++- .../modules/render/bgfx/texturehandleprovider.h | 1 + src/osd/modules/render/bgfx/texturemanager.cpp | 6 ++-- src/osd/modules/render/bgfxutil.cpp | 4 +-- src/osd/modules/render/bgfxutil.h | 2 +- src/osd/modules/render/drawbgfx.cpp | 2 +- 13 files changed, 66 insertions(+), 20 deletions(-) diff --git a/scripts/src/osd/modules.lua b/scripts/src/osd/modules.lua index a1b7f1d26df..d7987b5b80e 100644 --- a/scripts/src/osd/modules.lua +++ b/scripts/src/osd/modules.lua @@ -172,46 +172,84 @@ function osdmodulesbuild() MAME_DIR .. "src/osd/modules/render/bgfxutil.h", MAME_DIR .. "src/osd/modules/render/binpacker.cpp", MAME_DIR .. "src/osd/modules/render/bgfx/blendreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/blendreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/chain.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/chain.h", MAME_DIR .. "src/osd/modules/render/bgfx/chainentry.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/chainentry.h", MAME_DIR .. "src/osd/modules/render/bgfx/chainentryreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/chainentryreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/chainmanager.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/chainmanager.h", MAME_DIR .. "src/osd/modules/render/bgfx/chainreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/chainreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/clear.cpp", MAME_DIR .. "src/osd/modules/render/bgfx/clear.h", MAME_DIR .. "src/osd/modules/render/bgfx/clearreader.cpp", MAME_DIR .. "src/osd/modules/render/bgfx/clearreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/cullreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/cullreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/depthreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/depthreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/effect.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/effect.h", MAME_DIR .. "src/osd/modules/render/bgfx/effectmanager.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/effectmanager.h", MAME_DIR .. "src/osd/modules/render/bgfx/effectreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/effectreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/entryuniformreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/entryuniformreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/inputpair.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/inputpair.h", MAME_DIR .. "src/osd/modules/render/bgfx/frameparameter.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/frameparameter.h", MAME_DIR .. "src/osd/modules/render/bgfx/timeparameter.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/timeparameter.h", MAME_DIR .. "src/osd/modules/render/bgfx/paramreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/paramreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/paramuniform.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/paramuniform.h", MAME_DIR .. "src/osd/modules/render/bgfx/paramuniformreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/paramuniformreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/shadermanager.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/shadermanager.h", MAME_DIR .. "src/osd/modules/render/bgfx/slider.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/slider.h", MAME_DIR .. "src/osd/modules/render/bgfx/sliderreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/sliderreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/slideruniform.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/slideruniform.h", MAME_DIR .. "src/osd/modules/render/bgfx/slideruniformreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/slideruniformreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/statereader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/statereader.h", MAME_DIR .. "src/osd/modules/render/bgfx/suppressor.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/suppressor.h", MAME_DIR .. "src/osd/modules/render/bgfx/suppressorreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/suppressorreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/target.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/target.h", MAME_DIR .. "src/osd/modules/render/bgfx/targetreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/targetreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/targetmanager.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/targetmanager.h", MAME_DIR .. "src/osd/modules/render/bgfx/texture.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/texture.h", + MAME_DIR .. "src/osd/modules/render/bgfx/texturehandleprovider.h", MAME_DIR .. "src/osd/modules/render/bgfx/texturemanager.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/texturemanager.h", MAME_DIR .. "src/osd/modules/render/bgfx/uniform.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/uniform.h", MAME_DIR .. "src/osd/modules/render/bgfx/uniformreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/uniformreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/valueuniform.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/valueuniform.h", MAME_DIR .. "src/osd/modules/render/bgfx/valueuniformreader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/valueuniformreader.h", MAME_DIR .. "src/osd/modules/render/bgfx/view.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/view.h", MAME_DIR .. "src/osd/modules/render/bgfx/writereader.cpp", + MAME_DIR .. "src/osd/modules/render/bgfx/writereader.h", } includedirs { MAME_DIR .. "3rdparty/bgfx/examples/common", diff --git a/src/osd/modules/render/bgfx/chain.cpp b/src/osd/modules/render/bgfx/chain.cpp index 924bc6ab67f..c5bb7cf94b6 100644 --- a/src/osd/modules/render/bgfx/chain.cpp +++ b/src/osd/modules/render/bgfx/chain.cpp @@ -125,7 +125,7 @@ void bgfx_chain::process(chain_manager::screen_prim &prim, int view, int screen, for (bgfx_parameter* param : m_params) { - param->tick(frameTimeInSeconds* toMs); + param->tick(frameTimeInSeconds * toMs); } } diff --git a/src/osd/modules/render/bgfx/chainentry.cpp b/src/osd/modules/render/bgfx/chainentry.cpp index ca3354ee6b3..fd19b2695a3 100644 --- a/src/osd/modules/render/bgfx/chainentry.cpp +++ b/src/osd/modules/render/bgfx/chainentry.cpp @@ -60,7 +60,7 @@ bgfx_chain_entry::~bgfx_chain_entry() void bgfx_chain_entry::submit(int view, chain_manager::screen_prim &prim, texture_manager& textures, uint16_t screen_count, uint16_t screen_width, uint16_t screen_height, float screen_scale_x, float screen_scale_y, float screen_offset_x, float screen_offset_y, uint32_t rotation_type, bool swap_xy, uint64_t blend, int32_t screen) { - if (!setup_view(view, screen_width, screen_height, screen)) + if (!setup_view(textures, view, screen_width, screen_height, screen)) { return; } @@ -250,7 +250,7 @@ void bgfx_chain_entry::setup_auto_uniforms(chain_manager::screen_prim &prim, tex setup_screenindex_uniform(screen); } -bool bgfx_chain_entry::setup_view(int view, uint16_t screen_width, uint16_t screen_height, int32_t screen) const +bool bgfx_chain_entry::setup_view(texture_manager &textures, int view, uint16_t screen_width, uint16_t screen_height, int32_t screen) const { bgfx::FrameBufferHandle handle = BGFX_INVALID_HANDLE; uint16_t width = screen_width; @@ -272,8 +272,11 @@ bool bgfx_chain_entry::setup_view(int view, uint16_t screen_width, uint16_t scre const bgfx::Caps* caps = bgfx::getCaps(); + std::string name = m_inputs[0]->texture() + std::to_string(screen); + const float right_ratio = (float)textures.provider(name)->width() / textures.provider(name)->rowpixels(); + float projMat[16]; - bx::mtxOrtho(projMat, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth); + bx::mtxOrtho(projMat, 0.0f, right_ratio, 1.0f, 0.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth); bgfx::setViewTransform(view, nullptr, projMat); m_clear->bind(view); diff --git a/src/osd/modules/render/bgfx/chainentry.h b/src/osd/modules/render/bgfx/chainentry.h index 02bad7d1c10..3fb1dd54ca6 100644 --- a/src/osd/modules/render/bgfx/chainentry.h +++ b/src/osd/modules/render/bgfx/chainentry.h @@ -58,7 +58,7 @@ private: void setup_quaddims_uniform(chain_manager::screen_prim &prim) const; void setup_screenindex_uniform(int32_t screen) const; - bool setup_view(int view, uint16_t screen_width, uint16_t screen_height, int32_t screen) const; + bool setup_view(texture_manager& textures, int view, uint16_t screen_width, uint16_t screen_height, int32_t screen) const; void put_screen_buffer(uint16_t screen_width, uint16_t screen_height, uint32_t screen_tint, bgfx::TransientVertexBuffer* buffer) const; std::string m_name; diff --git a/src/osd/modules/render/bgfx/chainmanager.cpp b/src/osd/modules/render/bgfx/chainmanager.cpp index c2eea4c9a8b..6c22a640913 100644 --- a/src/osd/modules/render/bgfx/chainmanager.cpp +++ b/src/osd/modules/render/bgfx/chainmanager.cpp @@ -465,13 +465,13 @@ uint32_t chain_manager::update_screen_textures(uint32_t view, render_primitive * } bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::RGBA8; - uint16_t pitch = tex_width; + uint16_t pitch = prim.m_rowpixels; const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, prim.m_flags & PRIMFLAG_TEXFORMAT_MASK, - tex_width, tex_height, prim.m_rowpixels, prim.m_prim->texture.palette, prim.m_prim->texture.base, &pitch); + prim.m_rowpixels, tex_height, prim.m_prim->texture.palette, prim.m_prim->texture.base, &pitch); 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); + 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); m_textures.add_provider(full_name, texture); if (prim.m_prim->texture.palette) diff --git a/src/osd/modules/render/bgfx/target.h b/src/osd/modules/render/bgfx/target.h index 53fd70dd46b..be6e5884d22 100644 --- a/src/osd/modules/render/bgfx/target.h +++ b/src/osd/modules/render/bgfx/target.h @@ -46,6 +46,7 @@ public: // bgfx_texture_handle_provider 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 bgfx::TextureHandle texture() const override; virtual bool is_target() const override { return true; } diff --git a/src/osd/modules/render/bgfx/texture.cpp b/src/osd/modules/render/bgfx/texture.cpp index a187741798d..85f1d3645fb 100644 --- a/src/osd/modules/render/bgfx/texture.cpp +++ b/src/osd/modules/render/bgfx/texture.cpp @@ -32,16 +32,17 @@ 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) +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) : m_name(name) , m_format(format) , m_width(width) , m_height(height) + , m_rowpixels(rowpixels ? rowpixels : width) { bgfx::TextureInfo info; - bgfx::calcTextureSize(info, width, height, 1, false, false, 1, format); - m_texture = bgfx::createTexture2D(width, height, false, 1, format, flags, nullptr); - bgfx::updateTexture2D(m_texture, 0, 0, 0, 0, width, height, data, pitch); + bgfx::calcTextureSize(info, m_rowpixels, height, 1, false, false, 1, format); + m_texture = bgfx::createTexture2D(m_rowpixels, height, false, 1, format, flags, nullptr); + bgfx::updateTexture2D(m_texture, 0, 0, 0, 0, m_rowpixels, height, data, pitch); } bgfx_texture::~bgfx_texture() diff --git a/src/osd/modules/render/bgfx/texture.h b/src/osd/modules/render/bgfx/texture.h index 1d5b1dd9f6d..dc96e9acd77 100644 --- a/src/osd/modules/render/bgfx/texture.h +++ b/src/osd/modules/render/bgfx/texture.h @@ -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); + 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); virtual ~bgfx_texture(); // Getters @@ -31,6 +31,7 @@ public: // bgfx_texture_handle_provider 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 bgfx::TextureHandle texture() const override { return m_texture; } virtual bool is_target() const override { return false; } @@ -41,6 +42,7 @@ protected: bgfx::TextureFormat::Enum m_format; uint16_t m_width; uint16_t m_height; + uint16_t m_rowpixels; bgfx::TextureHandle m_texture; }; diff --git a/src/osd/modules/render/bgfx/texturehandleprovider.h b/src/osd/modules/render/bgfx/texturehandleprovider.h index 5620e54974e..b4b3f561d44 100644 --- a/src/osd/modules/render/bgfx/texturehandleprovider.h +++ b/src/osd/modules/render/bgfx/texturehandleprovider.h @@ -24,6 +24,7 @@ public: virtual bool is_target() const = 0; virtual uint16_t width() const = 0; virtual uint16_t height() const = 0; + virtual uint16_t rowpixels() const = 0; }; #endif // __DRAWBGFX_TEXTURE_HANDLE_PROVIDER__ diff --git a/src/osd/modules/render/bgfx/texturemanager.cpp b/src/osd/modules/render/bgfx/texturemanager.cpp index 861c6e5b617..a03a6b46125 100644 --- a/src/osd/modules/render/bgfx/texturemanager.cpp +++ b/src/osd/modules/render/bgfx/texturemanager.cpp @@ -118,7 +118,7 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form { bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8; uint16_t pitch = width; - const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, width, height, rowpixels, palette, base, &pitch); + const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, &pitch); bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)width, (uint16_t)height, mem, pitch); return handle; } @@ -146,7 +146,7 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form { bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8; uint16_t pitch = width; - const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, width, height, rowpixels, palette, base, &pitch); + const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, &pitch); bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)width, (uint16_t)height, mem, pitch); return handle; } @@ -157,7 +157,7 @@ bgfx::TextureHandle texture_manager::create_or_update_mame_texture(uint32_t form bgfx::TextureFormat::Enum dst_format = bgfx::TextureFormat::BGRA8; uint16_t pitch = width; - const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, width, height, rowpixels, palette, base, &pitch); + const bgfx::Memory* mem = bgfx_util::mame_texture_data_to_bgfx_texture_data(dst_format, format, rowpixels, height, palette, base, &pitch); handle = bgfx::createTexture2D(width, height, false, 1, dst_format, flags, nullptr); bgfx::updateTexture2D(handle, 0, 0, 0, 0, (uint16_t)width, (uint16_t)height, mem, pitch); diff --git a/src/osd/modules/render/bgfxutil.cpp b/src/osd/modules/render/bgfxutil.cpp index de3e3fe30d0..9d2bef27901 100644 --- a/src/osd/modules/render/bgfxutil.cpp +++ b/src/osd/modules/render/bgfxutil.cpp @@ -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 width, int height, int rowpixels, const rgb_t *palette, void *base, uint16_t *out_pitch) +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) { bgfx::TextureInfo info; switch (src_format) @@ -38,7 +38,7 @@ const bgfx::Memory* bgfx_util::mame_texture_data_to_bgfx_texture_data(bgfx::Text const bgfx::Memory* bgfx_util::mame_texture_data_to_argb32(uint32_t src_format, int width, int height, int rowpixels, const rgb_t *palette, void *base) { - const bgfx::Memory* mem = bgfx::alloc(width * height * 4); + const bgfx::Memory* mem = bgfx::alloc(rowpixels * height * 4); auto* dst = reinterpret_cast(mem->data); auto* src16 = reinterpret_cast(base); auto* src32 = reinterpret_cast(base); diff --git a/src/osd/modules/render/bgfxutil.h b/src/osd/modules/render/bgfxutil.h index a6a50436372..2f961f6551e 100644 --- a/src/osd/modules/render/bgfxutil.h +++ b/src/osd/modules/render/bgfxutil.h @@ -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 width, int height, int rowpixels, const rgb_t *palette, void *base, uint16_t *out_pitch = nullptr); + static const bgfx::Memory* mame_texture_data_to_bgfx_texture_data(bgfx::TextureFormat::Enum &dst_format, uint32_t format, int width, int height, const rgb_t *palette, void *base, uint16_t *out_pitch = nullptr); static const bgfx::Memory* mame_texture_data_to_argb32(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); }; diff --git a/src/osd/modules/render/drawbgfx.cpp b/src/osd/modules/render/drawbgfx.cpp index e9ca64ef392..2e9cb63f2e6 100644 --- a/src/osd/modules/render/drawbgfx.cpp +++ b/src/osd/modules/render/drawbgfx.cpp @@ -1171,7 +1171,7 @@ void renderer_bgfx::process_atlas_packs(std::vectortexture(), 0, 0, rect.x(), rect.y(), rect.width(), rect.height(), mem, pitch); } } -- cgit v1.2.3