public MeshFactory(SharpDX11Graphics graphics)
        {
            this.device = graphics.Device;
            this.inputAssembler = device.ImmediateContext.InputAssembler;
            this.demo = graphics.Demo;

            instanceDataDesc = new BufferDescription()
            {
                Usage = ResourceUsage.Dynamic,
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
            };

            InputElement[] elements = new InputElement[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0, InputClassification.PerVertexData, 0),
                new InputElement("WORLD", 0, Format.R32G32B32A32_Float, 0, 1, InputClassification.PerInstanceData, 1),
                new InputElement("WORLD", 1, Format.R32G32B32A32_Float, 16, 1, InputClassification.PerInstanceData, 1),
                new InputElement("WORLD", 2, Format.R32G32B32A32_Float, 32, 1, InputClassification.PerInstanceData, 1),
                new InputElement("WORLD", 3, Format.R32G32B32A32_Float, 48, 1, InputClassification.PerInstanceData, 1),
                new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 64, 1, InputClassification.PerInstanceData, 1)
            };
            inputLayout = new InputLayout(device, graphics.GetEffectPass().Description.Signature, elements);

            groundColor = ColorToUint(Color.Green);
            activeColor = ColorToUint(Color.Orange);
            passiveColor = ColorToUint(Color.OrangeRed);
            softBodyColor = ColorToUint(Color.LightBlue);
        }
        public PhysicsDebugDraw(DeviceManager manager)
        {
            device = manager.Direct3DDevice;
            inputAssembler = device.ImmediateContext.InputAssembler;
            lineArray = new PositionColored[0];

            using (var bc = HLSLCompiler.CompileFromFile(@"Shaders\PhysicsDebug.hlsl", "VSMain", "vs_5_0"))
            {
                vertexShader = new VertexShader(device, bc);

                InputElement[] elements = new InputElement[]
                {
                    new InputElement("SV_POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                    new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 12, 0, InputClassification.PerVertexData, 0)
                };
                inputLayout = new InputLayout(device, bc, elements);
            }

            vertexBufferDesc = new BufferDescription()
            {
                Usage = ResourceUsage.Dynamic,
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.Write
            };

            vertexBufferBinding = new VertexBufferBinding(null, PositionColored.Stride, 0);

            using (var bc = HLSLCompiler.CompileFromFile(@"Shaders\PhysicsDebug.hlsl", "PSMain", "ps_5_0"))
                pixelShader = new PixelShader(device, bc);
        }
 private void InitializeStages()
 {
     inputAssembler = nativeDeviceContext.InputAssembler;
     outputMerger   = nativeDeviceContext.OutputMerger;
     shaderStages[(int)ShaderStage.Vertex - 1]   = nativeDeviceContext.VertexShader;
     shaderStages[(int)ShaderStage.Hull - 1]     = nativeDeviceContext.HullShader;
     shaderStages[(int)ShaderStage.Domain - 1]   = nativeDeviceContext.DomainShader;
     shaderStages[(int)ShaderStage.Geometry - 1] = nativeDeviceContext.GeometryShader;
     shaderStages[(int)ShaderStage.Pixel - 1]    = nativeDeviceContext.PixelShader;
     shaderStages[(int)ShaderStage.Compute - 1]  = nativeDeviceContext.ComputeShader;
 }
        public PhysicsDebugDraw(SharpDX11Graphics graphics)
        {
            _device = graphics.Device;
            _inputAssembler = _device.ImmediateContext.InputAssembler;

            InputElement[] elements = {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 12, 0, InputClassification.PerVertexData, 0)
            };
            _inputLayout = new InputLayout(_device, graphics.GetDebugDrawPass().Description.Signature, elements);

            _vertexBufferDesc = new BufferDescription
            {
                Usage = ResourceUsage.Dynamic,
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.Write
            };

            _vertexBufferBinding = new VertexBufferBinding(null, PositionColored.Stride, 0);
        }
 internal void Apply(InputAssemblerStage inputAssemblerStage)
 {
     inputAssemblerStage.SetVertexBuffers(0, nativeVertexBufferBindings);
     inputAssemblerStage.SetIndexBuffer(nativeIndexBuffer, indexFormat, indexBufferOffset);
 }
        public MeshFactory(Demo demo)
        {
            this.demo = demo;
            this.device = demo.Device;
            this.inputAssembler = device.ImmediateContext.InputAssembler;

            instanceDataDesc = new BufferDescription()
            {
                Usage = ResourceUsage.Dynamic,
                BindFlags = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.Write,
                OptionFlags = ResourceOptionFlags.None,
            };

            InputElement[] elements = new InputElement[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
                new InputElement("NORMAL", 0, Format.R32G32B32_Float, 12, 0, InputClassification.PerVertexData, 0),
                new InputElement("WORLD", 0, Format.R32G32B32A32_Float, 0, 1, InputClassification.PerInstanceData, 1),
                new InputElement("WORLD", 1, Format.R32G32B32A32_Float, 16, 1, InputClassification.PerInstanceData, 1),
                new InputElement("WORLD", 2, Format.R32G32B32A32_Float, 32, 1, InputClassification.PerInstanceData, 1),
                new InputElement("WORLD", 3, Format.R32G32B32A32_Float, 48, 1, InputClassification.PerInstanceData, 1),
                new InputElement("COLOR", 0, Format.R8G8B8A8_UNorm, 64, 1, InputClassification.PerInstanceData, 1)
            };
            inputLayout = new InputLayout(device, demo.GetEffectPass().Description.Signature, elements);
        }
        protected void OnInitializeDevice()
        {
            Form.ClientSize = new Size(_width, _height);

            // SwapChain description
            var desc = new SwapChainDescription()
            {
                BufferCount = 1,
                ModeDescription = new ModeDescription(_width, _height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed = true,
                OutputHandle = Form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect = SwapEffect.Discard,
                Usage = Usage.RenderTargetOutput,
            };

            // Create Device and SwapChain
            SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out _device, out _swapChain);
            _immediateContext = _device.ImmediateContext;
            outputMerger = _immediateContext.OutputMerger;
            inputAssembler = _immediateContext.InputAssembler;

            Factory factory = _swapChain.GetParent<Factory>();
            factory.MakeWindowAssociation(Form.Handle, WindowAssociationFlags.None);

            var blendStateDesc = new BlendStateDescription();
            blendStateDesc.RenderTarget[0].IsBlendEnabled = true;
            blendStateDesc.RenderTarget[0].SourceBlend = BlendOption.SourceAlpha;
            blendStateDesc.RenderTarget[0].DestinationBlend = BlendOption.InverseSourceAlpha;
            blendStateDesc.RenderTarget[0].BlendOperation = BlendOperation.Add;
            blendStateDesc.RenderTarget[0].SourceAlphaBlend = BlendOption.One;
            blendStateDesc.RenderTarget[0].DestinationAlphaBlend = BlendOption.Zero;
            blendStateDesc.RenderTarget[0].AlphaBlendOperation = BlendOperation.Add;
            blendStateDesc.RenderTarget[0].RenderTargetWriteMask = ColorWriteMaskFlags.All;
            alphaBlendState = new BlendState(_device, blendStateDesc);
        }
 private void InitializeStages()
 {
     inputAssembler = nativeDeviceContext.InputAssembler;
     outputMerger = nativeDeviceContext.OutputMerger;
     shaderStages[(int)ShaderStage.Vertex - 1] = nativeDeviceContext.VertexShader;
     shaderStages[(int)ShaderStage.Hull - 1] = nativeDeviceContext.HullShader;
     shaderStages[(int)ShaderStage.Domain - 1] = nativeDeviceContext.DomainShader;
     shaderStages[(int)ShaderStage.Geometry - 1] = nativeDeviceContext.GeometryShader;
     shaderStages[(int)ShaderStage.Pixel - 1] = nativeDeviceContext.PixelShader;
     shaderStages[(int)ShaderStage.Compute - 1] = nativeDeviceContext.ComputeShader;
 }