Пример #1
0
        static void TryChangeToFullscreen()
        {
            if (m_changeToFullscreen.HasValue)
            {
                var md = m_changeToFullscreen.Value;

                try
                {
                    var adapterDevId = m_adapterInfoList[m_settings.AdapterOrdinal].AdapterDeviceId;
                    var outputId     = m_adapterInfoList[m_settings.AdapterOrdinal].OutputId;

                    m_swapchain.ResizeTarget(ref md);
                    m_swapchain.SetFullscreenState(true, GetFactory().Adapters[adapterDevId].Outputs.Length > outputId ? GetFactory().Adapters[adapterDevId].Outputs[outputId] : null);

                    md.RefreshRate.Numerator   = 0;
                    md.RefreshRate.Denominator = 0;
                    m_swapchain.ResizeTarget(ref md);

                    m_changeToFullscreen = null;

                    Log.WriteLine("DXGI SetFullscreenState succeded");
                }
                catch (SharpDX.SharpDXException e)
                {
                    if (e.ResultCode == SharpDX.DXGI.ResultCode.Unsupported)
                    {
                        m_changeToFullscreen = null;
                    }

                    Log.WriteLine("TryChangeToFullscreen failed with " + e.ResultCode);
                }
            }
        }
Пример #2
0
        static void TryChangeToFullscreen()
        {
            if (m_changeToFullscreen.HasValue)
            {
                var md = m_changeToFullscreen.Value;

                try
                {
                    var adapterDevId = m_adapterInfoList[m_settings.AdapterOrdinal].AdapterDeviceId;
                    var outputId     = m_adapterInfoList[m_settings.AdapterOrdinal].OutputId;

                    m_swapchain.ResizeTarget(ref md);
                    m_swapchain.SetFullscreenState(true, GetFactory().Adapters[adapterDevId].Outputs[outputId]);

                    md.RefreshRate.Numerator   = 0;
                    md.RefreshRate.Denominator = 0;
                    m_swapchain.ResizeTarget(ref md);

                    m_changeToFullscreen = null;

                    MyRender11.Log.WriteLine("DXGI SetFullscreenState succeded");
                }
                catch (SharpDX.SharpDXException e)
                {
                    // there might be some fatal exception, or minor exception saying that windows is overlapped/has no mouse focus and going to fullscreen should be done later
                }
            }
        }
Пример #3
0
 public void ShutDown()
 {
     SwapChain?.SetFullscreenState(false, null);
     AlphaEnableBlendingState?.Dispose();
     AlphaEnableBlendingState = null;
     AlphaDisableBlendingState?.Dispose();
     AlphaDisableBlendingState = null;
     DepthDisabledStencilState?.Dispose();
     DepthDisabledStencilState = null;
     RasterState?.Dispose();
     RasterState = null;
     RasterStateNoCulling?.Dispose();
     RasterStateNoCulling = null;
     RasterStateWirefram?.Dispose();
     RasterStateWirefram = null;
     DepthStencilView?.Dispose();
     DepthStencilView = null;
     DepthStencilState?.Dispose();
     DepthStencilState = null;
     DepthStencilBuffer?.Dispose();
     DepthStencilBuffer = null;
     RenderTargetView?.Dispose();
     RenderTargetView = null;
     DeviceContext?.Dispose();
     DeviceContext = null;
     Device?.Dispose();
     Device = null;
     SwapChain?.Dispose();
     SwapChain = null;
 }
        public void ShutDown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            SwapChain?.SetFullscreenState(false, null);

            // Dispose of all objects.
            DepthDisabledStencilState?.Dispose();
            DepthDisabledStencilState = null;
            RasterState?.Dispose();
            RasterState = null;
            DepthStencilView?.Dispose();
            DepthStencilView = null;
            DepthStencilState?.Dispose();
            DepthStencilState = null;
            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;
            RenderTargetView?.Dispose();
            RenderTargetView = null;
            DeviceContext?.Dispose();
            DeviceContext = null;
            Device?.Dispose();
            Device = null;
            SwapChain?.Dispose();
            SwapChain = null;
        }
