mesa: move more swrast-related #defines out of core Mesa
[mesa.git] / src / mesa / swrast / s_span.c
index 157392a619ab41d4b4aec31d8acdd4169825f8be..627ef1136aa5dd5d0dd504f80054fe64c18bece6 100644 (file)
@@ -139,7 +139,8 @@ _swrast_span_default_attribs(struct gl_context *ctx, SWspan *span)
       for (i = 0; i < ctx->Const.MaxTextureCoordUnits; i++) {
          const GLuint attr = FRAG_ATTRIB_TEX0 + i;
          const GLfloat *tc = ctx->Current.RasterTexCoords[i];
-         if (ctx->FragmentProgram._Current || ctx->ATIFragmentShader._Enabled) {
+         if (_swrast_use_fragment_program(ctx) ||
+             ctx->ATIFragmentShader._Enabled) {
             COPY_4V(span->attrStart[attr], tc);
          }
          else if (tc[3] > 0.0F) {
@@ -498,7 +499,7 @@ interpolate_texcoords(struct gl_context *ctx, SWspan *span)
                swrast_texture_image_const(img);
 
             needLambda = (obj->Sampler.MinFilter != obj->Sampler.MagFilter)
-               || ctx->FragmentProgram._Current;
+               || _swrast_use_fragment_program(ctx);
             /* LOD is calculated directly in the ansiotropic filter, we can
              * skip the normal lambda function as the result is ignored.
              */
@@ -518,7 +519,7 @@ interpolate_texcoords(struct gl_context *ctx, SWspan *span)
 
          if (needLambda) {
             GLuint i;
-            if (ctx->FragmentProgram._Current
+            if (_swrast_use_fragment_program(ctx)
                 || ctx->ATIFragmentShader._Enabled) {
                /* do perspective correction but don't divide s, t, r by q */
                const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3];
@@ -559,7 +560,7 @@ interpolate_texcoords(struct gl_context *ctx, SWspan *span)
          }
          else {
             GLuint i;
-            if (ctx->FragmentProgram._Current ||
+            if (_swrast_use_fragment_program(ctx) ||
                 ctx->ATIFragmentShader._Enabled) {
                /* do perspective correction but don't divide s, t, r by q */
                const GLfloat dwdx = span->attrStepX[FRAG_ATTRIB_WPOS][3];
@@ -833,7 +834,7 @@ add_specular(struct gl_context *ctx, SWspan *span)
    GLfloat (*col1)[4] = span->array->attribs[FRAG_ATTRIB_COL1];
    GLuint i;
 
-   ASSERT(!ctx->FragmentProgram._Current);
+   ASSERT(!_swrast_use_fragment_program(ctx));
    ASSERT(span->arrayMask & SPAN_RGBA);
    ASSERT(swrast->_ActiveAttribMask & FRAG_BIT_COL1);
    (void) swrast; /* silence warning */
@@ -971,25 +972,7 @@ convert_color_type(SWspan *span, GLenum newType, GLuint output)
 static inline void
 shade_texture_span(struct gl_context *ctx, SWspan *span)
 {
-   /* This is a hack to work around drivers such as i965 that:
-    *
-    *     - Set _MaintainTexEnvProgram to generate GLSL IR for
-    *       fixed-function fragment processing.
-    *     - Don't call _mesa_ir_link_shader to generate Mesa IR from
-    *       the GLSL IR.
-    *     - May use swrast to handle glDrawPixels.
-    *
-    * Since _mesa_ir_link_shader is never called, there is no Mesa IR
-    * to execute.  Instead do regular fixed-function processing.
-    *
-    * It is also worth noting that the software fixed-function path is
-    * much faster than the software shader path.
-    */
-   const bool use_fragment_program =
-      ctx->FragmentProgram._Current
-      && ctx->FragmentProgram._Current != ctx->FragmentProgram._TexEnvProgram;
-
-   if (use_fragment_program ||
+   if (_swrast_use_fragment_program(ctx) ||
        ctx->ATIFragmentShader._Enabled) {
       /* programmable shading */
       if (span->primitive == GL_BITMAP && span->array->ChanType != GL_FLOAT) {
@@ -1018,7 +1001,7 @@ shade_texture_span(struct gl_context *ctx, SWspan *span)
          interpolate_wpos(ctx, span);
 
       /* Run fragment program/shader now */
-      if (use_fragment_program) {
+      if (_swrast_use_fragment_program(ctx)) {
          _swrast_exec_fragment_program(ctx, span);
       }
       else {
@@ -1048,23 +1031,29 @@ shade_texture_span(struct gl_context *ctx, SWspan *span)
 /** Put colors at x/y locations into a renderbuffer */
 static void
 put_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
+           GLenum datatype,
            GLuint count, const GLint x[], const GLint y[],
            const void *values, const GLubyte *mask)
 {
+   gl_pack_ubyte_rgba_func pack_ubyte = NULL;
+   gl_pack_float_rgba_func pack_float = NULL;
    GLuint i;
 
+   if (datatype == GL_UNSIGNED_BYTE)
+      pack_ubyte = _mesa_get_pack_ubyte_rgba_function(rb->Format);
+   else
+      pack_float = _mesa_get_pack_float_rgba_function(rb->Format);
+
    for (i = 0; i < count; i++) {
       if (mask[i]) {
          GLubyte *dst = _swrast_pixel_address(rb, x[i], y[i]);
 
-         if (rb->DataType == GL_UNSIGNED_BYTE) {
-            _mesa_pack_ubyte_rgba_row(rb->Format, 1,
-                                      (const GLubyte (*)[4]) values + i, dst);
+         if (datatype == GL_UNSIGNED_BYTE) {
+            pack_ubyte((const GLubyte *) values + 4 * i, dst);
          }
          else {
-            assert(rb->DataType == GL_FLOAT);
-            _mesa_pack_float_rgba_row(rb->Format, count,
-                                      (const GLfloat (*)[4]) values + i, dst);
+            assert(datatype == GL_FLOAT);
+            pack_float((const GLfloat *) values + 4 * i, dst);
          }
       }
    }
@@ -1074,18 +1063,19 @@ put_values(struct gl_context *ctx, struct gl_renderbuffer *rb,
 /** Put row of colors into renderbuffer */
 void
 _swrast_put_row(struct gl_context *ctx, struct gl_renderbuffer *rb,
+                GLenum datatype,
                 GLuint count, GLint x, GLint y,
                 const void *values, const GLubyte *mask)
 {
    GLubyte *dst = _swrast_pixel_address(rb, x, y);
 
    if (!mask) {
-      if (rb->DataType == GL_UNSIGNED_BYTE) {
+      if (datatype == GL_UNSIGNED_BYTE) {
          _mesa_pack_ubyte_rgba_row(rb->Format, count,
                                    (const GLubyte (*)[4]) values, dst);
       }
       else {
-         assert(rb->DataType == GL_FLOAT);
+         assert(datatype == GL_FLOAT);
          _mesa_pack_float_rgba_row(rb->Format, count,
                                    (const GLfloat (*)[4]) values, dst);
       }
@@ -1096,7 +1086,7 @@ _swrast_put_row(struct gl_context *ctx, struct gl_renderbuffer *rb,
       /* We can't pass a 'mask' array to the _mesa_pack_rgba_row() functions
        * so look for runs where mask=1...
        */
-      runLen = 0;
+      runLen = runStart = 0;
       for (i = 0; i < count; i++) {
          if (mask[i]) {
             if (runLen == 0)
@@ -1107,13 +1097,13 @@ _swrast_put_row(struct gl_context *ctx, struct gl_renderbuffer *rb,
          if (!mask[i] || i == count - 1) {
             /* might be the end of a run of pixels */
             if (runLen > 0) {
-               if (rb->DataType == GL_UNSIGNED_BYTE) {
+               if (datatype == GL_UNSIGNED_BYTE) {
                   _mesa_pack_ubyte_rgba_row(rb->Format, runLen,
                                      (const GLubyte (*)[4]) values + runStart,
                                      dst + runStart * bpp);
                }
                else {
-                  assert(rb->DataType == GL_FLOAT);
+                  assert(datatype == GL_FLOAT);
                   _mesa_pack_float_rgba_row(rb->Format, runLen,
                                    (const GLfloat (*)[4]) values + runStart,
                                    dst + runStart * bpp);
@@ -1144,7 +1134,7 @@ _swrast_write_rgba_span( struct gl_context *ctx, SWspan *span)
    const GLbitfield64 origArrayAttribs = span->arrayAttribs;
    const GLenum origChanType = span->array->ChanType;
    void * const origRgba = span->array->rgba;
-   const GLboolean shader = (ctx->FragmentProgram._Current
+   const GLboolean shader = (_swrast_use_fragment_program(ctx)
                              || ctx->ATIFragmentShader._Enabled);
    const GLboolean shaderOrTexture = shader || ctx->Texture._EnabledCoordUnits;
    struct gl_framebuffer *fb = ctx->DrawBuffer;
@@ -1174,7 +1164,7 @@ _swrast_write_rgba_span( struct gl_context *ctx, SWspan *span)
       return;
    }
 
-   ASSERT(span->end <= MAX_WIDTH);
+   ASSERT(span->end <= SWRAST_MAX_WIDTH);
 
    /* Depth bounds test */
    if (ctx->Depth.BoundsTest && fb->Visual.depthBits > 0) {
@@ -1319,7 +1309,8 @@ _swrast_write_rgba_span( struct gl_context *ctx, SWspan *span)
       const GLuint numBuffers = fb->_NumColorDrawBuffers;
       const struct gl_fragment_program *fp = ctx->FragmentProgram._Current;
       const GLboolean multiFragOutputs = 
-         (fp && fp->Base.OutputsWritten >= (1 << FRAG_RESULT_DATA0));
+         _swrast_use_fragment_program(ctx)
+         && fp->Base.OutputsWritten >= (1 << FRAG_RESULT_DATA0);
       GLuint buf;
 
       for (buf = 0; buf < numBuffers; buf++) {
@@ -1328,20 +1319,22 @@ _swrast_write_rgba_span( struct gl_context *ctx, SWspan *span)
          /* color[fragOutput] will be written to buffer[buf] */
 
          if (rb) {
-            GLchan rgbaSave[MAX_WIDTH][4];
-            const GLuint fragOutput = multiFragOutputs ? buf : 0;
+            /* re-use one of the attribute array buffers for rgbaSave */
+            GLchan (*rgbaSave)[4] = (GLchan (*)[4]) span->array->attribs[0];
+            struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+            GLenum colorType = srb->ColorType;
 
-            /* set span->array->rgba to colors for render buffer's datatype */
-            if (rb->DataType != span->array->ChanType || fragOutput > 0) {
-               convert_color_type(span, rb->DataType, fragOutput);
+            assert(colorType == GL_UNSIGNED_BYTE ||
+                   colorType == GL_FLOAT);
+
+            /* set span->array->rgba to colors for renderbuffer's datatype */
+            if (span->array->ChanType != colorType) {
+               convert_color_type(span, colorType, 0);
             }
             else {
-               if (rb->DataType == GL_UNSIGNED_BYTE) {
+               if (span->array->ChanType == GL_UNSIGNED_BYTE) {
                   span->array->rgba = span->array->rgba8;
                }
-               else if (rb->DataType == GL_UNSIGNED_SHORT) {
-                  span->array->rgba = (void *) span->array->rgba16;
-               }
                else {
                   span->array->rgba = (void *)
                      span->array->attribs[FRAG_ATTRIB_COL0];
@@ -1373,13 +1366,16 @@ _swrast_write_rgba_span( struct gl_context *ctx, SWspan *span)
 
             if (span->arrayMask & SPAN_XY) {
                /* array of pixel coords */
-               put_values(ctx, rb, span->end,
+               put_values(ctx, rb,
+                          span->array->ChanType, span->end,
                           span->array->x, span->array->y,
                           span->array->rgba, span->array->mask);
             }
             else {
                /* horizontal run of pixels */
-               _swrast_put_row(ctx, rb, span->end, span->x, span->y,
+               _swrast_put_row(ctx, rb,
+                               span->array->ChanType,
+                               span->end, span->x, span->y,
                                span->array->rgba,
                                span->writeAll ? NULL: span->array->mask);
             }
@@ -1414,6 +1410,7 @@ _swrast_read_rgba_span( struct gl_context *ctx, struct gl_renderbuffer *rb,
                         GLuint n, GLint x, GLint y,
                         GLvoid *rgba)
 {
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
    GLenum dstType = GL_FLOAT;
    const GLint bufWidth = (GLint) rb->Width;
    const GLint bufHeight = (GLint) rb->Height;
@@ -1464,7 +1461,7 @@ _swrast_read_rgba_span( struct gl_context *ctx, struct gl_renderbuffer *rb,
             rb->_BaseFormat == GL_LUMINANCE_ALPHA ||
             rb->_BaseFormat == GL_ALPHA);
 
-      assert(rb->Map);
+      assert(srb->Map);
 
       src = _swrast_pixel_address(rb, x + skip, y);