gallium: Reformat tgsi_lowering.c for the normal style.
[mesa.git] / src / gallium / auxiliary / postprocess / pp_init.c
index 75417999b7e2a03fcdcef9f96bc9b10099f4d95d..05a08304762b79ceb8c4b94b1e75e73562c10347 100644 (file)
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
- * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  *
  **************************************************************************/
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
+#include "pipe/p_compiler.h"
 
 #include "postprocess/filters.h"
+#include "postprocess/pp_private.h"
 
 #include "pipe/p_screen.h"
 #include "util/u_inlines.h"
-#include "util/u_blit.h"
 #include "util/u_math.h"
+#include "util/u_debug.h"
+#include "util/u_memory.h"
 #include "cso_cache/cso_context.h"
 
 /** Initialize the post-processing queue. */
 struct pp_queue_t *
-pp_init(struct pipe_screen *pscreen, const unsigned int *enabled)
+pp_init(struct pipe_context *pipe, const unsigned int *enabled,
+        struct cso_context *cso)
 {
-
+   unsigned int num_filters = 0;
    unsigned int curpos = 0, i, tmp_req = 0;
    struct pp_queue_t *ppq;
-   pp_func *tmp_q;
 
    pp_debug("Initializing the post-processing queue.\n");
 
    /* How many filters were requested? */
    for (i = 0; i < PP_FILTERS; i++) {
       if (enabled[i])
-         curpos++;
+         num_filters++;
    }
-   if (!curpos)
+   if (num_filters == 0)
       return NULL;
 
-   ppq = calloc(1, sizeof(struct pp_queue_t));
-   tmp_q = calloc(curpos, sizeof(pp_func));
-   ppq->shaders = calloc(curpos, sizeof(void *));
-   ppq->verts = calloc(curpos, sizeof(unsigned int));
+   ppq = CALLOC(1, sizeof(struct pp_queue_t));
+
+   if (ppq == NULL) {
+      pp_debug("Unable to allocate memory for ppq.\n");
+      goto error;
+   }
+
+   ppq->pp_queue = CALLOC(num_filters, sizeof(pp_func));
+   if (ppq->pp_queue == NULL) {
+      pp_debug("Unable to allocate memory for pp_queue.\n");
+      goto error;
+   }
+
+   ppq->shaders = CALLOC(num_filters, sizeof(void *));
+   ppq->filters = CALLOC(num_filters, sizeof(unsigned int));
 
-   if (!tmp_q || !ppq || !ppq->shaders || !ppq->verts)
+   if ((ppq->shaders == NULL) ||
+       (ppq->filters == NULL)) {
+      pp_debug("Unable to allocate memory for shaders and filter arrays.\n");
       goto error;
+   }
 
-   ppq->p = pp_init_prog(ppq, pscreen);
-   if (!ppq->p)
+   ppq->p = pp_init_prog(ppq, pipe, cso);
+   if (ppq->p == NULL) {
+      pp_debug("pp_init_prog returned NULL.\n");
       goto error;
+   }
 
    /* Add the enabled filters to the queue, in order */
    curpos = 0;
-   ppq->pp_queue = tmp_q;
    for (i = 0; i < PP_FILTERS; i++) {
       if (enabled[i]) {
          ppq->pp_queue[curpos] = pp_filters[i].main;
          tmp_req = MAX2(tmp_req, pp_filters[i].inner_tmps);
+         ppq->filters[curpos] = i;
 
          if (pp_filters[i].shaders) {
             ppq->shaders[curpos] =
-               calloc(pp_filters[i].shaders + 1, sizeof(void *));
-            ppq->verts[curpos] = pp_filters[i].verts;
-            if (!ppq->shaders[curpos])
+               CALLOC(pp_filters[i].shaders + 1, sizeof(void *));
+            if (!ppq->shaders[curpos]) {
+               pp_debug("Unable to allocate memory for shader list.\n");
                goto error;
+            }
          }
-         pp_filters[i].init(ppq, curpos, enabled[i]);
+
+         /* Call the initialization function for the filter. */
+         if (!pp_filters[i].init(ppq, curpos, enabled[i])) {
+            pp_debug("Initialization for filter %u failed.\n", i);
+            goto error;
+         }           
 
          curpos++;
       }
    }
 
-   ppq->p->blitctx = util_create_blit(ppq->p->pipe, ppq->p->cso);
-   if (!ppq->p->blitctx)
-      goto error;
-
    ppq->n_filters = curpos;
    ppq->n_tmp = (curpos > 2 ? 2 : 1);
    ppq->n_inner_tmp = tmp_req;
@@ -103,16 +121,18 @@ pp_init(struct pipe_screen *pscreen, const unsigned int *enabled)
       ppq->shaders[i][0] = ppq->p->passvs;
 
    pp_debug("Queue successfully allocated. %u filter(s).\n", curpos);
-
+   
    return ppq;
 
  error:
-   pp_debug("Error setting up pp\n");
 
-   if (ppq)
-      free(ppq->p);
-   free(ppq);
-   free(tmp_q);
+   if (ppq) {
+      /* Assign curpos, since we only need to destroy initialized filters. */
+      ppq->n_filters = curpos;
+
+      /* Call the common free function which must handle partial initialization. */
+      pp_free(ppq);
+   }
 
    return NULL;
 }
