/// <summary>
        ///   Initializes a new instance of the <see cref = "T:SharpDX.Direct3D10.Texture2D" /> class.
        /// </summary>
        /// <param name = "device">The device with which to associate the texture.</param>
        /// <param name = "description">The description of the texture.</param>
        /// <param name = "data">An array of initial texture data for each subresource.</param>
        public Texture2D(Device device, Texture2DDescription description, DataRectangle[] data)
            : base(IntPtr.Zero)
        {
            DataBox[] subResourceDatas = null;

            if (data != null)
            {
                subResourceDatas = new DataBox[data.Length];
                for (int i = 0; i < subResourceDatas.Length; i++)
                {
                    subResourceDatas[i].DataPointer = data[i].DataPointer;
                    subResourceDatas[i].RowPitch = data[i].Pitch;
                }
            }
            device.CreateTexture2D(ref description, subResourceDatas, this);
        }
        public BitmapDrawingContext(int width, int height)
        {
            if (width < 0 || height < 0)
                throw new Exception("Negative BitmapDrawingContext's area");

            _width = width;
            _height = height;

            _device = new Device1(DriverType.Hardware, DeviceCreationFlags.BgraSupport, FeatureLevel.Level_10_0);
            var textureDesc = new Texture2DDescription
            {
                MipLevels = 1,
                ArraySize = 1,
                BindFlags = BindFlags.RenderTarget,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.B8G8R8A8_UNorm,
                OptionFlags = ResourceOptionFlags.None,
                Width = _width,
                Height = _height,
                Usage = ResourceUsage.Default,
                SampleDescription = new SampleDescription(1, 0)
            };

            _texture = new Texture2D(_device, textureDesc);
            _factory = new Factory(FactoryType.SingleThreaded, DebugLevel.None);
        }
Exemple #3
0
        public DrawingSurface(DrawingBackend backend, int width, int height)
        {
            if (width < 0 || height < 0)
                throw new Exception("Area of DrawingSurface's is neagative");

            _factory = backend.Factory;
            _device = backend.Device;

            _backend = backend;
            _width = width;
            _height = height;

            var textureDesc = new Texture2DDescription
            {
                MipLevels = 1,
                ArraySize = 1,
                BindFlags = BindFlags.RenderTarget,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.B8G8R8A8_UNorm,
                OptionFlags = ResourceOptionFlags.None,
                Width = _width,
                Height = _height,
                Usage = ResourceUsage.Default,
                SampleDescription = new SampleDescription(1, 0)
            };

            _texture = new Texture2D(_device, textureDesc);
        }
Exemple #4
0
        public StagingTexture(Device device3D)
        {
            Contract.Requires(device3D != null);

            _Device3D = device3D;

            _Description = Descriptions.StagingDescription;
        }
        private Texture2D CreateTexture(int width, int height, bool multiSampling)
        {
            var description = new SharpDX.Direct3D10.Texture2DDescription();

            description.ArraySize      = 1;
            description.BindFlags      = SharpDX.Direct3D10.BindFlags.RenderTarget | SharpDX.Direct3D10.BindFlags.ShaderResource;
            description.CpuAccessFlags = CpuAccessFlags.None;
            description.Format         = Format.B8G8R8A8_UNorm;
            description.MipLevels      = 1;

            // Multi-sample anti-aliasing
            int count, quality;

            if (multiSampling)
            {
                count   = 8;
                quality = device.CheckMultisampleQualityLevels(description.Format, count);
                if (quality == 0)
                {
                    count   = 4;
                    quality = device.CheckMultisampleQualityLevels(description.Format, count);
                }
                if (quality == 0)
                {
                    count = 1;
                }
            }
            else
            {
                count = 1;
            }
            if (count == 1)
            {
                quality = 1;
            }
            SampleDescription sampleDesc = new SampleDescription(count, 0);

            description.SampleDescription = sampleDesc;

            description.Usage       = ResourceUsage.Default;
            description.OptionFlags = ResourceOptionFlags.Shared;
            description.Height      = (int)height;
            description.Width       = (int)width;

            return(new Texture2D(device, description));
        }
 /// <summary>
 ///   Initializes a new instance of the <see cref = "T:SharpDX.Direct3D10.Texture2D" /> class.
 /// </summary>
 /// <param name = "device">The device with which to associate the texture.</param>
 /// <param name = "description">The description of the texture.</param>
 public Texture2D(Device device, Texture2DDescription description)
     : base(IntPtr.Zero)
 {
     device.CreateTexture2D(ref description, null, this);
 }
        void CreateBuffers()
        {
            DisposeBuffers();

            // New RenderTargetView from the backbuffer
            using (var bb = Texture2D.FromSwapChain<Texture2D>(_swapChain, 0))
            {
                renderView = new RenderTargetView(_device, bb);
                renderViews[0] = renderView;
            }

            Texture2DDescription gBufferDesc = new Texture2DDescription()
            {
                ArraySize = 1,
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.R8G8B8A8_UNorm,
                Width = _width,
                Height = _height,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            };

            gBufferLight = new Texture2D(_device, gBufferDesc);
            gBufferLightView = new RenderTargetView(_device, gBufferLight);

            gBufferNormal = new Texture2D(_device, gBufferDesc);
            gBufferNormalView = new RenderTargetView(_device, gBufferNormal);

            gBufferDiffuse = new Texture2D(_device, gBufferDesc);
            gBufferDiffuseView = new RenderTargetView(_device, gBufferDiffuse);

            gBufferViews = new RenderTargetView[] { gBufferLightView, gBufferNormalView, gBufferDiffuseView };

            ShaderResourceViewDescription gBufferResourceDesc = new ShaderResourceViewDescription()
            {
                Format = Format.R8G8B8A8_UNorm,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1,
                    MostDetailedMip = 0
                }
            };
            lightBufferRes = new ShaderResourceView(_device, gBufferLight, gBufferResourceDesc);
            normalBufferRes = new ShaderResourceView(_device, gBufferNormal, gBufferResourceDesc);
            diffuseBufferRes = new ShaderResourceView(_device, gBufferDiffuse, gBufferResourceDesc);

            Texture2DDescription depthDesc = new Texture2DDescription()
            {
                ArraySize = 1,
                BindFlags = BindFlags.DepthStencil | BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.R32_Typeless,
                Width = _width,
                Height = _height,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default
            };

            DepthStencilViewDescription depthViewDesc = new DepthStencilViewDescription()
            {
                Dimension = DepthStencilViewDimension.Texture2D,
                Format = Format.D32_Float,
            };

            ShaderResourceViewDescription resourceDesc = new ShaderResourceViewDescription()
            {
                Format = Format.R32_Float,
                Dimension = ShaderResourceViewDimension.Texture2D,
                Texture2D = new ShaderResourceViewDescription.Texture2DResource()
                {
                    MipLevels = 1,
                    MostDetailedMip = 0
                }
            };

            depthTexture = new Texture2D(_device, depthDesc);
            depthView = new DepthStencilView(_device, depthTexture, depthViewDesc);
            depthRes = new ShaderResourceView(_device, depthTexture, resourceDesc);

            lightDepthTexture = new Texture2D(_device, depthDesc);
            lightDepthView = new DepthStencilView(_device, lightDepthTexture, depthViewDesc);
            lightDepthRes = new ShaderResourceView(_device, lightDepthTexture, resourceDesc);

            lightBufferVar = effect2.GetVariableByName("lightBuffer").AsShaderResource();
            normalBufferVar = effect2.GetVariableByName("normalBuffer").AsShaderResource();
            diffuseBufferVar = effect2.GetVariableByName("diffuseBuffer").AsShaderResource();
            depthMapVar = effect2.GetVariableByName("depthMap").AsShaderResource();
            lightDepthMapVar = effect2.GetVariableByName("lightDepthMap").AsShaderResource();

            _device.Rasterizer.SetViewports(new Viewport(0, 0, _width, _height));
        }
        private void initD3D()
        {
            if (drawForm == null)
                return;

            swapChainDesc = new SwapChainDescription
            {
                BufferCount = 2,
                ModeDescription = new ModeDescription
                {
                    Width = drawForm.Width,
                    Height = drawForm.Height,
                    Format = Format.R8G8B8A8_UNorm,
                },
                Usage = Usage.RenderTargetOutput,
            };

            swapChainDesc.ModeDescription.RefreshRate.Numerator = 60;
            swapChainDesc.ModeDescription.RefreshRate.Denominator = 1;

            swapChainDesc.SampleDescription.Quality = 0;
            swapChainDesc.SampleDescription.Count = 1;

            swapChainDesc.OutputHandle = drawHandle;
            swapChainDesc.IsWindowed = true;

            D3D10.Device.CreateWithSwapChain(
                D3D10.DriverType.Hardware,
                DeviceCreationFlags.None,
                swapChainDesc,
                out d3d10Device,
                out swapChain
            );

            using (Texture2D backBuffer = swapChain.GetBackBuffer<Texture2D>(0))
                renderTargetView = new RenderTargetView(d3d10Device, backBuffer);

            d3d10Device.OutputMerger.SetTargets(renderTargetView);

            viewport = new Viewport
            {
                Width = drawForm.Width,
                Height = drawForm.Height,
                MinDepth = 0.0f,
                MaxDepth = 1.0f,
                X = 0,
                Y = 0
            };

            d3d10Device.Rasterizer.SetViewports(viewport);

            var zBufferTextureDescription = new Texture2DDescription
            {
                Format = Format.D16_UNorm,
                ArraySize = 1,
                MipLevels = 1,
                Width = drawForm.Width,
                Height = drawForm.Height,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                BindFlags = BindFlags.DepthStencil,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags = ResourceOptionFlags.None
            };
            using (var zBufferTexture = new Texture2D(this.d3d10Device, zBufferTextureDescription))
                depthStencilView = new DepthStencilView(this.d3d10Device, zBufferTexture);
            d3d10Device.OutputMerger.SetTargets(depthStencilView, renderTargetView);

            depthState = new DepthStencilStateDescription()
            {
                IsDepthEnabled = true,
                DepthWriteMask = DepthWriteMask.All,
                DepthComparison = Comparison.Less,
                IsStencilEnabled = true,
                StencilReadMask = 0xFF,
                StencilWriteMask = 0xFF,
                FrontFace = new DepthStencilOperationDescription()
                {
                    FailOperation = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Increment,
                    PassOperation = StencilOperation.Keep,
                    Comparison = Comparison.Always
                },
                BackFace = new DepthStencilOperationDescription()
                {
                    FailOperation = StencilOperation.Keep,
                    DepthFailOperation = StencilOperation.Decrement,
                    PassOperation = StencilOperation.Keep,
                    Comparison = Comparison.Always
                }
            };
            depthNonState = new DepthStencilStateDescription()
            {
                IsDepthEnabled = false
            };
            d3d10Device.OutputMerger.SetDepthStencilState(
                new DepthStencilState(
                    d3d10Device,
                    depthState
                ), 1
            );

            // Generic font?

            // Camera?

            d3d10Device.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

            d3d10Device.Rasterizer.State = new RasterizerState(d3d10Device,
                new RasterizerStateDescription
                {
                    CullMode = CullMode.Back,
                    FillMode = FillMode.Solid
                }
            );
        }
		private void CreateAndBindTargets(int sizeX, int sizeY)
		{
			_d3dImageSource.SetRenderTargetDX10(null);

			Disposer.RemoveAndDispose(ref this._renderTargetView);
			Disposer.RemoveAndDispose(ref this._renderTargetIntermediateView);
			Disposer.RemoveAndDispose(ref this._renderTargetIntermediateShaderResourceView);
			Disposer.RemoveAndDispose(ref this._depthStencilView);
			Disposer.RemoveAndDispose(ref this._renderTarget);
			Disposer.RemoveAndDispose(ref this._renderTargetIntermediate);
			Disposer.RemoveAndDispose(ref this._depthStencil);
			Disposer.RemoveAndDispose(ref this._gammaCorrector);

			if (sizeX >= 2 && sizeY >= 2)
			{
				Texture2DDescription colordesc = new Texture2DDescription
				{
					BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
					Format = Format.B8G8R8A8_UNorm,
					Width = sizeX,
					Height = sizeY,
					MipLevels = 1,
					SampleDescription = new SampleDescription(1, 0),
					Usage = ResourceUsage.Default,
					OptionFlags = ResourceOptionFlags.Shared,
					CpuAccessFlags = CpuAccessFlags.None,
					ArraySize = 1
				};

				Texture2DDescription renderTextureDescriptionForD3D9 = new Texture2DDescription
				{
					BindFlags = BindFlags.None,
					Format = Format.B8G8R8A8_UNorm,
					Width = sizeX,
					Height = sizeY,
					MipLevels = 1,
					SampleDescription = new SampleDescription(1, 0),
					Usage = ResourceUsage.Staging,
					OptionFlags = ResourceOptionFlags.Shared,
					CpuAccessFlags = CpuAccessFlags.Read,
					ArraySize = 1
				};

				Texture2DDescription depthdesc = new Texture2DDescription
				{
					BindFlags = BindFlags.DepthStencil,
					Format = Format.D32_Float_S8X24_UInt,
					Width = sizeX,
					Height = sizeY,
					MipLevels = 1,
					SampleDescription = new SampleDescription(1, 0),
					Usage = ResourceUsage.Default,
					OptionFlags = ResourceOptionFlags.None,
					CpuAccessFlags = CpuAccessFlags.None,
					ArraySize = 1,
				};

				this._renderTarget = new Texture2D(this._device, colordesc);
				this._renderTargetIntermediate = new Texture2D(this._device, colordesc);
				this._depthStencil = new Texture2D(this._device, depthdesc);
				this._renderTargetIntermediateView = new RenderTargetView(this._device, this._renderTargetIntermediate);
				this._renderTargetIntermediateShaderResourceView = new ShaderResourceView(this._device, this._renderTargetIntermediate);
				this._renderTargetView = new RenderTargetView(this._device, this._renderTarget);
				this._depthStencilView = new DepthStencilView(this._device, this._depthStencil);
				this._gammaCorrector = new D3D10GammaCorrector(_device, "Altaxo.CompiledShaders.Effects.GammaCorrector.cso");

				this._d3dImageSource.SetRenderTargetDX10(this._renderTarget);
			}
		}
        private void CreateAndBindTargets()
        {
            _d3DSurface.SetRenderTargetDX10(null);

            Disposer.RemoveAndDispose(ref _renderTargetView);
            Disposer.RemoveAndDispose(ref _depthStencilView);
            Disposer.RemoveAndDispose(ref _renderTarget);
            Disposer.RemoveAndDispose(ref _depthStencil);

            int width = Math.Max((int)base.ActualWidth, 100);
            int height = Math.Max((int)base.ActualHeight, 100);

            Texture2DDescription colordesc = new Texture2DDescription
            {
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format = Format.B8G8R8A8_UNorm,
                Width = width,
                Height = height,
                MipLevels = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                OptionFlags = ResourceOptionFlags.Shared,
                CpuAccessFlags = CpuAccessFlags.None,
                ArraySize = 1
            };

            Texture2DDescription depthdesc = new Texture2DDescription
            {
                BindFlags = BindFlags.DepthStencil,
                Format = Format.D32_Float_S8X24_UInt,
                Width = width,
                Height = height,
                MipLevels = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                OptionFlags = ResourceOptionFlags.None,
                CpuAccessFlags = CpuAccessFlags.None,
                ArraySize = 1,
            };

            _renderTarget = new Texture2D(_device, colordesc);
            _depthStencil = new Texture2D(_device, depthdesc);
            _renderTargetView = new RenderTargetView(_device, _renderTarget);
            _depthStencilView = new DepthStencilView(_device, _depthStencil);

            _d3DSurface.SetRenderTargetDX10(_renderTarget);
        }
        public byte[] extractRawBitmap()
        {
            // use a cpu bound resource

            var textureDesc = new Texture2DDescription
            {
                MipLevels = 1,
                ArraySize = 1,
                BindFlags = BindFlags.None,
                CpuAccessFlags = CpuAccessFlags.Read,
                Format = Format.B8G8R8A8_UNorm,
                OptionFlags = ResourceOptionFlags.None,
                Width = _width,
                Height = _height,
                Usage = ResourceUsage.Default,
                SampleDescription = new SampleDescription(1, 0)
            };

            using (var cpuTexture = new Texture2D(_device, textureDesc))
            {
                _device.CopyResource(_texture, cpuTexture);

                var res = new byte[4 * _width * _height];
                var data = cpuTexture.Map(0, MapMode.Read, MapFlags.None);
                try
                {
                    IntPtr sourcePtr = data.DataPointer;
                    int targetOffset = 0;
                    for (int i = 0; i != _height; ++i)
                    {
                        Marshal.Copy(sourcePtr, res, targetOffset, _width);
                        sourcePtr += data.Pitch;
                        targetOffset += _width;
                    }

                    return res;
                }
                finally
                {
                    cpuTexture.Unmap(0);
                }
            }
        }
