dx11test
changeset 3:aa1497adac80 tip
fixed line endings
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 21 Jun 2013 09:37:24 +0300 |
parents | 87ecd7292c23 |
children | |
files | shader.hlsl src/main.cc |
diffstat | 2 files changed, 371 insertions(+), 371 deletions(-) [+] |
line diff
1.1 --- a/shader.hlsl Fri Jun 21 09:09:09 2013 +0300 1.2 +++ b/shader.hlsl Fri Jun 21 09:37:24 2013 +0300 1.3 @@ -1,31 +1,31 @@ 1.4 -cbuffer RenderState : register(b0) { 1.5 - matrix modelview_matrix : packoffset(c0); 1.6 - matrix projection_matrix : packoffset(c4); 1.7 -}; 1.8 - 1.9 -struct VSInput { 1.10 - float4 pos : POSITION; 1.11 - float4 color : COLOR; 1.12 -}; 1.13 - 1.14 -struct VSOutput { 1.15 - float4 pos : SV_POSITION; 1.16 - float4 color : COLOR0; 1.17 -}; 1.18 - 1.19 -VSOutput vertex_main(VSInput input) 1.20 -{ 1.21 - VSOutput res; 1.22 - 1.23 - float4 vpos = mul(input.pos, modelview_matrix); 1.24 - 1.25 - res.pos = mul(vpos, projection_matrix); 1.26 - res.color = input.color; 1.27 - return res; 1.28 -} 1.29 - 1.30 - 1.31 -float4 pixel_main(VSOutput input) : SV_TARGET 1.32 -{ 1.33 - return input.color; 1.34 +cbuffer RenderState : register(b0) { 1.35 + matrix modelview_matrix : packoffset(c0); 1.36 + matrix projection_matrix : packoffset(c4); 1.37 +}; 1.38 + 1.39 +struct VSInput { 1.40 + float4 pos : POSITION; 1.41 + float4 color : COLOR; 1.42 +}; 1.43 + 1.44 +struct VSOutput { 1.45 + float4 pos : SV_POSITION; 1.46 + float4 color : COLOR0; 1.47 +}; 1.48 + 1.49 +VSOutput vertex_main(VSInput input) 1.50 +{ 1.51 + VSOutput res; 1.52 + 1.53 + float4 vpos = mul(input.pos, modelview_matrix); 1.54 + 1.55 + res.pos = mul(vpos, projection_matrix); 1.56 + res.color = input.color; 1.57 + return res; 1.58 +} 1.59 + 1.60 + 1.61 +float4 pixel_main(VSOutput input) : SV_TARGET 1.62 +{ 1.63 + return input.color; 1.64 } 1.65 \ No newline at end of file
2.1 --- a/src/main.cc Fri Jun 21 09:09:09 2013 +0300 2.2 +++ b/src/main.cc Fri Jun 21 09:37:24 2013 +0300 2.3 @@ -1,341 +1,341 @@ 2.4 -#include <stdio.h> 2.5 -#include <stdlib.h> 2.6 -#include <math.h> 2.7 -#include <stddef.h> 2.8 -#include <d3d11.h> 2.9 -#include <d3dx11.h> 2.10 - 2.11 -struct Vertex { 2.12 - float pos[3]; 2.13 - float color[4]; 2.14 -}; 2.15 - 2.16 -struct RenderState { 2.17 - float modelview[16]; 2.18 - float projection[16]; 2.19 -}; 2.20 - 2.21 -static bool init(); 2.22 -static void cleanup(); 2.23 -static void display(); 2.24 -static void reshape(int x, int y); 2.25 -static void keyb(int key, bool pressed); 2.26 -static HWND create_window(int xsz, int ysz); 2.27 -static void destroy_window(HWND win); 2.28 -static void main_loop(); 2.29 -static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam); 2.30 - 2.31 -static int width, height; 2.32 - 2.33 -static HWND win; 2.34 -static IDXGISwapChain *swap; 2.35 -static ID3D11Device *dev; 2.36 -static ID3D11DeviceContext *ctx; 2.37 -static ID3D11RenderTargetView *rtarg_view; 2.38 -static ID3D11InputLayout *vertex_layout; 2.39 -static ID3D11VertexShader *vsdr; 2.40 -static ID3D11PixelShader *psdr; 2.41 -static ID3D11Buffer *vbuf; 2.42 -static ID3D11Buffer *rstate_buf; 2.43 - 2.44 -static RenderState rstate; 2.45 - 2.46 -int main() 2.47 -{ 2.48 - if(!init()) { 2.49 - return 1; 2.50 - } 2.51 - atexit(cleanup); 2.52 - 2.53 - main_loop(); 2.54 - return 0; 2.55 -} 2.56 - 2.57 -static bool init() 2.58 -{ 2.59 - if(!(win = create_window(800, 600))) { 2.60 - return false; 2.61 - } 2.62 - 2.63 - unsigned int sdrflags = 0;//D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG; 2.64 - 2.65 - ID3DBlob *vsbuf, *psbuf, *msgblob; 2.66 - if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "vertex_main", "vs_4_0", sdrflags, 0, 0, &vsbuf, &msgblob, 0) != 0) { 2.67 - fprintf(stderr, "failed to load vertex shader\n"); 2.68 - if(msgblob->GetBufferSize() > 0) { 2.69 - fprintf(stderr, "Vertex Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 2.70 - } 2.71 - return false; 2.72 - } 2.73 - if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "pixel_main", "ps_4_0", sdrflags, 0, 0, &psbuf, &msgblob, 0) != 0) { 2.74 - fprintf(stderr, "failed to load pixel shader\n"); 2.75 - if(msgblob->GetBufferSize() > 0) { 2.76 - fprintf(stderr, "Pixel Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 2.77 - } 2.78 - return false; 2.79 - } 2.80 - 2.81 - if(dev->CreateVertexShader(vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), 0, &vsdr) != 0) { 2.82 - fprintf(stderr, "failed to create vertex shader\n"); 2.83 - return false; 2.84 - } 2.85 - if(dev->CreatePixelShader(psbuf->GetBufferPointer(), psbuf->GetBufferSize(), 0, &psdr) != 0) { 2.86 - fprintf(stderr, "failed to create pixel shader\n"); 2.87 - return false; 2.88 - } 2.89 - 2.90 - D3D11_INPUT_ELEMENT_DESC elem_desc[2]; 2.91 - elem_desc[0].SemanticName = "position"; 2.92 - elem_desc[0].SemanticIndex = 0; 2.93 - elem_desc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 2.94 - elem_desc[0].InputSlot = 0; 2.95 - elem_desc[0].AlignedByteOffset = 0; 2.96 - elem_desc[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 2.97 - elem_desc[0].InstanceDataStepRate = 0; 2.98 - 2.99 - elem_desc[1].SemanticName = "color"; 2.100 - elem_desc[1].SemanticIndex = 0; 2.101 - elem_desc[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 2.102 - elem_desc[1].InputSlot = 0; 2.103 - elem_desc[1].AlignedByteOffset = offsetof(Vertex, color); 2.104 - elem_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 2.105 - elem_desc[1].InstanceDataStepRate = 0; 2.106 - 2.107 - if(dev->CreateInputLayout(elem_desc, 2, vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), &vertex_layout) != 0) { 2.108 - fprintf(stderr, "failed to create vertex layout\n"); 2.109 - return 0; 2.110 - } 2.111 - vsbuf->Release(); 2.112 - psbuf->Release(); 2.113 - 2.114 - // --- create vertex buffer --- 2.115 - Vertex varr[] = { 2.116 - {{-0.6, -0.4, 0}, {1, 0, 0, 1}}, 2.117 - {{0.0, 0.6, 0}, {0, 1, 0, 1}}, 2.118 - {{0.6, -0.4, 0}, {0, 0, 1, 1}} 2.119 - }; 2.120 - 2.121 - D3D11_BUFFER_DESC buf_desc; 2.122 - memset(&buf_desc, 0, sizeof buf_desc); 2.123 - buf_desc.Usage = D3D11_USAGE_DEFAULT; 2.124 - buf_desc.ByteWidth = sizeof varr; 2.125 - buf_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 2.126 - 2.127 - D3D11_SUBRESOURCE_DATA subdata; 2.128 - memset(&subdata, 0, sizeof subdata); 2.129 - subdata.pSysMem = varr; 2.130 - if(dev->CreateBuffer(&buf_desc, &subdata, &vbuf) != 0) { 2.131 - fprintf(stderr, "failed to create vertex buffer\n"); 2.132 - return false; 2.133 - } 2.134 - 2.135 - // render state buffer 2.136 - memset(&buf_desc, 0, sizeof buf_desc); 2.137 - buf_desc.Usage = D3D11_USAGE_DEFAULT; 2.138 - buf_desc.ByteWidth = sizeof(RenderState); 2.139 - buf_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 2.140 - 2.141 - memset(&subdata, 0, sizeof subdata); 2.142 - subdata.pSysMem = &rstate; 2.143 - if(dev->CreateBuffer(&buf_desc, &subdata, &rstate_buf) != 0) { 2.144 - fprintf(stderr, "failed to create render state buffer\n"); 2.145 - return false; 2.146 - } 2.147 - 2.148 - return true; 2.149 -} 2.150 - 2.151 -static void cleanup() 2.152 -{ 2.153 - vbuf->Release(); 2.154 - rstate_buf->Release(); 2.155 - vsdr->Release(); 2.156 - psdr->Release(); 2.157 - vertex_layout->Release(); 2.158 - destroy_window(win); 2.159 -} 2.160 - 2.161 -static void set_identity(float *mat) 2.162 -{ 2.163 - mat[0] = mat[5] = mat[10] = mat[15] = 1.0; 2.164 - mat[1] = mat[2] = mat[3] = mat[4] = mat[6] = mat[7] = mat[8] = mat[9] = mat[11] = mat[12] = mat[13] = mat[14] = 0.0; 2.165 -} 2.166 - 2.167 -static void set_rotation_z(float *mat, float angle) 2.168 -{ 2.169 - set_identity(mat); 2.170 - 2.171 - mat[0] = cos(angle); 2.172 - mat[1] = -sin(angle); 2.173 - mat[4] = sin(angle); 2.174 - mat[5] = cos(angle); 2.175 -} 2.176 - 2.177 -static void set_ortho(float *mat, float aspect) 2.178 -{ 2.179 - set_identity(mat); 2.180 - mat[0] = 1.0 / aspect; 2.181 -} 2.182 - 2.183 -static void display() 2.184 -{ 2.185 - unsigned int msec = timeGetTime(); 2.186 - 2.187 - float fbcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; 2.188 - ctx->ClearRenderTargetView(rtarg_view, fbcolor); 2.189 - 2.190 - // set render state constant buffer data 2.191 - set_ortho(rstate.projection, (float)width / (float)height); 2.192 - set_rotation_z(rstate.modelview, msec / 1000.0); 2.193 - 2.194 - ctx->UpdateSubresource(rstate_buf, 0, 0, &rstate, 0, 0); 2.195 - ctx->VSSetConstantBuffers(0, 1, &rstate_buf); 2.196 - 2.197 - 2.198 - unsigned int stride = sizeof(Vertex); 2.199 - unsigned int offset = 0; 2.200 - ctx->IASetVertexBuffers(0, 1, &vbuf, &stride, &offset); 2.201 - ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 2.202 - ctx->IASetInputLayout(vertex_layout); 2.203 - 2.204 - ctx->VSSetShader(vsdr, 0, 0); 2.205 - ctx->PSSetShader(psdr, 0, 0); 2.206 - 2.207 - ctx->Draw(3, 0); 2.208 - 2.209 - swap->Present(0, 0); 2.210 -} 2.211 - 2.212 -static void reshape(int x, int y) 2.213 -{ 2.214 - width = x; 2.215 - height = y; 2.216 - 2.217 - D3D11_VIEWPORT vp; 2.218 - vp.Width = (float)x; 2.219 - vp.Height = (float)y; 2.220 - vp.MinDepth = 0; 2.221 - vp.MaxDepth = 1; 2.222 - vp.TopLeftX = 0; 2.223 - vp.TopLeftY = 0; 2.224 - ctx->RSSetViewports(1, &vp); 2.225 - 2.226 - // TODO probably we also need to resize render targets or whatever... 2.227 -} 2.228 - 2.229 -static void keyb(int key, bool pressed) 2.230 -{ 2.231 - if(key == 27) { 2.232 - exit(0); 2.233 - } 2.234 -} 2.235 - 2.236 -// ---- system crap ---- 2.237 - 2.238 -static HWND create_window(int xsz, int ysz) 2.239 -{ 2.240 - HINSTANCE app_inst = GetModuleHandle(0); 2.241 - 2.242 - WNDCLASS wclass; 2.243 - memset(&wclass, 0, sizeof wclass); 2.244 - wclass.hInstance = app_inst; 2.245 - wclass.lpfnWndProc = win_proc; 2.246 - wclass.lpszClassName = "mutantstargoatwin"; 2.247 - wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 2.248 - wclass.hIcon = LoadIcon(0, IDI_APPLICATION); 2.249 - wclass.hCursor = LoadCursor(0, IDC_ARROW); 2.250 - wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 2.251 - RegisterClass(&wclass); 2.252 - 2.253 - int posx = (GetSystemMetrics(SM_CXSCREEN) - xsz) / 2; 2.254 - int posy = (GetSystemMetrics(SM_CYSCREEN) - ysz) / 2; 2.255 - 2.256 - HWND win = CreateWindow("mutantstargoatwin", "DX11 Test", WS_OVERLAPPEDWINDOW, posx, posy, 2.257 - xsz, ysz, 0, 0, app_inst, 0); 2.258 - ShowWindow(win, SW_SHOW); 2.259 - 2.260 - // initialize D3D device 2.261 - DXGI_SWAP_CHAIN_DESC swap_desc; 2.262 - memset(&swap_desc, 0, sizeof swap_desc); 2.263 - swap_desc.BufferCount = 1; 2.264 - swap_desc.BufferDesc.Width = xsz; 2.265 - swap_desc.BufferDesc.Height = ysz; 2.266 - swap_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 2.267 - swap_desc.BufferDesc.RefreshRate.Numerator = 60; 2.268 - swap_desc.BufferDesc.RefreshRate.Denominator = 1; 2.269 - swap_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 2.270 - swap_desc.OutputWindow = win; 2.271 - swap_desc.SampleDesc.Count = 1; 2.272 - swap_desc.SampleDesc.Quality = 0; 2.273 - swap_desc.Windowed = 1; 2.274 - 2.275 - D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; 2.276 - 2.277 - if(D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, &feature_level, 1, 2.278 - D3D11_SDK_VERSION, &swap_desc, &swap, &dev, 0, &ctx) != 0) { 2.279 - fprintf(stderr, "Failed to create d3d device and swap chain\n"); 2.280 - return 0; 2.281 - } 2.282 - 2.283 - ID3D11Texture2D *rtex; 2.284 - if(swap->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&rtex) != 0) { 2.285 - fprintf(stderr, "Failed to get default render target texture\n"); 2.286 - return 0; 2.287 - } 2.288 - if(dev->CreateRenderTargetView(rtex, 0, &rtarg_view) != 0) { 2.289 - fprintf(stderr, "Failed to create render target view\n"); 2.290 - rtex->Release(); 2.291 - return 0; 2.292 - } 2.293 - rtex->Release(); 2.294 - ctx->OMSetRenderTargets(1, &rtarg_view, 0); 2.295 - 2.296 - reshape(xsz, ysz); 2.297 - return win; 2.298 -} 2.299 - 2.300 -static void destroy_window(HWND win) 2.301 -{ 2.302 - CloseWindow(win); 2.303 - UnregisterClass("mutantstargoatwin", GetModuleHandle(0)); 2.304 - 2.305 - rtarg_view->Release(); 2.306 - ctx->Release(); 2.307 - dev->Release(); 2.308 - swap->Release(); 2.309 -} 2.310 - 2.311 -static void main_loop() 2.312 -{ 2.313 - MSG msg; 2.314 - 2.315 - for(;;) { 2.316 - while(PeekMessage(&msg, win, 0, 0, PM_REMOVE)) { 2.317 - TranslateMessage(&msg); 2.318 - DispatchMessage(&msg); 2.319 - 2.320 - if(msg.message == WM_QUIT) { 2.321 - return; 2.322 - } 2.323 - } 2.324 - 2.325 - display(); 2.326 - } 2.327 -} 2.328 - 2.329 -static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam) 2.330 -{ 2.331 - switch(msg) { 2.332 - case WM_KEYDOWN: 2.333 - keyb(wparam, true); 2.334 - break; 2.335 - 2.336 - case WM_KEYUP: 2.337 - keyb(wparam, false); 2.338 - break; 2.339 - 2.340 - default: 2.341 - return DefWindowProc(win, msg, wparam, lparam); 2.342 - } 2.343 - return 0; 2.344 -} 2.345 +#include <stdio.h> 2.346 +#include <stdlib.h> 2.347 +#include <math.h> 2.348 +#include <stddef.h> 2.349 +#include <d3d11.h> 2.350 +#include <d3dx11.h> 2.351 + 2.352 +struct Vertex { 2.353 + float pos[3]; 2.354 + float color[4]; 2.355 +}; 2.356 + 2.357 +struct RenderState { 2.358 + float modelview[16]; 2.359 + float projection[16]; 2.360 +}; 2.361 + 2.362 +static bool init(); 2.363 +static void cleanup(); 2.364 +static void display(); 2.365 +static void reshape(int x, int y); 2.366 +static void keyb(int key, bool pressed); 2.367 +static HWND create_window(int xsz, int ysz); 2.368 +static void destroy_window(HWND win); 2.369 +static void main_loop(); 2.370 +static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam); 2.371 + 2.372 +static int width, height; 2.373 + 2.374 +static HWND win; 2.375 +static IDXGISwapChain *swap; 2.376 +static ID3D11Device *dev; 2.377 +static ID3D11DeviceContext *ctx; 2.378 +static ID3D11RenderTargetView *rtarg_view; 2.379 +static ID3D11InputLayout *vertex_layout; 2.380 +static ID3D11VertexShader *vsdr; 2.381 +static ID3D11PixelShader *psdr; 2.382 +static ID3D11Buffer *vbuf; 2.383 +static ID3D11Buffer *rstate_buf; 2.384 + 2.385 +static RenderState rstate; 2.386 + 2.387 +int main() 2.388 +{ 2.389 + if(!init()) { 2.390 + return 1; 2.391 + } 2.392 + atexit(cleanup); 2.393 + 2.394 + main_loop(); 2.395 + return 0; 2.396 +} 2.397 + 2.398 +static bool init() 2.399 +{ 2.400 + if(!(win = create_window(800, 600))) { 2.401 + return false; 2.402 + } 2.403 + 2.404 + unsigned int sdrflags = 0;//D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG; 2.405 + 2.406 + ID3DBlob *vsbuf, *psbuf, *msgblob; 2.407 + if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "vertex_main", "vs_4_0", sdrflags, 0, 0, &vsbuf, &msgblob, 0) != 0) { 2.408 + fprintf(stderr, "failed to load vertex shader\n"); 2.409 + if(msgblob->GetBufferSize() > 0) { 2.410 + fprintf(stderr, "Vertex Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 2.411 + } 2.412 + return false; 2.413 + } 2.414 + if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "pixel_main", "ps_4_0", sdrflags, 0, 0, &psbuf, &msgblob, 0) != 0) { 2.415 + fprintf(stderr, "failed to load pixel shader\n"); 2.416 + if(msgblob->GetBufferSize() > 0) { 2.417 + fprintf(stderr, "Pixel Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 2.418 + } 2.419 + return false; 2.420 + } 2.421 + 2.422 + if(dev->CreateVertexShader(vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), 0, &vsdr) != 0) { 2.423 + fprintf(stderr, "failed to create vertex shader\n"); 2.424 + return false; 2.425 + } 2.426 + if(dev->CreatePixelShader(psbuf->GetBufferPointer(), psbuf->GetBufferSize(), 0, &psdr) != 0) { 2.427 + fprintf(stderr, "failed to create pixel shader\n"); 2.428 + return false; 2.429 + } 2.430 + 2.431 + D3D11_INPUT_ELEMENT_DESC elem_desc[2]; 2.432 + elem_desc[0].SemanticName = "position"; 2.433 + elem_desc[0].SemanticIndex = 0; 2.434 + elem_desc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 2.435 + elem_desc[0].InputSlot = 0; 2.436 + elem_desc[0].AlignedByteOffset = 0; 2.437 + elem_desc[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 2.438 + elem_desc[0].InstanceDataStepRate = 0; 2.439 + 2.440 + elem_desc[1].SemanticName = "color"; 2.441 + elem_desc[1].SemanticIndex = 0; 2.442 + elem_desc[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 2.443 + elem_desc[1].InputSlot = 0; 2.444 + elem_desc[1].AlignedByteOffset = offsetof(Vertex, color); 2.445 + elem_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 2.446 + elem_desc[1].InstanceDataStepRate = 0; 2.447 + 2.448 + if(dev->CreateInputLayout(elem_desc, 2, vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), &vertex_layout) != 0) { 2.449 + fprintf(stderr, "failed to create vertex layout\n"); 2.450 + return 0; 2.451 + } 2.452 + vsbuf->Release(); 2.453 + psbuf->Release(); 2.454 + 2.455 + // --- create vertex buffer --- 2.456 + Vertex varr[] = { 2.457 + {{-0.6, -0.4, 0}, {1, 0, 0, 1}}, 2.458 + {{0.0, 0.6, 0}, {0, 1, 0, 1}}, 2.459 + {{0.6, -0.4, 0}, {0, 0, 1, 1}} 2.460 + }; 2.461 + 2.462 + D3D11_BUFFER_DESC buf_desc; 2.463 + memset(&buf_desc, 0, sizeof buf_desc); 2.464 + buf_desc.Usage = D3D11_USAGE_DEFAULT; 2.465 + buf_desc.ByteWidth = sizeof varr; 2.466 + buf_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 2.467 + 2.468 + D3D11_SUBRESOURCE_DATA subdata; 2.469 + memset(&subdata, 0, sizeof subdata); 2.470 + subdata.pSysMem = varr; 2.471 + if(dev->CreateBuffer(&buf_desc, &subdata, &vbuf) != 0) { 2.472 + fprintf(stderr, "failed to create vertex buffer\n"); 2.473 + return false; 2.474 + } 2.475 + 2.476 + // render state buffer 2.477 + memset(&buf_desc, 0, sizeof buf_desc); 2.478 + buf_desc.Usage = D3D11_USAGE_DEFAULT; 2.479 + buf_desc.ByteWidth = sizeof(RenderState); 2.480 + buf_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 2.481 + 2.482 + memset(&subdata, 0, sizeof subdata); 2.483 + subdata.pSysMem = &rstate; 2.484 + if(dev->CreateBuffer(&buf_desc, &subdata, &rstate_buf) != 0) { 2.485 + fprintf(stderr, "failed to create render state buffer\n"); 2.486 + return false; 2.487 + } 2.488 + 2.489 + return true; 2.490 +} 2.491 + 2.492 +static void cleanup() 2.493 +{ 2.494 + vbuf->Release(); 2.495 + rstate_buf->Release(); 2.496 + vsdr->Release(); 2.497 + psdr->Release(); 2.498 + vertex_layout->Release(); 2.499 + destroy_window(win); 2.500 +} 2.501 + 2.502 +static void set_identity(float *mat) 2.503 +{ 2.504 + mat[0] = mat[5] = mat[10] = mat[15] = 1.0; 2.505 + mat[1] = mat[2] = mat[3] = mat[4] = mat[6] = mat[7] = mat[8] = mat[9] = mat[11] = mat[12] = mat[13] = mat[14] = 0.0; 2.506 +} 2.507 + 2.508 +static void set_rotation_z(float *mat, float angle) 2.509 +{ 2.510 + set_identity(mat); 2.511 + 2.512 + mat[0] = cos(angle); 2.513 + mat[1] = -sin(angle); 2.514 + mat[4] = sin(angle); 2.515 + mat[5] = cos(angle); 2.516 +} 2.517 + 2.518 +static void set_ortho(float *mat, float aspect) 2.519 +{ 2.520 + set_identity(mat); 2.521 + mat[0] = 1.0 / aspect; 2.522 +} 2.523 + 2.524 +static void display() 2.525 +{ 2.526 + unsigned int msec = timeGetTime(); 2.527 + 2.528 + float fbcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; 2.529 + ctx->ClearRenderTargetView(rtarg_view, fbcolor); 2.530 + 2.531 + // set render state constant buffer data 2.532 + set_ortho(rstate.projection, (float)width / (float)height); 2.533 + set_rotation_z(rstate.modelview, msec / 1000.0); 2.534 + 2.535 + ctx->UpdateSubresource(rstate_buf, 0, 0, &rstate, 0, 0); 2.536 + ctx->VSSetConstantBuffers(0, 1, &rstate_buf); 2.537 + 2.538 + 2.539 + unsigned int stride = sizeof(Vertex); 2.540 + unsigned int offset = 0; 2.541 + ctx->IASetVertexBuffers(0, 1, &vbuf, &stride, &offset); 2.542 + ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 2.543 + ctx->IASetInputLayout(vertex_layout); 2.544 + 2.545 + ctx->VSSetShader(vsdr, 0, 0); 2.546 + ctx->PSSetShader(psdr, 0, 0); 2.547 + 2.548 + ctx->Draw(3, 0); 2.549 + 2.550 + swap->Present(0, 0); 2.551 +} 2.552 + 2.553 +static void reshape(int x, int y) 2.554 +{ 2.555 + width = x; 2.556 + height = y; 2.557 + 2.558 + D3D11_VIEWPORT vp; 2.559 + vp.Width = (float)x; 2.560 + vp.Height = (float)y; 2.561 + vp.MinDepth = 0; 2.562 + vp.MaxDepth = 1; 2.563 + vp.TopLeftX = 0; 2.564 + vp.TopLeftY = 0; 2.565 + ctx->RSSetViewports(1, &vp); 2.566 + 2.567 + // TODO probably we also need to resize render targets or whatever... 2.568 +} 2.569 + 2.570 +static void keyb(int key, bool pressed) 2.571 +{ 2.572 + if(key == 27) { 2.573 + exit(0); 2.574 + } 2.575 +} 2.576 + 2.577 +// ---- system crap ---- 2.578 + 2.579 +static HWND create_window(int xsz, int ysz) 2.580 +{ 2.581 + HINSTANCE app_inst = GetModuleHandle(0); 2.582 + 2.583 + WNDCLASS wclass; 2.584 + memset(&wclass, 0, sizeof wclass); 2.585 + wclass.hInstance = app_inst; 2.586 + wclass.lpfnWndProc = win_proc; 2.587 + wclass.lpszClassName = "mutantstargoatwin"; 2.588 + wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 2.589 + wclass.hIcon = LoadIcon(0, IDI_APPLICATION); 2.590 + wclass.hCursor = LoadCursor(0, IDC_ARROW); 2.591 + wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 2.592 + RegisterClass(&wclass); 2.593 + 2.594 + int posx = (GetSystemMetrics(SM_CXSCREEN) - xsz) / 2; 2.595 + int posy = (GetSystemMetrics(SM_CYSCREEN) - ysz) / 2; 2.596 + 2.597 + HWND win = CreateWindow("mutantstargoatwin", "DX11 Test", WS_OVERLAPPEDWINDOW, posx, posy, 2.598 + xsz, ysz, 0, 0, app_inst, 0); 2.599 + ShowWindow(win, SW_SHOW); 2.600 + 2.601 + // initialize D3D device 2.602 + DXGI_SWAP_CHAIN_DESC swap_desc; 2.603 + memset(&swap_desc, 0, sizeof swap_desc); 2.604 + swap_desc.BufferCount = 1; 2.605 + swap_desc.BufferDesc.Width = xsz; 2.606 + swap_desc.BufferDesc.Height = ysz; 2.607 + swap_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 2.608 + swap_desc.BufferDesc.RefreshRate.Numerator = 60; 2.609 + swap_desc.BufferDesc.RefreshRate.Denominator = 1; 2.610 + swap_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 2.611 + swap_desc.OutputWindow = win; 2.612 + swap_desc.SampleDesc.Count = 1; 2.613 + swap_desc.SampleDesc.Quality = 0; 2.614 + swap_desc.Windowed = 1; 2.615 + 2.616 + D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; 2.617 + 2.618 + if(D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, &feature_level, 1, 2.619 + D3D11_SDK_VERSION, &swap_desc, &swap, &dev, 0, &ctx) != 0) { 2.620 + fprintf(stderr, "Failed to create d3d device and swap chain\n"); 2.621 + return 0; 2.622 + } 2.623 + 2.624 + ID3D11Texture2D *rtex; 2.625 + if(swap->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&rtex) != 0) { 2.626 + fprintf(stderr, "Failed to get default render target texture\n"); 2.627 + return 0; 2.628 + } 2.629 + if(dev->CreateRenderTargetView(rtex, 0, &rtarg_view) != 0) { 2.630 + fprintf(stderr, "Failed to create render target view\n"); 2.631 + rtex->Release(); 2.632 + return 0; 2.633 + } 2.634 + rtex->Release(); 2.635 + ctx->OMSetRenderTargets(1, &rtarg_view, 0); 2.636 + 2.637 + reshape(xsz, ysz); 2.638 + return win; 2.639 +} 2.640 + 2.641 +static void destroy_window(HWND win) 2.642 +{ 2.643 + CloseWindow(win); 2.644 + UnregisterClass("mutantstargoatwin", GetModuleHandle(0)); 2.645 + 2.646 + rtarg_view->Release(); 2.647 + ctx->Release(); 2.648 + dev->Release(); 2.649 + swap->Release(); 2.650 +} 2.651 + 2.652 +static void main_loop() 2.653 +{ 2.654 + MSG msg; 2.655 + 2.656 + for(;;) { 2.657 + while(PeekMessage(&msg, win, 0, 0, PM_REMOVE)) { 2.658 + TranslateMessage(&msg); 2.659 + DispatchMessage(&msg); 2.660 + 2.661 + if(msg.message == WM_QUIT) { 2.662 + return; 2.663 + } 2.664 + } 2.665 + 2.666 + display(); 2.667 + } 2.668 +} 2.669 + 2.670 +static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam) 2.671 +{ 2.672 + switch(msg) { 2.673 + case WM_KEYDOWN: 2.674 + keyb(wparam, true); 2.675 + break; 2.676 + 2.677 + case WM_KEYUP: 2.678 + keyb(wparam, false); 2.679 + break; 2.680 + 2.681 + default: 2.682 + return DefWindowProc(win, msg, wparam, lparam); 2.683 + } 2.684 + return 0; 2.685 +}