Sign in to follow this  
CDProp

Some random questions related to HLSL shaders and the ID3DXBaseEffect interface

Recommended Posts

Hello, thanks in advance to whoever is able to help me with these. My first question is more of a humble observation, that I might be wrong about. Maybe someone can tell me if I'm missing something. It seems that there are a lot of things when it comes to shaders that aren't really conducive to writing readable code. Just to give other examples (I could name more if need be), if I have a uniform extern float3 in my fx file, and I want to set the value of it, I can't use the SetVector() method, because that expects a D3DXVECTOR4, not a D3DXVECTOR3. Possible solutions to the problem:
  • I could change my uniform extern to a float4 instead, but then I have to litter my shader code with .xyz's to ensure that the w component is ignored, which is sort of ugly.
  • I could cast my D3DXVECTOR3 reference to a D3DXVECTOR4 reference, but that seems dangerous.
  • Alternately, I could just create a float array with 3 elements, assign the x, y, and z values to [0], [1], and [2], respectively, and then use the SetFloatArray() method. But that seems messy too.
  • Maybe I could use SetFloatArray and cast my D3DXVECTOR3 as a float*? It'd be less messy, but I'm not sure it's safe.
It leads me to wonder, why isn't SetVector() simply overloaded to accept a D3DXVECTOR3 pointer? I seem to be getting the message that I should avoid 3-vectors. I read somewhere that, for interpolants at least, float4's are always used behind-the-scenes, even if you only need a float or a float2 or something, so you might as well use a float4 and see if you can squeeze some extra data into the unused elements, if possible. Maybe that has something to do with it? My second question has to do with caching certain values, because I know that setting shader constants like this can be expensive, so I want to do it as infrequently as possible. If I am making a draw call (DrawPrimitive or DrawIndexedPrimitive or whatever), and the current object I'm drawing uses the same effect as the last object I drew, can I assume that the shader constants I set for the last object will carry over to the current one? In other words, suppose that the last item used the same texture as the current item. If I can, I'd like to avoid setting the texture again this time. So, I could give my Renderer class an LPD3DTEXTURE9 as a member, to hold the address of the texture that was last used, and before setting the texture, I could check to make sure it's actually different. If not, I don't bother. And if the above will work, I guess my third question is, what sorts of data are usually cached in this way? I mean, aside from textures. Light positions? Light colors? Transformation matrices? Pretty much everything? My fourth question is, is the D3DX framework sort of a newbie framework, or is it taken seriously be professionals? I see a lot of code out there that doesn't involve fx files or anything like that. Instead, you have vsh and psh files, and the stuff in the technique block is replaced by calls (from main code) to SetRenderState(). Also, the D3DX libraries seem to contain a lot of useful macros and stuff, but I especially find the 3D math stuff indispensable, because I really don't want to have to write my own vector, matrix, and quaternion classes, along with all of the math functions needed. I understand the math perfectly (well, less-than-perfectly in the case of quaternions!); I just don't want to write it all myself! On the other hand, I don't mind writing my own Mesh, Sprite, and Font classes, and so I've pretty much ignored the D3DX versions of those.

Share this post


Link to post
Share on other sites
Quote:
My first question is more of a humble observation, that I might be wrong about. Maybe someone can tell me if I'm missing something. It seems that there are a lot of things when it comes to shaders that aren't really conducive to writing readable code.


The difference between the D3DXVECTOR4 and D3DXVECTOR3 is that D3DXVECTOR4 has an extra element which is w and is usually used to specify whether or not the vertices are already transformed (w = 1) or not transformed (w = 0)

Quote:

My second question has to do with caching certain values, because I know that setting shader constants like this can be expensive, so I want to do it as infrequently as possible.

As per your second and third question, I suggest reading this article on Shader constants

Quote:
My fourth question is, is the D3DX framework sort of a newbie framework

Definitely not a newbie framework. The D3DX Framework includes a lot of other functionality such as the Maths that you mentioned but as well as other operations such as D3DXLoadFrom* and the likes. It's definitely not a newbie framework.

I hope this helps!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this