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 #include "d3d10app.h"
28 #include <X11/Xlib.h>
29 #include <galliumdxgi.h>
30 #include <sys/time.h>
31
32 static d3d10_application* app;
33 static IDXGISwapChain* swap_chain;
34 unsigned width, height;
35 DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM;
36 static ID3D10Device* dev;
37 static ID3D10Device* ctx;
38
get_time()39 double get_time()
40 {
41 struct timeval tv;
42 gettimeofday(&tv, 0);
43 return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
44 }
45
main(int argc,char ** argv)46 int main(int argc, char** argv)
47 {
48 Display* dpy = XOpenDisplay(0);
49 Visual* visual = DefaultVisual(dpy, DefaultScreen(dpy));
50 Colormap cmap = XCreateColormap(dpy, RootWindow(dpy, DefaultScreen(dpy)), visual, AllocNone);
51 XSetWindowAttributes swa;
52 swa.colormap = cmap;
53 swa.border_pixel = 0;
54 swa.event_mask = StructureNotifyMask;
55 width = 512;
56 height = 512;
57 Window win = XCreateWindow(dpy, RootWindow(dpy, DefaultScreen(dpy)), 0, 0, width, height, 0, CopyFromParent, InputOutput, visual, CWBorderPixel | CWColormap| CWEventMask, &swa);
58 XMapWindow(dpy, win);
59
60 GalliumDXGIUseX11Display(dpy, 0);
61
62 DXGI_SWAP_CHAIN_DESC swap_chain_desc;
63 memset(&swap_chain_desc, 0, sizeof(swap_chain_desc));
64 swap_chain_desc.BufferDesc.Width = width;
65 swap_chain_desc.BufferDesc.Height = height;
66 swap_chain_desc.BufferDesc.Format = format;
67 swap_chain_desc.SampleDesc.Count = 1;
68 swap_chain_desc.SampleDesc.Quality = 0;
69 swap_chain_desc.OutputWindow = (HWND)win;
70 swap_chain_desc.Windowed = TRUE;
71 swap_chain_desc.BufferCount = 3;
72 swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
73 swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
74
75 D3D10_FEATURE_LEVEL1 feature_level = D3D10_FEATURE_LEVEL_10_0;
76
77 HRESULT hr;
78 if(0)
79 {
80 hr = D3D10CreateDeviceAndSwapChain(
81 NULL,
82 D3D10_DRIVER_TYPE_HARDWARE,
83 NULL,
84 D3D10_CREATE_DEVICE_SINGLETHREADED,
85 D3D10_SDK_VERSION,
86 &swap_chain_desc,
87 &swap_chain,
88 &dev);
89 }
90 else
91 {
92 hr = D3D10CreateDeviceAndSwapChain1(
93 NULL,
94 D3D10_DRIVER_TYPE_HARDWARE,
95 NULL,
96 D3D10_CREATE_DEVICE_SINGLETHREADED,
97 feature_level,
98 D3D10_SDK_VERSION,
99 &swap_chain_desc,
100 &swap_chain,
101 (ID3D10Device1**)&dev);
102 }
103 if(!SUCCEEDED(hr))
104 {
105 fprintf(stderr, "Failed to create D3D10 device (hresult %08x)\n", hr);
106 return 1;
107 }
108 ctx = dev;
109
110 app = d3d10_application_create();
111 if(!app->init(dev, argc, argv))
112 return 1;
113
114 double start_time = get_time();
115
116 MSG msg;
117 for(;;)
118 {
119 XEvent event;
120 if(XPending(dpy))
121 {
122 XNextEvent(dpy, &event);
123 if(event.type == DestroyNotify)
124 break;
125 switch(event.type)
126 {
127 case ConfigureNotify:
128 width = event.xconfigure.width;
129 height = event.xconfigure.height;
130 swap_chain->ResizeBuffers(3, width, height, format, 0);
131 break;
132 }
133 }
134 else if(width && height)
135 {
136 ID3D10Texture2D* tex;
137 ID3D10RenderTargetView* rtv;
138 ensure(swap_chain->GetBuffer(0, IID_ID3D10Texture2D, (void**)&tex));
139 ensure(dev->CreateRenderTargetView(tex, NULL, &rtv));
140
141 double ctime = get_time() - start_time;
142
143 app->draw(ctx, rtv, width, height, ctime);
144 ctx->OMSetRenderTargets(0, 0, 0);
145
146 tex->Release();
147 rtv->Release();
148 swap_chain->Present(0, 0);
149 }
150 else
151 XPeekEvent(dpy, &event);
152 }
153 return (int) msg.wParam;
154 }
155