Exemple #12
0
 private static void ConfigureStagingDescription(out Texture2D description)
 {
     description.Width = 0;
     description.ArraySize = 1;
     description.MipLevels = 0;
     description.Format = Format.R8G8B8A8_UNorm;
     description.Usage = ResourceUsage.Staging;
     description.BindFlags = BindFlags.None;
     description.SampleDescription = new SampleDescription(1, 0);
     description.OptionFlags = ResourceOptionFlags.None;
     description.CpuAccessFlags = CpuAccessFlags.Write;
     description.Height = 0;
 }
Exemple #13
0
		public void Export(int sizeX, int sizeY, ID3D10Scene scene, Altaxo.Graph.Gdi.GraphExportOptions options, System.IO.Stream toStream)
		{
			var device = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport | DeviceCreationFlags.Debug, FeatureLevel.Level_10_0);

			// try to get the highest MSAA level with the highest quality
			int sampleCount = 32;
			int qlevel_sampleCount = 0;

			for (; sampleCount >= 0; sampleCount /= 2)
			{
				if (0 != (qlevel_sampleCount = device.CheckMultisampleQualityLevels(Format.B8G8R8A8_UNorm, sampleCount))) // quality level for sample count
					break;
			}

			Texture2DDescription colordesc = new Texture2DDescription
			{
				BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
				Format = Format.B8G8R8A8_UNorm_SRgb,
				Width = sizeX,
				Height = sizeY,
				MipLevels = 1,
				SampleDescription = new SampleDescription(sampleCount, qlevel_sampleCount - 1),
				Usage = ResourceUsage.Default,
				OptionFlags = ResourceOptionFlags.Shared,
				CpuAccessFlags = CpuAccessFlags.None,
				ArraySize = 1
			};

			Texture2DDescription depthdesc = new Texture2DDescription
			{
				BindFlags = BindFlags.DepthStencil,
				Format = Format.D32_Float_S8X24_UInt,
				Width = sizeX,
				Height = sizeY,
				MipLevels = 1,
				SampleDescription = new SampleDescription(sampleCount, qlevel_sampleCount - 1),
				Usage = ResourceUsage.Default,
				OptionFlags = ResourceOptionFlags.None,
				CpuAccessFlags = CpuAccessFlags.None,
				ArraySize = 1,
			};

			var renderTarget = new Texture2D(device, colordesc);
			var depthStencil = new Texture2D(device, depthdesc);
			var renderTargetView = new RenderTargetView(device, renderTarget);
			var depthStencilView = new DepthStencilView(device, depthStencil);

			// Rendering

			device.OutputMerger.SetTargets(depthStencilView, renderTargetView);
			device.Rasterizer.SetViewports(new Viewport(0, 0, sizeX, sizeY, 0.0f, 1.0f));
			Color4 clearColor = new Color4(1, 1, 1, 0); // Transparent
			if (options.BackgroundBrush != null)
			{
				var axoColor = options.BackgroundBrush.Color.Color;
				clearColor = new Color4(axoColor.ScR, axoColor.ScG, axoColor.ScB, axoColor.ScA);
			}
			device.ClearRenderTargetView(renderTargetView, clearColor);
			device.ClearDepthStencilView(depthStencilView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1.0f, 0);

			scene.Attach(device, new PointD2D(sizeX, sizeY));
			scene.Render();

			device.Flush();

			Texture2D renderTarget2 = null;

			if (sampleCount > 1) // if renderTarget is an MSAA render target, we first have to copy it into a non-MSAA render target before we can copy it to a CPU texture and then hope to save it
			{
				// create a non-MSAA render target with the same size
				Texture2DDescription renderTarget2Description = new Texture2DDescription
				{
					BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
					Format = Format.B8G8R8A8_UNorm_SRgb,
					Width = sizeX,
					Height = sizeY,
					MipLevels = 1,
					SampleDescription = new SampleDescription(1, 0), // non MSAA
					Usage = ResourceUsage.Default,
					OptionFlags = ResourceOptionFlags.Shared,
					CpuAccessFlags = CpuAccessFlags.None,
					ArraySize = 1
				};

				renderTarget2 = new Texture2D(device, renderTarget2Description); // create non-MSAA render target
				device.ResolveSubresource(renderTarget, 0, renderTarget2, 0, renderTarget.Description.Format); // copy from MSAA render target to the non-MSAA render target

				var h = renderTarget; // exchange renderTarget with renderTarget2
				renderTarget = renderTarget2;
				renderTarget2 = h;
			}

			// renderTarget is now a non-MSAA renderTarget
			Texture2DExtensions.SaveToStream(renderTarget, options.ImageFormat, options.DestinationDpiResolution, toStream);

			scene.Detach();

			Disposer.RemoveAndDispose(ref depthStencilView);
			Disposer.RemoveAndDispose(ref renderTargetView);
			Disposer.RemoveAndDispose(ref renderTarget2);
			Disposer.RemoveAndDispose(ref renderTarget);
			Disposer.RemoveAndDispose(ref depthStencil);
			Disposer.RemoveAndDispose(ref device);
		}
Exemple #14
0
        Texture2D createTexture(int width, int height, Format format)
        {
            var texDesc = new Texture2DDescription
            {
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.Write,
                Format = format,
                Height = height,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Dynamic,
                Width = width
            };

            Texture2D texture = new Texture2D(Host.Device, texDesc);

            return (texture);
        }
Exemple #15
0
        Texture2D createTextureFromFile(string filename)
        {
            BitmapImage loadedImage = new BitmapImage();

            loadedImage.BeginInit();
            loadedImage.CacheOption = BitmapCacheOption.OnLoad;
            loadedImage.UriSource = new Uri(filename);
            loadedImage.EndInit();

            loadedImage.Freeze();

            int stride = loadedImage.PixelWidth * (loadedImage.Format.BitsPerPixel / 8);

            byte[] pixels = new byte[loadedImage.PixelHeight * stride];

            loadedImage.CopyPixels(pixels, stride, 0);

            pinnedArray = GCHandle.Alloc(pixels, GCHandleType.Pinned);
            IntPtr pixelPtr = pinnedArray.AddrOfPinnedObject();

            DataRectangle data = new DataRectangle(pixelPtr, stride);           

            var texDesc = new Texture2DDescription
            {
                ArraySize = 1,
                BindFlags = BindFlags.ShaderResource,
                CpuAccessFlags = CpuAccessFlags.None,
                Format = Format.B8G8R8A8_UNorm,
                Height = loadedImage.PixelHeight,
                MipLevels = 1,
                OptionFlags = ResourceOptionFlags.None,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                Width = loadedImage.PixelWidth
            };

            Texture2D texture = new Texture2D(Host.Device, texDesc, data);
                               
            return (texture);            
        }
