Skip to content

Instantly share code, notes, and snippets.

@Sketch9920
Forked from d7samurai/.readme.md
Created June 25, 2023 07:54
Show Gist options
  • Save Sketch9920/be2f89144a455c2d91965459650d6964 to your computer and use it in GitHub Desktop.
Save Sketch9920/be2f89144a455c2d91965459650d6964 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.. useful.. 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)
{
WNDCLASSA wndClass = { 0, DefWindowProcA, 0, 0, 0, 0, 0, 0, 0, "d7" };
RegisterClassA(&wndClass);
HWND window = CreateWindowExA(0, "d7", 0, 0x91000000, 0, 0, 0, 0, 0, 0, 0, 0);
///////////////////////////////////////////////////////////////////////////////////////////////
D3D_FEATURE_LEVEL featureLevels[] = { D3D_FEATURE_LEVEL_11_0 };
DXGI_SWAP_CHAIN_DESC swapChainDesc = { { 0, 0, {}, DXGI_FORMAT_B8G8R8A8_UNORM }, { 1 }, 32, 2, window, 1 };
IDXGISwapChain* swapChain;
ID3D11Device* device;
ID3D11DeviceContext* deviceContext;
D3D11CreateDeviceAndSwapChain(0, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_BGRA_SUPPORT, featureLevels, 1, 7, &swapChainDesc, &swapChain, &device, 0, &deviceContext);
swapChain->GetDesc(&swapChainDesc);
///////////////////////////////////////////////////////////////////////////////////////////////
ID3D11Texture2D* renderTarget;
swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&renderTarget);
ID3D11RenderTargetView* renderTargetView;
device->CreateRenderTargetView(renderTarget, 0, &renderTargetView);
///////////////////////////////////////////////////////////////////////////////////////////////
ID3DBlob* cso;
D3DCompileFromFile(L"shaders.hlsl", 0, 0, "vs", "vs_5_0", 0, 0, &cso, 0);
ID3D11VertexShader* vertexShader;
device->CreateVertexShader(cso->GetBufferPointer(), cso->GetBufferSize(), 0, &vertexShader);
///////////////////////////////////////////////////////////////////////////////////////////////
D3DCompileFromFile(L"shaders.hlsl", 0, 0, "ps", "ps_5_0", 0, 0, &cso, 0);
ID3D11PixelShader* pixelShader;
device->CreatePixelShader(cso->GetBufferPointer(), cso->GetBufferSize(), 0, &pixelShader);
///////////////////////////////////////////////////////////////////////////////////////////////
D3D11_VIEWPORT viewport = { 0, 0, (float)swapChainDesc.BufferDesc.Width, (float)swapChainDesc.BufferDesc.Height, 0, 1 };
///////////////////////////////////////////////////////////////////////////////////////////////
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);
///////////////////////////////////////////////////////////////////////////////////////////////
MSG msg;
GetMessageA(&msg, 0, WM_KEYFIRST, WM_KEYLAST);
}
struct vs_out
{
float4 pos : SV_POSITION;
float4 col : COL;
};
vs_out vs(uint vid : SV_VERTEXID)
{
vs_out output = { float4(vid * 0.5f, vid & 1, 1, 1.5f) - 0.5f, float4(vid == 0, vid == 1, vid == 2, 1) };
return output;
}
float4 ps(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