There was an old XNA sample from Microsoft that discussed Sprite Effects. There is a displacement effect that I used in the past with XNA and it’s failing in MonoGame. I found a ported version of this effect as I lost my copy:
I ran this effect two days ago in Visual Studio 2010 and XNA 4.0 Refresh. This works. However the shader is failing in MonoGame.
1). If I am using DirectX, MonoGame says it needs Pixel Shader 4.0 or higher. Shader will not work.
2). When using OpenGL, Pixel Shader 2.0 is fine, yet the shader still fails.
Could someone take a look at this and see what needs changing? I have been struggling with this basic shader for 4 days now.
Do you mean it wont compile and run or it is broken ?
Edit nvm ok i guess it works i had two textures that were about the same in there.
It compiles and runs with some minor changes.
If you need to make it run with ps_2_0 then look to the link at the bottom you would have to make further additional changes probably also including a basic vertex shader as well as shown in the post example.
It would not work properly when I was able to get it to compile. I will give your changes a try. Quick question though, why did you add two textures in the shader file instead of using the auto-texture? Is that something that changed with new HLSL/PixelShader?
sampler TextureSampler : register(s0);
That should use the texture from the SpriteBatch.Draw call right? But you set it as a parameter instead. Any reason why? That might be what the issue was.
I used this shader 10 years ago roughly when XNA was around.
Well im not sure this code is a faithful reconstruction maybe but the guy was passing a texture into the move function parameter that expected a float. I added a offset as well because it was drawing in the top left corner.
why not sampler TextureSampler : register(s0);
Habit superstition i do it the way i posted normally with no problems i also like to set the texture in game1 by name feel like it keeps everything cleaner and straight though i didn’t name them very good here.
That probably doesn’t matter i think it was the position semantic in the function causing the problem.
In the clip shader link you can see how to use a vertex shader with spritebatch as well with the default structs that line up with the default BasicEffect that took a minute to figure out. In the same post also if you scroll back up is the first version which is for hidef profile.
Anyways might be easier to just search the monogame forum for “Displacement Shader” im sure there are a few on here that regular people wrote.
There are a few issues in this shader, but minor, this has helped highlight an issue I have found with MG in that when rendering with a shader to a sprite batch, if I don’t sample the spritebatch texture before doing any other texture samples, the samples are not loaded by the graphics pipeline correctly. It seems to ignore the registers in this case.
Note: for the first texture you should be good. Any textures after the first one you’d need to pass as a parameter (which I’d do during initialization if it doesn’t change). [Also reminder to match up the default VS params to the PS input ones]
sampler TextureSampler : register(s0);
sampler DisplacementSampler : register(s1)
Texture = (DisplaceTexture); // <--- use SetValue for DisplaceTexture (must pass as parameter for textures when more than one)
Basically just confirming what the others have said – I think you can usually get away with not passing the first texture unless you do not use sprite-batch in which case using device.Textures = tex; should work for first texture but I’ve experienced it messing up before too (older MG version maybe) so I pass as parameter to be safe.
Charles_Humphrey, you are the best. Your changes worked just fine for me too. I guess something I need to keep in mind between the differences of XNA and MonoGame is that I need to sample all textures passed to the shader. I wonder if the vsOutput makes a difference too.
vOutput, do you mean the structure passed from the vertex shader to the pixel shader?
I don’t think it will, you can pack that with what ever you like in the vertex shader (if you write your own). But here with a SpriteBatch Draw call, I think it will return the regular stuff as shown in my example. It may include normals in there too, but for what you need, these three should be fine. The GPU pipeline will match the type and channel to the structure we give as best it can (think this is called vertex patching) though thinking about it, I think this describes what’s done when the pipeline is trying to match the vertex stream we pass with the intended vertex structure we describe in our shader. This is digging a little deeper than we need to here though lol
I think it’s the fact that the texture is being sampled first, before any other in the shader, before the displacement texture was samples first, this just ensured that this texture got sampled first. I imagine if you sample the displacement texture before that it will break it again… it’s v odd, and as I say I think a bug in MG
The more you play with them, the better you will get. It’s worth investing the time in them, as ultimately, EVERYTHING you send to the screen will go through a shader, and if you can write your own, you will have total control
I have a few samples on my repo, you could re purpose the post processing shaders I have in there for 2D sprite rendering, as in that sample I am using them on SpriteBatch Draw calls too Though, in my engine, I use a screen space quad, this stops these sort of issues we have just spoken about here too.
Good luck, it can be frustrating playing with shaders, but at the same time, it can be very rewarding too