r_filter.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. *-----------------------------------------------------------------------------*/
  30. #ifndef R_FILTER_H
  31. #define R_FILTER_H
  32. #define DITHER_DIM 4
  33. extern byte filter_ditherMatrix[DITHER_DIM][DITHER_DIM];
  34. #define FILTER_UVBITS 6
  35. #define FILTER_UVDIM (1<<FILTER_UVBITS)
  36. extern byte filter_roundedUVMap[FILTER_UVDIM*FILTER_UVDIM];
  37. extern byte filter_roundedRowMap[4*16];
  38. void R_FilterInit(void);
  39. // Use the dither matrix to determine whether a pixel is on or off based
  40. // on the overall intensity we're trying to simulate
  41. #define filter_getDitheredPixelLevel(x, y, intensity) \
  42. ((filter_ditherMatrix[(y)&(DITHER_DIM-1)][(x)&(DITHER_DIM-1)] < (intensity)) ? 1 : 0)
  43. // Choose current pixel or next pixel down based on dither of the fractional
  44. // texture V coord. texV is not a true fractional texture coord, so it
  45. // has to be converted using ((texV) - dcvars.yl) >> 8), which was empirically
  46. // derived. the "-dcvars.yl" is apparently required to offset some minor
  47. // shaking in coordinate y-axis and prevents dithering seams
  48. #define FILTER_GETV(x,y,texV,nextRowTexV) \
  49. (filter_getDitheredPixelLevel(x, y, (((texV) - yl) >> 8)&0xff) ? ((nextRowTexV)>>FRACBITS) : ((texV)>>FRACBITS))
  50. // Choose current column or next column to the right based on dither of the
  51. // fractional texture U coord
  52. #define filter_getDitheredForColumn(x, y, texV, nextRowTexV) \
  53. dither_sources[(filter_getDitheredPixelLevel(x, y, filter_fracu))][FILTER_GETV(x,y,texV,nextRowTexV)]
  54. #define filter_getRoundedForColumn(texV, nextRowTexV) \
  55. filter_getScale2xQuadColors( \
  56. source[ ((texV)>>FRACBITS) ], \
  57. source[ (MAX(0, ((texV)>>FRACBITS)-1)) ], \
  58. nextsource[ ((texV)>>FRACBITS) ], \
  59. source[ ((nextRowTexV)>>FRACBITS) ], \
  60. prevsource[ ((texV)>>FRACBITS) ] \
  61. ) \
  62. [ filter_roundedUVMap[ \
  63. ((filter_fracu>>(8-FILTER_UVBITS))<<FILTER_UVBITS) + \
  64. ((((texV)>>8) & 0xff)>>(8-FILTER_UVBITS)) \
  65. ] ]
  66. #define filter_getRoundedForSpan(texU, texV) \
  67. filter_getScale2xQuadColors( \
  68. source[ (((texU)>>16)&0x3f) | (((texV)>>10)&0xfc0) ], \
  69. source[ (((texU)>>16)&0x3f) | ((((texV)-FRACUNIT)>>10)&0xfc0) ], \
  70. source[ ((((texU)+FRACUNIT)>>16)&0x3f) | (((texV)>>10)&0xfc0) ], \
  71. source[ (((texU)>>16)&0x3f) | ((((texV)+FRACUNIT)>>10)&0xfc0) ], \
  72. source[ ((((texU)-FRACUNIT)>>16)&0x3f) | (((texV)>>10)&0xfc0) ] \
  73. ) \
  74. [ filter_roundedUVMap[ \
  75. (((((texU)>>8) & 0xff)>>(8-FILTER_UVBITS))<<FILTER_UVBITS) + \
  76. ((((texV)>>8) & 0xff)>>(8-FILTER_UVBITS)) \
  77. ] ]
  78. byte *filter_getScale2xQuadColors(byte e, byte b, byte f, byte h, byte d);
  79. // This is the horrendous macro version of the function commented out of
  80. // r_filter.c. It does a bilinear blend on the four source texels for a
  81. // given u and v
  82. #define filter_getFilteredForColumn32(depthmap, texV, nextRowTexV) ( \
  83. VID_PAL32( depthmap(nextsource[(nextRowTexV)>>FRACBITS]), (filter_fracu*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS) ) + \
  84. VID_PAL32( depthmap(source[(nextRowTexV)>>FRACBITS]), ((0xffff-filter_fracu)*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS) ) + \
  85. VID_PAL32( depthmap(source[(texV)>>FRACBITS]), ((0xffff-filter_fracu)*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS) ) + \
  86. VID_PAL32( depthmap(nextsource[(texV)>>FRACBITS]), (filter_fracu*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS) ))
  87. // The 16 bit method of the filtering doesn't really maintain enough
  88. // accuracy for discerning viewers, but the alternative requires converting
  89. // from 32 bit, which is slow and requires both the intPalette and the
  90. // shortPalette to be in memory at the same time.
  91. #define filter_getFilteredForColumn16(depthmap, texV, nextRowTexV) ( \
  92. VID_PAL16( depthmap(nextsource[(nextRowTexV)>>FRACBITS]), (filter_fracu*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS) ) + \
  93. VID_PAL16( depthmap(source[(nextRowTexV)>>FRACBITS]), ((0xffff-filter_fracu)*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS) ) + \
  94. VID_PAL16( depthmap(source[(texV)>>FRACBITS]), ((0xffff-filter_fracu)*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS) ) + \
  95. VID_PAL16( depthmap(nextsource[(texV)>>FRACBITS]), (filter_fracu*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS) ))
  96. #define filter_getFilteredForColumn15(depthmap, texV, nextRowTexV) ( \
  97. VID_PAL15( depthmap(nextsource[(nextRowTexV)>>FRACBITS]), (filter_fracu*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS) ) + \
  98. VID_PAL15( depthmap(source[(nextRowTexV)>>FRACBITS]), ((0xffff-filter_fracu)*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS) ) + \
  99. VID_PAL15( depthmap(source[(texV)>>FRACBITS]), ((0xffff-filter_fracu)*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS) ) + \
  100. VID_PAL15( depthmap(nextsource[(texV)>>FRACBITS]), (filter_fracu*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS) ))
  101. // Same as for column but wrapping at 64
  102. #define filter_getFilteredForSpan32(depthmap, texU, texV) ( \
  103. VID_PAL32( depthmap(source[ ((((texU)+FRACUNIT)>>16)&0x3f) | ((((texV)+FRACUNIT)>>10)&0xfc0)]), (unsigned int)(((texU)&0xffff)*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS)) + \
  104. VID_PAL32( depthmap(source[ (((texU)>>16)&0x3f) | ((((texV)+FRACUNIT)>>10)&0xfc0)]), (unsigned int)((0xffff-((texU)&0xffff))*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS)) + \
  105. VID_PAL32( depthmap(source[ (((texU)>>16)&0x3f) | (((texV)>>10)&0xfc0)]), (unsigned int)((0xffff-((texU)&0xffff))*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS)) + \
  106. VID_PAL32( depthmap(source[ ((((texU)+FRACUNIT)>>16)&0x3f) | (((texV)>>10)&0xfc0)]), (unsigned int)(((texU)&0xffff)*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS)))
  107. // Use 16 bit addition here since it's a little faster and the defects from
  108. // such low-accuracy blending are less visible on spans
  109. #define filter_getFilteredForSpan16(depthmap, texU, texV) ( \
  110. VID_PAL16( depthmap(source[ ((((texU)+FRACUNIT)>>16)&0x3f) | ((((texV)+FRACUNIT)>>10)&0xfc0)]), (unsigned int)(((texU)&0xffff)*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS)) + \
  111. VID_PAL16( depthmap(source[ (((texU)>>16)&0x3f) | ((((texV)+FRACUNIT)>>10)&0xfc0)]), (unsigned int)((0xffff-((texU)&0xffff))*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS)) + \
  112. VID_PAL16( depthmap(source[ (((texU)>>16)&0x3f) | (((texV)>>10)&0xfc0)]), (unsigned int)((0xffff-((texU)&0xffff))*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS)) + \
  113. VID_PAL16( depthmap(source[ ((((texU)+FRACUNIT)>>16)&0x3f) | (((texV)>>10)&0xfc0)]), (unsigned int)(((texU)&0xffff)*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS)))
  114. #define filter_getFilteredForSpan15(depthmap, texU, texV) ( \
  115. VID_PAL15( depthmap(source[ ((((texU)+FRACUNIT)>>16)&0x3f) | ((((texV)+FRACUNIT)>>10)&0xfc0)]), (unsigned int)(((texU)&0xffff)*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS)) + \
  116. VID_PAL15( depthmap(source[ (((texU)>>16)&0x3f) | ((((texV)+FRACUNIT)>>10)&0xfc0)]), (unsigned int)((0xffff-((texU)&0xffff))*((texV)&0xffff))>>(32-VID_COLORWEIGHTBITS)) + \
  117. VID_PAL15( depthmap(source[ (((texU)>>16)&0x3f) | (((texV)>>10)&0xfc0)]), (unsigned int)((0xffff-((texU)&0xffff))*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS)) + \
  118. VID_PAL15( depthmap(source[ ((((texU)+FRACUNIT)>>16)&0x3f) | (((texV)>>10)&0xfc0)]), (unsigned int)(((texU)&0xffff)*(0xffff-((texV)&0xffff)))>>(32-VID_COLORWEIGHTBITS)))
  119. // do red and blue at once for slight speedup
  120. #define GETBLENDED15_5050(col1, col2) \
  121. ((((col1&0x7c1f)+(col2&0x7c1f))>>1)&0x7c1f) | \
  122. ((((col1&0x03e0)+(col2&0x03e0))>>1)&0x03e0)
  123. #define GETBLENDED16_5050(col1, col2) \
  124. ((((col1&0xf81f)+(col2&0xf81f))>>1)&0xf81f) | \
  125. ((((col1&0x07e0)+(col2&0x07e0))>>1)&0x07e0)
  126. #define GETBLENDED32_5050(col1, col2) \
  127. ((((col1&0xff00ff)+(col2&0xff00ff))>>1)&0xff00ff) | \
  128. ((((col1&0x00ff00)+(col2&0x00ff00))>>1)&0x00ff00)
  129. #define GETBLENDED15_3268(col1, col2) \
  130. ((((col1&0x7c1f)*5+(col2&0x7c1f)*11)>>4)&0x7c1f) | \
  131. ((((col1&0x03e0)*5+(col2&0x03e0)*11)>>4)&0x03e0)
  132. #define GETBLENDED16_3268(col1, col2) \
  133. ((((col1&0xf81f)*5+(col2&0xf81f)*11)>>4)&0xf81f) | \
  134. ((((col1&0x07e0)*5+(col2&0x07e0)*11)>>4)&0x07e0)
  135. #define GETBLENDED32_3268(col1, col2) \
  136. ((((col1&0xff00ff)*5+(col2&0xff00ff)*11)>>4)&0xff00ff) | \
  137. ((((col1&0x00ff00)*5+(col2&0x00ff00)*11)>>4)&0x00ff00)
  138. #define GETBLENDED15_9406(col1, col2) \
  139. ((((col1&0x7c1f)*15+(col2&0x7c1f))>>4)&0x7c1f) | \
  140. ((((col1&0x03e0)*15+(col2&0x03e0))>>4)&0x03e0)
  141. #define GETBLENDED16_9406(col1, col2) \
  142. ((((col1&0xf81f)*15+(col2&0xf81f))>>4)&0xf81f) | \
  143. ((((col1&0x07e0)*15+(col2&0x07e0))>>4)&0x07e0)
  144. #define GETBLENDED32_9406(col1, col2) \
  145. ((((col1&0xff00ff)*15+(col2&0xff00ff))>>4)&0xff00ff) | \
  146. ((((col1&0x00ff00)*15+(col2&0x00ff00))>>4)&0x00ff00)
  147. #endif