Optimizing texture swaps

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Optimizing texture swaps

Post by Ats »

Hi, I remember Kjell warned me about texture swaps a while back: http://www.emix8.org/forum/viewtopic.php?p=8719#p8719
So I was wondering if it is worth keeping track of the current material in order to avoid @UseMaterial if it hasn't changed. Maybe it will speed up the rendering of my explosions?

I made a strip down example of it:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZLibrary>
      <Source>
<![CDATA[void setMaterial(int m)
{
  currentMaterial = m;
  switch(m)
  {
    case 1: @UseMaterial(Material: Material1); return;
    case 2: @UseMaterial(Material: Material2); return;
  }
}]]>
      </Source>
    </ZLibrary>
    <ZExpression>
      <Expression>
<![CDATA[for(int i=0; i<8; i++)
{
  Dummy.Position.X = random(0, 8);
  Dummy.Position.Y = random(0, 4);
  Dummy.n = 1;
  createModel(Dummy);
}
for(int j=0; j<8; j++)
{
  Dummy.Position.X = random(0, 8);
  Dummy.Position.Y = random(0, 4);
  Dummy.n = 2;
  createModel(Dummy);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="Dummy" Position="6.2233 -0.5997 0">
      <Definitions>
        <Variable Name="n" Type="1"/>
      </Definitions>
      <OnRender>
        <ZExpression>
          <Expression>
<![CDATA[if (currentMaterial != CurrentModel.n) SetMaterial(CurrentModel.n);
@RenderMesh(Mesh: CubeMesh);]]>
          </Expression>
        </ZExpression>
      </OnRender>
    </Model>
    <Mesh Name="CubeMesh">
      <Producers>
        <MeshBox/>
      </Producers>
    </Mesh>
    <Material Name="Material1">
      <Textures>
        <MaterialTexture Texture="Bitmap1" TexCoords="1"/>
      </Textures>
    </Material>
    <Material Name="Material2" Color="1 0 0 1"/>
    <Bitmap Name="Bitmap1">
      <Producers>
        <BitmapNoise/>
      </Producers>
    </Bitmap>
    <Variable Name="CurrentMaterial" Type="1"/>
  </Content>
</ZApplication>
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Optimizing texture swaps

Post by Kjell »

Hi Ats,
Ats wrote: Sun Feb 13, 2022 6:19 pmI was wondering if it is worth keeping track of the current material in order to avoid @UseMaterial if it hasn't changed.
No need for that. When you call UseMaterial it automatically checks whether the material is already active or not .. you can see the code for this here.

What helps in terms of performance is to make sure that models with the same material get rendered in succession of each other ( as much as possible ). So if you have 3 coins and 3 mushrooms you want to render them in the order of "coin coin coin mushroom mushroom mushroom" ( best case ) and not "coin mushroom coin mushroom coin mushroom" ( worst case ). One easy way to ensure this is to use a unique Model.Category for the coin and another for the mushroom model.

K
User avatar
VilleK
Site Admin
Posts: 2274
Joined: Mon Jan 15, 2007 4:50 pm
Location: Stockholm, Sweden
Contact:

Re: Optimizing texture swaps

Post by VilleK »

Exactly what Kjell said. He is the true ZGE guru :)
Post Reply