SharpDX 3.0.2 D3D11 - 如何从文件加载纹理并使其在着色器中工作?

时间:2016-03-17 18:16:42

标签: c# directx directx-11 sharpdx

我有D3D经验。我目前正在阅读Frank Luna关于D3D11的书,并尝试使用SharpDX在C#中使用无效框架。我现在坚持使用纹理。我不明白如何从文件加载纹理以及如何将其发送到着色器。

官方维基已死。或者至少它不会为我加载。 我在这里搜索但发现只有一些方法可以使用SharpDX.WIC来处理D2D,所以在D3D应用程序中使用一些D2D组件看起来有点奇怪。

4 个答案:

答案 0 :(得分:2)

很抱歉在一个旧帖子上发帖,但使用.NET框架中提供的内容来实现这一点并不是更好吗?

    if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
    {
        bitmap = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format32bppArgb);
    }
    var data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
    var ret = new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
    {
        Width = bitmap.Width,
        Height = bitmap.Height,
        ArraySize = 1,
        BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
        Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
        CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
        Format = SharpDX.DXGI.Format.B8G8R8A8_UNorm,
        MipLevels = 1,
        OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
        SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
    }, new SharpDX.DataRectangle(data.Scan0, data.Stride));
    bitmap.UnlockBits(data);
    return ret;

答案 1 :(得分:0)

在这里查看纹理加载器:How to load a texture from a file?

用法:

var device = this.DeviceManager.Direct3DDevice;
var texture = TextureLoader.CreateTexture2DFromBitmap(device, TextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "Texture2.png"));
ShaderResourceView textureView = new ShaderResourceView(device, texture);

答案 2 :(得分:0)

WIC不是direct2d的一部分,wic是加载我在directX中知道的图像的最佳方式,即使在c ++中也必须使用wic,所以只需按照第一个答案中的链接进行操作即可。

 public class TextureLoader
{
    /// <summary>
    /// Loads a bitmap using WIC.
    /// </summary>
    /// <param name="deviceManager"></param>
    /// <param name="filename"></param>
    /// <returns></returns>
    public static SharpDX.WIC.BitmapSource LoadBitmap(SharpDX.WIC.ImagingFactory2 factory, string filename)
    {
        var bitmapDecoder = new SharpDX.WIC.BitmapDecoder(
            factory,
            filename,
            SharpDX.WIC.DecodeOptions.CacheOnDemand
            );

        var formatConverter = new SharpDX.WIC.FormatConverter(factory);

        formatConverter.Initialize(
            bitmapDecoder.GetFrame(0),
            SharpDX.WIC.PixelFormat.Format32bppPRGBA,
            SharpDX.WIC.BitmapDitherType.None, 
            null,
            0.0, 
            SharpDX.WIC.BitmapPaletteType.Custom);

        return formatConverter;
    }

    /// <summary>
    /// Creates a <see cref="SharpDX.Direct3D11.Texture2D"/> from a WIC <see cref="SharpDX.WIC.BitmapSource"/>
    /// </summary>
    /// <param name="device">The Direct3D11 device</param>
    /// <param name="bitmapSource">The WIC bitmap source</param>
    /// <returns>A Texture2D</returns>
    public static SharpDX.Direct3D11.Texture2D CreateTexture2DFromBitmap(SharpDX.Direct3D11.Device device, SharpDX.WIC.BitmapSource bitmapSource)
    {
        // Allocate DataStream to receive the WIC image pixels
        int stride = bitmapSource.Size.Width * 4;
        using (var buffer = new SharpDX.DataStream(bitmapSource.Size.Height * stride, true, true))
        {
            // Copy the content of the WIC to the buffer
            bitmapSource.CopyPixels(stride, buffer);
            return new SharpDX.Direct3D11.Texture2D(device, new SharpDX.Direct3D11.Texture2DDescription()
            {
                Width = bitmapSource.Size.Width,
                Height = bitmapSource.Size.Height,
                ArraySize = 1,
                BindFlags = SharpDX.Direct3D11.BindFlags.ShaderResource,
                Usage = SharpDX.Direct3D11.ResourceUsage.Immutable,
                CpuAccessFlags = SharpDX.Direct3D11.CpuAccessFlags.None,
                Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm,
                MipLevels = 1,
                OptionFlags = SharpDX.Direct3D11.ResourceOptionFlags.None,
                SampleDescription = new SharpDX.DXGI.SampleDescription(1, 0),
            }, new SharpDX.DataRectangle(buffer.DataPointer, stride));
        }
    }
}

我只是从链接中取出它所以可能需要进行一些更改才能使用SharpDX 3,我使用几乎相同的代码路径,但我在加载阶段有更多选项,例如。对于法线非预乘,转换为Srgb和类似的东西。

答案 3 :(得分:0)

我个人喜欢在资源中保留一些纹理(在exe内部)。要从资源获取位图,您可以在C#中使用YOURPROJECT.Properties.Resources.BITMAPNAME。

然后,我为GIC +位图制作了一个转换器函数,用于WIC位图:

    public static unsafe SharpDX.WIC.Bitmap CreateWICBitmapFromGDI(
        System.Drawing.Bitmap gdiBitmap)
    {
        var wicFactory = new ImagingFactory();
        var wicBitmap = new SharpDX.WIC.Bitmap(
            wicFactory, gdiBitmap.Width, gdiBitmap.Height,
            SharpDX.WIC.PixelFormat.Format32bppBGRA, 
            BitmapCreateCacheOption.CacheOnLoad);

        System.Drawing.Rectangle rect = new System.Drawing.Rectangle(
            0, 0, gdiBitmap.Width, gdiBitmap.Height);
        var btmpData = gdiBitmap.LockBits(rect,
            System.Drawing.Imaging.ImageLockMode.WriteOnly,
            System.Drawing.Imaging.PixelFormat.Format32bppArgb);

        byte* pGDIData = (byte*)btmpData.Scan0;
        using (BitmapLock bl = wicBitmap.Lock(BitmapLockFlags.Write))
        {
            byte* pWICData = (byte*)bl.Data.DataPointer;
            for (int y = 0; y < gdiBitmap.Height; y++)
            {
                int offsetWIC = y * bl.Stride;
                int offsetGDI = y * btmpData.Stride;
                for (int x = 0; x < gdiBitmap.Width; x++)
                {
                    pWICData[offsetWIC + 0] = pGDIData[offsetGDI + 0];  //R
                    pWICData[offsetWIC + 1] = pGDIData[offsetGDI + 1];  //G
                    pWICData[offsetWIC + 2] = pGDIData[offsetGDI + 2];  //B
                    pWICData[offsetWIC + 3] = pGDIData[offsetGDI + 3];      //A
                    offsetWIC += 4;
                    offsetGDI += 4;
                }
            }
        }

        gdiBitmap.UnlockBits(btmpData);

        return wicBitmap;
    }

这将为您创建WIC位图,然后您可以使用CreateTexture2DFromBitmap在绘图管道中使用它。