Skip to content

Instantly share code, notes, and snippets.

@d7samurai
Last active August 24, 2025 05:57
Show Gist options
  • Save d7samurai/032c63fd91c400bc33fb79c9e74adc46 to your computer and use it in GitHub Desktop.
Save d7samurai/032c63fd91c400bc33fb79c9e74adc46 to your computer and use it in GitHub Desktop.

Revisions

  1. d7samurai revised this gist Mar 24, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
    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
    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;
  2. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    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
    swapchain->GetDesc(&swapchaindesc); // get actual dimensions (see lines 94, 98)

    ///////////////////////////////////////////////////////////////////////////////////////////////

  3. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ID3D11Texture2D* framebuffer;

    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&framebuffer); // get the swapchain's buffer
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&framebuffer); // get the swapchain's frame buffer

    ID3D11RenderTargetView* framebufferRTV;

  4. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ID3D11Texture2D* framebuffer;

    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&framebuffer); // grab buffer from swapchain
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&framebuffer); // get the swapchain's buffer

    ID3D11RenderTargetView* framebufferRTV;

  5. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ID3D11RenderTargetView* framebufferRTV;

    device->CreateRenderTargetView(framebuffer, nullptr, &framebufferRTV);
    device->CreateRenderTargetView(framebuffer, nullptr, &framebufferRTV); // make a render target [view] from it

    FLOAT clearcolor[4] = { 0.1725f, 0.1725f, 0.1725f, 1.0f }; // RGBA

  6. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -99,7 +99,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    D3D11_BUFFER_DESC constantbufferdesc = {};
    constantbufferdesc.ByteWidth = sizeof(constants) + 0xf & 0xfffffff0; // constant buffer size must be multiple of 16
    constantbufferdesc.Usage = D3D11_USAGE_IMMUTABLE;
    constantbufferdesc.Usage = D3D11_USAGE_IMMUTABLE; // never updated
    constantbufferdesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

    D3D11_SUBRESOURCE_DATA constantbufferSRD = { constants };
  7. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gpu.hlsl
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@

    cbuffer constants : register(b0)
    {
    float2 rn_screensize; // { 2 / width, -2 / height }
    float2 rn_screensize; // 2 / width, -2 / height
    }

    struct vertexdesc
  8. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -98,7 +98,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
    float constants[2] = { 2.0f / swapchaindesc.BufferDesc.Width, -2.0f / swapchaindesc.BufferDesc.Height }; // precalc for simple screen coordinate transform in shader (instead of full-on projection matrix)

    D3D11_BUFFER_DESC constantbufferdesc = {};
    constantbufferdesc.ByteWidth = sizeof(constants) + 0xf & 0xfffffff0;
    constantbufferdesc.ByteWidth = sizeof(constants) + 0xf & 0xfffffff0; // constant buffer size must be multiple of 16
    constantbufferdesc.Usage = D3D11_USAGE_IMMUTABLE;
    constantbufferdesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;

  9. d7samurai revised this gist Mar 21, 2025. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -65,8 +65,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    D3D11_INPUT_ELEMENT_DESC inputelementdesc[] = // maps to vertexdesc struct in gpu.hlsl via semantic names ("POS", "COL")
    {
    { "POS", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // float2 position
    { "COL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // float3 color
    { "POS", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // float2 position (x, y)
    { "COL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // float3 color (r, g, b)
    };

    ID3D11InputLayout* inputlayout;
  10. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing [arbitrarily placed triangle vertices with absolute pixel coordinate positioning](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L152-L154) (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and in-shader instanced quad expansion here. As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this line](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this line](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L162) to:
    ```c
    devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
    ```
  11. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing [arbitrarily placed triangle vertices with absolute pixel coordinate positioning](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L154-L156) (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and in-shader instanced quad expansion here. As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.
    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing [arbitrarily placed triangle vertices with absolute pixel coordinate positioning](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L152-L154) (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and in-shader instanced quad expansion here. As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this line](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
  12. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -159,7 +159,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    devicecontext->ClearRenderTargetView(framebufferRTV, clearcolor);

    devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); // 3 vertices per triangle
    devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
    devicecontext->IASetInputLayout(inputlayout);
    devicecontext->IASetVertexBuffers(0, 1, &vertexbuffer, &stride, &offset);

  13. d7samurai revised this gist Mar 21, 2025. 1 changed file with 11 additions and 21 deletions.
    32 changes: 11 additions & 21 deletions cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    device->CreateRenderTargetView(framebuffer, nullptr, &framebufferRTV);

    FLOAT clearcolor[4] = { 0.1725f, 0.1725f, 0.1725f, 1.0f }; // RGBA

    ///////////////////////////////////////////////////////////////////////////////////////////////

    ID3DBlob* vertexshaderCSO;
    @@ -89,6 +91,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    device->CreateRasterizerState(&rasterizerdesc, &rasterizerstate);

    D3D11_VIEWPORT viewport = { 0, 0, (float)swapchaindesc.BufferDesc.Width, (float)swapchaindesc.BufferDesc.Height, 0, 1 };

    ///////////////////////////////////////////////////////////////////////////////////////////////

    float constants[2] = { 2.0f / swapchaindesc.BufferDesc.Width, -2.0f / swapchaindesc.BufferDesc.Height }; // precalc for simple screen coordinate transform in shader (instead of full-on projection matrix)
    @@ -122,21 +126,11 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    device->CreateBuffer(&vertexbufferdesc, nullptr, &vertexbuffer);

    ///////////////////////////////////////////////////////////////////////////////////////////////

    FLOAT clearcolor[4] = { 0.1725f, 0.1725f, 0.1725f, 1.0f }; // RGBA

    D3D11_VIEWPORT viewport = { 0, 0, (float)swapchaindesc.BufferDesc.Width, (float)swapchaindesc.BufferDesc.Height, 0, 1 };

    UINT stride = sizeof(vertexdesc);
    UINT offset = 0;

    ///////////////////////////////////////////////////////////////////////////////////////////////

    vertexdesc* vertexbatch = (vertexdesc*)HeapAlloc(GetProcessHeap(), 0, sizeof(vertexdesc) * MAX_VERTICES); // per frame vertex batch (cpu-local buffer)

    ///////////////////////////////////////////////////////////////////////////////////////////////

    while (true)
    {
    MSG msg;
    @@ -149,19 +143,15 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ///////////////////////////////////////////////////////////////////////////////////////////

    int vertexcount = 0; // start a new vertex batch every frame

    vertexbatch[vertexcount++] = { 150, 100, 1.0f, 0.0f, 0.0f }; // vertex x, y, r, g, b
    vertexbatch[vertexcount++] = { 200, 250, 0.0f, 1.0f, 0.0f };
    vertexbatch[vertexcount++] = { 100, 200, 0.0f, 0.0f, 1.0f };

    ///////////////////////////////////////////////////////////////////////////////////////////

    D3D11_MAPPED_SUBRESOURCE vertexbufferMSR;

    devicecontext->Map(vertexbuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &vertexbufferMSR);
    {
    memcpy(vertexbufferMSR.pData, vertexbatch, sizeof(vertexdesc) * vertexcount); // send vertex batch to gpu
    vertexdesc* vertex = (vertexdesc*)vertexbufferMSR.pData;

    vertex[0] = { 150, 100, 1.0f, 0.0f, 0.0f }; // vertex x, y, r, g, b
    vertex[1] = { 200, 250, 0.0f, 1.0f, 0.0f };
    vertex[2] = { 100, 200, 0.0f, 0.0f, 1.0f };
    }
    devicecontext->Unmap(vertexbuffer, 0);

    @@ -185,10 +175,10 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ///////////////////////////////////////////////////////////////////////////////////////////

    devicecontext->Draw(vertexcount, 0);
    devicecontext->Draw(3, 0); // draw 3 vertices

    ///////////////////////////////////////////////////////////////////////////////////////////

    swapchain->Present(1, 0);
    }
    }
    }
  14. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -151,7 +151,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    int vertexcount = 0; // start a new vertex batch every frame

    vertexbatch[vertexcount++] = { 150, 100, 1.0f, 0.0f, 0.0f }; // x, y, r, g, b
    vertexbatch[vertexcount++] = { 150, 100, 1.0f, 0.0f, 0.0f }; // vertex x, y, r, g, b
    vertexbatch[vertexcount++] = { 200, 250, 0.0f, 1.0f, 0.0f };
    vertexbatch[vertexcount++] = { 100, 200, 0.0f, 0.0f, 1.0f };

  15. d7samurai revised this gist Mar 21, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
    D3D_FEATURE_LEVEL featurelevels[] = { D3D_FEATURE_LEVEL_11_0 };

    DXGI_SWAP_CHAIN_DESC swapchaindesc = {};
    swapchaindesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // note: non-srgb for simplicity here
    swapchaindesc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // non-srgb for simplicity here
    swapchaindesc.SampleDesc.Count = 1;
    swapchaindesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapchaindesc.BufferCount = 2;
  16. d7samurai revised this gist Mar 20, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing [arbitrarily placed triangle vertices with absolute pixel coordinate positioning](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L154-L156) (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and in-shader instanced quad expansion here. As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this line](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
    devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
    ```
  17. d7samurai revised this gist Mar 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -91,7 +91,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ///////////////////////////////////////////////////////////////////////////////////////////////

    float constants[2] = { 2.0f / swapchaindesc.BufferDesc.Width, -2.0f / swapchaindesc.BufferDesc.Height }; // precalc for simple screen position conversion in shader (instead of full-on projection matrix)
    float constants[2] = { 2.0f / swapchaindesc.BufferDesc.Width, -2.0f / swapchaindesc.BufferDesc.Height }; // precalc for simple screen coordinate transform in shader (instead of full-on projection matrix)

    D3D11_BUFFER_DESC constantbufferdesc = {};
    constantbufferdesc.ByteWidth = sizeof(constants) + 0xf & 0xfffffff0;
  18. d7samurai revised this gist Mar 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing [arbitrarily placed triangle vertices with absolute pixel coordinate positioning](https://gist.github.com/d7samurai/a8dc490c54a714f8385f512d95278e0b#file-cpu-cpp-L213-L248) (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and in-shader instanced quad expansion here. As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.
    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing [arbitrarily placed triangle vertices with absolute pixel coordinate positioning](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L154-L156) (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and in-shader instanced quad expansion here. As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
  19. d7samurai revised this gist Mar 19, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing arbitrarily placed triangle vertices. Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.
    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing [arbitrarily placed triangle vertices with absolute pixel coordinate positioning](https://gist.github.com/d7samurai/a8dc490c54a714f8385f512d95278e0b#file-cpu-cpp-L213-L248) (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and in-shader instanced quad expansion here. As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
  20. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing arbitrarily placed triangle vertices. Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.
    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing arbitrarily placed triangle vertices. Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. ~100 LOC. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
  21. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.
    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b) and allowing arbitrarily placed triangle vertices. Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
  22. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

    Plain 2D drawing in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.
    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
  23. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.
    Plain 2D drawing in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
  24. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Minimal D3D11 bonus material: even simpler 2D rendering
    # Minimal D3D11 bonus material: even simpler 2D

    [![image](https://gist.github.com/user-attachments/assets/ed2c558e-69f8-409e-9d26-03bbf2a2a346)](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46)

  25. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ///////////////////////////////////////////////////////////////////////////////////////////////

    D3D11_RASTERIZER_DESC rasterizerdesc = { D3D11_FILL_SOLID, D3D11_CULL_NONE }; // we keep CULL_NONE to be agnostic of triangle winding order
    D3D11_RASTERIZER_DESC rasterizerdesc = { D3D11_FILL_SOLID, D3D11_CULL_NONE }; // CULL_NONE to be agnostic of triangle winding order

    ID3D11RasterizerState* rasterizerstate;

  26. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    ///////////////////////////////////////////////////////////////////////////////////////////////

    D3D11_RASTERIZER_DESC rasterizerdesc = { D3D11_FILL_SOLID, D3D11_CULL_NONE };
    D3D11_RASTERIZER_DESC rasterizerdesc = { D3D11_FILL_SOLID, D3D11_CULL_NONE }; // we keep CULL_NONE to be agnostic of triangle winding order

    ID3D11RasterizerState* rasterizerstate;

  27. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles (no texturing or alpha blending). For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    This 'even simpler' variant renders solid color triangles; no texturing or alpha blending. For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
    devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
    ```
  28. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles (no texturing or alpha blending). For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L186) to:
    This 'even simpler' variant renders solid color triangles (no texturing or alpha blending). For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L172) to:
    ```c
    devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
    ```
  29. d7samurai revised this gist Mar 18, 2025. 1 changed file with 2 additions and 16 deletions.
    18 changes: 2 additions & 16 deletions cpu.cpp
    Original file line number Diff line number Diff line change
    @@ -151,22 +151,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine

    int vertexcount = 0; // start a new vertex batch every frame

    vertexdesc vertex;

    // vertex 1
    vertex.x = 150; // screen position in pixels (xy), relative to top-left
    vertex.y = 100;

    vertex.r = 1.0f; // color (rgb)
    vertex.g = 0.0f;
    vertex.b = 0.0f;

    vertexbatch[vertexcount++] = vertex; // add vertex to batch

    // vertex 2
    vertexbatch[vertexcount++] = { 200, 250, 0.0f, 1.0f, 0.0f }; // x, y, r, g, b

    // vertex 3
    vertexbatch[vertexcount++] = { 150, 100, 1.0f, 0.0f, 0.0f }; // x, y, r, g, b
    vertexbatch[vertexcount++] = { 200, 250, 0.0f, 1.0f, 0.0f };
    vertexbatch[vertexcount++] = { 100, 200, 0.0f, 0.0f, 1.0f };

    ///////////////////////////////////////////////////////////////////////////////////////////
  30. d7samurai revised this gist Mar 18, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion .readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    Plain 2D rendering in D3D11, without the distracting feature set of a [complete sprite renderer](https://gist.github.com/d7samurai/e51adec8a440126d028b87406556079b). Like the [original Minimal D3D11](https://gist.github.com/d7samurai/261c69490cce0620d0bfc93003cd1052), this one uses a canonical 1:1 `TRIANGLELIST` vertex buffer with input layout, so no fancy manual custom buffer fetching and instanced quad expansion in shader etc, as well as absolute pixel coordinate positioning (there's really [no need](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-gpu-hlsl-L23) for the sometimes confusing complication of a full-on projection matrix pipeline for UI and 2D games). As usual: complete, runnable single-function app. No modern C++, OOP or (other) obscuring cruft.

    This 'even simpler' variant renders solid color triangles (no [texturing](https://gist.github.com/d7samurai/a8dc490c54a714f8385f512d95278e0b) or alpha blending). For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L186) to:
    This 'even simpler' variant renders solid color triangles (no texturing or alpha blending). For line drawing, change [this](https://gist.github.com/d7samurai/032c63fd91c400bc33fb79c9e74adc46#file-cpu-cpp-L186) to:
    ```c
    devicecontext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
    ```