dx11test
changeset 0:647ba0689512
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 21 Jun 2013 07:33:06 +0300 |
parents | |
children | ec02798ee83b |
files | .hgignore dx11test.sln dx11test.vcxproj dx11test.vcxproj.filters main.cc shader.hlsl vec.h |
diffstat | 7 files changed, 552 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/.hgignore Fri Jun 21 07:33:06 2013 +0300 1.3 @@ -0,0 +1,7 @@ 1.4 +\.o$ 1.5 +\.swp$ 1.6 +Debug$ 1.7 +Release$ 1.8 +\.suo$ 1.9 +\.user$ 1.10 +\.sdf$
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/dx11test.sln Fri Jun 21 07:33:06 2013 +0300 2.3 @@ -0,0 +1,20 @@ 2.4 + 2.5 +Microsoft Visual Studio Solution File, Format Version 12.00 2.6 +# Visual Studio 2012 2.7 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "dx11test", "dx11test.vcxproj", "{F55C35D3-5A5C-4438-B8AA-E5DFA0EED734}" 2.8 +EndProject 2.9 +Global 2.10 + GlobalSection(SolutionConfigurationPlatforms) = preSolution 2.11 + Debug|Win32 = Debug|Win32 2.12 + Release|Win32 = Release|Win32 2.13 + EndGlobalSection 2.14 + GlobalSection(ProjectConfigurationPlatforms) = postSolution 2.15 + {F55C35D3-5A5C-4438-B8AA-E5DFA0EED734}.Debug|Win32.ActiveCfg = Debug|Win32 2.16 + {F55C35D3-5A5C-4438-B8AA-E5DFA0EED734}.Debug|Win32.Build.0 = Debug|Win32 2.17 + {F55C35D3-5A5C-4438-B8AA-E5DFA0EED734}.Release|Win32.ActiveCfg = Release|Win32 2.18 + {F55C35D3-5A5C-4438-B8AA-E5DFA0EED734}.Release|Win32.Build.0 = Release|Win32 2.19 + EndGlobalSection 2.20 + GlobalSection(SolutionProperties) = preSolution 2.21 + HideSolutionNode = FALSE 2.22 + EndGlobalSection 2.23 +EndGlobal
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/dx11test.vcxproj Fri Jun 21 07:33:06 2013 +0300 3.3 @@ -0,0 +1,95 @@ 3.4 +<?xml version="1.0" encoding="utf-8"?> 3.5 +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 3.6 + <ItemGroup Label="ProjectConfigurations"> 3.7 + <ProjectConfiguration Include="Debug|Win32"> 3.8 + <Configuration>Debug</Configuration> 3.9 + <Platform>Win32</Platform> 3.10 + </ProjectConfiguration> 3.11 + <ProjectConfiguration Include="Release|Win32"> 3.12 + <Configuration>Release</Configuration> 3.13 + <Platform>Win32</Platform> 3.14 + </ProjectConfiguration> 3.15 + </ItemGroup> 3.16 + <PropertyGroup Label="Globals"> 3.17 + <ProjectGuid>{F55C35D3-5A5C-4438-B8AA-E5DFA0EED734}</ProjectGuid> 3.18 + <Keyword>Win32Proj</Keyword> 3.19 + <RootNamespace>dx11test</RootNamespace> 3.20 + </PropertyGroup> 3.21 + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> 3.22 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> 3.23 + <ConfigurationType>Application</ConfigurationType> 3.24 + <UseDebugLibraries>true</UseDebugLibraries> 3.25 + <PlatformToolset>v110</PlatformToolset> 3.26 + <CharacterSet>MultiByte</CharacterSet> 3.27 + </PropertyGroup> 3.28 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> 3.29 + <ConfigurationType>Application</ConfigurationType> 3.30 + <UseDebugLibraries>false</UseDebugLibraries> 3.31 + <PlatformToolset>v110</PlatformToolset> 3.32 + <WholeProgramOptimization>true</WholeProgramOptimization> 3.33 + <CharacterSet>MultiByte</CharacterSet> 3.34 + </PropertyGroup> 3.35 + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> 3.36 + <ImportGroup Label="ExtensionSettings"> 3.37 + </ImportGroup> 3.38 + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> 3.39 + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> 3.40 + </ImportGroup> 3.41 + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> 3.42 + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> 3.43 + </ImportGroup> 3.44 + <PropertyGroup Label="UserMacros" /> 3.45 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> 3.46 + <LinkIncremental>true</LinkIncremental> 3.47 + </PropertyGroup> 3.48 + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> 3.49 + <LinkIncremental>false</LinkIncremental> 3.50 + </PropertyGroup> 3.51 + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> 3.52 + <ClCompile> 3.53 + <PrecompiledHeader> 3.54 + </PrecompiledHeader> 3.55 + <WarningLevel>Level3</WarningLevel> 3.56 + <Optimization>Disabled</Optimization> 3.57 + <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> 3.58 + </ClCompile> 3.59 + <Link> 3.60 + <SubSystem>Console</SubSystem> 3.61 + <GenerateDebugInformation>true</GenerateDebugInformation> 3.62 + <AdditionalDependencies>dxgi.lib;d3d11.lib;d3dx11.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies> 3.63 + </Link> 3.64 + </ItemDefinitionGroup> 3.65 + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> 3.66 + <ClCompile> 3.67 + <WarningLevel>Level3</WarningLevel> 3.68 + <PrecompiledHeader> 3.69 + </PrecompiledHeader> 3.70 + <Optimization>MaxSpeed</Optimization> 3.71 + <FunctionLevelLinking>true</FunctionLevelLinking> 3.72 + <IntrinsicFunctions>true</IntrinsicFunctions> 3.73 + <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> 3.74 + </ClCompile> 3.75 + <Link> 3.76 + <SubSystem>Console</SubSystem> 3.77 + <GenerateDebugInformation>true</GenerateDebugInformation> 3.78 + <EnableCOMDATFolding>true</EnableCOMDATFolding> 3.79 + <OptimizeReferences>true</OptimizeReferences> 3.80 + <AdditionalDependencies>dxgi.lib;d3d11.lib;d3dx11.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies> 3.81 + </Link> 3.82 + </ItemDefinitionGroup> 3.83 + <ItemGroup> 3.84 + <ClCompile Include="main.cc" /> 3.85 + <ClCompile Include="vec.cc" /> 3.86 + </ItemGroup> 3.87 + <ItemGroup> 3.88 + <ClInclude Include="vec.h" /> 3.89 + </ItemGroup> 3.90 + <ItemGroup> 3.91 + <None Include="shader.hlsl"> 3.92 + <FileType>Document</FileType> 3.93 + </None> 3.94 + </ItemGroup> 3.95 + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> 3.96 + <ImportGroup Label="ExtensionTargets"> 3.97 + </ImportGroup> 3.98 +</Project> 3.99 \ No newline at end of file
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/dx11test.vcxproj.filters Fri Jun 21 07:33:06 2013 +0300 4.3 @@ -0,0 +1,33 @@ 4.4 +<?xml version="1.0" encoding="utf-8"?> 4.5 +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 4.6 + <ItemGroup> 4.7 + <Filter Include="Source Files"> 4.8 + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> 4.9 + <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> 4.10 + </Filter> 4.11 + <Filter Include="Header Files"> 4.12 + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> 4.13 + <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions> 4.14 + </Filter> 4.15 + <Filter Include="Resource Files"> 4.16 + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> 4.17 + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> 4.18 + </Filter> 4.19 + </ItemGroup> 4.20 + <ItemGroup> 4.21 + <ClCompile Include="main.cc"> 4.22 + <Filter>Source Files</Filter> 4.23 + </ClCompile> 4.24 + <ClCompile Include="vec.cc"> 4.25 + <Filter>Source Files</Filter> 4.26 + </ClCompile> 4.27 + </ItemGroup> 4.28 + <ItemGroup> 4.29 + <ClInclude Include="vec.h"> 4.30 + <Filter>Header Files</Filter> 4.31 + </ClInclude> 4.32 + </ItemGroup> 4.33 + <ItemGroup> 4.34 + <None Include="shader.hlsl" /> 4.35 + </ItemGroup> 4.36 +</Project> 4.37 \ No newline at end of file
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/main.cc Fri Jun 21 07:33:06 2013 +0300 5.3 @@ -0,0 +1,342 @@ 5.4 +#include <stdio.h> 5.5 +#include <stdlib.h> 5.6 +#include <math.h> 5.7 +#include <stddef.h> 5.8 +#include <d3d11.h> 5.9 +#include <d3dx11.h> 5.10 +#include "vec.h" 5.11 + 5.12 +struct Vertex { 5.13 + float pos[3]; 5.14 + float color[4]; 5.15 +}; 5.16 + 5.17 +struct RenderState { 5.18 + float modelview[16]; 5.19 + float projection[16]; 5.20 +}; 5.21 + 5.22 +static bool init(); 5.23 +static void cleanup(); 5.24 +static void display(); 5.25 +static void reshape(int x, int y); 5.26 +static void keyb(int key, bool pressed); 5.27 +static HWND create_window(int xsz, int ysz); 5.28 +static void destroy_window(HWND win); 5.29 +static void main_loop(); 5.30 +static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam); 5.31 + 5.32 +static int width, height; 5.33 + 5.34 +static HWND win; 5.35 +static IDXGISwapChain *swap; 5.36 +static ID3D11Device *dev; 5.37 +static ID3D11DeviceContext *ctx; 5.38 +static ID3D11RenderTargetView *rtarg_view; 5.39 +static ID3D11InputLayout *vertex_layout; 5.40 +static ID3D11VertexShader *vsdr; 5.41 +static ID3D11PixelShader *psdr; 5.42 +static ID3D11Buffer *vbuf; 5.43 +static ID3D11Buffer *rstate_buf; 5.44 + 5.45 +static RenderState rstate; 5.46 + 5.47 +int main() 5.48 +{ 5.49 + if(!init()) { 5.50 + return 1; 5.51 + } 5.52 + atexit(cleanup); 5.53 + 5.54 + main_loop(); 5.55 + return 0; 5.56 +} 5.57 + 5.58 +static bool init() 5.59 +{ 5.60 + if(!(win = create_window(800, 600))) { 5.61 + return false; 5.62 + } 5.63 + 5.64 + unsigned int sdrflags = 0;//D3DCOMPILE_ENABLE_STRICTNESS | D3DCOMPILE_DEBUG; 5.65 + 5.66 + ID3DBlob *vsbuf, *psbuf, *msgblob; 5.67 + if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "vertex_main", "vs_4_0", sdrflags, 0, 0, &vsbuf, &msgblob, 0) != 0) { 5.68 + fprintf(stderr, "failed to load vertex shader\n"); 5.69 + if(msgblob->GetBufferSize() > 0) { 5.70 + fprintf(stderr, "Vertex Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 5.71 + } 5.72 + return false; 5.73 + } 5.74 + if(D3DX11CompileFromFile("shader.hlsl", 0, 0, "pixel_main", "ps_4_0", sdrflags, 0, 0, &psbuf, &msgblob, 0) != 0) { 5.75 + fprintf(stderr, "failed to load pixel shader\n"); 5.76 + if(msgblob->GetBufferSize() > 0) { 5.77 + fprintf(stderr, "Pixel Shader:\n%s\n", (char*)msgblob->GetBufferPointer()); 5.78 + } 5.79 + return false; 5.80 + } 5.81 + 5.82 + if(dev->CreateVertexShader(vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), 0, &vsdr) != 0) { 5.83 + fprintf(stderr, "failed to create vertex shader\n"); 5.84 + return false; 5.85 + } 5.86 + if(dev->CreatePixelShader(psbuf->GetBufferPointer(), psbuf->GetBufferSize(), 0, &psdr) != 0) { 5.87 + fprintf(stderr, "failed to create pixel shader\n"); 5.88 + return false; 5.89 + } 5.90 + 5.91 + D3D11_INPUT_ELEMENT_DESC elem_desc[2]; 5.92 + elem_desc[0].SemanticName = "position"; 5.93 + elem_desc[0].SemanticIndex = 0; 5.94 + elem_desc[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; 5.95 + elem_desc[0].InputSlot = 0; 5.96 + elem_desc[0].AlignedByteOffset = 0; 5.97 + elem_desc[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 5.98 + elem_desc[0].InstanceDataStepRate = 0; 5.99 + 5.100 + elem_desc[1].SemanticName = "color"; 5.101 + elem_desc[1].SemanticIndex = 0; 5.102 + elem_desc[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; 5.103 + elem_desc[1].InputSlot = 0; 5.104 + elem_desc[1].AlignedByteOffset = offsetof(Vertex, color); 5.105 + elem_desc[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; 5.106 + elem_desc[1].InstanceDataStepRate = 0; 5.107 + 5.108 + if(dev->CreateInputLayout(elem_desc, 2, vsbuf->GetBufferPointer(), vsbuf->GetBufferSize(), &vertex_layout) != 0) { 5.109 + fprintf(stderr, "failed to create vertex layout\n"); 5.110 + return 0; 5.111 + } 5.112 + vsbuf->Release(); 5.113 + psbuf->Release(); 5.114 + 5.115 + // --- create vertex buffer --- 5.116 + Vertex varr[] = { 5.117 + {{-0.6, -0.4, 0}, {1, 0, 0, 1}}, 5.118 + {{0.0, 0.6, 0}, {0, 1, 0, 1}}, 5.119 + {{0.6, -0.4, 0}, {0, 0, 1, 1}} 5.120 + }; 5.121 + 5.122 + D3D11_BUFFER_DESC buf_desc; 5.123 + memset(&buf_desc, 0, sizeof buf_desc); 5.124 + buf_desc.Usage = D3D11_USAGE_DEFAULT; 5.125 + buf_desc.ByteWidth = sizeof varr; 5.126 + buf_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; 5.127 + 5.128 + D3D11_SUBRESOURCE_DATA subdata; 5.129 + memset(&subdata, 0, sizeof subdata); 5.130 + subdata.pSysMem = varr; 5.131 + if(dev->CreateBuffer(&buf_desc, &subdata, &vbuf) != 0) { 5.132 + fprintf(stderr, "failed to create vertex buffer\n"); 5.133 + return false; 5.134 + } 5.135 + 5.136 + // render state buffer 5.137 + memset(&buf_desc, 0, sizeof buf_desc); 5.138 + buf_desc.Usage = D3D11_USAGE_DEFAULT; 5.139 + buf_desc.ByteWidth = sizeof(RenderState); 5.140 + buf_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 5.141 + 5.142 + memset(&subdata, 0, sizeof subdata); 5.143 + subdata.pSysMem = &rstate; 5.144 + if(dev->CreateBuffer(&buf_desc, &subdata, &rstate_buf) != 0) { 5.145 + fprintf(stderr, "failed to create render state buffer\n"); 5.146 + return false; 5.147 + } 5.148 + 5.149 + return true; 5.150 +} 5.151 + 5.152 +static void cleanup() 5.153 +{ 5.154 + vbuf->Release(); 5.155 + rstate_buf->Release(); 5.156 + vsdr->Release(); 5.157 + psdr->Release(); 5.158 + vertex_layout->Release(); 5.159 + destroy_window(win); 5.160 +} 5.161 + 5.162 +static void set_identity(float *mat) 5.163 +{ 5.164 + mat[0] = mat[5] = mat[10] = mat[15] = 1.0; 5.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; 5.166 +} 5.167 + 5.168 +static void set_rotation_z(float *mat, float angle) 5.169 +{ 5.170 + set_identity(mat); 5.171 + 5.172 + mat[0] = cos(angle); 5.173 + mat[1] = -sin(angle); 5.174 + mat[4] = sin(angle); 5.175 + mat[5] = cos(angle); 5.176 +} 5.177 + 5.178 +static void set_ortho(float *mat, float aspect) 5.179 +{ 5.180 + set_identity(mat); 5.181 + mat[0] = 1.0 / aspect; 5.182 +} 5.183 + 5.184 +static void display() 5.185 +{ 5.186 + unsigned int msec = timeGetTime(); 5.187 + 5.188 + float fbcolor[] = {0.2f, 0.2f, 0.2f, 1.0f}; 5.189 + ctx->ClearRenderTargetView(rtarg_view, fbcolor); 5.190 + 5.191 + // set render state constant buffer data 5.192 + set_ortho(rstate.projection, (float)width / (float)height); 5.193 + set_rotation_z(rstate.modelview, msec / 1000.0); 5.194 + 5.195 + ctx->UpdateSubresource(rstate_buf, 0, 0, &rstate, 0, 0); 5.196 + ctx->VSSetConstantBuffers(0, 1, &rstate_buf); 5.197 + 5.198 + 5.199 + unsigned int stride = sizeof(Vertex); 5.200 + unsigned int offset = 0; 5.201 + ctx->IASetVertexBuffers(0, 1, &vbuf, &stride, &offset); 5.202 + ctx->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); 5.203 + ctx->IASetInputLayout(vertex_layout); 5.204 + 5.205 + ctx->VSSetShader(vsdr, 0, 0); 5.206 + ctx->PSSetShader(psdr, 0, 0); 5.207 + 5.208 + ctx->Draw(3, 0); 5.209 + 5.210 + swap->Present(0, 0); 5.211 +} 5.212 + 5.213 +static void reshape(int x, int y) 5.214 +{ 5.215 + width = x; 5.216 + height = y; 5.217 + 5.218 + D3D11_VIEWPORT vp; 5.219 + vp.Width = (float)x; 5.220 + vp.Height = (float)y; 5.221 + vp.MinDepth = 0; 5.222 + vp.MaxDepth = 1; 5.223 + vp.TopLeftX = 0; 5.224 + vp.TopLeftY = 0; 5.225 + ctx->RSSetViewports(1, &vp); 5.226 + 5.227 + // TODO probably we also need to resize render targets or whatever... 5.228 +} 5.229 + 5.230 +static void keyb(int key, bool pressed) 5.231 +{ 5.232 + if(key == 27) { 5.233 + exit(0); 5.234 + } 5.235 +} 5.236 + 5.237 +// ---- system crap ---- 5.238 + 5.239 +static HWND create_window(int xsz, int ysz) 5.240 +{ 5.241 + HINSTANCE app_inst = GetModuleHandle(0); 5.242 + 5.243 + WNDCLASS wclass; 5.244 + memset(&wclass, 0, sizeof wclass); 5.245 + wclass.hInstance = app_inst; 5.246 + wclass.lpfnWndProc = win_proc; 5.247 + wclass.lpszClassName = "mutantstargoatwin"; 5.248 + wclass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; 5.249 + wclass.hIcon = LoadIcon(0, IDI_APPLICATION); 5.250 + wclass.hCursor = LoadCursor(0, IDC_ARROW); 5.251 + wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 5.252 + RegisterClass(&wclass); 5.253 + 5.254 + int posx = (GetSystemMetrics(SM_CXSCREEN) - xsz) / 2; 5.255 + int posy = (GetSystemMetrics(SM_CYSCREEN) - ysz) / 2; 5.256 + 5.257 + HWND win = CreateWindow("mutantstargoatwin", "DX11 Test", WS_OVERLAPPEDWINDOW, posx, posy, 5.258 + xsz, ysz, 0, 0, app_inst, 0); 5.259 + ShowWindow(win, SW_SHOW); 5.260 + 5.261 + // initialize D3D device 5.262 + DXGI_SWAP_CHAIN_DESC swap_desc; 5.263 + memset(&swap_desc, 0, sizeof swap_desc); 5.264 + swap_desc.BufferCount = 1; 5.265 + swap_desc.BufferDesc.Width = xsz; 5.266 + swap_desc.BufferDesc.Height = ysz; 5.267 + swap_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 5.268 + swap_desc.BufferDesc.RefreshRate.Numerator = 60; 5.269 + swap_desc.BufferDesc.RefreshRate.Denominator = 1; 5.270 + swap_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 5.271 + swap_desc.OutputWindow = win; 5.272 + swap_desc.SampleDesc.Count = 1; 5.273 + swap_desc.SampleDesc.Quality = 0; 5.274 + swap_desc.Windowed = 1; 5.275 + 5.276 + D3D_FEATURE_LEVEL feature_level = D3D_FEATURE_LEVEL_11_0; 5.277 + 5.278 + if(D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, &feature_level, 1, 5.279 + D3D11_SDK_VERSION, &swap_desc, &swap, &dev, 0, &ctx) != 0) { 5.280 + fprintf(stderr, "Failed to create d3d device and swap chain\n"); 5.281 + return 0; 5.282 + } 5.283 + 5.284 + ID3D11Texture2D *rtex; 5.285 + if(swap->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&rtex) != 0) { 5.286 + fprintf(stderr, "Failed to get default render target texture\n"); 5.287 + return 0; 5.288 + } 5.289 + if(dev->CreateRenderTargetView(rtex, 0, &rtarg_view) != 0) { 5.290 + fprintf(stderr, "Failed to create render target view\n"); 5.291 + rtex->Release(); 5.292 + return 0; 5.293 + } 5.294 + rtex->Release(); 5.295 + ctx->OMSetRenderTargets(1, &rtarg_view, 0); 5.296 + 5.297 + reshape(xsz, ysz); 5.298 + return win; 5.299 +} 5.300 + 5.301 +static void destroy_window(HWND win) 5.302 +{ 5.303 + CloseWindow(win); 5.304 + UnregisterClass("mutantstargoatwin", GetModuleHandle(0)); 5.305 + 5.306 + rtarg_view->Release(); 5.307 + ctx->Release(); 5.308 + dev->Release(); 5.309 + swap->Release(); 5.310 +} 5.311 + 5.312 +static void main_loop() 5.313 +{ 5.314 + MSG msg; 5.315 + 5.316 + for(;;) { 5.317 + while(PeekMessage(&msg, win, 0, 0, PM_REMOVE)) { 5.318 + TranslateMessage(&msg); 5.319 + DispatchMessage(&msg); 5.320 + 5.321 + if(msg.message == WM_QUIT) { 5.322 + return; 5.323 + } 5.324 + } 5.325 + 5.326 + display(); 5.327 + } 5.328 +} 5.329 + 5.330 +static long CALLBACK win_proc(HWND win, unsigned int msg, unsigned int wparam, long lparam) 5.331 +{ 5.332 + switch(msg) { 5.333 + case WM_KEYDOWN: 5.334 + keyb(wparam, true); 5.335 + break; 5.336 + 5.337 + case WM_KEYUP: 5.338 + keyb(wparam, false); 5.339 + break; 5.340 + 5.341 + default: 5.342 + return DefWindowProc(win, msg, wparam, lparam); 5.343 + } 5.344 + return 0; 5.345 +} 5.346 \ No newline at end of file
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/shader.hlsl Fri Jun 21 07:33:06 2013 +0300 6.3 @@ -0,0 +1,31 @@ 6.4 +cbuffer RenderState : register(b0) { 6.5 + matrix modelview_matrix : packoffset(c0); 6.6 + matrix projection_matrix : packoffset(c4); 6.7 +}; 6.8 + 6.9 +struct VSInput { 6.10 + float4 pos : POSITION; 6.11 + float4 color : COLOR; 6.12 +}; 6.13 + 6.14 +struct VSOutput { 6.15 + float4 pos : SV_POSITION; 6.16 + float4 color : COLOR0; 6.17 +}; 6.18 + 6.19 +VSOutput vertex_main(VSInput input) 6.20 +{ 6.21 + VSOutput res; 6.22 + 6.23 + float4 vpos = mul(input.pos, modelview_matrix); 6.24 + 6.25 + res.pos = mul(vpos, projection_matrix); 6.26 + res.color = input.color; 6.27 + return res; 6.28 +} 6.29 + 6.30 + 6.31 +float4 pixel_main(VSOutput input) : SV_TARGET 6.32 +{ 6.33 + return input.color; 6.34 +} 6.35 \ No newline at end of file
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/vec.h Fri Jun 21 07:33:06 2013 +0300 7.3 @@ -0,0 +1,24 @@ 7.4 +#ifndef VEC_H_ 7.5 +#define VEC_H_ 7.6 + 7.7 +class Vector3 { 7.8 +public: 7.9 + float x, y, z; 7.10 + 7.11 + Vector3(); 7.12 + Vector3(float x, float y, float z); 7.13 +}; 7.14 + 7.15 +Vector3::Vector3() 7.16 +{ 7.17 + x = y = z = 0.0f; 7.18 +} 7.19 + 7.20 +Vector3::Vector3(float x, float y, float z) 7.21 +{ 7.22 + this->x = x; 7.23 + this->y = y; 7.24 + this->z = z; 7.25 +} 7.26 + 7.27 +#endif // VEC_H_ 7.28 \ No newline at end of file