Skip to content

Metal Validation Layers require sourceBytesPerImage >= 512 bytes #537

Closed

Description

@austinEng

Ran into this undocumented restriction when running with Metal validation layers:

-[MTLDebugBlitCommandEncoder validateCopyFromBuffer:sourceOffset:sourceBytesPerRow:sourceBytesPerImage:sourceSize:toTexture:destinationSlice:destinationLevel:destinationOrigin:options:]:532: failed assertion `sourceBytesPerImage(448) must be >= (512).'
Process 31489 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00007fff5e1df2c2 libsystem_kernel.dylib`__pthread_kill + 10
libsystem_kernel.dylib`__pthread_kill:
->  0x7fff5e1df2c2 <+10>: jae    0x7fff5e1df2cc            ; <+20>
    0x7fff5e1df2c4 <+12>: movq   %rax, %rdi
    0x7fff5e1df2c7 <+15>: jmp    0x7fff5e1d9453            ; cerror_nocancel
    0x7fff5e1df2cc <+20>: retq   

Also, sourceBytesPerRow must be >= 64, but that's fine because WebGPU has rowPitch be a multiple of 256.

This has some validation implications on permissible values for imageHeight or (zPitch in #519), and it also means that imageHeight == 0 cannot truly mean "tightly packed" in #517.

Activity

  1. kvark commented on Jan 8, 2020

    @kvark
    Contributor

    Good point! Working around this may be annoying, and given the fact we already require some buffer offset and pitch alignment, it only seems logical to require the slice alignment as well. We can leave imageHeight == 0 only valid for cases of depth == 1, as suggested earlier by @Kangz IIRC.

  2. Kangz commented on Jan 8, 2020

    @Kangz
    Contributor

    Maybe this validation triggers in the Metal validation layer only when the copy is more than one slice. We could split copies of N slices into N copies of one slice to work around the issue. Also the Metal validation layers often warn about things that we haven't noticed causing bugs in practice. It would be nice to have confirmation that this is indeed a constraint of Metal.

  3. kainino0x commented on Jan 8, 2020

    @kainino0x
    Contributor

    My guess is that things that validation layers catch that otherwise work are usually problematic on some iOS devices.

  4. kvark commented on Jan 8, 2020

    @kvark
    Contributor

    @Kangz splitting would only work if each slice is at least 4 byte aligned in Metal, so there's got to be some alignment restriction on our slices, might as well be 512.

  5. kainino0x commented on Jan 8, 2020

    @kainino0x
    Contributor

    I'm not sure what the alignment restriction is; 512 is a minimum and not an alignment. Since the row pitch alignment is 256, I think this only affects one case: rowPitch = 256 bytes, height = 1, depth > 1.

  6. kvark commented on Jan 9, 2020

    @kvark
    Contributor

    @kainino0x bummer, you are right. This looks pretty minor then and can be trivially worked around.

  7. JusSn commented on Jan 28, 2020

    @JusSn
    Contributor

    @austinEng: Do you remember what texture format you are copying to? This limit may exist to prevent one from copying a region smaller than one block of a block-compressed format.

  8. austinEng commented on Jan 28, 2020

    @austinEng
    ContributorAuthor

    I don't remember the exact format, but it definitely wasn't block-compressed. It was 128 x 128 and either rgba8-unorm or depth32-float. Would it be possible for you to ask the Metal team about this specific validation?

  9. JusSn commented on Jan 28, 2020

    @JusSn
    Contributor

    @austinEng Yeah, that was their response to my asking :/ I'll keep digging!

  10. JusSn commented on Feb 3, 2020

    @JusSn
    Contributor

    the requirement is that the strides (ie: sourceBytesPerRow/sourceBytesPerImage) are all of:

    1. multiple of their smaller variant (ie: sourceBytesPerRow is a multiple of block size, and sourceBytesPerImage is a multiple of sourceBytesPerRow)
    2. cover at least sourceSize pixels (rounded up to block size, where applicable)

    for non-block types, say the mentioned 32bit types, you'd expect sourceBytesPerRow a multiple of 4, and at least sourceSize.width * 4

  11. added this to the V1.0 milestone on Sep 2, 2021
  12. Kangz commented on Nov 3, 2021

    @Kangz
    Contributor

    #537 (comment) gives validation rules that are already present in WebGPU. It seems like this might have been a Metal debug device bug. @litherum can you confirm and close this issue if it is the case?

  13. Kangz commented on Dec 14, 2021

    @Kangz
    Contributor

    I think we should just close this, and see if we find the Metal Debug Device error again when running the CTS against it.

  14. litherum commented on Dec 15, 2021

    @litherum
    Contributor

    The requirement isn't hardcoded to 512 bytes. The requirement is that the sourceBytesPerImage is greater than the row size of the image times the height of the image.

  15. kainino0x commented on Dec 15, 2021

    @kainino0x
    Contributor

    Huh. In that case, it should be accounted for in the spec with:

    If specified, layout.rowsPerImage must be greater than or equal to heightInBlocks.

    So if it was just that, I guess we had some bug at the time we saw that debug error. (And the debug error was rather confusing.)

  16. litherum commented on Dec 15, 2021

    @litherum
    Contributor

    I filed rdar://86501218 about improving the error message to be more descriptive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions