Debian Codegraph @

similar/3d/interp.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7
8 /*
9 *
10 * Polygon object interpreter
11 *
12 */
13
14 #include <stdexcept>
15 #include <stdlib.h>
16 #include "dxxsconf.h"
17 #include "dsx-ns.h"
18 #include "dxxerror.h"
19
20 #include "interp.h"
21 #include "console.h"
22 #include "common/3d/globvars.h"
23 #include "polyobj.h"
24 #include "byteutil.h"
25 #include "u_mem.h"
26
27 #include "compiler-range_for.h"
28 #include "d_range.h"
29 #include "d_zip.h"
30 #include "partial_range.h"
31
32 namespace dcx {
33
34 namespace {
35
36 constexpr std::integral_constant<unsigned, 0> OP_EOF{}; //eof
37 constexpr std::integral_constant<unsigned, 1> OP_DEFPOINTS{}; //defpoints
38 constexpr std::integral_constant<unsigned, 2> OP_FLATPOLY{}; //flat-shaded polygon
39 constexpr std::integral_constant<unsigned, 3> OP_TMAPPOLY{}; //texture-mapped polygon
40 constexpr std::integral_constant<unsigned, 4> OP_SORTNORM{}; //sort by normal
41 constexpr std::integral_constant<unsigned, 5> OP_RODBM{}; //rod bitmap
42 constexpr std::integral_constant<unsigned, 6> OP_SUBCALL{}; //call a subobject
43 constexpr std::integral_constant<unsigned, 7> OP_DEFP_START{}; //defpoints with start
44 constexpr std::integral_constant<unsigned, 8> OP_GLOW{}; //glow value for next poly
45
46 static inline int16_t *wp(uint8_t *p)
47 {
48 return reinterpret_cast<int16_t *>(p);
49 }
50
51 static inline const int16_t *wp(const uint8_t *p)
52 {
53 return reinterpret_cast<const int16_t *>(p);
54 }
55
56 static inline const vms_vector *vp(const uint8_t *p)
57 {
58 return reinterpret_cast<const vms_vector *>(p);
59 }
60
61 static inline int16_t w(const uint8_t *p)
62 {
63 return *wp(p);
64 }
65
66 constexpr vms_angvec zero_angles = {0,0,0};
67
68 class interpreter_ignore_op_defpoints
69 {
70 public:
71 static void op_defpoints(const uint8_t *, uint16_t)
72 {
73 }
74 };
75
76 class interpreter_ignore_op_defp_start
77 {
78 public:
79 static void op_defp_start(const uint8_t *, uint16_t)
80 {
81 }
82 };
83
84 class interpreter_ignore_op_flatpoly
85 {
86 public:
87 static void op_flatpoly(const uint8_t *, uint16_t)
88 {
89 }
90 };
91
92 class interpreter_ignore_op_tmappoly
93 {
94 public:
95 static void op_tmappoly(const uint8_t *, uint16_t)
96 {
97 }
98 };
99
100 class interpreter_ignore_op_rodbm
101 {
102 public:
103 static void op_rodbm(const uint8_t *)
104 {
105 }
106 };
107
108 class interpreter_ignore_op_glow
109 {
110 public:
111 static void op_glow(const uint8_t *)
112 {
113 }
114 };
115
116 }
117
118 }
119
120 class interpreter_track_model_extent
121 {
122 protected:
123 const std::span<const uint8_t> model;
124 public:
125 constexpr interpreter_track_model_extent(const std::span<const uint8_t> model) :
126 model(model)
127 {
128 }
129 uint8_t truncate_invalid_model(const unsigned line, uint8_t *const p, const std::ptrdiff_t d, const std::size_t size) const
130 {
131 const std::ptrdiff_t offset_from_base = p - model.data();
132 const std::ptrdiff_t offset_of_value = offset_from_base + d;
133 if (const auto model_length = model.size(); offset_of_value >= model_length || offset_of_value + size >= model_length)
134 {
135 auto &opref = *wp(p);
136 const auto badop{opref};
137 opref = OP_EOF;
138 const unsigned long uml = model_length;
139 const long ofb = offset_from_base;
140 const long oov = offset_of_value;
141 con_printf(CON_URGENT, "%s:%u: warning: invalid polymodel at %p with length %lu; opcode %u at offset %li references invalid offset %li; replacing invalid operation with EOF", __FILE__, line, model.data(), uml, badop, ofb, oov);
142 return 1;
143 }
144 return 0;
145 }
146 };
147
148 class interpreter_base
149 {
150 public:
151 static uint16_t get_raw_opcode(const uint8_t *const p)
152 {
153 return w(p);
154 }
155 static uint_fast32_t translate_opcode(const uint8_t *, const uint16_t op)
156 {
157 return op;
158 }
159 static uint16_t get_op_subcount(const uint8_t *const p)
160 {
161 return w(p + 2);
162 }
163 [[noreturn]]
164 static void op_default(const unsigned op, const uint8_t *const p)
165 {
166 char buf[64];
167 snprintf(buf, sizeof(buf), "invalid polygon model opcode %u at %p", op, p);
168 throw std::runtime_error(buf);
169 }
170 };
171
172 namespace dsx {
173
174 namespace {
175
176 static int16_t init_model_sub(uint8_t *model_sub_ptr, const std::span<const uint8_t> model_base);
177 #if DXX_BUILD_DESCENT == 1
178 static void validate_model_sub(uint8_t *model_sub_ptr, const std::span<const uint8_t> model_base);
179 #endif
180
181 class g3_poly_get_color_state :
182 public interpreter_ignore_op_defpoints,
183 public interpreter_ignore_op_defp_start,
184 public interpreter_ignore_op_tmappoly,
185 public interpreter_ignore_op_rodbm,
186 public interpreter_ignore_op_glow,
187 public interpreter_base
188 {
189 public:
190 int color{0};
191 void op_flatpoly(const uint8_t *const p, const uint_fast32_t nv)
192 {
193 if (nv > MAX_POINTS_PER_POLY)
194 return;
195 if (g3_check_normal_facing(*vp(p+4),*vp(p+16)) > 0) {
196 #if DXX_BUILD_DESCENT == 1
197 color = (w(p+28));
198 #elif DXX_BUILD_DESCENT == 2
199 color = gr_find_closest_color_15bpp(packed_color_r5g5b5{w(p + 28)});
200 #endif
201 }
202 }
203 void op_sortnorm(const uint8_t *const p)
204 {
205 const bool facing = g3_check_normal_facing(*vp(p+16),*vp(p+4)) > 0;
206 color = g3_poly_get_color(facing ? p + w(p + 28) : p + w(p + 30));
207 }
208 void op_subcall(const uint8_t *const p)
209 {
210 #if DXX_BUILD_DESCENT == 1
211 color = g3_poly_get_color(p+w(p+16));
212 #elif DXX_BUILD_DESCENT == 2
213 (void)p;
214 #endif
215 }
216 };
217
218 class g3_interpreter_draw_base
219 {
220 protected:
221 grs_bitmap *const *const model_bitmaps;
222 polygon_model_points &Interp_point_list;
223 grs_canvas &canvas;
224 const tmap_drawer_type tmap_drawer_ptr;
225 const submodel_angles anim_angles;
226 const g3s_lrgb model_light;
227 private:
228 void rotate(uint_fast32_t i, const vms_vector *const src, const uint_fast32_t n)
229 {
230 for (auto &&[dest, src] : zip(partial_range(Interp_point_list, i, i + n), unchecked_partial_range(src, n)))
231 g3_rotate_point(dest, src);
232 }
233 void set_color_by_model_light(fix g3s_lrgb::*const c, g3s_lrgb &o, const fix color) const
234 {
235 o.*c = fixmul(color, model_light.*c);
236 }
237 protected:
238 template <std::size_t N>
239 std::array<g3_draw_tmap_point *, N> prepare_point_list(const uint_fast32_t nv, const uint8_t *const p)
240 {
241 std::array<g3_draw_tmap_point *, N> point_list;
242 for (uint_fast32_t i = 0; i < nv; ++i)
243 point_list[i] = &Interp_point_list[wp(p + 30)[i]];
244 return point_list;
245 }
246 g3s_lrgb get_noglow_light(const uint8_t *const p) const
247 {
248 g3s_lrgb light;
249 const auto negdot = -vm_vec_build_dot(View_matrix.fvec, *vp(p + 16));
250 const auto color = (f1_0 / 4) + ((negdot * 3) / 4);
251 set_color_by_model_light(&g3s_lrgb::r, light, color);
252 set_color_by_model_light(&g3s_lrgb::g, light, color);
253 set_color_by_model_light(&g3s_lrgb::b, light, color);
254 return light;
255 }
256 g3_interpreter_draw_base(grs_bitmap *const *const mbitmaps, polygon_model_points &plist, grs_canvas &ccanvas, const tmap_drawer_type tmap_drawer_ptr, const submodel_angles aangles, const g3s_lrgb &mlight) :
257 model_bitmaps(mbitmaps), Interp_point_list(plist),
258 canvas(ccanvas),
259 tmap_drawer_ptr{tmap_drawer_ptr},
260 anim_angles(aangles), model_light(mlight)
261 {
262 }
263 void op_defpoints(const vms_vector *const src, const uint_fast32_t n)
264 {
265 rotate(0, src, n);
266 }
267 void op_defp_start(const uint8_t *const p, const vms_vector *const src, const uint_fast32_t n)
268 {
269 rotate(static_cast<int>(w(p + 4)), src, n);
270 }
271 static std::pair<uint16_t, uint16_t> get_sortnorm_offsets(const uint8_t *const p)
272 {
273 const uint16_t a = w(p + 30), b = w(p + 28);
274 return (g3_check_normal_facing(*vp(p + 16), *vp(p + 4)) > 0)
275 ? std::make_pair(a, b) //draw back then front
276 : std::make_pair(b, a) //not facing. draw front then back
277 ;
278 }
279 void op_rodbm(const uint8_t *const p)
280 {
281 const g3_instance_context viewer_context{View_matrix, View_position};
282 g3_draw_rod_tmap(canvas, *model_bitmaps[w(p + 2)], /* bot_point = */ g3_rotated_point{viewer_context, *vp(p + 20)}, w(p + 16), /* top_point = */ g3_rotated_point{viewer_context, *vp(p + 4)}, w(p + 32), g3s_lrgb{F1_0, F1_0, F1_0}, tmap_drawer_ptr);
283 }
284 void op_subcall(const uint8_t *const p, const glow_values_t *const glow_values)
285 {
286 auto &&ctx = g3_start_instance_angles(*vp(p + 4), anim_angles ? anim_angles[w(p + 2)] : zero_angles);
287 g3_draw_polygon_model(model_bitmaps, Interp_point_list, canvas, tmap_drawer_ptr, anim_angles, model_light, glow_values, p + w(p + 16));
288 g3_done_instance(ctx);
289 }
290 };
291
292 class g3_draw_polygon_model_state :
293 public interpreter_base,
294 g3_interpreter_draw_base
295 {
296 const glow_values_t *const glow_values;
297 unsigned glow_num = ~0u; //glow off by default
298 public:
299 g3_draw_polygon_model_state(grs_bitmap *const *const mbitmaps, polygon_model_points &plist, grs_canvas &ccanvas, const tmap_drawer_type tmap_drawer_ptr, const submodel_angles aangles, const g3s_lrgb &mlight, const glow_values_t *const glvalues) :
300 g3_interpreter_draw_base{mbitmaps, plist, ccanvas, tmap_drawer_ptr, aangles, mlight},
301 glow_values(glvalues)
302 {
303 }
304 void op_defpoints(const uint8_t *const p, const uint_fast32_t n)
305 {
306 g3_interpreter_draw_base::op_defpoints(vp(p + 4), n);
307 }
308 void op_defp_start(const uint8_t *const p, const uint_fast32_t n)
309 {
310 g3_interpreter_draw_base::op_defp_start(p, vp(p + 8), n);
311 }
312 void op_flatpoly(const uint8_t *const p, const uint_fast32_t nv)
313 {
314 if (nv > MAX_POINTS_PER_POLY)
315 return;
316 #if DXX_BUILD_DESCENT == 2
317 fix effective_glow_value;
318 if (glow_values && glow_num < glow_values->size())
319 {
320 effective_glow_value = (*glow_values)[glow_num];
321 if (effective_glow_value == -3)
322 return;
323 }
324 else
325 effective_glow_value = 0;
326 #endif
327 if (g3_check_normal_facing(*vp(p+4),*vp(p+16)) > 0)
328 {
329 #if DXX_BUILD_DESCENT == 1
330 const uint8_t color = w(p + 28);
331 #elif DXX_BUILD_DESCENT == 2
332 // DPH: Now we treat this color as 15bpp
333 const uint8_t color = effective_glow_value == -2
334 ? 255
335 : gr_find_closest_color_15bpp(packed_color_r5g5b5{w(p + 28)});
336 #endif
337 g3_draw_poly(canvas, nv, prepare_point_list<MAX_POINTS_PER_POLY>(nv, p), color);
338 }
339 }
340 static g3s_lrgb get_glow_light(const fix c)
341 {
342 return {c, c, c};
343 }
344 void op_tmappoly(const uint8_t *const p, const uint_fast32_t nv)
345 {
346 if (nv > MAX_POINTS_PER_POLY)
347 return;
348 if (!(g3_check_normal_facing(*vp(p+4),*vp(p+16)) > 0))
349 return;
350 //calculate light from surface normal
351 const auto &&light = (glow_values && glow_num < glow_values->size())
352 ? get_glow_light((*glow_values)[std::exchange(glow_num, -1)]) //yes glow
353 : get_noglow_light(p); //no glow
354 //now poke light into l values
355 std::array<g3s_uvl, MAX_POINTS_PER_POLY> uvl_list;
356 std::array<g3s_lrgb, MAX_POINTS_PER_POLY> lrgb_list;
357 const fix average_light = (light.r + light.g + light.b) / 3;
358 range_for (const uint_fast32_t i, xrange(nv))
359 {
360 lrgb_list[i] = light;
361 uvl_list[i] = (reinterpret_cast<const g3s_uvl *>(p+30+((nv&~1)+1)*2))[i];
362 uvl_list[i].l = average_light;
363 }
364 g3_draw_tmap(canvas, nv, prepare_point_list<MAX_POINTS_PER_POLY>(nv, p), uvl_list, lrgb_list, *model_bitmaps[w(p + 28)], tmap_drawer_ptr);
365 }
366 void op_sortnorm(const uint8_t *const p)
367 {
368 const auto &&offsets = get_sortnorm_offsets(p);
369 const auto a{offsets.first};
370 const auto b{offsets.second};
371 g3_draw_polygon_model(model_bitmaps, Interp_point_list, canvas, tmap_drawer_ptr, anim_angles, model_light, glow_values, p + a);
372 g3_draw_polygon_model(model_bitmaps, Interp_point_list, canvas, tmap_drawer_ptr, anim_angles, model_light, glow_values, p + b);
373 }
374 using g3_interpreter_draw_base::op_rodbm;
375 void op_subcall(const uint8_t *const p)
376 {
377 g3_interpreter_draw_base::op_subcall(p, glow_values);
378 }
379 void op_glow(const uint8_t *const p)
380 {
381 glow_num = w(p+2);
382 }
383 };
384
385 class g3_draw_morphing_model_state :
386 public interpreter_ignore_op_glow,
387 public interpreter_base,
388 g3_interpreter_draw_base
389 {
390 const vms_vector *const new_points;
391 static constexpr const glow_values_t *glow_values = nullptr;
392 public:
393 g3_draw_morphing_model_state(grs_bitmap *const *const mbitmaps, polygon_model_points &plist, grs_canvas &ccanvas, const tmap_drawer_type tmap_drawer_ptr, const submodel_angles aangles, const g3s_lrgb &mlight, const vms_vector *const npoints) :
394 g3_interpreter_draw_base{mbitmaps, plist, ccanvas, tmap_drawer_ptr, aangles, mlight},
395 new_points(npoints)
396 {
397 }
398 void op_defpoints(const uint8_t *, const uint_fast32_t n)
399 {
400 g3_interpreter_draw_base::op_defpoints(new_points, n);
401 }
402 void op_defp_start(const uint8_t *const p, const uint_fast32_t n)
403 {
404 g3_interpreter_draw_base::op_defp_start(p, new_points, n);
405 }
406 void op_flatpoly(const uint8_t *const p, const uint_fast32_t nv)
407 {
408 int ntris;
409 const uint8_t color = w(p+28);
410 unsigned i;
411 auto point_list = prepare_point_list<3>(i = 2, p);
412 for (ntris=nv-2;ntris;ntris--) {
413 point_list[2] = &Interp_point_list[wp(p+30)[i++]];
414 g3_check_and_draw_poly(canvas, point_list, color);
415 point_list[1] = point_list[2];
416 }
417 }
418 void op_tmappoly(const uint8_t *const p, const uint_fast32_t nv)
419 {
420 if (nv > MAX_POINTS_PER_POLY)
421 return;
422 std::array<g3s_uvl, MAX_POINTS_PER_POLY> uvl_list;
423 std::array<g3s_lrgb, MAX_POINTS_PER_POLY> lrgb_list;
424 lrgb_list.fill(get_noglow_light(p));
425 range_for (const uint_fast32_t i, xrange(nv))
426 uvl_list[i] = (reinterpret_cast<const g3s_uvl *>(p+30+((nv&~1)+1)*2))[i];
427 g3_draw_tmap(canvas, nv, prepare_point_list<MAX_POINTS_PER_POLY>(nv, p), uvl_list, lrgb_list, *model_bitmaps[w(p + 28)], tmap_drawer_ptr);
428 }
429 void op_sortnorm(const uint8_t *const p)
430 {
431 const auto &&offsets = get_sortnorm_offsets(p);
432 auto &a = offsets.first;
433 auto &b = offsets.second;
434 g3_draw_morphing_model(canvas, tmap_drawer_ptr, p + a, model_bitmaps, anim_angles, model_light, new_points, Interp_point_list);
435 g3_draw_morphing_model(canvas, tmap_drawer_ptr, p + b, model_bitmaps, anim_angles, model_light, new_points, Interp_point_list);
436 }
437 using g3_interpreter_draw_base::op_rodbm;
438 void op_subcall(const uint8_t *const p)
439 {
440 g3_interpreter_draw_base::op_subcall(p, glow_values);
441 }
442 };
443
444 template <typename T>
445 class model_load_state :
446 public interpreter_track_model_extent,
447 public interpreter_ignore_op_defpoints,
448 public interpreter_ignore_op_defp_start,
449 public interpreter_ignore_op_rodbm,
450 public interpreter_ignore_op_glow,
451 public interpreter_base
452 {
453 public:
454 using interpreter_track_model_extent::interpreter_track_model_extent;
455 int16_t init_bounded_model_sub(const unsigned line, uint8_t *const p, const std::ptrdiff_t d) const
456 {
457 if (truncate_invalid_model(line, p, d, sizeof(uint16_t)))
458 return 0;
459 return static_cast<const T *>(this)->init_sub_model(p + d);
460 }
461 void op_tmappoly(uint8_t *const p, const uint_fast32_t nv)
462 {
463 constexpr unsigned offset_texture = 28;
464 (void)nv;
465 Assert(nv > 2); //must have 3 or more points
466 if (truncate_invalid_model(__LINE__, p, offset_texture, sizeof(uint16_t)))
467 return;
468 static_cast<T *>(this)->update_texture(w(p + offset_texture));
469 }
470 void op_sortnorm(uint8_t *const p)
471 {
472 constexpr unsigned offset_submodel0 = 28;
473 constexpr unsigned offset_submodel1 = offset_submodel0 + sizeof(uint16_t);
474 if (truncate_invalid_model(__LINE__, p, offset_submodel1, sizeof(uint16_t)))
475 return;
476 const auto n0 = w(p + offset_submodel0);
477 const auto h0 = init_bounded_model_sub(__LINE__, p, n0);
478 const auto n1 = w(p + offset_submodel1);
479 const auto h1 = init_bounded_model_sub(__LINE__, p, n1);
480 const auto hm = std::max(h0, h1);
481 static_cast<T *>(this)->update_texture(hm);
482 }
483 void op_subcall(uint8_t *const p)
484 {
485 constexpr unsigned offset_displacement = 16;
486 if (truncate_invalid_model(__LINE__, p, offset_displacement, sizeof(uint16_t)))
487 return;
488 const auto n0 = w(p + offset_displacement);
489 const auto h0 = init_bounded_model_sub(__LINE__, p, n0);
490 static_cast<T *>(this)->update_texture(h0);
491 }
492 };
493
494 class init_model_sub_state :
495 public model_load_state<init_model_sub_state>
496 #if DXX_BUILD_DESCENT == 2
497 , public interpreter_ignore_op_flatpoly
498 #endif
499 {
500 public:
501 int16_t highest_texture_num = -1;
502 using model_load_state::model_load_state;
503 int16_t init_sub_model(uint8_t *const p) const
504 {
505 return init_model_sub(p, model);
506 }
507 void update_texture(const int16_t t)
508 {
509 if (highest_texture_num < t)
510 highest_texture_num = t;
511 }
512 #if DXX_BUILD_DESCENT == 1
513 void op_flatpoly(uint8_t *const p, const uint_fast32_t nv) const
514 {
515 //must have 3 or more points
516 if (nv <= 2)
517 return;
518 const auto p16 = wp(p + 28);
519 *p16 = static_cast<short>(gr_find_closest_color_15bpp(packed_color_r5g5b5{*p16}));
520 }
521 #endif
522 };
523
524 #if DXX_BUILD_DESCENT == 1
525 class validate_model_sub_state :
526 public model_load_state<validate_model_sub_state>,
527 public interpreter_ignore_op_flatpoly
528 {
529 public:
530 using model_load_state::model_load_state;
531 unsigned init_sub_model(uint8_t *const p) const
532 {
533 validate_model_sub(p, model);
534 return 0;
535 }
536 void update_texture(int16_t)
537 {
538 }
539 };
540 #endif
541
542 template <typename State>
543 [[nodiscard]]
544 static std::size_t dispatch_polymodel_op(const auto p, State &state, const uint_fast32_t op)
545 {
546 switch (op)
547 {
548 case OP_DEFPOINTS: {
549 const auto n = state.get_op_subcount(p);
550 const std::size_t record_size = n * sizeof(vms_vector) + 4;
551 state.op_defpoints(p, n);
552 return record_size;
553 }
554 case OP_DEFP_START: {
555 const auto n = state.get_op_subcount(p);
556 const std::size_t record_size = n * sizeof(vms_vector) + 8;
557 state.op_defp_start(p, n);
558 return record_size;
559 }
560 case OP_FLATPOLY: {
561 const auto n = state.get_op_subcount(p);
562 const std::size_t record_size = 30 + ((n & ~1) + 1) * 2;
563 state.op_flatpoly(p, n);
564 return record_size;
565 }
566 case OP_TMAPPOLY: {
567 const auto n = state.get_op_subcount(p);
568 const std::size_t record_size = 30 + ((n & ~1) + 1) * 2 + n * 12;
569 state.op_tmappoly(p, n);
570 return record_size;
571 }
572 case OP_SORTNORM: {
573 constexpr std::size_t record_size{32};
574 state.op_sortnorm(p);
575 return record_size;
576 }
577 case OP_RODBM: {
578 constexpr std::size_t record_size{36};
579 state.op_rodbm(p);
580 return record_size;
581 }
582 case OP_SUBCALL: {
583 constexpr std::size_t record_size{20};
584 state.op_subcall(p);
585 return record_size;
586 }
587 case OP_GLOW: {
588 constexpr std::size_t record_size{4};
589 state.op_glow(p);
590 return record_size;
591 }
592 default:
593 state.op_default(op, p);
594 }
595 }
596
597 static auto iterate_polymodel(auto p, auto &state)
598 {
599 for (uint16_t op; (op = state.get_raw_opcode(p)) != OP_EOF;)
600 p += dispatch_polymodel_op(p, state, state.translate_opcode(p, op));
601 return p;
602 }
603
604 }
605
606 }
607
608 #if DXX_WORDS_BIGENDIAN
609 namespace dcx {
610
611 namespace {
612
613 static inline fix *fp(uint8_t *p)
614 {
615 return reinterpret_cast<fix *>(p);
616 }
617
618 static inline vms_vector *vp(uint8_t *p)
619 {
620 return reinterpret_cast<vms_vector *>(p);
621 }
622
623 static void short_swap(short *s)
624 {
625 *s = SWAPSHORT(*s);
626 }
627
628 static void fix_swap(fix &f)
629 {
630 f = SWAPINT(f);
631 }
632
633 static void fix_swap(fix *f)
634 {
635 fix_swap(*f);
636 }
637
638 static void vms_vector_swap(vms_vector &v)
639 {
640 fix_swap(v.x);
641 fix_swap(v.y);
642 fix_swap(v.z);
643 }
644
645 class swap_polygon_model_data_state : public interpreter_base
646 {
647 public:
648 static uint_fast32_t translate_opcode(uint8_t *const p, const uint16_t op)
649 {
650 return *wp(p) = INTEL_SHORT(op);
651 }
652 static uint16_t get_op_subcount(const uint8_t *const p)
653 {
654 return SWAPSHORT(w(p + 2));
655 }
656 static void op_defpoints(uint8_t *const p, const uint_fast32_t n)
657 {
658 *wp(p + 2) = n;
659 range_for (const uint_fast32_t i, xrange(n))
660 vms_vector_swap(*vp((p + 4) + (i * sizeof(vms_vector))));
661 }
662 static void op_defp_start(uint8_t *const p, const uint_fast32_t n)
663 {
664 *wp(p + 2) = n;
665 short_swap(wp(p + 4));
666 range_for (const uint_fast32_t i, xrange(n))
667 vms_vector_swap(*vp((p + 8) + (i * sizeof(vms_vector))));
668 }
669 static void op_flatpoly(uint8_t *const p, const uint_fast32_t n)
670 {
671 *wp(p + 2) = n;
672 vms_vector_swap(*vp(p + 4));
673 vms_vector_swap(*vp(p + 16));
674 short_swap(wp(p+28));
675 for (uint_fast32_t i = 0; i < n; ++i)
676 short_swap(wp(p + 30 + (i * 2)));
677 }
678 static void op_tmappoly(uint8_t *const p, const uint_fast32_t n)
679 {
680 *wp(p + 2) = n;
681 vms_vector_swap(*vp(p + 4));
682 vms_vector_swap(*vp(p + 16));
683 range_for (const uint_fast32_t i, xrange(n)) {
684 const auto uvl_val = reinterpret_cast<g3s_uvl *>((p+30+((n&~1)+1)*2) + (i * sizeof(g3s_uvl)));
685 fix_swap(&uvl_val->u);
686 fix_swap(&uvl_val->v);
687 }
688 short_swap(wp(p+28));
689 range_for (const uint_fast32_t i, xrange(n))
690 short_swap(wp(p + 30 + (i * 2)));
691 }
692 void op_sortnorm(uint8_t *const p)
693 {
694 vms_vector_swap(*vp(p + 4));
695 vms_vector_swap(*vp(p + 16));
696 short_swap(wp(p + 28));
697 short_swap(wp(p + 30));
698 swap_polygon_model_data(p + w(p+28));
699 swap_polygon_model_data(p + w(p+30));
700 }
701 static void op_rodbm(uint8_t *const p)
702 {
703 vms_vector_swap(*vp(p + 20));
704 vms_vector_swap(*vp(p + 4));
705 short_swap(wp(p+2));
706 fix_swap(fp(p + 16));
707 fix_swap(fp(p + 32));
708 }
709 void op_subcall(uint8_t *const p)
710 {
711 short_swap(wp(p+2));
712 vms_vector_swap(*vp(p+4));
713 short_swap(wp(p+16));
714 swap_polygon_model_data(p + w(p+16));
715 }
716 static void op_glow(uint8_t *const p)
717 {
718 short_swap(wp(p + 2));
719 }
720 };
721
722 }
723
724 void swap_polygon_model_data(uint8_t *const data)
725 {
726 swap_polygon_model_data_state state;
727 iterate_polymodel(data, state);
728 }
729
730 }
731 #endif
732
733 #if DXX_WORDS_NEED_ALIGNMENT
734
735 #define MAX_CHUNKS 100 // increase if insufficent
736 namespace dcx {
737
738 namespace {
739
740 /*
741 * A chunk struct (as used for alignment) contains all relevant data
742 * concerning a piece of data that may need to be aligned.
743 * To align it, we need to copy it to an aligned position,
744 * and update all pointers to it.
745 * (Those pointers are actually offsets
746 * relative to start of model_data) to it.
747 */
748 struct chunk
749 {
750 const uint8_t *old_base; // where the offset sets off from (relative to beginning of model_data)
751 uint8_t *new_base; // where the base is in the aligned structure
752 short offset; // how much to add to base to get the address of the offset
753 short correction; // how much the value of the offset must be shifted for alignment
754 };
755
756 static void add_chunk(const uint8_t *old_base, uint8_t *new_base, int offset,
757 chunk *chunk_list, int *no_chunks)
758 {
759 Assert(*no_chunks + 1 < MAX_CHUNKS); //increase MAX_CHUNKS if you get this
760 chunk_list[*no_chunks].old_base = old_base;
761 chunk_list[*no_chunks].new_base = new_base;
762 chunk_list[*no_chunks].offset = offset;
763 chunk_list[*no_chunks].correction = 0;
764 (*no_chunks)++;
765 }
766
767 class get_chunks_state :
768 public interpreter_ignore_op_defpoints,
769 public interpreter_ignore_op_defp_start,
770 public interpreter_ignore_op_flatpoly,
771 public interpreter_ignore_op_tmappoly,
772 public interpreter_ignore_op_rodbm,
773 public interpreter_ignore_op_glow,
774 public interpreter_base
775 {
776 const uint8_t *const data;
777 uint8_t *const new_data;
778 chunk *const list;
779 int *const no;
780 public:
781 get_chunks_state(const uint8_t *const p, uint8_t *const ndata, chunk *const l, int *const n) :
782 data(p), new_data(ndata), list(l), no(n)
783 {
784 }
785 static uint_fast32_t translate_opcode(const uint8_t *, const uint16_t op)
786 {
787 return INTEL_SHORT(op);
788 }
789 static uint16_t get_op_subcount(const uint8_t *const p)
790 {
791 return GET_INTEL_SHORT(p + 2);
792 }
793 void op_sortnorm(const uint8_t *const p)
794 {
795 add_chunk(p, p - data + new_data, 28, list, no);
796 add_chunk(p, p - data + new_data, 30, list, no);
797 }
798 void op_subcall(const uint8_t *const p)
799 {
800 add_chunk(p, p - data + new_data, 16, list, no);
801 }
802 };
803
804 /*
805 * finds what chunks the data points to, adds them to the chunk_list,
806 * and returns the length of the current chunk
807 */
808 int get_chunks(const uint8_t *data, uint8_t *new_data, chunk *list, int *no)
809 {
810 get_chunks_state state(data, new_data, list, no);
811 auto p = iterate_polymodel(data, state);
812 return p + 2 - data;
813 }
814
815 static const uint8_t *old_dest(const chunk &o) // return where chunk is (in unaligned struct)
816 {
817 return GET_INTEL_SHORT(&o.old_base[o.offset]) + o.old_base;
818 }
819 static uint8_t *new_dest(const chunk &o) // return where chunk is (in aligned struct)
820 {
821 return GET_INTEL_SHORT(&o.old_base[o.offset]) + o.new_base + o.correction;
822 }
823
824 /*
825 * find chunk with smallest address
826 */
827 static int get_first_chunks_index(chunk *chunk_list, int no_chunks)
828 {
829 int first_index{0};
830 Assert(no_chunks >= 1);
831 for (int i = 1; i < no_chunks; i++)
832 if (old_dest(chunk_list[i]) < old_dest(chunk_list[first_index]))
833 first_index = i;
834 return first_index;
835 }
836
837 }
838
839 void align_polygon_model_data(polymodel *pm)
840 {
841 int chunk_len;
842 int total_correction{0};
843 chunk cur_ch;
844 chunk ch_list[MAX_CHUNKS];
845 int no_chunks{0};
846 constexpr unsigned SHIFT_SPACE = 500; // increase if insufficient
847 int tmp_size = pm->model_data_size + SHIFT_SPACE;
848 // where we build the aligned version of pm->model_data
849 const auto tmp = std::make_unique<uint8_t[]>(tmp_size);
850
851 //start with first chunk (is always aligned!)
852 const uint8_t *cur_old = pm->model_data.get();
853 auto cur_new = tmp.get();
854 chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
855 memcpy(cur_new, cur_old, chunk_len);
856 while (no_chunks > 0) {
857 int first_index = get_first_chunks_index(ch_list, no_chunks);
858 cur_ch = ch_list[first_index];
859 // remove first chunk from array:
860 no_chunks--;
861 for (int i = first_index; i < no_chunks; i++)
862 ch_list[i] = ch_list[i + 1];
863 // if (new) address unaligned:
864 const uintptr_t u = reinterpret_cast<uintptr_t>(new_dest(cur_ch));
865 if (u % 4L != 0) {
866 // calculate how much to move to be aligned
867 short to_shift = 4 - u % 4L;
868 // correct chunks' addresses
869 cur_ch.correction += to_shift;
870 for (int i = 0; i < no_chunks; i++)
871 ch_list[i].correction += to_shift;
872 total_correction += to_shift;
873 Assert(reinterpret_cast<uintptr_t>(new_dest(cur_ch)) % 4L == 0);
874 Assert(total_correction <= SHIFT_SPACE); // if you get this, increase SHIFT_SPACE
875 }
876 //write (corrected) chunk for current chunk:
877 *(reinterpret_cast<short *>(cur_ch.new_base + cur_ch.offset))
878 = INTEL_SHORT(static_cast<short>(cur_ch.correction + GET_INTEL_SHORT(cur_ch.old_base + cur_ch.offset)));
879 //write (correctly aligned) chunk:
880 cur_old = old_dest(cur_ch);
881 cur_new = new_dest(cur_ch);
882 chunk_len = get_chunks(cur_old, cur_new, ch_list, &no_chunks);
883 memcpy(cur_new, cur_old, chunk_len);
884 //correct submodel_ptr's for pm, too
885 for (auto &sp : pm->submodel_ptrs)
886 if (&pm->model_data[sp] >= cur_old &&
887 &pm->model_data[sp] < cur_old + chunk_len)
888 sp += (cur_new - tmp.get()) - (cur_old - pm->model_data.get());
889 }
890 pm->model_data_size += total_correction;
891 pm->model_data = std::make_unique<uint8_t[]>(pm->model_data_size);
892 memcpy(pm->model_data.get(), tmp.get(), pm->model_data_size);
893 }
894
895 }
896 #endif //def WORDS_NEED_ALIGNMENT
897
898 namespace dsx {
899
900 // check a polymodel for it's color and return it
901 int g3_poly_get_color(const uint8_t *p)
902 {
903 g3_poly_get_color_state state;
904 iterate_polymodel(p, state);
905 return state.color;
906 }
907
908 //calls the object interpreter to render an object. The object renderer
909 //is really a separate pipeline.
910 void g3_draw_polygon_model(grs_bitmap *const *const model_bitmaps, polygon_model_points &Interp_point_list, grs_canvas &canvas, const tmap_drawer_type tmap_drawer_ptr, const submodel_angles anim_angles, const g3s_lrgb model_light, const glow_values_t *const glow_values, const uint8_t *const p)
911 {
912 g3_draw_polygon_model_state state(model_bitmaps, Interp_point_list, canvas, tmap_drawer_ptr, anim_angles, model_light, glow_values);
913 iterate_polymodel(p, state);
914 }
915
916 #ifndef NDEBUG
917 static int nest_count;
918 #endif
919
920 //alternate interpreter for morphing object
921 void g3_draw_morphing_model(grs_canvas &canvas, const tmap_drawer_type tmap_drawer_ptr, const uint8_t *const p, grs_bitmap *const *const model_bitmaps, const submodel_angles anim_angles, const g3s_lrgb model_light, const vms_vector *new_points, polygon_model_points &Interp_point_list)
922 {
923 g3_draw_morphing_model_state state(model_bitmaps, Interp_point_list, canvas, tmap_drawer_ptr, anim_angles, model_light, new_points);
924 iterate_polymodel(p, state);
925 }
926
927 namespace {
928
929 static int16_t init_model_sub(uint8_t *const model_sub_ptr, const std::span<const uint8_t> model_base)
930 {
931 init_model_sub_state state(model_base);
932 Assert(++nest_count < 1000);
933 iterate_polymodel(model_sub_ptr, state);
934 return state.highest_texture_num;
935 }
936
937 }
938
939 //init code for bitmap models
940 int16_t g3_init_polygon_model(const std::span<uint8_t> model)
941 {
942 #ifndef NDEBUG
943 nest_count = 0;
944 #endif
945 return init_model_sub(model.data(), model);
946 }
947
948 #if DXX_BUILD_DESCENT == 1
949 namespace {
950
951 static void validate_model_sub(uint8_t *const model_sub_ptr, const std::span<const uint8_t> model_base)
952 {
953 validate_model_sub_state state(model_base);
954 assert(++nest_count < 1000);
955 iterate_polymodel(model_sub_ptr, state);
956 }
957
958 }
959
960 void g3_validate_polygon_model(const std::span<uint8_t> model)
961 {
962 #ifndef NDEBUG
963 nest_count = 0;
964 #endif
965 return validate_model_sub(model.data(), model);
966 }
967 #endif
968
969 }