dx11test
changeset 1:ec02798ee83b
moved the source files into the src dir, I hope I edited the project file correctly with the text editor
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 21 Jun 2013 07:42:07 +0300 |
parents | 647ba0689512 |
children | 87ecd7292c23 |
files | dx11test.vcxproj dx11test.vcxproj.filters main.cc src/main.cc src/vec.h vec.h |
diffstat | 6 files changed, 372 insertions(+), 376 deletions(-) [+] |
line diff
1.1 --- a/dx11test.vcxproj Fri Jun 21 07:33:06 2013 +0300 1.2 +++ b/dx11test.vcxproj Fri Jun 21 07:42:07 2013 +0300 1.3 @@ -78,11 +78,10 @@ 1.4 </Link> 1.5 </ItemDefinitionGroup> 1.6 <ItemGroup> 1.7 - <ClCompile Include="main.cc" /> 1.8 - <ClCompile Include="vec.cc" /> 1.9 + <ClCompile Include="src\main.cc" /> 1.10 </ItemGroup> 1.11 <ItemGroup> 1.12 - <ClInclude Include="vec.h" /> 1.13 + <ClInclude Include="src\vec.h" /> 1.14 </ItemGroup> 1.15 <ItemGroup> 1.16 <None Include="shader.hlsl"> 1.17 @@ -92,4 +91,4 @@ 1.18 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> 1.19 <ImportGroup Label="ExtensionTargets"> 1.20 </ImportGroup> 1.21 -</Project> 1.22 \ No newline at end of file 1.23 +</Project>
2.1 --- a/dx11test.vcxproj.filters Fri Jun 21 07:33:06 2013 +0300 2.2 +++ b/dx11test.vcxproj.filters Fri Jun 21 07:42:07 2013 +0300 2.3 @@ -15,19 +15,16 @@ 2.4 </Filter> 2.5 </ItemGroup> 2.6 <ItemGroup> 2.7 - <ClCompile Include="main.cc"> 2.8 - <Filter>Source Files</Filter> 2.9 - </ClCompile> 2.10 - <ClCompile Include="vec.cc"> 2.11 + <ClCompile Include="src\main.cc"> 2.12 <Filter>Source Files</Filter> 2.13 </ClCompile> 2.14 </ItemGroup> 2.15 <ItemGroup> 2.16 - <ClInclude Include="vec.h"> 2.17 + <ClInclude Include="src\vec.h"> 2.18 <Filter>Header Files</Filter> 2.19 </ClInclude> 2.20 </ItemGroup> 2.21 <ItemGroup> 2.22 <None Include="shader.hlsl" /> 2.23 </ItemGroup> 2.24 -</Project> 2.25 \ No newline at end of file 2.26 +</Project>
3.1 --- a/main.cc Fri Jun 21 07:33:06 2013 +0300 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,342 +0,0 @@ 3.4 -#include <stdio.h> 3.5 -#include <stdlib.h> 3.6 -#include <math.h> 3.7 -#include <stddef.h> 3.8 -#include <d3d11.h> 3.9 -#include <d3dx11.h> 3.10 -#include "vec.h" 3.11 - 3.12 -struct Vertex { 3.13 - float pos[3]; 3.14 - float color[4]; 3.15 -}; 3.16 - 3.17 -struct RenderState { 3.18 - float modelview[16]; 3.19 - float projection[16]; 3.20 -}; 3.21 - 3.22 -static bool init(); 3.23 -static void cleanup(); 3.24 -static void display(); 3.25 -static void reshape(int x, int y); 3.26 -static void keyb(int key, bool pressed); 3.27 -static HWND create_window(int xsz, int ysz); 3.28 -static void destroy_window(HWND win); 3.29 -static void main_loop(); 3.30 -static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam); 3.31 - 3.32 -static int width, height; 3.33 - 3.34 -static HWND win; 3.35 -static IDXGISwapChain *swap; 3.36 -static ID3D11Device *dev; 3.37 -static ID3D11DeviceContext *ctx; 3.38 -static ID3D11RenderTargetView *rtarg_view; 3.39 -static ID3D11InputLayout *vertex_layout; 3.40 -static ID3D11VertexShader *vsdr; 3.41 -static ID3D11PixelShader *psdr; 3.42 -static ID3D11Buffer *vbuf; 3.43 -static ID3D11Buffer *rstate_buf; 3.44 - 3.45 -static RenderState rstate; 3.46 - 3.47 -int main() 3.48 -{ 3.49 - if(!init()) { 3.50 - return 1; 3.51 - } 3.52 - atexit(cleanup); 3.53 - 3.54 - main_loop(); 3.55 - return 0; 3.56 -} 3.57 - 3.58 -static bool init() 3.59 -{ 3.60 - if(!(win = create_window(800, 600))) { 3.61 - return false; 3.62 - } 3.63 - 3.64 - unsigned int sdrflags = 0;//D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG; 3.65 - 3.66 - ID3DBlob *vsbuf, *psbuf, *msgblob; 3.67 - if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "vertex_main", "vs_4_0", sdrflags, 0, 0, &vsbuf, &msgblob, 0) != 0) { 3.68 - fprintf(stderr, "failed to load vertex shader\n"); 3.69 - if(msgblob->GetBufferSize() > 0) { 3.70 - fprintf(stderr, "Vertex Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 3.71 - } 3.72 - return false; 3.73 - } 3.74 - if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "pixel_main", "ps_4_0", sdrflags, 0, 0, &psbuf, &msgblob, 0) != 0) { 3.75 - fprintf(stderr, "failed to load pixel shader\n"); 3.76 - if(msgblob->GetBufferSize() > 0) { 3.77 - fprintf(stderr, "Pixel Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 3.78 - } 3.79 - return false; 3.80 - } 3.81 - 3.82 - if(dev->CreateVertexShader(vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), 0, &vsdr) != 0) { 3.83 - fprintf(stderr, "failed to create vertex shader\n"); 3.84 - return false; 3.85 - } 3.86 - if(dev->CreatePixelShader(psbuf->GetBufferPointer(), psbuf->GetBufferSize(), 0, &psdr) != 0) { 3.87 - fprintf(stderr, "failed to create pixel shader\n"); 3.88 - return false; 3.89 - } 3.90 - 3.91 - D3D11_INPUT_ELEMENT_DESC elem_desc[2]; 3.92 - elem_desc[0].SemanticName = "position"; 3.93 - elem_desc[0].SemanticIndex = 0; 3.94 - elem_desc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 3.95 - elem_desc[0].InputSlot = 0; 3.96 - elem_desc[0].AlignedByteOffset = 0; 3.97 - elem_desc[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 3.98 - elem_desc[0].InstanceDataStepRate = 0; 3.99 - 3.100 - elem_desc[1].SemanticName = "color"; 3.101 - elem_desc[1].SemanticIndex = 0; 3.102 - elem_desc[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 3.103 - elem_desc[1].InputSlot = 0; 3.104 - elem_desc[1].AlignedByteOffset = offsetof(Vertex, color); 3.105 - elem_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 3.106 - elem_desc[1].InstanceDataStepRate = 0; 3.107 - 3.108 - if(dev->CreateInputLayout(elem_desc, 2, vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), &vertex_layout) != 0) { 3.109 - fprintf(stderr, "failed to create vertex layout\n"); 3.110 - return 0; 3.111 - } 3.112 - vsbuf->Release(); 3.113 - psbuf->Release(); 3.114 - 3.115 - // --- create vertex buffer --- 3.116 - Vertex varr[] = { 3.117 - {{-0.6, -0.4, 0}, {1, 0, 0, 1}}, 3.118 - {{0.0, 0.6, 0}, {0, 1, 0, 1}}, 3.119 - {{0.6, -0.4, 0}, {0, 0, 1, 1}} 3.120 - }; 3.121 - 3.122 - D3D11_BUFFER_DESC buf_desc; 3.123 - memset(&buf_desc, 0, sizeof buf_desc); 3.124 - buf_desc.Usage = D3D11_USAGE_DEFAULT; 3.125 - buf_desc.ByteWidth = sizeof varr; 3.126 - buf_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 3.127 - 3.128 - D3D11_SUBRESOURCE_DATA subdata; 3.129 - memset(&subdata, 0, sizeof subdata); 3.130 - subdata.pSysMem = varr; 3.131 - if(dev->CreateBuffer(&buf_desc, &subdata, &vbuf) != 0) { 3.132 - fprintf(stderr, "failed to create vertex buffer\n"); 3.133 - return false; 3.134 - } 3.135 - 3.136 - // render state buffer 3.137 - memset(&buf_desc, 0, sizeof buf_desc); 3.138 - buf_desc.Usage = D3D11_USAGE_DEFAULT; 3.139 - buf_desc.ByteWidth = sizeof(RenderState); 3.140 - buf_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 3.141 - 3.142 - memset(&subdata, 0, sizeof subdata); 3.143 - subdata.pSysMem = &rstate; 3.144 - if(dev->CreateBuffer(&buf_desc, &subdata, &rstate_buf) != 0) { 3.145 - fprintf(stderr, "failed to create render state buffer\n"); 3.146 - return false; 3.147 - } 3.148 - 3.149 - return true; 3.150 -} 3.151 - 3.152 -static void cleanup() 3.153 -{ 3.154 - vbuf->Release(); 3.155 - rstate_buf->Release(); 3.156 - vsdr->Release(); 3.157 - psdr->Release(); 3.158 - vertex_layout->Release(); 3.159 - destroy_window(win); 3.160 -} 3.161 - 3.162 -static void set_identity(float *mat) 3.163 -{ 3.164 - mat[0] = mat[5] = mat[10] = mat[15] = 1.0; 3.165 - 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; 3.166 -} 3.167 - 3.168 -static void set_rotation_z(float *mat, float angle) 3.169 -{ 3.170 - set_identity(mat); 3.171 - 3.172 - mat[0] = cos(angle); 3.173 - mat[1] = -sin(angle); 3.174 - mat[4] = sin(angle); 3.175 - mat[5] = cos(angle); 3.176 -} 3.177 - 3.178 -static void set_ortho(float *mat, float aspect) 3.179 -{ 3.180 - set_identity(mat); 3.181 - mat[0] = 1.0 / aspect; 3.182 -} 3.183 - 3.184 -static void display() 3.185 -{ 3.186 - unsigned int msec = timeGetTime(); 3.187 - 3.188 - float fbcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; 3.189 - ctx->ClearRenderTargetView(rtarg_view, fbcolor); 3.190 - 3.191 - // set render state constant buffer data 3.192 - set_ortho(rstate.projection, (float)width / (float)height); 3.193 - set_rotation_z(rstate.modelview, msec / 1000.0); 3.194 - 3.195 - ctx->UpdateSubresource(rstate_buf, 0, 0, &rstate, 0, 0); 3.196 - ctx->VSSetConstantBuffers(0, 1, &rstate_buf); 3.197 - 3.198 - 3.199 - unsigned int stride = sizeof(Vertex); 3.200 - unsigned int offset = 0; 3.201 - ctx->IASetVertexBuffers(0, 1, &vbuf, &stride, &offset); 3.202 - ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 3.203 - ctx->IASetInputLayout(vertex_layout); 3.204 - 3.205 - ctx->VSSetShader(vsdr, 0, 0); 3.206 - ctx->PSSetShader(psdr, 0, 0); 3.207 - 3.208 - ctx->Draw(3, 0); 3.209 - 3.210 - swap->Present(0, 0); 3.211 -} 3.212 - 3.213 -static void reshape(int x, int y) 3.214 -{ 3.215 - width = x; 3.216 - height = y; 3.217 - 3.218 - D3D11_VIEWPORT vp; 3.219 - vp.Width = (float)x; 3.220 - vp.Height = (float)y; 3.221 - vp.MinDepth = 0; 3.222 - vp.MaxDepth = 1; 3.223 - vp.TopLeftX = 0; 3.224 - vp.TopLeftY = 0; 3.225 - ctx->RSSetViewports(1, &vp); 3.226 - 3.227 - // TODO probably we also need to resize render targets or whatever... 3.228 -} 3.229 - 3.230 -static void keyb(int key, bool pressed) 3.231 -{ 3.232 - if(key == 27) { 3.233 - exit(0); 3.234 - } 3.235 -} 3.236 - 3.237 -// ---- system crap ---- 3.238 - 3.239 -static HWND create_window(int xsz, int ysz) 3.240 -{ 3.241 - HINSTANCE app_inst = GetModuleHandle(0); 3.242 - 3.243 - WNDCLASS wclass; 3.244 - memset(&wclass, 0, sizeof wclass); 3.245 - wclass.hInstance = app_inst; 3.246 - wclass.lpfnWndProc = win_proc; 3.247 - wclass.lpszClassName = "mutantstargoatwin"; 3.248 - wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 3.249 - wclass.hIcon = LoadIcon(0, IDI_APPLICATION); 3.250 - wclass.hCursor = LoadCursor(0, IDC_ARROW); 3.251 - wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 3.252 - RegisterClass(&wclass); 3.253 - 3.254 - int posx = (GetSystemMetrics(SM_CXSCREEN) - xsz) / 2; 3.255 - int posy = (GetSystemMetrics(SM_CYSCREEN) - ysz) / 2; 3.256 - 3.257 - HWND win = CreateWindow("mutantstargoatwin", "DX11 Test", WS_OVERLAPPEDWINDOW, posx, posy, 3.258 - xsz, ysz, 0, 0, app_inst, 0); 3.259 - ShowWindow(win, SW_SHOW); 3.260 - 3.261 - // initialize D3D device 3.262 - DXGI_SWAP_CHAIN_DESC swap_desc; 3.263 - memset(&swap_desc, 0, sizeof swap_desc); 3.264 - swap_desc.BufferCount = 1; 3.265 - swap_desc.BufferDesc.Width = xsz; 3.266 - swap_desc.BufferDesc.Height = ysz; 3.267 - swap_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 3.268 - swap_desc.BufferDesc.RefreshRate.Numerator = 60; 3.269 - swap_desc.BufferDesc.RefreshRate.Denominator = 1; 3.270 - swap_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 3.271 - swap_desc.OutputWindow = win; 3.272 - swap_desc.SampleDesc.Count = 1; 3.273 - swap_desc.SampleDesc.Quality = 0; 3.274 - swap_desc.Windowed = 1; 3.275 - 3.276 - D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; 3.277 - 3.278 - if(D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, &feature_level, 1, 3.279 - D3D11_SDK_VERSION, &swap_desc, &swap, &dev, 0, &ctx) != 0) { 3.280 - fprintf(stderr, "Failed to create d3d device and swap chain\n"); 3.281 - return 0; 3.282 - } 3.283 - 3.284 - ID3D11Texture2D *rtex; 3.285 - if(swap->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&rtex) != 0) { 3.286 - fprintf(stderr, "Failed to get default render target texture\n"); 3.287 - return 0; 3.288 - } 3.289 - if(dev->CreateRenderTargetView(rtex, 0, &rtarg_view) != 0) { 3.290 - fprintf(stderr, "Failed to create render target view\n"); 3.291 - rtex->Release(); 3.292 - return 0; 3.293 - } 3.294 - rtex->Release(); 3.295 - ctx->OMSetRenderTargets(1, &rtarg_view, 0); 3.296 - 3.297 - reshape(xsz, ysz); 3.298 - return win; 3.299 -} 3.300 - 3.301 -static void destroy_window(HWND win) 3.302 -{ 3.303 - CloseWindow(win); 3.304 - UnregisterClass("mutantstargoatwin", GetModuleHandle(0)); 3.305 - 3.306 - rtarg_view->Release(); 3.307 - ctx->Release(); 3.308 - dev->Release(); 3.309 - swap->Release(); 3.310 -} 3.311 - 3.312 -static void main_loop() 3.313 -{ 3.314 - MSG msg; 3.315 - 3.316 - for(;;) { 3.317 - while(PeekMessage(&msg, win, 0, 0, PM_REMOVE)) { 3.318 - TranslateMessage(&msg); 3.319 - DispatchMessage(&msg); 3.320 - 3.321 - if(msg.message == WM_QUIT) { 3.322 - return; 3.323 - } 3.324 - } 3.325 - 3.326 - display(); 3.327 - } 3.328 -} 3.329 - 3.330 -static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam) 3.331 -{ 3.332 - switch(msg) { 3.333 - case WM_KEYDOWN: 3.334 - keyb(wparam, true); 3.335 - break; 3.336 - 3.337 - case WM_KEYUP: 3.338 - keyb(wparam, false); 3.339 - break; 3.340 - 3.341 - default: 3.342 - return DefWindowProc(win, msg, wparam, lparam); 3.343 - } 3.344 - return 0; 3.345 -} 3.346 \ No newline at end of file
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/main.cc Fri Jun 21 07:42:07 2013 +0300 4.3 @@ -0,0 +1,342 @@ 4.4 +#include <stdio.h> 4.5 +#include <stdlib.h> 4.6 +#include <math.h> 4.7 +#include <stddef.h> 4.8 +#include <d3d11.h> 4.9 +#include <d3dx11.h> 4.10 +#include "vec.h" 4.11 + 4.12 +struct Vertex { 4.13 + float pos[3]; 4.14 + float color[4]; 4.15 +}; 4.16 + 4.17 +struct RenderState { 4.18 + float modelview[16]; 4.19 + float projection[16]; 4.20 +}; 4.21 + 4.22 +static bool init(); 4.23 +static void cleanup(); 4.24 +static void display(); 4.25 +static void reshape(int x, int y); 4.26 +static void keyb(int key, bool pressed); 4.27 +static HWND create_window(int xsz, int ysz); 4.28 +static void destroy_window(HWND win); 4.29 +static void main_loop(); 4.30 +static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam); 4.31 + 4.32 +static int width, height; 4.33 + 4.34 +static HWND win; 4.35 +static IDXGISwapChain *swap; 4.36 +static ID3D11Device *dev; 4.37 +static ID3D11DeviceContext *ctx; 4.38 +static ID3D11RenderTargetView *rtarg_view; 4.39 +static ID3D11InputLayout *vertex_layout; 4.40 +static ID3D11VertexShader *vsdr; 4.41 +static ID3D11PixelShader *psdr; 4.42 +static ID3D11Buffer *vbuf; 4.43 +static ID3D11Buffer *rstate_buf; 4.44 + 4.45 +static RenderState rstate; 4.46 + 4.47 +int main() 4.48 +{ 4.49 + if(!init()) { 4.50 + return 1; 4.51 + } 4.52 + atexit(cleanup); 4.53 + 4.54 + main_loop(); 4.55 + return 0; 4.56 +} 4.57 + 4.58 +static bool init() 4.59 +{ 4.60 + if(!(win = create_window(800, 600))) { 4.61 + return false; 4.62 + } 4.63 + 4.64 + unsigned int sdrflags = 0;//D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG; 4.65 + 4.66 + ID3DBlob *vsbuf, *psbuf, *msgblob; 4.67 + if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "vertex_main", "vs_4_0", sdrflags, 0, 0, &vsbuf, &msgblob, 0) != 0) { 4.68 + fprintf(stderr, "failed to load vertex shader\n"); 4.69 + if(msgblob->GetBufferSize() > 0) { 4.70 + fprintf(stderr, "Vertex Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 4.71 + } 4.72 + return false; 4.73 + } 4.74 + if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "pixel_main", "ps_4_0", sdrflags, 0, 0, &psbuf, &msgblob, 0) != 0) { 4.75 + fprintf(stderr, "failed to load pixel shader\n"); 4.76 + if(msgblob->GetBufferSize() > 0) { 4.77 + fprintf(stderr, "Pixel Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 4.78 + } 4.79 + return false; 4.80 + } 4.81 + 4.82 + if(dev->CreateVertexShader(vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), 0, &vsdr) != 0) { 4.83 + fprintf(stderr, "failed to create vertex shader\n"); 4.84 + return false; 4.85 + } 4.86 + if(dev->CreatePixelShader(psbuf->GetBufferPointer(), psbuf->GetBufferSize(), 0, &psdr) != 0) { 4.87 + fprintf(stderr, "failed to create pixel shader\n"); 4.88 + return false; 4.89 + } 4.90 + 4.91 + D3D11_INPUT_ELEMENT_DESC elem_desc[2]; 4.92 + elem_desc[0].SemanticName = "position"; 4.93 + elem_desc[0].SemanticIndex = 0; 4.94 + elem_desc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 4.95 + elem_desc[0].InputSlot = 0; 4.96 + elem_desc[0].AlignedByteOffset = 0; 4.97 + elem_desc[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 4.98 + elem_desc[0].InstanceDataStepRate = 0; 4.99 + 4.100 + elem_desc[1].SemanticName = "color"; 4.101 + elem_desc[1].SemanticIndex = 0; 4.102 + elem_desc[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 4.103 + elem_desc[1].InputSlot = 0; 4.104 + elem_desc[1].AlignedByteOffset = offsetof(Vertex, color); 4.105 + elem_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 4.106 + elem_desc[1].InstanceDataStepRate = 0; 4.107 + 4.108 + if(dev->CreateInputLayout(elem_desc, 2, vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), &vertex_layout) != 0) { 4.109 + fprintf(stderr, "failed to create vertex layout\n"); 4.110 + return 0; 4.111 + } 4.112 + vsbuf->Release(); 4.113 + psbuf->Release(); 4.114 + 4.115 + // --- create vertex buffer --- 4.116 + Vertex varr[] = { 4.117 + {{-0.6, -0.4, 0}, {1, 0, 0, 1}}, 4.118 + {{0.0, 0.6, 0}, {0, 1, 0, 1}}, 4.119 + {{0.6, -0.4, 0}, {0, 0, 1, 1}} 4.120 + }; 4.121 + 4.122 + D3D11_BUFFER_DESC buf_desc; 4.123 + memset(&buf_desc, 0, sizeof buf_desc); 4.124 + buf_desc.Usage = D3D11_USAGE_DEFAULT; 4.125 + buf_desc.ByteWidth = sizeof varr; 4.126 + buf_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 4.127 + 4.128 + D3D11_SUBRESOURCE_DATA subdata; 4.129 + memset(&subdata, 0, sizeof subdata); 4.130 + subdata.pSysMem = varr; 4.131 + if(dev->CreateBuffer(&buf_desc, &subdata, &vbuf) != 0) { 4.132 + fprintf(stderr, "failed to create vertex buffer\n"); 4.133 + return false; 4.134 + } 4.135 + 4.136 + // render state buffer 4.137 + memset(&buf_desc, 0, sizeof buf_desc); 4.138 + buf_desc.Usage = D3D11_USAGE_DEFAULT; 4.139 + buf_desc.ByteWidth = sizeof(RenderState); 4.140 + buf_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 4.141 + 4.142 + memset(&subdata, 0, sizeof subdata); 4.143 + subdata.pSysMem = &rstate; 4.144 + if(dev->CreateBuffer(&buf_desc, &subdata, &rstate_buf) != 0) { 4.145 + fprintf(stderr, "failed to create render state buffer\n"); 4.146 + return false; 4.147 + } 4.148 + 4.149 + return true; 4.150 +} 4.151 + 4.152 +static void cleanup() 4.153 +{ 4.154 + vbuf->Release(); 4.155 + rstate_buf->Release(); 4.156 + vsdr->Release(); 4.157 + psdr->Release(); 4.158 + vertex_layout->Release(); 4.159 + destroy_window(win); 4.160 +} 4.161 + 4.162 +static void set_identity(float *mat) 4.163 +{ 4.164 + mat[0] = mat[5] = mat[10] = mat[15] = 1.0; 4.165 + 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; 4.166 +} 4.167 + 4.168 +static void set_rotation_z(float *mat, float angle) 4.169 +{ 4.170 + set_identity(mat); 4.171 + 4.172 + mat[0] = cos(angle); 4.173 + mat[1] = -sin(angle); 4.174 + mat[4] = sin(angle); 4.175 + mat[5] = cos(angle); 4.176 +} 4.177 + 4.178 +static void set_ortho(float *mat, float aspect) 4.179 +{ 4.180 + set_identity(mat); 4.181 + mat[0] = 1.0 / aspect; 4.182 +} 4.183 + 4.184 +static void display() 4.185 +{ 4.186 + unsigned int msec = timeGetTime(); 4.187 + 4.188 + float fbcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; 4.189 + ctx->ClearRenderTargetView(rtarg_view, fbcolor); 4.190 + 4.191 + // set render state constant buffer data 4.192 + set_ortho(rstate.projection, (float)width / (float)height); 4.193 + set_rotation_z(rstate.modelview, msec / 1000.0); 4.194 + 4.195 + ctx->UpdateSubresource(rstate_buf, 0, 0, &rstate, 0, 0); 4.196 + ctx->VSSetConstantBuffers(0, 1, &rstate_buf); 4.197 + 4.198 + 4.199 + unsigned int stride = sizeof(Vertex); 4.200 + unsigned int offset = 0; 4.201 + ctx->IASetVertexBuffers(0, 1, &vbuf, &stride, &offset); 4.202 + ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 4.203 + ctx->IASetInputLayout(vertex_layout); 4.204 + 4.205 + ctx->VSSetShader(vsdr, 0, 0); 4.206 + ctx->PSSetShader(psdr, 0, 0); 4.207 + 4.208 + ctx->Draw(3, 0); 4.209 + 4.210 + swap->Present(0, 0); 4.211 +} 4.212 + 4.213 +static void reshape(int x, int y) 4.214 +{ 4.215 + width = x; 4.216 + height = y; 4.217 + 4.218 + D3D11_VIEWPORT vp; 4.219 + vp.Width = (float)x; 4.220 + vp.Height = (float)y; 4.221 + vp.MinDepth = 0; 4.222 + vp.MaxDepth = 1; 4.223 + vp.TopLeftX = 0; 4.224 + vp.TopLeftY = 0; 4.225 + ctx->RSSetViewports(1, &vp); 4.226 + 4.227 + // TODO probably we also need to resize render targets or whatever... 4.228 +} 4.229 + 4.230 +static void keyb(int key, bool pressed) 4.231 +{ 4.232 + if(key == 27) { 4.233 + exit(0); 4.234 + } 4.235 +} 4.236 + 4.237 +// ---- system crap ---- 4.238 + 4.239 +static HWND create_window(int xsz, int ysz) 4.240 +{ 4.241 + HINSTANCE app_inst = GetModuleHandle(0); 4.242 + 4.243 + WNDCLASS wclass; 4.244 + memset(&wclass, 0, sizeof wclass); 4.245 + wclass.hInstance = app_inst; 4.246 + wclass.lpfnWndProc = win_proc; 4.247 + wclass.lpszClassName = "mutantstargoatwin"; 4.248 + wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 4.249 + wclass.hIcon = LoadIcon(0, IDI_APPLICATION); 4.250 + wclass.hCursor = LoadCursor(0, IDC_ARROW); 4.251 + wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 4.252 + RegisterClass(&wclass); 4.253 + 4.254 + int posx = (GetSystemMetrics(SM_CXSCREEN) - xsz) / 2; 4.255 + int posy = (GetSystemMetrics(SM_CYSCREEN) - ysz) / 2; 4.256 + 4.257 + HWND win = CreateWindow("mutantstargoatwin", "DX11 Test", WS_OVERLAPPEDWINDOW, posx, posy, 4.258 + xsz, ysz, 0, 0, app_inst, 0); 4.259 + ShowWindow(win, SW_SHOW); 4.260 + 4.261 + // initialize D3D device 4.262 + DXGI_SWAP_CHAIN_DESC swap_desc; 4.263 + memset(&swap_desc, 0, sizeof swap_desc); 4.264 + swap_desc.BufferCount = 1; 4.265 + swap_desc.BufferDesc.Width = xsz; 4.266 + swap_desc.BufferDesc.Height = ysz; 4.267 + swap_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 4.268 + swap_desc.BufferDesc.RefreshRate.Numerator = 60; 4.269 + swap_desc.BufferDesc.RefreshRate.Denominator = 1; 4.270 + swap_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 4.271 + swap_desc.OutputWindow = win; 4.272 + swap_desc.SampleDesc.Count = 1; 4.273 + swap_desc.SampleDesc.Quality = 0; 4.274 + swap_desc.Windowed = 1; 4.275 + 4.276 + D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; 4.277 + 4.278 + if(D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, &feature_level, 1, 4.279 + D3D11_SDK_VERSION, &swap_desc, &swap, &dev, 0, &ctx) != 0) { 4.280 + fprintf(stderr, "Failed to create d3d device and swap chain\n"); 4.281 + return 0; 4.282 + } 4.283 + 4.284 + ID3D11Texture2D *rtex; 4.285 + if(swap->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&rtex) != 0) { 4.286 + fprintf(stderr, "Failed to get default render target texture\n"); 4.287 + return 0; 4.288 + } 4.289 + if(dev->CreateRenderTargetView(rtex, 0, &rtarg_view) != 0) { 4.290 + fprintf(stderr, "Failed to create render target view\n"); 4.291 + rtex->Release(); 4.292 + return 0; 4.293 + } 4.294 + rtex->Release(); 4.295 + ctx->OMSetRenderTargets(1, &rtarg_view, 0); 4.296 + 4.297 + reshape(xsz, ysz); 4.298 + return win; 4.299 +} 4.300 + 4.301 +static void destroy_window(HWND win) 4.302 +{ 4.303 + CloseWindow(win); 4.304 + UnregisterClass("mutantstargoatwin", GetModuleHandle(0)); 4.305 + 4.306 + rtarg_view->Release(); 4.307 + ctx->Release(); 4.308 + dev->Release(); 4.309 + swap->Release(); 4.310 +} 4.311 + 4.312 +static void main_loop() 4.313 +{ 4.314 + MSG msg; 4.315 + 4.316 + for(;;) { 4.317 + while(PeekMessage(&msg, win, 0, 0, PM_REMOVE)) { 4.318 + TranslateMessage(&msg); 4.319 + DispatchMessage(&msg); 4.320 + 4.321 + if(msg.message == WM_QUIT) { 4.322 + return; 4.323 + } 4.324 + } 4.325 + 4.326 + display(); 4.327 + } 4.328 +} 4.329 + 4.330 +static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam) 4.331 +{ 4.332 + switch(msg) { 4.333 + case WM_KEYDOWN: 4.334 + keyb(wparam, true); 4.335 + break; 4.336 + 4.337 + case WM_KEYUP: 4.338 + keyb(wparam, false); 4.339 + break; 4.340 + 4.341 + default: 4.342 + return DefWindowProc(win, msg, wparam, lparam); 4.343 + } 4.344 + return 0; 4.345 +} 4.346 \ No newline at end of file
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/vec.h Fri Jun 21 07:42:07 2013 +0300 5.3 @@ -0,0 +1,24 @@ 5.4 +#ifndef VEC_H_ 5.5 +#define VEC_H_ 5.6 + 5.7 +class Vector3 { 5.8 +public: 5.9 + float x, y, z; 5.10 + 5.11 + Vector3(); 5.12 + Vector3(float x, float y, float z); 5.13 +}; 5.14 + 5.15 +Vector3::Vector3() 5.16 +{ 5.17 + x = y = z = 0.0f; 5.18 +} 5.19 + 5.20 +Vector3::Vector3(float x, float y, float z) 5.21 +{ 5.22 + this->x = x; 5.23 + this->y = y; 5.24 + this->z = z; 5.25 +} 5.26 + 5.27 +#endif // VEC_H_ 5.28 \ No newline at end of file
6.1 --- a/vec.h Fri Jun 21 07:33:06 2013 +0300 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,24 +0,0 @@ 6.4 -#ifndef VEC_H_ 6.5 -#define VEC_H_ 6.6 - 6.7 -class Vector3 { 6.8 -public: 6.9 - float x, y, z; 6.10 - 6.11 - Vector3(); 6.12 - Vector3(float x, float y, float z); 6.13 -}; 6.14 - 6.15 -Vector3::Vector3() 6.16 -{ 6.17 - x = y = z = 0.0f; 6.18 -} 6.19 - 6.20 -Vector3::Vector3(float x, float y, float z) 6.21 -{ 6.22 - this->x = x; 6.23 - this->y = y; 6.24 - this->z = z; 6.25 -} 6.26 - 6.27 -#endif // VEC_H_ 6.28 \ No newline at end of file