sp_flush.c revision 25485f4b69447514ab8b595aced90c75606a99bd
193d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell/**************************************************************************
293d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell *
393d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
493d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * All Rights Reserved.
593d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell *
693d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * Permission is hereby granted, free of charge, to any person obtaining a
793d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * copy of this software and associated documentation files (the
893d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * "Software"), to deal in the Software without restriction, including
993d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * without limitation the rights to use, copy, modify, merge, publish,
1093d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * distribute, sub license, and/or sell copies of the Software, and to
1193d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * permit persons to whom the Software is furnished to do so, subject to
1293d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * the following conditions:
1393d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell *
1493d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * The above copyright notice and this permission notice (including the
1593d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * next paragraph) shall be included in all copies or substantial portions
1693d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * of the Software.
1793d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell *
1893d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1993d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2093d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
2193d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
2293d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2393d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2493d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2593d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell *
2693d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell **************************************************************************/
2793d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell
2893d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell/* Author:
2993d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell *    Keith Whitwell <keith@tungstengraphics.com>
3093d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell */
3193d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell
3293d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell
3393d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell#include "pipe/p_defines.h"
34d42b7d5f8d5eef73c89c2362beab94647ce12281Vinson Lee#include "pipe/p_screen.h"
356acd63a4980951727939c0dd545a0324965b3834José Fonseca#include "draw/draw_context.h"
3693d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell#include "sp_flush.h"
3793d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell#include "sp_context.h"
38cae640eae63544211710a2848e23f6d1d74f827fBrian#include "sp_state.h"
392b2f761e2b0dc160793be2f48e811d2d455e1e22Brian#include "sp_tile_cache.h"
4080c78472ad43f4288c9ef5076074ba9d31a39885Keith Whitwell#include "sp_tex_tile_cache.h"
4193d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell
422b2f761e2b0dc160793be2f48e811d2d455e1e22Brian
4393d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwellvoid
4493d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwellsoftpipe_flush( struct pipe_context *pipe,
454abe1eb980ed76d2b2d3383eaab520d0aa2ae6f4Michel Dänzer		unsigned flags,
464abe1eb980ed76d2b2d3383eaab520d0aa2ae6f4Michel Dänzer                struct pipe_fence_handle **fence )
4793d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell{
482b2f761e2b0dc160793be2f48e811d2d455e1e22Brian   struct softpipe_context *softpipe = softpipe_context(pipe);
492b2f761e2b0dc160793be2f48e811d2d455e1e22Brian   uint i;
502b2f761e2b0dc160793be2f48e811d2d455e1e22Brian
515e345a653b0adc59487d786050abd01d4cb8b4caBrian   draw_flush(softpipe->draw);
525e345a653b0adc59487d786050abd01d4cb8b4caBrian
53c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell   if (flags & PIPE_FLUSH_TEXTURE_CACHE) {
54843f206a342751084cbd55d9fb0fdf78fd310e99Brian Paul      for (i = 0; i < softpipe->num_fragment_sampler_views; i++) {
55c97e4532bb517e52c09ce15246423fc861c13ea3Brian Paul         sp_flush_tex_tile_cache(softpipe->fragment_tex_cache[i]);
56c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell      }
57f6106566081978f663cf08e54bb8908cb58a5316Michal Krol      for (i = 0; i < softpipe->num_vertex_sampler_views; i++) {
588a619e62bffa6f21330df747940e322909937806Michal Krol         sp_flush_tex_tile_cache(softpipe->vertex_tex_cache[i]);
598a619e62bffa6f21330df747940e322909937806Michal Krol      }
6053bd9796a1395e4acde884ff55cb7ee18586595aZack Rusin      for (i = 0; i < softpipe->num_geometry_sampler_views; i++) {
6153bd9796a1395e4acde884ff55cb7ee18586595aZack Rusin         sp_flush_tex_tile_cache(softpipe->geometry_tex_cache[i]);
6253bd9796a1395e4acde884ff55cb7ee18586595aZack Rusin      }
63c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell   }
642b2f761e2b0dc160793be2f48e811d2d455e1e22Brian
6593a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell   if (flags & PIPE_FLUSH_SWAPBUFFERS) {
6693a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell      /* If this is a swapbuffers, just flush color buffers.
6793a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell       *
6893a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell       * The zbuffer changes are not discarded, but held in the cache
6993a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell       * in the hope that a later clear will wipe them out.
7093a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell       */
712299f21f8da816fc4588492965e7dac422da1a96Zack Rusin      for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++)
72c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell         if (softpipe->cbuf_cache[i])
73aa5db684382bd8662a83ca09ed000e4a5a1013f9Keith Whitwell            sp_flush_tile_cache(softpipe->cbuf_cache[i]);
747e8396399824108d62dc3e02b2af0422e98aab8eBrian
75c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell      /* Need this call for hardware buffers before swapbuffers.
76c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell       *
77c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell       * there should probably be another/different flush-type function
78c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell       * that's called before swapbuffers because we don't always want
79c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell       * to unmap surfaces when flushing.
80c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell       */
814617981ec72f7985941bee4b03c534d97ff96bc6Michel Dänzer      softpipe_unmap_transfers(softpipe);
8293a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell   }
8393a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell   else if (flags & PIPE_FLUSH_RENDER_CACHE) {
8493a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell      for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++)
8593a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell         if (softpipe->cbuf_cache[i])
8693a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell            sp_flush_tile_cache(softpipe->cbuf_cache[i]);
8793a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell
8893a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell      if (softpipe->zsbuf_cache)
8993a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell         sp_flush_tile_cache(softpipe->zsbuf_cache);
9093a026d4baf90266f4c9cc48d039b4d65ce1ab6dKeith Whitwell
915d0cf9ebb41c05b0c6fa6914ccbb1e1871e27099José Fonseca      softpipe->dirty_render_cache = FALSE;
92c9ed86a96483063f3d6789ed16645a3dca77d726Keith Whitwell   }
934abe1eb980ed76d2b2d3383eaab520d0aa2ae6f4Michel Dänzer
94d671cf460f99693ded1eccc6b32816d430098725José Fonseca   /* Enable to dump BMPs of the color/depth buffers each frame */
95d671cf460f99693ded1eccc6b32816d430098725José Fonseca#if 0
96d671cf460f99693ded1eccc6b32816d430098725José Fonseca   if(flags & PIPE_FLUSH_FRAME) {
97d671cf460f99693ded1eccc6b32816d430098725José Fonseca      static unsigned frame_no = 1;
98d671cf460f99693ded1eccc6b32816d430098725José Fonseca      static char filename[256];
99d671cf460f99693ded1eccc6b32816d430098725José Fonseca      util_snprintf(filename, sizeof(filename), "cbuf_%u.bmp", frame_no);
100b43c182f19c6291c88420fa12714f952c2b461fbKeith Whitwell      debug_dump_surface_bmp(softpipe, filename, softpipe->framebuffer.cbufs[0]);
101d671cf460f99693ded1eccc6b32816d430098725José Fonseca      util_snprintf(filename, sizeof(filename), "zsbuf_%u.bmp", frame_no);
102b43c182f19c6291c88420fa12714f952c2b461fbKeith Whitwell      debug_dump_surface_bmp(softpipe, filename, softpipe->framebuffer.zsbuf);
103d671cf460f99693ded1eccc6b32816d430098725José Fonseca      ++frame_no;
104d671cf460f99693ded1eccc6b32816d430098725José Fonseca   }
105d671cf460f99693ded1eccc6b32816d430098725José Fonseca#endif
106d671cf460f99693ded1eccc6b32816d430098725José Fonseca
1074abe1eb980ed76d2b2d3383eaab520d0aa2ae6f4Michel Dänzer   if (fence)
1084abe1eb980ed76d2b2d3383eaab520d0aa2ae6f4Michel Dänzer      *fence = NULL;
10993d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell}
11093d5cfd8b7c641c5656aed38ad2f9559e09502f0Keith Whitwell
11153e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
11253e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca/**
11353e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca * Flush context if necessary.
11453e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca *
11553e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca * Returns FALSE if it would have block, but do_not_block was set, TRUE
11653e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca * otherwise.
11753e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca *
11853e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca * TODO: move this logic to an auxiliary library?
11953e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca */
12053e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonsecaboolean
12153e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonsecasoftpipe_flush_resource(struct pipe_context *pipe,
12253e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca                        struct pipe_resource *texture,
12353e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca                        unsigned level,
1244c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                        int layer,
12553e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca                        unsigned flush_flags,
12653e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca                        boolean read_only,
12753e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca                        boolean cpu_access,
12853e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca                        boolean do_not_block)
12953e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca{
13053e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca   unsigned referenced;
13153e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
13225485f4b69447514ab8b595aced90c75606a99bdMarek Olšák   referenced = softpipe_is_resource_referenced(pipe, texture, level, layer);
13353e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
13425485f4b69447514ab8b595aced90c75606a99bdMarek Olšák   if ((referenced & SP_REFERENCED_FOR_WRITE) ||
13525485f4b69447514ab8b595aced90c75606a99bdMarek Olšák       ((referenced & SP_REFERENCED_FOR_READ) && !read_only)) {
13653e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
13753e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca      /*
13853e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca       * TODO: The semantics of these flush flags are too obtuse. They should
13953e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca       * disappear and the pipe driver should just ensure that all visible
14053e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca       * side-effects happen when they need to happen.
14153e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca       */
14225485f4b69447514ab8b595aced90c75606a99bdMarek Olšák      if (referenced & SP_REFERENCED_FOR_WRITE)
14353e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         flush_flags |= PIPE_FLUSH_RENDER_CACHE;
14453e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
14525485f4b69447514ab8b595aced90c75606a99bdMarek Olšák      if (referenced & SP_REFERENCED_FOR_READ)
14653e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         flush_flags |= PIPE_FLUSH_TEXTURE_CACHE;
14753e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
14853e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca      if (cpu_access) {
14953e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         /*
15053e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca          * Flush and wait.
15153e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca          */
15253e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
15353e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         struct pipe_fence_handle *fence = NULL;
15453e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
15553e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         if (do_not_block)
15653e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca            return FALSE;
15753e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
15853e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         pipe->flush(pipe, flush_flags, &fence);
15953e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
16053e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         if (fence) {
16153e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca            /*
16253e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca             * This is for illustrative purposes only, as softpipe does not
16353e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca             * have fences.
16453e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca             */
165b39bccbd4ed71e9585da4cf5acf7b887b2e90899Marek Olšák            pipe->screen->fence_finish(pipe->screen, fence, 0,
166b39bccbd4ed71e9585da4cf5acf7b887b2e90899Marek Olšák                                       PIPE_TIMEOUT_INFINITE);
16753e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca            pipe->screen->fence_reference(pipe->screen, &fence, NULL);
16853e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         }
16953e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca      } else {
17053e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         /*
17153e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca          * Just flush.
17253e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca          */
17353e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
17453e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca         pipe->flush(pipe, flush_flags, NULL);
17553e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca      }
17653e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca   }
17753e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca
17853e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca   return TRUE;
17953e94bd4adb218c5974c522389c3bcf40f3fa7e8José Fonseca}
180