Thread: HLSL, Error with Color

  1. #1
    Registered User
    Join Date
    Apr 2007

    HLSL, Error with Color

    I've been working my way through a coding book, and I've been taking their shaders and messing with them to create different ones. At this point however I'm wholly stuck on a pair of errors. Further, I can't find any documentation on the errors to help me out!

    At any rate, the code below has been simplified to only output one color because even that's failing. While it gets past the Vertex Shader, it pretty much has to be a VS to PS issue. As you can see, I've commented out everything in the pixel shader at this point aside from the one 1,1,1,1 color that it outputs. This should work under the original, but in this one it's upset. Is this being caused by a hangover of some piece of data from outVS?

    // ProcTexSheer.fx by Erich Ulmer, based on Frank Luna's Terrain.fx
    // Blends three textures together using a blending function to imitate
    // procedural texturing
    uniform extern float4x4 gWorldInverseTranspose;
    uniform extern float4x4 gViewProj;
    uniform extern float3  gDirToSunW;
    uniform extern texture gTex0;
    uniform extern texture gTex1;
    uniform extern texture gTex2;
    uniform extern texture gBlendMap;
    static float gTexScale = 16.0f;
    sampler Tex0S = sampler_state
    	Texture = <gTex0>;
    	MinFilter = LINEAR;
    	MagFilter = LINEAR;
    	MipFilter = POINT;
    	AddressU  = WRAP;
        AddressV  = WRAP;
    sampler Tex1S = sampler_state
    	Texture = <gTex1>;
    	MinFilter = LINEAR;
    	MagFilter = LINEAR;
    	MipFilter = POINT;
    	AddressU  = WRAP;
        AddressV  = WRAP;
    sampler Tex2S = sampler_state
    	Texture = <gTex2>;
    	MinFilter = LINEAR;
    	MagFilter = LINEAR;
    	MipFilter = POINT;
    	AddressU  = WRAP;
        AddressV  = WRAP;
    sampler BlendMapS = sampler_state
    	Texture = <gBlendMap>;
    	MinFilter = LINEAR;
    	MagFilter = LINEAR;
    	MipFilter = POINT;
    	AddressU  = WRAP;
        AddressV  = WRAP;
    struct OutputVS
        float4 posH         : POSITION0;
        float2 tiledTexC    : TEXCOORD0;
        float  shade        : TEXCOORD1;
        float3 B		: COLOR0;
    OutputVS TerrainVS(float3 posW : POSITION0,  // We assume terrain geometry is 
                       float3 normalW : NORMAL0, // directly in world space.
                       float2 tex0: TEXCOORD0)
        // Zero out our output.
    	OutputVS outVS = (OutputVS)0;
    	// Transform normal to world space.
    	float3 normalS = mul(float4(normalW, 0.0f), gWorldInverseTranspose).xyz;
    	normalS = normalize(normalS);
    	// Compute the texture (based on angle from XZ plane)        
    	float theta = abs(atan(normalW.y / sqrt(normalW.x * normalW.x + 
                          normalW.z * normalW.z))); //compute the angle
    	// Just compute a grayscale diffuse and ambient lighting 
    	// term--terrain has no specular reflectance. 
            outVS.shade = saturate(max(0.0f, dot(normalW, gDirToSunW)) + 0.3f);
    	// Transform to homogeneous clip space.
    	outVS.posH = mul(float4(posW, 1.0f), gViewProj);
    	// Pass on texture coordinates to be interpolated in rasterization.
    	outVS.tiledTexC    = tex0 * gTexScale; // Scale tex-coord to tile.
    	//Compute a blend pattern based on the normal
    	theta = 2*theta/3.142;	
    	outVS.B.r = 1.0f;
    	outVS.B.g = 1.0f;
    	outVS.B.b = 1.0f;
    	// Done--return the output.
        return outVS;
    float4 TerrainPS(float2 tiledTexC 	  : TEXCOORD0, 
    		 float  shade 		  : TEXCOORD1,
    		 float3  B		  : COLOR0) : COLOR1
        // Layer maps are tiled
        float3 c0 = tex2D(Tex0S, tiledTexC).rgb;
        float3 c1 = tex2D(Tex1S, tiledTexC).rgb;
        float3 c2 = tex2D(Tex2S, tiledTexC).rgb;
        // Find the inverse of all the blend weights so that we can
        // scale the total color to the range [0, 1].
        float totalInverse = 1.0f / (B.r + B.g + B.b);
        // Scale the colors by each layer by its corresponding weight
        // stored in the blendmap.  
        c0 *= B.r * totalInverse;
        c1 *= B.g * totalInverse;
        c2 *= B.b * totalInverse;
        // Sum the colors and modulate with the shade to brighten/darken.
        float3 final = (c0 + c1 + c2) * shade;    
        return float4(1.0f, 1.0f, 1.0f, 1.0f);
    technique TerrainTech
        pass P0
            vertexShader = compile vs_2_0 TerrainVS();
            pixelShader  = compile ps_2_0 TerrainPS(); //error occurs after equal sign. I.E. The output is bad
    Again, notice that I've attempted to fix everything by setting everything to 1, just to make a usable color, and it's still not allowing it.

    The error box I'm getting is:
    error x4530: pixel shader must minimally write all four components of COLOR0
    error x4538: COLOR outputs must be contiguous from COLOR0 to COLORn
    ProcTexSheer.fx (132,24): ID3DXEffectCompiler::CompileEffect: There was an error compiling expression
    ID3DXEffectCompiler: Compilation Failed
    I've tried making B a float4, normalizing all of B to '1.0f's and giving it "real" information. I'm at the end of my rope as to why it won't even do the most simple of requests at this point (setting all colors to 1,1,1,1)
    Last edited by blurrymadness; 08-02-2010 at 03:04 PM. Reason: added a comment indicating where (132,24) is

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    The return semantic on your pixel shader looks a bit odd. I'm used to just seeing COLOR there and not COLOR1. Is this for D3D10 or D3D9? Luna has 3 books right now - D3D9 fixed function, D3D9 shaders, and D3D10.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Yes it should either be COLOR or COLOR0. COLOR1 would output to a second rendertarget in slot 1, but you cannot output there without also assigning a COLOR0 which goes to slot 0, which is the backbuffer by default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Towr of Hanoi move the disc
    By WatchTower in forum C Programming
    Replies: 9
    Last Post: 07-17-2009, 03:48 AM
  2. Critique my lighting model.
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 08-12-2006, 06:23 PM
  3. egavga.bgi problem
    By sunil21 in forum A Brief History of
    Replies: 3
    Last Post: 09-22-2003, 05:06 PM
  4. My opinion on skin color
    By Leeman_s in forum A Brief History of
    Replies: 8
    Last Post: 07-11-2003, 12:12 PM
  5. Just one Question?
    By Irish-Slasher in forum C++ Programming
    Replies: 6
    Last Post: 02-12-2002, 10:19 AM