Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2271 leency 1
;TEX_X = 512
2
;TEX_Y = 512
3
;ROUND equ 8
4
;SIZE_X = 512
5
;SIZE_Y = 512
6
;TEX_SHIFT = 9
7
CATMULL_SHIFT equ 8
8
 
9
;------------------------------------------------------------------------
10
;- Procedure drawing textured triangle using Catmull Z-buffer algorithm -
11
;------------------------------------------------------------------------
12
tex_triangle_z:
13
;----------in - eax - x1 shl 16 + y1
14
;-------------- ebx - x2 shl 16 + y2
15
;---------------ecx - x3 shl 16 + y3
16
;---------------edx - pointer to Z-buffer
17
;---------------esi - pointer to texture buffer
18
;---------------edi - pointer to screen buffer
19
;-------------stack - texture coordinates
20
;------------------ - z coordinates
21
.tex_x1 equ  ebp+4
22
.tex_y1 equ  ebp+6
23
.tex_x2 equ  ebp+8
24
.tex_y2 equ  ebp+10
25
.tex_x3 equ  ebp+12
26
.tex_y3 equ  ebp+14
27
.z1	equ  word[ebp+16]
28
.z2	equ  word[ebp+18]
29
.z3	equ  word[ebp+20]
30
 
31
.tex_ptr equ dword[ebp-4]	 ; pointer to texture
32
.z_ptr	 equ dword[ebp-8]	 ; pointer to z-buffer
33
.x1	 equ word[ebp-10]
34
.y1	 equ word[ebp-12]
35
.x2	 equ word[ebp-14]
36
.y2	 equ word[ebp-16]
37
.x3	 equ word[ebp-18]
38
.y3	 equ word[ebp-20]
39
 
40
.dx12	  equ dword[ebp-24]
41
.tex_dx12 equ dword[ebp-28]
42
.tex_dy12 equ dword[ebp-32]
43
.dz12	  equ dword[ebp-36]
44
 
45
.dx13	  equ dword[ebp-40]
46
.tex_dx13 equ dword[ebp-44]
47
.tex_dy13 equ dword[ebp-48]
48
.dz13	  equ dword[ebp-52]
49
 
50
.dx23	  equ dword[ebp-56]
51
.tex_dx23 equ dword[ebp-60]
52
.tex_dy23 equ dword[ebp-64]
53
.dz23	  equ dword[ebp-68]
54
 
55
.scan_x1  equ dword[ebp-72]
56
.scan_x2  equ dword[ebp-76]
57
.scan_y1  equ dword[ebp-80]
58
.scan_y2  equ dword[ebp-84]
59
.cz1	  equ dword[ebp-88]
60
.cz2	  equ dword[ebp-92]
61
 
62
	mov	ebp,esp
63
	push	esi		  ; store memory pointers
64
	push	edx
65
.tt_sort3:
66
	cmp	ax,bx			  ;sort all parameters
67
	jle	.tt_sort1
68
	xchg	eax,ebx
69
	mov	edx,dword [.tex_x1]
70
	xchg	edx,dword [.tex_x2]
71
	mov	dword[.tex_x1],edx
72
	mov	dx,.z1
73
	xchg	dx,.z2
74
	mov	.z1,dx
75
.tt_sort1:
76
	cmp	bx,cx
77
	jle	.tt_sort2
78
	xchg	ebx,ecx
79
	mov	edx,dword [.tex_x2]
80
	xchg	edx,dword [.tex_x3]
81
	mov	dword [.tex_x2],edx
82
	mov	dx,.z2
83
	xchg	dx,.z3
84
	mov	.z2,dx
85
	jmp	.tt_sort3
86
.tt_sort2:
87
 
88
	push	eax	; and store to user friendly variables
89
	push	ebx
90
	push	ecx
91
 
92
	mov	 edx,80008000h	; eax,ebx,ecx are ANDd together into edx which means that
93
	and	 edx,ebx	; if *all* of them are negative a sign flag is raised
94
	and	 edx,ecx
95
	and	 edx,eax
96
	test	 edx,80008000h	; Check both X&Y at once
97
	jne	 .tt_loop2_end
98
	cmp	 ax,SIZE_Y
99
	jl	 @f
