Spritebatch With SharpDx

Started by
5 comments, last by unbird 9 years, 5 months ago

Hi,

I am trying a little experiment to render a simple image using the Spritebatch with SharpDX
I get the BlueCornflower clear But no Image.. Debug says that the file has been successfully loaded so i'm guessing its to do with the Device setup which i am not very familiar with.

Thanks


using System;
using SharpDX;
using SharpDX.Direct3D;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using SharpDX.Windows;
using Buffer = SharpDX.Direct3D11.Buffer;
using Device = SharpDX.Direct3D11.Device;
using Texture2D = SharpDX.Direct3D11.Texture2D; 
using SharpDX.Toolkit.Graphics;

namespace TestDriver
{
    static class Program
    {
        [STAThread]
        static void Main()
        {   
           RenderForm form = new RenderForm("Demo");

             var desc = new SwapChainDescription()
                           {
                               BufferCount = 1,
                               ModeDescription= 
                                   new ModeDescription(512, 512,
                                                       new Rational(60, 1), Format.R8G8B8A8_UNorm),
                               IsWindowed = true,
                               OutputHandle = form.Handle,
                               SampleDescription = new SampleDescription(1, 0),
                               SwapEffect = SwapEffect.Discard,
                               Usage = Usage.RenderTargetOutput
                           };

            // Create Device and SwapChain
            Device device;
            SwapChain swapChain;

            
          Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain);
          var context = device.ImmediateContext;

          var backBuffer = SharpDX.Direct3D11.Texture2D.FromSwapChain<SharpDX.Direct3D11.Texture2D>(swapChain, 0);
          var renderView = new RenderTargetView(device, backBuffer);


            GraphicsDevice graphics = GraphicsDevice.New(device);
            SpriteBatch spriteBatch = new SpriteBatch(graphics);


            string path = "image.dds";
            var image = Texture.Load(graphics, path,TextureFlags.ShaderResource, ResourceUsage.Immutable);
            var rectangle = new Rectangle(100, 100, 980, 272);
           
            RenderLoop.Run(form, () =>
            {
                context.ClearRenderTargetView(renderView, Color.CornflowerBlue);

                spriteBatch.Begin();
                spriteBatch.Draw(image,rectangle,Color.White);
                spriteBatch.End();

                swapChain.Present(0, PresentFlags.None);
            });
        }
    }
}


Advertisement

It looks like you're not specifying a Viewport to the device context. Aside from needing to define a viewport, SpriteBatch will internally use that to create a projection matrix as input for the sprite shader.



It looks like you're not specifying a Viewport to the device context. Aside from needing to define a viewport, SpriteBatch will internally use that to create a projection matrix as input for the sprite shader.

Sorry for the late reply,

Still can't seem to get it to work, I have tried the following line of code:


context.Rasterizer.SetViewport(0.0f, 0.0f, 512.0f, 512.0f);

But that seems to make no difference.

I mainly only use xna for my 2d games and only dabble in Dx and Opengl.. so any help or links to tutorials to get this working would be a great help.

Thanks.

You forgot to bind a render target:


    context.OutputMerger.SetRenderTargets(renderView);

You forgot to bind a render target:


    context.OutputMerger.SetRenderTargets(renderView);

Thanks, But still no dice sad.png

Also, When i try:

graphics.Clear(new Color4(1, 0, 0, 0));
instead of using the context, it says i'm missing a "presenter"

Could it be when i initialized the sprite batch and passed the device without a presenter its causing the problem?


I mainly only use xna for my 2d games

If you're looking for an analog to XNA, have you considered MonoGame? It's API-compatible with XNA, open source and cross-platform as well. http://monogame.net

Could it be when i initialized the sprite batch and passed the device without a presenter its causing the problem?


I doubt it. With the suggested corrections (viewport, render target), I do get a result from your code - though I used a different image, maybe that's the problem. How does your current complete code look ?

Low-level graphics programming is hard when you start out. When nothing gets rendered about a dozen things could have gone wrong. If you consider all the details it's probably around a hundred things.

First, you should consider these debugging hints. E.g. the DirectX debug layer would have reported your missing viewport.

Then also get familiar with PIX or the graphics debugger. It's also educational to run working samples (or even games) through PIX.

For beginners I recommend starting from working examples and change one thing at a time. Of course, for C#/SharpDX there aren't as many as for C++ - even less for the toolkit. Luckily someone took the time to transliterate the rastertek tutorials : SharpDX for Rastertek tutorials.

This topic is closed to new replies.

Advertisement