drivers: don't include texformat.h
[mesa.git] / src / mesa / drivers / dri / radeon / radeon_texstate.c
index 1aa5f980ce2c9b4690e06105d0f9240bd300bd42..c7786381ae5890aef89cd0d5ca565d7c8b562125 100644 (file)
@@ -38,7 +38,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 #include "main/colormac.h"
 #include "main/context.h"
 #include "main/macros.h"
-#include "main/texformat.h"
+#include "main/teximage.h"
 #include "main/texobj.h"
 #include "main/enums.h"
 
@@ -80,8 +80,10 @@ struct tx_table {
    GLuint format, filter;
 };
 
+/* XXX verify this table against MESA_FORMAT_x values */
 static const struct tx_table tx_table[] =
 {
+   _INVALID(NONE), /* MESA_FORMAT_NONE */
    _ALPHA(RGBA8888),
    _ALPHA_REV(RGBA8888),
    _ALPHA(ARGB8888),
@@ -276,7 +278,7 @@ static GLboolean radeonUpdateTextureEnv( GLcontext *ctx, int unit )
    assert( (texUnit->_ReallyEnabled == 0)
           || (texUnit->_Current != NULL) );
 
-   if ( RADEON_DEBUG & DEBUG_TEXTURE ) {
+   if ( RADEON_DEBUG & RADEON_TEXTURE ) {
       fprintf( stderr, "%s( %p, %d )\n", __FUNCTION__, (void *)ctx, unit );
    }
 
@@ -614,8 +616,9 @@ void radeonSetTexOffset(__DRIcontext * pDRICtx, GLint texname,
 
        if (!offset)
                return;
-
-       t->pp_txoffset = offset;
+       
+       t->bo = NULL;
+       t->override_offset = offset;
        t->pp_txpitch = pitch - 32;
 
        switch (depth) {
@@ -635,6 +638,125 @@ void radeonSetTexOffset(__DRIcontext * pDRICtx, GLint texname,
        }
 }
 
+void radeonSetTexBuffer2(__DRIcontext *pDRICtx, GLint target, GLint glx_texture_format,
+                        __DRIdrawable *dPriv)
+{
+       struct gl_texture_unit *texUnit;
+       struct gl_texture_object *texObj;
+       struct gl_texture_image *texImage;
+       struct radeon_renderbuffer *rb;
+       radeon_texture_image *rImage;
+       radeonContextPtr radeon;
+       r100ContextPtr rmesa;
+       struct radeon_framebuffer *rfb;
+       radeonTexObjPtr t;
+       uint32_t pitch_val;
+       uint32_t internalFormat, type, format;
+
+       type = GL_BGRA;
+       format = GL_UNSIGNED_BYTE;
+       internalFormat = (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT ? 3 : 4);
+
+       radeon = pDRICtx->driverPrivate;
+       rmesa = pDRICtx->driverPrivate;
+
+       rfb = dPriv->driverPrivate;
+        texUnit = &radeon->glCtx->Texture.Unit[radeon->glCtx->Texture.CurrentUnit];
+       texObj = _mesa_select_tex_object(radeon->glCtx, texUnit, target);
+        texImage = _mesa_get_tex_image(radeon->glCtx, texObj, target, 0);
+
+       rImage = get_radeon_texture_image(texImage);
+       t = radeon_tex_obj(texObj);
+        if (t == NULL) {
+           return;
+       }
+
+       radeon_update_renderbuffers(pDRICtx, dPriv);
+       /* back & depth buffer are useless free them right away */
+       rb = (void*)rfb->base.Attachment[BUFFER_DEPTH].Renderbuffer;
+       if (rb && rb->bo) {
+               radeon_bo_unref(rb->bo);
+        rb->bo = NULL;
+       }
+       rb = (void*)rfb->base.Attachment[BUFFER_BACK_LEFT].Renderbuffer;
+       if (rb && rb->bo) {
+               radeon_bo_unref(rb->bo);
+               rb->bo = NULL;
+       }
+       rb = rfb->color_rb[0];
+       if (rb->bo == NULL) {
+               /* Failed to BO for the buffer */
+               return;
+       }
+       
+       _mesa_lock_texture(radeon->glCtx, texObj);
+       if (t->bo) {
+               radeon_bo_unref(t->bo);
+               t->bo = NULL;
+       }
+       if (rImage->bo) {
+               radeon_bo_unref(rImage->bo);
+               rImage->bo = NULL;
+       }
+       if (t->mt) {
+               radeon_miptree_unreference(t->mt);
+               t->mt = NULL;
+       }
+       if (rImage->mt) {
+               radeon_miptree_unreference(rImage->mt);
+               rImage->mt = NULL;
+       }
+       _mesa_init_teximage_fields(radeon->glCtx, target, texImage,
+                                  rb->base.Width, rb->base.Height, 1, 0, rb->cpp);
+       texImage->RowStride = rb->pitch / rb->cpp;
+       texImage->TexFormat = radeonChooseTextureFormat(radeon->glCtx,
+                                                       internalFormat,
+                                                       type, format, 0);
+       rImage->bo = rb->bo;
+       radeon_bo_ref(rImage->bo);
+       t->bo = rb->bo;
+       radeon_bo_ref(t->bo);
+       t->tile_bits = 0;
+       t->image_override = GL_TRUE;
+       t->override_offset = 0;
+       t->pp_txpitch &= (1 << 13) -1;
+       pitch_val = rb->pitch;
+       switch (rb->cpp) {
+       case 4:
+               if (glx_texture_format == GLX_TEXTURE_FORMAT_RGB_EXT)
+                       t->pp_txformat = tx_table[MESA_FORMAT_RGB888].format;
+               else
+                       t->pp_txformat = tx_table[MESA_FORMAT_ARGB8888].format;
+               t->pp_txfilter |= tx_table[MESA_FORMAT_ARGB8888].filter;
+               break;
+       case 3:
+       default:
+               t->pp_txformat = tx_table[MESA_FORMAT_RGB888].format;
+               t->pp_txfilter |= tx_table[MESA_FORMAT_RGB888].filter;
+               break;
+       case 2:
+               t->pp_txformat = tx_table[MESA_FORMAT_RGB565].format;
+               t->pp_txfilter |= tx_table[MESA_FORMAT_RGB565].filter;
+               break;
+       }
+        t->pp_txsize = ((rb->base.Width - 1) << RADEON_TEX_USIZE_SHIFT)
+                  | ((rb->base.Height - 1) << RADEON_TEX_VSIZE_SHIFT);
+        t->pp_txformat |= RADEON_TXFORMAT_NON_POWER2;
+       t->pp_txpitch = pitch_val;
+        t->pp_txpitch -= 32;
+
+       t->validated = GL_TRUE;
+       _mesa_unlock_texture(radeon->glCtx, texObj);
+       return;
+}
+
+
+void radeonSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
+{
+        radeonSetTexBuffer2(pDRICtx, target, GLX_TEXTURE_FORMAT_RGBA_EXT, dPriv);
+}
+
+
 #define TEXOBJ_TXFILTER_MASK (RADEON_MAX_MIP_LEVEL_MASK |      \
                              RADEON_MIN_FILTER_MASK |          \
                              RADEON_MAG_FILTER_MASK |          \
@@ -658,11 +780,6 @@ void radeonSetTexOffset(__DRIcontext * pDRICtx, GLint texname,
 static void disable_tex_obj_state( r100ContextPtr rmesa, 
                                   int unit )
 {
-   /* do not use RADEON_DB_STATE to avoid stale texture caches */
-   uint32_t *cmd = &rmesa->hw.tex[unit].cmd[TEX_CMD_0];
-   GLuint se_coord_fmt = rmesa->hw.set.cmd[SET_SE_COORDFMT];
-   GLuint *txr_cmd = RADEON_DB_STATE( txr[unit] );
-
    RADEON_STATECHANGE( rmesa, tex[unit] );
 
    RADEON_STATECHANGE( rmesa, tcl );
@@ -715,14 +832,16 @@ static void import_tex_obj_state( r100ContextPtr rmesa,
    cmd[TEX_PP_TXFILTER] |= texobj->pp_txfilter & TEXOBJ_TXFILTER_MASK;
    cmd[TEX_PP_TXFORMAT] &= ~TEXOBJ_TXFORMAT_MASK;
    cmd[TEX_PP_TXFORMAT] |= texobj->pp_txformat & TEXOBJ_TXFORMAT_MASK;
-   cmd[TEX_PP_TXOFFSET] = texobj->pp_txoffset;
    cmd[TEX_PP_BORDER_COLOR] = texobj->pp_border_color;
 
-   if (texobj->base.Target == GL_TEXTURE_RECTANGLE_NV) {
-      GLuint *txr_cmd = RADEON_DB_STATE( txr[unit] );
+   if (texobj->pp_txformat & RADEON_TXFORMAT_NON_POWER2) {
+      uint32_t *txr_cmd = &rmesa->hw.txr[unit].cmd[TXR_CMD_0];
       txr_cmd[TXR_PP_TEX_SIZE] = texobj->pp_txsize; /* NPOT only! */
       txr_cmd[TXR_PP_TEX_PITCH] = texobj->pp_txpitch; /* NPOT only! */
-      RADEON_DB_STATECHANGE( rmesa, &rmesa->hw.txr[unit] );
+      RADEON_STATECHANGE( rmesa, txr[unit] );
+   }
+
+   if (texobj->base.Target == GL_TEXTURE_RECTANGLE_NV) {
       se_coord_fmt |= RADEON_VTX_ST0_NONPARAMETRIC << unit;
    }
    else {
@@ -742,8 +861,6 @@ static void import_tex_obj_state( r100ContextPtr rmesa,
       rmesa->hw.set.cmd[SET_SE_COORDFMT] = se_coord_fmt;
    }
 
-   texobj->dirty_state &= ~(1<<unit);
-
    rmesa->radeon.NewGLState |= _NEW_TEXTURE_MATRIX;
 }
 
@@ -813,14 +930,14 @@ static GLboolean radeon_validate_texgen( GLcontext *ctx, GLuint unit )
     */
    else if ( (texUnit->TexGenEnabled & S_BIT) &&
             (texUnit->TexGenEnabled & T_BIT) &&
-            (texUnit->GenModeS == texUnit->GenModeT) ) {
+            (texUnit->GenS.Mode == texUnit->GenT.Mode) ) {
       if ( ((texUnit->TexGenEnabled & R_BIT) &&
-           (texUnit->GenModeS != texUnit->GenModeR)) ||
+           (texUnit->GenS.Mode != texUnit->GenR.Mode)) ||
           ((texUnit->TexGenEnabled & Q_BIT) &&
-           (texUnit->GenModeS != texUnit->GenModeQ)) ) {
+           (texUnit->GenS.Mode != texUnit->GenQ.Mode)) ) {
         /* Mixed modes, fallback:
          */
-        if (RADEON_DEBUG & DEBUG_FALLBACKS)
+        if (RADEON_DEBUG & RADEON_FALLBACKS)
            fprintf(stderr, "fallback mixed texgen\n");
         return GL_FALSE;
       }
@@ -828,7 +945,7 @@ static GLboolean radeon_validate_texgen( GLcontext *ctx, GLuint unit )
    }
    else {
    /* some texgen mode not including both S and T bits */
-      if (RADEON_DEBUG & DEBUG_FALLBACKS)
+      if (RADEON_DEBUG & RADEON_FALLBACKS)
         fprintf(stderr, "fallback mixed texgen/nontexgen\n");
       return GL_FALSE;
    }
@@ -841,23 +958,23 @@ static GLboolean radeon_validate_texgen( GLcontext *ctx, GLuint unit )
       rmesa->hw.tcl.cmd[TCL_OUTPUT_VTXFMT] |= RADEON_Q_BIT(unit);
    }
 
-   switch (texUnit->GenModeS) {
+   switch (texUnit->GenS.Mode) {
    case GL_OBJECT_LINEAR:
       rmesa->TexGenEnabled |= RADEON_TEXGEN_INPUT_OBJ << inputshift;
       set_texgen_matrix( rmesa, unit,
-                        texUnit->ObjectPlaneS,
-                        texUnit->ObjectPlaneT,
-                        texUnit->ObjectPlaneR,
-                        texUnit->ObjectPlaneQ);
+                        texUnit->GenS.ObjectPlane,
+                        texUnit->GenT.ObjectPlane,
+                        texUnit->GenR.ObjectPlane,
+                        texUnit->GenQ.ObjectPlane);
       break;
 
    case GL_EYE_LINEAR:
       rmesa->TexGenEnabled |= RADEON_TEXGEN_INPUT_EYE << inputshift;
       set_texgen_matrix( rmesa, unit,
-                        texUnit->EyePlaneS,
-                        texUnit->EyePlaneT,
-                        texUnit->EyePlaneR,
-                        texUnit->EyePlaneQ);
+                        texUnit->GenS.EyePlane,
+                        texUnit->GenT.EyePlane,
+                        texUnit->GenR.EyePlane,
+                        texUnit->GenQ.EyePlane);
       break;
 
    case GL_REFLECTION_MAP_NV:
@@ -878,7 +995,7 @@ static GLboolean radeon_validate_texgen( GLcontext *ctx, GLuint unit )
    default:
       /* Unsupported mode, fallback:
        */
-      if (RADEON_DEBUG & DEBUG_FALLBACKS) 
+      if (RADEON_DEBUG & RADEON_FALLBACKS)
         fprintf(stderr, "fallback GL_SPHERE_MAP\n");
       return GL_FALSE;
    }
@@ -901,6 +1018,10 @@ static GLboolean setup_hardware_state(r100ContextPtr rmesa, radeonTexObj *t, int
    const struct gl_texture_image *firstImage;
    GLint log2Width, log2Height, log2Depth, texelBytes;
 
+   if ( t->bo ) {
+       return GL_TRUE;
+   }
+
    firstImage = t->base.Image[0][t->mt->firstLevel];   
 
    if (firstImage->Border > 0) {
@@ -911,18 +1032,18 @@ static GLboolean setup_hardware_state(r100ContextPtr rmesa, radeonTexObj *t, int
    log2Width  = firstImage->WidthLog2;
    log2Height = firstImage->HeightLog2;
    log2Depth  = firstImage->DepthLog2;
-   texelBytes = firstImage->TexFormat->TexelBytes;
+   texelBytes = _mesa_get_format_bytes(firstImage->TexFormat);
 
    if (!t->image_override) {
-      if (VALID_FORMAT(firstImage->TexFormat->MesaFormat)) {
+      if (VALID_FORMAT(firstImage->TexFormat)) {
        const struct tx_table *table = tx_table;
 
         t->pp_txformat &= ~(RADEON_TXFORMAT_FORMAT_MASK |
                             RADEON_TXFORMAT_ALPHA_IN_MAP);
         t->pp_txfilter &= ~RADEON_YUV_TO_RGB;   
         
-        t->pp_txformat |= table[ firstImage->TexFormat->MesaFormat ].format;
-        t->pp_txfilter |= table[ firstImage->TexFormat->MesaFormat ].filter;
+        t->pp_txformat |= table[ firstImage->TexFormat ].format;
+        t->pp_txfilter |= table[ firstImage->TexFormat ].filter;
       } else {
         _mesa_problem(NULL, "unexpected texture format in %s",
                       __FUNCTION__);
@@ -963,7 +1084,7 @@ static GLboolean setup_hardware_state(r100ContextPtr rmesa, radeonTexObj *t, int
                   | ((firstImage->Height - 1) << RADEON_TEX_VSIZE_SHIFT));
 
    if ( !t->image_override ) {
-      if (firstImage->IsCompressed)
+      if (_mesa_is_format_compressed(firstImage->TexFormat))
          t->pp_txpitch = (firstImage->Width + 63) & ~(63);
       else
          t->pp_txpitch = ((firstImage->Width * texelBytes) + 63) & ~(63);
@@ -973,6 +1094,7 @@ static GLboolean setup_hardware_state(r100ContextPtr rmesa, radeonTexObj *t, int
    if (t->base.Target == GL_TEXTURE_RECTANGLE_NV) {
       t->pp_txformat |= RADEON_TXFORMAT_NON_POWER2;
    }
+
    return GL_TRUE;
 }
 
@@ -985,27 +1107,23 @@ static GLboolean radeon_validate_texture(GLcontext *ctx, struct gl_texture_objec
    if (!radeon_validate_texture_miptree(ctx, texObj))
       return GL_FALSE;
 
-   /* yuv conversion only works in first unit */
-   if (unit != 0 && (t->pp_txfilter & RADEON_YUV_TO_RGB))
-      return GL_FALSE;
-
-
    ret = setup_hardware_state(rmesa, t, unit);
    if (ret == GL_FALSE)
      return GL_FALSE;
 
+   /* yuv conversion only works in first unit */
+   if (unit != 0 && (t->pp_txfilter & RADEON_YUV_TO_RGB))
+      return GL_FALSE;
+
    RADEON_STATECHANGE( rmesa, ctx );
    rmesa->hw.ctx.cmd[CTX_PP_CNTL] |= 
      (RADEON_TEX_0_ENABLE | RADEON_TEX_BLEND_0_ENABLE) << unit;
-
    RADEON_STATECHANGE( rmesa, tcl );
    rmesa->hw.tcl.cmd[TCL_OUTPUT_VTXFMT] |= RADEON_ST_BIT(unit);
 
    rmesa->recheck_texgen[unit] = GL_TRUE;
 
-   if (t->dirty_state & (1<<unit)) {
-      import_tex_obj_state( rmesa, unit, t );
-   }
+   import_tex_obj_state( rmesa, unit, t );
 
    if (rmesa->recheck_texgen[unit]) {
       GLboolean fallback = !radeon_validate_texgen( ctx, unit );
@@ -1026,16 +1144,16 @@ static GLboolean radeon_validate_texture(GLcontext *ctx, struct gl_texture_objec
 static GLboolean radeonUpdateTextureUnit( GLcontext *ctx, int unit )
 {
    r100ContextPtr rmesa = R100_CONTEXT(ctx);
-   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
-
 
    if (ctx->Texture.Unit[unit]._ReallyEnabled & TEXTURE_3D_BIT) {
+     rmesa->state.texture.unit[unit].texobj = NULL;
      return GL_FALSE;
    }
 
    if (!ctx->Texture.Unit[unit]._ReallyEnabled) {
      /* disable the unit */
      disable_tex_obj_state(rmesa, unit);
+     rmesa->state.texture.unit[unit].texobj = NULL;
      return GL_TRUE;
    }
 
@@ -1043,8 +1161,8 @@ static GLboolean radeonUpdateTextureUnit( GLcontext *ctx, int unit )
     _mesa_warning(ctx,
                  "failed to validate texture for unit %d.\n",
                  unit);
-    rmesa->state.texture.unit[unit].texobj = NULL;
-    return GL_FALSE;
+     rmesa->state.texture.unit[unit].texobj = NULL;
+     return GL_FALSE;
    }
    rmesa->state.texture.unit[unit].texobj = radeon_tex_obj(ctx->Texture.Unit[unit]._Current);
    return GL_TRUE;