optimization

This commit is contained in:
Jorijn van der Graaf 2025-11-26 00:00:50 +01:00
commit bf6793e41d
5 changed files with 51 additions and 43 deletions

View file

@ -7,6 +7,7 @@ int main() {
WindowWayland window(1280, 720, "Hello Input!"); WindowWayland window(1280, 720, "Hello Input!");
RenderingElement element( RenderingElement element(
true, //opaque, wether the element is opague or semi-transparant
2, //bufferWidth: the width of this elements pixel buffer 2, //bufferWidth: the width of this elements pixel buffer
1, //bufferHeight: the height of this elements pixel buffer 1, //bufferHeight: the height of this elements pixel buffer
FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor FractionalToMapped(0.5), //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor

View file

@ -31,16 +31,16 @@ import std;
using namespace Crafter; using namespace Crafter;
RenderingElement::RenderingElement() : Transform() { RenderingElement::RenderingElement(bool opaque) : Transform(), opaque(opaque) {
} }
RenderingElement::RenderingElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { RenderingElement::RenderingElement(bool opaque, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), opaque(opaque) {
} }
RenderingElement::RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { RenderingElement::RenderingElement(bool opaque, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling) : bufferWidth(bufferWidth), bufferHeight(bufferHeight), buffer(bufferWidth*bufferHeight), Transform(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), opaque(opaque) {
} }
@ -61,16 +61,26 @@ void RenderingElement::CopyNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uin
} }
void RenderingElement::UpdatePosition(Window& window) { void RenderingElement::UpdatePosition(Window& window) {
std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this); window.ScaleElement(*this);
bufferScaled.resize(scaled.width * scaled.height); if(oldWidth != scaled.width || oldHeight && scaled.height) {
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height); bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
}
for(Transform* child : children) { for(Transform* child : children) {
child->UpdatePosition(window, *this); child->UpdatePosition(window, *this);
} }
} }
void RenderingElement::UpdatePosition(Window& window, Transform& parent) { void RenderingElement::UpdatePosition(Window& window, Transform& parent) {
std::uint_fast32_t oldWidth = scaled.width;
std::uint_fast32_t oldHeight = scaled.height;
window.ScaleElement(*this, parent); window.ScaleElement(*this, parent);
if(oldWidth != scaled.width || oldHeight && scaled.height) {
bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
}
bufferScaled.resize(scaled.width * scaled.height); bufferScaled.resize(scaled.width * scaled.height);
CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height); CopyNearestNeighbour(bufferScaled.data(), scaled.width, scaled.height);
for(Transform* child : children) { for(Transform* child : children) {

View file

@ -178,22 +178,37 @@ void WindowWayland::RenderElement(Transform* transform) {
std::uint_fast32_t src_width = element->scaled.width; std::uint_fast32_t src_width = element->scaled.width;
std::uint_fast32_t src_height = element->scaled.height; std::uint_fast32_t src_height = element->scaled.height;
// Render clipped region // If element is opaque, we can simply copy pixels without blending
for (std::int_fast32_t y = clip_top; y < clip_bottom; y++) { if (element->opaque) {
std::int_fast32_t src_y = y - element->scaled.y; // Render clipped region
for (std::int_fast32_t y = clip_top; y < clip_bottom; y++) {
std::int_fast32_t src_y = y - element->scaled.y;
for (std::int_fast32_t x = clip_left; x < clip_right; x++) { for (std::int_fast32_t x = clip_left; x < clip_right; x++) {
std::int_fast32_t src_x = x - element->scaled.x; std::int_fast32_t src_x = x - element->scaled.x;
// Bounds check for source buffer // Bounds check for source buffer
if (src_x >= 0 && src_x < static_cast<std::int_fast32_t>(src_width) && src_y >= 0 && src_y < static_cast<std::int_fast32_t>(src_height)) { if (src_x >= 0 && src_x < static_cast<std::int_fast32_t>(src_width) && src_y >= 0 && src_y < static_cast<std::int_fast32_t>(src_height)) {
// Get pixel indices // Direct copy for opaque elements (skip blending)
std::uint_fast32_t dst_idx = y * width + x; framebuffer[y * width + x] = src_buffer[src_y * src_width + src_x];
std::uint_fast32_t src_idx = src_y * src_width + src_x; }
}
}
} else {
// Render clipped region with blending for non-opaque elements
for (std::int_fast32_t y = clip_top; y < clip_bottom; y++) {
std::int_fast32_t src_y = y - element->scaled.y;
// Blend pixels for (std::int_fast32_t x = clip_left; x < clip_right; x++) {
blend_pixel_optimized(framebuffer[dst_idx], src_buffer[src_idx]); std::int_fast32_t src_x = x - element->scaled.x;
// Bounds check for source buffer
if (src_x >= 0 && src_x < static_cast<std::int_fast32_t>(src_width) && src_y >= 0 && src_y < static_cast<std::int_fast32_t>(src_height)) {
// Blend pixels
blend_pixel_optimized(framebuffer[y * width + x], src_buffer[src_y * src_width + src_x]);
}
} }
} }
} }

View file

@ -61,27 +61,12 @@ namespace Crafter {
std::vector<Keyframe<T>> keyframes; std::vector<Keyframe<T>> keyframes;
std::uint_fast32_t currentFrame; std::uint_fast32_t currentFrame;
std::chrono::time_point<std::chrono::high_resolution_clock> startedAt; std::chrono::time_point<std::chrono::high_resolution_clock> startedAt;
mutable T cachedValue; // Cache the last computed value Animation(std::vector<Keyframe<T>>&& keyframes) : keyframes(std::move(keyframes)) {}
mutable std::chrono::time_point<std::chrono::high_resolution_clock> lastTime; // Track when cached value was computed
Animation(std::vector<Keyframe<T>>&& keyframes) : keyframes(std::move(keyframes)), currentFrame(0) {}
void Start(std::chrono::time_point<std::chrono::high_resolution_clock> time) { void Start(std::chrono::time_point<std::chrono::high_resolution_clock> time) {
currentFrame = 0; currentFrame = 0;
startedAt = time; startedAt = time;
// Invalidate cache
lastTime = std::chrono::time_point<std::chrono::high_resolution_clock>();
} }
T Play(std::chrono::time_point<std::chrono::high_resolution_clock> time) { T Play(std::chrono::time_point<std::chrono::high_resolution_clock> time) {
// Check if we can reuse cached value
if (lastTime != std::chrono::time_point<std::chrono::high_resolution_clock>()) {
// Only use cached value if we haven't moved forward in time
if (time <= lastTime) {
return cachedValue;
}
}
std::chrono::duration<double> elapsed = time - startedAt; // elapsed time since animation started std::chrono::duration<double> elapsed = time - startedAt; // elapsed time since animation started
std::chrono::duration<double> accumulated(0); std::chrono::duration<double> accumulated(0);
@ -92,21 +77,17 @@ namespace Crafter {
auto t = (elapsed - frameStartTime) / keyframes[i+1].duration.count(); auto t = (elapsed - frameStartTime) / keyframes[i+1].duration.count();
currentFrame = i; currentFrame = i;
cachedValue = LerpTuple( return LerpTuple(
keyframes[i].values, keyframes[i].values,
keyframes[i + 1].values, keyframes[i + 1].values,
t.count() t.count()
); );
lastTime = time;
return cachedValue;
} }
} }
// If we get here, we're past the last keyframe // If we get here, we're past the last keyframe
currentFrame = keyframes.size() - 1; currentFrame = keyframes.size() - 1;
cachedValue = keyframes.back().values; return keyframes.back().values;
lastTime = time;
return cachedValue;
} }
}; };
} }

