Skip to content

Commit

Permalink
Some optimization, fixed memory leak with D3D
Browse files Browse the repository at this point in the history
  • Loading branch information
nshtengauer committed Dec 13, 2024
1 parent 618d49d commit 1ee8610
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 47 deletions.
3 changes: 2 additions & 1 deletion gradle/addons/maven-publish.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class MavenPublishPlugin implements Plugin<Project> {
}

project.signing {
sign project.publishing.publications.mavenJava
if(project.properties.hasProperty("ossrhUsername"))
sign project.publishing.publications.mavenJava
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ open class ExternalObjectsCanvasWinD3D(
protected inner class ExternalObjectsSwapBuffer: SwapBuffer() {
private lateinit var fbo: Framebuffer
private lateinit var interopFBO: Framebuffer.Default
private lateinit var d3d9Texture: D3D9.Texture
private var memoryObj = 0

private lateinit var fxD3D9Texture: D3D9.Texture
private lateinit var fxTexture: Texture
private var fxTextureHandle = 0L
private lateinit var texture: Texture

private lateinit var sharedD3DTexture0: D3D9.Texture
private lateinit var sharedD3DTexture: D3D9.Texture
private lateinit var sharedD3DTextureSurface: D3D9.Surface

private lateinit var fxD3DTexture: D3D9.Texture
private lateinit var fxD3DTextureSurface: D3D9.Surface

override fun render(width: Int, height: Int): Framebuffer {
if(checkFramebufferSize(width, height))
Expand All @@ -66,10 +70,10 @@ open class ExternalObjectsCanvasWinD3D(
if(!this::fbo.isInitialized || fbo.width != width || fbo.height != height){
dispose()

d3d9Texture = d3d9Device.createTexture(width, height)
sharedD3DTexture0 = d3d9Device.createTexture(width, height)

memoryObj = glCreateMemoryObjectsEXT()
glImportMemoryWin32HandleEXT(memoryObj, 0, GL_HANDLE_TYPE_D3D11_IMAGE_KMT_EXT, d3d9Texture.sharedHandle)
glImportMemoryWin32HandleEXT(memoryObj, 0, GL_HANDLE_TYPE_D3D11_IMAGE_KMT_EXT, sharedD3DTexture0.sharedHandle)

val sharedTexture = glGenTextures()
glBindTexture(GL_TEXTURE_2D, sharedTexture)
Expand All @@ -84,32 +88,38 @@ open class ExternalObjectsCanvasWinD3D(
}

override fun getTextureForDisplay(g: Graphics): Texture {
val width = d3d9Texture.width
val height = d3d9Texture.height
val width = sharedD3DTexture0.width
val height = sharedD3DTexture0.height

if(!this::fxD3D9Texture.isInitialized || fxD3D9Texture.width != width || fxD3D9Texture.height != height){
if(!this::sharedD3DTexture.isInitialized || sharedD3DTexture.width != width || sharedD3DTexture.height != height){
disposeFXResources()

fxD3D9Texture = D3D9.Device.jfx.createTexture(width, height, d3d9Texture.sharedHandle)
fxTexture = GLFXUtils.createPermanentFXRTTexture(width, height)
fxTextureHandle = fetchDXTexHandle(fxTexture, g)
sharedD3DTexture = D3D9.Device.jfx.createTexture(width, height, sharedD3DTexture0.sharedHandle)
sharedD3DTextureSurface = sharedD3DTexture.getSurfaceLevel(0)

texture = GLFXUtils.createPermanentFXRTTexture(width, height)
fxD3DTexture = D3D9.Texture(width, height, fetchDXTexHandle(texture, g), 0)
fxD3DTextureSurface = fxD3DTexture.getSurfaceLevel(0)
}

D3D9.Device.jfx.stretchRect(fxD3D9Texture.handle, fxTextureHandle)
D3D9.Device.jfx.stretchRect(sharedD3DTextureSurface, fxD3DTextureSurface)

return fxTexture
return texture
}

override fun dispose() {
if (this::fbo.isInitialized) fbo.delete()
if (this::interopFBO.isInitialized) interopFBO.delete()
if (this::d3d9Texture.isInitialized) d3d9Texture.release()
if (this::sharedD3DTexture0.isInitialized) sharedD3DTexture0.release()
if (memoryObj != 0) glDeleteMemoryObjectsEXT(memoryObj)
}

override fun disposeFXResources() {
if (this::fxD3D9Texture.isInitialized) fxD3D9Texture.release()
if (this::fxTexture.isInitialized) fxTexture.dispose()
if (this::sharedD3DTextureSurface.isInitialized) sharedD3DTextureSurface.release()
if (this::sharedD3DTexture.isInitialized) sharedD3DTexture.release()
if (this::fxD3DTextureSurface.isInitialized) fxD3DTextureSurface.release()
if (this::fxD3DTexture.isInitialized) fxD3DTexture.release()
if (this::texture.isInitialized) texture.dispose()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ open class WGLDXInteropCanvas(
protected inner class NVDXInteropSwapBuffer: SwapBuffer(){
private lateinit var fbo: Framebuffer
private lateinit var interopFBO: Framebuffer.Default
private lateinit var d3d9Texture: D3D9.Texture
private lateinit var interopObject: WGLDX.Object

private lateinit var fxD3D9Texture: D3D9.Texture
private lateinit var fxTexture: Texture
private var fxTextureHandle = 0L
private lateinit var texture: Texture

private lateinit var sharedD3DTexture0: D3D9.Texture
private lateinit var sharedD3DTexture: D3D9.Texture
private lateinit var sharedD3DTextureSurface: D3D9.Surface

private lateinit var fxD3DTexture: D3D9.Texture
private lateinit var fxD3DTextureSurface: D3D9.Surface

override fun render(width: Int, height: Int): Framebuffer {
if(checkFramebufferSize(width, height, d3d9Device, interopDevice))
Expand All @@ -65,11 +69,11 @@ open class WGLDXInteropCanvas(
interopFBO = Framebuffer.Default(width, height)
interopFBO.bindFramebuffer()

d3d9Texture = device.createTexture(width, height)
WGLDX.linkShareHandle(d3d9Texture.handle, d3d9Texture.sharedHandle)
sharedD3DTexture0 = device.createTexture(width, height)
WGLDX.linkShareHandle(sharedD3DTexture0.handle, sharedD3DTexture0.sharedHandle)

interopObject = interopDevice.registerObject(
d3d9Texture.handle,
sharedD3DTexture0.handle,
interopFBO.texture,
GL_TEXTURE_2D,
WGL_ACCESS_WRITE_DISCARD_NV
Expand All @@ -80,32 +84,38 @@ open class WGLDXInteropCanvas(
}

override fun getTextureForDisplay(g: Graphics): Texture {
val width = d3d9Texture.width
val height = d3d9Texture.height
val width = sharedD3DTexture0.width
val height = sharedD3DTexture0.height

if(!this::fxD3D9Texture.isInitialized || fxD3D9Texture.width != width || fxD3D9Texture.height != height){
if(!this::sharedD3DTexture.isInitialized || sharedD3DTexture.width != width || sharedD3DTexture.height != height){
disposeFXResources()

fxD3D9Texture = D3D9.Device.jfx.createTexture(width, height, d3d9Texture.sharedHandle)
fxTexture = GLFXUtils.createPermanentFXTexture(width, height)
fxTextureHandle = fetchDXTexHandle(fxTexture, g)
sharedD3DTexture = D3D9.Device.jfx.createTexture(width, height, sharedD3DTexture0.sharedHandle)
sharedD3DTextureSurface = sharedD3DTexture.getSurfaceLevel(0)

texture = GLFXUtils.createPermanentFXTexture(width, height)
fxD3DTexture = D3D9.Texture(width, height, fetchDXTexHandle(texture, g), 0)
fxD3DTextureSurface = fxD3DTexture.getSurfaceLevel(0)
}

D3D9.Device.jfx.stretchRect(fxD3D9Texture.handle, fxTextureHandle)
D3D9.Device.jfx.stretchRect(sharedD3DTextureSurface, fxD3DTextureSurface)

return fxTexture
return texture
}

override fun dispose() {
if (this::interopObject.isInitialized) interopObject.release()
if (this::fbo.isInitialized) fbo.delete()
if (this::interopFBO.isInitialized) interopFBO.delete()
if (this::d3d9Texture.isInitialized) d3d9Texture.release()
if (this::sharedD3DTexture0.isInitialized) sharedD3DTexture0.release()
}

override fun disposeFXResources() {
if (this::fxD3D9Texture.isInitialized) fxD3D9Texture.release()
if (this::fxTexture.isInitialized) fxTexture.dispose()
if (this::sharedD3DTextureSurface.isInitialized) sharedD3DTextureSurface.release()
if (this::sharedD3DTexture.isInitialized) sharedD3DTexture.release()
if (this::fxD3DTextureSurface.isInitialized) fxD3DTextureSurface.release()
if (this::fxD3DTexture.isInitialized) fxD3DTexture.release()
if (this::texture.isInitialized) texture.dispose()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class D3D9 {
@JvmStatic external fun nStretchRect(device: Long, src: Long, dst: Long)
@JvmStatic external fun nGetTexture(device: Long, stage: Int): Long

@JvmStatic external fun nGetSurface(texture: Long, level: Int): Long
@JvmStatic external fun nReleaseSurface(surface: Long): Long

init {
GLFXUtils.loadLibrary()
}
Expand All @@ -35,8 +38,8 @@ class D3D9 {
Texture(width, height, this[0], this[1])
}

fun stretchRect(src: Long, dst: Long) =
nStretchRect(handle, src, dst)
fun stretchRect(src: Surface, dst: Surface) =
nStretchRect(handle, src.handle, dst.handle)

fun getTexture(stage: Int) =
nGetTexture(handle, stage)
Expand All @@ -50,5 +53,15 @@ class D3D9 {
) {
fun release() =
nReleaseTexture(handle)

fun getSurfaceLevel(level: Int) =
Surface(nGetSurface(handle, level))
}

data class Surface(
val handle: Long
) {
fun release() =
nReleaseSurface(handle)
}
}
24 changes: 15 additions & 9 deletions modules/core/native/win/dx9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,18 @@ jni_win_d3d9(void, nReleaseTexture)(JNIEnv* env, jobject, jlong handle) {
texture->Release();
}

jni_win_d3d9(void, nReleaseSurface)(JNIEnv* env, jobject, jlong _surface) {
IDirect3DSurface9* surface = (IDirect3DSurface9*)_surface;
surface->Release();
}

jni_win_d3d9(void, nStretchRect)(JNIEnv* env, jobject, jlong _device, jlong _src, jlong _dst) {
IDirect3DDevice9Ex* device = (IDirect3DDevice9Ex*)_device;


IDirect3DTexture9* srcTx = (IDirect3DTexture9*)_src;
IDirect3DTexture9* dstTx = (IDirect3DTexture9*)_dst;
IDirect3DSurface9* src = 0;
IDirect3DSurface9* dst = 0;
srcTx->GetSurfaceLevel(0, &src);
dstTx->GetSurfaceLevel(0, &dst);
IDirect3DSurface9* src = (IDirect3DSurface9*)_src;
IDirect3DSurface9* dst = (IDirect3DSurface9*)_dst;

device->StretchRect(src, NULL, dst, NULL, D3DTEXF_NONE);
src->Release();
dst->Release();
}

jni_win_d3d9(jlong, nGetTexture)(JNIEnv* env, jobject, jlong _device, jint stage) {
Expand All @@ -79,3 +77,11 @@ jni_win_d3d9(jlong, nGetTexture)(JNIEnv* env, jobject, jlong _device, jint stage
device->GetTexture(stage, &texture);
return (jlong)texture;
}


jni_win_d3d9(jlong, nGetSurface)(JNIEnv* env, jobject, jlong _texture, jint level) {
IDirect3DTexture9* texture = (IDirect3DTexture9*)_texture;
IDirect3DSurface9* surface = 0;
texture->GetSurfaceLevel(0, &surface);
return (jlong)surface;
}

0 comments on commit 1ee8610

Please sign in to comment.