Domanda

I'm creating a Windows Phone 8 app and I'm using SharpDX to load a texture.

I've tried to load PNG using Content.Load<Texture2D>("filename.png") but I've got errors and I've realized that SharpDX in Windows Phone 8 only accepts DDS textures and doesn't accept PNG/JPEG/BMP etc. I've converted my PNG to DDS using texconv filename.png and added the file to my solution just like other files that I'm using in my project (that I can load correctly). When I run my app, I'm getting an exception:

{SharpDX.Serialization.InvalidChunkException: Unexpected chunk [DDS /0x20534444] instead of [TKFX/0x58464B54] at SharpDX.Serialization.BinarySerializer.BeginChunk(FourCC chunkId)}

Well, I've converted my valid PNG to DDS using the texconv tool. From what I see, it's a file format error, and the fourCC code corresponds to DDS, which is as it should be. But SharpDX expects TKFX format (toolkit effect?) instead of DDS. Why? I am trying to load a texture with Content.Load<Texture2D> not Content.Load<Effect> (I also have it to load a shader and it works perfectly).

If anyone finds out how to load a PNG instead of DDS, that's even better!

È stato utile?

Soluzione

In case you still want to load PNG images, I'll just leave this code snippet here:

public static Texture2D FromImage(BitmapImage image, GraphicsDevice device)
{
    WriteableBitmap bitmap = new WriteableBitmap(image);

    return FromImageData(bitmap.Pixels, bitmap.PixelWidth, 
                            bitmap.PixelHeight, device);
}

public static Texture2D FromImageData(int[] data, int width, int height, GraphicsDevice device)
{
    Texture2D texture = Texture2D.New(device, width, height,
                                    PixelFormat.B8G8R8A8.UNorm);

    texture.SetData<int>(data);

    return texture;
}

Altri suggerimenti

I've figured the problem out. I've switched the order of the lines of loading my texture and loading my shader. This time, the texture loaded correctly, and my shader failed with this error:

{SharpDX.Serialization.InvalidChunkException: Unexpected chunk [TKFX/0x58464B54] instead of [TKTX/0x58544B54] at SharpDX.Serialization.BinarySerializer.BeginChunk(FourCC chunkId)}

It was expecting TKTX (possibly, toolkit texture) but got TKFX which is a shader. I don't currently have a solution for this misbehavior of ContentManager, but it wasn't about the DDS file.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top