xenko
xenko copied to clipboard
Need functionality to reset Append Buffer counter
Hello, I have been using Appends Buffers (DX11 functionality) in my project (2.0.0.2) and I realised that the functionality for resetting the counter is missing.
I ended up patching the run-time myself but that would be nice if this could be added on your side too.
Just for the heads up the function that allows resetting the counter is the following:
NativeDeviceContext.ComputeShader.SetUnorderedAccessView(slot, view, *counterValue*);
Cheers.
Yes, totally needed! Just hit the same problem.
Luckily Xenko caches the unordered access views internally, so in the meantime you can do a hack with reflection. Works nicely here:
public static class NativeDeviceUtils
{
static FieldInfo nativeDeviceContextFi;
static FieldInfo unorderedAccessViewsFi;
static NativeDeviceUtils()
{
var type = Type.GetType("SiliconStudio.Xenko.Graphics.CommandList, SiliconStudio.Xenko.Graphics");
var fields = type.GetRuntimeFields();
nativeDeviceContextFi = fields.Where(fi => fi.Name == "nativeDeviceContext").First();
unorderedAccessViewsFi = fields.Where(fi => fi.Name == "unorderedAccessViews").First();
}
public static SharpDX.Direct3D11.DeviceContext GetNativeDeviceContext(this CommandList commandList)
{
return (SharpDX.Direct3D11.DeviceContext)nativeDeviceContextFi.GetValue(commandList);
}
public static void ComputeShaderReApplyUnorderedAccessView(this CommandList commandList, int slot, int counterValue)
{
var uavs = (SharpDX.Direct3D11.UnorderedAccessView[])unorderedAccessViewsFi.GetValue(commandList);
commandList.GetNativeDeviceContext().ComputeShader.SetUnorderedAccessView(slot, uavs[slot], counterValue);
}
}
Then in your compute dispatcher simply write:
// Apply the effect
EffectInstance.Apply(context.GraphicsContext);
if (ResetCounter)
commandList.ComputeShaderReApplyUnorderedAccessView(0, CounterValue);
// Dispatch compute shader
commandList.Dispatch(ThreadGroupCounts.X, ThreadGroupCounts.Y, ThreadGroupCounts.Z);
For example make a copy of this class and add the two counter properties: https://github.com/SiliconStudio/xenko/blob/master-2.1/sources/engine/SiliconStudio.Xenko.Engine/Rendering/ComputeEffect/ComputeEffectShader.cs
The hack is of course limited to DX11 on Windows, but it can be adapted to other platforms with some work.