Updating PixelShaders from ps_1_0 to ps_4_0 and a Warning

As I’m going through and updating the code I ran into a following snag that took me a bit to figure out. In this case, I only had a Pixel Shader and no Vertex Shader. The shader worked just fine back in the day, but during the upgrade I was told that MonoGame only supported ps_4_0 and this shader was version 1.0 (ps_1_0).  Simply changing the version number in the shader file fixed that.

But then I had a bug where the shader wasn’t giving me the results I wanted.

After poking around a little bit, I finally found out that the method signature needs to explicitly have all of the parameters.  I found this information here:

http://www.software7.com/blog/pitfalls-when-developing-hlsl-shader/

 

The way my pixel shader looked original was:

 

sampler TextureSampler;

struct PixelInput
{
    float2 TexCoord : TEXCOORD0;   
};

float4 pixelShader(PixelInput input) : COLOR
{
    float4 color = tex2D( TextureSampler, input.TexCoord);
    return( color );
}

technique Default
{
    pass P0
    {
        PixelShader = compile ps_1_1 pixelShader();
    }
}

 

And I had to change it to:

sampler TextureSampler;

struct PixelInput
{
    float4 Pos : SV_POSITION;
    float4 Color : COLOR0;
    float2 TexCoord : TEXCOORD0;
};

float4 pixelShader(PixelInput input) : SV_TARGET0
{
    float4 color = tex2D( TextureSampler, input.TexCoord);
    return( color );
}

technique Default
{
    pass P0
    {
        PixelShader = compile ps_4_0 pixelShader();
    }
}

 

Once I made that small tweak (added in the different parameters (albeit through a struct)) it worked like a charm. A struct doesn’t have to be used, but the parameters should be in the right order it seems – position, then color, then texture coordinates.

 

Note, that this pixel shader doesn’t do anything but return the pixel of the texture passed to it.  Typically you would do some math on the color before returning it. Check out Chapter 15, under SimpleGame for some different pixel shader examples when I update the code to GitHub. (Before the end of the month…)

 

So make sure that your pixel shader parameters are all present and associated with the right semantics!

Upgrading My Old XNA Game Studio 3.0 Unleashed Code to run under MonoGame 3.5.1 (using XNA GS 4 APIs)

I’ve started working on upgrading my old XNA Game Studio 3.0 Unleashed book’s code so it will run on the Universal Windows Platform (UWP). I haven’t touched this code since it was created back in 2008/2009, but with the HoloLens coming, I thought it would be good to dust off the code and see what it would take to upgrade.

The reason for doing this is because of a .NET Rocks! podcast.  Richard and Carl interviewed Tom Spilman.  I heard Tom speak at a .NET usergroup in Seattle when I was in town for a conference a few years back. I’ve been impressed with the amount of work he has put into the MonoGame open source project to make it work on Windows.

Still, when I’ve tried to get into MonoGame for Windows 8 I had issue. It wasn’t ready yet, and I didn’t have time to try and fix it, so I went on to use Unity. I don’t regret that decision as Unity is a fantastic tool.

Still, I wanted to go back and look at my XNA code and see if I could get this to run in Windows 10.

Based off my initial results, I’m expecting I’ll have the updated code on GitHub by end of the month…

If you are interested in the original XNA GS 3.0 code, it is hosted on codeplex at:

https://xnags30unleashed.codeplex.com/

 

I’ll create another post when I get it put onto GitHub. I’ll be starting a fresh repo as I don’t believe there would be any benefit of starting with the 3.0 code.

 

Before I started, I had to install MonoGame. You can get the latest from here:

http://www.monogame.net/downloads/

I downloaded MonoGame 3.5.1 for Visual Studio

