gallium: add pipe_resource::nr_storage_samples, and set it same as nr_samples
[mesa.git] / src / gallium / auxiliary / postprocess / pp_mlaa.c
index b61cbaafb5e9529582bd8703e23528ec2033e90a..f003ee75fd5f3420b8f07f740930b5542e80cee3 100644 (file)
@@ -43,7 +43,8 @@
 #include "postprocess/postprocess.h"
 #include "postprocess/pp_mlaa.h"
 #include "postprocess/pp_filters.h"
-#include "util/u_blit.h"
+#include "postprocess/pp_private.h"
+
 #include "util/u_box.h"
 #include "util/u_sampler.h"
 #include "util/u_inlines.h"
 static float constants[] = { 1, 1, 0, 0 };
 static unsigned int dimensions[2] = { 0, 0 };
 
-static struct pipe_resource *constbuf, *areamaptex;
-
-/** Upload the constants. */
-static void
-up_consts(struct pipe_context *pipe)
-{
-   struct pipe_box box;
-
-   u_box_2d(0, 0, sizeof(constants), 1, &box);
-   pipe->transfer_inline_write(pipe, constbuf, 0, PIPE_TRANSFER_WRITE,
-                               &box, constants, sizeof(constants),
-                               sizeof(constants));
-}
-
 /** Run function of the MLAA filter. */
 static void
 pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
                    struct pipe_resource *out, unsigned int n, bool iscolor)
 {
 
-   struct program *p = ppq->p;
+   struct pp_program *p = ppq->p;
 
    struct pipe_depth_stencil_alpha_state mstencil;
    struct pipe_sampler_view v_tmp, *arr[3];
 
-   unsigned int w = p->framebuffer.width;
-   unsigned int h = p->framebuffer.height;
+   unsigned int w = 0;
+   unsigned int h = 0;
 
    const struct pipe_stencil_ref ref = { {1} };
+
+   /* Insufficient initialization checks. */
+   assert(p);
+   assert(ppq);
+   assert(ppq->areamaptex);
+   assert(ppq->inner_tmp);
+   assert(ppq->shaders[n]);
+
+   w = p->framebuffer.width;
+   h = p->framebuffer.height;
+
    memset(&mstencil, 0, sizeof(mstencil));
+
    cso_set_stencil_ref(p->cso, &ref);
 
    /* Init the pixel size constant */
    if (dimensions[0] != p->framebuffer.width ||
        dimensions[1] != p->framebuffer.height) {
-      constants[0] = 1.0 / p->framebuffer.width;
-      constants[1] = 1.0 / p->framebuffer.height;
+      constants[0] = 1.0f / p->framebuffer.width;
+      constants[1] = 1.0f / p->framebuffer.height;
 
-      up_consts(p->pipe);
       dimensions[0] = p->framebuffer.width;
       dimensions[1] = p->framebuffer.height;
    }
 
-   pipe_set_constant_buffer(p->pipe, PIPE_SHADER_VERTEX, 0, constbuf);
-   pipe_set_constant_buffer(p->pipe, PIPE_SHADER_FRAGMENT, 0, constbuf);
+   cso_set_constant_user_buffer(p->cso, PIPE_SHADER_VERTEX,
+                                0, constants, sizeof(constants));
+   cso_set_constant_user_buffer(p->cso, PIPE_SHADER_FRAGMENT,
+                                0, constants, sizeof(constants));
 
    mstencil.stencil[0].enabled = 1;
    mstencil.stencil[0].valuemask = mstencil.stencil[0].writemask = ~0;
@@ -122,12 +122,14 @@ pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
    pp_filter_set_fb(p);
    pp_filter_misc_state(p);
    cso_set_depth_stencil_alpha(p->cso, &mstencil);
-   p->pipe->clear(p->pipe, PIPE_CLEAR_STENCIL | PIPE_CLEAR_COLOR,
+   p->pipe->clear(p->pipe, PIPE_CLEAR_STENCIL | PIPE_CLEAR_COLOR0,
                   &p->clear_color, 0, 0);
 
-   cso_single_sampler(p->cso, 0, &p->sampler_point);
-   cso_single_sampler_done(p->cso);
-   cso_set_fragment_sampler_views(p->cso, 1, &p->view);
+   {
+      const struct pipe_sampler_state *samplers[] = {&p->sampler_point};
+      cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 1, samplers);
+   }
+   cso_set_sampler_views(p->cso, PIPE_SHADER_FRAGMENT, 1, &p->view);
 
    cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]);    /* offsetvs */
    cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][2]);
