Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 21-11-2009
Member
 
Join Date: Jun 2009
Posts: 92
Parameters shader generic

In carrying out my little 3D engine, I'd like to manage a number of different materials. Each material must be easily applied to a model and described using a file .xml and a file .fx.

The trouble is that in this context, I neither know the type nor the number of variables that will be transmitted to the graphics card, except the conventional matrix world, view, projection and lighting parameters.

For example, in the case of a water shader, I have many variable to pass:
- 2 textures: the maps of reflection and refraction.
- Several vectors: wind direction, wave height & co.
- Floats: such as the time elapsed since the last frame.

All these parameters are easy to pass, but I'd not like to do one class per material, the idea to add them via the editor, at the click. So at the risk of repeating myself, it would have all these parameters fully described in a file of materials (a .xml in my case).

Here is the line to add a parameter in a conventional manner (by the color of water):
Code:
this .effect.Parameters["xWaterColor"].SetValue( this .waterColor);
The worry is that SetValue () does not like generic types, it has an overload for each type supported.

So I thought of a solution that I find VERY ugly as follows:
Code:
private SerializableDictionary<string, float > floatList = new SerializableDictionary<string, float >();
private SerializableDictionary<string, int > intList = new SerializableDictionary<string, int >();
private SerializableDictionary <string, Vector2> vector2List = new SerializableDictionary <string, Vector2> (); 
[...]
For each type of parameters supported, I have a dictionary described in XML. The dictionary key is the name of the variable in HLSL code and value, the value to pass. One is ugly, and absolutely not flexible, by not supporting variables changing at runtime, these are only static values and described very heavy.
Reply With Quote
  #2  
Old 21-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Parameters shader generic

I did not really understand what you're looking to do, or what is your problem, but at the same time it is late. I tell myself that there is a time when your program does not do for you, this will necessarily work. If I understand correctly, you must pass parameters using this line:
this.effect.Parameters [ "xWaterColor"]. SetValue (this.waterColor);

And if I understand you go to a particular XML file. I imagine that your XML file contains the first parameter name "xWaterColor" and its value, float, int, Vector2... Isn't it easy, right?

You read the XML file, you retrieve each parameter with its name, you castes according to type, and you fill your collection this.effect.Parameters. It must miss a news, it's more complicated than that, perhaps, but I do not see, but at the same time it is late.
Reply With Quote
  #3  
Old 21-11-2009
Member
 
Join Date: Jun 2009
Posts: 92
Re: Parameters shader generic

Thank you for your reply. What I was trying to do is own implementation. Actually my XML file contains any event, a key pair - decorated with a value type.

Example:

Code:
<Item>
   <Key>WaterColor</key>
   <Value>
      <Vector4>
         <X>.2f</X>
         <Y>.1f</Y>
         <Z>.6f</Z>
         <W>1</W>
      </Vector4>
   </Value>
</Item>
I have therefore a key pair - value. The key is "WaterColor" which is the name of the variable HLSL. The value is contained in a Vector4.

The problem is that for this type of structure in the XML it must have the type of code I posted in my 1st message to receive, it is necessary that I drive in my code a

Code:
Dictionary<string, Vector4> vector4List;
I am forced to have that for each type and I just found that ugly. The ideal would be something like:

Code:
Dictionary<string, object> paramsList;
The problem is that I can not spend a SetValue () directly to a type object, it must be caster. And for the caster I must test the type.

Code:
if (paramsList.ElementAt(i).Value is Vector4)
   this.effect.Parameters[paramsList.ElementAt(i).Key].SetValue((Vector4)paramsList.ElementAt(i).Value);
I think it's even uglier than browse each dictionary type.

So the problem is not really "How do work," but how to make it really generic, because for me that remains more or less hard.
Reply With Quote
  #4  
Old 21-11-2009
Member
 
Join Date: Nov 2008
Posts: 1,054
Re: Parameters shader generic

You'll have to necessarily treat each type separately, your SetValue () is not of type object, you have no choice.

It is what elsewhere this.effect.Parameters as data type? It is a collection? Are you sure there is not a point of entry into type object?

Otherwise, careful not to repeat the same access several times

Code:
if (paramsList.ElementAt(i).Value is Vector4)
this.effect.Parameters[paramsList.ElementAt(i).Key].SetValue((Vector4)paramsList.ElementAt(i).Value);
You do paramsList.ElementAt access to (i) 3 times then it would be better to keep it in an intermediate variable. But it may be for example that you write it, ok

ParamsList What if?

And if Value is of type object, you have no choice! Forced to make a "is".

In fact to conclude, I still can not see really what you're looking to do, and especially in what order!

Did you try to read an XML file? In writing? Where do your data entry you're trying to treat? Very sharp!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Parameters shader generic"
Thread Thread Starter Forum Replies Last Post
Can we use 2 different 32bit shader to work as 64bit shader LoLaBoy Monitor & Video Cards 4 18-12-2011 03:33 PM
Realtime Shader In R12? Layton Windows Software 6 12-05-2010 11:31 PM
Difference Between Pixel Shader 2.0 and Pixel Shader 3.0 Aarif Monitor & Video Cards 4 12-03-2009 10:30 PM
Pixel Shader v1.1 Mr.Macroney Windows XP Support 9 25-11-2008 10:51 PM
Shader Model Szymon Video Games 2 22-10-2008 07:31 PM


All times are GMT +5.5. The time now is 10:02 AM.