I was trying XNA 3D tutorial at www.thehazymind.com when i stumble some problems at tutorial 3. i did the texturequad tutorial and when i tried to run it, i got an error :

Both a valid vertex shader and pixel shader (or valid effect) must be set on the device before draw operations may be performed.

Any suggestion on where to go from here I'm trying to understand the basic concept of XNA's 3D engine. thanks before



Re: XNA Game Studio Express Shader problem

Kyle_W

This is the basic procedure for applying an effect from the XNA docs:

http://msdn2.microsoft.com/en-us/library/bb203872.aspx





Re: XNA Game Studio Express Shader problem

Nic-Gun

thanks.. i tried using the fx file supplied there and my program worked, which means my fx file is the main problem.

the fx file is like this :

--------------------------------------------------------------------

float4x4 WorldViewProject;
sampler TextureSampler;

struct VS_INPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
};

struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 Texcoord : TEXCOORD0;
};

VS_OUTPUT Transform(VS_INPUT Input)
{
VS_OUTPUT Output;

Output.Position = mul(Input.Position, WorldViewProject);
Output.Texcoord = Input.Texcoord;

return Output;
}

struct PS_INPUT
{
float2 Texcoord : TEXCOORD0;
};

float4 Texture(PS_INPUT Input) : COLOR0
{
return tex2D(TextureSampler, Input.Texcoord);
};

technique TransformTexture
{
pass P0
{
VertexShader = compile vs_3_0 Transform();
PixelShader = compile ps_3_0 Texture();
}
}

--------------------------------------------------------------------

anyone knows what's the problem there thanks in advance





Re: XNA Game Studio Express Shader problem

Nic-Gun

i think i found the problem.. it's not in the fx file but it's in the code.. i didn't supply the texture file to the fx file..

that's what i thought just now..

but then when i supply the texture2d for the fx file, i got an error : The method call is invalid.

after that i used the fx file i found at msdn and it worked..

any suggestions guys thanks in advance..





Re: XNA Game Studio Express Shader problem

Kyle_W

Well the main difference I noticed between the MSDN sample shader and yours is that the sample is set to compile as vs_2_0 and ps_1_1 while your shader is set for vs_3_0 and ps_3_0. Perhaps your video card does not support Shader Model 3.0.

The only other difference I noticed is that in your pixel shader definition you are using the COLOR0 semantic where as in the MSDN sample they use the COLOR semantic. I'm not sure if this matters.





Re: XNA Game Studio Express Shader problem

NickMcCrea

The COLOR/COLOR0 semantic difference shouldn't matter, it's just the number of register your variable is in.

As Kyle says, it could be due to your card not supporting shader model 3. Try compiling as vs_2_0 and ps_1_1. It's the only thing that stands out to me.





Re: XNA Game Studio Express Shader problem

Nic-Gun

well, i tried changing the vs_3_0 to vs_2_0 and ps_3_0 to ps_1_1 and still it's the same :

The method call is invalid.

after that i tried changing the one from msdn into vs_3_0 and ps_3_0 and i got this error back :

Both a valid vertex shader and pixel shader (or valid effect) must be set on the device before draw operations may be performed.

finally i changed the one from msdn back to the original version and it worked..

strange..





Re: XNA Game Studio Express Shader problem

Kyle_W

Well the "method call is invalid" error seems to be related to your texture while it seems that the other error may in fact be related to the vertex and pixel shader compilation versions. I recommend that you leave it set as vs_2_0 and ps_1_1 in your code for now. Obviously the texture is not an issue in the MSDN sample since it doesn't use a texture.

Did you change the COLOR0 semantic to COLOR in your pixel shader as I suggested

I also noticed that the WorldViewProj variable in the MSDN sample has a WORLDVIEWPROJECTION semantic on it. I recommend that you add this in your effect.

Lastly, you indicated that the "method call is invalid" error occurs on the line that attempts to assign your texture to the effect right If not, please post the full code line where the error occurs as well as the line where you load the texture and the line where you assign the texture to the effect.





Re: XNA Game Studio Express Shader problem