@@ -142,7 +144,7 @@ pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
    mstencil.stencil[0].zpass_op = PIPE_STENCIL_OP_KEEP;
    cso_set_depth_stencil_alpha(p->cso, &mstencil);
 
-   pp_filter_setup_in(p, areamaptex);
+   pp_filter_setup_in(p, ppq->areamaptex);
    pp_filter_setup_out(p, ppq->inner_tmp[1]);
 
    u_sampler_view_default_template(&v_tmp, ppq->inner_tmp[0],
@@ -152,13 +154,14 @@ pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
 
    pp_filter_set_clear_fb(p);
 
-   cso_single_sampler(p->cso, 0, &p->sampler_point);
-   cso_single_sampler(p->cso, 1, &p->sampler_point);
-   cso_single_sampler(p->cso, 2, &p->sampler);
-   cso_single_sampler_done(p->cso);
+   {
+      const struct pipe_sampler_state *samplers[] =
+         {&p->sampler_point, &p->sampler_point, &p->sampler};
+      cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 3, samplers);
+   }
 
    arr[0] = p->view;
-   cso_set_fragment_sampler_views(p->cso, 3, arr);
+   cso_set_sampler_views(p->cso, PIPE_SHADER_FRAGMENT, 3, arr);
 
    cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][0]);    /* passvs */
    cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][3]);
@@ -176,19 +179,21 @@ pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
    pp_filter_set_fb(p);
 
    /* Blit the input to the output */
-   util_blit_pixels(p->blitctx, in, 0, 0, 0,
-                    w, h, 0, p->framebuffer.cbufs[0],
-                    0, 0, w, h, 0, PIPE_TEX_MIPFILTER_NEAREST);
+   pp_blit(p->pipe, in, 0, 0,
+           w, h, 0, p->framebuffer.cbufs[0],
+           0, 0, w, h);
 
    u_sampler_view_default_template(&v_tmp, in, in->format);
    arr[0] = p->pipe->create_sampler_view(p->pipe, in, &v_tmp);
 
-   cso_single_sampler(p->cso, 0, &p->sampler_point);
-   cso_single_sampler(p->cso, 1, &p->sampler_point);
-   cso_single_sampler_done(p->cso);
+   {
+      const struct pipe_sampler_state *samplers[] =
+         {&p->sampler_point, &p->sampler_point};
+      cso_set_samplers(p->cso, PIPE_SHADER_FRAGMENT, 2, samplers);
+   }
 
    arr[1] = p->view;
-   cso_set_fragment_sampler_views(p->cso, 2, arr);
+   cso_set_sampler_views(p->cso, PIPE_SHADER_FRAGMENT, 2, arr);
 
    cso_set_vertex_shader_handle(p->cso, ppq->shaders[n][1]);    /* offsetvs */
    cso_set_fragment_shader_handle(p->cso, ppq->shaders[n][4]);
@@ -205,32 +210,25 @@ pp_jimenezmlaa_run(struct pp_queue_t *ppq, struct pipe_resource *in,
 }
 
 /** The init function of the MLAA filter. */
-static void
+static bool
 pp_jimenezmlaa_init_run(struct pp_queue_t *ppq, unsigned int n,
                         unsigned int val, bool iscolor)
 {
 
    struct pipe_box box;
    struct pipe_resource res;
-   char *tmp_text;
-
-   constbuf = pipe_buffer_create(ppq->p->screen, PIPE_BIND_CONSTANT_BUFFER,
-                                 PIPE_USAGE_STATIC, sizeof(constants));
-   if (!constbuf) {
-      pp_debug("Failed to allocate constant buffer\n");
-      return;
-   }
-
-
-   pp_debug("mlaa: using %u max search steps\n", val);
+   char *tmp_text = NULL;
 
    tmp_text = CALLOC(sizeof(blend2fs_1) + sizeof(blend2fs_2) +
                      IMM_SPACE, sizeof(char));
 
    if (!tmp_text) {
       pp_debug("Failed to allocate shader space\n");
-      return;
+      return FALSE;
    }
+
+   pp_debug("mlaa: using %u max search steps\n", val);
+
    util_sprintf(tmp_text, "%s"
                 "IMM FLT32 {    %.8f,     0.0000,     0.0000,     0.0000}\n"
                 "%s\n", blend2fs_1, (float) val, blend2fs_2);
@@ -241,21 +239,25 @@ pp_jimenezmlaa_init_run(struct pp_queue_t *ppq, unsigned int n,
    res.format = PIPE_FORMAT_R8G8_UNORM;
    res.width0 = res.height0 = 165;
    res.bind = PIPE_BIND_SAMPLER_VIEW;
-   res.usage = PIPE_USAGE_STATIC;
-   res.depth0 = res.array_size = res.nr_samples = 1;
+   res.usage = PIPE_USAGE_DEFAULT;
+   res.depth0 = res.array_size = res.nr_samples = res.nr_storage_samples = 1;
 
    if (!ppq->p->screen->is_format_supported(ppq->p->screen, res.format,
                                             res.target, 1, res.bind))
       pp_debug("Areamap format not supported\n");
 
-   areamaptex = ppq->p->screen->resource_create(ppq->p->screen, &res);
+   ppq->areamaptex = ppq->p->screen->resource_create(ppq->p->screen, &res);
+   
+   if (ppq->areamaptex == NULL) {
+      pp_debug("Failed to allocate area map texture\n");
+      goto fail;
+   }
+   
    u_box_2d(0, 0, 165, 165, &box);
 
-   ppq->p->pipe->transfer_inline_write(ppq->p->pipe, areamaptex, 0,
-                                       PIPE_TRANSFER_WRITE, &box,
-                                       areamap, 165 * 2, sizeof(areamap));
-
-
+   ppq->p->pipe->texture_subdata(ppq->p->pipe, ppq->areamaptex, 0,
+                                 PIPE_TRANSFER_WRITE, &box,
+                                 areamap, 165 * 2, sizeof(areamap));
 
    ppq->shaders[n][1] = pp_tgsi_to_state(ppq->p->pipe, offsetvs, true,
                                          "offsetvs");
@@ -271,23 +273,35 @@ pp_jimenezmlaa_init_run(struct pp_queue_t *ppq, unsigned int n,
                                          "neigh3fs");
 
    FREE(tmp_text);
+
+   return TRUE;
+
+ fail:
+   
+   FREE(tmp_text);
+
+   /*
+    * Call the common free function for destruction of partially initialized
+    * resources.
+    */
+   pp_jimenezmlaa_free(ppq, n);
+
+   return FALSE;
 }
 
 /** Short wrapper to init the depth version. */
-void
+bool
 pp_jimenezmlaa_init(struct pp_queue_t *ppq, unsigned int n, unsigned int val)
 {
-
-   pp_jimenezmlaa_init_run(ppq, n, val, false);
+   return pp_jimenezmlaa_init_run(ppq, n, val, false);
 }
 
 /** Short wrapper to init the color version. */
-void
+bool
 pp_jimenezmlaa_init_color(struct pp_queue_t *ppq, unsigned int n,
                           unsigned int val)
 {
-
-   pp_jimenezmlaa_init_run(ppq, n, val, true);
+   return pp_jimenezmlaa_init_run(ppq, n, val, true);
 }
 
 /** Short wrapper to run the depth version. */
@@ -295,6 +309,9 @@ void
 pp_jimenezmlaa(struct pp_queue_t *ppq, struct pipe_resource *in,
                struct pipe_resource *out, unsigned int n)
 {
+   if (!ppq->depth) {
+      return;
+   }
    pp_jimenezmlaa_run(ppq, in, out, n, false);
 }
 
@@ -305,3 +322,15 @@ pp_jimenezmlaa_color(struct pp_queue_t *ppq, struct pipe_resource *in,
 {
    pp_jimenezmlaa_run(ppq, in, out, n, true);
 }
+
+
+/**
+ * Short wrapper to free the mlaa filter resources. Shaders are freed in
+ * the common code in pp_free.
+ */
+void
+pp_jimenezmlaa_free(struct pp_queue_t *ppq, unsigned int n)
+{
+   pipe_resource_reference(&ppq->areamaptex, NULL);
+}
+