[SOLVED] Instancing works only with robot.mesh

Problems building or running the engine, queries about how to use features etc.
Post Reply
Hilarius86
Halfling
Posts: 48
Joined: Thu Feb 14, 2019 11:27 am
x 8

[SOLVED] Instancing works only with robot.mesh

Post by Hilarius86 »

Ogre Version: :1.11.5:
Operating System: :Win10x64:
Render System: :DirectX11:

Hi,

what do I have to do to make instancing work with my own model, when it is working with the robot.mesh?

I tried to get instancing working on DirectX11. I started from the sample browser's New Instancing part. It was broken because it tried to work on tangents which are not present in the model. After I changed that, I was able to use the sample.

Code: Select all

InstanceManager* inMa = scnMgr->createInstanceManager("instanceManager", "robot.mesh", RGN_DEFAULT, InstanceManager::InstancingTechnique::TextureVTF, 4, InstanceManagerFlags::IM_VTFBESTFIT);

std::vector<InstancedEntity*> InstancedEntityList;

int mesh_x = 2;
int nrOfMeshes = mesh_x * mesh_x;
for (int i = 0; i < nrOfMeshes; i++)
{
  InstancedEntity* E_robot = scnMgr->createInstancedEntity("Examples/Instancing/VTF/Robot", "instanceManager");
  InstancedEntityList.push_back(E_robot);
  
  NodeList.push_back(scnMgr->getRootSceneNode()->createChildSceneNode());

  NodeList[i]->attachObject(InstancedEntityList[i]);
  NodeList[i]->setPosition(j, 0, k); // j and k get defined by a loop and result in coordinates (-4 0 -2, 0 0 -2, -4 0 0, 0 0 0)
}
I now want to change the model. For easy reproduction lets use the cube.mesh. Next 4 blocks are my material defintion with vertex_programm, fragment_programm and both shaders in HLSL. It's basically the version from the sample minus shadows.

Code: Select all

vertex_program Ogre/Instancing/VTF_hlsl_vs hlsl
{
	source VTFInstancing.hlsl
	entry_point main_vs_tata
	target vs_4_0 vs_4_0_level_9_1 vs_4_0_level_9_3
	uses_vertex_texture_fetch true
}


vertex_program Ogre/Instancing/VTF_vs unified
{
	delegate Ogre/Instancing/VTF_hlsl_vs

	default_params
	{
		param_named_auto	viewProjMatrix				viewproj_matrix
	}
}
abstract material Examples/Instancing/VTF
{
	technique
	{
		shadow_caster_material Examples/Instancing/VTF/shadow_caster

		pass
		{
			vertex_program_ref Ogre/Instancing/VTF_vs
			{
			}

			fragment_program_ref Ogre/Instancing_ps
			{
			}

			texture_unit Diffuse
			{
				texture_alias		DiffuseMap
			}

			texture_unit InstancingVTF
			{
				binding_type	vertex
				filtering		none
			}
		}
	}
}

material Examples/Instancing/VTF/Robot : Examples/Instancing/VTF
{
	set_texture_alias	DiffuseMap	r2skin.jpg
}

Code: Select all

fragment_program Ogre/Instancing_hlsl_ps hlsl
{
	source Instancing_ps.hlsl
	entry_point main_ps_tata
	target ps_4_0 ps_4_0_level_9_1 ps_4_0_level_9_3
}

fragment_program Ogre/Instancing_ps unified
{
	delegate Ogre/Instancing_hlsl_ps
}

Code: Select all

struct VS_INPUT
{
	float4 Position	:	POSITION;
	float3 Normal	:	NORMAL;

	float2 uv0		:	TEXCOORD0;
	float4 m01		:	TEXCOORD1;
	float4 m23		:	TEXCOORD2;
};

struct PS_INPUT
{
	float2 uv0		:	TEXCOORD0;
	float3 Normal	:	TEXCOORD1;
	float3 vPos		:	TEXCOORD2;
};

struct VS_OUTPUT
{
	float4 Position	:	POSITION;
	PS_INPUT	ps;
};

#define LOD 0
SamplerState SampleType;

VS_OUTPUT main_vs_tata( VS_INPUT input,
				   uniform float4x4 viewProjMatrix,
				   uniform Texture2D matrixTexture : register(t1)
				)
{
	VS_OUTPUT output;

	float4 worldPos	 = 0;
	float3 worldNorm = 0;


	float3x4 worldMatrix;
	worldMatrix[0] = matrixTexture.SampleLevel(SampleType, float2(input.m01.xy), LOD );
	worldMatrix[1] = matrixTexture.SampleLevel(SampleType, input.m01.zw, LOD );
	worldMatrix[2] = matrixTexture.SampleLevel(SampleType, input.m23.xy, LOD );

	worldPos = float4( mul( worldMatrix, input.Position ).xyz, 1.0f );
	worldNorm= mul( (float3x3)(worldMatrix), input.Normal );

	//Transform the position
	output.Position		= mul( viewProjMatrix, worldPos );

	output.ps.uv0		= input.uv0;

	//Pass Normal and position for Blinn Phong lighting
	output.ps.Normal	= normalize(worldNorm);
	output.ps.vPos		= worldPos.xyz;

	return output;
}

Code: Select all

struct VS_INPUT
{
	float4 Position	:	POSITION;
	float3 Normal	:	NORMAL;

	float2 uv0		:	TEXCOORD0;
	float4 m01		:	TEXCOORD1;
	float4 m23		:	TEXCOORD2;
};

