Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a GraphicsPropertiesFetcher class #2071

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
13691a3
Merge pull request #1 from PhoenicisOrg/master
ImperatorS79 Jan 21, 2019
ff9b79c
Merge pull request #2 from PhoenicisOrg/master
ImperatorS79 Aug 25, 2019
0774f93
Update pom.xml
ImperatorS79 Aug 25, 2019
1610c5e
Add files via upload
ImperatorS79 Aug 25, 2019
0adb378
Update ToolsConfiguration.java
ImperatorS79 Aug 25, 2019
f9095d5
Update GraphicsProperties.java
ImperatorS79 Aug 25, 2019
ec9a3ef
Update GraphicsPropertiesFetcher.java
ImperatorS79 Aug 25, 2019
3e6eeab
Update GraphicsPropertiesFetcher.java
ImperatorS79 Aug 25, 2019
a424044
Update GraphicsPropertiesFetcher.java
ImperatorS79 Aug 25, 2019
090bd4e
Fix OpenGL library not being loadable multiple times
ImperatorS79 Aug 25, 2019
1d5b026
Update GraphicsProperties.java
ImperatorS79 Aug 25, 2019
cd869f3
Update GraphicsPropertiesFetcher.java
ImperatorS79 Aug 25, 2019
e3bb0f8
Update GraphicsProperties.java
ImperatorS79 Aug 25, 2019
121ae07
Update GraphicsProperties.java
ImperatorS79 Aug 25, 2019
7cb22be
Update GraphicsPropertiesFetcher.java
ImperatorS79 Aug 25, 2019
2030616
Update GraphicsPropertiesFetcher.java
ImperatorS79 Aug 25, 2019
3678f8f
Fix travis and tr
ImperatorS79 Aug 25, 2019
1d70003
Also fetch core context Version
ImperatorS79 Aug 27, 2019
f40bf65
Update GraphicsPropertiesFetcher.java
ImperatorS79 Aug 27, 2019
386ecc0
Codacy I saw you
ImperatorS79 Aug 27, 2019
13fb641
Update pom.xml
ImperatorS79 Aug 27, 2019
986beaf
Update pom.xml
ImperatorS79 Aug 27, 2019
5d50f1c
Update GraphicsProperties.java
ImperatorS79 Sep 6, 2019
62faa23
Update GraphicsPropertiesFetcher.java
ImperatorS79 Sep 6, 2019
0d1559c
Update GraphicsPropertiesFetcher.java
ImperatorS79 Oct 3, 2019
10ddec0
Merge pull request #3 from PhoenicisOrg/master
ImperatorS79 Oct 3, 2019
35e9b1e
It builds
ImperatorS79 Oct 3, 2019
8ebc6ca
Update pom.xml
ImperatorS79 Oct 5, 2019
a50e013
Update pom.xml
ImperatorS79 Oct 5, 2019
36b1b4f
Test
ImperatorS79 Jan 10, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions phoenicis-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<artifactId>phoenicis-tools</artifactId>
<properties>
<project.root>${project.basedir}/../</project.root>
<lwjgl.version>3.2.3</lwjgl.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -126,5 +127,80 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-vulkan</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-bom</artifactId>
<version>${lwjgl.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<id>lwjgl-natives-linux-amd64</id>
<activation>
<os>
<family>unix</family>
<arch>amd64</arch>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we limiting us in any way if we specify amd64 here?

Copy link
Contributor Author

@ImperatorS79 ImperatorS79 Aug 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes straight from LWJGL website so I do not know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On linux and macOS LWJGL is only available in 64 bits.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if you run the code with Linux 32 bit or an older mac?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it would not work, obviously. De we really want to support such old architecture ? If so, the only possible solution is to write that as an external C++ code or to simply use glxinfo/vulkaninfo as stated in #2061.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to support as many platforms as we comfortably can. In my opinion it is likely that quite a lot of people still use older 32 bit hardware, with an older Linux 32 bit OS, but i think we should ask for some more opinions, @plata @qparis what do you think about this?

</os>
</activation>
<properties>
<lwjgl.natives>natives-linux</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-macos-amd64</id>
<activation>
<os>
<family>mac</family>
<arch>x86_64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-macos</lwjgl.natives>
</properties>
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-vulkan</artifactId>
<classifier>natives-macos</classifier>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.phoenicis.tools.system.ArchitectureFetcher;
import org.phoenicis.tools.system.OperatingSystemFetcher;
import org.phoenicis.tools.system.ScreenManager;
import org.phoenicis.tools.system.GraphicsPropertiesFetcher;
import org.phoenicis.tools.system.SystemConfiguration;
import org.phoenicis.tools.system.opener.AutomaticOpener;
import org.phoenicis.tools.system.opener.Opener;
Expand Down Expand Up @@ -167,4 +168,9 @@ public LnkParser lnkParser() {
public ScreenManager screenManager() {
return new ScreenManager();
}

@Bean
public GraphicsPropertiesFetcher graphicsPropertiesFetcher() {
return new GraphicsPropertiesFetcher();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2015-2017 PÂRIS Quentin
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package org.phoenicis.tools.system;

import org.phoenicis.configuration.security.Safe;

@Safe
/**
* This class contains some information about the graphics capabilities of the GPU:
* the GPU vendor, name (renderer), the OpenGL and Vulkan version
*/
public class GraphicsProperties {
private String vendor;
private String renderer;
private String openglVersion;
private String openglCoreVersion;
private String vulkanVersion;

public GraphicsProperties() {
this.vendor = "Unknown";
this.renderer = "Unknown";
this.openglVersion = "Unsupported";
this.openglCoreVersion = "Unsupported";
this.vulkanVersion = "Unsupported";
}

public void setVendor(String vendor) {
this.vendor = vendor;
}

public void setRenderer(String renderer) {
this.renderer = renderer;
}

public void setOpenGLVersion(String openglVersion) {
this.openglVersion = openglVersion;
}

public void setOpenGLCoreVersion(String openglCoreVersion) {
this.openglCoreVersion = openglCoreVersion;
}

public void setVulkanVersion(String vulkanVersion) {
this.vulkanVersion = vulkanVersion;
}

public String getVendor() {
return this.vendor;
}

public String getRenderer() {
return this.renderer;
}

public String getOpenGLVersion() {
return this.openglVersion;
}

public String getOpenGLCoreVersion() {
return this.openglCoreVersion;
}

public String getVulkanVersion() {
return this.vulkanVersion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* Copyright (C) 2015-2017 PÂRIS Quentin
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package org.phoenicis.tools.system;

import org.phoenicis.configuration.security.Safe;
import org.phoenicis.tools.system.GraphicsProperties;
import org.phoenicis.tools.system.ArchitectureFetcher;
import org.phoenicis.tools.system.OperatingSystemFetcher;
import org.phoenicis.entities.Architecture;
import org.phoenicis.entities.OperatingSystem;

import java.util.ArrayList;
import java.util.Arrays;
import static org.phoenicis.configuration.localisation.Localisation.tr;

import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.vulkan.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.glfw.GLFWVulkan.*;

// Code from LWJGL tutorial
@Safe
/**
* This class fetch the required properties to fill the class GraphicsProperties,
* using LWJGL to create a dummy window and context in order to access OpenGL and Vulkan properties.
*/
public class GraphicsPropertiesFetcher {
private long window = NULL;

/**
* Create an invisible glfx window and context, from which infos will be retrieved
*/
private void init(GraphicsProperties graphicsProperties) {
GLFWErrorCallback.createPrint(System.err).set();

if (!glfwInit())
throw new IllegalStateException(tr("Unable to initialize GLFW for testing graphic card capabilities"));

// We will now fetch maximum supported OpenGL core context version (3.2 -> 4.6)
ArrayList<ArrayList<Integer>> openglCoreVersion = new ArrayList<ArrayList<Integer>>(); // Versions that
// distinguish core and
// compatibility

openglCoreVersion.add(new ArrayList<Integer>(Arrays.asList(6, 5, 4, 3, 2, 1, 0))); // OpenGL 4.x
openglCoreVersion.add(new ArrayList<Integer>(Arrays.asList(3, 2))); // OpenGL 3.x

// We run through the versions than should provide a core context
boolean found = false;
for (int i = 0; i < openglCoreVersion.size(); ++i) {
if (!found) {
for (int j = 0; i < openglCoreVersion.get(i).size(); ++j) {
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, -i + 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, openglCoreVersion.get(i).get(j));
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

this.window = glfwCreateWindow(300, 300, "Test Window", NULL, NULL);

if (this.window != NULL) {
// We found a working core context
found = true;
graphicsProperties.setOpenGLCoreVersion(String.valueOf(-i + 4) + "."
+ String.valueOf(openglCoreVersion.get(i).get(j)));
glfwFreeCallbacks(this.window);
glfwDestroyWindow(this.window);
this.window = NULL;
break;
}

// No core context found, the context is then 3.1 or less, see fetchVendorRendererOpenGLVersion
}
}
}

glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

this.window = glfwCreateWindow(300, 300, "Test Window", NULL, NULL);
if (this.window == NULL)
throw new IllegalStateException(
tr("Failed to create the GLFW window for testing graphic card capabilities"));
}

/**
* Destroy glfw window and context
*/
private void terminate() {
glfwFreeCallbacks(this.window);
glfwDestroyWindow(this.window);

glfwTerminate();
glfwSetErrorCallback(null).free();
this.window = NULL;
}

/**
* Fetch graphics card vendor and OpenGL version
*/
private void fetchVendorRendererOpenGLVersion(GraphicsProperties graphicsProperties) {
// Allow LWJGL to connect with the glfw OpenGL context and to use gl* function
glfwMakeContextCurrent(this.window);
GL.createCapabilities();

graphicsProperties.setVendor(glGetString(GL_VENDOR));
graphicsProperties.setRenderer(glGetString(GL_RENDERER));
graphicsProperties.setOpenGLVersion(glGetString(GL_VERSION)); // The version can be inferior to
// openglCoreVersion
// If the compatibiltity context is not available
// for large OpenGL version
graphicsProperties.setOpenGLVersion(graphicsProperties.getOpenGLVersion().substring(0,
graphicsProperties.getOpenGLVersion().indexOf(' '))); // We only take to version number
}

/**
* Fetch Vulkan version
*/
private void fetchVulkanVersion(GraphicsProperties graphicsProperties) {
if (!glfwVulkanSupported()) {
return;
}

// Gets normally maximum Vulkan version fully supported
int version = VK.getInstanceVersionSupported();

// Convert the uint32 into a readable String (source: vulkaninfo source code)
graphicsProperties.setVulkanVersion(String.valueOf(version >> 22) + "." +
String.valueOf((version >> 12) & 0x3ff) + "." +
String.valueOf(version & 0xfff));
}

/**
* Fetch the current graphics properties (vendor, OpenGL version, Vulkan version) of the system
*
* @return The current graphics properties inside a GraphicsProperties object
*/
public GraphicsProperties getProperties() {
final GraphicsProperties graphicsProperties = new GraphicsProperties();

final OperatingSystemFetcher operatingSystemFetcher = new OperatingSystemFetcher();
OperatingSystem os = operatingSystemFetcher.fetchCurrentOperationSystem();

final ArchitectureFetcher architectureFetcher = new ArchitectureFetcher(operatingSystemFetcher);
Architecture arch = architectureFetcher.fetchCurrentArchitecture();

if (arch == Architecture.AMD64 && (os == OperatingSystem.LINUX || os == OperatingSystem.MACOSX)) {
init(graphicsProperties);

fetchVendorRendererOpenGLVersion(graphicsProperties);
fetchVulkanVersion(graphicsProperties);

terminate();
}

return graphicsProperties;
}
}