gallium/radeon: fix warnings
[mesa.git] / src / gallium / drivers / r300 / r300_state_derived.c
index e943f1a715f4ae4fd77330dd1be33a9ce7de5c7b..1013557bde9727c5295fe7a51d69fd4d397aa763 100644 (file)
@@ -316,6 +316,7 @@ static void r300_update_rs_block(struct r300_context *r300)
     struct r300_shader_semantics *fs_inputs = &r300_fs(r300)->shader->inputs;
     struct r300_rs_block rs = {0};
     int i, col_count = 0, tex_count = 0, fp_offset = 0, count, loc = 0, tex_ptr = 0;
+    int gen_offset = 0;
     void (*rX00_rs_col)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
     void (*rX00_rs_col_write)(struct r300_rs_block*, int, int, enum r300_rs_col_write_type);
     void (*rX00_rs_tex)(struct r300_rs_block*, int, int, enum r300_rs_swizzle);
@@ -436,9 +437,61 @@ static void r300_update_rs_block(struct r300_context *r300)
         fprintf(stderr, "r300: ERROR: FS input FACE unassigned.\n");
     }
 
+    /* Re-use color varyings for texcoords if possible.
+     *
+     * The colors are interpolated as 20-bit floats (reduced precision),
+     * Use this hack only if there are too many generic varyings.
+     * (number of generic varyings + fog + wpos > 8) */
+    if (r300->screen->caps.is_r500 && !any_bcolor_used && !r300->flatshade &&
+       fs_inputs->face == ATTR_UNUSED &&
+        vs_outputs->num_generic + (vs_outputs->fog != ATTR_UNUSED) +
+        (fs_inputs->wpos != ATTR_UNUSED) > 8) {
+       for (i = 0; i < ATTR_GENERIC_COUNT && col_count < 2; i++) {
+           /* Cannot use color varyings for sprite coords. */
+           if (fs_inputs->generic[i] != ATTR_UNUSED &&
+               (r300->sprite_coord_enable & (1 << i))) {
+               break;
+           }
+
+           if (vs_outputs->generic[i] != ATTR_UNUSED) {
+               /* Set up the color in VAP. */
+               rs.vap_vsm_vtx_assm |= R300_INPUT_CNTL_COLOR;
+               rs.vap_out_vtx_fmt[0] |=
+                       R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT << col_count;
+               stream_loc_notcl[loc++] = 2 + col_count;
+
+               /* Rasterize it. */
+               rX00_rs_col(&rs, col_count, col_count, SWIZ_XYZW);
+
+               /* Write it to the FS input register if it's needed by the FS. */
+               if (fs_inputs->generic[i] != ATTR_UNUSED) {
+                   rX00_rs_col_write(&rs, col_count, fp_offset, WRITE_COLOR);
+                   fp_offset++;
+
+                   DBG(r300, DBG_RS,
+                       "r300: Rasterized generic %i redirected to color %i and written to FS.\n",
+                       i, col_count);
+               } else {
+                   DBG(r300, DBG_RS, "r300: Rasterized generic %i redirected to color %i unused.\n",
+                       i, col_count);
+               }
+               col_count++;
+           } else {
+               /* Skip the FS input register, leave it uninitialized. */
+               /* If we try to set it to (0,0,0,1), it will lock up. */
+               if (fs_inputs->generic[i] != ATTR_UNUSED) {
+                   fp_offset++;
+
+                   DBG(r300, DBG_RS, "r300: FS input generic %i unassigned%s.\n", i);
+               }
+           }
+       }
+       gen_offset = i;
+    }
+
     /* Rasterize texture coordinates. */
