From cf454f287c80ac6d3a44a80483d743a646f4095c Mon Sep 17 00:00:00 2001 From: Jason Mitchell Date: Fri, 9 Feb 2024 17:35:58 -0800 Subject: [PATCH] GLSM Improvements (#272) * GLSM Improvements * Add an IntegerStateStack * Use IntegerStateStack to track the active texture unit * Track activeTextureUnit with GL_TEXTURE_BIT * Bugfix - Don't include color with GL_COLOR_BUFFER_BIT * Fully test glPush/PopAttrib for GL_COLOR_BUFFER_BIT * Split GLStateManager init into two pieces --- addon.gradle | 14 ++ dependencies.gradle | 12 ++ gradle.properties | 9 +- settings.gradle | 3 +- .../gtnewhorizons/angelica/glsm/Feature.java | 10 +- .../angelica/glsm/GLStateManager.java | 40 +++-- .../angelica/glsm/stacks/Color4Stack.java | 5 + .../glsm/stacks/IntegerStateStack.java | 42 +++++ .../angelica/glsm/states/IntegerState.java | 26 +++ .../angelica/hudcaching/HUDCaching.java | 22 +-- .../startup/MixinInitGLStateManager.java | 1 + .../angelica/proxy/ClientProxy.java | 1 + .../gtnewhorizons/angelica/GLSM_UnitTest.java | 161 ++++++++++++++++++ 13 files changed, 310 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/gtnewhorizons/angelica/glsm/stacks/IntegerStateStack.java create mode 100644 src/main/java/com/gtnewhorizons/angelica/glsm/states/IntegerState.java create mode 100644 src/test/java/com/gtnewhorizons/angelica/GLSM_UnitTest.java diff --git a/addon.gradle b/addon.gradle index 58cadb25b..8dc8db4db 100644 --- a/addon.gradle +++ b/addon.gradle @@ -1,3 +1,17 @@ + +test { + useJUnitPlatform() + testLogging { + events "passed", "skipped", "failed" + } + javaLauncher = javaToolchains.launcherFor { + languageVersion = JavaLanguageVersion.of(8) + } + dependsOn("extractNatives2") + jvmArgs += "-Djava.library.path=${tasks.extractNatives2.destinationFolder.asFile.get().path}" +} + + allprojects { gradle.projectsEvaluated { tasks.withType(JavaCompile) { diff --git a/dependencies.gradle b/dependencies.gradle index 1de51bc0b..06c4b6fe9 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -92,4 +92,16 @@ dependencies { // Hodgepodge transformedMod("net.industrial-craft:industrialcraft-2:2.2.828-experimental:dev") + + + // Tests + testImplementation(platform('org.junit:junit-bom:5.9.2')) + testImplementation('org.junit.jupiter:junit-jupiter') + testImplementation("org.mockito:mockito-core:3.+") + testImplementation("org.lwjgl.lwjgl:lwjgl:2.9.4-nightly-20150209") +} + +task copyDependencies(type: Copy) { + from configurations.default + into 'testDependencies' } diff --git a/gradle.properties b/gradle.properties index 367108307..387b823c7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -33,7 +33,7 @@ channel = stable mappingsVersion = 12 # Defines other MCP mappings for dependency deobfuscation. -remoteMappings = https://raw.githubusercontent.com/MinecraftForge/FML/1.7.10/conf/ +remoteMappings = https\://raw.githubusercontent.com/MinecraftForge/FML/1.7.10/conf/ # Select a default username for testing your mod. You can always override this per-run by running # `./gradlew runClient --username=AnotherPlayer`, or configuring this command in your IDE. @@ -61,6 +61,9 @@ gradleTokenModId = # [DEPRECATED] Mod name replacement token. gradleTokenModName = +# [DEPRECATED] Mod Group replacement token. +gradleTokenGroupName = + # [DEPRECATED] # Multiple source files can be defined here by providing a comma-separated list: Class1.java,Class2.java,Class3.java # public static final String VERSION = "GRADLETOKEN_VERSION"; @@ -123,7 +126,7 @@ includeWellKnownRepositories = true usesMavenPublishing = true # Maven repository to publish the mod to. -# mavenPublishUrl = https://nexus.gtnewhorizons.com/repository/releases/ +# mavenPublishUrl = https\://nexus.gtnewhorizons.com/repository/releases/ # Publishing to Modrinth requires you to set the MODRINTH_TOKEN environment variable to your current Modrinth API token. # @@ -187,5 +190,3 @@ disableSpotless = true # This is meant to be set in $HOME/.gradle/gradle.properties. # ideaCheckSpotlessOnBuild = true -# Non-GTNH properties -gradleTokenGroupName = diff --git a/settings.gradle b/settings.gradle index a573141aa..9238315cc 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,3 @@ - pluginManagement { repositories { maven { @@ -17,5 +16,5 @@ pluginManagement { } plugins { - id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.9' + id 'com.gtnewhorizons.gtnhsettingsconvention' version '1.0.12' } diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java b/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java index a64463b04..cc0eb41a7 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/Feature.java @@ -50,9 +50,8 @@ public class Feature { // GL_COLOR_LOGIC_OP enable bit // GL_INDEX_LOGIC_OP enable bit // Logic op function - , GLStateManager.colorMask // Color mode and index mode writemasks - , GLStateManager.color // Color mode and index mode writemasks - , GLStateManager.clearColor // Color mode and index mode clear values + , GLStateManager.colorMask // Color-mode and index-mode writemasks + , GLStateManager.clearColor // Color-mode and index-mode clear values )); attribToFeatures.put(GL11.GL_CURRENT_BIT, ImmutableSet.of( GLStateManager.color // Current RGBA color @@ -219,11 +218,14 @@ public class Feature { // Stencil buffer writemask )); final Set> textureAttribs = new HashSet<>(ImmutableSet.of( + GLStateManager.activeTextureUnit // Active texture unit // Enable bits for the four texture coordinates + // Border color for each texture image // Minification function for each texture image // Magnification function for each texture image // Texture coordinates and wrap mode for each texture image + // Color and mode for each texture environment // Enable bits GL_TEXTURE_GEN_x, x is S, T, R, and Q // GL_TEXTURE_GEN_MODE setting for S, T, R, and Q @@ -234,9 +236,11 @@ public class Feature { // Current Texture Bindings - GL_TEXTURE_BINDING_2D for(int i = 0 ; i < GLStateManager.MAX_TEXTURE_UNITS; i++) { textureAttribs.add(GLStateManager.textures.getTextureUnitBindings(i)); +// textureAttribs.add(GLStateManager.textures.getInfo(i)) } attribToFeatures.put(GL11.GL_TEXTURE_BIT, textureAttribs); + attribToFeatures.put(GL11.GL_TRANSFORM_BIT, ImmutableSet.of( // Coefficients of the six clipping planes // Enable bits for the user-definable clipping planes diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java index 3f2f51675..68df331e3 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/GLStateManager.java @@ -2,6 +2,7 @@ package com.gtnewhorizons.angelica.glsm; import com.gtnewhorizons.angelica.AngelicaMod; import com.gtnewhorizons.angelica.config.AngelicaConfig; +import com.gtnewhorizons.angelica.glsm.debug.OpenGLDebugging; import com.gtnewhorizons.angelica.glsm.stacks.AlphaStateStack; import com.gtnewhorizons.angelica.glsm.stacks.BlendStateStack; import com.gtnewhorizons.angelica.glsm.stacks.BooleanStateStack; @@ -10,6 +11,7 @@ import com.gtnewhorizons.angelica.glsm.stacks.ColorMaskStack; import com.gtnewhorizons.angelica.glsm.stacks.DepthStateStack; import com.gtnewhorizons.angelica.glsm.stacks.FogStateStack; import com.gtnewhorizons.angelica.glsm.stacks.IStateStack; +import com.gtnewhorizons.angelica.glsm.stacks.IntegerStateStack; import com.gtnewhorizons.angelica.glsm.stacks.MatrixModeStack; import com.gtnewhorizons.angelica.glsm.stacks.ViewPortStateStack; import com.gtnewhorizons.angelica.glsm.states.Color4; @@ -84,9 +86,9 @@ public class GLStateManager { // GLStateManager State Trackers private static final IntStack attribs = new IntArrayList(MAX_ATTRIB_STACK_DEPTH); - private static final IntStack activeTextureUnit = new IntArrayList(MAX_ATTRIB_STACK_DEPTH); + protected static final IntegerStateStack activeTextureUnit = new IntegerStateStack(); static { - activeTextureUnit.push(0); // GL_TEXTURE0 + activeTextureUnit.setValue(0); // GL_TEXTURE0 } @Getter protected static final TextureUnitArray textures = new TextureUnitArray(); @Getter protected static final BlendStateStack blendState = new BlendStateStack(); @@ -99,7 +101,7 @@ public class GLStateManager { @Getter protected static final FogStateStack fogState = new FogStateStack(); @Getter protected static final BooleanStateStack fogMode = new BooleanStateStack(GL11.GL_FOG); @Getter protected static final Color4Stack color = new Color4Stack(); - @Getter protected static final Color4Stack clearColor = new Color4Stack(); + @Getter protected static final Color4Stack clearColor = new Color4Stack(new Color4(0.0F, 0.0F, 0.0F, 0.0F)); @Getter protected static final ColorMaskStack colorMask = new ColorMaskStack(); @Getter protected static final BooleanStateStack cullState = new BooleanStateStack(GL11.GL_CULL_FACE); @Getter protected static final AlphaStateStack alphaState = new AlphaStateStack(); @@ -135,6 +137,8 @@ public class GLStateManager { private static final Map, ISettableState> glListStates = new Object2ObjectArrayMap<>(); private static final Int2ObjectMap, ISettableState>>> glListChanges = new Int2ObjectOpenHashMap<>(); + + public static class GLFeatureSet extends IntOpenHashSet { public GLFeatureSet addFeature(int feature) { super.add(feature); @@ -143,10 +147,11 @@ public class GLStateManager { } - public static void init() { + public static void preInit() { capabilities = GLContext.getCapabilities(); HAS_MULTIPLE_SET .addFeature(GL11.GL_ACCUM_CLEAR_VALUE) + .addFeature(GL14.GL_BLEND_COLOR) .addFeature(GL11.GL_COLOR_CLEAR_VALUE) .addFeature(GL11.GL_COLOR_WRITEMASK) .addFeature(GL11.GL_CURRENT_COLOR) @@ -171,6 +176,10 @@ public class GLStateManager { .addFeature(GL11.GL_TEXTURE_ENV_COLOR) .addFeature(GL11.GL_TEXTURE_MATRIX) .addFeature(GL11.GL_VIEWPORT); + } + + public static void init() { + RenderSystem.initRenderer(); @@ -247,7 +256,7 @@ public class GLStateManager { case GL11.GL_FOG -> fogMode.isEnabled(); case GL11.GL_LIGHTING -> lightingState.isEnabled(); case GL11.GL_SCISSOR_TEST -> scissorTest.isEnabled(); - case GL11.GL_TEXTURE_2D -> textures.getTextureUnitStates(activeTextureUnit.topInt()).isEnabled(); + case GL11.GL_TEXTURE_2D -> textures.getTextureUnitStates(activeTextureUnit.getValue()).isEnabled(); case GL12.GL_RESCALE_NORMAL -> rescaleNormalState.isEnabled(); default -> GL11.glIsEnabled(cap); }; @@ -266,7 +275,7 @@ public class GLStateManager { case GL11.GL_FOG -> fogMode.isEnabled(); case GL11.GL_LIGHTING -> lightingState.isEnabled(); case GL11.GL_SCISSOR_TEST -> scissorTest.isEnabled(); - case GL11.GL_TEXTURE_2D -> textures.getTextureUnitStates(activeTextureUnit.topInt()).isEnabled(); + case GL11.GL_TEXTURE_2D -> textures.getTextureUnitStates(activeTextureUnit.getValue()).isEnabled(); case GL12.GL_RESCALE_NORMAL -> rescaleNormalState.isEnabled(); default -> GL11.glGetBoolean(pname); }; @@ -342,8 +351,9 @@ public class GLStateManager { switch (pname) { case GL11.GL_MODELVIEW_MATRIX -> modelViewMatrix.get(0, params); case GL11.GL_PROJECTION_MATRIX -> projectionMatrix.get(0, params); - case GL11.GL_TEXTURE_MATRIX -> textures.getTextureUnitMatrix(getActiveTextureUnit()).get(0, params); +// case GL11.GL_TEXTURE_MATRIX -> textures.getTextureUnitMatrix(getActiveTextureUnit()).get(0, params); case GL11.GL_COLOR_CLEAR_VALUE -> clearColor.get(params); + case GL11.GL_CURRENT_COLOR -> color.get(params); default -> { if(!HAS_MULTIPLE_SET.contains(pname)) { params.put(0, glGetFloat(pname)); @@ -596,8 +606,7 @@ public class GLStateManager { public static void glActiveTexture(int texture) { final int newTexture = texture - GL13.GL_TEXTURE0; if (shouldBypassCache() || getActiveTextureUnit() != newTexture) { - activeTextureUnit.popInt(); - activeTextureUnit.push(newTexture); + activeTextureUnit.setValue(newTexture); GL13.glActiveTexture(texture); } } @@ -605,14 +614,13 @@ public class GLStateManager { public static void glActiveTextureARB(int texture) { final int newTexture = texture - GL13.GL_TEXTURE0; if (shouldBypassCache() || getActiveTextureUnit() != newTexture) { - activeTextureUnit.popInt(); - activeTextureUnit.push(newTexture); + activeTextureUnit.setValue(newTexture); ARBMultitexture.glActiveTextureARB(texture); } } public static int getBoundTexture() { - return getBoundTexture(activeTextureUnit.topInt()); + return getBoundTexture(activeTextureUnit.getValue()); } public static int getBoundTexture(int unit) { @@ -626,7 +634,7 @@ public class GLStateManager { return; } - final TextureBinding textureUnit = textures.getTextureUnitBindings(GLStateManager.activeTextureUnit.topInt()); + final TextureBinding textureUnit = textures.getTextureUnitBindings(GLStateManager.activeTextureUnit.getValue()); if (shouldBypassCache() || textureUnit.getBinding() != texture) { GL11.glBindTexture(target, texture); @@ -653,7 +661,7 @@ public class GLStateManager { public static void glDeleteTextures(int id) { onDeleteTexture(id); - textures.getTextureUnitBindings(GLStateManager.activeTextureUnit.topInt()).setBinding(-1); + textures.getTextureUnitBindings(GLStateManager.activeTextureUnit.getValue()).setBinding(-1); GL11.glDeleteTextures(id); } @@ -662,7 +670,7 @@ public class GLStateManager { onDeleteTexture(ids.get(i)); } - textures.getTextureUnitBindings(GLStateManager.activeTextureUnit.topInt()).setBinding(-1); + textures.getTextureUnitBindings(GLStateManager.activeTextureUnit.getValue()).setBinding(-1); GL11.glDeleteTextures(ids); } @@ -1081,7 +1089,7 @@ public class GLStateManager { } public static int getActiveTextureUnit() { - return activeTextureUnit.topInt(); + return activeTextureUnit.getValue(); } public static int getListMode() { diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Color4Stack.java b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Color4Stack.java index f33ee52f2..70fdae894 100644 --- a/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Color4Stack.java +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/Color4Stack.java @@ -16,6 +16,11 @@ public class Color4Stack extends Color4 implements IStateStack { } } + public Color4Stack(Color4 color4) { + this(); + set(color4); + } + public Color4Stack push() { if(pointer == stack.length) { throw new IllegalStateException("Stack overflow size " + (pointer + 1) + " reached"); diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/IntegerStateStack.java b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/IntegerStateStack.java new file mode 100644 index 000000000..0dd93121f --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/stacks/IntegerStateStack.java @@ -0,0 +1,42 @@ +package com.gtnewhorizons.angelica.glsm.stacks; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import com.gtnewhorizons.angelica.glsm.states.IntegerState; + +public class IntegerStateStack extends IntegerState implements IStateStack { + + protected final IntegerState[] stack; + + protected int pointer; + + public IntegerStateStack() { + stack = new IntegerState[GLStateManager.MAX_ATTRIB_STACK_DEPTH]; + for (int i = 0; i < GLStateManager.MAX_ATTRIB_STACK_DEPTH; i++) { + stack[i] = new IntegerState(); + } + } + + public IntegerStateStack push() { + if(pointer == stack.length) { + throw new IllegalStateException("Stack overflow size " + (pointer + 1) + " reached"); + } + + stack[pointer++].set(this); + return this; + } + + public IntegerStateStack push(int value) { + push().setValue(value); + return this; + } + + public IntegerStateStack pop() { + if(pointer == 0) { + throw new IllegalStateException("Stack underflow"); + } + + set(stack[--pointer]); + return this; + } + +} diff --git a/src/main/java/com/gtnewhorizons/angelica/glsm/states/IntegerState.java b/src/main/java/com/gtnewhorizons/angelica/glsm/states/IntegerState.java new file mode 100644 index 000000000..5b0dc693e --- /dev/null +++ b/src/main/java/com/gtnewhorizons/angelica/glsm/states/IntegerState.java @@ -0,0 +1,26 @@ +package com.gtnewhorizons.angelica.glsm.states; + +import lombok.Getter; +import lombok.Setter; + +public class IntegerState implements ISettableState { + @Getter @Setter private int value; + + @Override + public IntegerState set(IntegerState state) { + this.value = state.value; + return this; + } + + @Override + public boolean sameAs(Object state) { + if (this == state) return true; + if (!(state instanceof IntegerState integerState)) return false; + return value == integerState.value; + } + + @Override + public IntegerState copy() { + return new IntegerState().set(this); + } +} diff --git a/src/main/java/com/gtnewhorizons/angelica/hudcaching/HUDCaching.java b/src/main/java/com/gtnewhorizons/angelica/hudcaching/HUDCaching.java index 23463c839..c717cf089 100644 --- a/src/main/java/com/gtnewhorizons/angelica/hudcaching/HUDCaching.java +++ b/src/main/java/com/gtnewhorizons/angelica/hudcaching/HUDCaching.java @@ -38,9 +38,9 @@ public class HUDCaching { framebuffer.setFramebufferColor(0, 0, 0, 0); } private static boolean dirty = true; - + public static boolean renderingCacheOverride; - + /* * Some HUD features cause problems/inaccuracies when being rendered into cache. * We capture those and render them later @@ -55,7 +55,7 @@ public class HUDCaching { public static boolean renderCrosshairsCaptured; public static final HUDCaching INSTANCE = new HUDCaching(); - + private HUDCaching() {} @@ -65,11 +65,11 @@ public class HUDCaching { private static boolean isEnabled = true; private static final KeyBinding toggle = new KeyBinding("Toggle HUDCaching", 0, "Debug"); - static { + public static void registerKeyBindings() { ClientRegistry.registerKeyBinding(toggle); } -// @SubscribeEvent + // @SubscribeEvent // public void onFrame(RenderGameOverlayEvent.Post event) { // if (event.type == RenderGameOverlayEvent.ElementType.TEXT) { // final long currentTimeMillis = System.currentTimeMillis(); @@ -121,15 +121,15 @@ public class HUDCaching { } else { renderer.setupOverlayRendering(); } - + ScaledResolution resolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); int width = resolution.getScaledWidth(); int height = resolution.getScaledHeight(); GLStateManager.enableBlend(); - + // reset the color that may be applied by some items GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); - + // render bits that were captured when rendering into cache GuiIngameAccessor gui = (GuiIngameAccessor) ingame; if (renderVignetteCaptured) @@ -138,7 +138,7 @@ public class HUDCaching { } else { GLStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0); } - + if (ingame instanceof GuiIngameForge) { GuiIngameForgeAccessor guiForge = ((GuiIngameForgeAccessor) ingame); if (renderHelmetCaptured) { @@ -168,11 +168,11 @@ public class HUDCaching { GLStateManager.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); framebuffer.bindFramebufferTexture(); drawTexturedRect(tessellator, (float) resolution.getScaledWidth_double(), (float) resolution.getScaledHeight_double()); - + GLStateManager.tryBlendFuncSeparate(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO); mc.getTextureManager().bindTexture(Gui.icons); } - + /** * We are skipping certain render calls when rendering into cache, * however, we cannot skip the GL state changes. This will fix diff --git a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java index f5129724c..4681fce2a 100644 --- a/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java +++ b/src/main/java/com/gtnewhorizons/angelica/mixins/early/angelica/startup/MixinInitGLStateManager.java @@ -11,6 +11,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; public class MixinInitGLStateManager { @Inject(method = "initializeTextures", at = @At("RETURN")) private static void angelica$initializeGLStateManager(CallbackInfo ci) { + GLStateManager.preInit(); GLStateManager.init(); } } diff --git a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java index a514b15f0..d79632dff 100644 --- a/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java +++ b/src/main/java/com/gtnewhorizons/angelica/proxy/ClientProxy.java @@ -70,6 +70,7 @@ public class ClientProxy extends CommonProxy { if(AngelicaConfig.enableHudCaching) { FMLCommonHandler.instance().bus().register(HUDCaching.INSTANCE); MinecraftForge.EVENT_BUS.register(HUDCaching.INSTANCE); // TODO remove debug stuff, unused registration} + HUDCaching.registerKeyBindings(); } if(AngelicaConfig.enableSodium) { MinecraftForge.EVENT_BUS.register(SodiumDebugScreenHandler.INSTANCE); diff --git a/src/test/java/com/gtnewhorizons/angelica/GLSM_UnitTest.java b/src/test/java/com/gtnewhorizons/angelica/GLSM_UnitTest.java new file mode 100644 index 000000000..ca2b9a432 --- /dev/null +++ b/src/test/java/com/gtnewhorizons/angelica/GLSM_UnitTest.java @@ -0,0 +1,161 @@ +package com.gtnewhorizons.angelica; + +import com.gtnewhorizons.angelica.glsm.GLStateManager; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.lwjgl.BufferUtils; +import org.lwjgl.LWJGLException; +import org.lwjgl.opengl.Display; +import org.lwjgl.opengl.DisplayMode; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL14; +import org.lwjgl.opengl.GL20; +import org.lwjgl.opengl.PixelFormat; + +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class GLSM_UnitTest { + private static DisplayMode mode; + @BeforeAll + static void setup() throws LWJGLException { + mode = findDisplayMode(800, 600, Display.getDisplayMode().getBitsPerPixel()); + Display.setDisplayModeAndFullscreen(mode); + final PixelFormat format = new PixelFormat().withDepthBits(24); + Display.create(format); + GLStateManager.preInit(); + } + + @AfterAll + static void cleanup() { + Display.destroy(); + } + + private static DisplayMode findDisplayMode(int width, int height, int bpp) throws LWJGLException { + final DisplayMode[] modes = Display.getAvailableDisplayModes(); + for ( DisplayMode mode : modes ) { + if ( mode.getWidth() == width && mode.getHeight() == height && mode.getBitsPerPixel() >= bpp && mode.getFrequency() <= 60 ) { + return mode; + } + } + return Display.getDesktopDisplayMode(); + } + + void verifyState(int glCap, boolean expected) { + Stream.of(GLStateManager.glIsEnabled(glCap), GL11.glIsEnabled(glCap)).forEach(b -> assertEquals(expected, b)); + } + + void verifyState(int glCap, int expected) { + Stream.of(GLStateManager.glGetInteger(glCap), GL11.glGetInteger(glCap)).forEach(i -> assertEquals(expected, i)); + } + + void verifyState(int glCap, float expected) { + Stream.of(GLStateManager.glGetFloat(glCap), GL11.glGetFloat(glCap)).forEach(f -> assertEquals(expected, f, 0.0001f)); + } + + void verifyState(int glCap, float[] expected) { + final FloatBuffer glBuffer = BufferUtils.createFloatBuffer(16); + final FloatBuffer glsmBuffer = BufferUtils.createFloatBuffer(16); + + GL11.glGetFloat(glCap, glBuffer); + GLStateManager.glGetFloat(glCap, glsmBuffer); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i], glBuffer.get(i), 0.0001f, "GL State Mismatch"); + assertEquals(expected[i], glsmBuffer.get(i), 0.0001f, "GLSM State Mismatch"); + } + } + + private void verifyState(int glCap, boolean[] expected) { + final ByteBuffer glBuffer = BufferUtils.createByteBuffer(16); + final ByteBuffer glsmBuffer = BufferUtils.createByteBuffer(16); + + GL11.glGetBoolean(glCap, glBuffer); + GLStateManager.glGetBoolean(glCap, glsmBuffer); + for (int i = 0; i < expected.length; i++) { + assertEquals(expected[i] ? GL11.GL_TRUE : GL11.GL_FALSE, glBuffer.get(i), "GL State Mismatch"); + assertEquals(expected[i] ? GL11.GL_TRUE : GL11.GL_FALSE, glsmBuffer.get(i), "GLSM State Mismatch"); + } + } + + @Test + void testPushPopColorBufferBit() { + + GLStateManager.glPushAttrib(GL11.GL_COLOR_BUFFER_BIT); + /* + * GL_COLOR_BUFFER_BIT + * GL_ALPHA_TEST enable bit + * Alpha test function and reference value + * GL_BLEND enable bit + * Blending source and destination functions + * Constant blend color + * Blending equation + * GL_DITHER enable bit + * GL_DRAW_BUFFER setting + * GL_COLOR_LOGIC_OP enable bit + * GL_INDEX_LOGIC_OP enable bit + * Logic op function + * Color mode and index mode clear values + * Color mode and index mode writemasks + */ + + GLStateManager.enableAlphaTest(); + GLStateManager.glAlphaFunc(GL11.GL_NEVER, 1f); + GLStateManager.enableBlend(); + GLStateManager.glDisable(GL11.GL_DITHER); + GLStateManager.glEnable(GL11.GL_COLOR_LOGIC_OP); + GLStateManager.glEnable(GL11.GL_INDEX_LOGIC_OP); + GLStateManager.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); + GL14.glBlendColor(1f, 1f, 1f, 1f); // Not currently Implemented in GLSM + GL20.glBlendEquationSeparate(GL14.GL_FUNC_REVERSE_SUBTRACT, GL14.GL_FUNC_REVERSE_SUBTRACT); // Not currently Implemented in GLSM + GL11.glDrawBuffer(GL11.GL_FRONT_AND_BACK); // Not currently Implemented in GLSM + GL11.glLogicOp(GL11.GL_OR);// Not currently Implemented in GLSM + GLStateManager.glColor4f(0.5f, 0.5f, 0.5f, 0.5f); // This should not be reset + GLStateManager.glColorMask(false, false, false, false); + GLStateManager.glClearColor(0.5f, 0.5f, 0.5f, 0.5f); + + + verifyState(GL11.GL_ALPHA_TEST, true); + verifyState(GL11.GL_ALPHA_TEST_FUNC, GL11.GL_NEVER); + verifyState(GL11.GL_ALPHA_TEST_REF, 1f); + verifyState(GL11.GL_BLEND, true); + verifyState(GL11.GL_BLEND_SRC, GL11.GL_SRC_ALPHA); + verifyState(GL11.GL_BLEND_DST, GL11.GL_ONE_MINUS_SRC_ALPHA); + verifyState(GL14.GL_BLEND_COLOR, new float[]{1f, 1f, 1f, 1f}); + verifyState(GL14.GL_BLEND_EQUATION, GL14.GL_FUNC_REVERSE_SUBTRACT); + verifyState(GL20.GL_BLEND_EQUATION_ALPHA, GL14.GL_FUNC_REVERSE_SUBTRACT); + verifyState(GL11.GL_DITHER, false); + verifyState(GL11.GL_DRAW_BUFFER, GL11.GL_FRONT_AND_BACK); + verifyState(GL11.GL_COLOR_LOGIC_OP, true); + verifyState(GL11.GL_INDEX_LOGIC_OP, true); + verifyState(GL11.GL_LOGIC_OP_MODE, GL11.GL_OR); + verifyState(GL11.GL_CURRENT_COLOR, new float[]{0.5f, 0.5f, 0.5f, 0.5f}); + verifyState(GL11.GL_COLOR_WRITEMASK, new boolean[]{false, false, false, false}); + verifyState(GL11.GL_COLOR_CLEAR_VALUE, new float[]{0.5f, 0.5f, 0.5f, 0.5f}); + + + GLStateManager.glPopAttrib(); + verifyState(GL11.GL_ALPHA_TEST, false); + verifyState(GL11.GL_ALPHA_TEST_FUNC, GL11.GL_ALWAYS); + verifyState(GL11.GL_ALPHA_TEST_REF, 0f); + verifyState(GL11.GL_BLEND, false); + verifyState(GL11.GL_BLEND_SRC, GL11.GL_ONE); + verifyState(GL11.GL_BLEND_DST, GL11.GL_ZERO); + verifyState(GL14.GL_BLEND_COLOR, new float[]{0f, 0f, 0f, 0f}); + verifyState(GL14.GL_BLEND_EQUATION, GL14.GL_FUNC_ADD); + verifyState(GL20.GL_BLEND_EQUATION_ALPHA, GL14.GL_FUNC_ADD); + verifyState(GL11.GL_DITHER, true); + verifyState(GL11.GL_DRAW_BUFFER, GL11.GL_BACK); + verifyState(GL11.GL_COLOR_LOGIC_OP, false); + verifyState(GL11.GL_INDEX_LOGIC_OP, false); + verifyState(GL11.GL_LOGIC_OP_MODE, GL11.GL_COPY); + verifyState(GL11.GL_CURRENT_COLOR, new float[]{0.5f, 0.5f, 0.5f, 0.5f}); // This does not get reset + verifyState(GL11.GL_COLOR_WRITEMASK, new boolean[]{true, true, true, true}); + verifyState(GL11.GL_COLOR_CLEAR_VALUE, new float[]{0f, 0f, 0f, 0f}); + + } + +}