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