Jump to content
  • Advertisement
Sign in to follow this  
Mellkor

Spritebatch With SharpDx

This topic is 1512 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

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);
            });
        }
    }
}


Edited by Mellkor

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

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?

Edited by Mellkor

Share this post


Link to post
Share on other sites

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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!