public void TestRenderUsingGDI() { var game = new DX11Game(); game.InitDirectX(); // Create the DirectX11 texture2D. This texture will be shared with the DirectX10 // device. The DirectX10 device will be used to render text onto this texture. DirectX11 // will then draw this texture (blended) onto the screen. // The KeyedMutex flag is required in order to share this resource. SlimDX.Direct3D11.Texture2D textureD3D11 = new Texture2D(game.Device, new Texture2DDescription { Width = 100, Height = 100, MipLevels = 1, ArraySize = 1, Format = Format.B8G8R8A8_UNorm, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default, BindFlags = BindFlags.RenderTarget | BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.None, OptionFlags = ResourceOptionFlags.KeyedMutex }); var surface = textureD3D11.AsSurface(); var surface1 = Surface1.FromPointer(surface.ComPointer); game.GameLoopEvent += delegate { }; }
public Texture(Texture2D texture) { InternalTexture2D = texture; /* Cache the description for perf reasons */ Description = texture.Description; InternalDevice = InternalTexture2D.Device; /* SlimDX will only let us pull out the surface once until it is disposed. * We will do that here as it is the logic place to store ref to the DXGI surface */ InternalDxgiSurface = InternalTexture2D.AsSurface(); }
public void ResizeDevice(int width, int height) { lock (this) { if (width < 0) { throw new ArgumentOutOfRangeException("width", "Value must be positive."); } if (height < 0) { throw new ArgumentOutOfRangeException("height", "Value must be positive."); } if ((width <= this.width) && (height <= this.height)) { return; } // Recreate the render target // Assign result to temporary variable in case CreateTexture2D throws an exception. var texture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), true); if (this.texture != null) { this.texture.Dispose(); } this.texture = texture; var shareableTexture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), false); if (this.shareableTexture != null) { this.shareableTexture.Dispose(); } this.shareableTexture = shareableTexture; //if (renderTargetView != null) renderTargetView.Dispose(); //this.renderTargetView = new RenderTargetView(device, shareableTexture); this.width = texture.Description.Width; this.height = texture.Description.Height; SlimDX.DXGI.Surface surface = texture.AsSurface(); CreateRenderTarget(surface); if (DeviceResized != null) { DeviceResized(this, EventArgs.Empty); } } }
protected IDisposable CreateFontMapTexture(int width, int height, CharRenderCall[] drawCalls) { var texDesc = new Texture2DDescription { ArraySize = 1, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = height, Width = width, MipLevels = 1, OptionFlags = ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; var device10 = (Device1)D3DDevice10; var texture = new Texture2D(device10, texDesc); var rtv = new RenderTargetView(device10, texture); device10.ClearRenderTargetView(rtv, new Color4(0, 1, 1, 1)); // device10.ClearRenderTargetView(rtv, new Color4(1, 0, 0, 0)); var surface = texture.AsSurface(); var target = RenderTarget.FromDXGI((SlimDX.Direct2D.Factory)D2DFactory, surface, _rtp); var color = new SolidColorBrush(target, new Color4(1, 1, 1, 1)); target.BeginDraw(); foreach (var drawCall in drawCalls) { target.DrawTextLayout(drawCall.Position, (TextLayout)drawCall.TextLayout, color); } target.EndDraw(); color.Dispose(); // This is a workaround for Windows 8.1 machines. // If these lines would not be present, the shared resource would be empty. // TODO: find a nicer solution using (var ms = new MemoryStream()) Texture2D.ToStream(texture, ImageFileFormat.Bmp, ms); target.Dispose(); surface.Dispose(); rtv.Dispose(); return(texture); }
public IDrawingTarget BeginDraw(Color?clearColor) { #if NETFX_CORE var surface = _texture.QueryInterface <Surface>(); #else var surface = _texture.AsSurface(); #endif var rtProperties = new RenderTargetProperties { DpiX = 96, DpiY = 96, Type = RenderTargetType.Default, PixelFormat = new PixelFormat(Format.Unknown, AlphaMode.Premultiplied) }; var renderTarget = new RenderTarget(_factory, surface, rtProperties); renderTarget.BeginDraw(); // required to clear the render target // (not required on all machines, MacBook Air + Win7 seems to recycle old textures) renderTarget.Clear(clearColor != null ? clearColor.Value.import() : (Color4?)null); var state = new DrawingState(); var transform = new DrawingTransform(); var drawingTarget = new DrawingTarget(state, transform, renderTarget, _width, _height); var target = new DrawingTargetSplitter( _backend, state, transform, drawingTarget, drawingTarget, drawingTarget, drawingTarget, drawingTarget, () => { drawingTarget.Dispose(); renderTarget.EndDraw(); renderTarget.Dispose(); surface.Dispose(); }); var pixelAligner = PixelAligningDrawingTarget.Create(target, target.Dispose, state, transform); return(pixelAligner); }
public void ResizeDevice(int width, int height) { lock (this) { if (width < 0) { throw new ArgumentOutOfRangeException("width", "Value must be positive."); } if (height < 0) { throw new ArgumentOutOfRangeException("height", "Value must be positive."); } if ((width <= this.width) && (height <= this.height)) { return; } DirectXHelpers.SafeDispose(ref this.texture); var texture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), true); this.texture = texture; DirectXHelpers.SafeDispose(ref this.shareableTexture); var shareableTexture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), false); this.shareableTexture = shareableTexture; CreateD3D9TextureFromD3D10Texture(shareableTexture); this.width = texture.Description.Width; this.height = texture.Description.Height; using (SharpDX.DXGI.Surface surface = texture.AsSurface()) { CreateRenderTarget(surface); } if (DeviceResized != null) { DeviceResized(this, EventArgs.Empty); } } }
private void CreateCharTable(byte bytePrefix) { var TableDesc = new CharTableDescription(); //Get appropriate texture size int sizeX = (int)(Font.FontSize * 12); sizeX = (int)Math.Pow(2, Math.Ceiling(Math.Log(sizeX, 2))); //Try how many lines are needed: var tl = new TextLayout[256]; int line = 0, xPos = 0, yPos = 0; for (int i = 0; i < 256; ++i) { tl[i] = new TextLayout(ModelEx.FontManager.Instance.WriteFactory, Convert.ToChar(i + (bytePrefix << 8)).ToString(), Font); int charWidth = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutWidth + tl[i].OverhangMetrics.Left + tl[i].OverhangMetrics.Right); int charHeight = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutHeight + tl[i].OverhangMetrics.Top + tl[i].OverhangMetrics.Bottom); line = Math.Max(line, charHeight); if (xPos + charWidth >= sizeX) { xPos = 0; yPos += line; line = 0; } xPos += charWidth; } int sizeY = (int)(line + yPos); sizeY = (int)Math.Pow(2, Math.Ceiling(Math.Log(sizeY, 2))); //Create Texture var TexDesc = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = sizeY, Width = sizeX, MipLevels = 1, OptionFlags = ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; var texture = new Texture2D(ModelEx.FontManager.Instance.D3DDevice10, TexDesc); var rtv = new RenderTargetView(ModelEx.FontManager.Instance.D3DDevice10, texture); ModelEx.FontManager.Instance.D3DDevice10.ClearRenderTargetView(rtv, new SlimDX.Color4(0, 1, 1, 1)); //D3DDevice10.ClearRenderTargetView(rtv, new SlimDX.Color4(1, 0, 0, 0)); Surface surface = texture.AsSurface(); var target = RenderTarget.FromDXGI(ModelEx.FontManager.Instance.D2DFactory, surface, rtp); var color = new SolidColorBrush(target, new SlimDX.Color4(1, 1, 1, 1)); target.BeginDraw(); line = 0; xPos = 0; yPos = 0; //for (int i = 0; i < 256; ++i) for (int i = 0; i < 256; ++i) { //1 additional pixel on each side int charWidth = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutWidth + tl[i].OverhangMetrics.Left + tl[i].OverhangMetrics.Right); int charHeight = 2 + (int)Math.Ceiling(tl[i].Metrics.LayoutHeight + tl[i].OverhangMetrics.Top + tl[i].OverhangMetrics.Bottom); line = Math.Max(line, charHeight); if (xPos + charWidth >= sizeX) { xPos = 0; yPos += line; line = 0; } var charDesc = new CharDescription(); charDesc.CharSize = new Vector2(tl[i].Metrics.WidthIncludingTrailingWhitespace, tl[i].Metrics.Height); charDesc.OverhangLeft = tl[i].OverhangMetrics.Left + 1; charDesc.OverhangTop = tl[i].OverhangMetrics.Top + 1; //Make XPos + CD.Overhang.Left an integer number in order to draw at integer positions charDesc.OverhangLeft += (float)Math.Ceiling(xPos + charDesc.OverhangLeft) - (xPos + charDesc.OverhangLeft); //Make YPos + CD.Overhang.Top an integer number in order to draw at integer positions charDesc.OverhangTop += (float)Math.Ceiling(yPos + charDesc.OverhangTop) - (yPos + charDesc.OverhangTop); charDesc.OverhangRight = charWidth - charDesc.CharSize.X - charDesc.OverhangLeft; charDesc.OverhangBottom = charHeight - charDesc.CharSize.Y - charDesc.OverhangTop; charDesc.TexCoordsStart = new Vector2(((float)xPos / sizeX), ((float)yPos / sizeY)); charDesc.TexCoordsSize = new Vector2((float)charWidth / sizeX, (float)charHeight / sizeY); charDesc.TableDescription = TableDesc; TableDesc.Chars[i] = charDesc; target.DrawTextLayout(new PointF(xPos + charDesc.OverhangLeft, yPos + charDesc.OverhangTop), tl[i], color); xPos += charWidth; tl[i].Dispose(); } target.EndDraw(); color.Dispose(); //This is a workaround for Windows 8.1 machines. //If these lines would not be present, the shared resource would be empty. //TODO: find a nicer solution using (var ms = new MemoryStream()) Texture2D.ToStream(texture, ImageFileFormat.Bmp, ms); System.Threading.Monitor.Enter(D3DDevice11); var dxgiResource = new SlimDX.DXGI.Resource(texture); SlimDX.Direct3D11.Texture2D Texture11; if (PixCompatible) { Texture11 = new SlimDX.Direct3D11.Texture2D(D3DDevice11, new SlimDX.Direct3D11.Texture2DDescription() { ArraySize = 1, BindFlags = SlimDX.Direct3D11.BindFlags.ShaderResource | SlimDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = SlimDX.Direct3D11.CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = sizeY, Width = sizeX, MipLevels = 1, OptionFlags = SlimDX.Direct3D11.ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = SlimDX.Direct3D11.ResourceUsage.Default }); } else { Texture11 = D3DDevice11.OpenSharedResource <SlimDX.Direct3D11.Texture2D>(dxgiResource.SharedHandle); } var srv = new SlimDX.Direct3D11.ShaderResourceView(D3DDevice11, Texture11); TableDesc.Texture = Texture11; TableDesc.SRV = srv; rtv.Dispose(); System.Threading.Monitor.Exit(D3DDevice11); System.Diagnostics.Debug.WriteLine("Created Char Table " + bytePrefix + " in " + sizeX + " x " + sizeY); //System.Threading.Monitor.Enter(D3DDevice11); //SlimDX.Direct3D11.Texture2D.SaveTextureToFile(Sprite.Device.ImmediateContext, Texture11, SlimDX.Direct3D11.ImageFileFormat.Png, Font.FontFamilyName + "Table" + BytePrefix + ".png"); //System.Threading.Monitor.Exit(D3DDevice11); CharTables.Add(bytePrefix, TableDesc); dxgiResource.Dispose(); target.Dispose(); surface.Dispose(); texture.Dispose(); }
/// <summary> /// Creates the texture and necessary structures for 256 chars whose unicode number starts with the given byte. /// The table containing ASCII has a prefix of 0 (0x00/00 - 0x00/FF). /// </summary> /// <param name="BytePrefix">The byte prefix of characters.</param> private void CreateCharTable(byte BytePrefix) { var TableDesc = new CharTableDescription(); //Get appropriate texture size int SizeX = (int)(Font.FontSize * 12); SizeX = (int)Math.Pow(2, Math.Ceiling(Math.Log(SizeX, 2))); //Try how many lines are needed: var TL = new TextLayout[256]; float Line = 0, XPos = 0, YPos = 0; for (int i = 0; i < 256; ++i) { TL[i] = new TextLayout(WriteFactory, Convert.ToChar(i + (BytePrefix << 8)).ToString(), Font); float CharWidth = 2 + (float)Math.Ceiling(TL[i].Metrics.LayoutWidth + Math.Max(0, TL[i].OverhangMetrics.Left) + Math.Max(0, TL[i].OverhangMetrics.Right)); float CharHeight = 2 + (float)Math.Ceiling(TL[i].Metrics.LayoutHeight + Math.Max(0, TL[i].OverhangMetrics.Top) + Math.Max(0, TL[i].OverhangMetrics.Bottom)); Line = Math.Max(Line, CharHeight); if (XPos + CharWidth >= SizeX) { XPos = 0; YPos += Line; Line = 0; } XPos += CharWidth; } int SizeY = (int)(Line + YPos); SizeY = (int)Math.Pow(2, Math.Ceiling(Math.Log(SizeY, 2))); //Create Texture var TexDesc = new Texture2DDescription() { ArraySize = 1, BindFlags = BindFlags.ShaderResource | BindFlags.RenderTarget, CpuAccessFlags = CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, Height = SizeY, Width = SizeX, MipLevels = 1, OptionFlags = ResourceOptionFlags.KeyedMutex, SampleDescription = new SampleDescription(1, 0), Usage = ResourceUsage.Default }; var Texture = new Texture2D(D3DDevice10, TexDesc); var rtv = new RenderTargetView(D3DDevice10, Texture); D3DDevice10.ClearRenderTargetView(rtv, new global::SlimDX.Color4(0, 1, 1, 1)); //D3DDevice10.ClearRenderTargetView(rtv, new SlimDX.Color4(1, 0, 0, 0)); Surface Surface = Texture.AsSurface(); Surface1 s1; var Target = RenderTarget.FromDXGI(D2DFactory, Surface, rtp); var Color = new SolidColorBrush(Target, new global::SlimDX.Color4(1, 1, 1, 1)); Target.BeginDraw(); Line = 0; XPos = 0; YPos = 0; for (int i = 0; i < 256; ++i) { float CharWidth = 2 + (float)Math.Ceiling(TL[i].Metrics.LayoutWidth + Math.Max(0, TL[i].OverhangMetrics.Left) + Math.Max(0, TL[i].OverhangMetrics.Right)); float CharHeight = 2 + (float)Math.Ceiling(TL[i].Metrics.LayoutHeight + Math.Max(0, TL[i].OverhangMetrics.Top) + Math.Max(0, TL[i].OverhangMetrics.Bottom)); Line = Math.Max(Line, CharHeight); if (XPos + CharWidth >= SizeX) { XPos = 0; YPos += Line; Line = 0; } var CD = new CharDescription(); CD.CharSize = new Vector2(TL[i].Metrics.WidthIncludingTrailingWhitespace, TL[i].Metrics.Height); CD.OverhangLeft = TL[i].OverhangMetrics.Left; CD.OverhangTop = TL[i].OverhangMetrics.Top; CD.OverhangRight = TL[i].Metrics.LayoutWidth + TL[i].OverhangMetrics.Right - TL[i].Metrics.WidthIncludingTrailingWhitespace; CD.OverhangBottom = TL[i].Metrics.LayoutHeight + TL[i].OverhangMetrics.Bottom - TL[i].Metrics.Height; //Safety space around chars that are not unvisible if (CD.CharSize.X + CD.OverhangLeft + CD.OverhangRight > 0 && CD.CharSize.Y + CD.OverhangTop + CD.OverhangBottom > 0) { CD.OverhangBottom += 1; CD.OverhangTop += 1; CD.OverhangRight += 1; CD.OverhangLeft += 1; } //Correct overhangs, so size in pixels will be integer numbers //this avoids incorrect filtering results CD.OverhangRight += (float)Math.Ceiling(CD.OverhangLeft + CD.CharSize.X + CD.OverhangRight) - (CD.OverhangLeft + CD.CharSize.X + CD.OverhangRight); CD.OverhangBottom += (float)Math.Ceiling(CD.OverhangTop + CD.CharSize.Y + CD.OverhangBottom) - (CD.OverhangTop + CD.CharSize.Y + CD.OverhangBottom); CD.TexCoordsStart = new Vector2((XPos / SizeX), (YPos / SizeY)); CD.TexCoordsSize = new Vector2((CD.OverhangLeft + CD.CharSize.X + CD.OverhangRight) / SizeX, (CD.OverhangTop + CD.CharSize.Y + CD.OverhangBottom) / SizeY); CD.TableDescription = TableDesc; TableDesc.Chars[i] = CD; Target.DrawTextLayout(new PointF(XPos + CD.OverhangLeft, YPos + CD.OverhangTop), TL[i], Color); XPos += CharWidth; TL[i].Dispose(); } Target.EndDraw(); Color.Dispose(); System.Threading.Monitor.Enter(D3DDevice11); var DXGIResource = new global::SlimDX.DXGI.Resource(Texture); global::SlimDX.Direct3D11.Texture2D Texture11; if (PixCompatible) { Texture11 = new global::SlimDX.Direct3D11.Texture2D(D3DDevice11, new global::SlimDX.Direct3D11.Texture2DDescription() { ArraySize = 1, BindFlags = global::SlimDX.Direct3D11.BindFlags.ShaderResource | global::SlimDX.Direct3D11.BindFlags.RenderTarget, CpuAccessFlags = global::SlimDX.Direct3D11.CpuAccessFlags.None, Format = Format.R8G8B8A8_UNorm, Height = SizeY, Width = SizeX, MipLevels = 1, OptionFlags = global::SlimDX.Direct3D11.ResourceOptionFlags.Shared, SampleDescription = new SampleDescription(1, 0), Usage = global::SlimDX.Direct3D11.ResourceUsage.Default }); } else { Texture11 = D3DDevice11.OpenSharedResource <global::SlimDX.Direct3D11.Texture2D>(DXGIResource.SharedHandle); } var SRV = new global::SlimDX.Direct3D11.ShaderResourceView(D3DDevice11, Texture11); TableDesc.Texture = Texture11; TableDesc.SRV = SRV; rtv.Dispose(); System.Threading.Monitor.Exit(D3DDevice11); System.Diagnostics.Debug.WriteLine("Created Char Table " + BytePrefix + " in " + SizeX + " x " + SizeY); //System.Threading.Monitor.Enter(D3DDevice11); //SlimDX.Direct3D11.Texture2D.SaveTextureToFile(Sprite.Device.ImmediateContext, Texture11, SlimDX.Direct3D11.ImageFileFormat.Png, Font.FontFamilyName + "Table" + BytePrefix + ".png"); //System.Threading.Monitor.Exit(D3DDevice11); CharTables.Add(BytePrefix, TableDesc); DXGIResource.Dispose(); Target.Dispose(); Surface.Dispose(); Texture.Dispose(); }
public void ResizeDevice(int width, int height) { lock (this) { if (width < 0) { throw new ArgumentOutOfRangeException("width", "Value must be positive."); } if (height < 0) { throw new ArgumentOutOfRangeException("height", "Value must be positive."); } if ((width <= this.width) && (height <= this.height)) return; // Recreate the render target // Assign result to temporary variable in case CreateTexture2D throws an exception. var texture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), true); if (this.texture != null) { this.texture.Dispose(); } this.texture = texture; var shareableTexture = CreateTexture(Math.Max(width, this.width), Math.Max(height, this.height), false); if (this.shareableTexture != null) { this.shareableTexture.Dispose(); } this.shareableTexture = shareableTexture; //if (renderTargetView != null) renderTargetView.Dispose(); //this.renderTargetView = new RenderTargetView(device, shareableTexture); this.width = texture.Description.Width; this.height = texture.Description.Height; SlimDX.DXGI.Surface surface = texture.AsSurface(); CreateRenderTarget(surface); if (DeviceResized != null) DeviceResized(this, EventArgs.Empty); } }
public void Process(DX11Resource <DX11Texture2D> input, FeralTic.DX11.DX11RenderContext context) { DX11Texture2D t = input[context]; var height = t.Height; var width = t.Width; var imageLink = Output; var imageAttributes = imageLink.ImageAttributes; var desiredImageFormat = ToOpenCVFormat(t.Format); if (desiredImageFormat == TColorFormat.UnInitialised) { throw (new Exception("No suitible image type available for this texture type " + t.Format.ToString())); } //-- //check attributes and reinitialise the image and offscreen buffer if we haven't got the right description ready // if (imageAttributes == null || FOffscreenBuffer == null || !imageAttributes.Initialised || FOffscreenBuffer.Description.Format != t.Description.Format || imageAttributes.Width != t.Width || imageAttributes.Height != t.Height || imageAttributes.ColorFormat != desiredImageFormat) { if (FOffscreenBuffer != null) { FOffscreenBuffer.Dispose(); } var description = new Texture2DDescription() { Width = width, Height = height, Format = t.Format, MipLevels = 1, Usage = ResourceUsage.Staging, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, SampleDescription = new SampleDescription(1, 0), ArraySize = 1 }; FOffscreenBuffer = new Texture2D(context.Device, description); imageLink.Initialise(new CVImageAttributes(desiredImageFormat, t.Width, t.Height)); } // //-- //-- //copy the texture to offscreen buffer // context.CurrentDeviceContext.CopyResource(t.Resource, FOffscreenBuffer); // //-- //-- //copy the data out of the offscreen buffer // var surface = FOffscreenBuffer.AsSurface(); var bytesPerRow = imageAttributes.Stride; var data = MapForRead(context.CurrentDeviceContext); lock (imageLink.BackLock) { var image = imageLink.BackImage; try { var source = data.Data.DataPointer; image.SetPixels(source); var destination = image.Data; for (int row = 0; row < t.Height; row++) { CopyMemory(destination, source, bytesPerRow); source += data.RowPitch; destination += bytesPerRow; } } finally { UnMap(context.CurrentDeviceContext); } } // //-- imageLink.Swap(); }
protected override void OnResourceLoad() { base.OnResourceLoad(); m_Imposter.LoadResources(); m_Texture = new Texture2D(GameEnvironment.Device, new Texture2DDescription() { Format = SlimDX.DXGI.Format.R32_Float, Width = 640, Height = 480, MipLevels = 1, ArraySize = 1, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Dynamic, SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0) }); m_TextureSurface = m_Texture.AsSurface(); DataRectangle data = m_TextureSurface.Map(SlimDX.DXGI.MapFlags.Discard | SlimDX.DXGI.MapFlags.Write); //int rowSize = (m_Texture.Description.Width * 4); int stride = data.Pitch / 4; int stripeWidth = 16; int stripeValue = 0; bool stripeOn = false; for (int i = 0, ie = m_Texture.Description.Height; i < ie; i++) { stripeValue = 0; stripeOn = false; for (int p = 0; p < stride; p++) { if (stripeValue++ >= stripeWidth) { stripeOn = !stripeOn; stripeValue = 0; } if (stripeOn == true) { data.Data.Write(1f); } else { data.Data.Write(0f); } } } m_TextureSurface.Unmap(); m_TextureResourceView = new ShaderResourceView(GameEnvironment.Device, m_Texture, new ShaderResourceViewDescription() { Dimension = ShaderResourceViewDimension.Texture2D, Format = SlimDX.DXGI.Format.R32_Float, ArraySize = 1, MipLevels = 1, MostDetailedMip = 0, }); m_TestImage.TextureView = m_TextureResourceView; m_TestImage.LoadResources(); }
public override void LoadResources() { if (m_Source != null && m_Source.Device.DepthResolution != ImageResolution.Invalid) { if (Disposed == true) { m_Buffer = new float[KinectHelper.GetSizeForResolution(m_Source.Device.DepthResolution) * 4]; m_Texture = new Texture2D(GameEnvironment.Device, new Texture2DDescription() { Format = SlimDX.DXGI.Format.R32_Float, Width = KinectHelper.GetWidthForResolution(m_Source.Device.DepthResolution), Height = KinectHelper.GetHeightForResolution(m_Source.Device.DepthResolution), MipLevels = 1, ArraySize = 1, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Dynamic, SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0) }); m_TextureSurface = m_Texture.AsSurface(); DataRectangle data = m_TextureSurface.Map(SlimDX.DXGI.MapFlags.Discard | SlimDX.DXGI.MapFlags.Write); int stride = data.Pitch / 4; for (int i = 0, ie = m_Texture.Description.Height; i < ie; i++) { for (int p = 0; p < stride; p++) { data.Data.Write(0f); } } m_TextureSurface.Unmap(); m_TextureResourceView = new ShaderResourceView(GameEnvironment.Device, m_Texture, new ShaderResourceViewDescription() { Dimension = ShaderResourceViewDimension.Texture2D, Format = SlimDX.DXGI.Format.R32_Float, ArraySize = 1, MipLevels = 1, MostDetailedMip = 0, }); m_Image.TextureView = m_TextureResourceView; m_Image.LoadResources(); Disposed = false; } } }
public void LoadResources() { if (m_Disposed == true) { m_ScanBytes = new byte[m_Description.Width * 4]; m_Bytes = new byte[m_Description.Width * m_Description.Height * m_PixelSizeInBytes]; m_CaptureTexture = new Texture2D(GameEnvironment.Device, m_Description); m_CaptureSurface = m_CaptureTexture.AsSurface(); m_Bitmap = new System.Drawing.Bitmap(m_Description.Width, m_Description.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); m_Disposed = false; } }
public void RenderToTexture(Bitmap bmp) { BitmapData data = null; try { data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] dest = new byte[data.Stride * bmp.Height]; Marshal.Copy(data.Scan0, dest, 0, dest.Length); m_Texture = new Texture2D(GameEnvironment.Device, new Texture2DDescription() { Format = SlimDX.DXGI.Format.R16G16B16A16_Float, // .R16_Float, Width = bmp.Width, Height = bmp.Height, MipLevels = 1, ArraySize = 1, BindFlags = BindFlags.ShaderResource, CpuAccessFlags = CpuAccessFlags.Write, OptionFlags = ResourceOptionFlags.None, Usage = ResourceUsage.Dynamic, SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0) }); //DataRectangle dataRect = m_Texture.Map(0, MapMode.WriteDiscard, MapFlags.None); SlimDX.DXGI.Surface surface = m_Texture.AsSurface(); DataRectangle dataRect = surface.Map(SlimDX.DXGI.MapFlags.Write | SlimDX.DXGI.MapFlags.Discard); ; float cScale = 1f / 255f; for (int i = 0; i < bmp.Width * bmp.Height * 4; i += 4) { dataRect.Data.Write(new Half4(new Half((float)dest[i + 0] * cScale), new Half((float)dest[i + 1] * cScale), new Half((float)dest[i + 2] * cScale), new Half((float)dest[i + 3] * cScale))); } //m_Texture.Unmap(0); surface.Unmap(); surface.Dispose(); m_TextureView = new ShaderResourceView(GameEnvironment.Device, m_Texture, new ShaderResourceViewDescription() { Dimension = ShaderResourceViewDimension.Texture2D, Format = SlimDX.DXGI.Format.R16G16B16A16_Float, //.R16_Float, ArraySize = 1, MipLevels = 1, MostDetailedMip = 0, }); } finally { if (data != null) { bmp.UnlockBits(data); } } }
public void Evaluate(int SpreadMax) { if (!FInitialised) { FOutput[0] = new CVImageLink(); FInitialised = true; } try { if (FInput.PluginIO.IsConnected) { if (this.RenderRequest != null) { this.RenderRequest(this, this.FHost); } if (this.AssignedContext == null) { return; } var device = this.AssignedContext.Device; var context = this.AssignedContext; DX11Texture2D t = this.FInput[0][this.AssignedContext]; var height = t.Height; var width = t.Width; var imageLink = FOutput[0]; var imageAttributes = imageLink.ImageAttributes; var desiredImageFormat = ToOpenCVFormat(t.Format); if (desiredImageFormat == TColorFormat.UnInitialised) { throw (new Exception("No suitible image type available for this texture type" + t.Format.ToString())); } //-- //check attributes and reinitialise the image if we haven't got the right image ready // if (imageAttributes == null || FOffscreenBuffer == null || !imageAttributes.Initialised || FOffscreenBuffer.Description.Format != t.Description.Format || imageAttributes.Width != t.Width || imageAttributes.Height != t.Height || imageAttributes.ColourFormat != desiredImageFormat) { if (FOffscreenBuffer != null) { FOffscreenBuffer.Dispose(); } var description = new Texture2DDescription() { Width = width, Height = height, Format = t.Format, MipLevels = 1, Usage = ResourceUsage.Staging, BindFlags = BindFlags.None, CpuAccessFlags = CpuAccessFlags.Read, SampleDescription = new SampleDescription(1, 0), ArraySize = 1 }; FOffscreenBuffer = new Texture2D(this.AssignedContext.Device, description); imageLink.Initialise(new CVImageAttributes(desiredImageFormat, t.Width, t.Height)); } // //-- //-- //copy the texture to offscreen buffer // context.CurrentDeviceContext.CopyResource(t.Resource, FOffscreenBuffer); // //-- //-- //copy the data out of the offscreen buffer // var surface = FOffscreenBuffer.AsSurface(); var bytesPerRow = imageAttributes.Stride; var data = MapForRead(context.CurrentDeviceContext); lock (imageLink.BackLock) { var image = imageLink.BackImage; try { var source = data.Data.DataPointer; image.SetPixels(source); var destination = image.Data; for (int row = 0; row < t.Height; row++) { CopyMemory(destination, source, bytesPerRow); source += data.RowPitch; destination += bytesPerRow; } } finally { UnMap(context.CurrentDeviceContext); } } // //-- imageLink.Swap(); } } catch (Exception e) { FLogger.Log(e); } }