pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

razvan1024 private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Razvan Aguridan on Sun 25 Oct 16:20
report abuse | download | new post

  1. #include <Windows.h>
  2. #include <D3D11.h>
  3. #include <D3DX11.h>
  4.  
  5. struct SimpleVertex
  6. {
  7.         float pos[3];
  8.         float col[4];
  9. };
  10.  
  11. IDXGISwapChain *pSwapChain = NULL;
  12. ID3D11Device *pDevice = NULL;
  13. ID3D11DeviceContext *pImediateContext = NULL;
  14. ID3D11RenderTargetView *pRenderTargetView = NULL;
  15. ID3D11Buffer *pVertexBuffer = NULL;
  16. ID3D11Buffer *pIndexBuffer = NULL;
  17. ID3D11VertexShader *pVertexShader = NULL;
  18. ID3D11InputLayout *pLayout = NULL;
  19. ID3D11PixelShader *pPixelShader = NULL;
  20. ID3D11RasterizerState *pRasterizerState = NULL;
  21.  
  22. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  23. //void EnumAdapters();
  24.  
  25. bool InitD3D(HWND hwnd)
  26. {
  27.         DXGI_SWAP_CHAIN_DESC sd;
  28.         ZeroMemory(&sd, sizeof(DXGI_SWAP_CHAIN_DESC));
  29.         sd.BufferCount = 1;
  30.         sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  31.         sd.BufferDesc.Height = 480;
  32.         sd.BufferDesc.RefreshRate.Numerator = 60;
  33.         sd.BufferDesc.RefreshRate.Denominator = 1;
  34.         sd.BufferDesc.Width = 640;
  35.         sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  36.         sd.OutputWindow = hwnd;
  37.         sd.SampleDesc.Count = 1;
  38.         sd.SampleDesc.Quality = 0;
  39.         sd.Windowed = true;
  40.         sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  41.  
  42.         D3D_FEATURE_LEVEL reqFeat = D3D_FEATURE_LEVEL_10_0;
  43.         UINT numLevels = 1;
  44.         D3D_FEATURE_LEVEL featSupported;
  45.         HRESULT hr;
  46.  
  47.         hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &reqFeat, numLevels, D3D11_SDK_VERSION, &sd, &pSwapChain, &pDevice, &featSupported, &pImediateContext);
  48.  
  49.         if(FAILED(hr))
  50.                 return false;
  51.  
  52.         ID3D11Texture2D *pBackBuffer = NULL;
  53.  
  54.         hr = pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID *)&pBackBuffer);
  55.  
  56.         if(FAILED(hr))
  57.                 return false;
  58.  
  59.         pDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);
  60.  
  61.         pImediateContext->OMSetRenderTargets(1, &pRenderTargetView, NULL);
  62.  
  63.         pBackBuffer->Release();
  64.  
  65.         D3D11_VIEWPORT vp;
  66.         vp.Height = 480;
  67.         vp.MaxDepth = 1.0f;
  68.         vp.MinDepth = 0.0f;
  69.         vp.TopLeftX = 0;
  70.         vp.TopLeftY = 0;
  71.         vp.Width = 640;
  72.  
  73.         pImediateContext->RSSetViewports(1, &vp);
  74.  
  75.         SimpleVertex verts[3] = {
  76.                 {
  77.                         {
  78.                                 0, 0.5f, 0.5f
  79.                         },
  80.                         {
  81.                                 0, 0, 0.5f, 1.0f
  82.                         }
  83.                 },
  84.                 {
  85.                         {
  86.                                 0.5f, -0.5f, 0.5f
  87.                         },
  88.                         {
  89.                                 0.5f, 0, 0, 1.0f
  90.                         }
  91.                 },
  92.                 {
  93.                         {
  94.                                 -0.5f, -0.5f, 0.5f
  95.                         },
  96.                         {
  97.                                 0, 0.5f, 0, 1.0f
  98.                         }
  99.                 }
  100.         };
  101.  
  102.         D3D11_BUFFER_DESC vb_desc;
  103.         ZeroMemory(&vb_desc, sizeof(D3D11_BUFFER_DESC));
  104.         vb_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
  105.         vb_desc.ByteWidth = sizeof(SimpleVertex) * 3;
  106.         vb_desc.CPUAccessFlags = 0;
  107.         vb_desc.MiscFlags = 0;
  108.         vb_desc.Usage = D3D11_USAGE_DEFAULT;
  109.  
  110.         D3D11_SUBRESOURCE_DATA init_data;
  111.         ZeroMemory(&init_data, sizeof(D3D11_SUBRESOURCE_DATA));
  112.         init_data.pSysMem = verts;
  113.  
  114.         hr = pDevice->CreateBuffer(&vb_desc, &init_data, &pVertexBuffer);
  115.  
  116.         if(FAILED(hr))
  117.                 return false;
  118.  
  119.         unsigned int indexes[] = {
  120.                 2, 1, 0
  121.         };
  122.  
  123.         D3D11_BUFFER_DESC ib_desc;
  124.         ZeroMemory(&ib_desc, sizeof(D3D11_BUFFER_DESC));
  125.         ib_desc.BindFlags = D3D11_BIND_INDEX_BUFFER;
  126.         ib_desc.ByteWidth = sizeof(unsigned int) * 3;
  127.         ib_desc.CPUAccessFlags = 0;
  128.         ib_desc.MiscFlags = 0;
  129.         ib_desc.Usage = D3D11_USAGE_DEFAULT;
  130.  
  131.         ZeroMemory(&init_data, sizeof(D3D11_SUBRESOURCE_DATA));
  132.         init_data.pSysMem = indexes;
  133.  
  134.         hr = pDevice->CreateBuffer(&ib_desc, &init_data, &pIndexBuffer);
  135.  
  136.         if(FAILED(hr))
  137.                 return false;
  138.  
  139.         UINT strides = 0;
  140.         UINT offsets = 0;
  141.  
  142.         pImediateContext->IASetVertexBuffers(0, 1, &pVertexBuffer, &strides, &offsets);
  143.         pImediateContext->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0);
  144.  
  145.         ID3D10Blob *pBlob;
  146.         ID3D10Blob *pErrorBlob;
  147.  
  148.         hr = D3DX11CompileFromFile(L"simple.hlsl", NULL, NULL, "vs_main", "vs_4_0", D3D10_SHADER_DEBUG, NULL, NULL, &pBlob, &pErrorBlob, NULL);
  149.  
  150.         if(FAILED(hr))
  151.         {
  152.                 return false;
  153.         }
  154.  
  155.         if(pErrorBlob != NULL)
  156.         {
  157.                 pErrorBlob->Release();
  158.                 pErrorBlob = NULL;
  159.         }
  160.  
  161.         hr = pDevice->CreateVertexShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &pVertexShader);
  162.  
  163.         if(FAILED(hr))
  164.         {
  165.                 return false;
  166.         }
  167.  
  168.         D3D11_INPUT_ELEMENT_DESC layout[] =
  169.         {
  170.                 {
  171.                         "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0
  172.                 },
  173.                 {
  174.                         "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0
  175.                 }
  176.         };
  177.  
  178.         UINT numelems = sizeof(layout)/sizeof(layout[0]);
  179.  
  180.         hr = pDevice->CreateInputLayout(layout, numelems, pBlob->GetBufferPointer(), pBlob->GetBufferSize(), &pLayout);
  181.  
  182.         pBlob->Release();
  183.         pBlob = NULL;
  184.  
  185.         if(FAILED(hr))
  186.                 return false;   
  187.  
  188.         hr = D3DX11CompileFromFile(L"simple.hlsl", NULL, NULL, "ps_main", "ps_4_0", D3D10_SHADER_DEBUG, NULL, NULL, &pBlob, &pErrorBlob, NULL);
  189.  
  190.         if(FAILED(hr))
  191.         {
  192.                 return false;
  193.         }
  194.  
  195.         if(pErrorBlob != NULL)
  196.         {
  197.                 pErrorBlob->Release();
  198.                 pErrorBlob = NULL;
  199.         }
  200.  
  201.         hr = pDevice->CreatePixelShader(pBlob->GetBufferPointer(), pBlob->GetBufferSize(), NULL, &pPixelShader);
  202.  
  203.         if(FAILED(hr))
  204.                 return false;
  205.  
  206.         pBlob->Release();
  207.         pBlob = NULL;
  208.  
  209.         pImediateContext->IASetInputLayout(pLayout);
  210.         pImediateContext->VSSetShader(pVertexShader, NULL, 0);
  211.         pImediateContext->PSSetShader(pPixelShader, NULL, 0);
  212.  
  213.         return true;
  214. }
  215.  
  216. void Render()
  217. {
  218.         float clearColor[] = {0,0,1,1};
  219.         pImediateContext->ClearRenderTargetView(pRenderTargetView, clearColor);
  220.         pImediateContext->DrawIndexed(3, 0, 0);
  221.         pSwapChain->Present(0,0);
  222. }
  223.  
  224. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
  225. {
  226.         WNDCLASSEX wndclass;
  227.         ZeroMemory(&wndclass, sizeof(WNDCLASSEX));
  228.         wndclass.cbClsExtra = 0;
  229.         wndclass.cbSize = sizeof(WNDCLASSEX);
  230.         wndclass.cbWndExtra = 0;
  231.         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  232.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  233.         wndclass.hIcon = wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  234.         wndclass.hInstance = hInstance;
  235.         wndclass.lpfnWndProc = WndProc;
  236.         wndclass.lpszClassName = L"dx11test";
  237.         wndclass.lpszMenuName = NULL;
  238.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  239.         RegisterClassEx(&wndclass);
  240.         HWND hwnd = CreateWindow(L"dx11test",L"Test D3D11", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, (HWND)GetDesktopWindow(), NULL, hInstance, NULL);
  241.         ShowWindow(hwnd, nShowCmd);
  242.         UpdateWindow(hwnd);
  243.         if(!InitD3D(hwnd))
  244.                 MessageBox(hwnd, L"There was an error while trying to start D3D!", L"Error", MB_OK);
  245.         //EnumAdapters();
  246.         MSG msg;
  247.         ZeroMemory(&msg, sizeof(MSG));
  248.         while(msg.message != WM_QUIT)
  249.         {
  250.                 if(PeekMessage(&msg, NULL, 0,0,PM_REMOVE))
  251.                 {
  252.                         TranslateMessage(&msg);
  253.                         DispatchMessage(&msg);
  254.                 }
  255.                 else
  256.                 {
  257.                         Render();
  258.                 }
  259.         }
  260.         UnregisterClass(L"dx11test", hInstance);
  261.         return (int)msg.wParam;
  262. }
  263.  
  264. LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  265. {
  266.         switch(iMsg)
  267.         {
  268.         case WM_DESTROY:
  269.                 PostQuitMessage(0);
  270.                 return 0;
  271.         case WM_KEYDOWN:
  272.                 if(wParam == VK_ESCAPE)
  273.                 {
  274.                         DestroyWindow(hwnd);
  275.                         return 0;
  276.                 }
  277.         }
  278.         return DefWindowProc(hwnd, iMsg, wParam, lParam);
  279. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post