1bf215546Sopenharmony_ci/**********************************************************
2bf215546Sopenharmony_ci * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person
5bf215546Sopenharmony_ci * obtaining a copy of this software and associated documentation
6bf215546Sopenharmony_ci * files (the "Software"), to deal in the Software without
7bf215546Sopenharmony_ci * restriction, including without limitation the rights to use, copy,
8bf215546Sopenharmony_ci * modify, merge, publish, distribute, sublicense, and/or sell copies
9bf215546Sopenharmony_ci * of the Software, and to permit persons to whom the Software is
10bf215546Sopenharmony_ci * furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be
13bf215546Sopenharmony_ci * included in all copies or substantial portions of the Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16bf215546Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17bf215546Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18bf215546Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19bf215546Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20bf215546Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21bf215546Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci *
24bf215546Sopenharmony_ci **********************************************************/
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "util/u_framebuffer.h"
27bf215546Sopenharmony_ci#include "util/u_inlines.h"
28bf215546Sopenharmony_ci#include "util/u_pstipple.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "svga_cmd.h"
31bf215546Sopenharmony_ci#include "svga_context.h"
32bf215546Sopenharmony_ci#include "svga_screen.h"
33bf215546Sopenharmony_ci#include "svga_surface.h"
34bf215546Sopenharmony_ci#include "svga_resource_texture.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistatic void
38bf215546Sopenharmony_cisvga_set_scissor_states(struct pipe_context *pipe,
39bf215546Sopenharmony_ci                        unsigned start_slot,
40bf215546Sopenharmony_ci                        unsigned num_scissors,
41bf215546Sopenharmony_ci                        const struct pipe_scissor_state *scissors)
42bf215546Sopenharmony_ci{
43bf215546Sopenharmony_ci   ASSERTED struct svga_screen *svgascreen = svga_screen(pipe->screen);
44bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
45bf215546Sopenharmony_ci   unsigned i, num_sc;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci   assert(start_slot + num_scissors <= svgascreen->max_viewports);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci   for (i = 0, num_sc = start_slot; i < num_scissors; i++)  {
50bf215546Sopenharmony_ci      svga->curr.scissor[num_sc++] = scissors[i]; /* struct copy */
51bf215546Sopenharmony_ci   }
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_SCISSOR;
54bf215546Sopenharmony_ci}
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistatic void
58bf215546Sopenharmony_cisvga_set_polygon_stipple(struct pipe_context *pipe,
59bf215546Sopenharmony_ci                         const struct pipe_poly_stipple *stipple)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci   /* release old texture */
64bf215546Sopenharmony_ci   pipe_resource_reference(&svga->polygon_stipple.texture, NULL);
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   /* release old sampler view */
67bf215546Sopenharmony_ci   if (svga->polygon_stipple.sampler_view) {
68bf215546Sopenharmony_ci      pipe->sampler_view_destroy(pipe,
69bf215546Sopenharmony_ci                                 &svga->polygon_stipple.sampler_view->base);
70bf215546Sopenharmony_ci   }
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   /* create new stipple texture */
73bf215546Sopenharmony_ci   svga->polygon_stipple.texture =
74bf215546Sopenharmony_ci      util_pstipple_create_stipple_texture(pipe, stipple->stipple);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci   /* create new sampler view */
77bf215546Sopenharmony_ci   svga->polygon_stipple.sampler_view =
78bf215546Sopenharmony_ci      (struct svga_pipe_sampler_view *)
79bf215546Sopenharmony_ci      util_pstipple_create_sampler_view(pipe,
80bf215546Sopenharmony_ci                                        svga->polygon_stipple.texture);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   /* allocate sampler state, if first time */
83bf215546Sopenharmony_ci   if (!svga->polygon_stipple.sampler) {
84bf215546Sopenharmony_ci      svga->polygon_stipple.sampler = util_pstipple_create_sampler(pipe);
85bf215546Sopenharmony_ci   }
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_STIPPLE;
88bf215546Sopenharmony_ci}
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_civoid
92bf215546Sopenharmony_cisvga_cleanup_framebuffer(struct svga_context *svga)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   struct svga_screen *svgascreen = svga_screen(svga->pipe.screen);
95bf215546Sopenharmony_ci   struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
96bf215546Sopenharmony_ci   struct pipe_framebuffer_state *hw = &svga->state.hw_clear.framebuffer;
97bf215546Sopenharmony_ci   unsigned i;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   for (i = 0; i < svgascreen->max_color_buffers; i++) {
100bf215546Sopenharmony_ci      pipe_surface_reference(&curr->cbufs[i], NULL);
101bf215546Sopenharmony_ci      pipe_surface_reference(&hw->cbufs[i], NULL);
102bf215546Sopenharmony_ci   }
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   pipe_surface_reference(&curr->zsbuf, NULL);
105bf215546Sopenharmony_ci   pipe_surface_reference(&hw->zsbuf, NULL);
106bf215546Sopenharmony_ci}
107bf215546Sopenharmony_ci
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci#define DEPTH_BIAS_SCALE_FACTOR_D16    ((float)(1<<15))
110bf215546Sopenharmony_ci#define DEPTH_BIAS_SCALE_FACTOR_D24S8  ((float)(1<<23))
111bf215546Sopenharmony_ci#define DEPTH_BIAS_SCALE_FACTOR_D32    ((float)(1<<31))
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_cistatic void
115bf215546Sopenharmony_cisvga_set_framebuffer_state(struct pipe_context *pipe,
116bf215546Sopenharmony_ci                           const struct pipe_framebuffer_state *fb)
117bf215546Sopenharmony_ci{
118bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
119bf215546Sopenharmony_ci   struct pipe_framebuffer_state *dst = &svga->curr.framebuffer;
120bf215546Sopenharmony_ci   unsigned i;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci   /* make sure any pending drawing calls are flushed before changing
123bf215546Sopenharmony_ci    * the framebuffer state
124bf215546Sopenharmony_ci    */
125bf215546Sopenharmony_ci   svga_hwtnl_flush_retry(svga);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   dst->width = fb->width;
128bf215546Sopenharmony_ci   dst->height = fb->height;
129bf215546Sopenharmony_ci   dst->nr_cbufs = fb->nr_cbufs;
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   /* Check that all surfaces are the same size.
132bf215546Sopenharmony_ci    * Actually, the virtual hardware may support rendertargets with
133bf215546Sopenharmony_ci    * different size, depending on the host API and driver,
134bf215546Sopenharmony_ci    */
135bf215546Sopenharmony_ci   {
136bf215546Sopenharmony_ci      int width = 0, height = 0;
137bf215546Sopenharmony_ci      if (fb->zsbuf) {
138bf215546Sopenharmony_ci         width = fb->zsbuf->width;
139bf215546Sopenharmony_ci         height = fb->zsbuf->height;
140bf215546Sopenharmony_ci      }
141bf215546Sopenharmony_ci      for (i = 0; i < fb->nr_cbufs; ++i) {
142bf215546Sopenharmony_ci         if (fb->cbufs[i]) {
143bf215546Sopenharmony_ci            if (width && height) {
144bf215546Sopenharmony_ci               if (fb->cbufs[i]->width != width ||
145bf215546Sopenharmony_ci                   fb->cbufs[i]->height != height) {
146bf215546Sopenharmony_ci                  debug_warning("Mixed-size color and depth/stencil surfaces "
147bf215546Sopenharmony_ci                                "may not work properly");
148bf215546Sopenharmony_ci               }
149bf215546Sopenharmony_ci            }
150bf215546Sopenharmony_ci            else {
151bf215546Sopenharmony_ci               width = fb->cbufs[i]->width;
152bf215546Sopenharmony_ci               height = fb->cbufs[i]->height;
153bf215546Sopenharmony_ci            }
154bf215546Sopenharmony_ci         }
155bf215546Sopenharmony_ci      }
156bf215546Sopenharmony_ci   }
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci   util_copy_framebuffer_state(dst, fb);
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   if (svga->curr.framebuffer.zsbuf) {
161bf215546Sopenharmony_ci      switch (svga->curr.framebuffer.zsbuf->format) {
162bf215546Sopenharmony_ci      case PIPE_FORMAT_Z16_UNORM:
163bf215546Sopenharmony_ci         svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D16;
164bf215546Sopenharmony_ci         break;
165bf215546Sopenharmony_ci      case PIPE_FORMAT_Z24_UNORM_S8_UINT:
166bf215546Sopenharmony_ci      case PIPE_FORMAT_Z24X8_UNORM:
167bf215546Sopenharmony_ci      case PIPE_FORMAT_S8_UINT_Z24_UNORM:
168bf215546Sopenharmony_ci      case PIPE_FORMAT_X8Z24_UNORM:
169bf215546Sopenharmony_ci         svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D24S8;
170bf215546Sopenharmony_ci         break;
171bf215546Sopenharmony_ci      case PIPE_FORMAT_Z32_UNORM:
172bf215546Sopenharmony_ci         svga->curr.depthscale = 1.0f / DEPTH_BIAS_SCALE_FACTOR_D32;
173bf215546Sopenharmony_ci         break;
174bf215546Sopenharmony_ci      case PIPE_FORMAT_Z32_FLOAT:
175bf215546Sopenharmony_ci         svga->curr.depthscale = 1.0f / ((float)(1<<23));
176bf215546Sopenharmony_ci         break;
177bf215546Sopenharmony_ci      default:
178bf215546Sopenharmony_ci         svga->curr.depthscale = 0.0f;
179bf215546Sopenharmony_ci         break;
180bf215546Sopenharmony_ci      }
181bf215546Sopenharmony_ci   }
182bf215546Sopenharmony_ci   else {
183bf215546Sopenharmony_ci      svga->curr.depthscale = 0.0f;
184bf215546Sopenharmony_ci   }
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_FRAME_BUFFER;
187bf215546Sopenharmony_ci}
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_cistatic void
191bf215546Sopenharmony_cisvga_set_clip_state(struct pipe_context *pipe,
192bf215546Sopenharmony_ci                    const struct pipe_clip_state *clip)
193bf215546Sopenharmony_ci{
194bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci   svga->curr.clip = *clip; /* struct copy */
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_CLIP;
199bf215546Sopenharmony_ci}
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_cistatic void
203bf215546Sopenharmony_cisvga_set_viewport_states(struct pipe_context *pipe,
204bf215546Sopenharmony_ci                         unsigned start_slot,
205bf215546Sopenharmony_ci                         unsigned num_viewports,
206bf215546Sopenharmony_ci                         const struct pipe_viewport_state *viewports)
207bf215546Sopenharmony_ci{
208bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
209bf215546Sopenharmony_ci   ASSERTED struct svga_screen *svgascreen = svga_screen(pipe->screen);
210bf215546Sopenharmony_ci   unsigned i, num_vp;
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_ci   assert(start_slot + num_viewports <= svgascreen->max_viewports);
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   for (i = 0, num_vp = start_slot; i < num_viewports; i++)  {
215bf215546Sopenharmony_ci      svga->curr.viewport[num_vp++] = viewports[i]; /* struct copy */
216bf215546Sopenharmony_ci   }
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   svga->dirty |= SVGA_NEW_VIEWPORT;
219bf215546Sopenharmony_ci}
220bf215546Sopenharmony_ci
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci/**
223bf215546Sopenharmony_ci * Called by state tracker to specify a callback function the driver
224bf215546Sopenharmony_ci * can use to report info back to the gallium frontend.
225bf215546Sopenharmony_ci */
226bf215546Sopenharmony_cistatic void
227bf215546Sopenharmony_cisvga_set_debug_callback(struct pipe_context *pipe,
228bf215546Sopenharmony_ci                        const struct util_debug_callback *cb)
229bf215546Sopenharmony_ci{
230bf215546Sopenharmony_ci   struct svga_context *svga = svga_context(pipe);
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci   if (cb) {
233bf215546Sopenharmony_ci      svga->debug.callback = *cb;
234bf215546Sopenharmony_ci      svga->swc->debug_callback = &svga->debug.callback;
235bf215546Sopenharmony_ci   } else {
236bf215546Sopenharmony_ci      memset(&svga->debug.callback, 0, sizeof(svga->debug.callback));
237bf215546Sopenharmony_ci      svga->swc->debug_callback = NULL;
238bf215546Sopenharmony_ci   }
239bf215546Sopenharmony_ci}
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_civoid
243bf215546Sopenharmony_cisvga_init_misc_functions(struct svga_context *svga)
244bf215546Sopenharmony_ci{
245bf215546Sopenharmony_ci   svga->pipe.set_scissor_states = svga_set_scissor_states;
246bf215546Sopenharmony_ci   svga->pipe.set_polygon_stipple = svga_set_polygon_stipple;
247bf215546Sopenharmony_ci   svga->pipe.set_framebuffer_state = svga_set_framebuffer_state;
248bf215546Sopenharmony_ci   svga->pipe.set_clip_state = svga_set_clip_state;
249bf215546Sopenharmony_ci   svga->pipe.set_viewport_states = svga_set_viewport_states;
250bf215546Sopenharmony_ci   svga->pipe.set_debug_callback = svga_set_debug_callback;
251bf215546Sopenharmony_ci}
252