Пример #5
0
 public void Shutdown()
 {
     SwapChain?.SetFullscreenState(false, null);
     m_RSSolid?.Dispose();
     m_RSSolid = null;
     m_RSWireFrame?.Dispose();
     m_RSWireFrame = null;
     m_RSCullSolid?.Dispose();
     m_RSCullSolid = null;
     m_RSCullWireFrame?.Dispose();
     m_RSCullWireFrame = null;
     m_DepthStencilView?.Dispose();
     m_DepthStencilView = null;
     DepthStencilState?.Dispose();
     DepthStencilState = null;
     m_depthStencilBuffer?.Dispose();
     m_depthStencilBuffer = null;
     m_RenderTargetView?.Dispose();
     m_RenderTargetView = null;
     DeviceContext?.Dispose();
     DeviceContext = null;
     Device?.Dispose();
     Device = null;
     SwapChain?.Dispose();
     SwapChain = null;
 }
Пример #6
0
 protected override void Dispose(bool disposeManagedResources)
 {
     base.Dispose(disposeManagedResources);
     if (!deviceManager.Settings.IsWindowed)
     {
         SwapChain.SetFullscreenState(false, null);
     }
 }
Пример #7
0
 public override void Close()
 {
     if (!deviceManager.Settings.IsWindowed)
     {
         SwapChain.SetFullscreenState(false, null);
     }
     deviceManager.Dispose();
     Dispose();
 }
Пример #8
0
        public void Shutdown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            if (SwapChain != null)
            {
                SwapChain.SetFullscreenState(false, null);
            }

            if (DepthDisabledStencilState != null)
            {
                DepthDisabledStencilState.Dispose();
                DepthDisabledStencilState = null;
            }

            if (RasterState != null)
            {
                RasterState.Dispose();
                RasterState = null;
            }

            if (DepthStencilView != null)
            {
                DepthStencilView.Dispose();
                DepthStencilView = null;
            }

            if (DepthStencilState != null)
            {
                DepthStencilState.Dispose();
                DepthStencilState = null;
            }

            if (DepthStencilBuffer != null)
            {
                DepthStencilBuffer.Dispose();
                DepthStencilBuffer = null;
            }

            if (RenderTargetView != null)
            {
                RenderTargetView.Dispose();
                RenderTargetView = null;
            }

            if (Device != null)
            {
                Device.Dispose();
                Device = null;
            }

            if (SwapChain != null)
            {
                SwapChain.Dispose();
                SwapChain = null;
            }
        }
Пример #9
0
        private void Stop()
        {
            if (_swapChain != null)
            {
                _swapChain.SetFullscreenState(false, null);
            }

            Close();
            CleanUp();
        }
Пример #10
0
 public void Dispose()
 {
     if (!_disposed)
     {
         if (SwapChain != null && SwapChain.IsFullScreen == true) // Before shutting down set swap chain to windowed mode
         {
             SwapChain.SetFullscreenState(false, null);
         }
         DisposeHelper.DisposeAndSetToNull(_rasterStateSolid, DepthStencilView, _depthStencilState, _depthDisabledStencilState, _depthStencilBuffer, _renderTargetView, Device, SwapChain);
         _disposed = true;
     }
 }
Пример #11
0
 public void Shutdown()
 {
     swapChain.SetFullscreenState(false, null);
     rasterizerState.Dispose();
     depthStencilView.Dispose();
     depthStencilState.Dispose();
     depthStencilBuffer.Dispose();
     renderTargetView.Dispose();
     DeviceContext.Dispose();
     Device.Dispose();
     swapChain.Dispose();
 }
Пример #12
0
        public void Dispose()
        {
            _swapChain?.SetFullscreenState(false, null);

            DisposeObject(_rasterState);
            DisposeObject(_depthStencilView);
            DisposeObject(DepthStencilState);
            DisposeObject(_depthStencilBuffer);
            DisposeObject(_renderTargetView);
            DisposeObject(DeviceContext);
            DisposeObject(Device);
            DisposeObject(_swapChain);
        }
Пример #13
0
 public void Dispose()
 {
     if (_disposed)
     {
         return;
     }
     if (SwapChain != null && SwapChain.IsFullScreen) // Before shutting down set swap chain to windowed mode
     {
         SwapChain.SetFullscreenState(false, null);
     }
     DisposeHelper.DisposeAndSetToNull(_rasterStateSolid, _rasterStateWireFrame, _alphaDisabledBlendState, _alphaEnabledBlendState,
                                       DepthStencilView, RenderToTextureDepthStencilView, _depthStencilState, _depthDisabledStencilState, _depthStencilBuffer, _renderTargetView, Device, SwapChain);
     _disposed = true;
 }
Пример #14
0
        internal void EndCamera(int VSync)
        {
            using (var BackBuffer = SwapChain.GetBackBuffer <Texture2D>(0))
            {
                Device.ImmediateContext.ResolveSubresource(AntiAliasedBackBuffer, 0, BackBuffer, 0, FormatRGB);
            }

            if ((VSync == 0) != SwapChain.IsFullScreen)
            {
                SwapChain.SetFullscreenState(VSync == 0, null);
                ResizeBuffers();
            }

            SwapChain.Present(VSync, PresentFlags.None);
            CameraShader.End();
        }
Пример #15
0
        public void Shutdown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            _swapChain?.SetFullscreenState(false, null);

            _alphaEnableBlendingState?.Dispose();
            _alphaEnableBlendingState = null;

            _alphaDisableBlendingState?.Dispose();
            _alphaDisableBlendingState = null;

            BackBuffer?.Dispose();
            BackBuffer = null;

            _depthDisabledStencilState?.Dispose();
            _depthDisabledStencilState = null;

            _rasterState?.Dispose();
            _rasterState = null;

            DepthStencilView?.Dispose();
            DepthStencilView = null;

            _depthStencilState?.Dispose();
            _depthStencilState = null;

            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;

            _renderTargetView?.Dispose();
            _renderTargetView = null;

            DeviceContext?.Dispose();
            DeviceContext = null;

            Device?.Dispose();
            Device = null;

            BackBuffer?.Dispose();
            BackBuffer = null;

            _swapChain?.Dispose();
            _swapChain = null;
        }
Пример #16
0
        public void Dispose()
        {
            WaitForPrevFrame();

            swapChain.SetFullscreenState(false, null);

            eventHandle.Close();

            Utilities.Dispose(ref commandList);

            Utilities.Dispose(ref descriptorHeap);
            Utilities.Dispose(ref renderTarget);
            Utilities.Dispose(ref commandListAllocator);
            Utilities.Dispose(ref commandQueue);
            Utilities.Dispose(ref device);
            Utilities.Dispose(ref swapChain);

            window?.Dispose();
        }
Пример #17
0
 public void ShutDown()
 {
     SwapChain?.SetFullscreenState(false, null);
     RasterState?.Dispose();
     RasterState = null;
     DepthStencilView?.Dispose();
     DepthStencilView = null;
     DepthStencilState?.Dispose();
     DepthStencilState = null;
     DepthStencilBuffer?.Dispose();
     DepthStencilBuffer = null;
     RenderTargetView?.Dispose();
     RenderTargetView = null;
     DeviceContext?.Dispose();
     DeviceContext = null;
     Device?.Dispose();
     Device = null;
     SwapChain?.Dispose();
     SwapChain = null;
 }
        /// <summary>
        /// Cleanup allocations
        /// </summary>
        public void Dispose()
        {
            // wait for the GPU to be done with all resources
            WaitForPrevFrame();

            swapChain.SetFullscreenState(false, null);

            eventHandle.Close();

            // asset objects
            Utilities.Dispose(ref commandList);

            // pipeline objects
            Utilities.Dispose(ref descriptorHeap);
            Utilities.Dispose(ref renderTarget);
            Utilities.Dispose(ref commandListAllocator);
            Utilities.Dispose(ref commandQueue);
            Utilities.Dispose(ref device);
            Utilities.Dispose(ref swapChain);
        }
