BioImager  3.9.1
A .NET microscopy imaging library. Supports various microscopes by using imported libraries & GUI automation. Supported libraries include PriorĀ® & ZeissĀ® & all devices supported by Micromanager 2.0 and python-microscope.
Loading...
Searching...
No Matches
BioImager.DDX11 Class Reference

Public Member Functions

bool Initialize (DSystemConfiguration configuration, IntPtr windowHandle)
 
void ShutDown ()
 
void BeginScene (float red, float green, float blue, float alpha)
 
void EndScene ()
 

Properties

int VideoCardMemory [get]
 
string VideoCardDescription [get]
 
SharpDX.Direct3D11.Device Device [get]
 
DeviceContext DeviceContext [get]
 
DepthStencilState DepthStencilState [get, set]
 
Matrix ProjectionMatrix [get]
 
Matrix WorldMatrix [get, set]
 

Detailed Description

Definition at line 8 of file DDX11.cs.

Constructor & Destructor Documentation

◆ DDX11()

BioImager.DDX11.DDX11 ( )

Definition at line 26 of file DDX11.cs.

26{ }

Member Function Documentation

◆ BeginScene()

void BioImager.DDX11.BeginScene ( float  red,
float  green,
float  blue,
float  alpha 
)

Definition at line 263 of file DDX11.cs.

264 {
265 BeginScene(new Color4(red, green, blue, alpha));
266 }

◆ EndScene()

void BioImager.DDX11.EndScene ( )

Definition at line 275 of file DDX11.cs.

276 {
277 // Present the back buffer to the screen since rendering is complete.
278 if (VerticalSyncEnabled)
279 SwapChain.Present(1, PresentFlags.None); // Lock to screen refresh rate.
280 else
281 SwapChain.Present(0, PresentFlags.None); // Present as fast as possible.
282 }

◆ Initialize()

bool BioImager.DDX11.Initialize ( DSystemConfiguration  configuration,
IntPtr  windowHandle 
)

Definition at line 29 of file DDX11.cs.

30 {
31 try
32 {
33 // Store the vsync setting.
34 VerticalSyncEnabled = DSystemConfiguration.VerticalSyncEnabled;
35
36 // Create a DirectX graphics interface factory.
37 var factory = new Factory1();
38
39 // Use the factory to create an adapter for the primary graphics interface (video card).
40 var adapter = factory.GetAdapter1(0);
41
42 // Get the primary adapter output (monitor).
43 var monitor = adapter.GetOutput(0);
44
45 // Get modes that fit the DXGI_FORMAT_R8G8B8A8_UNORM display format for the adapter output (monitor).
46 var modes = monitor.GetDisplayModeList(Format.R8G8B8A8_UNorm, DisplayModeEnumerationFlags.Interlaced);
47
48 // Now go through all the display modes and find the one that matches the screen width and height.
49 // When a match is found store the the refresh rate for that monitor, if vertical sync is enabled.
50 // Otherwise we use maximum refresh rate.
51 var rational = new Rational(0, 1);
52 if (VerticalSyncEnabled)
53 {
54 foreach (var mode in modes)
55 {
56 if (mode.Width == configuration.Width && mode.Height == configuration.Height)
57 {
58 rational = new Rational(mode.RefreshRate.Numerator, mode.RefreshRate.Denominator);
59 break;
60 }
61 }
62 }
63
64 // Get the adapter (video card) description.
65 var adapterDescription = adapter.Description;
66
67 // Store the dedicated video card memory in megabytes.
68 //VideoCardMemory = adapterDescription.DedicatedVideoMemory >> 10 >> 10;
69
70 // Convert the name of the video card to a character array and store it.
71 VideoCardDescription = adapterDescription.Description.Trim('\0');
72
73 // Release the adapter output.
74 monitor.Dispose();
75 // Release the adapter.
76 adapter.Dispose();
77 // Release the factory.
78 factory.Dispose();
79
80 // Initialize the swap chain description.
81 var swapChainDesc = new SwapChainDescription()
82 {
83 // Set to a single back buffer.
84 BufferCount = 1,
85 // Set the width and height of the back buffer.
86 ModeDescription = new ModeDescription(configuration.Width, configuration.Height, rational, Format.R8G8B8A8_UNorm),
87 // Set the usage of the back buffer.
88 Usage = Usage.RenderTargetOutput,
89 // Set the handle for the window to render to.
90 OutputHandle = windowHandle,
91 // Turn multisampling off.
92 SampleDescription = new SampleDescription(1, 0),
93 // Set to full screen or windowed mode.
94 IsWindowed = !DSystemConfiguration.FullScreen,
95 // Don't set the advanced flags.
96 Flags = SwapChainFlags.None,
97 // Discard the back buffer content after presenting.
98 SwapEffect = SwapEffect.Discard
99 };
100
101 // Create the swap chain, Direct3D device, and Direct3D device context.
102 SharpDX.Direct3D11.Device device;
103 SwapChain swapChain;
104 SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out device, out swapChain);
105
106 Device = device;
107 SwapChain = swapChain;
108 DeviceContext = device.ImmediateContext;
109
110 // Get the pointer to the back buffer.
111 var backBuffer = Texture2D.FromSwapChain<Texture2D>(SwapChain, 0);
112
113 // Create the render target view with the back buffer pointer.
114 RenderTargetView = new RenderTargetView(device, backBuffer);
115
116 // Release pointer to the back buffer as we no longer need it.
117 backBuffer.Dispose();
118
119 // Initialize and set up the description of the depth buffer.
120 Texture2DDescription depthBufferDesc = new Texture2DDescription()
121 {
122 Width = configuration.Width,
123 Height = configuration.Height,
124 MipLevels = 1,
125 ArraySize = 1,
126 Format = Format.D24_UNorm_S8_UInt,
127 SampleDescription = new SampleDescription(1, 0),
128 Usage = ResourceUsage.Default,
129 BindFlags = BindFlags.DepthStencil,
130 CpuAccessFlags = CpuAccessFlags.None,
131 OptionFlags = ResourceOptionFlags.None
132 };
133
134 // Create the texture for the depth buffer using the filled out description.
135 DepthStencilBuffer = new Texture2D(device, depthBufferDesc);
136
137 // Initialize and set up the description of the stencil state.
138 DepthStencilStateDescription depthStencilDesc = new DepthStencilStateDescription()
139 {
140 IsDepthEnabled = true,
141 DepthWriteMask = DepthWriteMask.All,
142 DepthComparison = Comparison.Less,
143 IsStencilEnabled = true,
144 StencilReadMask = 0xFF,
145 StencilWriteMask = 0xFF,
146 // Stencil operation if pixel front-facing.
147 FrontFace = new DepthStencilOperationDescription()
148 {
149 FailOperation = StencilOperation.Keep,
150 DepthFailOperation = StencilOperation.Increment,
151 PassOperation = StencilOperation.Keep,
152 Comparison = Comparison.Always
153 },
154 // Stencil operation if pixel is back-facing.
155 BackFace = new DepthStencilOperationDescription()
156 {
157 FailOperation = StencilOperation.Keep,
158 DepthFailOperation = StencilOperation.Decrement,
159 PassOperation = StencilOperation.Keep,
160 Comparison = Comparison.Always
161 }
162 };
163
164 // Create the depth stencil state.
165 DepthStencilState = new DepthStencilState(Device, depthStencilDesc);
166
167 // Set the depth stencil state.
168 DeviceContext.OutputMerger.SetDepthStencilState(DepthStencilState, 1);
169
170 // Initialize and set up the depth stencil view.
171 DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
172 {
173 Format = Format.D24_UNorm_S8_UInt,
174 Dimension = DepthStencilViewDimension.Texture2D,
175 Texture2D = new DepthStencilViewDescription.Texture2DResource()
176 {
177 MipSlice = 0
178 }
179 };
180
181 // Create the depth stencil view.
182 DepthStencilView = new DepthStencilView(Device, DepthStencilBuffer, depthStencilViewDesc);
183
184 // Bind the render target view and depth stencil buffer to the output render pipeline.
185 DeviceContext.OutputMerger.SetTargets(DepthStencilView, RenderTargetView);
186
187 // Setup the raster description which will determine how and what polygon will be drawn.
188 RasterizerStateDescription rasterDesc = new RasterizerStateDescription()
189 {
190 IsAntialiasedLineEnabled = false,
191 CullMode = CullMode.Back,
192 DepthBias = 0,
193 DepthBiasClamp = .0f,
194 IsDepthClipEnabled = false,
195 FillMode = FillMode.Solid,
196 IsFrontCounterClockwise = false,
197 IsMultisampleEnabled = false,
198 IsScissorEnabled = false,
199 SlopeScaledDepthBias = .0f
200 };
201
202 // Create the rasterizer state from the description we just filled out.
203 RasterState = new RasterizerState(Device, rasterDesc);
204
205 // Now set the rasterizer state.
206 DeviceContext.Rasterizer.State = RasterState;
207
208 // Setup and create the viewport for rendering.
209 DeviceContext.Rasterizer.SetViewport(0, 0, configuration.Width, configuration.Height, 0, 1);
210
211 // Setup and create the projection matrix.
212 ProjectionMatrix = Matrix.PerspectiveFovLH((float)(Math.PI / 4), ((float)configuration.Width / (float)configuration.Height), DSystemConfiguration.ScreenNear, DSystemConfiguration.ScreenDepth);
213
214 // Initialize the world matrix to the identity matrix.
215 WorldMatrix = Matrix.Identity;
216
217 BlendStateDescription blendStateDescription = new BlendStateDescription
218 {
219 AlphaToCoverageEnable = false,
220 };
221
222 blendStateDescription.RenderTarget[0].IsBlendEnabled = true;
223 blendStateDescription.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
224 blendStateDescription.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
225 blendStateDescription.RenderTarget[0].BlendOperation = BlendOperation.Add;
226 blendStateDescription.RenderTarget[0].SourceAlphaBlend = BlendOption.Zero;
227 blendStateDescription.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
228 blendStateDescription.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
229 blendStateDescription.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
230
231 DeviceContext.OutputMerger.BlendState = new BlendState(device, blendStateDescription);
232
233 return true;
234 }
235 catch
236 {
237 return false;
238 }
239 }