-    for (i = 0; i < ATTR_GENERIC_COUNT && tex_count < 8; i++) {
-       bool sprite_coord = false;
+    for (i = gen_offset; i < ATTR_GENERIC_COUNT && tex_count < 8; i++) {
+       boolean sprite_coord = false;
 
        if (fs_inputs->generic[i] != ATTR_UNUSED) {
            sprite_coord = !!(r300->sprite_coord_enable & (1 << i));
@@ -605,7 +658,6 @@ static uint32_t r300_get_border_color(enum pipe_format format,
 {
     const struct util_format_description *desc;
     float border_swizzled[4] = {0};
-    unsigned i;
     union util_color uc = {0};
 
     desc = util_format_description(format);
@@ -616,7 +668,7 @@ static uint32_t r300_get_border_color(enum pipe_format format,
         case PIPE_FORMAT_Z16_UNORM:
             return util_pack_z(PIPE_FORMAT_Z16_UNORM, border[0]);
         case PIPE_FORMAT_X8Z24_UNORM:
-        case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
+        case PIPE_FORMAT_S8_UINT_Z24_UNORM:
             if (is_r500) {
                 return util_pack_z(PIPE_FORMAT_X8Z24_UNORM, border[0]);
             } else {
@@ -629,22 +681,7 @@ static uint32_t r300_get_border_color(enum pipe_format format,
     }
 
     /* Apply inverse swizzle of the format. */
-    for (i = 0; i < 4; i++) {
-        switch (desc->swizzle[i]) {
-        case UTIL_FORMAT_SWIZZLE_X:
-            border_swizzled[0] = border[i];
-            break;
-        case UTIL_FORMAT_SWIZZLE_Y:
-            border_swizzled[1] = border[i];
-            break;
-        case UTIL_FORMAT_SWIZZLE_Z:
-            border_swizzled[2] = border[i];
-            break;
-        case UTIL_FORMAT_SWIZZLE_W:
-            border_swizzled[3] = border[i];
-            break;
-        }
-    }
+    util_format_unswizzle_4f(border_swizzled, border, desc->swizzle);
 
     /* Compressed formats. */
     if (util_format_is_compressed(format)) {
@@ -671,6 +708,12 @@ static uint32_t r300_get_border_color(enum pipe_format format,
         case PIPE_FORMAT_LATC2_UNORM:
             util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
             return uc.ui;
+        case PIPE_FORMAT_DXT1_SRGB:
+        case PIPE_FORMAT_DXT1_SRGBA:
+        case PIPE_FORMAT_DXT3_SRGBA:
+        case PIPE_FORMAT_DXT5_SRGBA:
+            util_pack_color(border_swizzled, PIPE_FORMAT_B8G8R8A8_SRGB, &uc);
+            return uc.ui;
         default:
             util_pack_color(border_swizzled, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
             return uc.ui;
@@ -701,10 +744,18 @@ static uint32_t r300_get_border_color(enum pipe_format format,
 
         default:
         case 8:
-            if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED)
-               util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
-            else
-               util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
+            if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
+                util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SNORM, &uc);
+            } else if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
+                if (desc->nr_channels == 2) {
+                    border_swizzled[3] = border_swizzled[1];
+                    util_pack_color(border_swizzled, PIPE_FORMAT_L8A8_SRGB, &uc);
+                } else {
+                    util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_SRGB, &uc);
+                }
+            } else {
+                util_pack_color(border_swizzled, PIPE_FORMAT_R8G8B8A8_UNORM, &uc);
+            }
             break;
 
         case 10:
@@ -741,25 +792,6 @@ static uint32_t r300_get_border_color(enum pipe_format format,
     return uc.ui;
 }
 
-static boolean util_format_is_float(enum pipe_format format)
-{
-    const struct util_format_description *desc = util_format_description(format);
-    unsigned i;
-
-    if (!format)
-       return FALSE;
-
-    /* Find the first non-void channel. */
-    for (i = 0; i < 4; i++)
-        if (desc->channel[i].type != UTIL_FORMAT_TYPE_VOID)
-            break;
-
-    if (i == 4)
-        return FALSE;
-
-    return desc->channel[i].type == UTIL_FORMAT_TYPE_FLOAT ? TRUE : FALSE;
-}
-
 static void r300_merge_textures_and_samplers(struct r300_context* r300)
 {
     struct r300_textures_state *state =
@@ -768,9 +800,10 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
     struct r300_sampler_state *sampler;
     struct r300_sampler_view *view;
     struct r300_resource *tex;
-    unsigned min_level, max_level, i, j, size;
+    unsigned base_level, min_level, level_count, i, j, size;
     unsigned count = MIN2(state->sampler_view_count,
                           state->sampler_state_count);
+    boolean has_us_format = r300->screen->caps.has_us_format;
 
     /* The KIL opcode fix, see below. */
     if (!count && !r300->screen->caps.is_r500)
@@ -796,42 +829,47 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
             /* Set the border color. */
             texstate->border_color =
                 r300_get_border_color(view->base.format,
-                                      sampler->state.border_color,
+                                      sampler->state.border_color.f,
                                       r300->screen->caps.is_r500);
 
             /* determine min/max levels */
-            max_level = MIN3(sampler->max_lod + view->base.u.tex.first_level,
-                             tex->b.b.b.last_level, view->base.u.tex.last_level);
-            min_level = MIN2(sampler->min_lod + view->base.u.tex.first_level,
-                             max_level);
-
-            if (tex->tex.is_npot && min_level > 0) {
-                /* Even though we do not implement mipmapping for NPOT
-                 * textures, we should at least honor the minimum level
-                 * which is allowed to be displayed. We do this by setting up
-                 * the i-th mipmap level as the zero level. */
-                unsigned offset = tex->tex_offset +
-                                  tex->tex.offset_in_bytes[min_level];
+            base_level = view->base.u.tex.first_level;
+            min_level = sampler->min_lod;
+            level_count = MIN3(sampler->max_lod,
+                               tex->b.b.last_level - base_level,
+                               view->base.u.tex.last_level - base_level);
+
+            if (base_level + min_level) {
+                unsigned offset;
+
+                if (tex->tex.is_npot) {
+                    /* Even though we do not implement mipmapping for NPOT
+                     * textures, we should at least honor the minimum level
+                     * which is allowed to be displayed. We do this by setting up
+                     * an i-th mipmap level as the zero level. */
+                    base_level += min_level;
+                }
+                offset = tex->tex.offset_in_bytes[base_level];
 
                 r300_texture_setup_format_state(r300->screen, tex,
-                                                min_level,
+                                                view->base.format,
+                                                base_level,
+                                                view->width0_override,
+                                               view->height0_override,
                                                 &texstate->format);
                 texstate->format.tile_config |= offset & 0xffffffe0;
                 assert((offset & 0x1f) == 0);
-            } else {
-                texstate->format.tile_config |= tex->tex_offset & 0xffffffe0;
-                assert((tex->tex_offset & 0x1f) == 0);
             }
 
             /* Assign a texture cache region. */
             texstate->format.format1 |= view->texcache_region;
 
             /* Depth textures are kinda special. */
-            if (util_format_is_depth_or_stencil(tex->b.b.b.format)) {
+            if (util_format_is_depth_or_stencil(view->base.format)) {
                 unsigned char depth_swizzle[4];
 
                 if (!r300->screen->caps.is_r500 &&
-                    util_format_get_blocksizebits(tex->b.b.b.format) == 32) {
+                    util_format_get_blocksizebits(view->base.format) == 32) {
                     /* X24x8 is sampled as Y16X16 on r3xx-r4xx.
                      * The depth here is at the Y component. */
                     for (j = 0; j < 4; j++)
@@ -856,16 +894,22 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
             }
 
             if (r300->screen->caps.dxtc_swizzle &&
-                util_format_is_compressed(tex->b.b.b.format)) {
+                util_format_is_compressed(view->base.format)) {
                 texstate->filter1 |= R400_DXTC_SWIZZLE_ENABLE;
             }
 
             /* to emulate 1D textures through 2D ones correctly */
-            if (tex->b.b.b.target == PIPE_TEXTURE_1D) {
+            if (tex->b.b.target == PIPE_TEXTURE_1D) {
                 texstate->filter0 &= ~R300_TX_WRAP_T_MASK;
                 texstate->filter0 |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE);
             }
 
+            /* The hardware doesn't like CLAMP and CLAMP_TO_BORDER
+             * for the 3rd coordinate if the texture isn't 3D. */
+            if (tex->b.b.target != PIPE_TEXTURE_3D) {
+                texstate->filter0 &= ~R300_TX_WRAP_R_MASK;
+            }
+
             if (tex->tex.is_npot) {
                 /* NPOT textures don't support mip filter, unfortunately.
                  * This prevents incorrect rendering. */
@@ -891,12 +935,12 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
                 }
             } else {
                 /* the MAX_MIP level is the largest (finest) one */
-                texstate->format.format0 |= R300_TX_NUM_LEVELS(max_level);
+                texstate->format.format0 |= R300_TX_NUM_LEVELS(level_count);
                 texstate->filter0 |= R300_TX_MAX_MIP_LEVEL(min_level);
             }
 
             /* Float textures only support nearest and mip-nearest filtering. */
-            if (util_format_is_float(tex->b.b.b.format)) {
+            if (util_format_is_float(view->base.format)) {
                 /* No MAG linear filtering. */
                 if ((texstate->filter0 & R300_TX_MAG_FILTER_MASK) ==
                     R300_TX_MAG_FILTER_LINEAR) {
@@ -923,7 +967,7 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
 
             texstate->filter0 |= i << 28;
 
-            size += 16;
+            size += 16 + (has_us_format ? 2 : 0);
             state->count = i+1;
         } else {
             /* For the KIL opcode to work on r3xx-r4xx, the texture unit
@@ -952,7 +996,7 @@ static void r300_merge_textures_and_samplers(struct r300_context* r300)
                 texstate->border_color = 0;
 
                 texstate->filter0 |= i << 28;
-                size += 16;
+                size += 16 + (has_us_format ? 2 : 0);
                 state->count = i+1;
             }
         }
@@ -977,7 +1021,7 @@ static void r300_decompress_depth_textures(struct r300_context *r300)
                           state->sampler_state_count);
     unsigned i;
 
-    if (!r300->hyperz_locked || !r300->locked_zbuffer) {
+    if (!r300->locked_zbuffer) {
         return;
     }