-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdpsoftrast.c
5690 lines (5368 loc) · 238 KB
/
dpsoftrast.c
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 <stdio.h>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include "quakedef.h"
#include "thread.h"
#include "dpsoftrast.h"
#ifdef _MSC_VER
#pragma warning(disable : 4324)
#endif
#ifndef __cplusplus
typedef qboolean bool;
#endif
#define ALIGN_SIZE 16
#define ATOMIC_SIZE 4
#ifdef SSE_POSSIBLE
#if defined(__APPLE__)
#include <libkern/OSAtomic.h>
#define ALIGN(var) var __attribute__((__aligned__(16)))
#define ATOMIC(var) var __attribute__((__aligned__(4)))
#define MEMORY_BARRIER (_mm_sfence())
#define ATOMIC_COUNTER volatile int32_t
#define ATOMIC_INCREMENT(counter) (OSAtomicIncrement32Barrier(&(counter)))
#define ATOMIC_DECREMENT(counter) (OSAtomicDecrement32Barrier(&(counter)))
#define ATOMIC_ADD(counter, val) ((void)OSAtomicAdd32Barrier((val), &(counter)))
#elif defined(__GNUC__) && defined(WIN32)
#define ALIGN(var) var __attribute__((__aligned__(16)))
#define ATOMIC(var) var __attribute__((__aligned__(4)))
#define MEMORY_BARRIER (_mm_sfence())
//(__sync_synchronize())
#define ATOMIC_COUNTER volatile LONG
// this LONG * cast serves to fix an issue with broken mingw
// packages on Ubuntu; these only declare the function to take
// a LONG *, causing a compile error here. This seems to be
// error- and warn-free on platforms that DO declare
// InterlockedIncrement correctly, like mingw on Windows.
#define ATOMIC_INCREMENT(counter) (InterlockedIncrement((LONG *) &(counter)))
#define ATOMIC_DECREMENT(counter) (InterlockedDecrement((LONG *) &(counter)))
#define ATOMIC_ADD(counter, val) ((void)InterlockedExchangeAdd((LONG *) &(counter), (val)))
#elif defined(__GNUC__)
#define ALIGN(var) var __attribute__((__aligned__(16)))
#define ATOMIC(var) var __attribute__((__aligned__(4)))
#define MEMORY_BARRIER (_mm_sfence())
//(__sync_synchronize())
#define ATOMIC_COUNTER volatile int
#define ATOMIC_INCREMENT(counter) (__sync_add_and_fetch(&(counter), 1))
#define ATOMIC_DECREMENT(counter) (__sync_add_and_fetch(&(counter), -1))
#define ATOMIC_ADD(counter, val) ((void)__sync_fetch_and_add(&(counter), (val)))
#elif defined(_MSC_VER)
#define ALIGN(var) __declspec(align(16)) var
#define ATOMIC(var) __declspec(align(4)) var
#define MEMORY_BARRIER (_mm_sfence())
//(MemoryBarrier())
#define ATOMIC_COUNTER volatile LONG
#define ATOMIC_INCREMENT(counter) (InterlockedIncrement(&(counter)))
#define ATOMIC_DECREMENT(counter) (InterlockedDecrement(&(counter)))
#define ATOMIC_ADD(counter, val) ((void)InterlockedExchangeAdd(&(counter), (val)))
#endif
#endif
#ifndef ALIGN
#define ALIGN(var) var
#endif
#ifndef ATOMIC
#define ATOMIC(var) var
#endif
#ifndef MEMORY_BARRIER
#define MEMORY_BARRIER ((void)0)
#endif
#ifndef ATOMIC_COUNTER
#define ATOMIC_COUNTER int
#endif
#ifndef ATOMIC_INCREMENT
#define ATOMIC_INCREMENT(counter) (++(counter))
#endif
#ifndef ATOMIC_DECREMENT
#define ATOMIC_DECREMENT(counter) (--(counter))
#endif
#ifndef ATOMIC_ADD
#define ATOMIC_ADD(counter, val) ((void)((counter) += (val)))
#endif
#ifdef SSE_POSSIBLE
#include <emmintrin.h>
#if defined(__GNUC__) && (__GNUC < 4 || __GNUC_MINOR__ < 6) && !defined(__clang__)
#define _mm_cvtss_f32(val) (__builtin_ia32_vec_ext_v4sf ((__v4sf)(val), 0))
#endif
#define MM_MALLOC(size) _mm_malloc(size, ALIGN_SIZE)
static void *MM_CALLOC(size_t nmemb, size_t size)
{
void *ptr = _mm_malloc(nmemb*size, ALIGN_SIZE);
if (ptr != NULL) memset(ptr, 0, nmemb*size);
return ptr;
}
#define MM_FREE _mm_free
#else
#define MM_MALLOC(size) malloc(size)
#define MM_CALLOC(nmemb, size) calloc(nmemb, size)
#define MM_FREE free
#endif
typedef enum DPSOFTRAST_ARRAY_e
{
DPSOFTRAST_ARRAY_POSITION,
DPSOFTRAST_ARRAY_COLOR,
DPSOFTRAST_ARRAY_TEXCOORD0,
DPSOFTRAST_ARRAY_TEXCOORD1,
DPSOFTRAST_ARRAY_TEXCOORD2,
DPSOFTRAST_ARRAY_TEXCOORD3,
DPSOFTRAST_ARRAY_TEXCOORD4,
DPSOFTRAST_ARRAY_TEXCOORD5,
DPSOFTRAST_ARRAY_TEXCOORD6,
DPSOFTRAST_ARRAY_TEXCOORD7,
DPSOFTRAST_ARRAY_TOTAL
}
DPSOFTRAST_ARRAY;
typedef struct DPSOFTRAST_Texture_s
{
int flags;
int width;
int height;
int depth;
int sides;
DPSOFTRAST_TEXTURE_FILTER filter;
int mipmaps;
int size;
ATOMIC_COUNTER binds;
unsigned char *bytes;
int mipmap[DPSOFTRAST_MAXMIPMAPS][5];
}
DPSOFTRAST_Texture;
#define COMMAND_SIZE ALIGN_SIZE
#define COMMAND_ALIGN(var) ALIGN(var)
typedef COMMAND_ALIGN(struct DPSOFTRAST_Command_s
{
unsigned char opcode;
unsigned short commandsize;
}
DPSOFTRAST_Command);
enum { DPSOFTRAST_OPCODE_Reset = 0 };
#define DEFCOMMAND(opcodeval, name, fields) \
enum { DPSOFTRAST_OPCODE_##name = opcodeval }; \
typedef COMMAND_ALIGN(struct DPSOFTRAST_Command_##name##_s \
{ \
unsigned char opcode; \
unsigned short commandsize; \
fields \
} DPSOFTRAST_Command_##name );
#define DPSOFTRAST_DRAW_MAXCOMMANDPOOL 2097152
#define DPSOFTRAST_DRAW_MAXCOMMANDSIZE 16384
typedef ALIGN(struct DPSOFTRAST_State_Command_Pool_s
{
int freecommand;
int usedcommands;
ALIGN(unsigned char commands[DPSOFTRAST_DRAW_MAXCOMMANDPOOL]);
}
DPSOFTRAST_State_Command_Pool);
typedef ALIGN(struct DPSOFTRAST_State_Triangle_s
{
unsigned char mip[DPSOFTRAST_MAXTEXTUREUNITS]; // texcoord to screen space density values (for picking mipmap of textures)
float w[3];
ALIGN(float attribs[DPSOFTRAST_ARRAY_TOTAL][3][4]);
}
DPSOFTRAST_State_Triangle);
#define DPSOFTRAST_CALCATTRIB(triangle, span, data, slope, arrayindex) { \
slope = _mm_load_ps((triangle)->attribs[arrayindex][0]); \
data = _mm_add_ps(_mm_load_ps((triangle)->attribs[arrayindex][2]), \
_mm_add_ps(_mm_mul_ps(_mm_set1_ps((span)->x), slope), \
_mm_mul_ps(_mm_set1_ps((span)->y), _mm_load_ps((triangle)->attribs[arrayindex][1])))); \
}
#define DPSOFTRAST_CALCATTRIB4F(triangle, span, data, slope, arrayindex) { \
slope[0] = (triangle)->attribs[arrayindex][0][0]; \
slope[1] = (triangle)->attribs[arrayindex][0][1]; \
slope[2] = (triangle)->attribs[arrayindex][0][2]; \
slope[3] = (triangle)->attribs[arrayindex][0][3]; \
data[0] = (triangle)->attribs[arrayindex][2][0] + (span->x)*slope[0] + (span->y)*(triangle)->attribs[arrayindex][1][0]; \
data[1] = (triangle)->attribs[arrayindex][2][1] + (span->x)*slope[1] + (span->y)*(triangle)->attribs[arrayindex][1][1]; \
data[2] = (triangle)->attribs[arrayindex][2][2] + (span->x)*slope[2] + (span->y)*(triangle)->attribs[arrayindex][1][2]; \
data[3] = (triangle)->attribs[arrayindex][2][3] + (span->x)*slope[3] + (span->y)*(triangle)->attribs[arrayindex][1][3]; \
}
#define DPSOFTRAST_DRAW_MAXSUBSPAN 16
typedef ALIGN(struct DPSOFTRAST_State_Span_s
{
int triangle; // triangle this span was generated by
int x; // framebuffer x coord
int y; // framebuffer y coord
int startx; // usable range (according to pixelmask)
int endx; // usable range (according to pixelmask)
unsigned char *pixelmask; // true for pixels that passed depth test, false for others
int depthbase; // depthbuffer value at x (add depthslope*startx to get first pixel's depthbuffer value)
int depthslope; // depthbuffer value pixel delta
}
DPSOFTRAST_State_Span);
#define DPSOFTRAST_DRAW_MAXSPANS 1024
#define DPSOFTRAST_DRAW_MAXTRIANGLES 128
#define DPSOFTRAST_DRAW_MAXSPANLENGTH 256
#define DPSOFTRAST_VALIDATE_FB 1
#define DPSOFTRAST_VALIDATE_DEPTHFUNC 2
#define DPSOFTRAST_VALIDATE_BLENDFUNC 4
#define DPSOFTRAST_VALIDATE_DRAW (DPSOFTRAST_VALIDATE_FB | DPSOFTRAST_VALIDATE_DEPTHFUNC | DPSOFTRAST_VALIDATE_BLENDFUNC)
typedef enum DPSOFTRAST_BLENDMODE_e
{
DPSOFTRAST_BLENDMODE_OPAQUE,
DPSOFTRAST_BLENDMODE_ALPHA,
DPSOFTRAST_BLENDMODE_ADDALPHA,
DPSOFTRAST_BLENDMODE_ADD,
DPSOFTRAST_BLENDMODE_INVMOD,
DPSOFTRAST_BLENDMODE_MUL,
DPSOFTRAST_BLENDMODE_MUL2,
DPSOFTRAST_BLENDMODE_SUBALPHA,
DPSOFTRAST_BLENDMODE_PSEUDOALPHA,
DPSOFTRAST_BLENDMODE_INVADD,
DPSOFTRAST_BLENDMODE_TOTAL
}
DPSOFTRAST_BLENDMODE;
typedef ALIGN(struct DPSOFTRAST_State_Thread_s
{
void *thread;
int index;
int cullface;
int colormask[4];
int blendfunc[2];
int blendsubtract;
int depthmask;
int depthtest;
int depthfunc;
int scissortest;
int viewport[4];
int scissor[4];
float depthrange[2];
float polygonoffset[2];
float clipplane[4];
ALIGN(float fb_clipplane[4]);
int shader_mode;
int shader_permutation;
int shader_exactspecularmath;
DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
ALIGN(float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4]);
int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
// DPSOFTRAST_VALIDATE_ flags
int validate;
// derived values (DPSOFTRAST_VALIDATE_FB)
int fb_colormask;
int fb_scissor[4];
ALIGN(float fb_viewportcenter[4]);
ALIGN(float fb_viewportscale[4]);
// derived values (DPSOFTRAST_VALIDATE_DEPTHFUNC)
int fb_depthfunc;
// derived values (DPSOFTRAST_VALIDATE_BLENDFUNC)
int fb_blendmode;
// band boundaries
int miny1;
int maxy1;
int miny2;
int maxy2;
ATOMIC(volatile int commandoffset);
volatile bool waiting;
volatile bool starving;
void *waitcond;
void *drawcond;
void *drawmutex;
int numspans;
int numtriangles;
DPSOFTRAST_State_Span spans[DPSOFTRAST_DRAW_MAXSPANS];
DPSOFTRAST_State_Triangle triangles[DPSOFTRAST_DRAW_MAXTRIANGLES];
unsigned char pixelmaskarray[DPSOFTRAST_DRAW_MAXSPANLENGTH+4]; // LordHavoc: padded to allow some termination bytes
}
DPSOFTRAST_State_Thread);
typedef ALIGN(struct DPSOFTRAST_State_s
{
int fb_width;
int fb_height;
unsigned int *fb_depthpixels;
unsigned int *fb_colorpixels[4];
int viewport[4];
ALIGN(float fb_viewportcenter[4]);
ALIGN(float fb_viewportscale[4]);
float color[4];
ALIGN(float uniform4f[DPSOFTRAST_UNIFORM_TOTAL*4]);
int uniform1i[DPSOFTRAST_UNIFORM_TOTAL];
const float *pointer_vertex3f;
const float *pointer_color4f;
const unsigned char *pointer_color4ub;
const float *pointer_texcoordf[DPSOFTRAST_MAXTEXCOORDARRAYS];
int stride_vertex;
int stride_color;
int stride_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
int components_texcoord[DPSOFTRAST_MAXTEXCOORDARRAYS];
DPSOFTRAST_Texture *texbound[DPSOFTRAST_MAXTEXTUREUNITS];
int firstvertex;
int numvertices;
float *post_array4f[DPSOFTRAST_ARRAY_TOTAL];
float *screencoord4f;
int drawstarty;
int drawendy;
int drawclipped;
int shader_mode;
int shader_permutation;
int shader_exactspecularmath;
int texture_max;
int texture_end;
int texture_firstfree;
DPSOFTRAST_Texture *texture;
int bigendian;
// error reporting
const char *errorstring;
bool usethreads;
int interlace;
int numthreads;
DPSOFTRAST_State_Thread *threads;
ATOMIC(volatile int drawcommand);
DPSOFTRAST_State_Command_Pool commandpool;
}
DPSOFTRAST_State);
DPSOFTRAST_State dpsoftrast;
#define DPSOFTRAST_DEPTHSCALE (1024.0f*1048576.0f)
#define DPSOFTRAST_DEPTHOFFSET (128.0f)
#define DPSOFTRAST_BGRA8_FROM_RGBA32F(r,g,b,a) (((int)(r * 255.0f + 0.5f) << 16) | ((int)(g * 255.0f + 0.5f) << 8) | (int)(b * 255.0f + 0.5f) | ((int)(a * 255.0f + 0.5f) << 24))
#define DPSOFTRAST_DEPTH32_FROM_DEPTH32F(d) ((int)(DPSOFTRAST_DEPTHSCALE * (1-d)))
static void DPSOFTRAST_Draw_DepthTest(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_State_Span *span);
static void DPSOFTRAST_Draw_DepthWrite(const DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_State_Span *span);
static void DPSOFTRAST_RecalcViewport(const int *viewport, float *fb_viewportcenter, float *fb_viewportscale)
{
fb_viewportcenter[1] = viewport[0] + 0.5f * viewport[2] - 0.5f;
fb_viewportcenter[2] = dpsoftrast.fb_height - viewport[1] - 0.5f * viewport[3] - 0.5f;
fb_viewportcenter[3] = 0.5f;
fb_viewportcenter[0] = 0.0f;
fb_viewportscale[1] = 0.5f * viewport[2];
fb_viewportscale[2] = -0.5f * viewport[3];
fb_viewportscale[3] = 0.5f;
fb_viewportscale[0] = 1.0f;
}
static void DPSOFTRAST_RecalcThread(DPSOFTRAST_State_Thread *thread)
{
if (dpsoftrast.interlace)
{
thread->miny1 = (thread->index*dpsoftrast.fb_height)/(2*dpsoftrast.numthreads);
thread->maxy1 = ((thread->index+1)*dpsoftrast.fb_height)/(2*dpsoftrast.numthreads);
thread->miny2 = ((dpsoftrast.numthreads+thread->index)*dpsoftrast.fb_height)/(2*dpsoftrast.numthreads);
thread->maxy2 = ((dpsoftrast.numthreads+thread->index+1)*dpsoftrast.fb_height)/(2*dpsoftrast.numthreads);
}
else
{
thread->miny1 = thread->miny2 = (thread->index*dpsoftrast.fb_height)/dpsoftrast.numthreads;
thread->maxy1 = thread->maxy2 = ((thread->index+1)*dpsoftrast.fb_height)/dpsoftrast.numthreads;
}
}
static void DPSOFTRAST_RecalcClipPlane(DPSOFTRAST_State_Thread *thread)
{
thread->fb_clipplane[0] = thread->clipplane[0] / thread->fb_viewportscale[1];
thread->fb_clipplane[1] = thread->clipplane[1] / thread->fb_viewportscale[2];
thread->fb_clipplane[2] = thread->clipplane[2] / thread->fb_viewportscale[3];
thread->fb_clipplane[3] = thread->clipplane[3] / thread->fb_viewportscale[0];
thread->fb_clipplane[3] -= thread->fb_viewportcenter[1]*thread->fb_clipplane[0] + thread->fb_viewportcenter[2]*thread->fb_clipplane[1] + thread->fb_viewportcenter[3]*thread->fb_clipplane[2] + thread->fb_viewportcenter[0]*thread->fb_clipplane[3];
}
static void DPSOFTRAST_RecalcFB(DPSOFTRAST_State_Thread *thread)
{
// calculate framebuffer scissor, viewport, viewport clipped by scissor,
// and viewport projection values
int x1, x2;
int y1, y2;
x1 = thread->scissor[0];
x2 = thread->scissor[0] + thread->scissor[2];
y1 = dpsoftrast.fb_height - thread->scissor[1] - thread->scissor[3];
y2 = dpsoftrast.fb_height - thread->scissor[1];
if (!thread->scissortest) {x1 = 0;y1 = 0;x2 = dpsoftrast.fb_width;y2 = dpsoftrast.fb_height;}
if (x1 < 0) x1 = 0;
if (x2 > dpsoftrast.fb_width) x2 = dpsoftrast.fb_width;
if (y1 < 0) y1 = 0;
if (y2 > dpsoftrast.fb_height) y2 = dpsoftrast.fb_height;
thread->fb_scissor[0] = x1;
thread->fb_scissor[1] = y1;
thread->fb_scissor[2] = x2 - x1;
thread->fb_scissor[3] = y2 - y1;
DPSOFTRAST_RecalcViewport(thread->viewport, thread->fb_viewportcenter, thread->fb_viewportscale);
DPSOFTRAST_RecalcClipPlane(thread);
DPSOFTRAST_RecalcThread(thread);
}
static void DPSOFTRAST_RecalcDepthFunc(DPSOFTRAST_State_Thread *thread)
{
thread->fb_depthfunc = thread->depthtest ? thread->depthfunc : GL_ALWAYS;
}
static void DPSOFTRAST_RecalcBlendFunc(DPSOFTRAST_State_Thread *thread)
{
if (thread->blendsubtract)
{
switch ((thread->blendfunc[0]<<16)|thread->blendfunc[1])
{
#define BLENDFUNC(sfactor, dfactor, blendmode) \
case (sfactor<<16)|dfactor: thread->fb_blendmode = blendmode; break;
BLENDFUNC(GL_SRC_ALPHA, GL_ONE, DPSOFTRAST_BLENDMODE_SUBALPHA)
default: thread->fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE; break;
}
}
else
{
switch ((thread->blendfunc[0]<<16)|thread->blendfunc[1])
{
BLENDFUNC(GL_ONE, GL_ZERO, DPSOFTRAST_BLENDMODE_OPAQUE)
BLENDFUNC(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, DPSOFTRAST_BLENDMODE_ALPHA)
BLENDFUNC(GL_SRC_ALPHA, GL_ONE, DPSOFTRAST_BLENDMODE_ADDALPHA)
BLENDFUNC(GL_ONE, GL_ONE, DPSOFTRAST_BLENDMODE_ADD)
BLENDFUNC(GL_ZERO, GL_ONE_MINUS_SRC_COLOR, DPSOFTRAST_BLENDMODE_INVMOD)
BLENDFUNC(GL_ZERO, GL_SRC_COLOR, DPSOFTRAST_BLENDMODE_MUL)
BLENDFUNC(GL_DST_COLOR, GL_ZERO, DPSOFTRAST_BLENDMODE_MUL)
BLENDFUNC(GL_DST_COLOR, GL_SRC_COLOR, DPSOFTRAST_BLENDMODE_MUL2)
BLENDFUNC(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, DPSOFTRAST_BLENDMODE_PSEUDOALPHA)
BLENDFUNC(GL_ONE_MINUS_DST_COLOR, GL_ONE, DPSOFTRAST_BLENDMODE_INVADD)
default: thread->fb_blendmode = DPSOFTRAST_BLENDMODE_OPAQUE; break;
}
}
}
#define DPSOFTRAST_ValidateQuick(thread, f) ((thread->validate & (f)) ? (DPSOFTRAST_Validate(thread, f), 0) : 0)
static void DPSOFTRAST_Validate(DPSOFTRAST_State_Thread *thread, int mask)
{
mask &= thread->validate;
if (!mask)
return;
if (mask & DPSOFTRAST_VALIDATE_FB)
{
thread->validate &= ~DPSOFTRAST_VALIDATE_FB;
DPSOFTRAST_RecalcFB(thread);
}
if (mask & DPSOFTRAST_VALIDATE_DEPTHFUNC)
{
thread->validate &= ~DPSOFTRAST_VALIDATE_DEPTHFUNC;
DPSOFTRAST_RecalcDepthFunc(thread);
}
if (mask & DPSOFTRAST_VALIDATE_BLENDFUNC)
{
thread->validate &= ~DPSOFTRAST_VALIDATE_BLENDFUNC;
DPSOFTRAST_RecalcBlendFunc(thread);
}
}
static DPSOFTRAST_Texture *DPSOFTRAST_Texture_GetByIndex(int index)
{
if (index >= 1 && index < dpsoftrast.texture_end && dpsoftrast.texture[index].bytes)
return &dpsoftrast.texture[index];
return NULL;
}
static void DPSOFTRAST_Texture_Grow(void)
{
DPSOFTRAST_Texture *oldtexture = dpsoftrast.texture;
DPSOFTRAST_State_Thread *thread;
int i;
int j;
DPSOFTRAST_Flush();
// expand texture array as needed
if (dpsoftrast.texture_max < 1024)
dpsoftrast.texture_max = 1024;
else
dpsoftrast.texture_max *= 2;
dpsoftrast.texture = (DPSOFTRAST_Texture *)realloc(dpsoftrast.texture, dpsoftrast.texture_max * sizeof(DPSOFTRAST_Texture));
for (i = 0; i < DPSOFTRAST_MAXTEXTUREUNITS; i++)
if (dpsoftrast.texbound[i])
dpsoftrast.texbound[i] = dpsoftrast.texture + (dpsoftrast.texbound[i] - oldtexture);
for (j = 0; j < dpsoftrast.numthreads; j++)
{
thread = &dpsoftrast.threads[j];
for (i = 0; i < DPSOFTRAST_MAXTEXTUREUNITS; i++)
if (thread->texbound[i])
thread->texbound[i] = dpsoftrast.texture + (thread->texbound[i] - oldtexture);
}
}
int DPSOFTRAST_Texture_New(int flags, int width, int height, int depth)
{
int w;
int h;
int d;
int size;
int s;
int texnum;
int mipmaps;
int sides = (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) ? 6 : 1;
int texformat = flags & DPSOFTRAST_TEXTURE_FORMAT_COMPAREMASK;
DPSOFTRAST_Texture *texture;
if (width*height*depth < 1)
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: width, height or depth is less than 1";
return 0;
}
if (width > DPSOFTRAST_TEXTURE_MAXSIZE || height > DPSOFTRAST_TEXTURE_MAXSIZE || depth > DPSOFTRAST_TEXTURE_MAXSIZE)
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: texture size is too large";
return 0;
}
switch(texformat)
{
case DPSOFTRAST_TEXTURE_FORMAT_BGRA8:
case DPSOFTRAST_TEXTURE_FORMAT_RGBA8:
case DPSOFTRAST_TEXTURE_FORMAT_ALPHA8:
break;
case DPSOFTRAST_TEXTURE_FORMAT_DEPTH:
if (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP)
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
return 0;
}
if (depth != 1)
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH only permitted on 2D textures";
return 0;
}
if ((flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && (texformat == DPSOFTRAST_TEXTURE_FORMAT_DEPTH))
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FORMAT_DEPTH does not permit mipmaps";
return 0;
}
break;
}
if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP))
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_CUBEMAP can not be used on 3D textures";
return 0;
}
if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
return 0;
}
if (depth != 1 && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on 3D textures";
return 0;
}
if ((flags & DPSOFTRAST_TEXTURE_FLAG_CUBEMAP) && (flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: DPSOFTRAST_TEXTURE_FLAG_MIPMAP can not be used on cubemap textures";
return 0;
}
if ((width & (width-1)) || (height & (height-1)) || (depth & (depth-1)))
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_New: dimensions are not power of two";
return 0;
}
// find first empty slot in texture array
for (texnum = dpsoftrast.texture_firstfree;texnum < dpsoftrast.texture_end;texnum++)
if (!dpsoftrast.texture[texnum].bytes)
break;
dpsoftrast.texture_firstfree = texnum + 1;
if (dpsoftrast.texture_max <= texnum)
DPSOFTRAST_Texture_Grow();
if (dpsoftrast.texture_end <= texnum)
dpsoftrast.texture_end = texnum + 1;
texture = &dpsoftrast.texture[texnum];
memset(texture, 0, sizeof(*texture));
texture->flags = flags;
texture->width = width;
texture->height = height;
texture->depth = depth;
texture->sides = sides;
texture->binds = 0;
w = width;
h = height;
d = depth;
size = 0;
mipmaps = 0;
for (;;)
{
s = w * h * d * sides * 4;
texture->mipmap[mipmaps][0] = size;
texture->mipmap[mipmaps][1] = s;
texture->mipmap[mipmaps][2] = w;
texture->mipmap[mipmaps][3] = h;
texture->mipmap[mipmaps][4] = d;
size += s;
mipmaps++;
if (w * h * d == 1 || !(flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP))
break;
if (w > 1) w >>= 1;
if (h > 1) h >>= 1;
if (d > 1) d >>= 1;
}
texture->mipmaps = mipmaps;
texture->size = size;
// allocate the pixels now
texture->bytes = (unsigned char *)MM_CALLOC(1, size);
return texnum;
}
void DPSOFTRAST_Texture_Free(int index)
{
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
if (texture->binds)
DPSOFTRAST_Flush();
if (texture->bytes)
MM_FREE(texture->bytes);
texture->bytes = NULL;
memset(texture, 0, sizeof(*texture));
// adjust the free range and used range
if (dpsoftrast.texture_firstfree > index)
dpsoftrast.texture_firstfree = index;
while (dpsoftrast.texture_end > 0 && dpsoftrast.texture[dpsoftrast.texture_end-1].bytes == NULL)
dpsoftrast.texture_end--;
}
static void DPSOFTRAST_Texture_CalculateMipmaps(int index)
{
int i, x, y, z, w, layer0, layer1, row0, row1;
unsigned char *o, *i0, *i1, *i2, *i3;
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
if (texture->mipmaps <= 1)
return;
for (i = 1;i < texture->mipmaps;i++)
{
for (z = 0;z < texture->mipmap[i][4];z++)
{
layer0 = z*2;
layer1 = z*2+1;
if (layer1 >= texture->mipmap[i-1][4])
layer1 = texture->mipmap[i-1][4]-1;
for (y = 0;y < texture->mipmap[i][3];y++)
{
row0 = y*2;
row1 = y*2+1;
if (row1 >= texture->mipmap[i-1][3])
row1 = texture->mipmap[i-1][3]-1;
o = texture->bytes + texture->mipmap[i ][0] + 4*((texture->mipmap[i ][3] * z + y ) * texture->mipmap[i ][2]);
i0 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row0) * texture->mipmap[i-1][2]);
i1 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer0 + row1) * texture->mipmap[i-1][2]);
i2 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row0) * texture->mipmap[i-1][2]);
i3 = texture->bytes + texture->mipmap[i-1][0] + 4*((texture->mipmap[i-1][3] * layer1 + row1) * texture->mipmap[i-1][2]);
w = texture->mipmap[i][2];
if (layer1 > layer0)
{
if (texture->mipmap[i-1][2] > 1)
{
// average 3D texture
for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8, i2 += 8, i3 += 8)
{
o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + i2[0] + i2[4] + i3[0] + i3[4] + 4) >> 3;
o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + i2[1] + i2[5] + i3[1] + i3[5] + 4) >> 3;
o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + i2[2] + i2[6] + i3[2] + i3[6] + 4) >> 3;
o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + i2[3] + i2[7] + i3[3] + i3[7] + 4) >> 3;
}
}
else
{
// average 3D mipmap with parent width == 1
for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
{
o[0] = (i0[0] + i1[0] + i2[0] + i3[0] + 2) >> 2;
o[1] = (i0[1] + i1[1] + i2[1] + i3[1] + 2) >> 2;
o[2] = (i0[2] + i1[2] + i2[2] + i3[2] + 2) >> 2;
o[3] = (i0[3] + i1[3] + i2[3] + i3[3] + 2) >> 2;
}
}
}
else
{
if (texture->mipmap[i-1][2] > 1)
{
// average 2D texture (common case)
for (x = 0;x < w;x++, o += 4, i0 += 8, i1 += 8)
{
o[0] = (i0[0] + i0[4] + i1[0] + i1[4] + 2) >> 2;
o[1] = (i0[1] + i0[5] + i1[1] + i1[5] + 2) >> 2;
o[2] = (i0[2] + i0[6] + i1[2] + i1[6] + 2) >> 2;
o[3] = (i0[3] + i0[7] + i1[3] + i1[7] + 2) >> 2;
}
}
else
{
// 2D texture with parent width == 1
o[0] = (i0[0] + i1[0] + 1) >> 1;
o[1] = (i0[1] + i1[1] + 1) >> 1;
o[2] = (i0[2] + i1[2] + 1) >> 1;
o[3] = (i0[3] + i1[3] + 1) >> 1;
}
}
}
}
}
}
void DPSOFTRAST_Texture_UpdatePartial(int index, int mip, const unsigned char *pixels, int blockx, int blocky, int blockwidth, int blockheight)
{
DPSOFTRAST_Texture *texture;
unsigned char *dst;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
if (texture->binds)
DPSOFTRAST_Flush();
if (pixels)
{
dst = texture->bytes + texture->mipmap[0][1] +(-blocky * texture->mipmap[0][2] + blockx) * 4;
while (blockheight > 0)
{
dst -= texture->mipmap[0][2] * 4;
memcpy(dst, pixels, blockwidth * 4);
pixels += blockwidth * 4;
blockheight--;
}
}
DPSOFTRAST_Texture_CalculateMipmaps(index);
}
void DPSOFTRAST_Texture_UpdateFull(int index, const unsigned char *pixels)
{
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
if (texture->binds)
DPSOFTRAST_Flush();
if (pixels)
{
int i, stride = texture->mipmap[0][2]*4;
unsigned char *dst = texture->bytes + texture->mipmap[0][1];
for (i = texture->mipmap[0][3];i > 0;i--)
{
dst -= stride;
memcpy(dst, pixels, stride);
pixels += stride;
}
}
DPSOFTRAST_Texture_CalculateMipmaps(index);
}
int DPSOFTRAST_Texture_GetWidth(int index, int mip)
{
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
return texture->mipmap[mip][2];
}
int DPSOFTRAST_Texture_GetHeight(int index, int mip)
{
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
return texture->mipmap[mip][3];
}
int DPSOFTRAST_Texture_GetDepth(int index, int mip)
{
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
return texture->mipmap[mip][4];
}
unsigned char *DPSOFTRAST_Texture_GetPixelPointer(int index, int mip)
{
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return 0;
if (texture->binds)
DPSOFTRAST_Flush();
return texture->bytes + texture->mipmap[mip][0];
}
void DPSOFTRAST_Texture_Filter(int index, DPSOFTRAST_TEXTURE_FILTER filter)
{
DPSOFTRAST_Texture *texture;
texture = DPSOFTRAST_Texture_GetByIndex(index);if (!texture) return;
if (!(texture->flags & DPSOFTRAST_TEXTURE_FLAG_MIPMAP) && filter > DPSOFTRAST_TEXTURE_FILTER_LINEAR)
{
dpsoftrast.errorstring = "DPSOFTRAST_Texture_Filter: requested filter mode requires mipmaps";
return;
}
if (texture->binds)
DPSOFTRAST_Flush();
texture->filter = filter;
}
static void DPSOFTRAST_Draw_FlushThreads(void);
static void DPSOFTRAST_Draw_SyncCommands(void)
{
if(dpsoftrast.usethreads) MEMORY_BARRIER;
dpsoftrast.drawcommand = dpsoftrast.commandpool.freecommand;
}
static void DPSOFTRAST_Draw_FreeCommandPool(int space)
{
DPSOFTRAST_State_Thread *thread;
int i;
int freecommand = dpsoftrast.commandpool.freecommand;
int usedcommands = dpsoftrast.commandpool.usedcommands;
if (usedcommands <= DPSOFTRAST_DRAW_MAXCOMMANDPOOL-space)
return;
DPSOFTRAST_Draw_SyncCommands();
for(;;)
{
int waitindex = -1;
int commandoffset;
usedcommands = 0;
for (i = 0; i < dpsoftrast.numthreads; i++)
{
thread = &dpsoftrast.threads[i];
commandoffset = freecommand - thread->commandoffset;
if (commandoffset < 0)
commandoffset += DPSOFTRAST_DRAW_MAXCOMMANDPOOL;
if (commandoffset > usedcommands)
{
waitindex = i;
usedcommands = commandoffset;
}
}
if (usedcommands <= DPSOFTRAST_DRAW_MAXCOMMANDPOOL-space || waitindex < 0)
break;
thread = &dpsoftrast.threads[waitindex];
Thread_LockMutex(thread->drawmutex);
if (thread->commandoffset != dpsoftrast.drawcommand)
{
thread->waiting = true;
if (thread->starving) Thread_CondSignal(thread->drawcond);
Thread_CondWait(thread->waitcond, thread->drawmutex);
thread->waiting = false;
}
Thread_UnlockMutex(thread->drawmutex);
}
dpsoftrast.commandpool.usedcommands = usedcommands;
}
#define DPSOFTRAST_ALIGNCOMMAND(size) \
((size) + ((COMMAND_SIZE - ((size)&(COMMAND_SIZE-1))) & (COMMAND_SIZE-1)))
#define DPSOFTRAST_ALLOCATECOMMAND(name) \
((DPSOFTRAST_Command_##name *) DPSOFTRAST_AllocateCommand( DPSOFTRAST_OPCODE_##name , DPSOFTRAST_ALIGNCOMMAND(sizeof( DPSOFTRAST_Command_##name ))))
static void *DPSOFTRAST_AllocateCommand(int opcode, int size)
{
DPSOFTRAST_Command *command;
int freecommand = dpsoftrast.commandpool.freecommand;
int usedcommands = dpsoftrast.commandpool.usedcommands;
int extra = sizeof(DPSOFTRAST_Command);
if (DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand < size)
extra += DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand;
if (usedcommands > DPSOFTRAST_DRAW_MAXCOMMANDPOOL - (size + extra))
{
if (dpsoftrast.usethreads)
DPSOFTRAST_Draw_FreeCommandPool(size + extra);
else
DPSOFTRAST_Draw_FlushThreads();
freecommand = dpsoftrast.commandpool.freecommand;
usedcommands = dpsoftrast.commandpool.usedcommands;
}
if (DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand < size)
{
command = (DPSOFTRAST_Command *) &dpsoftrast.commandpool.commands[freecommand];
command->opcode = DPSOFTRAST_OPCODE_Reset;
usedcommands += DPSOFTRAST_DRAW_MAXCOMMANDPOOL - freecommand;
freecommand = 0;
}
command = (DPSOFTRAST_Command *) &dpsoftrast.commandpool.commands[freecommand];
command->opcode = opcode;
command->commandsize = size;
freecommand += size;
if (freecommand >= DPSOFTRAST_DRAW_MAXCOMMANDPOOL)
freecommand = 0;
dpsoftrast.commandpool.freecommand = freecommand;
dpsoftrast.commandpool.usedcommands = usedcommands + size;
return command;
}
static void DPSOFTRAST_UndoCommand(int size)
{
int freecommand = dpsoftrast.commandpool.freecommand;
int usedcommands = dpsoftrast.commandpool.usedcommands;
freecommand -= size;
if (freecommand < 0)
freecommand += DPSOFTRAST_DRAW_MAXCOMMANDPOOL;
usedcommands -= size;
dpsoftrast.commandpool.freecommand = freecommand;
dpsoftrast.commandpool.usedcommands = usedcommands;
}
DEFCOMMAND(1, Viewport, int x; int y; int width; int height;)
static void DPSOFTRAST_Interpret_Viewport(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_Command_Viewport *command)
{
thread->viewport[0] = command->x;
thread->viewport[1] = command->y;
thread->viewport[2] = command->width;
thread->viewport[3] = command->height;
thread->validate |= DPSOFTRAST_VALIDATE_FB;
}
void DPSOFTRAST_Viewport(int x, int y, int width, int height)
{
DPSOFTRAST_Command_Viewport *command = DPSOFTRAST_ALLOCATECOMMAND(Viewport);
command->x = x;
command->y = y;
command->width = width;
command->height = height;
dpsoftrast.viewport[0] = x;
dpsoftrast.viewport[1] = y;
dpsoftrast.viewport[2] = width;
dpsoftrast.viewport[3] = height;
DPSOFTRAST_RecalcViewport(dpsoftrast.viewport, dpsoftrast.fb_viewportcenter, dpsoftrast.fb_viewportscale);
}
DEFCOMMAND(2, ClearColor, float r; float g; float b; float a;)
static void DPSOFTRAST_Interpret_ClearColor(DPSOFTRAST_State_Thread *thread, const DPSOFTRAST_Command_ClearColor *command)
{
int i, x1, y1, x2, y2, w, h, x, y;
int miny1, maxy1, miny2, maxy2;
int bandy;
unsigned int *p;
unsigned int c;
DPSOFTRAST_Validate(thread, DPSOFTRAST_VALIDATE_FB);
miny1 = thread->miny1;
maxy1 = thread->maxy1;
miny2 = thread->miny2;
maxy2 = thread->maxy2;
x1 = thread->fb_scissor[0];
y1 = thread->fb_scissor[1];
x2 = thread->fb_scissor[0] + thread->fb_scissor[2];
y2 = thread->fb_scissor[1] + thread->fb_scissor[3];
if (y1 < miny1) y1 = miny1;
if (y2 > maxy2) y2 = maxy2;
w = x2 - x1;
h = y2 - y1;
if (w < 1 || h < 1)
return;
// FIXME: honor fb_colormask?
c = DPSOFTRAST_BGRA8_FROM_RGBA32F(command->r,command->g,command->b,command->a);
for (i = 0;i < 4;i++)
{
if (!dpsoftrast.fb_colorpixels[i])
continue;
for (y = y1, bandy = min(y2, maxy1); y < y2; bandy = min(y2, maxy2), y = max(y, miny2))
for (;y < bandy;y++)
{
p = dpsoftrast.fb_colorpixels[i] + y * dpsoftrast.fb_width;
for (x = x1;x < x2;x++)
p[x] = c;
}
}
}
void DPSOFTRAST_ClearColor(float r, float g, float b, float a)
{
DPSOFTRAST_Command_ClearColor *command = DPSOFTRAST_ALLOCATECOMMAND(ClearColor);
command->r = r;
command->g = g;
command->b = b;
command->a = a;
}
DEFCOMMAND(3, ClearDepth, float depth;)
static void DPSOFTRAST_Interpret_ClearDepth(DPSOFTRAST_State_Thread *thread, DPSOFTRAST_Command_ClearDepth *command)
{
int x1, y1, x2, y2, w, h, x, y;
int miny1, maxy1, miny2, maxy2;
int bandy;
unsigned int *p;
unsigned int c;
DPSOFTRAST_Validate(thread, DPSOFTRAST_VALIDATE_FB);
miny1 = thread->miny1;