本文整理汇总了C#中DataStream.Close方法的典型用法代码示例。如果您正苦于以下问题:C# DataStream.Close方法的具体用法?C# DataStream.Close怎么用?C# DataStream.Close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataStream
的用法示例。
在下文中一共展示了DataStream.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Create
public static void Create()
{
//without texcoord
DataStream stream = new DataStream(12 * 6, true, true);
stream.Write<Vector3>(new Vector3(-1, -1, 1));
stream.Write<Vector3>(new Vector3(-1, 1, 1));
stream.Write<Vector3>(new Vector3(1, 1, 1));
stream.Write<Vector3>(new Vector3(1, 1, 1));
stream.Write<Vector3>(new Vector3(1, -1, 1));
stream.Write<Vector3>(new Vector3(-1, -1, 1));
elements[0] = new SlimDX.Direct3D10.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0, 0);
stream.Position = 0;
buffer = new SlimDX.Direct3D10.Buffer(Game.gameClass.GetDevice(), stream, (int)stream.Length, D3D10.ResourceUsage.Immutable,
D3D10.BindFlags.VertexBuffer, D3D10.CpuAccessFlags.None, D3D10.ResourceOptionFlags.None);
binding = new SlimDX.Direct3D10.VertexBufferBinding(buffer, 12, 0);
stream.Close();
//with texcoord
stream = new DataStream(12 * 6*2, true, true);
stream.Write<Vector3>(new Vector3(-1, -1, 1));
stream.Write<Vector3>(Vector3.Zero);
stream.Write<Vector3>(new Vector3(-1, 1, 1));
stream.Write<Vector3>(Vector3.Zero);
stream.Write<Vector3>(new Vector3(1, 1, 1));
stream.Write<Vector3>(Vector3.Zero);
stream.Write<Vector3>(new Vector3(1, 1, 1));
stream.Write<Vector3>(Vector3.Zero);
stream.Write<Vector3>(new Vector3(1, -1, 1));
stream.Write<Vector3>(Vector3.Zero);
stream.Write<Vector3>(new Vector3(-1, -1, 1));
stream.Write<Vector3>(Vector3.Zero);
elementsWithTexCoord[0] = new SlimDX.Direct3D10.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0, 0);
elementsWithTexCoord[1] = new SlimDX.Direct3D10.InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32B32_Float, 12, 0);
stream.Position = 0;
bufferWithTexCoord = new SlimDX.Direct3D10.Buffer(Game.gameClass.GetDevice(), stream, (int)stream.Length, D3D10.ResourceUsage.Dynamic,
D3D10.BindFlags.VertexBuffer, D3D10.CpuAccessFlags.Write, D3D10.ResourceOptionFlags.None);
bindingWithTexCoord = new SlimDX.Direct3D10.VertexBufferBinding(bufferWithTexCoord, 24, 0);
stream.Close();
}
示例2: UpdateRectangles
public void UpdateRectangles( IList< Image4ub > images, IList< Rect2i > targets )
{
#if false
var wrapper = D3D10Wrapper.Instance;
var im = new Image4ub( this.Size, Color4ub.Red );
fixed( void* ptr = im.Pixels )
{
var userBuffer = new IntPtr( ptr );
var dataStream = new DataStream( userBuffer, 4 * im.NumPixels, true, true );
var sourceBox = new DataBox( 4 * im.Width, 4 * im.Width * im.Height, dataStream );
var targetRegion = new ResourceRegion
{
Back = 1,
Front = 0,
Bottom = 4096,
Top = 0,
Left = 0,
Right = 4096
};
wrapper.Device.UpdateSubresource( sourceBox, Texture, 0, targetRegion );
dataStream.Close();
}
return;
#endif
if( images.Count != targets.Count )
{
throw new ArgumentException( "images and targets must be of the same length" );
}
var rect = Texture.Map( 0, MapMode.WriteDiscard, MapFlags.None );
for( int i = 0; i < images.Count; ++i )
{
var im = images[ i ];
var target = targets[ i ];
for( int y = 0; y < im.Height; ++y )
{
int sourceOffset = 4 * y * im.Width;
int sourceCount = 4 * im.Width;
rect.Data.Position = 4 * ( ( y + target.Origin.y ) * Width + target.Origin.x );
rect.Data.Write( im.Pixels, sourceOffset, sourceCount );
}
}
rect.Data.Close();
Texture.Unmap( 0 );
}
示例3: SetRandomTex
private void SetRandomTex(int Seed)
{
// set the seed
_Seed = Seed;
// set the random size
int RandNum_Size = 256;
// set the description of the texture
Texture1DDescription RandomTex_Desc = new Texture1DDescription();
RandomTex_Desc.Format = Format.R8_UInt;
RandomTex_Desc.CpuAccessFlags = CpuAccessFlags.None;
RandomTex_Desc.Width = RandNum_Size * 2;
RandomTex_Desc.Usage = ResourceUsage.Default;
RandomTex_Desc.OptionFlags = ResourceOptionFlags.None;
RandomTex_Desc.MipLevels = 1;
RandomTex_Desc.BindFlags = BindFlags.ShaderResource;
RandomTex_Desc.ArraySize = 1;
// start the stream
DataStream stream = new DataStream(RandNum_Size * 2, true, true);
// Initialize the random generator
Random generator = new Random(Seed);
// allocate the random values
byte[] RandNum_Host = new byte[RandNum_Size * 2];
// Copy the source data twice to the generator array
for (int i = 0; i < RandNum_Size; i++) {
RandNum_Host[i] = (byte)generator.Next(256);
RandNum_Host[i + RandNum_Size] = RandNum_Host[i];
}
// pass the randoms to the stream
stream.WriteRange<byte>(RandNum_Host);
stream.Position = 0;
// create the texture and pass the data
Texture1D RandomTex = new Texture1D(Engine.g_device, RandomTex_Desc, stream);
// close the stream we don't need it any more
stream.Close();
// create the resource view to be able to pass it to the shader
ShaderResourceView RandomResourceView = new ShaderResourceView(Engine.g_device, RandomTex);
// set the Resource to the shader
RandomTex_Variable.SetResource(RandomResourceView);
}
示例4: PointCloud
public PointCloud(List<FxMaths.Vector.FxVector3f> Points, List<FxMaths.Vector.FxVector3f> Colors)
{
Device dev = Engine.g_device;
/// make a stream with the vertex to be able to insert it to the mesh
DataStream stream = new DataStream(Marshal.SizeOf(typeof(PointCloudParticle)) * Points.Count, true, true);
/// Init the shader
m_shader = new Shaders.ShaderPointCloud();
m_numParticles = Points.Count;
m_shader.SetTexture("c://Particle.png");
/// write the particles to the stream
for (int i = 0; i < m_numParticles; i++)
{
Points[i].WriteToDataStream(stream);
Colors[i].WriteToDataStream(stream);
}
/// reset the position in the stream
stream.Position = 0;
/// Fill the buffer with the vertices
m_BufferParticles = ComputeShader.CreateBuffer(m_numParticles, ComputeShader.SizeOfFloat3 * 2, AccessViewType.SRV, stream);
m_BufferParticles.DebugName = "ParticleBuffer";
m_srvBufferParticles = FXResourceVariable.InitSRVResource(dev, m_BufferParticles);
m_rvBufferParticles = m_shader.m_effect.GetResourceByName("particleBuffer");
m_rvBufferParticles.SetResource(m_srvBufferParticles);
// set the world matrix
m_WorldMatrix = Matrix.Scaling(_scale) * Matrix.RotationYawPitchRoll(_rotation.Y, _rotation.X, _rotation.Z) * Matrix.Translation(_position);
// close the stream
stream.Close();
}
示例5: RenderObject
public RenderObject(Device device)
{
cb.vp = Matrix.Identity;
cb.world = Matrix.Identity;
// load and compile the vertex shader
using (var bytecode = ShaderBytecode.CompileFromFile("simple.fx", "VShader", "vs_4_0", ShaderFlags.None, EffectFlags.None))
{
vsInputSignature = ShaderSignature.GetInputSignature(bytecode);
vertexShader = new VertexShader(device, bytecode);
}
// load and compile the pixel shader
using (var bytecode = ShaderBytecode.CompileFromFile("simple.fx", "PShader", "ps_4_0", ShaderFlags.None, EffectFlags.None))
pixelShader = new PixelShader(device, bytecode);
// Old school style.
/*
vertexSize = 24;
vertexCount = 3;
var vertexStream = new DataStream(vertexSize * vertexCount, true, true);
vertexStream.Write(new Vector3(0.0f, 5.0f, 0.5f));
vertexStream.Write(new Vector3(1, 0, 0)); // color
vertexStream.Write(new Vector3(5.0f, -5.0f, 0.5f));
vertexStream.Write(new Vector3(0, 1, 0)); // color
vertexStream.Write(new Vector3(-5.0f, -5.0f, 0.5f));
vertexStream.Write(new Vector3(0, 0, 1)); // color
vertexStream.Position = 0;
*/
// Use struct
Vertex[] vertices = new Vertex[] {
new Vertex() { pos = new Vector3(0.0f, 50.0f, 0.5f), col = new Vector3(1, 0, 0), uv = new Vector2(0, 0) },
new Vertex() { pos = new Vector3(50.0f, -50.0f, 0.5f), col = new Vector3(1, 1, 0), uv = new Vector2(1, 0) },
new Vertex() { pos = new Vector3(-50.0f, -50.0f, 0.5f), col = new Vector3(0, 1, 1), uv = new Vector2(1, 1) },
};
vertexSize = Marshal.SizeOf(typeof(Vertex));
vertexCount = vertices.Length;
var vertexStream = new DataStream(vertexSize * vertexCount, true, true);
foreach (var vertex in vertices)
{
vertexStream.Write(vertex);
}
vertexStream.Position = 0;
// 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),
new InputElement("TEXCOORD", 0, Format.R32G32_Float, 0)
};
vertexBufferLayout = new InputLayout(device, vsInputSignature, elements);
vertexBuffer = new Buffer(device, vertexStream, vertexSize * vertexCount, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
vertexStream.Close();
// Setup Constant Buffers
constantBuffer = new Buffer(device, Marshal.SizeOf(typeof(ConstantBuffer)), ResourceUsage.Dynamic, BindFlags.ConstantBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);
// http://asc-chalmers-project.googlecode.com/svn-history/r26/trunk/Source/AdvGraphicsProject/Program.cs
// Try load a texture
SamplerDescription samplerDescription = new SamplerDescription();
samplerDescription.AddressU = TextureAddressMode.Wrap;
samplerDescription.AddressV = TextureAddressMode.Wrap;
samplerDescription.AddressW = TextureAddressMode.Wrap;
samplerDescription.Filter = Filter.MinPointMagMipLinear;
samplerLinear = SamplerState.FromDescription(device, samplerDescription);
texture = Texture2D.FromFile(device, "Data/cco.png");
textureView = new ShaderResourceView(device, texture);
var desc = new BlendStateDescription()
{
AlphaToCoverageEnable = true,
IndependentBlendEnable = true
};
desc.RenderTargets[0].BlendEnable = false;
desc.RenderTargets[0].BlendOperation = BlendOperation.Add;
desc.RenderTargets[0].BlendOperationAlpha = BlendOperation.Add;
desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.Alpha;
desc.RenderTargets[0].SourceBlend = BlendOption.SourceAlpha;
desc.RenderTargets[0].DestinationBlend = BlendOption.InverseSourceAlpha;
desc.RenderTargets[0].DestinationBlendAlpha = BlendOption.InverseSourceAlpha;
desc.RenderTargets[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
blendState = BlendState.FromDescription(device, desc);
}
示例6: CreateMesh
/// <summary>
/// Create the mesh and stream it to the device
/// </summary>
public void CreateMesh()
{
Device dev = Engine.g_device;
/// make a stream with the vertex to be able to insert it to the mesh
DataStream stream = new DataStream(Marshal.SizeOf(typeof(Polygon)) * m_polygons.Count * 3, true, true);
/// write the polygons to the stream
stream.WriteRange<Polygon>(m_polygons.ToArray());
/// reset the position in the stream
stream.Position = 0;
/// Fill the buffer with the vertices
m_BufferVertices = new SharpDX.Direct3D11.Buffer(dev, stream, new BufferDescription() {
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = (int)stream.Length,
Usage = ResourceUsage.Default
});
// create the binder for the vertex
m_VertexBufferBinding = new VertexBufferBinding(m_BufferVertices, 56 /* the size of the Vertex */ , 0);
// close the stream
stream.Close();
/*
/// make a stream with the Indices to be able to insert it to the mesh
stream = new DataStream(sizeof(uint) * m_polygons.Count * 3, true, true);
for (uint i = 0; i < m_polygons.Count * 3; i++)
stream.Write<uint>(i);
/// reset the position in the stream
stream.Position = 0;
m_BufferIndices = new SharpDX.Direct3D11.Buffer(dev, stream, new BufferDescription() {
BindFlags = BindFlags.IndexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = (int)stream.Length,
Usage = ResourceUsage.Default
});
// close the stream
stream.Close();
*/
// create the input layout
m_input_layout = new InputLayout(
Engine.g_device,
m_shader.m_VertexShaderByteCode,
m_layout);
}
示例7: CreateScreenQuad
public static Buffer CreateScreenQuad(Device device)
{
var overlayVertices = new DataStream(VertexPositionTexture.SizeInBytes * 4, true, true);
overlayVertices.Write(new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0f)));
overlayVertices.Write(new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)));
overlayVertices.Write(new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)));
overlayVertices.Write(new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)));
// reset the vertex stream
overlayVertices.Position = 0;
// create the overlay vertex layout and buffer
var vertexBufferOverlay = new Buffer(device, overlayVertices, (int)overlayVertices.Length, ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
overlayVertices.Close();
return vertexBufferOverlay;
//Buffer vertexBuffer;
//// Top Left of screen is -1, +1
//// Bottom Right of screen is +1, -1
//var overlayVertices = new DataStream(VertexPositionTexture.SizeInBytes * 4, true, true);
//overlayVertices.Write(new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0f)));
//overlayVertices.Write(new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)));
//overlayVertices.Write(new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)));
//overlayVertices.Write(new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)));
//// reset the vertex stream
//overlayVertices.Position = 0;
//var vertexBufferDesc = new BufferDescription
//{
// //SizeInBytes = sizeof(float) * 5 * 4,
// SizeInBytes = (int)overlayVertices.Length,
// Usage = ResourceUsage.Default,
// BindFlags = BindFlags.VertexBuffer,
//};
//// create the overlay vertex layout and buffer
//vertexBuffer = new Buffer(device, overlayVertices, vertexBufferDesc);
//overlayVertices.Close();
//using (var data = new DataStream(vertexBufferDesc.SizeInBytes, false, true))
//{
// data.Write(new VertexPositionTexture(new Vector3(-1, 1, 0), new Vector2(0, 0f)));
// data.Write(new VertexPositionTexture(new Vector3(1, 1, 0), new Vector2(1, 0)));
// data.Write(new VertexPositionTexture(new Vector3(-1, -1, 0), new Vector2(0, 1)));
// data.Write(new VertexPositionTexture(new Vector3(1, -1, 0), new Vector2(1, 1)));
// // reset the vertex stream
// //data.Position = 0;
// //data.Write(new Vector3(0.5f, 0.5f, 0));
// //data.Write(new Vector2(1, 0));
// //data.Write(new Vector3(0.5f, -0.5f, 0));
// //data.Write(new Vector2(1, 1));
// //data.Write(new Vector3(-0.5f, 0.5f, 0));
// //data.Write(new Vector2(0, 0));
// //data.Write(new Vector3(-0.5f, -0.5f, 0));
// //data.Write(new Vector2(0, 1));
// vertexBuffer = new Buffer(device, data, vertexBufferDesc);
// //device.ImmediateContext.UnmapSubresource(vertexBuffer, 0);
//}
//return vertexBuffer;
}
示例8: CreateVertexBuffer
private void CreateVertexBuffer()
{
//create vertex buffer
int size = 8 * quadSize * quadSize * 6; //end size of each quad will be quadSize+1
DataStream stream = new DataStream(size, true, true);
for (int i = 0; i < quadSize; i++)
for (int j = 0; j < quadSize; j++)
{
stream.Write(new Vector2(i, j));
stream.Write(new Vector2(i, j + 1));
stream.Write(new Vector2(i + 1, j + 1));
stream.Write(new Vector2(i + 1, j + 1));
stream.Write(new Vector2(i + 1, j));
stream.Write(new Vector2(i, j));
}
stream.Position = 0;
vertexBuffer = new SlimDX.Direct3D10.Buffer(Game.gameClass.GetDevice(), stream, size, SlimDX.Direct3D10.ResourceUsage.Immutable,
SlimDX.Direct3D10.BindFlags.VertexBuffer, SlimDX.Direct3D10.CpuAccessFlags.None, SlimDX.Direct3D10.ResourceOptionFlags.None);
stream.Close();
instances = new Vector4[(Globals.mapSize / quadSize) * (Globals.mapSize / quadSize)];
vertexBufferBinding = new SlimDX.Direct3D10.VertexBufferBinding(vertexBuffer, sizeof(float) * 2, 0);
}
示例9: CreateBuffer
private Buffer CreateBuffer(Device device)
{
Vertex[] vertices = CreateVertices();
var stream = new DataStream(vertices.Length * Marshal.SizeOf(typeof(Vertex)), true, true);
foreach (var vertex in vertices)
{
stream.Write(vertex.Position);
stream.Write(vertex.Color);
stream.Write(vertex.Normal);
}
// Important: when specifying initial buffer data like this, the buffer will
// read from the current DataStream position; we must rewind the stream to
// the start of the data we just wrote.
stream.Position = 0;
var bufferDescription = new BufferDescription
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = (3 * Marshal.SizeOf(typeof(Vertex))),
Usage = ResourceUsage.Default
};
var buffer = new Buffer(device, stream, bufferDescription);
stream.Close();
return buffer;
}
示例10: Run
public void Run()
{
var form = new RenderForm("2d and 3d combined...it's like magic");
form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) form.Close(); };
// DirectX DXGI 1.1 factory
var factory1 = new Factory1();
// The 1st graphics adapter
var adapter1 = factory1.GetAdapter1(0);
// ---------------------------------------------------------------------------------------------
// Setup direct 3d version 11. It's context will be used to combine the two elements
// ---------------------------------------------------------------------------------------------
var description = new SwapChainDescription
{
BufferCount = 1,
ModeDescription = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput,
Flags = SwapChainFlags.AllowModeSwitch
};
Device11 device11;
SwapChain swapChain;
Device11.CreateWithSwapChain(adapter1, DeviceCreationFlags.None, description, out device11, out swapChain);
// create a view of our render target, which is the backbuffer of the swap chain we just created
RenderTargetView renderTargetView;
using (var resource = Resource.FromSwapChain<Texture2D>(swapChain, 0))
renderTargetView = new RenderTargetView(device11, resource);
// setting a viewport is required if you want to actually see anything
var context = device11.ImmediateContext;
var viewport = new Viewport(0.0f, 0.0f, form.ClientSize.Width, form.ClientSize.Height);
context.OutputMerger.SetTargets(renderTargetView);
context.Rasterizer.SetViewports(viewport);
//
// Create the DirectX11 texture2D. This texture will be shared with the DirectX10 device.
//
// The DirectX10 device will be used to render text onto this texture.
// DirectX11 will then draw this texture (blended) onto the screen.
// The KeyedMutex flag is required in order to share this resource between the two devices.
var textureD3D11 = new Texture2D(device11, new Texture2DDescription
{
Width = form.ClientSize.Width,
Height = form.ClientSize.Height,
MipLevels = 1,
ArraySize = 1,
Format = Format.B8G8R8A8_UNorm,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.SharedKeyedmutex
});
// ---------------------------------------------------------------------------------------------
// Setup a direct 3d version 10.1 adapter
// ---------------------------------------------------------------------------------------------
var device10 = new Device10(adapter1, SharpDX.Direct3D10.DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0);
// ---------------------------------------------------------------------------------------------
// Setup Direct 2d
// ---------------------------------------------------------------------------------------------
// Direct2D Factory
var factory2D = new SharpDX.Direct2D1.Factory(FactoryType.SingleThreaded, DebugLevel.Information);
// Here we bind the texture we've created on our direct3d11 device through the direct3d10
// to the direct 2d render target....
var sharedResource = textureD3D11.QueryInterface<SharpDX.DXGI.Resource>();
var textureD3D10 = device10.OpenSharedResource<SharpDX.Direct3D10.Texture2D>(sharedResource.SharedHandle);
var surface = textureD3D10.AsSurface();
var rtp = new RenderTargetProperties
{
MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_10,
Type = RenderTargetType.Hardware,
PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied)
};
var renderTarget2D = new RenderTarget(factory2D, surface, rtp);
var solidColorBrush = new SolidColorBrush(renderTarget2D, Colors.Red);
// ---------------------------------------------------------------------------------------------------
// Setup the rendering data
// ---------------------------------------------------------------------------------------------------
// Load Effect. This includes both the vertex and pixel shaders.
// Also can include more than one technique.
ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(
"effectDx11.fx",
//.........这里部分代码省略.........
示例11: Main
//.........这里部分代码省略.........
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, () =>
{
// clear the render target to a soothing blue
context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));
// draw the triangle
context.Draw(3, 0);
swapChain.Present(0, PresentFlags.None);
});
// clean up all resources
// anything we missed will show up in the debug output
vertices.Close();
vertexBuffer.Dispose();
layout.Dispose();
inputSignature.Dispose();
vertexShader.Dispose();
geometryShader.Dispose();
pixelShader.Dispose();
renderTarget.Dispose();
swapChain.Dispose();
device.Dispose();
}
示例12: CreateVertexBuffer
private void CreateVertexBuffer()
{
_vertexCount = _xRes*_yRes;
var vertexStream = new DataStream(_vertexCount*PointVertex.SizeOf, true, true);
// store pixel coordinates in each vertex
for (short y = 0; y < _yRes; y++)
{
for (short x = 0; x < _xRes; x++)
{
var pt = new PointVertex(x, y);
vertexStream.Write(pt);
}
}
vertexStream.Position = 0;
// create vertex buffer
_vertexBuffer = new Buffer(_dxDevice, vertexStream,
_vertexCount*PointVertex.SizeOf, ResourceUsage.Immutable,
BindFlags.VertexBuffer,
CpuAccessFlags.None, ResourceOptionFlags.None);
vertexStream.Close();
}
示例13: CreateIndexBuffer
private void CreateIndexBuffer()
{
_indexCount = (_xRes - 1)*(_yRes - 1)*6;
var indexStream = new DataStream(_indexCount* sizeof(uint), true, true);
// create index buffer
for (int y = 0; y < _yRes -1; y++)
{
for (int x = 0; x < _xRes -1; x++)
{
// first triangle
indexStream.Write(y*_xRes + x);
indexStream.Write((y+1)*_xRes + x);
indexStream.Write(y * _xRes + x + 1);
// second triangle
indexStream.Write((y + 1)*_xRes + x);
indexStream.Write((y + 1)*_xRes + x + 1);
indexStream.Write(y * _xRes + x + 1);
}
}
indexStream.Position = 0;
_indexBuffer = new Buffer(_dxDevice, indexStream, _indexCount*sizeof (uint), ResourceUsage.Immutable,
BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None);
indexStream.Close();
}
示例14: CreateBatch
private void CreateBatch(PositionTexCoordNormalVertex[] vertices, short[] indices, Renderer renderer)
{
Debug.Assert(vertices != null && indices != null);
int vertexBufferSize = PositionTexCoordNormalVertex.SizeInBytes * vertices.Length;
var verticesData = new DataStream(vertexBufferSize, true, true);
verticesData.WriteRange(vertices);
verticesData.Seek(0, SeekOrigin.Begin);
var vertexBuffer = new Buffer(renderer.Device, verticesData, vertexBufferSize, ResourceUsage.Default,
BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None);
verticesData.Close();
int indexBufferSize = sizeof(short) * indices.Length;
var indicesData = new DataStream(indexBufferSize, true, true);
indicesData.WriteRange(indices);
indicesData.Seek(0, SeekOrigin.Begin);
var indexBuffer = new Buffer(renderer.Device, indicesData, indexBufferSize, ResourceUsage.Default,
BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None);
indicesData.Close();
// Create batch
var vertexBufferBinding = new VertexBufferBinding(vertexBuffer, PositionTexCoordNormalVertex.SizeInBytes, 0);
var inputLayout = new InputLayout(renderer.Device, PositionTexCoordNormalVertex.InputElements,
effect.GetTechniqueByIndex(0).GetPassByIndex(0).Description.Signature);
batch = new DrawIndexedBatch(renderer.Device, effect, new[] { vertexBufferBinding }, inputLayout, indexBuffer, Format.R16_UInt,
indices.Length, 0, 0);
batches.Add(batch);
}
示例15: BuildQuad
static void BuildQuad()
{
ushort[] idx;
TexturedVertex[] quad = CreateTexturedQuad(Vector3.Zero, size.Width, size.Height, out idx);
// fill vertex and index buffers
DataStream stream = new DataStream(4 * 24, true, true);
stream.WriteRange(quad);
stream.Position = 0;
vertices = new SlimDX.Direct3D10.Buffer(device, stream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags =
ResourceOptionFlags.None,
SizeInBytes = 4 * 24,
Usage = ResourceUsage.Default
});
stream.Close();
stream = new DataStream(6 * sizeof(ushort), true, true);
stream.WriteRange(idx);
stream.Position = 0;
indices = new SlimDX.Direct3D10.Buffer(device, stream, new BufferDescription()
{
BindFlags = BindFlags.IndexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 6 * sizeof(ushort),
Usage = ResourceUsage.Default
});
}