2026-03-09 20:10:19 +01:00
|
|
|
/*
|
|
|
|
|
Crafter®.Graphics
|
|
|
|
|
Copyright (C) 2026 Catcrafts®
|
|
|
|
|
catcrafts.net
|
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
License version 3.0 as published by the Free Software Foundation;
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
*/
|
|
|
|
|
export module Crafter.Graphics:Rendertarget;
|
|
|
|
|
import Crafter.Math;
|
2026-03-10 22:32:50 +01:00
|
|
|
import Crafter.Asset;
|
2026-03-09 20:10:19 +01:00
|
|
|
import std;
|
|
|
|
|
import :Types;
|
|
|
|
|
import :Transform2D;
|
2026-03-10 22:32:50 +01:00
|
|
|
import :RenderingElement2DBase;
|
2026-03-09 20:10:19 +01:00
|
|
|
|
|
|
|
|
export namespace Crafter {
|
2026-03-12 21:13:53 +01:00
|
|
|
template<std::uint8_t Frames>
|
2026-03-10 22:32:50 +01:00
|
|
|
struct RendertargetBase {
|
2026-03-09 20:10:19 +01:00
|
|
|
#ifdef CRAFTER_TIMING
|
|
|
|
|
std::vector<std::tuple<const Transform*, std::uint32_t, std::uint32_t, std::chrono::nanoseconds>> renderTimings;
|
|
|
|
|
#endif
|
|
|
|
|
Transform2D transform;
|
2026-03-22 21:08:02 +01:00
|
|
|
std::uint16_t sizeX;
|
|
|
|
|
std::uint16_t sizeY;
|
2026-03-10 22:32:50 +01:00
|
|
|
RendertargetBase() = default;
|
2026-03-22 21:08:02 +01:00
|
|
|
RendertargetBase(std::uint16_t sizeX, std::uint16_t sizeY) : sizeX(sizeX), sizeY(sizeY), transform({0, 0, 1, 1, 0, 0, 0}){
|
2026-03-10 22:32:50 +01:00
|
|
|
transform.scaled.size.x = sizeX;
|
|
|
|
|
transform.scaled.size.y = sizeY;
|
|
|
|
|
transform.scaled.position.x = 0;
|
|
|
|
|
transform.scaled.position.y = 0;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-12 21:13:53 +01:00
|
|
|
template<typename T, std::uint8_t Channels, std::uint8_t Alignment, std::uint8_t Frames>
|
|
|
|
|
struct Rendertarget : RendertargetBase<Frames> {
|
|
|
|
|
Vector<T, Channels, Alignment>* buffer[Frames];
|
2026-03-10 22:32:50 +01:00
|
|
|
Rendertarget() = default;
|
2026-03-31 15:22:55 +02:00
|
|
|
Rendertarget(std::uint16_t sizeX, std::uint16_t sizeY) : RendertargetBase<Frames>(sizeX, sizeY) {
|
2026-03-10 22:32:50 +01:00
|
|
|
|
|
|
|
|
}
|
2026-03-13 01:06:55 +01:00
|
|
|
void RenderElement(Transform2D* elementTransform, std::uint8_t frame, std::vector<ClipRect>&& dirtyRects) {
|
2026-03-31 15:22:55 +02:00
|
|
|
RenderingElement2DBase<T, Frames>* element = dynamic_cast<RenderingElement2DBase<T, Frames>*>(elementTransform);
|
2026-03-12 01:07:46 +01:00
|
|
|
if(element) {
|
|
|
|
|
#ifdef CRAFTER_TIMING
|
|
|
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
|
#endif
|
2026-03-10 22:32:50 +01:00
|
|
|
|
2026-03-12 01:07:46 +01:00
|
|
|
if(element->scaled.size.x < 1 || element->scaled.size.y < 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-03-10 22:32:50 +01:00
|
|
|
|
2026-03-13 01:06:55 +01:00
|
|
|
for(ClipRect dirty : dirtyRects) {
|
2026-03-12 01:07:46 +01:00
|
|
|
dirty.left = std::max(element->scaled.position.x, dirty.left);
|
|
|
|
|
dirty.top = std::max(element->scaled.position.y, dirty.top);
|
2026-03-31 15:22:55 +02:00
|
|
|
dirty.right = std::min(std::uint16_t(element->scaled.position.x+element->scaled.size.x), dirty.right);
|
|
|
|
|
dirty.bottom = std::min(std::uint16_t(element->scaled.position.y+element->scaled.size.y), dirty.bottom);
|
2026-03-12 01:07:46 +01:00
|
|
|
|
2026-03-31 15:22:55 +02:00
|
|
|
const Vector<T, 4, 4>* src_buffer = element->buffer.data();
|
2026-03-12 01:07:46 +01:00
|
|
|
std::int32_t src_width = element->scaled.size.x;
|
|
|
|
|
std::int32_t src_height = element->scaled.size.y;
|
|
|
|
|
|
|
|
|
|
switch (element->opaque) {
|
2026-03-22 21:08:02 +01:00
|
|
|
case OpaqueType::FullyOpaque: {
|
2026-03-12 01:07:46 +01:00
|
|
|
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
|
|
|
|
std::int32_t src_y = y - element->scaled.position.y;
|
2026-03-22 21:08:02 +01:00
|
|
|
std::memcpy(&this->buffer[frame][y * this->sizeX], &src_buffer[src_y * src_width], dirty.right-dirty.left*sizeof(Vector<T, Channels, Alignment>));
|
2026-03-10 22:32:50 +01:00
|
|
|
}
|
2026-03-12 01:07:46 +01:00
|
|
|
break;
|
2026-03-22 21:08:02 +01:00
|
|
|
}
|
2026-03-24 05:24:46 +01:00
|
|
|
case OpaqueType::SemiOpaque:
|
2026-03-12 01:07:46 +01:00
|
|
|
case OpaqueType::Transparent:
|
2026-03-24 05:24:46 +01:00
|
|
|
if constexpr(std::same_as<T, _Float16>) {
|
|
|
|
|
for (std::uint16_t y = dirty.top; y < dirty.bottom; y++) {
|
|
|
|
|
std::uint16_t src_y = y - element->scaled.position.y;
|
2026-03-31 15:22:55 +02:00
|
|
|
std::uint32_t pixel_width = dirty.right - dirty.left;
|
|
|
|
|
constexpr std::uint32_t simd_width = VectorF16<1, 1>::MaxElement / 4;
|
|
|
|
|
std::uint32_t rows = pixel_width / simd_width;
|
2026-03-10 22:32:50 +01:00
|
|
|
|
2026-03-31 15:22:55 +02:00
|
|
|
for (std::uint32_t x = 0; x < rows; x++) {
|
|
|
|
|
std::uint16_t px = dirty.left + x * simd_width;
|
|
|
|
|
std::uint16_t src_x = px - element->scaled.position.x;
|
|
|
|
|
std::uint16_t dst_x = px;
|
|
|
|
|
|
|
|
|
|
VectorF16<4, simd_width> src(&src_buffer[src_y * src_width + src_x].v[0]);
|
|
|
|
|
VectorF16<4, simd_width> dst(&buffer[frame][y * this->sizeX + dst_x].v[0]);
|
|
|
|
|
VectorF16<4, simd_width> oneMinusSrcA = VectorF16<4, simd_width>(1) - src.Shuffle<{{3, 3, 3, 3}}>();
|
|
|
|
|
VectorF16<4, simd_width> result = VectorF16<4, simd_width>::MulitplyAdd(dst, oneMinusSrcA, src);
|
|
|
|
|
result.Store(buffer[frame][y * this->sizeX + dst_x]);
|
|
|
|
|
}
|
2026-03-24 05:24:46 +01:00
|
|
|
|
2026-03-31 15:22:55 +02:00
|
|
|
std::uint32_t remainder = pixel_width - (rows * simd_width);
|
|
|
|
|
std::uint16_t remainder_start = dirty.left + rows * simd_width;
|
2026-03-24 05:24:46 +01:00
|
|
|
|
2026-03-31 15:22:55 +02:00
|
|
|
for (std::uint8_t x = 0; x < remainder; x++) {
|
|
|
|
|
std::uint16_t px = remainder_start + x;
|
|
|
|
|
std::uint16_t src_x = px - element->scaled.position.x;
|
2026-03-24 05:24:46 +01:00
|
|
|
|
|
|
|
|
Vector<T, Channels, Alignment> src = src_buffer[src_y * src_width + src_x];
|
2026-03-31 15:22:55 +02:00
|
|
|
Vector<T, Channels, Alignment> dst = buffer[frame][y * this->sizeX + px];
|
|
|
|
|
_Float16 oneMinusSrcA = (_Float16)1.0f - src[3];
|
|
|
|
|
|
|
|
|
|
buffer[frame][y * this->sizeX + px] = Vector<T, Channels, Alignment>(
|
|
|
|
|
src.r + dst.r * oneMinusSrcA,
|
|
|
|
|
src.g + dst.g * oneMinusSrcA,
|
|
|
|
|
src.b + dst.b * oneMinusSrcA,
|
|
|
|
|
src.a + dst.a * oneMinusSrcA
|
2026-03-24 05:24:46 +01:00
|
|
|
);
|
|
|
|
|
}
|
2026-03-12 01:07:46 +01:00
|
|
|
}
|
2026-03-31 15:22:55 +02:00
|
|
|
} else {
|
|
|
|
|
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
|
|
|
|
|
std::int32_t src_y = y - element->scaled.position.y;
|
|
|
|
|
std::memcpy(&this->buffer[frame][y * this->sizeX], &src_buffer[src_y * src_width], dirty.right-dirty.left*sizeof(Vector<T, Channels, Alignment>));
|
2026-03-12 01:07:46 +01:00
|
|
|
}
|
2026-03-10 22:32:50 +01:00
|
|
|
}
|
2026-03-12 01:07:46 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2026-03-10 22:32:50 +01:00
|
|
|
}
|
2026-03-12 01:07:46 +01:00
|
|
|
#ifdef CRAFTER_TIMING
|
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
|
|
|
renderTimings.push_back({element, element->scaled.size.x, element->scaled.size.y, end-start});
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
std::sort(elementTransform->children.begin(), elementTransform->children.end(), [](Transform2D* a, Transform2D* b){ return a->anchor.z < b->anchor.z; });
|
|
|
|
|
for(Transform2D* child : elementTransform->children) {
|
2026-03-13 01:06:55 +01:00
|
|
|
this->RenderElement(child, frame, std::move(dirtyRects));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddOldRects(Transform2D* elementTransform, std::uint8_t frame, std::vector<ClipRect>& clipRects) {
|
2026-03-31 15:22:55 +02:00
|
|
|
RenderingElement2DBase<T, Frames>* element = dynamic_cast<RenderingElement2DBase<T, Frames>*>(elementTransform);
|
2026-03-13 01:06:55 +01:00
|
|
|
if(element) {
|
|
|
|
|
if(element->scaled.position.x != element->oldScale[frame].position.x || element->scaled.position.y != element->oldScale[frame].position.y || element->scaled.size.x != element->oldScale[frame].size.x || element->scaled.size.y != element->oldScale[frame].size.y || element->redraw[frame]) {
|
2026-03-31 15:22:55 +02:00
|
|
|
clipRects.emplace_back(std::max(element->scaled.position.x, std::uint16_t(0)), std::min(std::uint16_t(element->scaled.position.x + element->scaled.size.x), this->sizeX), std::max(element->scaled.position.y, std::uint16_t(0)), std::min(std::uint16_t(element->scaled.position.y + element->scaled.size.y), this->sizeY));
|
|
|
|
|
clipRects.emplace_back(std::max(element->oldScale[frame].position.x, std::uint16_t(0)), std::min(std::uint16_t(element->oldScale[frame].position.x + element->oldScale[frame].size.x), this->sizeX), std::max(element->oldScale[frame].position.y, std::uint16_t(0)), std::min(std::uint16_t(element->oldScale[frame].position.y + element->oldScale[frame].size.y), this->sizeY));
|
2026-03-13 01:06:55 +01:00
|
|
|
element->oldScale[frame] = element->scaled;
|
|
|
|
|
element->redraw[frame] = false;
|
2026-03-24 05:24:46 +01:00
|
|
|
} else if(element->redraw[frame]) {
|
2026-03-31 15:22:55 +02:00
|
|
|
clipRects.emplace_back(std::max(element->scaled.position.x, std::uint16_t(0)), std::min(std::uint16_t(element->scaled.position.x + element->scaled.size.x), this->sizeX), std::max(element->scaled.position.y, std::uint16_t(0)), std::min(std::uint16_t(element->scaled.position.y + element->scaled.size.y), this->sizeY));
|
2026-03-24 05:24:46 +01:00
|
|
|
element->oldScale[frame] = element->scaled;
|
|
|
|
|
element->redraw[frame] = false;
|
2026-03-13 01:06:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(Transform2D* child : elementTransform->children) {
|
|
|
|
|
AddOldRects(child, frame, clipRects);
|
2026-03-10 22:32:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-13 01:06:55 +01:00
|
|
|
|
|
|
|
|
bool Render(std::uint8_t frame) {
|
|
|
|
|
std::sort(this->transform.children.begin(), this->transform.children.end(), [](Transform2D* a, Transform2D* b){ return a->anchor.z < b->anchor.z; });
|
|
|
|
|
std::vector<ClipRect> clipRects;
|
|
|
|
|
for(Transform2D* child : this->transform.children) {
|
|
|
|
|
AddOldRects(child, frame, clipRects);
|
|
|
|
|
}
|
2026-03-12 01:07:46 +01:00
|
|
|
|
2026-03-10 22:32:50 +01:00
|
|
|
//std::vector<ClipRect> newClip;
|
|
|
|
|
// for (std::uint32_t i = 0; i < dirtyRects.size(); i++) {
|
|
|
|
|
// ClipRect rect = dirtyRects[i];
|
|
|
|
|
// for (std::uint32_t i2 = i + 1; i2 < dirtyRects.size(); i2++) {
|
|
|
|
|
// ClipRect existing = dirtyRects[i2];
|
|
|
|
|
// if(rect.bottom >= existing.top && rect.top <= existing.top) {
|
|
|
|
|
// newClip.push_back({
|
|
|
|
|
// .left = rect.left,
|
|
|
|
|
// .right = rect.right,
|
|
|
|
|
// .top = rect.top,
|
|
|
|
|
// .bottom = existing.top,
|
|
|
|
|
// });
|
|
|
|
|
// //-| shape
|
|
|
|
|
// if(rect.right > existing.right) {
|
|
|
|
|
// newClip.push_back({
|
|
|
|
|
// .left = existing.right,
|
|
|
|
|
// .right = rect.right,
|
|
|
|
|
// .top = existing.top,
|
|
|
|
|
// .bottom = existing.bottom,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// //|- shape
|
|
|
|
|
// if(rect.left < existing.left) {
|
|
|
|
|
// newClip.push_back({
|
|
|
|
|
// .left = rect.left,
|
|
|
|
|
// .right = existing.left,
|
|
|
|
|
// .top = existing.top,
|
|
|
|
|
// .bottom = existing.bottom,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// //-| or |- shape where rect extends further down
|
|
|
|
|
// if(rect.bottom > existing.bottom) {
|
|
|
|
|
// newClip.push_back({
|
|
|
|
|
// .left = rect.left,
|
|
|
|
|
// .right = rect.right,
|
|
|
|
|
// .top = existing.bottom,
|
|
|
|
|
// .bottom = rect.bottom,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// goto inner;
|
|
|
|
|
// }
|
|
|
|
|
// if (rect.left <= existing.right && rect.right >= existing.left) {
|
|
|
|
|
// newClip.push_back({
|
|
|
|
|
// .left = rect.left,
|
|
|
|
|
// .right = existing.left,
|
|
|
|
|
// .top = rect.top,
|
|
|
|
|
// .bottom = rect.bottom,
|
|
|
|
|
// });
|
|
|
|
|
// if (rect.right > existing.right) {
|
|
|
|
|
// newClip.push_back({
|
|
|
|
|
// .left = existing.right,
|
|
|
|
|
// .right = rect.right,
|
|
|
|
|
// .top = rect.top,
|
|
|
|
|
// .bottom = rect.bottom,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// goto inner;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// newClip.push_back(rect);
|
|
|
|
|
// inner:;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
//dirtyRects = std::move(newClip);
|
|
|
|
|
|
|
|
|
|
// std::memset(buffer, 0, width*height*4);
|
|
|
|
|
|
|
|
|
|
// std::cout << dirtyRects.size() << std::endl;
|
|
|
|
|
// // Color palette
|
|
|
|
|
// static const std::vector<Vector<std::uint8_t, 4>> colors = {
|
|
|
|
|
// {255, 0, 0, 255}, // red
|
|
|
|
|
// { 0, 255, 0, 255}, // green
|
|
|
|
|
// { 0, 0, 255, 255}, // blue
|
|
|
|
|
// {255, 255, 0, 255}, // yellow
|
|
|
|
|
// {255, 0, 255, 255}, // magenta
|
|
|
|
|
// { 0, 255, 255, 255}, // cyan
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// std::size_t rectIndex = 0;
|
|
|
|
|
|
|
|
|
|
// for (const ClipRect& rect : dirtyRects) {
|
|
|
|
|
// const Vector<std::uint8_t, 4>& color = colors[rectIndex % colors.size()];
|
|
|
|
|
|
|
|
|
|
// std::cout << std::format(
|
|
|
|
|
// "ClipRect {}: [{}, {}, {}, {}] Color = RGBA({}, {}, {}, {})",
|
|
|
|
|
// rectIndex,
|
|
|
|
|
// rect.left, rect.top, rect.right, rect.bottom,
|
|
|
|
|
// color.r, color.g, color.b, color.a
|
|
|
|
|
// ) << std::endl;
|
|
|
|
|
|
|
|
|
|
// for (std::int32_t y = rect.top; y < rect.bottom; ++y) {
|
|
|
|
|
// for (std::int32_t x = rect.left; x < rect.right; ++x) {
|
|
|
|
|
// buffer[y * width + x] = color;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// ++rectIndex;
|
|
|
|
|
// }
|
|
|
|
|
|
2026-03-13 01:06:55 +01:00
|
|
|
if (!clipRects.empty()) {
|
|
|
|
|
for (ClipRect rect : clipRects) {
|
2026-03-10 22:32:50 +01:00
|
|
|
for (std::int32_t y = rect.top; y < rect.bottom; y++) {
|
|
|
|
|
for (std::int32_t x = rect.left; x < rect.right; x++) {
|
2026-03-12 21:13:53 +01:00
|
|
|
this->buffer[frame][y * this->sizeX + x] = {0, 0, 0, 0};
|
2026-03-10 22:32:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 01:06:55 +01:00
|
|
|
for(Transform2D* child : this->transform.children) {
|
|
|
|
|
RenderElement(child, frame, std::move(clipRects));
|
2026-03-10 22:32:50 +01:00
|
|
|
}
|
2026-03-13 01:06:55 +01:00
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
2026-03-10 22:32:50 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-09 20:10:19 +01:00
|
|
|
};
|
|
|
|
|
}
|