diff --git a/buffer.cpp b/buffer.cpp index a0f470f..d80d15e 100644 --- a/buffer.cpp +++ b/buffer.cpp @@ -4,11 +4,7 @@ #include "datatypes.cpp" #include "helpers.cpp" -#include "shaders/shader.cpp" -// #include "shaders/colorshader.cpp" -// #include "shaders/gradientshader.cpp" -// #include "shaders/hgradientshader.cpp" -// #include "shaders/vgradientshader.cpp" +#include "shader.cpp" namespace UwU { // Buffer object - can be a wayland surface or a virtual buffer diff --git a/example b/example index 71d098b..55e4408 100755 Binary files a/example and b/example differ diff --git a/example.cpp b/example.cpp index d4a2ac4..54e9d52 100644 --- a/example.cpp +++ b/example.cpp @@ -16,13 +16,7 @@ #include "primatives/rectangle.cpp" #include "primatives/trapezoid.cpp" #include "primatives/triangle.cpp" -// #include "draw.cpp" -#include "shaders/shader.cpp" -// #include "shaders/colorshader.cpp" -// #include "shaders/gradientshader.cpp" -// #include "shaders/hgradientshader.cpp" -// #include "shaders/vgradientshader.cpp" -// #include "mapper.cpp" +#include "shader.cpp" #include @@ -38,13 +32,8 @@ static void onKey(void *user, uint32_t keycode, uint32_t state) { 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::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); UwU::Color green = UwU::Color(0x00ff00ff); UwU::Color blue = UwU::Color(0x0000ff80); @@ -54,34 +43,33 @@ int main(void) { // UwU::Buffer buffer = UwU::Buffer(surface.getWidth(), surface.getHeight()); UwU::Rectangle rectangle = UwU::Rectangle(69, 69, 420, 420); - // draw.rainbow(); - - - // draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red); + + // Benchmarking struct timespec start, end; -const int WARMUP = 100; -const int BATCH = 1000; -const int SAMPLES = 10; + 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); - 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); - 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); + for (int i = 0; i < WARMUP; i++) { + rectangle.draw(surface.buffer, fuchsia); + } + long times[SAMPLES]; + for (int s = 0; s < SAMPLES; s++) { + clock_gettime(CLOCK_MONOTONIC, &start); + for (int i = 0; i < BATCH; i++) { + rectangle.draw(surface.buffer, fuchsia); + } + clock_gettime(CLOCK_MONOTONIC, &end); + 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 01f782d..f80bab9 100644 --- a/primatives/rectangle.cpp +++ b/primatives/rectangle.cpp @@ -5,12 +5,7 @@ #include "../datatypes.cpp" #include "../helpers.cpp" #include "../buffer.cpp" -#include "../shaders/shader.cpp" -// #include "../shaders/colorshader.cpp" -// #include "../shaders/gradientshader.cpp" -// #include "../shaders/hgradientshader.cpp" -// #include "../shaders/vgradientshader.cpp" -// #include "../mapper.cpp" +#include "../shader.cpp" namespace UwU { // rectangle @@ -32,22 +27,10 @@ namespace UwU { 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; + buffer.drawPixel(Coord2(x, y), shader.c0); } - i += stride - ((x1 - x0) * 4); } return; diff --git a/shaders/shader.cpp b/shader.cpp similarity index 94% rename from shaders/shader.cpp rename to shader.cpp index aa61981..adb2bf1 100644 --- a/shaders/shader.cpp +++ b/shader.cpp @@ -2,9 +2,9 @@ #include -#include "../datatypes.cpp" -#include "../helpers.cpp" -#include "../buffer.cpp" +#include "datatypes.cpp" +#include "helpers.cpp" +#include "buffer.cpp" namespace UwU { // shader prototype diff --git a/shaders/colorshader.cpp b/shaders/colorshader.cpp deleted file mode 100644 index 47ef643..0000000 --- a/shaders/colorshader.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include - -#include "../datatypes.cpp" -#include "../helpers.cpp" -#include "../buffer.cpp" -#include "shader.cpp" - -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) { - (void)u; - (void)v; - return shaderColor; - } - }; -} \ No newline at end of file diff --git a/shaders/hgradientshader.cpp b/shaders/hgradientshader.cpp deleted file mode 100644 index dca2094..0000000 --- a/shaders/hgradientshader.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include - -#include "../datatypes.cpp" -#include "../helpers.cpp" -#include "../buffer.cpp" -#include "shader.cpp" - -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 hGradient(uint16_t u, uint16_t v) { - (void)v; - uint32_t t = (((uint32_t)u - (uint32_t)x0) * scale) / 0x01000000; - // if (v == 42) { - // printf("(%u, %u)\n", u, t); - // } - return Helpers::lerpColor(shaderColor0, shaderColor1, (uint8_t)t); - } - }; -} \ No newline at end of file diff --git a/shaders/vgradientshader.cpp b/shaders/vgradientshader.cpp deleted file mode 100644 index 520cc86..0000000 --- a/shaders/vgradientshader.cpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include - -#include "../datatypes.cpp" -#include "../helpers.cpp" -#include "../buffer.cpp" -#include "shader.cpp" - -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 vGradient(uint16_t u, uint16_t v) { - (void)u; - uint32_t t = (((uint32_t)v - (uint32_t)y0) * scale) / 0x01000000; - // if (v == 42) { - // printf("(%u, %u)\n", u, t); - // } - return Helpers::lerpColor(shaderColor0, shaderColor1, (uint8_t)t); - } - }; -} \ No newline at end of file