コード例 #1
0
        private void CreateTexture(int width, int height)
        {
            var description = new Texture2DDescription();

            description.ArraySize                    = 1;
            description.BindingOptions               = BindingOptions.RenderTarget | BindingOptions.ShaderResource;
            description.CpuAccessOptions             = CpuAccessOptions.None;
            description.Format                       = Format.B8G8R8A8UNorm;
            description.MipLevels                    = 1;
            description.MiscellaneousResourceOptions = MiscellaneousResourceOptions.Shared;
            description.SampleDescription            = new SampleDescription(1, 0);
            description.Usage = Usage.Default;

            description.Height = (uint)height;
            description.Width  = (uint)width;

            // Assign result to temporary variable in case CreateTexture2D throws
            Texture2D texture = _device.CreateTexture2D(description);

            if (_texture != null)
            {
                _texture.Dispose();
            }
            _texture = texture;
        }
コード例 #2
0
        void InitDevice()
        {
            // create Direct 3D device
            device    = D3DDevice.CreateDeviceAndSwapChain(renderHost.Handle);
            swapChain = device.SwapChain;

            // Create a render target view
            using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            // Create depth stencil texture
            Texture2DDescription descDepth = new Texture2DDescription()
            {
                Width             = (uint)renderHost.ActualWidth,
                Height            = (uint)renderHost.ActualHeight,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.D32Float,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0
                },
                BindingOptions = BindingOptions.DepthStencil,
            };

            depthStencil = device.CreateTexture2D(descDepth);

            // Create the depth stencil view
            DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
            {
                Format        = descDepth.Format,
                ViewDimension = DepthStencilViewDimension.Texture2D
            };

            depthStencilView = device.CreateDepthStencilView(depthStencil, depthStencilViewDesc);

            // bind the views to the device
            device.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { renderTargetView }, depthStencilView);

            // Setup the viewport
            Viewport vp = new Viewport()
            {
                Width    = (uint)renderHost.ActualWidth,
                Height   = (uint)renderHost.ActualHeight,
                MinDepth = 0.0f,
                MaxDepth = 1.0f,
                TopLeftX = 0,
                TopLeftY = 0
            };

            device.RS.Viewports = new Viewport[] { vp };
        }
コード例 #3
0
        void InitDevice()
        {
            // create Direct 3D device
            device = D3DDevice.CreateDeviceAndSwapChain( renderHost.Handle, out swapChain );

            // Create a render target view
            using (Texture2D pBuffer = swapChain.GetBuffer<Texture2D>(0))
            {
                renderTargetView = device.CreateRenderTargetView(pBuffer);
            }

            // Create depth stencil texture
            Texture2DDescription descDepth = new Texture2DDescription()
            {
                Width = (uint)renderHost.ActualWidth,
                Height = (uint)renderHost.ActualHeight,
                MipLevels = 1,
                ArraySize = 1,
                Format = Format.D32_FLOAT,
                SampleDescription = new SampleDescription()
                {
                    Count = 1,
                    Quality = 0
                },
                BindFlags = BindFlag.DepthStencil,
            };

            depthStencil = device.CreateTexture2D(descDepth);

            // Create the depth stencil view
            DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
            {
                Format = descDepth.Format,
                ViewDimension = DepthStencilViewDimension.Texture2D
            };
            depthStencilView = device.CreateDepthStencilView(depthStencil, depthStencilViewDesc);

            // bind the views to the device
            device.OM.SetRenderTargets(new RenderTargetView[] { renderTargetView }, depthStencilView );

            // Setup the viewport
            Viewport vp = new Viewport()
            {
                Width = (uint)renderHost.ActualWidth,
                Height = (uint)renderHost.ActualHeight,
                MinDepth = 0.0f,
                MaxDepth = 1.0f,
                TopLeftX = 0,
                TopLeftY = 0
            };
            
            device.RS.SetViewports(new Viewport[] { vp });
        }
