bkaradzic / bgfx

Cross-platform, graphics API agnostic, "Bring Your Own Engine/Framework" style rendering library.
https://bkaradzic.github.io/bgfx/overview.html
BSD 2-Clause "Simplified" License
15.02k stars 1.95k forks source link

<macOS> 08-update crash in copyFromTexture #2814

Open lemoon opened 2 years ago

lemoon commented 2 years ago

https://github.com/bkaradzic/bgfx/commit/17bb4dce49289ee16c783c2bc437c3a2aa2d8040 this commit will cause a crash on macOS

void copyFromTexture(
              id<MTLTexture> _sourceTexture
            , NSUInteger _sourceSlice
            , NSUInteger _sourceLevel
            , MTLOrigin _sourceOrigin
            , MTLSize _sourceSize
            , id<MTLTexture> _destinationTexture
            , NSUInteger _destinationSlice
            , NSUInteger _destinationLevel
            , MTLOrigin _destinationOrigin
            )
        {
            [m_obj copyFromTexture:_sourceTexture sourceSlice:_sourceSlice sourceLevel:_sourceLevel sourceOrigin:_sourceOrigin sourceSize:_sourceSize
                         toTexture:_destinationTexture destinationSlice:_destinationSlice destinationLevel:_destinationLevel destinationOrigin:_destinationOrigin];
        }

-[MTLDebugBlitCommandEncoder internalValidateCopyFromTexture:sourceSlice:sourceLevel:sourceOrigin:sourceSize:toTexture:destinationSlice:destinationLevel:destinationOrigin:options:]:486: failed assertion `(destinationOrigin.x + destinationSize.width)(4) must be <= width(2).'

add back this code will be fine

for (uint8_t lod = 0, num = imageContainer->m_numMips; lod < num; ++lod)
{
                    if (width < 4 || height < 4)
                    {
                        break;
                    }
...
}
attilaz commented 10 months ago

https://github.com/bkaradzic/bgfx/blob/e9e2e224ee777f841b0bd8fc119b10e688983a46/examples/08-update/update.cpp#L185

The problem is that blockWidth is incorrect when the texture is supported only by emulation. When emulation used instead of the real format an rgba8/bgra8 texture format is used.

One solution would be to modify example:

instead of this: const bimg::ImageBlockInfo& blockInfo = getBlockInfo(imageContainer->m_format);

do this:

   bimg::TextureFormat::Enum format = imageContainer->m_format;
   if ( 0 == (bgfx::getCaps()->formats[imageContainer->m_format] & BGFX_CAPS_FORMAT_TEXTURE_2D) )
               format = bimg::TextureFormat::BGRA8;

  const bimg::ImageBlockInfo& blockInfo = getBlockInfo(format);