Skip to main content
GameDev.net gamedev.net
🔒 Locked

[SharpDX] Loading texture from file

Started by BlackJoker May 24, 2015 at 12:09 PM 9 replies 12.8k views
Original Post
BlackJoker
BlackJoker

Hello to everyone.

I faced with a little problem trying to load a texture from file.

The thing is that I am using pure sharpdx without Toolkit and there is no way to load just a texture.

I can load texture directly into ShaderResourceView by calling static method FromStream() or FromFile(), but it create ShaderResourceView and I need texture1d, 2d or 3d.

Yes, I can query Interface for any of these textures, but is there a way in sharpDX just to load Texture directly from a file and then do what I need?

Bacterius
Bacterius

All these texture classes (Direct3D9.Texture, Direct3D11.Texture{1,2,3}D, etc) also have FromFile() static methods that you can use, along with FromMemory() and FromStream(). For instance http://sharpdx.org/documentation/api/m-sharpdx-direct3d11-resource-fromfile--1-1. Have you tried these?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”
BlackJoker
BlackJoker

Actually they are not there right now.

From***() is only valid for ShaderResourceView and not more.

BTW, documentation is quite old and it is for version 2.4.2. Current version - 3.0.

Stefan Fischlschweiger
Stefan Fischlschweiger

I'm using this code to load images to Textures and Bitmaps:


using SharpDX;
using SharpDX.Direct2D1;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.WIC;
using Device = SharpDX.Direct3D11.Device;
using DeviceContext = SharpDX.Direct2D1.DeviceContext;
using PixelFormat = SharpDX.WIC.PixelFormat;

namespace Content.Textures
{
    public static class TextureLoader
    {
        private static readonly ImagingFactory Imgfactory = new ImagingFactory();

        public static Bitmap1 LoadBitmap(string filename, DeviceContext context)
        {
            var props = new BitmapProperties1
            {
                PixelFormat =
                    new SharpDX.Direct2D1.PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied)
            };

            return Bitmap1.FromWicBitmap(context, LoadBitmap(filename), props);
        }

        private static BitmapSource LoadBitmap(string filename)
        {
            var d = new BitmapDecoder(
                Imgfactory,
                filename,
                DecodeOptions.CacheOnDemand
                );

            var frame = d.GetFrame(0);

            var fconv = new FormatConverter(Imgfactory);

            fconv.Initialize(
                frame,
                PixelFormat.Format32bppPRGBA,
                BitmapDitherType.None, null,
                0.0, BitmapPaletteType.Custom);
            return fconv;
        }

        public static Texture2D CreateTex2DFromFile(Device device, string filename)
        {
            var bSource = LoadBitmap(filename);
            return CreateTex2DFromBitmap(device, bSource);
        }

        public static Texture2D CreateTex2DFromBitmap(Device device, BitmapSource bsource)
        {

            Texture2DDescription desc;
            desc.Width = bsource.Size.Width;
            desc.Height = bsource.Size.Height;
            desc.ArraySize = 1;
            desc.BindFlags = BindFlags.ShaderResource;
            desc.Usage = ResourceUsage.Default;
            desc.CpuAccessFlags = CpuAccessFlags.None;
            desc.Format = Format.R8G8B8A8_UNorm;
            desc.MipLevels = 1;
            desc.OptionFlags = ResourceOptionFlags.None;
            desc.SampleDescription.Count = 1;
            desc.SampleDescription.Quality = 0;

            var s = new DataStream(bsource.Size.Height*bsource.Size.Width*4, true, true);
            bsource.CopyPixels(bsource.Size.Width*4, s);

            var rect = new DataRectangle(s.DataPointer, bsource.Size.Width*4);

            var t2D = new Texture2D(device, desc, rect);

            return t2D;
        }
    }
}
BlackJoker
BlackJoker

Lordadmiral Drake

Interesting solution. Will it correctly load texture cube also? (array of 6 textures in one file)

Did you try that?

And could you explain what means "4" here?


bsource.Size.Width*4

Stefan Fischlschweiger
Stefan Fischlschweiger

I don't know about cube textures since I haven't used any so far. I assume you'd need to modify the loader to process those too.


bsource.Size.Width*4 

is the size of one pixel line in bytes

Quoting from SharpDX documentation (BitmapSource.CopyPixels):

The cbStride parameter defines the count of bytes between two vertically adjacent pixels in the output buffer
BlackJoker
BlackJoker




I don't know about cube textures since I haven't used any so far. I assume you'd need to modify the loader to process those too.

You mean instead of


var frame = d.GetFrame(0);

I need to get number of frames and load all of them?

And the dame question regarding texture1d and 3d. Did you try to load at least any of them?

renman29
renman29

I was hopeful someone might work on some kind of tools for version 3+ (or a depot where ppl could put useful code)

(Right now, I'm using 2.6.3 toolkit package for my one project)

I suppose depending on one's needs, one could use the Paradox engine too and build from that - seems like it's kinda taken over as the toolkit.

I noticed it has stuff like Asset.Load or like Texture[type].New( //creation params... )

Frisk7
Frisk7

I see I'm not the only one having problems with this. I have been modifying one of my projects to use SharpDX 3.0.0 instead of 2.6.whatever, and it has mostly been problem-free - apart from the texture loading....and the code snippet by LordArmiral Drake above looks like a solution to my problems. Thanks.

Stefan Fischlschweiger
Stefan Fischlschweiger

This code works at least for jpg, png, tga and dds.

Glad I could help. Though I didn't write this code mostly. You can find part of it in the SharpDX samples, though I found it on some other site. It was some work to get rid of the toolkit. But i managed to kick it out of my code

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.