Skip to content

Instantly share code, notes, and snippets.

@weltkante
Created January 15, 2016 18:39
Show Gist options
  • Save weltkante/13b41a0289339ebf80e7 to your computer and use it in GitHub Desktop.
Save weltkante/13b41a0289339ebf80e7 to your computer and use it in GitHub Desktop.
D3D11Image + DepthBuffer test
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using Microsoft.Wpf.Interop.DirectX;
using SharpDX;
using SharpDX.D3DCompiler;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Mathematics.Interop;
namespace WpfApplication1
{
/*
This is some test code to check if depth buffers work with D3D11Image.
It does not follow any "best practices" and should not be used for anything
except to verify that the depth buffers work with D3D11Image.
nuget references:
<package id="Microsoft.Wpf.Interop.DirectX-x86" version="0.9.0-beta-22856" targetFramework="net461" />
<package id="SharpDX" version="3.0.0" targetFramework="net461" />
<package id="SharpDX.D3DCompiler" version="3.0.0" targetFramework="net461" />
<package id="SharpDX.Direct3D11" version="3.0.0" targetFramework="net461" />
<package id="SharpDX.DXGI" version="3.0.0" targetFramework="net461" />
<package id="SharpDX.Mathematics" version="3.0.0" targetFramework="net461" />
*/
public partial class MainWindow : Window
{
private D3D11Image mImage;
private SharpDX.Direct3D11.Device mDevice;
private DeviceContext mContext;
private RenderTargetView mRenderTarget;
private Texture2D mDepthTex;
private DepthStencilView mDepthView;
private Grid mHost;
private Image mImageControl;
public MainWindow()
{
InitializeComponent();
mImage = new D3D11Image();
mImage.OnRender = Render;
mImageControl = new Image();
mImageControl.Source = mImage;
mHost = new Grid();
mHost.Children.Add(mImageControl);
mHost.Loaded += delegate {
mDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware);
mContext = mDevice.ImmediateContext;
InputElement[] elements =
{
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0),
new InputElement("COLOR", 0, Format.B8G8R8A8_UNorm, 0),
};
var vertexShaderCode = ShaderBytecode.Compile(@"
float4x4 camera: register(b0);
void main(
float3 in_pos: POSITION0,
float4 in_color: COLOR0,
out float4 out_pos: SV_POSITION,
out float4 out_color: COLOR0)
{
out_pos = mul(camera, float4(in_pos, 1));
out_color = in_color;
}
", "main", "vs_5_0");
var pixelShaderCode = ShaderBytecode.Compile(@"
float4 main(
float4 pos: SV_POSITION,
float4 color: COLOR0): SV_TARGET
{
return color;
}
", "main", "ps_5_0");
mContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
mContext.InputAssembler.InputLayout = new InputLayout(mDevice, vertexShaderCode, elements);
mContext.VertexShader.Set(new VertexShader(mDevice, vertexShaderCode));
mContext.PixelShader.Set(new PixelShader(mDevice, pixelShaderCode));
mImage.SetPixelSize((int)mHost.ActualWidth, (int)mHost.ActualHeight);
mImage.WindowOwner = new WindowInteropHelper(this).Handle;
mImage.RequestRender();
};
Content = mHost;
}
private struct Vertex
{
public RawVector3 Position;
public RawColorBGRA Color;
public Vertex(Vector3 Position, ColorBGRA Color)
{
this.Position = Position;
this.Color = Color;
}
}
private void Render(IntPtr pRenderTarget, bool isNewRenderTarget)
{
if (isNewRenderTarget)
{
var surf = new ComObject(pRenderTarget).QueryInterface<SharpDX.DXGI.Resource>();
var res = mDevice.OpenSharedResource<SharpDX.Direct3D11.Resource>(surf.SharedHandle);
mRenderTarget = new RenderTargetView(mDevice, res);
mDepthTex = new Texture2D(mDevice, new Texture2DDescription {
BindFlags = BindFlags.DepthStencil,
Format = Format.D32_Float,
//Format = Format.D32_Float_S8X24_UInt, -- alternative format
Width = (int)mHost.ActualWidth,
Height = (int)mHost.ActualHeight,
MipLevels = 1,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
OptionFlags = ResourceOptionFlags.None,
CpuAccessFlags = CpuAccessFlags.None,
ArraySize = 1,
});
mDepthView = new DepthStencilView(mDevice, mDepthTex);
mContext.OutputMerger.SetTargets(mDepthView, mRenderTarget);
mContext.Rasterizer.SetViewport(0, 0, (int)mHost.ActualWidth, (int)mHost.ActualHeight);
}
mContext.ClearRenderTargetView(mRenderTarget, Color.White);
mContext.ClearDepthStencilView(mDepthView, DepthStencilClearFlags.Depth, 1, 0);
var camera = Matrix.LookAtLH(new Vector3(10, 5, -5), new Vector3(0, 0, 10), Vector3.Up)
* Matrix.PerspectiveFovLH(MathUtil.PiOverFour, (float)(mHost.ActualWidth / mHost.ActualHeight), 0.01f, 100.0f);
var buffer = SharpDX.Direct3D11.Buffer.Create(mDevice, ref camera,
new BufferDescription {
SizeInBytes = Marshal.SizeOf<Matrix>(),
Usage = ResourceUsage.Immutable,
BindFlags = BindFlags.ConstantBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
StructureByteStride = 0,
});
mContext.VertexShader.SetConstantBuffer(0, buffer);
Render(new Vertex[] {
new Vertex(new Vector3(0, 0, 10), Color.Blue),
new Vertex(new Vector3(0, 1, 10), Color.Blue),
new Vertex(new Vector3(1, 0, 15), Color.Blue),
new Vertex(new Vector3(1, 0, 15), Color.Blue),
new Vertex(new Vector3(0, 1, 10), Color.Blue),
new Vertex(new Vector3(1, 1, 15), Color.Blue),
});
Render(new Vertex[] {
new Vertex(new Vector3(0, 0, 20), Color.Red),
new Vertex(new Vector3(1, 0, 8), Color.Red),
new Vertex(new Vector3(0, 1, 20), Color.Red),
new Vertex(new Vector3(0, 1, 20), Color.Red),
new Vertex(new Vector3(1, 0, 8), Color.Red),
new Vertex(new Vector3(1, 1, 8), Color.Red),
});
mContext.Flush();
}
private void Render(params Vertex[] vd)
{
mContext.InputAssembler.SetVertexBuffers(0,
new VertexBufferBinding(SharpDX.Direct3D11.Buffer.Create(mDevice, vd, new BufferDescription {
SizeInBytes = Marshal.SizeOf<Vertex>() * vd.Length,
Usage = ResourceUsage.Immutable,
BindFlags = BindFlags.VertexBuffer}),
Marshal.SizeOf<Vertex>(), 0));
mContext.Draw(6, 0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment