Skip to main content
gamedev.net
🔒 Locked

Passing parameters

Started by JorenJoestar 5 replies 750+ views
Original Post
JorenJoestar
JorenJoestar
Hi guys, a simple question: how do you pass parameters (like renderer constant or sampler info, position of the light...) to your shaders? Think of a shader that takes material parameters and textures. Maybe also a light. How do you update you parameters? Thanks!
---------------------------------------http://badfoolprototype.blogspot.com/
dave
dave
ID3DXEffect::SetFloat();
ID3DXEffect::SetFloatArray();

...and when you are done setting constants and textures etc, don't forget to call:

ID3DXEffect::CommitChanges();

Hope that helps,
Striken
Striken
For a CG Vertex shader - define the parameter as (e.g):
CGparameter    _lightPosition;


Create the parameter as the vertex program will "see it" as:
_lightPosition   = cgGetNamedParameter(_vertexProgram, "lightPosition");


To set the value of the parameter: (where _lightWorldPos is a pointer to an array of 4 floats in this case)
cgGLSetParameter4fv( _lightPosition, _lightWorldPos );


If you want to pass parameters from your vertex to pixel shader you'll need to do that within the fragment's unused pre-defined components, such as a multi-texture-coordinate.

Hope that helps too.
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
JorenJoestar
JorenJoestar
Well...I'm saying about a flexible system, in which you have different shaders with different parameters...maybe for an object you have multitexture + material + spotlight, and on the next object only material and light...you need something flexible that pass parameters for each shader. And that also know where to take informations to pass! ;)
---------------------------------------http://badfoolprototype.blogspot.com/
dave
dave
Oh ok..

Well you could enforce a semantic standard that all shaders that are to be rendered with in your system abide by. This is what i did. You attach semantics to the floats, for example (IN HLSL):
float4 LightPosition : LIGHT_POSITION; // LIGHT_POSITION could be anything

You can then find all of these semantics and their associated variables by using:
ID3DXBaseEffect::GetParameterBySemantic();


Your engine would have to be able to provide the appropriate values that corresponds to each semantic, most commonly camera and light positions as well as variations of the world, view and projection matrices combined.

Hope that helps,
Striken
Striken
Do you mean have a single shader that for any unique object in the scene (for example):

1. Can handle lighting if required.
2. Can handle diffuse mapping if required.
3. Can handle specular mapping if required.
4. Can handle normal mapping if required etc?

And you want it to have "If(x) disable lighting" ? As far as I know, that can't be done. What you need to do is write multiple combinations of different shaders and apply the appropriate one when required:

- one that can handle all cases
- one that can handle only lighting and diffuse mapping
- one that can handle only diffuse mapping and specular mapping
- one that can handle only normal mapping

and so forth.

Alternatively depending on your complexity you could pass in a load of bools into your shader program, but conditionals inside a shader program is a big no-no.
Teach a programmer an answer, he can code for a day. Show a programmer the documentation, he can code for a lifetime.
JorenJoestar
JorenJoestar
Right Dave, this is what I want to do...I want to specify the input of the shader
(constants, sampler infos...) and then I retrieve all the infos and set on the shader.

I'm using shaders done in Cg and then compiled, so I work with assembly shaders.
The good thing is that I can build also a total array of float merging all the input of the shaders, then calling

m_Device->SetVertexShaderConstantf(...)

I can set all these parameters.
I don't want to use the DX Effect framework, I want to do something mine, to support also my OpenGL renderer...

I have to unify the way in which I write shaders to achieve this point.
---------------------------------------http://badfoolprototype.blogspot.com/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.