public H1ImageWrapper(String filePath) { // to manipulate WIC objects ImagingFactory imagingFactory = new ImagingFactory(); // to open the file that holds the bitmap data NativeFileStream fileStream = new NativeFileStream( filePath, NativeFileMode.Open, NativeFileAccess.Read); BitmapDecoder bitmapDecoder = new BitmapDecoder(imagingFactory, fileStream, DecodeOptions.CacheOnDemand // for the time being as we won't be needing to take advantage of special cache handling ); // to retrieve the frame index 0 (static image only have one frame) const Int32 StaticFrame = 0; BitmapFrameDecode frame = bitmapDecoder.GetFrame(StaticFrame); // convert out bitmaps to the same pixel format for the shake of normalization FormatConverter converter = new FormatConverter(imagingFactory); converter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPRGBA); // set the pixel format SetPixelFormat(converter.PixelFormat); // having the correct pixel format, we can finally create the desired SharpDX.Direct2D1.Bitmap1 Int32 width = converter.Size.Width; Int32 height = converter.Size.Height; Int32 stride = converter.Size.Width * 4; Int32 dataSize = height * stride; using (var buffer = new SharpDX.DataStream(dataSize, true, true)) { // copy the data to the buffer converter.CopyPixels(stride, buffer); H1GeneralBuffer generalBuffer = H1Global <H1ManagedRenderer> .Instance.CreateGeneralBuffer(Convert.ToUInt32(dataSize)); // mapping the data to the resource (buffer) generalBuffer.WriteData(buffer.DataPointer, dataSize); // first create texture resource //H1Texture2D textureObject = H1Global<H1ManagedRenderer>.Instance.CreateTexture2D(PixelFormat, width, height, new SharpDX.Vector4(), null); m_tempTextureObject = H1Global <H1ManagedRenderer> .Instance.CreateTexture2D(PixelFormat, width, height, new SharpDX.Vector4(), null); // copy texture region //H1Global<H1ManagedRenderer>.Instance.CopyTextureRegion(textureObject, generalBuffer); H1Global <H1ManagedRenderer> .Instance.CopyTextureRegion(m_tempTextureObject, generalBuffer); } //https://english.r2d2rigo.es/2014/08/12/loading-and-drawing-bitmaps-with-direct2d-using-sharpdx/ //http://stackoverflow.com/questions/9602102/loading-textures-with-sharpdx-in-windows-store-app //http://sharpdx.org/wiki/class-library-api/wic/ }
public void CopyTextureRegion(H1Texture2D texObject, H1GeneralBuffer generalBuffer) { // reuse the memory associated with command recording // we can only reset when the associated command lists have finished execution on the GPU m_CommandList.CommandAllocator.Reset(); // a command list can be reset after it has been added to the command queue via ExecuteCommandList m_CommandList.CommandList.Reset(m_CommandList.CommandAllocator, null); // @TODO - I need to change this into parallel copy texture region by managing multiple command queue H1GPUResourceManager refResourceManager = H1Global <H1ManagedRenderer> .Instance.ResourceManager; // create destination and source locations // TextureCopyLocation - describe a portion of texture for the purpose of texture copies TextureCopyLocation destLocation = new TextureCopyLocation(refResourceManager.GetTexture2D(Convert.ToInt32(texObject.Index)), 0); TextureCopyLocation srcLocation = new TextureCopyLocation(generalBuffer.Resource, new PlacedSubResourceFootprint() // describes the footprint of a placed subresource, including the offset and the D3D12_SUBRESOURCE_FOOTPRINT { Offset = 0, // the offset of the subresource within the parent resource in bytes Footprint = new SubResourceFootprint() // describes the format, with, height, depth and row-pitch of the subresource into the parent resource { Width = Convert.ToInt32(texObject.Width), Height = Convert.ToInt32(texObject.Height), Depth = 1, Format = H1RHIDefinitionHelper.ConvertToFormat(texObject.PixelFormat), RowPitch = Convert.ToInt32(texObject.Stride), } }); m_CommandList.CommandList.ResourceBarrierTransition(texObject.Resource, ResourceStates.GenericRead, ResourceStates.CopyDestination); m_CommandList.CommandList.CopyTextureRegion(destLocation, 0, 0, 0, srcLocation, null); m_CommandList.CommandList.ResourceBarrierTransition(texObject.Resource, ResourceStates.CopyDestination, ResourceStates.GenericRead); m_CommandList.CommandList.Close(); m_DeviceContext.MainCommandListPool.CommandQueue.ExecuteCommandList(m_CommandList.CommandList); }