Skip to main content
GameDev.net gamedev.net
🔒 Locked

Compilation Error in HSV Shader file

Started by khancoder Nov 21, 2009 at 5:30 AM 1 replies 1.6k views
Original Post
khancoder
khancoder
when i am compiling my .fx file using compile ps_2_0 i got the error Error: error X5608: Compiled shader code uses too many arithmetic instruction slots (66). Max. allowed by the target (ps_2_0) is 64. but when i am compiling using ps_2_a it run doesn't give any error, i can not use 2_a compiler so how can remove this error

float brightness;
float contrast;
float hue;
sampler TextureSampler  :register(s0);
// converts an RGB value to an HSV value
// taken from
// http://www.ati.com/developer/shaderx/ShaderX2_AdvancedImageProcessing.pdf
float4 RGB_to_HSV (float4 incolor)
{
    float delta;
    float colorMax, colorMin;
    float h,s,v;

    colorMax = max (incolor.r,incolor.g);
    colorMax = max (colorMax,incolor.b);
    colorMin = min (incolor.r,incolor.g);
    colorMin = min (colorMin,incolor.b);

    v = colorMax; // this is value

    if (colorMax != 0)
    {
        s = (colorMax - colorMin) / colorMax;
    }
    if (s != 0) // if not achromatic
    {
        delta = colorMax - colorMin;
	if (incolor.r == colorMax)
	{
             h = (incolor.g-incolor.b)/delta;
	}
	else if (incolor.g == colorMax)
        {
	    h = 2.0 + (incolor.b-incolor.r) / delta;
	}
	else // b is max
	{
	    h = 4.0 + (incolor.r-incolor.g)/delta;
	}
	h *= 60;
	if( h < 0)
	{
	    h +=360;
	}
	h = h/360.0; // moving h to be between 0 and 1.
    }
    float4 hsv = {h,s,v,1.0};
    return hsv;
}

//converts an HSV value to an RGB value
float4 HSV_to_RGB (float4 hsv)
{
	float4 outcolor = {0.0,0.0,0.0,1.0};
	float p,q,t;
	float h,s,v;
	float i;
	if (hsv[1] == 0) //Here i m getting this error
	{
            outcolor = hsv[2];
	}
	else
	{
		h = hsv.x * 360.0;
		s = hsv.y;
		v = hsv.z;
		
		if (h == 360.0)
		{
			h = 0;
		}
		
		h /= 60;
		i = floor (h);
		p = v * (1.0 - s);
		q = v * (1.0 - (s * (h-i)));
                t = v * (1.0 - (s * (1.0 -(h-i))));
		
		if (i == 0)
		{
			outcolor.r = v;
			outcolor.g = t;
			outcolor.b = p;
		}

		else if (i == 1)
		{
			outcolor.r = q;
			outcolor.g = v;
			outcolor.b = p;
		}
		else if (i == 2)
		{
			outcolor.r = p;
			outcolor.g = v;
			outcolor.b = t;
		}
		else if (i == 3)
		{
			outcolor.r = p;
			outcolor.g = q;
			outcolor.b = v;
		}
		else if (i == 4)
		{
			outcolor.r = t;
			outcolor.g = p;
			outcolor.b = v;
		}
		else if (i == 5)
		{
			outcolor.r = v;
			outcolor.g = p;
			outcolor.b = q;
		}
		else
		{
		        outcolor.r = v;
			outcolor.g = t;
			outcolor.b = p;
		}
	}
	return outcolor;
}

float4 TextureColor(in float2 texCoord	: TEXCOORD0) : COLOR
{
    float4 texCol1 = tex2D(TextureSampler, texCoord);
    float4 outColor = {0.0,0.0,0.0,1.0};
    float4 tmp={0.0,0.0,0.0,1.0};
    float4 hsv={hue,contrast,brightness,1.0};
    tmp=RGB_to_HSV(texCol1);
    tmp[0] = saturate (abs(tmp[0] + hue));
    tmp[1] = saturate (tmp[1] + contrast);
    tmp[2] = saturate (tmp[2] + brightness);
	
	//convert back
    outColor = HSV_to_RGB(tmp);
    // add the alpha back in as it gets stripped to save instructions in the
    // hsvToRgb functions
    outColor.a = texCol1.a;
    return outColor;
}

technique TransformTexture
{
    pass P0
    {
       CullMode = None;
        PixelShader  = compile ps_2_0 TextureColor();
    }
}



[Edited by - khancoder on November 21, 2009 5:51:11 AM]
MJP
MJP
The error is exactly what it says: you're using too many arithmetic instructions once your code gets compiled to ASM. ps_2_a has a higher limit for instructions, which is why it works. You'll have to optimize your code so that it produces less instructions.
khancoder
khancoder
Thnx MJP,
I am new to write to pixels shader, could you please give me some improvement pointer to get my code compile on ps_2_0. Many many thnx in advance.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.