Not sure how to start..........

Oct 25, 2014 at 1:10 AM
I am now very experienced with DirectX, unfortunately. I lost my contractor to family issues and I am trying to pick up the pieces. First, I downloaded the demo and it compiles and runs fine. I have been using Eric Richards C# version of the code from Lund's book to try and learn as much as I can. In one of his demos he uses you SpriteTextRenderer. Hence why I am on this path. He created a FontCache class that is interesting. In any case he is using the 1.25 version. I was trying the 2.0 Beta and now the 1.25. I have removed everything of Eric's for the moment to try and isolate. With either library my application compiles fine. However, in the routine where the D3D11 device is created (he is also using a D3D9 device as we are running WPF) I tried creating a New SpriteRenderer() and I always get the same exception:
System.IndexOutOfRangeException was caught
  Message=Index was outside the bounds of the array.
       at SpriteTextRenderer.SpriteRenderer.RefreshViewport()
       at SpriteTextRenderer.SpriteRenderer..ctor(Device device, Int32 bufferSize)
       at AdVise.Camera.InitD3D11(HwndSource hwnd, Int32 width, Int32 height) in c:\Users\jones_d.RSINT\Documents\My Projects\AdVISE_SB5\AdVISECore\Camera.cs:line 134
       at AdVise.Camera.BeginOpen(Uri uri) in c:\Users\jones_d.RSINT\Documents\My Projects\AdVISE_SB5\AdVISECore\Camera.cs:line 305
The code to create the Sprite is as follows:
protected SpriteRenderer Sprite;
private void InitD3D11(HwndSource hwnd, int width, int height)
    // Defining our swap chain
    var desc = new SwapChainDescription();
    desc.BufferCount = 1;
    desc.Usage = Usage.RenderTargetOutput;
    desc.ModeDescription = new ModeDescription(width, height, new Rational(60, 1), Format.R8G8B8A8_UNorm);
    desc.SampleDescription = new SampleDescription(1, 0);
    desc.OutputHandle = hwnd.Handle;
    desc.IsWindowed = true;
    desc.SwapEffect = SwapEffect.Discard;
// Create the device.
    FeatureLevel[] levels = { FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0 };
    SlimDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, levels, desc, out d3dDevice11, out swapChain);

       Sprite = new SpriteRenderer(d3dDevice11);
The only difference I can see is my guy is including a Levels array. Taking that out and I still get the exception.

Any and all help greatly appreciated!


Dell T7610
Xeon 12-core
16Gb RAM
Nvidia K5000 Video Card
Windows 7 Pro
Visual Studio 2013 Pro
Oct 25, 2014 at 8:47 AM
Hi Doug,

thanks for your post. I wasn't aware of this tutorial you mentioned. Very nice, indeed.
Your device initialization seems to miss a viewport. When the SpriteRenderer is initialized, it queries for the first viewport, which it needs in order to convert absolute coordinates. In your case, this does not seem to exist, so you get an IndexOutOfRangeException. You can add a viewport like this:
device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, (int)size.X, (int)size.Y, 0.0f, 1.0f));            
Marked as answer by Nico201 on 10/27/2014 at 7:09 AM
Oct 27, 2014 at 3:04 PM
Thank you, Nico.
Should have see the obvious! We are actually using a D3DImage to display the image data in a WPF control. Does the SpriteRenderer also work with a D3DImage or is it expecting a window to write to?

Oct 27, 2014 at 3:09 PM
The STR needs a valid D3D Device. Whether this is associated with a window or a D3DImage does not matter. You might also want to consider using WPF for sprite and text rendering. It will give you much more flexibility.
Oct 27, 2014 at 3:50 PM
Hi Nico,
Thanks for the fast response! Since I am learning all this on the fly and under duress, I am not sure even how to use WPF for Sprite and Text rendering. I do know that I have to use a Direct9 D3DImage as, apparently, WPF does not function well with DirectX11. So The fellow before me does create a DirectX 9 device. So I have a D3d9Device and a D3D11Device and 2D Textures created using the D3D11Device. Please point me to someplace on the web where I might be able to use your suggested method. I would be most grateful. Otherwise now that I can create a Sprite with STR maybe I should continue this way?

Oct 27, 2014 at 3:55 PM
You can just add TextBlocks etc. to your WPF application as you have added the D3DImage. It all depends on the use case, but for HUDs or other interfaces, the WPF system is probably better suited.
Oct 27, 2014 at 4:00 PM
True. That is the proper way and pretty simple to do. However, I have a HD raw video stream coming in that I need to overlay text on top of the video before I feed it to an encoder and then written to a file. I am certain this requires the ability to make the text overlay part of the video stream. Do this I think requires I use something like STR.