DirectX (Direct3D)
DirectX is a collection of APIs for multimedia applications - especially games. One of those APIs is the rendering API Direct3D.
Hello Triangle (D3D11)
See Minimal D3D11 bonus material: extra minimal triangle ยท GitHub
#pragma comment(lib, "user32") #pragma comment(lib, "d3d11") #pragma comment(lib, "d3dcompiler") #include <windows.h> #include <d3d11.h> #include <d3dcompiler.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { IDXGISwapChain* swapchain; ID3D11Device* device; ID3D11DeviceContext* devicecontext; ID3D11Texture2D* rendertarget; ID3D11RenderTargetView* rendertargetview; ID3DBlob* cso; ID3D11VertexShader* vertexshader; ID3D11PixelShader* pixelshader; WNDCLASSA wndclass = { 0, DefWindowProcA, 0, 0, 0, 0, 0, 0, 0, "d7" }; RegisterClassA(&wndclass); DXGI_SWAP_CHAIN_DESC swapchaindesc = { { 0, 0, {}, DXGI_FORMAT_R8G8B8A8_UNORM }, { 1 }, 32, 2, CreateWindowExA(0, "d7", 0, 0x91000000, 0, 0, 0, 0, 0, 0, 0, 0), 1 }; D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, 0, 0, 0, 7, &swapchaindesc, &swapchain, &device, 0, &devicecontext); swapchain->GetDesc(&swapchaindesc); swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&rendertarget); device->CreateRenderTargetView(rendertarget, 0, &rendertargetview); D3DCompileFromFile(L"gpu.hlsl", 0, 0, "vertex_shader", "vs_5_0", 0, 0, &cso, 0); device->CreateVertexShader(cso->GetBufferPointer(), cso->GetBufferSize(), 0, &vertexshader); D3DCompileFromFile(L"gpu.hlsl", 0, 0, "pixel_shader", "ps_5_0", 0, 0, &cso, 0); device->CreatePixelShader(cso->GetBufferPointer(), cso->GetBufferSize(), 0, &pixelshader); D3D11_VIEWPORT viewport = { 0, 0, (float)swapchaindesc.BufferDesc.Width, (float)swapchaindesc.BufferDesc.Height, 0, 1 }; devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); devicecontext->VSSetShader(vertexshader, 0, 0); devicecontext->RSSetViewports(1, &viewport); devicecontext->PSSetShader(pixelshader, 0, 0); devicecontext->OMSetRenderTargets(1, &rendertargetview, 0); devicecontext->Draw(3, 0); swapchain->Present(1, 0); GetMessageA((MSG*)WinMain, 0, WM_KEYFIRST, WM_KEYLAST); // PRESS ANY KEY TO EXIT }
struct vertex { float4 pos : SV_POSITION; float4 col : COL; }; vertex vertex_shader(uint vid : SV_VERTEXID) { vertex output = { float4(vid * 0.5f, vid & 1, 1, 1.5f) - 0.5f, float4(vid == 0, vid == 1, vid == 2, 1) }; return output; } float4 pixel_shader(vertex input) : SV_TARGET { return input.col; }