100
	cmp	 bx,SIZE_Y
101
	jl	 @f
102
	cmp	 cx,SIZE_Y
103
	jl	 @f
104
	ror	 eax,16
105
	ror	 ebx,16
106
	ror	 ecx,16
107
	cmp	 ax,SIZE_X
108
	jl	 @f
109
	cmp	 bx,SIZE_X
110
	jl	 @f
111
	cmp	 cx,SIZE_X
112
	jl	 @f
113
	jmp	 .tt_loop2_end
114
     @@:
115
	mov	 eax,dword[.tex_x1] ; texture coords must be in [0..TEX_X(Y)]
116
	mov	 ebx,dword[.tex_x2]
117
	mov	 ecx,dword[.tex_x3]
118
	mov	 edx,eax
119
	or	 edx,ebx
120
	or	 edx,ecx
121
	test	 edx,80008000h
122
	jne	 .tt_loop2_end
123
	cmp	 ax,TEX_X
124
	jge	 .tt_loop2_end
125
	cmp	 bx,TEX_X
126
	jge	 .tt_loop2_end
127
	cmp	 cx,TEX_X
128
	jge	 .tt_loop2_end
129
	ror	 eax,16
130
	ror	 ebx,16
131
	ror	 ecx,16
132
	cmp	 ax,TEX_Y
133
	jge	 .tt_loop2_end
134
	cmp	 bx,TEX_Y
135
	jge	 .tt_loop2_end
136
	cmp	 cx,TEX_Y
137
	jge	 .tt_loop2_end
138
 
139
 
140
	movsx	 ebx,.y2    ; calc delta
141
	sub	 bx,.y1
142
	jnz	 .tt_dx12_make
143
	xor	 edx,edx
144
	mov	 ecx,4
145
    @@:
146
	push	 edx
147
	loop	 @b
148
	jmp	 .tt_dx12_done
149
    .tt_dx12_make:
150
	mov	 ax,.x2
151
	sub	 ax,.x1
152
	cwde
153
	shl	 eax,ROUND
154
	cdq
155
	idiv	 ebx
156
       ; mov      .dx12,eax                      ; dx12 = (x2-x1)/(y2-y1)
157
	push	 eax
158
 
159
	mov	 ax,word[.tex_x2]
160
	sub	 ax,word[.tex_x1]
161
	cwde
162
	shl	 eax,ROUND
163
	cdq
164
	idiv	 ebx
165
       ; mov     [.tex_dx12],eax               ; tex_dx12 = (tex_x2-tex_x1)/(y2-y1)
166
	push	 eax
167
 
168
	mov	 ax,word[.tex_y2]
169
	sub	 ax,word[.tex_y1]
170
	cwde
171
	shl	 eax,ROUND
172
	cdq
173
	idiv	 ebx
174
     ;   mov    [.tex_dy12],eax              ; tex_dy12 = (tex_y2-tex_y1)/(y2-y1)
175
	push	 eax
176
 
177
	mov	 ax,.z2
178
	sub	 ax,.z1
179
	cwde
180
	shl	 eax,CATMULL_SHIFT
181
	cdq
182
	idiv	 ebx
183
	push	 eax
184
   .tt_dx12_done:
185
 
186
	movsx	 ebx,.y3    ; calc delta
187
	sub	 bx,.y1
188
	jnz	 .tt_dx13_make
189
	xor	 edx,edx
190
	mov	 ecx,4
191
    @@:
192
	push	 edx
193
	loop	 @b
194
	jmp	 .tt_dx13_done
195
    .tt_dx13_make:
196
	mov	 ax,.x3
197
	sub	 ax,.x1
198
	cwde
199
	shl	 eax,ROUND
200
	cdq
201
	idiv	 ebx
202
       ; mov      .dx12,eax                      ; dx13 = (x3-x1)/(y3-y1)
203
	push	  eax
204
 
205
	mov	 ax,word[.tex_x3]
206
	sub	 ax,word[.tex_x1]
207
	cwde
208
	shl	 eax,ROUND
209
	cdq
210
	idiv	 ebx
211
       ; mov     [.tex_dx12],eax               ; tex_dx13 = (tex_x3-tex_x1)/(y3-y1)
212
	push	 eax
213
 
