[{"content":"Technical notes on graphics, rendering, engine architecture, and software development.\n","date":"6 November 2026","externalUrl":null,"permalink":"/","section":"","summary":"Technical notes on graphics, rendering, engine architecture, and software development.\n","title":"","type":"page"},{"content":"","date":"6 November 2026","externalUrl":null,"permalink":"/post/","section":"Posts","summary":"","title":"Posts","type":"post"},{"content":" A smooth gradient looks simple, but it can be surprisingly difficult to compress. Small errors that disappear inside a detailed texture become visible as bands, color shifts, and block boundaries against a smooth background.\nThe key is how a format represents change: BC1 selects from a small color palette; ETC2 can instead describe a spatial color ramp. BC5, BC7, and ASTC offer different ways to improve precision or flexibility.\nBC1 versus ETC2 # These are actual encoded and decoded images, not simulated artifacts. The source is a gently varying, two-dimensional RGB gradient. Toggle Absolute error ×16 to expose small differences, then magnify the same crop to inspect individual blocks.\nAbsolute error ×16 Magnify the same 32×32 crop Original · 24 bpp RGB\nRMSE 0.00 BC1 · 4 bpp\nRMSE 2.31 ETC1 · 4 bpp\nRMSE 2.43 ETC2 RGB · 4 bpp\nRMSE 0.92 Full 192×192 images. RMSE is measured in 8-bit RGB code values; lower is better. Scroll horizontally on narrow screens to compare all columns.\nEnable JavaScript to toggle amplified error and magnified crops. The images above are actual decoded codec outputs.\nBC1 and ETC2 RGB both encode a 4×4 block into 64 bits: 4 bits per texel. Yet ETC2 is noticeably closer here. Why?\nBC1 Limitations # For an opaque block in four-color mode, BC1 stores:\nTwo RGB565 endpoints: 5 bits red, 6 green, 5 blue each. Two more colors interpolated approximately one-third and two-thirds between them. A 2-bit index per texel, selecting one of those four colors. All four colors lie on a single line in RGB space. This creates two distinct limitations:\nCoarse endpoints. A shallow gradient may change by only a few code values across a block, while the endpoint grid has roughly 8-value steps in red/blue and 4 in green. The encoder must compromise, sometimes introducing a color cast. A small, shared palette. Sixteen texels share at most four colors. If RGB changes in different directions along the image\u0026rsquo;s x and y axes, one color line cannot represent the block\u0026rsquo;s color distribution exactly—even with perfect endpoints. Neighboring blocks make those compromises independently, exposing their boundaries. Four-color BC1 can still represent some simple ramps well; it does not inevitably fail on every gradient. Its alternative three-color/transparent mode does not provide a larger RGB palette.\nETC2\u0026rsquo;s Advantage # For this example, yes: the source image is 192×192 texels. With ETC2\u0026rsquo;s 4×4 blocks, that gives $(192/4)\\times(192/4)=48\\times48=2{,}304$ blocks in total. Inspecting the compressed data shows that all 2,304 blocks (100%) use planar mode. RGB RMSE falls from 2.31 for BC1 to 0.92 for ETC2 in 8-bit code values.\nThe ETC1 comparison is useful: without ETC2\u0026rsquo;s new modes, error is 2.43 in this test. That isolates the practical benefit here, although these numbers describe these encoders and this image—not a universal ranking.\nETC1 Modes # ETC1 also stores a 4×4 RGB block in 64 bits, but it does not interpolate between two endpoints like BC1. Instead, it splits the block into two 2×4 or two 4×2 subblocks. Each half gets a base color and a small palette of brightness offsets.\nReconstruction works in three steps:\nSelect the texel\u0026rsquo;s subblock and its base color. Look up that subblock\u0026rsquo;s modifier table, chosen from eight fixed tables. Each contains four signed offsets; one example is $\\{-17,-5,+5,+17\\}$. Use the texel\u0026rsquo;s 2-bit index to choose an offset $m$, then reconstruct: $$ C=\\operatorname{clamp}_{[0,255]}\\bigl(C_{\\mathrm{base}}+m(1,1,1)\\bigr). $$The same offset is added to R, G, and B. The two ETC1 modes differ only in how the base colors are stored; the split and modifier mechanism stay the same:\nMode Base-color encoding Tradeoff Individual Two independent RGB444 colors The halves can have unrelated colors, but each base channel has only 4-bit precision Differential One RGB555 color plus a signed 3-bit difference per channel for the second Both bases have 5-bit precision, but must be close: each difference is −4 through +3 in the 5-bit domain Differential mode\u0026rsquo;s second base must remain within the valid 0–31 range before expansion to 8-bit channels. The difference is between subblock bases, not between neighboring texels.\nETC1 Block Layout # Think of the block as colors + settings + texel choices:\n24 bits: describe the two base colors. 8 bits: select the modifier tables, color mode, and split direction. 32 bits: each texel selects one of its half\u0026rsquo;s four offsets using 2 bits. Why this design? Small patches of natural imagery often have similar colors with mostly brightness variation. Sharing a base color and four offsets is cheaper than storing eight separate RGB texels. Two halves accommodate a simple color boundary, while different modifier tables handle different contrast levels. Fixed-size blocks and simple arithmetic also make random texture sampling inexpensive for GPU hardware.\nETC1 Example # In individual mode, expand each stored 4-bit channel by multiplying by 17. Then add the selected offset to every channel, clamping to 0–255. For example, A\u0026rsquo;s index 01 gives $(102,136,170)+17=\\mathbf{(119,153,187)}$.\nThe diagram includes both stored bases, selected tables, and all 16 results. Four colors per half means at most eight colors per block.\nIntended use: compact, opaque RGB color textures, especially on older mobile GPUs. Textured surfaces with local brightness variation suit this model better than independent data channels or clean multicolor gradients. The latter expose its restricted palettes and block seams. ETC1 has no planar mode; that is one of ETC2\u0026rsquo;s additions.\nETC2 Modes # ETC2 RGB8 keeps ETC1\u0026rsquo;s two modes and adds three alternatives, chosen per 4×4 block at the same 8-byte size:\nMode Representation Individual ETC1\u0026rsquo;s two independent RGB444 bases and brightness palettes Differential ETC1\u0026rsquo;s more precise, related RGB555 bases and brightness palettes T Four colors: $C_0$, $C_1-d$, $C_1$, $C_1+d$ H Four colors: $C_0-d$, $C_0+d$, $C_1-d$, $C_1+d$ Planar Three RGB676 anchors defining a spatial color ramp Why add modes? ETC1\u0026rsquo;s fixed halves struggle with mixed hues and smooth ramps:\nT/H: handle color groups such as red petals against green leaves. Any texel can select any of the four palette colors, regardless of the subblock split. Adding/subtracting $d$ changes all RGB channels equally, with clamping. T gives one group one color and the other three; H gives each two. Planar: replaces palette selection with horizontal and vertical color changes—better suited to skies and soft gradients. ETC2 Block Layout # Individual/differential keep ETC1\u0026rsquo;s layout. T/H retain texel indices; planar spends those bits on anchors instead:\nThese are logical bit budgets, not exact bit positions. There is no extra mode header: diff = 0 selects individual; otherwise, test the hypothetical second ETC1 differential base before clamping. Overflow outside 0–31 in red → T, otherwise green → H, otherwise blue → planar; no overflow means differential.\nStorage Size # ETC1 and ETC2 RGB8 use 4 bits per texel. Our 192×192 example occupies 2,304 × 8 = 18,432 bytes (18 KiB), versus 108 KiB for RGB8—a 6:1 reduction, excluding headers and mipmaps. Round dimensions up to multiples of four when counting blocks.\nFull ETC2 RGBA8/EAC doubles storage with a separate alpha block: 16 bytes per block, 8 bits per texel. The 1-bit punch-through variant stays at 8 bytes but has different mode rules.\nPlanar Mode # Planar mode stores O at (0,0), H at (4,0), and V at (0,4). O sets the starting color; H−O and V−O define horizontal and vertical change.\nTexel coordinates run from 0 to 3, so H and V sit one step beyond the last texels. Ignoring rounding and clamping:\n$$ C(x,y) \\approx O + \\frac{x}{4}(H-O) + \\frac{y}{4}(V-O). $$Apply this independently to RGB. For example, texel (2,1) uses half the horizontal change and one quarter of the vertical change, as shown above.\nUnlike BC1\u0026rsquo;s four-color line, this can produce 16 distinct colors with independent channel slopes. RGB676 anchors also have one more bit per channel than BC1\u0026rsquo;s endpoints. Smooth curved gradients often approximate a plane over a small block.\nIt remains lossy: anchors are quantized, outputs rounded and clamped, and neighboring blocks may not join perfectly. Curvature and encoder search quality also affect the result.\nBC5 # BC5 stores two independent BC4 blocks: one for R and one for G. Each channel gets 8-bit endpoints and a 3-bit index per texel, selecting from up to eight values. The block occupies 128 bits, or 8 bits per texel.\nThe important difference is independence: red and green no longer have to use the same interpolation index. Here red varies horizontally and green vertically; blue is zero only to visualize the two-channel data.\nAbsolute error ×16 Magnify the same 32×32 crop Original RG · 16 bpp RG\nRMSE 0.00 BC1 · 4 bpp\nRMSE 2.77 BC5 · 8 bpp\nRMSE 0.82 Full 192×192 images. RMSE is measured in 8-bit R/G code values; lower is better. Scroll horizontally on narrow screens to compare all columns.\nEnable JavaScript to toggle amplified error and magnified crops. The images above are actual decoded codec outputs.\nUse BC5 for two-channel data, not arbitrary RGB color. It is particularly useful for normal-map X/Y components. For unit normals on the positive-Z hemisphere, reconstruct:\n$$ z = \\sqrt{\\max(0,1-x^2-y^2)}. $$That works because the third component is constrained, not because BC5 somehow preserves blue. Compression errors may still require renormalization. For just one scalar gradient, BC4 provides the same independent-channel model at 4 bits per texel.\nBC7 # BC7 stores RGB/RGBA in 128-bit 4×4 blocks: 8 bits per texel. Its eight modes trade bits between endpoint precision, interpolation choices, and separate color groups.\nInterpolation # Each texel selects a weight between two endpoints, $E_0$ and $E_1$. For expanded 8-bit endpoint components:\n$$ C=\\left\\lfloor\\frac{(64-w)E_0+wE_1+32}{64}\\right\\rfloor. $$A 2-, 3-, or 4-bit index selects from 4, 8, or 16 fixed weights, respectively. For example, the 2-bit weights are $\\{0,21,43,64\\}$: approximately the endpoints and the one-third/two-thirds positions. More choices can reduce steps along a gradient; more endpoint precision helps place the ramp accurately in the first place.\nSubsets # A subset is a group of texels sharing an endpoint pair. Multi-subset modes choose a predefined partition pattern—not necessarily a rectangular split. Each subset gets its own color ramp.\nSubset Examples # BC7 stores two endpoints per subset, not always two per block. These examples use actual standard partition patterns. Each left-hand cell shows subset:interpolation index; the right-hand grid shows the decoded result.\nTwo subsets, mode 1: partition ID 3 assigns the red group A and green group B along a stepped boundary. It needs four endpoints: A₀/A₁ and B₀/B₁. At (2,0), the partition selects A; index 4 selects weight 37/64 between A\u0026rsquo;s endpoints, giving RGB (207,69,51).\nThree subsets, mode 2: partition ID 0 adds blue group C, requiring six endpoints. At (1,2), the partition selects C; index 2 selects weight 43/64 between C\u0026rsquo;s endpoints, giving RGB (77,133,220).\nThe encoder chooses a pattern from the mode\u0026rsquo;s allowed table—it cannot assign subsets arbitrarily per texel. Once the subset is known, the texel\u0026rsquo;s index selects a color only from that subset\u0026rsquo;s ramp, never by blending all four or six endpoints. Both examples are complete 16-byte blocks, verified against a BC7 decoder.\nMode Summary # Mode Subsets / channels Palette choices per texel Intuition 0 3, RGB 8 Fit three color groups with several shades each; coarser endpoints and only 16 partition patterns 1 2, RGB 8 Balance endpoint precision and shading detail for two color groups 2 3, RGB 4 Fit three groups with fewer shades; offers 64 partition patterns versus mode 0\u0026rsquo;s 16 3 2, RGB 4 Spend more on endpoint precision, less on shades; useful for two nearly flat color groups 4 1, RGB + separate scalar 8 / 4, assignable Let one channel vary independently, giving either stream the finer palette 5 1, RGB + separate scalar 4 / 4 More precise endpoints than mode 4, but fewer interpolation choices 6 1, RGBA together 16 Prioritize a finely sampled ramp; a good candidate for smooth, correlated channel variation 7 2, RGBA together 4 Fit two color/alpha groups when one RGBA ramp is insufficient Modes 0–3 imply opaque alpha. In modes 4/5, the separate scalar is normally alpha, but a channel swap can make it R, G, or B instead. For example, opacity can change without forcing RGB to move along its ramp. In 6/7, all four channels share one index per texel, so they move together within each subset.\nSome modes also use a P-bit: an extra low bit shared across an endpoint\u0026rsquo;s channels, sometimes also across its endpoint pair. It improves endpoint precision cheaply, but does not give every channel a freely chosen extra bit.\nBC7 Block Layout # Every mode spends the same 128 bits, but divides them differently. Two examples:\nMode 1 spends more on endpoints for two RGB ramps; mode 6 spends more on indices for one finely sampled RGBA ramp. Settings include the mode prefix, any partition selector, and P-bits. The index totals are slightly smaller than 16 × index width: one designated texel per subset omits its highest index bit, with endpoint ordering making that bit redundant.\nFor smooth gradients: mode 6 offers more intermediate values; partitions help when one color line is insufficient; modes 4/5 can decouple one channel. These are alternative fits, not quality levels—the encoder chooses per block.\nBC7 has no ETC2-style spatial planar mode: even its 16-color palette is still an endpoint ramp. Its added flexibility reduces RGB RMSE to 0.27 here, versus BC1\u0026rsquo;s 2.31, but remains lossy and costs twice as many bits.\nASTC # ASTC stores 128 bits per block, but lets us choose the block footprint: a larger footprint spreads those bits across more texels. The footprint is fixed for the texture, not chosen independently per block. Within each block, the encoder adapts how those bits are spent.\nLike BC7, it blends endpoints. For one partition, conceptually:\n$$ C(x,y) = (1-w(x,y))C_0 + w(x,y)C_1. $$The main difference is how it represents $w(x,y)$: ASTC can store a lower-resolution weight grid, then interpolate weights at texel positions. A smooth ramp may need only a few stored weights, leaving more bits for accurate endpoint colors.\nAdaptive Encoding # Choice Intuition Endpoint encoding Use a suitable representation—such as luminance, RGB, RGBA, or base-plus-offset—to avoid spending bits on unnecessary color information 1–4 partitions Give different color groups their own endpoint pairs, rather than forcing everything onto one ramp Weight-grid density and precision Use fewer weights for smooth changes, more for detail; trade weight accuracy against endpoint accuracy Dual-plane weights Let one channel, such as alpha, vary independently of the others; unavailable with four partitions For example, with endpoints $(40,80,120)$ and $(200,160,80)$, a weight of $1/4$ gives $(80,100,110)$ before codec-specific rounding. The grid determines that weight; the texel does not need a separately stored palette index.\nASTC Block Layout # Settings: weight-grid configuration, partition count/pattern, endpoint encoding, and any dual-plane channel selection. Endpoints: quantized color values for each partition\u0026rsquo;s ramp. Weights: one or two quantized grids, expanded to texel weights during decoding. Unlike BC7\u0026rsquo;s small set of fixed mode layouts, ASTC varies the endpoint/weight allocation. More partitions or weights leave fewer bits for other data. Its compact integer encoding supports non-power-of-two precision levels too—for example, three values with five possible levels fit in 7 bits, rather than three separate 3-bit fields totaling 9.\nAn ASTC weight plane is not ETC2\u0026rsquo;s planar mode. A single weight field still ties the color channels together along an endpoint line; smooth weight interpolation alone does not represent every RGB plane exactly.\nAbsolute error ×16 Magnify the same 32×32 crop Original · 24 bpp RGB\nRMSE 0.00 BC7 · 8 bpp\nRMSE 0.27 ASTC 4×4 · 8 bpp\nRMSE 0.18 ASTC 6×6 · 3.56 bpp\nRMSE 0.44 ASTC 8×8 · 2 bpp\nRMSE 0.61 Full 192×192 images. RMSE is measured in 8-bit RGB code values; lower is better. Scroll horizontally on narrow screens to compare all columns.\nEnable JavaScript to toggle amplified error and magnified crops. The images above are actual decoded codec outputs.\nASTC footprint Bits per texel RGB RMSE in this test 4×4 8.00 0.18 6×6 3.56 0.44 8×8 2.00 0.62 This simple gradient compresses particularly well, even at larger footprints. Do not generalize that result to detailed or curved content. Larger blocks cover more variation with the same 128 bits and generally make faithful reconstruction harder. Test several footprints on the actual asset.\nConclusion # For smooth gradients, choose a format that preserves small changes rather than just one that compresses well on detailed textures. These are useful starting points, subject to support on the target GPU:\nContent / target Suggested format Why RGB/RGBA on desktop and console with BC7 support BC7, 8 bits per texel Finer interpolation and more flexible endpoint fitting than BC1; a strong default when gradient quality matters RGB/RGBA on ASTC-capable devices ASTC 4×4 for quality; test 6×6 or 8×8 to reduce storage Flexible endpoint and weight representations; larger footprints worked well here, but need validation on the actual gradient Opaque RGB on ETC2-capable devices at 4 bits per texel ETC2 RGB8, with an encoder that considers planar mode Planar mode directly fits spatial color ramps, making it particularly suitable for locally planar gradients Two independent scalar channels BC5, 8 bits per texel Each channel gets its own endpoints and interpolation indices; not a replacement for RGB compression One scalar channel BC4, 4 bits per texel Avoids spending bits on unused color channels and fits the scalar independently BC1 and ETC1 are compatibility or budget choices, not first choices for clean gradients. Their restricted palettes can expose banding and block seams. ETC2 planar mode improves this at the same RGB bitrate, while BC7 and ASTC offer other quality/storage tradeoffs. None guarantees artifact-free results.\nCheck the decoded texture at its intended display size, with the actual color space, filtering, and mipmaps. Use the magnified error views to diagnose differences, not as the sole quality criterion. The measurements above favor this particular test image and encoder configuration; they are not a universal format ranking.\nIf visible artifacts remain unacceptable, keep the gradient uncompressed at suitable precision, or generate it procedurally when practical. Compression is only worthwhile if its savings justify the visible error.\n","date":"6 November 2026","externalUrl":null,"permalink":"/post/016-texture-compression-smooth-gradients/","section":"Posts","summary":"A smooth gradient looks simple, but it can be surprisingly difficult to compress. Small errors that disappear inside a detailed texture become visible as bands, color shifts, and block boundaries against a smooth background.\n","title":"Texture Compression of Smooth Gradients","type":"post"},{"content":" Line Fitting via Least Squares # Given a data with $n$ samples ${(x_0, y_0), (x_1, y_1), ..., (x_{n-1}, y_{n-1})}$, we want to approximate the function with a line $y = f(x) = mx + c$.\nWe want to minimize the the error function by:\n$ \\begin{aligned} \\quad E \u0026= \\sum_i{(f(x_i) - y_i)^2} \\\\\\\\ \u0026= \\sum_i{(m x_i + c - y_i)^2} \\\\\\\\ \\end{aligned} $\nNote the variables are $m$ and $c$ and they can be found by setting each partial derivative to $0$.\nLet\u0026rsquo;s find the first equation from $\\frac{\\partial E}{\\partial m}$:\n$ \\begin{aligned} \\quad \\frac{\\partial E}{\\partial m} \u0026= 0 \\\\\\\\ \\sum_i{2 (m x_i + c - y_i) x_i} \u0026= 0 \\\\\\\\ \\sum_i{2 (m x_i^2 + c x_i - x_i y_i)} \u0026= 0 \\\\\\\\ 2 (m \\sum_i{x_i^2} + c \\sum_i{x_i} - \\sum_i{x_i}{y_i}) \u0026= 0 \\\\\\\\ m \\sum_i{xi^2} + c \\sum_i{x_i} \u0026= \\sum_i{x_i}{y_i} \\\\\\\\ \\end{aligned} $\nsecond equation from $\\frac{\\partial E}{\\partial c}$:\n$ \\begin{aligned} \\quad \\frac{\\partial E}{\\partial c} \u0026= 0 \\\\\\\\ \\sum_i{2 (m x_i + c - y_i)} \u0026= 0 \\\\\\\\ 2 (m \\sum_i{x_i} + c \\sum_i{1} - \\sum_i{y_i}) \u0026= 0 \\\\\\\\ m \\sum_i{x_i} + c n \u0026= \\sum_i{y_i} \\\\\\\\ \\end{aligned} $\nWe have two unknowns $m$ and $c$ and two equations, if you work them out, you\u0026rsquo;ll get:\n$ \\begin{aligned} \\quad m \u0026= \\frac{n(\\sum_i{x_i y_i}) - (\\sum_i{x_i})(\\sum_i{y_i})}{n(\\sum_i{x_i^2}) - (\\sum_i{x_i})^2} \\\\\\\\ \\quad c \u0026= \\frac{(\\sum_i{x_i^2})(\\sum_i{y_i}) - (\\sum_i{x_i})(\\sum_i{x_i y_i})}{n(\\sum_i{x_i^2}) - (\\sum_i{x_i})^2} \\\\\\\\ \\end{aligned} $\n","date":"21 July 2023","externalUrl":null,"permalink":"/post/012-line-fitting/","section":"Posts","summary":"Line Fitting via Least Squares # Given a data with $n$ samples ${(x_0, y_0), (x_1, y_1), ..., (x_{n-1}, y_{n-1})}$, we want to approximate the function with a line $y = f(x) = mx + c$.\n","title":"Line Fitting","type":"post"},{"content":" In graphics, we often deal with spherical function, i.e. function that is defined on the surface of a sphere. Spherical Harmonics (SH) is a commonly used tool to represent spherical function by writing it as an infinite sum of these spherical harmonics. However, in games, we usually limit the number of basis functions to either 4 or 9 basis functions. They are called linear SH (SH2) and quadratic SH (SH3) respectively.\nSH is an orthonormal basis over the sphere $S$ with the following parameterization:\n$\\quad s = \\begin{bmatrix} x \\\\ y \\\\ z \\end{bmatrix} = \\begin{bmatrix} sin\\theta \\; cos\\phi \\\\ sin\\theta \\; sin\\phi \\\\ cos\\theta \\end{bmatrix}$\nLeast Square Formulation # Since there are only 4 coefficients for linear SH, it’s not that difficult to derive fully expanded equations which make things more readable. Let’s go back to basic and see how things work.\nWithout loss of generality, given a function $f(s)$ we want to approximate with basis functions $B_0(s)$, $B_1(s)$, $B_2(s)$ and $B_3(s)$. Let’s call this approximation as $g(s)$.\n$\\quad \\begin{aligned} f(s) \u0026\\approx g(s) \\\\ g(s) \u0026= b_0 B_0(s) + b_1 B_1(s) + b_2 B_2(s) + b_3 B_3(s) \\end{aligned} $\nIn order to find the coefficients $b_0, b_1, b_2, b_3$ we simply solve least square equation:\n$\\quad \\begin{aligned} E(s) \u0026= \\int (g(s) - f(s))^2 ds \\\\ \u0026= \\int ((b_0 B_0(s) + b_1 B_1(s) + b_2 B_2(s) + b_3 B_3(s)) - f(s))^2 ds \\end{aligned} $\nTo find the minimum we just set the derivative of each coefficient to 0:\n$\\quad \\frac{dE(s)}{db_0} = 0, \\frac{dE(s)}{db_1} = 0, \\frac{dE(s)}{db_2} = 0, \\frac{dE(s)}{db_3} = 0$\nSolving those equations and arranging it in a matrix, this will yield:\n$\\quad \\begin{aligned} \\begin{bmatrix} \\int B_0(s) B_0(s) ds \u0026 \\int B_0(s) B_1(s) ds \u0026 \\int B_0(s) B_2(s) ds \u0026 \\int B_0(s) B_3(s) ds \\\\ \\int B_1(s) B_0(s) ds \u0026 \\int B_1(s) B_1(s) ds \u0026 \\int B_1(s) B_2(s) ds \u0026 \\int B_1(s) B_3(s) ds \\\\ \\int B_2(s) B_0(s) ds \u0026 \\int B_2(s) B_1(s) ds \u0026 \\int B_2(s) B_2(s) ds \u0026 \\int B_2(s) B_3(s) ds \\\\ \\int B_3(s) B_0(s) ds \u0026 \\int B_3(s) B_1(s) ds \u0026 \\int B_3(s) B_2(s) ds \u0026 \\int B_3(s) B_3(s) ds \\end{bmatrix} \\begin{bmatrix} b_0 \\\\ b_1 \\\\ b_2 \\\\ b_3 \\end{bmatrix} \u0026= \\begin{bmatrix} \\int f(s) \\; B_0(s) ds \\\\ \\int f(s) \\; B_1(s) ds \\\\ \\int f(s) \\; B_2(s) ds \\\\ \\int f(s) \\; B_3(s) ds \\end{bmatrix} \\\\ A \\; b \u0026= c \\end{aligned} $\nWe can simply choose our basis functions $B_0(s), B_1(s), B_2(s), B_3(s)$ and to find our coefficients, we can simply do\n$\\quad b = A^{-1} c = Proj(f)$\nThis operation of finding coefficients is often called projection.\nStandard SH basis # Standard SH basis are orthonormal so the matrix $A$ is simply the identity matrix, the basis functions [1] and projection are given by:\nStandard SH Basis functions Standard SH Projection $\\quad \\begin{matrix} B_0(s) = \\; \\; \\; (\\frac{1}{2 \\sqrt{\\pi}}) \\; \\; \\\\ B_1(s) = - (\\frac{\\sqrt{3}}{2 \\sqrt{\\pi}}) y \\\\ B_2(s) = \\; \\; \\; (\\frac{\\sqrt{3}}{2 \\sqrt{\\pi}}) z \\\\ B_3(s) = - (\\frac{\\sqrt{3}}{2 \\sqrt{\\pi}}) x \\end{matrix}$ $\\quad \\begin{bmatrix} b_0 \\\\ b_1 \\\\ b_2 \\\\ b_3 \\end{bmatrix} = \\begin{bmatrix} \\int f(s) \\; B_0(s) ds \\\\ \\int f(s) \\; B_1(s) ds \\\\ \\int f(s) \\; B_2(s) ds \\\\ \\int f(s) \\; B_3(s) ds \\end{bmatrix} $ The original function can then be approximated with:\nStandard SH Reconstruction $\\quad g(s) = b_0 B_0(s) + b_1 B_1(s) + b_2 B_2(s) + b_3 B_3(s) $ This is all good however translating this directly in code yields many constants, for example $\\frac{1}{2 \\sqrt{\\pi}}, \\frac{\\sqrt{3}}{2 \\sqrt{\\pi}}$. It turns out we simplify the equations by removing the constants in the basis functions.\nSimplified SH basis # Following Geomerics formulation [2] using orthogonal (but not orthonormal) basis functions:\n$\\quad B_0(s) = 1, B_1(s) = x, B_2(s) = y, B_3(s) = z $\ncomputing $b = A^{-1} \\; c$ (note that A isn’t identity) yields the following coefficients:\n$\\quad \\begin{bmatrix} b_0 \\\\ b_1 \\\\ b_2 \\\\ b_3 \\end{bmatrix} = \\begin{bmatrix} \\frac{1}{4\\pi} \\int f(s) \\; B_0(s) ds \\\\ \\frac{3}{4\\pi} \\int f(s) \\; B_1(s) ds \\\\ \\frac{3}{4\\pi} \\int f(s) \\; B_2(s) ds \\\\ \\frac{3}{4\\pi} \\int f(s) \\; B_3(s) ds \\end{bmatrix} $\nFurthermore, we pull the constant factor $3$ from $b_1(s), b_2(s), b_3(s)$ to the reconstruction function. All of these yields the following:\nSimplified SH Basis functions Simplified SH Projection $\\quad \\begin{matrix} G_0(s) = 1 \\\\ G_1(s) = x \\\\ G_2(s) = y \\\\ G_3(s) = z \\end{matrix}$ $\\quad \\begin{bmatrix} g_0 \\\\ g_1 \\\\ g_2 \\\\ g_3 \\end{bmatrix} = \\begin{bmatrix} \\frac{1}{4\\pi} \\int f(s) \\; G_0(s) ds \\\\ \\frac{1}{4\\pi} \\int f(s) \\; G_1(s) ds \\\\ \\frac{1}{4\\pi} \\int f(s) \\; G_2(s) ds \\\\ \\frac{1}{4\\pi} \\int f(s) \\; G_3(s) ds \\end{bmatrix} $ Simplified SH Reconstruction $\\quad g(s) = g_0 G_0(s) + 3 \\times ( g_1 G_1(s) + g_2 G_2(s) + g_3 G_3(s) ) $ Note that there’s linear relationship between the two coefficients which mean we can use standard SH math and then convert to the simplified version or vice versa.\nStandard to Simplified Simplified to Standard $\\quad \\begin{matrix} g_0 = \\; \\; \\; \\; (\\frac{1}{2\\sqrt{\\pi}}) \\; b_0 \\\\ g_1 = -(\\frac{1}{2\\sqrt{3 \\pi}}) \\; b_3 \\\\ g_2 = -(\\frac{1}{2\\sqrt{3 \\pi}}) \\; b_1 \\\\ g_3 = \\; \\; \\; (\\frac{1}{2\\sqrt{3 \\pi}}) \\; b_2 \\\\ \\end{matrix} $ $\\quad \\begin{matrix} b_0 = \\; \\; \\; \\; (2\\sqrt{\\pi}) \\; g_0 \\\\ b_1 = -(2\\sqrt{3 \\pi}) \\; g_2 \\\\ b_2 = \\; \\; \\; (2\\sqrt{3 \\pi}) \\; g_3 \\\\ b_3 = -(2\\sqrt{3 \\pi}) \\; g_1 \\\\ \\end{matrix}$ Common SH operations # We will discuss common SH operations for both simplified and standard SH.\nProjection of Constant function # Constant function $f(s) = 1$ defined in a spherical cap $\\theta \\in [0, \\pi]$ and $\\phi \\in [0, 2\\pi]$.\nSimplified SH Standard SH $\\quad \\begin{bmatrix} g0 \\\\ g1 \\\\ g2 \\\\ g3 \\end{bmatrix} = \\begin{bmatrix} 1 \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix} $ $\\quad \\begin{bmatrix} b0 \\\\ b1 \\\\ b2 \\\\ b3 \\end{bmatrix} = \\begin{bmatrix} (2\\sqrt{\\pi}) \\\\ 0 \\\\ 0 \\\\ 0 \\end{bmatrix} $ It is clear to see that the reconstructed function $g(s)$ equals exactly to $f(s)$.\nProjection of Dirac Delta Function # Diract delta function $d$ is a function that is zero everywhere except at direction $d$ and integrates to $1$.\n$\\quad \\begin{matrix} \\delta(s) = \\begin{cases} \\infty \u0026\\text{if } s = d \\\\ 0 \u0026\\text{otherwise} \\end{cases} \u0026 \\; \\; \\; \\displaystyle \\int \\delta(s) ds = 1 \\end{matrix} $\nProjection of dirac delta function to SH is simply the evaluation of the basis functions. Representing dirac delta function requires infinite sum so truncating the terms yield inaccurate approximation. This however is the energy conserving projection.\nSimplified SH Standard SH $\\quad \\begin{bmatrix} g0 \\\\ g1 \\\\ g2 \\\\ g3 \\end{bmatrix} = \\begin{bmatrix} \\frac{1}{4\\pi} \\;\\; \\\\ \\frac{1}{4\\pi} x \\\\ \\frac{1}{4\\pi} y \\\\ \\frac{1}{4\\pi} z \\end{bmatrix} $ $\\quad \\begin{bmatrix} b0 \\\\ b1 \\\\ b2 \\\\ b3 \\end{bmatrix} = \\begin{bmatrix} (\\frac{1}{2 \\sqrt{\\pi}}) \\\\ - (\\frac{\\sqrt{3}}{2 \\sqrt{\\pi}}) y \\\\ \\; \\; (\\frac{\\sqrt{3}}{2 \\sqrt{\\pi}}) z \\\\ - (\\frac{\\sqrt{3}}{2 \\sqrt{\\pi}}) x \\end{bmatrix} $ Projection of (Clamped) Cos Function # Since full integration of cos function evaluates to 0, we need to clamp the domain to be hemispherical. Given $f(\\theta, \\phi) = cos(\\theta)$ with $\\theta \\in [0, \\frac{\\pi}{2}]$ and $\\phi \\in [0, 2\\pi]$, the projection to SH is as following:\nSimplified SH Standard SH $\\quad \\begin{bmatrix} g0 \\\\ g1 \\\\ g2 \\\\ g3 \\end{bmatrix} = \\begin{bmatrix} \\frac{1}{4} \\\\ 0 \\\\ 0 \\\\ \\frac{1}{6} \\end{bmatrix} $ $\\quad \\begin{bmatrix} b0 \\\\ b1 \\\\ b2 \\\\ b3 \\end{bmatrix} = \\begin{bmatrix} \\frac{\\sqrt{\\pi}}{2} \\\\ 0 \\\\ \\frac{\\sqrt{\\pi}}{\\sqrt{3}} \\\\ 0 \\end{bmatrix} $ Oriented (clamped) cos function can be obtained by rotating/aligning clamped cos SH to direction $d$.\nSimplified SH Standard SH $\\quad \\begin{bmatrix} g0 \\\\ g1 \\\\ g2 \\\\ g3 \\end{bmatrix} = \\begin{bmatrix} \\frac{1}{4} \\;\\;\\;\\; \\\\ \\frac{1}{6} d_x \\\\ \\frac{1}{6} d_y \\\\ \\frac{1}{6} d_z \\end{bmatrix} $ $\\quad \\begin{bmatrix} b0 \\\\ b1 \\\\ b2 \\\\ b3 \\end{bmatrix} = \\begin{bmatrix} (\\frac{\\sqrt{\\pi}}{2}) \\; \\; \\\\ - (\\frac{\\sqrt{\\pi}}{\\sqrt{3}}) \\; d_y \\\\ \\; \\; \\; (\\frac{\\sqrt{\\pi}}{\\sqrt{3}}) \\; d_z \\\\ - (\\frac{\\sqrt{\\pi}}{\\sqrt{3}}) \\; d_x \\end{bmatrix} $ Convolution with Cos Function # This can be done directly in the frequency domain easily due to circular symmetry (look at Convolution section in [1]). Given a function $f_{SH}(s)$ convolved with Cos Function $h_{SH}(s)$, the resulting function $g_{SH}(s)$ coefficients are given by:\nSimplified SH Standard SH $\\quad \\begin{bmatrix} g0 \\\\ g1 \\\\ g2 \\\\ g3 \\end{bmatrix} = \\begin{bmatrix} \\; \\; \\; \\; \\pi \\; f_{g0} \\; \\; \\\\ \\frac{2}{3} \\pi \\; f_{g1} \\\\ \\frac{2}{3} \\pi \\; f_{g2} \\\\ \\frac{2}{3} \\pi \\; f_{g3} \\end{bmatrix} $ $\\quad \\begin{bmatrix} b0 \\\\ b1 \\\\ b2 \\\\ b3 \\end{bmatrix} = \\begin{bmatrix} \\; \\; \\; \\; \\pi \\; f_{b0} \\; \\; \\\\ \\frac{2}{3} \\pi \\; f_{b1} \\\\ \\frac{2}{3} \\pi \\; f_{b2} \\\\ \\frac{2}{3} \\pi \\; f_{b3} \\end{bmatrix} $ **$f_{g0}, f_{g1}, f_{g2}, f_{g3}$ are coefficients for $f_{SH}$ with simplified SH basis **$f_{b0}, f_{b1}, f_{b2}, f_{b3}$ are coefficients for $f_{SH}$ with standard SH basis\nApplication to Rendering # Ok, now we have the knowledge of Spherical Harmonics, let’s now apply it to solve rendering equation:\n$\\displaystyle \\quad L_o(p, \\omega_o) = L_e(p,\\omega_o) + \\int_{S^2} f(p,\\omega_o,\\omega_i) \\; L_i(p,\\omega_i) \\; (n \\cdot \\omega_i) \\; d\\omega_i$\nIn order to use the equation above for games, we use the following assumptions:\n$c_d$ for the value read from diffuse/albedo texture. Light intensity $I$ which can be defined as $I = \\frac{L_i}{\\pi}$. Energy conserving Lambertian BRDF $f(p,\\omega_o,\\omega_i) = \\frac{c_d}{\\pi}$. We don’t care about emission at the moment, so $L_e(p,\\omega_o) = 0$. The rendering equation can then be rewritten as:\n$\\quad L_o = c_d \\int I_{\\omega_i} \\; (n \\cdot \\omega_i) \\; d\\omega_i = c_d \\cdot Irr(n)$\n**For directional light, the equation above becomes $L_o = c_d \\; I \\; (n \\cdot L)$ **For point light, we will need to add distance attenuation factor $\\frac{1}{d^2}$, so it becomes $L_o = c_d \\; \\frac{I}{d^2} \\; (n \\cdot \\ L)$\nNote that $(n \\cdot \\omega_i)$ is the clamped cosine function and the irradiance $Irr(n)$ can be computed by convolving $I$ with cos function and evaluate it at $n$.\n$\\quad \\begin{aligned} Irr(n) \u0026= \\int I_{\\omega_i} \\; (n \\cdot \\omega_i) \\; d\\omega_i \\\\ \u0026\\approx g_0 G_0(n) + 3 \\; ( g_1 G_1(n) + g_2 G_2(n) + g_3 G_3(n) ) \\\\ \u0026= (\\pi \\; i_{g0}) \\; G_0(n) + 3 \\; ((\\frac{2}{3}\\pi \\; i_{g1}) \\; G_1(n) + (\\frac{2}{3}\\pi \\; i_{g2}) \\; G_2(n) + (\\frac{2}{3}\\pi \\; i_{g3}) \\; G_3(n)) \\\\ \u0026= \\pi \\; (i_{g0} \\; G_0(n) + 2 \\; (i_{g1} \\; G_1(n) + i_{g2} \\; G_2(n) + i_{g3} \\; G_3(n))) \\\\ \u0026= \\pi \\; (i_{g0} + 2 \\; (i_{g1} \\; n_x + i_{g2} \\; n_y + i_{g3} \\; n_z)) \\end{aligned} $\nProjection of Ambient Light # Ambient light can be modeled as a constant in all direction and constant function can be reconstructed exactly in Spherical Harmonics.\nProjection of Directional Light # Directional light can be represented with delta function. So projection of directional light can be done with projection of delta function.\nReconstruction Quality # Unfortunately, linear SH isn’t very accurate even for a simple cos function. There are two main problems:\nNegative value (ringing) Peak value doesn’t match Handling Negative Value # Let’s look at the reconstruction again:\n$\\quad \\begin{aligned} g(s) \u0026= g_0 G_0(s) + 3 \\; ( g_1 G_1(s) + g_2 G_2(s) + g_3 G_3(s) ) \\\\ \u0026= g_0 + 3 \\; ( g_1 x + g_2 y + g_3 z) \\\\ \u0026= g_0 + 3 \\; \\begin{bmatrix} g_1 \\\\ g_2 \\\\ g_3 \\end{bmatrix} \\cdot \\begin{bmatrix} x \\\\ y \\\\ z \\end{bmatrix} \\\\ \u0026= g_0 + 3 \\; g \\cdot d \\end{aligned} $\nSince we are projecting a non-negative signal $g_0$ will be non-negative. In order to guarantee non-negative result, we just need to scale the remaining term so that the minimum value is $0$.\nBasically we want to find $c$ for the following:\n$\\quad \\begin{aligned} min(g(s)) \u0026= 0 \\; \\\\ g_0 + c \\; ( 3 \\; min( g \\cdot d ) ) \u0026= 0 \\\\ g_0 + c \\; ( 3 \\; min( |g| \\; |d| \\; cos(\\theta) ) ) \u0026= 0 \\\\ g_0 + c \\; ( -3 \\; |g| ) \u0026= 0 \\\\ c = \\frac{g_0}{3 \\; |g|} \\end{aligned} $\nNote that we will have 3 scaling factor for each RGB channel. In order to avoid color shifting we choose the minimum scaling factor $c = min(c_r, c_g, c_b)$.\nMatching the Maximum Value for Directional Light # The projection of delta function requires infinite sum and because we truncate the terms, there will be approximation error. For directional light it might be desirable to match the peak so that irradiance is 1 when normal is exactly the same as light direction.\nThe simplest way to do this is to find a constant $c$ to scale the SH coefficients of the directional light. We first project the directional light as delta function with direction $(n_x, n_y, n_z)$ and solve the following:\n$\\quad \\begin{aligned} c \\cdot Irr(n) \u0026= 1 \\\\ c \\cdot \\pi \\; (i_{g0} + 2 \\; (i_{g1} \\; n_x + i_{g2} \\; n_y + i_{g3} \\; n_z)) \u0026= 1 \\\\ c \\cdot \\pi \\; (\\frac{1}{4 \\pi} + 2 \\; (\\frac{1}{4 \\pi} n_x \\; n_x + \\frac{1}{4 \\pi} n_y \\; n_y + \\frac{1}{4 \\pi} n_z \\; n_z)) \u0026= 1 \\\\ c \\cdot \\pi \\; \\frac{3}{4 \\pi} \u0026= 1 \\\\ c \u0026= \\frac{4}{3} \\end{aligned} $\nWe can then use this to find the coefficients for directional light (we can simply scale the coefficients with intensity $I$):\nSimplified SH Standard SH $\\quad \\begin{bmatrix} g0 \\\\ g1 \\\\ g2 \\\\ g3 \\end{bmatrix} = \\begin{bmatrix} \\frac{1}{3\\pi} \\;\\; \\\\ \\frac{1}{3\\pi} x \\\\ \\frac{1}{3\\pi} y \\\\ \\frac{1}{3\\pi} z \\end{bmatrix} $ $\\quad \\begin{bmatrix} b0 \\\\ b1 \\\\ b2 \\\\ b3 \\end{bmatrix} = \\begin{bmatrix} (\\frac{2}{3 \\sqrt{\\pi}}) \\\\ - (\\frac{2}{\\sqrt{3 \\pi}}) y \\\\ \\; \\; (\\frac{2}{\\sqrt{3 \\pi}}) z \\\\ - (\\frac{2}{\\sqrt{3 \\pi}}) x \\end{bmatrix} $ Non Linear Reconstruction # One benefit of pulling the constant 3 from the simplified SH projection is that we can reason about the illumination based on the ratio of the coefficients, i.e. $r = \\frac{|R_1|}{R_0}$, whereby $R_0 = g_0$ and $|R_1| = \\sqrt{{g_1}^2 + {g_2}^2 + {g_3}^2}$.\nGraham Hazel ([3]) provides alternative way to reconstruct irradiance non-linearly by looking at multiple cases of $r$ and generalizing them. The original reference has excellent analysis and explanation, the following is just my note/understanding.\nThe irradiance reconstruction we want to look at:\n$\\quad \\begin{aligned} Irr(n) \u0026= \\pi \\; (g_0 + 2 \\; (g_1 \\; n_x + g_2 \\; n_y + g_3 \\; n_z)) \\\\ \u0026= \\pi \\; (R_0 + 2 \\; R_1 \\cdot n) \\\\ \u0026= \\pi \\; R_0 \\; (1 + 2 \\; \\frac{R_1}{R_0} \\cdot n) \\end{aligned} $\nAmbient light # For ambient light, it’s easy to see $r = 0$.\nDirectional light # For directional light (with direction $d$) projected to SH, the ratio will be:\n$\\quad \\begin{aligned} r \u0026= \\frac{|R_1|}{R_0} \\\\ \u0026= \\frac{\\sqrt{{g_1}^2 + {g_2}^2 + {g_3}^2}}{g_0} \\\\ \u0026= \\frac{\\sqrt{(\\frac{1}{4 \\pi} \\; d_x)^2 + (\\frac{1}{4 \\pi} \\; d_y)^2 + (\\frac{1}{4 \\pi} \\; d_z)^2}}{\\frac{1}{4 \\pi}} \\\\ \u0026= 1 \\end{aligned} $\nGiven above, the irradiance function can be simplified to $Irr(n) = \\pi \\; R_0 \\; (1 + 2 \\; \\frac{R_1}{|R_1|} \\cdot n)$.\nLet’s try to fit $g = (1 + 2 \\; \\frac{R_1}{|R_1|} \\cdot n)$ with quartic polynomial $f(x)$ where $x = \\frac{R_1}{|R_1|}$ and $x \\in [-1, 1]$.\n$\\quad \\begin{aligned} f(x) \u0026= a x^4 + b x^3 + c x^2 + d x + e \\\\ g(x) \u0026= (1 + 2 x) \\end{aligned} $\nWe want the following properties:\nMin value: $f(-1) = 0$. We want non-negative reconstruction Max value: $f(1) = \\frac{4}{3} \\; g(n) = 4$. look at Matching the Maximum Value section Preserve Energy: $\\displaystyle \\int_{-1}^{1} f(x) \\; dx = \\int_{-1}^{1} g(x) \\; dx = 2$ Derivative conditions: $f’(-1) = 0$ and $f”(-1) = 0$ If we put the 5 equations into a form of $Ax = b$, we will have the following:\nA b x $ A = \\begin{bmatrix} 1 \u0026 -1 \u0026 1 \u0026 -1 \u0026 1 \\\\ 1 \u0026 1 \u0026 1 \u0026 1 \u0026 1 \\\\ -4 \u0026 3 \u0026 -2 \u0026 1 \u0026 0 \\\\ 12 \u0026 -6 \u0026 2 \u0026 0 \u0026 0 \\\\ 6 \u0026 0 \u0026 10 \u0026 0 \u0026 30 \\\\ \\end{bmatrix}$ $b = \\begin{bmatrix} 0 \\\\ 4 \\\\ 0 \\\\ 0 \\\\ 30 \\end{bmatrix}$ $x = \\begin{bmatrix} 0 \\\\ \\frac{1}{2} \\\\ \\frac{3}{2} \\\\ \\frac{3}{2} \\\\ \\frac{1}{2} \\end{bmatrix}$ That yields:\n$\\quad \\begin{aligned} f(x) \u0026= \\frac{1}{2} x^3 + \\frac{3}{2} x^2 + \\frac{3}{2} x + \\frac{1}{2} \\\\ \u0026= \\frac{1}{2} (x + 1)^3 \\end{aligned} $\nReferences # https://www.ppsloan.org/publications/StupidSH36.pdf https://community.arm.com/cfs-file/__key/telligent-evolution-components-attachments/01-2066-00-00-00-01-27-70/Simplifying_2D00_Spherical_2D00_Harmonics_2D00_for_2D00_Lighting.pdf https://grahamhazel.com/blog/2017/12/22/converting-sh-radiance-to-irradiance/ https://www.sjbrown.co.uk/posts/ambient-and-directional-lighting-in-spherical-harmonics/ ","date":"30 November 2021","externalUrl":null,"permalink":"/post/010-linear-spherical-harmonics/","section":"Posts","summary":"In graphics, we often deal with spherical function, i.e. function that is defined on the surface of a sphere. Spherical Harmonics (SH) is a commonly used tool to represent spherical function by writing it as an infinite sum of these spherical harmonics. However, in games, we usually limit the number of basis functions to either 4 or 9 basis functions. They are called linear SH (SH2) and quadratic SH (SH3) respectively.\n","title":"Linear Spherical Harmonics","type":"post"},{"content":" Here\u0026rsquo;s my notes on transformation between 3d spaces. All maths tries to follow math textbook notation.\nInverse of 3x3 Matrix # Inverse of 3x3 Matrix can be defined as dot and cross products computation (so it\u0026rsquo;s easier to remember).\n$ \\quad U = \\begin{bmatrix} u_x \\\\\\\\ u_y \\\\\\\\ u_z \\end{bmatrix} \\quad V = \\begin{bmatrix} v_x \\\\\\\\ v_y \\\\\\\\ v_z \\end{bmatrix} \\quad W = \\begin{bmatrix} w_x \\\\\\\\ w_y \\\\\\\\ w_x \\end{bmatrix} $\n$ \\begin{aligned} \\quad M = \\begin{bmatrix} U \\\\\\\\ V \\\\\\\\ W \\\\\\\\ \\end{bmatrix} = \\begin{bmatrix} u_x \u0026 u_y \u0026 u_z \\\\\\\\ v_x \u0026 v_y \u0026 v_z \\\\\\\\ w_x \u0026 w_y \u0026 w_z \\\\\\\\ \\end{bmatrix} \\\\\\\\ \\end{aligned} $\n$ \\displaystyle \\quad M^{-1} = \\begin{bmatrix} \\frac{V \\times W}{U \\cdot (V \\times W)} \u0026 \\frac{W \\times U}{V \\cdot (W \\times U)} \u0026 \\frac{U \\times V}{W \\cdot (U \\times V)} \\end{bmatrix} $\nView Matrix # We can think View Matrix construction as translating the camera to the origin and aligning camera with the standard axes. This can be written as:\n$ \\quad V = R^T \\times T^{-1} = \\begin{bmatrix} u_x \u0026 u_y \u0026 u_z \u0026 0 \\\\\\\\ v_x \u0026 v_y \u0026 v_z \u0026 0 \\\\\\\\ w_x \u0026 w_y \u0026 w_z \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} \\times \\begin{bmatrix} 1 \u0026 0 \u0026 0 \u0026 -p_x \\\\\\\\ 0 \u0026 1 \u0026 0 \u0026 -p_y \\\\\\\\ 0 \u0026 0 \u0026 1 \u0026 -p_z \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} = \\begin{bmatrix} u_x \u0026 u_y \u0026 u_z \u0026 -(U \\cdot P) \\\\\\\\ v_x \u0026 v_y \u0026 v_y \u0026 -(V \\cdot P) \\\\\\\\ w_x \u0026 w_y \u0026 w_z \u0026 -(W \\cdot P) \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} $\nThe inverse is simply:\n$ \\quad V^{-1} = T \\times (R^T)^{-1} = \\begin{bmatrix} 1 \u0026 0 \u0026 0 \u0026 p_x \\\\\\\\ 0 \u0026 1 \u0026 0 \u0026 p_y \\\\\\\\ 0 \u0026 0 \u0026 1 \u0026 p_z \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} \\times \\begin{bmatrix} \\frac{V \\times W}{U \\cdot (V \\times W)} \u0026 \\frac{W \\times U}{V \\cdot (W \\times U)} \u0026 \\frac{U \\times V}{W \\cdot (U \\times V)} \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\end{bmatrix} $\nIf the axes $U, V, W$ are orthonormal then the inverse simplifies to:\n$ \\quad V^{-1} = T \\times R = \\begin{bmatrix} u_x \u0026 v_x \u0026 w_x \u0026 p_x \\\\\\\\ u_y \u0026 v_y \u0026 w_y \u0026 p_y \\\\\\\\ u_z \u0026 v_z \u0026 w_z \u0026 p_z \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} $\nProjection Matrix # Infinite Projection Matrix # Seems like most people have been moving towards infinite projection matrix with reversed Z, meaning nearZ is 1 and farZ is 0. Another nice things is it has simple inverse:\n$ \\quad P = \\begin{bmatrix} \\frac{1}{aspect \\\\; * \\\\; tan(0.5 * FOV_y)} \u0026 0 \u0026 0 \u0026 0 \\\\\\\\ 0 \u0026 \\frac{1}{tan(0.5 * FOV_y)} \u0026 0 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 near \\\\\\\\ 0 \u0026 0 \u0026 1 \u0026 0 \\\\\\\\ \\end{bmatrix} $\n$ \\quad P^{-1} = \\begin{bmatrix} aspect \\\\; * \\\\; tan(0.5 * FOV_y) \u0026 0 \u0026 0 \u0026 0 \\\\\\\\ 0 \u0026 tan(0.5 * FOV_y) \u0026 0 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\\\\\\\ 0 \u0026 0 \u0026 \\frac{1}{near} \u0026 0 \\\\\\\\ \\end{bmatrix} $\n$aspect = \\frac{viewport_{width}}{viewport_{height}}$\nViewport Matrix # Viewport Matrix transforms NDC space to screenPos. Let the bounds in screen space defined as:\n$ \\quad x_{min} = left \\\\\\\\ \\quad x_{max} = right \\\\\\\\ \\quad y_{min} = top \\\\\\\\ \\quad y_{max} = bottom \\\\\\\\ $\nand\n$ \\quad offset = 0.5 * \\begin{bmatrix} x_{min} + x_{max} \\\\\\\\ y_{min} + y_{max} \\end{bmatrix} \\\\\\\\ \\quad extent = 0.5 * \\begin{bmatrix} x_{max} - x_{min} \\\\\\\\ y_{max} - y_{min} \\end{bmatrix} $\nthen the Viewport Matrix $Vp$ is defined as (note that y-axis is flipped):\n$ \\begin{aligned} \\quad V_p \u0026= \\begin{bmatrix} extent_x \u0026 0 \u0026 0 \u0026 offset_x \\\\\\\\ 0 \u0026 -extent_y \u0026 0 \u0026 offset_y \\\\\\\\ 0 \u0026 0 \u0026 1 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} \\\\\\\\ \\quad V_p^-1 \u0026= \\begin{bmatrix} \\frac{1}{extent_x} \u0026 0 \u0026 0 \u0026 -\\frac{offset_x}{extent_x} \\\\\\\\ 0 \u0026 -\\frac{1}{extent_y} \u0026 0 \u0026 \\frac{offset_y}{extent_y} \\\\\\\\ 0 \u0026 0 \u0026 1 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\end{bmatrix} \\end{aligned} $\nFor full screen viewport, we have:\n$ \\begin{aligned} \\quad x_{min} \u0026= 0 \\quad\\quad x_{max} = width \\\\\\\\ \\quad y_{min} \u0026= 0 \\quad\\quad y_{max} = height \\\\\\\\ \\quad offset \u0026= extent = 0.5 * \\begin{bmatrix} width \\\\\\\\ height \\end{bmatrix} \\\\\\\\ \\end{aligned} $\nwhich yields:\n$ \\begin{aligned} \\quad V_p = \\begin{bmatrix} 0.5 * width \u0026 0 \u0026 0 \u0026 0.5 * width \\\\\\\\ 0 \u0026 -0.5 * height \u0026 0 \u0026 0.5 * height \\\\\\\\ 0 \u0026 0 \u0026 1 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\end{bmatrix} \\end{aligned} \\\\\\\\ \\quad V_p^-1 = \\begin{bmatrix} \\frac{2}{width} \u0026 0 \u0026 0 \u0026 -1 \\\\\\\\ 0 \u0026 -\\frac{2}{height} \u0026 0 \u0026 1 \\\\\\\\ 0 \u0026 0 \u0026 1 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 0 \u0026 1 \\end{bmatrix} $\nRotation From To Matrix # One common problem in computer graphics is to compute matrix that rotates unit vector $a$ (from) to another unit vector $b$ (to). The most intuitive way is to use formula for rotation matrix around axis $u$ and angle $\\theta$ [wiki]:\n$ \\begin{aligned} \\quad R \u0026= \\begin{bmatrix} u_x u_x (1 - cos\\theta) + cos\\theta \u0026 u_x u_y (1 - cos\\theta) - u_z sin\\theta \u0026 u_x u_z (1 - cos\\theta) + u_y sin\\theta \\\\\\\\ u_y u_x (1 - cos\\theta) + u_z sin\\theta \u0026 u_y u_y (1 - cos\\theta) + cos\\theta \u0026 u_y u_z (1 - cos\\theta) - u_x sin\\theta \\\\\\\\ u_z u_x (1 - cos\\theta) - u_y sin\\theta \u0026 u_z u_y (1 - cos\\theta) + u_x sin\\theta \u0026 u_z u_z (1 - cos\\theta) + cos\\theta \\end{bmatrix} \\\\\\\\ \u0026= u u^T (1 - cos\\theta) + cos\\theta \\begin{bmatrix} 1 \u0026 0 \u0026 0 \\\\\\\\ 0 \u0026 1 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} + sin\\theta \\begin{bmatrix} 0 \u0026 -u_z \u0026 u_y \\\\\\\\ u_z \u0026 0 \u0026 -u_x \\\\\\\\ -u_y \u0026 u_x \u0026 0 \\\\\\\\ \\end{bmatrix} \\end{aligned} $\nThe steps are:\ncompute rotation axis: $\\quad u = \\frac{from \\times to}{\\lVert from \\times to \\rVert}$ compute rotation angle: $\\quad \\theta = acos(from \\cdot to)$ compute rotation matrix: $\\quad R(u, \\theta)$ Plugging in the following identities:\n$ \\begin{aligned} \\quad v \u0026= a \\times b \\\\\\\\ \\quad u \u0026= \\frac{a \\times b}{\\lVert a \\times b \\rVert} = \\frac{v}{sin\\theta} \\\\\\\\ \\quad uu^T \u0026= \\frac{vv^T}{sin^2\\theta} = \\frac{vv^T}{1 - cos^2\\theta} = \\frac{vv^T}{(1 - cos\\theta)(1 + cos\\theta)} \\\\\\\\ \\quad cos\\theta \u0026= a \\cdot b \\end{aligned} $\nwe can obtain the final rotation matrix:\n$ \\begin{aligned} \\quad R \u0026= u u^T (1 - cos\\theta) + cos\\theta \\begin{bmatrix} 1 \u0026 0 \u0026 0 \\\\\\\\ 0 \u0026 1 \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 1 \\\\\\\\ \\end{bmatrix} + sin\\theta \\begin{bmatrix} 0 \u0026 -u_z \u0026 u_y \\\\\\\\ u_z \u0026 0 \u0026 -u_x \\\\\\\\ -u_y \u0026 u_x \u0026 0 \\\\\\\\ \\end{bmatrix} \\\\\\\\ \u0026= \\frac{vv^T}{1 + a \\cdot b} + \\begin{bmatrix} a \\cdot b \u0026 0 \u0026 0 \\\\\\\\ 0 \u0026 a \\cdot b \u0026 0 \\\\\\\\ 0 \u0026 0 \u0026 a \\cdot b \\end{bmatrix} + \\begin{bmatrix} 0 \u0026 -v_z \u0026 v_y \\\\\\\\ v_z \u0026 0 \u0026 -v_x \\\\\\\\ -v_y \u0026 v_x \u0026 0 \\end{bmatrix} \\\\\\\\ \u0026= \\frac{vv^T}{1 + a \\cdot b} + \\begin{bmatrix} a \\cdot b \u0026 -v_z \u0026 v_y \\\\\\\\ v_z \u0026 a \\cdot b \u0026 -v_x \\\\\\\\ -v_y \u0026 v_x \u0026 a \\cdot b \\end{bmatrix} \\end{aligned} $\nThe final result is exactly the same with [Inigo\u0026rsquo;s].\nScreen Position and Pixel # I make distinction between screen position and pixel so it\u0026rsquo;s clear on my head. This is also part of my own vocabulary:\nScreen Space is a continuous coordinate from [0, width] x [0, height] and Screen Position refers to the center pixel and usually obtained from SV_Position.xy. Pixel is a picture element that can be identified with pair of integer (x, y) and it ranges from [0, width) x [0, height) Given the pixelId (2, 5) the screen position is (2.5, 5.5). Floating point operations # When working with screen position and pixel, it is helpful to understand different behaviors of common floating point operations/conversions:\nfloor, ceil, frac are well understood\nround is used to round a number to the nearest integer. In some languages, this can mean \u0026ldquo;round half away from zero\u0026rdquo;. Various rounding operations are well explained in [wiki].\ntrunc is floor towards zero, i.e. $trunc(x) = sgn(x) \\lfloor |x| \\rfloor$. int(floatValue) actually performs truncation.\nfmod can be implemented as the following:\nfloat fmod(float x, float y) { // return fma(trunc(x/y), -y, x); return x - trunc(x/y) * y; } snapToPixel snaps screen position to the nearest pixel: float2 snapToPixel(float2 screenPos) { // screen | -1.00 | -0.75 | -0.50 | -0.25 | 0.00 | 0.25 | 0.50 | 0.75 | 1.00 | // --------+-------+-------+-------+-------+------+------+------+------+-------+ // pixel | -1.5 | -0.5 | -0.5 | -0.5 | -0.5 | 0.5 | 0.5 | 0.5 | 0.5 | // --------+-------+-------+-------+-------+------+------+------+------+-------+ return ceil(screenPos - 1.0f) + 0.5f; } wrapInt wraps around an integer number (including negative) for a given count. This behaves intuitively and not the same as mod. int wrapInt(int index, int count) { // idx | -5 -4 | -3 -2 -1 | 0 1 2 | 3 4 5 | // -----------------+---------+--------------+--------------+--------------+ // wrapInt(idx, 3) | 1 2 | 0 1 2 | 0 1 2 | 0 1 2 | // index % 3 | -2 -1 | 0 -2 -1 | 0 1 2 | 0 1 2 | // -----------------+---------+--------------+--------------+--------------+ return (index % count + count) % count; } vpIntersect intersect viewport in screen space, this can be useful to make sure sample position doesn\u0026rsquo;t go outside screen space. // ro has to be inside rect float rectIntersect(float2 ro, float2 rd, float2 rectMin, float2 rectMax) { float2 rcp_rd = rcp(rd); float2 t_min = (rectMin - ro) * rcp_rd; float2 t_max = (rectMax - ro) * rcp_rd; float2 t_front = max(t_min, t_max); float t = min(t_front.x, t_front.y); return t; } // viewport intersect in screen space float vpIntersect(float2 screenPos, float2 dir, float2 vpMin, float2 vpMax) { return rectIntersect(screenPos, dir, vpMin + 0.5, vpMax - 0.5); } References # https://docs.microsoft.com/en-us/windows/win32/dxtecharts/the-direct3d-transformation-pipeline https://iquilezles.org/www/articles/noacos/noacos.htm ","date":"26 October 2021","externalUrl":null,"permalink":"/post/009-transformation-notes/","section":"Posts","summary":"Here’s my notes on transformation between 3d spaces. All maths tries to follow math textbook notation.\nInverse of 3x3 Matrix # Inverse of 3x3 Matrix can be defined as dot and cross products computation (so it’s easier to remember).\n","title":"Transformation Notes","type":"post"},{"content":"Many times I found myself having difficulty with compute shaders and its mostly about terminology. As always, my way of overcoming this confusion is to make rules for it. This post is my personal rules about how I think about compute shaders in D3D, hopefully you will find this useful.\nNaming Convention # Multi dimensional (integer) indices are suffixed with id. Flat version of the indices are suffixed with index.\n// 1D uint threadIndex; // one dimension thread index // 2D (nD) uint2 threadId; // two dimensional thread index uint threadIndex = threadId.y * threadCountX + threadId.x; // flattened version of threadId There are three spaces to think about when working with compute shaders:\nGlobal space Group space Thread space Each space is just a partition of the larger space. When working in 2D domain, it\u0026rsquo;s helpful to map global space to the whole image, group space to the sub-rectangle/image tiles and thread space to an individual pixel.\nD3D uses a semi weird (to me) naming for Compute Shader system value semantics. Naming the variable as follow helps me think better:\n// thread space uint2 tid; // SV_GroupThreadID uint2 gsize; // Thread counts per group (available in shader since this is specified in [numthreads()]) uint tindex; // SV_GroupIndex, tindex = tid.y * gsize.x + tid.x // group space uint2 gid; // SV_GroupID uint2 gcount; // Group counts (tile count in each dimension, passed from constant buffer/hardcoded) uint gindex; // gindex = gid.y * gcount.x + gid.x // global space uint2 id; // SV_DispatchThreadID, id = gid * gsize + tid uint2 count; // Thread (pixel) count, count = gcount * gsize uint index; // index = id.y * count.x + id.x Compute Shader Dispatch # When dispatching compute threads, it\u0026rsquo;s help to think that we are working in group space:\nCPU dispatch groups // Total number of groups = gcount.x * gcount.y * gcount.z gcount = subdivideWorkIntoGroups(); // this maps the work (input) space to group space device-\u0026gt;Dispatch(gcount.x, gcount.y, gcount.z); Compute shader works per-group and needs to declare number of threads per group. // Total number of threads per group = gsize.x * gsize.y * gsize.z // gsize = uint3(GROUP_SIZE_X, GROUP_SIZE_Y, GROUP_SIZE_Z) [numthreads(GROUP_SIZE_X, GROUP_SIZE_Y, GROUP_SIZE_Z)] void cs_main(uint3 id : SV_DispatchThreadID, // global space uint3 gid : SV_GroupID, // group space uint3 tid : SV_GroupThreadID, // thread space uint tindex : SV_GroupIndex) // thead space { } Group size is usually correlated with groupshared memory size (16 KB per group). Usually, we want each thread to load data into groupshared memory, so:\nmax number of threads = 16KB / sizeof(per_thread_data).\nIf the problem has natural grouping (cluster) thats too big to fit into groupshared memory, the next best thing is to map this to wavefront size (64 for AMD). You will need to have a mapping from the application cluster space to group space.\nLimits # Some useful limits when designing ComputeShader:\ngroupsharedCapacity = 16KB; gcountMax = uint3(64K - 1); // D3D11_CS_DISPATCH_MAX_THREAD_GROUPS_PER_DIMENSION // D3D 11.x hardware gsizeMax = uint3(1024, 1024, 64); // D3D11_CS_THREAD_GROUP_MAX_X{_Y}{_Z} assert(gsizeMax.x * gsizeMax.y * gsizeMax.z \u0026lt; 1024); // D3D11_CS_THREAD_GROUP_MAX_THREADS_PER_GROUP // D3D 10.x hardware gsizeMax = uint3(768, 768, 1); // D3D11_CS_4_X_THREAD_GROUP_MAX_X{_Y}, D3D11_CS_4_X_DISPATCH_MAX_THREAD_GROUPS_IN_Z_DIMENSION assert(gsizeMax.x * gsizeMax.y * gsizeMax.z \u0026lt; 768); // D3D11_CS_4_X_THREAD_GROUP_MAX_THREADS_PER_GROUP Links # Compute Shader Overview - https://docs.microsoft.com/en-us/windows/win32/direct3d11/direct3d-11-advanced-stages-compute-shader ","date":"19 May 2021","externalUrl":null,"permalink":"/post/007-compute-shader-spaces/","section":"Posts","summary":"Many times I found myself having difficulty with compute shaders and its mostly about terminology. As always, my way of overcoming this confusion is to make rules for it. This post is my personal rules about how I think about compute shaders in D3D, hopefully you will find this useful.\n","title":"Compute Shader Spaces","type":"post"},{"content":" This is one of the most important equation for rendering:\n$\\displaystyle \\quad L_o(p, \\omega_o) = L_e(p,\\omega_o) + \\int_{S^2} f(p,\\omega_o,\\omega_i) \\\\; L_i(p,\\omega_i) \\\\; (n \\cdot \\omega_i) \\\\; d\\omega_i$\nThe equation above requires integration of incoming radiance over all possible directions in the hemisphere. Note that this formulation is more suited for distant lighting which is why it\u0026rsquo;s weighted by the solid angle $d\\omega_i$.\nSpherical Coordinates Formulation # When trying to come up with analytical solution to the rendering equation, we often do this in spherical coordinates. We need to take into account the change of domain of integration [wiki]:\n$\\quad d\\omega = sin\\theta \\\\; d\\theta d\\phi$\nWhich yields:\n$\\displaystyle \\quad L_o(p, \\omega_o) = L_e(p, \\omega_o) + \\int_{0}^{2\\pi}\\int_{0}^{\\pi} f(p,\\omega_o,\\theta,\\phi) \\\\; L_i(p,\\theta,\\phi) \\\\; |cos\\theta| \\\\; sin\\theta \\\\; d\\theta \\\\; d\\phi$\nSurface Formulation # In the context of patch tracing, it\u0026rsquo;s more common to formulate the rendering equation based on patches that we hit. This is done by considering all the visible hit points from rays shot from upper hemisphere of $p$.\nConsider starting at point $p = x$ and casting a ray in the direction of $\\omega_i$. Let $y = ray(x, \\omega_i)$ be the hit-point for the raycast. Assuming there\u0026rsquo;s no participating media, radiance is constant along rays through the scene and therefore $L(y, -\\omega_i) = L(p, \\omega_i)$ (we can drop the suffix from $L_i$ and $L_o$ for simplicity). If we just want to consider all points above the hemisphere of $x$, we will need to introduce visibility function $V(x, y)$ between the two points $x$ and $y$. Finally, we will need to account for the change of domain of integration from differential solid angle $d\\omega_i$ to differential area $dA_y$:\n$\\displaystyle \\quad d\\omega_i = \\frac{|cos\\theta_y|}{{r_{xy}}^2} \\\\; dA_y$\nTo simplify notation, we will introduce geometric term $G(x, y)$:\n$\\displaystyle \\quad G(x, y) = \\frac{|cos \\theta_x| \\\\; |cos\\theta_y|}{{r_{xy}}^2}$\nNote:\n$|cos \\theta_x| = max(n \\cdot \\omega_i, 0)$ $|cos \\theta_y| = max(n \\cdot -\\omega_i, 0)$ Which yields the following equation for surfaces:\n$\\displaystyle \\quad L(x, \\omega_0) = L_e(x,\\omega_0) + \\int_{M} f(x,\\omega_o,\\omega_i) \\\\; L(y, -\\omega_i) \\\\; V(x, y) \\\\; G(x, y) \\\\; dA_y$\nTable of Contents # Radiometry Irradiance Solid Angle Notation Radiometry # In order to understand the rendering equation, it\u0026rsquo;s important to get familiar with basic radiometric quantities oftenly used in rendering: flux, irradiance, radiant exitance, intensity and radiance. They are basically different ways of measuring photons. A photon at wavelength $\\lambda$ carries energy $Q = \\frac{h c}{\\lambda}$ where h is Planck\u0026rsquo;s constant and c is the speed of light. The four basic quantities can be derived from energy (J) by taking limits over time, area and directions.\nQuantity Formula Unit Definition (Radiant) Flux $$\\displaystyle \\Phi = \\frac{dQ}{dt}$$ $$W$$ Total amount of energy passing through surface per unit time (J/s). Also known as power. Irradiance $$\\displaystyle E = \\frac{d\\Phi}{dA}$$ $$W/m^2$$ Area density of arriving at a surface. Radiant exitance $$\\displaystyle M = \\frac{d\\Phi}{dA}$$ $$W/m^2$$ Area density of flux leaving a surface. (Radiant) Intensity $$\\displaystyle I = \\frac{d\\Phi}{d\\omega} $$ $$W/sr$$ Angular density of emitted power. Intensity describes directional distribution of light but only meaningful for point light sources. Radiance $$\\displaystyle L = \\frac{d\\Phi}{d\\omega \\\\; dA \\\\; cos\\theta}$$ $$W/(m^2 sr)$$ Flux density per unit area, per unit solid angle. Irradiance # Irradiance is an average density of power over a finite area $\\displaystyle E = \\frac{\\Phi}{A}$. We can compute irradiance at point $E(p)$ by taking the $\\displaystyle \\lim_{A \\to 0} \\frac{\\Delta \\Phi(p)}{\\Delta A} = \\frac{d\\Phi}{dA}$.\nIrradiance can also be computed by taking integral of radiance:\n$\\quad E(p,n) = \\displaystyle \\int_{S^2} L_i(p, \\omega) \\\\; |cos \\theta| \\\\; d\\omega$\nIrradiance via Monte Carlo # We can compute irradiance vis Monte Carlo estimator $\\hat{E} = \\frac{1}{N} \\sum_{i=0}^{N-1} \\frac{f(X_i)}{pdf(X_i)}$.\nFor uniform hemisphere sampling, pdf is just a constant $\\frac{1}{2 \\pi}$, which makes the estimator to be:\n$\\quad \\hat{E} = \\displaystyle \\frac{2 \\pi}{N} \\sum_{i=0}^{N-1} L_i \\\\; cos \\theta$\nFor cosine hemisphere sampling, pdf becomes $\\frac{cos \\theta}{\\pi}$, which makes the estimator to be:\n$\\quad \\hat{E} = \\displaystyle \\frac{\\pi}{N} \\sum_{i=0}^{N-1} L_i$\nSolid Angle # Solid angle $\\Omega$ is defined as:\n$\\displaystyle \\quad \\Omega = \\frac{A}{r^2}$\nwhere $A$ is the spherical surface area and $r$ is the radius of a sphere.\nDifferential Solid Angle # Given a unit sphere parameterized by $(\\phi, \\theta)$\n$\\quad x = sin\\theta \\\\; cos\\phi \\\\\\\\ \\quad y = sin\\theta \\\\; sin\\phi \\\\\\\\ \\quad z = cos\\theta$\nthe differential solid angle is\n$\\quad d\\Omega = sin(\\theta) d\\theta d\\phi$\nSpherical Cap # The projection of a sphere onto a surface of another sphere is spherical cap. Solid angle of spherical cap with apex angle $2\\theta$ is given by:\n$\\quad \\Omega = 2 \\pi (1 - cos\\theta)$\nWe can use this to approximate sphere radius $R$ at distance $d$ given solid angle $\\Omega$ (only valid when $d \u003e= R$):\n$\\quad sin\\theta = \\frac{R}{d}$\n$\\quad R^2 = d^2 \\\\; (1 - (1 - \\frac{\\Omega}{2\\pi})^2) = d^2 \\\\; \\frac{\\Omega}{2\\pi} (2 - \\frac{\\Omega}{2\\pi})$\nNotation # Linear Operators # Sometimes the rendering equation is written in operator equations which tend to be more concise than the integral equation.\nK is the local reflection operator that maps the field/incoming radiance function to the surface radiance function after one reflection (think BRDF). G is the field radiance operator that expressed the incident field radiance at each point in terms of the surface radiance of the surrounding environment. $$\\displaystyle (\\bm{G}L)(p,\\omega) \\equiv L(ray(p,\\omega), \\omega)$$ $ray(p, \\omega)$ is a raycast function that returns the first hit point. References # The Light Transport Equation: http://www.pbr-book.org/3ed-2018/Light_Transport_I_Surface_Reflection/The_Light_Transport_Equation.html Radiometry: https://pbr-book.org/3ed-2018/Color_and_Radiometry/Radiometry A Framework for the Analysis of error in Global Illumination Algorithms: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.114.5283\u0026rep=rep1\u0026type=pdf ","date":"14 May 2021","externalUrl":null,"permalink":"/post/006-the-rendering-equation/","section":"Posts","summary":"This is one of the most important equation for rendering:\n$\\displaystyle \\quad L_o(p, \\omega_o) = L_e(p,\\omega_o) + \\int_{S^2} f(p,\\omega_o,\\omega_i) \\\\; L_i(p,\\omega_i) \\\\; (n \\cdot \\omega_i) \\\\; d\\omega_i$\nThe equation above requires integration of incoming radiance over all possible directions in the hemisphere. Note that this formulation is more suited for distant lighting which is why it’s weighted by the solid angle $d\\omega_i$.\n","title":"The Rendering Equation","type":"post"},{"content":" The idea of Fourier is that a function (signal) can be decomposed as infinite number of sine/cosine waves (harmonics). We can consider $sin$ and $cos$ as the basis function and they form an orthogonal basis.\nWe can lookup on the Internet regarding the topic of Fourier and we will find there are extensive knowledge. Studying the terminology is important to avoid confusion. For starter, the concept of Fourier manifested into many variants:\nContinuous Time Discrete Fourier Series (CT-DFS) Discrete Time Discrete Fourier Series (DT-DFS) Continuous Time Fourier Transform (CTFT) Discrete Time Fourier Transform (DTFT) Discrete Fourier Transform (DFT) Fast Fourier Transform (FFT) The differences can be summarized in this table:\nSeries Input Periodicity Input Resolution Output Periodicity Output Resolution CT-DFS Infinite Periodic Continuous Aperiodic Discrete DT-DFS Infinite Periodic Discrete Periodic Discrete CTFT Infinite Aperiodic Continuous Aperiodic Continuous DTFT Infinite Aperiodic Discrete Periodic Continuous DFT Finite Aperiodic Discrete Periodic Discrete Note that FFT is an implmentation of DFT and practically what we use for computation. For more details on the differences, consult http://complextoreal.com/wp-content/uploads/2013/04/fft5.pdf.\nFourier Transform Pair # Forward Fourier Transform $\\mathcal{F} \\\\{f(x)\\\\}$ is mathematical operation that transforms a function of time (or space) into a function of frequency. Backward (Inverse) Fourier Transform $\\mathcal{F}^{-1}\\\\{\\hat{f}(\\omega)\\\\}$ reconstructs the function of time from function of frequency. Together they form Fourier Transform pair.\nFourier Transform is commonly expressed in terms of complex exponentials (instead of sine/cosine). Recall Euler\u0026rsquo;s Formula is $e^{ix} = cos(x) + i \\\\; sin(x)$.\nIn addition, since:\n$\\quad \\langle e^{imx}, e^{-inx} dx \\rangle = \\displaystyle\\int_{-\\pi}^{\\pi} e^{imx} e^{-inx} dx = \\begin{cases} 0 \u0026 \\quad m \\neq n \\\\\\\\ 2\\pi \u0026 \\quad m = n \\end{cases} $\n$\\quad \\langle e^{i 2\\pi mx}, e^{-i 2\\pi nx} dx \\rangle = \\displaystyle\\int_{-\\pi}^{\\pi} e^{i 2\\pi mx} e^{-i 2\\pi nx} dx = \\begin{cases} 0 \u0026 \\quad m \\neq n \\\\\\\\ 1 \u0026 \\quad m = n \\end{cases} $\nThis makes chosing $e^{i 2\\pi \\xi x}$ as basis function convenient. Note that $\\xi$ is used to denote an ordinary frequency (Hz) whereas $\\omega = 2 \\pi \\xi$ is for angular frequency (rad/s).\nContinuous Time Fourier Transform (CTFT) pair is given as:\nForward Transform: $\\hat{f}(\\xi) = \\displaystyle\\int_{-\\infty}^\\infty f(x) \\\\; e^{-i 2\\pi \\xi x} dx$ Backward Transform: $f(x) = \\displaystyle\\int_{-\\infty}^\\infty \\hat{f}(\\xi) \\\\; e^{i 2\\pi \\xi x} d\\xi$ In Discrete Time Fourier Transform (DTFT), the function $f(x)$ is sampled with period $T_s$ (freq $\\frac{1}{\\xi_s}$) and this can be expressed as:\n$\\quad f_s(x) = \\displaystyle\\sum_{n=-\\infty}^{\\infty} f(nT_s) \\\\; \\delta(x - nT_s)$\nplugging $f_s$ to CTFT forward transform yields:\n$\\quad \\hat{f}(\\xi) = \\displaystyle\\sum_{n=-\\infty}^\\infty f(nT_s) \\\\; e^{-i 2\\pi \\xi nT_s}$\nand let the n-th sample $f_n = f(nT_s)$ and $\\hat{\\xi} = \\xi \\\\; T_s = \\xi / \\xi_s$, this simplifies to:\n$\\quad \\hat{f}(\\hat{\\xi}) = \\displaystyle\\sum_{n=-\\infty}^{\\infty} f_n \\\\; e^{-i 2 \\pi \\hat{\\xi} n}$\nNote that the output of CTFT and DTFT are continuous function of frequency. In addition DTFT requires infinite sum which is not suitable for computation. Discrete Fourier Transform (DFT) approximates the result by sampling the output function.\nGiven $L$ samples of a function $f$ and $\\hat{f}$ is discretized by $N$, DFT can be expressed as:\n$\\quad \\hat{f_k} = \\displaystyle\\sum_{n=0}^{L-1} f_n \\\\; e^{-i \\frac{2\\pi}{N} k n}$\nNote: in most implementation $L = N$.\nTo summarize, Fourier Transform pairs are:\nForward Transform Backward (Inverse) Transform CTFT $\\hat{f}(\\xi) = \\displaystyle\\int_{-\\infty}^\\infty f(x) \\\\; e^{-i 2\\pi \\xi x} dx$ $f(x) = \\displaystyle\\int_{-\\infty}^\\infty \\hat{f}(\\xi) \\\\; e^{i 2\\pi \\xi x} d\\xi$ DFT $\\hat{f_k} = \\displaystyle\\sum_{n=0}^{N-1} f_n \\\\; e^{-i \\frac{2\\pi}{N} k n}$ $f_n = \\frac{1}{N} \\displaystyle\\sum_{k=0}^{N-1} \\hat{f_k} \\\\; e^{i \\frac{2\\pi}{N} k n}$ Properties of Fourier Transform # Spectral Derivative # The benefit of doing computation in Fourier space is that it is easy to compute derivative (a.k.a Spectral derivative).\n$\\mathcal{F}\\\\{\\frac{d}{dx} f(x)\\\\} = \\displaystyle\\int_{-\\infty}^\\infty \\frac{df}{dx} e^{-i \\omega x} dx$\n$ = -\\displaystyle\\int_{-\\infty}^\\infty f(x) (-i \\omega e^{-i \\omega x})dx + \\Bigl[ f(x) e^{-i \\omega x}\\Bigr]_{-\\infty}^\\infty$\n$ = i \\omega \\displaystyle\\int_{-\\infty}^\\infty f(x) e^{-i \\omega x} dx$\n$ = i \\omega \\mathcal{F}\\\\{f(x)\\\\}$\nIt turns out that computing FFT-based spectral derivative isn\u0026rsquo;t as straightforward as above. For complete discussion on this topic, please consult https://math.mit.edu/~stevenj/fft-deriv.pdf.\nIn summary, algorithm to compute first derivative:\nGiven $f_n$ for $0 \\le n \\lt N$, use an FFT to compute $\\hat{f_k}$ for $0 \\le k \\lt N$ Obtain $\\hat{f_k}^\\prime$ by multiplying $\\begin{cases} i \\frac{2 \\pi}{N} k \u0026 \\quad k \\lt N/2 \\\\\\\\ i \\frac{2 \\pi}{N} (k - N) \u0026 \\quad k \\gt N/2 \\\\\\\\ 0 \u0026 \\quad k = N/2 \\\\; (N \\\\; is \\\\; even) \\end{cases} $ Compute $f_n^\\prime$ from $\\hat{f_k}^\\prime$ via inverse FFT To compute second derivative:\nGiven $f_n$ for $0 \\le n \\lt N$, use an FFT to compute $\\hat{f_k}$ for $0 \\le k \\lt N$ Obtain $\\hat{f_k}^{\\prime\\prime}$ by multiplying $\\begin{cases} -(\\frac{2 \\pi}{N} k)^2 \u0026 \\quad k \\le N/2 \\\\\\\\ -(\\frac{2 \\pi}{N} (k-N))^2 \u0026 \\quad k \\gt N/2 \\end{cases} $ Compute $f_n^{\\prime\\prime}$ from $\\hat{f_k}^{\\prime\\prime}$ via inverse FFT Convolution # Convolution is defined as $(f * g) = \\displaystyle\\int_{-\\infty}^{\\infty} f(x - \\xi) g(\\xi) d\\xi$\nFourier Transform property for convolution is that:\n$\\mathcal{F}\\\\{(f * g)\\\\} = \\mathcal{F}\\\\{f\\\\} \\mathcal{F}\\\\{g\\\\} = \\hat{f} \\hat{g}$\nThis basically means convolution equals to multiplication in Fourier space. This is another benefit of Fourier Transform.\nParseval\u0026rsquo;s Theorem # The energy in $f$ is proportional with energy in its fourier transform $\\hat{f}$\n$\\displaystyle\\int_{-\\infty}^{\\infty} \\lvert \\hat{f(\\omega)} \\rvert^2 d\\omega = 2 \\pi \\displaystyle\\int_{-\\infty}^{\\infty} \\lvert f(x) \\rvert^2 dx$\nThis theorem is useful because it verifies that if we truncate the small Fourier coefficients, we are still capturing most of function $f$.\nLinear Operator # It\u0026rsquo;s useful to know that Fourier Transform is a linear operator:\n$\\mathcal{F}\\\\{\\alpha f(x) + \\beta g(x)\\\\} = \\alpha \\mathcal{F}\\\\{f\\\\} + \\beta \\mathcal{F}\\\\{g\\\\}$\nReferences # The Discrete Fourier Transform (DFT) - https://www.youtube.com/watch?v=nl9TZanwbBk Discrete Fourier Transform, DFT and FFT - http://complextoreal.com/wp-content/uploads/2013/04/fft5.pdf Notes on FFT-based differentiation - https://math.mit.edu/~stevenj/fft-deriv.pdf DSP Topic 7: Understanding the Discrete Fourier Transform (DFT) - https://www.youtube.com/watch?v=zSnuw4tM1kw ","date":"16 March 2021","externalUrl":null,"permalink":"/post/005-discrete-fourier-transform/","section":"Posts","summary":"The idea of Fourier is that a function (signal) can be decomposed as infinite number of sine/cosine waves (harmonics). We can consider $sin$ and $cos$ as the basis function and they form an orthogonal basis.\n","title":"Discrete Fourier Transform","type":"post"},{"content":"C++ has stream classes that are useful for input and output operations on files, io devices and strings. For example, we can use std::ostream to output to std::stringstream. However, there\u0026rsquo;s no built-in way to pre-allocate output buffer of std::stringstream which means there might be multiple allocations call as we output more bytes to the stream. Luckily, stream exposes the buffer that we can inherit and override some of the methods.\nDoing a full and proper implementation of custom std::streambuf is out of scope for this short article. I am just going to show a simple custom std::streambuf that pre-allocates a fixed capacity buffer used for stream out. We will use std::string to manage this custom string buffer and manipulate std::streambuf internal put pointers (so that tellp() will also work). The following protected members of std::streambuf can be used for manipulating put pointers (see https://www.cplusplus.com/reference/streambuf/streambuf/ for more complete description):\npbase() - pointer to the beginning of output sequence pptr() - pointer to current position of output sequence epptr() - pointer to end of output sequence pbump() - increase put pointer setp() - set output sequence pointers Here\u0026rsquo;s the code:\nclass FixedStreamBuf : public std::streambuf { std::string\u0026amp; buffer; public: FixedStreamBuf(std::string\u0026amp; buffer, size_t bufferMaxSize) : buffer(buffer) { buffer.resize(bufferMaxSize); setp(buffer.data(), buffer.data() + buffer.size()); } ~FixedStreamBuf() { const size_t writtenSize = pptr() - pbase(); buffer.resize(writtenSize); } protected: std::streamsize xsputn(const char* s, std::streamsize n) override { std::memcpy(pptr(), s, n); pbump(n); return n; }; int_type overflow(int_type ch) override { // Fixed stream buffer doesn\u0026#39;t handle overflow because we don\u0026#39;t want to throw std::runtime_error(\u0026#34;FixedStreamBuf overflow!\u0026#34;); } pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which) override { // Only output stream is supported if (which != std::ios_base::out) throw std::runtime_error(\u0026#34;Not supported\u0026#34;); if (dir == std::ios_base::cur) pbump(off); else if (dir == std::ios_base::end) setp(pbase(), epptr() + off, epptr()); else if (dir == std::ios_base::beg) setp(pbase(), pbase() + off, epptr()); return pptr() - pbase(); } }; Simple example on how to use this:\nstd::string streamData; // write to streamData via std::ostream { FixedStreamBuf streamBuf(streamData, 1024); std::ostream ostr(\u0026amp;streamBuf); ostr \u0026lt;\u0026lt; \u0026#34;Hello World!\u0026#34;; } // do something with streamData doSomething(streamData); References # std::streambuf C++ reference - https://www.cplusplus.com/reference/streambuf/streambuf/ Custom Stream Buffers - http://videocortex.io/2017/custom-stream-buffers/ ","date":"10 February 2021","externalUrl":null,"permalink":"/post/004-cpp-custom-streambuf/","section":"Posts","summary":"C++ has stream classes that are useful for input and output operations on files, io devices and strings. For example, we can use std::ostream to output to std::stringstream. However, there’s no built-in way to pre-allocate output buffer of std::stringstream which means there might be multiple allocations call as we output more bytes to the stream. Luckily, stream exposes the buffer that we can inherit and override some of the methods.\n","title":"Custom std::streambuf","type":"post"},{"content":"I have started using Ubuntu 20.04 LTS on my Surface Pro 3. Simple story is that I got fed up with the usual Windows issues (Windows update, background processes causing slowdown, etc) and decided to try Linux (I am still using Windows 10 on my desktop PC though).\nSwitching to Ubuntu 20.04 is not without issues, here I am sharing all issues I encountered.\nInstallation Issues # The plan was to dual boot Windows 10 and Ubuntu 20.04 LTS on Surface Pro 3.\nDisabling Bitlocker encryption # Ubuntu doesn\u0026rsquo;t like that the harddrive is protected with Bitlocker encryption. I had to disable it via Windows 10 command line:\nmanage-bde -off C: To check whether Bitlocker is fully disabled, use:\nmanage-bde status Install alongside windows # I wanted a hassle free installation, so after watching couple tutorial, I am made aware of \u0026lsquo;Install alongside windows\u0026rsquo; option. However, the option didn\u0026rsquo;t appear when I first tried to install Ubuntu. It turned out I didn\u0026rsquo;t have enough harddisk space! So after clearing up some space, the option appears.\nUsage Issues # Chrome complains access to \u0026lsquo;Default keyring\u0026rsquo; is locked # Found workaround simply by switching off \u0026ldquo;Automatic Login\u0026rdquo; in Settings \u0026raquo; Users \u0026raquo; Automatic Login.\nWifi keeps switching off # For some reason wifi keeps switching off for me. Disabling power save seems to do the trick (I am using vscode for text editor):\ncode /etc/NetworkManager/conf.d/default-wifi-powersave-on.conf Disable powersave by setting the value to 2 (and reboot):\n[connection] wifi.powersave = 2 Development Issues # Visual Studio Code launches slowly # For some reason installing Visual Studio Code via snap causes it to launch very slowly. I used snap list to figure that out and did sudo snap remove code to uninstall it. Subsequently, I downloaded .deb package from Microsoft\u0026rsquo;s website and installed it. This seems to fix the issue..\n","date":"30 November 2020","externalUrl":null,"permalink":"/post/003-ubuntu-tips/","section":"Posts","summary":"I have started using Ubuntu 20.04 LTS on my Surface Pro 3. Simple story is that I got fed up with the usual Windows issues (Windows update, background processes causing slowdown, etc) and decided to try Linux (I am still using Windows 10 on my desktop PC though).\n","title":"Ubuntu 20.04 LTS Tips","type":"post"},{"content":"git has become industry standard for version control system so if you are software developer, there\u0026rsquo;s no way to escape git. I\u0026rsquo;ve been using git for about a year for now and have developed my own workflow. My core philosophy is to make sure the commits are as linear as possible, this basically means I\u0026rsquo;m using rebase as much as I can. In addition, I use git mostly via command line and only use gui for browsing. This allows me to be as cross platform as possible.\nTable of Contents # New machine setup Workflow recipes Submodule Misc commands New machine setup # Use git config --list to view your config.\nUser setup # git config --global user.name \u0026lt;user_name\u0026gt; git config --global user.email \u0026lt;myemail@domain\u0026gt; Credential setup # I am using Github\u0026rsquo;s Personal Access Token (PAT).\nWindows: git config --global credential.helper manager Diff and merge tool # I am using Beyond Compare (you need to have the pro version for merge) for both diff and merge tool. It works for all platforms I care (Windows, Mac, Linux). For non-Windows platform, make sure you can open Beyond Compare from terminal, type bcompare.\ngit config --global diff.tool bc git config --global difftool.bc.trustExitCode true git config --global merge.tool bc gif config --global mergetool.bc.trustExitCode true For Windows:\ngit config --global difftool.bc.path \u0026#34;c:/Program Files (x86)/Beyond Compare 4/bcomp.exe\u0026#34; git config --global mergetool.bc.path \u0026#34;c:/Program Files (x86)/Beyond Compare 4/bcomp.exe\u0026#34; Workflow Recipes # Sync to latest # My main philosophy is to keep master (or main) branch clean and synced to origin.\nFetch and get info about latest changes\ngit fetch origin Make sure local repo is clean\ngit status git log --oneline -n 10 Proceed with merge if current repo is indeed clean\ngit merge origin/master Whenever you are ready to make a change, create a branch:\ngit checkout -b \u0026lt;branch_name\u0026gt; Generate and Applying Patchfile # Suppose you have been working on branch1 (and has complicated history) and want to start fresh on branch2. You can generate difference between branch1 and master to a patch file (Make sure you\u0026rsquo;ve merged master to branch1) and simply apply this patch file to branch2:\ngit diff master branch1 \u0026gt; ../patchfile git checkout master git checkout -b branch2 git apply ../patchfile (or git apply --reject ../patchfile) Updating last commit message # You can only update the last commit message\ngit commit --amend To allow # in commit message, do\ngit commit --amend --cleanup=whitespace Reviewing pull request # The idea is to create a branch pointing to the full request and then switch to that branch:\ngit fetch origin pull/\u0026lt;pr_number\u0026gt;/head:\u0026lt;local_branch_name\u0026gt; git checkout \u0026lt;local_branch_name\u0026gt; Diff tool # How to diff a specific file with master\ngit difftool master -- \u0026lt;path_to_file\u0026gt; Removing untracked files # If you want to delete untracked files/folder:\ngit clean -fd Stashing # I think of git stash similar to P4 shelve/unshelve. Here\u0026rsquo;s some useful stashing related (self explanatory) commands:\ngit stash git stash list // This will apply the last one git stash apply // Applying a specific stash git stash apply stash@{NUMBER} git stash show stash@{0} Submodule # Some commonly used submodule commands:\n// Listing submodule url git config --file=.gitmodules -l // Updating submodule url git submodule set-url \u0026lt;submodule_name\u0026gt; \u0026lt;url\u0026gt; // Put the submodule in the right state git submodule update --init --recursive \u0026lt;submodule_name\u0026gt; // Update submodule to latest git submodule update --remote --recursive \u0026lt;submodule_name\u0026gt; Misc commands # Fixing detached head # Think of detached HEAD as unnamed branch. The idea is to give this branch a name and manipulate it as you want:\ngit checkout -b \u0026lt;branch_name\u0026gt; # Manipulate as the new branch as you wish Removing (and ignoring) committed file # First, add the file/folder in .gitignore and then to remove from your repository and keep local copy:\ngit rm --cache \u0026lt;path-to-file\u0026gt; Update git to latest version # git update-git-for-windows ","date":"23 November 2020","externalUrl":null,"permalink":"/post/002-git-cheat-sheet/","section":"Posts","summary":"git has become industry standard for version control system so if you are software developer, there’s no way to escape git. I’ve been using git for about a year for now and have developed my own workflow. My core philosophy is to make sure the commits are as linear as possible, this basically means I’m using rebase as much as I can. In addition, I use git mostly via command line and only use gui for browsing. This allows me to be as cross platform as possible.\n","title":"Git cheat sheet","type":"post"},{"content":"Some useful C++ code snippets that I find useful from time to time.\nSimple Profiling # If you want to get statistical confidence, just use google benchmark https://github.com/google/benchmark. Otherwise, simple profiling can be done with chrono:\n#include \u0026lt;chrono\u0026gt; ... auto start = std::chrono::high_resolution_clock::now(); ... auto end = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast\u0026lt;std::chrono::milliseconds\u0026gt;(end - start); std::cout \u0026lt;\u0026lt; \u0026#34;Duration: \u0026#34; \u0026lt;\u0026lt; duration.count() \u0026lt;\u0026lt; \u0026#34;ms\\n\u0026#34;; Transaction Pattern # Sometimes it\u0026rsquo;s useful to have transactional code block where we can undo/commit a block of operations. We can use C++ RAII to do this:\nclass Transaction { bool committed = false; // state public: Transaction(/*some state*/) { ... } ~Transaction() { if (!committed) { /*undo operation*/ } } void Commit() noexcept { committed = true; } } Example usage is parsing where we want to parse a sequence-of-token or not at all\nvoid ParseSequence() { Transaction transaction(tokenizer); if (!tokenizer.Next(TokenType::Comma)) return; // automatically revert any changes if (!tokenizer.Next(TokenType::StringLiteral)) return // automatically revert any changes transaction.Commit(); // all operations succeeded } Get or Create std::map, std::unordered_map # std::unordered_map\u0026lt;Key, Value\u0026gt; map; auto [iter, newEntry] = map.emplace(key, Value{}); if (newEntry) iter-\u0026gt;second = Value(); Loop over map # std::unordered_map\u0026lt;Key, Value\u0026gt; map; for (const auto\u0026amp; [key, value] : map) { ... } String Format # template \u0026lt; typename... Args \u0026gt; inline std::string StringFormat(const char *format, Args... args) { size_t size = std::snprintf(nullptr, 0, format, args...) + 1; // Extra space for \u0026#39;\\0\u0026#39; auto buf = std::make_unique\u0026lt; char[] \u0026gt;(size); std::snprintf(buf.get(), size, format, args...); return std::string(buf.get(), buf.get() + size - 1); // We don\u0026#39;t want the \u0026#39;\\0\u0026#39; inside } Printf tricks # Print the first n characters:\nconst char* pStr = \u0026#34;C Tips and Tricks.\u0026#34;; printf( \u0026#34;%.*s\\n\u0026#34;, 5, pStr); // This will print \u0026#34;C Tips\u0026#34; Print n spaces:\nprintf(\u0026#34;%*s\u0026#34;, 5, \u0026#34;\u0026#34;); // This will print \u0026#34; \u0026#34; Unicode printf-style format specifiers # Complete story: https://devblogs.microsoft.com/oldnewthing/20190830-00/?p=102823\n// Stick to %hs and %ls for compatibility between Windows and C const char* narrowStr = \u0026#34;hello narrow\u0026#34;; const wchar_t* widestr = L\u0026#34;hello wide\u0026#34;; printf(\u0026#34;%hs\u0026#34;, narrowStr); printf(\u0026#34;%ls\u0026#34;, wideStr); Using \u0026lt;charconv\u0026gt; # C++17 has \u0026lt;charconv\u0026gt; that is useful to convert text to/from number.\n#include \u0026lt;charconv\u0026gt; const char* text = \u0026#34;123\u0026#34;; int value; if (std::from_chars(text, text + std::strlen(text), value).ec == std::errc{}) { // succeed } Class member size and offset # struct MyStruct { int field0; char field1; }; sizeof( MyStruct::field0 ) offsetof( MyStruct, field0 ) Transferring ownership # std::unique_ptr\u0026lt;T\u0026gt; ptr; ... using Storage = typename std::aligned_storage\u0026lt;sizeof(NonConstT), alignof(NonConstT)\u0026gt;::type; std::unique_ptr\u0026lt;Storage\u0026gt; stPtr = std::make_unique\u0026lt;Storage\u0026gt;(); // Transfer ownership to correct unique_ptr type ptr.reset( reinterpret_cast\u0026lt;T *\u0026gt;( stPtr.release() ) ); ","date":"30 October 2020","externalUrl":null,"permalink":"/post/001-cpp-useful-code-snippets/","section":"Posts","summary":"Some useful C++ code snippets that I find useful from time to time.\nSimple Profiling # If you want to get statistical confidence, just use google benchmark https://github.com/google/benchmark. Otherwise, simple profiling can be done with chrono:\n","title":"Useful C++ Code Snippets","type":"post"},{"content":"Hello world! This is my brand new blog powered by Hugo, Github and Firebase. I decided to start fresh and delete the old content. I hope things will be much easier going forward and that will encourage me to post more things.\ncout \u0026lt;\u0026lt; \u0026#34;Hello World!\\n\u0026#34;; ","date":"29 October 2020","externalUrl":null,"permalink":"/post/000-hello-world/","section":"Posts","summary":"Hello world! This is my brand new blog powered by Hugo, Github and Firebase. I decided to start fresh and delete the old content. I hope things will be much easier going forward and that will encourage me to post more things.\n","title":"Hello World!","type":"post"},{"content":"There are three different things here.\nFilesystem has mounts metadata last modified time file content Asset can import from file system \u0026ndash; has a cost imported asset has identity (asset uri), has unique content (file content hash \u0026ndash; auto dedup) can have metadata \u0026ndash; knows the asset type asset manager \u0026ndash; doesn\u0026rsquo;t cache, loads all metadata can build composite asset \u0026ndash; (because its asset it has all the above properties) can write to filesystem replication permission Resource \u0026ndash; in memory, used for game static resource \u0026ndash; created procedurally, created from asset (can be reloaded) dynamic resource \u0026ndash; updated ","externalUrl":null,"permalink":"/post/014-content-management-system/","section":"Posts","summary":"There are three different things here.\nFilesystem has mounts metadata last modified time file content Asset can import from file system – has a cost imported asset has identity (asset uri), has unique content (file content hash – auto dedup) can have metadata – knows the asset type asset manager – doesn’t cache, loads all metadata can build composite asset – (because its asset it has all the above properties) can write to filesystem replication permission Resource – in memory, used for game static resource – created procedurally, created from asset (can be reloaded) dynamic resource – updated ","title":"","type":"post"},{"content":"","externalUrl":null,"permalink":"/categories/","section":"Categories","summary":"","title":"Categories","type":"categories"},{"content":"","externalUrl":null,"permalink":"/tags/","section":"Tags","summary":"","title":"Tags","type":"tags"}]