Merge remote branch 'origin/master' into lp-binning
[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 "pipe/p_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_depth_stencil_alpha_state template_dsa;
64 struct pipe_sampler_state template_sampler_state;
65
66 /* Constant state objects. */
67 /* Vertex shaders. */
68 void *vs_col; /**< Vertex shader which passes {pos, color} to the output */
69 void *vs_tex; /**<Vertex shader which passes {pos, texcoord} to the output.*/
70
71 /* Fragment shaders. */
72 /* FS which outputs a color to multiple color buffers. */
73 void *fs_col[PIPE_MAX_COLOR_BUFS];
74
75 /* FS which outputs a color from a texture,
76 where the index is PIPE_TEXTURE_* to be sampled. */
77 void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
78
79 /* FS which outputs a depth from a texture,
80 where the index is PIPE_TEXTURE_* to be sampled. */
81 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
82
83 /* Blend state. */
84 void *blend_write_color; /**< blend state with writemask of RGBA */
85 void *blend_keep_color; /**< blend state with writemask of 0 */
86
87 /* Depth stencil alpha state. */
88 void *dsa_write_depth_stencil[0xff]; /**< indices are stencil clear values */
89 void *dsa_write_depth_keep_stencil;
90 void *dsa_keep_depth_stencil;
91
92 /* Sampler state for clamping to a miplevel. */
93 void *sampler_state[PIPE_MAX_TEXTURE_LEVELS];
94
95 /* Rasterizer state. */
96 void *rs_state;
97 };
98
99 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
100 {
101 struct blitter_context_priv *ctx;
102 struct pipe_blend_state blend;
103 struct pipe_depth_stencil_alpha_state *dsa;
104 struct pipe_rasterizer_state rs_state;
105 struct pipe_sampler_state *sampler_state;
106 unsigned i;
107
108 ctx = CALLOC_STRUCT(blitter_context_priv);
109 if (!ctx)
110 return NULL;
111
112 ctx->pipe = pipe;
113
114 /* init state objects for them to be considered invalid */
115 ctx->blitter.saved_blend_state = INVALID_PTR;
116 ctx->blitter.saved_dsa_state = INVALID_PTR;
117 ctx->blitter.saved_rs_state = INVALID_PTR;
118 ctx->blitter.saved_fs = INVALID_PTR;
119 ctx->blitter.saved_vs = INVALID_PTR;
120 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
121 ctx->blitter.saved_num_textures = ~0;
122 ctx->blitter.saved_num_sampler_states = ~0;
123
124 /* blend state objects */
125 memset(&blend, 0, sizeof(blend));
126 ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
127
128 blend.colormask = PIPE_MASK_RGBA;
129 ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
130
131 /* depth stencil alpha state objects */
132 dsa = &ctx->template_dsa;
133 ctx->dsa_keep_depth_stencil =
134 pipe->create_depth_stencil_alpha_state(pipe, dsa);
135
136 dsa->depth.enabled = 1;
137 dsa->depth.writemask = 1;
138 dsa->depth.func = PIPE_FUNC_ALWAYS;
139 ctx->dsa_write_depth_keep_stencil =
140 pipe->create_depth_stencil_alpha_state(pipe, dsa);
141
142 dsa->stencil[0].enabled = 1;
143 dsa->stencil[0].func = PIPE_FUNC_ALWAYS;
144 dsa->stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
145 dsa->stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
146 dsa->stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
147 dsa->stencil[0].valuemask = 0xff;
148 dsa->stencil[0].writemask = 0xff;
149 /* The DSA state objects which write depth and stencil are created
150 * on-demand. */
151
152 /* sampler state */
153 sampler_state = &ctx->template_sampler_state;
154 sampler_state->wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
155 sampler_state->wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
156 sampler_state->wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
157 /* The sampler state objects which sample from a specified mipmap level
158 * are created on-demand. */
159
160 /* rasterizer state */
161 memset(&rs_state, 0, sizeof(rs_state));
162 rs_state.front_winding = PIPE_WINDING_CW;
163 rs_state.cull_mode = PIPE_WINDING_NONE;
164 rs_state.bypass_vs_clip_and_viewport = 1;
165 rs_state.gl_rasterization_rules = 1;
166 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
167
168 /* fragment shaders are created on-demand */
169
170 /* vertex shaders */
171 {
172 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
173 TGSI_SEMANTIC_COLOR };
174 const uint semantic_indices[] = { 0, 0 };
175 ctx->vs_col =
176 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
177 semantic_indices);
178 }
179 {
180 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
181 TGSI_SEMANTIC_GENERIC };
182 const uint semantic_indices[] = { 0, 0 };
183 ctx->vs_tex =
184 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
185 semantic_indices);
186 }
187
188 /* set invariant vertex coordinates */
189 for (i = 0; i < 4; i++)
190 ctx->vertices[i][0][3] = 1; /*v.w*/
191
192 /* create the vertex buffer */
193 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
194 32,
195 PIPE_BUFFER_USAGE_VERTEX,
196 sizeof(ctx->vertices));
197
198 return &ctx->blitter;
199 }
200
201 void util_blitter_destroy(struct blitter_context *blitter)
202 {
203 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
204 struct pipe_context *pipe = ctx->pipe;
205 int i;
206
207 pipe->delete_blend_state(pipe, ctx->blend_write_color);
208 pipe->delete_blend_state(pipe, ctx->blend_keep_color);
209 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
210 pipe->delete_depth_stencil_alpha_state(pipe,
211 ctx->dsa_write_depth_keep_stencil);
212
213 for (i = 0; i < 0xff; i++)
214 if (ctx->dsa_write_depth_stencil[i])
215 pipe->delete_depth_stencil_alpha_state(pipe,
216 ctx->dsa_write_depth_stencil[i]);
217
218 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
219 pipe->delete_vs_state(pipe, ctx->vs_col);
220 pipe->delete_vs_state(pipe, ctx->vs_tex);
221
222 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
223 if (ctx->fs_texfetch_col[i])
224 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
225 if (ctx->fs_texfetch_depth[i])
226 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
227 }
228
229 for (i = 0; i < PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
230 if (ctx->fs_col[i])
231 pipe->delete_fs_state(pipe, ctx->fs_col[i]);
232
233 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
234 if (ctx->sampler_state[i])
235 pipe->delete_sampler_state(pipe, ctx->sampler_state[i]);
236
237 pipe_buffer_reference(&ctx->vbuf, NULL);
238 FREE(ctx);
239 }
240
241 static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx)
242 {
243 /* make sure these CSOs have been saved */
244 assert(ctx->blitter.saved_blend_state != INVALID_PTR &&
245 ctx->blitter.saved_dsa_state != INVALID_PTR &&
246 ctx->blitter.saved_rs_state != INVALID_PTR &&
247 ctx->blitter.saved_fs != INVALID_PTR &&
248 ctx->blitter.saved_vs != INVALID_PTR);
249 }
250
251 static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
252 {
253 struct pipe_context *pipe = ctx->pipe;
254
255 /* restore the state objects which are always required to be saved */
256 pipe->bind_blend_state(pipe, ctx->blitter.saved_blend_state);
257 pipe->bind_depth_stencil_alpha_state(pipe, ctx->blitter.saved_dsa_state);
258 pipe->bind_rasterizer_state(pipe, ctx->blitter.saved_rs_state);
259 pipe->bind_fs_state(pipe, ctx->blitter.saved_fs);
260 pipe->bind_vs_state(pipe, ctx->blitter.saved_vs);
261
262 ctx->blitter.saved_blend_state = INVALID_PTR;
263 ctx->blitter.saved_dsa_state = INVALID_PTR;
264 ctx->blitter.saved_rs_state = INVALID_PTR;
265 ctx->blitter.saved_fs = INVALID_PTR;
266 ctx->blitter.saved_vs = INVALID_PTR;
267
268 /* restore the state objects which are required to be saved before copy/fill
269 */
270 if (ctx->blitter.saved_fb_state.nr_cbufs != ~0) {
271 pipe->set_framebuffer_state(pipe, &ctx->blitter.saved_fb_state);
272 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
273 }
274
275 if (ctx->blitter.saved_num_sampler_states != ~0) {
276 pipe->bind_fragment_sampler_states(pipe,
277 ctx->blitter.saved_num_sampler_states,
278 ctx->blitter.saved_sampler_states);
279 ctx->blitter.saved_num_sampler_states = ~0;
280 }
281
282 if (ctx->blitter.saved_num_textures != ~0) {
283 pipe->set_fragment_sampler_textures(pipe,
284 ctx->blitter.saved_num_textures,
285 ctx->blitter.saved_textures);
286 ctx->blitter.saved_num_textures = ~0;
287 }
288 }
289
290 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
291 unsigned x1, unsigned y1,
292 unsigned x2, unsigned y2,
293 float depth)
294 {
295 int i;
296
297 /* set vertex positions */
298 ctx->vertices[0][0][0] = x1; /*v0.x*/
299 ctx->vertices[0][0][1] = y1; /*v0.y*/
300
301 ctx->vertices[1][0][0] = x2; /*v1.x*/
302 ctx->vertices[1][0][1] = y1; /*v1.y*/
303
304 ctx->vertices[2][0][0] = x2; /*v2.x*/
305 ctx->vertices[2][0][1] = y2; /*v2.y*/
306
307 ctx->vertices[3][0][0] = x1; /*v3.x*/
308 ctx->vertices[3][0][1] = y2; /*v3.y*/
309
310 for (i = 0; i < 4; i++)
311 ctx->vertices[i][0][2] = depth; /*z*/
312 }
313
314 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
315 const float *rgba)
316 {
317 int i;
318
319 for (i = 0; i < 4; i++) {
320 ctx->vertices[i][1][0] = rgba[0];
321 ctx->vertices[i][1][1] = rgba[1];
322 ctx->vertices[i][1][2] = rgba[2];
323 ctx->vertices[i][1][3] = rgba[3];
324 }
325 }
326
327 static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
328 struct pipe_surface *surf,
329 unsigned x1, unsigned y1,
330 unsigned x2, unsigned y2)
331 {
332 int i;
333 float s1 = x1 / (float)surf->width;
334 float t1 = y1 / (float)surf->height;
335 float s2 = x2 / (float)surf->width;
336 float t2 = y2 / (float)surf->height;
337
338 ctx->vertices[0][1][0] = s1; /*t0.s*/
339 ctx->vertices[0][1][1] = t1; /*t0.t*/
340
341 ctx->vertices[1][1][0] = s2; /*t1.s*/
342 ctx->vertices[1][1][1] = t1; /*t1.t*/
343
344 ctx->vertices[2][1][0] = s2; /*t2.s*/
345 ctx->vertices[2][1][1] = t2; /*t2.t*/
346
347 ctx->vertices[3][1][0] = s1; /*t3.s*/
348 ctx->vertices[3][1][1] = t2; /*t3.t*/
349
350 for (i = 0; i < 4; i++) {
351 ctx->vertices[i][1][2] = 0; /*r*/
352 ctx->vertices[i][1][3] = 1; /*q*/
353 }
354 }
355
356 static void blitter_set_texcoords_3d(struct blitter_context_priv *ctx,
357 struct pipe_surface *surf,
358 unsigned x1, unsigned y1,
359 unsigned x2, unsigned y2)
360 {
361 int i;
362 float depth = u_minify(surf->texture->depth0, surf->level);
363 float r = surf->zslice / depth;
364
365 blitter_set_texcoords_2d(ctx, surf, x1, y1, x2, y2);
366
367 for (i = 0; i < 4; i++)
368 ctx->vertices[i][1][2] = r; /*r*/
369 }
370
371 static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
372 struct pipe_surface *surf,
373 unsigned x1, unsigned y1,
374 unsigned x2, unsigned y2)
375 {
376 int i;
377 float s1 = x1 / (float)surf->width;
378 float t1 = y1 / (float)surf->height;
379 float s2 = x2 / (float)surf->width;
380 float t2 = y2 / (float)surf->height;
381 const float st[4][2] = {
382 {s1, t1}, {s2, t1}, {s2, t2}, {s1, t2}
383 };
384
385 util_map_texcoords2d_onto_cubemap(surf->face,
386 /* pointer, stride in floats */
387 &st[0][0], 2,
388 &ctx->vertices[0][1][0], 8);
389
390 for (i = 0; i < 4; i++)
391 ctx->vertices[i][1][3] = 1; /*q*/
392 }
393
394 static void blitter_draw_quad(struct blitter_context_priv *ctx)
395 {
396 struct pipe_context *pipe = ctx->pipe;
397
398 /* write vertices and draw them */
399 pipe_buffer_write(pipe->screen, ctx->vbuf,
400 0, sizeof(ctx->vertices), ctx->vertices);
401
402 util_draw_vertex_buffer(pipe, ctx->vbuf, 0, PIPE_PRIM_TRIANGLE_FAN,
403 4, /* verts */
404 2); /* attribs/vert */
405 }
406
407 static INLINE
408 void *blitter_get_state_write_depth_stencil(
409 struct blitter_context_priv *ctx,
410 unsigned stencil)
411 {
412 struct pipe_context *pipe = ctx->pipe;
413
414 stencil &= 0xff;
415
416 /* Create the DSA state on-demand. */
417 if (!ctx->dsa_write_depth_stencil[stencil]) {
418 ctx->template_dsa.stencil[0].ref_value = stencil;
419
420 ctx->dsa_write_depth_stencil[stencil] =
421 pipe->create_depth_stencil_alpha_state(pipe, &ctx->template_dsa);
422 }
423
424 return ctx->dsa_write_depth_stencil[stencil];
425 }
426
427 static INLINE
428 void **blitter_get_sampler_state(struct blitter_context_priv *ctx,
429 int miplevel)
430 {
431 struct pipe_context *pipe = ctx->pipe;
432 struct pipe_sampler_state *sampler_state = &ctx->template_sampler_state;
433
434 assert(miplevel < PIPE_MAX_TEXTURE_LEVELS);
435
436 /* Create the sampler state on-demand. */
437 if (!ctx->sampler_state[miplevel]) {
438 sampler_state->lod_bias = miplevel;
439 sampler_state->min_lod = miplevel;
440 sampler_state->max_lod = miplevel;
441
442 ctx->sampler_state[miplevel] = pipe->create_sampler_state(pipe,
443 sampler_state);
444 }
445
446 /* Return void** so that it can be passed to bind_fragment_sampler_states
447 * directly. */
448 return &ctx->sampler_state[miplevel];
449 }
450
451 static INLINE
452 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs)
453 {
454 struct pipe_context *pipe = ctx->pipe;
455 unsigned index = num_cbufs ? num_cbufs - 1 : 0;
456
457 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
458
459 if (!ctx->fs_col[index])
460 ctx->fs_col[index] =
461 util_make_fragment_clonecolor_shader(pipe, num_cbufs);
462
463 return ctx->fs_col[index];
464 }
465
466 static INLINE
467 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
468 unsigned tex_target)
469 {
470 struct pipe_context *pipe = ctx->pipe;
471
472 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
473
474 /* Create the fragment shader on-demand. */
475 if (!ctx->fs_texfetch_col[tex_target]) {
476 switch (tex_target) {
477 case PIPE_TEXTURE_1D:
478 ctx->fs_texfetch_col[PIPE_TEXTURE_1D] =
479 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_1D);
480 break;
481 case PIPE_TEXTURE_2D:
482 ctx->fs_texfetch_col[PIPE_TEXTURE_2D] =
483 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
484 break;
485 case PIPE_TEXTURE_3D:
486 ctx->fs_texfetch_col[PIPE_TEXTURE_3D] =
487 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_3D);
488 break;
489 case PIPE_TEXTURE_CUBE:
490 ctx->fs_texfetch_col[PIPE_TEXTURE_CUBE] =
491 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_CUBE);
492 break;
493 default:;
494 }
495 }
496
497 return ctx->fs_texfetch_col[tex_target];
498 }
499
500 static INLINE
501 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
502 unsigned tex_target)
503 {
504 struct pipe_context *pipe = ctx->pipe;
505
506 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
507
508 /* Create the fragment shader on-demand. */
509 if (!ctx->fs_texfetch_depth[tex_target]) {
510 switch (tex_target) {
511 case PIPE_TEXTURE_1D:
512 ctx->fs_texfetch_depth[PIPE_TEXTURE_1D] =
513 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_1D);
514 break;
515 case PIPE_TEXTURE_2D:
516 ctx->fs_texfetch_depth[PIPE_TEXTURE_2D] =
517 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_2D);
518 break;
519 case PIPE_TEXTURE_3D:
520 ctx->fs_texfetch_depth[PIPE_TEXTURE_3D] =
521 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_3D);
522 break;
523 case PIPE_TEXTURE_CUBE:
524 ctx->fs_texfetch_depth[PIPE_TEXTURE_CUBE] =
525 util_make_fragment_tex_shader_writedepth(pipe,TGSI_TEXTURE_CUBE);
526 break;
527 default:;
528 }
529 }
530
531 return ctx->fs_texfetch_depth[tex_target];
532 }
533
534 void util_blitter_clear(struct blitter_context *blitter,
535 unsigned width, unsigned height,
536 unsigned num_cbufs,
537 unsigned clear_buffers,
538 const float *rgba,
539 double depth, unsigned stencil)
540 {
541 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
542 struct pipe_context *pipe = ctx->pipe;
543
544 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
545
546 blitter_check_saved_CSOs(ctx);
547
548 /* bind CSOs */
549 if (clear_buffers & PIPE_CLEAR_COLOR)
550 pipe->bind_blend_state(pipe, ctx->blend_write_color);
551 else
552 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
553
554 if (clear_buffers & PIPE_CLEAR_DEPTHSTENCIL)
555 pipe->bind_depth_stencil_alpha_state(pipe,
556 blitter_get_state_write_depth_stencil(ctx, stencil));
557 else
558 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
559
560 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
561 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs));
562 pipe->bind_vs_state(pipe, ctx->vs_col);
563
564 blitter_set_clear_color(ctx, rgba);
565 blitter_set_rectangle(ctx, 0, 0, width, height, depth);
566 blitter_draw_quad(ctx);
567 blitter_restore_CSOs(ctx);
568 }
569
570 void util_blitter_copy(struct blitter_context *blitter,
571 struct pipe_surface *dst,
572 unsigned dstx, unsigned dsty,
573 struct pipe_surface *src,
574 unsigned srcx, unsigned srcy,
575 unsigned width, unsigned height,
576 boolean ignore_stencil)
577 {
578 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
579 struct pipe_context *pipe = ctx->pipe;
580 struct pipe_screen *screen = pipe->screen;
581 struct pipe_framebuffer_state fb_state;
582 boolean is_stencil, is_depth;
583 unsigned dst_tex_usage;
584
585 /* give up if textures are not set */
586 assert(dst->texture && src->texture);
587 if (!dst->texture || !src->texture)
588 return;
589
590 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
591 is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
592 dst_tex_usage = is_depth || is_stencil ? PIPE_TEXTURE_USAGE_DEPTH_STENCIL :
593 PIPE_TEXTURE_USAGE_RENDER_TARGET;
594
595 /* check if we can sample from and render to the surfaces */
596 /* (assuming copying a stencil buffer is not possible) */
597 if ((!ignore_stencil && is_stencil) ||
598 !screen->is_format_supported(screen, dst->format, dst->texture->target,
599 dst_tex_usage, 0) ||
600 !screen->is_format_supported(screen, src->format, src->texture->target,
601 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
602 util_surface_copy(pipe, FALSE, dst, dstx, dsty, src, srcx, srcy,
603 width, height);
604 return;
605 }
606
607 /* check whether the states are properly saved */
608 blitter_check_saved_CSOs(ctx);
609 assert(blitter->saved_fb_state.nr_cbufs != ~0);
610 assert(blitter->saved_num_textures != ~0);
611 assert(blitter->saved_num_sampler_states != ~0);
612 assert(src->texture->target < PIPE_MAX_TEXTURE_TYPES);
613
614 /* bind CSOs */
615 fb_state.width = dst->width;
616 fb_state.height = dst->height;
617
618 if (is_depth) {
619 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
620 pipe->bind_depth_stencil_alpha_state(pipe,
621 ctx->dsa_write_depth_keep_stencil);
622 pipe->bind_fs_state(pipe,
623 blitter_get_fs_texfetch_depth(ctx, src->texture->target));
624
625 fb_state.nr_cbufs = 0;
626 fb_state.zsbuf = dst;
627 } else {
628 pipe->bind_blend_state(pipe, ctx->blend_write_color);
629 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
630 pipe->bind_fs_state(pipe,
631 blitter_get_fs_texfetch_col(ctx, src->texture->target));
632
633 fb_state.nr_cbufs = 1;
634 fb_state.cbufs[0] = dst;
635 fb_state.zsbuf = 0;
636 }
637
638 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
639 pipe->bind_vs_state(pipe, ctx->vs_tex);
640 pipe->bind_fragment_sampler_states(pipe, 1,
641 blitter_get_sampler_state(ctx, src->level));
642 pipe->set_fragment_sampler_textures(pipe, 1, &src->texture);
643 pipe->set_framebuffer_state(pipe, &fb_state);
644
645 /* set texture coordinates */
646 switch (src->texture->target) {
647 case PIPE_TEXTURE_1D:
648 case PIPE_TEXTURE_2D:
649 blitter_set_texcoords_2d(ctx, src, srcx, srcy,
650 srcx+width, srcy+height);
651 break;
652 case PIPE_TEXTURE_3D:
653 blitter_set_texcoords_3d(ctx, src, srcx, srcy,
654 srcx+width, srcy+height);
655 break;
656 case PIPE_TEXTURE_CUBE:
657 blitter_set_texcoords_cube(ctx, src, srcx, srcy,
658 srcx+width, srcy+height);
659 break;
660 default:
661 assert(0);
662 }
663
664 blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height, 0);
665 blitter_draw_quad(ctx);
666 blitter_restore_CSOs(ctx);
667 }
668
669 void util_blitter_fill(struct blitter_context *blitter,
670 struct pipe_surface *dst,
671 unsigned dstx, unsigned dsty,
672 unsigned width, unsigned height,
673 unsigned value)
674 {
675 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
676 struct pipe_context *pipe = ctx->pipe;
677 struct pipe_screen *screen = pipe->screen;
678 struct pipe_framebuffer_state fb_state;
679 float rgba[4];
680 ubyte ub_rgba[4] = {0};
681 union util_color color;
682 int i;
683
684 assert(dst->texture);
685 if (!dst->texture)
686 return;
687
688 /* check if we can render to the surface */
689 if (util_format_is_depth_or_stencil(dst->format) || /* unlikely, but you never know */
690 !screen->is_format_supported(screen, dst->format, dst->texture->target,
691 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
692 util_surface_fill(pipe, dst, dstx, dsty, width, height, value);
693 return;
694 }
695
696 /* unpack the color */
697 color.ui = value;
698 util_unpack_color_ub(dst->format, &color,
699 ub_rgba, ub_rgba+1, ub_rgba+2, ub_rgba+3);
700 for (i = 0; i < 4; i++)
701 rgba[i] = ubyte_to_float(ub_rgba[i]);
702
703 /* check the saved state */
704 blitter_check_saved_CSOs(ctx);
705 assert(blitter->saved_fb_state.nr_cbufs != ~0);
706
707 /* bind CSOs */
708 pipe->bind_blend_state(pipe, ctx->blend_write_color);
709 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
710 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
711 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
712 pipe->bind_vs_state(pipe, ctx->vs_col);
713
714 /* set a framebuffer state */
715 fb_state.width = dst->width;
716 fb_state.height = dst->height;
717 fb_state.nr_cbufs = 1;
718 fb_state.cbufs[0] = dst;
719 fb_state.zsbuf = 0;
720 pipe->set_framebuffer_state(pipe, &fb_state);
721
722 blitter_set_clear_color(ctx, rgba);
723 blitter_set_rectangle(ctx, 0, 0, width, height, 0);
724 blitter_draw_quad(ctx);
725 blitter_restore_CSOs(ctx);
726 }