d3d10x11main.cpp revision 92617aeac109481258f0c3863d09c1b8903d438b
1/**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27#define INITGUID
28#include "d3d10app.h"
29#include <X11/Xlib.h>
30#include <GL/glx.h>
31#include <galliumdxgi.h>
32#include <sys/time.h>
33
34static d3d10_application* app;
35static IDXGISwapChain* swap_chain;
36unsigned width, height;
37DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
38static ID3D10Device* dev;
39static ID3D10Device* ctx;
40
41static int attributeList[] = {
42		GLX_RGBA,
43		GLX_RED_SIZE, 8,
44		GLX_GREEN_SIZE, 8,
45		GLX_BLUE_SIZE, 8,
46		None
47};
48
49double get_time()
50{
51	struct timeval tv;
52	gettimeofday(&tv, 0);
53	return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
54}
55
56int main(int argc, char** argv)
57{
58	Display* dpy = XOpenDisplay(0);
59	XVisualInfo* vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
60	Colormap cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
61	XSetWindowAttributes swa;
62	swa.colormap = cmap;
63	swa.border_pixel = 0;
64	swa.event_mask = StructureNotifyMask;
65	width = 512;
66	height = 512;
67	Window win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, width, height, 0, vi->depth, InputOutput, vi->visual, CWBorderPixel | CWColormap| CWEventMask, &swa);
68	XMapWindow(dpy, win);
69
70	GalliumDXGIUseX11Display(dpy, 0, 0);
71
72	DXGI_SWAP_CHAIN_DESC swap_chain_desc;
73	memset(&swap_chain_desc, 0, sizeof(swap_chain_desc));
74	swap_chain_desc.BufferDesc.Width = width;
75	swap_chain_desc.BufferDesc.Height = height;
76	swap_chain_desc.BufferDesc.Format = format;
77	swap_chain_desc.SampleDesc.Count = 1;
78	swap_chain_desc.SampleDesc.Quality = 0;
79	swap_chain_desc.OutputWindow = (HWND)win;
80	swap_chain_desc.Windowed = TRUE;
81	swap_chain_desc.BufferCount = 3;
82	swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
83	swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
84
85	D3D10_FEATURE_LEVEL1 feature_level = D3D10_FEATURE_LEVEL_10_0;
86
87	HRESULT hr;
88	if(0)
89	{
90		hr  = D3D10CreateDeviceAndSwapChain(
91			NULL,
92			D3D10_DRIVER_TYPE_HARDWARE,
93			NULL,
94			D3D10_CREATE_DEVICE_SINGLETHREADED,
95			D3D10_SDK_VERSION,
96			&swap_chain_desc,
97			&swap_chain,
98			&dev);
99	}
100	else
101	{
102		hr  = D3D10CreateDeviceAndSwapChain1(
103			NULL,
104			D3D10_DRIVER_TYPE_HARDWARE,
105			NULL,
106			D3D10_CREATE_DEVICE_SINGLETHREADED,
107			feature_level,
108			D3D10_SDK_VERSION,
109			&swap_chain_desc,
110			&swap_chain,
111			(ID3D10Device1**)&dev);
112	}
113	if(!SUCCEEDED(hr))
114	{
115		fprintf(stderr, "Failed to create D3D10 device (hresult %08x)\n", hr);
116		return 1;
117	}
118	ctx = dev;
119
120	app = d3d10_application_create();
121	if(!app->init(dev, argc, argv))
122		return 1;
123
124	double start_time = get_time();
125
126	MSG msg;
127	for(;;)
128	{
129		XEvent event;
130		if(XPending(dpy))
131		{
132			XNextEvent(dpy, &event);
133			if(event.type == DestroyNotify)
134				break;
135			switch(event.type)
136			{
137			case ConfigureNotify:
138				width = event.xconfigure.width;
139				height = event.xconfigure.height;
140				swap_chain->ResizeBuffers(3, width, height, format, 0);
141				break;
142			}
143		}
144		else if(width && height)
145		{
146			ID3D10Texture2D* tex;
147			ID3D10RenderTargetView* rtv;
148			ensure(swap_chain->GetBuffer(0, IID_ID3D10Texture2D, (void**)&tex));
149			ensure(dev->CreateRenderTargetView(tex, NULL, &rtv));
150
151			double ctime = get_time() - start_time;
152
153			app->draw(ctx, rtv, width, height, ctime);
154			ctx->OMSetRenderTargets(0, 0, 0);
155
156			tex->Release();
157			rtv->Release();
158			swap_chain->Present(0, 0);
159		}
160		else
161			XPeekEvent(dpy, &event);
162	}
163	return (int) msg.wParam;
164}
165