Ejemplo n.º 1
0
        public override void Init(IntPtr display, IntPtr window, uint samples, bool vsync, bool sRGB)
        {
            _display = display;
            _window  = window;
            _vsync   = vsync;

            FeatureLevel[] features =
            {
                FeatureLevel.Level_11_1,
                FeatureLevel.Level_11_0
            };

            SharpDX.Direct3D11.Device dev11 = new SharpDX.Direct3D11.Device(DriverType.Hardware, DeviceCreationFlags.None, features);
            _dev = dev11.QueryInterfaceOrNull <SharpDX.Direct3D11.Device1>();

            SampleDescription sampleDesc = new SampleDescription();

            do
            {
                if (_dev.CheckMultisampleQualityLevels(Format.R8G8B8A8_UNorm, (int)samples) > 0)
                {
                    sampleDesc.Count   = (int)samples;
                    sampleDesc.Quality = 0;
                    break;
                }
                else
                {
                    samples >>= 1;
                }
            } while (samples > 0);

            SwapChainDescription1 desc = new SwapChainDescription1();

            desc.Width             = 0;
            desc.Height            = 0;
            desc.Format            = sRGB ? Format.R8G8B8A8_UNorm_SRgb : Format.R8G8B8A8_UNorm;
            desc.Scaling           = Scaling.None;
            desc.SampleDescription = sampleDesc;
            desc.Usage             = Usage.RenderTargetOutput;
            desc.BufferCount       = 2;
            desc.SwapEffect        = SwapEffect.FlipSequential;
            desc.Flags             = SwapChainFlags.None;

            SharpDX.DXGI.Device2 dev = _dev.QueryInterface <SharpDX.DXGI.Device2>();
            Adapter  adapter         = dev.Adapter;
            Factory2 factory         = adapter.GetParent <Factory2>();

            if (display == IntPtr.Zero)
            {
                GCHandle          handle     = (GCHandle)window;
                SharpDX.ComObject coreWindow = new SharpDX.ComObject(handle.Target as object);
                _swapChain = new SwapChain1(factory, _dev, coreWindow, ref desc);
            }
            else
            {
                _swapChain = new SwapChain1(factory, _dev, window, ref desc);
            }

            _device = new RenderDeviceD3D11(_dev.ImmediateContext.NativePointer, sRGB);
        }
Ejemplo n.º 2
0
        private static void OnContentLoaded()
        {
            Log.LogCallback += (level, channel, message) =>
            {
                if (channel == "")
                {
                    // [TRACE] [DEBUG] [INFO] [WARNING] [ERROR]
                    string[] prefixes = new string[] { "T", "D", "I", "W", "E" };
                    string   prefix   = (int)level < prefixes.Length ? prefixes[(int)level] : " ";
                    Console.WriteLine("[NOESIS/" + prefix + "] " + message);
                }
            };

            GUI.Init();

            // Setup embedded resource providers
            EmbeddedXaml[] xamls = new EmbeddedXaml[]
            {
                new EmbeddedXaml {
                    filename = "Reflections.xaml", resource = "Reflections"
                }
            };
            GUI.SetXamlProvider(new EmbeddedXamlProvider(xamls, Properties.Resources.ResourceManager));

            // Data loading
            {
                var content = (Noesis.Grid)Noesis.GUI.LoadXaml("Reflections.xaml");
                view = Noesis.GUI.CreateView(content);
                IntPtr       deviceContext = app.Device.ImmediateContext.NativePointer;
                RenderDevice device        = new RenderDeviceD3D11(deviceContext);
                renderer = view.Renderer;
                renderer.Init(device);
            }

            // Attach to Application events
            app.Close      += OnClose;
            app.Tick       += OnTick;
            app.PreRender  += OnPreRender;
            app.PostRender += OnPostRender;
            app.Resize     += OnResize;
            app.MouseMove  += OnMouseMove;
            app.MouseDown  += OnMouseDown;
            app.MouseUp    += OnMouseUp;

            OnResize(app.Size.Width, app.Size.Height);

            isRunning = true;
        }
Ejemplo n.º 3
0
        public override ImageCapture CaptureRenderTarget(RenderTarget surface)
        {
            IntPtr             texNativePtr = RenderDeviceD3D11.GetTextureNativePointer(surface.Texture);
            ShaderResourceView view         = new ShaderResourceView(texNativePtr);

            using (Texture2D source = view.Resource.QueryInterface <Texture2D>())
            {
                Texture2DDescription desc = source.Description;
                desc.CpuAccessFlags = CpuAccessFlags.Read;
                desc.Usage          = ResourceUsage.Staging;
                desc.BindFlags      = BindFlags.None;

                using (Texture2D dest = new Texture2D(_dev, desc))
                {
                    _dev.ImmediateContext.CopyResource(source, dest);

                    SharpDX.DataBox data = _dev.ImmediateContext.MapSubresource(dest, 0,
                                                                                MapMode.Read, SharpDX.Direct3D11.MapFlags.None);

                    if (_imageCapture == null || _imageCapture.Width != desc.Width || _imageCapture.Height != desc.Height)
                    {
                        _imageCapture = new ImageCapture((uint)desc.Width, (uint)desc.Height);
                    }

                    byte[] dst = _imageCapture.Pixels;
                    IntPtr src = data.DataPointer;

                    for (int i = 0; i < desc.Height; ++i)
                    {
                        int dstRow = i * (int)_imageCapture.Stride;
                        int srcRow = i * data.RowPitch;

                        for (int j = 0; j < desc.Width; j++)
                        {
                            // RGBA -> BGRA
                            dst[dstRow + 4 * j + 2] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 0);
                            dst[dstRow + 4 * j + 1] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 1);
                            dst[dstRow + 4 * j + 0] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 2);
                            dst[dstRow + 4 * j + 3] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 3);
                        }
                    }

                    _dev.ImmediateContext.UnmapSubresource(dest, 0);
                }
            }

            return(_imageCapture);
        }
