• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Husbj

Check if ConsumeStructuredBuffer is empty?

4 posts in this topic

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?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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?

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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!

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0