Jump to content
  • Advertisement
Sign in to follow this  
Albeoris

How to draw a texture out of bounds of the RenderControl?

This topic is 1183 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 everyone!

 

Please, tell me: how to draw a texture out of bounds of the RenderControl?

I have used a WPF and the RenderControl as a child of the WindowsFormsHost.

 

Initialization:

ModeDescription bufferDescription = new ModeDescription()
{
    Width = 4096,
    Height = 4096,
    RefreshRate = new Rational(60, 1),
    Format = Format.R8G8B8A8_UNorm
};

SwapChainDescription swapChainDescription = new SwapChainDescription()
{
    ModeDescription = bufferDescription,
    SampleDescription = new SampleDescription(1, 0),
    Usage = Usage.RenderTargetOutput,
    BufferCount = 1,
    OutputHandle = RenderControl.Handle,
    IsWindowed = true
};

Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapChainDescription, out Device, out SwapChain);

using (Texture2D backBuffer = SwapChain.GetBackBuffer<Texture2D>(0))
    RenderTargetView = new RenderTargetView(Device, backBuffer);

ImmediateContext = Device.ImmediateContext;

GraphicsDevice = new GenericGraphicsDevice(Device);
GraphicsDevice.SetRenderTargets(RenderTargetView);

SpriteBatch = new SpriteBatch(GraphicsDevice);

Drawing:

ImmediateContext.Rasterizer.SetViewport(0, 0, viewportWidth, viewportHeight, 0, 1);
SwapChain.Present(1, PresentFlags.None);
RenderControl.Refresh();

ImmediateContext.ClearRenderTargetView(Colors.Purple);

SpriteBatch.Begin(SpriteSortMode.BackToFront, null);

SpriteBatch.Draw(dxTex.ShaderTextureView,
   Vector2.Zero, // position
   new Rectangle(0, 0, dxTex.Width, dxTex.Height), // rect
   new SharpDX.Color(0xff, 0xff, 0xff, 0xff), // color
   0, // rotationg
   Vector2.Zero, // origin
   Vector2.One, // scale
   SpriteEffects.None, // effects
   1.0f); // depth

dxControl.SpriteBatch.End();
If the texture in the bounds of the RenderControl it's working fine.
I have a horziontal and a vertical scrollbars. When their value changing, I update the viewport:
ImmediateContext.Rasterizer.SetViewport(-horizontalOffset, -verticalOffset, viewportWidth, viewportHeight, 0, 1);
SwapChain.Present(1, PresentFlags.None);
RenderControl.Refresh();

But I cannot see a texture out of bounds of the RenderControl:

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

 

And it scale together window! O.o

http://i.imgur.com/5nXAA7X.png

 

And the texture is displayed incompletely.

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

 

But after reload...

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

 

It's magic! O.o

 

What is wrong? Is it my fault? Or a bug in the RenderControl?

Please tell me - how to correctly render a texture in WPF, using SharpDX (DirectX11).

 

Thank you for your attention. Have a nice day.

Edited by Albeoris

Share this post


Link to post
Share on other sites
Advertisement

I don't know if it's a bug, but If I understand correctly:

I believe the display resolution of the backbuffer must be a valid device screen resolution, so it would use the closest matching resolution to what you set and I think SetViewport determines what portion of that will be rendered. If you need to render into a 4096x4096 space, myself, I would create a separate render-target and move the position of the render-target(as a texture to draw to backbuffer with spritebatch) around based on the scroll values -- so it pans around inside the display window.

Share this post


Link to post
Share on other sites

I don't know if it's a bug, but If I understand correctly:

I believe the display resolution of the backbuffer must be a valid device screen resolution, so it would use the closest matching resolution to what you set and I think SetViewport determines what portion of that will be rendered. If you need to render into a 4096x4096 space, myself, I would create a separate render-target and move the position of the render-target(as a texture to draw to backbuffer with spritebatch) around based on the scroll values -- so it pans around inside the display window.

Thank you, I have come to the same solution. I'll try.

 

Can you say something about a partial rendering, if before the first draw was not called texture.ToFile? It seems that the creation of the texture is performed asynchronously and aborting when I try to render a texture.

 

Creating:

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");
        return new DxTexture(texture, descriptor);
    }
}

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!