Skip to content

Commit

Permalink
Improved interop error catching
Browse files Browse the repository at this point in the history
  • Loading branch information
husker-dev committed Sep 20, 2023
1 parent 7722255 commit d3d4838
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 62 deletions.
6 changes: 3 additions & 3 deletions core/src/main/kotlin/com/huskerdev/openglfx/GLExecutor.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.huskerdev.openglfx

import com.huskerdev.openglfx.implementation.InteropImpl
import com.huskerdev.openglfx.implementation.SharedImpl
import com.huskerdev.openglfx.implementation.UniversalImpl
import com.huskerdev.openglfx.implementations.InteropImpl
import com.huskerdev.openglfx.implementations.SharedImpl
import com.huskerdev.openglfx.implementations.UniversalImpl
import java.nio.ByteBuffer


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract class OpenGLCanvas(
msaa: Int = 0
) = when (GraphicsPipeline.getPipeline().javaClass.canonicalName.split(".")[3]) {
"es2" -> executor::sharedCanvas
"d3d" -> if (DXInterop.isSupported) executor::interopCanvas else executor::universalCanvas
"d3d" -> if (DXInterop.isSupported()) executor::interopCanvas else executor::universalCanvas
else -> executor::universalCanvas
}(profile, flipY, msaa)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.huskerdev.openglfx.implementation
package com.huskerdev.openglfx.implementations

import com.huskerdev.ojgl.GLContext
import com.huskerdev.openglfx.*
Expand Down Expand Up @@ -77,9 +77,6 @@ open class InteropImpl(
context!!.makeCurrent()
initGLFunctions()
executor.initGLFunctionsImpl()

if (interopHandle == 0L)
interopHandle = wglDXOpenDeviceNV(fxDevice.handle)
}

context!!.makeCurrent()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.huskerdev.openglfx.implementation
package com.huskerdev.openglfx.implementations

import com.huskerdev.ojgl.GLContext
import com.huskerdev.openglfx.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.huskerdev.openglfx.implementation
package com.huskerdev.openglfx.implementations

import com.huskerdev.ojgl.GLContext
import com.huskerdev.openglfx.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.huskerdev.openglfx.utils.windows

import com.huskerdev.ojgl.GLContext
import com.huskerdev.openglfx.GLExecutor


const val WGL_ACCESS_WRITE_DISCARD_NV = 0x2

class DXInterop {
companion object {
var interopHandle = 0L

@JvmStatic private external fun nHasDXInterop(): Boolean
@JvmStatic private external fun nHasRenderDocLib(): Boolean

@JvmStatic external fun wglDXOpenDeviceNV(dxDevice: Long): Long
@JvmStatic external fun wglDXCloseDeviceNV(hDevice: Long): Boolean
Expand All @@ -23,17 +20,23 @@ class DXInterop {
@JvmStatic external fun createD3DTexture(device: Long, width: Int, height: Int): LongArray
@JvmStatic external fun replaceD3DTextureInResource(resource: Long, newTexture: Long)

val isSupported by lazy {
if(nHasRenderDocLib()) {
println("[OpenGLFX] WGL_NV_DX_interop is disabled due to RenderDoc library.")
return@lazy false
}

val context = GLContext.create()
context.makeCurrent()
val result = nHasDXInterop()
GLContext.delete(context)
return@lazy result
val interopHandle by lazy {
var context = GLContext.current()
return@lazy if(context.handle == 0L) {
context = GLContext.create()
context.makeCurrent()

val result = createInteropHandle()
GLContext.delete(context)
result
} else createInteropHandle()
}

private fun createInteropHandle(): Long {
GLExecutor.initGLFunctions()
return wglDXOpenDeviceNV(D3D9Device.fxInstance.handle)
}

fun isSupported() = interopHandle != 0L
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import com.huskerdev.openglfx.events.GLDisposeEvent
import com.huskerdev.openglfx.events.GLInitializeEvent
import com.huskerdev.openglfx.events.GLRenderEvent
import com.huskerdev.openglfx.events.GLReshapeEvent
import com.huskerdev.openglfx.implementation.InteropImpl
import com.huskerdev.openglfx.implementation.SharedImpl
import com.huskerdev.openglfx.implementation.UniversalImpl
import com.huskerdev.openglfx.implementations.InteropImpl
import com.huskerdev.openglfx.implementations.SharedImpl
import com.huskerdev.openglfx.implementations.UniversalImpl
import com.huskerdev.openglfx.jogl.events.JOGLDisposeEvent
import com.huskerdev.openglfx.jogl.events.JOGLInitializeEvent
import com.huskerdev.openglfx.jogl.events.JOGLRenderEvent
import com.huskerdev.openglfx.jogl.events.JOGLReshapeEvent
import com.jogamp.opengl.GL2
import com.jogamp.opengl.GL3
import com.jogamp.opengl.GLProfile
import jogamp.opengl.GLDrawableFactoryImpl
Expand Down
34 changes: 1 addition & 33 deletions native/src/dxinterop.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#include <jni.h>
#include <stdio.h>
#include <d3d9.h>
#include <string.h>
#include <psapi.h>
#include "openglfx.h"

// Emulate internal JavaFX's code for memory mapping
Expand Down Expand Up @@ -42,27 +39,6 @@ jlongArray createLongArray(JNIEnv* env, int size, jlong* elements) {

extern "C" {

JNIEXPORT jboolean JNICALL Java_com_huskerdev_openglfx_utils_windows_DXInterop_nHasDXInterop(JNIEnv* env, jobject) {
return a_GetProcAddress("wglDXOpenDeviceNV") != 0;
}

JNIEXPORT jboolean JNICALL Java_com_huskerdev_openglfx_utils_windows_DXInterop_nHasRenderDocLib(JNIEnv* env, jobject) {
HANDLE process = GetCurrentProcess();
HMODULE modules[1024];
DWORD modulesSize;

if(EnumProcessModules(process, modules, sizeof(modules), &modulesSize)){
for (unsigned int i = 0; i < (modulesSize / sizeof(HMODULE)); i++){
TCHAR moduleName[MAX_PATH];

if (GetModuleBaseName(process, modules[i], moduleName, sizeof(moduleName) / sizeof(TCHAR)))
if(strcmp(moduleName, "renderdoc.dll") == 0)
return true;
}
}
return false;
}

JNIEXPORT jlong JNICALL Java_com_huskerdev_openglfx_utils_windows_DXInterop_wglDXOpenDeviceNV(JNIEnv* env, jobject, jlong dxDevice) {
return (jlong)wglDXOpenDeviceNV((void*)dxDevice);
}
Expand Down Expand Up @@ -101,15 +77,7 @@ extern "C" {
// It is important to set NULL
IDirect3DTexture9* texture = NULL;
HANDLE sharedHandle = NULL;
HRESULT h = device->CreateTexture(width, height, 0, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, &sharedHandle);


IDirect3DSurface9* surface = NULL;
texture->GetSurfaceLevel(0, &surface);
RECT rect = {0, 0, 100, 100};

device->ColorFill(surface, &rect, D3DCOLOR_XRGB(255,0,155));

device->CreateTexture(width, height, 0, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &texture, &sharedHandle);

jlong array[] = { (jlong)texture, (jlong)sharedHandle };
return createLongArray(env, 2, array);
Expand Down

0 comments on commit d3d4838

Please sign in to comment.