draw: use util_pstipple_* function for stipple pattern textures and samplers
authorNicolai Hähnle <nicolai.haehnle@amd.com>
Thu, 21 Jan 2016 21:10:11 +0000 (16:10 -0500)
committerNicolai Hähnle <nicolai.haehnle@amd.com>
Tue, 9 Feb 2016 15:01:57 +0000 (10:01 -0500)
This reduces code duplication.

Suggested-by: Jose Fonseca <jfonseca@vmware.com>
Reviewed-by: Brian Paul <brianp@vmware.com>
Reviewed-by: Jose Fonseca <jfonseca@vmware.com>
src/gallium/auxiliary/draw/draw_pipe_pstipple.c
src/gallium/auxiliary/util/u_pstipple.c
src/gallium/auxiliary/util/u_pstipple.h

index e468cc3891086933bf431d64fb6ffc4a9cee9d90..0298334a28f53667a4a5f8a7d8bcec673124b6c4 100644 (file)
@@ -152,113 +152,6 @@ generate_pstip_fs(struct pstip_stage *pstip)
 }
 
 
-/**
- * Load texture image with current stipple pattern.
- */
-static void
-pstip_update_texture(struct pstip_stage *pstip)
-{
-   static const uint bit31 = 1 << 31;
-   struct pipe_context *pipe = pstip->pipe;
-   struct pipe_transfer *transfer;
-   const uint *stipple = pstip->state.stipple->stipple;
-   uint i, j;
-   ubyte *data;
-
-   data = pipe_transfer_map(pipe, pstip->texture, 0, 0,
-                                PIPE_TRANSFER_WRITE, 0, 0, 32, 32, &transfer);
-
-   /*
-    * Load alpha texture.
-    * Note: 0 means keep the fragment, 255 means kill it.
-    * We'll negate the texel value and use KILL_IF which kills if value
-    * is negative.
-    */
-   for (i = 0; i < 32; i++) {
-      for (j = 0; j < 32; j++) {
-         if (stipple[i] & (bit31 >> j)) {
-            /* fragment "on" */
-            data[i * transfer->stride + j] = 0;
-         }
-         else {
-            /* fragment "off" */
-            data[i * transfer->stride + j] = 255;
-         }
-      }
-   }
-
-   /* unmap */
-   pipe_transfer_unmap(pipe, transfer);
-}
-
-
-/**
- * Create the texture map we'll use for stippling.
- */
-static boolean
-pstip_create_texture(struct pstip_stage *pstip)
-{
-   struct pipe_context *pipe = pstip->pipe;
-   struct pipe_screen *screen = pipe->screen;
-   struct pipe_resource texTemp;
-   struct pipe_sampler_view viewTempl;
-
-   memset(&texTemp, 0, sizeof(texTemp));
-   texTemp.target = PIPE_TEXTURE_2D;
-   texTemp.format = PIPE_FORMAT_A8_UNORM; /* XXX verify supported by driver! */
-   texTemp.last_level = 0;
-   texTemp.width0 = 32;
-   texTemp.height0 = 32;
-   texTemp.depth0 = 1;
-   texTemp.array_size = 1;
-   texTemp.bind = PIPE_BIND_SAMPLER_VIEW;
-
-   pstip->texture = screen->resource_create(screen, &texTemp);
-   if (pstip->texture == NULL)
-      return FALSE;
-
-   u_sampler_view_default_template(&viewTempl,
-                                   pstip->texture,
-                                   pstip->texture->format);
-   pstip->sampler_view = pipe->create_sampler_view(pipe,
-                                                   pstip->texture,
-                                                   &viewTempl);
-   if (!pstip->sampler_view) {
-      return FALSE;
-   }
-
-   return TRUE;
-}
-
-
-/**
- * Create the sampler CSO that'll be used for stippling.
- */
-static boolean
-pstip_create_sampler(struct pstip_stage *pstip)
-{
-   struct pipe_sampler_state sampler;
-   struct pipe_context *pipe = pstip->pipe;
-
-   memset(&sampler, 0, sizeof(sampler));
-   sampler.wrap_s = PIPE_TEX_WRAP_REPEAT;
-   sampler.wrap_t = PIPE_TEX_WRAP_REPEAT;
-   sampler.wrap_r = PIPE_TEX_WRAP_REPEAT;
-   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
-   sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
-   sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
-   sampler.normalized_coords = 1;
-   sampler.min_lod = 0.0f;
-   sampler.max_lod = 0.0f;
-
-   pstip->sampler_cso = pipe->create_sampler_state(pipe, &sampler);
-   if (pstip->sampler_cso == NULL)
-      return FALSE;
-   
-   return TRUE;
-}
-
-
 /**
  * When we're about to draw our first stipple polygon in a batch, this function
  * is called to tell the driver to bind our modified fragment shader.
@@ -537,7 +430,8 @@ pstip_set_polygon_stipple(struct pipe_context *pipe,
    /* pass-through */
    pstip->driver_set_polygon_stipple(pstip->pipe, stipple);
 
-   pstip_update_texture(pstip);
+   util_pstipple_update_stipple_texture(pstip->pipe, pstip->texture,
+                                        pstip->state.stipple->stipple);
 }
 
 
@@ -573,10 +467,17 @@ draw_install_pstipple_stage(struct draw_context *draw,
    pstip->driver_set_polygon_stipple = pipe->set_polygon_stipple;
 
    /* create special texture, sampler state */
-   if (!pstip_create_texture(pstip))
+   pstip->texture = util_pstipple_create_stipple_texture(pipe, NULL);
+   if (!pstip->texture)
+      goto fail;
+
+   pstip->sampler_view = util_pstipple_create_sampler_view(pipe,
+                                                           pstip->texture);
+   if (!pstip->sampler_view)
       goto fail;
 
-   if (!pstip_create_sampler(pstip))
+   pstip->sampler_cso = util_pstipple_create_sampler(pipe);
+   if (!pstip->sampler_cso)
       goto fail;
 
    /* override the driver's functions */
index 3428172203b1c36776de48826a272a8f2b7835c7..74e6f99da67705d86d5b22a5e7db45a15915e897 100644 (file)
@@ -58,7 +58,7 @@
 #define NUM_NEW_TOKENS 53
 
 
-static void
+void
 util_pstipple_update_stipple_texture(struct pipe_context *pipe,
                                      struct pipe_resource *tex,
                                      const uint32_t pattern[32])
@@ -118,7 +118,7 @@ util_pstipple_create_stipple_texture(struct pipe_context *pipe,
 
    tex = screen->resource_create(screen, &templat);
 
-   if (tex)
+   if (tex && pattern)
       util_pstipple_update_stipple_texture(pipe, tex, pattern);
 
    return tex;
index ef8396f431819e52ee246f1a7970f177ec812b02..d1662be283942acfb6ed285f99baa4da60ac7dc7 100644 (file)
@@ -36,6 +36,11 @@ struct pipe_resource;
 struct pipe_shader_state;
 
 
+extern void
+util_pstipple_update_stipple_texture(struct pipe_context *pipe,
+                                     struct pipe_resource *tex,
+                                     const uint32_t pattern[32]);
+
 extern struct pipe_resource *
 util_pstipple_create_stipple_texture(struct pipe_context *pipe,
                                      const uint32_t pattern[32]);