-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathd3d9.cpp
More file actions
3467 lines (2966 loc) · 143 KB
/
d3d9.cpp
File metadata and controls
3467 lines (2966 loc) · 143 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "d3d9.h"
#include <emscripten.h>
#include <emscripten/html5.h>
#include <GLES3/gl3.h>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <vector>
#include <cstring>
#include <algorithm>
#include <unordered_set>
#include <cmath>
static const char* g_canvas_selector = "#canvas";
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
// Map D3DBLEND to GL enum
static GLenum d3d_blend_to_gl(DWORD blend) {
switch (blend) {
case D3DBLEND_ZERO: return GL_ZERO;
case D3DBLEND_ONE: return GL_ONE;
case D3DBLEND_SRCCOLOR: return GL_SRC_COLOR;
case D3DBLEND_INVSRCCOLOR: return GL_ONE_MINUS_SRC_COLOR;
case D3DBLEND_SRCALPHA: return GL_SRC_ALPHA;
case D3DBLEND_INVSRCALPHA: return GL_ONE_MINUS_SRC_ALPHA;
case D3DBLEND_DESTALPHA: return GL_DST_ALPHA;
case D3DBLEND_INVDESTALPHA: return GL_ONE_MINUS_DST_ALPHA;
case D3DBLEND_DESTCOLOR: return GL_DST_COLOR;
case D3DBLEND_INVDESTCOLOR: return GL_ONE_MINUS_DST_COLOR;
default: return GL_ONE;
}
}
static GLenum d3d_cmp_to_gl(DWORD func) {
switch (func) {
case D3DCMP_NEVER: return GL_NEVER;
case D3DCMP_LESS: return GL_LESS;
case D3DCMP_EQUAL: return GL_EQUAL;
case D3DCMP_LESSEQUAL: return GL_LEQUAL;
case D3DCMP_GREATER: return GL_GREATER;
case D3DCMP_NOTEQUAL: return GL_NOTEQUAL;
case D3DCMP_GREATEREQUAL: return GL_GEQUAL;
case D3DCMP_ALWAYS: return GL_ALWAYS;
default: return GL_ALWAYS;
}
}
static GLenum d3d_stencil_op_to_gl(DWORD op) {
switch (op) {
case D3DSTENCILOP_KEEP: return GL_KEEP;
case D3DSTENCILOP_ZERO: return GL_ZERO;
case D3DSTENCILOP_REPLACE: return GL_REPLACE;
case D3DSTENCILOP_INCRSAT: return GL_INCR;
case D3DSTENCILOP_DECRSAT: return GL_DECR;
case D3DSTENCILOP_INVERT: return GL_INVERT;
case D3DSTENCILOP_INCR: return GL_INCR_WRAP;
case D3DSTENCILOP_DECR: return GL_DECR_WRAP;
default: return GL_KEEP;
}
}
// WebGL context
static EMSCRIPTEN_WEBGL_CONTEXT_HANDLE g_webgl_context = 0;
// Shader sources
static const char* vertex_shader_source = R"(
attribute vec4 a_position;
attribute vec3 a_normal;
attribute vec4 a_color;
attribute vec2 a_texcoord;
attribute vec2 a_texcoord1;
varying vec4 v_color;
varying vec2 v_texcoord;
varying vec2 v_texcoord1;
varying vec4 v_position_world;
uniform mat4 u_mvp;
uniform mat4 u_world;
uniform bool u_fbo_flip_y;
// Fog
uniform bool u_fog_enabled;
uniform mat4 u_view_model;
varying float v_eye_depth;
// D3D9 FFP Lighting
uniform bool u_lighting_enabled;
uniform vec4 u_material_diffuse;
uniform vec4 u_material_ambient;
uniform vec3 u_global_ambient;
// Light 0 (point light)
uniform bool u_light0_enabled;
uniform vec3 u_light0_position;
uniform vec4 u_light0_diffuse;
uniform vec3 u_light0_ambient;
uniform vec3 u_light0_attenuation; // (att0, att1, att2)
uniform float u_light0_range;
// Light 1 (point light)
uniform bool u_light1_enabled;
uniform vec3 u_light1_position;
uniform vec4 u_light1_diffuse;
uniform vec3 u_light1_ambient;
uniform vec3 u_light1_attenuation;
uniform float u_light1_range;
// Light 2 (point light)
uniform bool u_light2_enabled;
uniform vec3 u_light2_position;
uniform vec4 u_light2_diffuse;
uniform vec3 u_light2_ambient;
uniform vec3 u_light2_attenuation;
uniform float u_light2_range;
// D3D9 FFP point light contribution
vec3 calcPointLight(vec3 lightPos, vec4 lightDiffuse, vec3 lightAmbient,
vec3 lightAtten, float lightRange,
vec3 worldPos, vec3 worldNormal) {
vec3 light_vec = lightPos - worldPos;
float dist = length(light_vec);
if (dist >= lightRange) return vec3(0.0);
vec3 light_dir = normalize(light_vec);
float NdotL = max(dot(worldNormal, light_dir), 0.0);
float atten = 1.0 / (lightAtten.x + lightAtten.y * dist + lightAtten.z * dist * dist);
return (u_material_ambient.rgb * lightAmbient
+ u_material_diffuse.rgb * lightDiffuse.rgb * NdotL) * atten;
}
void main() {
// Note: v_position_world is always computed even for XYZRHW (where a_position.w = rhw, not 1.0).
// Clip plane test uses this value. XYZRHW + clip plane is semantically invalid (XYZRHW coordinates
// are screen-space, not world-space). If this becomes an issue, clip plane
// test must skip for XYZRHW vertices (check D3DFVF_XYZRHW in C++ before enabling clip plane).
v_position_world = u_world * a_position;
gl_Position = u_mvp * a_position;
if (u_fbo_flip_y) gl_Position.y = -gl_Position.y;
v_texcoord = a_texcoord;
v_texcoord1 = a_texcoord1;
if (u_lighting_enabled) {
// D3D9 FFP per-vertex lighting formula
vec3 world_pos = (u_world * a_position).xyz;
vec3 world_normal = normalize(mat3(u_world) * a_normal);
// Global ambient: Material.Ambient * GlobalAmbient
vec3 lit_color = u_material_ambient.rgb * u_global_ambient;
// Per-light contributions (up to 3 point lights)
if (u_light0_enabled) {
lit_color += calcPointLight(u_light0_position, u_light0_diffuse, u_light0_ambient,
u_light0_attenuation, u_light0_range, world_pos, world_normal);
}
if (u_light1_enabled) {
lit_color += calcPointLight(u_light1_position, u_light1_diffuse, u_light1_ambient,
u_light1_attenuation, u_light1_range, world_pos, world_normal);
}
if (u_light2_enabled) {
lit_color += calcPointLight(u_light2_position, u_light2_diffuse, u_light2_ambient,
u_light2_attenuation, u_light2_range, world_pos, world_normal);
}
v_color = vec4(clamp(lit_color, 0.0, 1.0), u_material_diffuse.a);
} else {
v_color = a_color;
}
// Fog: pass eye-space Z to fragment shader for per-pixel fog (D3D9 table fog)
if (u_fog_enabled) {
vec4 view_pos = u_view_model * a_position;
v_eye_depth = abs(view_pos.z);
} else {
v_eye_depth = 0.0;
}
}
)";
static const char* fragment_shader_source = R"(
precision mediump float;
varying vec4 v_color;
varying vec2 v_texcoord;
varying vec2 v_texcoord1;
varying vec4 v_position_world;
uniform sampler2D u_texture;
uniform bool u_clip_plane_enabled;
uniform vec4 u_clip_plane;
uniform sampler2D u_texture_lm;
uniform bool u_use_texture;
uniform bool u_use_lightmap;
uniform int u_stage0_colorop;
uniform int u_stage0_colorarg1;
uniform int u_stage0_colorarg2;
uniform int u_stage0_alphaop;
uniform int u_stage0_alphaarg1;
uniform int u_stage0_alphaarg2;
uniform int u_stage1_op;
uniform bool u_alpha_test_enabled;
uniform float u_alpha_ref;
uniform vec4 u_texture_factor;
uniform float u_lod_bias;
// Fog (per-pixel, matches D3D9 table fog)
uniform bool u_fog_enabled;
uniform float u_fog_start;
uniform float u_fog_end;
uniform vec3 u_fog_color;
varying float v_eye_depth;
// Helper function to select texture argument source
vec4 selectTextureArg(int arg) {
// D3DTA_DIFFUSE = 0, D3DTA_CURRENT = 1, D3DTA_TEXTURE = 2, D3DTA_TFACTOR = 3
// Note: In GLSL ES 2.0, we can't use bitwise operations, but D3DTA values are already 0-6
if (arg == 0) return v_color; // D3DTA_DIFFUSE
if (arg == 1) return v_color; // D3DTA_CURRENT (for stage 0, same as DIFFUSE)
if (arg == 2) { // D3DTA_TEXTURE
// Only sample texture if it's actually being used
if (u_use_texture) {
return texture2D(u_texture, v_texcoord, u_lod_bias);
} else {
return vec4(1.0);
}
}
if (arg == 3) return u_texture_factor; // D3DTA_TFACTOR
return vec4(1.0); // Default
}
void main() {
// Clip plane test (shader-based, replaces hardware clip planes for WebGL)
if (u_clip_plane_enabled) {
float clip_dist = dot(v_position_world.xyz, u_clip_plane.xyz) + u_clip_plane.w;
if (clip_dist < 0.0) discard;
}
vec4 tex_color = vec4(1.0);
if (u_use_texture) {
tex_color = texture2D(u_texture, v_texcoord, u_lod_bias);
}
// Stage 0: Always process texture stage states (faithful D3D9 conversion)
vec4 stage0_color;
float stage0_alpha;
// Process COLOR operation
vec4 colorArg1 = selectTextureArg(u_stage0_colorarg1);
vec4 colorArg2 = selectTextureArg(u_stage0_colorarg2);
// D3DTOP: SELECTARG1=2, SELECTARG2=3, MODULATE=4, MODULATE2X=5
if (u_stage0_colorop == 2) {
stage0_color.rgb = colorArg1.rgb;
} else if (u_stage0_colorop == 3) {
stage0_color.rgb = colorArg2.rgb;
} else if (u_stage0_colorop == 4) {
stage0_color.rgb = colorArg1.rgb * colorArg2.rgb;
} else if (u_stage0_colorop == 5) {
// MODULATE2X for character rendering
stage0_color.rgb = clamp(colorArg1.rgb * colorArg2.rgb * 2.0, 0.0, 1.0);
} else {
// Default: MODULATE
stage0_color.rgb = colorArg1.rgb * colorArg2.rgb;
}
// Process ALPHA operation
vec4 alphaArg1 = selectTextureArg(u_stage0_alphaarg1);
vec4 alphaArg2 = selectTextureArg(u_stage0_alphaarg2);
if (u_stage0_alphaop == 2) {
stage0_alpha = alphaArg1.a;
} else if (u_stage0_alphaop == 3) {
stage0_alpha = alphaArg2.a;
} else if (u_stage0_alphaop == 4) {
stage0_alpha = alphaArg1.a * alphaArg2.a;
} else {
// Default: MODULATE
stage0_alpha = alphaArg1.a * alphaArg2.a;
}
vec4 final_color = vec4(stage0_color.rgb, stage0_alpha);
// Apply lightmap if enabled (Stage 1)
if (u_use_lightmap) {
vec4 lightmap = texture2D(u_texture_lm, v_texcoord1, u_lod_bias);
// D3DTOP values:
// 4 = MODULATE (x1)
// 5 = MODULATE2X (x2)
// 6 = MODULATE4X (x4)
// 8 = ADDSIGNED (result = arg1 + arg2 - 0.5, signed additive blend)
// 11 = ADDSMOOTH (result = arg1 + arg2 - arg1*arg2, reflection-like blend)
if (u_stage1_op == 11) { // ADDSMOOTH
// AddSmooth: dest + src - dest*src = dest + src*(1-dest)
final_color.rgb = final_color.rgb + lightmap.rgb - (final_color.rgb * lightmap.rgb);
} else if (u_stage1_op == 8) { // ADDSIGNED
// AddSigned: dest + src - 0.5 (biased additive blend)
final_color.rgb = clamp(final_color.rgb + lightmap.rgb - 0.5, 0.0, 1.0);
} else if (u_stage1_op == 6) { // MODULATE4X
// Match original shader: saturate(SceneColor * Lightmap * 4)
// This prevents white-out in bright areas while keeping dark areas dark
final_color.rgb = clamp(final_color.rgb * lightmap.rgb * 4.0, 0.0, 1.0);
} else if (u_stage1_op == 5) { // MODULATE2X
final_color.rgb = clamp(final_color.rgb * lightmap.rgb * 2.0, 0.0, 1.0);
} else { // Default/MODULATE (4)
final_color.rgb *= lightmap.rgb;
}
}
if (u_alpha_test_enabled) {
if (final_color.a < (u_alpha_ref / 255.0)) {
discard;
}
}
// Apply fog (per-pixel linear fog, matches D3D9 table fog)
if (u_fog_enabled) {
float fog_factor = clamp((u_fog_end - v_eye_depth) / (u_fog_end - u_fog_start), 0.0, 1.0);
final_color.rgb = mix(u_fog_color, final_color.rgb, fog_factor);
}
gl_FragColor = final_color;
}
)";
// Shader program
static GLuint g_shader_program = 0;
static GLint g_position_location = -1;
static GLint g_color_location = -1;
static GLint g_texcoord_location = -1;
static GLint g_texcoord1_location = -1;
static GLint g_texture_uniform = -1;
static GLint g_texture_lm_uniform = -1;
static GLint g_use_texture_uniform = -1;
static GLint g_use_lightmap_uniform = -1;
static GLint g_mvp_uniform = -1;
static GLint g_tex_matrix_uniform = -1;
static GLint g_use_tex_matrix_uniform = -1;
static GLint g_clip_plane_uniform = -1;
static GLint g_clip_plane_enabled_uniform = -1;
static GLint g_fbo_flip_y_uniform = -1;
static GLint g_stage0_colorop_uniform = -1;
static GLint g_stage0_colorarg1_uniform = -1;
static GLint g_stage0_colorarg2_uniform = -1;
static GLint g_stage0_alphaop_uniform = -1;
static GLint g_stage0_alphaarg1_uniform = -1;
static GLint g_stage0_alphaarg2_uniform = -1;
static GLint g_stage1_op_uniform = -1;
static GLint g_alpha_test_enabled_uniform = -1;
static GLint g_alpha_ref_uniform = -1;
static GLint g_texture_factor_uniform = -1;
static GLint g_lod_bias_uniform = -1;
// Fog uniforms
static GLint g_fog_enabled_uniform = -1;
static GLint g_fog_start_uniform = -1;
static GLint g_fog_end_uniform = -1;
static GLint g_fog_color_uniform = -1;
static GLint g_view_model_uniform = -1;
// FFP Lighting uniforms
static GLint g_normal_location = -1;
static GLint g_world_uniform = -1;
static GLint g_lighting_enabled_uniform = -1;
static GLint g_material_diffuse_uniform = -1;
static GLint g_material_ambient_uniform = -1;
static GLint g_global_ambient_uniform = -1;
static GLint g_light0_enabled_uniform = -1;
static GLint g_light0_position_uniform = -1;
static GLint g_light0_diffuse_uniform = -1;
static GLint g_light0_ambient_uniform = -1;
static GLint g_light0_attenuation_uniform = -1;
static GLint g_light0_range_uniform = -1;
static GLint g_light1_enabled_uniform = -1;
static GLint g_light1_position_uniform = -1;
static GLint g_light1_diffuse_uniform = -1;
static GLint g_light1_ambient_uniform = -1;
static GLint g_light1_attenuation_uniform = -1;
static GLint g_light1_range_uniform = -1;
static GLint g_light2_enabled_uniform = -1;
static GLint g_light2_position_uniform = -1;
static GLint g_light2_diffuse_uniform = -1;
static GLint g_light2_ambient_uniform = -1;
static GLint g_light2_attenuation_uniform = -1;
static GLint g_light2_range_uniform = -1;
// Current FVF
static DWORD g_current_fvf = 0;
// Current texture
static GLuint g_texture_stages[8] = { 0 };
static GLuint g_last_bound_texture_id = 0xFFFFFFFF;
static GLuint g_last_bound_lightmap_id = 0xFFFFFFFF;
// Track which GL textures have a complete mipmap chain
static std::unordered_set<GLuint> g_mipmapped_textures;
// Anisotropic filtering (EXT_texture_filter_anisotropic)
#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
#endif
#ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT
#define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF
#endif
static float g_max_anisotropy = 1.0f; // 1.0 = disabled (no extension)
// FFP State storage
static D3DLIGHT9 g_lights[8];
static bool g_light_enabled[8] = {false};
static D3DMATERIAL9 g_material;
static DWORD g_current_stencil_ref = 0;
static void init_shaders();
static void EnsureUPBuffers();
static GLuint g_up_vbo = 0;
static GLuint g_up_ibo = 0;
static size_t g_up_vbo_size = 0; // Track VBO size for persistent buffer optimization
static size_t g_up_ibo_size = 0; // Track IBO size for persistent buffer optimization
// State Caching for optimization
// NOTE: g_cached_texture is unified with g_last_bound_texture_id to prevent cache
// desync between DrawIndexedPrimitive (uses g_last_bound_texture_id) and
// DrawIndexedPrimitiveUP/DrawPrimitiveUP (used g_cached_texture). The desync caused
// "texParameter: no texture bound" errors and black screen when semi-transparent
// characters drew via DrawIndexedPrimitiveUP, unbinding the texture without
// updating g_last_bound_texture_id.
static GLuint& g_cached_texture = g_last_bound_texture_id;
static GLuint g_cached_program = 0xFFFFFFFF; // Last used shader program
static DWORD g_cached_fvf = 0xFFFFFFFF; // Last FVF
static bool g_cached_vertex_attribs[4] = {false}; // pos, color, tex0, tex1
// Viewport and Scissor state caching (to avoid glGet* calls)
static GLint g_cached_viewport[4] = {0, 0, 0, 0}; // Overwritten by first SetViewport
static GLboolean g_cached_scissor_test = GL_FALSE;
static GLint g_cached_scissor_box[4] = {0, 0, 0, 0}; // Overwritten by first SetScissorRect
static DWORD g_texture_stage_states[8][33];
static D3DVIEWPORT9 g_viewport;
static int g_logical_width = 0; // App's backbuffer resolution (set from D3DPRESENT_PARAMETERS)
static int g_logical_height = 0;
// Texture matrices
static D3DMATRIX g_texture_matrices[8];
// Sampler states [Sampler][Type]
static DWORD g_sampler_states[8][14]; // Up to D3DSAMP_DMAPOFFSET
// Transform matrices (for GetTransform)
D3DMATRIX g_transform_world;
#ifndef GL_COMPRESSED_RGB_S3TC_DXT1_EXT
#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
#endif
#ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
#endif
// GLI library maps RGB DXT1 to a custom FOURCC 'GLI1'; treat like DXT1
#define D3DFMT_GLI1 ((D3DFORMAT)0x31494C47)
// Helper to check for DXT format
static bool IsCompressedFormat(D3DFORMAT format) {
return (format == D3DFMT_DXT1 || format == D3DFMT_DXT3 || format == D3DFMT_DXT5
|| format == D3DFMT_GLI1);
}
static UINT GetFormatBpp(D3DFORMAT format) {
switch (format) {
case D3DFMT_A8R8G8B8:
case D3DFMT_X8R8G8B8:
case D3DFMT_A8B8G8R8:
case D3DFMT_X8B8G8R8:
return 32;
case D3DFMT_R8G8B8:
return 24;
case D3DFMT_R5G6B5:
case D3DFMT_A4R4G4B4:
case D3DFMT_A1R5G5B5:
case D3DFMT_INDEX16:
return 16;
default:
return 32;
}
}
// Convert D3D DXT format to GL extension enum
// D3D9 DXT1 always returns alpha=1.0, so use GL_COMPRESSED_RGB (not RGBA) for DXT1
// to match D3D9 behavior. RGBA variant has 1-bit punch-through alpha which causes
// unexpected alpha=0 in WebGL for textures that were designed as opaque.
GLenum GetGLCompressedFormat(D3DFORMAT fmt) {
switch (fmt) {
case D3DFMT_DXT1: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
case D3DFMT_GLI1: return GL_COMPRESSED_RGB_S3TC_DXT1_EXT; // GLI's RGB DXT1
case D3DFMT_DXT3: return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case D3DFMT_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
default: return 0;
}
}
// Debugging helper
#ifdef DEBUG_TEXTURE_TEST
#define CHECK_GL_ERROR(op) { op; CheckGLError(#op); }
#else
#define CHECK_GL_ERROR(op) op
#endif
// Correct CheckGLError implementation
void CheckGLError(const char* label);
// Manual UP buffers (Moved up for RestoreContext)
void EnsureUPBuffers() {
if (g_up_vbo == 0) {
glGenBuffers(1, &g_up_vbo);
}
if (g_up_ibo == 0) {
glGenBuffers(1, &g_up_ibo);
}
}
// Check for OpenGL errors (silent in release builds)
void CheckGLError(const char* label) {
GLenum err = glGetError();
while (err != GL_NO_ERROR) {
// Silently consume errors
err = glGetError();
}
}
static D3DMATRIX g_transform_view;
static D3DMATRIX g_transform_projection;
// Helper: Compile shader
static GLuint compile_shader(GLenum type, const char* source) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &source, nullptr);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char info_log[512];
glGetShaderInfoLog(shader, 512, nullptr, info_log);
printf("Shader compilation failed: %s\n", info_log);
glDeleteShader(shader);
return 0;
}
return shader;
}
// Helper: Initialize shaders
static void init_shaders() {
if (g_shader_program != 0) return;
// Initialize default sampler states
for (int i = 0; i < 8; ++i) {
g_sampler_states[i][D3DSAMP_ADDRESSU] = D3DTADDRESS_WRAP;
g_sampler_states[i][D3DSAMP_ADDRESSV] = D3DTADDRESS_WRAP;
g_sampler_states[i][D3DSAMP_ADDRESSW] = D3DTADDRESS_WRAP;
g_sampler_states[i][D3DSAMP_MINFILTER] = D3DTEXF_LINEAR;
g_sampler_states[i][D3DSAMP_MAGFILTER] = D3DTEXF_LINEAR;
g_sampler_states[i][D3DSAMP_MIPFILTER] = D3DTEXF_NONE; // D3D9 default; engine sets LINEAR for gameplay
}
GLuint vs = compile_shader(GL_VERTEX_SHADER, vertex_shader_source);
GLuint fs = compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
g_shader_program = glCreateProgram();
glAttachShader(g_shader_program, vs);
glAttachShader(g_shader_program, fs);
glLinkProgram(g_shader_program);
GLint success;
glGetProgramiv(g_shader_program, GL_LINK_STATUS, &success);
if (!success) {
char info_log[512];
glGetProgramInfoLog(g_shader_program, 512, nullptr, info_log);
printf("Shader linking failed: %s\n", info_log);
}
g_position_location = glGetAttribLocation(g_shader_program, "a_position");
g_normal_location = glGetAttribLocation(g_shader_program, "a_normal");
g_color_location = glGetAttribLocation(g_shader_program, "a_color");
g_texcoord_location = glGetAttribLocation(g_shader_program, "a_texcoord");
g_texcoord1_location = glGetAttribLocation(g_shader_program, "a_texcoord1");
g_texture_uniform = glGetUniformLocation(g_shader_program, "u_texture");
g_texture_lm_uniform = glGetUniformLocation(g_shader_program, "u_texture_lm");
g_use_texture_uniform = glGetUniformLocation(g_shader_program, "u_use_texture");
g_use_lightmap_uniform = glGetUniformLocation(g_shader_program, "u_use_lightmap");
g_mvp_uniform = glGetUniformLocation(g_shader_program, "u_mvp");
g_world_uniform = glGetUniformLocation(g_shader_program, "u_world");
g_tex_matrix_uniform = glGetUniformLocation(g_shader_program, "u_tex_matrix");
g_use_tex_matrix_uniform = glGetUniformLocation(g_shader_program, "u_use_tex_matrix");
g_clip_plane_uniform = glGetUniformLocation(g_shader_program, "u_clip_plane");
g_clip_plane_enabled_uniform = glGetUniformLocation(g_shader_program, "u_clip_plane_enabled");
g_fbo_flip_y_uniform = glGetUniformLocation(g_shader_program, "u_fbo_flip_y");
g_stage0_colorop_uniform = glGetUniformLocation(g_shader_program, "u_stage0_colorop");
g_stage0_colorarg1_uniform = glGetUniformLocation(g_shader_program, "u_stage0_colorarg1");
g_stage0_colorarg2_uniform = glGetUniformLocation(g_shader_program, "u_stage0_colorarg2");
g_stage0_alphaop_uniform = glGetUniformLocation(g_shader_program, "u_stage0_alphaop");
g_stage0_alphaarg1_uniform = glGetUniformLocation(g_shader_program, "u_stage0_alphaarg1");
g_stage0_alphaarg2_uniform = glGetUniformLocation(g_shader_program, "u_stage0_alphaarg2");
g_stage1_op_uniform = glGetUniformLocation(g_shader_program, "u_stage1_op");
g_alpha_test_enabled_uniform = glGetUniformLocation(g_shader_program, "u_alpha_test_enabled");
g_alpha_ref_uniform = glGetUniformLocation(g_shader_program, "u_alpha_ref");
g_texture_factor_uniform = glGetUniformLocation(g_shader_program, "u_texture_factor");
g_lod_bias_uniform = glGetUniformLocation(g_shader_program, "u_lod_bias");
// Fog uniforms
g_fog_enabled_uniform = glGetUniformLocation(g_shader_program, "u_fog_enabled");
g_fog_start_uniform = glGetUniformLocation(g_shader_program, "u_fog_start");
g_fog_end_uniform = glGetUniformLocation(g_shader_program, "u_fog_end");
g_fog_color_uniform = glGetUniformLocation(g_shader_program, "u_fog_color");
g_view_model_uniform = glGetUniformLocation(g_shader_program, "u_view_model");
// FFP Lighting uniforms
g_lighting_enabled_uniform = glGetUniformLocation(g_shader_program, "u_lighting_enabled");
g_material_diffuse_uniform = glGetUniformLocation(g_shader_program, "u_material_diffuse");
g_material_ambient_uniform = glGetUniformLocation(g_shader_program, "u_material_ambient");
g_global_ambient_uniform = glGetUniformLocation(g_shader_program, "u_global_ambient");
g_light0_enabled_uniform = glGetUniformLocation(g_shader_program, "u_light0_enabled");
g_light0_position_uniform = glGetUniformLocation(g_shader_program, "u_light0_position");
g_light0_diffuse_uniform = glGetUniformLocation(g_shader_program, "u_light0_diffuse");
g_light0_ambient_uniform = glGetUniformLocation(g_shader_program, "u_light0_ambient");
g_light0_attenuation_uniform = glGetUniformLocation(g_shader_program, "u_light0_attenuation");
g_light0_range_uniform = glGetUniformLocation(g_shader_program, "u_light0_range");
g_light1_enabled_uniform = glGetUniformLocation(g_shader_program, "u_light1_enabled");
g_light1_position_uniform = glGetUniformLocation(g_shader_program, "u_light1_position");
g_light1_diffuse_uniform = glGetUniformLocation(g_shader_program, "u_light1_diffuse");
g_light1_ambient_uniform = glGetUniformLocation(g_shader_program, "u_light1_ambient");
g_light1_attenuation_uniform = glGetUniformLocation(g_shader_program, "u_light1_attenuation");
g_light1_range_uniform = glGetUniformLocation(g_shader_program, "u_light1_range");
g_light2_enabled_uniform = glGetUniformLocation(g_shader_program, "u_light2_enabled");
g_light2_position_uniform = glGetUniformLocation(g_shader_program, "u_light2_position");
g_light2_diffuse_uniform = glGetUniformLocation(g_shader_program, "u_light2_diffuse");
g_light2_ambient_uniform = glGetUniformLocation(g_shader_program, "u_light2_ambient");
g_light2_attenuation_uniform = glGetUniformLocation(g_shader_program, "u_light2_attenuation");
g_light2_range_uniform = glGetUniformLocation(g_shader_program, "u_light2_range");
glDeleteShader(vs);
glDeleteShader(fs);
}
// Helper: Determinant of upper-left 3x3 sub-matrix (for orientation/winding detection)
// D3D9 automatically detects mirrored transforms (negative determinant) and flips culling.
// WebGL doesn't, so we compute this per draw call to set glFrontFace correctly.
static float det3x3(const D3DMATRIX& m) {
return m.m[0][0] * (m.m[1][1] * m.m[2][2] - m.m[1][2] * m.m[2][1])
- m.m[0][1] * (m.m[1][0] * m.m[2][2] - m.m[1][2] * m.m[2][0])
+ m.m[0][2] * (m.m[1][0] * m.m[2][1] - m.m[1][1] * m.m[2][0]);
}
// Helper: Matrix multiplication
static void matrix_multiply(D3DMATRIX* result, const D3DMATRIX* a, const D3DMATRIX* b) {
D3DMATRIX temp;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
temp.m[i][j] = 0;
for (int k = 0; k < 4; k++) {
temp.m[i][j] += a->m[i][k] * b->m[k][j];
}
}
}
*result = temp;
}
static void matrix_identity(D3DMATRIX* m) {
memset(m, 0, sizeof(D3DMATRIX));
m->m[0][0] = 1.0f;
m->m[1][1] = 1.0f;
m->m[2][2] = 1.0f;
m->m[3][3] = 1.0f;
}
// Helper: Set matrix uniform with CPU-side transpose
// Helper: Set matrix uniform.
// [IMPORTANT] D3D memory (Row-Major, v*M) is binary-equivalent to GL (Column-Major, M*v).
// Do NOT transpose manually here if using standard D3D matrix math.
static void gl_set_matrix_uniform(GLint location, const D3DMATRIX* matrix) {
if (location == -1 || !matrix) return;
glUniformMatrix4fv(location, 1, GL_FALSE, (const float*)matrix);
}
// Helper: Set FFP lighting uniforms (material, ambient, lights 0-2)
static void SetFFPLightingUniforms(const D3DMATRIX* world, const DWORD* render_states) {
gl_set_matrix_uniform(g_world_uniform, world);
glUniform4f(g_material_diffuse_uniform,
g_material.Diffuse.r, g_material.Diffuse.g, g_material.Diffuse.b, g_material.Diffuse.a);
glUniform4f(g_material_ambient_uniform,
g_material.Ambient.r, g_material.Ambient.g, g_material.Ambient.b, g_material.Ambient.a);
DWORD amb = render_states[D3DRS_AMBIENT];
glUniform3f(g_global_ambient_uniform,
((amb >> 16) & 0xFF) / 255.0f,
((amb >> 8) & 0xFF) / 255.0f,
(amb & 0xFF) / 255.0f);
// Light 0
if (g_light_enabled[0]) {
glUniform1i(g_light0_enabled_uniform, 1);
glUniform3f(g_light0_position_uniform, g_lights[0].Position.x, g_lights[0].Position.y, g_lights[0].Position.z);
glUniform4f(g_light0_diffuse_uniform, g_lights[0].Diffuse.r, g_lights[0].Diffuse.g, g_lights[0].Diffuse.b, g_lights[0].Diffuse.a);
glUniform3f(g_light0_ambient_uniform, g_lights[0].Ambient.r, g_lights[0].Ambient.g, g_lights[0].Ambient.b);
glUniform3f(g_light0_attenuation_uniform, g_lights[0].Attenuation0, g_lights[0].Attenuation1, g_lights[0].Attenuation2);
glUniform1f(g_light0_range_uniform, g_lights[0].Range);
} else {
glUniform1i(g_light0_enabled_uniform, 0);
}
// Light 1
if (g_light_enabled[1]) {
glUniform1i(g_light1_enabled_uniform, 1);
glUniform3f(g_light1_position_uniform, g_lights[1].Position.x, g_lights[1].Position.y, g_lights[1].Position.z);
glUniform4f(g_light1_diffuse_uniform, g_lights[1].Diffuse.r, g_lights[1].Diffuse.g, g_lights[1].Diffuse.b, g_lights[1].Diffuse.a);
glUniform3f(g_light1_ambient_uniform, g_lights[1].Ambient.r, g_lights[1].Ambient.g, g_lights[1].Ambient.b);
glUniform3f(g_light1_attenuation_uniform, g_lights[1].Attenuation0, g_lights[1].Attenuation1, g_lights[1].Attenuation2);
glUniform1f(g_light1_range_uniform, g_lights[1].Range);
} else {
glUniform1i(g_light1_enabled_uniform, 0);
}
// Light 2
if (g_light_enabled[2]) {
glUniform1i(g_light2_enabled_uniform, 1);
glUniform3f(g_light2_position_uniform, g_lights[2].Position.x, g_lights[2].Position.y, g_lights[2].Position.z);
glUniform4f(g_light2_diffuse_uniform, g_lights[2].Diffuse.r, g_lights[2].Diffuse.g, g_lights[2].Diffuse.b, g_lights[2].Diffuse.a);
glUniform3f(g_light2_ambient_uniform, g_lights[2].Ambient.r, g_lights[2].Ambient.g, g_lights[2].Ambient.b);
glUniform3f(g_light2_attenuation_uniform, g_lights[2].Attenuation0, g_lights[2].Attenuation1, g_lights[2].Attenuation2);
glUniform1f(g_light2_range_uniform, g_lights[2].Range);
} else {
glUniform1i(g_light2_enabled_uniform, 0);
}
}
// Global FBO for SetRenderTarget
static GLuint g_fbo = 0;
// D3D9Surface class
class D3D9Surface : public IDirect3DSurface9 {
public:
GLuint m_gl_object; // Texture ID or Renderbuffer ID
bool m_is_renderbuffer; // True if Renderbuffer (Depth), False if Texture
bool m_is_backbuffer; // True if this represents the default backbuffer (Canvas)
UINT m_width;
UINT m_height;
ULONG m_refCount; // Reference count for COM-style lifetime management
D3D9Surface(UINT width, UINT height, GLuint gl_obj, bool is_rb, bool is_bb = false)
: m_width(width), m_height(height), m_gl_object(gl_obj),
m_is_renderbuffer(is_rb), m_is_backbuffer(is_bb), m_refCount(1) {}
virtual ~D3D9Surface() {
if (m_is_renderbuffer && m_gl_object != 0 && !m_is_backbuffer) {
glDeleteRenderbuffers(1, &m_gl_object);
}
// If it's a texture surface, D3D9Texture owns the GL object, so we don't delete it here.
}
virtual ULONG AddRef() override {
return ++m_refCount;
}
virtual ULONG Release() override {
ULONG ref = --m_refCount;
if (ref == 0) {
delete this;
}
return ref;
}
virtual HRESULT LockRect(D3DLOCKED_RECT* pLockedRect, const RECT* pRect, DWORD Flags) override {
// Not implemented for Surfaces yet (usually needed for CPU access to render targets)
return D3DERR_INVALIDCALL;
}
virtual HRESULT UnlockRect() override {
return D3DERR_INVALIDCALL;
}
virtual HRESULT GetDesc(D3DSURFACE_DESC* pDesc) override {
if (!pDesc) return D3DERR_INVALIDCALL;
memset(pDesc, 0, sizeof(D3DSURFACE_DESC));
pDesc->Width = m_width;
pDesc->Height = m_height;
pDesc->Format = D3DFMT_X8R8G8B8; // Default format
pDesc->Type = m_is_renderbuffer ? D3DRTYPE_SURFACE : D3DRTYPE_TEXTURE;
pDesc->Pool = D3DPOOL_DEFAULT;
return D3D_OK;
}
};
// D3D9Texture class
class D3D9Texture : public IDirect3DTexture9 {
public:
GLuint m_texture;
UINT m_width;
UINT m_height;
UINT m_levels;
D3DFORMAT m_format;
void* m_locked_data;
public:
D3D9Texture(UINT width, UINT height, UINT levels, D3DFORMAT format)
: m_width(width), m_height(height), m_levels(levels), m_format(format), m_locked_data(nullptr) {
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
// Invalidate ALL texture binding caches since we bound outside of draw calls
g_last_bound_texture_id = 0xFFFFFFFF;
g_last_bound_lightmap_id = 0xFFFFFFFF;
g_cached_texture = 0xFFFFFFFF;
// [WASM] Use GL_LINEAR as default to avoid "Texture Incomplete" if no mipmaps provided
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// [WASM] Restrict to Level 0 only until mip levels are uploaded/generated.
// Prevents "Texture Incomplete" when mipmap filter is applied to a texture
// that hasn't had its full mip chain uploaded yet.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
// [WASM] Use CLAMP_TO_EDGE which is required for NPOT textures in WebGL 1.0
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
virtual ~D3D9Texture() {
if (m_texture) {
// Invalidate caches if this texture was cached (ID may be recycled by GL)
if (g_last_bound_texture_id == m_texture) g_last_bound_texture_id = 0xFFFFFFFF;
if (g_last_bound_lightmap_id == m_texture) g_last_bound_lightmap_id = 0xFFFFFFFF;
if (g_cached_texture == m_texture) g_cached_texture = 0xFFFFFFFF;
g_mipmapped_textures.erase(m_texture);
glDeleteTextures(1, &m_texture);
}
if (m_locked_data) {
delete[] static_cast<uint8_t*>(m_locked_data);
}
}
virtual ULONG AddRef() override {
return 1; // Stub: no ref counting for textures (owned by game code)
}
virtual ULONG Release() override {
delete this;
return 0;
}
virtual HRESULT GetSurfaceLevel(UINT Level, IDirect3DSurface9** ppSurfaceLevel) override {
if (!ppSurfaceLevel) return D3DERR_INVALIDCALL;
if (Level != 0) return D3DERR_INVALIDCALL; // Only level 0 supported for now
// Return a Surface wrapper for this texture
*ppSurfaceLevel = new D3D9Surface(m_width, m_height, m_texture, false);
return D3D_OK;
}
virtual HRESULT LockRect(UINT Level, D3DLOCKED_RECT* pLockedRect,
const void* pRect, DWORD Flags) override {
if (!pLockedRect) return -1;
if (m_locked_data) return -1; // Already locked - prevent memory leak
// Calculate mip dimensions
UINT mip_width = std::max(1u, m_width >> Level);
UINT mip_height = std::max(1u, m_height >> Level);
// Calculate size for allocation
size_t data_size = 0;
if (IsCompressedFormat(m_format)) {
// DXT1/GLI1: 8 bytes per 4x4 block
// DXT3/5: 16 bytes per 4x4 block
size_t block_size = (m_format == D3DFMT_DXT1 || m_format == D3DFMT_GLI1) ? 8 : 16;
size_t blocks_w = (mip_width + 3) / 4;
size_t blocks_h = (mip_height + 3) / 4;
data_size = blocks_w * blocks_h * block_size;
pLockedRect->Pitch = blocks_w * block_size;
} else {
// Uncompressed
UINT bpp = GetFormatBpp(m_format);
pLockedRect->Pitch = (mip_width * bpp + 7) / 8;
data_size = pLockedRect->Pitch * mip_height;
}
m_locked_data = new uint8_t[data_size];
pLockedRect->pBits = m_locked_data;
return D3D_OK;
}
virtual HRESULT UnlockRect(UINT Level) override {
if (!m_locked_data) return -1;
glBindTexture(GL_TEXTURE_2D, m_texture);
// Invalidate ALL texture binding caches since we bound outside of draw calls
g_last_bound_texture_id = 0xFFFFFFFF;
g_last_bound_lightmap_id = 0xFFFFFFFF;
g_cached_texture = 0xFFFFFFFF;
// Calculate mip dimensions again
UINT mip_width = std::max(1u, m_width >> Level);
UINT mip_height = std::max(1u, m_height >> Level);
if (IsCompressedFormat(m_format)) {
// Compressed upload
GLenum gl_fmt = GetGLCompressedFormat(m_format);
size_t block_size = (m_format == D3DFMT_DXT1 || m_format == D3DFMT_GLI1) ? 8 : 16;
size_t size = ((mip_width + 3) / 4) * ((mip_height + 3) / 4) * block_size;
CHECK_GL_ERROR(glCompressedTexImage2D(GL_TEXTURE_2D, Level, gl_fmt, mip_width, mip_height, 0, size, m_locked_data));
}
else
{
// Uncompressed upload: Convert to RGBA32 for WebGL
size_t pixel_count = mip_width * mip_height;
std::vector<uint32_t> rgba_pixels(pixel_count);
if (m_format == D3DFMT_A8R8G8B8 || m_format == D3DFMT_X8R8G8B8) {
uint32_t* src = static_cast<uint32_t*>(m_locked_data);
for (size_t i = 0; i < pixel_count; ++i) {
uint32_t color = src[i];
uint32_t b = (color >> 0) & 0xFF;
uint32_t g = (color >> 8) & 0xFF;
uint32_t r = (color >> 16) & 0xFF;
uint32_t a = (color >> 24) & 0xFF;
// Only force opaque for X8R8G8B8. A8R8G8B8 must respect actual alpha.
if (m_format == D3DFMT_X8R8G8B8) a = 0xFF;
rgba_pixels[i] = (r << 0) | (g << 8) | (b << 16) | (a << 24);
}
}
else if (m_format == D3DFMT_A4R4G4B4) {
uint16_t* src = static_cast<uint16_t*>(m_locked_data);
for (size_t i = 0; i < pixel_count; ++i) {
uint16_t color = src[i];
uint32_t a = ((color >> 12) & 0xF); a = (a << 4) | a;
uint32_t r = ((color >> 8) & 0xF); r = (r << 4) | r;
uint32_t g = ((color >> 4) & 0xF); g = (g << 4) | g;
uint32_t b = ((color >> 0) & 0xF); b = (b << 4) | b;
rgba_pixels[i] = (r << 0) | (g << 8) | (b << 16) | (a << 24);
}
}
else if (m_format == D3DFMT_A1R5G5B5 || m_format == 25 /* D3DFMT_X1R5G5B5 */) {
uint16_t* src = static_cast<uint16_t*>(m_locked_data);
for (size_t i = 0; i < pixel_count; ++i) {
uint16_t color = src[i];
uint32_t r = ((color >> 10) & 0x1F); r = (r << 3) | (r >> 2);
uint32_t g = ((color >> 5) & 0x1F); g = (g << 3) | (g >> 2);
uint32_t b = ((color >> 0) & 0x1F); b = (b << 3) | (b >> 2);
uint32_t a = (m_format == D3DFMT_A1R5G5B5) ? (((color >> 15) & 0x1) ? 255 : 0) : 255;
rgba_pixels[i] = (r << 0) | (g << 8) | (b << 16) | (a << 24);
}
}
else if (m_format == D3DFMT_R5G6B5) {
uint16_t* src = static_cast<uint16_t*>(m_locked_data);
for (size_t i = 0; i < pixel_count; ++i) {
uint16_t color = src[i];
uint32_t r = ((color >> 11) & 0x1F) << 3;
uint32_t g = ((color >> 5) & 0x3F) << 2;
uint32_t b = ((color >> 0) & 0x1F) << 3;
rgba_pixels[i] = (r << 0) | (g << 8) | (b << 16) | 0xFF000000;
}
}
else if (m_format == D3DFMT_R8G8B8) {
// 24-bit BGR (D3D R8G8B8 stores B,G,R byte order)
printf("[WARN] D3DFMT_R8G8B8 texture uploaded (%ux%u) - untested path\n",
mip_width, mip_height);
uint8_t* src = static_cast<uint8_t*>(m_locked_data);
for (size_t i = 0; i < pixel_count; ++i) {
uint32_t b = src[0];
uint32_t g = src[1];
uint32_t r = src[2];
rgba_pixels[i] = (r << 0) | (g << 8) | (b << 16) | 0xFF000000;
src += 3;
}
}
else {
// Fallback for other formats
uint32_t* src = static_cast<uint32_t*>(m_locked_data);
for (size_t i = 0; i < pixel_count; ++i) {
rgba_pixels[i] = src[i];
}
}
glTexImage2D(GL_TEXTURE_2D, Level, GL_RGBA, mip_width, mip_height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, rgba_pixels.data());
}
// Update mipmap state: expand GL_TEXTURE_MAX_LEVEL as levels are uploaded
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, Level);
if (Level > 0) {
// Compressed textures with pre-baked mip chain (DDS files)