Пример #19
0
        public void ShutDown()
        {
            // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
            SwapChain?.SetFullscreenState(false, null);
            RasterState?.Dispose();
            RasterState = null;
            depthStencilState?.Dispose();
            depthStencilState = null;
            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;
            _depthStencilView?.Dispose();
            _depthStencilView = null;
            _renderTargetView?.Dispose();
            _renderTargetView = null;
            DeviceContext?.Dispose();
            Device?.Dispose();
            SwapChain?.Dispose();



            AlphaEnableBlendingState?.Dispose();
            AlphaEnableBlendingState = null;
            AlphaDisableBlendingState?.Dispose();
            AlphaDisableBlendingState = null;
            DepthDisabledStencilState?.Dispose();
            DepthDisabledStencilState = null;
            //DepthStencilView?.Dispose();
            //DepthStencilView = null;
            DepthStencilState?.Dispose();
            DepthStencilState = null;
            DepthStencilBuffer?.Dispose();
            DepthStencilBuffer = null;


            if (main_thread_update != null)
            {
                //main_thread_update.Suspend();
                main_thread_update = null;
            }
            ShutDownGraphics();
        }
Пример #20
0
        public void Dispose()
        {
            _swapChain?.SetFullscreenState(false, null);

            AlphaAdditiveBlendState2?.Dispose();
            AlphaAdditiveBlendState2 = null;

            AlphaEnableBlendingState?.Dispose();
            AlphaEnableBlendingState = null;

            AlphaDisableBlendingState?.Dispose();
            AlphaDisableBlendingState = null;

            DepthDisabledStencilState?.Dispose();
            DepthDisabledStencilState = null;

            _rasterState?.Dispose();
            _rasterState = null;

            _depthStencilView?.Dispose();
            _depthStencilView = null;

            DepthStencilState?.Dispose();
            DepthStencilState = null;

            _depthStencilBuffer?.Dispose();
            _depthStencilBuffer = null;

            _renderTargetView?.Dispose();
            _renderTargetView = null;

            DeviceContext?.Dispose();
            DeviceContext = null;

            Device?.Dispose();
            Device = null;

            _swapChain?.Dispose();
            _swapChain = null;
        }
Пример #21
0
 public void SwitchFullscreen(bool fullscreen, Output output = null)
 {
     m_swapchain.SetFullscreenState(fullscreen, output);
 }
