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