/// <summary> /// Sets given content to the constant buffer. /// </summary> /// <param name="deviceContext">The context used for updating the constant buffer.</param> /// <param name="dataToSet">The data to set.</param> internal void SetData(D3D11.ID3D11DeviceContext deviceContext, T dataToSet) { var dataBox = deviceContext.Map(this.ConstantBuffer, 0, D3D11.MapMode.WriteDiscard, D3D11.MapFlags.None); SdxUtilities.Write(dataBox.DataPointer, ref dataToSet); deviceContext.Unmap(this.ConstantBuffer, 0); }
private static unsafe void EncodeImage(PixelBuffer image, WICFlags flags, IWICBitmapFrameEncode frame) { Guid pfGuid; if (!ToWIC(image.Format, out pfGuid)) { throw new NotSupportedException("Format not supported"); } frame.Initialize(); frame.SetSize(image.Width, image.Height); frame.SetResolution(72, 72); var targetGuid = pfGuid; frame.SetPixelFormat(ref targetGuid); if (targetGuid != pfGuid) { using (var source = Factory.CreateBitmapFromMemory(image.Width, image.Height, pfGuid, image.RowStride, image.BufferStride, image.DataPointer.ToPointer())) { using (var converter = Factory.CreateFormatConverter()) { using (var palette = Factory.CreatePalette()) { palette.InitializeFromBitmap(source, 256, true); converter.Initialize(source, targetGuid, GetWICDither(flags), palette, 0, BitmapPaletteType.Custom); var bpp = GetBitsPerPixel(targetGuid); if (bpp == 0) { throw new NotSupportedException("Unable to determine the Bpp for the target format"); } var rowPitch = (image.Width * bpp + 7) / 8; var slicePitch = rowPitch * image.Height; var temp = SdxUtilities.AllocateMemory(slicePitch); try { converter.CopyPixels(rowPitch, slicePitch, temp); frame.SetPalette(palette); frame.WritePixels(image.Height, temp, rowPitch, slicePitch); } finally { SdxUtilities.FreeMemory(temp); } } } } } else { // No conversion required frame.WritePixels(image.Height, image.DataPointer, image.RowStride, image.BufferStride); } frame.Commit(); }
public PixelFormat(PixelFormatFlags flags, int fourCC, int rgbBitCount, uint rBitMask, uint gBitMask, uint bBitMask, uint aBitMask) { Size = SdxUtilities.SizeOf <PixelFormat>(); Flags = flags; FourCC = fourCC; RGBBitCount = rgbBitCount; RBitMask = rBitMask; GBitMask = gBitMask; BBitMask = bBitMask; ABitMask = aBitMask; }
public void Dispose() { if (handle.HasValue) { handle.Value.Free(); } if (bufferIsDisposable) { SdxUtilities.FreeMemory(buffer); } }
/// <summary> /// Creates the constant buffer object. /// </summary> protected internal override D3D11.ID3D11Buffer CreateConstantBuffer(EngineDevice device) { using (var dataStream = new DataStream(SdxUtilities.SizeOf <T>(), true, true)) { dataStream.Write(_initialData); dataStream.Position = 0; return(device.DeviceD3D11_1.CreateBuffer( new D3D11.BufferDescription( _structureSize, D3D11.ResourceUsage.Dynamic, D3D11.BindFlags.ConstantBuffer, D3D11.CpuAccessFlags.Write, D3D11.ResourceOptionFlags.None, 0), dataStream.DataPointer)); } }
internal unsafe void Initialize(ImageDescription description, IntPtr dataPointer, int offset, GCHandle?handle, bool bufferIsDisposable, PitchFlags pitchFlags = PitchFlags.None) { if (!FormatHelper.IsValid(description.Format) || FormatHelper.IsVideo(description.Format)) { throw new InvalidOperationException("Unsupported DXGI Format"); } this.handle = handle; switch (description.Dimension) { case TextureDimension.Texture1D: if (description.Width <= 0 || description.Height != 1 || description.Depth != 1 || description.ArraySize == 0) { throw new InvalidOperationException("Invalid Width/Height/Depth/ArraySize for Image 1D"); } // Check that miplevels are fine description.MipLevels = MipMapHelper.CalculateMipLevels(description.Width, 1, description.MipLevels); break; case TextureDimension.Texture2D: case TextureDimension.TextureCube: if (description.Width <= 0 || description.Height <= 0 || description.Depth != 1 || description.ArraySize == 0) { throw new InvalidOperationException("Invalid Width/Height/Depth/ArraySize for Image 2D"); } if (description.Dimension == TextureDimension.TextureCube) { if (description.ArraySize % 6 != 0) { throw new InvalidOperationException("TextureCube must have an arraysize = 6"); } } // Check that miplevels are fine description.MipLevels = MipMapHelper.CalculateMipLevels(description.Width, description.Height, description.MipLevels); break; case TextureDimension.Texture3D: if (description.Width <= 0 || description.Height <= 0 || description.Depth <= 0 || description.ArraySize != 1) { throw new InvalidOperationException("Invalid Width/Height/Depth/ArraySize for Image 3D"); } // Check that miplevels are fine description.MipLevels = MipMapHelper.CalculateMipLevels(description.Width, description.Height, description.Depth, description.MipLevels); break; } // Calculate mipmaps int pixelBufferCount; mipMapToZIndex = CalculateImageArray(description, pitchFlags, out pixelBufferCount, out totalSizeInBytes); mipmapDescriptions = CalculateMipMapDescription(description, pitchFlags); zBufferCountPerArraySlice = mipMapToZIndex[mipMapToZIndex.Count - 1]; // Allocate all pixel buffers pixelBuffers = new PixelBuffer[pixelBufferCount]; pixelBufferArray = new PixelBufferArray(this); // Setup all pointers // only release buffer that is not pinned and is asked to be disposed. this.bufferIsDisposable = !handle.HasValue && bufferIsDisposable; buffer = dataPointer; if (dataPointer == IntPtr.Zero) { buffer = SdxUtilities.AllocateMemory(totalSizeInBytes); offset = 0; this.bufferIsDisposable = true; } SetupImageArray((IntPtr)((byte *)buffer + offset), totalSizeInBytes, description, pitchFlags, pixelBuffers); Description = description; // PreCompute databoxes dataBoxArray = this.ComputeDataBox(); }
public static Image Load(Stream imageStream) { // Else Read the whole stream into memory. return(Load(SdxUtilities.ReadStream(imageStream))); }
/// <summary> /// Initializes a new instance of the <see cref="TypeSafeConstantBufferResource{T}" /> class. /// </summary> public TypeSafeConstantBufferResource(T initialData) : base(SdxUtilities.SizeOf <T>()) { _initialData = initialData; _structureSize = SdxUtilities.SizeOf <T>(); }
internal static Image LoadFromWICMemory(IntPtr pSource, int size, bool makeACopy, GCHandle?handle) { var flags = WICFlags.AllFrames; Image image = null; // Create input stream for memory using (var stream = Factory.CreateStream(new DataStream(pSource, size, true, false))) { // If the decoder is unable to decode the image, than return null IWICBitmapDecoder decoder = null; try { decoder = Factory.CreateDecoderFromStream(stream, DecodeOptions.CacheOnDemand); using (var frame = decoder.GetFrame(0)) { // Get metadata Guid convertGuid; var tempDesc = DecodeMetadata(flags, decoder, frame, out convertGuid); // If not supported. if (!tempDesc.HasValue) { return(null); } var mdata = tempDesc.Value; if (mdata.ArraySize > 1 && (flags & WICFlags.AllFrames) != 0) { return(DecodeMultiframe(flags, mdata, decoder)); } image = DecodeSingleFrame(flags, mdata, convertGuid, frame); } } catch { image = null; } finally { if (decoder != null) { decoder.Dispose(); } } } // For WIC, we are not keeping the original buffer. if (image != null && !makeACopy) { if (handle.HasValue) { handle.Value.Free(); } else { SdxUtilities.FreeMemory(pSource); } } return(image); }