private void InitalizeGraphics() { if (Window.RenderCanvasHandle == IntPtr.Zero) { throw new InvalidOperationException("Window handle cannot be zero"); } SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = 1, Flags = SwapChainFlags.None, IsWindowed = true, OutputHandle = Window.RenderCanvasHandle, SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, ModeDescription = new ModeDescription() { Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, //Format = SlimDX.DXGI.Format.B8G8R8A8_UNorm, Width = Window.ClientSize.Width, Height = Window.ClientSize.Height, RefreshRate = new Rational(60, 1), Scaling = DisplayModeScaling.Unspecified, ScanlineOrdering = DisplayModeScanlineOrdering.Unspecified }, SampleDescription = new SampleDescription(1, 0) }; var giFactory = new SlimDX.DXGI.Factory(); var adapter = giFactory.GetAdapter(0); Device device; SwapChain swapChain; Device.CreateWithSwapChain(adapter, DriverType.Hardware, DeviceCreationFlags.None, swapChainDesc, out device, out swapChain); _swapChain = swapChain; GraphicsDevice = device; // create a view of our render target, which is the backbuffer of the swap chain we just created using (var resource = SlimDX.Direct3D10.Resource.FromSwapChain <Texture2D>(swapChain, 0)) { _backBuffer = new RenderTargetView(device, resource); } // setting a viewport is required if you want to actually see anything var viewport = new Viewport(0, 0, Window.ClientSize.Width, Window.ClientSize.Height); device.OutputMerger.SetTargets(_backBuffer); device.Rasterizer.SetViewports(viewport); CreateDepthStencil(); LoadVisualizationEffect(); // Allocate a large buffer to write the PhysX visualization vertices into // There's more optimized ways of doing this, but for this sample a large buffer will do _userPrimitivesBuffer = new SlimDX.Direct3D10.Buffer(GraphicsDevice, VertexPositionColor.SizeInBytes * 50000, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); var elements = new[] { new InputElement("Position", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("Color", 0, Format.R32G32B32A32_Float, 16, 0) }; _inputLayout = new InputLayout(GraphicsDevice, _visualizationEffect.RenderScenePass0.Description.Signature, elements); }
private void InitalizeGraphics() { if (Window.RenderCanvasHandle == IntPtr.Zero) throw new InvalidOperationException(); SwapChainDescription swapChainDesc = new SwapChainDescription() { BufferCount = 1, Flags = SwapChainFlags.None, IsWindowed = true, OutputHandle = Window.RenderCanvasHandle, SwapEffect = SwapEffect.Discard, Usage = Usage.RenderTargetOutput, ModeDescription = new ModeDescription() { Format = SlimDX.DXGI.Format.R8G8B8A8_UNorm, //Format = SlimDX.DXGI.Format.B8G8R8A8_UNorm, Width = (int)Window.RenderCanvasSize.Width, Height = (int)Window.RenderCanvasSize.Height, RefreshRate = new Rational(60, 1), Scaling = DisplayModeScaling.Unspecified, ScanlineOrdering = DisplayModeScanlineOrdering.Unspecified }, SampleDescription = new SampleDescription(1, 0) }; var giFactory = new SlimDX.DXGI.Factory(); var adapter = giFactory.GetAdapter(0); Device device; SwapChain swapChain; Device.CreateWithSwapChain(adapter, DriverType.Hardware, DeviceCreationFlags.Debug, swapChainDesc, out device, out swapChain); _swapChain = swapChain; GraphicsDevice = device; // create a view of our render target, which is the backbuffer of the swap chain we just created using (var resource = SlimDX.Direct3D10.Resource.FromSwapChain<Texture2D>(swapChain, 0)) { _backBuffer = new RenderTargetView(device, resource); } // setting a viewport is required if you want to actually see anything var viewport = new Viewport(0, 0, (int)Window.RenderCanvasSize.Width, (int)Window.RenderCanvasSize.Height); device.OutputMerger.SetTargets(_backBuffer); device.Rasterizer.SetViewports(viewport); CreateDepthStencil(); LoadVisualizationEffect(); // Allocate a large buffer to write the PhysX visualization vertices into // There's more optimized ways of doing this, but for this sample a large buffer will do _userPrimitivesBuffer = new SlimDX.Direct3D10.Buffer(GraphicsDevice, VertexPositionColor.SizeInBytes * 50000, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None); var elements = new[] { new InputElement("Position", 0, Format.R32G32B32A32_Float, 0, 0), new InputElement("Color", 0, Format.R32G32B32A32_Float, 16, 0) }; _inputLayout = new InputLayout(GraphicsDevice, _visualizationEffect.RenderScenePass0.Description.Signature, elements); // States var blendDesc = new BlendStateDescription() { SourceBlend = BlendOption.One, DestinationBlend = BlendOption.Zero, BlendOperation = BlendOperation.Add, SourceAlphaBlend = BlendOption.One, DestinationAlphaBlend = BlendOption.Zero, AlphaBlendOperation = BlendOperation.Add }; blendDesc.SetBlendEnable(0, true); _blendState = BlendState.FromDescription(device, blendDesc); var rasterDesc = new RasterizerStateDescription() { IsAntialiasedLineEnabled = false, IsMultisampleEnabled = false, FillMode = FillMode.Solid, CullMode = CullMode.None }; _rasterizerState = RasterizerState.FromDescription(device, rasterDesc); }
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 }); }
public virtual void Update(Device device, ShaderSignature effectSignature) { if (device != null && !device.Disposed) { mDevice = device; mSignature = effectSignature; // If there is less than 1 vertex then we can't make a point, let alone a shape! if (Vertices != null && Vertices.Count > 0) { CalculatePreTransformBoundingBox(); // Add Vertices to a datastream. using (DataStream dataStream = new DataStream(Vertices.NumBytes, true, true)) { dataStream.WriteRange(this.Vertices.ToArray()); dataStream.Position = 0; // Create a new data buffer description and buffer BufferDescription desc = new BufferDescription() { BindFlags = BindFlags.VertexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None, SizeInBytes = Vertices.NumBytes, Usage = ResourceUsage.Default }; if (vertexBuffer != null) vertexBuffer.Dispose(); vertexBuffer = new SlimDX.Direct3D10.Buffer(device, dataStream, desc); //dataStream.Close(); } if (Vertices != null && Vertices.Count > 0) { // Set the input layout. InputElement[] inputElements = Vertices[0].GetInputElements(); if (vertexLayout != null) vertexLayout.Dispose(); vertexLayout = new InputLayout(device, effectSignature, inputElements); // Draw Indexed if (Vertices.Indices != null && Vertices.Indices.Count > 0) { using (DataStream iStream = new DataStream(sizeof(int) * Vertices.Indices.Count, true, true)) { iStream.WriteRange(Vertices.Indices.ToArray()); iStream.Position = 0; BufferDescription desc = new BufferDescription() { Usage = ResourceUsage.Default, SizeInBytes = sizeof(int) * Vertices.Indices.Count, BindFlags = BindFlags.IndexBuffer, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.None }; if (indexBuffer != null) indexBuffer.Dispose(); indexBuffer = new Buffer(device, iStream, desc); //iStream.Close(); } } else { if (indexBuffer != null) indexBuffer.Dispose(); indexBuffer = null; } } else { if (vertexBuffer != null) vertexBuffer.Dispose(); vertexBuffer = null; } } } FireShapeChangeEvent(new ShapeChangeEventArgs(this, ShapeChangeEventArgs.ChangeAction.None)); }