SharpDX 3.0.2 D3D11 - 如何从文件加载的质感,使其在着色器工作?(SharpD

2019-09-29 17:35发布

我有D3D 0经验。 我目前正在读弗兰克Luna的关于D3D11的书,试图让使用SharpDX在C#示例工作虽然W / O效果的框架。 我坚持了纹理现在。 我不明白如何从文件,以及如何将其发送到着色器加载纹理。

官方Wiki是死了。 或者至少它不会加载我。 我搜索在这里,但只找到某种方式与SharpDX.WIC这与D2D所以它看起来有点怪异,我在D3D应用程序中使用一些D2D组件来做到这一点。

Answer 1:

看看这里的纹理装载机: 如何从文件加载纹理?

用法:

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


Answer 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;


Answer 3:

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和类似的东西。



Answer 4:

我个人比较喜欢保留一些纹理资源(内部EXE)。 为了充分利用资源,你可以在C#中使用YOURPROJECT.Properties.Resources.BITMAPNAME位图。

然后我做了GDI +位图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在绘图管线使用它。



文章来源: SharpDX 3.0.2 D3D11 - How to load texture from file and make it to work in shader?