vendor specific opengl stuff, show vram usage

This commit is contained in:
Bixilon 2021-06-20 19:23:08 +02:00
parent 0576f1920f
commit 85c965048f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
11 changed files with 160 additions and 8 deletions

View File

@ -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 <T : Renderer> get(renderer: RendererBuilder<T>): T? {

View File

@ -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 = ""

View File

@ -169,13 +169,6 @@ class Shader(
companion object {
private val DEFAULT_DEFINES: Map<String, (renderWindow: RenderWindow) -> 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"

View File

@ -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
}

View File

@ -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<Shader, Int> = synchronizedMapOf() // ToDo
private val capabilities: MutableSet<RenderingCapabilities> = 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<ResizeWindowEvent> {
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)

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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"
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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
}

View File

@ -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)
}