struct PS_INPUT
{
	float2 uv0		:	TEXCOORD0;
	float3 Normal	:	TEXCOORD1;
	float3 vPos		:	TEXCOORD2;
};

struct VS_OUTPUT
{
	float4 Position	:	POSITION;
	PS_INPUT	ps;
};

#define LOD 0
SamplerState SampleType;

VS_OUTPUT main_vs_tata( VS_INPUT input,
				   uniform float4x4 viewProjMatrix,
				   uniform Texture2D matrixTexture : register(t1)
				)
{
	VS_OUTPUT output;

	float4 worldPos	 = 0;
	float3 worldNorm = 0;


	float3x4 worldMatrix;
	worldMatrix[0] = matrixTexture.SampleLevel(SampleType, float2(input.m01.xy), LOD );
	worldMatrix[1] = matrixTexture.SampleLevel(SampleType, input.m01.zw, LOD );
	worldMatrix[2] = matrixTexture.SampleLevel(SampleType, input.m23.xy, LOD );

	worldPos = float4( mul( worldMatrix, input.Position ).xyz, 1.0f );
	worldNorm= mul( (float3x3)(worldMatrix), input.Normal );

	//Transform the position
	output.Position		= mul( viewProjMatrix, worldPos );

	output.ps.uv0		= input.uv0;

	//Pass Normal and position for Blinn Phong lighting
	output.ps.Normal	= normalize(worldNorm);
	output.ps.vPos		= worldPos.xyz;

	return output;
}
I even connected Nsight and used it to my (limited) knowledge. I looked into the Texture2D which is filled with the information for the scenenodes when running the robots, but empty when running the cubes. Any ideas? (216 lines for 3 coordinates, 18 bones and 4 meshes (==> 216), but all zeros when running cubes (12 lines a 4 zeros ==> guessing its 3 coordinates and 4 meshes for a single "bone", so size seems reasonable).

Code: Select all

red green blue alpha
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 0.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 0.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
0.0100 0.0000 0.0000 -4.0000
0.0000 0.0100 0.0000 0.0000
0.0000 0.0000 0.0100 -2.0000
Regards
Hilarius86
Last edited by Hilarius86 on Tue Jul 21, 2020 8:49 am, edited 1 time in total.
Hilarius86
Halfling
Posts: 48
Joined: Thu Feb 14, 2019 11:27 am
x 8

Re: Instancing works only with robot.mesh

Post by Hilarius86 »

I was able to produce the call stack, which is generating the position buffer for the robots. As assumed the line is not reached with the cube.mesh.

Image

Any ideas as to why that happens are still welcome.
Hilarius86
Halfling
Posts: 48
Joined: Thu Feb 14, 2019 11:27 am
x 8

Re: Instancing works only with robot.mesh

Post by Hilarius86 »

So the buffer is updated in OgreInstanceBatchVTF.cpp

Code: Select all

void BaseInstanceBatchVTF::_updateRenderQueue(RenderQueue* queue)
{
    InstanceBatch::_updateRenderQueue( queue );

    if( mBoundsUpdated || mDirtyAnimation || mManager->getCameraRelativeRendering() )
        updateVertexTexture(); // HERE

    mBoundsUpdated = false;
}
but only if the bounds are updated, the animation is dirty or if the world revolves around the camera. For the robot, the animation is dirty and therefore everything is setup correctly, but why are the bounds not updated when creating new InstancedEntities, attaching them to SceneNodes and moving the nodes?

In all the sources mBoundsUpdated is never set to true.

In OgreInstanceBatch.cpp the bold line is the solution for me.

Code: Select all

void InstanceBatch::_updateBounds(void)
{
  mFullBoundingBox.setNull();

  InstancedEntityVec::const_iterator itor = mInstancedEntities.begin();
  InstancedEntityVec::const_iterator end  = mInstancedEntities.end();

  Real maxScale = 0;
  while( itor != end )
  {
    InstancedEntity* ent = (*itor);
    //Only increase the bounding box for those objects we know are in the scene
    if( ent->isInScene() )
    {
      maxScale = std::max(maxScale, ent->getMaxScaleCoef());
      mFullBoundingBox.merge( ent->_getDerivedPosition() );
    }

    ++itor;
  }

  Real addToBound = maxScale * _getMeshReference()->getBoundingSphereRadius();
  mFullBoundingBox.setMaximum(mFullBoundingBox.getMaximum() + addToBound);
  mFullBoundingBox.setMinimum(mFullBoundingBox.getMinimum() - addToBound);
	

  mBoundingRadius = Math::boundingRadiusFromAABBCentered( mFullBoundingBox );
  if (mParentNode)
  {
    mParentNode->needUpdate();
  }
  mBoundsUpdated  = true; // HERE
  mBoundsDirty    = false;
}

Edit: No bold in Code, used comment.
paroj
OGRE Team Member
OGRE Team Member
Posts: 1994
Joined: Sun Mar 30, 2014 2:51 pm
x 1074
Contact:

Re: Instancing works only with robot.mesh

Post by paroj »

thanks for investigating this. Could you provide us a pull-request with your fixes?
https://github.com/OGRECave/ogre/pulls
Hilarius86
Halfling
Posts: 48
Joined: Thu Feb 14, 2019 11:27 am
x 8

Re: Instancing works only with robot.mesh

Post by Hilarius86 »

PR is out.
Post Reply