The Task is to compute the lengths of 64 3D Vectors in ComputeShader using a ConsumeStructuredBuffer as Input and AppendStructuredBuffer as Output.I always get the result 0 for the length. Even if i try to get the content of the ConsumeStructuredBuffer in the shader i always get 0. I checked the Dimensions of the ConsumeBuffer and the AppendBuffer in Shader and it gets me the correct results.

When you bind the buffers to the compute shader, do you specify the initial counts properly to reflect the number of items in them? This is important, or else your compute shader will think the buffer is empty, and when it tries to consume the values it will just get all zeros.

The way that Append/Consume buffers work is that there's a "hidden" counter on the buffer that holds the number of items in it. This number of items <= the number of elements in the buffer when you create it. There are three ways to change this counter: by calling Append (increments the counter), but calling Consume (decrements the counter), or by manually specifying the count when calling CSSetUnorderedAccessViews. In your particular case, if you don't specify the count when binding the UAV it will be 0 and Consume won't give you back an element from the buffer.

In general, you should be careful to consider whether you actually need to use an Append or Consume buffer. In any case where you know the number of elements ahead of time you typically don't need to use them. For instance if you run 100 threads and only some of them may output a value, then you want to use an Append buffer since it's unknown how many you will end up with. However if you then run another compute shader that reads in the N elements that were output and does some processing on them, then you don't need to use a Consume buffer since you can just copy the hidden counter out of the buffer and into a constant buffer. Then you can just access the elements without calling Consume, which is actually faster since the hardware doesn't need to do a global atomic decrement.

That "NumElements" is the maximum number of elements in the buffer. Append/Consume buffers maintain a separate counter indicating how many elements out of the total number are in use at any given period of time. It's conceptually similar to the difference between capacity() and size() in std::vector: the former tell you how much memory was allocated for the internal array of elements, while the latter tells you how many elements you've actually added to the array. Like I said before you need to change the counter you either need to Append to the buffer, you or you need to specify the count when binding the UAV.

Maybe there is an error on how i bind them. Do i have to bind both - the consume buffer and the append buffer with a UnorderedAccessVIew?

Yes, you do need to use UAVs for both of them. In your code above you are creating both of them, so I would assume you are using both... It might help if you just post the code where you set the UAVs in the compute shader, and we can point out what is going on more directly.

MJPs description is very precise and clear - please read through it again if it still isn't completely clear to you. In order for you to get something back when your shader code calls 'Consume', the internal counter has to think there is some elements already existing in it. If you don't use the 'initial counts' parameter when you bind the UAV to the shader stage, then you have to actually append values into that buffer with shader code.

If you want an example of this in action, you can look at the ParticleStorm demo from Hieroglyph 3. In that case, I append values directly in a shader to build up the number of particles in my append buffer. Those are then run through another shader pass that consumes each particle, updates it, then appends it again to another buffer. Follow along in your own code and determine what your structure count is at each point in your program. You can use the ID3D11DeviceContext::CopyStructureCount(...) method to find out what values are in your buffer's structure count.

Ok thanks, i understand. The problem is to set the initial counter for the buffer. I use the Effect11 Framework to set the UnordereredAccessViews (http://msdn.microsoft.com/en-us/library/windows/desktop/ff476783(v=vs.85).aspx). The methods seem to have no parameters to set the counter. That means i cant use the Effect11 Framwork to set an UnorderedAccessView to a ConsumeBuffer? I have to use the CSSetUnorderedAccessViews method..?

How are you checking the result of your calculations? I would suggest checking the value you get from the copy structure count method for each of your buffers before and after this pass are executed. If you are still unable to get it to return anything other than 0 for the result, then I would write a small compute shader that will fill your append buffer with with data (i.e. just add vectors that reflect the threadID of the compute shader thread). That would help you rule out that your data is not getting initialized properly, and it will also help you in debugging how the counter works when you are querying its value.

Is there any debug output in the console window when you execute your code? Have you enabled the debug layer when you are creating your device? Both of these will help you in debugging any issues if you haven't already checked them.