1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**********************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * obtaining a copy of this software and associated documentation
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * files (the "Software"), to deal in the Software without
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * restriction, including without limitation the rights to use, copy,
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * modify, merge, publish, distribute, sublicense, and/or sell copies
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software, and to permit persons to whom the Software is
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * furnished to do so, subject to the following conditions:
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice shall be
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * included in all copies or substantial portions of the Software.
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE.
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **********************************************************/
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "svga_cmd.h"
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "svga_debug.h"
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_defines.h"
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_pack_color.h"
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "svga_context.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "svga_state.h"
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "svga_surface.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic enum pipe_error
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgtry_clear(struct svga_context *svga,
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          unsigned buffers,
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          const union pipe_color_union *color,
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          double depth,
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          unsigned stencil)
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum pipe_error ret = PIPE_OK;
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   SVGA3dRect rect = { 0, 0, 0, 0 };
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   boolean restore_viewport = FALSE;
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   SVGA3dClearFlag flags = 0;
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_framebuffer_state *fb = &svga->curr.framebuffer;
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   union util_color uc = {0};
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ret = svga_update_state(svga, SVGA_STATE_HW_CLEAR);
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ret != PIPE_OK)
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return ret;
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (svga->rebind.rendertargets) {
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ret = svga_reemit_framebuffer_bindings(svga);
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (ret != PIPE_OK) {
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return ret;
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if ((buffers & PIPE_CLEAR_COLOR) && fb->cbufs[0]) {
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      flags |= SVGA3D_CLEAR_COLOR;
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      util_pack_color(color->f, PIPE_FORMAT_B8G8R8A8_UNORM, &uc);
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      rect.w = fb->cbufs[0]->width;
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      rect.h = fb->cbufs[0]->height;
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && fb->zsbuf) {
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (buffers & PIPE_CLEAR_DEPTH)
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         flags |= SVGA3D_CLEAR_DEPTH;
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if ((svga->curr.framebuffer.zsbuf->format == PIPE_FORMAT_S8_UINT_Z24_UNORM) &&
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          (buffers & PIPE_CLEAR_STENCIL))
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         flags |= SVGA3D_CLEAR_STENCIL;
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      rect.w = MAX2(rect.w, fb->zsbuf->width);
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      rect.h = MAX2(rect.h, fb->zsbuf->height);
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (memcmp(&rect, &svga->state.hw_clear.viewport, sizeof(rect)) != 0) {
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      restore_viewport = TRUE;
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ret = SVGA3D_SetViewport(svga->swc, &rect);
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (ret != PIPE_OK)
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return ret;
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ret = SVGA3D_ClearRect(svga->swc, flags, uc.ui, depth, stencil,
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          rect.x, rect.y, rect.w, rect.h);
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ret != PIPE_OK)
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return ret;
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (restore_viewport) {
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      memcpy(&rect, &svga->state.hw_clear.viewport, sizeof rect);
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ret = SVGA3D_SetViewport(svga->swc, &rect);
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return ret;
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Clear the given surface to the specified value.
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * No masking, no scissor (clear entire buffer).
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgsvga_clear(struct pipe_context *pipe, unsigned buffers,
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org           const union pipe_color_union *color,
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org	   double depth, unsigned stencil)
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct svga_context *svga = svga_context( pipe );
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum pipe_error ret;
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (buffers & PIPE_CLEAR_COLOR)
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      SVGA_DBG(DEBUG_DMA, "clear sid %p\n",
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               svga_surface(svga->curr.framebuffer.cbufs[0])->handle);
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* flush any queued prims (don't want them to appear after the clear!) */
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   svga_hwtnl_flush_retry(svga);
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   ret = try_clear( svga, buffers, color, depth, stencil );
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ret == PIPE_ERROR_OUT_OF_MEMORY) {
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Flush command buffer and retry:
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      svga_context_flush( svga, NULL );
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ret = try_clear( svga, buffers, color, depth, stencil );
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Mark target surfaces as dirty
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * TODO Mark only cleared surfaces.
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   svga_mark_surfaces_dirty(svga);
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert (ret == PIPE_OK);
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
139