Пример #22
0
        private void RenderFrame()
        {
            Input.XInputInput.Update();
            if ((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.Back) == SharpDX.XInput.GamepadButtonFlags.Back)
            {
                renderForm.Close();
            }
            if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.Start) == SharpDX.XInput.GamepadButtonFlags.Start) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.Start) != SharpDX.XInput.GamepadButtonFlags.Start))
            {
                swapChain.GetFullscreenState(out SharpDX.Mathematics.Interop.RawBool fs, out Output o);
                o?.Dispose();
                swapChain.SetFullscreenState(!fs, null);
            }
            fishManager.Update();
            fishRenderer.Update();
            foodManager.Update();

            //theta = 0.0f;

            if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.X) == SharpDX.XInput.GamepadButtonFlags.X) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.X) != SharpDX.XInput.GamepadButtonFlags.X))
            {
                camState = !camState;
            }

            if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadUp) == SharpDX.XInput.GamepadButtonFlags.DPadUp) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadUp) != SharpDX.XInput.GamepadButtonFlags.DPadUp))
            {
                Wireframe = !Wireframe;
            }
            if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadLeft) == SharpDX.XInput.GamepadButtonFlags.DPadLeft) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadLeft) != SharpDX.XInput.GamepadButtonFlags.DPadLeft))
            {
                frameSkip = frameSkip > 1 ? frameSkip - 1 : 1;
            }
            if (((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadRight) == SharpDX.XInput.GamepadButtonFlags.DPadRight) && ((Input.XInputInput.PrevState[0].Buttons & SharpDX.XInput.GamepadButtonFlags.DPadRight) != SharpDX.XInput.GamepadButtonFlags.DPadRight))
            {
                frameSkip++;
            }

            if (!camState)
            {
                SetCamera(Matrix.LookAtLH(new Vector3((float)Math.Sin(Math.Sin(theta) * 0.1f), 0, (float)Math.Cos(Math.Sin(theta) * 0.1f)) * -1.3333f, Vector3.Zero, Vector3.Up), //Matrix.LookAtLH(Vector3.BackwardLH * 5, Vector3.Zero, Vector3.Up),
                                                                                                                                                                                  //Matrix.OrthoLH(16.0f / 9.0f * 2.0f, 2.0f, 0, 10.0f));
                          Matrix.PerspectiveFovLH(MathUtil.DegreesToRadians(75.0f), RenderWidth / (float)RenderHeight, 0.1f, 20.0f));
            }
            else
            {
                SetCamera(Matrix.LookAtLH(new Vector3(fishManager.FishList[0].Position * 0.75f, -1.33333f), new Vector3(fishManager.FishList[0].Position * 0.75f, 0.0f), Vector3.Up),
                          Matrix.PerspectiveFovLH(MathUtil.DegreesToRadians(45.0f), RenderWidth / (float)RenderHeight, 0.1f, 20.0f));
            }

            theta += (float)Math.PI / 30.0f / 60.0f * (Input.XInputInput.State[0].LeftThumbY / (float)short.MaxValue + 0.25f) * 4.0f;

            if (frameNumber % (ulong)frameSkip == 0 && (Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.LeftShoulder) != SharpDX.XInput.GamepadButtonFlags.LeftShoulder)
            {
                DeviceContext.Rasterizer.State = Wireframe ? RasterizerStates.Wire : RasterizerStates.Back;

                //DeviceContext.OutputMerger.SetRenderTargets(renderTargetView);
                DeviceContext.PixelShader.SetShaderResource(0, null);
                DeviceContext.OutputMerger.SetRenderTargets(colorTarget.DSV, colorTarget.RTV);
                DeviceContext.OutputMerger.DepthStencilState = DepthStencilStates.Enabled;
                DeviceContext.ClearRenderTargetView(renderTargetView, new Color(0.0f, 0.0f, 0.0f));
                colorTarget.Clear(Color4.Lerp(new Color4(0, 0, 0.04f, 0.0f), new Color4(0.08f, 0.08f, 0.2f, 0.0f), Math.Max(0, -(float)Math.Cos(theta))));

                //RenderDebugTri();

                var intensity = Math.Max(0, -(float)Math.Cos(theta));
                var sunDir    = new Vector3((float)Math.Sin(theta), (float)Math.Cos(theta), 1.0f);
                Sun.Set(sunDir, new Vector3(1.0f, 1.0f, 1.0f), intensity);
                fishGlow.Intensity     = Math.Max(0, ((float)Math.Cos(intensity * MathUtil.Pi) + 0.2f) / 1.2f);
                raysRenderer.Intensity = 0.02f * (1 - fishGlow.Intensity);
                fishGlow.Intensity    *= 0.75f;

                decorRenderer.Render();

                fishRenderer.Render();
                DeviceContext.OutputMerger.SetRenderTargets(colorTarget.DSV, renderTargetView);

                //var pos = new Vector3(fishManager.FishList[0].Position, 0);
                //var np = Vector3.Transform(pos, Projection * View);
                var np = Vector3.Transform(sunDir * new Vector3(1, 1, 0) * -10, Projection * View);

                DeviceContext.OutputMerger.DepthStencilState = DepthStencilStates.Disabled;
                DeviceContext.Rasterizer.State = RasterizerStates.Back;
                raysRenderer.CentrePoint       = new Vector2(-sunDir.X * RenderWidth * 10.0f, sunDir.Y * RenderHeight * 10.0f);//new Vector2((np.X / np.W / 2.0f + 0.5f) * RenderWidth, (-np.Y / np.W / 2.0f + 0.5f) * RenderHeight);
                raysRenderer.Render(colorTarget.SRV, 1.0f / 60.0f);
                fishGlow.Render();
                DeviceContext.Rasterizer.State = Wireframe ? RasterizerStates.Wire : RasterizerStates.Back;

                DeviceContext.OutputMerger.DepthStencilState = DepthStencilStates.Enabled;
                foodRenderer.Render();

                decorRenderer.Render2();

                try
                {
                    if ((Input.XInputInput.State[0].Buttons & SharpDX.XInput.GamepadButtonFlags.RightShoulder) != SharpDX.XInput.GamepadButtonFlags.RightShoulder)
                    {
                        swapChain.Present(1, PresentFlags.None);
                    }
                    else
                    {
                        swapChain.Present(0, PresentFlags.None);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(Device.DeviceRemovedReason.ToString());
                    Console.ReadLine();
                    renderForm.Close();
                }
            }

            frameNumber++;
        }
Пример #23
0
        public void Initialize(int width, int height, List <PrimordialObject> primordialObject, string fileTitle)
        {
            this.fileTitle = fileTitle;
            _clock         = new Stopwatch();
            _clock.Start();
            _form = new RenderForm("PrimordialEngine");
            _form.FormBorderStyle = FormBorderStyle.None;
            _form.MouseMove      += mouseMove;
            var userControl = new UserControl();

            _form.Controls.Add(userControl);
            _form.ClientSize = new System.Drawing.Size(width, height);
            camera           = new Camera(60, width / (float)height);

            userControl.Size       = new Size(0, 0);
            userControl.KeyDown   += keyDown;
            userControl.MouseMove += mouseMove;
            userControl.KeyUp     += KeyUpEvent;

            var point = new System.Drawing.Point(_form.Location.X + (_form.Size.Width / 2), _form.Location.Y + (_form.Size.Height / 2));

            //Cursor.Position = point;

            _primordialObject = primordialObject[0];

            _swapChainDescription = new SwapChainDescription()
            {
                BufferCount       = 1,
                ModeDescription   = new ModeDescription(_form.ClientSize.Width, _form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                IsWindowed        = true,
                OutputHandle      = _form.Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Discard,
                Usage             = Usage.RenderTargetOutput
            };


            D3D11.Device.CreateWithSwapChain(DriverType.Hardware, D3D11.DeviceCreationFlags.None, _swapChainDescription, out _device, out _swapChain);
            _context = _device.ImmediateContext;

            _factory = _swapChain.GetParent <Factory>();
            _factory.MakeWindowAssociation(_form.Handle, WindowAssociationFlags.IgnoreAll);


            var shaderName = "DirectXRenderer\\MiniCube.hlsl";

            //var shaderName = @"DirectXRenderer\MiniCubeNoFrag.hlsl";
            //var shaderName = "DirectXRenderer\\MiniCubeNoShader.hlsl";
            //for(int i = 0; i < 100; i++) {
            //var shaderTime = _clock.ElapsedMilliseconds;
            _vertexShaderByteCode = ShaderBytecode.CompileFromFile(shaderName, "VS", "vs_4_0");
            _vertexShader         = new D3D11.VertexShader(_device, _vertexShaderByteCode);

            _pixelShaderByteCode = ShaderBytecode.CompileFromFile(shaderName, "PS", "ps_4_0");
            _pixelShader         = new D3D11.PixelShader(_device, _pixelShaderByteCode);

            // File.AppendAllText(fileTitle + "Shader.txt", (_clock.ElapsedMilliseconds - shaderTime).ToString() + "\n");
            // _vertexShaderByteCode?.Dispose();
            //_vertexShader?.Dispose();
            // _pixelShaderByteCode?.Dispose();
            // _pixelShader?.Dispose();
            //}
            _shaderSignature = ShaderSignature.GetInputSignature(_vertexShaderByteCode);

            _inputLayout = new D3D11.InputLayout(_device, _shaderSignature, new[]
            {
                new D3D11.InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
                new D3D11.InputElement("NORMAL", 0, Format.R32G32B32A32_Float, 32, 0)
            });

            //   for(int i = 0; i<100; i++) {
            //   var sendDataTime = _clock.ElapsedMilliseconds;
            _verticesBuffer = D3D11.Buffer.Create(_device, D3D11.BindFlags.VertexBuffer, _primordialObject.VertexData);
            //  File.AppendAllText(fileTitle + "Data.txt", (_clock.ElapsedMilliseconds - sendDataTime).ToString() + "\n");
            //  _verticesBuffer?.Dispose();
            //  _verticesBuffer = null;
            // }
            _contantBuffer  = new D3D11.Buffer(_device, Utilities.SizeOf <Matrix>(), D3D11.ResourceUsage.Default, D3D11.BindFlags.ConstantBuffer, D3D11.CpuAccessFlags.None, D3D11.ResourceOptionFlags.None, 0);
            _contantBuffer2 = new D3D11.Buffer(_device, Utilities.SizeOf <Matrix>(), D3D11.ResourceUsage.Default, D3D11.BindFlags.ConstantBuffer, D3D11.CpuAccessFlags.None, D3D11.ResourceOptionFlags.None, 0);
            _contantBuffer3 = new D3D11.Buffer(_device, Utilities.SizeOf <Matrix>(), D3D11.ResourceUsage.Default, D3D11.BindFlags.ConstantBuffer, D3D11.CpuAccessFlags.None, D3D11.ResourceOptionFlags.None, 0);
            _context.InputAssembler.InputLayout       = _inputLayout;
            _context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            _context.InputAssembler.SetVertexBuffers(0, new D3D11.VertexBufferBinding(_verticesBuffer, Utilities.SizeOf <VertexDataStruct>(), 0));
            _context.VertexShader.SetConstantBuffer(0, _contantBuffer);
            _context.VertexShader.SetConstantBuffer(1, _contantBuffer2);
            _context.VertexShader.SetConstantBuffer(2, _contantBuffer3);
            _context.VertexShader.Set(_vertexShader);
            _context.PixelShader.Set(_pixelShader);

            _view = Matrix.LookAtLH(new Vector3(0, 0, 0), new Vector3(0, 0, -5), Vector3.UnitY);

            _backBuffer  = null;
            _renderView  = null;
            _depthBuffer = null;
            _depthView   = null;

            _form.UserResized += (sender, args) => _userResized = true;

            _form.KeyUp += (sender, args) =>
            {
                if (args.KeyCode == Keys.F5)
                {
                    _swapChain.SetFullscreenState(true, null);
                }
                else if (args.KeyCode == Keys.F4)
                {
                    _swapChain.SetFullscreenState(false, null);
                }
                //else if (args.KeyCode == Keys.Escape)
                // _form.Close();
            };
        }
Пример #24
0
        public bool Initialize(Form form)
        {
            this.form = form;
            SwapChainDescription desc = new SwapChainDescription()
            {
                BufferCount       = 1,
                Usage             = Usage.RenderTargetOutput,
                OutputHandle      = form.Handle,
                IsWindowed        = true,
                ModeDescription   = new ModeDescription(0, 0, new Rational(60, 1), Format.R8G8B8A8_UNorm),
                SampleDescription = new SampleDescription(1, 0),
                Flags             = SwapChainFlags.AllowModeSwitch,
                SwapEffect        = SwapEffect.Discard
            };

            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain);

            context = device.ImmediateContext;

            factory = swapChain.GetParent <Factory>();
            factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            var vertexShaderByteCode = ShaderBytecode.CompileFromFile("MiniCube.fx", "VS", "vs_4_0");

            vertexShader = new VertexShader(device, vertexShaderByteCode);
            var pixelShaderByteCode = ShaderBytecode.CompileFromFile("MiniCube.fx", "PS", "ps_4_0");

            pixelShader = new PixelShader(device, pixelShaderByteCode);

            signature = ShaderSignature.GetInputSignature(vertexShaderByteCode);

            vertexShaderByteCode.Dispose();
            pixelShaderByteCode.Dispose();

            // Layout from VertexShader input signature
            layout = new InputLayout(device, signature, new[]
            {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
            });

            // Instantiate Vertex buiffer from vertex data
            vertexBuffer = Buffer.Create(device, BindFlags.VertexBuffer, new[]
            {
                new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),                       // Front
                new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),
                new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f),

                new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),                        // BACK
                new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f),

                new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),                        // Top
                new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f),

                new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),                       // Bottom
                new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),
                new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f),

                new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),                       // Left
                new Vector4(-1.0f, -1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(-1.0f, -1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(-1.0f, 1.0f, 1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),
                new Vector4(-1.0f, 1.0f, -1.0f, 1.0f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f),

                new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),                        // Right
                new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
                new Vector4(1.0f, -1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
                new Vector4(1.0f, -1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
                new Vector4(1.0f, 1.0f, -1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
                new Vector4(1.0f, 1.0f, 1.0f, 1.0f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f),
            });

            // Create Constant Buffer
            contantBuffer = new Buffer(device, Utilities.SizeOf <Matrix>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            // Prepare All the stages
            context.InputAssembler.InputLayout       = layout;
            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, Utilities.SizeOf <Vector4>() * 2, 0));
            context.VertexShader.SetConstantBuffer(0, contantBuffer);
            context.VertexShader.Set(vertexShader);
            context.PixelShader.Set(pixelShader);

            // Prepare matrices
            //            viewMatrix = Matrix.LookAtLH(new Vector3(0, 0, -5), new Vector3(0, 0, 0), Vector3.UnitY);
            projMatrix = Matrix.Identity;

            context.OutputMerger.SetTargets(renderTarget);
            userResized = true;

            clock = new Stopwatch();
            clock.Start();

            form.ResizeEnd += (sender, args) => userResized = true;

            form.KeyUp += (sender, args) =>
            {
                if (args.KeyCode == Keys.F5)
                {
                    swapChain.SetFullscreenState(true, null);
                }
                else if (args.KeyCode == Keys.F4)
                {
                    swapChain.SetFullscreenState(false, null);
                }
                else if (args.KeyCode == Keys.Escape)
                {
                    form.Close();
                }
            };

            return(true);
        }
Пример #25
0
        private void _renderForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
            //元素 P-点 L-线 T-三角形
            case System.Windows.Forms.Keys.P:
                _d3DDeviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.PointList;
                break;

            case System.Windows.Forms.Keys.L:
                _d3DDeviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.LineList;
                break;

            case System.Windows.Forms.Keys.T:
                _d3DDeviceContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
                break;

            //全屏
            case System.Windows.Forms.Keys.F5:
                _swapChain.SetFullscreenState(true, null);
                _resized = true;
                break;

            case System.Windows.Forms.Keys.F6:
                _swapChain.SetFullscreenState(false, null);
                _resized = true;
                break;

            //移动摄像机:
            case System.Windows.Forms.Keys.W:
                camPos += camLook * 1f;
                break;

            case System.Windows.Forms.Keys.S:
                camPos -= camLook * 1f;
                break;

            case System.Windows.Forms.Keys.A:
                camPos -= camRight * 1f;
                break;

            case System.Windows.Forms.Keys.D:
                camPos += camRight * 1f;
                break;

            case System.Windows.Forms.Keys.Space:
                camPos.Y += 1f;
                break;

            case System.Windows.Forms.Keys.Z:
                camPos.Y -= 1f;
                break;

            case System.Windows.Forms.Keys.Escape:
                _renderForm.Close();
                break;

            // 切换pass
            case System.Windows.Forms.Keys.F1:
                mfxPass = mfxPassW;
                break;

            case System.Windows.Forms.Keys.F2:
                mfxPass = mfxPassS;
                break;

            default:
                break;
            }
        }