Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting to convert the XBDEV.NET Mosaic Shader for use in my XNA project and having trouble. The compiler errors out because of the half globals.

At first I tried replacing the globals and just writing the variables explicitly in the code, but that garbles the Output. Next I tried replacing all the half with float vars, but that still garbles the resulting Image.

I call the effect file from SpriteBatch.Begin().

Is there a way to convert this shader to the new pixel shader conventions? Are there any good tutorials for this topic?

Here is the shader file for reference:

File: tiles.fx
Details: Modified version of the NVIDIA Composer FX Demo Program 2004
Produces a tiled mosaic effect on the output.

Requires:  Vertex Shader 1.1
           Pixel Shader 2.0     

Modified by: (


float4 ClearColor : DIFFUSE = { 0.0f, 0.0f, 0.0f, 1.0f};
float ClearDepth = 1.0f;

/******************************** TWEAKABLES *********************************/
half NumTiles = 40.0;

half Threshhold = 0.15;

half3 EdgeColor = {0.7f, 0.7f, 0.7f};


texture SceneMap : RENDERCOLORTARGET < 
float2 ViewportRatio = { 1.0f, 1.0f };
int MIPLEVELS = 1;
string format = "X8R8G8B8";
string UIWidget = "None";

sampler SceneSampler = sampler_state {
texture = <SceneMap>;
AddressU  = CLAMP;        
AddressV  = CLAMP;

/***************************** DATA STRUCTS **********************************/

struct vertexInput {
half3 Position : POSITION;
half3 TexCoord : TEXCOORD0;

/* data passed from vertex shader to pixel shader */
struct vertexOutput {
half4 HPosition : POSITION;
half2 UV        : TEXCOORD0;

 /******************************* Vertex shader *******************************/

vertexOutput VS_Quad( vertexInput IN)
vertexOutput OUT = (vertexOutput)0;
OUT.HPosition = half4(IN.Position, 1);
OUT.UV = IN.TexCoord.xy; 
return OUT;

/********************************** pixel shader *****************************/

half4 tilesPS(vertexOutput IN) : COLOR {
half size = 1.0/NumTiles;
half2 Pbase = IN.UV - fmod(IN.UV,size.xx);
half2 PCenter = Pbase + (size/2.0).xx;

half2 st = (IN.UV - Pbase)/size;
half4 c1 = (half4)0;
half4 c2 = (half4)0;
half4 invOff = half4((1-EdgeColor),1);
if (st.x > st.y) { c1 = invOff; }
half threshholdB =  1.0 - Threshhold;
if (st.x > threshholdB) { c2 = c1; }
if (st.y > threshholdB) { c2 = c1; }
half4 cBottom = c2;
c1 = (half4)0;
c2 = (half4)0;
if (st.x > st.y) { c1 = invOff; }
if (st.x < Threshhold) { c2 = c1; }
if (st.y < Threshhold) { c2 = c1; }
half4 cTop = c2;

half4 tileColor = tex2D(SceneSampler,PCenter);
half4 result = tileColor + cTop - cBottom;
return result;


technique tiles
pass p0 
    VertexShader = compile vs_1_1 VS_Quad();
    ZEnable = false;
    ZWriteEnable = false;
    CullMode = None;
    PixelShader = compile ps_2_0 tilesPS();
share|improve this question
Maybe I'm missing something - why are you trying to convert it, and what are you trying to convert it into? It's a HLSL shader for shader model 2.0 which ought to work with XNA just fine. XNA doesn't even support shader model 4.0 anyway, and only supports 3.0 on hidef. – doppelgreener Apr 3 '12 at 23:15
i'm trying to use this shader with xna, but the shader gives the error : error X3650: 'NumTiles': global variables cannot use the 'half' type in ps_2_0. To treat this variable as a float, use the backwards compatibility flag. ID3DXEffectCompiler::CompileEffect: There was an error compiling expression – Saikai Apr 4 '12 at 7:40
up vote 2 down vote accepted

I believe you should be able to fix this simply by replacing all the half variables by float variables.
The only difference is that a half is a 16-bit float value while a true float is 32-bits.

share|improve this answer
I thought that too...and certainly this is right. The rendering errors are a result of another error. I will open another Question with the real error and mark this as answer, cause it is the correct solution to the error x3650. – Saikai Apr 5 '12 at 21:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.