◆ ShutDown()

void BioImager.DDX11.ShutDown ( )

Definition at line 240 of file DDX11.cs.

241 {
242 // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
243 SwapChain?.SetFullscreenState(false, null);
244
245 // Dispose of all objects.
246 RasterState?.Dispose();
247 RasterState = null;
248 DepthStencilView?.Dispose();
249 DepthStencilView = null;
250 DepthStencilState?.Dispose();
251 DepthStencilState = null;
252 DepthStencilBuffer?.Dispose();
253 DepthStencilBuffer = null;
254 RenderTargetView?.Dispose();
255 RenderTargetView = null;
256 DeviceContext?.Dispose();
257 DeviceContext = null;
258 Device?.Dispose();
259 Device = null;
260 SwapChain?.Dispose();
261 SwapChain = null;
262 }

Property Documentation

◆ DepthStencilState

DepthStencilState BioImager.DDX11.DepthStencilState
getset

Definition at line 19 of file DDX11.cs.

19{ get; set; }

◆ Device

SharpDX.Direct3D11.Device BioImager.DDX11.Device
get

Definition at line 15 of file DDX11.cs.

15{ get; private set; }

◆ DeviceContext

DeviceContext BioImager.DDX11.DeviceContext
get

Definition at line 16 of file DDX11.cs.

16{ get; private set; }

◆ ProjectionMatrix

Matrix BioImager.DDX11.ProjectionMatrix
get

Definition at line 22 of file DDX11.cs.

22{ get; private set; }

◆ VideoCardDescription

string BioImager.DDX11.VideoCardDescription
get

Definition at line 13 of file DDX11.cs.

13{ get; private set; }

◆ VideoCardMemory

int BioImager.DDX11.VideoCardMemory
get

Definition at line 12 of file DDX11.cs.

12{ get; private set; }

◆ WorldMatrix

Matrix BioImager.DDX11.WorldMatrix
getset

Definition at line 23 of file DDX11.cs.

23{ get; set; }

The documentation for this class was generated from the following file: