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