2020-07-22 06:23:34 +01:00
package com.mojang.blaze3d.platform ;
import com.google.common.collect.Maps ;
import org.apache.logging.log4j.LogManager ;
import java.util.HashMap ;
import java.util.function.Consumer ;
import java.util.function.Supplier ;
import com.mojang.blaze3d.vertex.BufferBuilder ;
import com.mojang.blaze3d.vertex.Tesselator ;
2020-07-22 06:25:47 +01:00
import com.mojang.blaze3d.vertex.DefaultVertexFormat ;
import org.lwjgl.opengl.GL11 ;
2020-07-22 06:23:34 +01:00
import oshi.hardware.Processor ;
2020-07-22 06:25:47 +01:00
import org.lwjgl.opengl.GLCapabilities ;
2020-07-22 06:23:34 +01:00
import oshi.SystemInfo ;
2020-07-22 06:25:47 +01:00
import org.lwjgl.opengl.GL ;
2020-07-22 06:23:34 +01:00
import java.util.Iterator ;
import org.lwjgl.glfw.GLFWErrorCallback ;
import java.util.List ;
import com.google.common.base.Joiner ;
import org.lwjgl.glfw.GLFWErrorCallbackI ;
import com.google.common.collect.Lists ;
import java.util.function.LongSupplier ;
import org.lwjgl.Version ;
import org.lwjgl.glfw.GLFWVidMode ;
import org.lwjgl.glfw.GLFW ;
2020-07-22 06:25:47 +01:00
import com.mojang.blaze3d.systems.RenderSystem ;
2020-07-22 06:23:34 +01:00
import java.util.Map ;
import org.apache.logging.log4j.Logger ;
public class GLX {
private static final Logger LOGGER ;
private static String capsString ;
private static String cpuInfo ;
private static final Map < Integer , String > LOOKUP_MAP ;
public static String getOpenGLVersionString ( ) {
2020-07-22 06:25:47 +01:00
RenderSystem . assertThread ( RenderSystem : : isOnRenderThread ) ;
2020-07-22 06:23:34 +01:00
if ( GLFW . glfwGetCurrentContext ( ) = = 0L ) {
return " NO CONTEXT " ;
}
2020-07-22 06:25:47 +01:00
return GlStateManager . _getString ( 7937 ) + " GL version " + GlStateManager . _getString ( 7938 ) + " , " + GlStateManager . _getString ( 7936 ) ;
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:30:03 +01:00
public static int _getRefreshRate ( final Window cxx ) {
2020-07-22 06:25:47 +01:00
RenderSystem . assertThread ( RenderSystem : : isOnRenderThread ) ;
2020-07-22 06:30:03 +01:00
long long2 = GLFW . glfwGetWindowMonitor ( cxx . getWindow ( ) ) ;
2020-07-22 06:23:34 +01:00
if ( long2 = = 0L ) {
long2 = GLFW . glfwGetPrimaryMonitor ( ) ;
}
final GLFWVidMode gLFWVidMode4 = ( long2 = = 0L ) ? null : GLFW . glfwGetVideoMode ( long2 ) ;
return ( gLFWVidMode4 = = null ) ? 0 : gLFWVidMode4 . refreshRate ( ) ;
}
2020-07-22 06:25:47 +01:00
public static String _getLWJGLVersion ( ) {
RenderSystem . assertThread ( RenderSystem : : isInInitPhase ) ;
2020-07-22 06:23:34 +01:00
return Version . getVersion ( ) ;
}
2020-07-22 06:25:47 +01:00
public static LongSupplier _initGlfw ( ) {
RenderSystem . assertThread ( RenderSystem : : isInInitPhase ) ;
2020-07-22 06:23:34 +01:00
final IllegalStateException ex ;
Window . checkGlfwError ( ( integer , string ) - > {
new IllegalStateException ( String . format ( " GLFW error before init: [0x%X]%s " , integer , string ) ) ;
throw ex ;
} ) ;
final List < String > list1 = Lists . newArrayList ( ) ;
final GLFWErrorCallback gLFWErrorCallback2 = GLFW . glfwSetErrorCallback ( ( integer , long3 ) - > list1 . add ( String . format ( " GLFW error during init: [0x%X]%s " , integer , long3 ) ) ) ;
if ( GLFW . glfwInit ( ) ) {
final LongSupplier longSupplier3 = ( ) - > ( long ) ( GLFW . glfwGetTime ( ) * 1 . 0E9 ) ;
for ( final String string2 : list1 ) {
GLX . LOGGER . error ( " GLFW error collected during initialization: {} " , string2 ) ;
}
2020-07-22 06:25:47 +01:00
RenderSystem . setErrorCallback ( ( GLFWErrorCallbackI ) gLFWErrorCallback2 ) ;
2020-07-22 06:23:34 +01:00
return longSupplier3 ;
}
throw new IllegalStateException ( " Failed to initialize GLFW, errors: " + Joiner . on ( " , " ) . join ( list1 ) ) ;
}
2020-07-22 06:25:47 +01:00
public static void _setGlfwErrorCallback ( final GLFWErrorCallbackI gLFWErrorCallbackI ) {
RenderSystem . assertThread ( RenderSystem : : isInInitPhase ) ;
final GLFWErrorCallback gLFWErrorCallback2 = GLFW . glfwSetErrorCallback ( gLFWErrorCallbackI ) ;
if ( gLFWErrorCallback2 ! = null ) {
gLFWErrorCallback2 . free ( ) ;
}
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:30:03 +01:00
public static boolean _shouldClose ( final Window cxx ) {
return GLFW . glfwWindowShouldClose ( cxx . getWindow ( ) ) ;
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:25:47 +01:00
public static void _setupNvFogDistance ( ) {
RenderSystem . assertThread ( RenderSystem : : isOnRenderThread ) ;
2020-07-22 06:23:34 +01:00
if ( GL . getCapabilities ( ) . GL_NV_fog_distance ) {
2020-07-22 06:25:47 +01:00
GlStateManager . _fogi ( 34138 , 34139 ) ;
2020-07-22 06:23:34 +01:00
}
}
2020-07-22 06:25:47 +01:00
public static void _init ( final int integer , final boolean boolean2 ) {
RenderSystem . assertThread ( RenderSystem : : isInInitPhase ) ;
final GLCapabilities gLCapabilities3 = GL . getCapabilities ( ) ;
GLX . capsString = " Using framebuffer using " + GlStateManager . _init_fbo ( gLCapabilities3 ) ;
2020-07-22 06:23:34 +01:00
try {
2020-07-22 06:25:47 +01:00
final Processor [ ] arr4 = new SystemInfo ( ) . getHardware ( ) . getProcessors ( ) ;
GLX . cpuInfo = String . format ( " %dx %s " , arr4 . length , arr4 [ 0 ] ) . replaceAll ( " \\ s+ " , " " ) ;
2020-07-22 06:23:34 +01:00
}
catch ( Throwable t ) { }
2020-07-22 06:25:47 +01:00
GlDebug . enableDebugCallback ( integer , boolean2 ) ;
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:25:47 +01:00
public static String _getCapsString ( ) {
2020-07-22 06:23:34 +01:00
return GLX . capsString ;
}
2020-07-22 06:25:47 +01:00
public static String _getCpuInfo ( ) {
2020-07-22 06:23:34 +01:00
return ( GLX . cpuInfo = = null ) ? " <unknown> " : GLX . cpuInfo ;
}
2020-07-22 06:25:47 +01:00
public static void _renderCrosshair ( final int integer , final boolean boolean2 , final boolean boolean3 , final boolean boolean4 ) {
RenderSystem . assertThread ( RenderSystem : : isOnRenderThread ) ;
GlStateManager . _disableTexture ( ) ;
GlStateManager . _depthMask ( false ) ;
2020-07-22 06:30:03 +01:00
final Tesselator cym5 = RenderSystem . renderThreadTesselator ( ) ;
final BufferBuilder cyg6 = cym5 . getBuilder ( ) ;
2020-07-22 06:23:34 +01:00
GL11 . glLineWidth ( 4 . 0f ) ;
2020-07-22 06:30:03 +01:00
cyg6 . begin ( 1 , DefaultVertexFormat . POSITION_COLOR ) ;
2020-07-22 06:23:34 +01:00
if ( boolean2 ) {
2020-07-22 06:30:03 +01:00
cyg6 . vertex ( 0 . 0 , 0 . 0 , 0 . 0 ) . color ( 0 , 0 , 0 , 255 ) . endVertex ( ) ;
cyg6 . vertex ( integer , 0 . 0 , 0 . 0 ) . color ( 0 , 0 , 0 , 255 ) . endVertex ( ) ;
2020-07-22 06:23:34 +01:00
}
if ( boolean3 ) {
2020-07-22 06:30:03 +01:00
cyg6 . vertex ( 0 . 0 , 0 . 0 , 0 . 0 ) . color ( 0 , 0 , 0 , 255 ) . endVertex ( ) ;
cyg6 . vertex ( 0 . 0 , integer , 0 . 0 ) . color ( 0 , 0 , 0 , 255 ) . endVertex ( ) ;
2020-07-22 06:23:34 +01:00
}
if ( boolean4 ) {
2020-07-22 06:30:03 +01:00
cyg6 . vertex ( 0 . 0 , 0 . 0 , 0 . 0 ) . color ( 0 , 0 , 0 , 255 ) . endVertex ( ) ;
cyg6 . vertex ( 0 . 0 , 0 . 0 , integer ) . color ( 0 , 0 , 0 , 255 ) . endVertex ( ) ;
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:30:03 +01:00
cym5 . end ( ) ;
2020-07-22 06:23:34 +01:00
GL11 . glLineWidth ( 2 . 0f ) ;
2020-07-22 06:30:03 +01:00
cyg6 . begin ( 1 , DefaultVertexFormat . POSITION_COLOR ) ;
2020-07-22 06:23:34 +01:00
if ( boolean2 ) {
2020-07-22 06:30:03 +01:00
cyg6 . vertex ( 0 . 0 , 0 . 0 , 0 . 0 ) . color ( 255 , 0 , 0 , 255 ) . endVertex ( ) ;
cyg6 . vertex ( integer , 0 . 0 , 0 . 0 ) . color ( 255 , 0 , 0 , 255 ) . endVertex ( ) ;
2020-07-22 06:23:34 +01:00
}
if ( boolean3 ) {
2020-07-22 06:30:03 +01:00
cyg6 . vertex ( 0 . 0 , 0 . 0 , 0 . 0 ) . color ( 0 , 255 , 0 , 255 ) . endVertex ( ) ;
cyg6 . vertex ( 0 . 0 , integer , 0 . 0 ) . color ( 0 , 255 , 0 , 255 ) . endVertex ( ) ;
2020-07-22 06:23:34 +01:00
}
if ( boolean4 ) {
2020-07-22 06:30:03 +01:00
cyg6 . vertex ( 0 . 0 , 0 . 0 , 0 . 0 ) . color ( 127 , 127 , 255 , 255 ) . endVertex ( ) ;
cyg6 . vertex ( 0 . 0 , 0 . 0 , integer ) . color ( 127 , 127 , 255 , 255 ) . endVertex ( ) ;
2020-07-22 06:23:34 +01:00
}
2020-07-22 06:30:03 +01:00
cym5 . end ( ) ;
2020-07-22 06:23:34 +01:00
GL11 . glLineWidth ( 1 . 0f ) ;
2020-07-22 06:25:47 +01:00
GlStateManager . _depthMask ( true ) ;
GlStateManager . _enableTexture ( ) ;
2020-07-22 06:23:34 +01:00
}
public static String getErrorString ( final int integer ) {
return GLX . LOOKUP_MAP . get ( integer ) ;
}
public static < T > T make ( final Supplier < T > supplier ) {
return supplier . get ( ) ;
}
public static < T > T make ( final T object , final Consumer < T > consumer ) {
consumer . accept ( object ) ;
return object ;
}
static {
LOGGER = LogManager . getLogger ( ) ;
GLX . capsString = " " ;
LOOKUP_MAP = GLX . < Map < Integer , String > > make ( Maps . newHashMap ( ) , hashMap - > {
hashMap . put ( 0 , " No error " ) ;
hashMap . put ( 1280 , " Enum parameter is invalid for this function " ) ;
hashMap . put ( 1281 , " Parameter is invalid for this function " ) ;
hashMap . put ( 1282 , " Current state is invalid for this function " ) ;
hashMap . put ( 1283 , " Stack overflow " ) ;
hashMap . put ( 1284 , " Stack underflow " ) ;
hashMap . put ( 1285 , " Out of memory " ) ;
hashMap . put ( 1286 , " Operation on incomplete framebuffer " ) ;
hashMap . put ( 1286 , " Operation on incomplete framebuffer " ) ;
} ) ;
}
}