Skip to content

Instantly share code, notes, and snippets.

@d7samurai
Last active December 21, 2025 05:56
Show Gist options
  • Select an option

  • Save d7samurai/0ba944bf38a74f28ce8574baf4c34fac to your computer and use it in GitHub Desktop.

Select an option

Save d7samurai/0ba944bf38a74f28ce8574baf4c34fac to your computer and use it in GitHub Desktop.
Minimal fullscreen triangle

Minimal fullscreen triangle

fullscreen

#pragma comment(lib, "user32")
#pragma comment(lib, "d3d11")
#pragma comment(lib, "d3dcompiler")
///////////////////////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <d3d11.h>
#include <d3dcompiler.h>
///////////////////////////////////////////////////////////////////////////////////////////////////
#define TITLE "Minimal fullscreen triangle by d7samurai"
///////////////////////////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSA wndclass = { 0, DefWindowProcA, 0, 0, 0, 0, 0, 0, 0, TITLE };
RegisterClassA(&wndclass);
HWND window = CreateWindowExA(0, TITLE, TITLE, 0x91000000, 0, 0, 0, 0, 0, 0, 0, 0);
///////////////////////////////////////////////////////////////////////////////////////////////
D3D_FEATURE_LEVEL featurelevels[] = { D3D_FEATURE_LEVEL_11_0 };
DXGI_SWAP_CHAIN_DESC swapchaindesc = {};
swapchaindesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // non-srgb for simplicity here. see other minimal gists for srgb setup
swapchaindesc.SampleDesc.Count = 1;
swapchaindesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapchaindesc.BufferCount = 2;
swapchaindesc.OutputWindow = window;
swapchaindesc.Windowed = TRUE;
swapchaindesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
IDXGISwapChain* swapchain;
ID3D11Device* device;
ID3D11DeviceContext* devicecontext;
D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, D3D11_CREATE_DEVICE_BGRA_SUPPORT, featurelevels, ARRAYSIZE(featurelevels), D3D11_SDK_VERSION, &swapchaindesc, &swapchain, &device, nullptr, &devicecontext);
swapchain->GetDesc(&swapchaindesc); // get actual dimensions
///////////////////////////////////////////////////////////////////////////////////////////////
ID3D11Texture2D* framebuffer;
swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&framebuffer); // get the swapchain's buffer
ID3D11RenderTargetView* framebufferRTV;
device->CreateRenderTargetView(framebuffer, nullptr, &framebufferRTV); // and make it a render target [view]
///////////////////////////////////////////////////////////////////////////////////////////////
ID3DBlob* vertexshaderCSO;
D3DCompileFromFile(L"gpu.hlsl", 0, 0, "VsMain", "vs_5_0", 0, 0, &vertexshaderCSO, 0);
ID3D11VertexShader* vertexshader;
device->CreateVertexShader(vertexshaderCSO->GetBufferPointer(), vertexshaderCSO->GetBufferSize(), 0, &vertexshader);
///////////////////////////////////////////////////////////////////////////////////////////////
ID3DBlob* pixelshaderCSO;
D3DCompileFromFile(L"gpu.hlsl", 0, 0, "PsMain", "ps_5_0", 0, 0, &pixelshaderCSO, 0);
ID3D11PixelShader* pixelshader;
device->CreatePixelShader(pixelshaderCSO->GetBufferPointer(), pixelshaderCSO->GetBufferSize(), 0, &pixelshader);
///////////////////////////////////////////////////////////////////////////////////////////////
D3D11_RASTERIZER_DESC rasterizerdesc = { D3D11_FILL_SOLID, D3D11_CULL_NONE };
ID3D11RasterizerState* rasterizerstate;
device->CreateRasterizerState(&rasterizerdesc, &rasterizerstate);
///////////////////////////////////////////////////////////////////////////////////////////////
D3D11_VIEWPORT viewport = { 0, 0, (float)swapchaindesc.BufferDesc.Width, (float)swapchaindesc.BufferDesc.Height, 0, 1 };
///////////////////////////////////////////////////////////////////////////////////////////////
devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
devicecontext->VSSetShader(vertexshader, nullptr, 0);
devicecontext->RSSetViewports(1, &viewport);
devicecontext->RSSetState(rasterizerstate);
devicecontext->PSSetShader(pixelshader, nullptr, 0);
devicecontext->OMSetRenderTargets(1, &framebufferRTV, nullptr);
///////////////////////////////////////////////////////////////////////////////////////////
devicecontext->Draw(3, 0);
///////////////////////////////////////////////////////////////////////////////////////////
swapchain->Present(1, 0);
///////////////////////////////////////////////////////////////////////////////////////////////
MSG msg;
GetMessageA(&msg, nullptr, WM_KEYFIRST, WM_KEYLAST);
}
struct vs_out
{
float4 pos : SV_POSITION;
float4 col : COL;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
vs_out VsMain(uint vertexid : SV_VERTEXID)
{
vs_out output;
output.pos = float4(vertexid >> 1, vertexid & 1, 0, 0.5) * 4 - 1;
output.col = float4(vertexid == 0, (vertexid == 1) * 2, (vertexid == 2) * 2, 1);
return output;
}
float4 PsMain(vs_out input) : SV_TARGET
{
return input.col;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment