cache optimized rectangle drawing and made sure the methods could be inlined

This commit is contained in:
2026-07-08 13:33:43 -07:00
parent 85cfdaab49
commit 04afd9eee0
9 changed files with 143 additions and 36 deletions
+4 -4
View File
@@ -5,10 +5,10 @@
#include "datatypes.cpp" #include "datatypes.cpp"
#include "helpers.cpp" #include "helpers.cpp"
#include "shaders/shader.cpp" #include "shaders/shader.cpp"
#include "shaders/colorshader.cpp" // #include "shaders/colorshader.cpp"
// #include "shaders/gradientshader.cpp" // #include "shaders/gradientshader.cpp"
#include "shaders/hgradientshader.cpp" // #include "shaders/hgradientshader.cpp"
#include "shaders/vgradientshader.cpp" // #include "shaders/vgradientshader.cpp"
namespace UwU { namespace UwU {
// Buffer object - can be a wayland surface or a virtual buffer // Buffer object - can be a wayland surface or a virtual buffer
@@ -34,7 +34,7 @@ namespace UwU {
uint16_t stride; uint16_t stride;
void drawPixel(Coord2 coord, Color color) { void drawPixel(Coord2 coord, Color color) {
if (color.a == 0) {return;} // if (color.a == 0) {return;}
size_t i = (size_t)coord.y * stride + (size_t)coord.x * 4; size_t i = (size_t)coord.y * stride + (size_t)coord.x * 4;
// uint8_t b0 = pixels[i + 0]; // uint8_t b0 = pixels[i + 0];
// uint8_t g0 = pixels[i + 1]; // uint8_t g0 = pixels[i + 1];
+11
View File
@@ -70,4 +70,15 @@ namespace UwU {
// uint16_t *xxMap; // uint16_t *xxMap;
// }; // };
enum ShaderType {
COLOR,
HGRADIENT,
VGRADIENT
};
enum Direction {
H,
V
};
} }
BIN
View File
Binary file not shown.
+26 -9
View File
@@ -18,10 +18,10 @@
#include "primatives/triangle.cpp" #include "primatives/triangle.cpp"
// #include "draw.cpp" // #include "draw.cpp"
#include "shaders/shader.cpp" #include "shaders/shader.cpp"
#include "shaders/colorshader.cpp" // #include "shaders/colorshader.cpp"
// #include "shaders/gradientshader.cpp" // #include "shaders/gradientshader.cpp"
#include "shaders/hgradientshader.cpp" // #include "shaders/hgradientshader.cpp"
#include "shaders/vgradientshader.cpp" // #include "shaders/vgradientshader.cpp"
// #include "mapper.cpp" // #include "mapper.cpp"
#include <time.h> #include <time.h>
@@ -40,8 +40,9 @@ int main(void) {
uint16_t w = 800, h = 480; uint16_t w = 800, h = 480;
// UwU::GradientShader fuchsia = UwU::GradientShader(UwU::Color(0xb00b69ff), UwU::Color(0xb00b6940)); // UwU::GradientShader fuchsia = UwU::GradientShader(UwU::Color(0xb00b69ff), UwU::Color(0xb00b6940));
// UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff)); // UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff));
UwU::VGradientShader fuchsia = UwU::VGradientShader(69, UwU::Color(0xb00b69ff), 420, UwU::Color(0x00000000)); UwU::Shader fuchsia = UwU::Shader(UwU::Direction::H, 69, 420, UwU::Color(0xb00b69ff), UwU::Color(0x00000000));
int32_t rectScale = (255 * 65536) / 351; UwU::Shader warmup = UwU::Shader(UwU::Color(0xb00b69ff));
// int32_t rectScale = (255 * 65536) / 351;
// rectScale = 41233; //-23806 // rectScale = 41233; //-23806
// UwU::Mapper rectMapper = UwU::Mapper(UwU::Vec2(0, 0), UwU::Vec2(0, 0), UwU::Matrix4(0, 0, 0, 0)); // UwU::Mapper rectMapper = UwU::Mapper(UwU::Vec2(0, 0), UwU::Vec2(0, 0), UwU::Matrix4(0, 0, 0, 0));
UwU::Color red = UwU::Color(0xff000080); UwU::Color red = UwU::Color(0xff000080);
@@ -59,12 +60,28 @@ int main(void) {
// draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red); // draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
struct timespec start, end; struct timespec start, end;
const int WARMUP = 100;
const int BATCH = 1000;
const int SAMPLES = 10;
for (int i = 0; i < WARMUP; i++) {
UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480);
}
long times[SAMPLES];
for (int s = 0; s < SAMPLES; s++) {
clock_gettime(CLOCK_MONOTONIC, &start); clock_gettime(CLOCK_MONOTONIC, &start);
rectangle.draw(surface.buffer, fuchsia); for (int i = 0; i < BATCH; i++) rectangle.draw(surface.buffer, warmup);
UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480);
clock_gettime(CLOCK_MONOTONIC, &end); clock_gettime(CLOCK_MONOTONIC, &end);
long elapsed_ns = (end.tv_sec - start.tv_sec) * 1000000000L + times[s] = (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec);
(end.tv_nsec - start.tv_nsec); }
printf("Elapsed: %ld ns\n", elapsed_ns); long timeAvg = 0;
for (int i = 0; i < SAMPLES; i++) {
timeAvg += times[i];
}
timeAvg /= SAMPLES;
timeAvg /= BATCH;
printf("Elapsed: %ld ns\n", timeAvg);
UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480); UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480);
trapezoid.draw(surface.buffer, red); trapezoid.draw(surface.buffer, red);
+47 -14
View File
@@ -6,10 +6,10 @@
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shaders/shader.cpp" #include "../shaders/shader.cpp"
#include "../shaders/colorshader.cpp" // #include "../shaders/colorshader.cpp"
// #include "../shaders/gradientshader.cpp" // #include "../shaders/gradientshader.cpp"
#include "../shaders/hgradientshader.cpp" // #include "../shaders/hgradientshader.cpp"
#include "../shaders/vgradientshader.cpp" // #include "../shaders/vgradientshader.cpp"
// #include "../mapper.cpp" // #include "../mapper.cpp"
namespace UwU { namespace UwU {
@@ -29,19 +29,52 @@ namespace UwU {
uint16_t y1; uint16_t y1;
// Draw a rectangle // Draw a rectangle
template<typename ShaderType> void draw (Buffer buffer, Shader shader) {
void draw (Buffer buffer, ShaderType& shader) { if (shader.shaderType == COLOR) {
// printf("color: %u, %u, %u, %u", shader.color().r, shader.color().g, shader.color().b, shader.color().a); if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; Color c = shader.c0;
// Coord2 uv = Coord2(); uint8_t r = c.r;
for (uint16_t y = y0; y < y1; y++) { uint8_t g = c.g;
for (uint16_t x = x0; x < x1; x++) { uint8_t b = c.b;
// uv = mapper.map(Coord2(x, y)); size_t stride = buffer.stride;
Color c = shader.color(x, y); size_t i = (size_t)y0 * buffer.stride + (size_t)x0 * 4;
buffer.drawPixel(Coord2(x, y), c); for (uint16_t y = y0; y < y1; y++) {
for (uint16_t x = x0; x < x1; x++) {
// buffer.drawPixel(Coord2(x, y), shader.c0);
buffer.pixels[i + 0] = b;
buffer.pixels[i + 1] = g;
buffer.pixels[i + 2] = r;
buffer.pixels[i + 3] = 255;
i += 4;
}
i += stride - ((x1 - x0) * 4);
} }
return;
} else if (shader.shaderType == HGRADIENT) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
Color cols[x1 - x0];
for (uint16_t x = x0; x < x1; x++) {
cols[x - x0] = ShaderMethods::gradient(shader.u0, shader.scale, shader.c0, shader.c1, x);
}
for (uint16_t y = y0; y < y1; y++) {
for (uint16_t x = x0; x < x1; x++) {
buffer.drawPixel(Coord2(x, y), cols[x - x0]);
}
}
return;
} else if (shader.shaderType == VGRADIENT) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
for (uint16_t y = y0; y < y1; y++) {
Color c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y);
for (uint16_t x = x0; x < x1; x++) {
buffer.drawPixel(Coord2(x, y), c);
}
}
return;
} }
return;
} }
}; };
} }
+5 -1
View File
@@ -9,11 +9,15 @@
namespace UwU { namespace UwU {
class ColorShader { class ColorShader {
private:
public: public:
ColorShader(Color color) { ColorShader(Color color) {
this->shaderType = COLORSHADER;
this->shaderColor = color; this->shaderColor = color;
} }
ShaderType shaderType;
Color shaderColor; Color shaderColor;
Color color(uint16_t u, uint16_t v) { Color color(uint16_t u, uint16_t v) {
+3 -1
View File
@@ -11,17 +11,19 @@ namespace UwU {
class HGradientShader{ class HGradientShader{
public: public:
HGradientShader(uint16_t x0, Color color0, uint16_t x1, Color color1) { HGradientShader(uint16_t x0, Color color0, uint16_t x1, Color color1) {
this->shaderType = HGRADIENTSHADER;
this->x0 = x0; this->x0 = x0;
this->shaderColor0 = color0; this->shaderColor0 = color0;
this->scale = 0xffffffff / (1 + (uint32_t)x1 - (uint32_t)x0); this->scale = 0xffffffff / (1 + (uint32_t)x1 - (uint32_t)x0);
this->shaderColor1 = color1; this->shaderColor1 = color1;
} }
ShaderType shaderType;
uint16_t x0; uint16_t x0;
Color shaderColor0; Color shaderColor0;
uint32_t scale; uint32_t scale;
Color shaderColor1; Color shaderColor1;
Color color(uint16_t u, uint16_t v) { Color hGradient(uint16_t u, uint16_t v) {
(void)v; (void)v;
uint32_t t = (((uint32_t)u - (uint32_t)x0) * scale) / 0x01000000; uint32_t t = (((uint32_t)u - (uint32_t)x0) * scale) / 0x01000000;
// if (v == 42) { // if (v == 42) {
+43 -5
View File
@@ -10,12 +10,50 @@ namespace UwU {
// shader prototype // shader prototype
class Shader { class Shader {
public: public:
Shader() {} //COLOR Shader
Shader(Color color) {
this->shaderType = COLOR;
this->c0 = color;
}
Color color(uint16_t u, uint16_t v) { //HGRADIENT or VGRADIENT shader
(void)u; Shader(Direction direction, uint16_t uv0, uint16_t uv1, Color c0, Color c1) {
(void)v; this->c0 = c0;
return Color(); this->c1 = c1;
switch (direction) {
case H:
shaderType = HGRADIENT;
this->u0 = uv0;
this->scale = 0xffffffff / (1 + (uint32_t)uv1 - (uint32_t)uv0);
break;
case V:
shaderType = VGRADIENT;
this->v0 = uv0;
this->scale = 0xffffffff / (1 + (uint32_t)uv1 - (uint32_t)uv0);
break;
}
}
// BUFFER shader
// Shader(uint16_t u, uint16_t v, Buffer buffer) {
// this->shaderType = BUFFER;
// this->u0 = u;
// this->v0 = v;
// }
ShaderType shaderType;
uint16_t u0;
uint16_t v0;
Color c0;
Color c1;
uint32_t scale;
};
class ShaderMethods {
public:
static Color gradient(uint16_t uv0, uint32_t scale, Color c0, Color c1, uint16_t uv) {
uint32_t t = (((uint32_t)uv - (uint32_t)uv0) * scale) / 0x01000000;
return Helpers::lerpColor(c0, c1, (uint8_t)t);
} }
}; };
} }
+3 -1
View File
@@ -11,17 +11,19 @@ namespace UwU {
class VGradientShader { class VGradientShader {
public: public:
VGradientShader(uint16_t y0, Color color0, uint16_t y1, Color color1) { VGradientShader(uint16_t y0, Color color0, uint16_t y1, Color color1) {
this->shaderType = VGRADIENTSHADER;
this->y0 = y0; this->y0 = y0;
this->shaderColor0 = color0; this->shaderColor0 = color0;
this->scale = 0xffffffff / (1 + (uint32_t)y1 - (uint32_t)y0); this->scale = 0xffffffff / (1 + (uint32_t)y1 - (uint32_t)y0);
this->shaderColor1 = color1; this->shaderColor1 = color1;
} }
ShaderType shaderType;
uint16_t y0; uint16_t y0;
Color shaderColor0; Color shaderColor0;
uint32_t scale; uint32_t scale;
Color shaderColor1; Color shaderColor1;
Color color(uint16_t u, uint16_t v) { Color vGradient(uint16_t u, uint16_t v) {
(void)u; (void)u;
uint32_t t = (((uint32_t)v - (uint32_t)y0) * scale) / 0x01000000; uint32_t t = (((uint32_t)v - (uint32_t)y0) * scale) / 0x01000000;
// if (v == 42) { // if (v == 42) {