214
	mov	 ax,word[.tex_y3]
215
	sub	 ax,word[.tex_y1]
216
	cwde
217
	shl	 eax,ROUND
218
	cdq
219
	idiv	 ebx
220
     ;   mov    [.tex_dy12],eax              ; tex_dy13 = (tex_y3-tex_y1)/(y3-y1)
221
	push	 eax
222
 
223
	mov	 ax,.z3
224
	sub	 ax,.z1
225
	cwde
226
	shl	 eax,CATMULL_SHIFT
227
	cdq
228
	idiv	 ebx
229
	push	 eax
230
   .tt_dx13_done:
231
 
232
	mov	 bx,.y3    ; calc delta
233
	sub	 bx,.y2
234
	jnz	 .tt_dx23_make
235
	xor	 edx,edx
236
	mov	 ecx,4
237
    @@:
238
	push	 edx
239
	loop	 @b
240
	jmp	 .tt_dx23_done
241
    .tt_dx23_make:
242
	mov	 ax,.x3
243
	sub	 ax,.x2
244
	cwde
245
	shl	 eax,ROUND
246
	cdq
247
	movzx	 ebx,bx
248
	idiv	 ebx
249
       ; mov      .dx23,eax                      ; dx23 = (x3-x2)/(y3-y2)
250
	push	  eax
251
 
252
	mov	 ax,word[.tex_x3]
253
	sub	 ax,word[.tex_x2]
254
	cwde
255
	shl	 eax,ROUND
256
	cdq
257
	idiv	 ebx
258
       ; mov     [.tex_dx23],eax               ; tex_dx23 = (tex_x3-tex_x2)/(y3-y2)
259
	push	 eax
260
 
261
	mov	 ax,word[.tex_y3]
262
	sub	 ax,word[.tex_y2]
263
	cwde
264
	shl	 eax,ROUND
265
	cdq
266
	idiv	 ebx
267
     ;   mov    [.tex_dy23],eax              ; tex_dy23 = (tex_y3-tex_y2)/(y3-y2)
268
	push	 eax
269
 
270
	mov	 ax,.z3
271
	sub	 ax,.z2
272
	cwde
273
	shl	 eax,CATMULL_SHIFT
274
	cdq
275
	idiv	 ebx
276
	push	 eax
277
   .tt_dx23_done:
278
 
279
	movsx	 eax,.x1     ;eax - cur x1
280
	shl	 eax,ROUND   ;ebx - cur x2
281
	mov	 ebx,eax
282
 
283
	movsx	 edx, word[.tex_x1]
284
	shl	 edx,ROUND
285
      ;  mov [.scan_x1],edx
286
      ;  mov [.scan_x2],edx
287
	push	 edx
288
	push	 edx
289
	movsx	 edx, word[.tex_y1]
290
	shl	 edx,ROUND
291
       ; mov [.scan_y1],edx
292
       ; mov [.scan_y2],edx
293
	push	 edx
294
	push	 edx
295
	movsx	 edx,.z1
296
	shl	 edx,CATMULL_SHIFT
297
	push	 edx
298
	push	 edx
299
	mov	 cx,.y1
300
	cmp	 cx,.y2
301
	jge	 .tt_loop1_end
302
 
303
   .tt_loop1:
304
	pushad
305
 
306
	push	.z_ptr
307
	push	.cz1	  ; z coords shifted shl catmull_shift
308
	push	.cz2
309
	mov	edx, .scan_y2
310
	sar	edx, ROUND
311
	push	dx
312
	mov	edx, .scan_x2
313
	sar	edx, ROUND
314
	push	dx
315
	mov	edx, .scan_y1
316
	sar	edx, ROUND
317
	push	dx
318
	mov	edx, .scan_x1
319
	sar	edx, ROUND
320
	push	dx
321
	push	esi		  ;[.tex_ptr]
322
 
323
	push	cx
324
	sar	ebx,ROUND
325
	push	bx
326
	sar	eax,ROUND
327
	push	ax
328
	call	textured_line_z
329
 
330
	popad
331
	mov	edx,.dz13
332
	add	.cz1,edx
333
	mov	edx,.dz12
334
	add	.cz2,edx
335
 
336
	mov	edx, .tex_dx13