コード例 #4
0
        protected virtual void BufferSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (dxInitialized)
            {
                Texture2DDescription tdesc = new Texture2DDescription
                {
                    ArraySize         = 1,
                    Width             = (uint)e.NewSize.Width,
                    Height            = (uint)e.NewSize.Height,
                    Format            = WindowsAPICodePack.DirectX.Graphics.Format.B8G8R8A8UNorm,
                    MipLevels         = 1,
                    SampleDescription = new WindowsAPICodePack.DirectX.Graphics.SampleDescription()
                    {
                        Count = 1, Quality = 0
                    },
                    Usage          = Usage.Default,
                    BindingOptions = BindingOptions.RenderTarget | BindingOptions.ShaderResource,
                    MiscellaneousResourceOptions = MiscellaneousResourceOptions.Shared,
                    CpuAccessOptions             = CpuAccessOptions.None
                };

                using (Texture2D texture2D = device.CreateTexture2D(tdesc))
                {
                    renderTargetView = device.CreateRenderTargetView(texture2D);
                    deviceContext.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { renderTargetView });

                    /* Set the backbuffer, which is a ID3D11Texture2D pointer */
                    d3dImage.SetBackBufferEx(D3DResourceTypeEx.ID3D11Texture2D, texture2D.NativeInterface);
                }

                // viewport
                SwapChainDescription desc     = swapChain.Description;
                Viewport             viewport = new Viewport();
                viewport.Width    = (float)e.NewSize.Width;
                viewport.Height   = (float)e.NewSize.Height;
                viewport.MinDepth = 0.0f;
                viewport.MaxDepth = 1.0f;
                viewport.TopLeftX = 0;
                viewport.TopLeftY = 0;

                deviceContext.RS.Viewports = new Viewport[] { viewport };

                aspectRatio = e.NewSize.Width / e.NewSize.Height;

                Update();
            }
        }
コード例 #5
0
        protected virtual void BufferSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (dxInitialized)
            {
                Texture2DDescription tdesc = new Texture2DDescription
                {
                    ArraySize         = 1,
                    Width             = (uint)e.NewSize.Width,
                    Height            = (uint)e.NewSize.Height,
                    Format            = Format.B8G8R8A8_UNORM,
                    MipLevels         = 1,
                    SampleDescription = new SampleDescription {
                        Count = 1, Quality = 0
                    },
                    Usage          = Usage.Default,
                    BindFlags      = BindFlag.RenderTarget | BindFlag.ShaderResource,
                    MiscFlags      = ResourceMiscFlag.Shared,
                    CpuAccessFlags = 0
                };

                using (Texture2D texture2D = device.CreateTexture2D(tdesc))
                {
                    renderTargetView = device.CreateRenderTargetView(texture2D);
                    deviceContext.OM.SetRenderTargets(new RenderTargetView[] { renderTargetView });

                    /* Set the backbuffer, which is a ID3D11Texture2D pointer */
                    d3dImage.SetBackBufferEx(D3DResourceTypeEx.ID3D11Texture2D, texture2D.NativeInterface);
                }

                // viewport
                SwapChainDescription desc     = swapChain.Description;
                Viewport             viewport = new Viewport();
                viewport.Width    = (float)e.NewSize.Width;
                viewport.Height   = (float)e.NewSize.Height;
                viewport.MinDepth = 0.0f;
                viewport.MaxDepth = 1.0f;
                viewport.TopLeftX = 0;
                viewport.TopLeftY = 0;

                deviceContext.RS.SetViewports(new Viewport[] { viewport });

                aspectRatio = e.NewSize.Width / e.NewSize.Height;

                Update();
            }
        }
コード例 #6
0
        private void InitializeDX()
        {
            //perform DX Device and Context initialization
            HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "dxPlotterElement", IntPtr.Zero);

            device        = D3DDevice.CreateDeviceAndSwapChain(hwnd.Handle);
            swapChain     = device.SwapChain;
            deviceContext = device.ImmediateContext;

            CompositionTarget.Rendering += OnRenderingProccessReady;

            Texture2DDescription tdesc = new Texture2DDescription
            {
                ArraySize         = 1,
                Width             = 1,
                Height            = 1,
                Format            = Format.B8G8R8A8UNorm,
                MipLevels         = 1,
                SampleDescription = new SampleDescription {
                    Count = 1, Quality = 0
                },
                Usage          = Usage.Default,
                BindingOptions = BindingOptions.RenderTarget | BindingOptions.ShaderResource,
                MiscellaneousResourceOptions = MiscellaneousResourceOptions.Shared,
                CpuAccessOptions             = CpuAccessOptions.None
            };

            using (Texture2D texture2D = device.CreateTexture2D(tdesc))
            {
                renderTargetView = device.CreateRenderTargetView(texture2D);
                deviceContext.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { renderTargetView });

                d3dImage.SetBackBufferEx(D3DResourceTypeEx.ID3D11Texture2D, texture2D.NativeInterface);
            }

            dxInitialized = true;
            OnDeviceInitialized();
        }
