private void Render() { _target.ClearAll(); _modelPipeline.Apply(); _camera.Step(); _vsConstant.Value = _camera.GetViewProjectionMatrix(); _vsConstant.Update(); for (int pass = 0; pass < 5; ++pass) { _gsConstant.Value.EdgeIndex = (uint)pass; _gsConstant.Update(); for (int i = 0; i < Model.PartCount; ++i) { Model.GetPart(i).DrawAll(); } } { var point2D = _camera.WorldPosToControl(new Vector3(-20, -25, 45)); _spriteDebug.Apply(); _spriteDebug.DrawString(_spriteFont, "X", point2D.X, point2D.Y, 1000); } _device.Present(true); }
public void Render() { _psConstants.Update(); _target.Apply(); _pipeline.Apply(); _vertexBuffer.DrawAll(); }
private int DoRenderPass( RenderContext context, CommandList commandList, RenderBucket bucket, BoundingFrustum cameraFrustum, ResourceSet cloudResourceSet) { // TODO: Make culling batch size configurable at runtime bucket.RenderItems.CullAndSort(cameraFrustum, ParallelCullingBatchSize); if (bucket.RenderItems.CulledItemIndices.Count == 0) { return(0); } Matrix4x4?lastWorld = null; int? lastRenderItemIndex = null; foreach (var i in bucket.RenderItems.CulledItemIndices) { ref var renderItem = ref bucket.RenderItems[i]; if (lastRenderItemIndex == null || bucket.RenderItems[lastRenderItemIndex.Value].Pipeline != renderItem.Pipeline) { commandList.InsertDebugMarker("Setting pipeline"); commandList.SetPipeline(renderItem.Pipeline); SetGlobalResources(commandList, renderItem.ShaderSet.GlobalResourceSetIndices, cloudResourceSet); } if (renderItem.ShaderSet.GlobalResourceSetIndices.RenderItemConstants != null) { if (lastWorld == null || lastWorld.Value != renderItem.World) { _renderItemConstantsBufferVS.Value.World = renderItem.World; _renderItemConstantsBufferVS.Update(commandList); lastWorld = renderItem.World; } if (renderItem.RenderItemConstantsPS != null) { _renderItemConstantsBufferPS.Value = renderItem.RenderItemConstantsPS.Value; _renderItemConstantsBufferPS.Update(commandList); } } renderItem.BeforeRenderCallback.Invoke(commandList, context); commandList.SetIndexBuffer(renderItem.IndexBuffer, IndexFormat.UInt16); commandList.DrawIndexed( renderItem.IndexCount, 1, renderItem.StartIndex, 0, 0); lastRenderItemIndex = i; }
public void Apply() { if (this._effect.OnApply()) { this._effect.CurrentTechnique.Passes[0].Apply(); } else { GraphicsDevice graphicsDevice = this._effect.GraphicsDevice; if (this._vertexShader != null) { graphicsDevice.VertexShader = this._vertexShader; for (int slot = 0; slot < this._vertexShader.CBuffers.Length; ++slot) { ConstantBuffer buffer = this._effect.ConstantBuffers[this._vertexShader.CBuffers[slot]]; buffer.Update(this._effect.Parameters); graphicsDevice.SetConstantBuffer(ShaderStage.Vertex, slot, buffer); } } if (this._pixelShader != null) { graphicsDevice.PixelShader = this._pixelShader; foreach (SamplerInfo samplerInfo in this._pixelShader.Samplers) { Texture texture = this._effect.Parameters[samplerInfo.parameter].Data as Texture; if (texture != null) { graphicsDevice.Textures[samplerInfo.textureSlot] = texture; if (samplerInfo.state != null) { graphicsDevice.SamplerStates[samplerInfo.samplerSlot] = samplerInfo.state; } } } for (int slot = 0; slot < this._pixelShader.CBuffers.Length; ++slot) { ConstantBuffer buffer = this._effect.ConstantBuffers[this._pixelShader.CBuffers[slot]]; buffer.Update(this._effect.Parameters); graphicsDevice.SetConstantBuffer(ShaderStage.Pixel, slot, buffer); } } if (this._rasterizerState != null) { graphicsDevice.RasterizerState = this._rasterizerState; } if (this._blendState != null) { graphicsDevice.BlendState = this._blendState; } if (this._depthStencilState == null) { return; } graphicsDevice.DepthStencilState = this._depthStencilState; } }
public void Update(double delta) { if (_iterations > 0) { _context.DirectX.DeviceContext.PixelShader.SetShaderResource(0, null); _context.DirectX.DeviceContext.VertexShader.SetShaderResource(0, null); _timer.Tick(); for (int i = 0; i < 14 && _iterations > 0; i++) { Vector3 normal = new Vector3((float)RandomGenerator.GetDouble(-1, 1), (float)RandomGenerator.GetDouble(-1, 1), (float)RandomGenerator.GetDouble(-1, 1)); Vector4 data = new Vector4(Vector3.Normalize(normal), (float)RandomGenerator.GetDouble(-1, 1)); foreach (Face face in _faces) { _planeBuffer.Update(new PlaneData(data, Matrix.Transpose(face.Transform))); _context.DirectX.DeviceContext.ComputeShader.Set(_baseTerrainGeneration); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(0, face.TerrainUav); _context.DirectX.DeviceContext.ComputeShader.SetConstantBuffer(0, _planeBuffer.Buffer); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize - 1, TextureSize / BatchSize - 1, 1); } _iterations--; } _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(0, null); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(1, null); } else if (_iterations > -5000) { _context.DirectX.DeviceContext.PixelShader.SetShaderResource(0, null); _context.DirectX.DeviceContext.VertexShader.SetShaderResource(0, null); _context.DirectX.DeviceContext.PixelShader.SetShaderResource(1, null); _context.DirectX.DeviceContext.VertexShader.SetShaderResource(1, null); _timer.Tick(); for (int i = 0; i < 1 && _iterations > -5000; i++) { foreach (Face face in _faces) { _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(0, face.TerrainUav); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(1, face.WaterUav); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(2, face.FlowsLeftUav); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(3, face.FlowsTopUav); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(4, face.FlowsRightUav); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(5, face.FlowsBottomUav); _context.DirectX.DeviceContext.ComputeShader.Set(_flowsCalculation); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize - 1, TextureSize / BatchSize - 1, 1); _context.DirectX.DeviceContext.ComputeShader.Set(_updateWaterLevel); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize - 1, TextureSize / BatchSize - 1, 1); } _iterations--; } _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(0, null); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(1, null); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(2, null); } }
internal void UpdateConstants(ref DeviceContext context) { var cb = Constants; CFrame.GetViewMatrix(out cb.Data.ViewMatrix); Matrix.Multiply(ref cb.Data.ViewMatrix, ref _projectionMatrix, out _viewProjectionMatrix); cb.Data.ViewProjectionMatrix = _viewProjectionMatrix; Matrix.Invert(ref cb.Data.ViewProjectionMatrix, out cb.Data.InverseViewProjection); cb.Data.Position = CFrame.p; Constants.Update(ref context); }
protected override void OnExport(object Unknown = null) { passObject.world = Transform; passObjectView.Update(ref passObject); GraphicsPipeline.InputSlot[0] = CameraBuffer.View; GraphicsPipeline.InputSlot[1] = passObjectView; GraphicsPipeline.InputAssemblerStage.VertexBuffer = vertexBuffer; GraphicsPipeline.InputAssemblerStage.IndexBuffer = indexBuffer; GraphicsPipeline.InputAssemblerStage.PrimitiveType = PrimitiveType.TriangleList; GraphicsPipeline.PutObjectIndexed(indexBuffer.Count); base.OnExport(Unknown); }
protected override void OnExport(object Unknown = null) { cubeBuffer.world = Transform; cubeBufferUploader.Update(ref cubeBuffer); GraphicsPipeline.InputSlot[0] = Resource.CameraBuffer; GraphicsPipeline.InputSlot[1] = cubeBufferUploader; GraphicsPipeline.InputAssemblerStage.VertexBuffer = vertexBuffer; GraphicsPipeline.InputAssemblerStage.IndexBuffer = indexBuffer; GraphicsPipeline.InputAssemblerStage.PrimitiveType = PrimitiveType.TriangleList; GraphicsPipeline.Reset(graphicsPipelineState); GraphicsPipeline.PutObjectIndexed(indexBuffer.Count); }
private void SetGlobalLightingBufferVS(GraphicsDevice graphicsDevice) { var cloudShadowView = Matrix4x4.CreateLookAt( Vector3.Zero, Vector3.Normalize(new Vector3(0, 0.2f, -1)), Vector3.UnitY); var cloudShadowProjection = Matrix4x4.CreateOrthographic(1, 1, 0, 1); var lightingConstantsVS = new GlobalShaderResources.LightingConstantsVS { CloudShadowMatrix = cloudShadowView * cloudShadowProjection }; _globalLightingBufferVS.Value = lightingConstantsVS; _globalLightingBufferVS.Update(graphicsDevice); }
public void Initialize(ContentManager contentManager, ImageMap map) { _map = map; _usedTextures = new Dictionary <int, Texture>(); //get all used image id's var usedIds = new HashSet <int>(); foreach (var entry in Shape.Entries) { if (entry is GeometryTexturedTriangles gtt) { var assignment = _map.Mapping[gtt.Image]; usedIds.Add(assignment.TextureId); } } foreach (var id in usedIds) { var texturePath = "art/Textures/apt_" + MovieName + "_" + id.ToString() + ".tga"; var loadOptions = new TextureLoadOptions() { GenerateMipMaps = false }; _usedTextures.Add(id, contentManager.Load <Texture>(texturePath, loadOptions)); } Material = new SpriteMaterial(contentManager.EffectLibrary.Sprite); _materialConstantsBuffer = new ConstantBuffer <SpriteMaterial.MaterialConstants>(contentManager.GraphicsDevice); _materialConstantsBuffer.Value.Opacity = 1; _materialConstantsBuffer.Update(); Material.SetMaterialConstants(_materialConstantsBuffer.Buffer); }
private void UpdateSun() { var sunAngles = CalculateSunPosition(DateTime.Today() + _timeOfDay, _longitude, _latitude); var sunCF = CFrame.FromAxisAngle(Vector3.Up, (float)sunAngles.Azimuth) * CFrame.FromAxisAngle(Vector3.Right, (float)sunAngles.Altitude); var sunVector = sunCF.forward; SunVector = sunVector; LightingConstantBuffer.Data.AmbientColour = _ambientColour; LightingConstantBuffer.Data.OutdoorAmbient = _outdoorAmbient; LightingConstantBuffer.Data.SunLight = new PointLightData { Position = sunVector, Colour = SunColour }; lock (Renderer.Locker) { LightingConstantBuffer.Update(ref Renderer.Context); } Shadows.Direction = new SharpDX.Vector3(-sunVector.x, sunVector.y, -sunVector.z); }
private void DoRenderPass( CommandList commandList, RenderBucket bucket, BoundingFrustum cameraFrustum, Texture cloudTexture, Texture shadowMap) { // TODO: Make culling batch size configurable at runtime bucket.RenderItems.CullAndSort(cameraFrustum, ParallelCullingBatchSize); if (bucket.RenderItems.CulledItemIndices.Count == 0) { return; } Matrix4x4?lastWorld = null; int? lastRenderItemIndex = null; foreach (var i in bucket.RenderItems.CulledItemIndices) { ref var renderItem = ref bucket.RenderItems[i]; if (lastRenderItemIndex == null || bucket.RenderItems[lastRenderItemIndex.Value].Effect != renderItem.Effect) { var effect = renderItem.Effect; effect.Begin(commandList); SetDefaultMaterialProperties(renderItem.Material, cloudTexture, shadowMap); } if (lastRenderItemIndex == null || bucket.RenderItems[lastRenderItemIndex.Value].Material != renderItem.Material) { renderItem.Material.ApplyPipelineState(); renderItem.Effect.ApplyPipelineState(commandList); } if (lastRenderItemIndex == null || bucket.RenderItems[lastRenderItemIndex.Value].VertexBuffer0 != renderItem.VertexBuffer0) { commandList.SetVertexBuffer(0, renderItem.VertexBuffer0); } if (lastRenderItemIndex == null || bucket.RenderItems[lastRenderItemIndex.Value].VertexBuffer1 != renderItem.VertexBuffer1) { if (renderItem.VertexBuffer1 != null) { commandList.SetVertexBuffer(1, renderItem.VertexBuffer1); } } var renderItemConstantsVSParameter = renderItem.Effect.GetParameter("RenderItemConstantsVS", throwIfMissing: false); if (renderItemConstantsVSParameter != null) { if (lastWorld == null || lastWorld.Value != renderItem.World) { _renderItemConstantsBufferVS.Value.World = renderItem.World; _renderItemConstantsBufferVS.Update(commandList); lastWorld = renderItem.World; } renderItem.Material.SetProperty( "RenderItemConstantsVS", _renderItemConstantsBufferVS.Buffer); } if (renderItem.HouseColor != null) { var renderItemConstantsPSParameter = renderItem.Effect.GetParameter("RenderItemConstantsPS", throwIfMissing: false); if (renderItemConstantsPSParameter != null) { _renderItemConstantsBufferPS.Value.HouseColor = renderItem.HouseColor.Value.ToVector3(); _renderItemConstantsBufferPS.Update(commandList); renderItem.Material.SetProperty( "RenderItemConstantsPS", _renderItemConstantsBufferPS.Buffer); } } renderItem.Material.ApplyProperties(); renderItem.Effect.ApplyParameters(commandList); switch (renderItem.DrawCommand) { case DrawCommand.Draw: commandList.Draw( renderItem.VertexCount, 1, renderItem.VertexStart, 0); break; case DrawCommand.DrawIndexed: commandList.SetIndexBuffer(renderItem.IndexBuffer, IndexFormat.UInt16); commandList.DrawIndexed( renderItem.IndexCount, 1, renderItem.StartIndex, 0, 0); break; default: throw new System.Exception(); } lastRenderItemIndex = i; }
internal Terrain(MapFile mapFile, AssetLoadContext loadContext) { Map = mapFile; HeightMap = new HeightMap(mapFile.HeightMapData); _graphicsDevice = loadContext.GraphicsDevice; var indexBufferCache = AddDisposable(new TerrainPatchIndexBufferCache(loadContext.GraphicsDevice)); var tileDataTexture = AddDisposable(CreateTileDataTexture( loadContext.GraphicsDevice, mapFile, HeightMap)); var cliffDetailsBuffer = AddDisposable(CreateCliffDetails( loadContext.GraphicsDevice, mapFile)); CreateTextures( loadContext, mapFile.BlendTileData, out var textureArray, out var textureDetails); var textureDetailsBuffer = AddDisposable(loadContext.GraphicsDevice.CreateStaticStructuredBuffer(textureDetails)); var terrainPipeline = loadContext.ShaderResources.Terrain.Pipeline; _materialConstantsBuffer = AddDisposable( new ConstantBuffer <TerrainShaderResources.TerrainMaterialConstants>( loadContext.GraphicsDevice, "TerrainMaterialConstants")); _materialConstantsBuffer.Value = new TerrainShaderResources.TerrainMaterialConstants { MapBorderWidth = new Vector2(mapFile.HeightMapData.BorderWidth, mapFile.HeightMapData.BorderWidth) * HeightMap.HorizontalScale, MapSize = new Vector2(mapFile.HeightMapData.Width, mapFile.HeightMapData.Height) * HeightMap.HorizontalScale, IsMacroTextureStretched = mapFile.EnvironmentData?.IsMacroTextureStretched ?? false }; _materialConstantsBuffer.Update(loadContext.GraphicsDevice); var macroTexture = loadContext.AssetStore.Textures.GetByName(mapFile.EnvironmentData?.MacroTexture ?? "tsnoiseurb.dds"); RadiusCursorDecals = AddDisposable(new RadiusCursorDecals(loadContext.AssetStore, loadContext.GraphicsDevice)); var casuticsTextures = BuildCausticsTextureArray(loadContext.AssetStore); var materialResourceSet = AddDisposable(loadContext.ShaderResources.Terrain.CreateMaterialResourceSet( _materialConstantsBuffer.Buffer, tileDataTexture, cliffDetailsBuffer ?? loadContext.StandardGraphicsResources.GetNullStructuredBuffer(TerrainShaderResources.CliffInfo.Size), textureDetailsBuffer, textureArray, macroTexture, casuticsTextures)); RadiusCursorDecalsResourceSet = AddDisposable(loadContext.ShaderResources.RadiusCursor.CreateRadiusCursorDecalsResourceSet( RadiusCursorDecals.TextureArray, RadiusCursorDecals.DecalConstants, RadiusCursorDecals.DecalsBuffer)); Patches = CreatePatches( loadContext.GraphicsDevice, HeightMap, indexBufferCache, materialResourceSet, RadiusCursorDecalsResourceSet); var cloudTexture = loadContext.AssetStore.Textures.GetByName(mapFile.EnvironmentData?.CloudTexture ?? "tscloudmed.dds"); var cloudResourceLayout = AddDisposable(loadContext.GraphicsDevice.ResourceFactory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Global_CloudTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment)))); CloudResourceSet = AddDisposable(loadContext.GraphicsDevice.ResourceFactory.CreateResourceSet( new ResourceSetDescription( cloudResourceLayout, cloudTexture.Texture))); CloudResourceSet.Name = "Cloud resource set"; _shaderSet = loadContext.ShaderResources.Terrain.ShaderSet; _pipeline = terrainPipeline; }
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(); }
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 RenderShadowMap( Scene3D scene, GraphicsDevice graphicsDevice, CommandList commandList, Action <Framebuffer, BoundingFrustum> drawSceneCallback) { // TODO: Use terrain light for terrain self-shadowing? var light = scene.Lighting.CurrentLightingConfiguration.LightsPS.Object.Light0; // Calculate size of shadow map. var shadowMapSize = scene.Shadows.ShadowMapSize; var numCascades = (uint)scene.Shadows.ShadowMapCascades; if (_shadowData != null && _shadowData.ShadowMap != null && (_shadowData.ShadowMap.Width != shadowMapSize || _shadowData.ShadowMap.Height != shadowMapSize || _shadowData.NearPlaneDistance != scene.Camera.NearPlaneDistance || _shadowData.FarPlaneDistance != scene.Camera.FarPlaneDistance || _shadowData.NumSplits != numCascades)) { RemoveAndDispose(ref _shadowData); } if (_shadowData == null) { _shadowData = AddDisposable(new ShadowData( numCascades, scene.Camera.NearPlaneDistance, scene.Camera.FarPlaneDistance, shadowMapSize, graphicsDevice)); } if (scene.Shadows.ShadowsType != ShadowsType.None) { _shadowFrustumCalculator.CalculateShadowData( light, scene.Camera, _shadowData, scene.Shadows); _shadowConstants.Set(numCascades, scene.Shadows, _shadowData); // Render scene geometry to each split of the cascade. for (var splitIndex = 0; splitIndex < _shadowData.NumSplits; splitIndex++) { _lightFrustum.Matrix = _shadowData.ShadowCameraViewProjections[splitIndex]; drawSceneCallback( _shadowData.ShadowMapFramebuffers[splitIndex], _lightFrustum); } } else { _shadowConstants.ShadowsType = ShadowsType.None; } _shadowConstantsPSBuffer.Value = _shadowConstants; _shadowConstantsPSBuffer.Update(commandList); }
private void GenerateHeightMaps() { ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "initTerrain", "cs_5_0", Shader.ShaderFlags); ComputeShader initTerrain = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "initWater", "cs_5_0", Shader.ShaderFlags); ComputeShader initWater = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "applyRandomDisplacement", "cs_5_0", Shader.ShaderFlags); _baseTerrainGeneration = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "flowsCalculation", "cs_5_0", Shader.ShaderFlags); _flowsCalculation = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "updateWaterLevel", "cs_5_0", Shader.ShaderFlags); _updateWaterLevel = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); Texture2DDescription textureDescription = new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R32_Float, Height = TextureSize, Width = TextureSize, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; ConstantBuffer <ComputeData> computeBuffer = new ConstantBuffer <ComputeData>(_context); _context.DirectX.DeviceContext.ComputeShader.SetConstantBuffer(1, computeBuffer.Buffer); foreach (Face face in _faces) { Texture2D terrainTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.TerrainSrv = new ShaderResourceView(_context.DirectX.Device, terrainTexture); face.TerrainUav = new UnorderedAccessView(_context.DirectX.Device, terrainTexture); terrainTexture.Dispose(); Texture2D waterTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.WaterSrv = new ShaderResourceView(_context.DirectX.Device, waterTexture); face.WaterUav = new UnorderedAccessView(_context.DirectX.Device, waterTexture); waterTexture.Dispose(); Texture2D flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsLeftUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsTopUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsRightUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsBottomUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(0, face.TerrainUav); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(1, face.WaterUav); _context.DirectX.DeviceContext.ComputeShader.Set(initTerrain); computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, 0, 0, 0.0f)); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize, TextureSize / BatchSize, 1); _context.DirectX.DeviceContext.ComputeShader.Set(initWater); computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, 0, 0, 0.05f)); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize, TextureSize / BatchSize, 1); _context.DirectX.DeviceContext.ComputeShader.Set(initTerrain); computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, BatchSize / 2, BatchSize / 2, 0.5f)); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize - 1, TextureSize / BatchSize - 1, 1); } _planeBuffer = new ConstantBuffer <PlaneData>(_context); initTerrain.Dispose(); computeBuffer.Dispose(); }
internal Terrain(MapFile mapFile, AssetLoadContext loadContext) { Map = mapFile; HeightMap = new HeightMap(mapFile.HeightMapData); _loadContext = loadContext; _graphicsDevice = loadContext.GraphicsDevice; _indexBufferCache = AddDisposable(new TerrainPatchIndexBufferCache(loadContext.GraphicsDevice)); var tileDataTexture = AddDisposable(CreateTileDataTexture( loadContext.GraphicsDevice, mapFile, HeightMap)); var cliffDetailsBuffer = AddDisposable(CreateCliffDetails( loadContext.GraphicsDevice, mapFile)); CreateTextures( loadContext, mapFile.BlendTileData, out var textureArray, out var textureDetails); var textureDetailsBuffer = AddDisposable(loadContext.GraphicsDevice.CreateStaticStructuredBuffer(textureDetails)); var terrainPipeline = loadContext.ShaderResources.Terrain.Pipeline; _materialConstantsBuffer = AddDisposable( new ConstantBuffer <TerrainShaderResources.TerrainMaterialConstants>( loadContext.GraphicsDevice, "TerrainMaterialConstants")); _materialConstantsBuffer.Value = new TerrainShaderResources.TerrainMaterialConstants { MapBorderWidth = new Vector2(mapFile.HeightMapData.BorderWidth, mapFile.HeightMapData.BorderWidth) * HeightMap.HorizontalScale, MapSize = new Vector2(mapFile.HeightMapData.Width, mapFile.HeightMapData.Height) * HeightMap.HorizontalScale, IsMacroTextureStretched = mapFile.EnvironmentData?.IsMacroTextureStretched ?? false }; _materialConstantsBuffer.Update(loadContext.GraphicsDevice); var macroTexture = loadContext.AssetStore.Textures.GetByName(mapFile.EnvironmentData?.MacroTexture ?? "tsnoiseurb.dds"); var casuticsTextures = BuildCausticsTextureArray(loadContext.AssetStore); var materialResourceSet = AddDisposable(loadContext.ShaderResources.Terrain.CreateMaterialResourceSet( _materialConstantsBuffer.Buffer, tileDataTexture, cliffDetailsBuffer ?? loadContext.StandardGraphicsResources.GetNullStructuredBuffer(TerrainShaderResources.CliffInfo.Size), textureDetailsBuffer, textureArray, macroTexture, casuticsTextures)); _material = AddDisposable( new Material( loadContext.ShaderResources.Terrain, loadContext.ShaderResources.Terrain.Pipeline, materialResourceSet)); CloudTexture = loadContext.AssetStore.Textures.GetByName(mapFile.EnvironmentData?.CloudTexture ?? "tscloudmed.dds"); OnHeightMapChanged(); }
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(); }
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 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(); }
private void GenerateHeightMaps() { ShaderBytecode shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "initTerrain", "cs_5_0", Shader.ShaderFlags); ComputeShader initTerrain = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "initWater", "cs_5_0", Shader.ShaderFlags); ComputeShader initWater = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "applyRandomDisplacement", "cs_5_0", Shader.ShaderFlags); _baseTerrainGeneration = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "flowsCalculation", "cs_5_0", Shader.ShaderFlags); _flowsCalculation = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); shaderByteCode = ShaderBytecode.CompileFromFile(@"Data/Shaders/TerrainCompute.hlsl", "updateWaterLevel", "cs_5_0", Shader.ShaderFlags); _updateWaterLevel = new ComputeShader(_context.DirectX.Device, shaderByteCode); shaderByteCode.Dispose(); Texture2DDescription textureDescription = new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource | BindFlags.UnorderedAccess, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R32_Float, Height = TextureSize, Width = TextureSize, MipLevels = 1, OptionFlags = ResourceOptionFlags.None, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; ConstantBuffer<ComputeData> computeBuffer = new ConstantBuffer<ComputeData>(_context); _context.DirectX.DeviceContext.ComputeShader.SetConstantBuffer(1, computeBuffer.Buffer); foreach (Face face in _faces) { Texture2D terrainTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.TerrainSrv = new ShaderResourceView(_context.DirectX.Device, terrainTexture); face.TerrainUav = new UnorderedAccessView(_context.DirectX.Device, terrainTexture); terrainTexture.Dispose(); Texture2D waterTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.WaterSrv = new ShaderResourceView(_context.DirectX.Device, waterTexture); face.WaterUav = new UnorderedAccessView(_context.DirectX.Device, waterTexture); waterTexture.Dispose(); Texture2D flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsLeftUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsTopUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsRightUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); flowsTexture = new Texture2D(_context.DirectX.Device, textureDescription); face.FlowsBottomUav = new UnorderedAccessView(_context.DirectX.Device, flowsTexture); flowsTexture.Dispose(); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(0, face.TerrainUav); _context.DirectX.DeviceContext.ComputeShader.SetUnorderedAccessView(1, face.WaterUav); _context.DirectX.DeviceContext.ComputeShader.Set(initTerrain); computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, 0, 0, 0.0f)); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize, TextureSize / BatchSize, 1); _context.DirectX.DeviceContext.ComputeShader.Set(initWater); computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, 0, 0, 0.05f)); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize, TextureSize / BatchSize, 1); _context.DirectX.DeviceContext.ComputeShader.Set(initTerrain); computeBuffer.Update(new ComputeData(TextureSize - 1 - BatchSize, BatchSize / 2, BatchSize / 2, 0.5f)); _context.DirectX.DeviceContext.Dispatch(TextureSize / BatchSize - 1, TextureSize / BatchSize - 1, 1); } _planeBuffer = new ConstantBuffer<PlaneData>(_context); initTerrain.Dispose(); computeBuffer.Dispose(); }
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 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(); }
public void RenderWaterShaders( Scene3D scene, GraphicsDevice graphicsDevice, CommandList commandList, Action <Framebuffer, Framebuffer> drawSceneCallback) { // TODO: Get bump texture from water area somehow //_bumpTexture = assetStore.Textures.GetByName(bumpTexName); var reflectionMapSize = scene.Waters.ReflectionMapSize; var refractionMapSize = scene.Waters.RefractionMapSize; _farPlaneDistance = scene.Camera.FarPlaneDistance; _nearPlaneDistance = scene.Camera.NearPlaneDistance; _isRenderReflection = scene.Waters.IsRenderReflection; _isRenderRefraction = scene.Waters.IsRenderRefraction; UpdateTimer(); CalculateUVOffset(scene.Lighting.TimeOfDay); UpdateVariableBuffers(scene.Lighting.TimeOfDay); _waterConstantsPSBuffer.Update(commandList); if (_waterData != null && _waterData.ReflectionMap != null && _waterData.RefractionMap != null && (_waterData.ReflectionMapSize != reflectionMapSize || _waterData.RefractionMapSize != refractionMapSize)) { RemoveAndDispose(ref _waterData); RemoveAndDispose(ref _resourceSet); _deltaTimer.Reset(); } if (_waterData == null) { _waterData = AddDisposable(new WaterData( reflectionMapSize, refractionMapSize, graphicsDevice)); var texture = _waterTextureSet[scene.Lighting.TimeOfDay]; _resourceSet = AddDisposable(graphicsDevice.ResourceFactory.CreateResourceSet( new ResourceSetDescription( _waterShaderResources.WaterResourceLayout, _waterConstantsPSBuffer.Buffer, texture, _bumpTexture, graphicsDevice.Aniso4xSampler, _waterData.ReflectionMap, graphicsDevice.LinearSampler, _waterData.RefractionMap, graphicsDevice.LinearSampler, _waterData.RefractionDepthMap))); } // TODO: Fix soft edge water rendering when refraction is turned off if (!_isRenderReflection && !_isRenderRefraction) { drawSceneCallback(null, null); } else if (!_isRenderReflection) { drawSceneCallback(null, RefractionFramebuffer); } else if (!_isRenderRefraction) { drawSceneCallback(ReflectionFramebuffer, null); } else { drawSceneCallback(ReflectionFramebuffer, RefractionFramebuffer); } }
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 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(); }
public void UpdateConstants() => _constantBufferVS.Update();
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(); }
public ParticleSystem( ContentManager contentManager, FXParticleSystemTemplate template, GetMatrixReferenceDelegate getWorldMatrix) { Template = template; _getWorldMatrix = getWorldMatrix; var maxParticles = CalculateMaxParticles(); // If this system never emits any particles, there's no reason to fully initialise it. if (maxParticles == 0) { return; } _graphicsDevice = contentManager.GraphicsDevice; _renderItemConstantsBufferVS = AddDisposable(new ConstantBuffer <MeshShaderResources.RenderItemConstantsVS>(_graphicsDevice)); _velocityType = Template.EmissionVelocity; _volumeType = Template.EmissionVolume; var texturePath = Path.Combine("Art", "Textures", Template.ParticleName); var texture = contentManager.Load <Texture>(texturePath); _particleResourceSet = AddDisposable(contentManager.ShaderResources.Particle.CreateParticleResoureSet( _renderItemConstantsBufferVS.Buffer, texture)); _shaderSet = contentManager.ShaderResources.Particle.ShaderSet; _pipeline = contentManager.ShaderResources.Particle.GetCachedPipeline(Template.Shader); _initialDelay = Template.InitialDelay.GetRandomInt(); _startSizeRate = Template.StartSizeRate.GetRandomFloat(); _startSize = 0; _colorKeyframes = new List <ParticleColorKeyframe>(); var colors = Template.Colors; if (colors.Color1 != null) { _colorKeyframes.Add(new ParticleColorKeyframe(colors.Color1)); } void addColorKeyframe(RgbColorKeyframe keyframe, RgbColorKeyframe previous) { if (keyframe != null && keyframe.Time > previous.Time) { _colorKeyframes.Add(new ParticleColorKeyframe(keyframe)); } } addColorKeyframe(colors.Color2, colors.Color1); addColorKeyframe(colors.Color3, colors.Color2); addColorKeyframe(colors.Color4, colors.Color3); addColorKeyframe(colors.Color5, colors.Color4); addColorKeyframe(colors.Color6, colors.Color5); addColorKeyframe(colors.Color7, colors.Color6); addColorKeyframe(colors.Color8, colors.Color7); _particles = new Particle[maxParticles]; for (var i = 0; i < _particles.Length; i++) { _particles[i].AlphaKeyframes = new List <ParticleAlphaKeyframe>(); _particles[i].Dead = true; } _deadList = new List <int>(); _deadList.AddRange(Enumerable.Range(0, maxParticles)); var numVertices = maxParticles * 4; _vertexBuffer = AddDisposable(contentManager.GraphicsDevice.ResourceFactory.CreateBuffer( new BufferDescription( (uint)(ParticleShaderResources.ParticleVertex.VertexDescriptor.Stride * maxParticles * 4), BufferUsage.VertexBuffer | BufferUsage.Dynamic))); _vertices = new ParticleShaderResources.ParticleVertex[numVertices]; _indexBuffer = AddDisposable(CreateIndexBuffer( contentManager.GraphicsDevice, maxParticles, out _numIndices)); State = ParticleSystemState.Active; _beforeRender = (cl, context) => { // Only update once we know this particle system is visible on screen. Update(cl, context.GameTime); if (_worldMatrixChanged) { _renderItemConstantsBufferVS.Update(cl); } cl.SetGraphicsResourceSet(1, _particleResourceSet); cl.SetVertexBuffer(0, _vertexBuffer); }; }