public override MappedTexture2D Map(int subResource, MapType map, bool doNotWait)
        {
            MappedTexture2D m       = new MappedTexture2D();
            int             rowSize = Width * Utils.GetSize(glType) * Utils.GetElements(glClientFormat);

            m.RowPitch = rowSize + rowSize % PACK_ALIGNMENT;

            if (stagingBuffer == null)
            {
                if (Description.Options == ResourceOptionFlags.TextureCube)
                {
                    int arrayIndex;
                    int level;
                    Texture2DBase.DecoupleSubresource(subResource, MipLevels, out arrayIndex, out level);
                    GL.BindTexture(All.TextureCubeMap, texture);
                    GL.ExtGetTexSubImageQCOM(Utils.GetTextureCubeFace(arrayIndex), level, 0, 0, 0, Width, Height, 0, glClientFormat, glType, m.DataPointer);
                }
                else
                {
                    GL.BindTexture(All.Texture2D, texture);
                    GL.ExtGetTexSubImageQCOM(All.Texture2D, subResource, 0, 0, 0, Width, Height, 0, glClientFormat, glType, m.DataPointer);
                }
            }
            else
            {
                handle        = GCHandle.Alloc(stagingBuffer, GCHandleType.Pinned);
                m.DataPointer = ClrRuntime.Runtime.GetPtr(stagingBuffer, 0);
            }
            return(m);
        }
Example #2
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.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));
        }