st/xa: don't use user_buffer_create
[mesa.git] / src / gallium / state_trackers / xa / xa_renderer.c
1 /**********************************************************
2 * Copyright 2009-2011 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 * Authors:
26 * Zack Rusin <zackr-at-vmware-dot-com>
27 */
28
29 #include "xa_context.h"
30 #include "xa_priv.h"
31 #include <math.h>
32 #include "cso_cache/cso_context.h"
33 #include "util/u_inlines.h"
34 #include "util/u_sampler.h"
35 #include "util/u_draw_quad.h"
36
37 #define floatsEqual(x, y) (fabs(x - y) <= 0.00001f * MIN2(fabs(x), fabs(y)))
38 #define floatIsZero(x) (floatsEqual((x) + 1, 1))
39
40 #define NUM_COMPONENTS 4
41
42 void
43
44
45 renderer_set_constants(struct xa_context *r,
46 int shader_type, const float *params, int param_bytes);
47
48 static INLINE boolean
49 is_affine(float *matrix)
50 {
51 return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
52 && floatsEqual(matrix[8], 1);
53 }
54
55 static INLINE void
56 map_point(float *mat, float x, float y, float *out_x, float *out_y)
57 {
58 if (!mat) {
59 *out_x = x;
60 *out_y = y;
61 return;
62 }
63
64 *out_x = mat[0] * x + mat[3] * y + mat[6];
65 *out_y = mat[1] * x + mat[4] * y + mat[7];
66 if (!is_affine(mat)) {
67 float w = 1 / (mat[2] * x + mat[5] * y + mat[8]);
68
69 *out_x *= w;
70 *out_y *= w;
71 }
72 }
73
74 static INLINE void
75 renderer_draw(struct xa_context *r)
76 {
77 int num_verts = r->buffer_size / (r->attrs_per_vertex * NUM_COMPONENTS);
78
79 if (!r->buffer_size)
80 return;
81
82 cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
83 util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
84 num_verts, /* verts */
85 r->attrs_per_vertex); /* attribs/vert */
86 r->buffer_size = 0;
87 }
88
89 static INLINE void
90 renderer_draw_conditional(struct xa_context *r, int next_batch)
91 {
92 if (r->buffer_size + next_batch >= XA_VB_SIZE ||
93 (next_batch == 0 && r->buffer_size)) {
94 renderer_draw(r);
95 }
96 }
97
98 void
99 renderer_init_state(struct xa_context *r)
100 {
101 struct pipe_depth_stencil_alpha_state dsa;
102 struct pipe_rasterizer_state raster;
103 unsigned i;
104
105 /* set common initial clip state */
106 memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
107 cso_set_depth_stencil_alpha(r->cso, &dsa);
108
109 /* XXX: move to renderer_init_state? */
110 memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
111 raster.gl_rasterization_rules = 1;
112 raster.depth_clip = 1;
113 cso_set_rasterizer(r->cso, &raster);
114
115 /* vertex elements state */
116 memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);
117 for (i = 0; i < 3; i++) {
118 r->velems[i].src_offset = i * 4 * sizeof(float);
119 r->velems[i].instance_divisor = 0;
120 r->velems[i].vertex_buffer_index = 0;
121 r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
122 }
123 }
124
125 static INLINE void
126 add_vertex_color(struct xa_context *r, float x, float y, float color[4])
127 {
128 float *vertex = r->buffer + r->buffer_size;
129
130 vertex[0] = x;
131 vertex[1] = y;
132 vertex[2] = 0.f; /*z */
133 vertex[3] = 1.f; /*w */
134
135 vertex[4] = color[0]; /*r */
136 vertex[5] = color[1]; /*g */
137 vertex[6] = color[2]; /*b */
138 vertex[7] = color[3]; /*a */
139
140 r->buffer_size += 8;
141 }
142
143 static INLINE void
144 add_vertex_1tex(struct xa_context *r, float x, float y, float s, float t)
145 {
146 float *vertex = r->buffer + r->buffer_size;
147
148 vertex[0] = x;
149 vertex[1] = y;
150 vertex[2] = 0.f; /*z */
151 vertex[3] = 1.f; /*w */
152
153 vertex[4] = s; /*s */
154 vertex[5] = t; /*t */
155 vertex[6] = 0.f; /*r */
156 vertex[7] = 1.f; /*q */
157
158 r->buffer_size += 8;
159 }
160
161 static INLINE void
162 add_vertex_2tex(struct xa_context *r,
163 float x, float y, float s0, float t0, float s1, float t1)
164 {
165 float *vertex = r->buffer + r->buffer_size;
166
167 vertex[0] = x;
168 vertex[1] = y;
169 vertex[2] = 0.f; /*z */
170 vertex[3] = 1.f; /*w */
171
172 vertex[4] = s0; /*s */
173 vertex[5] = t0; /*t */
174 vertex[6] = 0.f; /*r */
175 vertex[7] = 1.f; /*q */
176
177 vertex[8] = s1; /*s */
178 vertex[9] = t1; /*t */
179 vertex[10] = 0.f; /*r */
180 vertex[11] = 1.f; /*q */
181
182 r->buffer_size += 12;
183 }
184
185 static void
186 add_vertex_data1(struct xa_context *r,
187 float srcX, float srcY, float dstX, float dstY,
188 float width, float height,
189 struct pipe_resource *src, const float *src_matrix)
190 {
191 float s0, t0, s1, t1, s2, t2, s3, t3;
192 float pt0[2], pt1[2], pt2[2], pt3[2];
193
194 pt0[0] = srcX;
195 pt0[1] = srcY;
196 pt1[0] = (srcX + width);
197 pt1[1] = srcY;
198 pt2[0] = (srcX + width);
199 pt2[1] = (srcY + height);
200 pt3[0] = srcX;
201 pt3[1] = (srcY + height);
202
203 if (src_matrix) {
204 map_point((float *)src_matrix, pt0[0], pt0[1], &pt0[0], &pt0[1]);
205 map_point((float *)src_matrix, pt1[0], pt1[1], &pt1[0], &pt1[1]);
206 map_point((float *)src_matrix, pt2[0], pt2[1], &pt2[0], &pt2[1]);
207 map_point((float *)src_matrix, pt3[0], pt3[1], &pt3[0], &pt3[1]);
208 }
209
210 s0 = pt0[0] / src->width0;
211 s1 = pt1[0] / src->width0;
212 s2 = pt2[0] / src->width0;
213 s3 = pt3[0] / src->width0;
214 t0 = pt0[1] / src->height0;
215 t1 = pt1[1] / src->height0;
216 t2 = pt2[1] / src->height0;
217 t3 = pt3[1] / src->height0;
218
219 /* 1st vertex */
220 add_vertex_1tex(r, dstX, dstY, s0, t0);
221 /* 2nd vertex */
222 add_vertex_1tex(r, dstX + width, dstY, s1, t1);
223 /* 3rd vertex */
224 add_vertex_1tex(r, dstX + width, dstY + height, s2, t2);
225 /* 4th vertex */
226 add_vertex_1tex(r, dstX, dstY + height, s3, t3);
227 }
228
229 static void
230 add_vertex_data2(struct xa_context *r,
231 float srcX, float srcY, float maskX, float maskY,
232 float dstX, float dstY, float width, float height,
233 struct pipe_resource *src,
234 struct pipe_resource *mask,
235 const float *src_matrix, const float *mask_matrix)
236 {
237 float src_s0, src_t0, src_s1, src_t1;
238 float mask_s0, mask_t0, mask_s1, mask_t1;
239 float spt0[2], spt1[2];
240 float mpt0[2], mpt1[2];
241
242 spt0[0] = srcX;
243 spt0[1] = srcY;
244 spt1[0] = srcX + width;
245 spt1[1] = srcY + height;
246
247 mpt0[0] = maskX;
248 mpt0[1] = maskY;
249 mpt1[0] = maskX + width;
250 mpt1[1] = maskY + height;
251
252 if (src_matrix) {
253 map_point((float *)src_matrix, spt0[0], spt0[1], &spt0[0], &spt0[1]);
254 map_point((float *)src_matrix, spt1[0], spt1[1], &spt1[0], &spt1[1]);
255 }
256
257 if (mask_matrix) {
258 map_point((float *)mask_matrix, mpt0[0], mpt0[1], &mpt0[0], &mpt0[1]);
259 map_point((float *)mask_matrix, mpt1[0], mpt1[1], &mpt1[0], &mpt1[1]);
260 }
261
262 src_s0 = spt0[0] / src->width0;
263 src_t0 = spt0[1] / src->height0;
264 src_s1 = spt1[0] / src->width0;
265 src_t1 = spt1[1] / src->height0;
266
267 mask_s0 = mpt0[0] / mask->width0;
268 mask_t0 = mpt0[1] / mask->height0;
269 mask_s1 = mpt1[0] / mask->width0;
270 mask_t1 = mpt1[1] / mask->height0;
271
272 /* 1st vertex */
273 add_vertex_2tex(r, dstX, dstY,
274 src_s0, src_t0, mask_s0, mask_t0);
275 /* 2nd vertex */
276 add_vertex_2tex(r, dstX + width, dstY,
277 src_s1, src_t0, mask_s1, mask_t0);
278 /* 3rd vertex */
279 add_vertex_2tex(r, dstX + width, dstY + height,
280 src_s1, src_t1, mask_s1, mask_t1);
281 /* 4th vertex */
282 add_vertex_2tex(r, dstX, dstY + height,
283 src_s0, src_t1, mask_s0, mask_t1);
284 }
285
286 static void
287 setup_vertex_data_yuv(struct xa_context *r,
288 float srcX,
289 float srcY,
290 float srcW,
291 float srcH,
292 float dstX,
293 float dstY,
294 float dstW, float dstH, struct xa_surface *srf[])
295 {
296 float s0, t0, s1, t1;
297 float spt0[2], spt1[2];
298 struct pipe_resource *tex;
299
300 spt0[0] = srcX;
301 spt0[1] = srcY;
302 spt1[0] = srcX + srcW;
303 spt1[1] = srcY + srcH;
304
305 tex = srf[0]->tex;
306 s0 = spt0[0] / tex->width0;
307 t0 = spt0[1] / tex->height0;
308 s1 = spt1[0] / tex->width0;
309 t1 = spt1[1] / tex->height0;
310
311 /* 1st vertex */
312 add_vertex_1tex(r, dstX, dstY, s0, t0);
313 /* 2nd vertex */
314 add_vertex_1tex(r, dstX + dstW, dstY, s1, t0);
315 /* 3rd vertex */
316 add_vertex_1tex(r, dstX + dstW, dstY + dstH, s1, t1);
317 /* 4th vertex */
318 add_vertex_1tex(r, dstX, dstY + dstH, s0, t1);
319 }
320
321 /* Set up framebuffer, viewport and vertex shader constant buffer
322 * state for a particular destinaton surface. In all our rendering,
323 * these concepts are linked.
324 */
325 void
326 renderer_bind_destination(struct xa_context *r,
327 struct pipe_surface *surface, int width, int height)
328 {
329
330 struct pipe_framebuffer_state fb;
331 struct pipe_viewport_state viewport;
332
333 /* Framebuffer uses actual surface width/height
334 */
335 memset(&fb, 0, sizeof fb);
336 fb.width = surface->width;
337 fb.height = surface->height;
338 fb.nr_cbufs = 1;
339 fb.cbufs[0] = surface;
340 fb.zsbuf = 0;
341
342 /* Viewport just touches the bit we're interested in:
343 */
344 viewport.scale[0] = width / 2.f;
345 viewport.scale[1] = height / 2.f;
346 viewport.scale[2] = 1.0;
347 viewport.scale[3] = 1.0;
348 viewport.translate[0] = width / 2.f;
349 viewport.translate[1] = height / 2.f;
350 viewport.translate[2] = 0.0;
351 viewport.translate[3] = 0.0;
352
353 /* Constant buffer set up to match viewport dimensions:
354 */
355 if (r->fb_width != width || r->fb_height != height) {
356 float vs_consts[8] = {
357 2.f / width, 2.f / height, 1, 1,
358 -1, -1, 0, 0
359 };
360
361 r->fb_width = width;
362 r->fb_height = height;
363
364 renderer_set_constants(r, PIPE_SHADER_VERTEX,
365 vs_consts, sizeof vs_consts);
366 }
367
368 cso_set_framebuffer(r->cso, &fb);
369 cso_set_viewport(r->cso, &viewport);
370 }
371
372 void
373 renderer_set_constants(struct xa_context *r,
374 int shader_type, const float *params, int param_bytes)
375 {
376 struct pipe_resource **cbuf =
377 (shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
378 &r->fs_const_buffer;
379
380 pipe_resource_reference(cbuf, NULL);
381 *cbuf = pipe_buffer_create(r->pipe->screen,
382 PIPE_BIND_CONSTANT_BUFFER, PIPE_USAGE_STATIC,
383 param_bytes);
384
385 if (*cbuf) {
386 pipe_buffer_write(r->pipe, *cbuf, 0, param_bytes, params);
387 }
388 pipe_set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
389 }
390
391 void
392 renderer_copy_prepare(struct xa_context *r,
393 struct pipe_surface *dst_surface,
394 struct pipe_resource *src_texture,
395 const enum xa_formats src_xa_format,
396 const enum xa_formats dst_xa_format)
397 {
398 struct pipe_context *pipe = r->pipe;
399 struct pipe_screen *screen = pipe->screen;
400 struct xa_shader shader;
401 uint32_t fs_traits = FS_COMPOSITE;
402
403 assert(screen->is_format_supported(screen, dst_surface->format,
404 PIPE_TEXTURE_2D, 0,
405 PIPE_BIND_RENDER_TARGET));
406 (void)screen;
407
408 /* set misc state we care about */
409 {
410 struct pipe_blend_state blend;
411
412 memset(&blend, 0, sizeof(blend));
413 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
414 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
415 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
416 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
417 blend.rt[0].colormask = PIPE_MASK_RGBA;
418 cso_set_blend(r->cso, &blend);
419 }
420
421 /* sampler */
422 {
423 struct pipe_sampler_state sampler;
424
425 memset(&sampler, 0, sizeof(sampler));
426 sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
427 sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
428 sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
429 sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
430 sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
431 sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
432 sampler.normalized_coords = 1;
433 cso_single_sampler(r->cso, 0, &sampler);
434 cso_single_sampler_done(r->cso);
435 }
436
437 renderer_bind_destination(r, dst_surface,
438 dst_surface->width, dst_surface->height);
439
440 /* texture/sampler view */
441 {
442 struct pipe_sampler_view templ;
443 struct pipe_sampler_view *src_view;
444
445 u_sampler_view_default_template(&templ,
446 src_texture, src_texture->format);
447 src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
448 cso_set_fragment_sampler_views(r->cso, 1, &src_view);
449 pipe_sampler_view_reference(&src_view, NULL);
450 }
451
452 /* shaders */
453 if (src_texture->format == PIPE_FORMAT_L8_UNORM)
454 fs_traits |= FS_SRC_LUMINANCE;
455 if (dst_surface->format == PIPE_FORMAT_L8_UNORM)
456 fs_traits |= FS_DST_LUMINANCE;
457 if (xa_format_a(dst_xa_format) != 0 &&
458 xa_format_a(src_xa_format) == 0)
459 fs_traits |= FS_SRC_SET_ALPHA;
460
461 shader = xa_shaders_get(r->shaders, VS_COMPOSITE, fs_traits);
462 cso_set_vertex_shader_handle(r->cso, shader.vs);
463 cso_set_fragment_shader_handle(r->cso, shader.fs);
464
465 r->buffer_size = 0;
466 r->attrs_per_vertex = 2;
467 }
468
469 void
470 renderer_copy(struct xa_context *r,
471 int dx,
472 int dy,
473 int sx,
474 int sy,
475 int width, int height, float src_width, float src_height)
476 {
477 float s0, t0, s1, t1;
478 float x0, y0, x1, y1;
479
480 /* XXX: could put the texcoord scaling calculation into the vertex
481 * shader.
482 */
483 s0 = sx / src_width;
484 s1 = (sx + width) / src_width;
485 t0 = sy / src_height;
486 t1 = (sy + height) / src_height;
487
488 x0 = dx;
489 x1 = dx + width;
490 y0 = dy;
491 y1 = dy + height;
492
493 /* draw quad */
494 renderer_draw_conditional(r, 4 * 8);
495 add_vertex_1tex(r, x0, y0, s0, t0);
496 add_vertex_1tex(r, x1, y0, s1, t0);
497 add_vertex_1tex(r, x1, y1, s1, t1);
498 add_vertex_1tex(r, x0, y1, s0, t1);
499 }
500
501 void
502 renderer_draw_yuv(struct xa_context *r,
503 float src_x,
504 float src_y,
505 float src_w,
506 float src_h,
507 int dst_x,
508 int dst_y, int dst_w, int dst_h, struct xa_surface *srf[])
509 {
510 const int num_attribs = 2; /*pos + tex coord */
511
512 setup_vertex_data_yuv(r,
513 src_x, src_y, src_w, src_h,
514 dst_x, dst_y, dst_w, dst_h, srf);
515
516 cso_set_vertex_elements(r->cso, num_attribs, r->velems);
517 util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
518 4, /* verts */
519 num_attribs); /* attribs/vert */
520 r->buffer_size = 0;
521 }
522
523 void
524 renderer_begin_solid(struct xa_context *r)
525 {
526 r->buffer_size = 0;
527 r->attrs_per_vertex = 2;
528 }
529
530 void
531 renderer_solid(struct xa_context *r,
532 int x0, int y0, int x1, int y1, float *color)
533 {
534 /*
535 * debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
536 * x0, y0, x1, y1, color[0], color[1], color[2], color[3]); */
537
538 renderer_draw_conditional(r, 4 * 8);
539
540 /* 1st vertex */
541 add_vertex_color(r, x0, y0, color);
542 /* 2nd vertex */
543 add_vertex_color(r, x1, y0, color);
544 /* 3rd vertex */
545 add_vertex_color(r, x1, y1, color);
546 /* 4th vertex */
547 add_vertex_color(r, x0, y1, color);
548 }
549
550 void
551 renderer_draw_flush(struct xa_context *r)
552 {
553 renderer_draw_conditional(r, 0);
554 }
555
556 void
557 renderer_begin_textures(struct xa_context *r)
558 {
559 r->attrs_per_vertex = 1 + r->num_bound_samplers;
560 r->buffer_size = 0;
561 }
562
563 void
564 renderer_texture(struct xa_context *r,
565 int *pos,
566 int width, int height,
567 const float *src_matrix,
568 const float *mask_matrix)
569 {
570 struct pipe_sampler_view **sampler_view = r->bound_sampler_views;
571
572 #if 0
573 if (src_matrix) {
574 debug_printf("src_matrix = \n");
575 debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
576 debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
577 debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
578 }
579 if (mask_matrix) {
580 debug_printf("mask_matrix = \n");
581 debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
582 debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
583 debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
584 }
585 #endif
586
587 switch(r->attrs_per_vertex) {
588 case 2:
589 renderer_draw_conditional(r, 4 * 8);
590 add_vertex_data1(r,
591 pos[0], pos[1], /* src */
592 pos[4], pos[5], /* dst */
593 width, height,
594 sampler_view[0]->texture, src_matrix);
595 break;
596 case 3:
597 renderer_draw_conditional(r, 4 * 12);
598 add_vertex_data2(r,
599 pos[0], pos[1], /* src */
600 pos[2], pos[3], /* mask */
601 pos[4], pos[5], /* dst */
602 width, height,
603 sampler_view[0]->texture, sampler_view[1]->texture,
604 src_matrix, mask_matrix);
605 break;
606 default:
607 break;
608 }
609 }