d3d11x11main.cpp revision 92617aeac109481258f0c3863d09c1b8903d438b
1#define INITGUID
2#include "d3d11app.h"
3#include <X11/Xlib.h>
4#include <GL/glx.h>
5#include <galliumdxgi.h>
6#include <sys/time.h>
7
8static d3d11_application* app;
9static IDXGISwapChain* swap_chain;
10unsigned width, height;
11DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
12static ID3D11Device* dev;
13static ID3D11DeviceContext* ctx;
14
15static int attributeList[] = {
16		GLX_RGBA,
17		GLX_RED_SIZE, 8,
18		GLX_GREEN_SIZE, 8,
19		GLX_BLUE_SIZE, 8,
20		None
21};
22
23double get_time()
24{
25	struct timeval tv;
26	gettimeofday(&tv, 0);
27	return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
28}
29
30int main(int argc, char** argv)
31{
32	Display* dpy = XOpenDisplay(0);
33	XVisualInfo* vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
34	Colormap cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
35	XSetWindowAttributes swa;
36	swa.colormap = cmap;
37	swa.border_pixel = 0;
38	swa.event_mask = StructureNotifyMask;
39	width = 512;
40	height = 512;
41	Window win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap| CWEventMask, &swa);
42	XMapWindow(dpy, win);
43
44	GalliumDXGIUseX11Display(dpy, 0, 0);
45
46	DXGI_SWAP_CHAIN_DESC swap_chain_desc;
47	memset(&swap_chain_desc, 0, sizeof(swap_chain_desc));
48	swap_chain_desc.BufferDesc.Width = width;
49	swap_chain_desc.BufferDesc.Height = height;
50	swap_chain_desc.BufferDesc.Format = format;
51	swap_chain_desc.SampleDesc.Count = 1;
52	swap_chain_desc.SampleDesc.Quality = 0;
53	swap_chain_desc.OutputWindow = (HWND)win;
54	swap_chain_desc.Windowed = TRUE;
55	swap_chain_desc.BufferCount = 3;
56	swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
57	swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
58
59	D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_10_0;
60
61	HRESULT hr  =D3D11CreateDeviceAndSwapChain(
62		NULL,
63		D3D_DRIVER_TYPE_HARDWARE,
64		NULL,
65		D3D11_CREATE_DEVICE_SINGLETHREADED,
66		NULL,
67		0,
68		D3D11_SDK_VERSION,
69		&swap_chain_desc,
70		&swap_chain,
71		&dev,
72		&feature_level,
73		&ctx);
74	if(!SUCCEEDED(hr))
75	{
76		fprintf(stderr, "Failed to create D3D11 device (hresult %08x)\n", hr);
77		return 1;
78	}
79
80	app = d3d11_application_create();
81	if(!app->init(dev, argc, argv))
82		return 1;
83
84	double start_time = get_time();
85
86	MSG msg;
87	for(;;)
88	{
89		XEvent event;
90		if(XPending(dpy))
91		{
92			XNextEvent(dpy, &event);
93			if(event.type == DestroyNotify)
94				break;
95			switch(event.type)
96			{
97			case ConfigureNotify:
98				width = event.xconfigure.width;
99				height = event.xconfigure.height;
100				swap_chain->ResizeBuffers(3, width, height, format, 0);
101				break;
102			}
103		}
104		else if(width && height)
105		{
106			ID3D11Texture2D* tex;
107			ID3D11RenderTargetView* rtv;
108			ensure(swap_chain->GetBuffer(0, IID_ID3D11Texture2D, (void**)&tex));
109			ensure(dev->CreateRenderTargetView(tex, NULL, &rtv));
110
111			double ctime = get_time() - start_time;
112
113			app->draw(ctx, rtv, width, height, ctime);
114			ctx->OMSetRenderTargets(0, 0, 0);
115
116			tex->Release();
117			rtv->Release();
118			swap_chain->Present(0, 0);
119		}
120		else
121			XPeekEvent(dpy, &event);
122	}
123	return (int) msg.wParam;
124}
125