gallium: fix refcount bug introduced in eb20e2984
[mesa.git] / src / mesa / state_tracker / st_gen_mipmap.c
index f6af37cfac3c2999dc5423787c65bb5f701fd9eb..b9d114b1c97c982b9847628b69fac53a3cadf0f0 100644 (file)
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
 #include "pipe/p_inlines.h"
-#include "pipe/p_winsys.h"
-#include "pipe/cso_cache/cso_cache.h"
+#include "util/u_gen_mipmap.h"
+
+#include "cso_cache/cso_cache.h"
+#include "cso_cache/cso_context.h"
 
 #include "st_context.h"
 #include "st_draw.h"
 #include "st_gen_mipmap.h"
 #include "st_program.h"
-#include "st_cb_drawpixels.h"
+#include "st_texture.h"
 #include "st_cb_texture.h"
 
 
-
-static void *blend_cso = NULL;
-static void *depthstencil_cso = NULL;
-static void *rasterizer_cso = NULL;
-static void *sampler_cso = NULL;
-
-static struct st_fragment_program *stfp = NULL;
-static struct st_vertex_program *stvp = NULL;
-
-
-
-static struct st_fragment_program *
-make_tex_fragment_program(GLcontext *ctx)
-{
-   struct st_fragment_program *stfp;
-   struct gl_program *p;
-   GLuint ic = 0;
-
-   p = ctx->Driver.NewProgram(ctx, GL_FRAGMENT_PROGRAM_ARB, 0);
-   if (!p)
-      return NULL;
-
-   p->NumInstructions = 2;
-
-   p->Instructions = _mesa_alloc_instructions(p->NumInstructions);
-   if (!p->Instructions) {
-      ctx->Driver.DeleteProgram(ctx, p);
-      return NULL;
-   }
-   _mesa_init_instructions(p->Instructions, p->NumInstructions);
-
-   /* TEX result.color, fragment.texcoord[0], texture[0], 2D; */
-   p->Instructions[ic].Opcode = OPCODE_TEX;
-   p->Instructions[ic].DstReg.File = PROGRAM_OUTPUT;
-   p->Instructions[ic].DstReg.Index = FRAG_RESULT_COLR;
-   p->Instructions[ic].SrcReg[0].File = PROGRAM_INPUT;
-   p->Instructions[ic].SrcReg[0].Index = FRAG_ATTRIB_TEX0;
-   p->Instructions[ic].TexSrcUnit = 0;
-   p->Instructions[ic].TexSrcTarget = TEXTURE_2D_INDEX;
-   ic++;
-
-   /* END; */
-   p->Instructions[ic++].Opcode = OPCODE_END;
-
-   assert(ic == p->NumInstructions);
-
-   p->InputsRead = FRAG_BIT_TEX0;
-   p->OutputsWritten = (1 << FRAG_RESULT_COLR);
-
-   stfp = (struct st_fragment_program *) p;
-
-   st_translate_fragment_program(ctx->st, stfp, NULL,
-                                 stfp->tokens, ST_MAX_SHADER_TOKENS);
-
-   return stfp;
-}
-
-
-
-
 /**
  * one-time init for generate mipmap
  * XXX Note: there may be other times we need no-op/simple state like this.
@@ -115,127 +57,18 @@ make_tex_fragment_program(GLcontext *ctx)
 void
 st_init_generate_mipmap(struct st_context *st)
 {
-   struct pipe_context *pipe = st->pipe;
-   struct pipe_blend_state blend;
-   struct pipe_rasterizer_state rasterizer;
-   struct pipe_sampler_state sampler;
-   struct pipe_depth_stencil_alpha_state depthstencil;
-
-   assert(!blend_cso);
-
-   memset(&blend, 0, sizeof(blend));
-   blend.colormask = PIPE_MASK_RGBA;
-   blend_cso = pipe->create_blend_state(pipe, &blend);
-
-   memset(&depthstencil, 0, sizeof(depthstencil));
-   depthstencil_cso = pipe->create_depth_stencil_alpha_state(pipe, &depthstencil);
-
-   memset(&rasterizer, 0, sizeof(rasterizer));
-   rasterizer_cso = pipe->create_rasterizer_state(pipe, &rasterizer);
-
-   memset(&sampler, 0, sizeof(sampler));
-   sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
-   sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
-   sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
-   sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
-   sampler.min_img_filter = PIPE_TEX_FILTER_LINEAR;
-   sampler.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
-   sampler.normalized_coords = 1;
-   sampler_cso = pipe->create_sampler_state(pipe, &sampler);
-
-   stfp = make_tex_fragment_program(st->ctx);
-   stvp = st_make_passthrough_vertex_shader(st, GL_FALSE);
+   st->gen_mipmap = util_create_gen_mipmap(st->pipe, st->cso_context);
 }
 
 
 void
-st_destroy_generate_mipmpap(struct st_context *st)
-{
-   struct pipe_context *pipe = st->pipe;
-
-   pipe->delete_blend_state(pipe, blend_cso);
-   pipe->delete_depth_stencil_alpha_state(pipe, depthstencil_cso);
-   pipe->delete_rasterizer_state(pipe, rasterizer_cso);
-   pipe->delete_sampler_state(pipe, sampler_cso);
-
-   /* XXX free stfp, stvp */
-
-   blend_cso = NULL;
-   depthstencil_cso = NULL;
-   rasterizer_cso = NULL;
-   sampler_cso = NULL;
-}
-
-
-static void
-simple_viewport(struct pipe_context *pipe, uint width, uint height)
-{
-   struct pipe_viewport_state vp;
-
-   vp.scale[0] =  0.5 * width;
-   vp.scale[1] = -0.5 * height;
-   vp.scale[2] = 1.0;
-   vp.scale[3] = 1.0;
-   vp.translate[0] = 0.5 * width;
-   vp.translate[1] = 0.5 * height;
-   vp.translate[2] = 0.0;
-   vp.translate[3] = 0.0;
-
-   pipe->set_viewport_state(pipe, &vp);
-}
-
-
-
-/*
- * Draw simple [-1,1]x[-1,1] quad
- */
-static void
-draw_quad(GLcontext *ctx)
+st_destroy_generate_mipmap(struct st_context *st)
 {
-   GLfloat verts[4][2][4]; /* four verts, two attribs, XYZW */
-   GLuint i;
-   GLfloat sLeft = 0.0, sRight = 1.0;
-   GLfloat tTop = 1.0, tBot = 0.0;
-   GLfloat x0 = -1.0, x1 = 1.0;
-   GLfloat y0 = -1.0, y1 = 1.0;
-
-   /* upper-left */
-   verts[0][0][0] = x0;    /* attr[0].x */
-   verts[0][0][1] = y0;    /* attr[0].y */
-   verts[0][1][0] = sLeft; /* attr[1].s */
-   verts[0][1][1] = tTop;  /* attr[1].t */
-
-   /* upper-right */
-   verts[1][0][0] = x1;
-   verts[1][0][1] = y0;
-   verts[1][1][0] = sRight;
-   verts[1][1][1] = tTop;
-
-   /* lower-right */
-   verts[2][0][0] = x1;
-   verts[2][0][1] = y1;
-   verts[2][1][0] = sRight;
-   verts[2][1][1] = tBot;
-
-   /* lower-left */
-   verts[3][0][0] = x0;
-   verts[3][0][1] = y1;
-   verts[3][1][0] = sLeft;
-   verts[3][1][1] = tBot;
-
-   /* same for all verts: */
-   for (i = 0; i < 4; i++) {
-      verts[i][0][2] = 0.0; /*Z*/
-      verts[i][0][3] = 1.0; /*W*/
-      verts[i][1][2] = 0.0; /*R*/
-      verts[i][1][3] = 1.0; /*Q*/
-   }
-
-   st_draw_vertices(ctx, PIPE_PRIM_QUADS, 4, (float *) verts, 2, GL_TRUE);
+   util_destroy_gen_mipmap(st->gen_mipmap);
+   st->gen_mipmap = NULL;
 }
 
 
