Skip to content

Commit

Permalink
JBR-6045 WLToolkit(Vulkan): Add options to select physical device
Browse files Browse the repository at this point in the history
Implemented -Dsun.java2d.vulkan=True and -Dsun.java2d.vulkan.deviceNumber=n VM options
  • Loading branch information
avu authored and jbrbot committed Nov 8, 2024
1 parent 8608092 commit 7ea0f44
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 13 deletions.
33 changes: 29 additions & 4 deletions src/java.desktop/share/native/common/java2d/vulkan/VKBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#define VALIDATION_LAYER_NAME "VK_LAYER_KHRONOS_validation"
static const uint32_t REQUIRED_VULKAN_VERSION = VK_MAKE_API_VERSION(0, 1, 2, 0);

bool VKGraphicsEnvironment::_verbose = false;
int VKGraphicsEnvironment::_requested_device_number = -1;
std::unique_ptr<VKGraphicsEnvironment> VKGraphicsEnvironment::_ge_instance = nullptr;

// ========== Graphics environment ==========
Expand Down Expand Up @@ -187,11 +189,33 @@ VKGraphicsEnvironment::VKGraphicsEnvironment() :
throw std::runtime_error("Vulkan: No suitable device found");
}
// Create virtual device for a physical device.
// TODO system property for manual choice of GPU
// TODO integrated/discrete presets
// TODO performance/power saving mode switch on the fly?
_default_device = &*_devices[0]; // TODO pick first just to check hat virtual device creation works
int _default_device_number = 0; // TODO pick first just to check that virtual device creation works

if (_verbose) {
fprintf(stderr, "Vulkan graphics devices:\n");
}

_requested_device_number = (_requested_device_number == -1) ? 0 : _requested_device_number;
if (_requested_device_number < 0 || _requested_device_number >= static_cast<int>(_devices.size())) {
if (_verbose) {
fprintf(stderr, " Requested device number (%d) not found, fallback to 0\n", _requested_device_number);
}
_requested_device_number = 0;
}
_default_device_number = _requested_device_number;
if (_verbose) {
for (auto devIter = _devices.begin(); devIter != _devices.end(); devIter++) {
auto devNum = std::distance(begin(_devices), devIter);
fprintf(stderr, " %c%ld: %s\n", devNum == _default_device_number ? '*' : ' ',
devNum, (*devIter)->name().c_str());
}
}
fprintf(stderr, "\n");
_default_device = &*_devices[_default_device_number]; // TODO pick first just to check hat virtual device creation works
_default_device->init();

}

vk::raii::Instance& VKGraphicsEnvironment::vk_instance() {
Expand Down Expand Up @@ -414,8 +438,9 @@ void VKDevice::submitCommandBuffer(vk::raii::CommandBuffer&& primary,
waitStages.clear();
}

extern "C" jboolean VK_Init() {

extern "C" jboolean VK_Init(jboolean verbose, jint requestedDevice) {
VKGraphicsEnvironment::set_verbose(verbose);
VKGraphicsEnvironment::set_requested_device(requestedDevice);
if (VKGraphicsEnvironment::graphics_environment() != nullptr) {
return true;
}
Expand Down
10 changes: 9 additions & 1 deletion src/java.desktop/share/native/common/java2d/vulkan/VKBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ class VKDevice : public vk::raii::Device, public vk::raii::PhysicalDevice {
explicit operator bool() const { // Initialized or not
return *((const vk::raii::Device&) *this);
}

std::string& name() {
return _name;
}
};

class VKGraphicsEnvironment {
Expand All @@ -136,9 +140,13 @@ class VKGraphicsEnvironment {
#endif
std::vector<std::unique_ptr<VKDevice>> _devices;
VKDevice* _default_device;
static bool _verbose;
static int _requested_device_number;
static std::unique_ptr<VKGraphicsEnvironment> _ge_instance;
VKGraphicsEnvironment();
public:
static void set_verbose(bool verbose) { _verbose = verbose; }
static void set_requested_device(int requested_device) { _requested_device_number = requested_device; }
static VKGraphicsEnvironment* graphics_environment();
static void dispose();
VKDevice& default_device();
Expand All @@ -148,7 +156,7 @@ class VKGraphicsEnvironment {
extern "C" {
#endif //__cplusplus

jboolean VK_Init();
jboolean VK_Init(jboolean verbose, jint requestedDevice);

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#ifndef VULKAN_ENABLED
#include "jni.h"

jboolean VK_Init() {
jboolean VK_Init(jboolean verbose, jint requestedDevice) {
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,40 @@ public class WLGraphicsEnvironment extends SunGraphicsEnvironment {
private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.wl.WLGraphicsEnvironment");

private static boolean vulkanEnabled = false;
private static boolean verboseVulkanStatus = false;
private static boolean vulkanRequested = false;
private static int vulkanRequestedDeviceNumber = -1;
@SuppressWarnings("removal")
private static boolean vulkanRequested =
private static String vulkanOption =
AccessController.doPrivileged(
(PrivilegedAction<Boolean>) () ->
"true".equals(System.getProperty("sun.java2d.vulkan")));
(PrivilegedAction<String>) () -> System.getProperty("sun.java2d.vulkan", ""));

@SuppressWarnings("removal")
private static String vulkanOptionDeviceNumber =
AccessController.doPrivileged(
(PrivilegedAction<String>) () -> System.getProperty("sun.java2d.vulkan.deviceNumber", "0"));

static {
vulkanRequested = "true".equalsIgnoreCase(vulkanOption);
try {
vulkanRequestedDeviceNumber = Integer.parseInt(vulkanOptionDeviceNumber);
} catch (NumberFormatException e) {
log.warning("Invalid Vulkan device number:" + vulkanOptionDeviceNumber);
}
verboseVulkanStatus = "True".equals(vulkanOption);


System.loadLibrary("awt");
SurfaceManagerFactory.setInstance(new UnixSurfaceManagerFactory());
if (vulkanRequested) {
vulkanEnabled = initVKWL();
vulkanEnabled = initVKWL(verboseVulkanStatus, vulkanRequestedDeviceNumber);
}
if (log.isLoggable(Level.FINE)) {
log.fine("Vulkan rendering enabled: " + (vulkanEnabled?"YES":"NO"));
}
}

private static native boolean initVKWL();
private static native boolean initVKWL(boolean verbose, int deviceNumber);

private WLGraphicsEnvironment() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ WLOutputByID(uint32_t id)
* Signature: ()Z
*/
JNIEXPORT jboolean JNICALL
Java_sun_awt_wl_WLGraphicsEnvironment_initVKWL(JNIEnv *env, jclass wlge)
Java_sun_awt_wl_WLGraphicsEnvironment_initVKWL(JNIEnv *env, jclass wlge, jboolean verbose, jint requestedDevice)
{
jboolean vkwlAvailable = JNI_FALSE;
/* TODO: The following sequence lead to uninitialized awt lock
Expand All @@ -278,7 +278,7 @@ Java_sun_awt_wl_WLGraphicsEnvironment_initVKWL(JNIEnv *env, jclass wlge)
WLGraphicsEnvironment.initVKWL()
*/
//AWT_LOCK();
vkwlAvailable = VK_Init();
vkwlAvailable = VK_Init(verbose, requestedDevice);
//AWT_UNLOCK();
return vkwlAvailable;
}
Expand Down

0 comments on commit 7ea0f44

Please sign in to comment.