Here is the steps I am taking to upgrade my old XNA 3.1 code to use the XNA 4.0 API inside of MonoGame 3.5.1

  • Installed MonoGame
  • Create New MonoGame app – picking Windows Universal. I do want this to run on HoloLens after all. 😀
  • Restored NuGet Packages
  • Copy over my content to the Content folder. (including the .contentproj file) (through explorer, if through VS, I don’t include them in the project)
  • Double click on the MonoGame content.mgcb content pipeline file.
  • This brings up the pipeline tool. I then import the .contentproj file.
  • I build and fix any issues. Some .fx files needed to be updated in my case as I was using vs_1_0 and ps_1_0 and MonoGame requires it to be 4_0 and above for both the vertex and pixel shaders.
  • If my Game class (not filename) was called something other than Game1, I opened the Game1 that loaded in the project and renamed it to the name of the game I was converting. I made sure to let Visual Studio do the renaming, so it renamed all the backend code correctly.
  • I then copied all my .cs files from my original project into Visual Studio.
  • If the Game file name was different than Game1.cs, then I deleted Game1.cs – otherwise, I just overwrote it.
  • I then went through the process of updating the XNA 3.1 API to XNA 4.0 API.

I remember there being a cheatsheet for this. Unfortunately, that page no longer works, but thanks to the Way Back Machine, we can still get to it:

http://web.archive.org/web/20130721142204/http://nelxon.com/resources/xna-3-1-to-xna-4-0-cheatsheet.php

 

Also, as usual, Shawn Hargreave’s blog was very helpful in overcoming any API difficulties:

https://blogs.msdn.microsoft.com/shawnhar/2010/04/02/state-objects-in-xna-game-studio-4-0/

 

In fact, here is a video of me upgrading one of the demos here:

Again, I’ll create a post when I actually get done and upload the code to GitHub. Can’t wait for my HoloLens to get here.

 

If you haven’t grabbed the HoloLens tools, make sure you get them:

https://developer.microsoft.com/en-us/windows/holographic/install_the_tools

XNA Game Studio 3.0 Unleashed Source Code Released as Open Source

I’m pleased to announce that the XNA Game Studio 3.0 Unleashed book’s source code has been released under the Ms-PL license on CodePlex.

So you can use Git and create a clone locally to start working with it or you can fork it and update it for XNA 4.0.

Have fun with it!

-Chad

From Radians or Degrees to Pi or Tau to a Binary Hand Dance

So last week I wrote a blog post on my XNAEssentials website demonstrating how to create a selection wheel / pie menu in XNA Game Studio 4.  Before I even announced it on twitter, Michael Neel (@ViNull) sent me this tweet.  Then in his @GameMarx podcast on Friday (which I watched on Saturday) he had guest speaker Andy Dunn (@The_ZMan) who he asked if he coded in Degrees or Radians.  It appears both of them code in radians.

Andy then talked about @ViHartViHart of http://vihart.com/ who has math videos including some videos stating that Pi is wrong.  There is this whole movement that I had no idea about.  There is even Tau day (which has good info). So this morning I spent some time looking at a couple of her videos.  They are awesome.  Perhaps I would have done better in Trigonometry if we had used Tau instead of Pi…

If you are interested here are some links:

http://vihart.com/blog/pi-is-still-wrong/

http://vihart.com/blog/pi-politics/

I must say, I’d like to use Tau instead of Pi.

Towards the end of that second video she talks about folks copywriting works based off of Pi and jokingly creates a Binary dance with her fingers.  Her next blog post expands on that and I thought it was great.

http://vihart.com/blog/binary-hand-dance/

So I just tried to add a little humor to my tutorial post about using degrees or radians when calculating angles and as a result I now know about someone I may be able to learn math from in a fun way.

So do a binary hand dance, or write some code, or learn something new …

For me, I’ve been learning Blender 3D.  I’m not an artist by any stretch but I’m able to at least put place holders that resemble the item I’m trying represent in my upcoming 3D game. I’ve learned more in the last 2 weeks about Blender than I have in the last 12 years in other 3D modeling packages. I’m amazed by how quickly some of these folks create things in their tutorials.  There are too many things to learn and no where near enough time. If you are also interested in some good tutorials on Blender 3D, check them out at http://blendercookie.com/

Oh and in case you were wondering … this is what Tau sounds like: