radeonsi: remove R600_CONTEXT_* flags
[mesa.git] / src / gallium / drivers / radeonsi / si_state_streamout.c
index 3410eb668fd3b557e7090a352f6242e7234c6ae9..3e83243e954913ecf0cfd03c8558ab7022383890 100644 (file)
@@ -1,12 +1,12 @@
 /*
- * Copyright 2012 Advanced Micro Devices, Inc.
+ * Copyright 2013 Advanced Micro Devices, Inc.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
- * on the rights to use, copy, modify, merge, publish, distribute, sub
- * license, and/or sell copies of the Software, and to permit persons to whom
- * the Software is furnished to do so, subject to the following conditions:
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
  *
  * The above copyright notice and this permission notice (including the next
  * paragraph) shall be included in all copies or substantial portions of the
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
- * USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * Authors:
- *      Christian König <christian.koenig@amd.com>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
  */
 
-#include "radeonsi_pipe.h"
+#include "si_pipe.h"
 #include "si_state.h"
+#include "sid.h"
+#include "radeon/r600_cs.h"
 
-/*
- * Stream out
- */
+#include "util/u_memory.h"
+
+static void si_set_streamout_enable(struct si_context *sctx, bool enable);
 
-#if 0
-void si_context_streamout_begin(struct r600_context *ctx)
+static inline void si_so_target_reference(struct si_streamout_target **dst,
+                                         struct pipe_stream_output_target *src)
 {
-       struct radeon_winsys_cs *cs = ctx->cs;
-       struct si_so_target **t = ctx->so_targets;
-       unsigned *strides = ctx->vs_shader_so_strides;
-       unsigned buffer_en, i;
-
-       buffer_en = (ctx->num_so_targets >= 1 && t[0] ? 1 : 0) |
-                   (ctx->num_so_targets >= 2 && t[1] ? 2 : 0) |
-                   (ctx->num_so_targets >= 3 && t[2] ? 4 : 0) |
-                   (ctx->num_so_targets >= 4 && t[3] ? 8 : 0);
-
-       ctx->num_cs_dw_streamout_end =
-               12 + /* flush_vgt_streamout */
-               util_bitcount(buffer_en) * 8 +
-               3;
-
-       si_need_cs_space(ctx,
-                          12 + /* flush_vgt_streamout */
-                          6 + /* enables */
-                          util_bitcount(buffer_en & ctx->streamout_append_bitmask) * 8 +
-                          util_bitcount(buffer_en & ~ctx->streamout_append_bitmask) * 6 +
-                          ctx->num_cs_dw_streamout_end, TRUE);
-
-       if (ctx->chip_class >= CAYMAN) {
-               evergreen_flush_vgt_streamout(ctx);
-               evergreen_set_streamout_enable(ctx, buffer_en);
+       pipe_so_target_reference((struct pipe_stream_output_target**)dst, src);
+}
+
+static struct pipe_stream_output_target *
+si_create_so_target(struct pipe_context *ctx,
+                   struct pipe_resource *buffer,
+                   unsigned buffer_offset,
+                   unsigned buffer_size)
+{
+       struct si_context *sctx = (struct si_context *)ctx;
+       struct si_streamout_target *t;
+       struct r600_resource *rbuffer = (struct r600_resource*)buffer;
+
+       t = CALLOC_STRUCT(si_streamout_target);
+       if (!t) {
+               return NULL;
        }
 
-       for (i = 0; i < ctx->num_so_targets; i++) {
-#if 0
-               if (t[i]) {
-                       t[i]->stride = strides[i];
-                       t[i]->so_index = i;
-
-                       cs->buf[cs->cdw++] = PKT3(PKT3_SET_CONTEXT_REG, 3, 0);
-                       cs->buf[cs->cdw++] = (R_028AD0_VGT_STRMOUT_BUFFER_SIZE_0 +
-                                                       16*i - SI_CONTEXT_REG_OFFSET) >> 2;
-                       cs->buf[cs->cdw++] = (t[i]->b.buffer_offset +
-                                                       t[i]->b.buffer_size) >> 2; /* BUFFER_SIZE (in DW) */
-                       cs->buf[cs->cdw++] = strides[i] >> 2;              /* VTX_STRIDE (in DW) */
-                       cs->buf[cs->cdw++] = 0;                    /* BUFFER_BASE */
-
-                       cs->buf[cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
-                       cs->buf[cs->cdw++] =
-                               si_context_bo_reloc(ctx, si_resource(t[i]->b.buffer),
-                                                     RADEON_USAGE_WRITE);
-
-                       if (ctx->streamout_append_bitmask & (1 << i)) {
-                               /* Append. */
-                               cs->buf[cs->cdw++] = PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0);
-                               cs->buf[cs->cdw++] = STRMOUT_SELECT_BUFFER(i) |
-                                                              STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_MEM); /* control */
-                               cs->buf[cs->cdw++] = 0; /* unused */
-                               cs->buf[cs->cdw++] = 0; /* unused */
-                               cs->buf[cs->cdw++] = 0; /* src address lo */
-                               cs->buf[cs->cdw++] = 0; /* src address hi */
-
-                               cs->buf[cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
-                               cs->buf[cs->cdw++] =
-                                       si_context_bo_reloc(ctx,  t[i]->filled_size,
-                                                             RADEON_USAGE_READ);
-                       } else {
-                               /* Start from the beginning. */
-                               cs->buf[cs->cdw++] = PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0);
-                               cs->buf[cs->cdw++] = STRMOUT_SELECT_BUFFER(i) |
-                                                              STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_PACKET); /* control */
-                               cs->buf[cs->cdw++] = 0; /* unused */
-                               cs->buf[cs->cdw++] = 0; /* unused */
-                               cs->buf[cs->cdw++] = t[i]->b.buffer_offset >> 2; /* buffer offset in DW */
-                               cs->buf[cs->cdw++] = 0; /* unused */
-                       }
-               }
-#endif
+       u_suballocator_alloc(sctx->b.allocator_zeroed_memory, 4, 4,
+                            &t->buf_filled_size_offset,
+                            (struct pipe_resource**)&t->buf_filled_size);
+       if (!t->buf_filled_size) {
+               FREE(t);
+               return NULL;
        }
+
+       t->b.reference.count = 1;
+       t->b.context = ctx;
+       pipe_resource_reference(&t->b.buffer, buffer);
+       t->b.buffer_offset = buffer_offset;
+       t->b.buffer_size = buffer_size;
+
+       util_range_add(&rbuffer->valid_buffer_range, buffer_offset,
+                      buffer_offset + buffer_size);
+       return &t->b;
 }
 
