r600g: move lots of state inline helpers to separate header.
[mesa.git] / src / gallium / drivers / r600 / r600_texture.c
index 9dc0208eb139120e2ce0e888e95ae2e762486c4d..b6698e3885ce0d74c660efb4aeb4487a9f99138c 100644 (file)
@@ -128,13 +128,25 @@ struct pipe_resource *r600_texture_create(struct pipe_screen *screen,
        return &resource->base.b;
 }
 
+static void r600_texture_destroy_state(struct pipe_resource *ptexture)
+{
+       struct r600_resource_texture *rtexture = (struct r600_resource_texture*)ptexture;
+
+       for (int i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++) {
+               radeon_state_fini(&rtexture->scissor[i]);
+               radeon_state_fini(&rtexture->db[i]);
+               for (int j = 0; j < 8; j++) {
+                       radeon_state_fini(&rtexture->cb[j][i]);
+               }
+       }
+}
+
 static void r600_texture_destroy(struct pipe_screen *screen,
                                 struct pipe_resource *ptex)
 {
        struct r600_resource_texture *rtex = (struct r600_resource_texture*)ptex;
        struct r600_resource *resource = &rtex->resource;
        struct r600_screen *rscreen = r600_screen(screen);
-       unsigned i;
 
        if (resource->bo) {
                radeon_bo_decref(rscreen->rw, resource->bo);
@@ -142,11 +154,7 @@ static void r600_texture_destroy(struct pipe_screen *screen,
        if (rtex->uncompressed) {
                radeon_bo_decref(rscreen->rw, rtex->uncompressed);
        }
-       for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++) {
-               radeon_state_decref(rtex->scissor[i]);
-               radeon_state_decref(rtex->cb0[i]);
-               radeon_state_decref(rtex->db[i]);
-       }
+       r600_texture_destroy_state(ptex);
        FREE(rtex);
 }
 
@@ -211,9 +219,12 @@ struct pipe_resource *r600_texture_from_handle(struct pipe_screen *screen,
        pipe_reference_init(&resource->base.b.reference, 1);
        resource->base.b.screen = screen;
        resource->bo = bo;
+       rtex->depth = 0;
        rtex->pitch_override = whandle->stride;
        rtex->bpt = util_format_get_blocksize(templ->format);
        rtex->pitch[0] = whandle->stride;
+       rtex->width[0] = templ->width0;
+       rtex->height[0] = templ->height0;
        rtex->offset[0] = 0;
        rtex->size = align(rtex->pitch[0] * templ->height0, 64);
 
@@ -444,6 +455,8 @@ uint32_t r600_translate_texformat(enum pipe_format format,
        };
        desc = util_format_description(format);
 
+       word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
+
        /* Colorspace (return non-RGB formats directly). */
        switch (desc->colorspace) {
                /* Depth stencil formats */
@@ -482,8 +495,6 @@ uint32_t r600_translate_texformat(enum pipe_format format,
                break;
        }
        
-       word4 |= r600_get_swizzle_combined(desc->swizzle, swizzle_view);
-
        /* S3TC formats. TODO */
        if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
                goto out_unknown;
@@ -621,7 +632,7 @@ out_word4:
                *yuv_format_p = yuv_format;
        return result;
 out_unknown:
-       R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));
+//     R600_ERR("Unable to handle texformat %d %s\n", format, util_format_name(format));
        return ~0;
 }
 
@@ -657,16 +668,13 @@ int r600_texture_from_depth(struct pipe_context *ctx, struct r600_resource_textu
        return 0;
 }
 
-static struct radeon_state *r600_texture_state_scissor(struct r600_screen *rscreen,
+static void r600_texture_state_scissor(struct r600_screen *rscreen,
                                        struct r600_resource_texture *rtexture,
                                        unsigned level)
 {
-       struct radeon_state *rstate;
-
-       rstate = radeon_state(rscreen->rw, R600_SCISSOR);
-       if (rstate == NULL)
-               return NULL;
+       struct radeon_state *rstate = &rtexture->scissor[level];
 
+       radeon_state_init(rstate, rscreen->rw, R600_STATE_SCISSOR, 0, 0);
        /* set states (most default value are 0 and struct already
         * initialized to 0, thus avoid resetting them)
         */
@@ -689,16 +697,10 @@ static struct radeon_state *r600_texture_state_scissor(struct r600_screen *rscre
        rstate->states[R600_SCISSOR__PA_SC_WINDOW_SCISSOR_BR] = S_028244_BR_X(rtexture->width[level]) | S_028244_BR_Y(rtexture->height[level]);
        rstate->states[R600_SCISSOR__PA_SC_WINDOW_SCISSOR_TL] = 0x80000000;
 
-       if (radeon_state_pm4(rstate)) {
-               radeon_state_decref(rstate);
-               return NULL;
-       }
-       return rstate;
+       radeon_state_pm4(rstate);
 }
 
-static struct radeon_state *r600_texture_state_cb0(struct r600_screen *rscreen,
-                               struct r600_resource_texture *rtexture,
-                               unsigned level)
+static void r600_texture_state_cb(struct r600_screen *rscreen, struct r600_resource_texture *rtexture, unsigned cb, unsigned level)
 {
        struct radeon_state *rstate;
        struct r600_resource *rbuffer;
@@ -707,9 +709,8 @@ static struct radeon_state *r600_texture_state_cb0(struct r600_screen *rscreen,
        unsigned format, swap, ntype;
        const struct util_format_description *desc;
 
-       rstate = radeon_state(rscreen->rw, R600_CB0);
-       if (rstate == NULL)
-               return NULL;
+       rstate = &rtexture->cb[cb][level];
+       radeon_state_init(rstate, rscreen->rw, R600_STATE_CB0 + cb, 0, 0);
        rbuffer = &rtexture->resource;
 
        /* set states (most default value are 0 and struct already
@@ -742,37 +743,30 @@ static struct radeon_state *r600_texture_state_cb0(struct r600_screen *rscreen,
                rstate->nbo = 3;
                color_info = S_0280A0_SOURCE_FORMAT(1);
        }
-       rstate->reloc_pm4_id[0] = R600_CB__CB_COLOR0_BASE_BO_ID;
-       rstate->reloc_pm4_id[1] = R600_CB__CB_COLOR0_FRAG_BO_ID;
-       rstate->reloc_pm4_id[2] = R600_CB__CB_COLOR0_TILE_BO_ID;
        color_info |= S_0280A0_FORMAT(format) |
                S_0280A0_COMP_SWAP(swap) |
                S_0280A0_BLEND_CLAMP(1) |
                S_0280A0_NUMBER_TYPE(ntype);
-       rstate->states[R600_CB__CB_COLOR0_BASE] = rtexture->offset[level] >> 8;
-       rstate->states[R600_CB__CB_COLOR0_INFO] = color_info;
-       rstate->states[R600_CB__CB_COLOR0_SIZE] = S_028060_PITCH_TILE_MAX(pitch) |
+       rstate->states[R600_CB0__CB_COLOR0_BASE] = rtexture->offset[level] >> 8;
+       rstate->states[R600_CB0__CB_COLOR0_INFO] = color_info;
+       rstate->states[R600_CB0__CB_COLOR0_SIZE] = S_028060_PITCH_TILE_MAX(pitch) |
                                                S_028060_SLICE_TILE_MAX(slice);
 
-       if (radeon_state_pm4(rstate)) {
-               radeon_state_decref(rstate);
-               return NULL;
-       }
-       return rstate;
+       radeon_state_pm4(rstate);
 }
 
-static struct radeon_state *r600_texture_state_db(struct r600_screen *rscreen,
-                               struct r600_resource_texture *rtexture,
-                               unsigned level)
+static void r600_texture_state_db(struct r600_screen *rscreen, struct r600_resource_texture *rtexture, unsigned level)
 {
-       struct radeon_state *rstate;
+       struct radeon_state *rstate = &rtexture->db[level];
        struct r600_resource *rbuffer;
        unsigned pitch, slice, format;
 
-       rstate = radeon_state(rscreen->rw, R600_DB);
-       if (rstate == NULL)
-               return NULL;
+       radeon_state_init(rstate, rscreen->rw, R600_STATE_DB, 0, 0);
        rbuffer = &rtexture->resource;
+       rtexture->tilled = 1;
+       rtexture->array_mode = 2;
+       rtexture->tile_type = 1;
+       rtexture->depth = 1;
 
        /* set states (most default value are 0 and struct already
         * initialized to 0, thus avoid resetting them)
@@ -787,41 +781,28 @@ static struct radeon_state *r600_texture_state_db(struct r600_screen *rscreen,
        rstate->states[R600_DB__DB_PREFETCH_LIMIT] = (rtexture->height[level] / 8) -1;
        rstate->states[R600_DB__DB_DEPTH_SIZE] = S_028000_PITCH_TILE_MAX(pitch) |
                                                S_028000_SLICE_TILE_MAX(slice);
-       rstate->reloc_pm4_id[0] = R600_DB__DB_DEPTH_BASE_BO_ID;
        rstate->bo[0] = radeon_bo_incref(rscreen->rw, rbuffer->bo);
        rstate->placement[0] = RADEON_GEM_DOMAIN_GTT;
        rstate->nbo = 1;
 
-       if (radeon_state_pm4(rstate)) {
-               radeon_state_decref(rstate);
-               return NULL;
-       }
-       return rstate;
+       radeon_state_pm4(rstate);
 }
 
 int r600_texture_scissor(struct pipe_context *ctx, struct r600_resource_texture *rtexture, unsigned level)
 {
        struct r600_screen *rscreen = r600_screen(ctx->screen);
 
-       if (rtexture->scissor[level] == NULL) {
-               rtexture->scissor[level] = r600_texture_state_scissor(rscreen, rtexture, level);
-               if (rtexture->scissor[level] == NULL) {
-                       R600_ERR("failed to create scissor for uncompressing depth\n");
-                       return -ENOMEM;
-               }
+       if (!rtexture->scissor[level].cpm4) {
+               r600_texture_state_scissor(rscreen, rtexture, level);
        }
        return 0;
 }
 
-static struct radeon_state *r600_texture_state_viewport(struct r600_screen *rscreen,
-                                       struct r600_resource_texture *rtexture,
-                                       unsigned level)
+static void r600_texture_state_viewport(struct r600_screen *rscreen, struct r600_resource_texture *rtexture, unsigned level)
 {
-       struct radeon_state *rstate;
+       struct radeon_state *rstate = &rtexture->viewport[level];
 
-       rstate = radeon_state(rscreen->rw, R600_VIEWPORT);
-       if (rstate == NULL)
-               return NULL;
+       radeon_state_init(rstate, rscreen->rw, R600_STATE_VIEWPORT, 0, 0);
 
        /* set states (most default value are 0 and struct already
         * initialized to 0, thus avoid resetting them)
@@ -835,23 +816,15 @@ static struct radeon_state *r600_texture_state_viewport(struct r600_screen *rscr
        rstate->states[R600_VIEWPORT__PA_CL_VTE_CNTL] = 0x0000043F;
        rstate->states[R600_VIEWPORT__PA_SC_VPORT_ZMAX_0] = 0x3F800000;
 
-       if (radeon_state_pm4(rstate)) {
-               radeon_state_decref(rstate);
-               return NULL;
-       }
-       return rstate;
+       radeon_state_pm4(rstate);
 }
 
-int r600_texture_cb0(struct pipe_context *ctx, struct r600_resource_texture *rtexture, unsigned level)
+int r600_texture_cb(struct pipe_context *ctx, struct r600_resource_texture *rtexture, unsigned cb, unsigned level)
 {
        struct r600_screen *rscreen = r600_screen(ctx->screen);
 
-       if (rtexture->cb0[level] == NULL) {
-               rtexture->cb0[level] = r600_texture_state_cb0(rscreen, rtexture, level);
-               if (rtexture->cb0[level] == NULL) {
-                       R600_ERR("failed to create cb0 state for texture\n");
-                       return -ENOMEM;
-               }
+       if (!rtexture->cb[cb][level].cpm4) {
+               r600_texture_state_cb(rscreen, rtexture, cb, level);
        }
        return 0;
 }
@@ -860,12 +833,8 @@ int r600_texture_db(struct pipe_context *ctx, struct r600_resource_texture *rtex
 {
        struct r600_screen *rscreen = r600_screen(ctx->screen);
 
-       if (rtexture->db[level] == NULL) {
-               rtexture->db[level] = r600_texture_state_db(rscreen, rtexture, level);
-               if (rtexture->db[level] == NULL) {
-                       R600_ERR("failed to create db state for texture\n");
-                       return -ENOMEM;
-               }
+       if (!rtexture->db[level].cpm4) {
+               r600_texture_state_db(rscreen, rtexture, level);
        }
        return 0;
 }
@@ -874,12 +843,8 @@ int r600_texture_viewport(struct pipe_context *ctx, struct r600_resource_texture
 {
        struct r600_screen *rscreen = r600_screen(ctx->screen);
 
-       if (rtexture->viewport[level] == NULL) {
-               rtexture->viewport[level] = r600_texture_state_viewport(rscreen, rtexture, level);
-               if (rtexture->viewport[level] == NULL) {
-                       R600_ERR("failed to create viewport state for texture\n");
-                       return -ENOMEM;
-               }
+       if (!rtexture->viewport[level].cpm4) {
+               r600_texture_state_viewport(rscreen, rtexture, level);
        }
        return 0;
 }