Esempio n. 1
0
        // TODO: Switch to lower quality pixel Format to reduce memory overhead
        // TODO: Find memory leak on move and resize
        public D3Drenderer(GraphView_StateManager manager, View.GraphView_CurveView View)
        {
            ((GameEditor_GraphView.ViewModel.CurveGraphViewModel)manager.ViewModel).Items.CollectionChanged += ViewModelChanged;
            Configuration.EnableObjectTracking = true;

            screenWidth  = (float)SystemParameters.PrimaryScreenWidth;
            screenHeight = (float)SystemParameters.PrimaryScreenHeight;

            _RenderForm = new RenderForm("");
            SetRenderDimensions(2048, 1280);

            //_RenderForm.
            _RenderForm.TopLevel        = false;
            _RenderForm.FormBorderStyle = FormBorderStyle.None;

            CreateSwapChain();

            manager.RefRenderer(this);
            mre = new ManualResetEvent(false);
            manager.SetupResetEvent(ref mre);

            // Create Device and set device context
            // D3D11 Device
            Device.CreateWithSwapChain(
                DriverType.Hardware,
                DeviceCreationFlags.BgraSupport,
                _SwapDesc,
                out _device, out _swapChain);

            _context = _device.ImmediateContext;
        }
        public GraphView_MainView()
        {
            InitializeComponent();

            //GraphView = new Canvas();
            GraphView = new GraphView_CurveView();
            GraphView.ClipToBounds = true;
            MenuView = new GraphView_MenuView(this);

            GraphMainGrid.Children.Add(MenuView);
            Grid.SetRow(MenuView, 0);

            GraphMainGrid.Children.Add(GraphView);
            Grid.SetRow(GraphView, 1);

            GraphView.MouseMove += MenuView.sodoff;
            this.Loaded         += GraphView_CurveView_Loaded;
        }
