I am trying to use a constant buffer in a program using SharpDX.Toolkit. I use the Toolkit because it has nice font support for writing on the screen.
The problem I face is in assigning data to the constant buffer.
In the screen below you may see a debug of my code. When it comes to the Draw event the buffer gets empty (obj:9 is active) and in Geometry Shader all values are 0.0f. On event 76 - GSSetShader the obj:16 is active, which contains the data I want to use, non-zero values. I have no idea why it gets overwritten/cleared. I tried assigning or updating buffer in numerous places in the draw method, but none of them helped. For sure it is not a problem of a slot, as in the debug window both (populated and empty) buffers are in slot 0.
NOTE: this way of loading data to constant buffer worked before I switched to using Toolkit, but then the device was explicitly created with SwapChain. I am not so experienced to tell if it may make any difference.
In my effect file I have defined the buffer as
cbuffer ConstantBuffer : register(b0)
{
float slideX;
float sinPi075;
float cosPi075;
float scaleK;
}
and in the main program
[StructLayout(LayoutKind.Explicit, Size = 16)]
struct GS_CONSTANT_BUF_DATA
{
[FieldOffset(0)]
public float slideX;
[FieldOffset(4)]
public float sinPi075;
[FieldOffset(8)]
public float cosPi075;
[FieldOffset(12)]
public float scaleK;
};
followed by
private SharpDX.Direct3D11.Buffer _constantBuffer;
private GS_CONSTANT_BUF_DATA constData;
private int sizeOfconstData;
private SharpDX.Direct3D11.Device _device;
private void InitializeConstantBuffer()
{
System.Diagnostics.Debug.WriteLine("InitializeConstantBuffer");
constData = new GS_CONSTANT_BUF_DATA
{
slideX = 0.0f,
sinPi075 = (float)Math.Sin(Math.PI * 0.75),
cosPi075 = (float)Math.Cos(Math.PI * 0.75),
scaleK = 0.003f
};
_device = (SharpDX.Direct3D11.Device)GraphicsDevice.MainDevice;
sizeOfconstData = Utilities.SizeOf<GS_CONSTANT_BUF_DATA>();
_constantBuffer = new SharpDX.Direct3D11.Buffer(
_device
, sizeOfconstData
, ResourceUsage.Default
, BindFlags.ConstantBuffer
, CpuAccessFlags.None
, ResourceOptionFlags.None
, 0);
_device.ImmediateContext.GeometryShader.SetConstantBuffer(0, _constantBuffer);
_device.ImmediateContext.UpdateSubresource(ref constData, _constantBuffer);
}
Draw method in the main program
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
for (int i = 0; i < technique.Passes.Count; ++i)
{
_device.ImmediateContext.GeometryShader.SetConstantBuffer(0, _constantBuffer);
_chartGeometry.Draw(
GraphicsDevice
, SharpDX.Toolkit.Graphics.PrimitiveType.LineList
, technique.Passes[i]);
}
}
And the Draw method associated with _chartGeometry
public void Draw(GraphicsDevice graphicsDevice, PrimitiveType primitiveType, EffectPass pass)
{
if (pass != null)
pass.Apply();
// Setup the Vertex Buffer
graphicsDevice.SetVertexBuffer(0, vertexBuffer);
// Setup the Vertex Buffer Input layout
graphicsDevice.SetVertexInputLayout(InputLayout);
graphicsDevice.Draw(primitiveType, 4, 0);
}
I have also tried changing buffer to dynamic, and hence, changing UpdateSubresource to Map/Unmap. The result is the same, see creen below, this time obj:16 (has data) and obj:8 (does not).
and while debuging GeometryShader locals show