Exemple #16
0
        private void CreateAndBindTargets()
        {
            this.D3DSurface.SetRenderTargetDX10(null);

            Disposer.RemoveAndDispose(ref this.D2DRenderTarget);
            Disposer.RemoveAndDispose(ref this.D2DFactory);
            Disposer.RemoveAndDispose(ref this.RenderTarget);

            int width = Math.Max((int)base.ActualWidth, 100);
            int height = Math.Max((int)base.ActualHeight, 100);

            Texture2DDescription colordesc = new Texture2DDescription
            {
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format = Format.B8G8R8A8_UNorm,
                Width = width,
                Height = height,
                MipLevels = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                OptionFlags = ResourceOptionFlags.Shared,
                CpuAccessFlags = CpuAccessFlags.None,
                ArraySize = 1
            };

            this.RenderTarget = new Texture2D(this.Device, colordesc);
            Surface surface = this.RenderTarget.QueryInterface<Surface>();

            D2DFactory = new SharpDX.Direct2D1.Factory();
            var rtp = new RenderTargetProperties(new SharpDX.Direct2D1.PixelFormat(Format.Unknown, SharpDX.Direct2D1.AlphaMode.Premultiplied));
            D2DRenderTarget = new RenderTarget(D2DFactory, surface, rtp);

            this.D3DSurface.SetRenderTargetDX10(this.RenderTarget);

            OnOnCreateAndBindTargetEnd(this.D2DRenderTarget);
            Debug.Print("OnOnCreateAndBindTargetEnd");
        }
