Skip to content

RenderState: Setters Missing Hash Invalidation #2593

@capdevon

Description

@capdevon

The following fields are part of the contentHashCode() computation:

hash = 79 * hash + this.frontStencilMask;
hash = 79 * hash + this.frontStencilReference;
hash = 79 * hash + this.backStencilMask;
hash = 79 * hash + this.backStencilReference;

However, their corresponding setters do not set cachedHashCode = -1:

public void setFrontStencilMask(int frontStencilMask) {
    this.frontStencilMask = frontStencilMask;
}

public void setBackStencilMask(int backStencilMask) {
    this.backStencilMask = backStencilMask;
}

public void setFrontStencilReference(int frontStencilReference) {
    this.frontStencilReference = frontStencilReference;
}

public void setBackStencilReference(int backStencilReference) {
    this.backStencilReference = backStencilReference;
}

Since these fields directly contribute to the hash, modifying them without invalidating the cached hash results in an inconsistent RenderState.

Expected Fix
Each setter should follow the same pattern used in correctly implemented methods such as setColorWrite:

public void setColorWrite(boolean colorWrite) {
    applyColorWrite = true;
    this.colorWrite = colorWrite;
    cachedHashCode = -1;
}

This ensures that contentHashCode() always reflects the actual state of the object.

Edit:
Additionally, RenderState.flipFaceCull() should be updated to call RenderState.setFaceCullMode(FaceCullMode) so that cull changes also invalidate the cached hash.

    /**
     * Toggles the current face culling mode between {@code Front} and {@code Back}.
     * <p>
     * This method inverts the culling direction: {@code Back → Front} and
     * {@code Front → Back}. Other modes ({@code Off} and {@code FrontAndBack})
     * are left unchanged.
     * <p>
     * Useful when geometry is discovered to have reversed normals and the
     * culling orientation must be corrected at runtime.
     */
    public void flipFaceCull() {
        switch (cullMode) {
            case Back:
                cullMode = FaceCullMode.Front;
                break;
            case Front:
                cullMode = FaceCullMode.Back;
                break;
        }
    }

Metadata

Metadata

Labels

Easy first fixdefectSomething that is supposed to work, but doesn't. Less severe than a "bug"

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions