Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Check if ConsumeStructuredBuffer is empty?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Husbjörn   Members   -  Reputation: 281

Like
0Likes
Like

Posted 24 August 2014 - 04:04 AM

This may be a stupid question but I'm currently failing to see the purpose of the ConsumeStructuredBuffer if it is impossible to tell when it is empty.

 

The obvious use for such a buffer is to read in an arbitrary, unknown number of elements and process those in a shader. In order to make sure they are all processed, enough threads for a "maximum load" have to be used. So normally you'll end up with more threads than data to process. This is acceptable as long as you can tell those threads that there's nothing to do for them, but it seems it isn't possible to retrieve the internal counter of append/consume buffers on the GPU, or even to tell when they're empty. Sure, you can query the counter on the CPU and copy it into a cbuffer or similar but that pretty much takes away the whole point of having a supposedly CPU-independent data structure, doesn't it? And if I know the counter through such means as a constant, I wouldn't need the consume behaviour at all but could rather just step through each element as through a normal StructuredBuffer.

 

Surely I must be missing something here... or?



Sponsor:

#2 MJP   Moderators   -  Reputation: 11620

Like
0Likes
Like

Posted 24 August 2014 - 02:31 PM

If a bunch of threads are are all simultaneously decrementing a variable, then there's no point in "checking" the value of that variable. This is because by time you actually check it and make a decision based on the value, the variable will have changed to something else. The best you could do is call Consume, and check if the resulting structure is all 0's.

In practice I've never actually found a good use for a ConsumeStructuredBuffer. In most cases after you've appended a bunch of elements into a buffer, you just want to then do operations on every operation in that buffer. For doing that, you can instead copy the count to a constant buffer (you can do this on the GPU using CopyStructureCount) and also copy the count to an indirect arguments buffer*. With an indirect args buffer you can use DispatchIndirect to dispatch as many thread groups as you need to process all of the elements in the buffer. Then in your compute shader, you can simply test if your calculated buffer index is less than the count that you copied to your constant buffer.

*Note that for filling out an indirect arguments buffer you usually don't want to just do a straight copy of your append buffer counter, but instead run a very simple compute shader that takes the buffer count and performs arithmetic to calculate the number of thread groups needed based on your thread group size.



#3 Husbjörn   Members   -  Reputation: 281

Like
0Likes
Like

Posted 24 August 2014 - 05:08 PM

Yes, you are right in that, but I thought it would make sense that you should somehow be able to avoid situations where you run past the end of the buffer. The most reasonable would be if the Consume function used an out argument and returned a boolean indicating whether the call failed instead I guess, but alas it does nothing of the kind...

 

I don't know how it escaped me that I could copy the counter directly into another buffer; I've been putting it in a staging buffer and then updated it to other buffers from the CPU. That at least makes the whole approach a bit less contradictory.

 


The best you could do is call Consume, and check if the resulting structure is all 0's.

Is that really safe? I think MSDN said consuming past the end of a buffer results in undefined behaviour. Also the counter seems to be less than clever and will happily go into the negatives causing wrong size information if you start appending to the buffer again. But if that all-zeroes thing work I guess it could be used, I'll do some tests with that :)

 

The indirect argument buffer approach sounds interesting as well, I haven't looked into that much than reading some brief passage about it. Also is it recommended to dispense a number of threads per group or can you theoretically (and without loosing performance) have say lots of thread groups that contain just a single thread? I've always assumed that you shouldn't do this and your asterix part seems to suggest the same, but at the same time it doesn't seem the thread groups actually map directly to SIMD blocks on the GPU... or do they? But if that was the case they should be a lot more limited as to how many you can make use of than they now are?



#4 MJP   Moderators   -  Reputation: 11620

Like
0Likes
Like

Posted 24 August 2014 - 11:07 PM

I don't know if the spec unconditionally guarantees that consuming past zero will return 0'd structures, but it does guarantee that buffers will return 0 for normal out-of-bounds loads. It's probably not a great idea to rely on it, but I'm pretty sure it will happen on Consiume for most hardware since the counter will wrap to negative values. Alternatively you can use IncrementCounter/DecrementCounter instead of Append and Consume, which gives you access to the counter value so that you can perform your own custom logic before loading.

 

As far as thread groups, all common GPU's execute threads in groups (it's fixed size for Nvidia and AMD, 32 for Nvidia and 64 for AMD) and you will waste execution resources if you don't issue enough work to use those threads. In general the IHV's have recommended dispatching thread groups that are a multiple of this size, in order to make sure that it can evenly fill up a thread group with warps/wavefronts. This is obviously hardware-specific so D3D11 doesn't make any guarantees about this, and technically I think that implementations are free to combine multiple thread groups into a single warp/wavefront. However I don't think that I would assume that to be the case. There's also the issue that you're limited to max of 64K thread groups in any dispatch dimension, so you may go over that limit for larger buffers.
 


Edited by MJP, 26 August 2014 - 11:36 AM.


#5 Husbjörn   Members   -  Reputation: 281

Like
0Likes
Like

Posted 26 August 2014 - 03:39 AM

It seems it does in fact return the full-zero thing for me as well, but as you say I don't know if that is safe to assume will be the case for any card. The counter approach does seem the most appropriate here, I'll try to switch to that and see how it fares. I guess the D3D11_BUFFER_UAV_FLAG_COUNTER cannot be combined with the D3D11_BUFFER_UAV_FLAG_APPEND flag? Actually I can just check that myself and if it doesn't I'll just use the IncrementCounter function for filling it out too.

 

Thanks for the threading explanation, I managed to track down some more detailed information from the "warp" keyword which I hadn't heard until now :)

Also I just found out you're on of the co-authors of the Practical Rendering with DirectX 11 book I've been referring a lot lately, so thanks a lot for that too!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS