Preshaders?

Forgive me for being a bit of a mac noob.

On DirectX hardware, the nvidia and ati compilers do a nice job of moving redundant calculations into a preshader…

I haven’t seen anything in the docs here, so it’s probably a pipe dream, but is there a formal notion of a preshader that can be written in the CG program?

I always feel bad putting calculations into the vertex program that really are only used to compute constants that are used throughout the frame.

I suppose I should just move these sorts of calculations to the app and feed them into the constant registers instead, but it would still be nice to know if the notion of a preshader exists.

That thing is actually performed by Direct3D (or maybe D3DX, not sure… but certainly not hardware specific). When compiling shaders, it strips constant calculations from them (in effect changing the shaders), and performs them on some sort of “virtual machine” on the CPU when activating the effect. If my memory serves correctly, this only happens when using D3DX effects, not when using straight shaders.

Nope, Cg does not have that functionality.

Yeah, that’s the way to go. Compute something in the script and feed shaders with Material’s SetVector, SetMatrix calls or one of Shader’s SetGlobal* functions if the data needs to be globally shared between all shaders.