Exemple #17
0
 /// <summary>
 ///   Initializes a new instance of the <see cref = "T:SharpDX.Direct3D10.Texture2D" /> class.
 /// </summary>
 /// <param name = "device">The device with which to associate the texture.</param>
 /// <param name = "description">The description of the texture.</param>
 /// <param name = "data">The initial texture data.</param>
 public Texture2D(Device device, Texture2DDescription description, DataRectangle data)
     : this(device, description, new[] {data})
 {
 }
Exemple #18
0
        private static void ConfigurePrivateTexture(out Texture2D description)
        {
            BindFlags binding = BindFlags.None;

            binding |= BindFlags.ShaderResource;
            binding |= BindFlags.RenderTarget;

            description.MipLevels = 1;
            description.ArraySize = 1;
            description.Format = Format.R8G8B8A8_UNorm;
            description.Usage = ResourceUsage.Default;
            description.BindFlags = binding;
            description.SampleDescription = SampleDescription;
            description.OptionFlags = ResourceOptionFlags.None;
            description.CpuAccessFlags = CpuAccessFlags.None;
            description.Height = 0;
            description.Width = 0;
        }
Exemple #19
0
        /// <summary>
        /// Crée le Device DirectX 10 et initialise les resources
        /// </summary>
        public void Initialize(int width, int height)
        {
            if (width <= 2)
                throw new ArgumentOutOfRangeException("width");
            else if (height <= 2)
                throw new ArgumentOutOfRangeException("height");

            Width = width;
            Height = height;

            Clean();

            hwndRenderingWindow = new Form();
            hwndRenderingWindow.Width = hwndRenderingWindow.Height = 100;

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

            // Create Device and SwapChain
            //Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.BgraSupport, desc, out device, out swapChain);
            Factory factory = new Factory();
            var adapter = factory.GetAdapter(0);
            device = new Device(adapter, DeviceCreationFlags.BgraSupport, SharpDX.Direct3D10.FeatureLevel.Level_10_0);
            swapChain = new SwapChain(factory, device, desc);

            //device = new Device(DriverType.Hardware, DeviceCreationFlags.BgraSupport, SharpDX.Direct3D10.FeatureLevel.Level_10_0);

            Texture2DDescription colordesc = new Texture2DDescription
            {
                BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource,
                Format = Format.B8G8R8A8_UNorm,
                Width = width,
                Height = height,
                MipLevels = 1,
                SampleDescription = new SampleDescription(1, 0),
                Usage = ResourceUsage.Default,
                OptionFlags = ResourceOptionFlags.Shared,
                CpuAccessFlags = CpuAccessFlags.None,
                ArraySize = 1
            };

            renderBuffer = new Texture2D(device, colordesc);
            renderView = new RenderTargetView(device, renderBuffer);

            LoadRenderShader();

            wpfImage = new DX10ImageSource();
            wpfImage.SetRenderTargetDX10(renderBuffer);

            RaisePropertyChanged("WPFImage");
        }
        private Texture2D CreateTexture(int width, int height, bool multiSampling)
        {
            var description = new SharpDX.Direct3D10.Texture2DDescription();
            description.ArraySize = 1;
            description.BindFlags = SharpDX.Direct3D10.BindFlags.RenderTarget | SharpDX.Direct3D10.BindFlags.ShaderResource;
            description.CpuAccessFlags = CpuAccessFlags.None;
            description.Format = Format.B8G8R8A8_UNorm;
            description.MipLevels = 1;
 
            // Multi-sample anti-aliasing
            int count, quality;
            if (multiSampling) 
            {
                count = 8;
                quality = device.CheckMultisampleQualityLevels(description.Format, count);
                if (quality == 0)
                {
                    count = 4;
                    quality = device.CheckMultisampleQualityLevels(description.Format, count);
                }
                if (quality == 0) count = 1;
            }
            else count = 1;
            if (count == 1) quality = 1;
            SampleDescription sampleDesc = new SampleDescription(count, 0);
            description.SampleDescription = sampleDesc;

            description.Usage = ResourceUsage.Default;
            description.OptionFlags = ResourceOptionFlags.Shared;
            description.Height = (int)height;
            description.Width = (int)width;

            return new Texture2D(device, description);
        }
Exemple #21
0
        public byte[] ExtractRawBitmap()
        {
            // use a cpu bound resource

            var textureDesc = new Texture2DDescription
            {
                MipLevels = 1,
                ArraySize = 1,
                BindFlags = BindFlags.None,
                CpuAccessFlags = CpuAccessFlags.Read,
                Format = Format.B8G8R8A8_UNorm,
                OptionFlags = ResourceOptionFlags.None,
                Width = _width,
                Height = _height,
                Usage = ResourceUsage.Staging,
                SampleDescription = new SampleDescription(1, 0)
            };

            using (var cpuTexture = new Texture2D(_device, textureDesc))
            {
            #if NETFX_CORE
                _device.ImmediateContext.CopyResource(_texture, cpuTexture);
            #else
                _device.CopyResource(_texture, cpuTexture);
            #endif
                var bytesPerLine = _width * 4;
                var res = new byte[bytesPerLine * _height];
            #if NETFX_CORE
                var data = _device.ImmediateContext.MapSubresource(cpuTexture, 0, MapMode.Read, MapFlags.None);
            #else
                var data = cpuTexture.Map(0, MapMode.Read, MapFlags.None);
            #endif
                try
                {
                    IntPtr sourcePtr = data.DataPointer;
                    int targetOffset = 0;
                    for (int i = 0; i != _height; ++i)
                    {
                        Marshal.Copy(sourcePtr, res, targetOffset, bytesPerLine);
            #if NETFX_CORE
                        sourcePtr += data.RowPitch;
            #else
                        sourcePtr += data.Pitch;
            #endif
                        targetOffset += bytesPerLine;
                    }

                    return res;
                }
                finally
                {
            #if NETFX_CORE
                    _device.ImmediateContext.UnmapSubresource(cpuTexture, 0);
            #else
                    cpuTexture.Unmap(0);
            #endif
                }
            }
        }