Pixel Shade EVERYTHING?

Started by
25 comments, last by Basiror 19 years, 8 months ago
Off Topic:
It is almost scary how closely you are following the same steps that I took (I think I am only about 2 months ahead now!!!). I used to pass the device around to all my objects as well, which is bad for many reasons (which you already stated), but switched when I switched to effect files!!

Back on topic:
It is essentially the same, but if you are going to be using effect files, you really should use the SetTexture method. As noted before, even fixed function pipeline parameters are defined in the .fx file, so it would only make sense to bind the texture to the file itself rather than to the device.

So as a global parameter in your effect file you would have:

texture myTexture_1;

then in your application code before rendering you would have:

MyEffect->SetTexture("myTexture_1", myTexture);

Then render your geometry.

Good Luck,

Jason Z
Advertisement
hehe, I guess its a narrow path to games engine guru ;)

I'm still having trouble getting my head round parsing Effect objects about, since nothing is stongly typed i.e. MyEffect->SetValue("SomeRandomStringGoesHere", someValue); just doesn't feel right, what if the effect file changes and the name of the property with it? I'm also hitting the wall of what if I want to support the old FF pipeline as well, everything that can be drawn will need to be able to draw using shaders AND the FF... for some objects this can cause a lot of code copying.

Perhaps there is a simpler method I'm not seeing?
Here is a sample .fx file - maybe seeing it will benefit you.

//-----------------------------------------------------------------------------// DiffuseTexture.fx//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// Uniform parameters//-----------------------------------------------------------------------------float4x4 Transform;texture DiffuseTexture; //-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// Samplers//-----------------------------------------------------------------------------sampler Sampler = sampler_state{    Texture   = (DiffuseTexture);    MipFilter = LINEAR;    MinFilter = LINEAR;    MagFilter = LINEAR;};//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// Structures//-----------------------------------------------------------------------------struct vertex{    float3 position : POSITION;    float3 normal   : NORMAL;    float2 texcoord : TEXCOORD0;};//-----------------------------------------------------------------------------struct fragment{    float4 position : POSITION;    float2 texcoord : TEXCOORD0;    float4 color    : COLOR0;};//-----------------------------------------------------------------------------struct pixel{	float4 color    : COLOR;};//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// Vertex shaders//-----------------------------------------------------------------------------fragment myvs( vertex IN ){    fragment OUT;	OUT.position = mul( Transform, float4(IN.position, 1) );	OUT.color = float4(1.0, 1.0, 1.0, 1.0);	OUT.texcoord = IN.texcoord;	return OUT;}//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// Techniques//-----------------------------------------------------------------------------technique Textured{    pass Pass0    {        ZEnable = true;        VertexShader = compile vs_1_1 myvs();        PixelShader  = null;		        Texture[0] = <DiffuseTexture>;        // Blending stage 0        ColorOp[0]   = SelectArg1;        ColorArg1[0] = Texture;        ColorArg2[0] = Diffuse;        AlphaOp[0] = SelectArg1;        AlphaArg1[0] = Diffuse;        AlphaArg2[0] = Texture;		        // Blending Stage 1        ColorOp[1] = Disable;        AlphaOp[1] = Disable;    }}//-----------------------------------------------------------------------------Technique WireFrame{    Pass P0    {        // Use transformation vertex shader        VertexShader = compile vs_1_1 myvs();        PixelShader  = null;		        // Disable texturing        ColorOp[0] = Disable;        AlphaOp[0] = Disable;		        // Set Wireframe fillmode        FillMode = Wireframe;        // Set ambient to white        Lighting = true;        Ambient = {1.0,1.0,1.0,1.0};        MaterialAmbient = {1.0,1.0,1.0,1.0};        AmbientMaterialSource = Material;    }}//-----------------------------------------------------------------------------


Essentially, once the effect is loaded, you just have to do two things:

1) Set the uniform parameters
2) Send your geometry with one of the Draw calls

Keep in mind that there is more to it than just willy nilly setting of texture names and

different variables. Each uniform parameter is mapped to a specific register in the gpu,

the text name is more for us to understand than anything else.

Also, this is not to mention the use of annotations and semantics to keep the meaning of the

variables compatible accross different effect files. Read up on them and if you have any

questions post them back up here.

Good Luck,

Jason Z
I didn't know you could do things like "Lighting = true" in a technique. I was thinking how the FF pipeline uses the Material object to set things like Diffuse, Specular etc properties before drawing. I was planning on implimenting something similar in vertex shaders so that those properties could be taken into account.

That sample is interesting, I was aiming to have lots of variables in my .fx file and set them at runtime i.e:

m_Effect.SetValue("WorldViewProjection", worldViewProj);m_Effect.SetValue("World", m_World.Map.Camera.WorldMatrix);m_Effect.SetValue("DirectionalLight1" , new Vector4(-0.6f, 0.08f, -0.82f, 1));m_Effect.SetValue("DirectionalLight2" , new Vector4(0.3f, -1, 1, 1));m_Effect.SetValue("AmbientColor" , new Vector4(0.4f, 0.4f, 0.4f, 1));m_Effect.SetValue("LightColor" , new Vector4(0.4f, 0.4f, 0.4f, 1));m_Effect.SetValue("MaterialColor" ,new Vector4(1, 1, 1, 1));


But from your example, such things are more or less hard coded into the .fx file. Is this the preferred method? I guess it is easier to modify an fx file than the source code to the engine. I am starting to get the feeling that HLSL should be used more as a part of the engine, simply written in a different language rather than a property of it.

Will that code u posted work with DirectX as is? I find it hard to know what code will and wont work when there are quite a few contradicting standards it seems, for example in the MSDN they always use the following to define the world x projecting x view matrix:

WorldProjectionView: WORLDPROJECTIONVIEW

yet, I have also seen examples where the "WORLDPROJECTIONVIEW" after the colon is skipped, such as your own, in fact, I don't even know why it is there, it doesn't seem to serve any purpose.

Thanks for your help here Jason, I want to set off down the right track, I've already boxed myself into some corners that have taken a while to code out of, often leaving a messy trail behind. But hey thats all part of the learning process :)
The word after the colon is called a semantic. You can find a good explanation in the DX docs for them.

Remember that .fx files are very flexible. You can hard code or use runtime variables wherever you want, so it can be adjusted to your needs.

The file should work "as is", just remember when setting the Transform matrix that it is the WorldViewProjection and that you transpose the matrix before setting it. Let me know if you have trouble getting it to work.

Jason Z
I've ordered the Cg Tutorial. I just can't find anything online that could come close to a good book. I'm going on holiday for a week now, but I'll check the book out when I get back. Thanks for all the help Jason. apriciate it :)
Quote:Original post by OrangyTang
Quote:Original post by Basiror
this should work pretty well complex scenes since overdraw is a lot higher

The overhead of switching shaders is almost certainly going to be greater than any gain from early z-rejection.



where do you switch a shader when you render static world geometry?
http://www.8ung.at/basiror/theironcross.html

This topic is closed to new replies.

Advertisement