コード例 #7
0
        private void InitializeDX()
        {
            //perform DX Device and Context initialization
            HwndSource hwnd = new HwndSource(0, 0, 0, 0, 0, "dxPlotterElement", IntPtr.Zero);
            device = D3DDevice.CreateDeviceAndSwapChain(hwnd.Handle, out swapChain);
            deviceContext = device.GetImmediateContext();

            CompositionTarget.Rendering += OnRenderingProccessReady; 

            Texture2DDescription tdesc = new Texture2DDescription
            {
                ArraySize = 1,
                Width = 1,
                Height = 1,
                Format = Format.B8G8R8A8_UNORM,
                MipLevels = 1,
                SampleDescription = new SampleDescription { Count = 1, Quality = 0 },
                Usage = Usage.Default,
                BindFlags = BindFlag.RenderTarget | BindFlag.ShaderResource,
                MiscFlags = ResourceMiscFlag.Shared,
                CpuAccessFlags = 0
            };

            using (Texture2D texture2D = device.CreateTexture2D(tdesc))
            {
                renderTargetView = device.CreateRenderTargetView(texture2D);
                deviceContext.OM.SetRenderTargets(new RenderTargetView[] { renderTargetView });

                d3dImage.SetBackBufferEx(D3DResourceTypeEx.ID3D11Texture2D, texture2D.NativeInterface);
            }

            dxInitialized = true;
            OnDeviceInitialized();

        }
コード例 #8
0
        void Window1_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (device != null)
            {
                //need to remove the reference to the swapchain's backbuffer to enable ResizeBuffers() call
                renderTargetView.Dispose();
                SwapChainDescription sd = swapChain.Description;
                swapChain.ResizeBuffers(
                    sd.BufferCount,
                    (uint)renderHost.ActualWidth,
                    (uint)renderHost.ActualHeight,
                    sd.BufferDescription.Format,
                    sd.Options);

                using (Texture2D pBuffer = swapChain.GetBuffer <Texture2D>(0))
                {
                    renderTargetView = device.CreateRenderTargetView(pBuffer);
                }

                // Create depth stencil texture
                Texture2DDescription descDepth = new Texture2DDescription()
                {
                    Width             = (uint)renderHost.ActualWidth,
                    Height            = (uint)renderHost.ActualHeight,
                    MipLevels         = 1,
                    ArraySize         = 1,
                    Format            = Format.D32Float,
                    SampleDescription = new SampleDescription()
                    {
                        Count   = 1,
                        Quality = 0
                    },
                    BindingOptions = BindingOptions.DepthStencil,
                };

                depthStencil = device.CreateTexture2D(descDepth);

                // Create the depth stencil view
                DepthStencilViewDescription depthStencilViewDesc = new DepthStencilViewDescription()
                {
                    Format        = descDepth.Format,
                    ViewDimension = DepthStencilViewDimension.Texture2D
                };
                depthStencilView = device.CreateDepthStencilView(depthStencil, depthStencilViewDesc);

                // bind the views to the device
                device.OM.RenderTargets = new OutputMergerRenderTargets(new RenderTargetView[] { renderTargetView }, depthStencilView);

                // Setup the viewport
                Viewport vp = new Viewport()
                {
                    Width    = (uint)renderHost.ActualWidth,
                    Height   = (uint)renderHost.ActualHeight,
                    MinDepth = 0.0f,
                    MaxDepth = 1.0f,
                    TopLeftX = 0,
                    TopLeftY = 0
                };

                device.RS.Viewports = new Viewport[] { vp };
            }
        }
