Skip to content

Instantly share code, notes, and snippets.

@suarezvictor
Created March 8, 2022 21:33
Show Gist options
  • Save suarezvictor/60b9ff571b524ac89bb6db84cf06a242 to your computer and use it in GitHub Desktop.
Save suarezvictor/60b9ff571b524ac89bb6db84cf06a242 to your computer and use it in GitHub Desktop.
// PipelineC and LiteX integration demo: basic graphics with dither
// Copyright (c) 2022 Victor Suarez Rovere <suarezvictor@gmail.com>
#include "pipelinec_compat.h"
extern int FRAME_WIDTH;
extern int FRAME_HEIGHT;
struct pixel_t { uint8_t a, b, g, r; };
#define DITHER
inline uint16_t gray(uint8_t v) { return v ^ (v<<1); }
inline uint9_t dither(uint8_t x, uint8_t y, uint9_t v)
{
uint8_t pattern = gray((x&7)^gray(y&7)) & 0xF;
return (pattern + v) & 0x1F0;
}
pixel_t render_pixel(uint16_t i, uint16_t j)
{
int16_t x = i << 1; x = x - (FRAME_WIDTH + 1);
int16_t y = j << 1; y = (FRAME_HEIGHT + 1) - y;
uint9_t r = 0;
uint9_t g = 0;
uint9_t b = 0;
if(x*x*4 + y*y*3 < FRAME_HEIGHT*FRAME_HEIGHT*4*4/(3*3))
g = (i-j) & 0xFF;
else
b = (i+j) & 0xFF;
#ifdef DITHER
r = dither(i, j, r);
g = dither(i, j, g);
b = dither(i, j, b);
#endif
pixel_t pix = {0, 0, 0, 0};
if(r >= 256) pix.r = 255; else pix.r = r;
if(g >= 256) pix.g = 255; else pix.g = g;
if(b >= 256) pix.b = 255; else pix.b = b;
return pix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment