package dxgi;
import static d3d11.D3D11.D3D11CreateDevice;
import static d3d11.D3D11.D3D11_SDK_VERSION;
import static d3d11.D3D11.D3D_DRIVER_TYPE.D3D_DRIVER_TYPE_HARDWARE;
import static dxgi.DXGI.DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM;
import static dxgi.DXGI.DXGI_SWAP_EFFECT.DXGI_SWAP_EFFECT_DISCARD;
import static dxgi.DXGI.DXGI_USAGE.DXGI_USAGE_RENDER_TARGET_OUTPUT;
import static org.bridj.Pointer.*;
import static org.junit.Assert.assertEquals;
import java.awt.Component;
import javax.swing.JFrame;
import org.bridj.Pointer;
import org.bridj.cpp.com.COMRuntime;
import org.bridj.jawt.JAWT;
import org.bridj.jawt.JAWTUtils;
import org.bridj.jawt.JawtLibrary.JNIEnv;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import sun.awt.AWTSecurityManager;
import windows.HWND;
import d3d11.core.D3D11_VIEWPORT;
import d3d11.core.ID3D11Device;
import d3d11.core.ID3D11DeviceContext;
import d3d11.resources.ID3D11Texture1D;
import d3d11.resources.ID3D11Texture2D;
import d3d11.resources.views.ID3D11RenderTargetView;
public class TestDXGI {
ID3D11Device device;
ID3D11DeviceContext immediateContext;
@After
public void destroy() {
// Release objects
immediateContext.Release();
device.Release();
}
@Before
public void init() {
Pointer<Pointer<ID3D11Device>> pDevice = allocatePointer(ID3D11Device.class);
Pointer<Pointer<ID3D11DeviceContext>> pDeviceContext = allocatePointer(ID3D11DeviceContext.class);
// Create device
int result = D3D11CreateDevice(null,
D3D_DRIVER_TYPE_HARDWARE,
null,
0,
null,
0,
D3D11_SDK_VERSION,
pDevice,
null,
pDeviceContext);
assertEquals(0, result);
device = new ID3D11Device(pDevice.get());
immediateContext = new ID3D11DeviceContext(pDeviceContext.get());
}
@Test
public void testSwapChain() throws InterruptedException {
// Get DXGI factory (shall create swap chain with same factory of D3D11 device)
IDXGIDevice1 DXGIDevice = device.QueryInterface(IDXGIDevice1.class);
IDXGIAdapter1 DXGIAdapter = DXGIDevice.GetParent(IDXGIAdapter1.class);
IDXGIFactory1 DXGIFactory = DXGIAdapter.GetParent(IDXGIFactory1.class);
DXGIAdapter.Release();
// Create JFrame
JFrame frame = new JFrame("Test D3D11");
frame.setSize(800, 600);
frame.setIgnoreRepaint(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Create Swap Chain
DXGI_SWAP_CHAIN_DESC desc = new DXGI_SWAP_CHAIN_DESC();
desc.BufferCount(1)
.Windowed(1)
.OutputWindow(new HWND(pointerToAddress(JAWTUtils.getNativePeerHandle(frame))))
.BufferUsage(DXGI_USAGE_RENDER_TARGET_OUTPUT)
.SwapEffect(DXGI_SWAP_EFFECT_DISCARD);
desc.BufferDesc().Width(800)
.Height(600)
.Format(DXGI_FORMAT_R8G8B8A8_UNORM)
.RefreshRate().Numerator(60)
.Denominator(1);
desc.SampleDesc().Count(1)
.Quality(0);
final IDXGISwapChain swapChain = DXGIFactory.CreateSwapChain(DXGIDevice, desc);
DXGIDevice.Release();
// Get back buffer
ID3D11Texture2D backBuffer = swapChain.GetBuffer(0, ID3D11Texture2D.class);
// Create render target view
final ID3D11RenderTargetView rtView = device.CreateRenderTargetView(backBuffer, null);
// Set render target
Pointer<Pointer<ID3D11RenderTargetView>> ppRTV = allocatePointer(ID3D11RenderTargetView.class);
ppRTV.set(pointerTo(rtView));
immediateContext.OMSetRenderTargets(1, ppRTV, null);
ppRTV.release();
// Set viewport
D3D11_VIEWPORT vp = new D3D11_VIEWPORT();
vp.Width(800).Height(600).MinDepth(0).MaxDepth(1).TopLeftX(0).TopLeftY(0);
immediateContext.RSSetViewports(1, pointerTo(vp));
// Clear screen with some color
final Pointer<Float> pColor = allocateFloats(4).setFloats(new float[] {0.7f, 0.8f, 0.3f, 1.0f});
JNIEnv env = JAWTUtils.getJNIEnv();
JAWT jawt = JAWTUtils.getJAWT(env);
while(frame.isEnabled()) {
JAWTUtils.withLockedSurface(env, jawt, frame, new JAWTUtils.LockedComponentRunnable() {
@Override
public void run(Component arg0, long arg1) {
// TODO Auto-generated method stub
immediateContext.ClearRenderTargetView(pointerTo(rtView), pColor);
// Present screen
swapChain.Present(0, 0);
//frame.repaint();
}
});
}
// Wait to see results
//Thread.sleep(5000);
// Release Swap chain and Factory
rtView.Release();
backBuffer.Release();
swapChain.Release();
DXGIFactory.Release();
// Dispose JFrame
frame.dispose();
}
}