Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
innovation through irritation™

d7samurai

💭
innovation through irritation™
View GitHub Profile
@d7samurai
d7samurai / .readme.md
Last active Feb 3, 2023
Minimal D3D11 sprite renderer
View .readme.md

Minimal D3D11 sprite renderer

Minimal D3D11 sprite renderer: basic back-to-front sprite rendering reference code with example sprite sheet animation logic. As usual: Complete, runnable single-function app. No modern C++ / OOP / obscuring cruft.

adventurer

Swap out the sprite sheet with a font atlas for a lightweight GUI / text renderer. Clip individual sprites and glyphs by offsetting screenPos and atlasPos to top left corner of visible area and adjusting size accordingly (all values in pixels):

sprite.screenPos.x += 17; // * FAT_PIXEL_SIZE
sprite.screenPos.y += 10; // * FAT_PIXEL_SIZE
@d7samurai
d7samurai / .readme.md
Last active Jan 14, 2023
Lightweight float_to_string
View .readme.md

Lightweight float_to_string w/padding

Sure, this is a "quick and dirty" solution; it expects well-formed input and definitely doesn't cover every nook and cranny of the IEEE specification. But it does what most people need most of the time, namely (aligned) printouts of floating point numbers within pedestrian parameters - without any fuss.

fts2

float_to_string usage*:

print(float_to_string(563.825, 2,  0,  0 )); // 2 decimals, left  align
print(float_to_string(563.825, 2, 8, '0')); // 2 decimals, right align, total width 8, pad with '0'
View .readme.md

Minimal WASAPI

Minimal WASAPI reference implementation. Runnable console application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft. Produces a steady sine wave sound.

@d7samurai
d7samurai / .readme.md
Last active Feb 1, 2023
Minimal D3D11 bonus material: pixel art antialiasing
View .readme.md

Minimal D3D11 bonus material: pixel art antialiasing

A minimal Direct3D 11 implementation of "antialiased point sampling", useful for smooth fractional movement and non-integer scaling of pixel art AKA "fat pixel" aesthetics.

Also view below side-by-side point sampling comparison on YouTube (video is zoomed in to counter implicit downsampling & compression artifacts and make aa effect more apparent) or check out the Shadertoy.

skull

The actual sampler is set to bilinear filtering (the default D3D11 sampler state) in order to utilize single-texture-read hardware interpolation, then emulating point sampling in the shader and applying AA at the fat pixel boundaries. Use with premultiplied alpha textures* and keep a one pixel transparent border around each sprite/tile.

@d7samurai
d7samurai / .readme.md
Last active Feb 3, 2023
Minimal D3D11 bonus material: extra minimal triangle
View .readme.md

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

@d7samurai
d7samurai / .readme.md
Last active Jan 29, 2023
Minimal D3D11 pt3
View .readme.md

Minimal D3D11 pt3

An elaboration on Minimal D3D11 pt2, adding shadowmapping and incorporating various improvements and alternative approaches to the rendering setup, such as manual vertex fetching, samplerless texture lookup, null shader depth map rendering and procedurally generated texture and instance data.

As before, this is intended to be an an "API familiarizer" - an uncluttered Direct3D 11 setup & basic rendering reference implementation, in the form of a complete, runnable Windows application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft, only ~190 LOC. View on YouTube

minimald3d11pt3

@d7samurai
d7samurai / .readme.md
Last active Jan 28, 2023
Minimal D3D11 pt2
View .readme.md

Minimal D3D11 pt2

Follow-up to Minimal D3D11, adding instanced rendering. As before: An uncluttered Direct3D 11 setup & basic rendering primer / API familiarizer. Complete, runnable Windows application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft.

The main difference here is that the hollow cube is rendered using DrawIndexedInstanced (which saves a lot of vertices compared to the original, so model data is now small enough to be included in the source without being too much in the way), but also all trigonometry and matrix math is moved to the vertex shader, further simplifying the main code.

Each instance is merely this piece of geometry, consisting of 4 triangles:

instanced1

..which is then repeated 24 times, rotated and colored:

View .readme.md

Minimal D3D11

Minimal D3D11 reference implementation: An uncluttered Direct3D 11 setup & basic rendering primer / API familiarizer. Complete, runnable Windows application contained in a single function and laid out in a linear, step-by-step fashion that should be easy to follow from the code alone. ~215 LOC. No modern C++ / OOP / obscuring cruft. View on YouTube

hollowcube

Also check out Minimal D3D11 pt2, reconfigured for instanced rendering and with a smaller, tighter, simplified overall code structure, or Minimal D3D11 pt3, with shadowmapping + showcasing a range of alternative setup / rendering techniques.