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