r300g: cleanup texture creation code
[mesa.git] / src / gallium / drivers / r300 / r300_state.c
index 6177ab3a5478e5507aad8d63b25371c4052797fe..6e2a6ca0e42d87f850ecc3b409d5beeeacac975f 100644 (file)
@@ -23,6 +23,7 @@
 
 #include "draw/draw_context.h"
 
+#include "util/u_blitter.h"
 #include "util/u_math.h"
 #include "util/u_memory.h"
 #include "util/u_pack_color.h"
@@ -428,14 +429,19 @@ static void r300_set_clip_state(struct pipe_context* pipe,
     clip->clip = *state;
 
     if (r300->screen->caps.has_tcl) {
-        BEGIN_CB(clip->cb, 29);
-        OUT_CB_REG(R300_VAP_PVS_VECTOR_INDX_REG,
-                (r300->screen->caps.is_r500 ?
-                 R500_PVS_UCP_START : R300_PVS_UCP_START));
-        OUT_CB_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, 6 * 4);
-        OUT_CB_TABLE(state->ucp, 6 * 4);
+        r300->clip_state.size = 2 + !!state->nr * 3 + state->nr * 4;
+
+        BEGIN_CB(clip->cb, r300->clip_state.size);
+        if (state->nr) {
+           OUT_CB_REG(R300_VAP_PVS_VECTOR_INDX_REG,
+                   (r300->screen->caps.is_r500 ?
+                    R500_PVS_UCP_START : R300_PVS_UCP_START));
+           OUT_CB_ONE_REG(R300_VAP_PVS_UPLOAD_DATA, state->nr * 4);
+           OUT_CB_TABLE(state->ucp, state->nr * 4);
+        }
         OUT_CB_REG(R300_VAP_CLIP_CNTL, ((1 << state->nr) - 1) |
-                R300_PS_UCP_MODE_CLIP_AS_TRIFAN);
+                R300_PS_UCP_MODE_CLIP_AS_TRIFAN |
+                (state->depth_clamp ? R300_CLIP_DISABLE : 0));
         END_CB;
 
         r300->clip_state.dirty = TRUE;
@@ -608,32 +614,43 @@ static void r300_set_stencil_ref(struct pipe_context* pipe,
     r300->dsa_state.dirty = TRUE;
 }
 
+static void r300_tex_set_tiling_flags(struct r300_context *r300,
+                                      struct r300_texture *tex, unsigned level)
+{
+    /* Check if the macrotile flag needs to be changed.
+     * Skip changing the flags otherwise. */
+    if (tex->desc.macrotile[tex->surface_level] !=
+        tex->desc.macrotile[level]) {
+        /* Tiling determines how DRM treats the buffer data.
+         * We must flush CS when changing it if the buffer is referenced. */
+        if (r300->rws->cs_is_buffer_referenced(r300->cs,
+                                               tex->buffer, R300_REF_CS))
+            r300->context.flush(&r300->context, 0, NULL);
+
+        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
+                tex->desc.microtile, tex->desc.macrotile[level],
+                tex->desc.stride_in_bytes[0]);
+
+        tex->surface_level = level;
+    }
+}
+
 /* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
 static void r300_fb_set_tiling_flags(struct r300_context *r300,
-                               const struct pipe_framebuffer_state *old_state,
-                               const struct pipe_framebuffer_state *new_state)
+                               const struct pipe_framebuffer_state *state)
 {
-    struct r300_texture *tex;
-    unsigned i, level;
+    unsigned i;
 
     /* Set tiling flags for new surfaces. */
-    for (i = 0; i < new_state->nr_cbufs; i++) {
-        tex = r300_texture(new_state->cbufs[i]->texture);
-        level = new_state->cbufs[i]->level;
-
-        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
-                tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
-                tex->microtile,
-                tex->mip_macrotile[level]);
+    for (i = 0; i < state->nr_cbufs; i++) {
+        r300_tex_set_tiling_flags(r300,
+                                  r300_texture(state->cbufs[i]->texture),
+                                  state->cbufs[i]->level);
     }
-    if (new_state->zsbuf) {
-        tex = r300_texture(new_state->zsbuf->texture);
-        level = new_state->zsbuf->level;
-
-        r300->rws->buffer_set_tiling(r300->rws, tex->buffer,
-                tex->pitch[0] * util_format_get_blocksize(tex->b.b.format),
-                tex->microtile,
-                tex->mip_macrotile[level]);
+    if (state->zsbuf) {
+        r300_tex_set_tiling_flags(r300,
+                                  r300_texture(state->zsbuf->texture),
+                                  state->zsbuf->level);
     }
 }
 
@@ -654,27 +671,37 @@ static void r300_print_fb_surf_info(struct pipe_surface *surf, unsigned index,
             surf->zslice, surf->face, surf->level,
             util_format_short_name(surf->format),
 
-            rtex->macrotile ? "YES" : " NO", rtex->microtile ? "YES" : " NO",
-            rtex->hwpitch[0], tex->width0, tex->height0, tex->depth0,
+            rtex->desc.macrotile[0] ? "YES" : " NO",
+            rtex->desc.microtile ? "YES" : " NO",
+            rtex->desc.stride_in_pixels[0],
+            tex->width0, tex->height0, tex->depth0,
             tex->last_level, util_format_short_name(tex->format));
 }
 
-static void copy_framebuffer_state(struct pipe_framebuffer_state *dst,
-                                   const struct pipe_framebuffer_state *src)
+void r300_mark_fb_state_dirty(struct r300_context *r300,
+                              enum r300_fb_state_change change)
 {
-    unsigned i;
+    struct pipe_framebuffer_state *state = r300->fb_state.state;
 
-    for (i = 0; i < src->nr_cbufs; i++) {
-        pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
-    }
-    for (; i < dst->nr_cbufs; i++) {
-        pipe_surface_reference(&dst->cbufs[i], NULL);
+    /* What is marked as dirty depends on the enum r300_fb_state_change. */
+    r300->gpu_flush.dirty = TRUE;
+    r300->fb_state.dirty = TRUE;
+    r300->hyperz_state.dirty = TRUE;
+
+    if (change == R300_CHANGED_FB_STATE) {
+        r300->aa_state.dirty = TRUE;
+        r300->fb_state_pipelined.dirty = TRUE;
     }
-    pipe_surface_reference(&dst->zsbuf, src->zsbuf);
 
-    dst->nr_cbufs = src->nr_cbufs;
-    dst->width = src->width;
-    dst->height = src->height;
+    /* Now compute the fb_state atom size. */
+    r300->fb_state.size = 2 + (8 * state->nr_cbufs);
+
+    if (r300->cbzb_clear)
+        r300->fb_state.size += 10;
+    else if (state->zsbuf)
+        r300->fb_state.size += r300->screen->caps.has_hiz ? 18 : 14;
+
+    /* The size of the rest of atoms stays the same. */
 }
 
 static void
@@ -705,12 +732,6 @@ static void
         draw_flush(r300->draw);
     }
 
-    r300->gpu_flush.dirty = TRUE;
-    r300->aa_state.dirty = TRUE;
-    r300->fb_state.dirty = TRUE;
-    r300->hyperz_state.dirty = TRUE;
-    r300->fb_state_pipelined.dirty = TRUE;
-
     /* If nr_cbufs is changed from zero to non-zero or vice versa... */
     if (!!old_state->nr_cbufs != !!state->nr_cbufs) {
         r300->blend_state.dirty = TRUE;
@@ -721,14 +742,11 @@ static void
     }
 
     /* The tiling flags are dependent on the surface miplevel, unfortunately. */
-    r300_fb_set_tiling_flags(r300, r300->fb_state.state, state);
+    r300_fb_set_tiling_flags(r300, state);
 
-    copy_framebuffer_state(r300->fb_state.state, state);
+    util_assign_framebuffer_state(r300->fb_state.state, state);
 
-    r300->fb_state.size =
-            2 +
-            (8 * state->nr_cbufs) +
-            (state->zsbuf ? (r300->screen->caps.has_hiz ? 18 : 14) : 0);
+    r300_mark_fb_state_dirty(r300, R300_CHANGED_FB_STATE);
 
     /* Polygon offset depends on the zbuffer bit depth. */
     if (state->zsbuf && r300->polygon_offset_enabled) {
@@ -886,9 +904,9 @@ static void* r300_create_rs_state(struct pipe_context* pipe,
 
     /* Point sprites texture coordinates, 0: lower left, 1: upper right */
     float point_texcoord_left;      /* R300_GA_POINT_S0: 0x4200 */
-    float point_texcoord_bottom;    /* R300_GA_POINT_T0: 0x4204 */
+    float point_texcoord_bottom = 0;/* R300_GA_POINT_T0: 0x4204 */
     float point_texcoord_right;     /* R300_GA_POINT_S1: 0x4208 */
-    float point_texcoord_top;       /* R300_GA_POINT_T1: 0x420c */
+    float point_texcoord_top = 0;   /* R300_GA_POINT_T1: 0x420c */
     CB_LOCALS;
 
     /* Copy rasterizer state. */
@@ -981,6 +999,9 @@ static void* r300_create_rs_state(struct pipe_context* pipe,
                 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK);
         /* XXX this might need to be scaled up */
         line_stipple_value = state->line_stipple_pattern;
+    } else {
+        line_stipple_config = 0;
+        line_stipple_value = 0;
     }
 
     if (state->flatshade) {
@@ -1025,7 +1046,7 @@ static void* r300_create_rs_state(struct pipe_context* pipe,
     OUT_CB(line_control);
     OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_ENABLE, 2);
     OUT_CB(polygon_offset_enable);
-    rs->cull_mode_index = 25 - cs_count;
+    rs->cull_mode_index = 9;
     OUT_CB(cull_mode);
     OUT_CB_REG(R300_GA_LINE_STIPPLE_CONFIG, line_stipple_config);
     OUT_CB_REG(R300_GA_LINE_STIPPLE_VALUE, line_stipple_value);
@@ -1161,7 +1182,7 @@ static void*
 
     lod_bias = CLAMP((int)(state->lod_bias * 32 + 1), -(1 << 9), (1 << 9) - 1);
 
-    sampler->filter1 |= lod_bias << R300_LOD_BIAS_SHIFT;
+    sampler->filter1 |= (lod_bias << R300_LOD_BIAS_SHIFT) & R300_LOD_BIAS_MASK;
 
     /* This is very high quality anisotropic filtering for R5xx.
      * It's good for benchmarking the performance of texturing but
@@ -1275,7 +1296,7 @@ static void r300_set_fragment_sampler_views(struct pipe_context* pipe,
             /* Set the texrect factor in the fragment shader.
              * Needed for RECT and NPOT fallback. */
             texture = r300_texture(views[i]->texture);
-            if (texture->uses_pitch) {
+            if (texture->desc.uses_stride_addressing) {
                 r300->fs_rc_constant_state.dirty = TRUE;
             }
 
@@ -1725,8 +1746,7 @@ static void r300_set_constant_buffer(struct pipe_context *pipe,
 {
     struct r300_context* r300 = r300_context(pipe);
     struct r300_constant_buffer *cbuf;
-    struct pipe_transfer *tr;
-    float *mapped;
+    uint32_t *mapped = r300_buffer(buf)->user_buffer;
     int max_size = 0, max_size_bytes = 0, clamped_size = 0;
 
     switch (shader) {
@@ -1749,8 +1769,7 @@ static void r300_set_constant_buffer(struct pipe_context *pipe,
     max_size_bytes = max_size * 4 * sizeof(float);
 
     if (buf == NULL || buf->width0 == 0 ||
-        (mapped = pipe_buffer_map(pipe, buf, PIPE_TRANSFER_READ, &tr)) == NULL)
-    {
+        (mapped = r300_buffer(buf)->constant_buffer) == NULL) {
         cbuf->count = 0;
         return;
     }
@@ -1768,17 +1787,7 @@ static void r300_set_constant_buffer(struct pipe_context *pipe,
 
         clamped_size = MIN2(buf->width0, max_size_bytes);
         cbuf->count = clamped_size / (4 * sizeof(float));
-
-        if (shader == PIPE_SHADER_FRAGMENT && !r300->screen->caps.is_r500) {
-            unsigned i,j;
-
-            /* Convert constants to float24. */
-            for (i = 0; i < cbuf->count; i++)
-                for (j = 0; j < 4; j++)
-                    cbuf->constants[i][j] = pack_float24(mapped[i*4+j]);
-        } else {
-            memcpy(cbuf->constants, mapped, clamped_size);
-        }
+        cbuf->ptr = mapped;
     }
 
     if (shader == PIPE_SHADER_VERTEX) {
@@ -1794,8 +1803,6 @@ static void r300_set_constant_buffer(struct pipe_context *pipe,
     } else if (shader == PIPE_SHADER_FRAGMENT) {
         r300->fs_constants.dirty = TRUE;
     }
-
-    pipe_buffer_unmap(pipe, buf, tr);
 }
 
 void r300_init_state_functions(struct r300_context* r300)