Class AdaptiveTessellationController

java.lang.Object
eu.svjatoslav.sixth.e3d.renderer.raster.tessellation.AdaptiveTessellationController

public class AdaptiveTessellationController extends Object
Controls adaptive tessellation thresholds based on polygon budget.

This controller dynamically adjusts the screen-space tessellation threshold to keep the number of tessellated polygons approximately within a target budget. When polygon count exceeds the budget, the threshold is increased (less tessellation, larger polygons). When below budget and threshold is above minimum, it's decreased (better quality, smaller polygons).

Frame lifecycle:

  1. resetFrameCount() called at frame start to clear counter
  2. addTessellatedPolygons(int) called during transform for each tessellated polygon
  3. endFrame() called after painting to adjust threshold based on count

Adjustment logic:

  • If polygonCount exceeds TARGET: increase threshold by INCREASE_FACTOR
  • If polygonCount is below 80% of TARGET and threshold above MIN: decrease threshold
  • Threshold is clamped to MIN_THRESHOLD as the lower bound

Thread safety: Uses volatile for threshold and AtomicInteger for count. Safe for multi-threaded rendering pipelines.

See Also:
  • Field Details

    • MIN_THRESHOLD

      public static final double MIN_THRESHOLD
      Minimum tessellation threshold in pixels. This is the base value from TexturedTriangle.TESSELLATION_THRESHOLD_PIXELS. Threshold will never go below this.
      See Also:
    • TARGET_POLYGON_COUNT

      public static final int TARGET_POLYGON_COUNT
      Target polygon count for tessellated triangles. The controller tries to keep actual count close to this value.
      See Also:
  • Constructor Details

    • AdaptiveTessellationController

      public AdaptiveTessellationController()
  • Method Details

    • getInstance

      public static AdaptiveTessellationController getInstance()
      Returns the singleton instance.
      Returns:
      the adaptive tessellation controller
    • getThreshold

      public double getThreshold()
      Returns the current tessellation threshold in screen pixels.

      This value is read by tessellators during the transform phase to determine when to subdivide triangles.

      Returns:
      the current threshold in pixels
    • resetFrameCount

      public void resetFrameCount()
      Resets the polygon counter at the start of a new frame.

      Called from ShapeCollection.transformShapes() before processing shapes.

    • addTessellatedPolygons

      public void addTessellatedPolygons(int count)
      Adds tessellated polygons to the current frame's count.

      Called from TexturedTriangle.transform() after tessellation completes, with the number of sub-triangles produced.

      Parameters:
      count - the number of tessellated polygons to add
    • endFrame

      public boolean endFrame()
      Called at the end of each frame to adjust threshold based on polygon count.

      Adjustment rules:

      • Adjustment is proportional to how far count is from target (smoother convergence)
      • Minimum change per frame is 2% to ensure gradual adjustment
      • Maximum change per frame is 10% to prevent sudden jumps
      • Threshold is always clamped between MIN_THRESHOLD and MAX_THRESHOLD
      Returns:
      true if threshold changed significantly (should invalidate caches)
    • getLastPolygonCount

      public int getLastPolygonCount()
      Returns the polygon count from the last frame.
      Returns:
      the last frame's polygon count
    • getCurrentFrameCount

      public int getCurrentFrameCount()
      Returns the current frame's polygon count (so far).
      Returns:
      the current frame's polygon count
    • reset

      public void reset()
      Resets the threshold to the minimum value and clears counters.

      Useful when starting a new scene or resetting renderer state.