private void CreateSwapChain() { if (this.swapChain == null && this.RenderContext != null) { this.swapChain = new DX11SwapChain(this.RenderContext, this.node.Window.Handle, SlimDX.DXGI.Format.R8G8B8A8_UNorm, new SlimDX.DXGI.SampleDescription(1, 0), 60, 2, false); this.RenderContext.Factory.SetWindowAssociation(this.node.Window.Handle, SlimDX.DXGI.WindowAssociationFlags.IgnoreAll); } }
public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; if (this.updateddevices.Contains(context)) { return; } int samplecount = Convert.ToInt32(FInAASamplesPerPixel[0].Name); SampleDescription sd = new SampleDescription(samplecount, 0); if (this.FResized || this.FInvalidateSwapChain || this.FOutBackBuffer[0][context] == null) { this.FOutBackBuffer[0].Dispose(context); List <SampleDescription> sds = context.GetMultisampleFormatInfo(Format.R8G8B8A8_UNorm); int maxlevels = sds[sds.Count - 1].Count; if (sd.Count > maxlevels) { logger.Log(LogType.Warning, "Multisample count too high for this format, reverted to: " + maxlevels); sd.Count = maxlevels; } this.FOutBackBuffer[0][context] = new DX11SwapChain(context, this.Handle, Format.R8G8B8A8_UNorm, sd, 60, this.FInBufferCount[0]); #if DEBUG this.FOutBackBuffer[0][context].Resource.DebugName = "BackBuffer"; #endif this.depthmanager.NeedReset = true; } DX11SwapChain sc = this.FOutBackBuffer[0][context]; if (this.FResized) { //if (!sc.IsFullScreen) //{ // sc.Resize(); // } //this.FInvalidateSwapChain = true; } if (!this.renderers.ContainsKey(context)) { this.renderers.Add(context, new DX11GraphicsRenderer(this.FHost, context)); } this.depthmanager.Update(context, sc.Width, sc.Height, sd); this.updateddevices.Add(context); }
public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; if (this.updateddevices.Contains(context)) { return; } SampleDescription sd = new SampleDescription(1, 0); if (this.FResized || this.FInvalidateSwapChain || this.swapchain == null) { if (this.swapchain != null) { this.swapchain.Dispose(); } this.swapchain = new DX11SwapChain(context, this.form.Handle, Format.R8G8B8A8_UNorm, sd, this.FInRate[0]); } if (this.renderer == null) { this.renderer = new DX11GraphicsRenderer(this.FHost, context); } this.updateddevices.Add(context); if (this.FInFullScreen[0] != this.swapchain.IsFullScreen) { if (this.FInFullScreen[0]) { this.prevx = this.form.Width; this.prevy = this.form.Height; /*Screen screen = Screen.FromControl(this.form);*/ this.form.FormBorderStyle = FormBorderStyle.None; this.form.Width = Convert.ToInt32(this.FInRes[0].X); this.form.Height = Convert.ToInt32(this.FInRes[0].Y); this.swapchain.Resize(); this.swapchain.SetFullScreen(true); this.setfull = false; } else { this.swapchain.SetFullScreen(false); this.form.FormBorderStyle = FormBorderStyle.Fixed3D; this.form.Width = this.prevx; this.form.Height = this.prevy; this.swapchain.Resize(); } } }
public void Update(IPluginIO pin, DX11RenderContext context) { Device device = context.Device; if (this.updateddevices.Contains(context)) { return; } int samplecount = Convert.ToInt32(FInAASamplesPerPixel[0].Name); SampleDescription sd = new SampleDescription(samplecount, 0); if (this.FResized || this.FInvalidateSwapChain || this.FOutBackBuffer[0][context] == null) { EnumEntry bbf = this.FCfgBackBufferFormat[0]; this.FOutBackBuffer[0].Dispose(context); //NOTE ENUM BROKEN Format fmt = (Format)Enum.Parse(typeof(Format), this.FCfgBackBufferFormat[0].Name); this.FOutBackBuffer[0][context] = new DX11SwapChain(context, this.Handle, fmt, sd); #if DEBUG this.FOutBackBuffer[0][context].Resource.DebugName = "BackBuffer"; #endif this.depthmanager.NeedReset = true; } DX11SwapChain sc = this.FOutBackBuffer[0][context]; if (this.FResized) { //if (!sc.IsFullScreen) //{ // sc.Resize(); // } //this.FInvalidateSwapChain = true; } if (!this.renderers.ContainsKey(context)) { this.renderers.Add(context, new DX11GraphicsRenderer(this.FHost, context)); } this.depthmanager.Update(context, sc.Width, sc.Height, sd); this.updateddevices.Add(context); }
private void CreateSwapChain() { if (this.swapChain == null && this.RenderContext != null) { NativeWindow nativeWindow = new NativeWindow(); nativeWindow.AssignHandle(this.node.Window.Handle); this.form.Show(nativeWindow);// IWin32Window ^ w = Control::FromHandle(myWindowHandle); this.form.FormBorderStyle = FormBorderStyle.None; this.form.Left = 1920; this.form.Top = 0; this.form.Width = 1920; this.form.Height = 1080; this.swapChain = new DX11SwapChain(this.RenderContext, this.form.Handle, SlimDX.DXGI.Format.R8G8B8A8_UNorm, new SlimDX.DXGI.SampleDescription(1, 0), 60, 2, false); this.RenderContext.Factory.SetWindowAssociation(this.form.Handle, SlimDX.DXGI.WindowAssociationFlags.IgnoreAll); } }
public void Render(DX11RenderContext context) { Device device = context.Device; if (!this.updateddevices.Contains(context)) { this.Update(context); } if (this.rendereddevices.Contains(context)) { return; } Exception exception = null; if (this.FInEnabled[0]) { if (this.BeginQuery != null) { this.BeginQuery(context); } DX11SwapChain chain = this.FOutBackBuffer[0][context]; DX11GraphicsRenderer renderer = this.renderers[context]; renderer.EnableDepth = this.FInDepthBuffer[0]; renderer.DepthStencil = this.depthmanager.GetDepthStencil(context); renderer.DepthMode = this.depthmanager.Mode; renderer.SetRenderTargets(chain); renderer.SetTargets(); try { if (this.FInClear[0]) { //Remove Shader view if bound as is context.CurrentDeviceContext.ClearRenderTargetView(chain.RTV, this.FInBgColor[0]); } if (this.FInClearDepth[0]) { if (this.FInDepthBuffer[0]) { this.depthmanager.Clear(context, this.FInClearDepthValue[0]); } } //Only call render if layer connected if (this.FInLayer.IsConnected) { int rtmax = Math.Max(this.FInProjection.SliceCount, this.FInView.SliceCount); rtmax = Math.Max(rtmax, this.FInViewPort.SliceCount); settings.ViewportCount = rtmax; bool viewportpop = this.FInViewPort.IsConnected; for (int i = 0; i < rtmax; i++) { this.RenderSlice(context, settings, i, viewportpop); } } if (this.EndQuery != null) { this.EndQuery(context); } } catch (Exception ex) { exception = ex; } finally { renderer.CleanTargets(); } } this.rendereddevices.Add(context); //Rethrow if (exception != null) { throw exception; } }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect body index sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "BodyIndexView.fx", "PS_NormalizedView"); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; bool doUpload = false; BodyIndexFrameData currentData = null; DynamicBodyIndexTexture texture = new DynamicBodyIndexTexture(device); KinectSensorBodyIndexFrameProvider provider = new KinectSensorBodyIndexFrameProvider(sensor); provider.FrameReceived += (sender, args) => { currentData = args.FrameData; doUpload = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (doUpload) { texture.Copy(context, currentData); doUpload = false; } context.RenderTargetStack.Push(swapChain); device.Primitives.ApplyFullTriVS(context); context.Context.PixelShader.Set(pixelShader); context.Context.PixelShader.SetSampler(0, device.SamplerStates.LinearClamp); context.Context.PixelShader.SetShaderResource(0, texture.NormalizedView); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); texture.Dispose(); provider.Dispose(); pixelShader.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect Simple hd face sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); VertexShader vertexShader = ShaderCompiler.CompileFromFile <VertexShader>(device, "HdFaceView.fx", "VS"); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "HdFaceView.fx", "PS"); HdFaceIndexBuffer faceIndexBuffer = new HdFaceIndexBuffer(device, 1); DynamicHdFaceStructuredBuffer faceVertexBuffer = new DynamicHdFaceStructuredBuffer(device, 1); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); cbCamera camera = new cbCamera(); camera.Projection = Matrix.PerspectiveFovLH(1.57f * 0.5f, 1.3f, 0.01f, 100.0f); camera.View = Matrix.Translation(0.0f, 0.0f, 0.5f); camera.Projection.Transpose(); camera.View.Transpose(); ConstantBuffer <cbCamera> cameraBuffer = new ConstantBuffer <cbCamera>(device); cameraBuffer.Update(context, ref camera); bool doQuit = false; bool doUpload = false; KinectBody[] bodyFrame = null; KinectSensorBodyFrameProvider provider = new KinectSensorBodyFrameProvider(sensor); form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; FaceModel currentFaceModel = new FaceModel(); FaceAlignment currentFaceAlignment = new FaceAlignment(); SingleHdFaceProcessor hdFaceProcessor = new SingleHdFaceProcessor(sensor); hdFaceProcessor.HdFrameReceived += (sender, args) => { currentFaceModel = args.FaceModel; currentFaceAlignment = args.FaceAlignment; doUpload = true; }; provider.FrameReceived += (sender, args) => { bodyFrame = args.FrameData; var body = bodyFrame.TrackedOnly().ClosestBodies().FirstOrDefault(); if (body != null) { hdFaceProcessor.AssignBody(body); } else { hdFaceProcessor.Suspend(); } }; context.Context.Rasterizer.State = device.RasterizerStates.WireFrame; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (doUpload) { var vertices = currentFaceModel.CalculateVerticesForAlignment(currentFaceAlignment).ToArray(); faceVertexBuffer.Copy(context, vertices); doUpload = false; } context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); if (hdFaceProcessor.IsValid) { context.RenderTargetStack.Push(swapChain); context.Context.VertexShader.SetShaderResource(0, faceVertexBuffer.ShaderView); context.Context.VertexShader.SetConstantBuffer(0, cameraBuffer.Buffer); //Draw lines context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.Set(vertexShader); //Attach index buffer, null topology since we fetch faceIndexBuffer.AttachWithLayout(context); faceIndexBuffer.Draw(context, 1); context.RenderTargetStack.Pop(); } swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); cameraBuffer.Dispose(); faceIndexBuffer.Dispose(); faceVertexBuffer.Dispose(); provider.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); hdFaceProcessor.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect RGB Joint sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); //VertexShader vertexShader = ShaderCompiler.CompileFromFile<VertexShader>(device, "ColorJointView.fx", "VS"); SharpDX.D3DCompiler.ShaderSignature signature; VertexShader vertexShader = ShaderCompiler.CompileFromFile(device, "ColorJointView.fx", "VS_Color", out signature); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "ColorJointView.fx", "PS_Color"); DX11IndexedGeometry circle = device.Primitives.Segment(new Segment() { Resolution = 32 }); DX11InstancedIndexedDrawer drawer = new DX11InstancedIndexedDrawer(); circle.AssignDrawer(drawer); InputLayout layout; var bc = new ShaderBytecode(signature); circle.ValidateLayout(bc, out layout); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); Color4[] statusColor = new Color4[] { Color.Red, Color.Yellow, Color.Green }; //Note cbuffer should have a minimum size of 16 bytes, so we create verctor4 instead of vector2 SharpDX.Vector4 jointSize = new SharpDX.Vector4(0.04f, 0.07f, 0.0f, 1.0f); ConstantBuffer <SharpDX.Vector4> cbSize = new ConstantBuffer <SharpDX.Vector4>(device); cbSize.Update(context, ref jointSize); DX11StructuredBuffer colorTableBuffer = DX11StructuredBuffer.CreateImmutable <Color4>(device, statusColor); bool doQuit = false; bool doUpload = false; bool uploadImage = false; KinectBody[] bodyFrame = null; BodyColorPositionBuffer positionBuffer = new BodyColorPositionBuffer(device); BodyJointStatusBuffer statusBuffer = new BodyJointStatusBuffer(device); KinectSensorBodyFrameProvider provider = new KinectSensorBodyFrameProvider(sensor); provider.FrameReceived += (sender, args) => { bodyFrame = args.FrameData; doUpload = true; }; ColorRGBAFrameData rgbFrame = null; DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider colorProvider = new KinectSensorColorRGBAFrameProvider(sensor); colorProvider.FrameReceived += (sender, args) => { rgbFrame = args.FrameData; uploadImage = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (doUpload) { var tracked = bodyFrame.TrackedOnly(); var colorSpace = tracked.Select(kb => new ColorSpaceKinectJoints(kb, sensor.CoordinateMapper)); positionBuffer.Copy(context, colorSpace); statusBuffer.Copy(context, tracked); drawer.InstanceCount = colorSpace.Count() * Microsoft.Kinect.Body.JointCount; } if (uploadImage) { colorTexture.Copy(context, rgbFrame); } context.RenderTargetStack.Push(swapChain); context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); device.Primitives.ApplyFullTri(context, colorTexture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); circle.Bind(context, layout); context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.Set(vertexShader); context.Context.VertexShader.SetShaderResource(0, positionBuffer.ShaderView); context.Context.VertexShader.SetShaderResource(1, statusBuffer.ShaderView); context.Context.VertexShader.SetShaderResource(2, colorTableBuffer.ShaderView); context.Context.VertexShader.SetConstantBuffer(0, cbSize.Buffer); circle.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); colorProvider.Dispose(); colorTexture.Dispose(); positionBuffer.Dispose(); statusBuffer.Dispose(); colorTableBuffer.Dispose(); cbSize.Dispose(); provider.Dispose(); circle.Dispose(); layout.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); sensor.Close(); }
public void Render(DX11RenderContext context) { Device device = context.Device; if (!this.updateddevices.Contains(context)) { this.Update(null, context); } if (this.rendereddevices.Contains(context)) { return; } if (this.FInEnabled[0]) { if (this.BeginQuery != null) { this.BeginQuery(context); } DX11SwapChain chain = this.FOutBackBuffer[0][context]; DX11GraphicsRenderer renderer = this.renderers[context]; renderer.EnableDepth = this.FInDepthBuffer[0]; renderer.DepthStencil = this.depthmanager.GetDepthStencil(context); renderer.DepthMode = this.depthmanager.Mode; renderer.SetRenderTargets(chain); renderer.SetTargets(); if (this.FInClear[0]) { //Remove Shader view if bound as is context.CurrentDeviceContext.ClearRenderTargetView(chain.RTV, this.FInBgColor[0].Color); } if (this.FInClearDepth[0]) { if (this.FInDepthBuffer[0]) { this.depthmanager.Clear(context); } } //Only call render if layer connected if (this.FInLayer.PluginIO.IsConnected) { int rtmax = Math.Max(this.FInProjection.SliceCount, this.FInView.SliceCount); rtmax = Math.Max(rtmax, this.FInViewPort.SliceCount); settings.ViewportCount = rtmax; bool viewportpop = this.FInViewPort.PluginIO.IsConnected; float cw = (float)this.ClientSize.Width; float ch = (float)this.ClientSize.Height; for (int i = 0; i < rtmax; i++) { settings.ViewportIndex = i; settings.View = this.FInView[i]; Matrix proj = this.FInProjection[i]; Matrix aspect = Matrix.Invert(this.FInAspect[i]); Matrix crop = Matrix.Invert(this.FInCrop[i]); settings.Projection = proj * aspect * crop; settings.ViewProjection = settings.View * settings.Projection; settings.BackBuffer = this.FOutBackBuffer[0][context]; settings.RenderWidth = this.FOutBackBuffer[0][context].Resource.Description.Width; settings.RenderHeight = this.FOutBackBuffer[0][context].Resource.Description.Height; settings.ResourceSemantics.Clear(); settings.CustomSemantics.Clear(); if (viewportpop) { context.RenderTargetStack.PushViewport(this.FInViewPort[i].Normalize(cw, ch)); } //Call render on all layers for (int j = 0; j < this.FInLayer.SliceCount; j++) { this.FInLayer[j][context].Render(this.FInLayer.PluginIO, context, settings); } if (viewportpop) { context.RenderTargetStack.PopViewport(); } } } if (this.EndQuery != null) { this.EndQuery(context); } renderer.CleanTargets(); //OnDevice.RenderTargetStack.Pop(); } this.rendereddevices.Add(context); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect Simple filtered point cloud view sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); ComputeShader computeShader = ShaderCompiler.CompileFromFile <ComputeShader>(device, "PointCloudFilter.fx", "CS_Filter"); VertexShader vertexShader = ShaderCompiler.CompileFromFile <VertexShader>(device, "PointCloudJointView.fx", "VS"); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "PointCloudJointView.fx", "PS"); BodyCameraPositionBuffer positionBuffer = new BodyCameraPositionBuffer(device); DX11StructuredBuffer colorTableBuffer = DX11StructuredBuffer.CreateImmutable <Color4>(device, ColorTable); DX11NullGeometry nullGeom = new DX11NullGeometry(device); nullGeom.Topology = SharpDX.Direct3D.PrimitiveTopology.PointList; InstancedIndirectBuffer indirectDrawBuffer = new InstancedIndirectBuffer(device); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); cbCamera camera = new cbCamera(); camera.Projection = Matrix.PerspectiveFovLH(1.57f * 0.5f, 1.3f, 0.01f, 100.0f); camera.View = Matrix.Translation(0.0f, 0.0f, 2.0f); camera.Projection.Transpose(); camera.View.Transpose(); ConstantBuffer <cbCamera> cameraBuffer = new ConstantBuffer <cbCamera>(device); cameraBuffer.Update(context, ref camera); bool doQuit = false; bool uploadCamera = false; bool uploadBodyIndex = false; bool uploadBody = false; CameraRGBFrameData rgbFrame = new CameraRGBFrameData(); DynamicCameraRGBTexture cameraTexture = new DynamicCameraRGBTexture(device); KinectSensorDepthFrameProvider provider = new KinectSensorDepthFrameProvider(sensor); provider.FrameReceived += (sender, args) => { rgbFrame.Update(sensor.CoordinateMapper, args.DepthData); uploadCamera = true; }; BodyIndexFrameData bodyIndexFrame = null; DynamicBodyIndexTexture bodyIndexTexture = new DynamicBodyIndexTexture(device); KinectSensorBodyIndexFrameProvider bodyIndexProvider = new KinectSensorBodyIndexFrameProvider(sensor); bodyIndexProvider.FrameReceived += (sender, args) => { bodyIndexFrame = args.FrameData; uploadBodyIndex = true; }; AppendPointCloudBuffer pointCloudBuffer = new AppendPointCloudBuffer(device); KinectBody[] bodyFrame = null; KinectSensorBodyFrameProvider bodyFrameProvider = new KinectSensorBodyFrameProvider(sensor); bodyFrameProvider.FrameReceived += (sender, args) => { bodyFrame = args.FrameData; uploadBody = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (uploadCamera) { cameraTexture.Copy(context.Context, rgbFrame); uploadCamera = false; } if (uploadBodyIndex) { bodyIndexTexture.Copy(context.Context, bodyIndexFrame); uploadBodyIndex = false; } if (uploadBody) { positionBuffer.Copy(context, bodyFrame.TrackedOnly().ClosestBodies()); uploadBody = false; } //Prepare compute shader context.Context.ComputeShader.Set(computeShader); context.Context.ComputeShader.SetShaderResource(0, cameraTexture.ShaderView); context.Context.ComputeShader.SetShaderResource(1, bodyIndexTexture.RawView); //Set raw view here, we do not sample context.Context.ComputeShader.SetUnorderedAccessView(0, pointCloudBuffer.UnorderedView, 0); //Don't forget to set count to 0 context.Context.Dispatch(Consts.DepthWidth / 8, Consts.DepthHeight / 8, 1); //No iDivUp here, since it's not needed context.Context.ComputeShader.SetUnorderedAccessView(0, null); //Make runtime happy, and if we don't unbind we can't set as srv context.Context.CopyStructureCount(indirectDrawBuffer.ArgumentBuffer, 0, pointCloudBuffer.UnorderedView); //Draw filter buffer context.RenderTargetStack.Push(swapChain); context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); context.Context.VertexShader.Set(vertexShader); context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.SetShaderResource(0, pointCloudBuffer.ShaderView); context.Context.VertexShader.SetShaderResource(1, positionBuffer.ShaderView); context.Context.VertexShader.SetShaderResource(2, colorTableBuffer.ShaderView); context.Context.VertexShader.SetConstantBuffer(0, cameraBuffer.Buffer); nullGeom.Bind(context, null); context.Context.DrawInstancedIndirect(indirectDrawBuffer.ArgumentBuffer, 0); context.Context.VertexShader.SetShaderResource(0, null); //Make runtime happy context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); cameraBuffer.Dispose(); cameraTexture.Dispose(); bodyIndexTexture.Dispose(); provider.Dispose(); bodyIndexProvider.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); sensor.Close(); positionBuffer.Dispose(); colorTableBuffer.Dispose(); swapChain.Dispose(); context.Dispose(); device.Dispose(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect hd face projected to rgb"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); VertexShader vertexShader = ShaderCompiler.CompileFromFile <VertexShader>(device, "ProjectedHdFaceView.fx", "VS"); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "ProjectedHdFaceView.fx", "PS"); HdFaceIndexBuffer faceIndexBuffer = new HdFaceIndexBuffer(device, 1); DynamicRgbSpaceFaceStructuredBuffer faceRgbBuffer = new DynamicRgbSpaceFaceStructuredBuffer(device, 1); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; bool doUpload = false; KinectBody[] bodyFrame = null; KinectSensorBodyFrameProvider provider = new KinectSensorBodyFrameProvider(sensor); form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; FaceModel currentFaceModel = new FaceModel(); FaceAlignment currentFaceAlignment = new FaceAlignment(); SingleHdFaceProcessor hdFaceProcessor = new SingleHdFaceProcessor(sensor); hdFaceProcessor.HdFrameReceived += (sender, args) => { currentFaceModel = args.FaceModel; currentFaceAlignment = args.FaceAlignment; doUpload = true; }; bool uploadColor = false; ColorRGBAFrameData currentData = null; DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider colorProvider = new KinectSensorColorRGBAFrameProvider(sensor); colorProvider.FrameReceived += (sender, args) => { currentData = args.FrameData; uploadColor = true; }; provider.FrameReceived += (sender, args) => { bodyFrame = args.FrameData; var body = bodyFrame.TrackedOnly().ClosestBodies().FirstOrDefault(); if (body != null) { hdFaceProcessor.AssignBody(body); } else { hdFaceProcessor.Suspend(); } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (doUpload) { var vertices = currentFaceModel.CalculateVerticesForAlignment(currentFaceAlignment).ToArray(); var vertRgb = new ColorSpacePoint[vertices.Length]; sensor.CoordinateMapper.MapCameraPointsToColorSpace(vertices, vertRgb); faceRgbBuffer.Copy(context, vertRgb); doUpload = false; } if (uploadColor) { colorTexture.Copy(context, currentData); uploadColor = false; } context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); context.RenderTargetStack.Push(swapChain); context.Context.Rasterizer.State = device.RasterizerStates.BackCullSolid; context.Context.OutputMerger.BlendState = device.BlendStates.Disabled; device.Primitives.ApplyFullTri(context, colorTexture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); if (hdFaceProcessor.IsValid) { context.Context.Rasterizer.State = device.RasterizerStates.WireFrame; context.Context.OutputMerger.BlendState = device.BlendStates.AlphaBlend; context.Context.VertexShader.SetShaderResource(0, faceRgbBuffer.ShaderView); //Draw lines context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.Set(vertexShader); //Attach index buffer, null topology since we fetch faceIndexBuffer.AttachWithLayout(context); faceIndexBuffer.Draw(context, 1); } context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); colorProvider.Dispose(); colorTexture.Dispose(); faceIndexBuffer.Dispose(); faceRgbBuffer.Dispose(); provider.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect Simple point cloud view sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); VertexShader vertexShader = ShaderCompiler.CompileFromFile <VertexShader>(device, "ColoredPointCloudView.fx", "VS"); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "ColoredPointCloudView.fx", "PS"); DX11NullInstancedDrawer nulldrawer = new DX11NullInstancedDrawer(); nulldrawer.VertexCount = Consts.DepthWidth; nulldrawer.InstanceCount = Consts.DepthHeight; DX11NullGeometry nullGeom = new DX11NullGeometry(device, nulldrawer); nullGeom.Topology = SharpDX.Direct3D.PrimitiveTopology.PointList; KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); cbCamera camera = new cbCamera(); camera.Projection = Matrix.PerspectiveFovLH(1.57f * 0.5f, 1.3f, 0.01f, 100.0f); camera.View = Matrix.Translation(0.0f, 0.0f, 2.0f); camera.Projection.Transpose(); camera.View.Transpose(); ConstantBuffer <cbCamera> cameraBuffer = new ConstantBuffer <cbCamera>(device); cameraBuffer.Update(context, ref camera); bool doQuit = false; bool uploadCamera = false; bool uploadRgb = false; CameraRGBFrameData cameraFrame = new CameraRGBFrameData(); DynamicCameraRGBTexture cameraTexture = new DynamicCameraRGBTexture(device); DepthToColorFrameData depthToColorFrame = new DepthToColorFrameData(); DynamicDepthToColorTexture depthToColorTexture = new DynamicDepthToColorTexture(device); KinectSensorDepthFrameProvider provider = new KinectSensorDepthFrameProvider(sensor); provider.FrameReceived += (sender, args) => { cameraFrame.Update(sensor.CoordinateMapper, args.DepthData); depthToColorFrame.Update(sensor.CoordinateMapper, args.DepthData); uploadCamera = true; }; //Get coordinate map + rgb ColorRGBAFrameData colorFrame = new ColorRGBAFrameData(); DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider colorProvider = new KinectSensorColorRGBAFrameProvider(sensor); colorProvider.FrameReceived += (sender, args) => { colorFrame = args.FrameData; uploadRgb = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (uploadCamera) { cameraTexture.Copy(context.Context, cameraFrame); depthToColorTexture.Copy(context.Context, depthToColorFrame); uploadCamera = false; } if (uploadRgb) { colorTexture.Copy(context.Context, colorFrame); uploadRgb = false; } context.RenderTargetStack.Push(swapChain); context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); context.Context.VertexShader.Set(vertexShader); context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.SetShaderResource(0, cameraTexture.ShaderView); context.Context.VertexShader.SetShaderResource(1, colorTexture.ShaderView); context.Context.VertexShader.SetShaderResource(2, depthToColorTexture.ShaderView); context.Context.VertexShader.SetSampler(0, device.SamplerStates.LinearClamp); context.Context.VertexShader.SetConstantBuffer(0, cameraBuffer.Buffer); nullGeom.Bind(context, null); nullGeom.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); cameraBuffer.Dispose(); cameraTexture.Dispose(); provider.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); sensor.Close(); colorTexture.Dispose(); colorProvider.Dispose(); depthToColorFrame.Dispose(); depthToColorTexture.Dispose(); }
public void Update(DX11RenderContext context) { Device device = context.Device; if (this.updateddevices.Contains(context)) { return; } int samplecount = Convert.ToInt32(FInAASamplesPerPixel[0].Name); SampleDescription sd = new SampleDescription(samplecount, 0); if (this.FResized || this.FInvalidateSwapChain || this.FOutBackBuffer[0][context] == null) { //Set preset first int bufferCount = this.FInBufferCount[0]; if (bufferCount < 1) { logger.Log(LogType.Warning, "Less than one buffer specified, seting to 1"); bufferCount = 1; } if (this.FInFlipSequential[0]) { if (bufferCount < 2) { logger.Log(LogType.Warning, "Flip sequential mode requires at least 2 buffers, setting to 2"); bufferCount = 2; } if (sd.Count > 1) { logger.Log(LogType.Warning, "Flip sequential mode does not support Multisampling, disabling"); sd.Count = 1; } } else { List <SampleDescription> sds = context.GetMultisampleFormatInfo(Format.R8G8B8A8_UNorm); int maxlevels = sds[sds.Count - 1].Count; if (sd.Count > maxlevels) { logger.Log(LogType.Warning, "Multisample count too high for this format, reverted to: " + maxlevels); sd.Count = maxlevels; } } this.FOutBackBuffer.SafeDisposeAll(context); this.FOutBackBuffer[0][context] = new DX11SwapChain(context, this.Handle, Format.R8G8B8A8_UNorm, sd, this.FInRefreshRate[0], bufferCount, this.FInFlipSequential[0]); this.FInvalidateSwapChain = false; #if DEBUG this.FOutBackBuffer[0][context].Resource.DebugName = "BackBuffer"; #endif this.depthmanager.NeedReset = true; } DX11SwapChain sc = this.FOutBackBuffer[0][context]; if (!this.renderers.ContainsKey(context)) { this.renderers.Add(context, new DX11GraphicsRenderer(context)); } this.depthmanager.Update(context, sc.Width, sc.Height, sd); this.updateddevices.Add(context); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect multiple hd faces projected to rgb"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); VertexShader vertexShader = ShaderCompiler.CompileFromFile <VertexShader>(device, "ProjectedHdFaceView.fx", "VS"); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "ProjectedHdFaceView.fx", "PS"); int maxFaceCount = Consts.MaxBodyCount; int faceVertexCount = (int)Microsoft.Kinect.Face.FaceModel.VertexCount; var vertRgbTempBuffer = new ColorSpacePoint[faceVertexCount]; ColorSpacePoint[] facePoints = new ColorSpacePoint[faceVertexCount * maxFaceCount]; HdFaceIndexBuffer faceIndexBuffer = new HdFaceIndexBuffer(device, maxFaceCount); DynamicRgbSpaceFaceStructuredBuffer faceRgbBuffer = new DynamicRgbSpaceFaceStructuredBuffer(device, maxFaceCount); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; bool invalidateFace = false; KinectSensorBodyFrameProvider provider = new KinectSensorBodyFrameProvider(sensor); BodyTrackingProcessor bodyTracker = new BodyTrackingProcessor(); MultipleHdFaceProcessor multiFace = new MultipleHdFaceProcessor(sensor, bodyTracker, maxFaceCount); form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; bool uploadColor = false; ColorRGBAFrameData currentData = null; DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider colorProvider = new KinectSensorColorRGBAFrameProvider(sensor); colorProvider.FrameReceived += (sender, args) => { currentData = args.FrameData; uploadColor = true; }; provider.FrameReceived += (sender, args) => { bodyTracker.Next(args.FrameData); }; multiFace.OnFrameResultsChanged += (sender, args) => { invalidateFace = true; }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (invalidateFace) { int offset = 0; foreach (var data in multiFace.CurrentResults) { var vertices = data.FaceModel.CalculateVerticesForAlignment(data.FaceAlignment).ToArray(); sensor.CoordinateMapper.MapCameraPointsToColorSpace(vertices, vertRgbTempBuffer); Array.Copy(vertRgbTempBuffer, 0, facePoints, offset, faceVertexCount); offset += faceVertexCount; } faceRgbBuffer.Copy(context, facePoints, multiFace.CurrentResults.Count * faceVertexCount); invalidateFace = false; } if (uploadColor) { colorTexture.Copy(context, currentData); uploadColor = false; } context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); context.RenderTargetStack.Push(swapChain); context.Context.Rasterizer.State = device.RasterizerStates.BackCullSolid; context.Context.OutputMerger.BlendState = device.BlendStates.Disabled; device.Primitives.ApplyFullTri(context, colorTexture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); if (multiFace.CurrentResults.Count > 0) { context.Context.Rasterizer.State = device.RasterizerStates.WireFrame; context.Context.OutputMerger.BlendState = device.BlendStates.AlphaBlend; context.Context.VertexShader.SetShaderResource(0, faceRgbBuffer.ShaderView); //Draw lines context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.Set(vertexShader); //Attach index buffer, null topology since we fetch faceIndexBuffer.AttachWithLayout(context); faceIndexBuffer.Draw(context, multiFace.CurrentResults.Count); } context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); colorProvider.Dispose(); colorTexture.Dispose(); faceIndexBuffer.Dispose(); faceRgbBuffer.Dispose(); provider.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect simple pilot sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); //Allow to draw using direct2d on top of swapchain var context2d = new SharpDX.Direct2D1.DeviceContext(swapChain.Texture.QueryInterface <SharpDX.DXGI.Surface>()); //Call release on texture since queryinterface does an addref Marshal.Release(swapChain.Texture.NativePointer); var textFormat = new SharpDX.DirectWrite.TextFormat(device.DWriteFactory, "Arial", 16.0f); var blackBrush = new SharpDX.Direct2D1.SolidColorBrush(context2d, SharpDX.Color.Black); var whiteBrush = new SharpDX.Direct2D1.SolidColorBrush(context2d, SharpDX.Color.White); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; KinectSensorColorRGBAFrameProvider provider = new KinectSensorColorRGBAFrameProvider(sensor); DynamicColorRGBATextureProcessor colorProcessor = new DynamicColorRGBATextureProcessor(provider, device); KinectPilotProcessor pilot = KinectPilotProcessor.Default; KinectSensorBodyFrameProvider bodyFrameProvider = new KinectSensorBodyFrameProvider(sensor); bodyFrameProvider.FrameReceived += (sender, args) => { var body = args.FrameData.TrackedOnly().ClosestBodies().FirstOrDefault(); if (body != null) { pilot.Process(body.GetJointTable()); } }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } colorProcessor.Update(context); context.RenderTargetStack.Push(swapChain); device.Primitives.ApplyFullTri(context, colorProcessor.Texture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Pop(); context2d.BeginDraw(); var rect = new SharpDX.RectangleF(0, 0, 200, 130); context2d.FillRectangle(rect, blackBrush); context2d.DrawText("Elevation: " + pilot.Elevation, textFormat, rect, whiteBrush); rect.Top += 30; context2d.DrawText("Steering Y: " + pilot.SteeringY, textFormat, rect, whiteBrush); rect.Top += 30; context2d.DrawText("Steering Z: " + pilot.SterringZ, textFormat, rect, whiteBrush); rect.Top += 30; context2d.DrawText("Push: " + pilot.Push, textFormat, rect, whiteBrush); context2d.EndDraw(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); colorProcessor.Dispose(); provider.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect color sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); //Allow to draw using direct2d on top of swapchain var context2d = new SharpDX.Direct2D1.DeviceContext(swapChain.Texture.QueryInterface <SharpDX.DXGI.Surface>()); //Call release on texture since queryinterface does an addref Marshal.Release(swapChain.Texture.NativePointer); var whiteBrush = new SharpDX.Direct2D1.SolidColorBrush(context2d, SharpDX.Color.White); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); KinectBody[] bodyFrame = null; KinectSensorBodyFrameProvider bodyProvider = new KinectSensorBodyFrameProvider(sensor); bool doQuit = false; bool doUpload = false; ColorRGBAFrameData currentData = null; DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider provider = new KinectSensorColorRGBAFrameProvider(sensor); provider.FrameReceived += (sender, args) => { currentData = args.FrameData; doUpload = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; FaceFrameResult frameResult = null; SingleFaceProcessor faceProcessor = new SingleFaceProcessor(sensor); faceProcessor.FaceResultAcquired += (sender, args) => { frameResult = args.FrameResult; }; Func <PointF, Vector2> map = new Func <PointF, Vector2>((p) => { float x = p.X / 1920.0f * (float)swapChain.Width; float y = p.Y / 1080.0f * (float)swapChain.Height; return(new Vector2(x, y)); }); Func <float, float, Vector2> mapxy = new Func <float, float, Vector2>((px, py) => { float x = px / 1920.0f * (float)swapChain.Width; float y = py / 1080.0f * (float)swapChain.Height; return(new Vector2(x, y)); }); bodyProvider.FrameReceived += (sender, args) => { bodyFrame = args.FrameData; var body = bodyFrame.TrackedOnly().ClosestBodies().FirstOrDefault(); if (body != null) { faceProcessor.AssignBody(body); } else { faceProcessor.Suspend(); } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (doUpload) { colorTexture.Copy(context, currentData); } context.RenderTargetStack.Push(swapChain); device.Primitives.ApplyFullTri(context, colorTexture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Pop(); if (frameResult != null) { context2d.BeginDraw(); var colorBound = frameResult.FaceBoundingBoxInColorSpace; RectangleF rect = new RectangleF(); Vector2 topLeft = mapxy(colorBound.Left, colorBound.Top); Vector2 bottomRight = mapxy(colorBound.Right, colorBound.Bottom); rect.Top = topLeft.Y; rect.Bottom = bottomRight.Y; rect.Left = topLeft.X; rect.Right = bottomRight.X; context2d.DrawRectangle(rect, whiteBrush, 3.0f); foreach (PointF point in frameResult.FacePointsInColorSpace.Values) { var ellipse = new SharpDX.Direct2D1.Ellipse() { Point = map(point), RadiusX = 5, RadiusY = 5 }; context2d.FillEllipse(ellipse, whiteBrush); } context2d.EndDraw(); } swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); colorTexture.Dispose(); provider.Dispose(); bodyProvider.Dispose(); faceProcessor.Dispose(); whiteBrush.Dispose(); context2d.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect color sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; bool doUpload = false; InfraredFrameData currentData = null; DynamicInfraredTexture irTexture = new DynamicInfraredTexture(device); KinectSensorInfraredFrameProvider provider = new KinectSensorInfraredFrameProvider(sensor); provider.FrameReceived += (sender, args) => { currentData = args.FrameData; doUpload = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (doUpload) { irTexture.Copy(context, currentData); } context.RenderTargetStack.Push(swapChain); device.Primitives.ApplyFullTri(context, irTexture.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); irTexture.Dispose(); provider.Dispose(); sensor.Close(); }
public void Update(DX11RenderContext context) { Device device = context.Device; if (this.updateddevices.Contains(context)) { return; } int samplecount = Convert.ToInt32(FInAASamplesPerPixel[0].Name); SampleDescription sd = new SampleDescription(samplecount, 0); if (this.FResized || this.FInvalidateSwapChain || this.swapchain == null) { List <SampleDescription> sds = context.GetMultisampleFormatInfo(Format.R8G8B8A8_UNorm); int maxlevels = sds[sds.Count - 1].Count; if (sd.Count > maxlevels) { logger.Log(LogType.Warning, "Multisample count too high for this format, reverted to: " + maxlevels); sd.Count = maxlevels; } if (this.swapchain != null) { this.swapchain.Dispose(); } this.swapchain = new DX11SwapChain(context, this.form.Handle, Format.R8G8B8A8_UNorm, sd, this.FInRate[0], 2, this.FInFlipSequential[0]); this.FInvalidateSwapChain = false; } if (this.renderer == null) { this.renderer = new DX11GraphicsRenderer(context); } bool fs = this.swapchain.IsFullScreen; if (this.FInFullScreen[0] != fs) { if (this.FInFullScreen[0]) { this.prevx = this.form.Width; this.prevy = this.form.Height; /*Screen screen = Screen.FromControl(this.form);*/ this.form.FormBorderStyle = FormBorderStyle.None; this.form.Width = Convert.ToInt32(this.FInRes[0].X); this.form.Height = Convert.ToInt32(this.FInRes[0].Y); this.depthmanager.NeedReset = true; this.swapchain.Resize(); this.swapchain.SetFullScreen(true); } else { this.swapchain.SetFullScreen(false); this.form.FormBorderStyle = FormBorderStyle.Fixed3D; this.form.Width = this.prevx; this.form.Height = this.prevy; this.swapchain.Resize(); } } this.depthmanager.Update(context, this.swapchain.Width, this.swapchain.Height, sd); this.updateddevices.Add(context); this.FOutIsFullScreen[0] = fs; }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect depth local stream sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); PixelShader pixelShaderRaw = ShaderCompiler.CompileFromFile <PixelShader>(device, "DepthToWorld.fx", "PS_Raw"); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); KinectFrameServer frameServer = new KinectFrameServer(32000, sensor); KinectFrameClient frameClient = new KinectFrameClient(IPAddress.Parse("127.0.0.1"), 32000); RayTableTexture rayTable = RayTableTexture.FromCoordinateMapper(device, sensor.CoordinateMapper); RenderCameraTexture renderCamera = new RenderCameraTexture(device); frameClient.Connect(); bool doQuit = false; bool uploadDepth = false; bool uploadBody = false; int mode = 0; //0 = body index, 1 = depth, 2 = world DepthFrameData depthData = null; DynamicDepthTexture depth = new DynamicDepthTexture(device); IDepthFrameProvider networkDepth = (IDepthFrameProvider)frameClient; networkDepth.FrameReceived += (sender, args) => { depthData = args.DepthData; uploadDepth = true; }; BodyIndexFrameData bodyIndexData = null; DynamicBodyIndexTexture bodyIndexTexture = new DynamicBodyIndexTexture(device); IBodyIndexFrameProvider networkBody = (IBodyIndexFrameProvider)frameClient; networkBody.FrameReceived += (sender, args) => { bodyIndexData = args.FrameData; uploadBody = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } if (args.KeyCode == Keys.Space) { mode++; if (mode > 2) { mode = 0; } } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (uploadDepth) { depth.Copy(context, depthData); uploadDepth = false; if (mode == 2) { //Convert depth to world context.Context.OutputMerger.SetRenderTargets(renderCamera.RenderView); device.Primitives.ApplyFullTriVS(context); context.Context.PixelShader.Set(pixelShaderRaw); context.Context.PixelShader.SetShaderResource(0, depth.RawView); context.Context.PixelShader.SetShaderResource(1, rayTable.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Apply(); } } if (uploadBody) { bodyIndexTexture.Copy(context, bodyIndexData); uploadBody = false; } context.RenderTargetStack.Push(swapChain); if (mode == 0) { device.Primitives.ApplyFullTri(context, bodyIndexTexture.NormalizedView); } else if (mode == 1) { device.Primitives.ApplyFullTri(context, depth.NormalizedView); } else { device.Primitives.ApplyFullTri(context, renderCamera.ShaderView); } device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); depth.Dispose(); bodyIndexTexture.Dispose(); frameClient.Stop(); frameServer.Dispose(); rayTable.Dispose(); renderCamera.Dispose(); pixelShaderRaw.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect background subtraction sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); PixelShader depthPixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "FilterDepthView.fx", "PS_Sample"); PixelShader rgbPixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "FilterRGBView.fx", "PS_Sample"); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); bool doQuit = false; bool swapMode = false; bool uploadColor = false; bool uploadBodyIndex = false; //We need color and body index for subtraction ColorRGBAFrameData colorData = null; DynamicColorRGBATexture colorTexture = new DynamicColorRGBATexture(device); KinectSensorColorRGBAFrameProvider colorProvider = new KinectSensorColorRGBAFrameProvider(sensor); colorProvider.FrameReceived += (sender, args) => { colorData = args.FrameData; uploadColor = true; }; BodyIndexFrameData bodyIndexData = null; DynamicBodyIndexTexture bodyIndexTexture = new DynamicBodyIndexTexture(device); KinectSensorBodyIndexFrameProvider bodyIndexProvider = new KinectSensorBodyIndexFrameProvider(sensor); bodyIndexProvider.FrameReceived += (sender, args) => { bodyIndexData = args.FrameData; uploadBodyIndex = true; }; bool uploadColorToDepth = false; bool uploadDepthToColor = false; ColorToDepthFrameData colorToDepthData = new ColorToDepthFrameData(); DepthToColorFrameData depthToColorData = new DepthToColorFrameData(); KinectSensorDepthFrameProvider depthProvider = new KinectSensorDepthFrameProvider(sensor); depthProvider.FrameReceived += (sender, args) => { if (!swapMode) { colorToDepthData.Update(sensor.CoordinateMapper, args.DepthData); uploadColorToDepth = true; } else { depthToColorData.Update(sensor.CoordinateMapper, args.DepthData); uploadDepthToColor = true; } }; DynamicColorToDepthTexture colorToDepthTexture = new DynamicColorToDepthTexture(device); DynamicDepthToColorTexture depthToColorTexture = new DynamicDepthToColorTexture(device); form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } if (args.KeyCode == Keys.Space) { swapMode = !swapMode; } }; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (uploadColor) { colorTexture.Copy(context, colorData); uploadColor = false; } if (uploadBodyIndex) { bodyIndexTexture.Copy(context, bodyIndexData); uploadBodyIndex = false; } if (uploadColorToDepth) { colorToDepthTexture.Copy(context, colorToDepthData); uploadColorToDepth = false; } if (uploadDepthToColor) { depthToColorTexture.Copy(context, depthToColorData); uploadDepthToColor = false; } ShaderResourceView view = swapMode ? depthToColorTexture.ShaderView : colorToDepthTexture.ShaderView; PixelShader shader = swapMode ? depthPixelShader : rgbPixelShader; context.RenderTargetStack.Push(swapChain); device.Primitives.ApplyFullTriVS(context); context.Context.PixelShader.Set(shader); context.Context.PixelShader.SetShaderResource(0, colorTexture.ShaderView); //Note: make sure to use normalized view as we use untyped resource here context.Context.PixelShader.SetShaderResource(1, bodyIndexTexture.NormalizedView); context.Context.PixelShader.SetShaderResource(2, view); context.Context.PixelShader.SetSampler(0, device.SamplerStates.LinearClamp); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); depthProvider.Dispose(); colorToDepthData.Dispose(); depthToColorData.Dispose(); colorToDepthTexture.Dispose(); depthToColorTexture.Dispose(); colorTexture.Dispose(); colorProvider.Dispose(); bodyIndexData.Dispose(); bodyIndexProvider.Dispose(); depthPixelShader.Dispose(); rgbPixelShader.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm("Kinect Camera Joint sample"); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); DX11DepthStencil depthStencil = new DX11DepthStencil(device, swapChain.Width, swapChain.Height, eDepthFormat.d24s8); //VertexShader vertexShader = ShaderCompiler.CompileFromFile<VertexShader>(device, "ColorJointView.fx", "VS"); SharpDX.D3DCompiler.ShaderSignature signature; VertexShader vertexShader = ShaderCompiler.CompileFromFile(device, "CameraJointView.fx", "VS_Color", out signature); PixelShader pixelShader = ShaderCompiler.CompileFromFile <PixelShader>(device, "CameraJointView.fx", "PS_Color"); VertexShader vertexShaderLine = ShaderCompiler.CompileFromFile <VertexShader>(device, "CameraJointView.fx", "VS"); PixelShader pixelShaderLine = ShaderCompiler.CompileFromFile <PixelShader>(device, "CameraJointView.fx", "PS_White"); JointTableIndexBuffer indexBuffer = new JointTableIndexBuffer(device, 6); DX11IndexedGeometry cube = device.Primitives.Box(new Box() { Size = new Vector3(0.05f) }); DX11InstancedIndexedDrawer drawer = new DX11InstancedIndexedDrawer(); cube.AssignDrawer(drawer); InputLayout layout; var bc = new ShaderBytecode(signature); cube.ValidateLayout(bc, out layout); KinectSensor sensor = KinectSensor.GetDefault(); sensor.Open(); Color4[] statusColor = new Color4[] { Color.Red, Color.Yellow, Color.Green }; cbCamera camera = new cbCamera(); camera.Projection = Matrix.PerspectiveFovLH(1.57f, 1.3f, 0.1f, 100.0f); camera.View = Matrix.Translation(0.0f, 0.0f, 2.0f); camera.Projection.Transpose(); camera.View.Transpose(); ConstantBuffer <cbCamera> cameraBuffer = new ConstantBuffer <cbCamera>(device); cameraBuffer.Update(context, ref camera); DX11StructuredBuffer colorTableBuffer = DX11StructuredBuffer.CreateImmutable <Color4>(device, statusColor); bool doQuit = false; bool doUpload = false; int bodyCount = 0; KinectBody[] bodyFrame = null; BodyCameraPositionBuffer positionBuffer = new BodyCameraPositionBuffer(device); BodyJointStatusBuffer statusBuffer = new BodyJointStatusBuffer(device); KinectSensorBodyFrameProvider provider = new KinectSensorBodyFrameProvider(sensor); provider.FrameReceived += (sender, args) => { bodyFrame = args.FrameData; doUpload = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } }; context.Context.OutputMerger.DepthStencilState = device.DepthStencilStates.LessReadWrite; context.Context.Rasterizer.State = device.RasterizerStates.BackCullSolid; RenderLoop.Run(form, () => { if (doQuit) { form.Dispose(); return; } if (doUpload) { var tracked = bodyFrame.TrackedOnly(); bodyCount = tracked.Count(); positionBuffer.Copy(context, tracked); statusBuffer.Copy(context, tracked); drawer.InstanceCount = tracked.Count() * Microsoft.Kinect.Body.JointCount; } context.RenderTargetStack.Push(depthStencil, false, swapChain); context.Context.ClearRenderTargetView(swapChain.RenderView, SharpDX.Color.Black); depthStencil.Clear(context); /*Position buffer and cbuffers are the same data and in same slot, * so we bind them only once*/ context.Context.VertexShader.SetShaderResource(0, positionBuffer.ShaderView); context.Context.VertexShader.SetConstantBuffer(0, cameraBuffer.Buffer); //Draw lines context.Context.PixelShader.Set(pixelShaderLine); context.Context.VertexShader.Set(vertexShaderLine); //Attach index buffer, null topology since we fetch indexBuffer.AttachWithLayout(context); indexBuffer.Draw(context, bodyCount); //Draw cubes cube.Bind(context, layout); context.Context.VertexShader.Set(vertexShader); context.Context.PixelShader.Set(pixelShader); context.Context.VertexShader.SetShaderResource(1, statusBuffer.ShaderView); context.Context.VertexShader.SetShaderResource(2, colorTableBuffer.ShaderView); cube.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); depthStencil.Dispose(); context.Dispose(); device.Dispose(); positionBuffer.Dispose(); statusBuffer.Dispose(); colorTableBuffer.Dispose(); cameraBuffer.Dispose(); provider.Dispose(); cube.Dispose(); layout.Dispose(); pixelShader.Dispose(); vertexShader.Dispose(); pixelShaderLine.Dispose(); vertexShaderLine.Dispose(); indexBuffer.Dispose(); sensor.Close(); }
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); RenderForm form = new RenderForm(header); RenderDevice device = new RenderDevice(SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport); RenderContext context = new RenderContext(device); DX11SwapChain swapChain = DX11SwapChain.FromHandle(device, form.Handle); PixelShader pixelShaderRaw = ShaderCompiler.CompileFromFile <PixelShader>(device, "DepthToWorld.fx", "PS_Raw"); PixelShader pixelShaderNorm = ShaderCompiler.CompileFromFile <PixelShader>(device, "DepthToWorld.fx", "PS_Normalized"); RayTableTexture rayTable = null; KinectSensor sensor = KinectSensor.GetDefault(); sensor.CoordinateMapper.CoordinateMappingChanged += (s, e) => { if (rayTable == null) { rayTable = RayTableTexture.FromCoordinateMapper(device, sensor.CoordinateMapper); } }; sensor.Open(); RenderCameraTexture renderCamera = new RenderCameraTexture(device); bool doQuit = false; bool doUpload = false; bool useRaw = true; DepthFrameData currentData = null; DynamicDepthTexture depth = new DynamicDepthTexture(device); KinectSensorDepthFrameProvider provider = new KinectSensorDepthFrameProvider(sensor); provider.FrameReceived += (sender, args) => { currentData = args.DepthData; doUpload = true; }; form.KeyDown += (sender, args) => { if (args.KeyCode == Keys.Escape) { doQuit = true; } if (args.KeyCode == Keys.Space) { useRaw = !useRaw; } }; RenderLoop.Run(form, () => { form.Text = header + (useRaw ? " - Raw Mode": " - Normalized Mode"); if (doQuit) { form.Dispose(); return; } if (doUpload && rayTable != null) { depth.Copy(context, currentData); //Convert depth to world context.Context.OutputMerger.SetRenderTargets(renderCamera.RenderView); device.Primitives.ApplyFullTriVS(context); if (useRaw) { context.Context.PixelShader.Set(pixelShaderRaw); context.Context.PixelShader.SetShaderResource(0, depth.RawView); } else { context.Context.PixelShader.Set(pixelShaderNorm); context.Context.PixelShader.SetShaderResource(0, depth.NormalizedView); } context.Context.PixelShader.SetShaderResource(1, rayTable.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Apply(); } context.RenderTargetStack.Push(swapChain); device.Primitives.ApplyFullTri(context, renderCamera.ShaderView); device.Primitives.FullScreenTriangle.Draw(context); context.RenderTargetStack.Pop(); swapChain.Present(0, SharpDX.DXGI.PresentFlags.None); }); swapChain.Dispose(); context.Dispose(); device.Dispose(); depth.Dispose(); renderCamera.Dispose(); if (rayTable != null) { rayTable.Dispose(); } provider.Dispose(); pixelShaderNorm.Dispose(); pixelShaderRaw.Dispose(); sensor.Close(); }