   HLSL major weirdness [SOLVED]
Quick backgroundIn my engine, I automatically create two basic, hard-coded shaders. One is called "default" and it is used to render all the models that the user creates but for which he or she doesn't define a custom shader. The other one is the "null shader" and that one is used to render hit models and what it does is transform all vertices into projection space (like usual) but not render anything at all. These hit models are only used for collision/hit detection.So far, so good. It turns out that calling BeginPass ( ) on the default shader crashes my application with the following message:Illegal exceptionAn exception 0xC0000005 occurred in APP.EXE at EIP = 0x6C38F334 ; program pointer is +5, ACI version 3.20.1088, gtags (1,58)I have to point out that this engine is not a standalone system but rather a plug-in to another program so I'm not so sure as to how useful the above information is to anybody. It sure as heck doesn't tell me anything... :pThe null shader works fine. I have been able to pinpoint the cause for the crash to the exact line. It's in the shader code:// default shaderstd::stringstream s;s << "float4x4 matWorldViewProj;" << std::endl;s << "struct VS_Input { float4 Position : POSITION; float2 UV : TEXCOORD0; };" << std::endl;s << "struct VS_Output { float4 Position : POSITION; float2 UV : TEXCOORD0; };" << std::endl;s << "VS_Output MyVertexShader ( VS_Input In ) { VS_Output Out; Out.Position = mul ( In.Position, matWorldViewProj ); Out.UV = In.UV; return Out; }" << std::endl;s << "struct PS_Input { float2 UV : TEXCOORD0; };" << std::endl;s << "struct PS_Output { float4 Color : COLOR; };" << std::endl;s << "PS_Output MyPixelShader ( PS_Input In, sampler2D tex0 ) { PS_Output Out; Out.Color = tex2D ( tex0, In.UV ); return Out; }" << std::endl;s << "technique EntryPoint { pass SinglePass { VertexShader = compile vs_2_0 MyVertexShader ( ); PixelShader = compile ps_2_0 MyPixelShader ( ); } }";The questionWhenever I use the "tex2D ( )" expression in the pixel shader above, BeginPass crashes. When I replace that line in the shader with for example "Out.Color = float4 ( In.UV.x, In.UV.y, In.UV.x, 1.0f );", all works fine.It also doesn't matter in what way I use BeginPass (ie. whether or not I retrieve the number of passes and loop through them or just NULL the arguments and hard-code it to only run one pass, no difference).I'm really stumped. Any help is greatly appreciated.EDIT: For clarification, BeginPass ( ) does not FAIL, it really crashes right away. Also, calling effect->Begin ( ) before that does always work, never FAIL or crash...[Edited by - d h k on April 5, 2010 1:49:07 PM]
  Have you tried to use the reference device combined with the debug runtime? It is really difficult to crash a device non-deliberately this way, and you get warnings and errors to your debugger.   ;  Thanks for the answer. I'm already using the debug libraries. Unfortunately I can't run it using a debugger as it's a plug-in to an external program to which I don't have the source.Then again, how could one simple line of HLSL cause this behavior in the first place? Sounds pretty weird to me. :pAny more ideas/further information? This is very important for me, I hope I'll manage to solve this annoying issue that holds the development back so much.   ;  Well, the effect compiles just fine.Are you sure that the texture pointer that you use in the slot 0 is valid? That is, does the texture creation succeed? A NULL pointer is also a supported value, but a random pointer is not.   ;  And try to separate the sampler and texture variables to effect globals. There may be some bug related to the way that you pass the sampler as a pixel shader parameter.EDIT: Now that I mentioned this, I see that you don't pass a sampler at all to the pixel shader function compile. As the system tries to access a non-existing sampler, an access violation occurs.   ;  Interesting. When I render my models I follow this order:for each model:   effect->Begin ( );   effect->BeginPass ( );   // send values to shader   effect->CommitChanges ( ); for each subset in the model: device->SetTexture ( 0, texture ); // render subset   effect->EndPass ( );   effect->End ( );So, tex0 is not set when I call BeginPass in this order? Is this my mistake? Do I have to call BeginPass only AFTER I have send values (and textures) to the shader?I'm a little confused. After all, this way of doing things used to work absolutely fine before and in other projects, never failed me. :DThanks for your effort so far!   ;  Try to refactor the effect as follows:float4x4 matWorldViewProj;struct VS_Input { float4 Position : POSITION; float2 UV : TEXCOORD0; };struct VS_Output { float4 Position : POSITION; float2 UV : TEXCOORD0; };VS_Output MyVertexShader ( VS_Input In ) { VS_Output Out; Out.Position = mul ( In.Position, matWorldViewProj ); Out.UV = In.UV; return Out; }struct PS_Input { float2 UV : TEXCOORD0; };struct PS_Output { float4 Color : COLOR; };texture tex : register(t0); //global texture variable using texture slot 0sampler smp = sampler_state{    Texture = <tex>;    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;};PS_Output MyPixelShader ( PS_Input In) { PS_Output Out; Out.Color = tex2D ( smp, In.UV ); return Out; }technique EntryPoint { pass SinglePass { VertexShader = compile vs_2_0 MyVertexShader ( ); PixelShader = compile ps_2_0 MyPixelShader ( ); } };;  Wow, that did the trick. I'm incredibly thankful for your continued support. Rating reflects that. :D   ;  No problem, glad I could help :)The fault with the original code was that you never set the sampler that you referred in the pixel shader. It is possible to set the sampler as a parameter to the pixel shader function as you tried to do, but then you must mark it as "uniform" and be sure to set it from within the host program or when you call the compiler within the technique.   