Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * Blitter utility to facilitate acceleration of the clear, surface_copy,
30 * and surface_fill functions.
31 *
32 * @author Marek Olšák
33 */
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_inlines.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "pipe/p_state.h"
40
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_blitter.h"
45 #include "util/u_draw_quad.h"
46 #include "util/u_pack_color.h"
47 #include "util/u_rect.h"
48 #include "util/u_simple_shaders.h"
49 #include "util/u_texture.h"
50
51 #define INVALID_PTR ((void*)~0)
52
53 struct blitter_context_priv
54 {
55 struct blitter_context blitter;
56
57 struct pipe_context *pipe; /**< pipe context */
58 struct pipe_buffer *vbuf; /**< quad */
59
60 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
61
62 /* Templates for various state objects. */
63 struct pipe_sampler_state template_sampler_state;
64
65 /* Constant state objects. */
66 /* Vertex shaders. */
67 void *vs_col; /**< Vertex shader which passes {pos, color} to the output */
68 void *vs_tex; /**< Vertex shader which passes {pos, texcoord} to the output.*/
69
70 /* Fragment shaders. */
71 /* FS which outputs a color to multiple color buffers. */
72 void *fs_col[PIPE_MAX_COLOR_BUFS];
73
74 /* FS which outputs a color from a texture,
75 where the index is PIPE_TEXTURE_* to be sampled. */
76 void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
77
78 /* FS which outputs a depth from a texture,
79 where the index is PIPE_TEXTURE_* to be sampled. */
80 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
81
82 /* Blend state. */
83 void *blend_write_color; /**< blend state with writemask of RGBA */
84 void *blend_keep_color; /**< blend state with writemask of 0 */
85
86 /* Depth stencil alpha state. */
87 void *dsa_write_depth_stencil;
88 void *dsa_write_depth_keep_stencil;
89 void *dsa_keep_depth_stencil;
90
91 void *velem_state;
92
93 /* Sampler state for clamping to a miplevel. */
94 void *sampler_state[PIPE_MAX_TEXTURE_LEVELS];
95
96 /* Rasterizer state. */
97 void *rs_state;
98
99 /* Viewport state. */
100 struct pipe_viewport_state viewport;
101
102 /* Clip state. */
103 struct pipe_clip_state clip;
104 };
105
106 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
107 {
108 struct blitter_context_priv *ctx;
109 struct pipe_blend_state blend = { 0 };
110 struct pipe_depth_stencil_alpha_state dsa = { { 0 } };
111 struct pipe_rasterizer_state rs_state = { 0 };
112 struct pipe_sampler_state *sampler_state;
113 struct pipe_vertex_element velem[2];
114 unsigned i;
115
116 ctx = CALLOC_STRUCT(blitter_context_priv);
117 if (!ctx)
118 return NULL;
119
120 ctx->pipe = pipe;
121
122 /* init state objects for them to be considered invalid */
123 ctx->blitter.saved_blend_state = INVALID_PTR;
124 ctx->blitter.saved_dsa_state = INVALID_PTR;
125 ctx->blitter.saved_rs_state = INVALID_PTR;
126 ctx->blitter.saved_fs = INVALID_PTR;
127 ctx->blitter.saved_vs = INVALID_PTR;
128 ctx->blitter.saved_velem_state = INVALID_PTR;
129 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
130 ctx->blitter.saved_num_textures = ~0;
131 ctx->blitter.saved_num_sampler_states = ~0;
132
133 /* blend state objects */
134 ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
135
136 blend.rt[0].colormask = PIPE_MASK_RGBA;
137 ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
138
139 /* depth stencil alpha state objects */
140 ctx->dsa_keep_depth_stencil =
141 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
142
143 dsa.depth.enabled = 1;
144 dsa.depth.writemask = 1;
145 dsa.depth.func = PIPE_FUNC_ALWAYS;
146 ctx->dsa_write_depth_keep_stencil =
147 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
148
149 dsa.stencil[0].enabled = 1;
150 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
151 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
152 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
153 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
154 dsa.stencil[0].valuemask = 0xff;
155 dsa.stencil[0].writemask = 0xff;
156 ctx->dsa_write_depth_stencil =
157 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
158 /* The DSA state objects which write depth and stencil are created
159 * on-demand. */
160
161 /* sampler state */
162 sampler_state = &ctx->template_sampler_state;
163 sampler_state->wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
164 sampler_state->wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
165 sampler_state->wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
166 /* The sampler state objects which sample from a specified mipmap level
167 * are created on-demand. */
168
169 /* rasterizer state */
170 memset(&rs_state, 0, sizeof(rs_state));
171 rs_state.front_winding = PIPE_WINDING_CW;
172 rs_state.cull_mode = PIPE_WINDING_NONE;
173 rs_state.gl_rasterization_rules = 1;
174 rs_state.flatshade = 1;
175 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
176
177 /* vertex elements state */
178 memset(&velem[0], 0, sizeof(velem[0]) * 2);
179 for (i = 0; i < 2; i++) {
180 velem[i].src_offset = i * 4 * sizeof(float);
181 velem[i].instance_divisor = 0;
182 velem[i].vertex_buffer_index = 0;
183 velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
184 }
185 ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
186
187 /* fragment shaders are created on-demand */
188
189 /* vertex shaders */
190 {
191 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
192 TGSI_SEMANTIC_COLOR };
193 const uint semantic_indices[] = { 0, 0 };
194 ctx->vs_col =
195 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
196 semantic_indices);
197 }
198 {
199 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
200 TGSI_SEMANTIC_GENERIC };
201 const uint semantic_indices[] = { 0, 0 };
202 ctx->vs_tex =
203 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
204 semantic_indices);
205 }
206
207 /* set invariant vertex coordinates */
208 for (i = 0; i < 4; i++)
209 ctx->vertices[i][0][3] = 1; /*v.w*/
210
211 /* create the vertex buffer */
212 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
213 32,
214 PIPE_BUFFER_USAGE_VERTEX,
215 sizeof(ctx->vertices));
216
217 return &ctx->blitter;
218 }
219
220 void util_blitter_destroy(struct blitter_context *blitter)
221 {
222 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
223 struct pipe_context *pipe = ctx->pipe;
224 int i;
225
226 pipe->delete_blend_state(pipe, ctx->blend_write_color);
227 pipe->delete_blend_state(pipe, ctx->blend_keep_color);
228 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
229 pipe->delete_depth_stencil_alpha_state(pipe,
230 ctx->dsa_write_depth_keep_stencil);
231 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
232
233 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
234 pipe->delete_vs_state(pipe, ctx->vs_col);
235 pipe->delete_vs_state(pipe, ctx->vs_tex);
236 pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
237
238 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
239 if (ctx->fs_texfetch_col[i])
240 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
241 if (ctx->fs_texfetch_depth[i])
242 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
243 }
244
245 for (i = 0; i < PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
246 if (ctx->fs_col[i])
247 pipe->delete_fs_state(pipe, ctx->fs_col[i]);
248
249 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
250 if (ctx->sampler_state[i])
251 pipe->delete_sampler_state(pipe, ctx->sampler_state[i]);
252
253 pipe_buffer_reference(&ctx->vbuf, NULL);
254 FREE(ctx);
255 }
256
257 static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx)
258 {
259 /* make sure these CSOs have been saved */
260 assert(ctx->blitter.saved_blend_state != INVALID_PTR &&
261 ctx->blitter.saved_dsa_state != INVALID_PTR &&
262 ctx->blitter.saved_rs_state != INVALID_PTR &&
263 ctx->blitter.saved_fs != INVALID_PTR &&
264 ctx->blitter.saved_vs != INVALID_PTR &&
265 ctx->blitter.saved_velem_state != INVALID_PTR);
266 }
267
268 static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
269 {
270 struct pipe_context *pipe = ctx->pipe;
271
272 /* restore the state objects which are always required to be saved */
273 pipe->bind_blend_state(pipe, ctx->blitter.saved_blend_state);
274 pipe->bind_depth_stencil_alpha_state(pipe, ctx->blitter.saved_dsa_state);
275 pipe->bind_rasterizer_state(pipe, ctx->blitter.saved_rs_state);
276 pipe->bind_fs_state(pipe, ctx->blitter.saved_fs);
277 pipe->bind_vs_state(pipe, ctx->blitter.saved_vs);
278 pipe->bind_vertex_elements_state(pipe, ctx->blitter.saved_velem_state);
279
280 ctx->blitter.saved_blend_state = INVALID_PTR;
281 ctx->blitter.saved_dsa_state = INVALID_PTR;
282 ctx->blitter.saved_rs_state = INVALID_PTR;
283 ctx->blitter.saved_fs = INVALID_PTR;
284 ctx->blitter.saved_vs = INVALID_PTR;
285 ctx->blitter.saved_velem_state = INVALID_PTR;
286
287 pipe->set_stencil_ref(pipe, &ctx->blitter.saved_stencil_ref);
288
289 pipe->set_viewport_state(pipe, &ctx->blitter.saved_viewport);
290 pipe->set_clip_state(pipe, &ctx->blitter.saved_clip);
291
292 /* restore the state objects which are required to be saved before copy/fill
293 */
294 if (ctx->blitter.saved_fb_state.nr_cbufs != ~0) {
295 pipe->set_framebuffer_state(pipe, &ctx->blitter.saved_fb_state);
296 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
297 }
298
299 if (ctx->blitter.saved_num_sampler_states != ~0) {
300 pipe->bind_fragment_sampler_states(pipe,
301 ctx->blitter.saved_num_sampler_states,
302 ctx->blitter.saved_sampler_states);
303 ctx->blitter.saved_num_sampler_states = ~0;
304 }
305
306 if (ctx->blitter.saved_num_textures != ~0) {
307 pipe->set_fragment_sampler_textures(pipe,
308 ctx->blitter.saved_num_textures,
309 ctx->blitter.saved_textures);
310 ctx->blitter.saved_num_textures = ~0;
311 }
312 }
313
314 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
315 unsigned x1, unsigned y1,
316 unsigned x2, unsigned y2,
317 unsigned width, unsigned height,
318 float depth)
319 {
320 int i;
321
322 /* set vertex positions */
323 ctx->vertices[0][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v0.x*/
324 ctx->vertices[0][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v0.y*/
325
326 ctx->vertices[1][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v1.x*/
327 ctx->vertices[1][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v1.y*/
328
329 ctx->vertices[2][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v2.x*/
330 ctx->vertices[2][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v2.y*/
331
332 ctx->vertices[3][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v3.x*/
333 ctx->vertices[3][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v3.y*/
334
335 for (i = 0; i < 4; i++)
336 ctx->vertices[i][0][2] = depth; /*z*/
337
338 /* viewport */
339 ctx->viewport.scale[0] = 0.5f * width;
340 ctx->viewport.scale[1] = 0.5f * height;
341 ctx->viewport.scale[2] = 1.0f;
342 ctx->viewport.scale[3] = 1.0f;
343 ctx->viewport.translate[0] = 0.5f * width;
344 ctx->viewport.translate[1] = 0.5f * height;
345 ctx->viewport.translate[2] = 0.0f;
346 ctx->viewport.translate[3] = 0.0f;
347 ctx->pipe->set_viewport_state(ctx->pipe, &ctx->viewport);
348
349 /* clip */
350 ctx->pipe->set_clip_state(ctx->pipe, &ctx->clip);
351 }
352
353 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
354 const float *rgba)
355 {
356 int i;
357
358 for (i = 0; i < 4; i++) {
359 ctx->vertices[i][1][0] = rgba[0];
360 ctx->vertices[i][1][1] = rgba[1];
361 ctx->vertices[i][1][2] = rgba[2];
362 ctx->vertices[i][1][3] = rgba[3];
363 }
364 }
365
366 static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
367 struct pipe_surface *surf,
368 unsigned x1, unsigned y1,
369 unsigned x2, unsigned y2)
370 {
371 int i;
372 float s1 = x1 / (float)surf->width;
373 float t1 = y1 / (float)surf->height;
374 float s2 = x2 / (float)surf->width;
375 float t2 = y2 / (float)surf->height;
376
377 ctx->vertices[0][1][0] = s1; /*t0.s*/
378 ctx->vertices[0][1][1] = t1; /*t0.t*/
379
380 ctx->vertices[1][1][0] = s2; /*t1.s*/
381 ctx->vertices[1][1][1] = t1; /*t1.t*/
382
383 ctx->vertices[2][1][0] = s2; /*t2.s*/
384 ctx->vertices[2][1][1] = t2; /*t2.t*/
385
386 ctx->vertices[3][1][0] = s1; /*t3.s*/
387 ctx->vertices[3][1][1] = t2; /*t3.t*/
388
389 for (i = 0; i < 4; i++) {
390 ctx->vertices[i][1][2] = 0; /*r*/
391 ctx->vertices[i][1][3] = 1; /*q*/
392 }
393 }
394
395 static void blitter_set_texcoords_3d(struct blitter_context_priv *ctx,
396 struct pipe_surface *surf,
397 unsigned x1, unsigned y1,
398 unsigned x2, unsigned y2)
399 {
400 int i;
401 float depth = u_minify(surf->texture->depth0, surf->level);
402 float r = surf->zslice / depth;
403
404 blitter_set_texcoords_2d(ctx, surf, x1, y1, x2, y2);
405
406 for (i = 0; i < 4; i++)
407 ctx->vertices[i][1][2] = r; /*r*/
408 }
409
410 static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
411 struct pipe_surface *surf,
412 unsigned x1, unsigned y1,
413 unsigned x2, unsigned y2)
414 {
415 int i;
416 float s1 = x1 / (float)surf->width;
417 float t1 = y1 / (float)surf->height;
418 float s2 = x2 / (float)surf->width;
419 float t2 = y2 / (float)surf->height;
420 float st[4][2];
421
422 st[0][0] = s1;
423 st[0][1] = t1;
424 st[1][0] = s2;
425 st[1][1] = t1;
426 st[2][0] = s2;
427 st[2][1] = t2;
428 st[3][0] = s1;
429 st[3][1] = t2;
430
431 util_map_texcoords2d_onto_cubemap(surf->face,
432 /* pointer, stride in floats */
433 &st[0][0], 2,
434 &ctx->vertices[0][1][0], 8);
435
436 for (i = 0; i < 4; i++)
437 ctx->vertices[i][1][3] = 1; /*q*/
438 }
439
440 static void blitter_draw_quad(struct blitter_context_priv *ctx)
441 {
442 struct pipe_context *pipe = ctx->pipe;
443
444 /* write vertices and draw them */
445 pipe_buffer_write(pipe->screen, ctx->vbuf,
446 0, sizeof(ctx->vertices), ctx->vertices);
447
448 util_draw_vertex_buffer(pipe, ctx->vbuf, 0, PIPE_PRIM_TRIANGLE_FAN,
449 4, /* verts */
450 2); /* attribs/vert */
451 }
452
453 static INLINE
454 void **blitter_get_sampler_state(struct blitter_context_priv *ctx,
455 int miplevel)
456 {
457 struct pipe_context *pipe = ctx->pipe;
458 struct pipe_sampler_state *sampler_state = &ctx->template_sampler_state;
459
460 assert(miplevel < PIPE_MAX_TEXTURE_LEVELS);
461
462 /* Create the sampler state on-demand. */
463 if (!ctx->sampler_state[miplevel]) {
464 sampler_state->lod_bias = miplevel;
465 sampler_state->min_lod = miplevel;
466 sampler_state->max_lod = miplevel;
467
468 ctx->sampler_state[miplevel] = pipe->create_sampler_state(pipe,
469 sampler_state);
470 }
471
472 /* Return void** so that it can be passed to bind_fragment_sampler_states
473 * directly. */
474 return &ctx->sampler_state[miplevel];
475 }
476
477 static INLINE
478 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs)
479 {
480 struct pipe_context *pipe = ctx->pipe;
481 unsigned index = num_cbufs ? num_cbufs - 1 : 0;
482
483 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
484
485 if (!ctx->fs_col[index])
486 ctx->fs_col[index] =
487 util_make_fragment_clonecolor_shader(pipe, num_cbufs);
488
489 return ctx->fs_col[index];
490 }
491
492 static INLINE
493 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
494 unsigned tex_target)
495 {
496 struct pipe_context *pipe = ctx->pipe;
497
498 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
499
500 /* Create the fragment shader on-demand. */
501 if (!ctx->fs_texfetch_col[tex_target]) {
502 switch (tex_target) {
503 case PIPE_TEXTURE_1D:
504 ctx->fs_texfetch_col[PIPE_TEXTURE_1D] =
505 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_1D);
506 break;
507 case PIPE_TEXTURE_2D:
508 ctx->fs_texfetch_col[PIPE_TEXTURE_2D] =
509 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
510 break;
511 case PIPE_TEXTURE_3D:
512 ctx->fs_texfetch_col[PIPE_TEXTURE_3D] =
513 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_3D);
514 break;
515 case PIPE_TEXTURE_CUBE:
516 ctx->fs_texfetch_col[PIPE_TEXTURE_CUBE] =
517 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_CUBE);
518 break;
519 default:;
520 }
521 }
522
523 return ctx->fs_texfetch_col[tex_target];
524 }
525
526 static INLINE
527 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
528 unsigned tex_target)
529 {
530 struct pipe_context *pipe = ctx->pipe;
531
532 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
533
534 /* Create the fragment shader on-demand. */
535 if (!ctx->fs_texfetch_depth[tex_target]) {
536 switch (tex_target) {
537 case PIPE_TEXTURE_1D:
538 ctx->fs_texfetch_depth[PIPE_TEXTURE_1D] =
539 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_1D);
540 break;
541 case PIPE_TEXTURE_2D:
542 ctx->fs_texfetch_depth[PIPE_TEXTURE_2D] =
543 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_2D);
544 break;
545 case PIPE_TEXTURE_3D:
546 ctx->fs_texfetch_depth[PIPE_TEXTURE_3D] =
547 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_3D);
548 break;
549 case PIPE_TEXTURE_CUBE:
550 ctx->fs_texfetch_depth[PIPE_TEXTURE_CUBE] =
551 util_make_fragment_tex_shader_writedepth(pipe,TGSI_TEXTURE_CUBE);
552 break;
553 default:;
554 }
555 }
556
557 return ctx->fs_texfetch_depth[tex_target];
558 }
559
560 void util_blitter_clear(struct blitter_context *blitter,
561 unsigned width, unsigned height,
562 unsigned num_cbufs,
563 unsigned clear_buffers,
564 const float *rgba,
565 double depth, unsigned stencil)
566 {
567 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
568 struct pipe_context *pipe = ctx->pipe;
569 struct pipe_stencil_ref sr = { { 0 } };
570
571 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
572
573 blitter_check_saved_CSOs(ctx);
574
575 /* bind CSOs */
576 if (clear_buffers & PIPE_CLEAR_COLOR)
577 pipe->bind_blend_state(pipe, ctx->blend_write_color);
578 else
579 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
580
581 if (clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) {
582 sr.ref_value[0] = stencil & 0xff;
583 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
584 pipe->set_stencil_ref(pipe, &sr);
585 }
586 else
587 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
588
589 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
590 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
591 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs));
592 pipe->bind_vs_state(pipe, ctx->vs_col);
593
594 blitter_set_clear_color(ctx, rgba);
595 blitter_set_rectangle(ctx, 0, 0, width, height, width, height, depth);
596 blitter_draw_quad(ctx);
597 blitter_restore_CSOs(ctx);
598 }
599
600 static boolean
601 is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
602 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
603 {
604 if (sx1 >= dx2 || sx2 <= dx1 || sy1 >= dy2 || sy2 <= dy1) {
605 return FALSE;
606 } else {
607 return TRUE;
608 }
609 }
610
611 static void util_blitter_do_copy(struct blitter_context *blitter,
612 struct pipe_surface *dst,
613 unsigned dstx, unsigned dsty,
614 struct pipe_surface *src,
615 unsigned srcx, unsigned srcy,
616 unsigned width, unsigned height,
617 boolean is_depth)
618 {
619 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
620 struct pipe_context *pipe = ctx->pipe;
621 struct pipe_framebuffer_state fb_state;
622
623 assert(blitter->saved_fb_state.nr_cbufs != ~0);
624 assert(blitter->saved_num_textures != ~0);
625 assert(blitter->saved_num_sampler_states != ~0);
626 assert(src->texture->target < PIPE_MAX_TEXTURE_TYPES);
627
628 /* bind CSOs */
629 fb_state.width = dst->width;
630 fb_state.height = dst->height;
631
632 if (is_depth) {
633 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
634 pipe->bind_depth_stencil_alpha_state(pipe,
635 ctx->dsa_write_depth_keep_stencil);
636 pipe->bind_fs_state(pipe,
637 blitter_get_fs_texfetch_depth(ctx, src->texture->target));
638
639 fb_state.nr_cbufs = 0;
640 fb_state.zsbuf = dst;
641 } else {
642 pipe->bind_blend_state(pipe, ctx->blend_write_color);
643 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
644 pipe->bind_fs_state(pipe,
645 blitter_get_fs_texfetch_col(ctx, src->texture->target));
646
647 fb_state.nr_cbufs = 1;
648 fb_state.cbufs[0] = dst;
649 fb_state.zsbuf = 0;
650 }
651
652 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
653 pipe->bind_vs_state(pipe, ctx->vs_tex);
654 pipe->bind_fragment_sampler_states(pipe, 1,
655 blitter_get_sampler_state(ctx, src->level));
656 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
657 pipe->set_fragment_sampler_textures(pipe, 1, &src->texture);
658 pipe->set_framebuffer_state(pipe, &fb_state);
659
660 /* set texture coordinates */
661 switch (src->texture->target) {
662 case PIPE_TEXTURE_1D:
663 case PIPE_TEXTURE_2D:
664 blitter_set_texcoords_2d(ctx, src, srcx, srcy,
665 srcx+width, srcy+height);
666 break;
667 case PIPE_TEXTURE_3D:
668 blitter_set_texcoords_3d(ctx, src, srcx, srcy,
669 srcx+width, srcy+height);
670 break;
671 case PIPE_TEXTURE_CUBE:
672 blitter_set_texcoords_cube(ctx, src, srcx, srcy,
673 srcx+width, srcy+height);
674 break;
675 default:
676 assert(0);
677 }
678
679 blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height, dst->width, dst->height, 0);
680 blitter_draw_quad(ctx);
681
682 }
683
684 static void util_blitter_overlap_copy(struct blitter_context *blitter,
685 struct pipe_surface *dst,
686 unsigned dstx, unsigned dsty,
687 struct pipe_surface *src,
688 unsigned srcx, unsigned srcy,
689 unsigned width, unsigned height)
690 {
691 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
692 struct pipe_context *pipe = ctx->pipe;
693 struct pipe_screen *screen = pipe->screen;
694
695 struct pipe_texture texTemp;
696 struct pipe_texture *texture;
697 struct pipe_surface *tex_surf;
698
699 /* check whether the states are properly saved */
700 blitter_check_saved_CSOs(ctx);
701
702 memset(&texTemp, 0, sizeof(texTemp));
703 texTemp.target = PIPE_TEXTURE_2D;
704 texTemp.format = dst->texture->format; /* XXX verify supported by driver! */
705 texTemp.last_level = 0;
706 texTemp.width0 = width;
707 texTemp.height0 = height;
708 texTemp.depth0 = 1;
709
710 texture = screen->texture_create(screen, &texTemp);
711 if (!texture)
712 return;
713
714 tex_surf = screen->get_tex_surface(screen, texture, 0, 0, 0,
715 PIPE_BUFFER_USAGE_GPU_READ |
716 PIPE_BUFFER_USAGE_GPU_WRITE);
717
718 /* blit from the src to the temp */
719 util_blitter_do_copy(blitter, tex_surf, 0, 0,
720 src, srcx, srcy,
721 width, height,
722 FALSE);
723 util_blitter_do_copy(blitter, dst, dstx, dsty,
724 tex_surf, 0, 0,
725 width, height,
726 FALSE);
727 pipe_surface_reference(&tex_surf, NULL);
728 pipe_texture_reference(&texture, NULL);
729 blitter_restore_CSOs(ctx);
730 }
731
732 void util_blitter_copy(struct blitter_context *blitter,
733 struct pipe_surface *dst,
734 unsigned dstx, unsigned dsty,
735 struct pipe_surface *src,
736 unsigned srcx, unsigned srcy,
737 unsigned width, unsigned height,
738 boolean ignore_stencil)
739 {
740 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
741 struct pipe_context *pipe = ctx->pipe;
742 struct pipe_screen *screen = pipe->screen;
743 boolean is_stencil, is_depth;
744 unsigned dst_tex_usage;
745
746 /* give up if textures are not set */
747 assert(dst->texture && src->texture);
748 if (!dst->texture || !src->texture)
749 return;
750
751 if (dst->texture == src->texture) {
752 if (is_overlap(srcx, srcx + width, srcy, srcy + height,
753 dstx, dstx + width, dsty, dsty + height)) {
754 util_blitter_overlap_copy(blitter, dst, dstx, dsty, src, srcx, srcy,
755 width, height);
756 return;
757 }
758 }
759
760 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
761 is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
762 dst_tex_usage = is_depth || is_stencil ? PIPE_TEXTURE_USAGE_DEPTH_STENCIL :
763 PIPE_TEXTURE_USAGE_RENDER_TARGET;
764
765 /* check if we can sample from and render to the surfaces */
766 /* (assuming copying a stencil buffer is not possible) */
767 if ((!ignore_stencil && is_stencil) ||
768 !screen->is_format_supported(screen, dst->format, dst->texture->target,
769 dst_tex_usage, 0) ||
770 !screen->is_format_supported(screen, src->format, src->texture->target,
771 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
772 util_surface_copy(pipe, FALSE, dst, dstx, dsty, src, srcx, srcy,
773 width, height);
774 return;
775 }
776
777 /* check whether the states are properly saved */
778 blitter_check_saved_CSOs(ctx);
779 util_blitter_do_copy(blitter,
780 dst, dstx, dsty,
781 src, srcx, srcy,
782 width, height, is_depth);
783 blitter_restore_CSOs(ctx);
784 }
785
786 void util_blitter_fill(struct blitter_context *blitter,
787 struct pipe_surface *dst,
788 unsigned dstx, unsigned dsty,
789 unsigned width, unsigned height,
790 unsigned value)
791 {
792 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
793 struct pipe_context *pipe = ctx->pipe;
794 struct pipe_screen *screen = pipe->screen;
795 struct pipe_framebuffer_state fb_state;
796 float rgba[4];
797 ubyte ub_rgba[4] = {0};
798 union util_color color;
799 int i;
800
801 assert(dst->texture);
802 if (!dst->texture)
803 return;
804
805 /* check if we can render to the surface */
806 if (util_format_is_depth_or_stencil(dst->format) || /* unlikely, but you never know */
807 !screen->is_format_supported(screen, dst->format, dst->texture->target,
808 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
809 util_surface_fill(pipe, dst, dstx, dsty, width, height, value);
810 return;
811 }
812
813 /* unpack the color */
814 color.ui = value;
815 util_unpack_color_ub(dst->format, &color,
816 ub_rgba, ub_rgba+1, ub_rgba+2, ub_rgba+3);
817 for (i = 0; i < 4; i++)
818 rgba[i] = ubyte_to_float(ub_rgba[i]);
819
820 /* check the saved state */
821 blitter_check_saved_CSOs(ctx);
822 assert(blitter->saved_fb_state.nr_cbufs != ~0);
823
824 /* bind CSOs */
825 pipe->bind_blend_state(pipe, ctx->blend_write_color);
826 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
827 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
828 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
829 pipe->bind_vs_state(pipe, ctx->vs_col);
830 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
831
832 /* set a framebuffer state */
833 fb_state.width = dst->width;
834 fb_state.height = dst->height;
835 fb_state.nr_cbufs = 1;
836 fb_state.cbufs[0] = dst;
837 fb_state.zsbuf = 0;
838 pipe->set_framebuffer_state(pipe, &fb_state);
839
840 blitter_set_clear_color(ctx, rgba);
841 blitter_set_rectangle(ctx, 0, 0, width, height, dst->width, dst->height, 0);
842 blitter_draw_quad(ctx);
843 blitter_restore_CSOs(ctx);
844 }