Reputation: 61
I have 0 experience with D3D. I am currently reading Frank Luna's book about D3D11 and trying to make examples working in C# using SharpDX though w/o effect framework. I stuck with texturing now. I don't understand how to load texture from file and how to send it to shader.
Official wiki is dead. Or at least it doesn't load for me. I searched here but found only some way to do it with SharpDX.WIC which is with D2D so it looks a bit weird for me to use some D2D components in D3D app.
Upvotes: 2
Views: 8029
Reputation: 390
I personally like to keep some textures in resources (inside exe). To get the bitmap from resources you can use YOURPROJECT.Properties.Resources.BITMAPNAME in C#.
Then I've made a little converter function for GDI+ bitmaps to WIC bitmap:
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;
}
This will create WIC bitmap for you and then you can use CreateTexture2DFromBitmap to use it in your drawing pipeline.
Upvotes: 0
Reputation: 518
Sorry for posting on an old thread, but isn't it much better to use what is provided in .NET framework to achieve this?
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;
Upvotes: 3
Reputation: 144
WIC is not part of direct2d, wic is the best way to load images I know of in directX even in c++ you must use wic so just follow the link in the first answer and you will be fine.
Or
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));
}
}
}
I just pulled it from the link so it may need some changes to work with SharpDX 3,i use almost the same code path but I have more options in the loading stage eg. non-premultiplied for normals, convert to Srgb and stuff like that.
Upvotes: 0
Reputation: 852
Look here for the texture loader: How to load a texture from a file?
Usage:
var device = this.DeviceManager.Direct3DDevice;
var texture = TextureLoader.CreateTexture2DFromBitmap(device, TextureLoader.LoadBitmap(new SharpDX.WIC.ImagingFactory2(), "Texture2.png"));
ShaderResourceView textureView = new ShaderResourceView(device, texture);
Upvotes: 0