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