Esempio n. 3
0
        /// <summary>
        /// Starts render loop
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="View"></param>
        public void Render(GraphView_StateManager manager, View.GraphView_CurveView View)
        {
            var signature = CreateShaders("Smooth.fx");
            //var signature = CreateShaders();
            var layout = new InputLayout(_device, signature, new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32B32A32_Float, 32, 0)
            });

            SetSamplerState();

            // Instantiate buffers
            CreateVertexBuffer();
            CreateConstantBuffer();

            // Prepare Context
            SetUpContext(layout);

            // Prepare matrices
            var    view = Matrix.OrthoLH(_RenderForm.ClientSize.Width, _RenderForm.ClientSize.Width, 0.00f, 10000.0f);
            Matrix proj = Matrix.Identity;

            // Declare texture for rendering
            bool             userResized = true;
            Texture2D        backBuffer  = null;
            RenderTargetView renderView  = null;
            Texture2D        depthBuffer = null;
            DepthStencilView depthView   = null;

            var camera = ((ViewModel.CurveGraphViewModel)manager.ViewModel).Camera;

            var rastDesc = new RasterizerStateDescription {
                FillMode = FillMode.Solid,
                CullMode = CullMode.None,
                IsAntialiasedLineEnabled = true,
                IsMultisampleEnabled     = true
            };

            var viewPort = new Viewport(0, 0, _RenderForm.ClientSize.Width, _RenderForm.ClientSize.Height);

            _RenderForm.UserResized += (sender, args) => userResized = true;


            // Main loop
            RenderLoop.Run(_RenderForm, () => {
                //TODO: Check for if collectionchanged event has been raised and trigger vertex recalculation

                if (IsViewModelChanged || !MessageQueue.IsEmpty || true)
                {
                    CreateSwapChain();

                    IsViewModelChanged = false;

                    if (true)
                    {
                        if (WindowResized())
                        {
                            userResized = true;
                        }

                        SetSamplerState();
                    }

                    // If Form resized
                    if (userResized)
                    {
                        Console.WriteLine("resizing");
                        // Dispose all previous allocated resources
                        Utilities.Dispose(ref backBuffer);
                        Utilities.Dispose(ref renderView);
                        Utilities.Dispose(ref depthBuffer);
                        Utilities.Dispose(ref depthView);

                        // Resizing buffers
                        _swapChain.ResizeBuffers(
                            _SwapDesc.BufferCount,
                            //2048,
                            //1280,
                            _RenderForm.Width,
                            _RenderForm.Height,
                            Format.B8G8R8A8_UNorm,
                            SwapChainFlags.None
                            );

                        backBuffer = Texture2D.FromSwapChain <Texture2D>(_swapChain, 0);
                        renderView = new RenderTargetView(_device, backBuffer);

                        // Create the depth buffer
                        depthBuffer = new Texture2D(_device, new Texture2DDescription()
                        {
                            Format    = Format.D32_Float_S8X24_UInt,
                            ArraySize = 1,
                            MipLevels = 1,
                            //Width = 2048,
                            //Height = 1280,
                            Width             = _RenderForm.Width,
                            Height            = _RenderForm.Height,
                            SampleDescription = new SampleDescription(1, 0),
                            Usage             = ResourceUsage.Default,
                            BindFlags         = BindFlags.DepthStencil,
                            CpuAccessFlags    = CpuAccessFlags.None,
                            OptionFlags       = ResourceOptionFlags.None
                        });

                        // Create the depth buffer view
                        depthView       = new DepthStencilView(_device, depthBuffer);
                        viewPort.Width  = _RenderForm.ClientSize.Width;
                        viewPort.Height = _RenderForm.ClientSize.Height;

                        // Setup targets and viewport for rendering
                        _context.Rasterizer.SetViewport(viewPort);
                        _context.OutputMerger.SetTargets(depthView, renderView);
                        _context.Rasterizer.State = new RasterizerState(_device, rastDesc);

                        userResized = false;
                    }

                    Thread vertexThread = new Thread(() => CalculateQuads(manager));
                    vertexThread.Start();

                    // World Matrix
                    // The World Matrix translates the position of your vertices from model space to World space.
                    // That means it applies its position in the world and its rotation
                    var world = CalculateWorldTransform(camera);

                    // A view matrix tells the GPU the position of the camera, the point the camera is facing,
                    // and the up vector for the camera
                    view = Matrix.LookAtLH(new Vector3(0, 0, -100), new Vector3(0, 0, 0), Vector3.Up);

                    // A projection matrix tells the GPU the FOV, aspect ratio, and near and far clipping planes for the camera
                    //proj = Matrix.OrthoLH(1280, 720, 0.00f, 1000.0f);
                    proj         = Matrix.OrthoLH(screenWidth, screenHeight, 0.00f, 1000.0f);
                    var viewProj = Matrix.Multiply(view, proj);

                    //var worldViewProj = Matrix.Multiply(world, proj);
                    var worldViewProj = Matrix.Multiply(world, viewProj);

                    // Clear views and set background as transparent
                    _context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
                    _context.ClearRenderTargetView(renderView, new SharpDX.Mathematics.Interop.RawColor4(0, 0, 0, 0.0f));

                    var screenPoints = PointToScreenSpace(manager, viewPort, worldViewProj);

                    worldViewProj.Transpose();

                    _context.UpdateSubresource(ref worldViewProj, _ConstantBuffer);

                    // Instantiate Vertex buffer from vertex data
                    vertexThread.Join();
                    CreateVertexBuffer();
                    SetUpContext(layout);

                    // Draw
                    _context.Draw(vertices.Length, 0);

                    // Convert backbuffer to texture, then to bitmap for display
                    D2D1.BitmapRenderTarget target = RenderBitmap();

                    // Get byte array for bitmap
                    byte[] data = CalculateBitmapBytes(backBuffer);

                    DisplayBitmap(target, data, screenPoints, manager);

                    // Dispose all previous allocated resources
                    _context.Flush();
                    screenPoints = null;
                    GC.Collect();

                    if (displayThread != null)
                    {
                        displayThread.Join();
                    }

                    mre.Reset();
                    mre.WaitOne();
                }
                // End of renderloop
            });

            // Release all resources
            _context.ClearState();
            _context.Flush();
            DisposeResources();

            signature.Dispose();
            layout.Dispose();

            depthBuffer.Dispose();
            backBuffer.Dispose();

            depthView.Dispose();
            renderView.Dispose();
        }