Skip to content

Instantly share code, notes, and snippets.

@lylejantzi3rd
Forked from mmozeiko/shader.hlsl
Created June 17, 2021 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lylejantzi3rd/60e5e2492d31b6f5e04d9abf1001165a to your computer and use it in GitHub Desktop.
Save lylejantzi3rd/60e5e2492d31b6f5e04d9abf1001165a to your computer and use it in GitHub Desktop.
compute shader for rendering monospaced glyphs in grid
struct TermCell
{
uint GlyphIndex; // index into GlyphMapping buffer
uint Foreground;
uint Background;
};
cbuffer ConstBuffer : register(b0)
{
uint2 CellSize;
uint2 TermSize;
};
// TermSize.x * TermSize.y amount of cells to render as output
StructuredBuffer<TermCell> Cells : register(t0);
// contains x/y coordinate of CellSize rect to take from GlyphTexture
StructuredBuffer<uint2> GlyphMapping : register(t1);
Texture2D<float3> GlyphTexture : register(t2);
RWTexture2D<float4> Output : register(u0);
float3 GetColor(uint i)
{
int r = i & 0xff;
int g = (i >> 8) & 0xff;
int b = (i >> 16) & 0xff;
return float3(r, g, b) / 255.0;
}
// dispatch with (TermSize*CellSize+7)/8 groups for x,y and 1 for z
[numthreads(8, 8, 1)]
void shader(uint3 Id: SV_DispatchThreadID)
{
uint2 ScreenPos = Id.xy;
uint2 CellIndex = ScreenPos / CellSize;
uint2 CellPos = ScreenPos % CellSize;
TermCell Cell = Cells[CellIndex.y * TermSize.x + CellIndex.x];
uint2 GlyphPos = GlyphMapping[Cell.GlyphIndex];
uint2 PixelPos = GlyphPos + CellPos;
float3 Alpha = GlyphTexture[PixelPos];
float3 Background = GetColor(Cell.Background);
float3 Foreground = GetColor(Cell.Foreground);
// TODO: proper ClearType blending
float3 Color = lerp(Background, Foreground, Alpha);
Output[ScreenPos] = float4(Color, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment