+ return count;
+}
+
+/**
+ * Emit any extra vertex shader constants into the buffer pointed
+ * to by 'dest'.
+ * In particular, these would be the scale and bias factors computed
+ * from the framebuffer size which are used to copy with differences in
+ * GL vs D3D coordinate spaces. See svga_tgsi_insn.c for more info.
+ * \return number of float[4] constants put into the dest buffer
+ */
+static unsigned
+svga_get_extra_vs_constants(struct svga_context *svga, float *dest)
+{
+ const struct svga_shader_variant *variant = svga->state.hw_draw.vs;
+ unsigned count = 0;
+
+ /* SVGA_NEW_VS_VARIANT
+ */
+ if (variant->key.vs.need_prescale) {
+ count += svga_get_prescale_constants(svga, &dest);
+ }
+
+ if (variant->key.vs.undo_viewport) {
+ /* Used to convert window coords back to NDC coords */
+ dest[0] = 1.0f / svga->curr.viewport.scale[0];
+ dest[1] = 1.0f / svga->curr.viewport.scale[1];
+ dest[2] = -svga->curr.viewport.translate[0];
+ dest[3] = -svga->curr.viewport.translate[1];
+ dest += 4;
+ count += 1;
+ }
+
+ /* SVGA_NEW_CLIP */
+ count += svga_get_clip_plane_constants(svga, variant, &dest);
+
+ /* common constants */
+ count += svga_get_extra_constants_common(svga, variant,
+ PIPE_SHADER_VERTEX, dest);
+
+ assert(count <= MAX_EXTRA_CONSTS);
+
+ return count;
+}
+
+/**
+ * Emit any extra geometry shader constants into the buffer pointed
+ * to by 'dest'.
+ */
+static unsigned
+svga_get_extra_gs_constants(struct svga_context *svga, float *dest)
+{
+ const struct svga_shader_variant *variant = svga->state.hw_draw.gs;
+ unsigned count = 0;
+
+ /* SVGA_NEW_GS_VARIANT
+ */
+
+ /* Constants for point sprite
+ * These are used in the transformed gs that supports point sprite.
+ * They need to be added before the prescale constants.
+ */
+ if (variant->key.gs.wide_point) {
+ count += svga_get_pt_sprite_constants(svga, &dest);
+ }
+
+ if (variant->key.gs.need_prescale) {
+ count += svga_get_prescale_constants(svga, &dest);
+ }
+
+ /* SVGA_NEW_CLIP */
+ count += svga_get_clip_plane_constants(svga, variant, &dest);
+
+ /* common constants */
+ count += svga_get_extra_constants_common(svga, variant,
+ PIPE_SHADER_GEOMETRY, dest);
+
+ assert(count <= MAX_EXTRA_CONSTS);
+ return count;
+}
+
+
+/*
+ * Check and emit a range of shader constant registers, trying to coalesce
+ * successive shader constant updates in a single command in order to save
+ * space on the command buffer. This is a HWv8 feature.
+ */
+static enum pipe_error
+emit_const_range(struct svga_context *svga,
+ enum pipe_shader_type shader,
+ unsigned offset,
+ unsigned count,
+ const float (*values)[4])
+{
+ unsigned i, j;
+ enum pipe_error ret;
+
+ assert(shader == PIPE_SHADER_VERTEX ||
+ shader == PIPE_SHADER_FRAGMENT);
+ assert(!svga_have_vgpu10(svga));
+
+#ifdef DEBUG
+ if (offset + count > SVGA3D_CONSTREG_MAX) {
+ debug_printf("svga: too many constants (offset %u + count %u = %u (max = %u))\n",
+ offset, count, offset + count, SVGA3D_CONSTREG_MAX);
+ }
+#endif
+
+ if (offset > SVGA3D_CONSTREG_MAX) {
+ /* This isn't OK, but if we propagate an error all the way up we'll
+ * just get into more trouble.
+ * XXX note that offset is always zero at this time so this is moot.
+ */
+ return PIPE_OK;
+ }
+
+ if (offset + count > SVGA3D_CONSTREG_MAX) {
+ /* Just drop the extra constants for now.
+ * Ideally we should not have allowed the app to create a shader
+ * that exceeds our constant buffer size but there's no way to
+ * express that in gallium at this time.
+ */
+ count = SVGA3D_CONSTREG_MAX - offset;
+ }
+
+ i = 0;
+ while (i < count) {
+ if (memcmp(svga->state.hw_draw.cb[shader][offset + i],
+ values[i],
+ 4 * sizeof(float)) != 0) {
+ /* Found one dirty constant
+ */
+ if (SVGA_DEBUG & DEBUG_CONSTS)
+ debug_printf("%s %s %d: %f %f %f %f\n",
+ __FUNCTION__,
+ shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
+ offset + i,
+ values[i][0],
+ values[i][1],
+ values[i][2],
+ values[i][3]);
+
+ /* Look for more consecutive dirty constants.
+ */
+ j = i + 1;
+ while (j < count &&
+ j < i + MAX_CONST_REG_COUNT &&
+ memcmp(svga->state.hw_draw.cb[shader][offset + j],
+ values[j],
+ 4 * sizeof(float)) != 0) {
+
+ if (SVGA_DEBUG & DEBUG_CONSTS)
+ debug_printf("%s %s %d: %f %f %f %f\n",
+ __FUNCTION__,
+ shader == PIPE_SHADER_VERTEX ? "VERT" : "FRAG",
+ offset + j,
+ values[j][0],
+ values[j][1],
+ values[j][2],
+ values[j][3]);
+
+ ++j;
+ }
+
+ assert(j >= i + 1);
+
+ /* Send them all together.
+ */
+ if (svga_have_gb_objects(svga)) {
+ ret = SVGA3D_SetGBShaderConstsInline(svga->swc,
+ offset + i, /* start */
+ j - i, /* count */
+ svga_shader_type(shader),
+ SVGA3D_CONST_TYPE_FLOAT,
+ values + i);
+ }
+ else {
+ ret = SVGA3D_SetShaderConsts(svga->swc,
+ offset + i, j - i,
+ svga_shader_type(shader),
+ SVGA3D_CONST_TYPE_FLOAT,
+ values + i);
+ }
+ if (ret != PIPE_OK) {
+ return ret;
+ }
+
+ /*
+ * Local copy of the hardware state.
+ */
+ memcpy(svga->state.hw_draw.cb[shader][offset + i],
+ values[i],
+ (j - i) * 4 * sizeof(float));
+
+ i = j + 1;
+
+ svga->hud.num_const_updates++;
+
+ } else {
+ ++i;
+ }
+ }
+
+ return PIPE_OK;