Exemplo n.º 1
0
        public void Setup(D3D11Device device)
        {
            if (m_vsCompiled == null)
            {
                return;
            }

            // Prepare All the stages
            device.Context.InputAssembler.InputLayout = m_layout;

            if (m_vs == null)
            {
                m_vs = new VertexShader(device.Device, m_vsCompiled);
            }
            device.Context.VertexShader.Set(m_vs);

            if (m_ps == null)
            {
                m_ps = new PixelShader(device.Device, m_psCompiled);
            }
            device.Context.PixelShader.Set(m_ps);

            if (m_layout == null)
            {
                // Layout from VertexShader input signature
                m_layout = new InputLayout(
                    device.Device,
                    ShaderSignature.GetInputSignature(m_vsCompiled),
                    InputElements.Value);
            }
            device.Context.InputAssembler.InputLayout = m_layout;
        }
Exemplo n.º 2
0
 void Update(D3D11Device device, T value)
 {
     if (_buffer == null)
     {
         _buffer           = Buffer.Create(device.Device, BindFlags.ConstantBuffer, ref value);
         _buffer.DebugName = typeof(T).Name;
     }
     device.Context.UpdateSubresource(ref value, _buffer);
 }
Exemplo n.º 3
0
        public bool SetIndices(D3D11Device device)
        {
            var indexBuffer = GetIndexBuffer(device.Device);

            if (indexBuffer == null)
            {
                return(false);
            }
            device.Context.InputAssembler.SetIndexBuffer(indexBuffer,
                                                         SharpDX.DXGI.Format.R32_UInt, 0);
            return(true);
        }
Exemplo n.º 4
0
        public void Setup(D3D11Device device, Color4 clear)
        {
            if (_rtv == null)
            {
                Create();
            }
            device.Context.ClearRenderTargetView(_rtv, clear);
            device.Context.ClearDepthStencilView(_dsv,
                                                 DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil,
                                                 1.0f, 0);

            device.Context.OutputMerger.SetTargets(_dsv, _rtv);
        }
Exemplo n.º 5
0
        Bitmap GetOrCreateBitmap(D3D11Device device, D3D11RenderTarget renderTarget)
        {
            Bitmap bitmap;

            if (!m_rtBitmapMap.TryGetValue(renderTarget, out bitmap))
            {
                using (var surface = renderTarget.Texture.QueryInterface <SharpDX.DXGI.Surface>())
                {
                    bitmap = new Bitmap(device.D2DDeviceContext, surface, GetBP);
                }
            }
            return(bitmap);
        }
Exemplo n.º 6
0
        public void Setup(D3D11Device device)
        {
            _constants.SetPSConstants(device, 0, Color);

            // shader
            Shader.Setup(device);

            // material
            device.Context.PixelShader.SetShaderResource(0, GetOrCreateSRV(device.Device));
            device.Context.PixelShader.SetSampler(0, GetOrCreateSamplerState(device.Device));
            device.Context.Rasterizer.State = GetRasterizerState(device.Device);
            device.Context.OutputMerger.SetDepthStencilState(GetOrCreateDepthStencilState(device.Device));
        }
Exemplo n.º 7
0
        void CreateBitmap(D3D11Device device)
        {
            Dispose();

            var pf = new PixelFormat(SharpDX.DXGI.Format.B8G8R8A8_UNorm, AlphaMode.Ignore);
            var bp = new BitmapProperties1(pf, device.Dpi.Height, device.Dpi.Width,
                                           BitmapOptions.CannotDraw | BitmapOptions.Target)
            ;

            using (var surface = _getSurface())
            {
                m_bitmap = new Bitmap1(device.D2DDeviceContext, surface);
            }
        }
Exemplo n.º 8
0
        public void Begin(D3D11Device device, Scene scene, Color4 clear)
        {
            m_device = device;
            m_scene  = scene;

            if (m_bitmap == null)
            {
                CreateBitmap(m_device);
            }

            m_device.D2DDeviceContext.Target = m_bitmap;
            m_device.D2DDeviceContext.BeginDraw();
            m_device.D2DDeviceContext.Clear(clear);
            m_device.D2DDeviceContext.Transform = Matrix3x2.Identity;
        }