-void si_context_streamout_end(struct r600_context *ctx)
+static void si_so_target_destroy(struct pipe_context *ctx,
+                                struct pipe_stream_output_target *target)
 {
-       struct radeon_winsys_cs *cs = ctx->cs;
-       struct si_so_target **t = ctx->so_targets;
-       unsigned i, flush_flags = 0;
-
-       evergreen_flush_vgt_streamout(ctx);
-
-       for (i = 0; i < ctx->num_so_targets; i++) {
-#if 0
-               if (t[i]) {
-                       cs->buf[cs->cdw++] = PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0);
-                       cs->buf[cs->cdw++] = STRMOUT_SELECT_BUFFER(i) |
-                                                      STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_NONE) |
-                                                      STRMOUT_STORE_BUFFER_FILLED_SIZE; /* control */
-                       cs->buf[cs->cdw++] = 0; /* dst address lo */
-                       cs->buf[cs->cdw++] = 0; /* dst address hi */
-                       cs->buf[cs->cdw++] = 0; /* unused */
-                       cs->buf[cs->cdw++] = 0; /* unused */
-
-                       cs->buf[cs->cdw++] = PKT3(PKT3_NOP, 0, 0);
-                       cs->buf[cs->cdw++] =
-                               si_context_bo_reloc(ctx,  t[i]->filled_size,
-                                                     RADEON_USAGE_WRITE);
-
-                       flush_flags |= S_0085F0_SO0_DEST_BASE_ENA(1) << i;
-               }
-#endif
+       struct si_streamout_target *t = (struct si_streamout_target*)target;
+       pipe_resource_reference(&t->b.buffer, NULL);
+       r600_resource_reference(&t->buf_filled_size, NULL);
+       FREE(t);
+}
+
+void si_streamout_buffers_dirty(struct si_context *sctx)
+{
+       if (!sctx->streamout.enabled_mask)
+               return;
+
+       si_mark_atom_dirty(sctx, &sctx->streamout.begin_atom);
+       si_set_streamout_enable(sctx, true);
+}
+
+static void si_set_streamout_targets(struct pipe_context *ctx,
+                                    unsigned num_targets,
+                                    struct pipe_stream_output_target **targets,
+                                    const unsigned *offsets)
+{
+       struct si_context *sctx = (struct si_context *)ctx;
+       struct si_buffer_resources *buffers = &sctx->rw_buffers;
+       struct si_descriptors *descs = &sctx->descriptors[SI_DESCS_RW_BUFFERS];
+       unsigned old_num_targets = sctx->streamout.num_targets;
+       unsigned i, bufidx;
+
+       /* We are going to unbind the buffers. Mark which caches need to be flushed. */
+       if (sctx->streamout.num_targets && sctx->streamout.begin_emitted) {
+               /* Since streamout uses vector writes which go through TC L2
+                * and most other clients can use TC L2 as well, we don't need
+                * to flush it.
+                *
+                * The only cases which requires flushing it is VGT DMA index
+                * fetching (on <= CIK) and indirect draw data, which are rare
+                * cases. Thus, flag the TC L2 dirtiness in the resource and
+                * handle it at draw call time.
+                */
+               for (i = 0; i < sctx->streamout.num_targets; i++)
+                       if (sctx->streamout.targets[i])
+                               r600_resource(sctx->streamout.targets[i]->b.buffer)->TC_L2_dirty = true;
+
+               /* Invalidate the scalar cache in case a streamout buffer is
+                * going to be used as a constant buffer.
+                *
+                * Invalidate TC L1, because streamout bypasses it (done by
+                * setting GLC=1 in the store instruction), but it can contain
+                * outdated data of streamout buffers.
+                *
+                * VS_PARTIAL_FLUSH is required if the buffers are going to be
+                * used as an input immediately.
+                */
+               sctx->b.flags |= SI_CONTEXT_INV_SMEM_L1 |
+                                SI_CONTEXT_INV_VMEM_L1 |
+                                SI_CONTEXT_VS_PARTIAL_FLUSH;
        }
 
-       evergreen_set_streamout_enable(ctx, 0);
+       /* All readers of the streamout targets need to be finished before we can
+        * start writing to the targets.
+        */
+       if (num_targets)
+               sctx->b.flags |= SI_CONTEXT_PS_PARTIAL_FLUSH |
+                                SI_CONTEXT_CS_PARTIAL_FLUSH;
 
-       ctx->atom_surface_sync.flush_flags |= flush_flags;
-       si_atom_dirty(ctx, &ctx->atom_surface_sync.atom);
+       /* Streamout buffers must be bound in 2 places:
+        * 1) in VGT by setting the VGT_STRMOUT registers
+        * 2) as shader resources
+        */
 
-       ctx->num_cs_dw_streamout_end = 0;
+       /* Stop streamout. */
+       if (sctx->streamout.num_targets && sctx->streamout.begin_emitted)
+               si_emit_streamout_end(sctx);
 
-       /* XXX print some debug info */
-       for (i = 0; i < ctx->num_so_targets; i++) {
-               if (!t[i])
+       /* Set the new targets. */
+       unsigned enabled_mask = 0, append_bitmask = 0;
+       for (i = 0; i < num_targets; i++) {
+               si_so_target_reference(&sctx->streamout.targets[i], targets[i]);
+               if (!targets[i])
                        continue;
 
-               uint32_t *ptr = ctx->ws->buffer_map(t[i]->filled_size->cs_buf, ctx->cs, RADEON_USAGE_READ);
-               printf("FILLED_SIZE%i: %u\n", i, *ptr);
-               ctx->ws->buffer_unmap(t[i]->filled_size->cs_buf);
+               si_context_add_resource_size(ctx, targets[i]->buffer);
+               enabled_mask |= 1 << i;
+
+               if (offsets[i] == ((unsigned)-1))
+                       append_bitmask |= 1 << i;
+       }
+
+       for (; i < sctx->streamout.num_targets; i++)
+               si_so_target_reference(&sctx->streamout.targets[i], NULL);
+
+       sctx->streamout.enabled_mask = enabled_mask;
+       sctx->streamout.num_targets = num_targets;
+       sctx->streamout.append_bitmask = append_bitmask;
+
+       /* Update dirty state bits. */
+       if (num_targets) {
+               si_streamout_buffers_dirty(sctx);
+       } else {
+               si_set_atom_dirty(sctx, &sctx->streamout.begin_atom, false);
+               si_set_streamout_enable(sctx, false);
+       }
+
+       /* Set the shader resources.*/
+       for (i = 0; i < num_targets; i++) {
+               bufidx = SI_VS_STREAMOUT_BUF0 + i;
+
+               if (targets[i]) {
+                       struct pipe_resource *buffer = targets[i]->buffer;
+                       uint64_t va = r600_resource(buffer)->gpu_address;
+
+                       /* Set the descriptor.
+                        *
+                        * On VI, the format must be non-INVALID, otherwise
+                        * the buffer will be considered not bound and store
+                        * instructions will be no-ops.
+                        */
+                       uint32_t *desc = descs->list + bufidx*4;
+                       desc[0] = va;
+                       desc[1] = S_008F04_BASE_ADDRESS_HI(va >> 32);
+                       desc[2] = 0xffffffff;
+                       desc[3] = S_008F0C_DST_SEL_X(V_008F0C_SQ_SEL_X) |
+                                 S_008F0C_DST_SEL_Y(V_008F0C_SQ_SEL_Y) |
+                                 S_008F0C_DST_SEL_Z(V_008F0C_SQ_SEL_Z) |
+                                 S_008F0C_DST_SEL_W(V_008F0C_SQ_SEL_W) |
+                                 S_008F0C_DATA_FORMAT(V_008F0C_BUF_DATA_FORMAT_32);
+
+                       /* Set the resource. */
+                       pipe_resource_reference(&buffers->buffers[bufidx],
+                                               buffer);
+                       radeon_add_to_buffer_list_check_mem(&sctx->b, &sctx->b.gfx,
+                                                           (struct r600_resource*)buffer,
+                                                           buffers->shader_usage,
+                                                           RADEON_PRIO_SHADER_RW_BUFFER,
+                                                           true);
+                       r600_resource(buffer)->bind_history |= PIPE_BIND_STREAM_OUTPUT;
+
+                       buffers->enabled_mask |= 1u << bufidx;
+               } else {
+                       /* Clear the descriptor and unset the resource. */
+                       memset(descs->list + bufidx*4, 0,
+                              sizeof(uint32_t) * 4);
+                       pipe_resource_reference(&buffers->buffers[bufidx],
+                                               NULL);
+                       buffers->enabled_mask &= ~(1u << bufidx);
+               }
+       }
+       for (; i < old_num_targets; i++) {
+               bufidx = SI_VS_STREAMOUT_BUF0 + i;
+               /* Clear the descriptor and unset the resource. */
+               memset(descs->list + bufidx*4, 0, sizeof(uint32_t) * 4);
+               pipe_resource_reference(&buffers->buffers[bufidx], NULL);
+               buffers->enabled_mask &= ~(1u << bufidx);
        }
+
+       sctx->descriptors_dirty |= 1u << SI_DESCS_RW_BUFFERS;
 }
 
-void evergreen_flush_vgt_streamout(struct si_context *ctx)
+static void si_flush_vgt_streamout(struct si_context *sctx)
 {
-       struct radeon_winsys_cs *cs = ctx->cs;
-
-       cs->buf[cs->cdw++] = PKT3(PKT3_SET_CONFIG_REG, 1, 0);
-       cs->buf[cs->cdw++] = (R_0084FC_CP_STRMOUT_CNTL - SI_CONFIG_REG_OFFSET) >> 2;
-       cs->buf[cs->cdw++] = 0;
-
-       cs->buf[cs->cdw++] = PKT3(PKT3_EVENT_WRITE, 0, 0);
-       cs->buf[cs->cdw++] = EVENT_TYPE(EVENT_TYPE_SO_VGTSTREAMOUT_FLUSH) | EVENT_INDEX(0);
-
-       cs->buf[cs->cdw++] = PKT3(PKT3_WAIT_REG_MEM, 5, 0);
-       cs->buf[cs->cdw++] = WAIT_REG_MEM_EQUAL; /* wait until the register is equal to the reference value */
-       cs->buf[cs->cdw++] = R_0084FC_CP_STRMOUT_CNTL >> 2;  /* register */
-       cs->buf[cs->cdw++] = 0;
-       cs->buf[cs->cdw++] = S_0084FC_OFFSET_UPDATE_DONE(1); /* reference value */
-       cs->buf[cs->cdw++] = S_0084FC_OFFSET_UPDATE_DONE(1); /* mask */
-       cs->buf[cs->cdw++] = 4; /* poll interval */
+       struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
+       unsigned reg_strmout_cntl;
+
+       /* The register is at different places on different ASICs. */
+       if (sctx->b.chip_class >= CIK) {
+               reg_strmout_cntl = R_0300FC_CP_STRMOUT_CNTL;
+               radeon_set_uconfig_reg(cs, reg_strmout_cntl, 0);
+       } else {
+               reg_strmout_cntl = R_0084FC_CP_STRMOUT_CNTL;
+               radeon_set_config_reg(cs, reg_strmout_cntl, 0);
+       }
+
+       radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 0, 0));
+       radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_SO_VGTSTREAMOUT_FLUSH) | EVENT_INDEX(0));
+
+       radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
+       radeon_emit(cs, WAIT_REG_MEM_EQUAL); /* wait until the register is equal to the reference value */
+       radeon_emit(cs, reg_strmout_cntl >> 2);  /* register */
+       radeon_emit(cs, 0);
+       radeon_emit(cs, S_0084FC_OFFSET_UPDATE_DONE(1)); /* reference value */
+       radeon_emit(cs, S_0084FC_OFFSET_UPDATE_DONE(1)); /* mask */
+       radeon_emit(cs, 4); /* poll interval */
 }
 
-void evergreen_set_streamout_enable(struct si_context *ctx, unsigned buffer_enable_bit)
+static void si_emit_streamout_begin(struct r600_common_context *rctx, struct r600_atom *atom)
 {
-       struct radeon_winsys_cs *cs = ctx->cs;
+       struct si_context *sctx = (struct si_context*)rctx;
+       struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
+       struct si_streamout_target **t = sctx->streamout.targets;
+       uint16_t *stride_in_dw = sctx->streamout.stride_in_dw;
+       unsigned i;
 
-       if (buffer_enable_bit) {
-               cs->buf[cs->cdw++] = PKT3(PKT3_SET_CONTEXT_REG, 1, 0);
-               cs->buf[cs->cdw++] = (R_028B94_VGT_STRMOUT_CONFIG - SI_CONTEXT_REG_OFFSET) >> 2;
-               cs->buf[cs->cdw++] = S_028B94_STREAMOUT_0_EN(1);
+       si_flush_vgt_streamout(sctx);
 
-               cs->buf[cs->cdw++] = PKT3(PKT3_SET_CONTEXT_REG, 1, 0);
-               cs->buf[cs->cdw++] = (R_028B98_VGT_STRMOUT_BUFFER_CONFIG - SI_CONTEXT_REG_OFFSET) >> 2;
-               cs->buf[cs->cdw++] = S_028B98_STREAM_0_BUFFER_EN(buffer_enable_bit);
-       } else {
-               cs->buf[cs->cdw++] = PKT3(PKT3_SET_CONTEXT_REG, 1, 0);
-               cs->buf[cs->cdw++] = (R_028B94_VGT_STRMOUT_CONFIG - SI_CONTEXT_REG_OFFSET) >> 2;
-               cs->buf[cs->cdw++] = S_028B94_STREAMOUT_0_EN(0);
+       for (i = 0; i < sctx->streamout.num_targets; i++) {
+               if (!t[i])
+                       continue;
+
+               t[i]->stride_in_dw = stride_in_dw[i];
+
+               /* SI binds streamout buffers as shader resources.
+                * VGT only counts primitives and tells the shader
+                * through SGPRs what to do. */
+               radeon_set_context_reg_seq(cs, R_028AD0_VGT_STRMOUT_BUFFER_SIZE_0 + 16*i, 2);
+               radeon_emit(cs, (t[i]->b.buffer_offset +
+                                t[i]->b.buffer_size) >> 2);    /* BUFFER_SIZE (in DW) */
+               radeon_emit(cs, stride_in_dw[i]);               /* VTX_STRIDE (in DW) */
+
+               if (sctx->streamout.append_bitmask & (1 << i) && t[i]->buf_filled_size_valid) {
+                       uint64_t va = t[i]->buf_filled_size->gpu_address +
+                                     t[i]->buf_filled_size_offset;
+
+                       /* Append. */
+                       radeon_emit(cs, PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0));
+                       radeon_emit(cs, STRMOUT_SELECT_BUFFER(i) |
+                                   STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_MEM)); /* control */
+                       radeon_emit(cs, 0); /* unused */
+                       radeon_emit(cs, 0); /* unused */
+                       radeon_emit(cs, va); /* src address lo */
+                       radeon_emit(cs, va >> 32); /* src address hi */
+
+                       radeon_add_to_buffer_list(&sctx->b,  &sctx->b.gfx,
+                                                 t[i]->buf_filled_size,
+                                                 RADEON_USAGE_READ,
+                                                 RADEON_PRIO_SO_FILLED_SIZE);
+               } else {
+                       /* Start from the beginning. */
+                       radeon_emit(cs, PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0));
+                       radeon_emit(cs, STRMOUT_SELECT_BUFFER(i) |
+                                   STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_FROM_PACKET)); /* control */
+                       radeon_emit(cs, 0); /* unused */
+                       radeon_emit(cs, 0); /* unused */
+                       radeon_emit(cs, t[i]->b.buffer_offset >> 2); /* buffer offset in DW */
+                       radeon_emit(cs, 0); /* unused */
+               }
        }
-}
 
-#endif
+       sctx->streamout.begin_emitted = true;
+}
 
-struct pipe_stream_output_target *
-si_create_so_target(struct pipe_context *ctx,
-                   struct pipe_resource *buffer,
-                   unsigned buffer_offset,
-                   unsigned buffer_size)
+void si_emit_streamout_end(struct si_context *sctx)
 {
-#if 0
-       struct si_context *rctx = (struct r600_context *)ctx;
-       struct si_so_target *t;
-       void *ptr;
+       struct radeon_winsys_cs *cs = sctx->b.gfx.cs;
+       struct si_streamout_target **t = sctx->streamout.targets;
+       unsigned i;
+       uint64_t va;
 
-       t = CALLOC_STRUCT(si_so_target);
-       if (!t) {
-               return NULL;
+       si_flush_vgt_streamout(sctx);
+
+       for (i = 0; i < sctx->streamout.num_targets; i++) {
+               if (!t[i])
+                       continue;
+
+               va = t[i]->buf_filled_size->gpu_address + t[i]->buf_filled_size_offset;
+               radeon_emit(cs, PKT3(PKT3_STRMOUT_BUFFER_UPDATE, 4, 0));
+               radeon_emit(cs, STRMOUT_SELECT_BUFFER(i) |
+                           STRMOUT_OFFSET_SOURCE(STRMOUT_OFFSET_NONE) |
+                           STRMOUT_STORE_BUFFER_FILLED_SIZE); /* control */
+               radeon_emit(cs, va);     /* dst address lo */
+               radeon_emit(cs, va >> 32); /* dst address hi */
+               radeon_emit(cs, 0); /* unused */
+               radeon_emit(cs, 0); /* unused */
+
+               radeon_add_to_buffer_list(&sctx->b,  &sctx->b.gfx,
+                                         t[i]->buf_filled_size,
+                                         RADEON_USAGE_WRITE,
+                                         RADEON_PRIO_SO_FILLED_SIZE);
+
+               /* Zero the buffer size. The counters (primitives generated,
+                * primitives emitted) may be enabled even if there is not
+                * buffer bound. This ensures that the primitives-emitted query
+                * won't increment. */
+               radeon_set_context_reg(cs, R_028AD0_VGT_STRMOUT_BUFFER_SIZE_0 + 16*i, 0);
+
+               t[i]->buf_filled_size_valid = true;
        }
 
-       t->b.reference.count = 1;
-       t->b.context = ctx;
-       pipe_resource_reference(&t->b.buffer, buffer);
-       t->b.buffer_offset = buffer_offset;
-       t->b.buffer_size = buffer_size;
+       sctx->streamout.begin_emitted = false;
+}
 
-       t->filled_size = si_resource_create_custom(ctx->screen, PIPE_USAGE_STATIC, 4);
-       ptr = rctx->ws->buffer_map(t->filled_size->cs_buf, rctx->cs, PIPE_TRANSFER_WRITE);
-       memset(ptr, 0, t->filled_size->buf->size);
-       rctx->ws->buffer_unmap(t->filled_size->cs_buf);
+/* STREAMOUT CONFIG DERIVED STATE
+ *
+ * Streamout must be enabled for the PRIMITIVES_GENERATED query to work.
+ * The buffer mask is an independent state, so no writes occur if there
+ * are no buffers bound.
+ */
 
-       return &t->b;
-#endif
-       return NULL;
+static void si_emit_streamout_enable(struct r600_common_context *rctx,
+                                    struct r600_atom *atom)
+{
+       struct si_context *sctx = (struct si_context*)rctx;
+
+       radeon_set_context_reg_seq(sctx->b.gfx.cs, R_028B94_VGT_STRMOUT_CONFIG, 2);
+       radeon_emit(sctx->b.gfx.cs,
+                   S_028B94_STREAMOUT_0_EN(si_get_strmout_en(sctx)) |
+                   S_028B94_RAST_STREAM(0) |
+                   S_028B94_STREAMOUT_1_EN(si_get_strmout_en(sctx)) |
+                   S_028B94_STREAMOUT_2_EN(si_get_strmout_en(sctx)) |
+                   S_028B94_STREAMOUT_3_EN(si_get_strmout_en(sctx)));
+       radeon_emit(sctx->b.gfx.cs,
+                   sctx->streamout.hw_enabled_mask &
+                   sctx->streamout.enabled_stream_buffers_mask);
 }
 
-void si_so_target_destroy(struct pipe_context *ctx,
-                         struct pipe_stream_output_target *target)
+static void si_set_streamout_enable(struct si_context *sctx, bool enable)
 {
-#if 0
-       struct si_so_target *t = (struct r600_so_target*)target;
-       pipe_resource_reference(&t->b.buffer, NULL);
-       si_resource_reference(&t->filled_size, NULL);
-       FREE(t);
-#endif
+       bool old_strmout_en = si_get_strmout_en(sctx);
+       unsigned old_hw_enabled_mask = sctx->streamout.hw_enabled_mask;
+
+       sctx->streamout.streamout_enabled = enable;
+
+       sctx->streamout.hw_enabled_mask = sctx->streamout.enabled_mask |
+                                         (sctx->streamout.enabled_mask << 4) |
+                                         (sctx->streamout.enabled_mask << 8) |
+                                         (sctx->streamout.enabled_mask << 12);
+
+       if ((old_strmout_en != si_get_strmout_en(sctx)) ||
+            (old_hw_enabled_mask != sctx->streamout.hw_enabled_mask))
+               si_mark_atom_dirty(sctx, &sctx->streamout.enable_atom);
 }
 
-void si_set_so_targets(struct pipe_context *ctx,
-                      unsigned num_targets,
-                      struct pipe_stream_output_target **targets,
-                      unsigned append_bitmask)
+void si_update_prims_generated_query_state(struct si_context *sctx,
+                                          unsigned type, int diff)
 {
-       assert(num_targets == 0);
-#if 0
-       struct si_context *rctx = (struct r600_context *)ctx;
-       unsigned i;
+       if (type == PIPE_QUERY_PRIMITIVES_GENERATED) {
+               bool old_strmout_en = si_get_strmout_en(sctx);
 
-       /* Stop streamout. */
-       if (rctx->num_so_targets) {
-               si_context_streamout_end(rctx);
-       }
+               sctx->streamout.num_prims_gen_queries += diff;
+               assert(sctx->streamout.num_prims_gen_queries >= 0);
 
-       /* Set the new targets. */
-       for (i = 0; i < num_targets; i++) {
-               pipe_so_target_reference((struct pipe_stream_output_target**)&rctx->so_targets[i], targets[i]);
-       }
-       for (; i < rctx->num_so_targets; i++) {
-               pipe_so_target_reference((struct pipe_stream_output_target**)&rctx->so_targets[i], NULL);
+               sctx->streamout.prims_gen_query_enabled =
+                       sctx->streamout.num_prims_gen_queries != 0;
+
+               if (old_strmout_en != si_get_strmout_en(sctx))
+                       si_mark_atom_dirty(sctx, &sctx->streamout.enable_atom);
        }
+}
 
-       rctx->num_so_targets = num_targets;
-       rctx->streamout_start = num_targets != 0;
-       rctx->streamout_append_bitmask = append_bitmask;
-#endif
+void si_init_streamout_functions(struct si_context *sctx)
+{
+       sctx->b.b.create_stream_output_target = si_create_so_target;
+       sctx->b.b.stream_output_target_destroy = si_so_target_destroy;
+       sctx->b.b.set_stream_output_targets = si_set_streamout_targets;
+       sctx->streamout.begin_atom.emit = si_emit_streamout_begin;
+       sctx->streamout.enable_atom.emit = si_emit_streamout_enable;
 }