Exemple #1
0
        public bool InitializeGraphics(Control control)
        {
            this.control = control;

            AttachMouseEventHandler(control);
            control.Resize += new System.EventHandler(form_Resize);

            System.Drawing.Size clientSize = control.ClientSize;

            // Create Device and SwapChain
            device = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, new SharpDX.Direct3D.FeatureLevel[] { SharpDX.Direct3D.FeatureLevel.Level_10_0 });

            factory1  = new SharpDX.DXGI.Factory1();
            swapChain = new SwapChain(factory1, device, new SwapChainDescription()
            {
                ModeDescription   = new ModeDescription(clientSize.Width, clientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = DetectSampleDescription(device, Format.D32_Float_S8X24_UInt),
                Usage             = Usage.RenderTargetOutput,
                BufferCount       = 1,
                OutputHandle      = control.Handle,
                IsWindowed        = true,
                SwapEffect        = SwapEffect.Discard
            });
            // Ignore all windows events
            factory1.MakeWindowAssociation(control.Handle, WindowAssociationFlags.IgnoreAll);

            CreateViewportAndProjection(ref clientSize);

            string skel_file = Path.Combine(Application.StartupPath, @"resources\skeleton.bin");

            skeleton = new hkaSkeleton();
            skeleton.Load(skel_file);

            string anim_file = Path.Combine(Application.StartupPath, @"resources\idle.bin");

            anim = new hkaAnimation();
            if (anim.Load(anim_file))
            {
                string source_file = Path.ChangeExtension(anim_file, ".hkx");
                LoadAnimationSuccessful(source_file);
            }

            renderer3d.InitializeGraphics(device, skeleton);
            renderer3d.CreateDeviceResources(swapChain, ref viewport);

            renderer2d.CreateDeviceIndependentResources(skeleton);

            return(true);
        }
Exemple #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="RenderControl" /> class.
        /// </summary>
        public RenderControl()
        {
            SwapChainDescription swapCHainDesc = new SwapChainDescription
            {
                BufferCount     = 2,
                Usage           = Usage.RenderTargetOutput,
                OutputHandle    = Handle,
                IsWindowed      = true,
                ModeDescription =
                    new ModeDescription(
                        Width,
                        Height,
                        new Rational(60, 1),
                        Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };

            Device.CreateWithSwapChain(
                DriverType.Hardware,
                DeviceCreationFlags.BgraSupport,
                swapCHainDesc,
                out _device,
                out _swapChain);

            Debug.Assert(_swapChain != null, "_swapChain != null");

            // ReSharper disable once AssignNullToNotNullAttribute
            _backBuffer = Surface.FromSwapChain(_swapChain, 0);
            Debug.Assert(_backBuffer != null, "_backBuffer != null");

            Size2F dpi = DirectXResourceManager.FactoryD2D.DesktopDpi;

            RenderTarget renderTarget = new RenderTarget(
                DirectXResourceManager.FactoryD2D,
                _backBuffer,
                new RenderTargetProperties
            {
                DpiX        = dpi.Width,
                DpiY        = dpi.Height,
                MinLevel    = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
                PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Ignore),
                Type        = RenderTargetType.Default,
                Usage       = RenderTargetUsage.None
            });

            _renderTargetContainer = RenderTargetContainer.CreateContainer(renderTarget, out _renderTargetRef);

            using (FactoryDXGI factory = _swapChain.GetParent <FactoryDXGI>())
            {
                Debug.Assert(factory != null, "factory != null");
                factory.MakeWindowAssociation(Handle, WindowAssociationFlags.IgnoreAltEnter);
            }

            _renderThread = new Thread(RenderLoop)
            {
                Name         = "Render Thread",
                IsBackground = true
            };
        }
Exemple #3
0
        public void Run()
        {
            #region setup resources
            var mainForm = new RenderForm();

            var scDescription = new SwapChainDescription()
            {
                BufferCount     = 1,
                ModeDescription =
                    new ModeDescription(
                        0,
                        0,
                        new Rational(60, 1),
                        Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = mainForm.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };

            //DeviceCreationFlags.Debug flag below will show debug layer messages in your output window.
            //Need proper version of windows sdk for it to work, otherwise it will throw an exception.
            //You also need to right click your project->properties->debug (on the left panel)-> check "enable native code debugging"

            // Create Device and SwapChain
            _d3d11.Device.CreateWithSwapChain(
                DriverType.Hardware,
                DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug,
                new[] { _d3d.FeatureLevel.Level_12_1 },
                scDescription,
                out d3d11Device,
                out swapChain);

            // Ignore all windows events
            dxgiFactory = swapChain.GetParent <_dxgi.Factory1>();
            dxgiFactory.MakeWindowAssociation(mainForm.Handle, WindowAssociationFlags.IgnoreAll);

            d2dFactory  = new _d2d.Factory();
            d2dFactory4 = d2dFactory.QueryInterface <_d2d.Factory4>();

            dxgiDevice        = d3d11Device.QueryInterface <_dxgi.Device>();
            d2dDevice3        = new _d2d.Device3(d2dFactory4, dxgiDevice);
            d2dDeviceContext3 = new _d2d.DeviceContext3(d2dDevice3, DeviceContextOptions.None);
            #endregion

            #region create drawing input
            sourceImage = createD2DBitmap(@"yourFile.png", d2dDeviceContext3);

            spriteBatch = new SpriteBatch(d2dDeviceContext3);
            var destinationRects = new RawRectangleF[1];
            destinationRects[0] = new RectangleF(100, 50, sourceImage.Size.Width, sourceImage.Size.Height);

            var sourceRects = new RawRectangle[1];
            sourceRects[0] = new RectangleF(0, 0, sourceImage.Size.Width, sourceImage.Size.Height);
            #endregion

            #region mainLoop
            RenderLoop.Run(mainForm, () =>
            {
                if (d2dTarget != null)
                {
                    d2dTarget.Dispose();
                    d2dTarget = null;
                }

                using (var backBuffer = Texture2D.FromSwapChain <Texture2D>(swapChain, 0))
                {
                    using (var surface = backBuffer.QueryInterface <Surface>())
                    {
                        var bmpProperties = new BitmapProperties1(
                            new PixelFormat(Format.R8G8B8A8_UNorm, _d2d.AlphaMode.Premultiplied),
                            dpiX: 96,
                            dpiY: 96,
                            bitmapOptions: BitmapOptions.Target | BitmapOptions.CannotDraw);

                        d2dTarget = new Bitmap1(
                            d2dDeviceContext3,
                            surface,
                            bmpProperties);

                        d2dDeviceContext3.Target = d2dTarget;
                    }
                }

                //the key missing piece: cannot use per primitive antialiasing with spritebatch
                d2dDeviceContext3.AntialiasMode = AntialiasMode.Aliased;
                d2dDeviceContext3.BeginDraw();

                spriteBatch.Clear();
                spriteBatch.AddSprites(
                    1,
                    destinationRects,
                    sourceRects,
                    null,
                    null,
                    destinationRectanglesStride: 0,     //0 stride because there is only 1 element
                    sourceRectanglesStride: 0,
                    colorsStride: 0,
                    transformsStride: 0);

                d2dDeviceContext3.DrawSpriteBatch(
                    spriteBatch: spriteBatch,
                    startIndex: 0,
                    spriteCount: 1,
                    bitmap: sourceImage,
                    interpolationMode: BitmapInterpolationMode.Linear,
                    spriteOptions: SpriteOptions.ClampToSourceRectangle);

                d2dDeviceContext3.EndDraw();

                //first param set to 1 would indicate waitVerticalBlanking
                swapChain.Present(0, PresentFlags.None);
            });
            #endregion
        }