Skip to content

Allow discarding out of bounds indexed draw calls #320

Closed

Description

@iraiter2

We believe that some implementations would benefit (when robust buffer access isn't available) from being able to discard indexed draws that have out of bounds values in the index buffer. Assuming we eventually have a debug mode, we could still alert the user that the draw call was discarded.

Here is one potential implementation we believe would benefit from being able to discard these calls: Google doc

Activity

  1. kainino0x commented on Jun 6, 2019

    @kainino0x
    Contributor

    PTAL, we'd like to see if we can come to an agreement next meeting.

  2. magcius commented on Jun 6, 2019

    @magcius

    Would this validation still be required if the indexes are uploaded on the CPU? It seems like a crazy thing to require for every draw call.

  3. kdashg commented on Jun 6, 2019

    @kdashg
    Contributor

    This would exacerbate portability issues. What platforms do we expect to run on that do not have RBAB?

  4. magcius commented on Jun 6, 2019

    @magcius

    Vulkan's robustBufferAccess is not guaranteed to give you 0s when reading out of range, only to not crash.

  5. kdashg commented on Jun 6, 2019

    @kdashg
    Contributor

    FWIW, in Firefox, we currently just cache [start, end] => max_index queries for index buffers, and invalidate the cache map on buffer updates.
    Assuming most buffers are immutable and that indexed draw ranges repeat (i.e. they aren't completely random), this works quite well.

    Firefox used to use a binary tree with wide leaves for tracking max_index, but it's hard to get good cache locality/behavior with this approach.

  6. kdashg commented on Jun 6, 2019

    @kdashg
    Contributor

    Vulkan's robustBufferAccess is not guaranteed to give you 0s when reading out of range, only to not crash.

    Right, but it's perhaps attractive to just surface the RBA behavior to WebGPU, for better or worse, and rely on debug/development checks to point out when this happens.

  7. magcius commented on Jun 6, 2019

    @magcius

    I think the concern is security -- drivers have been known to leak other surface contents or uninitialized data from OOB reads.

  8. kdashg commented on Jun 6, 2019

    @kdashg
    Contributor

    RBA is pretty constrained:

     Out-of-bounds buffer loads will return any of the following values:
    ▪ Values from anywhere within the memory range(s) bound to the buffer (possibly including bytes of memory past the end of the buffer, up to the end of the bound range).
    ▪ Zero values, or (0,0,0,x) vectors for vector reads where x is a valid value represented in the type of the vector components and may be any of:
    ▪ 0, 1, or the maximum representable positive integer value, for signed or unsigned integer components
    ▪ 0.0 or 1.0, for floating-point components
    
  9. kainino0x commented on Jun 7, 2019

    @kainino0x
    Contributor

    As far as we could tell, Metal has no robust buffer access behaviors, and is perfectly happy to GPU-reset if a draw is out of bounds, an index in an index buffer is out of bounds, or a compute shader reads/writes out of bounds of a storage buffer.

    Maybe this has changed in Metal 3.

  10. kainino0x commented on Jun 7, 2019

    @kainino0x
    Contributor

    Hm, speaking of Metal 3, I can't find ANYTHING announcing it. But the feature set tables have changed (that's where I noticed) and there's one mention of it in the documentation.

  11. kdashg commented on Jun 7, 2019

    @kdashg
    Contributor

    @litherum What's the OOB access story on Metal?

  12. 14 remaining items

  13. kdashg commented on Jun 14, 2019

    @kdashg
    Contributor

    Either index or indirect, really.

    Is your goal to make OOB fetches fast? Because I think we should happily slow-path those.

  14. kainino0x commented on Jun 14, 2019

    @kainino0x
    Contributor

    Rewriting the indirect buffer (the 5 ints) won't help us make the indirect call valid; we would have to no-op. It's the contents of the index buffer which are a problem.

    Are you alluding to doing index buffer rewrites (which would be very expensive), but only if the values in the index buffer would be invalid? We considered this at some point (probably should have written it down), but I think it requires a GPU-CPU roundtrip: a compute shader determines whether rewriting is necessary, we report the result back to the CPU, the CPU rewrites the draw call if necessary. I suppose on Metal it's probably possible to avoid the roundtrip by using argument buffers?

  15. iraiter2 commented on Jun 18, 2019

    @iraiter2
    ContributorAuthor

    Test from the meeting notes:

    A modified version of Apple’s Hello Triangle from https://developer.apple.com/metal/sample-code/
    500,000 triangles were rendered with position and color. One method used vertex attributes (push), and the other used a buffer to pull position / color based on vertex_id. FPS was measured using xcode.
    IR: Results:
    We ran the test on Nvidia / AMD / Intel and found no difference in FPS. This indicates that pulling works about just as well as pushing. However, we didn’t test mobile devices yet. Benchmarks on Nvidia with OpenGL from https://www.reddit.com/r/opengl/comments/6mj2uz/opengl_programmable_pulling_benchmark/ suggest it may be very slightly slower.

    We ran the same test on:
    iPad Air A1474 with 500k tris: 20 fps for both pull and push
    iPhone 7+ with 1 million tris: 45 fps for both pull and push
    iPhone xs max with 2 million tris: 30 fps for both pull and push
    Looking at the frame time which had slightly more precision, there was still no difference.

  16. kvark commented on Jun 21, 2019

    @kvark
    Contributor

    About the approach of replacing all indexed calls by indirect indexed calls. Is this suggested as a general solution? It seem to me that doing this may not even be possible:

    • on Metal, "Indirect draw and dispatch arguments" are not available on MTLGPUFamilyApple1, MTLGPUFamilyApple2, and MTLGPUFamilyCommon1
    • on Vulkan, "drawIndirectFirstInstance" feature is not available on 1/9th of devices
  17. kainino0x commented on Jun 21, 2019

    @kainino0x
    Contributor

    I think you're right that technique wouldn't work everywhere.

  18. devshgraphicsprogramming commented on Aug 7, 2019

    @devshgraphicsprogramming

    I think just clamping the index values (either via programmable pulling or shadow copy of index buffer) could work better than no-op'ing.

    Take worst case scenario, shadow copy, yes it will take more memory than your B-Tree or the segment tree, but trees are notoriously inefficient to construct on GPUs which are massively parallel, you either use atomics and painful synch or waste memory, also workload distribution is a problem.

    Also lets not forget index buffers are 2 to 4 byte indices = 6 to 12 bytes per triangle
    Whereas the most modest vertex type will be about 18 bytes (12 pos, 4 color, 4 uv packed), and average vertex re-use for normal meshes is 1.5, so that would make 24 bytes per triangle.

    TL;DR vertex data storage cost dwarfs index buffer storage cost.

    I don't really see why you'd need a segment tree for clamping, you already know the pipeline bound for the drawcall and hence the shader input buffer ranges bound, those are are segments... I don't see a need for a segment tree as ranges are already known. Am I missing something ?

  19. kainino0x commented on Aug 7, 2019

    @kainino0x
    Contributor

    The segment tree would be used to check the min/max for the actually-used range of the index buffer (determined by the setIndexBuffer offset and draw call indexCount). If the values in the that range of the buffer are out of bounds, we would no-op the call (not clamp the index values).

  20. devshgraphicsprogramming commented on Aug 8, 2019

    @devshgraphicsprogramming

    But then you'd have to query the range-tree for every draw call, there most likely will be interdependent sections where you can only check 1 drawcall ahead + fences that will prevent async/overlapped execution. Example: compute shader generates index buffer for indirect draw, then indirect draw happens immediately after, so there's only one place your validation CS can go.

    If you don't have a batch of hundreds of drawcalls to validate then you're gonna seriously undersaturate the GPU, as no-oping a drawcall is basically a reduction to a bitwise mask (make drawcount 0).

    GPU Reductions either require painfully slow AND or SET/EXCHANGE atomics (at least one per work-group) or multiple passes with memory barriers inbetween.

    Programmable Vertex Pulling could be implemented with UTB/TBO on all GPUs (TBO reads in vx shader are not SSBO access), could be cached and have no memory overhead. Plus TBO implements most vertex formats as texel formats with automatic conversions ;)

    Although vertex strides could be painful to do (but luckily no vertex format allows for unaligned offsets).

  21. kainino0x commented on Aug 9, 2019

    @kainino0x
    Contributor

    I think it was agreed that vertex pulling is most likely the best approach when we don't have robust buffer access (which is only on Metal, where Idan demonstrated that vertex pulling works well, and on unreliable Vulkan drivers). Thank you for the detailed analysis, it makes things more concrete.

    Anyway, given the pushback on this proposal, I think we are willing to drop it until it comes up again.

  22. kainino0x commented on Aug 9, 2019

    @kainino0x
    Contributor

    Actually, I remembered something @kvark said early on this thread.

    Doing vertex pulling (even inside the implementation) requires us to know the vertex buffer layout at pipeline layout creation. This looks like it won't affect Metal (where we don't use the pipeline layout until pipeline creation time anyway), but it does mean that doing a workaround for an unreliable Vulkan driver becomes more painful (have to delay pipeline layout creation until pipeline creation time).

    Are there any more thoughts on that?

  23. devshgraphicsprogramming commented on Aug 9, 2019

    @devshgraphicsprogramming

    Regarding pipeline layout creation, I don't see why you'd need to delay pipeline layout creation until pipeline creation time. On Vulkan the pipeline includes the vertex buffer layout, so you can hotpatch the pipeline layout quite easily to remove vertex inputs and replace them with Uniform Texel Buffers.

    The lovely thing about UTBs is that they count against the texture slot limit, and not against the SSBO limit.

    I wouldn't try to over-optimize and try and put more than 2 vertex buffers in one UTB.

    You have a choice of:

    • One BufferView per VkBuffer used as UTB for a Vertex Buffer Input
    • Multiple BufferView's with offsets per VkBuffer , this might be better because max UTB ranges bound could be as small as 64k
  24. kvark commented on Aug 9, 2019

    @kvark
    Contributor

    On Vulkan the pipeline includes the vertex buffer layout, so you can hotpatch the pipeline layout quite easily to remove vertex inputs and replace them with Uniform Texel Buffers.

    If we are doing this implicitly, what descriptor set do we put the vertex buffers in? Reserving a set for this sounds like an easy solution, but then on some systems we only have 4 sets maximum, and so we reducing it to 3 could be quite limiting for the user.

  25. kainino0x commented on Sep 16, 2019

    @kainino0x
    Contributor

    We resolved on the call today to allow the originally suggested behavior (allow discarding indexed draw calls which result in out of bounds reads).

  26. kainino0x commented on Sep 17, 2019

    @kainino0x
    Contributor

    (Pending input from @litherum who wasn't there.)

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions