home *** CD-ROM | disk | FTP | other *** search
/ Amiga Magazin: Amiga-CD 2000 April & May / AMIGA_2000_04.iso / patches / mesa3.1 / mesa-3_1.lha / src / AOS / wrpDisplay / wrpDepth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-23  |  11.1 KB  |  505 lines

  1. /*
  2.  * $Id: $
  3.  */
  4.  
  5. /*
  6.  * Mesa 3-D graphics library
  7.  * Version:  3.1
  8.  * Copyright (C) 1995  Brian Paul  (brianp@ssec.wisc.edu)
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Library General Public
  12.  * License as published by the Free Software Foundation; either
  13.  * version 2 of the License, or (at your option) any later version.
  14.  *
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18.  * Library General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU Library General Public
  21.  * License along with this library; if not, write to the Free
  22.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #define    rup(pos)    (GLdepth) ((pos) * MAX_DEPTH + 0.5)
  26. #define    rdown(pos)    (((W3D_Float) (pos) + 0.5) / MAX_DEPTH)
  27.  
  28. GLuint wrpDepthTestSpan(GLcontext * ctx,
  29.             GLuint n, GLint x, GLint y, const GLdepth z[],
  30.             GLubyte mask[])
  31. {
  32.   amigaMesaContext amesa;
  33.   struct TDdriver *TDdriver;
  34.   W3D_Context *TDcontext;
  35.   W3D_Double zf[MAX_WIDTH];
  36.   W3D_Double *zptr = zf;
  37.   GLubyte *m = mask;
  38.   GLuint i;
  39.   GLuint passed = 0;
  40.  
  41.   DEBUGOUT(1, "wrpDepthTestSpan()\n");
  42.  
  43.   amesa = (amigaMesaContext) ctx->DriverCtx;
  44.   TDdriver = amesa->TDdriver;
  45.   TDcontext = TDdriver->td_ctx;
  46.  
  47.   x = FIXx(x);
  48.   y = FIXy(y);
  49.  
  50.   FlushHardware(TDdriver, TDcontext);
  51.  
  52.   /* these depth tests are really slow (VRAM accesses) therefore
  53.    * GL applications, which can't be handled by the installed
  54.    * 3D hardware should be run with hardware acceleration disabled
  55.    */
  56.  
  57.   /* switch cases ordered from most frequent to less frequent */
  58.   switch (ctx->Depth.Func) {
  59.     case GL_LESS:
  60.       if (ctx->Depth.Mask) {
  61.     /* Update Z buffer */
  62.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  63.     for (i = 0; i < n; i++, zptr++, m++) {
  64.       if (*m) {
  65.         if (z[i] < rup(*zptr)) {
  66.           /* pass */
  67.           *zptr = rdown(z[i]);
  68.           passed++;
  69.         }
  70.         else
  71.           /* fail */
  72.           *m = 0;
  73.       }
  74.     }
  75.     W3D_WriteZSpan(TDcontext, x, y, n, zf, (UBYTE *) mask);
  76.       }
  77.       else {
  78.     /* Don't update Z buffer */
  79.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  80.     for (i = 0; i < n; i++, zptr++, m++) {
  81.       if (*m) {
  82.         if (z[i] < rup(*zptr))
  83.           passed++;
  84.         else
  85.           *m = 0;
  86.       }
  87.     }
  88.       }
  89.       break;
  90.     case GL_LEQUAL:
  91.       if (ctx->Depth.Mask) {
  92.     /* Update Z buffer */
  93.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  94.     for (i = 0; i < n; i++, zptr++, m++) {
  95.       if (*m) {
  96.         if (z[i] <= rup(*zptr)) {
  97.           *zptr = rdown(z[i]);
  98.           passed++;
  99.         }
  100.         else {
  101.           *m = 0;
  102.         }
  103.       }
  104.     }
  105.     W3D_WriteZSpan(TDcontext, x, y, n, zf, (UBYTE *) mask);
  106.       }
  107.       else {
  108.     /* Don't update Z buffer */
  109.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  110.     for (i = 0; i < n; i++, zptr++, m++) {
  111.       if (*m) {
  112.         if (z[i] <= rup(*zptr))
  113.           passed++;
  114.         else
  115.           *m = 0;
  116.       }
  117.     }
  118.       }
  119.       break;
  120.     case GL_GEQUAL:
  121.       if (ctx->Depth.Mask) {
  122.     /* Update Z buffer */
  123.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  124.     for (i = 0; i < n; i++, zptr++, m++) {
  125.       if (*m) {
  126.         if (z[i] >= rup(*zptr)) {
  127.           *zptr = rdown(z[i]);
  128.           passed++;
  129.         }
  130.         else
  131.           *m = 0;
  132.       }
  133.     }
  134.     W3D_WriteZSpan(TDcontext, x, y, n, zf, (UBYTE *) mask);
  135.       }
  136.       else {
  137.     /* Don't update Z buffer */
  138.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  139.     for (i = 0; i < n; i++, zptr++, m++) {
  140.       if (*m) {
  141.         if (z[i] >= rup(*zptr))
  142.           passed++;
  143.         else
  144.           *m = 0;
  145.       }
  146.     }
  147.       }
  148.       break;
  149.     case GL_GREATER:
  150.       if (ctx->Depth.Mask) {
  151.     /* Update Z buffer */
  152.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  153.     for (i = 0; i < n; i++, zptr++, m++) {
  154.       if (*m) {
  155.         if (z[i] > rup(*zptr)) {
  156.           *zptr = rdown(z[i]);
  157.           passed++;
  158.         }
  159.         else
  160.           *m = 0;
  161.       }
  162.     }
  163.     W3D_WriteZSpan(TDcontext, x, y, n, zf, (UBYTE *) mask);
  164.       }
  165.       else {
  166.     /* Don't update Z buffer */
  167.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  168.     for (i = 0; i < n; i++, zptr++, m++) {
  169.       if (*m) {
  170.         if (z[i] > rup(*zptr))
  171.           passed++;
  172.         else
  173.           *m = 0;
  174.       }
  175.     }
  176.       }
  177.       break;
  178.     case GL_NOTEQUAL:
  179.       if (ctx->Depth.Mask) {
  180.     /* Update Z buffer */
  181.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  182.     for (i = 0; i < n; i++, zptr++, m++) {
  183.       if (*m) {
  184.         if (z[i] != rup(*zptr)) {
  185.           *zptr = rdown(z[i]);
  186.           passed++;
  187.         }
  188.         else
  189.           *m = 0;
  190.       }
  191.     }
  192.     W3D_WriteZSpan(TDcontext, x, y, n, zf, (UBYTE *) mask);
  193.       }
  194.       else {
  195.     /* Don't update Z buffer */
  196.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  197.     for (i = 0; i < n; i++, zptr++, m++) {
  198.       if (*m) {
  199.         if (z[i] != rup(*zptr))
  200.           passed++;
  201.         else
  202.           *m = 0;
  203.       }
  204.     }
  205.       }
  206.       break;
  207.     case GL_EQUAL:
  208.       if (ctx->Depth.Mask) {
  209.     /* Update Z buffer */
  210.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  211.     for (i = 0; i < n; i++, zptr++, m++) {
  212.       if (*m) {
  213.         if (z[i] == rup(*zptr)) {
  214.           *zptr = rdown(z[i]);
  215.           passed++;
  216.         }
  217.         else
  218.           *m = 0;
  219.       }
  220.     }
  221.     W3D_WriteZSpan(TDcontext, x, y, n, zf, (UBYTE *) mask);
  222.       }
  223.       else {
  224.     /* Don't update Z buffer */
  225.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  226.     for (i = 0; i < n; i++, zptr++, m++) {
  227.       if (*m) {
  228.         if (z[i] == rup(*zptr))
  229.           passed++;
  230.         else
  231.           *m = 0;
  232.       }
  233.     }
  234.       }
  235.       break;
  236.     case GL_ALWAYS:
  237.       if (ctx->Depth.Mask) {
  238.     /* Update Z buffer */
  239.     W3D_ReadZSpan(TDcontext, x, y, n, zf);
  240.     for (i = 0; i < n; i++, zptr++, m++) {
  241.       if (*m) {
  242.         *zptr = rdown(z[i]);
  243.         passed++;
  244.       }
  245.     }
  246.     W3D_WriteZSpan(TDcontext, x, y, n, zf, (UBYTE *) mask);
  247.       }
  248.       else
  249.     /* Don't update Z buffer or mask */
  250.     passed = n;
  251.       break;
  252.     case GL_NEVER:
  253.       bzero(mask, n * sizeof(GLubyte));
  254.     default:
  255.       break;
  256.   }                                        /*switch */
  257.  
  258.   return passed;
  259. }
  260.  
  261. void wrpDepthTestPixels(GLcontext * ctx,
  262.             GLuint n, const GLint x[], const GLint y[],
  263.             const GLdepth z[], GLubyte mask[])
  264. {
  265.   amigaMesaContext amesa;
  266.   struct TDdriver *TDdriver;
  267.   W3D_Context *TDcontext;
  268.   W3D_Double f;
  269.   GLuint i;
  270.  
  271.   DEBUGOUT(1, "wrpSetupDriver()\n");
  272.  
  273.   amesa = (amigaMesaContext) ctx->DriverCtx;
  274.   TDdriver = amesa->TDdriver;
  275.   TDcontext = TDdriver->td_ctx;
  276.  
  277.   FlushHardware(TDdriver, TDcontext);
  278.  
  279.   /* these depth tests are really slow (VRAM accesses) therefore
  280.    * GL applications, which can't be handled by the installed
  281.    * 3D hardware should be run with hardware acceleration disabled
  282.    */
  283.  
  284.   /* switch cases ordered from most frequent to less frequent */
  285.   switch (ctx->Depth.Func) {
  286.     case GL_LESS:
  287.       if (ctx->Depth.Mask) {
  288.     /* Update Z buffer */
  289.     for (i = 0; i < n; i++) {
  290.       if (mask[i]) {
  291.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  292.         if (z[i] < rup(f)) {
  293.           f = rdown(z[i]);
  294.           W3D_WriteZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  295.         }
  296.         else
  297.           mask[i] = 0;
  298.       }
  299.     }
  300.       }
  301.       else {
  302.     /* Don't update Z buffer */
  303.     for (i = 0; i < n; i++) {
  304.       if (mask[i]) {
  305.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  306.         if (z[i] >= rup(f))
  307.           mask[i] = 0;
  308.       }
  309.     }
  310.       }
  311.       break;
  312.     case GL_LEQUAL:
  313.       if (ctx->Depth.Mask) {
  314.     /* Update Z buffer */
  315.     for (i = 0; i < n; i++) {
  316.       if (mask[i]) {
  317.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  318.         if (z[i] <= rup(f)) {
  319.           f = rdown(z[i]);
  320.           W3D_WriteZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  321.         }
  322.         else
  323.           mask[i] = 0;
  324.       }
  325.     }
  326.       }
  327.       else {
  328.     /* Don't update Z buffer */
  329.     for (i = 0; i < n; i++) {
  330.       if (mask[i]) {
  331.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  332.         if (z[i] > rup(f))
  333.           mask[i] = 0;
  334.       }
  335.     }
  336.       }
  337.       break;
  338.     case GL_GEQUAL:
  339.       if (ctx->Depth.Mask) {
  340.     /* Update Z buffer */
  341.     for (i = 0; i < n; i++) {
  342.       if (mask[i]) {
  343.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  344.         if (z[i] >= rup(f)) {
  345.           f = rdown(z[i]);
  346.           W3D_WriteZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  347.         }
  348.         else
  349.           mask[i] = 0;
  350.       }
  351.     }
  352.       }
  353.       else {
  354.     /* Don't update Z buffer */
  355.     for (i = 0; i < n; i++) {
  356.       if (mask[i]) {
  357.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  358.         if (z[i] < rup(f))
  359.           mask[i] = 0;
  360.       }
  361.     }
  362.       }
  363.       break;
  364.     case GL_GREATER:
  365.       if (ctx->Depth.Mask) {
  366.     /* Update Z buffer */
  367.     for (i = 0; i < n; i++) {
  368.       if (mask[i]) {
  369.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  370.         if (z[i] > rup(f)) {
  371.           f = rdown(z[i]);
  372.           W3D_WriteZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  373.         }
  374.         else
  375.           mask[i] = 0;
  376.       }
  377.     }
  378.       }
  379.       else {
  380.     /* Don't update Z buffer */
  381.     for (i = 0; i < n; i++) {
  382.       if (mask[i]) {
  383.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  384.         if (z[i] <= rup(f))
  385.           mask[i] = 0;
  386.       }
  387.     }
  388.       }
  389.       break;
  390.     case GL_NOTEQUAL:
  391.       if (ctx->Depth.Mask) {
  392.     /* Update Z buffer */
  393.     for (i = 0; i < n; i++) {
  394.       if (mask[i]) {
  395.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  396.         if (z[i] != rup(f)) {
  397.           f = rdown(z[i]);
  398.           W3D_WriteZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  399.         }
  400.         else
  401.           mask[i] = 0;
  402.       }
  403.     }
  404.       }
  405.       else {
  406.     /* Don't update Z buffer */
  407.     for (i = 0; i < n; i++) {
  408.       if (mask[i]) {
  409.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  410.         if (z[i] == rup(f))
  411.           mask[i] = 0;
  412.       }
  413.     }
  414.       }
  415.       break;
  416.     case GL_EQUAL:
  417.       if (ctx->Depth.Mask) {
  418.     /* Update Z buffer */
  419.     for (i = 0; i < n; i++) {
  420.       if (mask[i]) {
  421.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  422.         if (z[i] == rup(f)) {
  423.           f = rdown(z[i]);
  424.           W3D_WriteZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  425.         }
  426.         else
  427.           mask[i] = 0;
  428.       }
  429.     }
  430.       }
  431.       else {
  432.     /* Don't update Z buffer */
  433.     for (i = 0; i < n; i++) {
  434.       if (mask[i]) {
  435.         W3D_ReadZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  436.         if (z[i] != rup(f))
  437.           mask[i] = 0;
  438.       }
  439.     }
  440.       }
  441.       break;
  442.     case GL_ALWAYS:
  443.       if (ctx->Depth.Mask) {
  444.     /* Update Z buffer */
  445.     for (i = 0; i < n; i++) {
  446.       if (mask[i]) {
  447.         f = rdown(z[i]);
  448.         W3D_WriteZPixel(TDcontext, FIXx(x[i]), FIXy(y[i]), &f);
  449.       }
  450.     }
  451.       }
  452.       break;
  453.     case GL_NEVER:
  454.       /* depth test never passes */
  455.       bzero(mask, n * sizeof(GLubyte));
  456.     default:
  457.       break;
  458.   }                                        /*switch */
  459. }
  460.  
  461. void wrpReadDepthSpanFloat(GLcontext * ctx,
  462.                GLuint n, GLint x, GLint y, GLfloat depth[])
  463. {
  464.   amigaMesaContext amesa;
  465.   struct TDdriver *TDdriver;
  466.   W3D_Context *TDcontext;
  467.   W3D_Double z[MAX_WIDTH];
  468.   GLint i;
  469.  
  470.   DEBUGOUT(1, "wrpSetupDriver()\n");
  471.  
  472.   amesa = (amigaMesaContext) ctx->DriverCtx;
  473.   TDdriver = amesa->TDdriver;
  474.   TDcontext = TDdriver->td_ctx;
  475.  
  476.   FlushHardware(TDdriver, TDcontext);
  477.  
  478.   W3D_ReadZSpan(TDcontext, FIXx(x), FIXy(y), n, z);
  479.   for (i = 0; i < n; i++)
  480.     depth[i] = (float)z[i];
  481. }
  482.  
  483. void wrpReadDepthSpanInt(GLcontext * ctx,
  484.              GLuint n, GLint x, GLint y, GLdepth depth[])
  485. {
  486.   amigaMesaContext amesa;
  487.   struct TDdriver *TDdriver;
  488.   W3D_Context *TDcontext;
  489.   W3D_Double z[MAX_WIDTH];
  490.   GLint i;
  491.  
  492.   DEBUGOUT(1, "wrpSetupDriver()\n");
  493.  
  494.   amesa = (amigaMesaContext) ctx->DriverCtx;
  495.   TDdriver = amesa->TDdriver;
  496.   TDcontext = TDdriver->td_ctx;
  497.  
  498.   FlushHardware(TDdriver, TDcontext);
  499.  
  500.   /* implicitly assumes, that GLfloat == float! */
  501.   W3D_ReadZSpan(TDcontext, FIXx(x), FIXy(y), n, z);
  502.   for (i = 0; i < n; i++)
  503.     depth[i] = rup(z[i]);
  504. }
  505.