<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class=""><br class="">
</div>
<div class="">Reviewed-by: George Kyriazis <<a href="mailto:george.kyriazis@intel.com" class="">george.kyriazis@intel.com</a>></div>
<div class=""><br class="">
</div>
<div class="">With the assumption that there are additional changes forthcoming.</div>
<div class=""><br class="">
</div>
<div class="">
<div>
<blockquote type="cite" class="">
<div class="">On Apr 13, 2017, at 5:40 PM, Bruce Cherniak <<a href="mailto:bruce.cherniak@intel.com" class="">bruce.cherniak@intel.com</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="">This patch enables multisample antialiasing in the OpenSWR software renderer.<br class="">
<br class="">
MSAA is a proof-of-concept/work-in-progress with bug fixes and performance<br class="">
on the way.  We wanted to get the changes out now to allow several customers<br class="">
to begin experimenting with MSAA in a software renderer.  So as not to<br class="">
impact current customers, MSAA is turned off by default - previous<br class="">
functionality and performance remain intact.  It is easily enabled via<br class="">
environment variables, as described below.<br class="">
<br class="">
It has only been tested with the glx-lib winsys.  The intention is to<br class="">
enable other state-trackers, both Windows and Linux and more fully support<br class="">
FBOs.<br class="">
<br class="">
There are 2 environment variables that affect behavior:<br class="">
<br class="">
* SWR_MSAA_FORCE_ENABLE - force MSAA on, for apps that are not designed<br class="">
 for MSAA... Beware, results will vary.  This is mainly for testing.<br class="">
<br class="">
* SWR_MSAA_MAX_SAMPLE_COUNT - sets maximum supported number of<br class="">
 samples (1,2,4,8,16), or 0 to disable MSAA altogether.<br class="">
 (The default is currently 0.)<br class="">
