Page 1 of 1

Shared parameter sets

Posted: Tue Feb 17, 2009 6:51 pm
by sinbad
I pulled off one of the 1.8 TODO items today, shared parameter sets.

You can now define a named, shared parameter set by using GpuProgramManager::createSharedParameters. You define the structure of the parameter set through that, and set values on it in much the same way as you do for GpuProgramParameters. You can then reference that shared parameter set using GpuProgramParameters::addSharedParameters, and any values that match that you set in the shared parameter set will become available to that material. From then on when you need to update the parameters for all materials referencing this set, you only have to do so in one place, ie on the shared parameters.

Note that only manual parameters are supported in the shared parameter sets for now (no auto parameters). Since this is primarily designed to reduce the effort involved in updating manual parameters across many materials, auto parameters are not needed.

Also note that you can use the shared parameter set even if you don't use all of the parameters contained within it. It will simply match up the relevant ones and use those. Obviously, only named parameters are supported.

Shared parameter sets are named so that you can reference them easily in scripts and from elsewhere in code. I'm still working on the scripting interface to this though, only the code-level version is in SVN right now.

The base implementation involves the values in the shared parameter set being copied across to the individual parameter sets when being bound. However, it is up to the rendersystem whether this copying actually happens, and there are Ogre::Any variables on GpuProgramParameters, GpuSharedParameters and GpuSharedParametersUsage designed to hold RenderSystem-specific data should it wish to take another approach. For example, in Dx10 you could detect matches with a separate constant buffer defined in the code, and instead of copying / merging values, store the shared parameter set in its own constant buffer and only update it once per frame (there's a frame number counter on it to help with this). I haven't implemented this yet but the facility is there. This could also be done in GLSL with GL_EXT_bindable_uniform, although from the searching I've done it doesn't seem to be very well supported.

I'll be sorting out the script interface for my next update anyway. Hope this is useful to people.

Re: Shared parameter sets

Posted: Tue Feb 17, 2009 6:54 pm
by sinbad
Code example:

Code: Select all

		testSharedParams = GpuProgramManager::getSingleton().createSharedParameters("SinbadsParams");

		// define the shared param structure
		testSharedParams->addConstantDefinition("colourInput", GCT_FLOAT4);
		testSharedParams->addConstantDefinition("someFloatParam", GCT_FLOAT1);

		// set some initial values
		Vector4 col(0.7, 0.0, 0.0, 1.0);
		testSharedParams->setNamedConstant("colourInput", col);
		testSharedParams->setNamedConstant("someFloatParam", 0.1f);

		// reference shared params, and set autos
		GpuProgramParametersSharedPtr params = pass->getVertexProgramParameters();
		params ->setNamedAutoConstant("worldViewProj", GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
		params ->addSharedParameters("SinbadsParams");

		params = pass2->getVertexProgramParameters();
		params ->setNamedAutoConstant("worldViewProj", GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
		params ->addSharedParameters("SinbadsParams");

                // now I can update testSharedParams and it will affect both param sets in both materials / passes



Re: Shared parameter sets

Posted: Tue Feb 17, 2009 9:01 pm
by Assaf Raman
Nice work, I can see many uses for this.

Re: Shared parameter sets

Posted: Tue Feb 17, 2009 10:15 pm
by Wolfmanfx
A nice addtion would to automate the registration of the shared parameter set so that GpuProgramManager adds the shared parameter to every gpu programs (or just Global Shared parameters).

Re: Shared parameter sets

Posted: Tue Feb 17, 2009 11:48 pm
by Kencho
Assaf Raman wrote:Nice work, I can see many uses for this.
Me too :)

Re: Shared parameter sets

Posted: Wed Feb 18, 2009 2:23 am
by iloseall
Thanks!
I think I can provide Parameters of Wind(Dynamic) into all of Material easily Now.

Re: Shared parameter sets

Posted: Wed Feb 18, 2009 10:50 am
by psquare
Hey, that's really cool.

I was doing this externally, via something called 'ParamManager'. Now I can use this :-)

Many thanks!

Re: Shared parameter sets

Posted: Wed Feb 18, 2009 1:31 pm
by sinbad
Wolfmanfx wrote:A nice addtion would to automate the registration of the shared parameter set so that GpuProgramManager adds the shared parameter to every gpu programs (or just Global Shared parameters).
Hmm, interesting idea. I went with 'shared' rather than 'global' just because I thought it was more flexible to be able to have more than one set. But maybe an option to make the set be merged into all parameter sets created since then is doable (slight timing issues with having loaded the program so that names can be matched up, but probably not impossible).

Re: Shared parameter sets

Posted: Wed Feb 18, 2009 7:06 pm
by sinbad
Ok, scripting support has now been added.

Code: Select all

shared_params SomeSharedParams
{
	// A parameter with values initialised
	shared_param_named colourParam float4 0.3 0.2 0.1 1
	// An array parameter
	shared_param_named someArrayParam float4 [3] 
	// A matrix parameter
	shared_param_named aMatrixParam matrix4x4
}

material TestShared1
{
	technique
	{
		pass
		{
			vertex_program_ref YayForShadersvp
			{
				shared_params_ref SomeSharedParams
				// other parameters (autos and pass-specific manual settings)
			}
			// and so on
		}
	}
}
Initial values are supported, although if you never change these values then it's not worth using a shared parameter set at all. But, I figured people might want to set them up with defaults in the script and alter them in code later. Arrays and matrix types are supported too. The manual is updated with the details.

Further enhancements for Dx10 constant buffers and automatically included shared param sets may be considered later.

Re: Shared parameter sets

Posted: Wed Feb 18, 2009 9:11 pm
by haffax
Very good. This makes consistent forward shading much easier to implement. Thanks. :)

Re: Shared parameter sets

Posted: Thu Feb 19, 2009 5:15 am
by iloseall
I try to use Shared Parameter in my Editor project.
I can not modify the ConstantDefinition in GpuSharedParameters.
I add "removeAllConstantDefinition" method for this.

https://sourceforge.net/tracker2/?func= ... tid=302997

Re: Shared parameter sets

Posted: Thu Feb 19, 2009 6:49 pm
by sinbad
Ok thanks, but it's important to note that for performance, the mapping between the shared parameter set and the GpuProgramParameters is set up when the shared set is first referenced by the parameters. Therefore, changing the shared set at runtime when parameters are referencing it is dangerous - it's safe to add new entries (existing param sets referring to the shared set will just never see them), but removing them can cause a crash if you don't also detach and reattach the shared sets to the parameter sets. This was the reason for omitting a 'remove' method.

We can add remove methods, but a strong health warning needs to be put on it.

Re: Shared parameter sets

Posted: Fri Feb 20, 2009 2:33 am
by iloseall
In my editor , shared parameter set, material,compositor can be modified dynamic for.
remove method only used for Developer in Editor.So he can try difference shared parameters and type match the shader's modify.
I think in release client , it is stable.

I has use shared parameter set for dynamic wind direction and force for all tree\grass. And it work fine.
Set the shared parameter ref in my tree ,grass ,etc material .then update the wind parameters in FrameListener::update with wind function.
I like shared parameter.

Can I set a sampler(Cube) parameter in shared parameter set?So I can change env cube dynamic with Charactor position and attach it for all object easily.

Re: Shared parameter sets

Posted: Fri Feb 20, 2009 10:30 pm
by sinbad
iloseall wrote:Can I set a sampler(Cube) parameter in shared parameter set?So I can change env cube dynamic with Charactor position and attach it for all object easily.
Not with this feature, since it only controls parameters rather than other material properties like texture bindings. You can do this kind of thing with script templates, but of course that doesn't help at runtime.

What you could do though is use a single named Texture instance, and use a manual loader to populate the content. When you want to change it, reload it and repopulate the data from another file in your manual loader (the texture can still be called the same thing). Any materials using that texture will then get the new content. The standard loader assumes name == filename, but that doesn't have to be the case.

Re: Shared parameter sets

Posted: Sun Feb 22, 2009 11:52 pm
by lf3thn4d
Hey, this sounds like just THE thing for PSSM split points! :) I like!

Re: Shared parameter sets

Posted: Mon Feb 23, 2009 9:26 am
by Caphalor
Or global shadow softness. :) A really useful feature!

Re: Shared parameter sets

Posted: Tue Feb 24, 2009 6:29 pm
by sinbad
For info, I put in a more robust mechanism for picking up changes to the shared parameter definitions when params are already using them. You can remove individual or all definitions now and add new ones, and the changes will be picked up.

Re: Shared parameter sets

Posted: Thu Feb 26, 2009 12:21 pm
by randall
This could also be done in GLSL with GL_EXT_bindable_uniform, although from the searching I've done it doesn't seem to be very well supported.
Actually, both NVIDIA and AMD supports GL_EXT_bindable_uniform. AMD has implemented this extension in new Catalyst 9.2 driver.

Re: Shared parameter sets

Posted: Sat Apr 04, 2009 11:01 am
by cloud
I've a question, if I create shaders in code with HighLevelGpuProgramManager::createProgram then set the program in a pass I'll already have named params for e.g. getVertexProgramParameters(). I tried just doing pass->getVertexProgramParameters()->addSharedParameters, which didn't seem to work. So are some of the named params then in two places, once in the normal params and once in the shared set? And is there an easier way to get what i want than sort of comparing the two and creating a third that doesn't include any of the shared sets params in the base and then doing addSharedParameters, I think that might get tricky.

Re: Shared parameter sets

Posted: Sat Apr 04, 2009 5:07 pm
by sinbad
The shared parameters don't change what's already present in the actual per-pass parameters. What it does is create a mapping from the named parameter in the shared set to the named parameter in the pass-specific set, and automatically updates them. The mapping is very efficient since its based on buffer offsets, not the name, after the association has been set.

I don't see what's 'tricky'. Create a shared parameter set to contain your shared parameters, with the same names as the parameters you want to match up with. Then add the shared parameters to as many pass-specific params as you like. OGRE then updates all the pass instances from that one shared set which you update.

Re: Shared parameter sets

Posted: Sun Apr 05, 2009 7:50 am
by cloud
Think I've realised what I was doing wrong, I was cloning a material that used shared parameters, but clone didn't seem to actually clone the link :oops:

Re: Shared parameter sets

Posted: Mon Apr 06, 2009 5:31 pm
by sinbad
cloud wrote:Think I've realised what I was doing wrong, I was cloning a material that used shared parameters, but clone didn't seem to actually clone the link :oops:
Ok, this is a bug; GpuSharedParametersUsage needs re-pointing after being copied to point at the clone.