minor optimisations

This commit is contained in:
koniifer 2024-11-10 11:48:44 +00:00
parent 8808ed5bfe
commit 3333b1706f
4 changed files with 106 additions and 106 deletions

6
Cargo.lock generated
View file

@ -228,12 +228,12 @@ dependencies = [
[[package]] [[package]]
name = "hbbytecode" name = "hbbytecode"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#7865d692a156c1d31f63b8f04b5d51472d1ff30a" source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#8b98c2ed1becb92046bb7b687ca00813da441248"
[[package]] [[package]]
name = "hblang" name = "hblang"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#7865d692a156c1d31f63b8f04b5d51472d1ff30a" source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#8b98c2ed1becb92046bb7b687ca00813da441248"
dependencies = [ dependencies = [
"hashbrown 0.15.1", "hashbrown 0.15.1",
"hbbytecode", "hbbytecode",
@ -245,7 +245,7 @@ dependencies = [
[[package]] [[package]]
name = "hbvm" name = "hbvm"
version = "0.1.0" version = "0.1.0"
source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#7865d692a156c1d31f63b8f04b5d51472d1ff30a" source = "git+https://git.ablecorp.us/AbleOS/holey-bytes.git#8b98c2ed1becb92046bb7b687ca00813da441248"
dependencies = [ dependencies = [
"hbbytecode", "hbbytecode",
] ]

View file

@ -35,17 +35,14 @@ BitmapColorHeader := packed struct {
from := fn(bmp: ^u8): ?Surface { from := fn(bmp: ^u8): ?Surface {
file_header := @as(^BitmapFileHeader, @bitcast(bmp)) file_header := @as(^BitmapFileHeader, @bitcast(bmp))
info_header := @as(^BitmapInfoHeader, @bitcast(bmp + @sizeof(BitmapFileHeader))) info_header := @as(^BitmapInfoHeader, @bitcast(bmp + @sizeof(BitmapFileHeader)))
bmp += file_header.offset
if file_header.magic != 0x4D42 | info_header.width == 0 | info_header.height == 0 { if file_header.magic != 0x4D42 | info_header.width == 0 | info_header.height == 0 {
log.error("Invalid BMP image.\0") log.error("Invalid BMP image.\0")
return null return null
} }
width := @as(uint, info_header.width) lhs := Surface.(@bitcast(bmp + file_header.offset), info_header.width, info_header.height)
height := @as(uint, info_header.height) rhs := new_surface(info_header.width, info_header.height)
lhs := Surface.(@bitcast(bmp), width, height)
rhs := new_surface(width, height)
put_surface(rhs, lhs, .(0, 0), true) put_surface(rhs, lhs, .(0, 0), true)
return rhs return rhs

View file

@ -28,7 +28,7 @@ QuiteOkayHeader := packed struct {
} }
be_to_le := fn(big: u32): u32 { be_to_le := fn(big: u32): u32 {
return (big & 0xFF000000) >> 24 | (big & 0xFF0000) >> 8 | (big & 0xFF00) << 8 | (big & 0xFF) << 24 return big >> 24 | big >> 8 & 0xFF00 | big << 8 & 0xFF0000 | big << 24
} }
from := fn(qoi: ^u8): ?Surface { from := fn(qoi: ^u8): ?Surface {

View file

@ -20,25 +20,12 @@ new_surface := fn(width: uint, height: uint): Surface {
) )
} }
surface_from_ptr := fn(ptr: ^Color, width: uint, height: uint): Surface {
return .(
ptr,
width,
height,
)
}
clone_surface := fn(surface: ^Surface): Surface { clone_surface := fn(surface: ^Surface): Surface {
new := new_surface(surface.width, surface.height) new := new_surface(surface.width, surface.height)
@inline(memory.copy, Color, surface.buf, new.buf, @intcast(surface.width * surface.height)) @inline(memory.copy, Color, surface.buf, new.buf, @intcast(surface.width * surface.height))
return new return new
} }
// ! is broken, check memory.free function
free_surface := fn(surface: Surface): void {
return @inline(memory.free, Color, surface.buf, @intcast(surface.width * surface.height), false)
}
init := fn(doublebuffer: bool): Surface { init := fn(doublebuffer: bool): Surface {
framebuffer = dt.get(^Color, "framebuffer/fb0/ptr\0") framebuffer = dt.get(^Color, "framebuffer/fb0/ptr\0")
width := dt.get(uint, "framebuffer/fb0/width\0") width := dt.get(uint, "framebuffer/fb0/width\0")
@ -55,7 +42,6 @@ clear := fn(surface: Surface, color: Color): void {
} }
sync := fn(surface: Surface): void { sync := fn(surface: Surface): void {
// vague safety
if surface.buf == framebuffer { if surface.buf == framebuffer {
return return
} }
@ -81,9 +67,8 @@ put_filled_rect := fn(surface: Surface, pos: Vec2(uint), tr: Vec2(uint), color:
rows_to_fill := tr.y rows_to_fill := tr.y
loop if rows_to_fill <= 1 break else { loop if rows_to_fill <= 1 break else {
// inline is broked @inline(memory.set, Color, &color, top_start_idx, tr.x)
memory.set(Color, &color, top_start_idx, @bitcast(tr.x)) @inline(memory.set, Color, &color, bottom_start_idx, tr.x)
memory.set(Color, &color, bottom_start_idx, @bitcast(tr.x))
top_start_idx += surface.width top_start_idx += surface.width
bottom_start_idx -= surface.width bottom_start_idx -= surface.width
@ -91,7 +76,7 @@ put_filled_rect := fn(surface: Surface, pos: Vec2(uint), tr: Vec2(uint), color:
} }
if rows_to_fill == 1 { if rows_to_fill == 1 {
@inline(memory.set, Color, &color, top_start_idx, @bitcast(tr.x)) @inline(memory.set, Color, &color, top_start_idx, tr.x)
} }
return return
@ -181,30 +166,36 @@ put_line := fn(surface: Surface, p0: Vec2(uint), p1: Vec2(uint), color: Color):
} }
put_surface := fn(surface: Surface, top: Surface, pos: Vec2(uint), flip_v: bool): void { put_surface := fn(surface: Surface, top: Surface, pos: Vec2(uint), flip_v: bool): void {
top_start_idx := @inline(indexptr, surface, pos.x, pos.y) src_top_cursor := top.buf
bottom_start_idx := @inline(indexptr, surface, pos.x, pos.y + top.height - 1) src_bottom_cursor := top.buf + top.width * (top.height - 1)
dst_top_idx := @inline(indexptr, surface, pos.x, pos.y)
dst_bottom_idx := @inline(indexptr, surface, pos.x, pos.y + top.height - 1)
dst_increment := surface.width
if flip_v {
dst_increment = -dst_increment
tmp := dst_top_idx
dst_top_idx = dst_bottom_idx
dst_bottom_idx = tmp
}
rows_to_copy := top.height rows_to_copy := top.height
top_cursor := top.buf
bottom_cursor := top.buf + top.width * (top.height - 1)
loop if rows_to_copy <= 1 break else { loop if rows_to_copy <= 1 break else {
if flip_v { @inline(memory.copy, Color, src_top_cursor, dst_top_idx, top.width)
@inline(memory.copy, Color, top_cursor, bottom_start_idx, @bitcast(top.width)) @inline(memory.copy, Color, src_bottom_cursor, dst_bottom_idx, top.width)
@inline(memory.copy, Color, bottom_cursor, top_start_idx, @bitcast(top.width))
} else {
@inline(memory.copy, Color, top_cursor, top_start_idx, @bitcast(top.width))
@inline(memory.copy, Color, bottom_cursor, bottom_start_idx, @bitcast(top.width))
}
top_start_idx += surface.width dst_top_idx += dst_increment
bottom_start_idx -= surface.width dst_bottom_idx -= dst_increment
top_cursor += top.width src_top_cursor += top.width
bottom_cursor -= top.width src_bottom_cursor -= top.width
rows_to_copy -= 2 rows_to_copy -= 2
} }
if rows_to_copy == 1 { if rows_to_copy == 1 {
@inline(memory.copy, Color, top_cursor, top_start_idx, @bitcast(top.width)) @inline(memory.copy, Color, src_top_cursor, dst_top_idx, top.width)
} }
return return
@ -260,126 +251,138 @@ put_hline := fn(surface: Surface, y: uint, x0: uint, x1: uint, color: Color): vo
return return
} }
utf8_len_table := [u8].(0, 0, 2, 3)
put_text := fn(surface: Surface, font: Font, pos: Vec2(uint), color: Color, str: ^u8): void { put_text := fn(surface: Surface, font: Font, pos: Vec2(uint), color: Color, str: ^u8): void {
cursor := Vec2(uint).(pos.x, pos.y) cursor := Vec2(uint).(pos.x, pos.y)
current_char := str
loop if *current_char == 0 break else { max_y := surface.height - font.height
glyph_data := memory.dangling(u8) next_line_y := font.height + font.line_gap
char_advance := font.width + font.char_gap
surface_width := surface.width
if font.unicode != null { loop if *str == 0 break else {
code_point := @as(uint, 0) if cursor.y > max_y break
first_byte := *current_char
num_bytes := 1
if (first_byte & 0x80) == 0 { glyph_data := @as(^u8, idk)
code_point = first_byte code_point := @as(uint, 0)
} else if (first_byte & 0xE0) == 0xC0 {
num_bytes = 2 if (*str & 0x80) == 0 {
code_point = first_byte & 0x1F if *str == 10 {
} else if (first_byte & 0xF0) == 0xE0 { cursor.x = pos.x
num_bytes = 3 cursor.y += next_line_y
code_point = first_byte & 0xF str += 1
} else if (first_byte & 0xF8) == 0xF0 {
// handle later
current_char += 1
continue
} else {
current_char += 1
continue continue
} }
if font.unicode == null {
if *str > font.num_glyphs {
str += 1
continue
}
glyph_data = @inline(get_glyph, font, *str)
} else {
if *str < UNC_TABLE_SIZE {
glyph_index := *(font.unicode + *str)
if glyph_index == 0xFFFF {
str += 1
continue
}
glyph_data = font.data + glyph_index * font.bytes_per_glyph
} else {
str += 1
continue
}
}
str += 1
} else if font.unicode != null {
first_byte := *str
num_bytes := @as(uint, 0)
num_bytes = utf8_len_table[first_byte >> 5 & 0x3]
if num_bytes == 0 {
str += 1
continue
}
code_point = first_byte & (0x7F >> num_bytes | 0x1F)
valid_sequence := true valid_sequence := true
i := 1 bytes_processed := 1
loop if i >= num_bytes break else {
current_char += 1 loop if bytes_processed >= num_bytes break else {
// have to check twice due to odd compiler bug...? str += 1
if *current_char == 0 | (*current_char & 0xC0) != 0x80 { if *str == 0 | (*str & 0xC0) != 0x80 {
valid_sequence = false valid_sequence = false
} }
if valid_sequence == false { if valid_sequence == false {
break break
} }
code_point = code_point << 6 | *current_char & 0x3F code_point = code_point << 6 | *str & 0x3F
i += 1 bytes_processed += 1
} }
if valid_sequence == false { if valid_sequence == false {
current_char += 1 str += 1
continue continue
} }
current_char += 1 str += 1
if code_point == 10 { if code_point == 10 {
cursor.x = pos.x cursor.x = pos.x
cursor.y += font.height + font.line_gap cursor.y += next_line_y
continue continue
} }
if code_point < UNC_TABLE_SIZE { if code_point >= UNC_TABLE_SIZE {
glyph_index := *(font.unicode + code_point) continue
}
if glyph_index == 0xFFFF { glyph_index := *(font.unicode + code_point)
continue if glyph_index == 0xFFFF {
}
glyph_data = font.data + glyph_index * font.bytes_per_glyph
} else {
continue continue
} }
} else { glyph_data = font.data + glyph_index * font.bytes_per_glyph
if *current_char > font.num_glyphs {
continue
}
glyph_data = @inline(get_glyph, font, *current_char)
if *current_char == 10 {
cursor.x = pos.x
cursor.y += font.height + font.line_gap
current_char += 1
continue
}
current_char += 1
} }
if cursor.y + font.height > surface.height break if cursor.x + font.width >= surface_width {
if cursor.x % surface.width + font.width >= surface.width {
cursor.x = pos.x cursor.x = pos.x
cursor.y += font.height + font.line_gap cursor.y += next_line_y
} }
dest := @inline(indexptr, surface, cursor.x, cursor.y) dest := @inline(indexptr, surface, cursor.x, cursor.y)
rows_remaining := font.height rows := font.height
loop if rows_remaining == 0 break else { loop if rows == 0 break else {
byte := *glyph_data byte := *glyph_data
pixel_dest := dest pixel_dest := dest
mask := @as(u8, 0x80) mask := @as(u8, 0x80)
bits_remaining := font.width bits := font.width
loop if bits_remaining == 0 break else { loop if bits == 0 break else {
if (byte & mask) != 0 { if (byte & mask) != 0 {
*pixel_dest = color *pixel_dest = color
} }
pixel_dest += 1 pixel_dest += 1
mask >>= 1 mask >>= 1
if mask == 0 & bits_remaining > 0 { if mask == 0 & bits > 0 {
glyph_data += 1 glyph_data += 1
byte = *glyph_data byte = *glyph_data
mask = 0x80 mask = 0x80
} }
bits_remaining -= 1 bits -= 1
} }
if mask != 0x80 { if mask != 0x80 {
glyph_data += 1 glyph_data += 1
} }
dest += surface.width dest += surface_width
rows_remaining -= 1 rows -= 1
} }
cursor.x += font.width + font.char_gap cursor.x += char_advance
} }
return return