@@ -141,39 +161,77 @@ pp_free_fbos(struct pp_queue_t *ppq)
    ppq->fbos_init = false;
 }
 
-/** Free the pp queue. Called on context termination. */
+/** 
+ * Free the pp queue. Called on context termination and failure in
+ * pp_init.
+ */
 void
 pp_free(struct pp_queue_t *ppq)
 {
-
    unsigned int i, j;
 
-   pp_free_fbos(ppq);
-
-   util_destroy_blit(ppq->p->blitctx);
+   if (!ppq)
+      return;
 
-   cso_set_fragment_sampler_views(ppq->p->cso, 0, NULL);
-   cso_release_all(ppq->p->cso);
+   pp_free_fbos(ppq);
 
-   for (i = 0; i < ppq->n_filters; i++) {
-      for (j = 0; j < PP_MAX_PASSES && ppq->shaders[i][j]; j++) {
-         if (j >= ppq->verts[i]) {
-            ppq->p->pipe->delete_fs_state(ppq->p->pipe, ppq->shaders[i][j]);
-            ppq->shaders[i][j] = NULL;
-         }
-         else if (ppq->shaders[i][j] != ppq->p->passvs) {
-            ppq->p->pipe->delete_vs_state(ppq->p->pipe, ppq->shaders[i][j]);
-            ppq->shaders[i][j] = NULL;
+   if (ppq->p) {
+      if (ppq->p->pipe && ppq->filters && ppq->shaders) {
+         for (i = 0; i < ppq->n_filters; i++) {
+            unsigned int filter = ppq->filters[i];
+
+            if (ppq->shaders[i] == NULL) {
+               continue;
+            }
+
+            /*
+             * Common shader destruction code for all postprocessing
+             * filters.
+             */
+            for (j = 0; j < pp_filters[filter].shaders; j++) {
+               if (ppq->shaders[i][j] == NULL) {
+                  /* We reached the end of initialized shaders. */
+                  break;
+               }
+
+               if (ppq->shaders[i][j] == ppq->p->passvs) {
+                  continue;
+               }
+
+               assert(ppq);
+               assert(ppq->p);
+               assert(ppq->p->pipe);
+               if (j >= pp_filters[filter].verts) {
+                  assert(ppq->p->pipe->delete_fs_state);
+                  ppq->p->pipe->delete_fs_state(ppq->p->pipe,
+                                                ppq->shaders[i][j]);
+                  ppq->shaders[i][j] = NULL;
+               } else {
+                  assert(ppq->p->pipe->delete_vs_state);
+                  ppq->p->pipe->delete_vs_state(ppq->p->pipe,
+                                                ppq->shaders[i][j]);
+                  ppq->shaders[i][j] = NULL;
+               }
+            }
+
+            /* Finally call each filter type's free functionality. */
+            pp_filters[filter].free(ppq, i);
          }
       }
-   }
 
-   cso_destroy_context(ppq->p->cso);
-   ppq->p->pipe->destroy(ppq->p->pipe);
+      FREE(ppq->p);
+   }
 
-   free(ppq->p);
-   free(ppq->pp_queue);
-   free(ppq);
+   /*
+    * Handle partial initialization for common resource destruction
+    * in the create path.
+    */
+   FREE(ppq->filters);
+   FREE(ppq->shaders);
+   FREE(ppq->pp_queue);
+  
+   FREE(ppq);
 
    pp_debug("Queue taken down.\n");
 }
@@ -184,21 +242,21 @@ pp_debug(const char *fmt, ...)
 {
    va_list ap;
 
-   if (!getenv("PP_DEBUG"))
+   if (!debug_get_bool_option("PP_DEBUG", FALSE))
       return;
 
    va_start(ap, fmt);
-   vfprintf(stderr, fmt, ap);
+   _debug_vprintf(fmt, ap);
    va_end(ap);
 }
 
 /** Allocate the temp FBOs. Called on makecurrent and resize. */
 void
-pp_init_fbos(struct pp_queue_t *ppq, const unsigned int w,
-             const unsigned int h, struct pipe_resource *indepth)
+pp_init_fbos(struct pp_queue_t *ppq, unsigned int w,
+             unsigned int h)
 {
 
-   struct program *p = ppq->p;  /* The lazy will inherit the earth */
+   struct pp_program *p = ppq->p;  /* The lazy will inherit the earth */
 
    unsigned int i;
    struct pipe_resource tmp_res;
@@ -218,7 +276,7 @@ pp_init_fbos(struct pp_queue_t *ppq, const unsigned int w,
    tmp_res.depth0 = 1;
    tmp_res.array_size = 1;
    tmp_res.last_level = 0;
-   tmp_res.bind = p->surf.usage = PIPE_BIND_RENDER_TARGET;
+   tmp_res.bind = PIPE_BIND_RENDER_TARGET;
 
    if (!p->screen->is_format_supported(p->screen, tmp_res.format,
                                        tmp_res.target, 1, tmp_res.bind))
@@ -242,18 +300,14 @@ pp_init_fbos(struct pp_queue_t *ppq, const unsigned int w,
          goto error;
    }
 
-   tmp_res.format = p->surf.format = indepth->format;
-   tmp_res.bind = p->surf.usage = PIPE_BIND_DEPTH_STENCIL;
-   ppq->depth = indepth;
-   if (!ppq->depth)
-      goto error;
+   tmp_res.bind = PIPE_BIND_DEPTH_STENCIL;
 
-   tmp_res.format = p->surf.format = PIPE_FORMAT_S8_USCALED_Z24_UNORM;
+   tmp_res.format = p->surf.format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
 
    if (!p->screen->is_format_supported(p->screen, tmp_res.format,
                                        tmp_res.target, 1, tmp_res.bind)) {
 
-      tmp_res.format = p->surf.format = PIPE_FORMAT_Z24_UNORM_S8_USCALED;
+      tmp_res.format = p->surf.format = PIPE_FORMAT_Z24_UNORM_S8_UINT;
 
       if (!p->screen->is_format_supported(p->screen, tmp_res.format,
                                           tmp_res.target, 1, tmp_res.bind))
@@ -265,12 +319,11 @@ pp_init_fbos(struct pp_queue_t *ppq, const unsigned int w,
    if (!ppq->stencil || !ppq->stencils)
       goto error;
 
-
    p->framebuffer.width = w;
    p->framebuffer.height = h;
 
-   p->viewport.scale[0] = p->viewport.translate[0] = (float) w / 2.0;
-   p->viewport.scale[1] = p->viewport.translate[1] = (float) h / 2.0;
+   p->viewport.scale[0] = p->viewport.translate[0] = (float) w / 2.0f;
+   p->viewport.scale[1] = p->viewport.translate[1] = (float) h / 2.0f;
    p->viewport.scale[3] = 1.0f;
    p->viewport.translate[3] = 0.0f;