Skip to content

Instantly share code, notes, and snippets.

@d7samurai
Last active March 28, 2024 22:10
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save d7samurai/1e9a1f1a366740f7d8a3a20397fcfa6b to your computer and use it in GitHub Desktop.
Save d7samurai/1e9a1f1a366740f7d8a3a20397fcfa6b to your computer and use it in GitHub Desktop.
Minimal D3D11 bonus material: extra minimal triangle

Minimal D3D11 bonus material: extra minimal triangle

A quick side exercise to see how little code one can get away with to put that RGB triangle on screen using D3D11 (without bending over backwards with trickery). This is a complete app in < 40 LOC (including the HLSL).

pretty minimal d3d11 triangle

For more.. elaborate.. D3D11 reference code, see the original Minimal D3D11, Minimal D3D11 pt2 and Minimal D3D11 pt3

#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;
}
@abjmakes
Copy link

For anyone else who runs into the issue where the window doesn't open, ensure that gpu.hlsl is in the current working directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment