+
+/*
+ * 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,
+ unsigned shader,
+ unsigned offset,
+ unsigned count,
+ const float (*values)[4])
+{
+ unsigned i, j;
+ enum pipe_error ret;
+
+#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;
+ } else {
+ ++i;
+ }
+ }
+
+ return PIPE_OK;
+}
+
+
+/**
+ * Emit all the constants in a constant buffer for a shader stage.
+ */
+static enum pipe_error
+emit_consts(struct svga_context *svga, unsigned shader)