specular lighting and render system

Discussion area about developing or extending OGRE, adding plugins for it or building applications on it. No newbie questions please, use the Help forum for that.
Post Reply
User avatar
bishopnator
Goblin
Posts: 225
Joined: Thu Apr 26, 2007 11:43 am
Location: Slovakia / Switzerland
x 5

specular lighting and render system

Post by bishopnator »

Hi, I found, that ogre has always turned-on specular lighting in DX render system:

Code: Select all

	//---------------------------------------------------------------------
	void D3D9RenderSystem::_beginFrame()
	{
		HRESULT hr;

		if( !mActiveViewport )
			OGRE_EXCEPT( Exception::ERR_INTERNAL_ERROR, "Cannot begin frame - no viewport selected.", "D3D9RenderSystem::_beginFrame" );

		if( FAILED( hr = mpD3DDevice->BeginScene() ) )
		{
			String msg = DXGetErrorDescription9(hr);
			OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error beginning frame :" + msg, "D3D9RenderSystem::_beginFrame" );
		}

		if(!mBasicStatesInitialised)
		{
			// First-time 
			// setup some defaults
			// Allow specular
			hr = __SetRenderState(D3DRS_SPECULARENABLE, TRUE);
			if (FAILED(hr))
			{
				String msg = DXGetErrorDescription9(hr);
				OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR, "Error enabling alpha blending option : " + msg, "D3D9RenderSystem::_beginFrame");
			}
			mBasicStatesInitialised = true;
		}

	}
I found in DX documentation in 'Performance Optimizations' section following tip:
Specular highlights almost double the cost of a light. Use them only when you must. Set the D3DRS_SPECULARENABLE render state to 0, the default value, whenever possible. When defining materials, you must set the specular power value to zero to turn off specular highlights for that material; just setting the specular color to 0,0,0 is not enough.
I wrote "simple" shader for calculating lighting on my model with specularity and difference in fps was significant - if I added specular color calculation fps decreased from 200 to 100fps (GeForce 8600GT). I didn't test it with fixed pipeline, but with older cards I remember, that it significaly decreased fps. So my question is if somebody though about this issue and why specular calculations are always enabled? (maybe I missed something, but I found D3DRS_SPECULARENABLE only in _beginFrame method)
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

Hmm, this is interesting. I'm surprised it even made a difference when you had shaders enabled though, since fixed function should not be used. I'll see if we can make this conditional.
User avatar
bishopnator
Goblin
Posts: 225
Joined: Thu Apr 26, 2007 11:43 am
Location: Slovakia / Switzerland
x 5

Post by bishopnator »

I tried this:
diffuse+ambient (200fps):

Code: Select all

	// per vertex lightning calculation - like D3D implementation
	float4 fvDiffuse = lightDiffuse * saturate(dot(normLightDir, worldNormal));
	o.vColor = lightAmbient + fvDiffuse;
diffuse+ambient+specular(100fps):

Code: Select all

	// per vertex lightning calculation - like D3D implementation
	float4 fvDiffuse = lightDiffuse * saturate(dot(normLightDir, worldNormal));
	o.vColor = lightAmbient + fvDiffuse;
	float3 fvH = normalize(normalize(eyePos - vPosition.xyz) + normLightDir);		
	float4 fvSpecular = lightSpecular * pow(dot(worldNormal, fvH), matShininess);	
   o.vColor += fvSpecular;
I only suppose that in fixed pipeline if specular lighting is enabled than it is always calculated. Then I search DX API and I found performance tip which I pasted here in this thread. I didn't try to modify ogre (remove enabling specular lighting) and compare fps with and without specular in fixed pipeline.
User avatar
sinbad
OGRE Retired Team Member
OGRE Retired Team Member
Posts: 19269
Joined: Sun Oct 06, 2002 11:19 pm
Location: Guernsey, Channel Islands
x 66
Contact:

Post by sinbad »

Ah ok. Yes, no guarantee that turning it off in fixed-function will make a measurable difference, probably depends on the driver. We can check though.
Post Reply