Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sim: fb updates #71

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions simulator/src/compositor.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { WIDTH, HEIGHT } from "./constants";
import * as constants from "./constants";
import * as GL from "./webgl-constants";
import type { Framebuffer } from "./framebuffer";

const PALETTE_SIZE = 4;

export class WebGLCompositor {
flipped = new Uint16Array(WIDTH * HEIGHT);

constructor (public gl: WebGLRenderingContext) {
const canvas = gl.canvas;
canvas.addEventListener("webglcontextlost", event => {
Expand Down Expand Up @@ -110,8 +109,18 @@ export class WebGLCompositor {
const gl = this.gl;
const bytes = framebuffer.bytes;

// Rotate and swap byte order
// Would not be required if WebGL supported `UNSIGNED_SHORT_5_6_5_REV`
for (let x = 0; x < WIDTH; x++) {
for (let y = 0; y < HEIGHT; y++) {
const fi = x + y * WIDTH;
const bi = x * HEIGHT + y;
this.flipped[fi] = ((bytes[bi] & 0xff) << 8) | (bytes[bi] >> 8);
}
}

// Upload framebuffer
gl.texImage2D(GL.TEXTURE_2D, 0, GL.RGB565, WIDTH, HEIGHT, 0, GL.RGB, GL.UNSIGNED_SHORT_5_6_5, bytes);
gl.texImage2D(GL.TEXTURE_2D, 0, GL.RGB565, WIDTH, HEIGHT, 0, GL.RGB, GL.UNSIGNED_SHORT_5_6_5, this.flipped);

// Draw the fullscreen quad
gl.drawArrays(GL.TRIANGLES, 0, 6);
Expand Down
3 changes: 2 additions & 1 deletion simulator/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const ADDR_CONTROLS = 0x04;
export const ADDR_LIGHT_LEVEL = 0x06;
export const ADDR_NEOPIXELS = 0x08;
export const ADDR_RED_LED = 0x1c;
export const ADDR_FRAMEBUFFER = 0x1e;
export const ADDR_BATTERY_LEVEL = 0x1e;
export const ADDR_FRAMEBUFFER = 0x20;

export const CONTROLS_START = 1;
export const CONTROLS_SELECT = 2;
Expand Down
44 changes: 17 additions & 27 deletions simulator/src/framebuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export class Framebuffer {
}

fillScreen (color: number): void {
this.bytes.fill(color);
this.bytes.fill(((color & 0xff) << 8) | (color >> 8));
}

drawPoint (color: number, x: number, y: number) {
this.bytes[WIDTH * y + x] = color;
this.bytes[x * HEIGHT + y] = ((color & 0xff) << 8) | (color >> 8);
}

drawPointUnclipped (color: number, x: number, y: number) {
Expand All @@ -27,27 +27,17 @@ export class Framebuffer {
}
}

drawHLineFast(color: number, startX: number, y: number, endX: number) {
const yOff = WIDTH * y;
this.bytes.fill(color, yOff + startX, yOff + endX);
}

drawHLineUnclipped(color: number, startX: number, y: number, endX: number) {
if (y >= 0 && y < HEIGHT) {
if (startX < 0) {
startX = 0;
}
if (endX > WIDTH) {
endX = WIDTH;
}
if (startX < endX) {
this.drawHLineFast(color, startX, y, endX);
}
drawHLine(color: number, x: number, y: number, len: number) {
if (x + len <= 0 || y < 0 || y >= HEIGHT) {
return;
}
}

drawHLine(color: number, x: number, y: number, len: number) {
this.drawHLineUnclipped(color, x, y, x + len);
const startX = Math.max(0, x);
const endX = Math.min(WIDTH, x + len);

for (let xx = startX; xx < endX; xx++) {
this.drawPointUnclipped(color, xx, y);
}
}

drawVLine(color: number, x: number, y: number, len: number) {
Expand Down Expand Up @@ -77,7 +67,7 @@ export class Framebuffer {

if (fillColor !== OPTIONAL_COLOR_NONE) {
for (let yy = startY; yy < endY; ++yy) {
this.drawHLineFast(fillColor, startX, yy, endX);
this.drawHLine(fillColor, startX, yy, endX - startX);
}
}

Expand All @@ -98,12 +88,12 @@ export class Framebuffer {

// Top edge
if (y >= 0 && y < HEIGHT) {
this.drawHLineFast(strokeColor, startX, y, endX);
this.drawHLine(strokeColor, startX, y, endX - startX);
}

// Bottom edge
if (endYUnclamped > 0 && endYUnclamped <= HEIGHT) {
this.drawHLineFast(strokeColor, startX, endYUnclamped - 1, endX);
this.drawHLine(strokeColor, startX, endYUnclamped - 1, endX - startX);
}
}
}
Expand All @@ -125,7 +115,7 @@ export class Framebuffer {
if (strokeColor === OPTIONAL_COLOR_NONE && fillColor === OPTIONAL_COLOR_NONE) {
return;
}

let a = width - 1;
const b = height - 1;
let b1 = b % 2; // Compensates for precision loss when dividing
Expand Down Expand Up @@ -161,8 +151,8 @@ export class Framebuffer {
const len = east - start;

if (fillColor !== OPTIONAL_COLOR_NONE && len > 0) { // Only draw fill if the length from west to east is not 0
this.drawHLineUnclipped(fillColor, start, north, east); /* I and III. Quadrant */
this.drawHLineUnclipped(fillColor, start, south, east); /* II and IV. Quadrant */
this.drawHLine(fillColor, start, north, east - start); /* I and III. Quadrant */
this.drawHLine(fillColor, start, south, east - start); /* II and IV. Quadrant */
}

const err2 = 2 * err;
Expand Down
Loading