1946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca/**************************************************************************
2946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
3946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * All Rights Reserved.
5946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
6946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * Permission is hereby granted, free of charge, to any person obtaining a
7946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * copy of this software and associated documentation files (the
8946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * "Software"), to deal in the Software without restriction, including
9946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * without limitation the rights to use, copy, modify, merge, publish,
10946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * distribute, sub license, and/or sell copies of the Software, and to
11946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * permit persons to whom the Software is furnished to do so, subject to
12946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * the following conditions:
13946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
14946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * The above copyright notice and this permission notice (including the
15946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * next paragraph) shall be included in all copies or substantial portions
16946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * of the Software.
17946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
18946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
26946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca **************************************************************************/
27946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
28946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca/* Author:
29946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *    Keith Whitwell <keith@tungstengraphics.com>
30946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca */
31946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
32946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
33946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca#include "pipe/p_defines.h"
34cf1e4b15a4d21342320fed0fdb19ba6211e4628bVinson Lee#include "pipe/p_screen.h"
3547ecb51eb48b91388d95f0ad6efbaa1b7d9bd9f3Brian Paul#include "util/u_string.h"
36946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca#include "draw/draw_context.h"
37946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca#include "lp_flush.h"
38946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca#include "lp_context.h"
39c1013f5d404880046f304de706d4216b08bd3011Keith Whitwell#include "lp_setup.h"
40946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
41946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
420639765b2850739af1678f10fc0c5706d5827776Brian Paul/**
43217926f350265a4d416a1bc550567c1334a37875Roland Scheidegger * \param fence  if non-null, returns pointer to a fence which can be waited on
440639765b2850739af1678f10fc0c5706d5827776Brian Paul */
45946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonsecavoid
46946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonsecallvmpipe_flush( struct pipe_context *pipe,
47b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell                struct pipe_fence_handle **fence,
48b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell                const char *reason)
49946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca{
50946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca   struct llvmpipe_context *llvmpipe = llvmpipe_context(pipe);
51946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
52946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca   draw_flush(llvmpipe->draw);
53946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
540639765b2850739af1678f10fc0c5706d5827776Brian Paul   /* ask the setup module to flush */
557e02303497237cde958c28608477d0c355a8038bMarek Olšák   lp_setup_flush(llvmpipe->setup, fence, reason);
56946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
57946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca   /* Enable to dump BMPs of the color/depth buffers each frame */
5847ecb51eb48b91388d95f0ad6efbaa1b7d9bd9f3Brian Paul   if (0) {
597e02303497237cde958c28608477d0c355a8038bMarek Olšák      static unsigned frame_no = 1;
607e02303497237cde958c28608477d0c355a8038bMarek Olšák      char filename[256];
617e02303497237cde958c28608477d0c355a8038bMarek Olšák      unsigned i;
627e02303497237cde958c28608477d0c355a8038bMarek Olšák
637e02303497237cde958c28608477d0c355a8038bMarek Olšák      for (i = 0; i < llvmpipe->framebuffer.nr_cbufs; i++) {
647e02303497237cde958c28608477d0c355a8038bMarek Olšák         util_snprintf(filename, sizeof(filename), "cbuf%u_%u", i, frame_no);
657e02303497237cde958c28608477d0c355a8038bMarek Olšák         debug_dump_surface_bmp(&llvmpipe->pipe, filename, llvmpipe->framebuffer.cbufs[i]);
667e02303497237cde958c28608477d0c355a8038bMarek Olšák      }
677e02303497237cde958c28608477d0c355a8038bMarek Olšák
687e02303497237cde958c28608477d0c355a8038bMarek Olšák      if (0) {
697e02303497237cde958c28608477d0c355a8038bMarek Olšák         util_snprintf(filename, sizeof(filename), "zsbuf_%u", frame_no);
707e02303497237cde958c28608477d0c355a8038bMarek Olšák         debug_dump_surface_bmp(&llvmpipe->pipe, filename, llvmpipe->framebuffer.zsbuf);
7116ecd2f0780c7b9123aa50d5a174c127a1e408b2Brian Paul      }
727e02303497237cde958c28608477d0c355a8038bMarek Olšák
737e02303497237cde958c28608477d0c355a8038bMarek Olšák      ++frame_no;
74946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca   }
75946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca}
76946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
77b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwellvoid
78b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwellllvmpipe_finish( struct pipe_context *pipe,
79b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell                 const char *reason )
80b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell{
81b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell   struct pipe_fence_handle *fence = NULL;
827e02303497237cde958c28608477d0c355a8038bMarek Olšák   llvmpipe_flush(pipe, &fence, reason);
83b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell   if (fence) {
84bfe88e69988b3d3bdff0b9f6051d0428e1315653Marek Olšák      pipe->screen->fence_finish(pipe->screen, fence, PIPE_TIMEOUT_INFINITE);
85b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell      pipe->screen->fence_reference(pipe->screen, &fence, NULL);
86b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell   }
87b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell}
88bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
89bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca/**
90bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca * Flush context if necessary.
91bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca *
9243b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca * Returns FALSE if it would have block, but do_not_block was set, TRUE
9343b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca * otherwise.
9443b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca *
95bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca * TODO: move this logic to an auxiliary library?
96bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca */
97bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonsecaboolean
987951630d0877fc3c293151ad6476bec7288e63c6José Fonsecallvmpipe_flush_resource(struct pipe_context *pipe,
992cd128ab443addeb6e013f80d7c3f6639a66b2f1José Fonseca                        struct pipe_resource *resource,
1007951630d0877fc3c293151ad6476bec7288e63c6José Fonseca                        unsigned level,
1014c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                        int layer,
1027951630d0877fc3c293151ad6476bec7288e63c6José Fonseca                        boolean read_only,
1037951630d0877fc3c293151ad6476bec7288e63c6José Fonseca                        boolean cpu_access,
104b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell                        boolean do_not_block,
105b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell                        const char *reason)
106bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca{
107bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca   unsigned referenced;
108bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
10925485f4b69447514ab8b595aced90c75606a99bdMarek Olšák   referenced = llvmpipe_is_resource_referenced(pipe, resource, level, layer);
110bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
11125485f4b69447514ab8b595aced90c75606a99bdMarek Olšák   if ((referenced & LP_REFERENCED_FOR_WRITE) ||
11225485f4b69447514ab8b595aced90c75606a99bdMarek Olšák       ((referenced & LP_REFERENCED_FOR_READ) && !read_only)) {
113bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
114bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca      if (cpu_access) {
115bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca         /*
116bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca          * Flush and wait.
117bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca          */
11843b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca         if (do_not_block)
11943b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca            return FALSE;
12043b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca
121b6e03eafe3311142445ca42c1574d3f6998eecc3Keith Whitwell         llvmpipe_finish(pipe, reason);
122bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca      } else {
123bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca         /*
124bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca          * Just flush.
125bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca          */
126bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
1277e02303497237cde958c28608477d0c355a8038bMarek Olšák         llvmpipe_flush(pipe, NULL, reason);
128bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca      }
129bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca   }
130bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
131bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca   return TRUE;
132bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca}
133