Subversion Repositories Kolibri OS

Rev

Rev 6172 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;
  2. ;
  3. ; Z buffer: 16 bits Z / 16 bits color
  4. ;
  5. ;
  6. ;include 'zbuffer.inc'
  7.  
  8. ;output:
  9. ; eax - указатель на ZBuffer (0 если не удача)
  10. align 16
  11. proc ZB_open uses ecx edi, xsize:dword, ysize:dword, mode:dword,\
  12.                 nb_colors:dword, color_indexes:dword, color_table:dword, frame_buffer:dword
  13.  
  14.         stdcall gl_malloc, sizeof.ZBuffer
  15.         cmp eax,0
  16.         jne @f
  17.                 stdcall dbg_print,f_zb_opn,err_1
  18.                 jmp .end_f
  19.         @@:
  20.         mov edi,eax
  21.  
  22.         mov eax,[ysize]
  23.         mov [edi+ZBuffer.ysize],eax
  24.         mov eax,[xsize]
  25.         mov [edi+ZBuffer.xsize],eax
  26.         imul eax,PSZB
  27.         mov [edi+ZBuffer.linesize],eax
  28.         mov eax,[mode]
  29.         mov [edi+ZBuffer.mode],eax
  30.  
  31. if TGL_FEATURE_32_BITS eq 1
  32.         cmp eax,ZB_MODE_RGBA
  33.         je .correct
  34. end if
  35. if TGL_FEATURE_24_BITS eq 1
  36.         cmp eax,ZB_MODE_RGB24
  37.         je .correct
  38. end if
  39.         cmp eax,ZB_MODE_5R6G5B
  40.         jne @f
  41.         .correct:
  42.                 mov dword[edi+ZBuffer.nb_colors],0
  43.                 jmp .end_s
  44.         @@: ;default:
  45.                 stdcall dbg_print,f_zb_opn,err_3
  46.                 jmp .error
  47.         .end_s:
  48.  
  49.         mov ecx,[edi+ZBuffer.xsize]
  50.         imul ecx,[edi+ZBuffer.ysize]
  51.         shl ecx,1 ;*= sizeof(unsigned short)
  52.  
  53.         stdcall gl_malloc, ecx
  54.         mov [edi+ZBuffer.zbuf],eax
  55.         cmp eax,0
  56.         jne @f
  57.                 stdcall dbg_print,f_zb_opn,err_2
  58.                 jmp .error
  59.         @@:
  60.  
  61.         mov dword[edi+ZBuffer.frame_buffer_allocated],0
  62.         mov dword[edi+ZBuffer.pbuf],0 ;NULL
  63.  
  64.         mov dword[edi+ZBuffer.current_texture],0 ;NULL
  65.  
  66.         mov eax,edi
  67.         jmp .end_f
  68.         .error:
  69.                 stdcall gl_free,edi
  70.                 xor eax,eax
  71.         .end_f:
  72.         ret
  73. endp
  74.  
  75. ;void ZB_close(ZBuffer * zb)
  76. ;{
  77. ;    if (zb->frame_buffer_allocated)
  78. ;       gl_free(zb->pbuf);
  79.  
  80. ;    gl_free(zb->zbuf);
  81. ;    gl_free(zb);
  82. ;}
  83.  
  84. align 16
  85. proc ZB_resize uses eax ebx ecx edi esi, zb:dword, frame_buffer:dword, xsize:dword, ysize:dword
  86.         mov ebx,[zb]
  87.  
  88.         mov edi,[xsize]
  89.         mov esi,[ysize]
  90.        
  91.         mov [ebx+ZBuffer.xsize], edi
  92.         mov [ebx+ZBuffer.ysize], esi
  93.  
  94.         mov eax,edi
  95.         imul eax,PSZB
  96.         mov [ebx+ZBuffer.linesize],eax ;zb.linesize = (xsize * PSZB + 3) & ~3
  97.  
  98.         mov ecx,edi
  99.         imul ecx,esi
  100.         shl ecx,1 ;*= sizeof(unsigned short)
  101.  
  102.         stdcall gl_free,dword[ebx+ZBuffer.zbuf]
  103.         stdcall gl_malloc,ecx
  104.         mov [ebx+ZBuffer.zbuf],eax
  105.  
  106.         cmp dword[ebx+ZBuffer.frame_buffer_allocated],0
  107.         je @f
  108.                 stdcall gl_free,dword[ebx+ZBuffer.pbuf]
  109.         @@:
  110.  
  111.         cmp dword[frame_buffer],0
  112.         jne .els_0
  113.                 inc esi
  114.                 imul esi,dword[ebx+ZBuffer.linesize]
  115.                 stdcall gl_malloc,esi
  116.                 mov dword[ebx+ZBuffer.pbuf],eax
  117.                 mov dword[ebx+ZBuffer.frame_buffer_allocated],1
  118.                 jmp @f
  119.         .els_0:
  120.                 mov eax,[frame_buffer]
  121.                 mov dword[ebx+ZBuffer.pbuf],eax
  122.                 mov dword[ebx+ZBuffer.frame_buffer_allocated],0
  123.         @@:
  124.         ret
  125. endp
  126.  
  127. ;static void ZB_copyBuffer(ZBuffer * zb,
  128. ;                          void *buf,
  129. ;                          int linesize)
  130. ;{
  131. ;    unsigned char *p1;
  132. ;    PIXEL *q;
  133. ;    int y, n;
  134.  
  135. ;    q = zb->pbuf;
  136. ;    p1 = buf;
  137. ;    n = zb->xsize * PSZB;
  138. ;    for (y = 0; y < zb->ysize; y++) {
  139. ;       memcpy(p1, q, n);
  140. ;       p1 += linesize;
  141. ;       q = (PIXEL *) ((char *) q + zb->linesize);
  142. ;    }
  143. ;}
  144.  
  145. ;#if TGL_FEATURE_RENDER_BITS == 16
  146.  
  147. ;/* 32 bpp copy */
  148.  
  149. ;#ifdef TGL_FEATURE_32_BITS
  150.  
  151. ;#define RGB16_TO_RGB32(p0,p1,v)\
  152. ;{\
  153. ;    unsigned int g,b,gb;\
  154. ;    g = (v & 0x07E007E0) << 5;\
  155. ;    b = (v & 0x001F001F) << 3;\
  156. ;    gb = g | b;\
  157. ;    p0 = (gb & 0x0000FFFF) | ((v & 0x0000F800) << 8);\
  158. ;    p1 = (gb >> 16) | ((v & 0xF8000000) >> 8);\
  159. ;}
  160.  
  161. ;static void ZB_copyFrameBufferRGB32(ZBuffer * zb,
  162. ;                                    void *buf,
  163. ;                                    int linesize)
  164. ;{
  165. ;    unsigned short *q;
  166. ;    unsigned int *p, *p1, v, w0, w1;
  167. ;    int y, n;
  168.  
  169. ;    q = zb->pbuf;
  170. ;    p1 = (unsigned int *) buf;
  171.  
  172. ;    for (y = 0; y < zb->ysize; y++) {
  173. ;       p = p1;
  174. ;       n = zb->xsize >> 2;
  175. ;       do {
  176. ;           v = *(unsigned int *) q;
  177. ;#if BYTE_ORDER == BIG_ENDIAN
  178. ;           RGB16_TO_RGB32(w1, w0, v);
  179. ;#else
  180. ;           RGB16_TO_RGB32(w0, w1, v);
  181. ;#endif
  182. ;           p[0] = w0;
  183. ;           p[1] = w1;
  184.  
  185. ;           v = *(unsigned int *) (q + 2);
  186. ;#if BYTE_ORDER == BIG_ENDIAN
  187. ;           RGB16_TO_RGB32(w1, w0, v);
  188. ;#else
  189. ;           RGB16_TO_RGB32(w0, w1, v);
  190. ;#endif
  191. ;           p[2] = w0;
  192. ;           p[3] = w1;
  193.  
  194. ;           q += 4;
  195. ;           p += 4;
  196. ;       } while (--n > 0);
  197.  
  198. ;       p1 += linesize;
  199. ;    }
  200. ;}
  201.  
  202. ;#endif
  203.  
  204. ;/* 24 bit packed pixel handling */
  205.  
  206. ;#ifdef TGL_FEATURE_24_BITS
  207.  
  208. ;/* order: RGBR GBRG BRGB */
  209.  
  210. ;/* XXX: packed pixel 24 bit support not tested */
  211. ;/* XXX: big endian case not optimised */
  212.  
  213. ;#if BYTE_ORDER == BIG_ENDIAN
  214.  
  215. ;#define RGB16_TO_RGB24(p0,p1,p2,v1,v2)\
  216. ;{\
  217. ;    unsigned int r1,g1,b1,gb1,g2,b2,gb2;\
  218. ;    v1 = (v1 << 16) | (v1 >> 16);\
  219. ;    v2 = (v2 << 16) | (v2 >> 16);\
  220. ;    r1 = (v1 & 0xF800F800);\
  221. ;    g1 = (v1 & 0x07E007E0) << 5;\
  222. ;    b1 = (v1 & 0x001F001F) << 3;\
  223. ;    gb1 = g1 | b1;\
  224. ;    p0 = ((gb1 & 0x0000FFFF) << 8) | (r1 << 16) | (r1 >> 24);\
  225. ;    g2 = (v2 & 0x07E007E0) << 5;\
  226. ;    b2 = (v2 & 0x001F001F) << 3;\
  227. ;    gb2 = g2 | b2;\
  228. ;    p1 = (gb1 & 0xFFFF0000) | (v2 & 0xF800) | ((gb2 >> 8) & 0xff);\
  229. ;    p2 = (gb2 << 24) | ((v2 & 0xF8000000) >> 8) | (gb2 >> 16);\
  230. ;}
  231.  
  232. ;#else
  233.  
  234. ;#define RGB16_TO_RGB24(p0,p1,p2,v1,v2)\
  235. ;{\
  236. ;    unsigned int r1,g1,b1,gb1,g2,b2,gb2;\
  237. ;    r1 = (v1 & 0xF800F800);\
  238. ;    g1 = (v1 & 0x07E007E0) << 5;\
  239. ;    b1 = (v1 & 0x001F001F) << 3;\
  240. ;    gb1 = g1 | b1;\
  241. ;    p0 = ((gb1 & 0x0000FFFF) << 8) | (r1 << 16) | (r1 >> 24);\
  242. ;    g2 = (v2 & 0x07E007E0) << 5;\
  243. ;    b2 = (v2 & 0x001F001F) << 3;\
  244. ;    gb2 = g2 | b2;\
  245. ;    p1 = (gb1 & 0xFFFF0000) | (v2 & 0xF800) | ((gb2 >> 8) & 0xff);\
  246. ;    p2 = (gb2 << 24) | ((v2 & 0xF8000000) >> 8) | (gb2 >> 16);\
  247. ;}
  248.  
  249. ;#endif
  250.  
  251. ;static void ZB_copyFrameBufferRGB24(ZBuffer * zb,
  252. ;                                    void *buf, int linesize)
  253. ;{
  254. ;    unsigned short *q;
  255. ;    unsigned int *p, *p1, w0, w1, w2, v0, v1;
  256. ;    int y, n;
  257.  
  258. ;    q = zb->pbuf;
  259. ;    p1 = (unsigned int *) buf;
  260. ;    linesize = linesize * 3;
  261.  
  262. ;    for (y = 0; y < zb->ysize; y++) {
  263. ;       p = p1;
  264. ;       n = zb->xsize >> 2;
  265. ;       do {
  266. ;           v0 = *(unsigned int *) q;
  267. ;           v1 = *(unsigned int *) (q + 2);
  268. ;           RGB16_TO_RGB24(w0, w1, w2, v0, v1);
  269. ;           p[0] = w0;
  270. ;           p[1] = w1;
  271. ;           p[2] = w2;
  272.  
  273. ;           q += 4;
  274. ;           p += 3;
  275. ;       } while (--n > 0);
  276. ;
  277. ;       (char *) p1 += linesize;
  278. ;    }
  279. ;}
  280.  
  281. ;#endif
  282.  
  283. ;void ZB_copyFrameBuffer(ZBuffer * zb, void *buf,
  284. ;                       int linesize)
  285. ;{
  286. ;    switch (zb->mode) {
  287. ;#ifdef TGL_FEATURE_32_BITS
  288. ;    case ZB_MODE_RGBA:
  289. ;       ZB_copyFrameBufferRGB32(zb, buf, linesize >> 1);
  290. ;       break;
  291. ;#endif
  292. ;#ifdef TGL_FEATURE_24_BITS
  293. ;    case ZB_MODE_RGB24:
  294. ;       ZB_copyFrameBufferRGB24(zb, buf, linesize >> 1);
  295. ;       break;
  296. ;#endif
  297. ;    default:
  298. ;       assert(0);
  299. ;    }
  300. ;}
  301.  
  302. ;#endif /* TGL_FEATURE_RENDER_BITS == 16 */
  303.  
  304. ;#if TGL_FEATURE_RENDER_BITS == 24
  305.  
  306. ;#define RGB24_TO_RGB16(r, g, b) \
  307. ;  ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
  308.  
  309. ;/* XXX: not optimized */
  310. ;static void ZB_copyFrameBuffer5R6G5B(ZBuffer * zb,
  311. ;                                     void *buf, int linesize)
  312. ;{
  313. ;    PIXEL *q;
  314. ;    unsigned short *p, *p1;
  315. ;    int y, n;
  316.  
  317. ;    q = zb->pbuf;
  318. ;    p1 = (unsigned short *) buf;
  319.  
  320. ;    for (y = 0; y < zb->ysize; y++) {
  321. ;       p = p1;
  322. ;       n = zb->xsize >> 2;
  323. ;       do {
  324. ;            p[0] = RGB24_TO_RGB16(q[0], q[1], q[2]);
  325. ;            p[1] = RGB24_TO_RGB16(q[3], q[4], q[5]);
  326. ;            p[2] = RGB24_TO_RGB16(q[6], q[7], q[8]);
  327. ;            p[3] = RGB24_TO_RGB16(q[9], q[10], q[11]);
  328. ;           q = (PIXEL *)((char *)q + 4 * PSZB);
  329. ;           p += 4;
  330. ;       } while (--n > 0);
  331. ;       p1 = (unsigned short *)((char *)p1 + linesize);
  332. ;    }
  333. ;}
  334.  
  335. ;void ZB_copyFrameBuffer(ZBuffer * zb, void *buf,
  336. ;                       int linesize)
  337. ;{
  338. ;    switch (zb->mode) {
  339. ;#ifdef TGL_FEATURE_24_BITS
  340. ;    case ZB_MODE_RGB24:
  341. ;       ZB_copyBuffer(zb, buf, linesize);
  342. ;       break;
  343. ;#endif
  344. ;    default:
  345. ;       assert(0);
  346. ;    }
  347. ;}
  348.  
  349. ;#endif /* TGL_FEATURE_RENDER_BITS == 24 */
  350.  
  351. ;#if TGL_FEATURE_RENDER_BITS == 32
  352.  
  353. ;#define RGB32_TO_RGB16(v) \
  354. ;  (((v >> 8) & 0xf800) | (((v) >> 5) & 0x07e0) | (((v) & 0xff) >> 3))
  355.  
  356. ;/* XXX: not optimized */
  357. ;static void ZB_copyFrameBuffer5R6G5B(ZBuffer * zb,
  358. ;                                     void *buf, int linesize)
  359. ;{
  360. ;    PIXEL *q;
  361. ;    unsigned short *p, *p1;
  362. ;    int y, n;
  363.  
  364. ;    q = zb->pbuf;
  365. ;    p1 = (unsigned short *) buf;
  366.  
  367. ;    for (y = 0; y < zb->ysize; y++) {
  368. ;       p = p1;
  369. ;       n = zb->xsize >> 2;
  370. ;       do {
  371. ;            p[0] = RGB32_TO_RGB16(q[0]);
  372. ;            p[1] = RGB32_TO_RGB16(q[1]);
  373. ;            p[2] = RGB32_TO_RGB16(q[2]);
  374. ;            p[3] = RGB32_TO_RGB16(q[3]);
  375. ;           q += 4;
  376. ;           p += 4;
  377. ;       } while (--n > 0);
  378. ;       p1 = (unsigned short *)((char *)p1 + linesize);
  379. ;    }
  380. ;}
  381.  
  382. ;void ZB_copyFrameBuffer(ZBuffer * zb, void *buf,
  383. ;                       int linesize)
  384. ;{
  385. ;    switch (zb->mode) {
  386. ;#ifdef TGL_FEATURE_32_BITS
  387. ;    case ZB_MODE_RGBA:
  388. ;       ZB_copyBuffer(zb, buf, linesize);
  389. ;       break;
  390. ;#endif
  391. ;    default:
  392. ;       assert(0);
  393. ;    }
  394. ;}
  395.  
  396. ;#endif /* TGL_FEATURE_RENDER_BITS == 32 */
  397.  
  398.  
  399. ;
  400. ; adr must be aligned on an 'int'
  401. ;
  402. ;destroy:
  403. ; ecx, edi
  404. align 16
  405. proc memset_s uses eax, adr:dword, val:dword, count:dword
  406.         mov eax,[val]
  407.         mov di,ax
  408.         ror eax,16
  409.         mov ax,di
  410.         mov ecx,[count]
  411.         shr ecx,1
  412.         mov edi,[adr]
  413.         rep stosd
  414.  
  415.         bt dword[count],0
  416.         jnc @f
  417.                 stosw
  418.         @@:
  419.         ret
  420. endp
  421.  
  422. align 16
  423. proc memset_l uses eax ecx edi, adr:dword, val:dword, count:dword
  424.         mov eax,[val]
  425.         mov ecx,[count]
  426.         mov edi,[adr]
  427.         rep stosd
  428.         ret
  429. endp
  430.  
  431. ;input:
  432. ; count - число пикселей RGB для закраски
  433. ;destroy:
  434. ; eax, ecx, edi, esi
  435. align 16
  436. proc memset_RGB24, adr:dword, r:dword, g:dword, b:dword, count:dword
  437.         mov esi,[adr]
  438.         mov eax,[r] ;копируем в буфер первые 12 байт (минимальное число кратное 3 и 4)
  439.         mov byte[esi],al
  440.         mov byte[esi+3],al
  441.         mov byte[esi+6],al
  442.         mov byte[esi+9],al
  443.         mov eax,[g]
  444.         mov byte[esi+1],al
  445.         mov byte[esi+4],al
  446.         mov byte[esi+7],al
  447.         mov byte[esi+10],al
  448.         mov eax,[b]
  449.         mov byte[esi+2],al
  450.         mov byte[esi+5],al
  451.         mov byte[esi+8],al
  452.         mov byte[esi+11],al
  453.  
  454.         mov ecx,[count]
  455.         shr ecx,2
  456.         cmp ecx,1
  457.         jle .end_f ;если ширина буфера меньше 12 байт, то выходим
  458.         dec ecx
  459.         lea edi,[esi+12]
  460.  
  461.         mov eax,[esi]
  462.         cmp eax,[esi+4]
  463.         jne @f
  464.                 ;если r=g и g=b и b=r
  465.                 lea ecx,[ecx+2*ecx] ;ecx*=3
  466.                 rep stosd
  467.                 mov ecx,[count]
  468.                 and ecx,3
  469.                 cmp ecx,0
  470.                 je .end_f
  471.                 lea ecx,[ecx+2*ecx] ;ecx*=3
  472.                 rep stosb
  473.                 jmp .end_f
  474. align 16
  475.         @@: ;если r!=g или g!=b или b!=r
  476.                 movsd
  477.                 movsd
  478.                 movsd
  479.                 sub esi,12
  480.         loop @b
  481.                 mov ecx,[count]
  482.                 and ecx,3
  483.                 cmp ecx,0
  484.                 je .end_f
  485.                 lea ecx,[ecx+2*ecx] ;ecx*=3
  486.                 rep movsb
  487.         .end_f:
  488.         ret
  489. endp
  490.  
  491. align 16
  492. proc ZB_clear uses eax ebx ecx edi esi, zb:dword, clear_z:dword, z:dword,\
  493.         clear_color:dword, r:dword, g:dword, b:dword
  494. ;if TGL_FEATURE_RENDER_BITS != 24
  495. ;       color dd ?
  496. ;end if
  497.  
  498.         mov eax,[zb]
  499.         cmp dword[clear_z],0
  500.         je @f
  501.                 mov ebx,[eax+ZBuffer.xsize]
  502.                 imul ebx,[eax+ZBuffer.ysize]
  503.                 stdcall memset_s, [eax+ZBuffer.zbuf],[z],ebx
  504.         @@:
  505.         cmp dword[clear_color],0
  506.         je @f
  507. if TGL_FEATURE_RENDER_BITS eq 32
  508.                 ;color = RGB_TO_PIXEL(r, g, b)
  509.                 ;memset_l(ebx, color, zb->xsize)
  510. end if
  511. if TGL_FEATURE_RENDER_BITS eq 24
  512.                 mov ebx,[eax+ZBuffer.xsize]
  513.                 imul ebx,[eax+ZBuffer.ysize]
  514.                 push ebx
  515.                 mov ebx,[b]
  516.                 shr ebx,8
  517.                 push ebx
  518.                 mov ebx,[g]
  519.                 shr ebx,8
  520.                 push ebx
  521.                 mov ebx,[r]
  522.                 shr ebx,8
  523.                 push ebx
  524.                 stdcall memset_RGB24, [eax+ZBuffer.pbuf]
  525. end if
  526.         @@:
  527.         ret
  528. endp
  529.