u_blitter.c revision 7864933acd81892be0692555ae6d37d4293adb2a
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
82   /* Blend state. */
83   void *blend_write_color;   /**< blend state with writemask of RGBA */
84   void *blend_keep_color;    /**< blend state with writemask of 0 */
85
86   /* Depth stencil alpha state. */
87   void *dsa_write_depth_stencil;
88   void *dsa_write_depth_keep_stencil;
89   void *dsa_keep_depth_stencil;
90   void *dsa_keep_depth_write_stencil;
91
92   /* Vertex elements states. */
93   void *velem_state;
94   void *velem_uint_state;
95   void *velem_sint_state;
96   void *velem_state_readbuf;
97
98   /* Sampler state. */
99   void *sampler_state;
100
101   /* Rasterizer state. */
102   void *rs_state;
103   void *rs_discard_state;
104
105   /* Viewport state. */
106   struct pipe_viewport_state viewport;
107
108   /* Destination surface dimensions. */
109   unsigned dst_width;
110   unsigned dst_height;
111
112   boolean has_geometry_shader;
113   boolean vertex_has_integers;
114   boolean has_stream_out;
115};
116
117static void blitter_draw_rectangle(struct blitter_context *blitter,
118                                   unsigned x, unsigned y,
119                                   unsigned width, unsigned height,
120                                   float depth,
121                                   enum blitter_attrib_type type,
122                                   const union pipe_color_union *attrib);
123
124
125struct blitter_context *util_blitter_create(struct pipe_context *pipe)
126{
127   struct blitter_context_priv *ctx;
128   struct pipe_blend_state blend;
129   struct pipe_depth_stencil_alpha_state dsa;
130   struct pipe_rasterizer_state rs_state;
131   struct pipe_sampler_state sampler_state;
132   struct pipe_vertex_element velem[2];
133   unsigned i;
134
135   ctx = CALLOC_STRUCT(blitter_context_priv);
136   if (!ctx)
137      return NULL;
138
139   ctx->base.pipe = pipe;
140   ctx->base.draw_rectangle = blitter_draw_rectangle;
141
142   /* init state objects for them to be considered invalid */
143   ctx->base.saved_blend_state = INVALID_PTR;
144   ctx->base.saved_dsa_state = INVALID_PTR;
145   ctx->base.saved_rs_state = INVALID_PTR;
146   ctx->base.saved_fs = INVALID_PTR;
147   ctx->base.saved_vs = INVALID_PTR;
148   ctx->base.saved_gs = INVALID_PTR;
149   ctx->base.saved_velem_state = INVALID_PTR;
150   ctx->base.saved_fb_state.nr_cbufs = ~0;
151   ctx->base.saved_num_sampler_views = ~0;
152   ctx->base.saved_num_sampler_states = ~0;
153   ctx->base.saved_num_vertex_buffers = ~0;
154   ctx->base.saved_num_so_targets = ~0;
155
156   ctx->has_geometry_shader =
157      pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
158                                     PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
159   ctx->vertex_has_integers =
160      pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_VERTEX,
161                                     PIPE_SHADER_CAP_INTEGERS);
162   ctx->has_stream_out =
163      pipe->screen->get_param(pipe->screen,
164                              PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
165
166   /* blend state objects */
167   memset(&blend, 0, sizeof(blend));
168   ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
169
170   blend.rt[0].colormask = PIPE_MASK_RGBA;
171   ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
172
173   /* depth stencil alpha state objects */
174   memset(&dsa, 0, sizeof(dsa));
175   ctx->dsa_keep_depth_stencil =
176      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
177
178   dsa.depth.enabled = 1;
179   dsa.depth.writemask = 1;
180   dsa.depth.func = PIPE_FUNC_ALWAYS;
181   ctx->dsa_write_depth_keep_stencil =
182      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
183
184   dsa.stencil[0].enabled = 1;
185   dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
186   dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
187   dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
188   dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
189   dsa.stencil[0].valuemask = 0xff;
190   dsa.stencil[0].writemask = 0xff;
191   ctx->dsa_write_depth_stencil =
192      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
193
194   dsa.depth.enabled = 0;
195   dsa.depth.writemask = 0;
196   ctx->dsa_keep_depth_write_stencil =
197      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
198
199   /* sampler state */
200   memset(&sampler_state, 0, sizeof(sampler_state));
201   sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
202   sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
203   sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
204   sampler_state.normalized_coords = 1;
205   ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
206
207   /* rasterizer state */
208   memset(&rs_state, 0, sizeof(rs_state));
209   rs_state.cull_face = PIPE_FACE_NONE;
210   rs_state.gl_rasterization_rules = 1;
211   rs_state.flatshade = 1;
212   rs_state.depth_clip = 1;
213   ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
214
215   if (ctx->has_stream_out) {
216      rs_state.rasterizer_discard = 1;
217      ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
218   }
219
220   /* vertex elements states */
221   memset(&velem[0], 0, sizeof(velem[0]) * 2);
222   for (i = 0; i < 2; i++) {
223      velem[i].src_offset = i * 4 * sizeof(float);
224      velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
225   }
226   ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
227
228   if (ctx->vertex_has_integers) {
229      memset(&velem[0], 0, sizeof(velem[0]) * 2);
230      velem[0].src_offset = 0;
231      velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
232      velem[1].src_offset = 4 * sizeof(float);
233      velem[1].src_format = PIPE_FORMAT_R32G32B32A32_SINT;
234      ctx->velem_sint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
235
236      memset(&velem[0], 0, sizeof(velem[0]) * 2);
237      velem[0].src_offset = 0;
238      velem[0].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
239      velem[1].src_offset = 4 * sizeof(float);
240      velem[1].src_format = PIPE_FORMAT_R32G32B32A32_UINT;
241      ctx->velem_uint_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
242   }
243
244   if (ctx->has_stream_out) {
245      velem[0].src_format = PIPE_FORMAT_R32_UINT;
246      ctx->velem_state_readbuf = pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
247   }
248
249   /* fragment shaders are created on-demand */
250
251   /* vertex shaders */
252   {
253      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
254                                      TGSI_SEMANTIC_GENERIC };
255      const uint semantic_indices[] = { 0, 0 };
256      ctx->vs =
257         util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
258                                             semantic_indices);
259   }
260   if (ctx->has_stream_out) {
261      struct pipe_stream_output_info so;
262      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
263      const uint semantic_indices[] = { 0 };
264
265      memset(&so, 0, sizeof(so));
266      so.num_outputs = 1;
267      so.output[0].num_components = 1;
268      so.stride[0] = 1;
269
270      ctx->vs_pos_only =
271         util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
272                                                     semantic_indices, &so);
273   }
274
275   /* set invariant vertex coordinates */
276   for (i = 0; i < 4; i++)
277      ctx->vertices[i][0][3] = 1; /*v.w*/
278
279   ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
280
281   return &ctx->base;
282}
283
284void util_blitter_destroy(struct blitter_context *blitter)
285{
286   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
287   struct pipe_context *pipe = blitter->pipe;
288   int i;
289
290   pipe->delete_blend_state(pipe, ctx->blend_write_color);
291   pipe->delete_blend_state(pipe, ctx->blend_keep_color);
292   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
293   pipe->delete_depth_stencil_alpha_state(pipe,
294                                          ctx->dsa_write_depth_keep_stencil);
295   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
296   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
297
298   pipe->delete_rasterizer_state(pipe, ctx->rs_state);
299   if (ctx->rs_discard_state)
300      pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
301   pipe->delete_vs_state(pipe, ctx->vs);
302   if (ctx->vs_pos_only)
303      pipe->delete_vs_state(pipe, ctx->vs_pos_only);
304   pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
305   if (ctx->vertex_has_integers) {
306      pipe->delete_vertex_elements_state(pipe, ctx->velem_sint_state);
307      pipe->delete_vertex_elements_state(pipe, ctx->velem_uint_state);
308   }
309   if (ctx->velem_state_readbuf)
310      pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf);
311
312   for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
313      if (ctx->fs_texfetch_col[i])
314         pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
315      if (ctx->fs_texfetch_depth[i])
316         pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
317   }
318
319   for (i = 0; i <= PIPE_MAX_COLOR_BUFS; i++) {
320      if (ctx->fs_col[i])
321         pipe->delete_fs_state(pipe, ctx->fs_col[i]);
322      if (ctx->fs_col_int[i])
323         pipe->delete_fs_state(pipe, ctx->fs_col_int[i]);
324   }
325
326   pipe->delete_sampler_state(pipe, ctx->sampler_state);
327   u_upload_destroy(ctx->upload);
328   FREE(ctx);
329}
330
331static void blitter_set_running_flag(struct blitter_context_priv *ctx)
332{
333   if (ctx->base.running) {
334      _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
335                    __LINE__);
336   }
337   ctx->base.running = TRUE;
338}
339
340static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
341{
342   if (!ctx->base.running) {
343      _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
344                    __LINE__);
345   }
346   ctx->base.running = FALSE;
347}
348
349static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
350{
351   assert(ctx->base.saved_num_vertex_buffers != ~0);
352   assert(ctx->base.saved_velem_state != INVALID_PTR);
353   assert(ctx->base.saved_vs != INVALID_PTR);
354   assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
355   assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0);
356   assert(ctx->base.saved_rs_state != INVALID_PTR);
357}
358
359static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
360{
361   struct pipe_context *pipe = ctx->base.pipe;
362   unsigned i;
363
364   /* Vertex buffers. */
365   pipe->set_vertex_buffers(pipe,
366                            ctx->base.saved_num_vertex_buffers,
367                            ctx->base.saved_vertex_buffers);
368
369   for (i = 0; i < ctx->base.saved_num_vertex_buffers; i++) {
370      if (ctx->base.saved_vertex_buffers[i].buffer) {
371         pipe_resource_reference(&ctx->base.saved_vertex_buffers[i].buffer,
372                                 NULL);
373      }
374   }
375   ctx->base.saved_num_vertex_buffers = ~0;
376
377   /* Vertex elements. */
378   pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
379   ctx->base.saved_velem_state = INVALID_PTR;
380
381   /* Vertex shader. */
382   pipe->bind_vs_state(pipe, ctx->base.saved_vs);
383   ctx->base.saved_vs = INVALID_PTR;
384
385   /* Geometry shader. */
386   if (ctx->has_geometry_shader) {
387      pipe->bind_gs_state(pipe, ctx->base.saved_gs);
388      ctx->base.saved_gs = INVALID_PTR;
389   }
390
391   /* Stream outputs. */
392   if (ctx->has_stream_out) {
393      pipe->set_stream_output_targets(pipe,
394                                      ctx->base.saved_num_so_targets,
395                                      ctx->base.saved_so_targets, ~0);
396
397      for (i = 0; i < ctx->base.saved_num_so_targets; i++)
398         pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
399
400      ctx->base.saved_num_so_targets = ~0;
401   }
402
403   /* Rasterizer. */
404   pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
405   ctx->base.saved_rs_state = INVALID_PTR;
406}
407
408static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
409{
410   assert(ctx->base.saved_fs != INVALID_PTR);
411   assert(ctx->base.saved_dsa_state != INVALID_PTR);
412   assert(ctx->base.saved_blend_state != INVALID_PTR);
413}
414
415static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
416{
417   struct pipe_context *pipe = ctx->base.pipe;
418
419   /* Fragment shader. */
420   pipe->bind_fs_state(pipe, ctx->base.saved_fs);
421   ctx->base.saved_fs = INVALID_PTR;
422
423   /* Depth, stencil, alpha. */
424   pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
425   ctx->base.saved_dsa_state = INVALID_PTR;
426
427   /* Blend state. */
428   pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
429   ctx->base.saved_blend_state = INVALID_PTR;
430
431   /* Miscellaneous states. */
432   /* XXX check whether these are saved and whether they need to be restored
433    * (depending on the operation) */
434   pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
435   pipe->set_viewport_state(pipe, &ctx->base.saved_viewport);
436}
437
438static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
439{
440   assert(ctx->base.saved_fb_state.nr_cbufs != ~0);
441}
442
443static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
444{
445   struct pipe_context *pipe = ctx->base.pipe;
446
447   pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
448   util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
449}
450
451static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
452{
453   assert(ctx->base.saved_num_sampler_states != ~0);
454   assert(ctx->base.saved_num_sampler_views != ~0);
455}
456
457static void blitter_restore_textures(struct blitter_context_priv *ctx)
458{
459   struct pipe_context *pipe = ctx->base.pipe;
460   unsigned i;
461
462   /* Fragment sampler states. */
463   pipe->bind_fragment_sampler_states(pipe,
464                                      ctx->base.saved_num_sampler_states,
465                                      ctx->base.saved_sampler_states);
466   ctx->base.saved_num_sampler_states = ~0;
467
468   /* Fragment sampler views. */
469   pipe->set_fragment_sampler_views(pipe,
470                                    ctx->base.saved_num_sampler_views,
471                                    ctx->base.saved_sampler_views);
472
473   for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
474      pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
475
476   ctx->base.saved_num_sampler_views = ~0;
477}
478
479static void blitter_set_rectangle(struct blitter_context_priv *ctx,
480                                  unsigned x1, unsigned y1,
481                                  unsigned x2, unsigned y2,
482                                  float depth)
483{
484   int i;
485
486   /* set vertex positions */
487   ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
488   ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
489
490   ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
491   ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
492
493   ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
494   ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
495
496   ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
497   ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
498
499   for (i = 0; i < 4; i++)
500      ctx->vertices[i][0][2] = depth; /*z*/
501
502   /* viewport */
503   ctx->viewport.scale[0] = 0.5f * ctx->dst_width;
504   ctx->viewport.scale[1] = 0.5f * ctx->dst_height;
505   ctx->viewport.scale[2] = 1.0f;
506   ctx->viewport.scale[3] = 1.0f;
507   ctx->viewport.translate[0] = 0.5f * ctx->dst_width;
508   ctx->viewport.translate[1] = 0.5f * ctx->dst_height;
509   ctx->viewport.translate[2] = 0.0f;
510   ctx->viewport.translate[3] = 0.0f;
511   ctx->base.pipe->set_viewport_state(ctx->base.pipe, &ctx->viewport);
512}
513
514static void blitter_set_clear_color(struct blitter_context_priv *ctx,
515                                    const union pipe_color_union *color)
516{
517   int i;
518
519   if (color) {
520      for (i = 0; i < 4; i++) {
521         uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1];
522         uiverts[0] = color->ui[0];
523         uiverts[1] = color->ui[1];
524         uiverts[2] = color->ui[2];
525         uiverts[3] = color->ui[3];
526      }
527   } else {
528      for (i = 0; i < 4; i++) {
529         ctx->vertices[i][1][0] = 0;
530         ctx->vertices[i][1][1] = 0;
531         ctx->vertices[i][1][2] = 0;
532         ctx->vertices[i][1][3] = 0;
533      }
534   }
535}
536
537static void get_texcoords(struct pipe_sampler_view *src,
538                          unsigned src_width0, unsigned src_height0,
539                          unsigned x1, unsigned y1,
540                          unsigned x2, unsigned y2,
541                          float out[4])
542{
543   struct pipe_resource *tex = src->texture;
544   unsigned level = src->u.tex.first_level;
545   boolean normalized = tex->target != PIPE_TEXTURE_RECT;
546
547   if (normalized) {
548      out[0] = x1 / (float)u_minify(src_width0,  level);
549      out[1] = y1 / (float)u_minify(src_height0, level);
550      out[2] = x2 / (float)u_minify(src_width0,  level);
551      out[3] = y2 / (float)u_minify(src_height0, level);
552   } else {
553      out[0] = x1;
554      out[1] = y1;
555      out[2] = x2;
556      out[3] = y2;
557   }
558}
559
560static void set_texcoords_in_vertices(const float coord[4],
561                                      float *out, unsigned stride)
562{
563   out[0] = coord[0]; /*t0.s*/
564   out[1] = coord[1]; /*t0.t*/
565   out += stride;
566   out[0] = coord[2]; /*t1.s*/
567   out[1] = coord[1]; /*t1.t*/
568   out += stride;
569   out[0] = coord[2]; /*t2.s*/
570   out[1] = coord[3]; /*t2.t*/
571   out += stride;
572   out[0] = coord[0]; /*t3.s*/
573   out[1] = coord[3]; /*t3.t*/
574}
575
576static void blitter_set_texcoords(struct blitter_context_priv *ctx,
577                                  struct pipe_sampler_view *src,
578                                  unsigned src_width0, unsigned src_height0,
579                                  unsigned layer,
580                                  unsigned x1, unsigned y1,
581                                  unsigned x2, unsigned y2)
582{
583   unsigned i;
584   float coord[4];
585   float face_coord[4][2];
586
587   get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
588
589   if (src->texture->target == PIPE_TEXTURE_CUBE) {
590      set_texcoords_in_vertices(coord, &face_coord[0][0], 2);
591      util_map_texcoords2d_onto_cubemap(layer,
592                                        /* pointer, stride in floats */
593                                        &face_coord[0][0], 2,
594                                        &ctx->vertices[0][1][0], 8);
595   } else {
596      set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
597   }
598
599   /* Set the layer. */
600   switch (src->texture->target) {
601   case PIPE_TEXTURE_3D:
602      {
603         float r = layer / (float)u_minify(src->texture->depth0,
604                                           src->u.tex.first_level);
605         for (i = 0; i < 4; i++)
606            ctx->vertices[i][1][2] = r; /*r*/
607      }
608      break;
609
610   case PIPE_TEXTURE_1D_ARRAY:
611      for (i = 0; i < 4; i++)
612         ctx->vertices[i][1][1] = layer; /*t*/
613      break;
614
615   case PIPE_TEXTURE_2D_ARRAY:
616      for (i = 0; i < 4; i++)
617         ctx->vertices[i][1][2] = layer; /*r*/
618      break;
619
620   default:;
621   }
622}
623
624static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
625                                       unsigned width, unsigned height)
626{
627   ctx->dst_width = width;
628   ctx->dst_height = height;
629}
630
631static INLINE
632void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs,
633                         boolean int_format)
634{
635   struct pipe_context *pipe = ctx->base.pipe;
636
637   assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
638
639   if (int_format) {
640      if (!ctx->fs_col_int[num_cbufs])
641         ctx->fs_col_int[num_cbufs] =
642            util_make_fragment_cloneinput_shader(pipe, num_cbufs,
643                                                 TGSI_SEMANTIC_GENERIC,
644                                                 TGSI_INTERPOLATE_CONSTANT);
645      return ctx->fs_col_int[num_cbufs];
646   } else {
647      if (!ctx->fs_col[num_cbufs])
648         ctx->fs_col[num_cbufs] =
649            util_make_fragment_cloneinput_shader(pipe, num_cbufs,
650                                                 TGSI_SEMANTIC_GENERIC,
651                                                 TGSI_INTERPOLATE_LINEAR);
652      return ctx->fs_col[num_cbufs];
653   }
654}
655
656/** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
657static unsigned
658pipe_tex_to_tgsi_tex(enum pipe_texture_target pipe_tex_target)
659{
660   switch (pipe_tex_target) {
661   case PIPE_TEXTURE_1D:
662      return TGSI_TEXTURE_1D;
663   case PIPE_TEXTURE_2D:
664      return TGSI_TEXTURE_2D;
665   case PIPE_TEXTURE_RECT:
666      return TGSI_TEXTURE_RECT;
667   case PIPE_TEXTURE_3D:
668      return TGSI_TEXTURE_3D;
669   case PIPE_TEXTURE_CUBE:
670      return TGSI_TEXTURE_CUBE;
671   case PIPE_TEXTURE_1D_ARRAY:
672      return TGSI_TEXTURE_1D_ARRAY;
673   case PIPE_TEXTURE_2D_ARRAY:
674      return TGSI_TEXTURE_2D_ARRAY;
675   default:
676      assert(0 && "unexpected texture target");
677      return TGSI_TEXTURE_UNKNOWN;
678   }
679}
680
681
682static INLINE
683void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
684                                  unsigned tex_target)
685{
686   struct pipe_context *pipe = ctx->base.pipe;
687
688   assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
689
690   /* Create the fragment shader on-demand. */
691   if (!ctx->fs_texfetch_col[tex_target]) {
692      unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
693
694      ctx->fs_texfetch_col[tex_target] =
695        util_make_fragment_tex_shader(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR);
696   }
697
698   return ctx->fs_texfetch_col[tex_target];
699}
700
701static INLINE
702void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
703                                    unsigned tex_target)
704{
705   struct pipe_context *pipe = ctx->base.pipe;
706
707   assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
708
709   /* Create the fragment shader on-demand. */
710   if (!ctx->fs_texfetch_depth[tex_target]) {
711      unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
712
713      ctx->fs_texfetch_depth[tex_target] =
714         util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
715                                                  TGSI_INTERPOLATE_LINEAR);
716   }
717
718   return ctx->fs_texfetch_depth[tex_target];
719}
720
721static void blitter_draw(struct blitter_context_priv *ctx,
722                         unsigned x1, unsigned y1,
723                         unsigned x2, unsigned y2,
724                         float depth)
725{
726   struct pipe_resource *buf = NULL;
727   unsigned offset = 0;
728
729   blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
730
731   u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices,
732                 &offset, &buf);
733   u_upload_unmap(ctx->upload);
734   util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, offset,
735                           PIPE_PRIM_TRIANGLE_FAN, 4, 2);
736}
737
738static void blitter_draw_rectangle(struct blitter_context *blitter,
739                                   unsigned x1, unsigned y1,
740                                   unsigned x2, unsigned y2,
741                                   float depth,
742                                   enum blitter_attrib_type type,
743                                   const union pipe_color_union *attrib)
744{
745   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
746
747   switch (type) {
748      case UTIL_BLITTER_ATTRIB_COLOR:
749         blitter_set_clear_color(ctx, attrib);
750         break;
751
752      case UTIL_BLITTER_ATTRIB_TEXCOORD:
753         set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
754         break;
755
756      default:;
757   }
758
759   blitter_draw(ctx, x1, y1, x2, y2, depth);
760}
761
762static void util_blitter_clear_custom(struct blitter_context *blitter,
763                                      unsigned width, unsigned height,
764                                      unsigned num_cbufs,
765                                      unsigned clear_buffers,
766                                      enum pipe_format cbuf_format,
767                                      const union pipe_color_union *color,
768                                      double depth, unsigned stencil,
769                                      void *custom_blend, void *custom_dsa)
770{
771   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
772   struct pipe_context *pipe = ctx->base.pipe;
773   struct pipe_stencil_ref sr = { { 0 } };
774   boolean int_format = util_format_is_pure_integer(cbuf_format);
775   assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
776
777   blitter_set_running_flag(ctx);
778   blitter_check_saved_vertex_states(ctx);
779   blitter_check_saved_fragment_states(ctx);
780
781   /* bind states */
782   if (custom_blend) {
783      pipe->bind_blend_state(pipe, custom_blend);
784   } else if (clear_buffers & PIPE_CLEAR_COLOR) {
785      pipe->bind_blend_state(pipe, ctx->blend_write_color);
786   } else {
787      pipe->bind_blend_state(pipe, ctx->blend_keep_color);
788   }
789
790   if (custom_dsa) {
791      pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
792   } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
793      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
794   } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
795      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
796   } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
797      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
798   } else {
799      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
800   }
801
802   sr.ref_value[0] = stencil & 0xff;
803   pipe->set_stencil_ref(pipe, &sr);
804
805   pipe->bind_rasterizer_state(pipe, ctx->rs_state);
806   if (util_format_is_pure_sint(cbuf_format)) {
807      pipe->bind_vertex_elements_state(pipe, ctx->velem_sint_state);
808   } else if (util_format_is_pure_uint(cbuf_format)) {
809      pipe->bind_vertex_elements_state(pipe, ctx->velem_uint_state);
810   } else {
811      pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
812   }
813   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs, int_format));
814   pipe->bind_vs_state(pipe, ctx->vs);
815   if (ctx->has_geometry_shader)
816      pipe->bind_gs_state(pipe, NULL);
817
818   blitter_set_dst_dimensions(ctx, width, height);
819   blitter->draw_rectangle(blitter, 0, 0, width, height, depth,
820                           UTIL_BLITTER_ATTRIB_COLOR, color);
821
822   blitter_restore_vertex_states(ctx);
823   blitter_restore_fragment_states(ctx);
824   blitter_unset_running_flag(ctx);
825}
826
827void util_blitter_clear(struct blitter_context *blitter,
828                        unsigned width, unsigned height,
829                        unsigned num_cbufs,
830                        unsigned clear_buffers,
831                        enum pipe_format cbuf_format,
832                        const union pipe_color_union *color,
833                        double depth, unsigned stencil)
834{
835   util_blitter_clear_custom(blitter, width, height, num_cbufs,
836                             clear_buffers, cbuf_format, color, depth, stencil,
837                             NULL, NULL);
838}
839
840void util_blitter_clear_depth_custom(struct blitter_context *blitter,
841                                     unsigned width, unsigned height,
842                                     double depth, void *custom_dsa)
843{
844    static const union pipe_color_union color;
845    util_blitter_clear_custom(blitter, width, height, 0,
846                              0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa);
847}
848
849static
850boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
851                   unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
852{
853   return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1;
854}
855
856void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
857                                      struct pipe_resource *dst,
858                                      unsigned dstlevel,
859                                      unsigned dstz,
860                                      const struct pipe_box *srcbox)
861{
862    memset(dst_templ, 0, sizeof(*dst_templ));
863    dst_templ->format = dst->format;
864    if (util_format_is_depth_or_stencil(dst->format)) {
865	dst_templ->usage = PIPE_BIND_DEPTH_STENCIL;
866    } else {
867	dst_templ->usage = PIPE_BIND_RENDER_TARGET;
868    }
869    dst_templ->format = util_format_linear(dst->format);
870    dst_templ->u.tex.level = dstlevel;
871    dst_templ->u.tex.first_layer = dstz;
872    dst_templ->u.tex.last_layer = dstz + srcbox->depth - 1;
873}
874
875void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
876                                      struct pipe_resource *src,
877                                      unsigned srclevel)
878{
879    memset(src_templ, 0, sizeof(*src_templ));
880    src_templ->format = util_format_linear(src->format);
881    src_templ->u.tex.first_level = srclevel;
882    src_templ->u.tex.last_level = srclevel;
883    src_templ->u.tex.first_layer = 0;
884    src_templ->u.tex.last_layer =
885        src->target == PIPE_TEXTURE_3D ? src->depth0 - 1
886                                       : src->array_size - 1;
887    src_templ->swizzle_r = PIPE_SWIZZLE_RED;
888    src_templ->swizzle_g = PIPE_SWIZZLE_GREEN;
889    src_templ->swizzle_b = PIPE_SWIZZLE_BLUE;
890    src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA;
891}
892
893void util_blitter_copy_texture(struct blitter_context *blitter,
894                               struct pipe_resource *dst,
895                               unsigned dstlevel,
896                               unsigned dstx, unsigned dsty, unsigned dstz,
897                               struct pipe_resource *src,
898                               unsigned srclevel,
899                               const struct pipe_box *srcbox,
900                               boolean ignore_stencil)
901{
902   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
903   struct pipe_context *pipe = ctx->base.pipe;
904   struct pipe_screen *screen = pipe->screen;
905   struct pipe_surface *dst_view, dst_templ;
906   struct pipe_sampler_view src_templ, *src_view;
907   unsigned bind;
908   boolean is_stencil, is_depth;
909
910   /* Give up if textures are not set. */
911   assert(dst && src);
912   if (!dst || !src)
913      return;
914
915   assert(src->target < PIPE_MAX_TEXTURE_TYPES);
916
917   /* Is this a ZS format? */
918   is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
919   is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
920
921   if (is_depth || is_stencil)
922      bind = PIPE_BIND_DEPTH_STENCIL;
923   else
924      bind = PIPE_BIND_RENDER_TARGET;
925
926   /* Check if we can sample from and render to the surfaces. */
927   /* (assuming copying a stencil buffer is not possible) */
928   if ((!ignore_stencil && is_stencil) ||
929       !screen->is_format_supported(screen, dst->format, dst->target,
930                                    dst->nr_samples, bind) ||
931       !screen->is_format_supported(screen, src->format, src->target,
932                                    src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
933      blitter_set_running_flag(ctx);
934      util_resource_copy_region(pipe, dst, dstlevel, dstx, dsty, dstz,
935                                src, srclevel, srcbox);
936      blitter_unset_running_flag(ctx);
937      return;
938   }
939
940   /* Initialize the surface. */
941   util_blitter_default_dst_texture(&dst_templ, dst, dstlevel, dstz, srcbox);
942   dst_view = pipe->create_surface(pipe, dst, &dst_templ);
943
944   /* Initialize the sampler view. */
945   util_blitter_default_src_texture(&src_templ, src, srclevel);
946   src_view = pipe->create_sampler_view(pipe, src, &src_templ);
947
948   /* Copy. */
949   util_blitter_copy_texture_view(blitter, dst_view, dstx, dsty, src_view,
950                                  srcbox, src->width0, src->height0);
951
952   pipe_surface_reference(&dst_view, NULL);
953   pipe_sampler_view_reference(&src_view, NULL);
954}
955
956void util_blitter_copy_texture_view(struct blitter_context *blitter,
957                                    struct pipe_surface *dst,
958                                    unsigned dstx, unsigned dsty,
959                                    struct pipe_sampler_view *src,
960                                    const struct pipe_box *srcbox,
961                                    unsigned src_width0, unsigned src_height0)
962{
963   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
964   struct pipe_context *pipe = ctx->base.pipe;
965   struct pipe_framebuffer_state fb_state;
966   enum pipe_texture_target src_target = src->texture->target;
967   unsigned width = srcbox->width;
968   unsigned height = srcbox->height;
969
970   /* Sanity checks. */
971   if (dst->texture == src->texture &&
972       dst->u.tex.level == src->u.tex.first_level) {
973      assert(!is_overlap(srcbox->x, srcbox->x + width, srcbox->y, srcbox->y + height,
974                         dstx, dstx + width, dsty, dsty + height));
975   }
976   /* XXX should handle 3d regions */
977   assert(srcbox->depth == 1);
978
979   /* Check whether the states are properly saved. */
980   blitter_set_running_flag(ctx);
981   blitter_check_saved_vertex_states(ctx);
982   blitter_check_saved_fragment_states(ctx);
983   blitter_check_saved_textures(ctx);
984   blitter_check_saved_fb_state(ctx);
985
986   /* Initialize framebuffer state. */
987   fb_state.width = dst->width;
988   fb_state.height = dst->height;
989
990   if (util_format_is_depth_or_stencil(dst->format)) {
991      pipe->bind_blend_state(pipe, ctx->blend_keep_color);
992      pipe->bind_depth_stencil_alpha_state(pipe,
993                                           ctx->dsa_write_depth_keep_stencil);
994      pipe->bind_fs_state(pipe,
995            blitter_get_fs_texfetch_depth(ctx, src_target));
996
997      fb_state.nr_cbufs = 0;
998      fb_state.zsbuf = dst;
999   } else {
1000      pipe->bind_blend_state(pipe, ctx->blend_write_color);
1001      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1002      pipe->bind_fs_state(pipe,
1003            blitter_get_fs_texfetch_col(ctx, src_target));
1004
1005      fb_state.nr_cbufs = 1;
1006      fb_state.cbufs[0] = dst;
1007      fb_state.zsbuf = 0;
1008   }
1009
1010   /* Set rasterizer state, shaders, and textures. */
1011   pipe->bind_rasterizer_state(pipe, ctx->rs_state);
1012   pipe->bind_vs_state(pipe, ctx->vs);
1013   if (ctx->has_geometry_shader)
1014      pipe->bind_gs_state(pipe, NULL);
1015   pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state);
1016   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1017   pipe->set_fragment_sampler_views(pipe, 1, &src);
1018   pipe->set_framebuffer_state(pipe, &fb_state);
1019
1020   blitter_set_dst_dimensions(ctx, dst->width, dst->height);
1021
1022   switch (src_target) {
1023      /* Draw the quad with the draw_rectangle callback. */
1024      case PIPE_TEXTURE_1D:
1025      case PIPE_TEXTURE_2D:
1026      case PIPE_TEXTURE_RECT:
1027         {
1028            /* Set texture coordinates. - use a pipe color union
1029             * for interface purposes.
1030             * XXX pipe_color_union is a wrong name since we use that to set
1031             * texture coordinates too.
1032             */
1033            union pipe_color_union coord;
1034            get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1035                          srcbox->x+width, srcbox->y+height, coord.f);
1036
1037            /* Draw. */
1038            blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1039                                    UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1040         }
1041         break;
1042
1043      /* Draw the quad with the generic codepath. */
1044      default:
1045         /* Set texture coordinates. */
1046         switch (src_target) {
1047         case PIPE_TEXTURE_1D_ARRAY:
1048         case PIPE_TEXTURE_2D_ARRAY:
1049         case PIPE_TEXTURE_3D:
1050         case PIPE_TEXTURE_CUBE:
1051            blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z,
1052                                  srcbox->y, srcbox->x,
1053                                  srcbox->x + width, srcbox->y + height);
1054            break;
1055
1056         default:
1057            assert(0);
1058         }
1059
1060         blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0);
1061         break;
1062   }
1063
1064   blitter_restore_vertex_states(ctx);
1065   blitter_restore_fragment_states(ctx);
1066   blitter_restore_textures(ctx);
1067   blitter_restore_fb_state(ctx);
1068   blitter_unset_running_flag(ctx);
1069}
1070
1071/* Clear a region of a color surface to a constant value. */
1072void util_blitter_clear_render_target(struct blitter_context *blitter,
1073                                      struct pipe_surface *dstsurf,
1074                                      const union pipe_color_union *color,
1075                                      unsigned dstx, unsigned dsty,
1076                                      unsigned width, unsigned height)
1077{
1078   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1079   struct pipe_context *pipe = ctx->base.pipe;
1080   struct pipe_framebuffer_state fb_state;
1081
1082   assert(dstsurf->texture);
1083   if (!dstsurf->texture)
1084      return;
1085
1086   /* check the saved state */
1087   blitter_set_running_flag(ctx);
1088   blitter_check_saved_vertex_states(ctx);
1089   blitter_check_saved_fragment_states(ctx);
1090   blitter_check_saved_fb_state(ctx);
1091
1092   /* bind states */
1093   pipe->bind_blend_state(pipe, ctx->blend_write_color);
1094   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1095   pipe->bind_rasterizer_state(pipe, ctx->rs_state);
1096   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1097   pipe->bind_vs_state(pipe, ctx->vs);
1098   if (ctx->has_geometry_shader)
1099      pipe->bind_gs_state(pipe, NULL);
1100   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1101
1102   /* set a framebuffer state */
1103   fb_state.width = dstsurf->width;
1104   fb_state.height = dstsurf->height;
1105   fb_state.nr_cbufs = 1;
1106   fb_state.cbufs[0] = dstsurf;
1107   fb_state.zsbuf = 0;
1108   pipe->set_framebuffer_state(pipe, &fb_state);
1109
1110   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1111   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1112                           UTIL_BLITTER_ATTRIB_COLOR, color);
1113
1114   blitter_restore_vertex_states(ctx);
1115   blitter_restore_fragment_states(ctx);
1116   blitter_restore_fb_state(ctx);
1117   blitter_unset_running_flag(ctx);
1118}
1119
1120/* Clear a region of a depth stencil surface. */
1121void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
1122                                      struct pipe_surface *dstsurf,
1123                                      unsigned clear_flags,
1124                                      double depth,
1125                                      unsigned stencil,
1126                                      unsigned dstx, unsigned dsty,
1127                                      unsigned width, unsigned height)
1128{
1129   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1130   struct pipe_context *pipe = ctx->base.pipe;
1131   struct pipe_framebuffer_state fb_state;
1132   struct pipe_stencil_ref sr = { { 0 } };
1133
1134   assert(dstsurf->texture);
1135   if (!dstsurf->texture)
1136      return;
1137
1138   /* check the saved state */
1139   blitter_set_running_flag(ctx);
1140   blitter_check_saved_vertex_states(ctx);
1141   blitter_check_saved_fragment_states(ctx);
1142   blitter_check_saved_fb_state(ctx);
1143
1144   /* bind states */
1145   pipe->bind_blend_state(pipe, ctx->blend_keep_color);
1146   if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1147      sr.ref_value[0] = stencil & 0xff;
1148      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1149      pipe->set_stencil_ref(pipe, &sr);
1150   }
1151   else if (clear_flags & PIPE_CLEAR_DEPTH) {
1152      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1153   }
1154   else if (clear_flags & PIPE_CLEAR_STENCIL) {
1155      sr.ref_value[0] = stencil & 0xff;
1156      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1157      pipe->set_stencil_ref(pipe, &sr);
1158   }
1159   else
1160      /* hmm that should be illegal probably, or make it a no-op somewhere */
1161      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1162
1163   pipe->bind_rasterizer_state(pipe, ctx->rs_state);
1164   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1165   pipe->bind_vs_state(pipe, ctx->vs);
1166   if (ctx->has_geometry_shader)
1167      pipe->bind_gs_state(pipe, NULL);
1168   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1169
1170   /* set a framebuffer state */
1171   fb_state.width = dstsurf->width;
1172   fb_state.height = dstsurf->height;
1173   fb_state.nr_cbufs = 0;
1174   fb_state.cbufs[0] = 0;
1175   fb_state.zsbuf = dstsurf;
1176   pipe->set_framebuffer_state(pipe, &fb_state);
1177
1178   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1179   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, depth,
1180                           UTIL_BLITTER_ATTRIB_NONE, NULL);
1181
1182   blitter_restore_vertex_states(ctx);
1183   blitter_restore_fragment_states(ctx);
1184   blitter_restore_fb_state(ctx);
1185   blitter_unset_running_flag(ctx);
1186}
1187
1188/* draw a rectangle across a region using a custom dsa stage - for r600g */
1189void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
1190				       struct pipe_surface *zsurf,
1191				       struct pipe_surface *cbsurf,
1192				       void *dsa_stage, float depth)
1193{
1194   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1195   struct pipe_context *pipe = ctx->base.pipe;
1196   struct pipe_framebuffer_state fb_state;
1197
1198   assert(zsurf->texture);
1199   if (!zsurf->texture)
1200      return;
1201
1202   /* check the saved state */
1203   blitter_set_running_flag(ctx);
1204   blitter_check_saved_vertex_states(ctx);
1205   blitter_check_saved_fragment_states(ctx);
1206   blitter_check_saved_fb_state(ctx);
1207
1208   /* bind states */
1209   pipe->bind_blend_state(pipe, ctx->blend_write_color);
1210   pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
1211
1212   pipe->bind_rasterizer_state(pipe, ctx->rs_state);
1213   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1214   pipe->bind_vs_state(pipe, ctx->vs);
1215   if (ctx->has_geometry_shader)
1216      pipe->bind_gs_state(pipe, NULL);
1217   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1218
1219   /* set a framebuffer state */
1220   fb_state.width = zsurf->width;
1221   fb_state.height = zsurf->height;
1222   fb_state.nr_cbufs = 1;
1223   if (cbsurf) {
1224	   fb_state.cbufs[0] = cbsurf;
1225	   fb_state.nr_cbufs = 1;
1226   } else {
1227	   fb_state.cbufs[0] = NULL;
1228	   fb_state.nr_cbufs = 0;
1229   }
1230   fb_state.zsbuf = zsurf;
1231   pipe->set_framebuffer_state(pipe, &fb_state);
1232
1233   blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
1234   blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
1235                           UTIL_BLITTER_ATTRIB_NONE, NULL);
1236
1237   blitter_restore_vertex_states(ctx);
1238   blitter_restore_fragment_states(ctx);
1239   blitter_restore_fb_state(ctx);
1240   blitter_unset_running_flag(ctx);
1241}
1242
1243void util_blitter_copy_buffer(struct blitter_context *blitter,
1244                              struct pipe_resource *dst,
1245                              unsigned dstx,
1246                              struct pipe_resource *src,
1247                              unsigned srcx,
1248                              unsigned size)
1249{
1250   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1251   struct pipe_context *pipe = ctx->base.pipe;
1252   struct pipe_vertex_buffer vb;
1253   struct pipe_stream_output_target *so_target;
1254
1255   if (srcx >= src->width0 ||
1256       dstx >= dst->width0) {
1257      return;
1258   }
1259   if (srcx + size > src->width0) {
1260      size = src->width0 - srcx;
1261   }
1262   if (dstx + size > dst->width0) {
1263      size = dst->width0 - dstx;
1264   }
1265
1266   /* Drivers not capable of Stream Out should not call this function
1267    * in the first place. */
1268   assert(ctx->has_stream_out);
1269
1270   /* Some alignment is required. */
1271   if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
1272       !ctx->has_stream_out) {
1273      struct pipe_box box;
1274      u_box_1d(srcx, size, &box);
1275      util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
1276      return;
1277   }
1278
1279   blitter_set_running_flag(ctx);
1280   blitter_check_saved_vertex_states(ctx);
1281
1282   vb.buffer = src;
1283   vb.buffer_offset = srcx;
1284   vb.stride = 4;
1285
1286   pipe->set_vertex_buffers(pipe, 1, &vb);
1287   pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf);
1288   pipe->bind_vs_state(pipe, ctx->vs_pos_only);
1289   if (ctx->has_geometry_shader)
1290      pipe->bind_gs_state(pipe, NULL);
1291   pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
1292
1293   so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
1294   pipe->set_stream_output_targets(pipe, 1, &so_target, 0);
1295
1296   util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
1297
1298   blitter_restore_vertex_states(ctx);
1299   blitter_unset_running_flag(ctx);
1300   pipe_so_target_reference(&so_target, NULL);
1301}
1302