diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 328348564..356ab59f7 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -323,7 +323,7 @@ class RenderWindow( } fun assertOnRenderThread() { - check(Thread.currentThread() == renderThread) { "Current thread (${Thread.currentThread().name} is not the render thread!" } + check(Thread.currentThread() === renderThread) { "Current thread (${Thread.currentThread().name} is not the render thread!" } } operator fun get(renderer: RendererBuilder): T? { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDSystemDebugNode.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDSystemDebugNode.kt index 990f224cc..2bb6c843b 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDSystemDebugNode.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/hud/nodes/debug/HUDSystemDebugNode.kt @@ -54,6 +54,8 @@ class HUDSystemDebugNode(hudRenderer: HUDRenderer) : DebugScreenNode(hudRenderer private val gpuText = text("TBA") private val gpuVersionText = text("TBA") + private val gpuMemoryText = text("TBA") + init { text() @@ -67,6 +69,10 @@ class HUDSystemDebugNode(hudRenderer: HUDRenderer) : DebugScreenNode(hudRenderer text("Mods: ${ModLoader.MOD_MAP.size} active, ${hudRenderer.connection.eventListenerSize} listeners") } + init { + text() + } + private val targetPosition = text("TBA") private val targetBlockState = text("TBA") @@ -86,6 +92,8 @@ class HUDSystemDebugNode(hudRenderer: HUDRenderer) : DebugScreenNode(hudRenderer memoryText.sText = "Memory: ${getUsedMemoryPercent()}% ${getFormattedUsedMemory()}/${SystemInformation.MAX_MEMORY_TEXT}" allocatedMemoryText.sText = "Allocated: ${getAllocatedMemoryPercent()}% ${getFormattedAllocatedMemory()}" + gpuMemoryText.sText = "VRAM: ${UnitFormatter.formatBytes(hudRenderer.renderWindow.renderSystem.usedVRAM)} / ${UnitFormatter.formatBytes(hudRenderer.renderWindow.renderSystem.availableVRAM)} | ${UnitFormatter.formatBytes(hudRenderer.renderWindow.renderSystem.maximumVRAM)}" + val rayCastHit = hudRenderer.renderWindow.inputHandler.camera.getTargetBlock() if (rayCastHit == null) { targetPosition.sText = "" diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/shader/Shader.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/shader/Shader.kt index ebbd5c7ae..4d98650bf 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/shader/Shader.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/shader/Shader.kt @@ -169,13 +169,6 @@ class Shader( companion object { private val DEFAULT_DEFINES: Map Any?> = mapOf( - "__NVIDIA" to { - if (glGetString(GL_VENDOR)?.lowercase()?.contains("nvidia") == true) { - "" - } else { - null - } - }, "ANIMATED_TEXTURE_COUNT" to { MMath.clamp(it.textures.animator.animatedTextures.size, 1, TextureArray.MAX_ANIMATED_TEXTURES) } @@ -233,6 +226,11 @@ class Shader( for ((name, value) in DEFAULT_DEFINES) { value(renderWindow)?.let { pushDefine(name, it) } } + + // ToDo: Don't do that! + if (renderWindow.renderSystem is OpenGLRenderSystem) { + renderWindow.renderSystem.vendor.define?.let { pushDefine(it, "") } + } } line.startsWith("uniform ") -> { // ToDo: Packed in layout reader.readUnquotedString() // "uniform" diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt index 6739d4f20..2da11bf13 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/RenderSystem.kt @@ -51,5 +51,10 @@ interface RenderSystem { var polygonMode: PolygonModes + + val usedVRAM: Long + val availableVRAM: Long + val maximumVRAM: Long + fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLRenderSystem.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLRenderSystem.kt index 87204296b..a824fd693 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLRenderSystem.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/OpenGLRenderSystem.kt @@ -17,6 +17,7 @@ import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.modding.events.ResizeWindowEvent import de.bixilon.minosoft.gui.rendering.shader.Shader import de.bixilon.minosoft.gui.rendering.system.base.* +import de.bixilon.minosoft.gui.rendering.system.opengl.vendor.* import de.bixilon.minosoft.modding.event.CallbackEventInvoker import de.bixilon.minosoft.util.KUtil.synchronizedMapOf import de.bixilon.minosoft.util.KUtil.synchronizedSetOf @@ -31,6 +32,9 @@ class OpenGLRenderSystem( ) : RenderSystem { val shaders: MutableMap = synchronizedMapOf() // ToDo private val capabilities: MutableSet = synchronizedSetOf() + lateinit var vendor: OpenGLVendor + private set + var blendingSource = BlendingFunctions.ONE private set var blendingDestination = BlendingFunctions.ZERO @@ -50,6 +54,16 @@ class OpenGLRenderSystem( override fun init() { GL.createCapabilities() + val vendorString = glGetString(GL_VENDOR)?.lowercase() + + vendor = when { + vendorString == null -> OtherOpenGLVendor + vendorString.contains("nvidia") -> NvidiaOpenGLVendor + vendorString.contains("intel") -> MesaOpenGLVendor + vendorString.contains("amd") || vendorString.contains("ati") -> ATIOpenGLVendor // ToDo + else -> OtherOpenGLVendor + } + renderWindow.connection.registerEvent(CallbackEventInvoker.of { renderWindow.queue += { glViewport(0, 0, it.size.x, it.size.y) @@ -122,6 +136,15 @@ class OpenGLRenderSystem( field = value } + override val usedVRAM: Long + get() = vendor.usedVRAM + + override val availableVRAM: Long + get() = vendor.availableVRAM + + override val maximumVRAM: Long + get() = vendor.maximumVRAM + override fun readPixels(start: Vec2i, end: Vec2i, type: PixelTypes): ByteBuffer { val buffer: ByteBuffer = BufferUtils.createByteBuffer((end.x - start.x) * (end.y - start.y) * type.bytes) glReadPixels(start.x, start.y, end.x, end.y, type.gl, GL_UNSIGNED_BYTE, buffer) diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/ATIOpenGLVendor.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/ATIOpenGLVendor.kt new file mode 100644 index 000000000..f1595420e --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/ATIOpenGLVendor.kt @@ -0,0 +1,24 @@ +/* + * Minosoft + * Copyright (C) 2021 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.opengl.vendor + +import org.lwjgl.opengl.ATIMeminfo.GL_VBO_FREE_MEMORY_ATI +import org.lwjgl.opengl.GL11.glGetInteger + +object ATIOpenGLVendor : OpenGLVendor { + override val define: String = "__ATI" + + override val availableVRAM: Long + get() = glGetInteger(GL_VBO_FREE_MEMORY_ATI).toLong() * 1024 +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/MesaOpenGLVendor.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/MesaOpenGLVendor.kt new file mode 100644 index 000000000..5e4dc4c27 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/MesaOpenGLVendor.kt @@ -0,0 +1,18 @@ +/* + * Minosoft + * Copyright (C) 2021 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.opengl.vendor + +object MesaOpenGLVendor : OpenGLVendor { + override val define: String = "__MESA" +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/NvidiaOpenGLVendor.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/NvidiaOpenGLVendor.kt new file mode 100644 index 000000000..40281abd2 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/NvidiaOpenGLVendor.kt @@ -0,0 +1,29 @@ +/* + * Minosoft + * Copyright (C) 2021 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.opengl.vendor + +import org.lwjgl.opengl.GL11.glGetInteger +import org.lwjgl.opengl.NVXGPUMemoryInfo.GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX +import org.lwjgl.opengl.NVXGPUMemoryInfo.GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX + +object NvidiaOpenGLVendor : OpenGLVendor { + override val define: String = "__NVIDIA" + + override val availableVRAM: Long + get() = glGetInteger(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX).toLong() * 1024 + + override val maximumVRAM: Long + get() = glGetInteger(GL_GPU_MEMORY_INFO_DEDICATED_VIDMEM_NVX).toLong() * 1024 + +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/OpenGLVendor.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/OpenGLVendor.kt new file mode 100644 index 000000000..9e0e85e8b --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/OpenGLVendor.kt @@ -0,0 +1,26 @@ +/* + * Minosoft + * Copyright (C) 2021 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.opengl.vendor + +interface OpenGLVendor { + + val define: String? + + val usedVRAM: Long + get() = -1L + val availableVRAM: Long + get() = -1L + val maximumVRAM: Long + get() = -1L +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/OtherOpenGLVendor.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/OtherOpenGLVendor.kt new file mode 100644 index 000000000..665d34589 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/vendor/OtherOpenGLVendor.kt @@ -0,0 +1,18 @@ +/* + * Minosoft + * Copyright (C) 2021 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.opengl.vendor + +object OtherOpenGLVendor : OpenGLVendor { + override val define: String? = null +} diff --git a/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt b/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt index ab76308c2..2c9360787 100644 --- a/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt +++ b/src/main/java/de/bixilon/minosoft/util/UnitFormatter.kt @@ -19,6 +19,9 @@ object UnitFormatter { private val TIME_UNITS = arrayOf("ns", "μs", "ms", "s", "m", "h", "d", "w", "M", "Y") fun formatBytes(bytes: Long): String { + if (bytes < 0) { + return "Unknown" + } return formatUnit(bytes, BYTE_UNITS, 1024L) }