コード例 #9
0
        private static ShaderResourceView LoadFromDecoder(D3DDevice device, ImagingFactory factory, BitmapDecoder bitmapDecoder)
        {
            if (bitmapDecoder.FrameCount == 0)
                throw new ArgumentException("Image file successfully loaded, but it has no image frames.");

            BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);
            BitmapSource bitmapSource = bitmapFrameDecode.ToBitmapSource();

            // create texture description
            Texture2DDescription textureDescription = new Texture2DDescription()
            {
                Width = bitmapSource.Size.Width,
                Height = bitmapSource.Size.Height,
                MipLevels = 1,
                ArraySize = 1,
                Format = Format.R8G8B8A8_UNORM,
                SampleDescription = new SampleDescription()
                {
                    Count = 1,
                    Quality = 0,
                },
                Usage = Usage.Dynamic,
                BindFlags = BindFlag.ShaderResource,
                CpuAccessFlags = CpuAccessFlag.Write,
                MiscFlags = 0
            };

            // create texture
            Texture2D texture = device.CreateTexture2D(textureDescription);

            // Create a format converter
            WICFormatConverter converter = factory.CreateFormatConverter();
            converter.Initialize(
                bitmapSource,
                PixelFormats.Pf32bppRGBA,
                BitmapDitherType.None,
                BitmapPaletteType.Custom);

            // get bitmap data
            byte[] buffer = converter.CopyPixels();

            // Copy bitmap data to texture
            MappedTexture2D texmap = texture.Map(0, Map.WriteDiscard, MapFlag.Unspecified);
            Marshal.Copy(buffer, 0, texmap.Data, buffer.Length);
            texture.Unmap(0);

            // create shader resource view description
            ShaderResourceViewDescription srvDescription = new ShaderResourceViewDescription()
            {
                Format = textureDescription.Format,
                ViewDimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new Texture2DShaderResourceView()
                {
                    MipLevels = textureDescription.MipLevels,
                    MostDetailedMip = 0
                }
            };

            // create shader resource view from texture
            return device.CreateShaderResourceView(texture, srvDescription);
        }
コード例 #10
0
ファイル: TextureLoader.cs プロジェクト: kagada/Arianrhod
        private static ShaderResourceView LoadFromDecoder(D3DDevice device, ImagingFactory factory, BitmapDecoder bitmapDecoder)
        {
            if (bitmapDecoder.FrameCount == 0)
            {
                throw new ArgumentException("Image file successfully loaded, but it has no image frames.");
            }

            BitmapFrameDecode bitmapFrameDecode = bitmapDecoder.GetFrame(0);
            BitmapSource      bitmapSource      = bitmapFrameDecode.ToBitmapSource();

            // create texture description
            Texture2DDescription textureDescription = new Texture2DDescription()
            {
                Width             = bitmapSource.Size.Width,
                Height            = bitmapSource.Size.Height,
                MipLevels         = 1,
                ArraySize         = 1,
                Format            = Format.R8G8B8A8UNorm,
                SampleDescription = new SampleDescription()
                {
                    Count   = 1,
                    Quality = 0,
                },
                Usage                        = Usage.Dynamic,
                BindingOptions               = BindingOptions.ShaderResource,
                CpuAccessOptions             = CpuAccessOptions.Write,
                MiscellaneousResourceOptions = MiscellaneousResourceOptions.None
            };

            // create texture
            Texture2D texture = device.CreateTexture2D(textureDescription);

            // Create a format converter
            FormatConverter converter = factory.CreateFormatConverter();

            converter.Initialize(
                bitmapSource,
                PixelFormats.Prgba32Bpp,
                BitmapDitherType.None,
                BitmapPaletteType.Custom);

            // get bitmap data
            byte[] buffer = converter.CopyPixels();

            // Copy bitmap data to texture
            MappedTexture2D texmap = texture.Map(0, Map.WriteDiscard, Microsoft.WindowsAPICodePack.DirectX.Direct3D10.MapOptions.None);

            Marshal.Copy(buffer, 0, texmap.Data, buffer.Length);
            texture.Unmap(0);

            // create shader resource view description
            ShaderResourceViewDescription srvDescription = new ShaderResourceViewDescription()
            {
                Format        = textureDescription.Format,
                ViewDimension = ShaderResourceViewDimension.Texture2D,
                Texture2D     = new Texture2DShaderResourceView()
                {
                    MipLevels       = textureDescription.MipLevels,
                    MostDetailedMip = 0
                }
            };

            // create shader resource view from texture
            return(device.CreateShaderResourceView(texture, srvDescription));
        }