rendering update

This commit is contained in:
Jorijn van der Graaf 2026-03-22 21:08:02 +01:00
commit 7fdab4f62b
4 changed files with 33 additions and 49 deletions

View file

@ -31,10 +31,10 @@ export namespace Crafter {
std::vector<std::tuple<const Transform*, std::uint32_t, std::uint32_t, std::chrono::nanoseconds>> renderTimings;
#endif
Transform2D transform;
std::int32_t sizeX;
std::int32_t sizeY;
std::uint16_t sizeX;
std::uint16_t sizeY;
RendertargetBase() = default;
RendertargetBase(std::int16_t sizeX, std::int16_t sizeY) : sizeX(sizeX), sizeY(sizeY), transform({0, 0, 1, 1, 0, 0, 0}){
RendertargetBase(std::uint16_t sizeX, std::uint16_t sizeY) : sizeX(sizeX), sizeY(sizeY), transform({0, 0, 1, 1, 0, 0, 0}){
transform.scaled.size.x = sizeX;
transform.scaled.size.y = sizeY;
transform.scaled.position.x = 0;
@ -71,56 +71,30 @@ export namespace Crafter {
std::int32_t src_height = element->scaled.size.y;
switch (element->opaque) {
case OpaqueType::FullyOpaque:
case OpaqueType::FullyOpaque: {
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
std::int32_t src_y = y - element->scaled.position.y;
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
std::int32_t src_x = x - element->scaled.position.x;
this->buffer[frame][y * this->sizeX + x] = src_buffer[src_y * src_width + src_x];
}
std::memcpy(&this->buffer[frame][y * this->sizeX], &src_buffer[src_y * src_width], dirty.right-dirty.left*sizeof(Vector<T, Channels, Alignment>));
}
break;
case OpaqueType::SemiOpaque:
// For semi-opaque, we can avoid blending when alpha is 0 or 255
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
std::int32_t src_y = y - element->scaled.position.y;
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
std::int32_t src_x = x - element->scaled.position.x;
Vector<std::uint8_t, 4> src_pixel = src_buffer[src_y * src_width + src_x];
if (src_pixel.a == 0) {
continue;
}
this->buffer[frame][y * this->sizeX + x] = src_pixel;
}
}
break;
}
case OpaqueType::Transparent:
// For transparent, always perform blending
for (std::int32_t y = dirty.top; y < dirty.bottom; y++) {
std::int32_t src_y = y - element->scaled.position.y;
for (std::int32_t x = dirty.left; x < dirty.right; x++) {
std::int32_t src_x = x - element->scaled.position.x;
for (std::uint16_t y = dirty.top; y < dirty.bottom; y++) {
std::uint16_t src_y = y - element->scaled.position.y;
for (std::uint16_t x = dirty.left; x < dirty.right; x++) {
std::uint16_t src_x = x - element->scaled.position.x;
Vector<T, Channels, Alignment> src = src_buffer[src_y * src_width + src_x];
Vector<T, Channels, Alignment> dst = buffer[frame][y * this->sizeX + x];
if(src.a == 0) {
continue;
}
float srcA = src.a / 255.0f;
float dstA = dst.a / 255.0f;
float dstA = dst.a / 255.0f;k
float outA = srcA + dstA * (1.0f - srcA);
this->buffer[frame][y * this->sizeX + x] = Vector<T, Channels, Alignment>(
static_cast<T>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA),
static_cast<T>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA),
static_cast<T>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA),
static_cast<T>((src.r + dst.r * (1.0f - srcA)) / outA),
static_cast<T>((src.g + dst.g * (1.0f - srcA)) / outA),
static_cast<T>((src.b + dst.b * (1.0f - srcA)) / outA),
static_cast<T>(outA * 255)
);
}