Nic-Gun

well the fx file i'm using right now is from another page of msdn.. it's like this :

-------------------------------------------------------------------------

uniform extern float4x4 WorldViewProj : WORLDVIEWPROJECTION;
uniform extern texture UserTexture;

struct VS_OUTPUT
{
    float4 position : POSITION;
    float4 textureCoordinate : TEXCOORD0;
};

sampler textureSampler = sampler_state
{
    Texture = <UserTexture>;
    mipfilter = LINEAR;
};

VS_OUTPUT Transform(
    float4 Position : POSITION,
    float4 TextureCoordinate : TEXCOORD0 )
{
    VS_OUTPUT Out = (VS_OUTPUT)0;

    Out.position = mul(Position, WorldViewProj);
    Out.textureCoordinate = TextureCoordinate;

     return Out;
}

float4 ApplyTexture(VS_OUTPUT vsout) : COLOR
{
    return tex2D(textureSampler, vsout.textureCoordinate).rgba;
}

technique TransformAndTexture
{
    pass P0
    {
        vertexShader = compile vs_2_0 Transform();
        pixelShader = compile ps_2_0 ApplyTexture();
    }
}

-------------------------------------------------------------------------

and this one is using texture too and i'm using the same texture that i used for the first fx file. i tried changing the COLOR0 to COLOR and added WORLDVIEWPROJECTION to the first fx file but still i got method call is invalid.

btw my code would be something like this :

-------------------------------------------------------------------------

private string myAsset;
private Effect myEffect;

public Effect Effect { get { return myEffect; } }

private string myTexture;

public EShader(string asset, string texture)
{
    myAsset = asset;
    myTexture = texture;
}

public void LoadGraphicsContent(ContentManager myLoader)
{
    Texture2D texture = myLoader.Load<Texture2D>(myTexture);

    myEffect = myLoader.Load<Effect>(myAsset);

    myEffect.Parameters["WorldViewProj"].SetValue(Matrix.Identity);
    myEffect.Parameters["UserTexture"].SetValue(texture); //this is where the error occured
    myEffect.CurrentTechnique = myEffect.Techniques["TransformAndTexture"];
}

-------------------------------------------------------------------------





Re: XNA Game Studio Express Shader problem

Kyle_W

Well I assume that the LoadGraphicsContent method you are showing here is your own custom method because the LoadGraphicsContent method on the Game class only has a bool loadAllContent parameter. I also assume that you are saying that the effect you just posted is the one that fails while the MSDN sample that I posted a link for earlier is the one that works.

You are not showing what value is being assigned to the myTexture string variable. The value of this variable needs to match the asset name of the texture as it appears in the properties of the texture in Solution Explorer.





Re: XNA Game Studio Express Shader problem

Nic-Gun

the LoadGraphicsContent code is from Michael Schuld's blog and the shader file too is from there. the tutorial page i'm talking about is this :

http://www.thehazymind.com/archives/2006/10/more_rendering.htm

well i was calling

EShader shader = new EShader(@"Content/Shaders/Texture", @"Content/Textures/logo");

which of course get the texture.fx from the shaders folder and a logo.jpg from the textures folder. the asset name for the logo file is of course logo.

what i noticed is that in the first fx file it used

sampler TextureSampler;

while in the other fx file it used

uniform extern texture UserTexture;

any idea what the difference is





Re: XNA Game Studio Express Shader problem

NickMcCrea

A sampler is used to return a sample of a texture at a particular texture coordinate, and can also perform certain types of filtering. So you might have an effect file that specifies a texture, and then a sampler that uses that texture.

texture tex0;

sampler3D s_3D = sampler_state {

texture = (tex0);

mipfilter = LINEAR;

addressu = wrap;

};

Then, you could have a function somewhere in your effect file that uses the sampler to pick out values from that texture, using the settings described in the sampler. In other words, return the values of sampler s_3D at coordinate tex. So samplers are used to 'sample' textures, based on settings described in the sample state.

float3 sample_3D(float3 tex : TEXCOORD0) : COLOR

{

return tex3D(s_3D, tex);

}