Does Iphone support shadow

Discussion of issues specific to mobile platforms such as iOS, Android, Symbian and Meego.
User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 99
Contact:

Re: Does Iphone support shadow

Post by Wolfmanfx »

Can you make a patch? Screenshot would also nice :)
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

i have changed 2 files:

OgreSceneManager.cpp

Code: Select all


void SceneManager::initShadowVolumeMaterials(void)
{
    /* This should have been set in the SceneManager constructor, but if you
       created the SceneManager BEFORE the Root object, you will need to call
       SceneManager::_setDestinationRenderSystem manually.
     */
    assert( mDestRenderSystem );

    if (mShadowMaterialInitDone)
        return;

    if (!mShadowDebugPass)
    {
        MaterialPtr matDebug = 
            MaterialManager::getSingleton().getByName("Ogre/Debug/ShadowVolumes");
        if (matDebug.isNull())
        {
            // Create
            matDebug = MaterialManager::getSingleton().create(
                "Ogre/Debug/ShadowVolumes", 
                ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME);
            mShadowDebugPass = matDebug->getTechnique(0)->getPass(0);
            mShadowDebugPass->setSceneBlending(SBT_ADD); 
            mShadowDebugPass->setLightingEnabled(false);
            mShadowDebugPass->setDepthWriteEnabled(false);
            TextureUnitState* t = mShadowDebugPass->createTextureUnitState();
            t->setColourOperationEx(LBX_MODULATE, LBS_MANUAL, LBS_CURRENT, 
                ColourValue(0.7, 0.0, 0.2));
            mShadowDebugPass->setCullingMode(CULL_NONE);

            if (mDestRenderSystem->getCapabilities()->hasCapability(
                RSC_VERTEX_PROGRAM))
            {
                ShadowVolumeExtrudeProgram::initialise();

                // Enable the (infinite) point light extruder for now, just to get some params
                mShadowDebugPass->setVertexProgram(
                    ShadowVolumeExtrudeProgram::programNames[ShadowVolumeExtrudeProgram::POINT_LIGHT]);
				mShadowDebugPass->setFragmentProgram(ShadowVolumeExtrudeProgram::frgProgramName);				
                mInfiniteExtrusionParams = 
                    mShadowDebugPass->getVertexProgramParameters();
                mInfiniteExtrusionParams->setNamedAutoConstant("worldviewproj_matrix", 
                    GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
                mInfiniteExtrusionParams->setNamedAutoConstant("light_position_object_space", 
                    GpuProgramParameters::ACT_LIGHT_POSITION_OBJECT_SPACE);
                // Note ignored extra parameter - for compatibility with finite extrusion vertex program
                //mInfiniteExtrusionParams->setNamedAutoConstant("shadow_extrusion_distance", 
					//GpuProgramParameters::ACT_SHADOW_EXTRUSION_DISTANCE);
            }	
            matDebug->compile();

        }
        else
        {
            mShadowDebugPass = matDebug->getTechnique(0)->getPass(0);

            if (mDestRenderSystem->getCapabilities()->hasCapability(RSC_VERTEX_PROGRAM))
            {
                mInfiniteExtrusionParams = mShadowDebugPass->getVertexProgramParameters();
            }
        }
    }

    if (!mShadowStencilPass)
    {

        MaterialPtr matStencil = MaterialManager::getSingleton().getByName(
            "Ogre/StencilShadowVolumes");
        if (matStencil.isNull())
        {
            // Init
            matStencil = MaterialManager::getSingleton().create(
                "Ogre/StencilShadowVolumes",
                ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME);
            mShadowStencilPass = matStencil->getTechnique(0)->getPass(0);

            if (mDestRenderSystem->getCapabilities()->hasCapability(
                RSC_VERTEX_PROGRAM))
            {

                // Enable the finite point light extruder for now, just to get some params
                mShadowStencilPass->setVertexProgram(
                    ShadowVolumeExtrudeProgram::programNames[ShadowVolumeExtrudeProgram::POINT_LIGHT_FINITE]);
				mShadowStencilPass->setFragmentProgram(ShadowVolumeExtrudeProgram::frgProgramName);				
                mFiniteExtrusionParams = mShadowStencilPass->getVertexProgramParameters();
                mFiniteExtrusionParams->setNamedAutoConstant("worldviewproj_matrix", 
                    GpuProgramParameters::ACT_WORLDVIEWPROJ_MATRIX);
                mFiniteExtrusionParams->setNamedAutoConstant("light_position_object_space", 
                    GpuProgramParameters::ACT_LIGHT_POSITION_OBJECT_SPACE);
                // Note extra parameter
                //mFiniteExtrusionParams->setNamedAutoConstant("shadow_extrusion_distance", 
                  //  GpuProgramParameters::ACT_SHADOW_EXTRUSION_DISTANCE);
            }
            matStencil->compile();
            // Nothing else, we don't use this like a 'real' pass anyway,
            // it's more of a placeholder
        }
        else
        {
            mShadowStencilPass = matStencil->getTechnique(0)->getPass(0);

            if (mDestRenderSystem->getCapabilities()->hasCapability(RSC_VERTEX_PROGRAM))
            {
                mFiniteExtrusionParams = mShadowStencilPass->getVertexProgramParameters();
            }
        }
    }




    if (!mShadowModulativePass)
    {

        MaterialPtr matModStencil = MaterialManager::getSingleton().getByName(
            "Ogre/StencilShadowModulationPass");
        if (matModStencil.isNull())
        {
            // Init
            matModStencil = MaterialManager::getSingleton().create(
                "Ogre/StencilShadowModulationPass",
                ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME);
            mShadowModulativePass = matModStencil->getTechnique(0)->getPass(0);
            mShadowModulativePass->setSceneBlending(SBF_DEST_COLOUR, SBF_ZERO); 
            mShadowModulativePass->setLightingEnabled(false);
            mShadowModulativePass->setDepthWriteEnabled(false);
            mShadowModulativePass->setDepthCheckEnabled(false);
            mShadowModulativePass->setFragmentProgram("Ogre/FullScreenQuadFragmentProgram");
            mShadowModulativePass->setVertexProgram("Ogre/FullScreenQuadVertexProgram");
            TextureUnitState* t = mShadowModulativePass->createTextureUnitState();
            t->setColourOperationEx(LBX_MODULATE, LBS_MANUAL, LBS_CURRENT, 
                mShadowColour);
            mShadowModulativePass->setCullingMode(CULL_NONE);
        }
        else
        {
            mShadowModulativePass = matModStencil->getTechnique(0)->getPass(0);
        }
    }
.
.
.
.
.
I moved that code to named constants, because indexed didn't work.

And in OgreShadowVolulumeExtrusionProgram:

Code: Select all

String ShadowVolumeExtrudeProgram::mFullscreenQuadFs_glsles = 
    "#version 100\n"
    "precision highp float;\n"
    "precision highp int;\n"
    "precision lowp sampler2D;\n"
    "precision lowp samplerCube;\n\n"
    "uniform sampler2D quadTexture;\n"
    "varying vec4 texCoords;\n"
    "void main()\n"
    "{\n"
    "    //gl_FragColor = texture2D(quadTexture,vec2(texCoords));\n"
        "    gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n"
    "}\n";

    
    String ShadowVolumeExtrudeProgram::mFullscreenQuadVs_glsles = 
    "#version 100\n"
    "precision highp float;\n"
    "precision highp int;\n"

    "// Directional light extrude - FINITE\n"
    "attribute vec4 uv0;\n"
    "attribute vec4 position;\n\n"
    "varying vec4 texCoords;  // texCoords\n\n"
    "void main()\n"
    "{\n"
    "\n"
    "    texCoords = uv0;\n"
    "    gl_Position = position;\n"
    "\n"
    "}\n";

and there ::

void ShadowVolumeExtrudeProgram::initialise(void)

Code: Select all


else if (syntax == "glsles")
					{
						HighLevelGpuProgramPtr vp = 
							HighLevelGpuProgramManager::getSingleton().createProgram(
							programNames[v], ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME,
							"glsles", GPT_VERTEX_PROGRAM);
						vp->setSource(ShadowVolumeExtrudeProgram::getProgramSource(
							vertexProgramLightTypes[v], syntax, 
							vertexProgramFinite[v], vertexProgramDebug[v]));
						vp->setParameter("target", syntax);
						vp->load();

						if (frgProgramName.empty())
						{
							frgProgramName = "Ogre/ShadowFrgProgram";
							HighLevelGpuProgramPtr fp = 
								HighLevelGpuProgramManager::getSingleton().createProgram(
								frgProgramName, ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME,
								"glsles", GPT_FRAGMENT_PROGRAM);
							fp->setSource(mGeneralFs_glsles);
							fp->setParameter("target", "glsles");
							fp->load();
						}
                        [b]//that added by me[/b]
                        HighLevelGpuProgramPtr vp1 = 
                        HighLevelGpuProgramManager::getSingleton().createProgram(
                                                                                 "Ogre/FullScreenQuadVertexProgram", ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME,
                                                                                 "glsles", GPT_VERTEX_PROGRAM);
						vp1->setSource(mFullscreenQuadVs_glsles);
						vp1->setParameter("target", syntax);
						vp1->load();
                        
                        HighLevelGpuProgramPtr fp1 = 
                        HighLevelGpuProgramManager::getSingleton().createProgram(
                                                                                 "Ogre/FullScreenQuadFragmentProgram", ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME,
                                                                                 "glsles", GPT_FRAGMENT_PROGRAM);
                        fp1->setSource(mFullscreenQuadFs_glsles);
                        fp1->setParameter("target", "glsles");
                        fp1->load();
                        

don't forget to add stencil buffer FBO attacment as mentioned above.

I have added shaders for rendering a modulative quad programmatically, because rtshader_system failed to do it
Compiler error: unknown error in RTShaderSystem.material(23): token class, rtshader_system, unrecognized.


That's the reason why only modulative stencil shadows work.

Here is a result:
IMG_0636.PNG
IMG_0637.PNG
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

the main cpu impact is made by calculating of shadow volume. i'll try to implement in on NEON using accelerate.framework. maybe someone want to help me :)?
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

I don't have the code handy at the moment, which code are you speaking of? I'm already working on some NEON optimization for skinning and might as well tack that on too. BTW, to make it portable we'll have to use inline asm(it pains me to not be able to use Accelerate but it has to be this way)
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

i profiled the code and saw that math and vector operations take a lot of time. so maybe the math lib can be optimized. Will try today with Accelerate.
Screen shot 2011-09-20 at 9.05.31 AM.png
Last edited by blackmonster on Tue Sep 20, 2011 7:09 am, edited 1 time in total.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

BTW, can my changes be applied to 1.8 branch? i can't submit a patch because i'm not sure that my changes are required in case of rtshader_system is enabled and works.

Parsing script RTShaderSystem.material
Compiler error: unknown error in RTShaderSystem.material(23): token class, rtshader_system, unrecognized.
User avatar
Wolfmanfx
OGRE Team Member
OGRE Team Member
Posts: 1525
Joined: Fri Feb 03, 2006 10:37 pm
Location: Austria - Leoben
x 99
Contact:

Re: Does Iphone support shadow

Post by Wolfmanfx »

Rtss is working this bug is fixed in the main trunk just make an update
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

ok, thanks, will try now
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

bug with vertex program assigment is not yet fixed.
app crashes at

Code: Select all

GLSLESLinkProgram::GLSLESLinkProgram(GLSLESGpuProgram* vertexProgram, GLSLESGpuProgram* fragmentProgram)
        : mVertexProgram(vertexProgram)
		, mFragmentProgram(fragmentProgram)
		, mUniformRefsBuilt(false)
        , mLinked(false)
		, mTriedToLinkAndFailed(false)
	{
		// init CustomAttributesIndexs
		for(size_t i = 0 ; i < VES_COUNT; i++)
			for(size_t j = 0 ; j < OGRE_MAX_TEXTURE_COORD_SETS; j++)
		{
			mCustomAttributesIndexes[i][j] = NULL_CUSTOM_ATTRIBUTES_INDEX;
		}
        
        if (!mVertexProgram || !mFragmentProgram)
        {
            OGRE_EXCEPT(Exception::ERR_RENDERINGAPI_ERROR,
                        "Attempted to create a shader program without both a vertex and fragment program.", //terminate called after throwing an instance of   'Ogre::RenderingAPIException'
                        "GLSLESLinkProgram::GLSLESLinkProgram");
        }
	}
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

here is a patch (sourceforge hasn't yet sent me a confirmation email)
Attachments
ogre_1of1.patch.zip
(3.8 KiB) Downloaded 127 times
User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Does Iphone support shadow

Post by jacmoe »

What do you mean 'sourceforge' ?
Use our Mantis :
http://www.ogre3d.org/mantis/ :)
Use your forum credentials
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

i wanted to apply a patch on ogre's threre:
https://sourceforge.net/tracker/?group_ ... tid=302997,
i registered and now i'm waiting for the confirmation email
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

just rewrote few Ogre::Math and Ogre::Vector3 and Ogre::Vector4 functions with Accelerate.Framework (with ARM NEON support). got 47 FPS on ipod4 in release mode with the same camera and light positions as on screenshots above. Now the task is to move the sillhoute detection to it.
Also would be good if somebody pointed me how to enable SHADOWTYPE_STENCIL_ADDITIVE, because now it doesn't work due to incorrect fragment program generation in shadow volume rendering pass
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

masterfalcon wrote:I don't have the code handy at the moment, which code are you speaking of? I'm already working on some NEON optimization for skinning and might as well tack that on too. BTW, to make it portable we'll have to use inline asm(it pains me to not be able to use Accelerate but it has to be this way)

are you using any intrinsics?
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

Doing all by hand. No intrinsics.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

that's very cool. i just finished to rewrite the lightfacing function on neon intrinsics. now will make few test and if it's ok will rewrite using pure assembly
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

Already got that one down. OptimisedUtil has been my main focus since it is a huge CPU bottleneck. I'm on to optimizing the routines now.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

Is there any chance for my patch to be added to ogre?
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

Quite possibly. Next time we go through the patch list I'll take a look at it.

Make sure to submit a contributor agreement too.
blackmonster
Halfling
Posts: 55
Joined: Tue May 17, 2011 11:20 am
x 1

Re: Does Iphone support shadow

Post by blackmonster »

here is how i did a function of light facing calculation:

Code: Select all

   void OptimisedUtilGeneral::calculateLightFacing(
        const Vector4& lightPos,
        const Vector4* faceNormals,
        char* lightFacings,
        size_t numFaces)
    {
//        for (size_t i = 0; i < numFaces; ++i)
//         {
//                     *lightFacings++ = (lightPos.dotProductStd(*faceNormals++) > 0);
//                }
        float32x4_t n0, n1, n2, n3;
        int32x4_t mask;
        float32x4_t zeroVector = vdupq_n_f32(0.0f);
        float32x4_t lp = vld1q_f32(&lightPos.x);
        float32x4x2_t tmp1, tmp2;
        size_t numIterations = numFaces / 5;
        uint32 masks[] = {0,0,0,0};
        for (size_t i = 0; i < numIterations; ++i)
        {
            
           // break;
            n0 = vld1q_f32(&faceNormals[0].x);
            n1 = vld1q_f32(&faceNormals[1].x);
            n2 = vld1q_f32(&faceNormals[2].x);
            n3 = vld1q_f32(&faceNormals[3].x);
            
            
            n0 = vmulq_f32(n0,lp);
            n1 = vmulq_f32(n1,lp);
            n2 = vmulq_f32(n2,lp);
            n3 = vmulq_f32(n3,lp);
            
            faceNormals += 4;
            
            tmp1 = vuzpq_f32(n0, n1);
            tmp2 = vuzpq_f32(n2, n3);
            n0 = vaddq_f32(tmp1.val[0], tmp1.val[1]);
            n1 = vaddq_f32(tmp2.val[0], tmp2.val[1]);
            tmp1 = vuzpq_f32(n0, n1);
            n3 = vaddq_f32(tmp1.val[0], tmp1.val[1]);
            mask = vcgtq_f32(n3, zeroVector);
            vst1q_u32(masks,mask);
            //20 cycles NEON stall. do some work on ARM
            
            *(lightFacings+4) = (lightPos.dotProductStd(*(faceNormals)) > 0);
            for (int k = 0; k< 4; k++)
                *lightFacings++ = masks[k] > 0;//(lightPos.dotProductStd(*faceNormals++) > 0);
//            *lightFacings++=(lightPos.dotProductStd(*faceNormals++) > 0);
            //vst1q_u32(masks,mask);
            //lightFacings += 4;
            faceNormals++;
            lightFacings++;
        }
        for(int i=0; i< numFaces % 5; i++)
            *lightFacings++ = (lightPos.dotProductStd(*faceNormals++) > 0);
        

    }
it works, but i haven't noticed any FPS gain, but profiler shown the 3 times lower cpu utilization in that function. maybe i'm doing the storring vector from neon to memory in a wrong way?
SufferinPup
Greenskin
Posts: 119
Joined: Tue Mar 02, 2010 11:09 pm
x 2

Re: Does Iphone support shadow

Post by SufferinPup »

This thread is a bit tough to follow, what is going to be the final word for shadows on iOS for 1.8?

I tried out RC1, and in the shadow sample stencil works fine but texture shadows caused some very odd results (on an iPad2). Is stencil going to be the only option due to hardware restrictions? I'm a bit worried about generating shadow volumes for semi-complex scenes with such a low end cpu, even the latest hardware is pokey in that area. :)

As an aside, I wonder if texture-based shadows would be faster on the newer iOS hardware (A5) due to the powerful GPU.
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

Texture shadows work. But stencil do not. The patch provided for stencil shadows had some issues. I'll try it again to see if they can be resolved.
SufferinPup
Greenskin
Posts: 119
Joined: Tue Mar 02, 2010 11:09 pm
x 2

Re: Does Iphone support shadow

Post by SufferinPup »

Oh strange, thats the opposite of what I'm seeing in RC1. I'll be on the lookout for RC2! I attached some shots.
IMG_0034.PNG
IMG_0035.PNG
Great work on 1.8 though, it is super cool playing with compositors on the device.
User avatar
masterfalcon
OGRE Team Member
OGRE Team Member
Posts: 4270
Joined: Sun Feb 25, 2007 4:56 am
Location: Bloomington, MN
x 126
Contact:

Re: Does Iphone support shadow

Post by masterfalcon »

I should fix that. It's actually deceiving you. Texture shadows are actually being forced without updating the menu.
SufferinPup
Greenskin
Posts: 119
Joined: Tue Mar 02, 2010 11:09 pm
x 2

Re: Does Iphone support shadow

Post by SufferinPup »

Ahah yeah after playing with it more I see what you're saying. Very confusing, sorry! The compositor demo also uses texture shadows, if anyone else wants to see them in action elsewhere. :) I don't know if stencil shadows would be a win or worth spending time fixing, but I'm certainly curious about their performance vs. texture shadows on the various hardware out there.
Post Reply