comparison python/src/glide/mod.rs @ 772:7492d384d122 default tip

Rust: Add a Glide renderer (2D only for now) This is an experiment for a Rust renderer, iterating over the Python data using pyo3. It requires --feature=glide to be passed to cargo build, doesn’t support NPOT textures, text rendering, the background, or even msg faces, some of that may come in a future changeset.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 05 Sep 2022 17:53:36 +0200
parents
children
comparison
equal deleted inserted replaced
771:79c3f782dd41 772:7492d384d122
1 use pyo3::prelude::*;
2 use pyo3::types::{PyList, PyDict, PySequence};
3 use pyo3::exceptions::PyTypeError;
4 use std::collections::{HashMap, BTreeMap};
5 use image::GenericImageView;
6
7 mod gr;
8
9 #[inline(always)]
10 fn pixel_to_rgb332(pixel: [u8; 4]) -> [u8; 1] {
11 [(pixel[0] & 0xe0) | ((pixel[1] >> 3) & 0x1c) | (pixel[2] >> 6)]
12 }
13
14 #[inline(always)]
15 fn pixel_to_argb8332(pixel: [u8; 4]) -> [u8; 2] {
16 [(pixel[0] & 0xe0) | ((pixel[1] >> 3) & 0x1c) | (pixel[2] >> 6), pixel[3]]
17 }
18
19 #[inline(always)]
20 fn pixel_to_argb4444(pixel: [u8; 4]) -> [u8; 2] {
21 [(pixel[1] & 0xf0) | (pixel[2] >> 4), (pixel[3] & 0xf0) | (pixel[0] >> 4)]
22 }
23
24 #[inline(always)]
25 fn pixel_to_argb1555(pixel: [u8; 4]) -> [u8; 2] {
26 [((pixel[1] << 2) & 0xe0) | (pixel[2] >> 3), (pixel[3] & 0x80) | ((pixel[0] >> 1) & 0x7c) | (pixel[1] >> 6)]
27 }
28
29 #[inline(always)]
30 fn pixel_to_rgb565(pixel: [u8; 4]) -> [u8; 2] {
31 [((pixel[1] << 3) & 0xe0) | (pixel[2] >> 3), (pixel[0] & 0xf8) | (pixel[1] >> 5)]
32 }
33
34 fn merge_alpha(rgb: &image::DynamicImage, alpha: &image::DynamicImage) -> Vec<u8> {
35 let alpha = match alpha.grayscale() {
36 image::DynamicImage::ImageLuma8(img) => img,
37 foo => panic!("TODO {:?} {:?}", alpha, foo),
38 };
39 rgb
40 .pixels()
41 .zip(alpha.pixels())
42 .map(|((_x, _y, rgb), alpha)| pixel_to_argb4444([rgb[0], rgb[1], rgb[2], alpha[0]]))
43 .flatten()
44 .collect::<Vec<_>>()
45 }
46
47 #[derive(Debug)]
48 struct TextureManager {
49 tmu: u32,
50 next_tex_location: u32,
51 max_tex_location: u32,
52 textures: BTreeMap<u32, gr::TextureFormat>,
53 }
54
55 impl TextureManager {
56 fn new(tmu: u32) -> TextureManager {
57 let next_tex_location = gr::tex_min_address(tmu);
58 let max_tex_location = gr::tex_max_address(tmu);
59 let textures = BTreeMap::new();
60 TextureManager {
61 tmu,
62 next_tex_location,
63 max_tex_location,
64 textures,
65 }
66 }
67
68 fn download(&mut self, tex: &gr::TexInfo) -> PyResult<u32> {
69 let location = self.next_tex_location;
70 let size = gr::tex_calc_mem_required(tex.small_lod, tex.large_lod, tex.aspect, tex.format);
71 if location + size > self.max_tex_location {
72 return Err(PyTypeError::new_err("Out of memory"));
73 }
74 gr::tex_download_mip_map(self.tmu, location, gr::EvenOdd::Both, tex);
75 self.next_tex_location += size;
76 self.textures.insert(location, tex.format);
77 Ok(location)
78 }
79
80 fn get(&self, address: u32) -> gr::TexInfo {
81 if let Some(&format) = self.textures.get(&address) {
82 gr::TexInfo::new(256, 256, format)
83 } else {
84 unreachable!("Not uploaded texture at address 0x{:08x}!", address);
85 }
86 }
87 }
88
89 #[pyclass]
90 struct GameRenderer {
91 #[pyo3(get, set)]
92 size: (u32, u32, u32, u32),
93
94 texture_manager: TextureManager,
95 }
96
97 #[pymethods]
98 impl GameRenderer {
99 #[new]
100 fn new() -> GameRenderer {
101 let size = (0, 0, 0, 0);
102 let texture_manager = TextureManager::new(0);
103 GameRenderer {
104 size,
105 texture_manager,
106 }
107 }
108
109 fn start(&self, common: PyObject) {
110 gr::color_combine_function(gr::ColorCombineFnc::TextureTimesItrgb);
111 gr::alpha_blend_function(gr::Blend::SrcAlpha, gr::Blend::OneMinusSrcAlpha, gr::Blend::One, gr::Blend::Zero);
112 gr::alpha_source(gr::AlphaSource::TextureAlphaTimesIteratedAlpha);
113 gr::tex_combine_function(0, gr::TextureCombineFnc::Decal);
114 }
115
116 fn load_textures(&mut self, py: Python, anms: HashMap<String, Vec<PyObject>>) -> PyResult<()> {
117 for (filename, anm) in anms {
118 for anm in anm {
119 let png_rgb: String = anm.getattr(py, "first_name")?.extract(py)?;
120 let png_alpha: Option<String> = anm.getattr(py, "secondary_name")?.extract(py)?;
121 let (_, png_rgb) = png_rgb.rsplit_once('/').unwrap();
122 use std::path::PathBuf;
123 let texture_address = if let Some(png_alpha) = png_alpha {
124 let (_, png_alpha) = png_alpha.rsplit_once('/').unwrap();
125 //image::load_from_memory_with_format(b"", image::ImageFormat::Png).unwrap();
126 let rgb = image::open(["/", "tmp", "touhou", png_rgb].iter().collect::<PathBuf>()).unwrap();
127 let alpha = image::open(["/", "tmp", "touhou", png_alpha].iter().collect::<PathBuf>()).unwrap();
128 assert_eq!(rgb.dimensions(), alpha.dimensions());
129 let (width, height) = rgb.dimensions();
130 let rgba = merge_alpha(&rgb, &alpha);
131 let tex = gr::TexInfo::with_data(width, height, gr::TextureFormat::Argb4444, &rgba);
132 self.texture_manager.download(&tex)?
133 } else {
134 //image::load_from_memory_with_format(b"", image::ImageFormat::Png).unwrap();
135 let rgb = image::open(["/", "tmp", "touhou", png_rgb].iter().collect::<PathBuf>()).unwrap();
136 let (width, height) = rgb.dimensions();
137 let rgb = rgb.pixels()
138 .map(|(x, y, rgb)| pixel_to_rgb565([rgb[0], rgb[1], rgb[2], 0xff]))
139 .flatten()
140 .collect::<Vec<_>>();
141 let tex = gr::TexInfo::with_data(width, height, gr::TextureFormat::Rgb565, &rgb);
142 self.texture_manager.download(&tex)?
143 };
144 anm.setattr(py, "texture", texture_address)?;
145 let texture: u32 = anm.getattr(py, "texture")?.extract(py)?;
146 }
147 }
148 Ok(())
149 }
150
151 fn load_background(&self, background: PyObject) {
152 println!("TODO: GameRenderer::load_background({background})");
153 }
154
155 fn render_elements(&self, py: Python, elements: &PyList, shift: (f32, f32)) -> PyResult<()> {
156 let module = py.import("pytouhou.ui.glide.sprite")?;
157 let get_sprite_rendering_data = module.getattr("get_sprite_rendering_data")?;
158 let mut prev_texture = u32::MAX;
159 for element in elements.iter() {
160 /*
161 // TODO: only for enemies.
162 let visible: bool = element.getattr("visible")?.extract()?;
163 if !visible {
164 continue;
165 }
166 */
167 let x: f32 = element.getattr("x")?.extract()?;
168 let y: f32 = element.getattr("y")?.extract()?;
169 let sprite = element.getattr("sprite")?;
170 if !sprite.is_none() {
171 let (pos, mut texcoords, color): ([f32; 12], [f32; 4], u32) = get_sprite_rendering_data.call1((sprite,))?.extract()?;
172 for coord in &mut texcoords {
173 *coord *= 256.0;
174 }
175 let anm = sprite.getattr("anm")?;
176 let texture = anm.getattr("texture")?.extract()?;
177 if texture != prev_texture {
178 let tex = self.texture_manager.get(texture);
179 gr::tex_source(0, texture, gr::EvenOdd::Both, &tex);
180 prev_texture = texture;
181 }
182 draw_triangle(x + shift.0, y + shift.1, pos, texcoords, color);
183 }
184 }
185 Ok(())
186 }
187
188 fn render(&self, py: Python, game: PyObject) -> PyResult<()> {
189 gr::buffer_clear(0x000000ff, 0xff, 0xffff);
190 for things in ["enemies", "effects", "players_bullets"/*, "lasers_sprites()"*/, "players"/*, "msg_sprites()"*/, "bullets", "lasers", "cancelled_bullets", "items", "labels"] {
191 let things = game.getattr(py, things)?;
192 let things: &PyList = things.extract(py)?;
193 self.render_elements(py, things, (32.0, 16.0))?;
194 }
195 let interface = game.getattr(py, "interface")?;
196 let boss = game.getattr(py, "boss")?;
197 self.render_interface(py, interface, !boss.is_none(py))?;
198 Ok(())
199 }
200
201 fn render_interface(&self, py: Python, interface: PyObject, boss: bool) -> PyResult<()> {
202 let items = interface.getattr(py, "items")?;
203 let items: &PyList = items.extract(py)?;
204 self.render_elements(py, items, (0.0, 0.0))?;
205 /*
206 // TODO: figure out why this doesn’t render alphanumeric characters.
207 let labels = interface.getattr(py, "labels")?;
208 let labels: &PyDict = labels.extract(py)?;
209 self.render_elements(py, labels.values(), (0.0, 0.0))?;
210 */
211 if boss {
212 let items = interface.getattr(py, "boss_items")?;
213 let items: &PyList = items.extract(py)?;
214 self.render_elements(py, items, (0.0, 0.0))?;
215 }
216 Ok(())
217 }
218 }
219
220 fn draw_triangle(ox: f32, oy: f32, pos: [f32; 12], texcoords: [f32; 4], color: u32) {
221 let a = gr::Vertex::new(ox + pos[0], oy + pos[4], texcoords[0], texcoords[2], color);
222 let b = gr::Vertex::new(ox + pos[1], oy + pos[5], texcoords[1], texcoords[2], color);
223 let c = gr::Vertex::new(ox + pos[2], oy + pos[6], texcoords[1], texcoords[3], color);
224 let d = gr::Vertex::new(ox + pos[3], oy + pos[7], texcoords[0], texcoords[3], color);
225 gr::draw_triangle(&a, &b, &c);
226 gr::draw_triangle(&a, &c, &d);
227 }
228
229 #[pyfunction]
230 fn init(options: HashMap<String, String>) {
231 gr::glide_init();
232 gr::sst_select(0);
233 }
234
235 #[pyfunction]
236 fn shutdown() {
237 gr::glide_shutdown();
238 }
239
240 #[pyfunction]
241 fn create_window(title: &str, posx: u32, posy: u32, width: u32, height: u32, frameskip: u32) {
242 gr::sst_win_open(640, 480, 60);
243 }
244
245 #[pyfunction]
246 fn buffer_swap() {
247 gr::buffer_swap(1);
248 }
249
250 pub fn module(py: Python) -> PyResult<&PyModule> {
251 let m = PyModule::new(py, "glide")?;
252 m.add_class::<GameRenderer>()?;
253 m.add_function(wrap_pyfunction!(init, m)?)?;
254 m.add_function(wrap_pyfunction!(shutdown, m)?)?;
255 m.add_function(wrap_pyfunction!(create_window, m)?)?;
256 m.add_function(wrap_pyfunction!(buffer_swap, m)?)?;
257 Ok(&m)
258 }