gallium/swr: Fix compilation warnings
[mesa.git] / src / gallium / drivers / swr / swr_state.cpp
index 12da99fc48065d08a17f52d626aef590d9649ed9..01758f5f9101a069bfa6273fc8083a47553d69e5 100644 (file)
  * IN THE SOFTWARE.
  ***************************************************************************/
 
+#include <llvm/Config/llvm-config.h>
+
+#if LLVM_VERSION_MAJOR < 7
 // llvm redefines DEBUG
 #pragma push_macro("DEBUG")
 #undef DEBUG
+#endif
+
+#include <rasterizer/core/state.h>
 #include "JitManager.h"
+
+#if LLVM_VERSION_MAJOR < 7
 #pragma pop_macro("DEBUG")
+#endif
 
 #include "common/os.h"
 #include "jit_api.h"
@@ -34,7 +43,7 @@
 #include "core/state_funcs.h"
 
 #include "gallivm/lp_bld_tgsi.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 
 #include "util/u_memory.h"
 #include "util/u_inlines.h"
@@ -45,6 +54,7 @@
 
 #include "swr_state.h"
 #include "swr_context.h"
+#include "gen_surf_state_llvm.h"
 #include "gen_swr_context_llvm.h"
 #include "swr_screen.h"
 #include "swr_resource.h"
@@ -61,6 +71,7 @@ swr_create_blend_state(struct pipe_context *pipe,
                        const struct pipe_blend_state *blend)
 {
    struct swr_blend_state *state = CALLOC_STRUCT(swr_blend_state);
+   assert(state != nullptr);
 
    memcpy(&state->pipe, blend, sizeof(*blend));
 
@@ -302,11 +313,6 @@ swr_set_sampler_views(struct pipe_context *pipe,
    /* set the new sampler views */
    ctx->num_sampler_views[shader] = num;
    for (i = 0; i < num; i++) {
-      /* Note: we're using pipe_sampler_view_release() here to work around
-       * a possible crash when the old view belongs to another context that
-       * was already destroyed.
-       */
-      pipe_sampler_view_release(pipe, &ctx->sampler_views[shader][start + i]);
       pipe_sampler_view_reference(&ctx->sampler_views[shader][start + i],
                                   views[i]);
    }
@@ -345,8 +351,10 @@ swr_create_vs_state(struct pipe_context *pipe,
       // soState.streamToRasterizer not used
 
       for (uint32_t i = 0; i < stream_output->num_outputs; i++) {
+         unsigned attrib_slot = stream_output->output[i].register_index;
+         attrib_slot = swr_so_adjust_attrib(attrib_slot, swr_vs);
          swr_vs->soState.streamMasks[stream_output->output[i].stream] |=
-            1 << (stream_output->output[i].register_index - 1);
+            (1 << attrib_slot);
       }
       for (uint32_t i = 0; i < MAX_SO_STREAMS; i++) {
         swr_vs->soState.streamNumEntries[i] =
@@ -428,13 +436,10 @@ swr_create_gs_state(struct pipe_context *pipe,
       return NULL;
 
    swr_gs->pipe.tokens = tgsi_dup_tokens(gs->tokens);
-
    lp_build_tgsi_info(gs->tokens, &swr_gs->info);
-
    return swr_gs;
 }
 
-
 static void
 swr_bind_gs_state(struct pipe_context *pipe, void *gs)
 {
@@ -458,6 +463,86 @@ swr_delete_gs_state(struct pipe_context *pipe, void *gs)
    swr_fence_work_delete_gs(screen->flush_fence, swr_gs);
 }
 
+static void *
+swr_create_tcs_state(struct pipe_context *pipe,
+                     const struct pipe_shader_state *tcs)
+{
+   struct swr_tess_control_shader *swr_tcs = new swr_tess_control_shader;
+   if (!swr_tcs)
+      return NULL;
+
+   swr_tcs->pipe.tokens = tgsi_dup_tokens(tcs->tokens);
+   lp_build_tgsi_info(tcs->tokens, &swr_tcs->info);
+   return swr_tcs;
+}
+
+static void
+swr_bind_tcs_state(struct pipe_context *pipe, void *tcs)
+{
+   struct swr_context *ctx = swr_context(pipe);
+
+   if (ctx->tcs == tcs)
+      return;
+
+   ctx->tcs = (swr_tess_control_shader *)tcs;
+   ctx->dirty |= SWR_NEW_TCS;
+   ctx->dirty |= SWR_NEW_TS;
+}
+
+static void
+swr_delete_tcs_state(struct pipe_context *pipe, void *tcs)
+{
+   struct swr_tess_control_shader *swr_tcs = (swr_tess_control_shader *)tcs;
+   FREE((void *)swr_tcs->pipe.tokens);
+   struct swr_screen *screen = swr_screen(pipe->screen);
+
+   /* Defer deleton of tcs state */
+   swr_fence_work_delete_tcs(screen->flush_fence, swr_tcs);
+}
+
+static void *
+swr_create_tes_state(struct pipe_context *pipe,
+                     const struct pipe_shader_state *tes)
+{
+   struct swr_tess_evaluation_shader *swr_tes = new swr_tess_evaluation_shader;
+   if (!swr_tes)
+      return NULL;
+
+   swr_tes->pipe.tokens = tgsi_dup_tokens(tes->tokens);
+   lp_build_tgsi_info(tes->tokens, &swr_tes->info);
+   return swr_tes;
+}
+
+static void
+swr_bind_tes_state(struct pipe_context *pipe, void *tes)
+{
+   struct swr_context *ctx = swr_context(pipe);
+
+   if (ctx->tes == tes)
+      return;
+
+   // Save current tessellator state first
+   if (ctx->tes != nullptr) {
+      ctx->tes->ts_state = ctx->tsState;
+   }
+
+   ctx->tes = (swr_tess_evaluation_shader *)tes;
+
+   ctx->dirty |= SWR_NEW_TES;
+   ctx->dirty |= SWR_NEW_TS;
+}
+
+static void
+swr_delete_tes_state(struct pipe_context *pipe, void *tes)
+{
+   struct swr_tess_evaluation_shader *swr_tes = (swr_tess_evaluation_shader *)tes;
+   FREE((void *)swr_tes->pipe.tokens);
+   struct swr_screen *screen = swr_screen(pipe->screen);
+
+   /* Defer deleton of tes state */
+   swr_fence_work_delete_tes(screen->flush_fence, swr_tes);
+}
+
 static void
 swr_set_constant_buffer(struct pipe_context *pipe,
                         enum pipe_shader_type shader,
@@ -479,8 +564,11 @@ swr_set_constant_buffer(struct pipe_context *pipe,
       ctx->dirty |= SWR_NEW_FSCONSTANTS;
    } else if (shader == PIPE_SHADER_GEOMETRY) {
       ctx->dirty |= SWR_NEW_GSCONSTANTS;
+   } else if (shader == PIPE_SHADER_TESS_CTRL) {
+      ctx->dirty |= SWR_NEW_TCSCONSTANTS;
+   } else if (shader == PIPE_SHADER_TESS_EVAL) {
+      ctx->dirty |= SWR_NEW_TESCONSTANTS;
    }
-
    if (cb && cb->user_buffer) {
       pipe_resource_reference(&constants, NULL);
    }
@@ -496,7 +584,7 @@ swr_create_vertex_elements_state(struct pipe_context *pipe,
    assert(num_elements <= PIPE_MAX_ATTRIBS);
    velems = new swr_vertex_element_state;
    if (velems) {
-      memset(&velems->fsState, 0, sizeof(velems->fsState));
+      memset((void*)&velems->fsState, 0, sizeof(velems->fsState));
       velems->fsState.bVertexIDOffsetEnable = true;
       velems->fsState.numAttribs = num_elements;
       for (unsigned i = 0; i < num_elements; i++) {
@@ -529,7 +617,7 @@ swr_create_vertex_elements_state(struct pipe_context *pipe,
             ? ComponentControl::StoreSrc
             : ComponentControl::Store1Fp;
          velems->fsState.layout[i].ComponentPacking = ComponentEnable::XYZW;
-         velems->fsState.layout[i].InstanceDataStepRate =
+         velems->fsState.layout[i].InstanceAdvancementState =
             attribs[i].instance_divisor;
 
          /* Calculate the pitch of each stream */
@@ -618,16 +706,21 @@ swr_set_clip_state(struct pipe_context *pipe,
 static void
 swr_set_scissor_states(struct pipe_context *pipe,
                        unsigned start_slot,
-                       unsigned num_viewports,
-                       const struct pipe_scissor_state *scissor)
+                       unsigned num_scissors,
+                       const struct pipe_scissor_state *scissors)
 {
    struct swr_context *ctx = swr_context(pipe);
 
-   ctx->scissor = *scissor;
-   ctx->swr_scissor.xmin = scissor->minx;
-   ctx->swr_scissor.xmax = scissor->maxx;
-   ctx->swr_scissor.ymin = scissor->miny;
-   ctx->swr_scissor.ymax = scissor->maxy;
+   memcpy(ctx->scissors + start_slot, scissors,
+          sizeof(struct pipe_scissor_state) * num_scissors);
+
+   for (unsigned i = 0; i < num_scissors; i++) {
+      auto idx = start_slot + i;
+      ctx->swr_scissors[idx].xmin = scissors[idx].minx;
+      ctx->swr_scissors[idx].xmax = scissors[idx].maxx;
+      ctx->swr_scissors[idx].ymin = scissors[idx].miny;
+      ctx->swr_scissors[idx].ymax = scissors[idx].maxy;
+   }
    ctx->dirty |= SWR_NEW_SCISSOR;
 }
 
@@ -639,7 +732,7 @@ swr_set_viewport_states(struct pipe_context *pipe,
 {
    struct swr_context *ctx = swr_context(pipe);
 
-   ctx->viewport = *vpt;
+   memcpy(ctx->viewports + start_slot, vpt, sizeof(struct pipe_viewport_state) * num_viewports);
    ctx->dirty |= SWR_NEW_VIEWPORT;
 }
 
@@ -650,7 +743,7 @@ swr_set_framebuffer_state(struct pipe_context *pipe,
 {
    struct swr_context *ctx = swr_context(pipe);
 
-   boolean changed = !util_framebuffer_state_equal(&ctx->framebuffer, fb);
+   bool changed = !util_framebuffer_state_equal(&ctx->framebuffer, fb);
 
    assert(fb->width <= KNOB_GUARDBAND_WIDTH);
    assert(fb->height <= KNOB_GUARDBAND_HEIGHT);
@@ -732,7 +825,7 @@ swr_update_resource_status(struct pipe_context *pipe,
    /* VBO vertex buffers */
    for (uint32_t i = 0; i < ctx->num_vertex_buffers; i++) {
       struct pipe_vertex_buffer *vb = &ctx->vertex_buffer[i];
-      if (!vb->is_user_buffer)
+      if (!vb->is_user_buffer && vb->buffer.resource)
          swr_resource_read(vb->buffer.resource);
    }
 
@@ -793,7 +886,9 @@ swr_update_texture_state(struct swr_context *ctx,
 
          jit_tex->width = res->width0;
          jit_tex->height = res->height0;
-         jit_tex->base_ptr = swr->pBaseAddress;
+         jit_tex->base_ptr = (uint8_t*)swr->xpBaseAddress;
+         jit_tex->num_samples = swr->numSamples;
+         jit_tex->sample_stride = 0;
          if (view->target != PIPE_BUFFER) {
             jit_tex->first_level = view->u.tex.first_level;
             jit_tex->last_level = view->u.tex.last_level;
@@ -866,8 +961,18 @@ swr_update_constants(struct swr_context *ctx, enum pipe_shader_type shaderType)
       num_constants = pDC->num_constantsGS;
       scratch = &ctx->scratch->gs_constants;
       break;
+   case PIPE_SHADER_TESS_CTRL:
+      constant = pDC->constantTCS;
+      num_constants = pDC->num_constantsTCS;
+      scratch = &ctx->scratch->tcs_constants;
+      break;
+   case PIPE_SHADER_TESS_EVAL:
+      constant = pDC->constantTES;
+      num_constants = pDC->num_constantsTES;
+      scratch = &ctx->scratch->tes_constants;
+      break;
    default:
-      debug_printf("Unsupported shader type constants\n");
+      assert(0 && "Unsupported shader type constants");
       return;
    }
 
@@ -900,7 +1005,7 @@ swr_change_rt(struct swr_context *ctx,
    struct SWR_SURFACE_STATE *rt = &pDC->renderTargets[attachment];
 
    /* Do nothing if the render target hasn't changed */
-   if ((!sf || !sf->texture) && rt->pBaseAddress == nullptr)
+   if ((!sf || !sf->texture) && (void*)(rt->xpBaseAddress) == nullptr)
       return false;
 
    /* Deal with disabling RT up front */
@@ -916,12 +1021,12 @@ swr_change_rt(struct swr_context *ctx,
    const SWR_SURFACE_STATE *swr_surface = &swr->swr;
    SWR_FORMAT fmt = mesa_to_swr_format(sf->format);
 
-   if (attachment == SWR_ATTACHMENT_STENCIL && swr->secondary.pBaseAddress) {
+   if (attachment == SWR_ATTACHMENT_STENCIL && swr->secondary.xpBaseAddress) {
       swr_surface = &swr->secondary;
       fmt = swr_surface->format;
    }
 
-   if (rt->pBaseAddress == swr_surface->pBaseAddress &&
+   if (rt->xpBaseAddress == swr_surface->xpBaseAddress &&
        rt->format == fmt &&
        rt->lod == sf->u.tex.level &&
        rt->arrayIndex == sf->u.tex.first_layer)
@@ -930,11 +1035,16 @@ swr_change_rt(struct swr_context *ctx,
    bool need_fence = false;
 
    /* StoreTile for changed target */
-   if (rt->pBaseAddress) {
+   if (rt->xpBaseAddress) {
       /* If changing attachment to a new target, mark tiles as
        * INVALID so they are reloaded from surface. */
       swr_store_render_target(&ctx->pipe, attachment, SWR_TILE_INVALID);
       need_fence = true;
+   } else {
+      /* if no previous attachment, invalidate tiles that may be marked
+       * RESOLVED because of an old attachment */
+      swr_invalidate_render_target(&ctx->pipe, attachment, sf->width, sf->height);
+      /* no need to set fence here */
    }
 
    /* Make new attachment */
@@ -946,6 +1056,47 @@ swr_change_rt(struct swr_context *ctx,
    return need_fence;
 }
 
+/*
+ * for cases where resources are shared between contexts, invalidate
+ * this ctx's resource. so it can be fetched fresh.  Old ctx's resource
+ * is already stored during a flush
+ */
+static inline void
+swr_invalidate_buffers_after_ctx_change(struct pipe_context *pipe)
+{
+   struct swr_context *ctx = swr_context(pipe);
+
+   for (uint32_t i = 0; i < ctx->framebuffer.nr_cbufs; i++) {
+      struct pipe_surface *cb = ctx->framebuffer.cbufs[i];
+      if (cb) {
+         struct swr_resource *res = swr_resource(cb->texture);
+         if (res->curr_pipe != pipe) {
+            /* if curr_pipe is NULL (first use), status should not be WRITE */
+            assert(res->curr_pipe || !(res->status & SWR_RESOURCE_WRITE));
+            if (res->status & SWR_RESOURCE_WRITE) {
+               swr_invalidate_render_target(pipe, i, cb->width, cb->height);
+            }
+         }
+         res->curr_pipe = pipe;
+      }
+   }
+   if (ctx->framebuffer.zsbuf) {
+      struct pipe_surface *zb = ctx->framebuffer.zsbuf;
+      if (zb) {
+         struct swr_resource *res = swr_resource(zb->texture);
+         if (res->curr_pipe != pipe) {
+            /* if curr_pipe is NULL (first use), status should not be WRITE */
+            assert(res->curr_pipe || !(res->status & SWR_RESOURCE_WRITE));
+            if (res->status & SWR_RESOURCE_WRITE) {
+               swr_invalidate_render_target(pipe, SWR_ATTACHMENT_DEPTH, zb->width, zb->height);
+               swr_invalidate_render_target(pipe, SWR_ATTACHMENT_STENCIL, zb->width, zb->height);
+            }
+         }
+         res->curr_pipe = pipe;
+      }
+   }
+}
+
 static inline void
 swr_user_vbuf_range(const struct pipe_draw_info *info,
                     const struct swr_vertex_element_state *velems,
@@ -957,16 +1108,17 @@ swr_user_vbuf_range(const struct pipe_draw_info *info,
 {
    /* FIXME: The size is too large - we don't access the full extra stride. */
    unsigned elems;
+   unsigned elem_pitch = vb->stride + velems->stream_pitch[i];
    if (velems->instanced_bufs & (1U << i)) {
       elems = info->instance_count / velems->min_instance_div[i] + 1;
       *totelems = info->start_instance + elems;
       *base = info->start_instance * vb->stride;
-      *size = elems * vb->stride;
+      *size = elems * elem_pitch;
    } else if (vb->stride) {
       elems = info->max_index - info->min_index + 1;
-      *totelems = info->max_index + 1;
-      *base = info->min_index * vb->stride;
-      *size = elems * vb->stride;
+      *totelems = (info->max_index + info->index_bias) + 1;
+      *base = (info->min_index + info->index_bias) * vb->stride;
+      *size = elems * elem_pitch;
    } else {
       *totelems = 1;
       *base = 0;
@@ -985,6 +1137,25 @@ swr_update_poly_stipple(struct swr_context *ctx)
           sizeof(ctx->poly_stipple.pipe.stipple));
 }
 
+
+static struct tgsi_shader_info *
+swr_get_last_fe(const struct swr_context *ctx)
+{
+   tgsi_shader_info *pLastFE = &ctx->vs->info.base;
+
+   if (ctx->gs) {
+      pLastFE = &ctx->gs->info.base;
+   }
+   else if (ctx->tes) {
+      pLastFE = &ctx->tes->info.base;
+   }
+   else if (ctx->tcs) {
+      pLastFE = &ctx->tcs->info.base;
+   }
+   return pLastFE;
+}
+
+
 void
 swr_update_derived(struct pipe_context *pipe,
                    const struct pipe_draw_info *p_draw_info)
@@ -1026,13 +1197,15 @@ swr_update_derived(struct pipe_context *pipe,
    }
 
    /* Update screen->pipe to current pipe context. */
-   if (screen->pipe != pipe)
-      screen->pipe = pipe;
+   screen->pipe = pipe;
 
    /* Any state that requires dirty flags to be re-triggered sets this mask */
    /* For example, user_buffer vertex and index buffers. */
    unsigned post_update_dirty_flags = 0;
 
+   /* bring resources that changed context up-to-date */
+   swr_invalidate_buffers_after_ctx_change(pipe);
+
    /* Render Targets */
    if (ctx->dirty & SWR_NEW_FRAMEBUFFER) {
       struct pipe_framebuffer_state *fb = &ctx->framebuffer;
@@ -1070,6 +1243,8 @@ swr_update_derived(struct pipe_context *pipe,
    /* Raster state */
    if (ctx->dirty & (SWR_NEW_RASTERIZER |
                      SWR_NEW_VS | // clipping
+                     SWR_NEW_TES |
+                     SWR_NEW_TCS |
                      SWR_NEW_FRAMEBUFFER)) {
       pipe_rasterizer_state *rasterizer = ctx->rasterizer;
       pipe_framebuffer_state *fb = &ctx->framebuffer;
@@ -1148,107 +1323,122 @@ swr_update_derived(struct pipe_context *pipe,
       if (zb && swr_resource(zb->texture)->has_depth)
          rastState->depthFormat = swr_resource(zb->texture)->swr.format;
 
-      rastState->depthClipEnable = rasterizer->depth_clip;
+      rastState->depthClipEnable = rasterizer->depth_clip_near;
+      rastState->clipEnable = rasterizer->depth_clip_near | rasterizer->depth_clip_far;
       rastState->clipHalfZ = rasterizer->clip_halfz;
 
-      rastState->clipDistanceMask =
-         ctx->vs->info.base.num_written_clipdistance ?
-         ctx->vs->info.base.clipdist_writemask & rasterizer->clip_plane_enable :
-         rasterizer->clip_plane_enable;
-
-      rastState->cullDistanceMask =
-         ctx->vs->info.base.culldist_writemask << ctx->vs->info.base.num_written_clipdistance;
-
-      SwrSetRastState(ctx->swrContext, rastState);
-   }
-
-   /* Scissor */
-   if (ctx->dirty & SWR_NEW_SCISSOR) {
-      SwrSetScissorRects(ctx->swrContext, 1, &ctx->swr_scissor);
+      ctx->api.pfnSwrSetRastState(ctx->swrContext, rastState);
    }
 
    /* Viewport */
    if (ctx->dirty & (SWR_NEW_VIEWPORT | SWR_NEW_FRAMEBUFFER
                      | SWR_NEW_RASTERIZER)) {
-      pipe_viewport_state *state = &ctx->viewport;
+      pipe_viewport_state *state = &ctx->viewports[0];
       pipe_framebuffer_state *fb = &ctx->framebuffer;
       pipe_rasterizer_state *rasterizer = ctx->rasterizer;
 
-      SWR_VIEWPORT *vp = &ctx->derived.vp;
+      SWR_VIEWPORT *vp = &ctx->derived.vp[0];
       SWR_VIEWPORT_MATRICES *vpm = &ctx->derived.vpm;
 
-      vp->x = state->translate[0] - state->scale[0];
-      vp->width = 2 * state->scale[0];
-      vp->y = state->translate[1] - fabs(state->scale[1]);
-      vp->height = 2 * fabs(state->scale[1]);
-      util_viewport_zmin_zmax(state, rasterizer->clip_halfz,
-                              &vp->minZ, &vp->maxZ);
-
-      vpm->m00[0] = state->scale[0];
-      vpm->m11[0] = state->scale[1];
-      vpm->m22[0] = state->scale[2];
-      vpm->m30[0] = state->translate[0];
-      vpm->m31[0] = state->translate[1];
-      vpm->m32[0] = state->translate[2];
-
-      /* Now that the matrix is calculated, clip the view coords to screen
-       * size.  OpenGL allows for -ve x,y in the viewport. */
-      if (vp->x < 0.0f) {
-         vp->width += vp->x;
-         vp->x = 0.0f;
-      }
-      if (vp->y < 0.0f) {
-         vp->height += vp->y;
-         vp->y = 0.0f;
-      }
-      vp->width = std::min(vp->width, (float)fb->width - vp->x);
-      vp->height = std::min(vp->height, (float)fb->height - vp->y);
+      for (unsigned i = 0; i < KNOB_NUM_VIEWPORTS_SCISSORS; i++) {
+         vp->x = state->translate[0] - state->scale[0];
+         vp->width = 2 * state->scale[0];
+         vp->y = state->translate[1] - fabs(state->scale[1]);
+         vp->height = 2 * fabs(state->scale[1]);
+         util_viewport_zmin_zmax(state, rasterizer->clip_halfz,
+                                 &vp->minZ, &vp->maxZ);
 
-      SwrSetViewports(ctx->swrContext, 1, vp, vpm);
-   }
+         if (rasterizer->depth_clip_near) {
+            vp->minZ = 0.0f;
+         }
 
-   /* Set vertex & index buffers */
-   /* (using draw info if called by swr_draw_vbo) */
-   /* TODO: This is always true, because the index buffer comes from
-    * pipe_draw_info.
-    */
-   if (1 || ctx->dirty & SWR_NEW_VERTEX) {
-      uint32_t scratch_total;
-      uint8_t *scratch = NULL;
+         if (rasterizer->depth_clip_far) {
+            vp->maxZ = 1.0f;
+         }
 
-      /* If being called by swr_draw_vbo, copy draw details */
-      struct pipe_draw_info info = {0};
-      if (p_draw_info)
-         info = *p_draw_info;
+         vpm->m00[i] = state->scale[0];
+         vpm->m11[i] = state->scale[1];
+         vpm->m22[i] = state->scale[2];
+         vpm->m30[i] = state->translate[0];
+         vpm->m31[i] = state->translate[1];
+         vpm->m32[i] = state->translate[2];
+
+         /* Now that the matrix is calculated, clip the view coords to screen
+          * size.  OpenGL allows for -ve x,y in the viewport. */
+         if (vp->x < 0.0f) {
+            vp->width += vp->x;
+            vp->x = 0.0f;
+         }
+         if (vp->y < 0.0f) {
+            vp->height += vp->y;
+            vp->y = 0.0f;
+         }
+         vp->width = std::min(vp->width, (float) fb->width - vp->x);
+         vp->height = std::min(vp->height, (float) fb->height - vp->y);
 
-      /* We must get all the scratch space in one go */
-      scratch_total = 0;
-      for (UINT i = 0; i < ctx->num_vertex_buffers; i++) {
-         struct pipe_vertex_buffer *vb = &ctx->vertex_buffer[i];
+         vp++;
+         state++;
+      }
+      ctx->api.pfnSwrSetViewports(ctx->swrContext, KNOB_NUM_VIEWPORTS_SCISSORS,
+                                  &ctx->derived.vp[0], &ctx->derived.vpm);
+   }
 
-         if (!vb->is_user_buffer)
-            continue;
+   /* When called from swr_clear (p_draw_info = null), render targets,
+    * rasterState and viewports (dependent on render targets) are the only
+    * necessary validation.  Defer remaining validation by setting
+    * post_update_dirty_flags and clear all dirty flags.  BackendState is
+    * still unconditionally validated below */
+   if (!p_draw_info) {
+      post_update_dirty_flags = ctx->dirty & ~(SWR_NEW_FRAMEBUFFER |
+                                               SWR_NEW_RASTERIZER |
+                                               SWR_NEW_VIEWPORT);
+      ctx->dirty = 0;
+   }
 
-         uint32_t elems, base, size;
-         swr_user_vbuf_range(&info, ctx->velems, vb, i, &elems, &base, &size);
-         scratch_total += AlignUp(size, 4);
-      }
+   /* Scissor */
+   if (ctx->dirty & SWR_NEW_SCISSOR) {
+      ctx->api.pfnSwrSetScissorRects(ctx->swrContext, KNOB_NUM_VIEWPORTS_SCISSORS, ctx->swr_scissors);
+   }
 
-      if (scratch_total) {
-         scratch = (uint8_t *)swr_copy_to_scratch_space(
-               ctx, &ctx->scratch->vertex_buffer, NULL, scratch_total);
-      }
+   /* Set vertex & index buffers */
+   if (ctx->dirty & SWR_NEW_VERTEX) {
+      const struct pipe_draw_info &info = *p_draw_info;
 
       /* vertex buffers */
       SWR_VERTEX_BUFFER_STATE swrVertexBuffers[PIPE_MAX_ATTRIBS];
       for (UINT i = 0; i < ctx->num_vertex_buffers; i++) {
-         uint32_t size, pitch, elems, partial_inbounds;
-         uint32_t min_vertex_index;
+         uint32_t size = 0, pitch = 0, elems = 0, partial_inbounds = 0;
+         uint32_t min_vertex_index = 0;
          const uint8_t *p_data;
          struct pipe_vertex_buffer *vb = &ctx->vertex_buffer[i];
 
          pitch = vb->stride;
-         if (!vb->is_user_buffer) {
+         if (vb->is_user_buffer) {
+            /* Client buffer
+             * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
+             * revalidate on each draw */
+            post_update_dirty_flags |= SWR_NEW_VERTEX;
+
+            uint32_t base;
+            swr_user_vbuf_range(&info, ctx->velems, vb, i, &elems, &base, &size);
+            partial_inbounds = 0;
+            min_vertex_index = info.min_index + info.index_bias;
+
+            size = AlignUp(size, 4);
+            /* If size of client memory copy is too large, don't copy. The
+             * draw will access user-buffer directly and then block.  This is
+             * faster than queuing many large client draws. */
+            if (size >= screen->client_copy_limit) {
+               post_update_dirty_flags |= SWR_BLOCK_CLIENT_DRAW;
+               p_data = (const uint8_t *) vb->buffer.user;
+            } else {
+               /* Copy only needed vertices to scratch space */
+               const void *ptr = (const uint8_t *) vb->buffer.user + base;
+               ptr = (uint8_t *)swr_copy_to_scratch_space(
+                     ctx, &ctx->scratch->vertex_buffer, ptr, size);
+               p_data = (const uint8_t *)ptr - base;
+            }
+         } else if (vb->buffer.resource) {
             /* VBO */
             if (!pitch) {
                /* If pitch=0 (ie vb->stride), buffer contains a single
@@ -1269,37 +1459,20 @@ swr_update_derived(struct pipe_context *pipe,
             }
 
             p_data = swr_resource_data(vb->buffer.resource) + vb->buffer_offset;
-         } else {
-            /* Client buffer
-             * client memory is one-time use, re-trigger SWR_NEW_VERTEX to
-             * revalidate on each draw */
-            post_update_dirty_flags |= SWR_NEW_VERTEX;
-
-            uint32_t base;
-            swr_user_vbuf_range(&info, ctx->velems, vb, i, &elems, &base, &size);
-            partial_inbounds = 0;
-            min_vertex_index = info.min_index;
-
-            /* Copy only needed vertices to scratch space */
-            size = AlignUp(size, 4);
-            const void *ptr = (const uint8_t *) vb->buffer.user + base;
-            memcpy(scratch, ptr, size);
-            ptr = scratch;
-            scratch += size;
-            p_data = (const uint8_t *)ptr - base;
-         }
+         } else
+            p_data = NULL;
 
          swrVertexBuffers[i] = {0};
          swrVertexBuffers[i].index = i;
          swrVertexBuffers[i].pitch = pitch;
-         swrVertexBuffers[i].pData = p_data;
+         swrVertexBuffers[i].xpData = (gfxptr_t) p_data;
          swrVertexBuffers[i].size = size;
          swrVertexBuffers[i].minVertex = min_vertex_index;
          swrVertexBuffers[i].maxVertex = elems;
          swrVertexBuffers[i].partialInboundsSize = partial_inbounds;
       }
 
-      SwrSetVertexBuffers(
+      ctx->api.pfnSwrSetVertexBuffers(
          ctx->swrContext, ctx->num_vertex_buffers, swrVertexBuffers);
 
       /* index buffer, if required (info passed in by swr_draw_vbo) */
@@ -1308,7 +1481,7 @@ swr_update_derived(struct pipe_context *pipe,
          const uint8_t *p_data;
          uint32_t size, pitch;
 
-         pitch = p_draw_info->index_size ? p_draw_info->index_size : sizeof(uint32_t);
+         pitch = info.index_size ? info.index_size : sizeof(uint32_t);
          index_type = swr_convert_index_type(pitch);
 
          if (!info.has_user_indices) {
@@ -1324,21 +1497,29 @@ swr_update_derived(struct pipe_context *pipe,
             post_update_dirty_flags |= SWR_NEW_VERTEX;
 
             size = info.count * pitch;
-            size = AlignUp(size, 4);
 
-            /* Copy indices to scratch space */
-            const void *ptr = info.index.user;
-            ptr = swr_copy_to_scratch_space(
-               ctx, &ctx->scratch->index_buffer, ptr, size);
-            p_data = (const uint8_t *)ptr;
+            size = AlignUp(size, 4);
+            /* If size of client memory copy is too large, don't copy. The
+             * draw will access user-buffer directly and then block.  This is
+             * faster than queuing many large client draws. */
+            if (size >= screen->client_copy_limit) {
+               post_update_dirty_flags |= SWR_BLOCK_CLIENT_DRAW;
+               p_data = (const uint8_t *) info.index.user;
+            } else {
+               /* Copy indices to scratch space */
+               const void *ptr = info.index.user;
+               ptr = swr_copy_to_scratch_space(
+                     ctx, &ctx->scratch->index_buffer, ptr, size);
+               p_data = (const uint8_t *)ptr;
+            }
          }
 
          SWR_INDEX_BUFFER_STATE swrIndexBuffer;
-         swrIndexBuffer.format = swr_convert_index_type(p_draw_info->index_size);
-         swrIndexBuffer.pIndices = p_data;
+         swrIndexBuffer.format = swr_convert_index_type(info.index_size);
+         swrIndexBuffer.xpIndices = (gfxptr_t) p_data;
          swrIndexBuffer.size = size;
 
-         SwrSetIndexBuffer(ctx->swrContext, &swrIndexBuffer);
+         ctx->api.pfnSwrSetIndexBuffer(ctx->swrContext, &swrIndexBuffer);
       }
 
       struct swr_vertex_element_state *velems = ctx->velems;
@@ -1351,6 +1532,8 @@ swr_update_derived(struct pipe_context *pipe,
    /* GeometryShader */
    if (ctx->dirty & (SWR_NEW_GS |
                      SWR_NEW_VS |
+                     SWR_NEW_TCS |
+                     SWR_NEW_TES |
                      SWR_NEW_SAMPLER |
                      SWR_NEW_SAMPLER_VIEW)) {
       if (ctx->gs) {
@@ -1363,7 +1546,7 @@ swr_update_derived(struct pipe_context *pipe,
          } else {
             func = swr_compile_gs(ctx, key);
          }
-         SwrSetGsFunc(ctx->swrContext, func);
+         ctx->api.pfnSwrSetGsFunc(ctx->swrContext, func);
 
          /* JIT sampler state */
          if (ctx->dirty & SWR_NEW_SAMPLER) {
@@ -1381,20 +1564,122 @@ swr_update_derived(struct pipe_context *pipe,
                                      ctx->swrDC.texturesGS);
          }
 
-         SwrSetGsState(ctx->swrContext, &ctx->gs->gsState);
+         ctx->api.pfnSwrSetGsState(ctx->swrContext, &ctx->gs->gsState);
       } else {
          SWR_GS_STATE state = { 0 };
-         SwrSetGsState(ctx->swrContext, &state);
-         SwrSetGsFunc(ctx->swrContext, NULL);
+         ctx->api.pfnSwrSetGsState(ctx->swrContext, &state);
+         ctx->api.pfnSwrSetGsFunc(ctx->swrContext, NULL);
       }
    }
 
-   /* VertexShader */
-   if (ctx->dirty & (SWR_NEW_VS |
-                     SWR_NEW_RASTERIZER | // for clip planes
+   // We may need to restore tessellation state
+   // This restored state may be however overwritten
+   // during shader compilation
+   if (ctx->dirty & SWR_NEW_TS) {
+      if (ctx->tes != nullptr) {
+         ctx->tsState = ctx->tes->ts_state;
+         ctx->api.pfnSwrSetTsState(ctx->swrContext, &ctx->tsState);
+      } else {
+         SWR_TS_STATE state = { 0 };
+         ctx->api.pfnSwrSetTsState(ctx->swrContext, &state);
+      }
+   }
+
+   // Tessellation Evaluation Shader
+   // Compile TES first, because TCS is optional
+   if (ctx->dirty & (SWR_NEW_GS |
+                     SWR_NEW_VS |
+                     SWR_NEW_TCS |
+                     SWR_NEW_TES |
                      SWR_NEW_SAMPLER |
-                     SWR_NEW_SAMPLER_VIEW |
-                     SWR_NEW_FRAMEBUFFER)) {
+                     SWR_NEW_SAMPLER_VIEW)) {
+      if (ctx->tes) {
+         swr_jit_tes_key key;
+         swr_generate_tes_key(key, ctx, ctx->tes);
+
+         auto search = ctx->tes->map.find(key);
+         PFN_TES_FUNC func;
+         if (search != ctx->tes->map.end()) {
+            func = search->second->shader;
+         } else {
+            func = swr_compile_tes(ctx, key);
+         }
+
+         ctx->api.pfnSwrSetDsFunc(ctx->swrContext, func);
+
+         /* JIT sampler state */
+         if (ctx->dirty & SWR_NEW_SAMPLER) {
+            swr_update_sampler_state(ctx,
+                                     PIPE_SHADER_TESS_EVAL,
+                                     key.nr_samplers,
+                                     ctx->swrDC.samplersTES);
+         }
+
+         /* JIT sampler view state */
+         if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
+            swr_update_texture_state(ctx,
+                                     PIPE_SHADER_TESS_EVAL,
+                                     key.nr_sampler_views,
+                                     ctx->swrDC.texturesTES);
+         }
+
+         // Update tessellation state in case it's been updated
+         ctx->api.pfnSwrSetTsState(ctx->swrContext, &ctx->tsState);
+      } else {
+         ctx->api.pfnSwrSetDsFunc(ctx->swrContext, NULL);
+      }
+   }
+
+   /* Tessellation Control Shader */
+   if (ctx->dirty & (SWR_NEW_GS |
+                     SWR_NEW_VS |
+                     SWR_NEW_TCS |
+                     SWR_NEW_TES |
+                     SWR_NEW_SAMPLER |
+                     SWR_NEW_SAMPLER_VIEW)) {
+      if (ctx->tcs) {
+         ctx->tcs->vertices_per_patch = p_draw_info->vertices_per_patch;
+
+         swr_jit_tcs_key key;
+         swr_generate_tcs_key(key, ctx, ctx->tcs);
+
+         auto search = ctx->tcs->map.find(key);
+         PFN_TCS_FUNC func;
+         if (search != ctx->tcs->map.end()) {
+            func = search->second->shader;
+         } else {
+            func = swr_compile_tcs(ctx, key);
+         }
+
+         ctx->api.pfnSwrSetHsFunc(ctx->swrContext, func);
+
+         /* JIT sampler state */
+         if (ctx->dirty & SWR_NEW_SAMPLER) {
+            swr_update_sampler_state(ctx,
+                                     PIPE_SHADER_TESS_CTRL,
+                                     key.nr_samplers,
+                                     ctx->swrDC.samplersTCS);
+         }
+
+         /* JIT sampler view state */
+         if (ctx->dirty & (SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
+            swr_update_texture_state(ctx,
+                                     PIPE_SHADER_TESS_CTRL,
+                                     key.nr_sampler_views,
+                                     ctx->swrDC.texturesTCS);
+         }
+
+         // Update tessellation state in case it's been updated
+         ctx->api.pfnSwrSetTsState(ctx->swrContext, &ctx->tsState);
+      } else {
+         ctx->api.pfnSwrSetHsFunc(ctx->swrContext, NULL);
+      }
+   }
+
+   /* VertexShader */
+   if (ctx->dirty
+       & (SWR_NEW_VS | SWR_NEW_RASTERIZER | // for clip planes
+          SWR_NEW_SAMPLER | SWR_NEW_SAMPLER_VIEW | SWR_NEW_FRAMEBUFFER)) {
       swr_jit_vs_key key;
       swr_generate_vs_key(key, ctx, ctx->vs);
       auto search = ctx->vs->map.find(key);
@@ -1404,14 +1689,12 @@ swr_update_derived(struct pipe_context *pipe,
       } else {
          func = swr_compile_vs(ctx, key);
       }
-      SwrSetVertexFunc(ctx->swrContext, func);
+      ctx->api.pfnSwrSetVertexFunc(ctx->swrContext, func);
 
       /* JIT sampler state */
       if (ctx->dirty & SWR_NEW_SAMPLER) {
-         swr_update_sampler_state(ctx,
-                                  PIPE_SHADER_VERTEX,
-                                  key.nr_samplers,
-                                  ctx->swrDC.samplersVS);
+         swr_update_sampler_state(
+            ctx, PIPE_SHADER_VERTEX, key.nr_samplers, ctx->swrDC.samplersVS);
       }
 
       /* JIT sampler view state */
@@ -1440,6 +1723,8 @@ swr_update_derived(struct pipe_context *pipe,
    if (ctx->dirty & (SWR_NEW_FS |
                      SWR_NEW_VS |
                      SWR_NEW_GS |
+                     SWR_NEW_TES |
+                     SWR_NEW_TCS |
                      SWR_NEW_RASTERIZER |
                      SWR_NEW_SAMPLER |
                      SWR_NEW_SAMPLER_VIEW |
@@ -1460,7 +1745,7 @@ swr_update_derived(struct pipe_context *pipe,
       psState.writesODepth = ctx->fs->info.base.writes_z;
       psState.usesSourceDepth = ctx->fs->info.base.reads_z;
       psState.shadingRate = SWR_SHADING_RATE_PIXEL;
-      psState.numRenderTargets = ctx->framebuffer.nr_cbufs;
+      psState.renderTargetMask = (1 << ctx->framebuffer.nr_cbufs) - 1;
       psState.posOffset = SWR_PS_POSITION_SAMPLE_NONE;
       uint32_t barycentricsMask = 0;
 #if 0
@@ -1492,7 +1777,7 @@ swr_update_derived(struct pipe_context *pipe,
       psState.barycentricsMask = barycentricsMask;
       psState.usesUAV = false; // XXX
       psState.forceEarlyZ = false;
-      SwrSetPixelShaderState(ctx->swrContext, &psState);
+      ctx->api.pfnSwrSetPixelShaderState(ctx->swrContext, &psState);
 
       /* JIT sampler state */
       if (ctx->dirty & (SWR_NEW_SAMPLER |
@@ -1530,6 +1815,16 @@ swr_update_derived(struct pipe_context *pipe,
       swr_update_constants(ctx, PIPE_SHADER_GEOMETRY);
    }
 
+   /* Tessellation Control Shader Constants */
+   if (ctx->dirty & SWR_NEW_TCSCONSTANTS) {
+      swr_update_constants(ctx, PIPE_SHADER_TESS_CTRL);
+   }
+
+   /* Tessellation Evaluation Shader Constants */
+   if (ctx->dirty & SWR_NEW_TESCONSTANTS) {
+      swr_update_constants(ctx, PIPE_SHADER_TESS_EVAL);
+   }
+
    /* Depth/stencil state */
    if (ctx->dirty & (SWR_NEW_DEPTH_STENCIL_ALPHA | SWR_NEW_FRAMEBUFFER)) {
       struct pipe_depth_state *depth = &(ctx->depth_stencil->depth);
@@ -1581,12 +1876,12 @@ swr_update_derived(struct pipe_context *pipe,
       depthStencilState.depthTestEnable = depth->enabled;
       depthStencilState.depthTestFunc = swr_convert_depth_func(depth->func);
       depthStencilState.depthWriteEnable = depth->writemask;
-      SwrSetDepthStencilState(ctx->swrContext, &depthStencilState);
+      ctx->api.pfnSwrSetDepthStencilState(ctx->swrContext, &depthStencilState);
 
       depthBoundsState.depthBoundsTestEnable = depth->bounds_test;
       depthBoundsState.depthBoundsTestMinValue = depth->bounds_min;
       depthBoundsState.depthBoundsTestMaxValue = depth->bounds_max;
-      SwrSetDepthBoundsState(ctx->swrContext, &depthBoundsState);
+      ctx->api.pfnSwrSetDepthBoundsState(ctx->swrContext, &depthBoundsState);
    }
 
    /* Blend State */
@@ -1615,7 +1910,7 @@ swr_update_derived(struct pipe_context *pipe,
          blendState.renderTarget[0].writeDisableGreen = 1;
          blendState.renderTarget[0].writeDisableBlue = 1;
          blendState.renderTarget[0].writeDisableAlpha = 1;
-         SwrSetBlendFunc(ctx->swrContext, 0, NULL);
+         ctx->api.pfnSwrSetBlendFunc(ctx->swrContext, 0, NULL);
       }
       else
          for (int target = 0;
@@ -1647,7 +1942,7 @@ swr_update_derived(struct pipe_context *pipe,
             if (compileState.blendState.blendEnable == false &&
                 compileState.blendState.logicOpEnable == false &&
                 ctx->depth_stencil->alpha.enabled == 0) {
-               SwrSetBlendFunc(ctx->swrContext, target, NULL);
+               ctx->api.pfnSwrSetBlendFunc(ctx->swrContext, target, NULL);
                continue;
             }
 
@@ -1670,7 +1965,7 @@ swr_update_derived(struct pipe_context *pipe,
             compileState.alphaTestFormat = ALPHA_TEST_FLOAT32; // xxx
 
             compileState.Canonicalize();
-            
+
             PFN_BLEND_JIT_FUNC func = NULL;
             auto search = ctx->blendJIT->find(compileState);
             if (search != ctx->blendJIT->end()) {
@@ -1683,43 +1978,44 @@ swr_update_derived(struct pipe_context *pipe,
 
                ctx->blendJIT->insert(std::make_pair(compileState, func));
             }
-            SwrSetBlendFunc(ctx->swrContext, target, func);
+            ctx->api.pfnSwrSetBlendFunc(ctx->swrContext, target, func);
          }
 
-      SwrSetBlendState(ctx->swrContext, &blendState);
+      ctx->api.pfnSwrSetBlendState(ctx->swrContext, &blendState);
    }
 
    if (ctx->dirty & SWR_NEW_STIPPLE) {
       swr_update_poly_stipple(ctx);
    }
 
-   if (ctx->dirty & (SWR_NEW_VS | SWR_NEW_SO | SWR_NEW_RASTERIZER)) {
+   if (ctx->dirty & (SWR_NEW_VS | SWR_NEW_TCS | SWR_NEW_TES | SWR_NEW_SO | SWR_NEW_RASTERIZER)) {
       ctx->vs->soState.rasterizerDisable =
          ctx->rasterizer->rasterizer_discard;
-      SwrSetSoState(ctx->swrContext, &ctx->vs->soState);
+      ctx->api.pfnSwrSetSoState(ctx->swrContext, &ctx->vs->soState);
 
       pipe_stream_output_info *stream_output = &ctx->vs->pipe.stream_output;
 
-      for (uint32_t i = 0; i < ctx->num_so_targets; i++) {
+      for (uint32_t i = 0; i < MAX_SO_STREAMS; i++) {
          SWR_STREAMOUT_BUFFER buffer = {0};
-         if (!ctx->so_targets[i])
-            continue;
-         buffer.enable = true;
-         buffer.pBuffer =
-            (uint32_t *)(swr_resource_data(ctx->so_targets[i]->buffer) +
-                         ctx->so_targets[i]->buffer_offset);
-         buffer.bufferSize = ctx->so_targets[i]->buffer_size >> 2;
-         buffer.pitch = stream_output->stride[i];
-         buffer.streamOffset = 0;
-
-         SwrSetSoBuffers(ctx->swrContext, &buffer, i);
+         if (ctx->so_targets[i]) {
+             buffer.enable = true;
+             buffer.pBuffer =
+                (gfxptr_t)(swr_resource_data(ctx->so_targets[i]->buffer) +
+                             ctx->so_targets[i]->buffer_offset);
+             buffer.bufferSize = ctx->so_targets[i]->buffer_size >> 2;
+             buffer.pitch = stream_output->stride[i];
+             buffer.streamOffset = 0;
+        }
+
+         ctx->api.pfnSwrSetSoBuffers(ctx->swrContext, &buffer, i);
       }
    }
 
+
    if (ctx->dirty & (SWR_NEW_CLIP | SWR_NEW_RASTERIZER | SWR_NEW_VS)) {
       // shader exporting clip distances overrides all user clip planes
       if (ctx->rasterizer->clip_plane_enable &&
-          !ctx->vs->info.base.num_written_clipdistance)
+          !swr_get_last_fe(ctx)->num_written_clipdistance)
       {
          swr_draw_context *pDC = &ctx->swrDC;
          memcpy(pDC->userClipPlanes,
@@ -1732,7 +2028,12 @@ swr_update_derived(struct pipe_context *pipe,
    SWR_BACKEND_STATE backendState = {0};
    if (ctx->gs) {
       backendState.numAttributes = ctx->gs->info.base.num_outputs - 1;
-   } else {
+   } else
+   if (ctx->tes) {
+      backendState.numAttributes = ctx->tes->info.base.num_outputs - 1;
+      // no case for TCS, because if TCS is active, TES must be active
+      // as well - pipeline stages after tessellation does not support patches
+   }  else {
       backendState.numAttributes = ctx->vs->info.base.num_outputs - 1;
       if (ctx->fs->info.base.uses_primid) {
          backendState.numAttributes++;
@@ -1756,14 +2057,24 @@ swr_update_derived(struct pipe_context *pipe,
       (ctx->rasterizer->flatshade ? ctx->fs->flatConstantMask : 0);
    backendState.pointSpriteTexCoordMask = ctx->fs->pointSpriteMask;
 
-   struct tgsi_shader_info *pLastFE =
-      ctx->gs ?
-      &ctx->gs->info.base :
-      &ctx->vs->info.base;
+   struct tgsi_shader_info *pLastFE = swr_get_last_fe(ctx);
+
    backendState.readRenderTargetArrayIndex = pLastFE->writes_layer;
    backendState.readViewportArrayIndex = pLastFE->writes_viewport_index;
+   backendState.vertexAttribOffset = VERTEX_ATTRIB_START_SLOT; // TODO: optimize
+
+   backendState.clipDistanceMask =
+      pLastFE->num_written_clipdistance ?
+      pLastFE->clipdist_writemask & ctx->rasterizer->clip_plane_enable :
+      ctx->rasterizer->clip_plane_enable;
+
+   backendState.cullDistanceMask =
+      pLastFE->culldist_writemask << pLastFE->num_written_clipdistance;
 
-   SwrSetBackendState(ctx->swrContext, &backendState);
+   // Assume old layout of SGV, POSITION, CLIPCULL, ATTRIB
+   backendState.vertexClipCullOffset = backendState.vertexAttribOffset - 2;
+
+   ctx->api.pfnSwrSetBackendState(ctx->swrContext, &backendState);
 
    /* Ensure that any in-progress attachment change StoreTiles finish */
    if (swr_is_fence_pending(screen->flush_fence))
@@ -1827,6 +2138,7 @@ swr_set_so_targets(struct pipe_context *pipe,
    }
 
    swr->num_so_targets = num_targets;
+   swr->swrDC.soPrims = &swr->so_primCounter;
 
    swr->dirty |= SWR_NEW_SO;
 }
@@ -1867,6 +2179,14 @@ swr_state_init(struct pipe_context *pipe)
    pipe->bind_gs_state = swr_bind_gs_state;
    pipe->delete_gs_state = swr_delete_gs_state;
 
+   pipe->create_tcs_state = swr_create_tcs_state;
+   pipe->bind_tcs_state = swr_bind_tcs_state;
+   pipe->delete_tcs_state = swr_delete_tcs_state;
+
+   pipe->create_tes_state = swr_create_tes_state;
+   pipe->bind_tes_state = swr_bind_tes_state;
+   pipe->delete_tes_state = swr_delete_tes_state;
+
    pipe->set_constant_buffer = swr_set_constant_buffer;
 
    pipe->create_vertex_elements_state = swr_create_vertex_elements_state;