i965/miptree: Set refcount before failing via _release()
[mesa.git] / src / mesa / drivers / dri / i965 / gen7_sol_state.c
index 9c8e1a059c65462feb7ee1959a3fbfd977d5741d..f54b370cd408dd6ff08b74459d44271de64ef130 100644 (file)
 #include "intel_buffer_objects.h"
 #include "main/transformfeedback.h"
 
-static void
-upload_3dstate_so_buffers(struct brw_context *brw)
-{
-   struct gl_context *ctx = &brw->ctx;
-   /* BRW_NEW_TRANSFORM_FEEDBACK */
-   struct gl_transform_feedback_object *xfb_obj =
-      ctx->TransformFeedback.CurrentObject;
-   const struct gl_transform_feedback_info *linked_xfb_info =
-      xfb_obj->program->sh.LinkedTransformFeedback;
-   int i;
-
-   /* Set up the up to 4 output buffers.  These are the ranges defined in the
-    * gl_transform_feedback_object.
-    */
-   for (i = 0; i < 4; i++) {
-      struct intel_buffer_object *bufferobj =
-        intel_buffer_object(xfb_obj->Buffers[i]);
-      drm_bacon_bo *bo;
-      uint32_t start, end;
-      uint32_t stride;
-
-      if (!xfb_obj->Buffers[i]) {
-        /* The pitch of 0 in this command indicates that the buffer is
-         * unbound and won't be written to.
-         */
-        BEGIN_BATCH(4);
-        OUT_BATCH(_3DSTATE_SO_BUFFER << 16 | (4 - 2));
-        OUT_BATCH((i << SO_BUFFER_INDEX_SHIFT));
-        OUT_BATCH(0);
-        OUT_BATCH(0);
-        ADVANCE_BATCH();
-
-        continue;
-      }
-
-      stride = linked_xfb_info->Buffers[i].Stride * 4;
-
-      start = xfb_obj->Offset[i];
-      assert(start % 4 == 0);
-      end = ALIGN(start + xfb_obj->Size[i], 4);
-      bo = intel_bufferobj_buffer(brw, bufferobj, start, end - start);
-      assert(end <= bo->size);
-
-      BEGIN_BATCH(4);
-      OUT_BATCH(_3DSTATE_SO_BUFFER << 16 | (4 - 2));
-      OUT_BATCH((i << SO_BUFFER_INDEX_SHIFT) | stride);
-      OUT_RELOC(bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, start);
-      OUT_RELOC(bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, end);
-      ADVANCE_BATCH();
-   }
-}
-
-/**
- * Outputs the 3DSTATE_SO_DECL_LIST command.
- *
- * The data output is a series of 64-bit entries containing a SO_DECL per
- * stream.  We only have one stream of rendering coming out of the GS unit, so
- * we only emit stream 0 (low 16 bits) SO_DECLs.
- */
-void
-gen7_upload_3dstate_so_decl_list(struct brw_context *brw,
-                                 const struct brw_vue_map *vue_map)
-{
-   struct gl_context *ctx = &brw->ctx;
-   /* BRW_NEW_TRANSFORM_FEEDBACK */
-   struct gl_transform_feedback_object *xfb_obj =
-      ctx->TransformFeedback.CurrentObject;
-   const struct gl_transform_feedback_info *linked_xfb_info =
-      xfb_obj->program->sh.LinkedTransformFeedback;
-   uint16_t so_decl[MAX_VERTEX_STREAMS][128];
-   int buffer_mask[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
-   int next_offset[BRW_MAX_SOL_BUFFERS] = {0, 0, 0, 0};
-   int decls[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
-   int max_decls = 0;
-   STATIC_ASSERT(ARRAY_SIZE(so_decl[0]) >= MAX_PROGRAM_OUTPUTS);
-
-   memset(so_decl, 0, sizeof(so_decl));
-
-   /* Construct the list of SO_DECLs to be emitted.  The formatting of the
-    * command is feels strange -- each dword pair contains a SO_DECL per stream.
-    */
-   for (unsigned i = 0; i < linked_xfb_info->NumOutputs; i++) {
-      int buffer = linked_xfb_info->Outputs[i].OutputBuffer;
-      uint16_t decl = 0;
-      int varying = linked_xfb_info->Outputs[i].OutputRegister;
-      const unsigned components = linked_xfb_info->Outputs[i].NumComponents;
-      unsigned component_mask = (1 << components) - 1;
-      unsigned stream_id = linked_xfb_info->Outputs[i].StreamId;
-      unsigned decl_buffer_slot = buffer << SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT;
-      assert(stream_id < MAX_VERTEX_STREAMS);
-
-      /* gl_PointSize is stored in VARYING_SLOT_PSIZ.w
-       * gl_Layer is stored in VARYING_SLOT_PSIZ.y
-       * gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
-       */
-      if (varying == VARYING_SLOT_PSIZ) {
-         assert(components == 1);
-         component_mask <<= 3;
-      } else if (varying == VARYING_SLOT_LAYER) {
-         assert(components == 1);
-         component_mask <<= 1;
-      } else if (varying == VARYING_SLOT_VIEWPORT) {
-         assert(components == 1);
-         component_mask <<= 2;
-      } else {
-         component_mask <<= linked_xfb_info->Outputs[i].ComponentOffset;
-      }
-
-      buffer_mask[stream_id] |= 1 << buffer;
-
-      decl |= decl_buffer_slot;
-      if (varying == VARYING_SLOT_LAYER || varying == VARYING_SLOT_VIEWPORT) {
-         decl |= vue_map->varying_to_slot[VARYING_SLOT_PSIZ] <<
-            SO_DECL_REGISTER_INDEX_SHIFT;
-      } else {
-         assert(vue_map->varying_to_slot[varying] >= 0);
-         decl |= vue_map->varying_to_slot[varying] <<
-            SO_DECL_REGISTER_INDEX_SHIFT;
-      }
-      decl |= component_mask << SO_DECL_COMPONENT_MASK_SHIFT;
-
-      /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
-       * array.  Instead, it simply increments DstOffset for the following
-       * input by the number of components that should be skipped.
-       *
-       * Our hardware is unusual in that it requires us to program SO_DECLs
-       * for fake "hole" components, rather than simply taking the offset
-       * for each real varying.  Each hole can have size 1, 2, 3, or 4; we
-       * program as many size = 4 holes as we can, then a final hole to
-       * accommodate the final 1, 2, or 3 remaining.
-       */
-      int skip_components =
-         linked_xfb_info->Outputs[i].DstOffset - next_offset[buffer];
-
-      next_offset[buffer] += skip_components;
-
-      while (skip_components >= 4) {
-         so_decl[stream_id][decls[stream_id]++] =
-            SO_DECL_HOLE_FLAG | 0xf | decl_buffer_slot;
-         skip_components -= 4;
-      }
-      if (skip_components > 0)
-         so_decl[stream_id][decls[stream_id]++] =
-            SO_DECL_HOLE_FLAG | ((1 << skip_components) - 1) |
-            decl_buffer_slot;
-
-      assert(linked_xfb_info->Outputs[i].DstOffset == next_offset[buffer]);
-
-      next_offset[buffer] += components;
-
-      so_decl[stream_id][decls[stream_id]++] = decl;
-
-      if (decls[stream_id] > max_decls)
-         max_decls = decls[stream_id];
-   }
-
-   BEGIN_BATCH(max_decls * 2 + 3);
-   OUT_BATCH(_3DSTATE_SO_DECL_LIST << 16 | (max_decls * 2 + 1));
-
-   OUT_BATCH((buffer_mask[0] << SO_STREAM_TO_BUFFER_SELECTS_0_SHIFT) |
-             (buffer_mask[1] << SO_STREAM_TO_BUFFER_SELECTS_1_SHIFT) |
-             (buffer_mask[2] << SO_STREAM_TO_BUFFER_SELECTS_2_SHIFT) |
-             (buffer_mask[3] << SO_STREAM_TO_BUFFER_SELECTS_3_SHIFT));
-
-   OUT_BATCH((decls[0] << SO_NUM_ENTRIES_0_SHIFT) |
-             (decls[1] << SO_NUM_ENTRIES_1_SHIFT) |
-             (decls[2] << SO_NUM_ENTRIES_2_SHIFT) |
-             (decls[3] << SO_NUM_ENTRIES_3_SHIFT));
-
-   for (int i = 0; i < max_decls; i++) {
-      /* Stream 1 | Stream 0 */
-      OUT_BATCH(((uint32_t) so_decl[1][i]) << 16 | so_decl[0][i]);
-      /* Stream 3 | Stream 2 */
-      OUT_BATCH(((uint32_t) so_decl[3][i]) << 16 | so_decl[2][i]);
-   }
-
-   ADVANCE_BATCH();
-}
-
-static bool
-query_active(struct gl_query_object *q)
-{
-   return q && q->Active;
-}
-
-static void
-upload_3dstate_streamout(struct brw_context *brw, bool active,
-                        const struct brw_vue_map *vue_map)
-{
-   struct gl_context *ctx = &brw->ctx;
-   /* BRW_NEW_TRANSFORM_FEEDBACK */
-   struct gl_transform_feedback_object *xfb_obj =
-      ctx->TransformFeedback.CurrentObject;
-   uint32_t dw1 = 0, dw2 = 0, dw3 = 0, dw4 = 0;
-   int i;
-
-   if (active) {
-      const struct gl_transform_feedback_info *linked_xfb_info =
-         xfb_obj->program->sh.LinkedTransformFeedback;
-      int urb_entry_read_offset = 0;
-      int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
-        urb_entry_read_offset;
-
-      dw1 |= SO_FUNCTION_ENABLE;
-      dw1 |= SO_STATISTICS_ENABLE;
-
-      /* BRW_NEW_RASTERIZER_DISCARD */
-      if (ctx->RasterDiscard) {
-         if (!query_active(ctx->Query.PrimitivesGenerated[0])) {
-            dw1 |= SO_RENDERING_DISABLE;
-         } else {
-            perf_debug("Rasterizer discard with a GL_PRIMITIVES_GENERATED "
-                       "query active relies on the clipper.");
-         }
-      }
-
-      /* _NEW_LIGHT */
-      if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION)
-        dw1 |= SO_REORDER_TRAILING;
-
-      if (brw->gen < 8) {
-         for (i = 0; i < 4; i++) {
-            if (xfb_obj->Buffers[i]) {
-               dw1 |= SO_BUFFER_ENABLE(i);
-            }
-         }
-      }
-
-      /* We always read the whole vertex.  This could be reduced at some
-       * point by reading less and offsetting the register index in the
-       * SO_DECLs.
-       */
-      dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_0_VERTEX_READ_OFFSET);
-      dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_0_VERTEX_READ_LENGTH);
-
-      dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_1_VERTEX_READ_OFFSET);
-      dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_1_VERTEX_READ_LENGTH);
-
-      dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_2_VERTEX_READ_OFFSET);
-      dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_2_VERTEX_READ_LENGTH);
-
-      dw2 |= SET_FIELD(urb_entry_read_offset, SO_STREAM_3_VERTEX_READ_OFFSET);
-      dw2 |= SET_FIELD(urb_entry_read_length - 1, SO_STREAM_3_VERTEX_READ_LENGTH);
-
-      if (brw->gen >= 8) {
-        /* Set buffer pitches; 0 means unbound. */
-        if (xfb_obj->Buffers[0])
-           dw3 |= linked_xfb_info->Buffers[0].Stride * 4;
-        if (xfb_obj->Buffers[1])
-           dw3 |= (linked_xfb_info->Buffers[1].Stride * 4) << 16;
-        if (xfb_obj->Buffers[2])
-           dw4 |= linked_xfb_info->Buffers[2].Stride * 4;
-        if (xfb_obj->Buffers[3])
-           dw4 |= (linked_xfb_info->Buffers[3].Stride * 4) << 16;
-      }
-   }
-
-   const int dwords = brw->gen >= 8 ? 5 : 3;
-
-   BEGIN_BATCH(dwords);
-   OUT_BATCH(_3DSTATE_STREAMOUT << 16 | (dwords - 2));
-   OUT_BATCH(dw1);
-   OUT_BATCH(dw2);
-   if (dwords > 3) {
-      OUT_BATCH(dw3);
-      OUT_BATCH(dw4);
-   }
-   ADVANCE_BATCH();
-}
-
-static void
-upload_sol_state(struct brw_context *brw)
-{
-   struct gl_context *ctx = &brw->ctx;
-   /* BRW_NEW_TRANSFORM_FEEDBACK */
-   bool active = _mesa_is_xfb_active_and_unpaused(ctx);
-
-   if (active) {
-      if (brw->gen >= 8)
-         gen8_upload_3dstate_so_buffers(brw);
-      else
-         upload_3dstate_so_buffers(brw);
-
-      /* BRW_NEW_VUE_MAP_GEOM_OUT */
-      gen7_upload_3dstate_so_decl_list(brw, &brw->vue_map_geom_out);
-   }
-
-   /* Finally, set up the SOL stage.  This command must always follow updates to
-    * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
-    * MMIO register updates (current performed by the kernel at each batch
-    * emit).
-    */
-   upload_3dstate_streamout(brw, active, &brw->vue_map_geom_out);
-}
-
-const struct brw_tracked_state gen7_sol_state = {
-   .dirty = {
-      .mesa  = _NEW_LIGHT,
-      .brw   = BRW_NEW_BATCH |
-               BRW_NEW_BLORP |
-               BRW_NEW_RASTERIZER_DISCARD |
-               BRW_NEW_VUE_MAP_GEOM_OUT |
-               BRW_NEW_TRANSFORM_FEEDBACK,
-   },
-   .emit = upload_sol_state,
-};
-
 void
 gen7_begin_transform_feedback(struct gl_context *ctx, GLenum mode,
                               struct gl_transform_feedback_object *obj)