draw: corrections to allow for different cliptest cases
[mesa.git] / src / gallium / winsys / r600 / drm / radeon_ctx.c
index 47fca76136825902c0b774b0f0854a7a260acc17..7ccb52459055729b564f3916619e3a5a3cd3bb3d 100644 (file)
 #include "radeon_drm.h"
 #include "bof.h"
 
-static int radeon_ctx_set_bo_new(struct radeon_ctx *ctx, struct radeon_bo *bo)
+static int radeon_ctx_set_bo_new(struct radeon_ctx *ctx, struct radeon_ws_bo *bo)
 {
        if (ctx->nbo >= RADEON_CTX_MAX_PM4)
                return -EBUSY;
-       ctx->bo[ctx->nbo] = bo;
+       /* take a reference to the kernel bo */
+       radeon_bo_reference(ctx->radeon, &ctx->bo[ctx->nbo], radeon_bo_pb_get_bo(bo->pb));
        ctx->nbo++;
        return 0;
 }
 
-static struct radeon_bo *radeon_ctx_get_bo(struct radeon_ctx *ctx, unsigned reloc)
-{
-       struct radeon_cs_reloc *greloc;
-       unsigned i;
-
-       greloc = (void *)(((u8 *)ctx->reloc) + reloc * 4);
-       for (i = 0; i < ctx->nbo; i++) {
-               if (ctx->bo[i]->handle == greloc->handle) {
-                       return radeon_bo_incref(ctx->radeon, ctx->bo[i]);
-               }
-       }
-       fprintf(stderr, "%s no bo for reloc[%d 0x%08X] %d\n", __func__, reloc, greloc->handle, ctx->nbo);
-       return NULL;
-}
-
 static void radeon_ctx_get_placement(struct radeon_ctx *ctx, unsigned reloc, u32 *placement)
 {
        struct radeon_cs_reloc *greloc;
@@ -74,7 +60,7 @@ static void radeon_ctx_get_placement(struct radeon_ctx *ctx, unsigned reloc, u32
 void radeon_ctx_clear(struct radeon_ctx *ctx)
 {
        for (int i = 0; i < ctx->nbo; i++) {
-               ctx->bo[i] = radeon_bo_decref(ctx->radeon, ctx->bo[i]);
+               radeon_bo_reference(ctx->radeon, &ctx->bo[i], NULL);
        }
        ctx->ndwords = RADEON_CTX_MAX_PM4;
        ctx->cdwords = 0;
@@ -82,29 +68,30 @@ void radeon_ctx_clear(struct radeon_ctx *ctx)
        ctx->nbo = 0;
 }
 
-int radeon_ctx_init(struct radeon_ctx *ctx, struct radeon *radeon)
+struct radeon_ctx *radeon_ctx_init(struct radeon *radeon)
 {
+       struct radeon_ctx *ctx;
        if (radeon == NULL)
-               return -EINVAL;
-       memset(ctx, 0, sizeof(struct radeon_ctx));
+               return NULL;
+       ctx = calloc(1, sizeof(struct radeon_ctx));
        ctx->radeon = radeon_incref(radeon);
        radeon_ctx_clear(ctx);
        ctx->pm4 = malloc(RADEON_CTX_MAX_PM4 * 4);
        if (ctx->pm4 == NULL) {
                radeon_ctx_fini(ctx);
-               return -ENOMEM;
+               return NULL;
        }
        ctx->reloc = malloc(sizeof(struct radeon_cs_reloc) * RADEON_CTX_MAX_PM4);
        if (ctx->reloc == NULL) {
                radeon_ctx_fini(ctx);
-               return -ENOMEM;
+               return NULL;
        }
-       ctx->bo = malloc(sizeof(void *) * RADEON_CTX_MAX_PM4);
+       ctx->bo = calloc(sizeof(void *), RADEON_CTX_MAX_PM4);
        if (ctx->bo == NULL) {
                radeon_ctx_fini(ctx);
-               return -ENOMEM;
+               return NULL;
        }
-       return 0;
+       return ctx;
 }
 
 void radeon_ctx_fini(struct radeon_ctx *ctx)
@@ -115,29 +102,29 @@ void radeon_ctx_fini(struct radeon_ctx *ctx)
                return;
 
        for (i = 0; i < ctx->nbo; i++) {
-               ctx->bo[i] = radeon_bo_decref(ctx->radeon, ctx->bo[i]);
+               radeon_bo_reference(ctx->radeon, &ctx->bo[i], NULL);
        }
        ctx->radeon = radeon_decref(ctx->radeon);
        free(ctx->bo);
        free(ctx->pm4);
        free(ctx->reloc);
-       memset(ctx, 0, sizeof(struct radeon_ctx));
+       free(ctx);
 }
 
 static int radeon_ctx_state_bo(struct radeon_ctx *ctx, struct radeon_state *state)
 {
        unsigned i, j;
        int r;
-
+       struct radeon_bo *state_bo;
        if (state == NULL)
                return 0;
        for (i = 0; i < state->nbo; i++) {
                for (j = 0; j < ctx->nbo; j++) {
-                       if (state->bo[i] == ctx->bo[j])
+                       state_bo = radeon_bo_pb_get_bo(state->bo[i]->pb);
+                       if (state_bo == ctx->bo[j])
                                break;
                }
                if (j == ctx->nbo) {
-                       radeon_bo_incref(ctx->radeon, state->bo[i]);
                        r = radeon_ctx_set_bo_new(ctx, state->bo[i]);
                        if (r)
                                return r;
@@ -156,6 +143,8 @@ int radeon_ctx_submit(struct radeon_ctx *ctx)
 
        if (!ctx->cdwords)
                return 0;
+
+       radeon_bo_pbmgr_flush_maps(ctx->radeon->kman);
 #if 0
        for (r = 0; r < ctx->cdwords; r++) {
                fprintf(stderr, "0x%08X\n", ctx->pm4[r]);
@@ -178,13 +167,14 @@ int radeon_ctx_submit(struct radeon_ctx *ctx)
        return r;
 }
 
-static int radeon_ctx_reloc(struct radeon_ctx *ctx, struct radeon_bo *bo,
+static int radeon_ctx_reloc(struct radeon_ctx *ctx, struct radeon_ws_bo *bo,
                        unsigned id, unsigned *placement)
 {
        unsigned i;
+       unsigned bo_handle = radeon_ws_bo_get_handle(bo);
 
        for (i = 0; i < ctx->nreloc; i++) {
-               if (ctx->reloc[i].handle == bo->handle) {
+               if (ctx->reloc[i].handle == bo_handle) {
                        ctx->pm4[id] = i * sizeof(struct radeon_cs_reloc) / 4;
                        return 0;
                }
@@ -192,7 +182,7 @@ static int radeon_ctx_reloc(struct radeon_ctx *ctx, struct radeon_bo *bo,
        if (ctx->nreloc >= RADEON_CTX_MAX_PM4) {
                return -EBUSY;
        }
-       ctx->reloc[ctx->nreloc].handle = bo->handle;
+       ctx->reloc[ctx->nreloc].handle = bo_handle;
        ctx->reloc[ctx->nreloc].read_domain = placement[0] | placement [1];
        ctx->reloc[ctx->nreloc].write_domain = placement[0] | placement [1];
        ctx->reloc[ctx->nreloc].flags = 0;
@@ -259,25 +249,24 @@ int radeon_ctx_set_draw(struct radeon_ctx *ctx, struct radeon_draw *draw)
 {
        unsigned previous_cdwords;
        int r = 0;
+       int i;
 
-       for (int i = 0; i < (ctx->radeon->nstate_per_shader * R600_SHADER_MAX); i++) {
+       for (i = 0; i < ctx->radeon->max_states; i++) {
                r = radeon_ctx_state_bo(ctx, draw->state[i]);
                if (r)
                        return r;
        }
        previous_cdwords = ctx->cdwords;
-       for (int i = 0, id = 0; i < ctx->radeon->nstate_per_shader; i++) {
-               for (int j = 0; j < R600_SHADER_MAX; j++) {
-                       id = j * ctx->radeon->nstate_per_shader + i;
-                       if (draw->state[id]) {
-                               r = radeon_ctx_state_schedule(ctx, draw->state[id]);
-                               if (r) {
-                                       ctx->cdwords = previous_cdwords;
-                                       return r;
-                               }
+       for (i = 0; i < ctx->radeon->max_states; i++) {
+               if (draw->state[i]) {
+                       r = radeon_ctx_state_schedule(ctx, draw->state[i]);
+                       if (r) {
+                               ctx->cdwords = previous_cdwords;
+                               return r;
                        }
                }
        }
+
        return 0;
 }
 
@@ -308,7 +297,7 @@ void radeon_ctx_dump_bof(struct radeon_ctx *ctx, const char *file)
 {
        bof_t *bcs, *blob, *array, *bo, *size, *handle, *device_id, *root;
        unsigned i;
-
+       unsigned bo_size;
        root = device_id = bcs = blob = array = bo = size = handle = NULL;
        root = bof_object();
        if (root == NULL)
@@ -321,7 +310,6 @@ void radeon_ctx_dump_bof(struct radeon_ctx *ctx, const char *file)
        bof_decref(device_id);
        device_id = NULL;
        /* dump relocs */
-printf("%d relocs\n", ctx->nreloc);
        blob = bof_blob(ctx->nreloc * 16, ctx->reloc);
        if (blob == NULL)
                goto out_err;
@@ -330,7 +318,6 @@ printf("%d relocs\n", ctx->nreloc);
        bof_decref(blob);
        blob = NULL;
        /* dump cs */
-printf("%d pm4\n", ctx->cdwords);
        blob = bof_blob(ctx->cdwords * 4, ctx->pm4);
        if (blob == NULL)
                goto out_err;
@@ -346,7 +333,8 @@ printf("%d pm4\n", ctx->cdwords);
                bo = bof_object();
                if (bo == NULL)
                        goto out_err;
-               size = bof_int32(ctx->bo[i]->size);
+               bo_size = ctx->bo[i]->size;
+               size = bof_int32(bo_size);
                if (size == NULL)
                        goto out_err;
                if (bof_object_set(bo, "size", size))
@@ -361,7 +349,7 @@ printf("%d pm4\n", ctx->cdwords);
                bof_decref(handle);
                handle = NULL;
                radeon_bo_map(ctx->radeon, ctx->bo[i]);
-               blob = bof_blob(ctx->bo[i]->size, ctx->bo[i]->data);
+               blob = bof_blob(bo_size, ctx->bo[i]->data);
                radeon_bo_unmap(ctx->radeon, ctx->bo[i]);
                if (blob == NULL)
                        goto out_err;
@@ -377,7 +365,6 @@ printf("%d pm4\n", ctx->cdwords);
        if (bof_object_set(root, "bo", array))
                goto out_err;
        bof_dump_file(root, file);
-printf("done dump\n");
 out_err:
        bof_decref(blob);
        bof_decref(array);