View file

@ -31,10 +31,11 @@ export namespace Crafter {
std::vector<Pixel_BU8_GU8_RU8_AU8> bufferScaled; std::vector<Pixel_BU8_GU8_RU8_AU8> bufferScaled;
std::uint_fast32_t bufferWidth; std::uint_fast32_t bufferWidth;
std::uint_fast32_t bufferHeight; std::uint_fast32_t bufferHeight;
bool opaque = false;
RenderingElement(); RenderingElement(bool opague = false);
RenderingElement(std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling); RenderingElement(bool opague, std::int_fast32_t anchorX, std::int_fast32_t anchorY, std::uint_fast32_t relativeWidth, std::uint_fast32_t relativeHeight, std::int_fast32_t anchorOffsetX, std::int_fast32_t anchorOffsetY, std::int_fast32_t z, bool ignoreScaling);
RenderingElement(std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false); RenderingElement(bool opague, std::uint_fast32_t bufferWidth, std::uint_fast32_t bufferHeight, std::int_fast32_t anchorX = FractionalToMapped(0.5), std::int_fast32_t anchorY = FractionalToMapped(0.5), std::uint_fast32_t relativeWidth = FractionalToMapped(1), std::uint_fast32_t relativeHeight = FractionalToMapped(1), std::int_fast32_t anchorOffsetX = FractionalToMapped(0.5), std::int_fast32_t anchorOffsetY = FractionalToMapped(0.5), std::int_fast32_t z = 0, bool ignoreScaling = false);
RenderingElement(RenderingElement&) = delete; RenderingElement(RenderingElement&) = delete;
RenderingElement& operator=(RenderingElement&) = delete; RenderingElement& operator=(RenderingElement&) = delete;