本文整理汇总了C#中RenderTargetView.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# RenderTargetView.Dispose方法的具体用法?C# RenderTargetView.Dispose怎么用?C# RenderTargetView.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RenderTargetView
的用法示例。
在下文中一共展示了RenderTargetView.Dispose方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
var form = new RenderForm("SlimDX - MiniTri Direct3D 10 Sample");
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};
Device device;
SwapChain swapChain;
Device.CreateWithSwapChain(null, DriverType.Hardware, DeviceCreationFlags.Debug, desc, out device, out swapChain);
//Stops Alt+enter from causing fullscreen skrewiness.
device.Factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderView = new RenderTargetView(device, backBuffer);
var effect = Effect.FromFile(device, "MiniTri.fx", "fx_4_0");
var technique = effect.GetTechniqueByIndex(0);
var pass = technique.GetPassByIndex(0);
var layout = new InputLayout(device, pass.Description.Signature, new[] {
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
});
var stream = new DataStream(3 * 32, true, true);
stream.WriteRange(new[] {
new Vector4(0.0f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)
});
stream.Position = 0;
var vertices = new SlimDX.Direct3D10.Buffer(device, stream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 3 * 32,
Usage = ResourceUsage.Default
});
stream.Dispose();
device.OutputMerger.SetTargets(renderView);
device.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
MessagePump.Run(form, () =>
{
device.ClearRenderTargetView(renderView, Color.Black);
device.InputAssembler.SetInputLayout(layout);
device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
device.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 32, 0));
for (int i = 0; i < technique.Description.PassCount; ++i)
{
pass.Apply();
device.Draw(3, 0);
}
swapChain.Present(0, PresentFlags.None);
});
vertices.Dispose();
layout.Dispose();
effect.Dispose();
renderView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
//foreach (var item in ObjectTable.Objects)
// item.Dispose();
}
示例2: Main
//.........这里部分代码省略.........
device.Factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);
Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderView = new RenderTargetView(device, backBuffer);
var bytecode = ShaderBytecode.CompileFromFile("Render.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
var effect = new Effect(device, bytecode);
var technique = effect.GetTechniqueByIndex(0);
var pass = technique.GetPassByIndex(0);
String errors;
var computeByteCode = ShaderBytecode.CompileFromFile("compute.fx", "CS", "cs_5_0", ShaderFlags.None, EffectFlags.None, null, null, out errors);
var compute = new ComputeShader(device, computeByteCode);
// shader variable handles
var conwayResourceH = effect.GetVariableByName("tex").AsResource();
var resolutionInvH = effect.GetVariableByName("resolutionInv").AsVector();
resolutionInvH.Set(new Vector2(1.0f / form.ClientSize.Width, 1.0f / form.ClientSize.Height));
EffectVectorVariable lightPosSSH = effect.GetVariableByName("lightPosSS").AsVector();
// create texture, fill it with random data
Texture2DDescription textureDesc = new Texture2DDescription()
{
Width = form.ClientSize.Width,
Height = form.ClientSize.Height,
MipLevels = 1,
ArraySize = 1,
CpuAccessFlags = CpuAccessFlags.None,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
OptionFlags = ResourceOptionFlags.None,
BindFlags = BindFlags.UnorderedAccess | BindFlags.ShaderResource,
Format = Format.R32_Float
};
var random = new Random();
var data = new float[form.ClientSize.Width * form.ClientSize.Height];
for (int i = 0; i < form.ClientSize.Width; ++i)
{
for (int j = 0; j < form.ClientSize.Height; ++j)
data[i * form.ClientSize.Height + j] = (float)random.Next(2);
}
DataStream ds = new DataStream(data, true, false);
DataRectangle dataRect = new DataRectangle(4 * form.ClientSize.Width, ds);
Texture2D conwayTex = new Texture2D(device, textureDesc, dataRect);
// Create SRV and UAV over the same texture
UnorderedAccessView conwayUAV = new UnorderedAccessView(device, conwayTex);
ShaderResourceView conwaySRV = new ShaderResourceView(device, conwayTex);
// On the more typical setup where you switch shaders,
// you will have to set the texture after every
conwayResourceH.SetResource(conwaySRV);
device.ImmediateContext.OutputMerger.SetTargets(renderView);
device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));
device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
Vector2 lightPosSS;
float angle = 0;
MessagePump.Run(form, () =>
{
// this does the light rotation
angle += 0.002f;
lightPosSS = new Vector2((float)Math.Sin(angle) * 0.5f + 0.5f, (float)Math.Cos(angle) * 0.5f + 0.5f);
lightPosSSH.Set(lightPosSS);
device.ImmediateContext.ComputeShader.Set(compute);
device.ImmediateContext.ComputeShader.SetUnorderedAccessView(conwayUAV, 0);
device.ImmediateContext.Dispatch(form.ClientSize.Width / 16 + 1, form.ClientSize.Height / 16 + 1, 1);
// After running the CS you have to unset UAV from the shader, so you can use it as SRV
device.ImmediateContext.ComputeShader.SetUnorderedAccessView(null, 0);
device.ImmediateContext.ClearRenderTargetView(renderView, Color.Black);
for (int i = 0; i < technique.Description.PassCount; ++i)
{
pass.Apply(device.ImmediateContext);
// No vertices are send as they are created in the vertex shader on the fly.
device.ImmediateContext.Draw(4, 0);
}
swapChain.Present(0, PresentFlags.None);
});
computeByteCode.Dispose();
conwayUAV.Dispose();
conwaySRV.Dispose();
conwayTex.Dispose();
ds.Dispose();
bytecode.Dispose();
effect.Dispose();
renderView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
}
示例3: Main
//.........这里部分代码省略.........
// Load effect from file. It is a basic effect that renders a full screen quad through
// an ortho projectio=n matrix
effect = Effect.FromFile(device, "Texture.fx", "fx_4_0", ShaderFlags.Debug, EffectFlags.None);
EffectTechnique technique = effect.GetTechniqueByIndex(0);
EffectPass pass = technique.GetPassByIndex(0);
InputLayout layout = new InputLayout(device, pass.Description.Signature, new[]
{
new InputElement(
"POSITION", 0,
Format.
R32G32B32A32_Float,
0, 0),
new InputElement(
"TEXCOORD", 0,
Format.
R32G32_Float,
16, 0)
});
//effect.GetVariableByName("mWorld").AsMatrix().SetMatrix(
// Matrix.Translation(Layout.OrthographicTransform(Vector2.Zero, 99, size)));
//effect.GetVariableByName("mView").AsMatrix().SetMatrix(qCam.View);
//effect.GetVariableByName("mProjection").AsMatrix().SetMatrix(qCam.OrthoProjection);
//effect.GetVariableByName("tDiffuse").AsResource().SetResource(srv);
// Set RT and Viewports
device.OutputMerger.SetTargets(renderView);
device.Rasterizer.SetViewports(new Viewport(0, 0, size.Width, size.Height, 0.0f, 1.0f));
// Create solid rasterizer state
RasterizerStateDescription rDesc = new RasterizerStateDescription()
{
CullMode = CullMode.None,
IsDepthClipEnabled = true,
FillMode = FillMode.Solid,
IsAntialiasedLineEnabled = false,
IsFrontCounterclockwise = true,
//IsMultisampleEnabled = true,
};
RasterizerState rState = RasterizerState.FromDescription(device, rDesc);
device.Rasterizer.State = rState;
Texture2DDescription rtDesc = new Texture2DDescription
{
ArraySize = 1,
Width = size.Width,
Height = size.Height,
BindFlags = BindFlags.RenderTarget,
CpuAccessFlags = CpuAccessFlags.None,
Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm,
OptionFlags = ResourceOptionFlags.None,
Usage = ResourceUsage.Default,
MipLevels = 1,
SampleDescription = new SampleDescription(1, 0)
};
rtTex = new Texture2D(device, rtDesc);
rv = new RenderTargetView(device, rtTex);
stereoizedTexture = Make3D(sourceTexture);
//ResizeDevice(new Size(1920, 1080), true);
Console.WriteLine(form.ClientSize);
// Main Loop
MessagePump.Run(form, () =>
{
device.ClearRenderTargetView(renderView, Color.Cyan);
//device.InputAssembler.SetInputLayout(layout);
//device.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleList);
//device.OutputMerger.SetTargets(rv);
//device.InputAssembler.SetVertexBuffers(0,
// new VertexBufferBinding(vertices, 24, 0));
//device.InputAssembler.SetIndexBuffer(indices, Format.R16_UInt, 0);
//for (int i = 0; i < technique.Description.PassCount; ++i)
//{
// // Render the full screen quad
// pass.Apply();
// device.DrawIndexed(6, 0, 0);
//}
ResourceRegion stereoSrcBox = new ResourceRegion { Front = 0, Back = 1, Top = 0, Bottom = size.Height, Left = 0, Right = size.Width };
device.CopySubresourceRegion(stereoizedTexture, 0, stereoSrcBox, backBuffer, 0, 0, 0, 0);
//device.CopyResource(rv.Resource, backBuffer);
swapChain.Present(0, PresentFlags.None);
});
// Dispose resources
vertices.Dispose();
layout.Dispose();
effect.Dispose();
renderView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
rState.Dispose();
stereoizedTexture.Dispose();
sourceTexture.Dispose();
indices.Dispose();
srv.Dispose();
}
示例4: Main
static void Main()
{
Device device;
SwapChain swapChain;
var form = new RenderForm("Test Game");
var description = new SwapChainDescription()
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
// create a view of our render target, which is the backbuffer of the swap chain we just created
RenderTargetView renderTarget;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
// setting a viewport is required if you want to actually see anything
var context = device.ImmediateContext;
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);
// handle alt+enter ourselves
form.KeyDown += (o, e) =>
{
if (e.Alt && e.KeyCode == Keys.Enter)
swapChain.IsFullScreen = !swapChain.IsFullScreen;
};
// handle form size changes
form.UserResized += (o, e) =>
{
renderTarget.Dispose();
swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
context.OutputMerger.SetTargets(renderTarget);
};
// Create a renderobject
var renderObject = new RenderObject(device);
// Create some gameobjects to populate world
var gameObjects = new List<GameObject>();
gameObjects.Add(
new GameObject() { RenderObj = renderObject }
);
var angle = 0.0f;
MessagePump.Run(form, () =>
{
// clear the render target to a soothing blue
context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));
renderObject.cb.vp = Matrix.OrthoLH(viewport.Width, viewport.Height, 0, 1);
// Render each gameobject
foreach (var obj in gameObjects)
{
renderObject.cb.world = Matrix.Translation(new Vector3(
(float)Math.Cos(angle) * 100.0f,
(float)Math.Sin(angle) * 100.0f,
0));
obj.RenderObj.Render(context);
}
angle += 0.0001f;
swapChain.Present(0, PresentFlags.None);
});
// clean up all resources
// anything we missed will show up in the debug output
renderObject.Dispose();
renderTarget.Dispose();
swapChain.Dispose();
device.Dispose();
}
示例5: Main
static void Main()
{
var form = new RenderForm("DotRocket/SlimDX example");
var description = new SwapChainDescription()
{
BufferCount = 1,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
// Setup rendering
Device device;
SwapChain swapChain;
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
RenderTargetView renderTarget;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
var context = device.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
// Prevent alt+enter (broken on WinForms)
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);
// Setup audio-streaming
XAudio2 xaudio2 = new XAudio2();
stream = new XWMAStream("tune.xwma");
MasteringVoice masteringVoice = new MasteringVoice(xaudio2);
sourceVoice = new SourceVoice(xaudio2, stream.Format);
audioBuffer = new AudioBuffer();
audioBuffer.AudioData = stream;
audioBuffer.AudioBytes = (int)stream.Length;
audioBuffer.Flags = BufferFlags.EndOfStream;
sourceVoice.SubmitSourceBuffer(audioBuffer, stream.DecodedPacketsInfo);
sourceVoice.Start();
// Setup DotRocket
#if DEBUG
DotRocket.Device rocket = new DotRocket.ClientDevice("sync");
rocket.OnPause += Pause;
rocket.OnSetRow += SetRow;
rocket.OnIsPlaying += IsPlaying;
rocket.Connect("localhost", 1338);
#else
DotRocket.Device rocket = new DotRocket.PlayerDevice("sync");
#endif
// Get our belowed tracks!
DotRocket.Track clear_r = rocket.GetTrack("clear.r");
DotRocket.Track clear_g = rocket.GetTrack("clear.g");
DotRocket.Track clear_b = rocket.GetTrack("clear.b");
MessagePump.Run(form, () =>
{
// Hammertime.
double row = ((double)(sourceVoice.State.SamplesPlayed - samplesBias) / stream.Format.SamplesPerSecond) * rowRate;
// Paint some stuff.
rocket.Update((int)System.Math.Floor(row));
context.ClearRenderTargetView(renderTarget, new Color4(
clear_r.GetValue(row),
clear_g.GetValue(row),
clear_b.GetValue(row)));
swapChain.Present(0, PresentFlags.None);
});
// clean up all resources
// anything we missed will show up in the debug output
renderTarget.Dispose();
swapChain.Dispose();
device.Dispose();
}
示例6: Renderer
public Renderer(MainForm form, Control target)
{
t = new Stopwatch();
mainForm = form;
targetControl = target;
form.Renderer = this;
description = new SwapChainDescription()
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = target.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out device, out swapChain);
// create a view of our render target, which is the backbuffer of the swap chain we just created
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
// setting a viewport is required if you want to actually see anything
context = device.ImmediateContext;
viewport = new Viewport(0.0f, 0.0f, target.ClientSize.Width, target.ClientSize.Height);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
CreateShaders(File.ReadAllText("vteffect.fx"));
// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(target.Handle, WindowAssociationFlags.IgnoreAltEnter);
// handle alt+enter ourselves
form.KeyDown += (o, e) =>
{
//if (e.Alt && e.KeyCode == Keys.Enter)
//swapChain.IsFullScreen = !swapChain.IsFullScreen;
};
// handle form size changes
form.Resize += (o, e) =>
{
renderTarget.Dispose();
viewport = new Viewport(0.0f, 0.0f, target.ClientSize.Width, target.ClientSize.Height);
swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
SetTexture(currentEntry);
};
MessagePump.Run(form, Render);
// clean up all resources
// anything we missed will show up in the debug output
vertices.Close();
vertexBuffer.Dispose();
layout.Dispose();
inputSignature.Dispose();
vertexShader.Dispose();
pixelShader.Dispose();
renderTarget.Dispose();
swapChain.Dispose();
device.Dispose();
UpdateRenderer = true;
}
示例7: Main
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form1();
Device device;
SwapChain swapChain;
ShaderSignature inputSignature;
VertexShader vertexShader;
GeometryShader geometryShader;
PixelShader pixelShader;
var description = new SwapChainDescription()
{
BufferCount = 2,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, description, out device, out swapChain);
// create a view of our render target, which is the backbuffer of the swap chain we just created
RenderTargetView renderTarget;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
// setting a viewport is required if you want to actually see anything
var context = device.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
// load and compile the vertex shader
using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "VS", "vs_4_0", ShaderFlags.None, EffectFlags.None))
{
inputSignature = ShaderSignature.GetInputSignature(bytecode);
vertexShader = new VertexShader(device, bytecode);
}
using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "GS", "gs_4_0", ShaderFlags.None, EffectFlags.None))
{
geometryShader = new GeometryShader(device, bytecode);
}
// load and compile the pixel shader
using (var bytecode = ShaderBytecode.CompileFromFile("Voxel.fx", "PS", "ps_4_0", ShaderFlags.None, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);
// create test vertex data, making sure to rewind the stream afterward
var vertices = new DataStream(12 * 3, true, true);
vertices.Write(new Vector3(0.0f, 0.5f, 0.5f));
vertices.Write(new Vector3(0.5f, -0.5f, 0.5f));
vertices.Write(new Vector3(-0.5f, -0.5f, 0.5f));
vertices.Position = 0;
// create the vertex layout and buffer
var elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
var layout = new InputLayout(device, inputSignature, elements);
var vertexBuffer = new Buffer(device, vertices, 12 * 3, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// configure the Input Assembler portion of the pipeline with the vertex data
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 12, 0));
// set the shaders
context.VertexShader.Set(vertexShader);
// context.GeometryShader.Set(geometryShader);
context.PixelShader.Set(pixelShader);
// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);
// handle alt+enter ourselves
form.KeyDown += (o, e) =>
{
if (e.Alt && e.KeyCode == Keys.Enter)
swapChain.IsFullScreen = !swapChain.IsFullScreen;
};
// handle form size changes
form.Resize += (o, e) =>
{
renderTarget.Dispose();
swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
context.OutputMerger.SetTargets(renderTarget);
};
MessagePump.Run(form, () =>
//.........这里部分代码省略.........
示例8: init
public void init()
{
form = new RenderForm("My HelloWorld SlimDX app");
var description = new SwapChainDescription()
{
BufferCount = 1,
Usage = Usage.RenderTargetOutput,
OutputHandle = form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
SampleDescription = new SampleDescription(1, 0),
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, description, out device, out swapChain);
// create a view of our render target, which is the backbuffer of the swap chain we just created
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
// setting a viewport is required if you want to actually see anything
context = device.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
context.OutputMerger.SetTargets(renderTarget);
context.Rasterizer.SetViewports(viewport);
// load and compile the vertex shader
using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
{
inputSignature = ShaderSignature.GetInputSignature(bytecode);
vertexShader = new VertexShader(device, bytecode);
}
// load and compile the pixel shader
using (var bytecode = ShaderBytecode.CompileFromFile("triangle.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);
elements = new[] { new InputElement("POSITION", 0, Format.R32G32B32_Float, 0) };
layout = new InputLayout(device, inputSignature, elements);
// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAltEnter);
// handle alt+enter ourselves
form.KeyDown += (o, e) =>
{
if (e.Alt && e.KeyCode == System.Windows.Forms.Keys.Enter)
swapChain.IsFullScreen = !swapChain.IsFullScreen;
};
// handle form size changes
form.UserResized += (o, e) =>
{
renderTarget.Dispose();
swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
context.OutputMerger.SetTargets(renderTarget);
};
}
示例9: Initialize
public void Initialize()
{
var description = new SwapChainDescription
{
BufferCount = 1,
Usage = Usage.RenderTargetOutput,
OutputHandle = _form.Handle,
IsWindowed = true,
ModeDescription = new ModeDescription
{
Width = 0,
Height = 0,
Format = Format.R8G8B8A8_UNorm,
RefreshRate = new Rational(60, 1),
Scaling = DisplayModeScaling.Unspecified,
ScanlineOrdering = DisplayModeScanlineOrdering.Unspecified
},
SampleDescription = new SampleDescription
{
Count = 1,
Quality = 0
},
Flags = SwapChainFlags.AllowModeSwitch,
SwapEffect = SwapEffect.Discard
};
// Create device
SlimDX.Direct3D10_1.Device1.CreateWithSwapChain(null, DriverType.Hardware, DeviceCreationFlags.None,
description, out _device, out _swapChain);
using (var resource = Resource.FromSwapChain<Texture2D>(_swapChain, 0))
{
_renderTargetView = new RenderTargetView(_device, resource);
}
InitializeDepthBuffer();
InitializeMeshes();
InitializeWritableTexture();
InitializeShaders("passthrough.fx");
_viewport = new Viewport(0, 0, _form.ClientSize.Width, _form.ClientSize.Height, 0.0f, 1.0f);
_device.Rasterizer.SetViewports(_viewport);
_device.OutputMerger.SetTargets(_depthStencilView, _renderTargetView);
_rasterizerState = RasterizerState.FromDescription(_device, new RasterizerStateDescription
{
IsDepthClipEnabled = false,
FillMode = FillMode.Solid,
CullMode = CullMode.None
});
_device.Rasterizer.State = _rasterizerState;
_form.UserResized += (sender, e) =>
{
_renderTargetView.Dispose();
_swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm,
SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(_swapChain, 0))
{
_renderTargetView = new RenderTargetView(_device, resource);
}
InitializeDepthBuffer();
InitializeWritableTexture();
_device.OutputMerger.SetTargets(_depthStencilView, _renderTargetView);
};
}
示例10: HoverForm
//.........这里部分代码省略.........
BindFlags = BindFlags.DepthStencil,
CpuAccessFlags = CpuAccessFlags.None,
Format = Format.D32_Float,
Height = this.ClientSize.Height,
Width = this.ClientSize.Width,
MipLevels = 1,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default
};
using (var depthBuffer = new Texture2D(device, depthBufferDescription))
{
depthStencilView = new DepthStencilView(device, depthBuffer);
depthStencilState = DepthStencilState.FromDescription(device, new DepthStencilStateDescription {
IsDepthEnabled = true,
IsStencilEnabled = false,
DepthWriteMask = DepthWriteMask.All,
DepthComparison = Comparison.LessEqual
});
}
// Setup wireframe mode
rasteriserState = RasterizerState.FromDescription(device, new RasterizerStateDescription
{
CullMode = SlimDX.Direct3D11.CullMode.None,
FillMode = SlimDX.Direct3D11.FillMode.Wireframe
});
// setting a viewport is required if you want to actually see anything
context = device.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, this.ClientSize.Width, this.ClientSize.Height);
context.OutputMerger.SetTargets(depthStencilView, renderTarget);
context.OutputMerger.DepthStencilState = depthStencilState;
context.Rasterizer.State = rasteriserState;
context.Rasterizer.SetViewports(viewport);
// load and compile the vertex shader
using (var bytecode = ShaderBytecode.CompileFromFile("shader.fx", "VShader", "vs_4_0", ShaderFlags.Debug, EffectFlags.None))
{
inputSignature = ShaderSignature.GetInputSignature(bytecode);
vertexShader = new VertexShader(device, bytecode);
}
// load and compile the pixel shader
using (var bytecode = ShaderBytecode.CompileFromFile("shader.fx", "PShader", "ps_4_0", ShaderFlags.Debug, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);
// create test vertex data, making sure to rewind the stream afterward
vertices = CreateTriangleListFromMaze(maze);
camera.Position = FindHumanStartPosition(maze);
// create the vertex layout and buffer
var elements = new[] {
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0),
new InputElement("COLOR", 0, Format.R32G32B32_Float, 0)
};
layout = new InputLayout(device, inputSignature, elements);
vertexBuffer = new Buffer(device, vertices, (int)vertices.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
// configure the Input Assembler portion of the pipeline with the vertex data
context.InputAssembler.InputLayout = layout;
context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, 24, 0));
// set the shaders
context.VertexShader.Set(vertexShader);
context.PixelShader.Set(pixelShader);
// crate the constant buffer
constantBuffer = new Buffer(device, new BufferDescription
{
Usage = ResourceUsage.Default,
SizeInBytes = Marshal.SizeOf(typeof(ConstantBuffer)),
BindFlags = BindFlags.ConstantBuffer
});
// prevent DXGI handling of alt+enter, which doesn't work properly with Winforms
using (var factory = swapChain.GetParent<Factory>())
factory.SetWindowAssociation(this.Handle, WindowAssociationFlags.IgnoreAltEnter);
// handle alt+enter ourselves
this.KeyDown += (o, e) =>
{
if (e.Alt && e.KeyCode == Keys.Enter)
swapChain.IsFullScreen = !swapChain.IsFullScreen;
};
// handle form size changes
this.UserResized += (o, e) =>
{
renderTarget.Dispose();
swapChain.ResizeBuffers(2, 0, 0, Format.R8G8B8A8_UNorm, SwapChainFlags.AllowModeSwitch);
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTarget = new RenderTargetView(device, resource);
context.OutputMerger.SetTargets(renderTarget);
};
this.KeyDown += new KeyEventHandler(HoverForm_KeyDown);
}