Quine shader!

This commit is contained in:
Crispy 2024-08-11 12:24:31 +02:00
parent 35ebc03b6f
commit fb2ee82f67
12 changed files with 694 additions and 69 deletions

1
text_shader/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

124
text_shader/Cargo.lock generated Normal file
View file

@ -0,0 +1,124 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "autocfg"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bytemuck"
version = "1.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "102087e286b4677862ea56cf8fc58bb2cdfa8725c40ffb80fe3a008eb7f2fc83"
[[package]]
name = "byteorder-lite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crc32fast"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
dependencies = [
"cfg-if",
]
[[package]]
name = "fdeflate"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f9bfee30e4dedf0ab8b422f03af778d9612b63f502710fc500a334ebe2de645"
dependencies = [
"simd-adler32",
]
[[package]]
name = "flate2"
version = "1.0.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f211bbe8e69bbd0cfdea405084f128ae8b4aaa6b0b522fc8f2b009084797920"
dependencies = [
"crc32fast",
"miniz_oxide",
]
[[package]]
name = "image"
version = "0.25.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99314c8a2152b8ddb211f924cdae532d8c5e4c8bb54728e12fff1b0cd5963a10"
dependencies = [
"bytemuck",
"byteorder-lite",
"num-traits",
"png",
]
[[package]]
name = "miniz_oxide"
version = "0.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08"
dependencies = [
"adler",
"simd-adler32",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "png"
version = "0.17.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06e4b0d3d1312775e782c86c91a111aa1f910cbb65e1337f9975b5f9a554b5e1"
dependencies = [
"bitflags",
"crc32fast",
"fdeflate",
"flate2",
"miniz_oxide",
]
[[package]]
name = "simd-adler32"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe"
[[package]]
name = "text_shader"
version = "0.1.0"
dependencies = [
"image",
]

9
text_shader/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "text_shader"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
image = { version = "0.25.2", default-features = false, features = ["png"] }

6
text_shader/out.h Normal file

File diff suppressed because one or more lines are too long

79
text_shader/src/main.rs Normal file
View file

@ -0,0 +1,79 @@
use std::{
fs::{read_to_string, File},
io::Write,
};
use image::{GenericImageView, ImageReader};
const FULL_CHARSET: &[u8] = b" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\n\t";
fn main() {
const A:u8 = FULL_CHARSET[64];
// source code conversion
let source_text = read_to_string("../Assets/test/quine.shader").unwrap();
let mut encoded_text: Vec<u32> = Vec::new();
let mut temp = 0;
let mut i = 0;
let mut blob_start = 0;
for (index, char) in source_text.chars().enumerate() {
if char == '?' {
blob_start = index;
}
temp <<= 8;
let char_index = FULL_CHARSET
.iter()
.position(|&c| c == char as u8)
.unwrap_or_else(|| {
println!("char {char:?} not in charset");
0
});
temp |= (char_index as u32) & 0xff;
i += 1;
if i == 4 {
i = 0;
encoded_text.push(temp);
temp = 0;
}
}
let mut out_string = String::new();
out_string += "\t\t\t\tconst uint text[] = {";
for t in &encoded_text {
out_string += &format!("{t:#010x},");
}
out_string += "};\n";
out_string += &format!("\t\t\t\tconst uint text_len = {};\n", encoded_text.len());
out_string += &format!("\t\t\t\tconst uint blob_start = {blob_start};\n");
// font
let img = ImageReader::open("vgafont.png").unwrap().decode().unwrap();
let mut out = Vec::new();
for &c in FULL_CHARSET {
let col = c as u32 & 0b1_1111;
let row = (c as u32 >> 5) & 0b111;
let mut encoded: [u32; 4] = [0; 4];
for section in 0..4 {
for y in 0..4 {
for x in 0..8 {
let px = col * 9 + x;
let py = row * 16 + (16 - 1 - section * 4) - y;
let pixel = (img.get_pixel(px, py).0[0] != 0) as u32;
let offset = x + ((y & 3) << 3);
encoded[section as usize] |= pixel << offset;
}
}
}
out.push(encoded);
}
out_string += "\n\t\t\t\tconst uint font_a[48][4] = {";
for (i, c) in out.iter().enumerate() {
if i == 48 {
out_string += &format!("}};\n\t\t\t\tconst uint font_b[{}][4] = {{", out.len() - 48);
}
out_string += &format!("{{{},{},{},{}}},", c[0], c[1], c[2], c[3]);
}
out_string += "};\n";
let mut file = File::create("out.h").unwrap();
file.write_all(out_string.as_bytes()).unwrap();
}

BIN
text_shader/vgafont.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB