1
\$\begingroup\$

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: [email protected] (www.xbdev.net)

*/
/*****************************************************************************/

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;
MIPFILTER = NONE;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};


/***************************** 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();
}
}
\$\endgroup\$
2
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ Apr 3, 2012 at 23:15
  • \$\begingroup\$ 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 \$\endgroup\$
    – Saikai
    Apr 4, 2012 at 7:40

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Saikai
    Apr 5, 2012 at 21:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .