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