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