337
	add	.scan_x1, edx
338
	mov	edx, .tex_dx12
339
	add	.scan_x2, edx
340
	mov	edx, .tex_dy13
341
	add	.scan_y1, edx
342
	mov	edx, .tex_dy12
343
	add	.scan_y2, edx
344
 
345
	add	eax, .dx13
346
	add	ebx, .dx12
347
	inc	cx
348
	cmp	cx,.y2
349
	jl	.tt_loop1
350
 
351
  .tt_loop1_end:
352
 
353
 
354
	mov	cx,.y2
355
	cmp	cx,.y3
356
	jge	.tt_loop2_end
357
 
358
	movsx	ebx,.x2
359
	shl	ebx,ROUND
360
	movsx	edx,.z2
361
	shl	edx,CATMULL_SHIFT
362
	mov	.cz2,edx
363
	movzx	edx, word [.tex_x2]
364
	shl	edx,ROUND
365
	mov	.scan_x2,edx
366
	movzx	edx, word[.tex_y2]
367
	shl	edx,ROUND
368
	mov	.scan_y2,edx
369
 
370
.tt_loop2:
371
 
372
	pushad
373
 
374
	push	.z_ptr
375
	push	.cz1	  ; z coords shifted shl catmull_shift
376
	push	.cz2
377
	mov	edx, .scan_y2
378
	sar	edx, ROUND
379
	push	dx
380
	mov	edx, .scan_x2
381
	sar	edx, ROUND
382
	push	dx
383
	mov	edx, .scan_y1
384
	sar	edx, ROUND
385
	push	dx
386
	mov	edx, .scan_x1
387
	sar	edx, ROUND
388
	push	dx
389
	push	esi		  ;[.tex_ptr]
390
 
391
	push	cx
392
	sar	ebx,ROUND
393
	push	bx
394
	sar	eax,ROUND
395
	push	ax
396
	call	textured_line_z
397
 
398
	popad
399
 
400
 
401
	mov	edx,.dz13
402
	add	.cz1,edx
403
	mov	edx,.dz23
404
	add	.cz2,edx
405
 
406
	mov	edx, .tex_dx13
407
	add	.scan_x1, edx
408
	mov	edx, .tex_dx23
409
	add	.scan_x2, edx
410
	mov	edx, .tex_dy13
411
	add	.scan_y1, edx
412
	mov	edx, .tex_dy23
413
	add	.scan_y2, edx
414
 
415
	add	eax, .dx13
416
	add	ebx, .dx23
417
	inc	cx
418
	cmp	cx,.y3
419
	jl	.tt_loop2
420
 
421
.tt_loop2_end:
422
 
423
.tt_end:
424
	mov esp,ebp
425
ret 18
426
 
427
textured_line_z:
428
;-----in -edi screen buffer pointer
429
;------------ stack:
430
  .x1 equ word [ebp+4]
431
  .x2 equ word [ebp+6]
432
  .y  equ word [ebp+8]
433
 
434
  .tex_ptr equ dword [ebp+10]
435
  .tex_x1  equ ebp+14
436
  .tex_y1  equ ebp+16
437
  .tex_x2  equ ebp+18
438
  .tex_y2  equ ebp+20
439
  .z2	   equ dword [ebp+22]	  ;z1, z2  coords shifted shl CATMULL_SHIFT
440
  .z1	   equ dword [ebp+26]
441
  .z_ptr   equ dword [ebp+30]
442
 
443
  .tex_dx  equ dword [ebp-4]
444
  .tex_dy  equ dword [ebp-8]
445
  .dz	   equ dword [ebp-12]
446
  .cz	   equ dword [ebp-16]
447
  .c_tex_x equ dword [ebp-20]  ; current tex x
448
 
449
 
450
	mov	ebp,esp
451
 
452
	mov	ax,.y
453
	or	ax,ax
454
	jl	.tl_quit
455
	cmp	ax,SIZE_Y
456
	jge	.tl_quit
457
 
458
	mov	ax,.x1
459
	cmp	ax,.x2
460
	je	.tl_quit
461
	jl	.tl_ok
462
 
463
	xchg	ax,.x2	  ; sort params
464
	mov	.x1,ax
465
 
