created buffer shader and added shader logic to rectangle

This commit is contained in:
2026-07-08 16:50:17 -07:00
parent 43c893984f
commit 89afdfce9b
9 changed files with 73 additions and 50 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
#include "datatypes.cpp" #include "datatypes.cpp"
#include "helpers.cpp" #include "helpers.cpp"
#include "shader.cpp" // #include "shader.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 -33
View File
@@ -26,41 +26,41 @@ namespace UwU {
}; };
// Coord2 datatype for xy coordinates, from top-left of surface // Coord2 datatype for xy coordinates, from top-left of surface
struct Coord2 { // struct Coord2 {
Coord2(uint16_t x, uint16_t y) { // Coord2(uint16_t x, uint16_t y) {
this->x = x; // this->x = x;
this->y = y; // this->y = y;
} // }
Coord2() {} // Coord2() {}
uint16_t x; // uint16_t x;
uint16_t y; // uint16_t y;
}; // };
// Vec2 datatype for signed x, y coordinates // Vec2 datatype for signed x, y coordinates
struct Vec2 { // struct Vec2 {
Vec2(int16_t x, int16_t y) { // Vec2(int16_t x, int16_t y) {
this->x = x; // this->x = x;
this->y = y; // this->y = y;
} // }
Vec2() {} // Vec2() {}
int16_t x; // int16_t x;
int16_t y; // int16_t y;
}; // };
// 2x2 matrix, Q15.16 signed fixed point // // 2x2 matrix, Q15.16 signed fixed point
struct Matrix4 { // struct Matrix4 {
Matrix4(int32_t xx, int32_t xy, int32_t yx, int32_t yy) { // Matrix4(int32_t xx, int32_t xy, int32_t yx, int32_t yy) {
this->xx = xx; // this->xx = xx;
this->xy = xy; // this->xy = xy;
this->yx = yx; // this->yx = yx;
this->yy = yy; // this->yy = yy;
} // }
Matrix4() {} // Matrix4() {}
int32_t xx; // x' = x*xx + y*xy // int32_t xx; // x' = x*xx + y*xy
int32_t xy; // int32_t xy;
int32_t yx; // y' = x*yx + y*yy // int32_t yx; // y' = x*yx + y*yy
int32_t yy; // int32_t yy;
}; // };
// class Mapper { // class Mapper {
// public: // public:
@@ -74,7 +74,8 @@ namespace UwU {
enum ShaderType { enum ShaderType {
COLOR, COLOR,
HGRADIENT, HGRADIENT,
VGRADIENT VGRADIENT,
BUFFER
}; };
enum Direction { enum Direction {
BIN
View File
Binary file not shown.
+6 -4
View File
@@ -32,7 +32,7 @@ static void onKey(void *user, uint32_t keycode, uint32_t state) {
int main(void) { int main(void) {
uint16_t w = 800, h = 480; uint16_t w = 800, h = 480;
UwU::Shader fuchsia = UwU::Shader(UwU::Direction::H, 360, 600, UwU::Color(0xb00b69ff), UwU::Color(0x00000000)); UwU::Shader fuchsia = UwU::Shader(UwU::Direction::V, 100, 300, UwU::Color(0xb00b69ff), UwU::Color(0x00000000));
UwU::Shader red = UwU::Shader(UwU::Direction::V, 69, 420, UwU::Color(0xff0000ff), UwU::Color(0x00000000)); UwU::Shader red = UwU::Shader(UwU::Direction::V, 69, 420, UwU::Color(0xff0000ff), UwU::Color(0x00000000));
UwU::Shader warmup = UwU::Shader(UwU::Color(0xb00b69ff)); UwU::Shader warmup = UwU::Shader(UwU::Color(0xb00b69ff));
UwU::Color green = UwU::Color(0x00ff00ff); UwU::Color green = UwU::Color(0x00ff00ff);
@@ -40,14 +40,17 @@ int main(void) {
// UwU::Color alpha = UwU::Color(0x00000000); // UwU::Color alpha = UwU::Color(0x00000000);
UwU::Surface surface = UwU::Surface(w, h, onKey); UwU::Surface surface = UwU::Surface(w, h, onKey);
// UwU::Buffer buffer = UwU::Buffer(surface.getWidth(), surface.getHeight()); UwU::Buffer buffer = UwU::Buffer(800, 480);
UwU::Shader triangleBuffer = UwU::Shader(69, 69, buffer);
UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420); UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420);
UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480); UwU::Trapezoid trapezoid = UwU::Trapezoid(300, 400, 360, 380, 600, 480);
UwU::Triangle triangle = UwU::Triangle(280, 100, 240, 290, 320, 300); UwU::Triangle triangle = UwU::Triangle(280, 100, 240, 290, 320, 300);
UwU::Point point = UwU::Point(387, 43); UwU::Point point = UwU::Point(387, 43);
rectangle.draw(surface.buffer, red);
triangle.draw(buffer, fuchsia);
rectangle.draw(surface.buffer, triangleBuffer);
// Benchmarking // Benchmarking
struct timespec start, end; struct timespec start, end;
@@ -75,7 +78,6 @@ int main(void) {
timeAvg /= BATCH; timeAvg /= BATCH;
printf("Elapsed: %ld ns\n", timeAvg); printf("Elapsed: %ld ns\n", timeAvg);
triangle.draw(surface.buffer, fuchsia);
point.draw(surface.buffer, green); point.draw(surface.buffer, green);
+1
View File
@@ -5,6 +5,7 @@
#include "../datatypes.cpp" #include "../datatypes.cpp"
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shader.cpp"
namespace UwU { namespace UwU {
// line // line
+1
View File
@@ -5,6 +5,7 @@
#include "../datatypes.cpp" #include "../datatypes.cpp"
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shader.cpp"
namespace UwU { namespace UwU {
// point // point
+10 -4
View File
@@ -25,8 +25,8 @@ namespace UwU {
// Draw a rectangle // Draw a rectangle
void draw (Buffer buffer, Shader shader) { void draw (Buffer buffer, Shader shader) {
if (shader.shaderType == COLOR) {
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;};
if (shader.shaderType == COLOR) {
for (uint16_t y = y0; y < y1; y++) { for (uint16_t y = y0; y < y1; y++) {
for (uint16_t x = x0; x < x1; x++) { for (uint16_t x = x0; x < x1; x++) {
buffer.drawPixel(x, y, shader.c0); buffer.drawPixel(x, y, shader.c0);
@@ -35,7 +35,6 @@ namespace UwU {
return; return;
} else if (shader.shaderType == HGRADIENT) { } else if (shader.shaderType == HGRADIENT) {
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
// Precompute gradient LUT // Precompute gradient LUT
Color cols[x1 - x0]; Color cols[x1 - x0];
@@ -51,7 +50,6 @@ namespace UwU {
return; return;
} else if (shader.shaderType == VGRADIENT) { } 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++) { for (uint16_t y = y0; y < y1; y++) {
Color c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y); Color c = ShaderMethods::gradient(shader.v0, shader.scale, shader.c0, shader.c1, y);
for (uint16_t x = x0; x < x1; x++) { for (uint16_t x = x0; x < x1; x++) {
@@ -59,8 +57,16 @@ namespace UwU {
} }
} }
return; return;
}
} else if (shader.shaderType == BUFFER) {
for (uint16_t y = y0; y < y1; y++) {
for (uint16_t x = x0; x < x1; x++) {
buffer.drawPixel(x, y, ShaderMethods::buffer(shader.u0, shader.v0, shader.buffer, x, y));
}
}
return;
}
return;
} }
}; };
} }
+1
View File
@@ -6,6 +6,7 @@
#include "../datatypes.cpp" #include "../datatypes.cpp"
#include "../helpers.cpp" #include "../helpers.cpp"
#include "../buffer.cpp" #include "../buffer.cpp"
#include "../shader.cpp"
namespace UwU { namespace UwU {
class Triangle { class Triangle {
+18 -7
View File
@@ -35,11 +35,12 @@ namespace UwU {
} }
// BUFFER shader // BUFFER shader
// Shader(uint16_t u, uint16_t v, Buffer buffer) { Shader(uint16_t u, uint16_t v, Buffer buffer) {
// this->shaderType = BUFFER; this->shaderType = BUFFER;
// this->u0 = u; this->u0 = u;
// this->v0 = v; this->v0 = v;
// } this->buffer = buffer;
}
ShaderType shaderType; ShaderType shaderType;
uint16_t u0; uint16_t u0;
@@ -47,13 +48,23 @@ namespace UwU {
Color c0; Color c0;
Color c1; Color c1;
uint32_t scale; uint32_t scale;
Buffer buffer;
}; };
class ShaderMethods { class ShaderMethods {
public: public:
static Color gradient(uint16_t uv0, uint32_t scale, Color c0, Color c1, uint16_t uv) { static Color gradient(uint16_t uv0, uint32_t scale, Color c0, Color c1, uint16_t xy) {
uint32_t t = (((uint32_t)uv - (uint32_t)uv0) * scale) / 0x01000000; uint32_t t = (((uint32_t)xy - (uint32_t)uv0) * scale) / 0x01000000;
return Helpers::lerpColor(c0, c1, (uint8_t)t); return Helpers::lerpColor(c0, c1, (uint8_t)t);
} }
static Color buffer(uint16_t u0, uint16_t v0, Buffer buffer, uint16_t x, uint16_t y) {
size_t i = ((size_t)y - (size_t)v0) * buffer.stride + ((size_t)x - (size_t)u0) * 4;
uint8_t b = buffer.pixels[i + 0];
uint8_t g = buffer.pixels[i + 1];
uint8_t r = buffer.pixels[i + 2];
uint8_t a = buffer.pixels[i + 3];
return Color(r, g, b, a);
}
}; };
} }