ForwardPlusFlipY-Property will be set without ForwardPus

Anything and everything that's related to OGRE or the wider graphics field that doesn't fit into the other forums.
Post Reply
a_programmer
Gnoblar
Posts: 7
Joined: Sun Nov 24, 2019 6:34 pm

ForwardPlusFlipY-Property will be set without ForwardPus

Post by a_programmer »

The following code sets ForwardPlusFlipY property on glsl shader even if no ForwardPlus is active. Hlsl is ignored. So for me with the same code the textures are flipped for GLSL and not for HLSL with ForwardPlus not enabled.


OgreHlms.cpp: https://github.com/OGRECave/ogre-next/b ... reHlms.cpp

Code: Select all

            ForwardPlusBase *forwardPlus = sceneManager->_getActivePassForwardPlus();
            if( forwardPlus )
                forwardPlus->setHlmsPassProperties( this );

            if( mShaderFileExt == ".glsl" )
            {
                //Actually the problem is not texture flipping, but origin. In D3D11,
                //we need to always flip because origin is different, but it's consistent
                //between texture and render window. In GL, RenderWindows don't need
                //to flip, but textures do.
                const RenderPassDescriptor *renderPassDesc = mRenderSystem->getCurrentPassDescriptor();
                setProperty( HlmsBaseProp::ForwardPlusFlipY, renderPassDesc->requiresTextureFlipping() );
            }

800.PixelShader_piece_ps.any: https://github.com/OGRECave/ogre-next/b ... ece_ps.any

Code: Select all

@property( two_sided_lighting )
	@property( hlms_forwardplus_flipY )
		@piece( two_sided_flip_normal )* (gl_FrontFacing ? -1.0 : 1.0)@end
	@else
		@piece( two_sided_flip_normal )* (gl_FrontFacing ? 1.0 : -1.0)@end
	@end
@end
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: ForwardPlusFlipY-Property will be set without ForwardPus

Post by dark_sylinc »

What is the problem you're having (i.e. picture? RenderDoc capture?)

Despite its name (ForwardPlus) which is how it first appeared, this setting is actually required regardless of whether ForwardPlus is used. Therefore the code you pointed out is correct.

Properly dealing with Y flipping in OpenGL can be tricky though, thus providing us with more info may help us find what is going wrong.
a_programmer
Gnoblar
Posts: 7
Joined: Sun Nov 24, 2019 6:34 pm

Re: ForwardPlusFlipY-Property will be set without ForwardPus

Post by a_programmer »

Hi again. I think I described my problem wrong. Bem flipping I did not mean Y-axis flipping, but front and back face of a shape. I have a two sided material. The front face shows the texture and the back face gets a color. With Glsl then the front face becomes back face and back face becomes front face.



By HLSL
Image


By GLSL

Image


To see this effect please take Sample_Postprocessing and change the following:
Set TwoSidedLighting property on the datablock of the house.

Code: Select all

        
        // House
        item = sceneManager->createItem( "tudorhouse.mesh",
                                         ResourceGroupManager::AUTODETECT_RESOURCE_GROUP_NAME,
                                         SCENE_STATIC );

        assert(dynamic_cast<Ogre::HlmsPbsDatablock*>(item->getSubItem(0)->getDatablock()));
        Ogre::HlmsPbsDatablock* db = static_cast<Ogre::HlmsPbsDatablock*>(
            item->getSubItem(0)->getDatablock());
        db->setTwoSidedLighting(true);
Add the following file to your HLMS folder.
For example Custom_ps_posExecution_piece_ps.any

Code: Select all

@piece( custom_ps_posExecution )
	@property( two_sided_lighting )
		outPs_colour0 = gl_FrontFacing ? outPs_colour0 : float4(1,0,0,1);
	@end
@end

Also, HLSL replaces gl_FrontFacing with SV_IsFrontFace only if two_sided_lighting-property is enabled.
Maybe you could include this in CrossPlatformSettings_piece_all. But that's another story.
User avatar
dark_sylinc
OGRE Team Member
OGRE Team Member
Posts: 5292
Joined: Sat Jul 21, 2007 4:55 pm
Location: Buenos Aires, Argentina
x 1278
Contact:

Re: ForwardPlusFlipY-Property will be set without ForwardPus

Post by dark_sylinc »

OpenGL renders windows bottom to top, OpenGL renders FBO and D3D renders everything top to bottom.

This inconsistency in OpenGL requires us to flip the Y coordinate in the projection matrix for render windows behind the scenes so that everything rendered to windows doesn't look upside down.

You can see the C++ action in OgreHlmsPbs.cpp (and same for Unlit):

Code: Select all

if( renderPassDesc->requiresTextureFlipping() )
{
    projectionMatrix[1][0] = -projectionMatrix[1][0];
    projectionMatrix[1][1] = -projectionMatrix[1][1];
    projectionMatrix[1][2] = -projectionMatrix[1][2];
    projectionMatrix[1][3] = -projectionMatrix[1][3];
}
However a side effect of this Y flipping is that front faces become back faces and viceversa; because Y flipping causes clockwise directions to become counterclockwise and viceversa.

Behind the scenes we flip the culling mode to account for this in OgreHlms.cpp (see HlmsPassPso::InvertVertexWinding):

Code: Select all

//We need to invert culling mode.
if( pso.pass.strongMacroblockBits & HlmsPassPso::InvertVertexWinding )
{
    prepassMacroblock.mCullMode = prepassMacroblock.mCullMode == CULL_CLOCKWISE ? CULL_ANTICLOCKWISE : CULL_CLOCKWISE;
}
But gl_FrontFacing gives you the 'raw' value, and we cannot hide that from you.
Therefore for your code to run properly you have to do:

Code: Select all

@piece( custom_ps_posExecution )
	@property( two_sided_lighting )
		@property( hlms_forwardplus_flipY )
			outPs_colour0 = gl_FrontFacing ? outPs_colour0 : float4(1,0,0,1);
		@else
			outPs_colour0 = gl_FrontFacing ? float4(1,0,0,1) : outPs_colour0;
		@end
	@end
@end
Otherwise without the hlms_forwardplus_flipY bit, rendering will act inconsistently in OpenGL.
Post Reply