public void CanBindMultipleVertexBuffers() { // Arrange. var device = new Device(); var inputAssembler = new InputAssemblerStage(device); var vertices = new[] { new TestVertex.PositionNormalTexture(Vector3.Zero, Vector3.Zero, Vector2.Zero), new TestVertex.PositionNormalTexture(Vector3.Zero, Vector3.Zero, Vector2.Zero) }; var vertexBuffer1 = device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), vertices); var vertexBuffer2 = device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), vertices); // Act. inputAssembler.SetVertexBuffers(0, new[] { new VertexBufferBinding(vertexBuffer1, 0, TestVertex.PositionNormalTexture.SizeInBytes), new VertexBufferBinding(vertexBuffer2, 0, TestVertex.PositionNormalTexture.SizeInBytes) }); // Assert. var vertexBufferBindings = new VertexBufferBinding[2]; inputAssembler.GetVertexBuffers(0, 2, vertexBufferBindings); Assert.That(vertexBufferBindings[0].Buffer, Is.EqualTo(vertexBuffer1)); Assert.That(vertexBufferBindings[1].Buffer, Is.EqualTo(vertexBuffer2)); }
public void CanGetVertexStreamForLineListWithMultipleVertexBuffers() { // Arrange. var device = new Device(); var inputAssembler = new InputAssemblerStage(device); var inputSignature = GetTestVertexPositionNormalTextureShaderBytecode(); var inputElements = TestVertex.PositionNormal.InputElements .Union(new[] { new InputElement("TEXCOORD", 0, Format.R32G32_Float, 1, 0) }) .ToArray(); inputAssembler.InputLayout = new InputLayout(device, inputElements, inputSignature); inputAssembler.PrimitiveTopology = PrimitiveTopology.LineList; var positionsAndNormals = new[] { new TestVertex.PositionNormal(new Vector3(1, 2, 3), new Vector3(3, 2, 1)), new TestVertex.PositionNormal(new Vector3(4, 5, 6), new Vector3(4, 6, 8)) }; var texCoords = new[] { new Vector2(3, 4), new Vector2(0.5f, 0.3f) }; inputAssembler.SetVertexBuffers(0, new[] { new VertexBufferBinding(device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), positionsAndNormals), 0, TestVertex.PositionNormal.SizeInBytes), new VertexBufferBinding(device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), texCoords), 0, Vector2.SizeInBytes) }); // Act. var vertexStream = inputAssembler.GetVertexStream(inputSignature, 2, 0).ToList(); // Assert. Assert.That(vertexStream, Has.Count.EqualTo(2)); Assert.That(vertexStream[0].InstanceID, Is.EqualTo(0)); Assert.That(vertexStream[0].VertexID, Is.EqualTo(0)); Assert.That(vertexStream[0].Data, Is.EqualTo(new[] { new Number4(1.0f, 2.0f, 3.0f, 0.0f), new Number4(3.0f, 2.0f, 1.0f, 0.0f), new Number4(3.0f, 4.0f, 0.0f, 0.0f) })); Assert.That(vertexStream[1].InstanceID, Is.EqualTo(0)); Assert.That(vertexStream[1].VertexID, Is.EqualTo(1)); Assert.That(vertexStream[1].Data, Is.EqualTo(new[] { new Number4(4.0f, 5.0f, 6.0f, 0.0f), new Number4(4.0f, 6.0f, 8.0f, 0.0f), new Number4(0.5f, 0.3f, 0.0f, 0.0f) })); }
void Render() { frameAccumulator += _frameDelta; ++frameCount; if (frameAccumulator >= 1.0f) { FramesPerSecond = frameCount / frameAccumulator; frameAccumulator = 0.0f; frameCount = 0; } // Clear targets _device.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0); _device.ClearRenderTargetView(renderView, ambient); _device.ClearRenderTargetView(gBufferLightView, ambient); _device.ClearRenderTargetView(gBufferNormalView, ambient); _device.ClearRenderTargetView(gBufferDiffuseView, ambient); meshFactory.InitInstancedRender(); // Depth map pass if (shadowsEnabled) { _device.ClearDepthStencilView(lightDepthView, DepthStencilClearFlags.Depth, 1.0f, 0); outputMerger.SetDepthStencilState(lightDepthStencilState, 0); outputMerger.SetRenderTargets(0, new RenderTargetView[0], lightDepthView); shadowGenPass.Apply(); OnRender(); effect2.GetVariableByName("lightDepthMap").AsShaderResource().SetResource(lightDepthRes); } // Render pass outputMerger.SetDepthStencilState(depthStencilState, 0); outputMerger.SetRenderTargets(3, gBufferViews, depthView); gBufferGenPass.Apply(); OnRender(); // G-buffer render pass outputMerger.SetDepthStencilState(null, 0); outputMerger.SetRenderTargets(1, renderViews, null); gBufferRenderPass.Apply(); inputAssembler.SetVertexBuffers(0, quadBinding); inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip; inputAssembler.InputLayout = quadBufferLayout; _device.Draw(4, 0); Info.OnRender(FramesPerSecond); _swapChain.Present(0, PresentFlags.None); }
public void CanGetVertexStreamForIndexedLineList() { // Arrange. var device = new Device(); var inputAssembler = new InputAssemblerStage(device); var inputSignature = GetTestVertexPositionNormalTextureShaderBytecode(); inputAssembler.InputLayout = new InputLayout(device, TestVertex.PositionNormalTexture.InputElements, inputSignature); inputAssembler.PrimitiveTopology = PrimitiveTopology.LineList; var vertices = new[] { new TestVertex.PositionNormalTexture(), new TestVertex.PositionNormalTexture(), new TestVertex.PositionNormalTexture(new Vector3(1, 2, 3), new Vector3(3, 2, 1), new Vector2(3, 4)), new TestVertex.PositionNormalTexture(new Vector3(4, 5, 6), new Vector3(4, 6, 8), new Vector2(0.5f, 0.3f)) }; var vertexBuffer = device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), vertices); inputAssembler.SetVertexBuffers(0, new[] { new VertexBufferBinding(vertexBuffer, 0, TestVertex.PositionNormalTexture.SizeInBytes) }); var indexBuffer = device.CreateBuffer(new BufferDescription(BindFlags.IndexBuffer), new ushort[] { 2, 16, 1, 0 }); inputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 2); // Act. var vertexStream = inputAssembler.GetVertexStreamIndexed(inputSignature, 2, 1, 2).ToList(); // Assert. Assert.That(vertexStream, Has.Count.EqualTo(2)); Assert.That(vertexStream[0].InstanceID, Is.EqualTo(0)); Assert.That(vertexStream[0].VertexID, Is.EqualTo(1)); Assert.That(vertexStream[0].Data, Is.EqualTo(new[] { new Number4(4.0f, 5.0f, 6.0f, 0.0f), new Number4(4.0f, 6.0f, 8.0f, 0.0f), new Number4(0.5f, 0.3f, 0.0f, 0.0f) })); Assert.That(vertexStream[1].InstanceID, Is.EqualTo(0)); Assert.That(vertexStream[1].VertexID, Is.EqualTo(2)); Assert.That(vertexStream[1].Data, Is.EqualTo(new[] { new Number4(1.0f, 2.0f, 3.0f, 0.0f), new Number4(3.0f, 2.0f, 1.0f, 0.0f), new Number4(3.0f, 4.0f, 0.0f, 0.0f) })); }
public void DrawDebugWorld(DynamicsWorld world) { world.DebugDrawWorld(); if (lines.Count == 0) { return; } inputAssembler.InputLayout = inputLayout; if (lineArray.Length != lines.Count) { lineArray = new PositionColored[lines.Count]; lines.CopyTo(lineArray); if (vertexBuffer != null) { vertexBuffer.Dispose(); } vertexBufferDesc.SizeInBytes = PositionColored.Stride * lines.Count; using (var data = new DataStream(vertexBufferDesc.SizeInBytes, false, true)) { data.WriteRange(lineArray); data.Position = 0; vertexBuffer = new Buffer(device, data, vertexBufferDesc); } vertexBufferBinding.Buffer = vertexBuffer; } else { lines.CopyTo(lineArray); DataStream ds; var map = device.ImmediateContext.MapSubresource(vertexBuffer, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None, out ds); ds.WriteRange(lineArray); device.ImmediateContext.UnmapSubresource(vertexBuffer, 0); } inputAssembler.SetVertexBuffers(0, vertexBufferBinding); inputAssembler.PrimitiveTopology = global::SharpDX.Direct3D.PrimitiveTopology.LineList; device.ImmediateContext.VertexShader.Set(vertexShader); device.ImmediateContext.PixelShader.Set(pixelShader); device.ImmediateContext.Draw(lines.Count, 0); lines.Clear(); }
public void DrawDebugWorld(DynamicsWorld world) { world.DebugDrawWorld(); if (lines.Count == 0) { return; } inputAssembler.InputLayout = inputLayout; if (lineArray.Length != lines.Count) { lineArray = new PositionColored[lines.Count]; lines.CopyTo(lineArray); if (vertexBuffer != null) { vertexBuffer.Dispose(); } vertexBufferDesc.SizeInBytes = PositionColored.Stride * lines.Count; using (var data = new DataStream(vertexBufferDesc.SizeInBytes, false, true)) { data.WriteRange(lineArray); data.Position = 0; vertexBuffer = new Buffer(device, data, vertexBufferDesc); } vertexBufferBinding.Buffer = vertexBuffer; } else { lines.CopyTo(lineArray); using (var map = vertexBuffer.Map(MapMode.WriteDiscard)) { map.WriteRange(lineArray); } vertexBuffer.Unmap(); } inputAssembler.SetVertexBuffers(0, vertexBufferBinding); inputAssembler.PrimitiveTopology = global::SharpDX.Direct3D.PrimitiveTopology.LineList; device.Draw(lines.Count, 0); lines.Clear(); }
public void RenderInstanced() { inputAssembler.InputLayout = inputLayout; foreach (ShapeData s in shapes.Values) { inputAssembler.SetVertexBuffers(0, s.BufferBindings); inputAssembler.PrimitiveTopology = s.PrimitiveTopology; if (s.IndexBuffer != null) { inputAssembler.SetIndexBuffer(s.IndexBuffer, s.IndexFormat, 0); device.ImmediateContext.DrawIndexedInstanced(s.IndexCount, s.InstanceDataList.Count, 0, 0, 0); } else { device.ImmediateContext.DrawInstanced(s.VertexCount, s.InstanceDataList.Count, 0, 0); } } }
/* * protected override void Dispose(bool disposing) * { * if (disposing) * { * if (vertexBuffer != null) * { * vertexBuffer.Dispose(); * vertexBuffer = null; * } * } * * base.Dispose(disposing); * } */ public void DrawDebugWorld(DynamicsWorld world) { world.DebugDrawWorld(); if (LineIndex == 0) { return; } _inputAssembler.InputLayout = _inputLayout; if (_vertexCount != LineIndex) { if (_vertexBuffer != null) { _vertexBuffer.Dispose(); } _vertexCount = LineIndex; _vertexBufferDesc.SizeInBytes = PositionColored.Stride * _vertexCount; using (var data = new DataStream(_vertexBufferDesc.SizeInBytes, false, true)) { data.WriteRange(Lines, 0, _vertexCount); data.Position = 0; _vertexBuffer = new Buffer(_device, data, _vertexBufferDesc); } _vertexBufferBinding.Buffer = _vertexBuffer; } else { using (var map = _vertexBuffer.Map(MapMode.WriteDiscard)) { map.WriteRange(Lines, 0, _vertexCount); } _vertexBuffer.Unmap(); } _inputAssembler.SetVertexBuffers(0, _vertexBufferBinding); _inputAssembler.PrimitiveTopology = global::SharpDX.Direct3D.PrimitiveTopology.LineList; _device.Draw(_vertexCount, 0); LineIndex = 0; }
void RenderLights() { inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; inputAssembler.InputLayout = lightVolumeInputLayout; inputAssembler.SetVertexBuffers(0, pointLightVolumeVertexBufferBinding); inputAssembler.SetIndexBuffer(pointLightVolumeIndexBuffer, Format.R32_UInt, 0); lightDepthBufferVar.SetResource(depthBufferRes); lightNormalBufferVar.SetResource(normalBufferRes); Vector3 eyePosition = MathHelper.Convert(Demo.Freelook.Eye); var previousState = _immediateContext.Rasterizer.State; // Camera outside light volume _immediateContext.Rasterizer.State = backCullState; outputMerger.SetDepthStencilState(outsideLightVolumeDepthState); foreach (var light in lights) { float radius = light.Radius; float bias = radius * 2; if ((light.Position - eyePosition).LengthSquared() >= (radius * radius) + bias) { RenderLight(light); } } // Camera inside light volume _immediateContext.Rasterizer.State = frontCullState; outputMerger.SetDepthStencilState(insideLightVolumeDepthState); foreach (var light in lights) { float bias = light.Radius * 2; if ((light.Position - eyePosition).LengthSquared() < (light.Radius * light.Radius) + bias) { RenderLight(light); } } _immediateContext.Rasterizer.State = previousState; }
internal void Apply(InputAssemblerStage inputAssemblerStage) { inputAssemblerStage.SetVertexBuffers(0, nativeVertexBufferBindings); inputAssemblerStage.SetIndexBuffer(nativeIndexBuffer, indexFormat, indexBufferOffset); }
void Render() { // Clear targets _device.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0); _device.ClearRenderTargetView(renderView, ambient); _device.ClearRenderTargetView(gBufferLightView, ambient); _device.ClearRenderTargetView(gBufferNormalView, ambient); _device.ClearRenderTargetView(gBufferDiffuseView, ambient); _meshFactory.InitInstancedRender(Demo.World.CollisionObjectArray); // Light depth map pass if (shadowsEnabled) { _device.ClearDepthStencilView(lightDepthView, DepthStencilClearFlags.Depth, 1.0f, 0); outputMerger.SetDepthStencilState(lightDepthStencilState, 0); outputMerger.SetRenderTargets(0, new RenderTargetView[0], lightDepthView); shadowGenPass.Apply(); OnRender(); lightDepthMapVar.SetResource(lightDepthRes); } // Render pass lightBufferVar.SetResource(null); normalBufferVar.SetResource(null); diffuseBufferVar.SetResource(null); depthMapVar.SetResource(null); lightDepthMapVar.SetResource(null); outputMerger.SetDepthStencilState(depthStencilState, 0); outputMerger.SetRenderTargets(3, gBufferViews, depthView); gBufferGenPass.Apply(); OnRender(); if (Demo.IsDebugDrawEnabled) { debugDrawPass.Apply(); (Demo.World.DebugDrawer as PhysicsDebugDraw).DrawDebugWorld(Demo.World); } outputMerger.SetDepthStencilState(null, 0); info.OnRender(Demo.FramesPerSecond); // G-buffer render pass lightBufferVar.SetResource(lightBufferRes); normalBufferVar.SetResource(normalBufferRes); diffuseBufferVar.SetResource(diffuseBufferRes); depthMapVar.SetResource(depthRes); lightDepthMapVar.SetResource(lightDepthRes); outputMerger.SetRenderTargets(1, renderViews, null); gBufferRenderPass.Apply(); inputAssembler.SetVertexBuffers(0, quadBinding); inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip; inputAssembler.InputLayout = quadBufferLayout; _device.Draw(4, 0); // G-buffer overlay pass diffuseBufferVar.SetResource(info.OverlayBufferRes); gBufferOverlayPass.Apply(); _device.Draw(4, 0); _swapChain.Present(0, PresentFlags.None); }
public void CanGetVertexStreamForInstancedLineList() { // Arrange. var device = new Device(); var inputAssembler = new InputAssemblerStage(device); var inputSignature = GetInstancedTestVertexPositionNormalShaderBytecode(); var inputElements = TestVertex.PositionNormal.InputElements .Union(new[] { new InputElement("INSTANCEPOS", 0, Format.R32G32B32_Float, 1, InputElement.AppendAligned, InputClassification.PerInstanceData, 1), new InputElement("INSTANCESCALE", 0, Format.R32G32B32_Float, 1, InputElement.AppendAligned, InputClassification.PerInstanceData, 1), new InputElement("INSTANCECOLOR", 0, Format.R32G32B32_Float, 2, InputElement.AppendAligned, InputClassification.PerInstanceData, 2) }) .ToArray(); inputAssembler.InputLayout = new InputLayout(device, inputElements, inputSignature); inputAssembler.PrimitiveTopology = PrimitiveTopology.LineList; var positionsAndNormals = new[] { new TestVertex.PositionNormal(new Vector3(1, 2, 3), new Vector3(3, 2, 1)), new TestVertex.PositionNormal(new Vector3(4, 5, 6), new Vector3(4, 6, 8)) }; var instancePositions = new[] { new Vector3(0, 0, 0), new Vector3(0, 0, 0), new Vector3(-2, 3, 16), // Position new Vector3(1, 1, 1), // Scale new Vector3(5, 3, 11), new Vector3(2, 2, 2), new Vector3(2, 5, 10), new Vector3(3, 3, 3), new Vector3(12, 15, 8), new Vector3(0.5f, 0.5f, 0.5f) }; var instanceColors = new[] { new Color3F(0, 0, 0), new Color3F(1, 0, 0), new Color3F(0, 1, 0) }; inputAssembler.SetVertexBuffers(0, new[] { new VertexBufferBinding(device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), positionsAndNormals), 0, TestVertex.PositionNormal.SizeInBytes), new VertexBufferBinding(device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), instancePositions), 0, Vector3.SizeInBytes * 2), new VertexBufferBinding(device.CreateBuffer(new BufferDescription(BindFlags.VertexBuffer), instanceColors), 0, Color3F.SizeInBytes) }); // Act. var vertexStream = inputAssembler.GetVertexStreamInstanced(inputSignature, 2, 4, 0, 1).ToList(); // Assert. Assert.That(vertexStream, Has.Count.EqualTo(8)); Assert.That(vertexStream[0].InstanceID, Is.EqualTo(0)); Assert.That(vertexStream[0].VertexID, Is.EqualTo(0)); Assert.That(vertexStream[0].Data, Is.EqualTo(new[] { new Number4(1.0f, 2.0f, 3.0f, 0.0f), // Position new Number4(3.0f, 2.0f, 1.0f, 0.0f), // Normal new Number4(-2.0f, 3.0f, 16.0f, 0.0f), // Instance Pos new Number4(1.0f, 1.0f, 1.0f, 0.0f), // Instance Scale new Number4(1.0f, 0.0f, 0.0f, 0.0f) // Instance Colour })); Assert.That(vertexStream[1].InstanceID, Is.EqualTo(0)); Assert.That(vertexStream[1].VertexID, Is.EqualTo(1)); Assert.That(vertexStream[1].Data, Is.EqualTo(new[] { new Number4(4.0f, 5.0f, 6.0f, 0.0f), new Number4(4.0f, 6.0f, 8.0f, 0.0f), new Number4(-2.0f, 3.0f, 16.0f, 0.0f), new Number4(1.0f, 1.0f, 1.0f, 0.0f), new Number4(1.0f, 0.0f, 0.0f, 0.0f) })); Assert.That(vertexStream[2].InstanceID, Is.EqualTo(1)); Assert.That(vertexStream[2].VertexID, Is.EqualTo(0)); Assert.That(vertexStream[2].Data, Is.EqualTo(new[] { new Number4(1.0f, 2.0f, 3.0f, 0.0f), new Number4(3.0f, 2.0f, 1.0f, 0.0f), new Number4(5.0f, 3.0f, 11.0f, 0.0f), new Number4(2.0f, 2.0f, 2.0f, 0.0f), new Number4(1.0f, 0.0f, 0.0f, 0.0f) })); Assert.That(vertexStream[3].InstanceID, Is.EqualTo(1)); Assert.That(vertexStream[3].VertexID, Is.EqualTo(1)); Assert.That(vertexStream[3].Data, Is.EqualTo(new[] { new Number4(4.0f, 5.0f, 6.0f, 0.0f), new Number4(4.0f, 6.0f, 8.0f, 0.0f), new Number4(5.0f, 3.0f, 11.0f, 0.0f), new Number4(2.0f, 2.0f, 2.0f, 0.0f), new Number4(1.0f, 0.0f, 0.0f, 0.0f) })); Assert.That(vertexStream[4].InstanceID, Is.EqualTo(2)); Assert.That(vertexStream[4].VertexID, Is.EqualTo(0)); Assert.That(vertexStream[4].Data, Is.EqualTo(new[] { new Number4(1.0f, 2.0f, 3.0f, 0.0f), // Position new Number4(3.0f, 2.0f, 1.0f, 0.0f), // Normal new Number4(2.0f, 5.0f, 10.0f, 0.0f), // Instance Pos new Number4(3.0f, 3.0f, 3.0f, 0.0f), // Instance Scale new Number4(0.0f, 1.0f, 0.0f, 0.0f) // Instance Colour })); Assert.That(vertexStream[5].InstanceID, Is.EqualTo(2)); Assert.That(vertexStream[5].VertexID, Is.EqualTo(1)); Assert.That(vertexStream[5].Data, Is.EqualTo(new[] { new Number4(4.0f, 5.0f, 6.0f, 0.0f), new Number4(4.0f, 6.0f, 8.0f, 0.0f), new Number4(2.0f, 5.0f, 10.0f, 0.0f), new Number4(3.0f, 3.0f, 3.0f, 0.0f), new Number4(0.0f, 1.0f, 0.0f, 0.0f) })); Assert.That(vertexStream[6].InstanceID, Is.EqualTo(3)); Assert.That(vertexStream[6].VertexID, Is.EqualTo(0)); Assert.That(vertexStream[6].Data, Is.EqualTo(new[] { new Number4(1.0f, 2.0f, 3.0f, 0.0f), new Number4(3.0f, 2.0f, 1.0f, 0.0f), new Number4(12.0f, 15.0f, 8.0f, 0.0f), new Number4(0.5f, 0.5f, 0.5f, 0.0f), new Number4(0.0f, 1.0f, 0.0f, 0.0f) })); Assert.That(vertexStream[7].InstanceID, Is.EqualTo(3)); Assert.That(vertexStream[7].VertexID, Is.EqualTo(1)); Assert.That(vertexStream[7].Data, Is.EqualTo(new[] { new Number4(4.0f, 5.0f, 6.0f, 0.0f), new Number4(4.0f, 6.0f, 8.0f, 0.0f), new Number4(12.0f, 15.0f, 8.0f, 0.0f), new Number4(0.5f, 0.5f, 0.5f, 0.0f), new Number4(0.0f, 1.0f, 0.0f, 0.0f) })); }
public void Bind(InputAssemblerStage inputAssembler) { inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList; inputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, VertexInfo.SizeInBytes, 0)); inputAssembler.SetIndexBuffer(indexBuffer, SharpDX.DXGI.Format.R32_UInt, 0); }