-
 /**
  * Generate mipmap levels using hardware rendering.
  * \return TRUE if successful, FALSE if not possible
@@ -247,71 +80,19 @@ st_render_mipmap(struct st_context *st,
                  uint baseLevel, uint lastLevel)
 {
    struct pipe_context *pipe = st->pipe;
-   struct pipe_framebuffer_state fb;
-   const uint face = _mesa_tex_target_to_face(target), zslice = 0;
-   const uint first_level_save = pt->first_level;
-   uint dstLevel;
+   struct pipe_screen *screen = pipe->screen;
+   const uint face = _mesa_tex_target_to_face(target);
 
    assert(target != GL_TEXTURE_3D); /* not done yet */
 
    /* check if we can render in the texture's format */
-   if (!pipe->is_format_supported(pipe, pt->format, PIPE_SURFACE)) {
+   if (!screen->is_format_supported(screen, pt->format, target,
+                                    PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
       return FALSE;
    }
 
-   /* init framebuffer state */
-   memset(&fb, 0, sizeof(fb));
-   fb.num_cbufs = 1;
-
-   /* bind CSOs */
-   pipe->bind_blend_state(pipe, blend_cso);
-   pipe->bind_depth_stencil_alpha_state(pipe, depthstencil_cso);
-   pipe->bind_rasterizer_state(pipe, rasterizer_cso);
-   pipe->bind_sampler_state(pipe, 0, sampler_cso);
-
-   /* bind shaders */
-   pipe->bind_fs_state(pipe, stfp->fs->data);
-   pipe->bind_vs_state(pipe, stvp->cso->data);
-
-   /*
-    * XXX for small mipmap levels, it may be faster to use the software
-    * fallback path...
-    */
-   for (dstLevel = baseLevel + 1; dstLevel <= lastLevel; dstLevel++) {
-      const uint srcLevel = dstLevel - 1;
-
-      /*
-       * Setup framebuffer / dest surface
-       */
-      fb.cbufs[0] = pipe->get_tex_surface(pipe, pt, face, dstLevel, zslice);
-      pipe->set_framebuffer_state(pipe, &fb);
-
-      simple_viewport(pipe, pt->width[dstLevel], pt->height[dstLevel]);
-
-      /*
-       * Setup src texture, override pt->first_level so we sample from
-       * the right mipmap level.
-       */
-      pt->first_level = srcLevel;
-      pipe->set_sampler_texture(pipe, 0, pt);
-
-      draw_quad(st->ctx);
-   }
-
-   /* restore first_level */
-   pt->first_level = first_level_save;
-
-   /* restore pipe state */
-   if (st->state.rasterizer)
-      pipe->bind_rasterizer_state(pipe, st->state.rasterizer->data);
-   if (st->state.fs)
-      pipe->bind_fs_state(pipe, st->state.fs->data);
-   if (st->state.vs)
-      pipe->bind_vs_state(pipe, st->state.vs->cso->data);
-   if (st->state.sampler[0])
-      pipe->bind_sampler_state(pipe, 0, st->state.sampler[0]->data);
-   pipe->set_sampler_texture(pipe, 0, st->state.sampler_texture[0]);
-   pipe->set_viewport_state(pipe, &st->state.viewport);
+   util_gen_mipmap(st->gen_mipmap, pt, face, baseLevel, lastLevel,
+                   PIPE_TEX_FILTER_LINEAR);
 
    return TRUE;
 }
@@ -322,7 +103,7 @@ fallback_generate_mipmap(GLcontext *ctx, GLenum target,
                          struct gl_texture_object *texObj)
 {
    struct pipe_context *pipe = ctx->st->pipe;
-   struct pipe_winsys *ws = pipe->winsys;
+   struct pipe_screen *screen = pipe->screen;
    struct pipe_texture *pt = st_get_texobj_texture(texObj);
    const uint baseLevel = texObj->BaseLevel;
    const uint lastLevel = pt->last_level;
@@ -342,26 +123,29 @@ fallback_generate_mipmap(GLcontext *ctx, GLenum target,
       const ubyte *srcData;
       ubyte *dstData;
 
-      srcSurf = pipe->get_tex_surface(pipe, pt, face, srcLevel, zslice);
-      dstSurf = pipe->get_tex_surface(pipe, pt, face, dstLevel, zslice);
+      srcSurf = screen->get_tex_surface(screen, pt, face, srcLevel, zslice,
+                                        PIPE_BUFFER_USAGE_CPU_READ);
+      dstSurf = screen->get_tex_surface(screen, pt, face, dstLevel, zslice,
+                                        PIPE_BUFFER_USAGE_CPU_WRITE);
 
-      srcData = (ubyte *) ws->buffer_map(ws, srcSurf->buffer,
-                                         PIPE_BUFFER_USAGE_CPU_READ)
+      srcData = (ubyte *) pipe_buffer_map(pipe->screen, srcSurf->buffer,
+                                          PIPE_BUFFER_USAGE_CPU_READ)
               + srcSurf->offset;
-      dstData = (ubyte *) ws->buffer_map(ws, dstSurf->buffer,
-                                         PIPE_BUFFER_USAGE_CPU_WRITE)
+      dstData = (ubyte *) pipe_buffer_map(pipe->screen, dstSurf->buffer,
+                                          PIPE_BUFFER_USAGE_CPU_WRITE)
               + dstSurf->offset;
 
-      /* XXX need to take stride/pitch info into account... */
       _mesa_generate_mipmap_level(target, datatype, comps,
                    0 /*border*/,
                    pt->width[srcLevel], pt->height[srcLevel], pt->depth[srcLevel],
+                   srcSurf->stride, /* stride in bytes */
                    srcData,
                    pt->width[dstLevel], pt->height[dstLevel], pt->depth[dstLevel],
+                   dstSurf->stride, /* stride in bytes */
                    dstData);
 
-      ws->buffer_unmap(ws, srcSurf->buffer);
-      ws->buffer_unmap(ws, dstSurf->buffer);
+      pipe_buffer_unmap(pipe->screen, srcSurf->buffer);
+      pipe_buffer_unmap(pipe->screen, dstSurf->buffer);
 
       pipe_surface_reference(&srcSurf, NULL);
       pipe_surface_reference(&dstSurf, NULL);
@@ -415,6 +199,6 @@ st_generate_mipmap(GLcontext *ctx, GLenum target,
       dstImage->TexFormat = srcImage->TexFormat;
 
       stImage = (struct st_texture_image *) dstImage;
-      stImage->pt = pt;
+      pipe_texture_reference(&stImage->pt, pt);
    }
 }