1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**************************************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software.
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/* Author:
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *    Keith Whitwell <keith@tungstengraphics.com>
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_defines.h"
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_screen.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "draw/draw_context.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_flush.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_context.h"
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_state.h"
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_tile_cache.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "sp_tex_tile_cache.h"
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_memory.h"
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgsoftpipe_flush( struct pipe_context *pipe,
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                unsigned flags,
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                struct pipe_fence_handle **fence )
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct softpipe_context *softpipe = softpipe_context(pipe);
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   uint i;
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   draw_flush(softpipe->draw);
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (flags & SP_FLUSH_TEXTURE_CACHE) {
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      unsigned sh;
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (sh = 0; sh < Elements(softpipe->tex_cache); sh++) {
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         for (i = 0; i < softpipe->num_sampler_views[sh]; i++) {
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            sp_flush_tex_tile_cache(softpipe->tex_cache[sh][i]);
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* If this is a swapbuffers, just flush color buffers.
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * The zbuffer changes are not discarded, but held in the cache
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * in the hope that a later clear will wipe them out.
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++)
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (softpipe->cbuf_cache[i])
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         sp_flush_tile_cache(softpipe->cbuf_cache[i]);
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (softpipe->zsbuf_cache)
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      sp_flush_tile_cache(softpipe->zsbuf_cache);
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   softpipe->dirty_render_cache = FALSE;
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Need this call for hardware buffers before swapbuffers.
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * there should probably be another/different flush-type function
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * that's called before swapbuffers because we don't always want
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * to unmap surfaces when flushing.
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   softpipe_unmap_transfers(softpipe);
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Enable to dump BMPs of the color/depth buffers each frame */
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#if 0
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if(flags & PIPE_FLUSH_FRAME) {
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static unsigned frame_no = 1;
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      static char filename[256];
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      util_snprintf(filename, sizeof(filename), "cbuf_%u.bmp", frame_no);
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      debug_dump_surface_bmp(softpipe, filename, softpipe->framebuffer.cbufs[0]);
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      util_snprintf(filename, sizeof(filename), "zsbuf_%u.bmp", frame_no);
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      debug_dump_surface_bmp(softpipe, filename, softpipe->framebuffer.zsbuf);
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ++frame_no;
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#endif
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (fence)
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *fence = (void*)(intptr_t)1;
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgsoftpipe_flush_wrapped( struct pipe_context *pipe,
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        struct pipe_fence_handle **fence )
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   softpipe_flush(pipe, SP_FLUSH_TEXTURE_CACHE, fence);
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Flush context if necessary.
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Returns FALSE if it would have block, but do_not_block was set, TRUE
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * otherwise.
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TODO: move this logic to an auxiliary library?
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgboolean
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgsoftpipe_flush_resource(struct pipe_context *pipe,
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        struct pipe_resource *texture,
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        unsigned level,
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        int layer,
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        unsigned flush_flags,
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        boolean read_only,
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        boolean cpu_access,
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        boolean do_not_block)
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned referenced;
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   referenced = softpipe_is_resource_referenced(pipe, texture, level, layer);
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if ((referenced & SP_REFERENCED_FOR_WRITE) ||
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       ((referenced & SP_REFERENCED_FOR_READ) && !read_only)) {
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /*
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * TODO: The semantics of these flush flags are too obtuse. They should
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * disappear and the pipe driver should just ensure that all visible
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * side-effects happen when they need to happen.
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (referenced & SP_REFERENCED_FOR_READ)
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         flush_flags |= SP_FLUSH_TEXTURE_CACHE;
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (cpu_access) {
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /*
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * Flush and wait.
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         struct pipe_fence_handle *fence = NULL;
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (do_not_block)
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            return FALSE;
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         softpipe_flush(pipe, flush_flags, &fence);
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (fence) {
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            /*
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org             * This is for illustrative purposes only, as softpipe does not
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org             * have fences.
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org             */
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            pipe->screen->fence_finish(pipe->screen, fence,
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                       PIPE_TIMEOUT_INFINITE);
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            pipe->screen->fence_reference(pipe->screen, &fence, NULL);
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      } else {
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /*
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * Just flush.
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         softpipe_flush(pipe, flush_flags, NULL);
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return TRUE;
175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
176