MultiSampling doesn't work on RenderTarget2D (DirectX)

I was doing some interop between WPF and MonoGame and it seemed that the most efficient way it a shared RenderTarget, after I implemented that I noticed that the rendered model’s edges were aliased, so I went ahead and set the preferred multisample count to 2 but I got an Exception from SharpDX saying Invalid Parameter (but didn’t specify which parameter, how can I figure it out?).

The Exception was in RenderTarget2d.DirectX.cs in the GenerateIfRequired method when trying to call the constructor for DepthStencilView, I did some basic debugging and found that the MultiSample Quality gets set to -1, I don’t know what the Quality value should mean but -1 doesn’t sound like a correct value to me. (I also tried setting it to 0 and one but it didn’t work)

Is this a known bug or missing feature? Should I open an issue for it on GitHub? Any workarounds besides using Super Sampling?

It just hasn’t been implemented yet.

There was an older PR that added some support for it…

implemented multisampling on directx rendertarget by ossaltu · Pull Request #2185 · MonoGame/MonoGame · GitHub

… but it was incomplete and needs reworking.

The main reason I haven’t implemented it yet is that in our project we use FXAA as a post effect because we have a deferred renderer.

As far as I see, the problem with the mentioned pull requests is that it doesn’t follow the new partial class system right? If that’s the case then I might look into it and re-implement.

and about FXAA, I’ve seen that some methods didn’t work with XNA and I’ve seen that MonoGame has it’s own effect system (afaik you can’t take fx files used by XNA and compile them using MGCB), so can you link me to one that’s ready to work with MonoGame on Windows DirectX?

Thanks again for your help and your fast replies, I really appreciate it :smiley:

That isn’t completely accurate. We can process FX files in MGCB, but they often need small changes to support the DirectX 11 HLSL compiler.

Other than ours… I know of no others. Ours has a bunch of unrelated stuff in it which is tricky to strip out.

1 Like

So I finally decided to start implementing FXAA, I searched for some time and found ended up with this nice sample.
I ran the sample on XNA4 and everything seemed fine, then I started porting it to MonoGame…
I faced a few problems, first was that tex2Dlod wasn’t supported on ps_4_0_level_9_1 for some reason, so I tried replacing that with ps_4_0 and I also tried replacing tex2Dlod with tex2Dbias (it might not do the same thing, but I just wanted to build successfully), both solutions made the content compile successfully but I got a runtime error…
The error is in GraphicsDevice.DirectX.cs in the GetInputLayout method, specifically this line:

layout = new SharpDX.Direct3D11.InputLayout(_d3dDevice, shader.Bytecode, decl.GetInputLayout());

It gives me an error saying “The parameter is incorrect”, so I looked at the parameters and compared them to the other times when this succeeds (when running some other shader code), everything looked fine except that the shader Bytecode was surprisingly small, it was just 1048 bytes! I have a much smaller shader and it’s 3500 bytes!
So it seems that the problem happens when loading the shader, maybe it allocates a buffer with the wrong size before reading into it, can this be because I’m using an fxh file?

Edit 1: I made an INSANE attempt by replacing “#include “Fxaa3_11.fxh”” with the contents of “Fxaa3_11.fxh” :smiley: … but I got the exact same error with the exact same short Bytecode length :confused:

Edit 2: The size of the Bytecode was small because the problem is with the vertex shader and not the pixel shader (also I was on the “Standard” and not “FXAA” Technique), I just discovered I’ve been looking in the wrong place (after tracing the whole effect content reading operation! :open_mouth:), maybe the wrong parameter is the declaration part?

Edit 3: SV_Position0 instead of Position0 … ouch … … … worst gotcha ever! :expressionless:

1 Like

Yea… that means the VB VertexDeclaration format does not match the inputs to your vertex shader.

I really need to spend some time and output a better error message in this case. It seems to trip up all users new to DX and/or DX11.

Yep… a DX thing. The hint is in the effect writing docs, but people miss it all the time.

Yeah I did read that part, and I made that change in a previous shader code, but I forgot to do it this time, and yes, the error message doesn’t make it easy to figure out what went wrong :expressionless:

Anyways, I successfully ported the sample to MonoGame after fixing a few other weird problems, I had to remove the half-pixel offset from the projection matrix to make it work, it seems that MonoGame handle’s SpriteBatched half-pixel issue differently.
The other weird problem is that the Vertex Shader output and Pixel Shader input didn’t match, I don’t know why/how that worked in XNA but it did, I had to change it though to make it work in MonoGame (might be another DX9 vs DX11 thing).

It is exactly that. :slight_smile:

Hi @hassanselim0, would you mind to share your shader code? I am currently working on a WPF application using MonoGame and bumped into the same problem. Would safe me a lot of time, if I could work with a MonoGame compatible shader. Thanks in advance!

Okay, got it working. I will send a link during the next few days.

Here you go:

Hi, for anyone trying to use this, it doesn’t work anymore with the latest developpement build.

What I had to do to get it working was to remove the sampler2D from the arguments of the FxaaPixelShader_PC and FxaaPixelShader_Console functions (sampler2D are global to the shader now)

Thanks for sharing this, I got fxaa to work with my game using it.

(Still, msaa on render target should probably be fixed, as it’s the easier to use option, unfortunately I don’t know enough DirectX to do it)