Exemplo n.º 9
0
 void GetOrRenderTarget(D3D11Device device, uint id, RectangleF rect)
 {
     if (rect != m_rect)
     {
         m_rect = rect;
         if (m_renderTarget != null)
         {
             m_renderTarget.Dispose();
             m_renderTarget = null;
         }
     }
     if (m_renderTarget == null)
     {
         m_renderTarget = D3D11RenderTarget.Create(device, (int)rect.Width, (int)rect.Height);
     }
 }
Exemplo n.º 10
0
 public static D3D11RenderTarget Create(D3D11Device device, int w, int h)
 {
     return(new D3D11RenderTarget(() => new Texture2D(device.Device, new Texture2DDescription
     {
         Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
         ArraySize = 1,
         MipLevels = 1,
         Width = w,
         Height = h,
         SampleDescription = new SharpDX.DXGI.SampleDescription
         {
             Count = 1,
             Quality = 0
         },
         BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource
     })));
 }
Exemplo n.º 11
0
        public bool SetVertices(D3D11Device device, D3D11Shader shader)
        {
            var inputs = shader.InputElements.Value;

            if (inputs == null)
            {
                return(false);
            }
            if (!HasPositionAttribute)
            {
                return(false);
            }

            device.Context.InputAssembler.PrimitiveTopology = Topology;

            var vertices = GetVertexBuffer(device, inputs);

            device.Context.InputAssembler.SetVertexBuffers(0,
                                                           new VertexBufferBinding(vertices, Stride, 0));
            return(true);
        }
Exemplo n.º 12
0
 public void Draw(D3D11Device device, int offset, int count)
 {
     device.Context.Draw(count, offset);
 }
Exemplo n.º 13
0
 public void DrawIndexed(D3D11Device device, int offset, int count)
 {
     device.Context.DrawIndexed(count, offset, 0);
 }
Exemplo n.º 14
0
        SharpDX.Direct3D11.Buffer GetVertexBuffer(D3D11Device device,
                                                  InputElement[] inputs)
        {
            if (inputs != _inputs)
            {
                Dispose();
                _inputs = inputs;
            }

            if (m_vertexBuffer == null)
            {
                Stride = inputs.Sum(y => GetSize(y));
                var pos = m_attributes[Semantics.POSITION];

                // fill buffer
                _buffer = new InterleavedBuffer(Stride, VertexCount);
                int offset = 0;
                foreach (var input in inputs)
                {
                    VertexAttribute attr;
                    var             semantics = (Semantics)Enum.Parse(typeof(Semantics), input.SemanticName, true);
                    if (m_attributes.TryGetValue(semantics, out attr))
                    {
                        _buffer.Set(attr.Value, attr.ElementSize, offset);
                    }
                    offset += GetSize(input);
                }

                var desc = new BufferDescription
                {
                    Usage          = ResourceUsage.Dynamic,
                    BindFlags      = BindFlags.VertexBuffer,
                    CpuAccessFlags = CpuAccessFlags.Write,
                    SizeInBytes    = _buffer.Buffer.Length,
                };
                m_vertexBuffer           = SharpDX.Direct3D11.Buffer.Create(device.Device, _buffer.Buffer, desc);
                m_vertexBuffer.DebugName = "VertexBuffer";
            }

            if (_skinnedPosition != null)
            {
                //
                // skinning
                //
                var box = device.Context.MapSubresource(m_vertexBuffer, 0, MapMode.WriteDiscard, SharpDX.Direct3D11.MapFlags.None);

                int offset = 0;
                foreach (var input in inputs)
                {
                    // search position sematntics
                    VertexAttribute attr;
                    var             semantics = (Semantics)Enum.Parse(typeof(Semantics), input.SemanticName, true);
                    if (semantics == Semantics.POSITION)
                    {
                        if (m_attributes.TryGetValue(semantics, out attr))
                        {
                            _buffer.Set(_skinnedPosition, offset);
                        }
                        break;
                    }
                    offset += GetSize(input);
                }

                Marshal.Copy(_buffer.Buffer, 0, box.DataPointer, _buffer.Buffer.Length);

                device.Context.UnmapSubresource(m_vertexBuffer, 0);
            }

            return(m_vertexBuffer);
        }
Exemplo n.º 15
0
 public void SetPSConstants(D3D11Device device, int slot, T value)
 {
     Update(device, value);
     device.Context.PixelShader.SetConstantBuffer(slot, _buffer);
 }