Skip to content

Commit

Permalink
Change: Minor caching for some shader matrix operations
Browse files Browse the repository at this point in the history
  • Loading branch information
leezer3 committed Jul 20, 2021
1 parent a792af9 commit 304df79
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions source/LibRender2/BaseRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,13 @@ public void RestoreAlphaFunc()
}
}


// Cached object state and matricies for shader drawing
private ObjectState lastObjectState;
private Matrix4D lastModelMatrix;
private Matrix4D lastModelViewMatrix;
private bool sendToShader;

/// <summary>Draws a face using the current shader</summary>
/// <param name="State">The FaceState to draw</param>
/// <param name="IsDebugTouchMode">Whether debug touch mode</param>
Expand All @@ -974,19 +981,35 @@ public void RenderFace(FaceState State, bool IsDebugTouchMode = false)
RenderFace(CurrentShader, State.Object, State.Face, IsDebugTouchMode);
}

/// <summary>Draws a face using the specified shader and matricies</summary>
/// <param name="Shader">The shader to use</param>
/// <param name="State">The ObjectState to draw</param>
/// <param name="Face">The Face within the ObjectState</param>
/// <param name="ModelMatrix">The model matrix to use</param>
/// <param name="ModelViewMatrix">The modelview matrix to use</param>
public void RenderFace(Shader Shader, ObjectState State, MeshFace Face, Matrix4D ModelMatrix, Matrix4D ModelViewMatrix)
{
lastModelMatrix = ModelMatrix;
lastModelViewMatrix = ModelViewMatrix;
sendToShader = true;
RenderFace(Shader, State, Face);
}

/// <summary>Draws a face using the specified shader</summary>
/// <param name="Shader">The shader to use</param>
/// <param name="State">The FaceState to draw</param>
/// <param name="State">The ObjectState to draw</param>
/// <param name="Face">The Face within the ObjectState</param>
/// <param name="IsDebugTouchMode">Whether debug touch mode</param>
public void RenderFace(Shader Shader, ObjectState State, MeshFace Face, bool IsDebugTouchMode = false)
{
Matrix4D modelMatrix = State.ModelMatrix * Camera.TranslationMatrix;
Matrix4D modelViewMatrix = modelMatrix * CurrentViewMatrix;
RenderFace(Shader, State, Face, modelMatrix, modelViewMatrix, IsDebugTouchMode);
}
if (State != lastObjectState && !sendToShader)
{
lastObjectState = State;
lastModelMatrix = State.ModelMatrix * Camera.TranslationMatrix;
lastModelViewMatrix = lastModelMatrix * CurrentViewMatrix;
sendToShader = true;
}

public void RenderFace(Shader Shader, ObjectState State, MeshFace Face, Matrix4D modelMatrix, Matrix4D modelViewMatrix, bool IsDebugTouchMode = false)
{
if (State.Prototype.Mesh.Vertices.Length < 1)
{
return;
Expand Down Expand Up @@ -1014,9 +1037,13 @@ public void RenderFace(Shader Shader, ObjectState State, MeshFace Face, Matrix4D
}

// matrix
Shader.SetCurrentModelViewMatrix(modelViewMatrix);
Shader.SetCurrentTextureMatrix(State.TextureTranslation);

if (sendToShader)
{
Shader.SetCurrentModelViewMatrix(lastModelViewMatrix);
Shader.SetCurrentTextureMatrix(State.TextureTranslation);
sendToShader = false;
}

if (OptionWireFrame || IsDebugTouchMode)
{
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
Expand Down Expand Up @@ -1074,7 +1101,7 @@ public void RenderFace(Shader Shader, ObjectState State, MeshFace Face, Matrix4D
float distanceFactor;
if (material.GlowAttenuationData != 0)
{
distanceFactor = (float)Glow.GetDistanceFactor(modelMatrix, State.Prototype.Mesh.Vertices, ref Face, material.GlowAttenuationData);
distanceFactor = (float)Glow.GetDistanceFactor(lastModelMatrix, State.Prototype.Mesh.Vertices, ref Face, material.GlowAttenuationData);
}
else
{
Expand Down

0 comments on commit 304df79

Please sign in to comment.