u_blitter.c revision 187d7fb2fec7da889f25366696faaac4c2e8f9c4
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, clear_render_target,
30 * clear_depth_stencil, and resource_copy_region functions.
31 *
32 * @author Marek Olšák
33 */
34
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"
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_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"
51
52#define INVALID_PTR ((void*)~0)
53
54struct blitter_context_priv
55{
56   struct blitter_context base;
57
58   struct u_upload_mgr *upload;
59
60   float vertices[4][2][4];   /**< {pos, color} or {pos, texcoord} */
61
62   /* Templates for various state objects. */
63
64   /* Constant state objects. */
65   /* Vertex shaders. */
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.*/
68
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];
73
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];
77
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];
83
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];
89
90   /* Blend state. */
91   void *blend_write_color;   /**< blend state with writemask of RGBA */
92   void *blend_keep_color;    /**< blend state with writemask of 0 */
93
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;
99
100   /* Vertex elements states. */
101   void *velem_state;
102   void *velem_uint_state;
103   void *velem_sint_state;
104   void *velem_state_readbuf;
105
106   /* Sampler state. */
107   void *sampler_state;
108
109   /* Rasterizer state. */
110   void *rs_state;
111   void *rs_discard_state;
112
113   /* Viewport state. */
114   struct pipe_viewport_state viewport;
115
116   /* Destination surface dimensions. */
117   unsigned dst_width;
118   unsigned dst_height;
119
120   boolean has_geometry_shader;
121   boolean vertex_has_integers;
122   boolean has_stream_out;
123   boolean has_stencil_export;
124};
125
126
127struct blitter_context *util_blitter_create(struct pipe_context *pipe)
128{
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];
135   unsigned i;
136
137   ctx = CALLOC_STRUCT(blitter_context_priv);
138   if (!ctx)
139      return NULL;
140
141   ctx->base.pipe = pipe;
142   ctx->base.draw_rectangle = util_blitter_draw_rectangle;
143
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;
157
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;
167
168   ctx->has_stencil_export =
169         pipe->screen->get_param(pipe->screen,
170                                 PIPE_CAP_SHADER_STENCIL_EXPORT);
171
172   /* blend state objects */
173   memset(&blend, 0, sizeof(blend));
174   ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
175
176   blend.rt[0].colormask = PIPE_MASK_RGBA;
177   ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
178
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);
183
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);
189
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);
199
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);
204
205   /* sampler state */
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);
212
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);
220
221   if (ctx->has_stream_out) {
222      rs_state.rasterizer_discard = 1;
223      ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
224   }
225
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;
231   }
232   ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
233
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]);
241
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]);
248   }
249
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]);
253   }
254
255   /* fragment shaders are created on-demand */
256
257   /* vertex shaders */
258   {
259      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
260                                      TGSI_SEMANTIC_GENERIC };
261      const uint semantic_indices[] = { 0, 0 };
262      ctx->vs =
263         util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
264                                             semantic_indices);
265   }
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 };
270
271      memset(&so, 0, sizeof(so));
272      so.num_outputs = 1;
273      so.output[0].num_components = 1;
274      so.stride[0] = 1;
275
276      ctx->vs_pos_only =
277         util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
278                                                     semantic_indices, &so);
279   }
280
281   /* set invariant vertex coordinates */
282   for (i = 0; i < 4; i++)
283      ctx->vertices[i][0][3] = 1; /*v.w*/
284
285   ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
286
287   return &ctx->base;
288}
289
290void util_blitter_destroy(struct blitter_context *blitter)
291{
292   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
293   struct pipe_context *pipe = blitter->pipe;
294   int i;
295
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);
303
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);
314   }
315   if (ctx->velem_state_readbuf)
316      pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf);
317
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]);
327   }
328
329   for (i = 0; i <= PIPE_MAX_COLOR_BUFS; i++) {
330      if (ctx->fs_col[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]);
334   }
335
336   pipe->delete_sampler_state(pipe, ctx->sampler_state);
337   u_upload_destroy(ctx->upload);
338   FREE(ctx);
339}
340
341static void blitter_set_running_flag(struct blitter_context_priv *ctx)
342{
343   if (ctx->base.running) {
344      _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
345                    __LINE__);
346   }
347   ctx->base.running = TRUE;
348}
349
350static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
351{
352   if (!ctx->base.running) {
353      _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
354                    __LINE__);
355   }
356   ctx->base.running = FALSE;
357}
358
359static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
360{
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);
367}
368
369static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
370{
371   struct pipe_context *pipe = ctx->base.pipe;
372   unsigned i;
373
374   /* Vertex buffers. */
375   pipe->set_vertex_buffers(pipe,
376                            ctx->base.saved_num_vertex_buffers,
377                            ctx->base.saved_vertex_buffers);
378
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,
382                                 NULL);
383      }
384   }
385   ctx->base.saved_num_vertex_buffers = ~0;
386
387   /* Vertex elements. */
388   pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
389   ctx->base.saved_velem_state = INVALID_PTR;
390
391   /* Vertex shader. */
392   pipe->bind_vs_state(pipe, ctx->base.saved_vs);
393   ctx->base.saved_vs = INVALID_PTR;
394
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;
399   }
400
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);
406
407      for (i = 0; i < ctx->base.saved_num_so_targets; i++)
408         pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
409
410      ctx->base.saved_num_so_targets = ~0;
411   }
412
413   /* Rasterizer. */
414   pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
415   ctx->base.saved_rs_state = INVALID_PTR;
416}
417
418static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
419{
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);
423}
424
425static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
426{
427   struct pipe_context *pipe = ctx->base.pipe;
428
429   /* Fragment shader. */
430   pipe->bind_fs_state(pipe, ctx->base.saved_fs);
431   ctx->base.saved_fs = INVALID_PTR;
432
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;
436
437   /* Blend state. */
438   pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
439   ctx->base.saved_blend_state = INVALID_PTR;
440
441   /* Sample mask. */
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;
445   }
446
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);
452}
453
454static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
455{
456   assert(ctx->base.saved_fb_state.nr_cbufs != ~0);
457}
458
459static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
460{
461   struct pipe_context *pipe = ctx->base.pipe;
462
463   pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
464   util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
465}
466
467static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
468{
469   assert(ctx->base.saved_num_sampler_states != ~0);
470   assert(ctx->base.saved_num_sampler_views != ~0);
471}
472
473static void blitter_restore_textures(struct blitter_context_priv *ctx)
474{
475   struct pipe_context *pipe = ctx->base.pipe;
476   unsigned i;
477
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;
483
484   /* Fragment sampler views. */
485   pipe->set_fragment_sampler_views(pipe,
486                                    ctx->base.saved_num_sampler_views,
487                                    ctx->base.saved_sampler_views);
488
489   for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
490      pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
491
492   ctx->base.saved_num_sampler_views = ~0;
493}
494
495static void blitter_set_rectangle(struct blitter_context_priv *ctx,
496                                  unsigned x1, unsigned y1,
497                                  unsigned x2, unsigned y2,
498                                  float depth)
499{
500   int i;
501
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*/
505
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*/
508
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*/
511
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*/
514
515   for (i = 0; i < 4; i++)
516      ctx->vertices[i][0][2] = depth; /*z*/
517
518   /* viewport */
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);
528}
529
530static void blitter_set_clear_color(struct blitter_context_priv *ctx,
531                                    const union pipe_color_union *color)
532{
533   int i;
534
535   if (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];
542      }
543   } else {
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;
549      }
550   }
551}
552
553static 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,
557                          float out[4])
558{
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;
563
564   if (normalized) {
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);
569   } else {
570      out[0] = x1;
571      out[1] = y1;
572      out[2] = x2;
573      out[3] = y2;
574   }
575}
576
577static void set_texcoords_in_vertices(const float coord[4],
578                                      float *out, unsigned stride)
579{
580   out[0] = coord[0]; /*t0.s*/
581   out[1] = coord[1]; /*t0.t*/
582   out += stride;
583   out[0] = coord[2]; /*t1.s*/
584   out[1] = coord[1]; /*t1.t*/
585   out += stride;
586   out[0] = coord[2]; /*t2.s*/
587   out[1] = coord[3]; /*t2.t*/
588   out += stride;
589   out[0] = coord[0]; /*t3.s*/
590   out[1] = coord[3]; /*t3.t*/
591}
592
593static 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)
599{
600   unsigned i;
601   float coord[4];
602   float face_coord[4][2];
603
604   get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
605
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);
612   } else {
613      set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
614   }
615
616   /* Set the layer. */
617   switch (src->texture->target) {
618   case PIPE_TEXTURE_3D:
619      {
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*/
624      }
625      break;
626
627   case PIPE_TEXTURE_1D_ARRAY:
628      for (i = 0; i < 4; i++)
629         ctx->vertices[i][1][1] = layer; /*t*/
630      break;
631
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*/
636      }
637      break;
638
639   case PIPE_TEXTURE_2D:
640      for (i = 0; i < 4; i++) {
641         ctx->vertices[i][1][2] = sample; /*r*/
642      }
643      break;
644
645   default:;
646   }
647}
648
649static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
650                                       unsigned width, unsigned height)
651{
652   ctx->dst_width = width;
653   ctx->dst_height = height;
654}
655
656static INLINE
657void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs,
658                         boolean int_format)
659{
660   struct pipe_context *pipe = ctx->base.pipe;
661
662   assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
663
664   if (int_format) {
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];
671   } else {
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];
678   }
679}
680
681static INLINE
682void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
683                                  struct pipe_resource *tex)
684{
685   struct pipe_context *pipe = ctx->base.pipe;
686
687   assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
688
689   if (tex->nr_samples > 1) {
690      void **shader = &ctx->fs_texfetch_col_msaa[tex->target];
691
692      /* Create the fragment shader on-demand. */
693      if (!*shader) {
694         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
695                                                       tex->nr_samples);
696
697         *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex);
698      }
699
700      return *shader;
701   } else {
702      void **shader = &ctx->fs_texfetch_col[tex->target];
703
704      /* Create the fragment shader on-demand. */
705      if (!*shader) {
706         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
707
708         *shader =
709            util_make_fragment_tex_shader(pipe, tgsi_tex,
710                                          TGSI_INTERPOLATE_LINEAR);
711      }
712
713      return *shader;
714   }
715}
716
717static INLINE
718void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
719                                    struct pipe_resource *tex)
720{
721   struct pipe_context *pipe = ctx->base.pipe;
722
723   assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
724
725   if (tex->nr_samples > 1) {
726      void **shader = &ctx->fs_texfetch_depth_msaa[tex->target];
727
728      /* Create the fragment shader on-demand. */
729      if (!*shader) {
730         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
731                                                       tex->nr_samples);
732
733         *shader =
734            util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
735      }
736
737      return *shader;
738   } else {
739      void **shader = &ctx->fs_texfetch_depth[tex->target];
740
741      /* Create the fragment shader on-demand. */
742      if (!*shader) {
743         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
744
745         *shader =
746            util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
747                                                     TGSI_INTERPOLATE_LINEAR);
748      }
749
750      return *shader;
751   }
752}
753
754static INLINE
755void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
756                                           struct pipe_resource *tex)
757{
758   struct pipe_context *pipe = ctx->base.pipe;
759
760   assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
761
762   if (tex->nr_samples > 1) {
763      void **shader = &ctx->fs_texfetch_depthstencil_msaa[tex->target];
764
765      /* Create the fragment shader on-demand. */
766      if (!*shader) {
767         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
768                                                       tex->nr_samples);
769
770         *shader =
771            util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
772      }
773
774      return *shader;
775   } else {
776      void **shader = &ctx->fs_texfetch_depthstencil[tex->target];
777
778      /* Create the fragment shader on-demand. */
779      if (!*shader) {
780         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
781
782         *shader =
783            util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
784                                                     TGSI_INTERPOLATE_LINEAR);
785      }
786
787      return *shader;
788   }
789}
790
791static INLINE
792void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
793                                      struct pipe_resource *tex)
794{
795   struct pipe_context *pipe = ctx->base.pipe;
796
797   assert(tex->target < PIPE_MAX_TEXTURE_TYPES);
798
799   if (tex->nr_samples > 1) {
800      void **shader = &ctx->fs_texfetch_stencil_msaa[tex->target];
801
802      /* Create the fragment shader on-demand. */
803      if (!*shader) {
804         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target,
805                                                       tex->nr_samples);
806
807         *shader =
808            util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
809      }
810
811      return *shader;
812   } else {
813      void **shader = &ctx->fs_texfetch_stencil[tex->target];
814
815      /* Create the fragment shader on-demand. */
816      if (!*shader) {
817         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(tex->target, 0);
818
819         *shader =
820            util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
821                                                       TGSI_INTERPOLATE_LINEAR);
822      }
823
824      return *shader;
825   }
826}
827
828static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx)
829{
830   struct pipe_context *pipe = ctx->base.pipe;
831
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);
838}
839
840static void blitter_draw(struct blitter_context_priv *ctx,
841                         unsigned x1, unsigned y1,
842                         unsigned x2, unsigned y2,
843                         float depth)
844{
845   struct pipe_resource *buf = NULL;
846   unsigned offset = 0;
847
848   blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
849
850   u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices,
851                 &offset, &buf);
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);
856}
857
858void util_blitter_draw_rectangle(struct blitter_context *blitter,
859                                 unsigned x1, unsigned y1,
860                                 unsigned x2, unsigned y2,
861                                 float depth,
862                                 enum blitter_attrib_type type,
863                                 const union pipe_color_union *attrib)
864{
865   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
866
867   switch (type) {
868      case UTIL_BLITTER_ATTRIB_COLOR:
869         blitter_set_clear_color(ctx, attrib);
870         break;
871
872      case UTIL_BLITTER_ATTRIB_TEXCOORD:
873         set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
874         break;
875
876      default:;
877   }
878
879   blitter_draw(ctx, x1, y1, x2, y2, depth);
880}
881
882static void util_blitter_clear_custom(struct blitter_context *blitter,
883                                      unsigned width, unsigned height,
884                                      unsigned num_cbufs,
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)
890{
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);
896
897   blitter_set_running_flag(ctx);
898   blitter_check_saved_vertex_states(ctx);
899   blitter_check_saved_fragment_states(ctx);
900
901   /* bind states */
902   if (custom_blend) {
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);
906   } else {
907      pipe->bind_blend_state(pipe, ctx->blend_keep_color);
908   }
909
910   if (custom_dsa) {
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);
918   } else {
919      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
920   }
921
922   sr.ref_value[0] = stencil & 0xff;
923   pipe->set_stencil_ref(pipe, &sr);
924
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);
929   } else {
930      pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
931   }
932   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs, int_format));
933   pipe->set_sample_mask(pipe, ~0);
934
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);
939
940   blitter_restore_vertex_states(ctx);
941   blitter_restore_fragment_states(ctx);
942   blitter_unset_running_flag(ctx);
943}
944
945void util_blitter_clear(struct blitter_context *blitter,
946                        unsigned width, unsigned height,
947                        unsigned num_cbufs,
948                        unsigned clear_buffers,
949                        enum pipe_format cbuf_format,
950                        const union pipe_color_union *color,
951                        double depth, unsigned stencil)
952{
953   util_blitter_clear_custom(blitter, width, height, num_cbufs,
954                             clear_buffers, cbuf_format, color, depth, stencil,
955                             NULL, NULL);
956}
957
958void util_blitter_custom_clear_depth(struct blitter_context *blitter,
959                                     unsigned width, unsigned height,
960                                     double depth, void *custom_dsa)
961{
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);
965}
966
967static
968boolean is_overlap(unsigned dstx, unsigned dsty, unsigned dstz,
969		   const struct pipe_box *srcbox)
970{
971   struct pipe_box src = *srcbox;
972
973   if (src.width < 0) {
974      src.x += src.width;
975      src.width = -src.width;
976   }
977   if (src.height < 0) {
978      src.y += src.height;
979      src.height = -src.height;
980   }
981   if (src.depth < 0) {
982      src.z += src.depth;
983      src.depth = -src.depth;
984   }
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;
988}
989
990void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
991                                      struct pipe_resource *dst,
992                                      unsigned dstlevel,
993                                      unsigned dstz,
994                                      const struct pipe_box *srcbox)
995{
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;
1000    } else {
1001	dst_templ->usage = PIPE_BIND_RENDER_TARGET;
1002    }
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;
1007}
1008
1009void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
1010                                      struct pipe_resource *src,
1011                                      unsigned srclevel)
1012{
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;
1025}
1026
1027boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
1028                                       const struct pipe_resource *dst,
1029                                       const struct pipe_resource *src,
1030                                       unsigned mask)
1031{
1032   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1033   struct pipe_screen *screen = ctx->base.pipe->screen;
1034
1035   if (dst) {
1036      unsigned bind;
1037      boolean is_stencil;
1038      const struct util_format_description *desc =
1039            util_format_description(dst->format);
1040
1041      is_stencil = util_format_has_stencil(desc);
1042
1043      /* Stencil export must be supported for stencil copy. */
1044      if ((mask & PIPE_MASK_S) && is_stencil && !ctx->has_stencil_export) {
1045         return FALSE;
1046      }
1047
1048      if (is_stencil || util_format_has_depth(desc))
1049         bind = PIPE_BIND_DEPTH_STENCIL;
1050      else
1051         bind = PIPE_BIND_RENDER_TARGET;
1052
1053      if (!screen->is_format_supported(screen, dst->format, dst->target,
1054                                       dst->nr_samples, bind)) {
1055         return FALSE;
1056      }
1057   }
1058
1059   if (src) {
1060      if (!screen->is_format_supported(screen, src->format, src->target,
1061                                 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1062         return FALSE;
1063      }
1064
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);
1070
1071         if (stencil_format != src->format &&
1072             !screen->is_format_supported(screen, stencil_format, src->target,
1073                                 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1074            return FALSE;
1075         }
1076      }
1077   }
1078
1079   return TRUE;
1080}
1081
1082void 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)
1089{
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;
1094
1095   assert(dst && src);
1096   assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1097
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);
1101
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);
1105
1106   /* Copy. */
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);
1110
1111   pipe_surface_reference(&dst_view, NULL);
1112   pipe_sampler_view_reference(&src_view, NULL);
1113}
1114
1115void 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,
1123                                    unsigned mask)
1124{
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);
1134
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);
1137
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;
1142
1143      if (!blit_depth)
1144         return;
1145   }
1146
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));
1151   }
1152   /* XXX should handle 3d regions */
1153   assert(srcbox->depth == 1);
1154
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);
1161
1162   /* Initialize framebuffer state. */
1163   fb_state.width = dst->width;
1164   fb_state.height = dst->height;
1165
1166   if (blit_depth || blit_stencil) {
1167      pipe->bind_blend_state(pipe, ctx->blend_keep_color);
1168
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));
1184      }
1185
1186      fb_state.nr_cbufs = 0;
1187      fb_state.zsbuf = dst;
1188   } else {
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));
1193
1194      fb_state.nr_cbufs = 1;
1195      fb_state.cbufs[0] = dst;
1196      fb_state.zsbuf = 0;
1197   }
1198
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};
1204
1205      templ = *src;
1206      templ.format = util_format_stencil_only(templ.format);
1207      assert(templ.format != PIPE_FORMAT_NONE);
1208
1209      views[0] = src;
1210      views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
1211
1212      pipe->set_fragment_sampler_views(pipe, 2, views);
1213      pipe->bind_fragment_sampler_states(pipe, 2, samplers);
1214
1215      pipe_sampler_view_reference(&views[1], NULL);
1216   } else {
1217      pipe->set_fragment_sampler_views(pipe, 1, &src);
1218      pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state);
1219   }
1220
1221   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1222   pipe->set_framebuffer_state(pipe, &fb_state);
1223   pipe->set_sample_mask(pipe, dst_sample_mask);
1224
1225   blitter_set_common_draw_rect_state(ctx);
1226   blitter_set_dst_dimensions(ctx, dst->width, dst->height);
1227
1228   if ((src_target == PIPE_TEXTURE_1D ||
1229        src_target == PIPE_TEXTURE_2D ||
1230        src_target == PIPE_TEXTURE_RECT) &&
1231       src->texture->nr_samples <= 1) {
1232      /* Draw the quad with the draw_rectangle callback. */
1233
1234      /* Set texture coordinates. - use a pipe color union
1235       * for interface purposes.
1236       * XXX pipe_color_union is a wrong name since we use that to set
1237       * texture coordinates too.
1238       */
1239      union pipe_color_union coord;
1240      get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1241                    srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f);
1242
1243      /* Draw. */
1244      blitter->draw_rectangle(blitter, dstx, dsty, dstx+abs_width, dsty+abs_height, 0,
1245                              UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1246   } else {
1247      /* Draw the quad with the generic codepath. */
1248      blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z,
1249                            src_sample,
1250                            srcbox->x, srcbox->y,
1251                            srcbox->x + srcbox->width, srcbox->y + srcbox->height);
1252      blitter_draw(ctx, dstx, dsty, dstx+abs_width, dsty+abs_height, 0);
1253   }
1254
1255   blitter_restore_vertex_states(ctx);
1256   blitter_restore_fragment_states(ctx);
1257   blitter_restore_textures(ctx);
1258   blitter_restore_fb_state(ctx);
1259   blitter_unset_running_flag(ctx);
1260}
1261
1262/* Clear a region of a color surface to a constant value. */
1263void util_blitter_clear_render_target(struct blitter_context *blitter,
1264                                      struct pipe_surface *dstsurf,
1265                                      const union pipe_color_union *color,
1266                                      unsigned dstx, unsigned dsty,
1267                                      unsigned width, unsigned height)
1268{
1269   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1270   struct pipe_context *pipe = ctx->base.pipe;
1271   struct pipe_framebuffer_state fb_state;
1272
1273   assert(dstsurf->texture);
1274   if (!dstsurf->texture)
1275      return;
1276
1277   /* check the saved state */
1278   blitter_set_running_flag(ctx);
1279   blitter_check_saved_vertex_states(ctx);
1280   blitter_check_saved_fragment_states(ctx);
1281   blitter_check_saved_fb_state(ctx);
1282
1283   /* bind states */
1284   pipe->bind_blend_state(pipe, ctx->blend_write_color);
1285   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1286   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1287   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1288
1289   /* set a framebuffer state */
1290   fb_state.width = dstsurf->width;
1291   fb_state.height = dstsurf->height;
1292   fb_state.nr_cbufs = 1;
1293   fb_state.cbufs[0] = dstsurf;
1294   fb_state.zsbuf = 0;
1295   pipe->set_framebuffer_state(pipe, &fb_state);
1296   pipe->set_sample_mask(pipe, ~0);
1297
1298   blitter_set_common_draw_rect_state(ctx);
1299   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1300   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1301                           UTIL_BLITTER_ATTRIB_COLOR, color);
1302
1303   blitter_restore_vertex_states(ctx);
1304   blitter_restore_fragment_states(ctx);
1305   blitter_restore_fb_state(ctx);
1306   blitter_unset_running_flag(ctx);
1307}
1308
1309/* Clear a region of a depth stencil surface. */
1310void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
1311                                      struct pipe_surface *dstsurf,
1312                                      unsigned clear_flags,
1313                                      double depth,
1314                                      unsigned stencil,
1315                                      unsigned dstx, unsigned dsty,
1316                                      unsigned width, unsigned height)
1317{
1318   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1319   struct pipe_context *pipe = ctx->base.pipe;
1320   struct pipe_framebuffer_state fb_state;
1321   struct pipe_stencil_ref sr = { { 0 } };
1322
1323   assert(dstsurf->texture);
1324   if (!dstsurf->texture)
1325      return;
1326
1327   /* check the saved state */
1328   blitter_set_running_flag(ctx);
1329   blitter_check_saved_vertex_states(ctx);
1330   blitter_check_saved_fragment_states(ctx);
1331   blitter_check_saved_fb_state(ctx);
1332
1333   /* bind states */
1334   pipe->bind_blend_state(pipe, ctx->blend_keep_color);
1335   if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1336      sr.ref_value[0] = stencil & 0xff;
1337      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1338      pipe->set_stencil_ref(pipe, &sr);
1339   }
1340   else if (clear_flags & PIPE_CLEAR_DEPTH) {
1341      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1342   }
1343   else if (clear_flags & PIPE_CLEAR_STENCIL) {
1344      sr.ref_value[0] = stencil & 0xff;
1345      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1346      pipe->set_stencil_ref(pipe, &sr);
1347   }
1348   else
1349      /* hmm that should be illegal probably, or make it a no-op somewhere */
1350      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1351
1352   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1353   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1354
1355   /* set a framebuffer state */
1356   fb_state.width = dstsurf->width;
1357   fb_state.height = dstsurf->height;
1358   fb_state.nr_cbufs = 0;
1359   fb_state.cbufs[0] = 0;
1360   fb_state.zsbuf = dstsurf;
1361   pipe->set_framebuffer_state(pipe, &fb_state);
1362   pipe->set_sample_mask(pipe, ~0);
1363
1364   blitter_set_common_draw_rect_state(ctx);
1365   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1366   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, depth,
1367                           UTIL_BLITTER_ATTRIB_NONE, NULL);
1368
1369   blitter_restore_vertex_states(ctx);
1370   blitter_restore_fragment_states(ctx);
1371   blitter_restore_fb_state(ctx);
1372   blitter_unset_running_flag(ctx);
1373}
1374
1375/* draw a rectangle across a region using a custom dsa stage - for r600g */
1376void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
1377				       struct pipe_surface *zsurf,
1378				       struct pipe_surface *cbsurf,
1379				       unsigned sample_mask,
1380				       void *dsa_stage, float depth)
1381{
1382   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1383   struct pipe_context *pipe = ctx->base.pipe;
1384   struct pipe_framebuffer_state fb_state;
1385
1386   assert(zsurf->texture);
1387   if (!zsurf->texture)
1388      return;
1389
1390   /* check the saved state */
1391   blitter_set_running_flag(ctx);
1392   blitter_check_saved_vertex_states(ctx);
1393   blitter_check_saved_fragment_states(ctx);
1394   blitter_check_saved_fb_state(ctx);
1395
1396   /* bind states */
1397   pipe->bind_blend_state(pipe, ctx->blend_write_color);
1398   pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
1399   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1400   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1401
1402   /* set a framebuffer state */
1403   fb_state.width = zsurf->width;
1404   fb_state.height = zsurf->height;
1405   fb_state.nr_cbufs = 1;
1406   if (cbsurf) {
1407	   fb_state.cbufs[0] = cbsurf;
1408	   fb_state.nr_cbufs = 1;
1409   } else {
1410	   fb_state.cbufs[0] = NULL;
1411	   fb_state.nr_cbufs = 0;
1412   }
1413   fb_state.zsbuf = zsurf;
1414   pipe->set_framebuffer_state(pipe, &fb_state);
1415   pipe->set_sample_mask(pipe, sample_mask);
1416
1417   blitter_set_common_draw_rect_state(ctx);
1418   blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
1419   blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
1420                           UTIL_BLITTER_ATTRIB_NONE, NULL);
1421
1422   blitter_restore_vertex_states(ctx);
1423   blitter_restore_fragment_states(ctx);
1424   blitter_restore_fb_state(ctx);
1425   blitter_unset_running_flag(ctx);
1426}
1427
1428void util_blitter_copy_buffer(struct blitter_context *blitter,
1429                              struct pipe_resource *dst,
1430                              unsigned dstx,
1431                              struct pipe_resource *src,
1432                              unsigned srcx,
1433                              unsigned size)
1434{
1435   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1436   struct pipe_context *pipe = ctx->base.pipe;
1437   struct pipe_vertex_buffer vb;
1438   struct pipe_stream_output_target *so_target;
1439
1440   if (srcx >= src->width0 ||
1441       dstx >= dst->width0) {
1442      return;
1443   }
1444   if (srcx + size > src->width0) {
1445      size = src->width0 - srcx;
1446   }
1447   if (dstx + size > dst->width0) {
1448      size = dst->width0 - dstx;
1449   }
1450
1451   /* Drivers not capable of Stream Out should not call this function
1452    * in the first place. */
1453   assert(ctx->has_stream_out);
1454
1455   /* Some alignment is required. */
1456   if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
1457       !ctx->has_stream_out) {
1458      struct pipe_box box;
1459      u_box_1d(srcx, size, &box);
1460      util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
1461      return;
1462   }
1463
1464   blitter_set_running_flag(ctx);
1465   blitter_check_saved_vertex_states(ctx);
1466
1467   vb.buffer = src;
1468   vb.buffer_offset = srcx;
1469   vb.stride = 4;
1470
1471   pipe->set_vertex_buffers(pipe, 1, &vb);
1472   pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf);
1473   pipe->bind_vs_state(pipe, ctx->vs_pos_only);
1474   if (ctx->has_geometry_shader)
1475      pipe->bind_gs_state(pipe, NULL);
1476   pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
1477
1478   so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
1479   pipe->set_stream_output_targets(pipe, 1, &so_target, 0);
1480
1481   util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
1482
1483   blitter_restore_vertex_states(ctx);
1484   blitter_unset_running_flag(ctx);
1485   pipe_so_target_reference(&so_target, NULL);
1486}
1487
1488/* probably radeon specific */
1489void util_blitter_custom_resolve_color(struct blitter_context *blitter,
1490				       struct pipe_resource *dst,
1491				       unsigned dst_level,
1492				       unsigned dst_layer,
1493				       struct pipe_resource *src,
1494				       unsigned src_layer,
1495				       void *custom_blend)
1496{
1497   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1498   struct pipe_context *pipe = ctx->base.pipe;
1499   struct pipe_framebuffer_state fb_state;
1500   struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
1501
1502   blitter_set_running_flag(ctx);
1503   blitter_check_saved_vertex_states(ctx);
1504   blitter_check_saved_fragment_states(ctx);
1505
1506   /* bind states */
1507   pipe->bind_blend_state(pipe, custom_blend);
1508   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1509   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1510   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1511   pipe->set_sample_mask(pipe, (1ull << MAX2(1, src->nr_samples)) - 1);
1512
1513   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1514   surf_tmpl.format = dst->format;
1515   surf_tmpl.u.tex.level = dst_level;
1516   surf_tmpl.u.tex.first_layer = dst_layer;
1517   surf_tmpl.u.tex.last_layer = dst_layer;
1518   surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
1519
1520   dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
1521
1522   surf_tmpl.u.tex.level = 0;
1523   surf_tmpl.u.tex.first_layer = src_layer;
1524   surf_tmpl.u.tex.last_layer = src_layer;
1525
1526   srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
1527
1528   /* set a framebuffer state */
1529   fb_state.width = src->width0;
1530   fb_state.height = src->height0;
1531   fb_state.nr_cbufs = 2;
1532   fb_state.cbufs[0] = srcsurf;
1533   fb_state.cbufs[1] = dstsurf;
1534   fb_state.zsbuf = NULL;
1535   pipe->set_framebuffer_state(pipe, &fb_state);
1536
1537   blitter_set_common_draw_rect_state(ctx);
1538   blitter_set_dst_dimensions(ctx, src->width0, src->height0);
1539   blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
1540                           0, 0, NULL);
1541   blitter_restore_fb_state(ctx);
1542   blitter_restore_vertex_states(ctx);
1543   blitter_restore_fragment_states(ctx);
1544   blitter_unset_running_flag(ctx);
1545
1546   pipe_surface_reference(&srcsurf, NULL);
1547   pipe_surface_reference(&dstsurf, NULL);
1548}
1549
1550void util_blitter_custom_color(struct blitter_context *blitter,
1551                               struct pipe_surface *dstsurf,
1552                               void *custom_blend)
1553{
1554   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1555   struct pipe_context *pipe = ctx->base.pipe;
1556   struct pipe_framebuffer_state fb_state;
1557
1558   assert(dstsurf->texture);
1559   if (!dstsurf->texture)
1560      return;
1561
1562   /* check the saved state */
1563   blitter_set_running_flag(ctx);
1564   blitter_check_saved_vertex_states(ctx);
1565   blitter_check_saved_fragment_states(ctx);
1566   blitter_check_saved_fb_state(ctx);
1567
1568   /* bind states */
1569   pipe->bind_blend_state(pipe, custom_blend);
1570   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1571   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1572   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1573   pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
1574
1575   /* set a framebuffer state */
1576   fb_state.width = dstsurf->width;
1577   fb_state.height = dstsurf->height;
1578   fb_state.nr_cbufs = 1;
1579   fb_state.cbufs[0] = dstsurf;
1580   fb_state.zsbuf = 0;
1581   pipe->set_framebuffer_state(pipe, &fb_state);
1582   pipe->set_sample_mask(pipe, ~0);
1583
1584   blitter_set_common_draw_rect_state(ctx);
1585   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1586   blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
1587                           0, 0, NULL);
1588
1589   blitter_restore_vertex_states(ctx);
1590   blitter_restore_fragment_states(ctx);
1591   blitter_restore_fb_state(ctx);
1592   blitter_unset_running_flag(ctx);
1593}
1594