Merge branch 'radeon-texrewrite-clean' into mesa_7_7_branch
[mesa.git] / src / gallium / drivers / svga / svga_state_framebuffer.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 "pipe/p_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29
30 #include "svga_context.h"
31 #include "svga_state.h"
32 #include "svga_cmd.h"
33 #include "svga_debug.h"
34
35 #include "svga_hw_reg.h"
36
37
38 /***********************************************************************
39 * Hardware state update
40 */
41
42
43 static int emit_framebuffer( struct svga_context *svga,
44 unsigned dirty )
45 {
46 const struct pipe_framebuffer_state *curr = &svga->curr.framebuffer;
47 struct pipe_framebuffer_state *hw = &svga->state.hw_clear.framebuffer;
48 unsigned i;
49 enum pipe_error ret;
50
51 /* XXX: Need shadow state in svga->hw to eliminate redundant
52 * uploads, especially of NULL buffers.
53 */
54
55 for(i = 0; i < PIPE_MAX_COLOR_BUFS; ++i) {
56 if (curr->cbufs[i] != hw->cbufs[i]) {
57 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_COLOR0 + i, curr->cbufs[i]);
58 if (ret != PIPE_OK)
59 return ret;
60
61 pipe_surface_reference(&hw->cbufs[i], curr->cbufs[i]);
62 }
63 }
64
65
66 if (curr->zsbuf != hw->zsbuf) {
67 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_DEPTH, curr->zsbuf);
68 if (ret != PIPE_OK)
69 return ret;
70
71 if (curr->zsbuf &&
72 curr->zsbuf->format == PIPE_FORMAT_Z24S8_UNORM) {
73 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_STENCIL, curr->zsbuf);
74 if (ret != PIPE_OK)
75 return ret;
76 }
77 else {
78 ret = SVGA3D_SetRenderTarget(svga->swc, SVGA3D_RT_STENCIL, NULL);
79 if (ret != PIPE_OK)
80 return ret;
81 }
82
83 pipe_surface_reference(&hw->zsbuf, curr->zsbuf);
84 }
85
86
87 return 0;
88 }
89
90
91 struct svga_tracked_state svga_hw_framebuffer =
92 {
93 "hw framebuffer state",
94 SVGA_NEW_FRAME_BUFFER,
95 emit_framebuffer
96 };
97
98
99
100
101 /***********************************************************************
102 */
103
104 static int emit_viewport( struct svga_context *svga,
105 unsigned dirty )
106 {
107 const struct pipe_viewport_state *viewport = &svga->curr.viewport;
108 struct svga_prescale prescale;
109 SVGA3dRect rect;
110 /* Not sure if this state is relevant with POSITIONT. Probably
111 * not, but setting to 0,1 avoids some state pingponging.
112 */
113 float range_min = 0.0;
114 float range_max = 1.0;
115 float flip = -1.0;
116 boolean degenerate = FALSE;
117 enum pipe_error ret;
118
119 float fb_width = svga->curr.framebuffer.width;
120 float fb_height = svga->curr.framebuffer.height;
121
122 memset( &prescale, 0, sizeof(prescale) );
123
124 if (svga->curr.rast->templ.bypass_vs_clip_and_viewport) {
125
126 /* Avoid POSITIONT as it has a non trivial implementation outside the D3D
127 * API. Always generate a vertex shader.
128 */
129 rect.x = 0;
130 rect.y = 0;
131 rect.w = svga->curr.framebuffer.width;
132 rect.h = svga->curr.framebuffer.height;
133
134 prescale.scale[0] = 2.0 / (float)rect.w;
135 prescale.scale[1] = - 2.0 / (float)rect.h;
136 prescale.scale[2] = 1.0;
137 prescale.scale[3] = 1.0;
138 prescale.translate[0] = -1.0f;
139 prescale.translate[1] = 1.0f;
140 prescale.translate[2] = 0;
141 prescale.translate[3] = 0;
142 prescale.enabled = TRUE;
143 } else {
144
145 /* Examine gallium viewport transformation and produce a screen
146 * rectangle and possibly vertex shader pre-transformation to
147 * get the same results.
148 */
149 float fx = viewport->scale[0] * -1.0 + viewport->translate[0];
150 float fy = flip * viewport->scale[1] * -1.0 + viewport->translate[1];
151 float fw = viewport->scale[0] * 2;
152 float fh = flip * viewport->scale[1] * 2;
153
154 SVGA_DBG(DEBUG_VIEWPORT,
155 "\ninitial %f,%f %fx%f\n",
156 fx,
157 fy,
158 fw,
159 fh);
160
161 prescale.scale[0] = 1.0;
162 prescale.scale[1] = 1.0;
163 prescale.scale[2] = 1.0;
164 prescale.scale[3] = 1.0;
165 prescale.translate[0] = 0;
166 prescale.translate[1] = 0;
167 prescale.translate[2] = 0;
168 prescale.translate[3] = 0;
169 prescale.enabled = TRUE;
170
171
172
173 if (fw < 0) {
174 prescale.scale[0] *= -1.0;
175 prescale.translate[0] += -fw;
176 fw = -fw;
177 fx = viewport->scale[0] * 1.0 + viewport->translate[0];
178 }
179
180 if (fh < 0) {
181 prescale.scale[1] *= -1.0;
182 prescale.translate[1] += -fh;
183 fh = -fh;
184 fy = flip * viewport->scale[1] * 1.0 + viewport->translate[1];
185 }
186
187 if (fx < 0) {
188 prescale.translate[0] += fx;
189 prescale.scale[0] *= fw / (fw + fx);
190 fw += fx;
191 fx = 0;
192 }
193
194 if (fy < 0) {
195 prescale.translate[1] += fy;
196 prescale.scale[1] *= fh / (fh + fy);
197 fh += fy;
198 fy = 0;
199 }
200
201 if (fx + fw > fb_width) {
202 prescale.scale[0] *= fw / (fb_width - fx);
203 prescale.translate[0] -= fx * (fw / (fb_width - fx));
204 prescale.translate[0] += fx;
205 fw = fb_width - fx;
206
207 }
208
209 if (fy + fh > fb_height) {
210 prescale.scale[1] *= fh / (fb_height - fy);
211 prescale.translate[1] -= fy * (fh / (fb_height - fy));
212 prescale.translate[1] += fy;
213 fh = fb_height - fy;
214 }
215
216 if (fw < 0 || fh < 0) {
217 fw = fh = fx = fy = 0;
218 degenerate = TRUE;
219 goto out;
220 }
221
222
223 /* D3D viewport is integer space. Convert fx,fy,etc. to
224 * integers.
225 *
226 * TODO: adjust pretranslate correct for any subpixel error
227 * introduced converting to integers.
228 */
229 rect.x = fx;
230 rect.y = fy;
231 rect.w = fw;
232 rect.h = fh;
233
234 SVGA_DBG(DEBUG_VIEWPORT,
235 "viewport error %f,%f %fx%f\n",
236 fabs((float)rect.x - fx),
237 fabs((float)rect.y - fy),
238 fabs((float)rect.w - fw),
239 fabs((float)rect.h - fh));
240
241 SVGA_DBG(DEBUG_VIEWPORT,
242 "viewport %d,%d %dx%d\n",
243 rect.x,
244 rect.y,
245 rect.w,
246 rect.h);
247
248
249 /* Finally, to get GL rasterization rules, need to tweak the
250 * screen-space coordinates slightly relative to D3D which is
251 * what hardware implements natively.
252 */
253 if (svga->curr.rast->templ.gl_rasterization_rules) {
254 float adjust_x = 0.0;
255 float adjust_y = 0.0;
256
257 switch (svga->curr.reduced_prim) {
258 case PIPE_PRIM_LINES:
259 adjust_x = -0.5;
260 adjust_y = 0;
261 break;
262 case PIPE_PRIM_POINTS:
263 case PIPE_PRIM_TRIANGLES:
264 adjust_x = -0.375;
265 adjust_y = -0.5;
266 break;
267 }
268
269 prescale.translate[0] += adjust_x;
270 prescale.translate[1] += adjust_y;
271 prescale.translate[2] = 0.5; /* D3D clip space */
272 prescale.scale[2] = 0.5; /* D3D clip space */
273 }
274
275
276 range_min = viewport->scale[2] * -1.0 + viewport->translate[2];
277 range_max = viewport->scale[2] * 1.0 + viewport->translate[2];
278
279 /* D3D (and by implication SVGA) doesn't like dealing with zmax
280 * less than zmin. Detect that case, flip the depth range and
281 * invert our z-scale factor to achieve the same effect.
282 */
283 if (range_min > range_max) {
284 float range_tmp;
285 range_tmp = range_min;
286 range_min = range_max;
287 range_max = range_tmp;
288 prescale.scale[2] = -prescale.scale[2];
289 }
290 }
291
292 if (prescale.enabled) {
293 float H[2];
294 float J[2];
295 int i;
296
297 SVGA_DBG(DEBUG_VIEWPORT,
298 "prescale %f,%f %fx%f\n",
299 prescale.translate[0],
300 prescale.translate[1],
301 prescale.scale[0],
302 prescale.scale[1]);
303
304 H[0] = (float)rect.w / 2.0;
305 H[1] = -(float)rect.h / 2.0;
306 J[0] = (float)rect.x + (float)rect.w / 2.0;
307 J[1] = (float)rect.y + (float)rect.h / 2.0;
308
309 SVGA_DBG(DEBUG_VIEWPORT,
310 "H %f,%f\n"
311 "J %fx%f\n",
312 H[0],
313 H[1],
314 J[0],
315 J[1]);
316
317 /* Adjust prescale to take into account the fact that it is
318 * going to be applied prior to the perspective divide and
319 * viewport transformation.
320 *
321 * Vwin = H(Vc/Vc.w) + J
322 *
323 * We want to tweak Vwin with scale and translation from above,
324 * as in:
325 *
326 * Vwin' = S Vwin + T
327 *
328 * But we can only modify the values at Vc. Plugging all the
329 * above together, and rearranging, eventually we get:
330 *
331 * Vwin' = H(Vc'/Vc'.w) + J
332 * where:
333 * Vc' = SVc + KVc.w
334 * K = (T + (S-1)J) / H
335 *
336 * Overwrite prescale.translate with values for K:
337 */
338 for (i = 0; i < 2; i++) {
339 prescale.translate[i] = ((prescale.translate[i] +
340 (prescale.scale[i] - 1.0) * J[i]) / H[i]);
341 }
342
343 SVGA_DBG(DEBUG_VIEWPORT,
344 "clipspace %f,%f %fx%f\n",
345 prescale.translate[0],
346 prescale.translate[1],
347 prescale.scale[0],
348 prescale.scale[1]);
349 }
350
351 out:
352 if (degenerate) {
353 rect.x = 0;
354 rect.y = 0;
355 rect.w = 1;
356 rect.h = 1;
357 prescale.enabled = FALSE;
358 }
359
360 if (memcmp(&rect, &svga->state.hw_clear.viewport, sizeof(rect)) != 0) {
361 ret = SVGA3D_SetViewport(svga->swc, &rect);
362 if(ret != PIPE_OK)
363 return ret;
364
365 memcpy(&svga->state.hw_clear.viewport, &rect, sizeof(rect));
366 assert(sizeof(rect) == sizeof(svga->state.hw_clear.viewport));
367 }
368
369 if (svga->state.hw_clear.depthrange.zmin != range_min ||
370 svga->state.hw_clear.depthrange.zmax != range_max)
371 {
372 ret = SVGA3D_SetZRange(svga->swc, range_min, range_max );
373 if(ret != PIPE_OK)
374 return ret;
375
376 svga->state.hw_clear.depthrange.zmin = range_min;
377 svga->state.hw_clear.depthrange.zmax = range_max;
378 }
379
380 if (memcmp(&prescale, &svga->state.hw_clear.prescale, sizeof prescale) != 0) {
381 svga->dirty |= SVGA_NEW_PRESCALE;
382 svga->state.hw_clear.prescale = prescale;
383 }
384
385 return 0;
386 }
387
388
389 struct svga_tracked_state svga_hw_viewport =
390 {
391 "hw viewport state",
392 ( SVGA_NEW_FRAME_BUFFER |
393 SVGA_NEW_VIEWPORT |
394 SVGA_NEW_RAST |
395 SVGA_NEW_REDUCED_PRIMITIVE ),
396 emit_viewport
397 };
398
399
400 /***********************************************************************
401 * Scissor state
402 */
403 static int emit_scissor_rect( struct svga_context *svga,
404 unsigned dirty )
405 {
406 const struct pipe_scissor_state *scissor = &svga->curr.scissor;
407 SVGA3dRect rect;
408
409 rect.x = scissor->minx;
410 rect.y = scissor->miny;
411 rect.w = scissor->maxx - scissor->minx; /* + 1 ?? */
412 rect.h = scissor->maxy - scissor->miny; /* + 1 ?? */
413
414 return SVGA3D_SetScissorRect(svga->swc, &rect);
415 }
416
417
418 struct svga_tracked_state svga_hw_scissor =
419 {
420 "hw scissor state",
421 SVGA_NEW_SCISSOR,
422 emit_scissor_rect
423 };
424
425
426 /***********************************************************************
427 * Userclip state
428 */
429
430 static int emit_clip_planes( struct svga_context *svga,
431 unsigned dirty )
432 {
433 unsigned i;
434 enum pipe_error ret;
435
436 /* TODO: just emit directly from svga_set_clip_state()?
437 */
438 for (i = 0; i < svga->curr.clip.nr; i++) {
439 ret = SVGA3D_SetClipPlane( svga->swc,
440 i,
441 svga->curr.clip.ucp[i] );
442 if(ret != PIPE_OK)
443 return ret;
444 }
445
446 return 0;
447 }
448
449
450 struct svga_tracked_state svga_hw_clip_planes =
451 {
452 "hw viewport state",
453 SVGA_NEW_CLIP,
454 emit_clip_planes
455 };