Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
comp500 committed Jul 3, 2023
1 parent fbe9577 commit 6d5ab3c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
import org.lwjgl.opengl.*;

public class DXGLFramebufferD3D12 {
private final D3D12GraphicsCommandList commandListTransitionToRenderTarget;
private final D3D12GraphicsCommandList commandListTransitionToPresent;
private final D3D12GraphicsCommandList[] commandListTransitionToRenderTargetArr;
private final D3D12GraphicsCommandList[] commandListTransitionToPresentArr;
private final D3D12CommandAllocator allocator;

private final D3D12Resource dxResource;
private final D3D12Fence dxFence;
private final D3D12CommandQueue dxCommandQueue;
private long fenceValue = 0;
private final D3D12GraphicsCommandList dxCommandTransitionToRenderTarget;
private final D3D12GraphicsCommandList dxCommandTransitionToPresent;
private final D3D12GraphicsCommandList[] dxCommandTransitionToRenderTargetArr;
private final D3D12GraphicsCommandList[] dxCommandTransitionToPresentArr;
private final D3D12CommandAllocator dxCommandAllocator;

private final int glTexture;
private final int glFramebuffer;
private final int glMemoryObject;
private final int glSemaphore;

private long fenceValue = 0;

public DXGLFramebufferD3D12(int width, int height, int bufferIdx,
DXGISwapchain3 dxSwapchain, D3D12Device dxDevice,
D3D12CommandQueue dxCommandQueue) {
Expand All @@ -38,7 +39,7 @@ public DXGLFramebufferD3D12(int width, int height, int bufferIdx,
dxResource = new D3D12Resource(colorBufferBuf.getValue());

D3D12ResourceDesc desc = dxResource.GetDesc(new D3D12ResourceDesc());
D3D12ResourceAllocationInfo allocInfo = dxDevice.GetResourceAllocationInfo(new D3D12ResourceAllocationInfo(), new WinDef.UINT(0), new WinDef.UINT(1), desc.getPointer());
D3D12ResourceAllocationInfo allocInfo = dxDevice.GetResourceAllocationInfo(new D3D12ResourceAllocationInfo(), new WinDef.UINT(0), new D3D12ResourceDesc[]{desc});
long size = allocInfo.SizeInBytes.longValue();
long sizeOrig = (long) width * height * 4; // RGBA;
System.out.println("Got size " + size + " (orig " + sizeOrig + ")");
Expand Down Expand Up @@ -101,27 +102,27 @@ public DXGLFramebufferD3D12(int width, int height, int bufferIdx,
PointerByReference allocBuf = new PointerByReference();
COMUtils.checkRC(dxDevice.CreateCommandAllocator(new WinDef.UINT(D3D12GraphicsCommandList.D3D12_COMMAND_LIST_TYPE_DIRECT),
new Guid.REFIID(D3D12CommandAllocator.IID_ID3D12CommandAllocator), allocBuf));
allocator = new D3D12CommandAllocator(allocBuf.getValue());
dxCommandAllocator = new D3D12CommandAllocator(allocBuf.getValue());
// Create command lists for claiming the backbuffer
{
PointerByReference listBuf = new PointerByReference();
COMUtils.checkRC(dxDevice.CreateCommandList(new WinDef.UINT(0),
new WinDef.UINT(D3D12GraphicsCommandList.D3D12_COMMAND_LIST_TYPE_DIRECT), allocator, Pointer.NULL,
new WinDef.UINT(D3D12GraphicsCommandList.D3D12_COMMAND_LIST_TYPE_DIRECT), dxCommandAllocator, Pointer.NULL,
new Guid.REFIID(D3D12GraphicsCommandList.IID_ID3D12GraphicsCommandList), listBuf));
commandListTransitionToPresent = new D3D12GraphicsCommandList(listBuf.getValue());
commandListTransitionToPresent.ResourceBarrier(new D3D12ResourceBarrier[]{barrierTransitionToPresent});
COMUtils.checkRC(commandListTransitionToPresent.Close());
commandListTransitionToPresentArr = new D3D12GraphicsCommandList[] {commandListTransitionToPresent};
dxCommandTransitionToPresent = new D3D12GraphicsCommandList(listBuf.getValue());
dxCommandTransitionToPresent.ResourceBarrier(new D3D12ResourceBarrier[]{barrierTransitionToPresent});
COMUtils.checkRC(dxCommandTransitionToPresent.Close());
dxCommandTransitionToPresentArr = new D3D12GraphicsCommandList[] {dxCommandTransitionToPresent};
}
{
PointerByReference listBuf = new PointerByReference();
COMUtils.checkRC(dxDevice.CreateCommandList(new WinDef.UINT(0),
new WinDef.UINT(D3D12GraphicsCommandList.D3D12_COMMAND_LIST_TYPE_DIRECT), allocator, Pointer.NULL,
new WinDef.UINT(D3D12GraphicsCommandList.D3D12_COMMAND_LIST_TYPE_DIRECT), dxCommandAllocator, Pointer.NULL,
new Guid.REFIID(D3D12GraphicsCommandList.IID_ID3D12GraphicsCommandList), listBuf));
commandListTransitionToRenderTarget = new D3D12GraphicsCommandList(listBuf.getValue());
commandListTransitionToRenderTarget.ResourceBarrier(new D3D12ResourceBarrier[]{barrierTransitionToRenderTarget});
COMUtils.checkRC(commandListTransitionToRenderTarget.Close());
commandListTransitionToRenderTargetArr = new D3D12GraphicsCommandList[] {commandListTransitionToRenderTarget};
dxCommandTransitionToRenderTarget = new D3D12GraphicsCommandList(listBuf.getValue());
dxCommandTransitionToRenderTarget.ResourceBarrier(new D3D12ResourceBarrier[]{barrierTransitionToRenderTarget});
COMUtils.checkRC(dxCommandTransitionToRenderTarget.Close());
dxCommandTransitionToRenderTargetArr = new D3D12GraphicsCommandList[] {dxCommandTransitionToRenderTarget};
}

this.dxCommandQueue = dxCommandQueue;
Expand All @@ -133,9 +134,9 @@ public void free() {
EXTMemoryObject.glDeleteMemoryObjectsEXT(glMemoryObject);
// TODO: clean up shared handle?
dxResource.Release();
commandListTransitionToPresent.Release();
commandListTransitionToRenderTarget.Release();
allocator.Release();
dxCommandTransitionToPresent.Release();
dxCommandTransitionToRenderTarget.Release();
dxCommandAllocator.Release();
//WGLNVDXInterop.wglDXUnregisterObjectNV(d3dDeviceGl, d3dTargets.get());
//d3dTargets.flip();
}
Expand All @@ -144,7 +145,7 @@ public void free() {

public void bind() {
// D3D: lock backbuffer then signal fence (in queue)
dxCommandQueue.ExecuteCommandLists(commandListTransitionToRenderTargetArr);
dxCommandQueue.ExecuteCommandLists(dxCommandTransitionToRenderTargetArr);
COMUtils.checkRC(dxCommandQueue.Signal(dxFence, ++fenceValue));

// OpenGL: wait for semaphore (server-side), bind framebuffer
Expand All @@ -164,7 +165,7 @@ public void unbind() {

// D3D: wait for fence then unlock backbuffer
COMUtils.checkRC(dxCommandQueue.Wait(dxFence, fenceValue));
dxCommandQueue.ExecuteCommandLists(commandListTransitionToPresentArr);
dxCommandQueue.ExecuteCommandLists(dxCommandTransitionToPresentArr);

// Give GL a hint that it should do some work (as we're not using swapbuffers)
GL32C.glFlush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,9 @@ public void setup(boolean initiallyFullscreen) {
DXGIFactory4 factory = new DXGIFactory4(factoryRef.getValue());

// Check device LUID/description (using DXGIAdapter GetDesc)
DXGILUID adapterLuid = new DXGILUID();
d3dDevice.GetAdapterLuid(adapterLuid.getPointer());
adapterLuid.read();
DXGILUID adapterLuid = d3dDevice.GetAdapterLuid(new DXGILUID());
PointerByReference adapterRef = new PointerByReference();
COMUtils.checkRC(factory.EnumAdapterByLuid(adapterLuid, new Guid.REFIID(DXGIAdapter.IID_IDXGIAdapter), adapterRef));
COMUtils.checkRC(factory.EnumAdapterByLuid(adapterLuid.byValue(), new Guid.REFIID(DXGIAdapter.IID_IDXGIAdapter), adapterRef));
DXGIAdapter adapter = new DXGIAdapter(adapterRef.getValue());
DXGIAdapterDesc adapterDesc = new DXGIAdapterDesc();
COMUtils.checkRC(adapter.GetDesc(adapterDesc));
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/link/infra/dxjni/D3D12Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public WinNT.HRESULT CreateCommandList(WinDef.UINT nodeMask, WinDef.UINT type, D
return (WinNT.HRESULT) _invokeNativeObject(12, new Object[]{this.getPointer(), nodeMask, type, pCommandAllocator, pInitialState, riid, commandList}, WinNT.HRESULT.class);
}

public D3D12ResourceAllocationInfo GetResourceAllocationInfo(D3D12ResourceAllocationInfo retVal, WinDef.UINT visibleMask, WinDef.UINT numResourceDescs, Pointer pResourceDescs) {
return (D3D12ResourceAllocationInfo) _invokeNativeObject(25, new Object[]{this.getPointer(), retVal, visibleMask, numResourceDescs, pResourceDescs}, D3D12ResourceAllocationInfo.class);
public D3D12ResourceAllocationInfo GetResourceAllocationInfo(D3D12ResourceAllocationInfo retVal, WinDef.UINT visibleMask, D3D12ResourceDesc[] pResourceDescs) {
return (D3D12ResourceAllocationInfo) _invokeNativeObject(25, new Object[]{this.getPointer(), retVal, visibleMask, new WinDef.UINT(pResourceDescs.length), pResourceDescs}, D3D12ResourceAllocationInfo.class);
}

public WinNT.HRESULT CreateSharedHandle(D3D12DeviceChild object, Pointer attributes, WinDef.DWORD access, WTypes.LPWSTR name, WinNT.HANDLEByReference handle) {
Expand All @@ -39,7 +39,7 @@ public WinNT.HRESULT CreateFence(long InitialValue, WinDef.UINT Flags, Guid.REFI
return (WinNT.HRESULT) _invokeNativeObject(36, new Object[]{this.getPointer(), InitialValue, Flags, riid, ppFence}, WinNT.HRESULT.class);
}

public void GetAdapterLuid(Pointer RetVal) {
_invokeNativeObject(43, new Object[]{this.getPointer(), RetVal}, Pointer.class);
public DXGILUID GetAdapterLuid(DXGILUID RetVal) {
return (DXGILUID) _invokeNativeObject(43, new Object[]{this.getPointer(), RetVal}, DXGILUID.class);
}
}
22 changes: 20 additions & 2 deletions src/main/java/link/infra/dxjni/DXGILUID.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package link.infra.dxjni;

import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef;

/**
* WinNT.LUID is pass-by-reference, so we use a custom interface
* WinNT.LUID is pass-by-reference only, so we use a custom interface
*/
@Structure.FieldOrder({"LowPart", "HighPart"})
public class DXGILUID extends Structure implements Structure.ByValue {
public class DXGILUID extends Structure {
public WinDef.DWORD LowPart;
public WinDef.LONG HighPart;

public DXGILUID() {}
public DXGILUID(Pointer ptr) {
super(ptr);
}

public static class ByValue extends DXGILUID implements Structure.ByValue {
public ByValue(Pointer ptr) {
super(ptr);
}
}

public ByValue byValue() {
ByValue bv = new ByValue(getPointer());
bv.read();
return bv;
}
}

0 comments on commit 6d5ab3c

Please sign in to comment.