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