466
	mov	eax,dword[.tex_x1]
467
	xchg	eax,dword[.tex_x2]
468
	mov	dword[.tex_x1],eax
469
 
470
	mov	eax,.z1
471
	xchg	eax,.z2
472
	mov	.z1,eax
473
     .tl_ok:
474
	cmp	.x1,SIZE_X
475
	jge	.tl_quit
476
	cmp	.x2,0
477
	jle	.tl_quit
478
 
479
	mov	bx,.x2
480
	sub	bx,.x1
481
	movsx	ebx,bx
482
 
483
	mov	ax,word[.tex_x2]       ; calc .dtx
484
	sub	ax,word[.tex_x1]
485
	cwde
486
	shl	eax,ROUND
487
	cdq
488
	idiv	ebx
489
	push	eax
490
 
491
	mov	ax,word[.tex_y2]       ; calc .dty
492
	sub	ax,word[.tex_y1]
493
	cwde
494
	shl	eax,ROUND
495
	cdq
496
	idiv	ebx
497
	push	eax
498
 
499
	mov	eax,.z2       ; calc .dz
500
	sub	eax,.z1
501
	cdq
502
	idiv	ebx
503
	push	eax
504
 
505
	cmp    .x1,0	      ; clipping
506
	jge    @f
507
 
508
	movsx	ebx,.x1
509
	neg	ebx
510
	imul	ebx	      ; eax = .dz * abs(.x1)
511
	add	.z1,eax
512
	mov	.x1,0
513
 
514
	mov	eax,.tex_dy
515
	sar	eax,ROUND
516
	imul	ebx
517
	add	word[.tex_y1],ax
518
 
519
	mov	eax,.tex_dx
520
	sar	eax,ROUND
521
	imul	ebx
522
	add	word[.tex_x1],ax
523
      @@:
524
	cmp	.x2,SIZE_X
525
	jl	@f
526
	mov	.x2,SIZE_X
527
      @@:
528
 
529
	movsx	ebx,.y	      ; calc mem begin in buffers
530
	mov	eax,SIZE_X
531
	mul	ebx
532
	movsx	ebx,.x1
533
	add	eax,ebx
534
	mov	ebx,eax
535
 
536
	lea	eax,[eax*3]
537
	add	edi,eax 	      ; edi - scr buff
538
	shl	ebx,2
539
	add	.z_ptr,ebx	      ; z buffer pointer
540
 
541
	mov	cx,.x2
542
	sub	cx,.x1
543
	movzx	ecx,cx
544
 
545
 
546
	movzx	eax,word[.tex_x1]
547
	shl	eax,ROUND
548
	movzx	ebx,word[.tex_y1]
549
	shl	ebx,ROUND
550
	push	.z1	      ; .cz
551
	push	eax	      ;.c_tex_x
552
     .tl_loop:
553
	mov	edx,.z_ptr			;  eax - temp
554
	mov	eax,.cz 			;  ebx - cur tex y shl ROUND
555
	cmp	eax,[edx]			;  ecx - l.lenght
556
	jge	.skip	      ; ebx - cur tex_y ;  edx - temp
557
	mov	esi,ebx 			;  edi - scr buff
558
	sar	esi,ROUND			;  esi - tex_ptr temp
559
	shl	esi,TEX_SHIFT			;  .z_ptr - cur pointer to z buff
560
	mov	eax,.c_tex_x			;  .cz - cur z coord shl CATMULL_SHIFT
561
	sar	eax,ROUND
562
	add	esi,eax
563
	and	esi,TEXTURE_SIZE
564
	lea	esi,[esi*3]
565
	add	esi,.tex_ptr
566
	movsd
567
	dec	edi
568
	mov	eax,.cz
569
	mov	esi,.z_ptr
570
	mov	dword[esi],eax	  ; renew z buffer
571
	jmp	.no_skip
572
     .skip:
573
	add	edi,3
574
     .no_skip:
575
	add	.z_ptr,4
576
	mov	eax,.dz
577
	add	.cz,eax
578
	mov	eax,.tex_dx
579
	add	.c_tex_x,eax
580
	add	ebx,.tex_dy
581
	loop	.tl_loop
582
 .tl_quit:
583
	mov	esp,ebp
584
ret 30
585