   weird vertex shader problem 1_1
  I have a vertex shader that does diffuse lighting which works fine (actually it seems rather bright compared to the fixed function pipeline lighting mode).The last two lines are:output.diffuse = (AmbientMtrl * AmbientLightIntensity) + (s * (DiffuseLightIntensity * DiffuseMtrl));return output;However, when I change the line to:output.diffuse = (AmbientMtrl * AmbientLightIntensity);return output;The completely unrelated line:DiffuseConstTable->SetVector(Device, LightDirHandle, &dirToLight);causes the error:0xC0000005: Access violation reading location 0x00000000.I have a handle to a vector that inputs the direction to the light in the vertex shader.  This error occurs before I even use the vertex shader to draw, so the AmbientMtrl and AmbientLightIntensity aren't set yet.  I'm compiling from file.What kind of problem would cause working code in one place to break when I'm only changing the output value like this?   
  The HLSL compiler has an optimizer which is enabled by default. When the HLSL file is compiled, any redundant parts of your code, that have no effect on the actual output of the shader, are removed. This also applies to constants -- if one isn't used for anything that has any effect on output -- it gets removed. This optimization allows your shader to run faster.When you remove the part that adds the diffuse lighting to the shader's output, any code used to calculate the diffuse lighting becomes redundant, and gets optimized out. Once that gets optimized out, the "dirToLight" constant becomes redundant, and gets optimized out as well.This means that as far as DX is concerned, the "dirToLight" constant never existed. When you attempt to set its value, it can't find it.You have two options:1) Disable optimizations in the HLSL compiler (bad). You can set a flag to disable optimizations in the D3DXCreateEffect call, or if you're using fxc, you can use an argument to disable it.2) Check if a constant exists before settings its value (good). If the constant doesn't exists -- don't attempt to set its value.Hope this helps.   ;  Beautiful answer.Calling D3DXCompileShaderFromFile with the flag D3DXSHADER_SKIPOPTIMIZATION unfortunately did not work.  But, if I do actually use the values, it does indeed work.  Thanks.   