Ejemplo n.º 4
0
        public override ImageCapture CaptureRenderTarget(RenderTarget surface)
        {
            IntPtr texNativePtr = RenderDeviceD3D11.GetTextureNativePointer(surface.Texture);

            SharpDX.Direct3D12.Resource            texture = new SharpDX.Direct3D12.Resource(texNativePtr);
            SharpDX.Direct3D12.ResourceDescription texDesc = texture.Description;

            // Transition PIXEL_SHADER_RESOURCE -> COPY_SOURCE
            SharpDX.Direct3D12.ResourceBarrier barrier = new SharpDX.Direct3D12.ResourceBarrier();
            barrier.Type       = SharpDX.Direct3D12.ResourceBarrierType.Transition;
            barrier.Flags      = SharpDX.Direct3D12.ResourceBarrierFlags.None;
            barrier.Transition = new SharpDX.Direct3D12.ResourceTransitionBarrier(texture,
                                                                                  SharpDX.Direct3D12.ResourceStates.PixelShaderResource,
                                                                                  SharpDX.Direct3D12.ResourceStates.CopySource)
            {
                Subresource = 0
            };

            _commands.ResourceBarrier(barrier);

            long totalBytes;

            long[] rowSize = { 0 };
            _dev.GetCopyableFootprints(ref texDesc, 0, 1, 0, null, null, rowSize, out totalBytes);
            long pitch = (rowSize[0] + 255) & ~255;

            // Create staging texture
            SharpDX.Direct3D12.HeapProperties heap;
            heap.Type                 = SharpDX.Direct3D12.HeapType.Readback;
            heap.CPUPageProperty      = SharpDX.Direct3D12.CpuPageProperty.Unknown;
            heap.MemoryPoolPreference = SharpDX.Direct3D12.MemoryPool.Unknown;
            heap.CreationNodeMask     = 0;
            heap.VisibleNodeMask      = 0;

            SharpDX.Direct3D12.ResourceDescription desc;
            desc.Dimension         = SharpDX.Direct3D12.ResourceDimension.Buffer;
            desc.Alignment         = 0;
            desc.Width             = pitch * texDesc.Height * 4;
            desc.Height            = 1;
            desc.DepthOrArraySize  = 1;
            desc.MipLevels         = 1;
            desc.Format            = SharpDX.DXGI.Format.Unknown;
            desc.SampleDescription = new SampleDescription {
                Count = 1, Quality = 0
            };
            desc.Layout = SharpDX.Direct3D12.TextureLayout.RowMajor;
            desc.Flags  = SharpDX.Direct3D12.ResourceFlags.None;

            SharpDX.Direct3D12.Resource stagingTex = _dev.CreateCommittedResource(heap, SharpDX.Direct3D12.HeapFlags.None,
                                                                                  desc, SharpDX.Direct3D12.ResourceStates.CopyDestination, null);

            SharpDX.Direct3D12.TextureCopyLocation copySrc = new SharpDX.Direct3D12.TextureCopyLocation(texture, 0);
            SharpDX.Direct3D12.TextureCopyLocation copyDst = new SharpDX.Direct3D12.TextureCopyLocation(texture,
                                                                                                        new SharpDX.Direct3D12.PlacedSubResourceFootprint
            {
                Offset    = 0,
                Footprint = new SharpDX.Direct3D12.SubResourceFootprint
                {
                    Width    = (int)texDesc.Width,
                    Height   = (int)texDesc.Height,
                    Depth    = 1,
                    RowPitch = (int)pitch,
                    Format   = texDesc.Format
                }
            });

            // Wait GPU completion
            _commands.CopyTextureRegion(copyDst, 0, 0, 0, copySrc, null);
            _commands.Close();
            _queue.ExecuteCommandList(_commands);
            WaitForGpu();

            // Ensure image capture object is created with the appropriate size
            if (_imageCapture == null || _imageCapture.Width != desc.Width || _imageCapture.Height != desc.Height)
            {
                _imageCapture = new ImageCapture((uint)desc.Width, (uint)desc.Height);
            }

            // Map and copy to image
            byte[] dst = _imageCapture.Pixels;
            IntPtr src = stagingTex.Map(0);

            for (int i = 0; i < desc.Height; ++i)
            {
                int dstRow = i * (int)_imageCapture.Stride;
                int srcRow = i * (int)pitch;

                for (int j = 0; j < desc.Width; j++)
                {
                    // RGBA -> BGRA
                    dst[dstRow + 4 * j + 2] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 0);
                    dst[dstRow + 4 * j + 1] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 1);
                    dst[dstRow + 4 * j + 0] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 2);
                    dst[dstRow + 4 * j + 3] = Noesis.Marshal.ReadByte(src, srcRow + 4 * j + 3);
                }
            }

            stagingTex.Unmap(0);

            return(_imageCapture);
        }