本文整理汇总了C#中SwapChain.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# SwapChain.Dispose方法的具体用法?C# SwapChain.Dispose怎么用?C# SwapChain.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SwapChain
的用法示例。
在下文中一共展示了SwapChain.Dispose方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateSwapchain
public static SwapChain3 CreateSwapchain(RenderForm form, CommandQueue queue,Config config)
{
using (var Factory = new Factory4())
{
var swapChainDesc = new SwapChainDescription()
{
BufferCount = config.FrameCount,
ModeDescription = new ModeDescription(config.Width, config.Height, new Rational(config.RefreshRate, 1), config.Format),
Usage = Usage.RenderTargetOutput,
SwapEffect = SwapEffect.FlipDiscard,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(config.SampleCount, config.SampleQuality),
IsWindowed = true
};
var tempSwapChain = new SwapChain(Factory, queue, swapChainDesc);
var SwapChain = tempSwapChain.QueryInterface<SwapChain3>();
tempSwapChain.Dispose();
return SwapChain;
}
}
示例2: LoadPipeline
private void LoadPipeline(RenderForm form)
{
int width = form.ClientSize.Width;
int height = form.ClientSize.Height;
viewport.Width = width;
viewport.Height = height;
viewport.MaxDepth = 1.0f;
scissorRect.Right = width;
scissorRect.Bottom = height;
#if DEBUG
// Enable the D3D12 debug layer.
{
DebugInterface.Get().EnableDebugLayer();
}
#endif
device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0);
using (var factory = new Factory4())
{
// Describe and create the command queue.
CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct);
commandQueue = device.CreateCommandQueue(queueDesc);
// Describe and create the swap chain.
SwapChainDescription swapChainDesc = new SwapChainDescription()
{
BufferCount = FrameCount,
ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
Usage = Usage.RenderTargetOutput,
SwapEffect = SwapEffect.FlipDiscard,
OutputHandle = form.Handle,
//Flags = SwapChainFlags.None,
SampleDescription = new SampleDescription(1, 0),
IsWindowed = true
};
SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
swapChain = tempSwapChain.QueryInterface<SwapChain3>();
tempSwapChain.Dispose();
frameIndex = swapChain.CurrentBackBufferIndex;
}
// Create descriptor heaps.
// Describe and create a render target view (RTV) descriptor heap.
DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription()
{
DescriptorCount = FrameCount,
Flags = DescriptorHeapFlags.None,
Type = DescriptorHeapType.RenderTargetView
};
renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc);
DescriptorHeapDescription srvHeapDesc = new DescriptorHeapDescription()
{
DescriptorCount = 1,
Flags = DescriptorHeapFlags.ShaderVisible,
Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView
};
shaderRenderViewHeap = device.CreateDescriptorHeap(srvHeapDesc);
rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
// Create frame resources.
CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
for (int n = 0; n < FrameCount; n++)
{
renderTargets[n] = swapChain.GetBackBuffer<Resource>(n);
device.CreateRenderTargetView(renderTargets[n], null, rtvHandle);
rtvHandle += rtvDescriptorSize;
}
commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
}
示例3: LoadPipeline
private void LoadPipeline(RenderForm form)
{
int width = form.ClientSize.Width;
int height = form.ClientSize.Height;
viewport.Width = width;
viewport.Height = height;
viewport.MaxDepth = 1.0f;
scissorRect.Right = width;
scissorRect.Bottom = height;
#if DEBUG
// Enable the D3D12 debug layer.
{
DebugInterface.Get().EnableDebugLayer();
}
#endif
device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0);
using (var factory = new Factory4())
{
// Describe and create the command queue.
CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct);
commandQueue = device.CreateCommandQueue(queueDesc);
// Describe and create the swap chain.
SwapChainDescription swapChainDesc = new SwapChainDescription()
{
BufferCount = FrameCount,
ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
Usage = Usage.RenderTargetOutput,
SwapEffect = SwapEffect.FlipDiscard,
OutputHandle = form.Handle,
//Flags = SwapChainFlags.None,
SampleDescription = new SampleDescription(1, 0),
IsWindowed = true
};
SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
swapChain = tempSwapChain.QueryInterface<SwapChain3>();
tempSwapChain.Dispose();
frameIndex = swapChain.CurrentBackBufferIndex;
}
// Create descriptor heaps.
// Describe and create a render target view (RTV) descriptor heap.
DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription()
{
DescriptorCount = FrameCount,
Flags = DescriptorHeapFlags.None,
Type = DescriptorHeapType.RenderTargetView
};
renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc);
rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
// Create frame resources.
CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
for (int n = 0; n < FrameCount; n++)
{
renderTargets[n] = swapChain.GetBackBuffer<Resource>(n);
device.CreateRenderTargetView(renderTargets[n], null, rtvHandle);
rtvHandle += rtvDescriptorSize;
}
//create depth buffer;
DescriptorHeapDescription dsvHeapDesc = new DescriptorHeapDescription()
{
DescriptorCount = FrameCount,
Flags = DescriptorHeapFlags.None,
Type = DescriptorHeapType.DepthStencilView
};
depthStencilViewHeap = device.CreateDescriptorHeap(dsvHeapDesc);
CpuDescriptorHandle dsvHandle = depthStencilViewHeap.CPUDescriptorHandleForHeapStart;
ClearValue depthOptimizedClearValue = new ClearValue()
{
Format = Format.D32_Float,
DepthStencil = new DepthStencilValue() { Depth = 1.0F, Stencil = 0 },
};
depthTarget = device.CreateCommittedResource(
new HeapProperties(HeapType.Default),
HeapFlags.None,
new ResourceDescription(ResourceDimension.Texture2D, 0, width, height, 1, 0, Format.D32_Float, 1, 0, TextureLayout.Unknown, ResourceFlags.AllowDepthStencil),
ResourceStates.DepthWrite, depthOptimizedClearValue);
var depthView = new DepthStencilViewDescription()
{
Format = Format.D32_Float,
Dimension = DepthStencilViewDimension.Texture2D,
Flags = DepthStencilViewFlags.None,
};
//bind depth buffer
device.CreateDepthStencilView(depthTarget, null, dsvHandle);
commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
bundleAllocator = device.CreateCommandAllocator(CommandListType.Bundle);
//.........这里部分代码省略.........
示例4: Main
static void Main(string[] args)
{
// Select a File to play
var openFileDialog = new OpenFileDialog { Title = "Select a file", Filter = "Media Files(*.WMV;*.MP4;*.AVI)|*.WMV;*.MP4;*.AVI" };
var result = openFileDialog.ShowDialog();
if (result == DialogResult.Cancel)
{
return;
}
// Initialize MediaFoundation
MediaManager.Startup();
var renderForm = new SharpDX.Windows.RenderForm();
device = CreateDeviceForVideo(out dxgiManager);
// Creates the MediaEngineClassFactory
var mediaEngineFactory = new MediaEngineClassFactory();
//Assign our dxgi manager, and set format to bgra
MediaEngineAttributes attr = new MediaEngineAttributes();
attr.VideoOutputFormat = (int)SharpDX.DXGI.Format.B8G8R8A8_UNorm;
attr.DxgiManager = dxgiManager;
// Creates MediaEngine for AudioOnly
var mediaEngine = new MediaEngine(mediaEngineFactory, attr, MediaEngineCreateFlags.None);
// Register our PlayBackEvent
mediaEngine.PlaybackEvent += OnPlaybackCallback;
// Query for MediaEngineEx interface
mediaEngineEx = mediaEngine.QueryInterface<MediaEngineEx>();
// Opens the file
var fileStream = openFileDialog.OpenFile();
// Create a ByteStream object from it
var stream = new ByteStream(fileStream);
// Creates an URL to the file
var url = new Uri(openFileDialog.FileName, UriKind.RelativeOrAbsolute);
// Set the source stream
mediaEngineEx.SetSourceFromByteStream(stream, url.AbsoluteUri);
// Wait for MediaEngine to be ready
if (!eventReadyToPlay.WaitOne(1000))
{
Console.WriteLine("Unexpected error: Unable to play this file");
}
//Create our swapchain
swapChain = CreateSwapChain(device, renderForm.Handle);
//Get DXGI surface to be used by our media engine
var texture = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var surface = texture.QueryInterface<SharpDX.DXGI.Surface>();
//Get our video size
int w, h;
mediaEngine.GetNativeVideoSize(out w, out h);
// Play the music
mediaEngineEx.Play();
long ts;
RenderLoop.Run(renderForm, () =>
{
//Transfer frame if a new one is available
if (mediaEngine.OnVideoStreamTick(out ts))
{
mediaEngine.TransferVideoFrame(surface, null, new SharpDX.Rectangle(0, 0, w, h), null);
}
swapChain.Present(1, SharpDX.DXGI.PresentFlags.None);
});
mediaEngine.Shutdown();
swapChain.Dispose();
device.Dispose();
}
示例5: LoadPipeline
private void LoadPipeline(RenderForm form)
{
int width = form.ClientSize.Width;
int height = form.ClientSize.Height;
#if DEBUG
// Enable the D3D12 debug layer.
{
DebugInterface.Get().EnableDebugLayer();
}
#endif
device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_11_0);
using (var factory = new Factory4())
{
// Describe and create the command queue.
CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct);
commandQueue = device.CreateCommandQueue(queueDesc);
// Describe and create the swap chain.
SwapChainDescription swapChainDesc = new SwapChainDescription()
{
BufferCount = FrameCount,
ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
Usage = Usage.RenderTargetOutput,
SwapEffect = SwapEffect.FlipDiscard,
OutputHandle = form.Handle,
//Flags = SwapChainFlags.None,
SampleDescription = new SampleDescription(1, 0),
IsWindowed = true
};
SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
swapChain = tempSwapChain.QueryInterface<SwapChain3>();
tempSwapChain.Dispose();
frameIndex = swapChain.CurrentBackBufferIndex;
}
// Create descriptor heaps.
// Describe and create a render target view (RTV) descriptor heap.
renderTargetViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription()
{
DescriptorCount = FrameCount,
Flags = DescriptorHeapFlags.None,
Type = DescriptorHeapType.RenderTargetView
});
rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
//create depth buffer;
depthStencilViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription()
{
DescriptorCount = FrameCount,
Flags = DescriptorHeapFlags.None,
Type = DescriptorHeapType.DepthStencilView
});
//constant buffer view heap
constantBufferViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription()
{
DescriptorCount = 100,
Type = DescriptorHeapType.ConstantBufferViewShaderResourceViewUnorderedAccessView,
Flags = DescriptorHeapFlags.ShaderVisible
});
//Create targets
CreateTargets(width, height);
//sampler buffer view heap
samplerViewHeap = device.CreateDescriptorHeap(new DescriptorHeapDescription()
{
DescriptorCount = 10,
Type = DescriptorHeapType.Sampler,
Flags = DescriptorHeapFlags.ShaderVisible
});
//bind sampler
device.CreateSampler(new SamplerStateDescription()
{
Filter = Filter.ComparisonMinMagMipLinear,
AddressU = TextureAddressMode.Wrap,
AddressV = TextureAddressMode.Wrap,
AddressW = TextureAddressMode.Wrap,
MinimumLod = float.MinValue,
MaximumLod = float.MaxValue,
MipLodBias = 0,
MaximumAnisotropy = 0,
ComparisonFunction = Comparison.Never
}, samplerViewHeap.CPUDescriptorHandleForHeapStart);
commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
bundleAllocator = device.CreateCommandAllocator(CommandListType.Bundle);
form.UserResized += (sender, e) =>
{
isResizing = true;
};
}
示例6: LoadPipeline
private void LoadPipeline(RenderForm form)
{
int width = form.ClientSize.Width;
int height = form.ClientSize.Height;
viewport.Width = width;
viewport.Height = height;
viewport.MaxDepth = 1.0f;
scissorRect.Right = width;
scissorRect.Bottom = height;
#if DEBUG
// Enable the D3D12 debug layer.
{
DebugInterface.Get().EnableDebugLayer();
}
#endif
device = new Device(null, SharpDX.Direct3D.FeatureLevel.Level_12_0);
using (var factory = new Factory4())
{
// Describe and create the command queue.
CommandQueueDescription queueDesc = new CommandQueueDescription(CommandListType.Direct);
commandQueue = device.CreateCommandQueue(queueDesc);
// Describe and create the swap chain.
SwapChainDescription swapChainDesc = new SwapChainDescription()
{
BufferCount = FrameCount,
ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
Usage = Usage.RenderTargetOutput,
SwapEffect = SwapEffect.FlipDiscard,
OutputHandle = form.Handle,
//Flags = SwapChainFlags.None,
SampleDescription = new SampleDescription(1, 0),
IsWindowed = true
};
SwapChain tempSwapChain = new SwapChain(factory, commandQueue, swapChainDesc);
swapChain = tempSwapChain.QueryInterface<SwapChain3>();
tempSwapChain.Dispose();
frameIndex = swapChain.CurrentBackBufferIndex;
}
// Create descriptor heaps.
// Describe and create a render target view (RTV) descriptor heap.
DescriptorHeapDescription rtvHeapDesc = new DescriptorHeapDescription()
{
DescriptorCount = FrameCount,
Flags = DescriptorHeapFlags.None,
Type = DescriptorHeapType.RenderTargetView
};
renderTargetViewHeap = device.CreateDescriptorHeap(rtvHeapDesc);
rtvDescriptorSize = device.GetDescriptorHandleIncrementSize(DescriptorHeapType.RenderTargetView);
//Init Direct3D11 device from Direct3D12 device
device11 = SharpDX.Direct3D11.Device.CreateFromDirect3D12(device, SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport, null, null, commandQueue);
deviceContext11 = device11.ImmediateContext;
device11on12 = device11.QueryInterface<SharpDX.Direct3D11.ID3D11On12Device>();
var d2dFactory = new SharpDX.Direct2D1.Factory(SharpDX.Direct2D1.FactoryType.MultiThreaded);
// Create frame resources.
CpuDescriptorHandle rtvHandle = renderTargetViewHeap.CPUDescriptorHandleForHeapStart;
for (int n = 0; n < FrameCount; n++)
{
renderTargets[n] = swapChain.GetBackBuffer<Resource>(n);
device.CreateRenderTargetView(renderTargets[n], null, rtvHandle);
rtvHandle += rtvDescriptorSize;
//init Direct2D surfaces
SharpDX.Direct3D11.D3D11ResourceFlags format = new SharpDX.Direct3D11.D3D11ResourceFlags()
{
BindFlags = (int)SharpDX.Direct3D11.BindFlags.RenderTarget,
CPUAccessFlags = (int)SharpDX.Direct3D11.CpuAccessFlags.None
};
device11on12.CreateWrappedResource(
renderTargets[n], format,
(int)ResourceStates.Present,
(int)ResourceStates.RenderTarget,
typeof(SharpDX.Direct3D11.Resource).GUID,
out wrappedBackBuffers[n]);
//Init direct2D surface
var d2dSurface = wrappedBackBuffers[n].QueryInterface<Surface>();
direct2DRenderTarget[n] = new SharpDX.Direct2D1.RenderTarget(d2dFactory, d2dSurface, new SharpDX.Direct2D1.RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied)));
d2dSurface.Dispose();
}
commandAllocator = device.CreateCommandAllocator(CommandListType.Direct);
d2dFactory.Dispose();
//Init font
var directWriteFactory = new SharpDX.DirectWrite.Factory();
textFormat = new SharpDX.DirectWrite.TextFormat(directWriteFactory, "Arial", SharpDX.DirectWrite.FontWeight.Bold, SharpDX.DirectWrite.FontStyle.Normal, 48) { TextAlignment = SharpDX.DirectWrite.TextAlignment.Leading, ParagraphAlignment = SharpDX.DirectWrite.ParagraphAlignment.Near };
textBrush = new SharpDX.Direct2D1.SolidColorBrush(direct2DRenderTarget[0], Color.White);
//.........这里部分代码省略.........
示例7: InitializeOculus
//.........这里部分代码省略.........
int textureIndex = eyeTexture.SwapTextureSet.CurrentIndex++;
immediateContext.OutputMerger.SetRenderTargets(eyeTexture.DepthStencilView, eyeTexture.RenderTargetViews[textureIndex]);
immediateContext.ClearRenderTargetView(eyeTexture.RenderTargetViews[textureIndex], Color.Black);
immediateContext.ClearDepthStencilView(eyeTexture.DepthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);
immediateContext.Rasterizer.SetViewport(eyeTexture.Viewport);
//added a custom rasterizer
immediateContext.Rasterizer.State = rasterizerState;
// Retrieve the eye rotation quaternion and use it to calculate the LookAt direction and the LookUp direction.
Quaternion rotationQuaternion = SharpDXHelpers.ToQuaternion(eyePoses[eyeIndex].Orientation);
Matrix rotationMatrix = Matrix.RotationQuaternion(rotationQuaternion);
Vector3 lookUp = Vector3.Transform(new Vector3(0, -1, 0), rotationMatrix).ToVector3();
Vector3 lookAt = Vector3.Transform(new Vector3(0, 0, 1), rotationMatrix).ToVector3();
Vector3 viewPosition = position - eyePoses[eyeIndex].Position.ToVector3();
//use this to get the first rotation to goal
Matrix world = Matrix.Scaling(1.0f) /** Matrix.RotationX(timeSinceStart*0.2f) */* Matrix.RotationY(timeSinceStart * 2 / 10f) /** Matrix.RotationZ(timeSinceStart*3/10f)*/;
Matrix viewMatrix = Matrix.LookAtRH(viewPosition, viewPosition + lookAt, lookUp);
Matrix projectionMatrix = OVR.ovrMatrix4f_Projection(eyeTexture.FieldOfView, 0.1f, 10.0f, OVR.ProjectionModifier.None).ToMatrix();
projectionMatrix.Transpose();
Matrix worldViewProjection = world * viewMatrix * projectionMatrix;
worldViewProjection.Transpose();
// Update the transformation matrix.
immediateContext.UpdateSubresource(ref worldViewProjection, constantBuffer);
// Draw the cube
//immediateContext.Draw(vertices.Length/2, 0);
immediateContext.DrawIndexed(indices.Length, 0, 0);
}
hmd.SubmitFrame(0, layers);
immediateContext.CopyResource(mirrorTextureD3D11, backBuffer);
swapChain.Present(0, PresentFlags.None);
if (newTextureArrived == true)
{
newTextureArrived = false;
DataBox map = device.ImmediateContext.MapSubresource(myTexture, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);
//load the BitMapSource with appropriate formating (Format32bppPRGBA)
SharpDX.WIC.BitmapSource bitMap = LoadBitmap(new SharpDX.WIC.ImagingFactory(), streamTexture);
//string newFile = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + @"\img_merged.jpg";
//SharpDX.WIC.BitmapSource bitMap = LoadBitmapFromFile(new SharpDX.WIC.ImagingFactory(), newFile);
int width = bitMap.Size.Width;
int height = bitMap.Size.Height;
int stride = bitMap.Size.Width * 4;
bitMap.CopyPixels(stride, map.DataPointer, height * stride);
device.ImmediateContext.UnmapSubresource(myTexture, 0);
//bitMap.Dispose();
streamTexture.Seek(0, SeekOrigin.Begin);
}
});
#endregion
// Release all resources
inputLayout.Dispose();
constantBuffer.Dispose();
indexBuffer.Dispose();
vertexBuffer.Dispose();
inputLayout.Dispose();
shaderSignature.Dispose();
pixelShader.Dispose();
pixelShaderByteCode.Dispose();
vertexShader.Dispose();
vertexShaderByteCode.Dispose();
mirrorTextureD3D11.Dispose();
layers.Dispose();
eyeTextures[0].Dispose();
eyeTextures[1].Dispose();
immediateContext.ClearState();
immediateContext.Flush();
immediateContext.Dispose();
depthStencilState.Dispose();
depthStencilView.Dispose();
depthBuffer.Dispose();
backBufferRenderTargetView.Dispose();
backBuffer.Dispose();
swapChain.Dispose();
factory.Dispose();
// Disposing the device, before the hmd, will cause the hmd to fail when disposing.
// Disposing the device, after the hmd, will cause the dispose of the device to fail.
// It looks as if the hmd steals ownership of the device and destroys it, when it's shutting down.
// device.Dispose();
hmd.Dispose();
oculus.Dispose();
}