<br class="">
<br class="">
---<br class="">
src/gallium/drivers/swr/swr_context.cpp |  90 +++++++++++++++++-<br class="">
src/gallium/drivers/swr/swr_context.h   |   3 +<br class="">
src/gallium/drivers/swr/swr_resource.h  |   4 +<br class="">
src/gallium/drivers/swr/swr_screen.cpp  | 159 +++++++++++++++++++++++++++++---<br class="">
src/gallium/drivers/swr/swr_screen.h    |   8 ++<br class="">
src/gallium/drivers/swr/swr_state.cpp   |  74 +++++++++++++--<br class="">
6 files changed, 313 insertions(+), 25 deletions(-)<br class="">
<br class="">
diff --git a/src/gallium/drivers/swr/swr_context.cpp b/src/gallium/drivers/swr/swr_context.cpp<br class="">
index 6f46d66..aa5cca8 100644<br class="">
--- a/src/gallium/drivers/swr/swr_context.cpp<br class="">
+++ b/src/gallium/drivers/swr/swr_context.cpp<br class="">
@@ -267,20 +267,104 @@ swr_resource_copy(struct pipe_context *pipe,<br class="">
}<br class="">
<br class="">
<br class="">
+/* XXX: This resolve is incomplete and suboptimal. It will be removed once the<br class="">
+ * pipelined resolve blit works. */<br class="">
+void<br class="">
+swr_do_msaa_resolve(struct pipe_resource *src_resource,<br class="">
+                    struct pipe_resource *dst_resource)<br class="">
+{<br class="">
+   /* This is a pretty dumb inline resolve.  It only supports 8-bit formats<br class="">
+    * (ex RGBA8/BGRA8) - which are most common display formats anyway.<br class="">
+    */<br class="">
+<br class="">
+   /* quick check for 8-bit and number of components */<br class="">
+   uint8_t bits_per_component =<br class="">
+      util_format_get_component_bits(src_resource->format,<br class="">
+            UTIL_FORMAT_COLORSPACE_RGB, 0);<br class="">
+<br class="">
+   /* Unsupported resolve format */<br class="">
+   assert(src_resource->format == dst_resource->format);<br class="">
+   assert(bits_per_component == 8);<br class="">
+   if ((src_resource->format != dst_resource->format) ||<br class="">
+       (bits_per_component != 8)) {<br class="">
+      return;<br class="">
+   }<br class="">
+<br class="">
+   uint8_t src_num_comps = util_format_get_nr_components(src_resource->format);<br class="">
+<br class="">
+   SWR_SURFACE_STATE *src_surface = &swr_resource(src_resource)->swr;<br class="">
+   SWR_SURFACE_STATE *dst_surface = &swr_resource(dst_resource)->swr;<br class="">
+<br class="">
+   uint32_t *src, *dst, offset;<br class="">
+   uint32_t num_samples = src_surface->numSamples;<br class="">
+   float recip_num_samples = 1.0f / num_samples;<br class="">
+   for (uint32_t y = 0; y < src_surface->height; y++) {<br class="">
+      for (uint32_t x = 0; x < src_surface->width; x++) {<br class="">
+         float r = 0.0f;<br class="">
+         float g = 0.0f;<br class="">
+         float b = 0.0f;<br class="">
+         float a = 0.0f;<br class="">
+         for (uint32_t sampleNum = 0;  sampleNum < num_samples; sampleNum++) {<br class="">
+            offset = ComputeSurfaceOffset<false>(x, y, 0, 0, sampleNum, 0, src_surface);<br class="">
+            src = (uint32_t *) src_surface->pBaseAddress + offset/src_num_comps;<br class="">
+            const uint32_t sample = *src;<br class="">
+            r += (float)((sample >> 24) & 0xff) / 255.0f * recip_num_samples;<br class="">
+            g += (float)((sample >> 16) & 0xff) / 255.0f * recip_num_samples;<br class="">
+            b += (float)((sample >>  8) & 0xff) / 255.0f * recip_num_samples;<br class="">
+            a += (float)((sample      ) & 0xff) / 255.0f * recip_num_samples;<br class="">
+         }<br class="">
+         uint32_t result = 0;<br class="">
+         result  = ((uint8_t)(r * 255.0f) & 0xff) << 24;<br class="">
+         result |= ((uint8_t)(g * 255.0f) & 0xff) << 16;<br class="">
+         result |= ((uint8_t)(b * 255.0f) & 0xff) <<  8;<br class="">
+         result |= ((uint8_t)(a * 255.0f) & 0xff);<br class="">
+         offset = ComputeSurfaceOffset<false>(x, y, 0, 0, 0, 0, src_surface);<br class="">
+         dst = (uint32_t *) dst_surface->pBaseAddress + offset/src_num_comps;<br class="">
+         *dst = result;<br class="">
+      }<br class="">
+   }<br class="">
+}<br class="">
+<br class="">
+<br class="">
static void<br class="">
swr_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)<br class="">
{<br class="">
   struct swr_context *ctx = swr_context(pipe);<br class="">
+   /* Make a copy of the const blit_info, so we can modify it */<br class="">
   struct pipe_blit_info info = *blit_info;<br class="">
<br class="">
-   if (blit_info->render_condition_enable && !swr_check_render_cond(pipe))<br class="">
+   if (info.render_condition_enable && !swr_check_render_cond(pipe))<br class="">
      return;<br class="">
<br class="">
   if (info.src.resource->nr_samples > 1 && info.dst.resource->nr_samples <= 1<br class="">
       && !util_format_is_depth_or_stencil(info.src.resource->format)<br class="">
       && !util_format_is_pure_integer(info.src.resource->format)) {<br class="">
-      debug_printf("swr: color resolve unimplemented\n");<br class="">
-      return;<br class="">
+      debug_printf("swr_blit: color resolve : %d -> %d\n",<br class="">
+            info.src.resource->nr_samples, info.dst.resource->nr_samples);<br class="">
+<br class="">
+      /* Because the resolve is being done inline (not pipelined),<br class="">
+       * resources need to be stored out of hottiles and the pipeline empty.<br class="">
+       *<br class="">
+       * Resources are marked unused following fence finish because all<br class="">
+       * pipeline operations are complete.  Validation of the blit will mark<br class="">
+       * them are read/write again.<br class="">
+       */<br class="">
+      swr_store_dirty_resource(pipe, info.src.resource, SWR_TILE_RESOLVED);<br class="">
+      swr_store_dirty_resource(pipe, info.dst.resource, SWR_TILE_RESOLVED);<br class="">
+      swr_fence_finish(pipe->screen, NULL, swr_screen(pipe->screen)->flush_fence, 0);<br class="">
+      swr_resource_unused(info.src.resource);<br class="">
+      swr_resource_unused(info.dst.resource);<br class="">
+<br class="">
+      struct pipe_resource *src_resource = info.src.resource;<br class="">
+      struct pipe_resource *resolve_target =<br class="">
+         swr_resource(src_resource)->resolve_target;<br class="">
+<br class="">
+      /* Inline resolve samples into resolve target resource, then continue<br class="">
+       * the blit. */<br class="">
+      swr_do_msaa_resolve(src_resource, resolve_target);<br class="">
+<br class="">
+      /* The resolve target becomes the new source for the blit.  */<br class="">
+      info.src.resource = resolve_target;<br class="">
   }<br class="">
<br class="">
   if (util_try_blit_via_copy_region(pipe, &info)) {<br class="">
diff --git a/src/gallium/drivers/swr/swr_context.h b/src/gallium/drivers/swr/swr_context.h<br class="">
index be65a20..233d95a 100644<br class="">
--- a/src/gallium/drivers/swr/swr_context.h<br class="">
+++ b/src/gallium/drivers/swr/swr_context.h<br class="">
@@ -196,4 +196,7 @@ void swr_clear_init(struct pipe_context *pipe);<br class="">
void swr_draw_init(struct pipe_context *pipe);<br class="">
<br class="">
void swr_finish(struct pipe_context *pipe);<br class="">
+<br class="">
+void swr_do_msaa_resolve(struct pipe_resource *src_resource,<br class="">
+                         struct pipe_resource *dst_resource);<br class="">
#endif<br class="">
diff --git a/src/gallium/drivers/swr/swr_resource.h b/src/gallium/drivers/swr/swr_resource.h<br class="">
index 41abd77..ae9954c 100644<br class="">
--- a/src/gallium/drivers/swr/swr_resource.h<br class="">
+++ b/src/gallium/drivers/swr/swr_resource.h<br class="">
@@ -46,6 +46,10 @@ struct swr_resource {<br class="">
<br class="">
   struct sw_displaytarget *display_target;<br class="">
<br class="">
+   /* If resource is multisample, then this points to a alternate resource<br class="">
+    * containing the resolved multisample surface, otherwise null */<br class="">
+   struct pipe_resource *resolve_target;<br class="">
+<br class="">
   size_t mip_offsets[PIPE_MAX_TEXTURE_LEVELS];<br class="">
   size_t secondary_mip_offsets[PIPE_MAX_TEXTURE_LEVELS];<br class="">
<br class="">
diff --git a/src/gallium/drivers/swr/swr_screen.cpp b/src/gallium/drivers/swr/swr_screen.cpp<br class="">
index 87fd898..f88989b 100644<br class="">
--- a/src/gallium/drivers/swr/swr_screen.cpp<br class="">
+++ b/src/gallium/drivers/swr/swr_screen.cpp<br class="">
@@ -61,6 +61,11 @@<br class="">
#define SWR_MAX_TEXTURE_CUBE_LEVELS 14  /* 8K x 8K for now */<br class="">
#define SWR_MAX_TEXTURE_ARRAY_LAYERS 512 /* 8K x 512 / 8K x 8K x 512 */<br class="">
<br class="">
+/* Flag indicates creation of alternate surface, to prevent recursive loop<br class="">
+ * in resource creation when msaa_force_enable is set. */<br class="">
+#define SWR_RESOURCE_FLAG_ALT_SURFACE (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)<br class="">
+<br class="">
+<br class="">
static const char *<br class="">
swr_get_name(struct pipe_screen *screen)<br class="">
{<br class="">
@@ -78,13 +83,14 @@ swr_get_vendor(struct pipe_screen *screen)<br class="">
}<br class="">
<br class="">
static boolean<br class="">
-swr_is_format_supported(struct pipe_screen *screen,<br class="">
+swr_is_format_supported(struct pipe_screen *_screen,<br class="">
                        enum pipe_format format,<br class="">
                        enum pipe_texture_target target,<br class="">
                        unsigned sample_count,<br class="">
                        unsigned bind)<br class="">
{<br class="">
-   struct sw_winsys *winsys = swr_screen(screen)->winsys;<br class="">
+   struct swr_screen *screen = swr_screen(_screen);<br class="">
+   struct sw_winsys *winsys = screen->winsys;<br class="">
   const struct util_format_description *format_desc;<br class="">
<br class="">
   assert(target == PIPE_BUFFER || target == PIPE_TEXTURE_1D<br class="">
@@ -100,7 +106,8 @@ swr_is_format_supported(struct pipe_screen *screen,<br class="">
   if (!format_desc)<br class="">
      return FALSE;<br class="">
<br class="">
-   if (sample_count > 1)<br class="">
+   if ((sample_count > screen->msaa_max_count)<br class="">
+      || !util_is_power_of_two(sample_count))<br class="">
      return FALSE;<br class="">
<br class="">
   if (bind & PIPE_BIND_DISPLAY_TARGET) {<br class="">
@@ -235,7 +242,6 @@ swr_get_param(struct pipe_screen *screen, enum pipe_cap param)<br class="">
   case PIPE_CAP_QUERY_TIMESTAMP:<br class="">
   case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:<br class="">
   case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:<br class="">
-   case PIPE_CAP_FAKE_SW_MSAA:<br class="">
   case PIPE_CAP_DRAW_INDIRECT:<br class="">
   case PIPE_CAP_UMA:<br class="">
   case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:<br class="">
@@ -250,6 +256,15 @@ swr_get_param(struct pipe_screen *screen, enum pipe_cap param)<br class="">
   case PIPE_CAP_DOUBLES:<br class="">
      return 1;<br class="">
<br class="">
+   /* MSAA support<br class="">
+    * If user has explicitly set max_sample_count = 0 (via SWR_MSAA_MAX_COUNT)<br class="">
+    * then disable all MSAA support and go back to old caps. */<br class="">
+   case PIPE_CAP_TEXTURE_MULTISAMPLE:<br class="">
+   case PIPE_CAP_MULTISAMPLE_Z_RESOLVE:<br class="">
+      return swr_screen(screen)->msaa_max_count ? 1 : 0;<br class="">
+   case PIPE_CAP_FAKE_SW_MSAA:<br class="">
+      return swr_screen(screen)->msaa_max_count ? 0 : 1;<br class="">
+<br class="">
      /* unsupported features */<br class="">
   case PIPE_CAP_ANISOTROPIC_FILTER:<br class="">
   case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:<br class="">
@@ -264,7 +279,6 @@ swr_get_param(struct pipe_screen *screen, enum pipe_cap param)<br class="">
   case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY:<br class="">
   case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY:<br class="">
   case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY:<br class="">
-   case PIPE_CAP_TEXTURE_MULTISAMPLE:<br class="">
   case PIPE_CAP_TGSI_TEXCOORD:<br class="">
   case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:<br class="">
   case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:<br class="">
@@ -276,7 +290,6 @@ swr_get_param(struct pipe_screen *screen, enum pipe_cap param)<br class="">
   case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:<br class="">
   case PIPE_CAP_SAMPLER_VIEW_TARGET:<br class="">
   case PIPE_CAP_VERTEXID_NOBASE:<br class="">
-   case PIPE_CAP_MULTISAMPLE_Z_RESOLVE:<br class="">
   case PIPE_CAP_RESOURCE_FROM_USER_MEMORY:<br class="">
   case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:<br class="">
   case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:<br class="">
@@ -712,6 +725,14 @@ swr_texture_layout(struct swr_screen *screen,<br class="">
   if (pt->bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)) {<br class="">
      res->swr.halign = KNOB_MACROTILE_X_DIM;<br class="">
      res->swr.valign = KNOB_MACROTILE_Y_DIM;<br class="">
+<br class="">
+      /* If SWR_MSAA_FORCE_ENABLE is set, turn on MSAA and override requested<br class="">
+       * surface sample count. */<br class="">
+      if (screen->msaa_force_enable) {<br class="">
+         res->swr.numSamples = screen->msaa_max_count;<br class="">
+         fprintf(stderr,"swr_texture_layout: forcing sample count: %d\n",<br class="">
+                 res->swr.numSamples);<br class="">
+      }<br class="">
   } else {<br class="">
      res->swr.halign = 1;<br class="">
      res->swr.valign = 1;<br class="">
@@ -790,8 +811,8 @@ swr_texture_layout(struct swr_screen *screen,<br class="">
         ComputeSurfaceOffset<false>(0, 0, 0, 0, 0, level, &res->swr);<br class="">
   }<br class="">
<br class="">
-   size_t total_size =<br class="">
-      (size_t)res->swr.depth * res->swr.qpitch * res->swr.pitch;<br class="">
+   size_t total_size = res->swr.depth * res->swr.qpitch * res->swr.pitch *<br class="">
+                       res->swr.numSamples;<br class="">
   if (total_size > SWR_MAX_TEXTURE_SIZE)<br class="">
      return false;<br class="">
<br class="">
@@ -808,9 +829,11 @@ swr_texture_layout(struct swr_screen *screen,<br class="">
               ComputeSurfaceOffset<false>(0, 0, 0, 0, 0, level, &res->secondary);<br class="">
         }<br class="">
<br class="">
-         res->secondary.pBaseAddress = (uint8_t *)AlignedMalloc(<br class="">
-            res->secondary.depth * res->secondary.qpitch *<br class="">
-            res->secondary.pitch, 64);<br class="">
+         total_size = res->secondary.depth * res->secondary.qpitch *<br class="">
+                      res->secondary.pitch * res->secondary.numSamples;<br class="">
+<br class="">
+         res->secondary.pBaseAddress = (uint8_t *) AlignedMalloc(total_size,<br class="">
+                                                                 64);<br class="">
      }<br class="">
   }<br class="">
<br class="">
@@ -827,6 +850,51 @@ swr_can_create_resource(struct pipe_screen *screen,<br class="">
   return swr_texture_layout(swr_screen(screen), &res, false);<br class="">
}<br class="">
<br class="">
+/* Helper function that conditionally creates a single-sample resolve resource<br class="">
+ * and attaches it to main multisample resource. */<br class="">
+static boolean<br class="">
+swr_create_resolve_resource(struct pipe_screen *_screen,<br class="">
+                            struct swr_resource *msaa_res)<br class="">
+{<br class="">
+   struct swr_screen *screen = swr_screen(_screen);<br class="">
+<br class="">
+   /* If resource is multisample, create a single-sample resolve resource */<br class="">
+   if (msaa_res->base.nr_samples > 1 || (screen->msaa_force_enable &&<br class="">
+            !(msaa_res->base.flags & SWR_RESOURCE_FLAG_ALT_SURFACE))) {<br class="">
+<br class="">
+      /* Create a single-sample copy of the resource.  Copy the original<br class="">
+       * resource parameters and set flag to prevent recursion when re-calling<br class="">
+       * resource_create */<br class="">
+      struct pipe_resource alt_template = msaa_res->base;<br class="">
+      alt_template.nr_samples = 0;<br class="">
+      alt_template.flags |= SWR_RESOURCE_FLAG_ALT_SURFACE;<br class="">
+<br class="">
+      /* Note: Display_target is a special single-sample resource, only the<br class="">
+       * display_target has been created already. */<br class="">
+      if (msaa_res->base.bind & (PIPE_BIND_DISPLAY_TARGET | PIPE_BIND_SCANOUT<br class="">
+               | PIPE_BIND_SHARED)) {<br class="">
+         /* Allocate the multisample buffers. */<br class="">
+         if (!swr_texture_layout(screen, msaa_res, true))<br class="">
+            return false;<br class="">
+<br class="">
+         /* Alt resource will only be bound as PIPE_BIND_RENDER_TARGET<br class="">
+          * remove the DISPLAY_TARGET, SCANOUT, and SHARED bindings */<br class="">
+         alt_template.bind = PIPE_BIND_RENDER_TARGET;<br class="">
+      }<br class="">
+<br class="">
+      /* Allocate single-sample resolve surface */<br class="">
+      struct pipe_resource *alt;<br class="">
+      alt = _screen->resource_create(_screen, &alt_template);<br class="">
+      if (!alt)<br class="">
+         return false;<br class="">
+<br class="">
+      /* Attach it to the multisample resource */<br class="">
+      msaa_res->resolve_target = alt;<br class="">
+   }<br class="">
+<br class="">
+   return true; /* success */<br class="">
+}<br class="">
+<br class="">
static struct pipe_resource *<br class="">
swr_resource_create(struct pipe_screen *_screen,<br class="">
                    const struct pipe_resource *templat)<br class="">
@@ -845,7 +913,7 @@ swr_resource_create(struct pipe_screen *_screen,<br class="">
                            | PIPE_BIND_SHARED)) {<br class="">
         /* displayable surface<br class="">
          * first call swr_texture_layout without allocating to finish<br class="">
-          * filling out the SWR_SURFAE_STATE in res */<br class="">
+          * filling out the SWR_SURFACE_STATE in res */<br class="">
         swr_texture_layout(screen, res, false);<br class="">
         if (!swr_displaytarget_layout(screen, res))<br class="">
            goto fail;<br class="">
@@ -854,6 +922,12 @@ swr_resource_create(struct pipe_screen *_screen,<br class="">
         if (!swr_texture_layout(screen, res, true))<br class="">
            goto fail;<br class="">
      }<br class="">
+<br class="">
+      /* If resource was multisample, create resolve resource and attach<br class="">
+       * it to multisample resource. */<br class="">
+      if (!swr_create_resolve_resource(_screen, res))<br class="">
+            goto fail;<br class="">
+<br class="">
   } else {<br class="">
      /* other data (vertex buffer, const buffer, etc) */<br class="">
      assert(util_format_get_blocksize(templat->format) == 1);<br class="">
@@ -862,7 +936,7 @@ swr_resource_create(struct pipe_screen *_screen,<br class="">
      assert(templat->last_level == 0);<br class="">
<br class="">
      /* Easiest to just call swr_texture_layout, as it sets up<br class="">
-       * SWR_SURFAE_STATE in res */<br class="">
+       * SWR_SURFACE_STATE in res */<br class="">
      if (!swr_texture_layout(screen, res, true))<br class="">
         goto fail;<br class="">
   }<br class="">
@@ -888,9 +962,24 @@ swr_resource_destroy(struct pipe_screen *p_screen, struct pipe_resource *pt)<br class="">
      struct sw_winsys *winsys = screen->winsys;<br class="">
      winsys->displaytarget_destroy(winsys, spr->display_target);<br class="">
<br class="">
+      if (spr->swr.numSamples > 1) {<br class="">
+         /* Free an attached resolve resource */<br class="">
+         struct swr_resource *alt = swr_resource(spr->resolve_target);<br class="">
+         swr_fence_work_free(screen->flush_fence, alt->swr.pBaseAddress, true);<br class="">
+<br class="">
+         /* Free multisample buffer */<br class="">
+         swr_fence_work_free(screen->flush_fence, spr->swr.pBaseAddress, true);<br class="">
+      }<br class="">
   } else {<br class="">
      /* For regular resources, defer deletion */<br class="">
      swr_resource_unused(pt);<br class="">
+<br class="">
+      if (spr->swr.numSamples > 1) {<br class="">
+         /* Free an attached resolve resource */<br class="">
+         struct swr_resource *alt = swr_resource(spr->resolve_target);<br class="">
+         swr_fence_work_free(screen->flush_fence, alt->swr.pBaseAddress, true);<br class="">
+      }<br class="">
+<br class="">
      swr_fence_work_free(screen->flush_fence, spr->swr.pBaseAddress, true);<br class="">
      swr_fence_work_free(screen->flush_fence,<br class="">
                          spr->secondary.pBaseAddress, true);<br class="">
@@ -919,6 +1008,23 @@ swr_flush_frontbuffer(struct pipe_screen *p_screen,<br class="">
      SwrEndFrame(swr_context(pipe)->swrContext);<br class="">
   }<br class="">
<br class="">
+   /* Multisample surfaces need to be resolved before present */<br class="">
+   if (pipe && spr->swr.numSamples > 1) {<br class="">
+      struct pipe_resource *resolve_target = spr->resolve_target;<br class="">
+<br class="">
+      /* Do an inline surface resolve into the resolve target resource<br class="">
+       * XXX: This works, just not optimal. Work on using a pipelined blit. */<br class="">
+      swr_do_msaa_resolve(resource, resolve_target);<br class="">
+<br class="">
+      /* Once resolved, copy into display target */<br class="">
+      SWR_SURFACE_STATE *resolve = &swr_resource(resolve_target)->swr;<br class="">
+<br class="">
+      void *map = winsys->displaytarget_map(winsys, spr->display_target,<br class="">
+                                            PIPE_TRANSFER_WRITE);<br class="">
+      memcpy(map, resolve->pBaseAddress, resolve->pitch * resolve->height);<br class="">
+      winsys->displaytarget_unmap(winsys, spr->display_target);<br class="">
+   }<br class="">
+<br class="">
   debug_assert(spr->display_target);<br class="">
   if (spr->display_target)<br class="">
      winsys->displaytarget_display(<br class="">
@@ -986,6 +1092,33 @@ swr_create_screen_internal(struct sw_winsys *winsys)<br class="">
<br class="">
   util_format_s3tc_init();<br class="">
<br class="">
+   /* XXX msaa under development, disable by default for now */<br class="">
+   screen->msaa_max_count = 0; /* was SWR_MAX_NUM_MULTISAMPLES; */<br class="">
+<br class="">
+   /* validate env override values, within range and power of 2 */<br class="">
+   int msaa_max_count = debug_get_num_option("SWR_MSAA_MAX_COUNT", 0);<br class="">
+   if (msaa_max_count) {<br class="">
+      if ((msaa_max_count < 0) || (msaa_max_count > SWR_MAX_NUM_MULTISAMPLES)<br class="">
+            || !util_is_power_of_two(msaa_max_count)) {<br class="">
+         fprintf(stderr, "SWR_MSAA_MAX_COUNT invalid: %d\n", msaa_max_count);<br class="">
+         fprintf(stderr, "must be power of 2 between 1 and %d" \<br class="">
+                         " (or 0 to disable msaa)\n",<br class="">
+               SWR_MAX_NUM_MULTISAMPLES);<br class="">
+         msaa_max_count = 0;<br class="">
+      }<br class="">
+<br class="">
+      fprintf(stderr, "SWR_MSAA_MAX_COUNT: %d\n", msaa_max_count);<br class="">
+      if (!msaa_max_count)<br class="">
+         fprintf(stderr, "(msaa disabled)\n");<br class="">
+<br class="">
+      screen->msaa_max_count = msaa_max_count;<br class="">
+   }<br class="">
+<br class="">
+   screen->msaa_force_enable = debug_get_bool_option(<br class="">
+         "SWR_MSAA_FORCE_ENABLE", false);<br class="">
+   if (screen->msaa_force_enable)<br class="">
+      fprintf(stderr, "SWR_MSAA_FORCE_ENABLE: true\n");<br class="">
+<br class="">
   return &screen->base;<br class="">
}<br class="">
<br class="">
diff --git a/src/gallium/drivers/swr/swr_screen.h b/src/gallium/drivers/swr/swr_screen.h<br class="">
index 0c82a2e..dc1bb47 100644<br class="">
--- a/src/gallium/drivers/swr/swr_screen.h<br class="">
+++ b/src/gallium/drivers/swr/swr_screen.h<br class="">
@@ -24,10 +24,15 @@<br class="">
#ifndef SWR_SCREEN_H<br class="">
#define SWR_SCREEN_H<br class="">
<br class="">
+#include "swr_resource.h"<br class="">
+<br class="">
#include "pipe/p_screen.h"<br class="">
#include "pipe/p_defines.h"<br class="">
+#include "util/u_format.h"<br class="">
#include "api.h"<br class="">
<br class="">
+#include "memory/TilingFunctions.h"<br class="">
+<br class="">
struct sw_winsys;<br class="">
<br class="">
struct swr_screen {<br class="">
@@ -38,6 +43,9 @@ struct swr_screen {<br class="">
<br class="">
   struct sw_winsys *winsys;<br class="">
<br class="">
+   boolean msaa_force_enable;<br class="">
+   uint8_t msaa_max_count;<br class="">
+<br class="">
   HANDLE hJitMgr;<br class="">
};<br class="">
<br class="">
diff --git a/src/gallium/drivers/swr/swr_state.cpp b/src/gallium/drivers/swr/swr_state.cpp<br class="">
index 5cc01dd..aa9fe09 100644<br class="">
--- a/src/gallium/drivers/swr/swr_state.cpp<br class="">
+++ b/src/gallium/drivers/swr/swr_state.cpp<br class="">
@@ -30,6 +30,7 @@<br class="">
#include "common/os.h"<br class="">
#include "jit_api.h"<br class="">
#include "gen_state_llvm.h"<br class="">
+#include "core/multisample.h"<br class="">
<br class="">
#include "gallivm/lp_bld_tgsi.h"<br class="">
#include "util/u_format.h"<br class="">
@@ -668,6 +669,9 @@ swr_set_framebuffer_state(struct pipe_context *pipe,<br class="">
   if (changed) {<br class="">
      util_copy_framebuffer_state(&ctx->framebuffer, fb);<br class="">
<br class="">
+      /* 0 and 1 both indicate no msaa.  Core doesn't understand 0 samples */<br class="">
+      ctx->framebuffer.samples = std::max((ubyte)1, ctx->framebuffer.samples);<br class="">
+<br class="">
      ctx->dirty |= SWR_NEW_FRAMEBUFFER;<br class="">
   }<br class="">
}<br class="">
@@ -685,6 +689,36 @@ swr_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)<br class="">
}<br class="">
<br class="">
/*<br class="">
+ * MSAA fixed sample position table<br class="">
+ * used by update_derived and get_sample_position<br class="">
+ * (integer locations on a 16x16 grid)<br class="">
+ */<br class="">
+static const uint8_t swr_sample_positions[][2] =<br class="">
+{ /* 1x*/ { 8, 8},<br class="">
+  /* 2x*/ {12,12},{ 4, 4},<br class="">
+  /* 4x*/ { 6, 2},{14, 6},{ 2,10},{10,14},<br class="">
+  /* 8x*/ { 9, 5},{ 7,11},{13, 9},{ 5, 3},<br class="">
+          { 3,13},{ 1, 7},{11,15},{15, 1},<br class="">
+  /*16x*/ { 9, 9},{ 7, 5},{ 5,10},{12, 7},<br class="">
+          { 3, 6},{10,13},{13,11},{11, 3},<br class="">
+          { 6,14},{ 8, 1},{ 4, 2},{ 2,12},<br class="">
+          { 0, 8},{15, 4},{14,15},{ 1, 0} };<br class="">
+<br class="">
+static void<br class="">
+swr_get_sample_position(struct pipe_context *pipe,<br class="">
+                        unsigned sample_count, unsigned sample_index,<br class="">
+                        float *out_value)<br class="">
+{<br class="">
+   /* validate sample_count */<br class="">
+   sample_count = GetNumSamples(GetSampleCount(sample_count));<br class="">
+<br class="">
+   const uint8_t *sample = swr_sample_positions[sample_count-1 + sample_index];<br class="">
+   out_value[0] = sample[0] / 16.0f;<br class="">
+   out_value[1] = sample[1] / 16.0f;<br class="">
+}<br class="">
+<br class="">
+<br class="">
+/*<br class="">
 * Update resource in-use status<br class="">
 * All resources bound to color or depth targets marked as WRITE resources.<br class="">
 * VBO Vertex/index buffers and texture views marked as READ resources.<br class="">
@@ -1060,9 +1094,30 @@ swr_update_derived(struct pipe_context *pipe,<br class="">
      rastState->pointSpriteTopOrigin =<br class="">
         rasterizer->sprite_coord_mode == PIPE_SPRITE_COORD_UPPER_LEFT;<br class="">
<br class="">
-      /* XXX TODO: Add multisample */<br class="">
-      rastState->sampleCount = SWR_MULTISAMPLE_1X;<br class="">
+      /* If SWR_MSAA_FORCE_ENABLE is set, turn msaa on */<br class="">
+      if (screen->msaa_force_enable && !rasterizer->multisample) {<br class="">
+         /* Force enable and use the value the surface was created with */<br class="">
+         rasterizer->multisample = true;<br class="">
+         fb->samples = swr_resource(fb->cbufs[0]->texture)->swr.numSamples;<br class="">
+         fprintf(stderr,"msaa force enable: %d samples\n", fb->samples);<br class="">
+      }<br class="">
+<br class="">
+      rastState->sampleCount = GetSampleCount(fb->samples);<br class="">
      rastState->forcedSampleCount = false;<br class="">
+      rastState->bIsCenterPattern = !rasterizer->multisample;<br class="">
+      rastState->pixelLocation = SWR_PIXEL_LOCATION_CENTER;<br class="">
+<br class="">
+      /* Only initialize sample positions if msaa is enabled */<br class="">
+      if (rasterizer->multisample) {<br class="">
+         for (uint32_t i = 0; i < fb->samples; i++) {<br class="">
+            const uint8_t *sample = swr_sample_positions[fb->samples-1 + i];<br class="">
+            rastState->samplePositions.SetXi(i, sample[0] << 4);<br class="">
+            rastState->samplePositions.SetYi(i, sample[1] << 4);<br class="">
+            rastState->samplePositions.SetX (i, sample[0] / 16.0f);<br class="">
+            rastState->samplePositions.SetY (i, sample[1] / 16.0f);<br class="">
+         }<br class="">
+         rastState->samplePositions.PrecalcSampleData(fb->samples);<br class="">
+      }<br class="">
<br class="">
      bool do_offset = false;<br class="">
      switch (rasterizer->fill_front) {<br class="">
@@ -1375,9 +1430,9 @@ swr_update_derived(struct pipe_context *pipe,<br class="">
      psState.inputCoverage = SWR_INPUT_COVERAGE_NORMAL;<br class="">
      psState.writesODepth = ctx->fs->info.base.writes_z;<br class="">
      psState.usesSourceDepth = ctx->fs->info.base.reads_z;<br class="">
-      psState.shadingRate = SWR_SHADING_RATE_PIXEL; // XXX<br class="">
+      psState.shadingRate = SWR_SHADING_RATE_PIXEL;<br class="">
      psState.numRenderTargets = ctx->framebuffer.nr_cbufs;<br class="">
-      psState.posOffset = SWR_PS_POSITION_SAMPLE_NONE; // XXX msaa<br class="">
+      psState.posOffset = SWR_PS_POSITION_SAMPLE_NONE;<br class="">
      uint32_t barycentricsMask = 0;<br class="">
#if 0<br class="">
      // when we switch to mesa-master<br class="">
@@ -1507,6 +1562,7 @@ swr_update_derived(struct pipe_context *pipe,<br class="">
<br class="">
   /* Blend State */<br class="">
   if (ctx->dirty & (SWR_NEW_BLEND |<br class="">
+                     SWR_NEW_RASTERIZER |<br class="">
                     SWR_NEW_FRAMEBUFFER |<br class="">
                     SWR_NEW_DEPTH_STENCIL_ALPHA)) {<br class="">
      struct pipe_framebuffer_state *fb = &ctx->framebuffer;<br class="">
@@ -1520,9 +1576,8 @@ swr_update_derived(struct pipe_context *pipe,<br class="">
      blendState.alphaTestReference =<br class="">
         *((uint32_t*)&ctx->depth_stencil->alpha.ref_value);<br class="">
<br class="">
-      // XXX MSAA<br class="">
-      blendState.sampleMask = 0;<br class="">
-      blendState.sampleCount = SWR_MULTISAMPLE_1X;<br class="">
+      blendState.sampleMask = ctx->sample_mask;<br class="">
+      blendState.sampleCount = GetSampleCount(fb->samples);<br class="">
<br class="">
      /* If there are no color buffers bound, disable writes on RT0<br class="">
       * and skip loop */<br class="">
@@ -1578,8 +1633,8 @@ swr_update_derived(struct pipe_context *pipe,<br class="">
                compileState.blendState.alphaBlendFunc);<br class="">
            compileState.desc.alphaToCoverageEnable =<br class="">
               ctx->blend->pipe.alpha_to_coverage;<br class="">
-            compileState.desc.sampleMaskEnable = 0; // XXX<br class="">
-            compileState.desc.numSamples = 1; // XXX<br class="">
+            compileState.desc.sampleMaskEnable = (blendState.sampleMask != 0);<br class="">
+            compileState.desc.numSamples = fb->samples;<br class="">
<br class="">
            compileState.alphaTestFunction =<br class="">
               swr_convert_depth_func(ctx->depth_stencil->alpha.func);<br class="">
@@ -1781,6 +1836,7 @@ swr_state_init(struct pipe_context *pipe)<br class="">
   pipe->set_stencil_ref = swr_set_stencil_ref;<br class="">
<br class="">
   pipe->set_sample_mask = swr_set_sample_mask;<br class="">
+   pipe->get_sample_position = swr_get_sample_position;<br class="">
<br class="">
   pipe->create_stream_output_target = swr_create_so_target;<br class="">
   pipe->stream_output_target_destroy = swr_destroy_so_target;<br class="">
-- <br class="">
2.7.4<br class="">
<br class="">
_______________________________________________<br class="">
mesa-dev mailing list<br class="">
<a href="mailto:mesa-dev@lists.freedesktop.org" class="">mesa-dev@lists.freedesktop.org</a><br class="">
https://lists.freedesktop.org/mailman/listinfo/mesa-dev<br class="">
</div>
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>