1 /**************************************************************************
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target,
30 * clear_depth_stencil, and resource_copy_region functions.
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"
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_sampler.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_surface.h"
49 #include "util/u_texture.h"
50 #include "util/u_upload_mgr.h"
52 #define INVALID_PTR ((void*)~0)
54 struct blitter_context_priv
56 struct blitter_context base;
58 struct u_upload_mgr *upload;
60 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
62 /* Templates for various state objects. */
64 /* Constant state objects. */
66 void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
67 void *vs_pos_only; /**< Vertex shader which passes pos to the output.*/
69 /* Fragment shaders. */
70 /* The shader at index i outputs color to color buffers 0,1,...,i-1. */
71 void *fs_col[PIPE_MAX_COLOR_BUFS+1];
72 void *fs_col_int[PIPE_MAX_COLOR_BUFS+1];
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];
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 void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES];
82 void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES];
84 /* FS which outputs one sample from a multisample texture. */
85 void *fs_texfetch_col_msaa[PIPE_MAX_TEXTURE_TYPES];
86 void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
87 void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
88 void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
91 void *blend_write_color; /**< blend state with writemask of RGBA */
92 void *blend_keep_color; /**< blend state with writemask of 0 */
94 /* Depth stencil alpha state. */
95 void *dsa_write_depth_stencil;
96 void *dsa_write_depth_keep_stencil;
97 void *dsa_keep_depth_stencil;
98 void *dsa_keep_depth_write_stencil;
100 /* Vertex elements states. */
102 void *velem_uint_state;
103 void *velem_sint_state;
104 void *velem_state_readbuf;
109 /* Rasterizer state. */
111 void *rs_discard_state;
113 /* Viewport state. */
114 struct pipe_viewport_state viewport;
116 /* Destination surface dimensions. */
120 boolean has_geometry_shader;
121 boolean vertex_has_integers;
122 boolean has_stream_out;
123 boolean has_stencil_export;
127 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
129 struct blitter_context_priv *ctx;
130 struct pipe_blend_state blend;
131 struct pipe_depth_stencil_alpha_state dsa;
132 struct pipe_rasterizer_state rs_state;
133 struct pipe_sampler_state sampler_state;
134 struct pipe_vertex_element velem[2];
137 ctx = CALLOC_STRUCT(blitter_context_priv);
141 ctx->base.pipe = pipe;
142 ctx->base.draw_rectangle = util_blitter_draw_rectangle;
144 /* init state objects for them to be considered invalid */
145 ctx->base.saved_blend_state = INVALID_PTR;
146 ctx->base.saved_dsa_state = INVALID_PTR;
147 ctx->base.saved_rs_state = INVALID_PTR;
148 ctx->base.saved_fs = INVALID_PTR;
149 ctx->base.saved_vs = INVALID_PTR;
150 ctx->base.saved_gs = INVALID_PTR;
151 ctx->base.saved_velem_state = INVALID_PTR;
152 ctx->base.saved_fb_state.nr_cbufs = ~0;
153 ctx->base.saved_num_sampler_views = ~0;
154 ctx->base.saved_num_sampler_states = ~0;
155 ctx->base.saved_num_vertex_buffers = ~0;
156 ctx->base.saved_num_so_targets = ~0;
158 ctx->has_geometry_shader =
159 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
160 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
161 ctx->vertex_has_integers =
162 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_VERTEX,
163 PIPE_SHADER_CAP_INTEGERS);
164 ctx->has_stream_out =
165 pipe->screen->get_param(pipe->screen,
166 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
168 ctx->has_stencil_export =
169 pipe->screen->get_param(pipe->screen,
170 PIPE_CAP_SHADER_STENCIL_EXPORT);
172 /* blend state objects */
173 memset(&blend, 0, sizeof(blend));
174 ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
176 blend.rt[0].colormask = PIPE_MASK_RGBA;
177 ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
179 /* depth stencil alpha state objects */
180 memset(&dsa, 0, sizeof(dsa));
181 ctx->dsa_keep_depth_stencil =
182 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
184 dsa.depth.enabled = 1;
185 dsa.depth.writemask = 1;
186 dsa.depth.func = PIPE_FUNC_ALWAYS;
187 ctx->dsa_write_depth_keep_stencil =
188 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
190 dsa.stencil[0].enabled = 1;
191 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
192 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
193 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
194 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
195 dsa.stencil[0].valuemask = 0xff;
196 dsa.stencil[0].writemask = 0xff;
197 ctx->dsa_write_depth_stencil =
198 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
200 dsa.depth.enabled = 0;
201 dsa.depth.writemask = 0;
202 ctx->dsa_keep_depth_write_stencil =
203 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
206 memset(&sampler_state, 0, sizeof(sampler_state));
207 sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
208 sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
209 sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
210 sampler_state.normalized_coords = 1;
211 ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
213 /* rasterizer state */
214 memset(&rs_state, 0, sizeof(rs_state));
215 rs_state.cull_face = PIPE_FACE_NONE;
216 rs_state.gl_rasterization_rules = 1;
217 rs_state.flatshade = 1;
218 rs_state.depth_clip = 1;
219 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
221 if (ctx->has_stream_out) {
222 rs_state.rasterizer_discard = 1;
223 ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
226 /* vertex elements states */
227 memset(&velem[0], 0, sizeof(velem[0]) * 2);
228 for (i = 0; i < 2; i++) {
229 velem[i].src_offset = i * 4 * sizeof(float);
230 velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
232 ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
234 if (ctx->vertex_has_integers) {
235 memset(&velem[0], 0, sizeof(velem[0]) * 2);
236 velem[0].src_offset = 0;
237 velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
238 velem[1].src_offset = 4 * sizeof(float);
239 velem[1].src_format = PIPE_FORMAT_R32G32B32A32_SINT;
240 ctx->velem_sint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
242 memset(&velem[0], 0, sizeof(velem[0]) * 2);
243 velem[0].src_offset = 0;
244 velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
245 velem[1].src_offset = 4 * sizeof(float);
246 velem[1].src_format = PIPE_FORMAT_R32G32B32A32_UINT;
247 ctx->velem_uint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
250 if (ctx->has_stream_out) {
251 velem[0].src_format = PIPE_FORMAT_R32_UINT;
252 ctx->velem_state_readbuf = pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
255 /* fragment shaders are created on-demand */
259 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
260 TGSI_SEMANTIC_GENERIC };
261 const uint semantic_indices[] = { 0, 0 };
263 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
266 if (ctx->has_stream_out) {
267 struct pipe_stream_output_info so;
268 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
269 const uint semantic_indices[] = { 0 };
271 memset(&so, 0, sizeof(so));
273 so.output[0].num_components = 1;
277 util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
278 semantic_indices, &so);
281 /* set invariant vertex coordinates */
282 for (i = 0; i < 4; i++)
283 ctx->vertices[i][0][3] = 1; /*v.w*/
285 ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
290 void util_blitter_destroy(struct blitter_context *blitter)
292 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
293 struct pipe_context *pipe = blitter->pipe;
296 pipe->delete_blend_state(pipe, ctx->blend_write_color);
297 pipe->delete_blend_state(pipe, ctx->blend_keep_color);
298 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
299 pipe->delete_depth_stencil_alpha_state(pipe,
300 ctx->dsa_write_depth_keep_stencil);
301 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
302 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
304 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
305 if (ctx->rs_discard_state)
306 pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
307 pipe->delete_vs_state(pipe, ctx->vs);
308 if (ctx->vs_pos_only)
309 pipe->delete_vs_state(pipe, ctx->vs_pos_only);
310 pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
311 if (ctx->vertex_has_integers) {
312 pipe->delete_vertex_elements_state(pipe, ctx->velem_sint_state);
313 pipe->delete_vertex_elements_state(pipe, ctx->velem_uint_state);
315 if (ctx->velem_state_readbuf)
316 pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf);
318 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
319 if (ctx->fs_texfetch_col[i])
320 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
321 if (ctx->fs_texfetch_depth[i])
322 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
323 if (ctx->fs_texfetch_depthstencil[i])
324 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]);
325 if (ctx->fs_texfetch_stencil[i])
326 pipe->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]);
329 for (i = 0; i <= PIPE_MAX_COLOR_BUFS; i++) {
331 pipe->delete_fs_state(pipe, ctx->fs_col[i]);
332 if (ctx->fs_col_int[i])
333 pipe->delete_fs_state(pipe, ctx->fs_col_int[i]);
336 pipe->delete_sampler_state(pipe, ctx->sampler_state);
337 u_upload_destroy(ctx->upload);
341 static void blitter_set_running_flag(struct blitter_context_priv *ctx)
343 if (ctx->base.running) {
344 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
347 ctx->base.running = TRUE;
350 static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
352 if (!ctx->base.running) {
353 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
356 ctx->base.running = FALSE;
359 static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
361 assert(ctx->base.saved_num_vertex_buffers != ~0);
362 assert(ctx->base.saved_velem_state != INVALID_PTR);
363 assert(ctx->base.saved_vs != INVALID_PTR);
364 assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
365 assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0);
366 assert(ctx->base.saved_rs_state != INVALID_PTR);
369 static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
371 struct pipe_context *pipe = ctx->base.pipe;
374 /* Vertex buffers. */
375 pipe->set_vertex_buffers(pipe,
376 ctx->base.saved_num_vertex_buffers,
377 ctx->base.saved_vertex_buffers);
379 for (i = 0; i < ctx->base.saved_num_vertex_buffers; i++) {
380 if (ctx->base.saved_vertex_buffers[i].buffer) {
381 pipe_resource_reference(&ctx->base.saved_vertex_buffers[i].buffer,
385 ctx->base.saved_num_vertex_buffers = ~0;
387 /* Vertex elements. */
388 pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
389 ctx->base.saved_velem_state = INVALID_PTR;
392 pipe->bind_vs_state(pipe, ctx->base.saved_vs);
393 ctx->base.saved_vs = INVALID_PTR;
395 /* Geometry shader. */
396 if (ctx->has_geometry_shader) {
397 pipe->bind_gs_state(pipe, ctx->base.saved_gs);
398 ctx->base.saved_gs = INVALID_PTR;
401 /* Stream outputs. */
402 if (ctx->has_stream_out) {
403 pipe->set_stream_output_targets(pipe,
404 ctx->base.saved_num_so_targets,
405 ctx->base.saved_so_targets, ~0);
407 for (i = 0; i < ctx->base.saved_num_so_targets; i++)
408 pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
410 ctx->base.saved_num_so_targets = ~0;
414 pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
415 ctx->base.saved_rs_state = INVALID_PTR;
418 static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
420 assert(ctx->base.saved_fs != INVALID_PTR);
421 assert(ctx->base.saved_dsa_state != INVALID_PTR);
422 assert(ctx->base.saved_blend_state != INVALID_PTR);
425 static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
427 struct pipe_context *pipe = ctx->base.pipe;
429 /* Fragment shader. */
430 pipe->bind_fs_state(pipe, ctx->base.saved_fs);
431 ctx->base.saved_fs = INVALID_PTR;
433 /* Depth, stencil, alpha. */
434 pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
435 ctx->base.saved_dsa_state = INVALID_PTR;
438 pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
439 ctx->base.saved_blend_state = INVALID_PTR;
442 if (ctx->base.is_sample_mask_saved) {
443 pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
444 ctx->base.is_sample_mask_saved = FALSE;
447 /* Miscellaneous states. */
448 /* XXX check whether these are saved and whether they need to be restored
449 * (depending on the operation) */
450 pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
451 pipe->set_viewport_state(pipe, &ctx->base.saved_viewport);
454 static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
456 assert(ctx->base.saved_fb_state.nr_cbufs != ~0);
459 static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
461 struct pipe_context *pipe = ctx->base.pipe;
463 pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
464 util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
467 static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
469 assert(ctx->base.saved_num_sampler_states != ~0);
470 assert(ctx->base.saved_num_sampler_views != ~0);
473 static void blitter_restore_textures(struct blitter_context_priv *ctx)
475 struct pipe_context *pipe = ctx->base.pipe;
478 /* Fragment sampler states. */
479 pipe->bind_fragment_sampler_states(pipe,
480 ctx->base.saved_num_sampler_states,
481 ctx->base.saved_sampler_states);
482 ctx->base.saved_num_sampler_states = ~0;
484 /* Fragment sampler views. */
485 pipe->set_fragment_sampler_views(pipe,
486 ctx->base.saved_num_sampler_views,
487 ctx->base.saved_sampler_views);
489 for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
490 pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
492 ctx->base.saved_num_sampler_views = ~0;
495 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
496 unsigned x1, unsigned y1,
497 unsigned x2, unsigned y2,
502 /* set vertex positions */
503 ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
504 ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
506 ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
507 ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
509 ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
510 ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
512 ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
513 ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
515 for (i = 0; i < 4; i++)
516 ctx->vertices[i][0][2] = depth; /*z*/
519 ctx->viewport.scale[0] = 0.5f * ctx->dst_width;
520 ctx->viewport.scale[1] = 0.5f * ctx->dst_height;
521 ctx->viewport.scale[2] = 1.0f;
522 ctx->viewport.scale[3] = 1.0f;
523 ctx->viewport.translate[0] = 0.5f * ctx->dst_width;
524 ctx->viewport.translate[1] = 0.5f * ctx->dst_height;
525 ctx->viewport.translate[2] = 0.0f;
526 ctx->viewport.translate[3] = 0.0f;
527 ctx->base.pipe->set_viewport_state(ctx->base.pipe, &ctx->viewport);
530 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
531 const union pipe_color_union *color)
536 for (i = 0; i < 4; i++) {
537 uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1];
538 uiverts[0] = color->ui[0];
539 uiverts[1] = color->ui[1];
540 uiverts[2] = color->ui[2];
541 uiverts[3] = color->ui[3];
544 for (i = 0; i < 4; i++) {
545 ctx->vertices[i][1][0] = 0;
546 ctx->vertices[i][1][1] = 0;
547 ctx->vertices[i][1][2] = 0;
548 ctx->vertices[i][1][3] = 0;
553 static void get_texcoords(struct pipe_sampler_view *src,
554 unsigned src_width0, unsigned src_height0,
555 unsigned x1, unsigned y1,
556 unsigned x2, unsigned y2,
559 struct pipe_resource *tex = src->texture;
560 unsigned level = src->u.tex.first_level;
561 boolean normalized = tex->target != PIPE_TEXTURE_RECT &&
562 tex->nr_samples <= 1;
565 out[0] = x1 / (float)u_minify(src_width0, level);
566 out[1] = y1 / (float)u_minify(src_height0, level);
567 out[2] = x2 / (float)u_minify(src_width0, level);
568 out[3] = y2 / (float)u_minify(src_height0, level);
577 static void set_texcoords_in_vertices(const float coord[4],
578 float *out, unsigned stride)
580 out[0] = coord[0]; /*t0.s*/
581 out[1] = coord[1]; /*t0.t*/
583 out[0] = coord[2]; /*t1.s*/
584 out[1] = coord[1]; /*t1.t*/
586 out[0] = coord[2]; /*t2.s*/
587 out[1] = coord[3]; /*t2.t*/
589 out[0] = coord[0]; /*t3.s*/
590 out[1] = coord[3]; /*t3.t*/
593 static void blitter_set_texcoords(struct blitter_context_priv *ctx,
594 struct pipe_sampler_view *src,
595 unsigned src_width0, unsigned src_height0,
596 unsigned layer, unsigned sample,
597 unsigned x1, unsigned y1,
598 unsigned x2, unsigned y2)
602 float face_coord[4][2];
604 get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
606 if (src->texture->target == PIPE_TEXTURE_CUBE) {
607 set_texcoords_in_vertices(coord, &face_coord[0][0], 2);
608 util_map_texcoords2d_onto_cubemap(layer,
609 /* pointer, stride in floats */
610 &face_coord[0][0], 2,
611 &ctx->vertices[0][1][0], 8);
613 set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
617 switch (src->texture->target) {
618 case PIPE_TEXTURE_3D:
620 float r = layer / (float)u_minify(src->texture->depth0,
621 src->u.tex.first_level);
622 for (i = 0; i < 4; i++)
623 ctx->vertices[i][1][2] = r; /*r*/
627 case PIPE_TEXTURE_1D_ARRAY:
628 for (i = 0; i < 4; i++)
629 ctx->vertices[i][1][1] = layer; /*t*/
632 case PIPE_TEXTURE_2D_ARRAY:
633 for (i = 0; i < 4; i++) {
634 ctx->vertices[i][1][2] = layer; /*r*/
635 ctx->vertices[i][1][3] = sample; /*q*/
639 case PIPE_TEXTURE_2D:
640 for (i = 0; i < 4; i++) {
641 ctx->vertices[i][1][2] = sample; /*r*/
649 static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
650 unsigned width, unsigned height)
652 ctx->dst_width = width;
653 ctx->dst_height = height;
657 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs,
660 struct pipe_context *pipe = ctx->base.pipe;
662 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
665 if (!ctx->fs_col_int[num_cbufs])
666 ctx->fs_col_int[num_cbufs] =
667 util_make_fragment_cloneinput_shader(pipe, num_cbufs,
668 TGSI_SEMANTIC_GENERIC,
669 TGSI_INTERPOLATE_CONSTANT);
670 return ctx->fs_col_int[num_cbufs];
672 if (!ctx->fs_col[num_cbufs])
673 ctx->fs_col[num_cbufs] =
674 util_make_fragment_cloneinput_shader(pipe, num_cbufs,
675 TGSI_SEMANTIC_GENERIC,
676 TGSI_INTERPOLATE_LINEAR);
677 return ctx->fs_col[num_cbufs];
682 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
683 struct pipe_resource *tex)
685 struct pipe_context *pipe = ctx->base.pipe;
687 assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
689 if (tex->nr_samples > 1) {
690 void **shader = &ctx->fs_texfetch_col_msaa[tex->target];
692 /* Create the fragment shader on-demand. */
694 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
697 *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex);
702 void **shader = &ctx->fs_texfetch_col[tex->target];
704 /* Create the fragment shader on-demand. */
706 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
709 util_make_fragment_tex_shader(pipe, tgsi_tex,
710 TGSI_INTERPOLATE_LINEAR);
718 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
719 struct pipe_resource *tex)
721 struct pipe_context *pipe = ctx->base.pipe;
723 assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
725 if (tex->nr_samples > 1) {
726 void **shader = &ctx->fs_texfetch_depth_msaa[tex->target];
728 /* Create the fragment shader on-demand. */
730 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
734 util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
739 void **shader = &ctx->fs_texfetch_depth[tex->target];
741 /* Create the fragment shader on-demand. */
743 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
746 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
747 TGSI_INTERPOLATE_LINEAR);
755 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
756 struct pipe_resource *tex)
758 struct pipe_context *pipe = ctx->base.pipe;
760 assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
762 if (tex->nr_samples > 1) {
763 void **shader = &ctx->fs_texfetch_depthstencil_msaa[tex->target];
765 /* Create the fragment shader on-demand. */
767 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
771 util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
776 void **shader = &ctx->fs_texfetch_depthstencil[tex->target];
778 /* Create the fragment shader on-demand. */
780 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
783 util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
784 TGSI_INTERPOLATE_LINEAR);
792 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
793 struct pipe_resource *tex)
795 struct pipe_context *pipe = ctx->base.pipe;
797 assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
799 if (tex->nr_samples > 1) {
800 void **shader = &ctx->fs_texfetch_stencil_msaa[tex->target];
802 /* Create the fragment shader on-demand. */
804 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
808 util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
813 void **shader = &ctx->fs_texfetch_stencil[tex->target];
815 /* Create the fragment shader on-demand. */
817 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
820 util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
821 TGSI_INTERPOLATE_LINEAR);
828 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx)
830 struct pipe_context *pipe = ctx->base.pipe;
832 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
833 pipe->bind_vs_state(pipe, ctx->vs);
834 if (ctx->has_geometry_shader)
835 pipe->bind_gs_state(pipe, NULL);
836 if (ctx->has_stream_out)
837 pipe->set_stream_output_targets(pipe, 0, NULL, 0);
840 static void blitter_draw(struct blitter_context_priv *ctx,
841 unsigned x1, unsigned y1,
842 unsigned x2, unsigned y2,
845 struct pipe_resource *buf = NULL;
848 blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
850 u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices,
852 u_upload_unmap(ctx->upload);
853 util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, offset,
854 PIPE_PRIM_TRIANGLE_FAN, 4, 2);
855 pipe_resource_reference(&buf, NULL);
858 void util_blitter_draw_rectangle(struct blitter_context *blitter,
859 unsigned x1, unsigned y1,
860 unsigned x2, unsigned y2,
862 enum blitter_attrib_type type,
863 const union pipe_color_union *attrib)
865 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
868 case UTIL_BLITTER_ATTRIB_COLOR:
869 blitter_set_clear_color(ctx, attrib);
872 case UTIL_BLITTER_ATTRIB_TEXCOORD:
873 set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
879 blitter_draw(ctx, x1, y1, x2, y2, depth);
882 static void util_blitter_clear_custom(struct blitter_context *blitter,
883 unsigned width, unsigned height,
885 unsigned clear_buffers,
886 enum pipe_format cbuf_format,
887 const union pipe_color_union *color,
888 double depth, unsigned stencil,
889 void *custom_blend, void *custom_dsa)
891 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
892 struct pipe_context *pipe = ctx->base.pipe;
893 struct pipe_stencil_ref sr = { { 0 } };
894 boolean int_format = util_format_is_pure_integer(cbuf_format);
895 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
897 blitter_set_running_flag(ctx);
898 blitter_check_saved_vertex_states(ctx);
899 blitter_check_saved_fragment_states(ctx);
903 pipe->bind_blend_state(pipe, custom_blend);
904 } else if (clear_buffers & PIPE_CLEAR_COLOR) {
905 pipe->bind_blend_state(pipe, ctx->blend_write_color);
907 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
911 pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
912 } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
913 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
914 } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
915 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
916 } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
917 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
919 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
922 sr.ref_value[0] = stencil & 0xff;
923 pipe->set_stencil_ref(pipe, &sr);
925 if (util_format_is_pure_sint(cbuf_format)) {
926 pipe->bind_vertex_elements_state(pipe, ctx->velem_sint_state);
927 } else if (util_format_is_pure_uint(cbuf_format)) {
928 pipe->bind_vertex_elements_state(pipe, ctx->velem_uint_state);
930 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
932 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs, int_format));
933 pipe->set_sample_mask(pipe, ~0);
935 blitter_set_common_draw_rect_state(ctx);
936 blitter_set_dst_dimensions(ctx, width, height);
937 blitter->draw_rectangle(blitter, 0, 0, width, height, depth,
938 UTIL_BLITTER_ATTRIB_COLOR, color);
940 blitter_restore_vertex_states(ctx);
941 blitter_restore_fragment_states(ctx);
942 blitter_unset_running_flag(ctx);
945 void util_blitter_clear(struct blitter_context *blitter,
946 unsigned width, unsigned height,
948 unsigned clear_buffers,
949 enum pipe_format cbuf_format,
950 const union pipe_color_union *color,
951 double depth, unsigned stencil)
953 util_blitter_clear_custom(blitter, width, height, num_cbufs,
954 clear_buffers, cbuf_format, color, depth, stencil,
958 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
959 unsigned width, unsigned height,
960 double depth, void *custom_dsa)
962 static const union pipe_color_union color;
963 util_blitter_clear_custom(blitter, width, height, 0,
964 0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa);
968 boolean is_overlap(unsigned dstx, unsigned dsty, unsigned dstz,
969 const struct pipe_box *srcbox)
971 struct pipe_box src = *srcbox;
975 src.width = -src.width;
977 if (src.height < 0) {
979 src.height = -src.height;
983 src.depth = -src.depth;
985 return src.x < dstx+src.width && src.x+src.width > dstx &&
986 src.y < dsty+src.height && src.y+src.height > dsty &&
987 src.z < dstz+src.depth && src.z+src.depth > dstz;
990 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
991 struct pipe_resource *dst,
994 const struct pipe_box *srcbox)
996 memset(dst_templ, 0, sizeof(*dst_templ));
997 dst_templ->format = dst->format;
998 if (util_format_is_depth_or_stencil(dst->format)) {
999 dst_templ->usage = PIPE_BIND_DEPTH_STENCIL;
1001 dst_templ->usage = PIPE_BIND_RENDER_TARGET;
1003 dst_templ->format = util_format_linear(dst->format);
1004 dst_templ->u.tex.level = dstlevel;
1005 dst_templ->u.tex.first_layer = dstz;
1006 dst_templ->u.tex.last_layer = dstz + srcbox->depth - 1;
1009 void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
1010 struct pipe_resource *src,
1013 memset(src_templ, 0, sizeof(*src_templ));
1014 src_templ->format = util_format_linear(src->format);
1015 src_templ->u.tex.first_level = srclevel;
1016 src_templ->u.tex.last_level = srclevel;
1017 src_templ->u.tex.first_layer = 0;
1018 src_templ->u.tex.last_layer =
1019 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1020 : src->array_size - 1;
1021 src_templ->swizzle_r = PIPE_SWIZZLE_RED;
1022 src_templ->swizzle_g = PIPE_SWIZZLE_GREEN;
1023 src_templ->swizzle_b = PIPE_SWIZZLE_BLUE;
1024 src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA;
1027 boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
1028 const struct pipe_resource *dst,
1029 const struct pipe_resource *src,
1032 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1033 struct pipe_screen *screen = ctx->base.pipe->screen;
1038 const struct util_format_description *desc =
1039 util_format_description(dst->format);
1041 is_stencil = util_format_has_stencil(desc);
1043 /* Stencil export must be supported for stencil copy. */
1044 if ((mask & PIPE_MASK_S) && is_stencil && !ctx->has_stencil_export) {
1048 if (is_stencil || util_format_has_depth(desc))
1049 bind = PIPE_BIND_DEPTH_STENCIL;
1051 bind = PIPE_BIND_RENDER_TARGET;
1053 if (!screen->is_format_supported(screen, dst->format, dst->target,
1054 dst->nr_samples, bind)) {
1060 if (!screen->is_format_supported(screen, src->format, src->target,
1061 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1065 /* Check stencil sampler support for stencil copy. */
1066 if (util_format_has_stencil(util_format_description(src->format))) {
1067 enum pipe_format stencil_format =
1068 util_format_stencil_only(src->format);
1069 assert(stencil_format != PIPE_FORMAT_NONE);
1071 if (stencil_format != src->format &&
1072 !screen->is_format_supported(screen, stencil_format, src->target,
1073 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1082 void util_blitter_copy_texture(struct blitter_context *blitter,
1083 struct pipe_resource *dst,
1084 unsigned dst_level, unsigned dst_sample_mask,
1085 unsigned dstx, unsigned dsty, unsigned dstz,
1086 struct pipe_resource *src,
1087 unsigned src_level, unsigned src_sample,
1088 const struct pipe_box *srcbox)
1090 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1091 struct pipe_context *pipe = ctx->base.pipe;
1092 struct pipe_surface *dst_view, dst_templ;
1093 struct pipe_sampler_view src_templ, *src_view;
1096 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1098 /* Initialize the surface. */
1099 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz, srcbox);
1100 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1102 /* Initialize the sampler view. */
1103 util_blitter_default_src_texture(&src_templ, src, src_level);
1104 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1107 util_blitter_copy_texture_view(blitter, dst_view, dst_sample_mask, dstx,
1108 dsty, src_view, src_sample, srcbox,
1109 src->width0, src->height0, PIPE_MASK_RGBAZS);
1111 pipe_surface_reference(&dst_view, NULL);
1112 pipe_sampler_view_reference(&src_view, NULL);
1115 void util_blitter_copy_texture_view(struct blitter_context *blitter,
1116 struct pipe_surface *dst,
1117 unsigned dst_sample_mask,
1118 unsigned dstx, unsigned dsty,
1119 struct pipe_sampler_view *src,
1120 unsigned src_sample,
1121 const struct pipe_box *srcbox,
1122 unsigned src_width0, unsigned src_height0,
1125 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1126 struct pipe_context *pipe = ctx->base.pipe;
1127 struct pipe_framebuffer_state fb_state;
1128 enum pipe_texture_target src_target = src->texture->target;
1129 int abs_width = abs(srcbox->width);
1130 int abs_height = abs(srcbox->height);
1131 boolean blit_stencil, blit_depth;
1132 const struct util_format_description *src_desc =
1133 util_format_description(src->format);
1135 blit_depth = util_format_has_depth(src_desc) && (mask & PIPE_MASK_Z);
1136 blit_stencil = util_format_has_stencil(src_desc) && (mask & PIPE_MASK_S);
1138 /* If you want a fallback for stencil copies,
1139 * use util_blitter_copy_texture. */
1140 if (blit_stencil && !ctx->has_stencil_export) {
1141 blit_stencil = FALSE;
1147 /* Sanity checks. */
1148 if (dst->texture == src->texture &&
1149 dst->u.tex.level == src->u.tex.first_level) {
1150 assert(!is_overlap(dstx, dsty, 0, srcbox));
1152 /* XXX should handle 3d regions */
1153 assert(srcbox->depth == 1);
1155 /* Check whether the states are properly saved. */
1156 blitter_set_running_flag(ctx);
1157 blitter_check_saved_vertex_states(ctx);
1158 blitter_check_saved_fragment_states(ctx);
1159 blitter_check_saved_textures(ctx);
1160 blitter_check_saved_fb_state(ctx);
1162 /* Initialize framebuffer state. */
1163 fb_state.width = dst->width;
1164 fb_state.height = dst->height;
1166 if (blit_depth || blit_stencil) {
1167 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
1169 if (blit_depth && blit_stencil) {
1170 pipe->bind_depth_stencil_alpha_state(pipe,
1171 ctx->dsa_write_depth_stencil);
1172 pipe->bind_fs_state(pipe,
1173 blitter_get_fs_texfetch_depthstencil(ctx, src->texture));
1174 } else if (blit_depth) {
1175 pipe->bind_depth_stencil_alpha_state(pipe,
1176 ctx->dsa_write_depth_keep_stencil);
1177 pipe->bind_fs_state(pipe,
1178 blitter_get_fs_texfetch_depth(ctx, src->texture));
1179 } else { /* is_stencil */
1180 pipe->bind_depth_stencil_alpha_state(pipe,
1181 ctx->dsa_keep_depth_write_stencil);
1182 pipe->bind_fs_state(pipe,
1183 blitter_get_fs_texfetch_stencil(ctx, src->texture));
1186 fb_state.nr_cbufs = 0;
1187 fb_state.zsbuf = dst;
1189 pipe->bind_blend_state(pipe, ctx->blend_write_color);
1190 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1191 pipe->bind_fs_state(pipe,
1192 blitter_get_fs_texfetch_col(ctx, src->texture));
1194 fb_state.nr_cbufs = 1;
1195 fb_state.cbufs[0] = dst;
1199 if (blit_depth && blit_stencil) {
1200 /* Setup two samplers, one for depth and the other one for stencil. */
1201 struct pipe_sampler_view templ;
1202 struct pipe_sampler_view *views[2];
1203 void *samplers[2] = {ctx->sampler_state, ctx->sampler_state};
1206 templ.format = util_format_stencil_only(templ.format);
1207 assert(templ.format != PIPE_FORMAT_NONE);
1210 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
1212 pipe->set_fragment_sampler_views(pipe, 2, views);
1213 pipe->bind_fragment_sampler_states(pipe, 2, samplers);
1215 pipe_sampler_view_reference(&views[1], NULL);
1216 } else if (blit_stencil) {
1217 /* Set a stencil-only sampler view for it not to sample depth instead. */
1218 struct pipe_sampler_view templ;
1219 struct pipe_sampler_view *view;
1222 templ.format = util_format_stencil_only(templ.format);
1223 assert(templ.format != PIPE_FORMAT_NONE);
1225 view = pipe->create_sampler_view(pipe, src->texture, &templ);
1227 pipe->set_fragment_sampler_views(pipe, 1, &view);
1228 pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state);
1230 pipe_sampler_view_reference(&view, NULL);
1232 pipe->set_fragment_sampler_views(pipe, 1, &src);
1233 pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state);
1236 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1237 pipe->set_framebuffer_state(pipe, &fb_state);
1238 pipe->set_sample_mask(pipe, dst_sample_mask);
1240 blitter_set_common_draw_rect_state(ctx);
1241 blitter_set_dst_dimensions(ctx, dst->width, dst->height);
1243 if ((src_target == PIPE_TEXTURE_1D ||
1244 src_target == PIPE_TEXTURE_2D ||
1245 src_target == PIPE_TEXTURE_RECT) &&
1246 src->texture->nr_samples <= 1) {
1247 /* Draw the quad with the draw_rectangle callback. */
1249 /* Set texture coordinates. - use a pipe color union
1250 * for interface purposes.
1251 * XXX pipe_color_union is a wrong name since we use that to set
1252 * texture coordinates too.
1254 union pipe_color_union coord;
1255 get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1256 srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f);
1259 blitter->draw_rectangle(blitter, dstx, dsty, dstx+abs_width, dsty+abs_height, 0,
1260 UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1262 /* Draw the quad with the generic codepath. */
1263 blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z,
1265 srcbox->x, srcbox->y,
1266 srcbox->x + srcbox->width, srcbox->y + srcbox->height);
1267 blitter_draw(ctx, dstx, dsty, dstx+abs_width, dsty+abs_height, 0);
1270 blitter_restore_vertex_states(ctx);
1271 blitter_restore_fragment_states(ctx);
1272 blitter_restore_textures(ctx);
1273 blitter_restore_fb_state(ctx);
1274 blitter_unset_running_flag(ctx);
1277 /* Clear a region of a color surface to a constant value. */
1278 void util_blitter_clear_render_target(struct blitter_context *blitter,
1279 struct pipe_surface *dstsurf,
1280 const union pipe_color_union *color,
1281 unsigned dstx, unsigned dsty,
1282 unsigned width, unsigned height)
1284 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1285 struct pipe_context *pipe = ctx->base.pipe;
1286 struct pipe_framebuffer_state fb_state;
1288 assert(dstsurf->texture);
1289 if (!dstsurf->texture)
1292 /* check the saved state */
1293 blitter_set_running_flag(ctx);
1294 blitter_check_saved_vertex_states(ctx);
1295 blitter_check_saved_fragment_states(ctx);
1296 blitter_check_saved_fb_state(ctx);
1299 pipe->bind_blend_state(pipe, ctx->blend_write_color);
1300 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1301 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1302 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1304 /* set a framebuffer state */
1305 fb_state.width = dstsurf->width;
1306 fb_state.height = dstsurf->height;
1307 fb_state.nr_cbufs = 1;
1308 fb_state.cbufs[0] = dstsurf;
1310 pipe->set_framebuffer_state(pipe, &fb_state);
1311 pipe->set_sample_mask(pipe, ~0);
1313 blitter_set_common_draw_rect_state(ctx);
1314 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1315 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1316 UTIL_BLITTER_ATTRIB_COLOR, color);
1318 blitter_restore_vertex_states(ctx);
1319 blitter_restore_fragment_states(ctx);
1320 blitter_restore_fb_state(ctx);
1321 blitter_unset_running_flag(ctx);
1324 /* Clear a region of a depth stencil surface. */
1325 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
1326 struct pipe_surface *dstsurf,
1327 unsigned clear_flags,
1330 unsigned dstx, unsigned dsty,
1331 unsigned width, unsigned height)
1333 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1334 struct pipe_context *pipe = ctx->base.pipe;
1335 struct pipe_framebuffer_state fb_state;
1336 struct pipe_stencil_ref sr = { { 0 } };
1338 assert(dstsurf->texture);
1339 if (!dstsurf->texture)
1342 /* check the saved state */
1343 blitter_set_running_flag(ctx);
1344 blitter_check_saved_vertex_states(ctx);
1345 blitter_check_saved_fragment_states(ctx);
1346 blitter_check_saved_fb_state(ctx);
1349 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
1350 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1351 sr.ref_value[0] = stencil & 0xff;
1352 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1353 pipe->set_stencil_ref(pipe, &sr);
1355 else if (clear_flags & PIPE_CLEAR_DEPTH) {
1356 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1358 else if (clear_flags & PIPE_CLEAR_STENCIL) {
1359 sr.ref_value[0] = stencil & 0xff;
1360 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1361 pipe->set_stencil_ref(pipe, &sr);
1364 /* hmm that should be illegal probably, or make it a no-op somewhere */
1365 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1367 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1368 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1370 /* set a framebuffer state */
1371 fb_state.width = dstsurf->width;
1372 fb_state.height = dstsurf->height;
1373 fb_state.nr_cbufs = 0;
1374 fb_state.cbufs[0] = 0;
1375 fb_state.zsbuf = dstsurf;
1376 pipe->set_framebuffer_state(pipe, &fb_state);
1377 pipe->set_sample_mask(pipe, ~0);
1379 blitter_set_common_draw_rect_state(ctx);
1380 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1381 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, depth,
1382 UTIL_BLITTER_ATTRIB_NONE, NULL);
1384 blitter_restore_vertex_states(ctx);
1385 blitter_restore_fragment_states(ctx);
1386 blitter_restore_fb_state(ctx);
1387 blitter_unset_running_flag(ctx);
1390 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1391 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
1392 struct pipe_surface *zsurf,
1393 struct pipe_surface *cbsurf,
1394 unsigned sample_mask,
1395 void *dsa_stage, float depth)
1397 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1398 struct pipe_context *pipe = ctx->base.pipe;
1399 struct pipe_framebuffer_state fb_state;
1401 assert(zsurf->texture);
1402 if (!zsurf->texture)
1405 /* check the saved state */
1406 blitter_set_running_flag(ctx);
1407 blitter_check_saved_vertex_states(ctx);
1408 blitter_check_saved_fragment_states(ctx);
1409 blitter_check_saved_fb_state(ctx);
1412 pipe->bind_blend_state(pipe, ctx->blend_write_color);
1413 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
1414 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1415 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1417 /* set a framebuffer state */
1418 fb_state.width = zsurf->width;
1419 fb_state.height = zsurf->height;
1420 fb_state.nr_cbufs = 1;
1422 fb_state.cbufs[0] = cbsurf;
1423 fb_state.nr_cbufs = 1;
1425 fb_state.cbufs[0] = NULL;
1426 fb_state.nr_cbufs = 0;
1428 fb_state.zsbuf = zsurf;
1429 pipe->set_framebuffer_state(pipe, &fb_state);
1430 pipe->set_sample_mask(pipe, sample_mask);
1432 blitter_set_common_draw_rect_state(ctx);
1433 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
1434 blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
1435 UTIL_BLITTER_ATTRIB_NONE, NULL);
1437 blitter_restore_vertex_states(ctx);
1438 blitter_restore_fragment_states(ctx);
1439 blitter_restore_fb_state(ctx);
1440 blitter_unset_running_flag(ctx);
1443 void util_blitter_copy_buffer(struct blitter_context *blitter,
1444 struct pipe_resource *dst,
1446 struct pipe_resource *src,
1450 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1451 struct pipe_context *pipe = ctx->base.pipe;
1452 struct pipe_vertex_buffer vb;
1453 struct pipe_stream_output_target *so_target;
1455 if (srcx >= src->width0 ||
1456 dstx >= dst->width0) {
1459 if (srcx + size > src->width0) {
1460 size = src->width0 - srcx;
1462 if (dstx + size > dst->width0) {
1463 size = dst->width0 - dstx;
1466 /* Drivers not capable of Stream Out should not call this function
1467 * in the first place. */
1468 assert(ctx->has_stream_out);
1470 /* Some alignment is required. */
1471 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
1472 !ctx->has_stream_out) {
1473 struct pipe_box box;
1474 u_box_1d(srcx, size, &box);
1475 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
1479 blitter_set_running_flag(ctx);
1480 blitter_check_saved_vertex_states(ctx);
1483 vb.buffer_offset = srcx;
1486 pipe->set_vertex_buffers(pipe, 1, &vb);
1487 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf);
1488 pipe->bind_vs_state(pipe, ctx->vs_pos_only);
1489 if (ctx->has_geometry_shader)
1490 pipe->bind_gs_state(pipe, NULL);
1491 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
1493 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
1494 pipe->set_stream_output_targets(pipe, 1, &so_target, 0);
1496 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
1498 blitter_restore_vertex_states(ctx);
1499 blitter_unset_running_flag(ctx);
1500 pipe_so_target_reference(&so_target, NULL);
1503 /* probably radeon specific */
1504 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
1505 struct pipe_resource *dst,
1508 struct pipe_resource *src,
1510 unsigned sample_mask,
1513 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1514 struct pipe_context *pipe = ctx->base.pipe;
1515 struct pipe_framebuffer_state fb_state;
1516 struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
1518 blitter_set_running_flag(ctx);
1519 blitter_check_saved_vertex_states(ctx);
1520 blitter_check_saved_fragment_states(ctx);
1523 pipe->bind_blend_state(pipe, custom_blend);
1524 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1525 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1526 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1527 pipe->set_sample_mask(pipe, sample_mask);
1529 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1530 surf_tmpl.format = dst->format;
1531 surf_tmpl.u.tex.level = dst_level;
1532 surf_tmpl.u.tex.first_layer = dst_layer;
1533 surf_tmpl.u.tex.last_layer = dst_layer;
1534 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
1536 dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
1538 surf_tmpl.u.tex.level = 0;
1539 surf_tmpl.u.tex.first_layer = src_layer;
1540 surf_tmpl.u.tex.last_layer = src_layer;
1542 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
1544 /* set a framebuffer state */
1545 fb_state.width = src->width0;
1546 fb_state.height = src->height0;
1547 fb_state.nr_cbufs = 2;
1548 fb_state.cbufs[0] = srcsurf;
1549 fb_state.cbufs[1] = dstsurf;
1550 fb_state.zsbuf = NULL;
1551 pipe->set_framebuffer_state(pipe, &fb_state);
1553 blitter_set_common_draw_rect_state(ctx);
1554 blitter_set_dst_dimensions(ctx, src->width0, src->height0);
1555 blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
1557 blitter_restore_fb_state(ctx);
1558 blitter_restore_vertex_states(ctx);
1559 blitter_restore_fragment_states(ctx);
1560 blitter_unset_running_flag(ctx);
1562 pipe_surface_reference(&srcsurf, NULL);
1563 pipe_surface_reference(&dstsurf, NULL);
1566 void util_blitter_custom_color(struct blitter_context *blitter,
1567 struct pipe_surface *dstsurf,
1570 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1571 struct pipe_context *pipe = ctx->base.pipe;
1572 struct pipe_framebuffer_state fb_state;
1574 assert(dstsurf->texture);
1575 if (!dstsurf->texture)
1578 /* check the saved state */
1579 blitter_set_running_flag(ctx);
1580 blitter_check_saved_vertex_states(ctx);
1581 blitter_check_saved_fragment_states(ctx);
1582 blitter_check_saved_fb_state(ctx);
1585 pipe->bind_blend_state(pipe, custom_blend);
1586 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1587 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1588 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1589 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
1591 /* set a framebuffer state */
1592 fb_state.width = dstsurf->width;
1593 fb_state.height = dstsurf->height;
1594 fb_state.nr_cbufs = 1;
1595 fb_state.cbufs[0] = dstsurf;
1597 pipe->set_framebuffer_state(pipe, &fb_state);
1598 pipe->set_sample_mask(pipe, ~0);
1600 blitter_set_common_draw_rect_state(ctx);
1601 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1602 blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
1605 blitter_restore_vertex_states(ctx);
1606 blitter_restore_fragment_states(ctx);
1607 blitter_restore_fb_state(ctx);
1608 blitter_unset_running_flag(ctx);