From 2c0bc512ad00b2c150a848315dba91e31c838bd1 Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Sat, 25 Nov 2023 18:56:25 +0100 Subject: [PATCH] rgba buffer fixes, tests --- .../texture/data/buffer/RGB8BufferTest.kt | 97 +++++++++++++++++ .../texture/data/buffer/RGBA8BufferTest.kt | 100 ++++++++++++++++++ .../base/texture/data/buffer/TextureBuffer.kt | 2 +- .../opengl/texture/OpenGLTextureArray.kt | 1 - .../dynamic/OpenGLDynamicTextureArray.kt | 5 +- 5 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGB8BufferTest.kt create mode 100644 src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGBA8BufferTest.kt diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGB8BufferTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGB8BufferTest.kt new file mode 100644 index 000000000..35997f670 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGB8BufferTest.kt @@ -0,0 +1,97 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.system.base.texture.data.buffer + +import de.bixilon.kotlinglm.vec2.Vec2i +import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY_INSTANCE +import org.testng.Assert.assertEquals +import org.testng.annotations.Test + +@Test(groups = ["textures"]) +class RGB8BufferTest { + + fun `set rgb and check buffer at 0,0`() { + val source = RGB8Buffer(Vec2i(12, 13)) + source.setRGBA(0, 0, 0x11, 0x22, 0x33, 0x00) + assertEquals(source.data.get(0), 0x11) + assertEquals(source.data.get(1), 0x22) + assertEquals(source.data.get(2), 0x33) + } + + fun `set rgba parts and check buffer at random`() { + val source = RGB8Buffer(Vec2i(12, 13)) + source.setRGBA(9, 3, 0x11, 0x22, 0x33, 0x00) + assertEquals(source.data.get(135 + 0), 0x11) + assertEquals(source.data.get(135 + 1), 0x22) + assertEquals(source.data.get(135 + 2), 0x33) + } + + fun `set rgba and check buffer at random`() { + val source = RGB8Buffer(Vec2i(12, 13)) + source.setRGBA(9, 3, 0x112233FF) + assertEquals(source.data.get(135 + 0), 0x11) + assertEquals(source.data.get(135 + 1), 0x22) + assertEquals(source.data.get(135 + 2), 0x33) + } + + fun `get rgba at 0,0`() { + val source = RGB8Buffer(Vec2i(12, 13)) + source.data.put(0, 0x11).put(1, 0x22).put(2, 0x33).put(3, 0x44) + val rgba = source.getRGBA(0, 0) + assertEquals(rgba, 0x112233FF) + assertEquals(source.getR(0, 0), 0x11) + assertEquals(source.getG(0, 0), 0x22) + assertEquals(source.getB(0, 0), 0x33) + assertEquals(source.getA(0, 0), 0xFF) + } + + fun `get rgba at 3,3`() { + val source = RGB8Buffer(Vec2i(12, 13)) + source.data.put(117 + 0, 0x11).put(117 + 1, 0x22).put(117 + 2, 0x33) + val rgba = source.getRGBA(3, 3) + assertEquals(rgba, 0x112233FF) + assertEquals(source.getR(3, 3), 0x11) + assertEquals(source.getG(3, 3), 0x22) + assertEquals(source.getB(3, 3), 0x33) + assertEquals(source.getA(3, 3), 0xFF) + } + + + fun `put complete texture`() { + val source = RGB8Buffer(Vec2i(12, 13)) + source.setRGBA(0, 0, 0x11, 0x22, 0x33, 0x00) + source.setRGBA(10, 11, 0x11, 0x22, 0x33, 0x00) + source.setRGBA(11, 12, 0x11, 0x22, 0x33, 0x00) + + val destination = RGB8Buffer(Vec2i(12, 13)) + destination.put(source, Vec2i.EMPTY_INSTANCE, Vec2i.EMPTY_INSTANCE, Vec2i(12, 13)) + + assertEquals(destination.getRGBA(0, 0), 0x112233FF) + assertEquals(destination.getRGBA(10, 11), 0x112233FF) + assertEquals(destination.getRGBA(11, 12), 0x112233FF) + } + + + fun `put part of texture`() { + val source = RGB8Buffer(Vec2i(5, 4)) + source.setRGBA(1, 1, 0x11, 0x22, 0x33, 0x00) + source.setRGBA(3, 3, 0x11, 0x22, 0x33, 0x00) + + val destination = RGB8Buffer(Vec2i(11, 9)) + destination.put(source, Vec2i(1, 1), Vec2i(3, 3), Vec2i(4, 3)) + + assertEquals(destination.getRGBA(3, 3), 0x112233FF) + assertEquals(destination.getRGBA(5, 5), 0x112233FF) + } +} diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGBA8BufferTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGBA8BufferTest.kt new file mode 100644 index 000000000..856e7c8ca --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/RGBA8BufferTest.kt @@ -0,0 +1,100 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.system.base.texture.data.buffer + +import de.bixilon.kotlinglm.vec2.Vec2i +import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY_INSTANCE +import org.testng.Assert.assertEquals +import org.testng.annotations.Test + +@Test(groups = ["textures"]) +class RGBA8BufferTest { + + fun `set rgba and check buffer at 0,0`() { + val source = RGBA8Buffer(Vec2i(12, 13)) + source.setRGBA(0, 0, 0x11, 0x22, 0x33, 0x44) + assertEquals(source.data.get(0), 0x11) + assertEquals(source.data.get(1), 0x22) + assertEquals(source.data.get(2), 0x33) + assertEquals(source.data.get(3), 0x44) + } + + fun `set rgba parts and check buffer at random`() { + val source = RGBA8Buffer(Vec2i(12, 13)) + source.setRGBA(9, 3, 0x11, 0x22, 0x33, 0x44) + assertEquals(source.data.get(180 + 0), 0x11) + assertEquals(source.data.get(180 + 1), 0x22) + assertEquals(source.data.get(180 + 2), 0x33) + assertEquals(source.data.get(180 + 3), 0x44) + } + + fun `set rgba and check buffer at random`() { + val source = RGBA8Buffer(Vec2i(12, 13)) + source.setRGBA(9, 3, 0x11223344) + assertEquals(source.data.get(180 + 0), 0x11) + assertEquals(source.data.get(180 + 1), 0x22) + assertEquals(source.data.get(180 + 2), 0x33) + assertEquals(source.data.get(180 + 3), 0x44) + } + + fun `get rgba at 0,0`() { + val source = RGBA8Buffer(Vec2i(12, 13)) + source.data.put(0, 0x11).put(1, 0x22).put(2, 0x33).put(3, 0x44) + val rgba = source.getRGBA(0, 0) + assertEquals(rgba, 0x11223344) + assertEquals(source.getR(0, 0), 0x11) + assertEquals(source.getG(0, 0), 0x22) + assertEquals(source.getB(0, 0), 0x33) + assertEquals(source.getA(0, 0), 0x44) + } + + fun `get rgba at 3,3`() { + val source = RGBA8Buffer(Vec2i(12, 13)) + source.data.put(156 + 0, 0x11).put(156 + 1, 0x22).put(156 + 2, 0x33).put(156 + 3, 0x44) + val rgba = source.getRGBA(3, 3) + assertEquals(rgba, 0x11223344) + assertEquals(source.getR(3, 3), 0x11) + assertEquals(source.getG(3, 3), 0x22) + assertEquals(source.getB(3, 3), 0x33) + assertEquals(source.getA(3, 3), 0x44) + } + + + fun `put complete texture`() { + val source = RGBA8Buffer(Vec2i(12, 13)) + source.setRGBA(0, 0, 0x11, 0x22, 0x33, 0x44) + source.setRGBA(10, 11, 0x11, 0x22, 0x33, 0x44) + source.setRGBA(11, 12, 0x11, 0x22, 0x33, 0x44) + + val destination = RGBA8Buffer(Vec2i(12, 13)) + destination.put(source, Vec2i.EMPTY_INSTANCE, Vec2i.EMPTY_INSTANCE, Vec2i(12, 13)) + + assertEquals(destination.getRGBA(0, 0), 0x11223344) + assertEquals(destination.getRGBA(10, 11), 0x11223344) + assertEquals(destination.getRGBA(11, 12), 0x11223344) + } + + + fun `put part of texture`() { + val source = RGBA8Buffer(Vec2i(5, 4)) + source.setRGBA(1, 1, 0x11, 0x22, 0x33, 0x44) + source.setRGBA(3, 3, 0x11, 0x22, 0x33, 0x44) + + val destination = RGBA8Buffer(Vec2i(11, 9)) + destination.put(source, Vec2i(1, 1), Vec2i(3, 3), Vec2i(4, 3)) + + assertEquals(destination.getRGBA(3, 3), 0x11223344) + assertEquals(destination.getRGBA(5, 5), 0x11223344) + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/TextureBuffer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/TextureBuffer.kt index 0cb169ed5..5dc525c47 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/TextureBuffer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/data/buffer/TextureBuffer.kt @@ -65,7 +65,7 @@ interface TextureBuffer { for (y in 0 until size.y) { for (x in 0 until size.x) { val rgba = source.getRGBA(sourceOffset.x + x, sourceOffset.y + y) - setRGBA(targetOffset.x + x, targetOffset.y, rgba) + setRGBA(targetOffset.x + x, targetOffset.y + y, rgba) } } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt index e4f8c0367..619752642 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt @@ -194,7 +194,6 @@ class OpenGLTextureArray( for (texture in textures) { val renderData = texture.renderData as OpenGLTextureData for ((level, buffer) in texture.data.collect().withIndex()) { - buffer.data.rewind() buffer.data.flip() glTexSubImage3D(GL_TEXTURE_2D_ARRAY, level, 0, 0, renderData.index, buffer.size.x, buffer.size.y, 1, buffer.glFormat, buffer.glType, buffer.data) } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/dynamic/OpenGLDynamicTextureArray.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/dynamic/OpenGLDynamicTextureArray.kt index 26f15c031..bc058f7b0 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/dynamic/OpenGLDynamicTextureArray.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/dynamic/OpenGLDynamicTextureArray.kt @@ -62,7 +62,6 @@ class OpenGLDynamicTextureArray( // clear first glTexSubImage3D(GL_TEXTURE_2D_ARRAY, level, 0, 0, index, resolution shr level, resolution shr level, 1, GL_RGBA, GL_UNSIGNED_BYTE, empty) } - buffer.data.rewind() buffer.data.flip() glTexSubImage3D(GL_TEXTURE_2D_ARRAY, level, 0, 0, index, buffer.size.x, buffer.size.y, 1, buffer.glFormat, buffer.glType, buffer.data) } @@ -70,7 +69,7 @@ class OpenGLDynamicTextureArray( override fun upload() { if (handle >= 0) throw MemoryLeakException("Texture was not unloaded!") - val textureId = OpenGLTextureUtil.createTextureArray() + val handle = OpenGLTextureUtil.createTextureArray() for (level in 0 until OpenGLTextureUtil.MAX_MIPMAP_LEVELS) { glTexImage3D(GL_TEXTURE_2D_ARRAY, level, GL_RGBA, resolution shr level, resolution shr level, textures.size, 0, GL_RGBA, GL_UNSIGNED_BYTE, null as ByteBuffer?) } @@ -80,7 +79,7 @@ class OpenGLDynamicTextureArray( if (texture.data == null) continue unsafeUpload(index, texture) } - this.handle = textureId + this.handle = handle for (shader in shaders) { unsafeUse(shader)