Skip to content

Commit 01ea578

Browse files
committed
URenderPipeline: Fix rendering being affected by global glColor state
The `glColor` state is initially `1f, 1f, 1f, 1f`, but other code (e.g. until 1.14 the vanilla FontRenderer) may change it and if it doesn't change it back afterwards, users of `URenderPipeline` may not get the expected render result (anything which doesn't explicitly use per-vertex colors will implicitly get colored with whatever glColor was last set). The chance of some user of `URenderPipeline` intentionally relying on this global state leaking in feels sufficiently low, that I don't think any backwards compatibility needs to be maintained for it. I don't think it is even necessary to make this behavior configurable, since coloring is ordinarily just done via the per-vertex color. And modern OpenGL doesn't support it anyway.
1 parent 5f02456 commit 01ea578

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/main/kotlin/gg/essential/universal/render/ManagedGlState.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gg.essential.universal.render
33

44
import gg.essential.universal.UGraphics
55
import gg.essential.universal.shader.BlendState
6+
import org.lwjgl.BufferUtils
67
import org.lwjgl.opengl.GL11
78
import java.nio.ByteBuffer
89

@@ -27,6 +28,10 @@ internal class ManagedGlState(
2728
var alphaTest: Boolean,
2829
var alphaTestFunc: Int,
2930
var alphaTestRef: Float,
31+
var colorR: Float,
32+
var colorG: Float,
33+
var colorB: Float,
34+
var colorA: Float,
3035
val texture2DStates: MutableList<Boolean>,
3136
) {
3237
constructor(other: ManagedGlState) : this(
@@ -45,6 +50,10 @@ internal class ManagedGlState(
4550
alphaTest = other.alphaTest,
4651
alphaTestFunc = other.alphaTestFunc,
4752
alphaTestRef = other.alphaTestRef,
53+
colorR = other.colorR,
54+
colorG = other.colorG,
55+
colorB = other.colorB,
56+
colorA = other.colorA,
4857
texture2DStates = other.texture2DStates.toMutableList(),
4958
)
5059

@@ -184,6 +193,13 @@ internal class ManagedGlState(
184193
//#endif
185194
GlStateManager.alphaFunc(alphaTestFunc, alphaTestRef)
186195
}
196+
if (curr.colorR != colorR || curr.colorG != colorG || curr.colorB != colorB && curr.colorA != colorA) {
197+
curr.colorR = colorR
198+
curr.colorG = colorG
199+
curr.colorB = colorB
200+
curr.colorA = colorA
201+
GlStateManager.color(colorR, colorG, colorB, colorA)
202+
}
187203
for ((index, wantEnabled) in texture2DStates.withIndex()) {
188204
val isEnabled = curr.texture2DStates.getOrNull(index)
189205
if (isEnabled == wantEnabled) continue
@@ -222,17 +238,33 @@ internal class ManagedGlState(
222238
//$$ alphaTest = false,
223239
//$$ alphaTestFunc = 0,
224240
//$$ alphaTestRef = 0f,
241+
//$$ colorR = 0f,
242+
//$$ colorG = 0f,
243+
//$$ colorB = 0f,
244+
//$$ colorA = 0f,
225245
//#else
226246
shadeModel = GL11.glGetInteger(GL11.GL_SHADE_MODEL),
227247
alphaTest = GL11.glGetBoolean(GL11.GL_ALPHA_TEST),
228248
alphaTestFunc = GL11.glGetInteger(GL11.GL_ALPHA_TEST_FUNC),
229249
alphaTestRef = GL11.glGetFloat(GL11.GL_ALPHA_TEST_REF),
250+
colorR = run {
251+
//#if MC>=11600
252+
//$$ GL11.glGetFloatv(GL11.GL_CURRENT_COLOR, tmpFloatBuffer)
253+
//#else
254+
GL11.glGetFloat(GL11.GL_CURRENT_COLOR, tmpFloatBuffer)
255+
//#endif
256+
tmpFloatBuffer.get(0)
257+
},
258+
colorG = tmpFloatBuffer.get(1),
259+
colorB = tmpFloatBuffer.get(2),
260+
colorA = tmpFloatBuffer.get(3),
230261
//#endif
231262
texture2DStates = mutableListOf(), // populated on demand
232263
)
233264

234265
// Note: LWJGL2 requires a buffer of 16 elements, even if the properties we query have fewer
235266
private val tmpByteBuffer = ByteBuffer.allocateDirect(16)
267+
private val tmpFloatBuffer = BufferUtils.createFloatBuffer(16)
236268

237269
private fun glGetBooleans(param: Int, count: Int) =
238270
tmpByteBuffer

src/main/kotlin/gg/essential/universal/render/URenderPipeline.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ class URenderPipeline private constructor(
492492
alphaTest = true,
493493
alphaTestFunc = GL11.GL_ALWAYS,
494494
alphaTestRef = 0f,
495+
colorR = 1f,
496+
colorG = 1f,
497+
colorB = 1f,
498+
colorA = 1f,
495499
//#if MC>=11700
496500
//$$ texture2DStates = mutableListOf(),
497501
//#else

0 commit comments

Comments
 (0)