Master the fundamental concepts of gpu architecture through this focused micro-challenge.
Screens are 2D, but memory is 1D. A framebuffer stores pixels row after row in row-major order. The pixel at coordinates (x, y) on a surface of width W lives at linear index y * W + x. With 4 bytes per pixel in RGBA8 format, the first byte of that pixel sits at byte offset (y * W + x) * 4.
For example, on an 800x600 display, the pixel at (3, 2) has index 2 * 800 + 3 = 1603 and byte offset 6412.
Every rasterizer plot, every texture sample, every CUDA image kernel, and every Linux framebuffer console driver computes this same expression. GPU memory coalescing, covered later in this subtrack, is entirely about warps of threads reading consecutive values produced by this formula. Column-major layouts exist in linear algebra libraries, but framebuffers almost always use row-major indexing because scanlines are natural horizontal strips.
index = y * width + xbyte = index * 40 <= x < width and 0 <= y < heightThis task asks you to read x, y, and width from stdin and print the linear index and RGBA8 byte offset. You will use this arithmetic in every rasterizer task that follows, from Bresenham line drawing through z-buffering. Getting the multiply order wrong is one of the most common off-by-one bugs when porting rendering code between APIs.
A framebuffer is a 2D image stored as one flat array, row after row (row-major). Every pixel write a GPU or rasterizer performs starts with the same calculation: turn (x, y) into an array index, then into a byte offset. Compute both for an RGBA8 framebuffer, which uses 4 bytes per pixel.
Four integers: x y width height.
y × width + x. Byte offset: index × 4.0 ≤ x < width and 0 ≤ y < height.Two lines, index N and byte M. For a pixel outside the framebuffer, print (x, y) is outside the WxH framebuffer instead. For malformed input or a non-positive size, print invalid input.
Input:
cLoading…
Output:
cLoading…
long: a 4K framebuffer is 33 million bytes, and bigger ones overflow a 32-bit int quickly.Hidden tests cover the last pixel of a 4K frame and a zero-sized framebuffer.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works