Jump to content
  • Advertisement
Sign in to follow this  
Albeoris

The texture appears partially

This topic is 1011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I am trying to load a texture from memory in the one device and render to the other.

If I'll save the texture to a file before drawing it's works fine.

But if I will not to do that the texture will not be shown or appears partially.

 

http://i.imgur.com/ndngFHS.png

 

Crating of the texture:

_device = new Device(DriverType.Hardware, DeviceCreationFlags.Debug);

private static DxTexture Read2DTextureFromStream(GtexData gtex, Stream input)
{
    Texture2DDescription descriptor = Get2DTextureDescription(gtex);

    using (SafeUnmanagedArray array = new SafeUnmanagedArray(gtex.MipMapData.Sum(d => d.Length)))
    {
        DataRectangle[] rects = new DataRectangle[gtex.MipMapData.Length];
        using (UnmanagedMemoryStream io = array.OpenStream(FileAccess.Write))
        {
            byte[] buff = new byte[32 * 1024];
            for (int index = 0; index < gtex.MipMapData.Length; index++)
            {
                GtexMipMapLocation mimMap = gtex.MipMapData[index];
                Int32 pitch = GetPitch(descriptor, index);
                rects[index] = CreateDataRectangle(array, io, pitch);
                input.SetPosition(mimMap.Offset);
                input.CopyToStream(io, mimMap.Length, buff);
            }
        }
        
        Texture2D texture = new Texture2D(_device, descriptor, rects);
        // Resource.ToFile(_device.ImmediateContext, texture, ImageFileFormat.Dds, "tmp.dds"); // <=== Workaround
        return new DxTexture(texture, descriptor);
    }
}

private static Texture2DDescription Get2DTextureDescription(GtexData gtex)
{
    if (gtex.Header.IsCubeMap)
        throw new Exception("IsCubeMap: true");

    return new Texture2DDescription
    {
        ArraySize = 1,
        Width = gtex.Header.Width,
        Height = gtex.Header.Height,
        Format = GetDxFormat(gtex.Header.Format),
        
        MipLevels = gtex.Header.MipMapCount,
        
        BindFlags = BindFlags.ShaderResource,
        OptionFlags = ResourceOptionFlags.Shared,
        
        Usage = ResourceUsage.Default,
        CpuAccessFlags = CpuAccessFlags.None,
        SampleDescription = new SampleDescription(1, 0)
    };
}

Rendering:

public void Draw(Device device, SpriteBatch spriteBatch, Vector2 position, Rectangle? sourceRectangle, float layerDepth)
{
    ShaderResourceView shaderView = GetShaderResourceView(device);
    spriteBatch.Draw(shaderView,
        position,
        sourceRectangle,
        new Color(0xff, 0xff, 0xff, 0xff),
        0,
        Vector2.Zero,
        Vector2.One,
        SpriteEffects.None, layerDepth);
}

private ShaderResourceView GetShaderResourceView(Device device)
{
    lock (_sharedViews)
    {
        IntPtr viewPointer;
        if (_sharedViews.TryGetValue(device.NativePointer, out viewPointer))
            return new ShaderResourceView(viewPointer);

        using (SharpDX.DXGI.Resource resource = Texture.QueryInterface<SharpDX.DXGI.Resource>())
        using (Resource sharedResource = device.OpenSharedResource<Resource>(resource.SharedHandle))
        {
            ShaderResourceView shaderView = new ShaderResourceView(device, sharedResource);
            _sharedViews.Add(device.NativePointer, shaderView.NativePointer);
            return shaderView;
        }
    }
}

What did I do wrong?

Share this post


Link to post
Share on other sites
Advertisement

The first device is used to load the textures, convert it, and save it to the file. The second device is part of the preview control, which is used to display the texture. The third device used in the editor window. Preview window and editor window can be displayed at same time.

Edited by Albeoris

Share this post


Link to post
Share on other sites

You don't need anything more than one 3d Device as far as I know. Of course there may be more advanced scenarios than I have considered. But I don't think your's is one of them.

Share this post


Link to post
Share on other sites

I'd like to see more code, such as the call to Draw()

 

1) private void OnRenderControlPaint(object sender, PaintEventArgs e)

https://github.com/Albeoris/Pulse/blob/b51b441814e9763f423ffeeff52e88a26ba4b9c3/Pulse.DriectX/UiDxControl.cs

 

2) add { DxControl.DrawSprites += (device, spriteBatch, clipRectangle) => value(device, spriteBatch, new Rectangle(X, Y, clipRectangle.Width, clipRectangle.Height)); }

https://github.com/Albeoris/Pulse/blob/b51b441814e9763f423ffeeff52e88a26ba4b9c3/Pulse.UI/Controls/UiDxViewport.cs

 

3) private void DrawSprites(Device device, SpriteBatch spritebatch, Rectangle cliprectangle)

https://github.com/Albeoris/Pulse/blob/b51b441814e9763f423ffeeff52e88a26ba4b9c3/Pulse.UI/Controls/UiDxTextureViewer.cs

 

4) public void Draw(Device device, SpriteBatch spriteBatch, Vector2 position, Rectangle? sourceRectangle, float layerDepth)

https://github.com/Albeoris/Pulse/blob/b51b441814e9763f423ffeeff52e88a26ba4b9c3/Pulse.DriectX/DxTexture.cs

 

Done.

 


You don't need anything more than one 3d Device as far as I know. Of course there may be more advanced scenarios than I have considered. But I don't think your's is one of them.


You can do all this using a single Device instance

 

Sorry, but I need.

Share this post


Link to post
Share on other sites

Does your preview view (which I assume is your UiDXControl.cs) only display the plain texture or a full preview of the main editor window?

 

EDIT. I took the liberty of pulling your code from github and try run it myself to see if I can come up with a solution that way. But I'm getting heaps of xaml errors ^^

Edited by Lordadmiral Drake

Share this post


Link to post
Share on other sites


Does your preview view (which I assume is your UiDXControl.cs) only display the plain texture or a full preview of the main editor window?

Yes.

 


EDIT. I took the liberty of pulling your code from github and try run it myself to see if I can come up with a solution that way. But I'm getting heaps of xaml errors ^^

Latest version is required Visual Studio 2015. Heaps of xaml errors are result of comiling errors of the other library.

Share this post


Link to post
Share on other sites

I had a look at the code but without being able to debug the application, it seems like a pretty difficult prospect to find what's wrong.

When you save it to file are you saving it on one device, and then copying to the other device ? Is the copy operation chopping off the bottom of the texture?

When i look at the texture, it looks like it's not loading properly to me, if that image is the save. Or else you're clipping it, but I can't tell which part is texture and which part is client space.

Edited by Gavin Williams

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!