diff --git a/buffer.cpp b/buffer.cpp index c873c55..a0f470f 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -5,10 +5,10 @@ #include "datatypes.cpp" #include "helpers.cpp" #include "shaders/shader.cpp" -#include "shaders/colorshader.cpp" +// #include "shaders/colorshader.cpp" // #include "shaders/gradientshader.cpp" -#include "shaders/hgradientshader.cpp" -#include "shaders/vgradientshader.cpp" +// #include "shaders/hgradientshader.cpp" +// #include "shaders/vgradientshader.cpp" namespace UwU { // Buffer object - can be a wayland surface or a virtual buffer @@ -34,7 +34,7 @@ namespace UwU { uint16_t stride; 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; // uint8_t b0 = pixels[i + 0]; // uint8_t g0 = pixels[i + 1]; diff --git a/datatypes.cpp b/datatypes.cpp index ecbd130..47659d8 100644 --- a/datatypes.cpp +++ b/datatypes.cpp @@ -70,4 +70,15 @@ namespace UwU { // uint16_t *xxMap; // }; + + enum ShaderType { + COLOR, + HGRADIENT, + VGRADIENT + }; + + enum Direction { + H, + V + }; } \ No newline at end of file diff --git a/example b/example index 89f3eaf..71d098b 100755 Binary files a/example and b/example differ diff --git a/example.cpp b/example.cpp index 4de2243..d4a2ac4 100644 --- a/example.cpp +++ b/example.cpp @@ -18,10 +18,10 @@ #include "primatives/triangle.cpp" // #include "draw.cpp" #include "shaders/shader.cpp" -#include "shaders/colorshader.cpp" +// #include "shaders/colorshader.cpp" // #include "shaders/gradientshader.cpp" -#include "shaders/hgradientshader.cpp" -#include "shaders/vgradientshader.cpp" +// #include "shaders/hgradientshader.cpp" +// #include "shaders/vgradientshader.cpp" // #include "mapper.cpp" #include @@ -40,8 +40,9 @@ int main(void) { uint16_t w = 800, h = 480; // UwU::GradientShader fuchsia = UwU::GradientShader(UwU::Color(0xb00b69ff), UwU::Color(0xb00b6940)); // UwU::ColorShader fuchsia = UwU::ColorShader(UwU::Color(0xb00b69ff)); - UwU::VGradientShader fuchsia = UwU::VGradientShader(69, UwU::Color(0xb00b69ff), 420, UwU::Color(0x00000000)); - int32_t rectScale = (255 * 65536) / 351; + UwU::Shader fuchsia = UwU::Shader(UwU::Direction::H, 69, 420, UwU::Color(0xb00b69ff), UwU::Color(0x00000000)); + UwU::Shader warmup = UwU::Shader(UwU::Color(0xb00b69ff)); + // int32_t rectScale = (255 * 65536) / 351; // rectScale = 41233; //-23806 // 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); @@ -57,14 +58,30 @@ int main(void) { // draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red); - + 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); - 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); - long elapsed_ns = (end.tv_sec - start.tv_sec) * 1000000000L + - (end.tv_nsec - start.tv_nsec); - printf("Elapsed: %ld ns\n", elapsed_ns); + times[s] = (end.tv_sec - start.tv_sec) * 1000000000L + (end.tv_nsec - start.tv_nsec); +} +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); trapezoid.draw(surface.buffer, red); diff --git a/primatives/rectangle.cpp b/primatives/rectangle.cpp index 5a3daf5..01f782d 100644 --- a/primatives/rectangle.cpp +++ b/primatives/rectangle.cpp @@ -6,10 +6,10 @@ #include "../helpers.cpp" #include "../buffer.cpp" #include "../shaders/shader.cpp" -#include "../shaders/colorshader.cpp" +// #include "../shaders/colorshader.cpp" // #include "../shaders/gradientshader.cpp" -#include "../shaders/hgradientshader.cpp" -#include "../shaders/vgradientshader.cpp" +// #include "../shaders/hgradientshader.cpp" +// #include "../shaders/vgradientshader.cpp" // #include "../mapper.cpp" namespace UwU { @@ -29,19 +29,52 @@ namespace UwU { uint16_t y1; // Draw a rectangle - template - void draw (Buffer buffer, ShaderType& shader) { - // 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;}; - // Coord2 uv = Coord2(); - for (uint16_t y = y0; y < y1; y++) { - for (uint16_t x = x0; x < x1; x++) { - // uv = mapper.map(Coord2(x, y)); - Color c = shader.color(x, y); - buffer.drawPixel(Coord2(x, y), c); + void draw (Buffer buffer, Shader shader) { + if (shader.shaderType == COLOR) { + if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;}; + Color c = shader.c0; + uint8_t r = c.r; + uint8_t g = c.g; + uint8_t b = c.b; + size_t stride = buffer.stride; + size_t i = (size_t)y0 * buffer.stride + (size_t)x0 * 4; + 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; + } }; } \ No newline at end of file diff --git a/shaders/colorshader.cpp b/shaders/colorshader.cpp index eef2bde..47ef643 100644 --- a/shaders/colorshader.cpp +++ b/shaders/colorshader.cpp @@ -9,11 +9,15 @@ namespace UwU { class ColorShader { + private: + + public: ColorShader(Color color) { + this->shaderType = COLORSHADER; this->shaderColor = color; } - + ShaderType shaderType; Color shaderColor; Color color(uint16_t u, uint16_t v) { diff --git a/shaders/hgradientshader.cpp b/shaders/hgradientshader.cpp index 85e15ab..dca2094 100644 --- a/shaders/hgradientshader.cpp +++ b/shaders/hgradientshader.cpp @@ -11,17 +11,19 @@ namespace UwU { class HGradientShader{ public: HGradientShader(uint16_t x0, Color color0, uint16_t x1, Color color1) { + this->shaderType = HGRADIENTSHADER; this->x0 = x0; this->shaderColor0 = color0; this->scale = 0xffffffff / (1 + (uint32_t)x1 - (uint32_t)x0); this->shaderColor1 = color1; } + ShaderType shaderType; uint16_t x0; Color shaderColor0; uint32_t scale; Color shaderColor1; - Color color(uint16_t u, uint16_t v) { + Color hGradient(uint16_t u, uint16_t v) { (void)v; uint32_t t = (((uint32_t)u - (uint32_t)x0) * scale) / 0x01000000; // if (v == 42) { diff --git a/shaders/shader.cpp b/shaders/shader.cpp index dabab4a..aa61981 100644 --- a/shaders/shader.cpp +++ b/shaders/shader.cpp @@ -10,12 +10,50 @@ namespace UwU { // shader prototype class Shader { public: - Shader() {} + //COLOR Shader + Shader(Color color) { + this->shaderType = COLOR; + this->c0 = color; + } - Color color(uint16_t u, uint16_t v) { - (void)u; - (void)v; - return Color(); + //HGRADIENT or VGRADIENT shader + Shader(Direction direction, uint16_t uv0, uint16_t uv1, Color c0, Color c1) { + this->c0 = c0; + 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); } }; } \ No newline at end of file diff --git a/shaders/vgradientshader.cpp b/shaders/vgradientshader.cpp index 04553de..520cc86 100644 --- a/shaders/vgradientshader.cpp +++ b/shaders/vgradientshader.cpp @@ -11,17 +11,19 @@ namespace UwU { class VGradientShader { public: VGradientShader(uint16_t y0, Color color0, uint16_t y1, Color color1) { + this->shaderType = VGRADIENTSHADER; this->y0 = y0; this->shaderColor0 = color0; this->scale = 0xffffffff / (1 + (uint32_t)y1 - (uint32_t)y0); this->shaderColor1 = color1; } + ShaderType shaderType; uint16_t y0; Color shaderColor0; uint32_t scale; Color shaderColor1; - Color color(uint16_t u, uint16_t v) { + Color vGradient(uint16_t u, uint16_t v) { (void)u; uint32_t t = (((uint32_t)v - (uint32_t)y0) * scale) / 0x01000000; // if (v == 42) {