1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.DXGI;
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class DirectX_2D
{
public SharpDX.Direct3D11.Device Device11 = null;
public SharpDX.Direct3D10.Device1 Device10 = null;
public SwapChain SwapChain = null;
public SharpDX.Direct2D1.Factory Factory = null;
public SharpDX.Direct2D1.RenderTarget Graphic = null;
public Surface Surface = null;
public SharpDX.Direct3D11.Texture2D backBuffer = null;
public SharpDX.Direct3D11.RenderTargetView renderTargetView = null;
public SharpDX.Direct3D11.Texture2D depthBuffer = null;
public SharpDX.Direct3D11.DepthStencilView depthStencilView = null;
public DirectX_2D(Form1 form)
{
// Initialisation
try
{
// SwapChain description
SwapChainDescription descriptionSwap = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription =
new ModeDescription(form.panel1.ClientSize.Width, form.panel1.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.panel1.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
// DirectX 11
SharpDX.Direct3D.FeatureLevel[] levels11 = {SharpDX.Direct3D.FeatureLevel.Level_11_0,SharpDX.Direct3D.FeatureLevel.Level_10_1,
SharpDX.Direct3D.FeatureLevel.Level_10_0,SharpDX.Direct3D.FeatureLevel.Level_9_3,
SharpDX.Direct3D.FeatureLevel.Level_9_2,SharpDX.Direct3D.FeatureLevel.Level_9_1};
try
{
SharpDX.Direct3D11.DeviceCreationFlags flag = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
SharpDX.Direct3D11.Device.CreateWithSwapChain(SharpDX.Direct3D.DriverType.Hardware, flag,
levels11, descriptionSwap, out Device11, out SwapChain);
backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(SwapChain, 0);
renderTargetView = new SharpDX.Direct3D11.RenderTargetView(Device11, backBuffer);
depthBuffer = new SharpDX.Direct3D11.Texture2D(Device11, new SharpDX.Direct3D11.Texture2DDescription()
{
Format = Format.R8G8B8A8_UNorm,
ArraySize = 1,
MipLevels = 1,
Width = form.panel1.ClientSize.Width,
Height = form.panel1.ClientSize.Height,
SampleDescription = new SampleDescription(1, 0),
Usage = SharpDX.Direct3D11.ResourceUsage.Default,
BindFlags = SharpDX.Direct3D11.BindFlags.DepthStencil,
CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
});
depthStencilView = new SharpDX.Direct3D11.DepthStencilView(Device11, depthBuffer);
SharpDX.Direct3D11.RasterizerStateDescription rasterizerDescription = new SharpDX.Direct3D11.RasterizerStateDescription()
{
CullMode = SharpDX.Direct3D11.CullMode.None,
FillMode = SharpDX.Direct3D11.FillMode.Solid,
IsAntialiasedLineEnabled = true,
IsFrontCounterClockwise = true,
IsMultisampleEnabled = true,
IsDepthClipEnabled = true,
IsScissorEnabled = false
};
Device11.ImmediateContext.Rasterizer.State =new SharpDX.Direct3D11.RasterizerState(Device11, rasterizerDescription);
Device11.ImmediateContext.Rasterizer.SetViewport(new Viewport(0, 0, (int)form.panel1.ClientSize.Width, (int)form.panel1.ClientSize.Height));
Device11.ImmediateContext.OutputMerger.SetTargets(depthStencilView, renderTargetView);
goto OK1;
}
catch
{
goto Suite1;
}
OK1:
Factory = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded);
SharpDX.Direct3D11.Texture2D backBuffer11 = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(SwapChain, 0);
Surface = backBuffer11.QueryInterface<Surface>();
Graphic = new RenderTarget(Factory, Surface, new RenderTargetProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)));
return;
// DirectX 10
Suite1:
SharpDX.Direct3D10.FeatureLevel[] levels10 ={SharpDX.Direct3D10.FeatureLevel.Level_10_1,SharpDX.Direct3D10.FeatureLevel.Level_10_0,
SharpDX.Direct3D10.FeatureLevel.Level_9_3,SharpDX.Direct3D10.FeatureLevel.Level_9_2,
SharpDX.Direct3D10.FeatureLevel.Level_9_1};
foreach (var level2 in levels10)
{
try
{
SharpDX.Direct3D10.Device1.CreateWithSwapChain(SharpDX.Direct3D10.DriverType.Hardware, SharpDX.Direct3D10.DeviceCreationFlags.BgraSupport,
descriptionSwap, level2, out Device10, out SwapChain);
goto OK2;
}
catch (ArgumentException) // E_INVALIDARG
{
continue; // Try the next feature level
}
catch (OutOfMemoryException) // E_OUTOFMEMORY
{
continue; // Try the next feature level
}
catch (Exception) // SharpDX.Direct3D10.Direct3D10Exception D3DERR_INVALIDCALL or E_FAIL
{
continue; // Try the next feature level
}
}
throw new SystemException();
OK2:
Factory = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded);
SharpDX.Direct3D10.Texture2D backBuffer10 = SharpDX.Direct3D10.Texture2D.FromSwapChain<SharpDX.Direct3D10.Texture2D>(SwapChain, 0);
Surface = backBuffer10.QueryInterface<Surface>();
Graphic = new RenderTarget(Factory, Surface, new RenderTargetProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Premultiplied)));
Graphic.DotsPerInch = new Size2F(96, 96);
}
catch (Exception e)
{
MessageBox.Show(e.Message + " -> Erreur vue2D: construction du device graphique impossible.");
Dispose();
Environment.Exit(0);
}
}
public void Dispose()
{
if (Device10 != null)
{
Device10.Dispose();
Device10 = null;
}
if (Device11 != null)
{
Device11.Dispose();
Device11 = null;
}
if (SwapChain != null)
{
SwapChain.Dispose();
SwapChain = null;
}
if (Graphic != null)
{
Graphic.Dispose();
Graphic = null;
}
if (Factory != null)
{
Factory.Dispose();
Factory = null;
}
if (backBuffer == null)
{
backBuffer.Dispose();
backBuffer = null;
}
if (renderTargetView == null)
{
renderTargetView.Dispose();
renderTargetView = null;
}
if (depthBuffer == null)
{
depthBuffer.Dispose();
depthBuffer = null;
}
if (depthStencilView == null)
{
depthStencilView.Dispose();
depthStencilView = null;
}
}
public void Render(Form1 form)
{
if (Graphic == null)
return;
Graphic.BeginDraw();
Graphic.Clear(SharpDX.Color.Black);
StrokeStyleProperties strokeProperties = new StrokeStyleProperties() { StartCap = CapStyle.Round, EndCap = CapStyle.Round };
StrokeStyle stroke = new StrokeStyle(Factory, strokeProperties);
SolidColorBrush brush = new SolidColorBrush(Graphic, new Color(255, 255, 255, 255));
Graphic.DrawLine(new Vector2(0, 0), new Vector2(form.panel1.Width, form.panel1.Height), brush, 1f, stroke);
brush.Dispose();
stroke.Dispose();
Graphic.EndDraw();
SwapChain.Present(0, PresentFlags.None);
}
}
} |
Partager