1848b8605Smrg/**************************************************************************
2848b8605Smrg *
3848b8605Smrg * Copyright 2007 VMware, Inc.
4848b8605Smrg * All Rights Reserved.
5848b8605Smrg *
6848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7848b8605Smrg * copy of this software and associated documentation files (the
8848b8605Smrg * "Software"), to deal in the Software without restriction, including
9848b8605Smrg * without limitation the rights to use, copy, modify, merge, publish,
10848b8605Smrg * distribute, sub license, and/or sell copies of the Software, and to
11848b8605Smrg * permit persons to whom the Software is furnished to do so, subject to
12848b8605Smrg * the following conditions:
13848b8605Smrg *
14848b8605Smrg * The above copyright notice and this permission notice (including the
15848b8605Smrg * next paragraph) shall be included in all copies or substantial portions
16848b8605Smrg * of the Software.
17848b8605Smrg *
18848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19848b8605Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20848b8605Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21848b8605Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22848b8605Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23848b8605Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24848b8605Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25848b8605Smrg *
26848b8605Smrg **************************************************************************/
27848b8605Smrg
28848b8605Smrg/* Author:
29848b8605Smrg *    Keith Whitwell <keithw@vmware.com>
30848b8605Smrg */
31848b8605Smrg
32848b8605Smrg
33848b8605Smrg#include "pipe/p_defines.h"
34848b8605Smrg#include "pipe/p_screen.h"
35848b8605Smrg#include "draw/draw_context.h"
36848b8605Smrg#include "sp_flush.h"
37848b8605Smrg#include "sp_context.h"
38848b8605Smrg#include "sp_state.h"
39848b8605Smrg#include "sp_tile_cache.h"
40848b8605Smrg#include "sp_tex_tile_cache.h"
41b8e80941Smrg#include "util/u_debug_image.h"
42848b8605Smrg#include "util/u_memory.h"
43848b8605Smrg#include "util/u_string.h"
44848b8605Smrg
45848b8605Smrg
46848b8605Smrgvoid
47848b8605Smrgsoftpipe_flush( struct pipe_context *pipe,
48848b8605Smrg                unsigned flags,
49848b8605Smrg                struct pipe_fence_handle **fence )
50848b8605Smrg{
51848b8605Smrg   struct softpipe_context *softpipe = softpipe_context(pipe);
52848b8605Smrg   uint i;
53848b8605Smrg
54848b8605Smrg   draw_flush(softpipe->draw);
55848b8605Smrg
56848b8605Smrg   if (flags & SP_FLUSH_TEXTURE_CACHE) {
57848b8605Smrg      unsigned sh;
58848b8605Smrg
59b8e80941Smrg      for (sh = 0; sh < ARRAY_SIZE(softpipe->tex_cache); sh++) {
60848b8605Smrg         for (i = 0; i < softpipe->num_sampler_views[sh]; i++) {
61848b8605Smrg            sp_flush_tex_tile_cache(softpipe->tex_cache[sh][i]);
62848b8605Smrg         }
63848b8605Smrg      }
64848b8605Smrg   }
65848b8605Smrg
66848b8605Smrg   /* If this is a swapbuffers, just flush color buffers.
67848b8605Smrg    *
68848b8605Smrg    * The zbuffer changes are not discarded, but held in the cache
69848b8605Smrg    * in the hope that a later clear will wipe them out.
70848b8605Smrg    */
71848b8605Smrg   for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++)
72848b8605Smrg      if (softpipe->cbuf_cache[i])
73848b8605Smrg         sp_flush_tile_cache(softpipe->cbuf_cache[i]);
74848b8605Smrg
75848b8605Smrg   if (softpipe->zsbuf_cache)
76848b8605Smrg      sp_flush_tile_cache(softpipe->zsbuf_cache);
77848b8605Smrg
78848b8605Smrg   softpipe->dirty_render_cache = FALSE;
79848b8605Smrg
80848b8605Smrg   /* Enable to dump BMPs of the color/depth buffers each frame */
81848b8605Smrg#if 0
82848b8605Smrg   if (flags & PIPE_FLUSH_END_OF_FRAME) {
83848b8605Smrg      static unsigned frame_no = 1;
84848b8605Smrg      static char filename[256];
85848b8605Smrg      util_snprintf(filename, sizeof(filename), "cbuf_%u.bmp", frame_no);
86848b8605Smrg      debug_dump_surface_bmp(pipe, filename, softpipe->framebuffer.cbufs[0]);
87848b8605Smrg      util_snprintf(filename, sizeof(filename), "zsbuf_%u.bmp", frame_no);
88848b8605Smrg      debug_dump_surface_bmp(pipe, filename, softpipe->framebuffer.zsbuf);
89848b8605Smrg      ++frame_no;
90848b8605Smrg   }
91848b8605Smrg#endif
92848b8605Smrg
93848b8605Smrg   if (fence)
94848b8605Smrg      *fence = (void*)(intptr_t)1;
95848b8605Smrg}
96848b8605Smrg
97848b8605Smrgvoid
98848b8605Smrgsoftpipe_flush_wrapped(struct pipe_context *pipe,
99848b8605Smrg                       struct pipe_fence_handle **fence,
100848b8605Smrg                       unsigned flags)
101848b8605Smrg{
102848b8605Smrg   softpipe_flush(pipe, SP_FLUSH_TEXTURE_CACHE, fence);
103848b8605Smrg}
104848b8605Smrg
105848b8605Smrg
106848b8605Smrg/**
107848b8605Smrg * Flush context if necessary.
108848b8605Smrg *
109848b8605Smrg * Returns FALSE if it would have block, but do_not_block was set, TRUE
110848b8605Smrg * otherwise.
111848b8605Smrg *
112848b8605Smrg * TODO: move this logic to an auxiliary library?
113848b8605Smrg */
114848b8605Smrgboolean
115848b8605Smrgsoftpipe_flush_resource(struct pipe_context *pipe,
116848b8605Smrg                        struct pipe_resource *texture,
117848b8605Smrg                        unsigned level,
118848b8605Smrg                        int layer,
119848b8605Smrg                        unsigned flush_flags,
120848b8605Smrg                        boolean read_only,
121848b8605Smrg                        boolean cpu_access,
122848b8605Smrg                        boolean do_not_block)
123848b8605Smrg{
124848b8605Smrg   unsigned referenced;
125848b8605Smrg
126848b8605Smrg   referenced = softpipe_is_resource_referenced(pipe, texture, level, layer);
127848b8605Smrg
128848b8605Smrg   if ((referenced & SP_REFERENCED_FOR_WRITE) ||
129848b8605Smrg       ((referenced & SP_REFERENCED_FOR_READ) && !read_only)) {
130848b8605Smrg
131848b8605Smrg      /*
132848b8605Smrg       * TODO: The semantics of these flush flags are too obtuse. They should
133848b8605Smrg       * disappear and the pipe driver should just ensure that all visible
134848b8605Smrg       * side-effects happen when they need to happen.
135848b8605Smrg       */
136848b8605Smrg      if (referenced & SP_REFERENCED_FOR_READ)
137848b8605Smrg         flush_flags |= SP_FLUSH_TEXTURE_CACHE;
138848b8605Smrg
139848b8605Smrg      if (cpu_access) {
140848b8605Smrg         /*
141848b8605Smrg          * Flush and wait.
142848b8605Smrg          */
143848b8605Smrg
144848b8605Smrg         struct pipe_fence_handle *fence = NULL;
145848b8605Smrg
146848b8605Smrg         if (do_not_block)
147848b8605Smrg            return FALSE;
148848b8605Smrg
149848b8605Smrg         softpipe_flush(pipe, flush_flags, &fence);
150848b8605Smrg
151848b8605Smrg         if (fence) {
152848b8605Smrg            /*
153848b8605Smrg             * This is for illustrative purposes only, as softpipe does not
154848b8605Smrg             * have fences.
155848b8605Smrg             */
156b8e80941Smrg            pipe->screen->fence_finish(pipe->screen, NULL, fence,
157848b8605Smrg                                       PIPE_TIMEOUT_INFINITE);
158848b8605Smrg            pipe->screen->fence_reference(pipe->screen, &fence, NULL);
159848b8605Smrg         }
160848b8605Smrg      } else {
161848b8605Smrg         /*
162848b8605Smrg          * Just flush.
163848b8605Smrg          */
164848b8605Smrg
165848b8605Smrg         softpipe_flush(pipe, flush_flags, NULL);
166848b8605Smrg      }
167848b8605Smrg   }
168848b8605Smrg
169848b8605Smrg   return TRUE;
170848b8605Smrg}
171b8e80941Smrg
172b8e80941Smrgvoid softpipe_texture_barrier(struct pipe_context *pipe, unsigned flags)
173b8e80941Smrg{
174b8e80941Smrg   struct softpipe_context *softpipe = softpipe_context(pipe);
175b8e80941Smrg   uint i, sh;
176b8e80941Smrg
177b8e80941Smrg   for (sh = 0; sh < ARRAY_SIZE(softpipe->tex_cache); sh++) {
178b8e80941Smrg      for (i = 0; i < softpipe->num_sampler_views[sh]; i++) {
179b8e80941Smrg         sp_flush_tex_tile_cache(softpipe->tex_cache[sh][i]);
180b8e80941Smrg      }
181b8e80941Smrg   }
182b8e80941Smrg
183b8e80941Smrg   for (i = 0; i < softpipe->framebuffer.nr_cbufs; i++)
184b8e80941Smrg      if (softpipe->cbuf_cache[i])
185b8e80941Smrg         sp_flush_tile_cache(softpipe->cbuf_cache[i]);
186b8e80941Smrg
187b8e80941Smrg   if (softpipe->zsbuf_cache)
188b8e80941Smrg      sp_flush_tile_cache(softpipe->zsbuf_cache);
189b8e80941Smrg
190b8e80941Smrg   softpipe->dirty_render_cache = FALSE;
191b8e80941Smrg}
192b8e80941Smrg
193b8e80941Smrgvoid softpipe_memory_barrier(struct pipe_context *pipe, unsigned flags)
194b8e80941Smrg{
195b8e80941Smrg   if (!(flags & ~PIPE_BARRIER_UPDATE))
196b8e80941Smrg      return;
197b8e80941Smrg
198b8e80941Smrg   softpipe_texture_barrier(pipe, 0);
199b8e80941Smrg}
200