u_blitter.c revision 848b8605
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 VMWARE 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, resource_copy_region, and blit 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
54#define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
55   ((clear_buffers) / PIPE_CLEAR_COLOR0)
56
57#define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
58#define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
59
60struct blitter_context_priv
61{
62   struct blitter_context base;
63
64   struct u_upload_mgr *upload;
65
66   float vertices[4][2][4];   /**< {pos, color} or {pos, texcoord} */
67
68   /* Templates for various state objects. */
69
70   /* Constant state objects. */
71   /* Vertex shaders. */
72   void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
73   void *vs_pos_only; /**< Vertex shader which passes pos to the output.*/
74   void *vs_layered; /**< Vertex shader which sets LAYER = INSTANCEID. */
75
76   /* Fragment shaders. */
77   void *fs_empty;
78   void *fs_write_one_cbuf;
79   void *fs_write_all_cbufs;
80
81   /* FS which outputs a color from a texture,
82      where the index is PIPE_TEXTURE_* to be sampled. */
83   void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
84
85   /* FS which outputs a depth from a texture,
86      where the index is PIPE_TEXTURE_* to be sampled. */
87   void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
88   void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES];
89   void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES];
90
91   /* FS which outputs one sample from a multisample texture. */
92   void *fs_texfetch_col_msaa[PIPE_MAX_TEXTURE_TYPES];
93   void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
94   void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
95   void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
96
97   /* FS which outputs an average of all samples. */
98   void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
99   void *fs_resolve_sint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
100   void *fs_resolve_uint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
101
102   /* Blend state. */
103   void *blend[PIPE_MASK_RGBA+1]; /**< blend state with writemask */
104   void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1];
105
106   /* Depth stencil alpha state. */
107   void *dsa_write_depth_stencil;
108   void *dsa_write_depth_keep_stencil;
109   void *dsa_keep_depth_stencil;
110   void *dsa_keep_depth_write_stencil;
111
112   /* Vertex elements states. */
113   void *velem_state;
114   void *velem_state_readbuf[4]; /**< X, XY, XYZ, XYZW */
115
116   /* Sampler state. */
117   void *sampler_state;
118   void *sampler_state_linear;
119   void *sampler_state_rect;
120   void *sampler_state_rect_linear;
121
122   /* Rasterizer state. */
123   void *rs_state, *rs_state_scissor, *rs_discard_state;
124
125   /* Viewport state. */
126   struct pipe_viewport_state viewport;
127
128   /* Destination surface dimensions. */
129   unsigned dst_width;
130   unsigned dst_height;
131
132   boolean has_geometry_shader;
133   boolean has_stream_out;
134   boolean has_stencil_export;
135   boolean has_texture_multisample;
136
137   /* The Draw module overrides these functions.
138    * Always create the blitter before Draw. */
139   void   (*bind_fs_state)(struct pipe_context *, void *);
140   void   (*delete_fs_state)(struct pipe_context *, void *);
141};
142
143static struct pipe_surface *
144util_blitter_get_next_surface_layer(struct pipe_context *pipe,
145                                    struct pipe_surface *surf);
146
147struct blitter_context *util_blitter_create(struct pipe_context *pipe)
148{
149   struct blitter_context_priv *ctx;
150   struct pipe_blend_state blend;
151   struct pipe_depth_stencil_alpha_state dsa;
152   struct pipe_rasterizer_state rs_state;
153   struct pipe_sampler_state sampler_state;
154   struct pipe_vertex_element velem[2];
155   unsigned i;
156
157   ctx = CALLOC_STRUCT(blitter_context_priv);
158   if (!ctx)
159      return NULL;
160
161   ctx->base.pipe = pipe;
162   ctx->base.draw_rectangle = util_blitter_draw_rectangle;
163   ctx->base.get_next_surface_layer = util_blitter_get_next_surface_layer;
164
165   ctx->bind_fs_state = pipe->bind_fs_state;
166   ctx->delete_fs_state = pipe->delete_fs_state;
167
168   /* init state objects for them to be considered invalid */
169   ctx->base.saved_blend_state = INVALID_PTR;
170   ctx->base.saved_dsa_state = INVALID_PTR;
171   ctx->base.saved_rs_state = INVALID_PTR;
172   ctx->base.saved_fs = INVALID_PTR;
173   ctx->base.saved_vs = INVALID_PTR;
174   ctx->base.saved_gs = INVALID_PTR;
175   ctx->base.saved_velem_state = INVALID_PTR;
176   ctx->base.saved_fb_state.nr_cbufs = ~0;
177   ctx->base.saved_num_sampler_views = ~0;
178   ctx->base.saved_num_sampler_states = ~0;
179   ctx->base.saved_num_so_targets = ~0;
180
181   ctx->has_geometry_shader =
182      pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
183                                     PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
184   ctx->has_stream_out =
185      pipe->screen->get_param(pipe->screen,
186                              PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
187
188   ctx->has_stencil_export =
189         pipe->screen->get_param(pipe->screen,
190                                 PIPE_CAP_SHADER_STENCIL_EXPORT);
191
192   ctx->has_texture_multisample =
193      pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE);
194
195   /* blend state objects */
196   memset(&blend, 0, sizeof(blend));
197
198   for (i = 0; i <= PIPE_MASK_RGBA; i++) {
199      blend.rt[0].colormask = i;
200      ctx->blend[i] = pipe->create_blend_state(pipe, &blend);
201   }
202
203   /* depth stencil alpha state objects */
204   memset(&dsa, 0, sizeof(dsa));
205   ctx->dsa_keep_depth_stencil =
206      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
207
208   dsa.depth.enabled = 1;
209   dsa.depth.writemask = 1;
210   dsa.depth.func = PIPE_FUNC_ALWAYS;
211   ctx->dsa_write_depth_keep_stencil =
212      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
213
214   dsa.stencil[0].enabled = 1;
215   dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
216   dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
217   dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
218   dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
219   dsa.stencil[0].valuemask = 0xff;
220   dsa.stencil[0].writemask = 0xff;
221   ctx->dsa_write_depth_stencil =
222      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
223
224   dsa.depth.enabled = 0;
225   dsa.depth.writemask = 0;
226   ctx->dsa_keep_depth_write_stencil =
227      pipe->create_depth_stencil_alpha_state(pipe, &dsa);
228
229   /* sampler state */
230   memset(&sampler_state, 0, sizeof(sampler_state));
231   sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
232   sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
233   sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
234   sampler_state.normalized_coords = 1;
235   ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
236   sampler_state.normalized_coords = 0;
237   ctx->sampler_state_rect = pipe->create_sampler_state(pipe, &sampler_state);
238
239   sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
240   sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
241   sampler_state.normalized_coords = 1;
242   ctx->sampler_state_linear = pipe->create_sampler_state(pipe, &sampler_state);
243   sampler_state.normalized_coords = 0;
244   ctx->sampler_state_rect_linear = pipe->create_sampler_state(pipe, &sampler_state);
245
246   /* rasterizer state */
247   memset(&rs_state, 0, sizeof(rs_state));
248   rs_state.cull_face = PIPE_FACE_NONE;
249   rs_state.half_pixel_center = 1;
250   rs_state.bottom_edge_rule = 1;
251   rs_state.flatshade = 1;
252   rs_state.depth_clip = 1;
253   ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
254
255   rs_state.scissor = 1;
256   ctx->rs_state_scissor = pipe->create_rasterizer_state(pipe, &rs_state);
257
258   if (ctx->has_stream_out) {
259      rs_state.scissor = 0;
260      rs_state.rasterizer_discard = 1;
261      ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
262   }
263
264   ctx->base.vb_slot = 0; /* 0 for now */
265
266   /* vertex elements states */
267   memset(&velem[0], 0, sizeof(velem[0]) * 2);
268   for (i = 0; i < 2; i++) {
269      velem[i].src_offset = i * 4 * sizeof(float);
270      velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
271      velem[i].vertex_buffer_index = ctx->base.vb_slot;
272   }
273   ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
274
275   if (ctx->has_stream_out) {
276      static enum pipe_format formats[4] = {
277         PIPE_FORMAT_R32_UINT,
278         PIPE_FORMAT_R32G32_UINT,
279         PIPE_FORMAT_R32G32B32_UINT,
280         PIPE_FORMAT_R32G32B32A32_UINT
281      };
282
283      for (i = 0; i < 4; i++) {
284         velem[0].src_format = formats[i];
285         velem[0].vertex_buffer_index = ctx->base.vb_slot;
286         ctx->velem_state_readbuf[i] =
287               pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
288      }
289   }
290
291   /* Fragment shaders are created on-demand, except these.
292    * The interpolation must be constant for integer texture clearing to work.
293    */
294   ctx->fs_empty = util_make_empty_fragment_shader(pipe);
295   ctx->fs_write_one_cbuf =
296      util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
297                                            TGSI_INTERPOLATE_CONSTANT, FALSE);
298   ctx->fs_write_all_cbufs =
299      util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
300                                            TGSI_INTERPOLATE_CONSTANT, TRUE);
301
302   /* vertex shaders */
303   {
304      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
305                                      TGSI_SEMANTIC_GENERIC };
306      const uint semantic_indices[] = { 0, 0 };
307      ctx->vs =
308         util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
309                                             semantic_indices);
310   }
311
312   if (ctx->has_stream_out) {
313      struct pipe_stream_output_info so;
314      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
315      const uint semantic_indices[] = { 0 };
316
317      memset(&so, 0, sizeof(so));
318      so.num_outputs = 1;
319      so.output[0].num_components = 1;
320      so.stride[0] = 1;
321
322      ctx->vs_pos_only =
323         util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
324                                                     semantic_indices, &so);
325   }
326
327   if (pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) &&
328       pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT)) {
329      ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe);
330   }
331
332   /* set invariant vertex coordinates */
333   for (i = 0; i < 4; i++)
334      ctx->vertices[i][0][3] = 1; /*v.w*/
335
336   ctx->upload = u_upload_create(pipe, 65536, 4, PIPE_BIND_VERTEX_BUFFER);
337
338   return &ctx->base;
339}
340
341void util_blitter_destroy(struct blitter_context *blitter)
342{
343   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
344   struct pipe_context *pipe = blitter->pipe;
345   int i, j, f;
346
347   for (i = 0; i <= PIPE_MASK_RGBA; i++) {
348      pipe->delete_blend_state(pipe, ctx->blend[i]);
349   }
350   for (i = 0; i < Elements(ctx->blend_clear); i++) {
351      if (ctx->blend_clear[i])
352         pipe->delete_blend_state(pipe, ctx->blend_clear[i]);
353   }
354   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
355   pipe->delete_depth_stencil_alpha_state(pipe,
356                                          ctx->dsa_write_depth_keep_stencil);
357   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
358   pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
359
360   pipe->delete_rasterizer_state(pipe, ctx->rs_state);
361   pipe->delete_rasterizer_state(pipe, ctx->rs_state_scissor);
362   if (ctx->rs_discard_state)
363      pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
364   pipe->delete_vs_state(pipe, ctx->vs);
365   if (ctx->vs_pos_only)
366      pipe->delete_vs_state(pipe, ctx->vs_pos_only);
367   if (ctx->vs_layered)
368      pipe->delete_vs_state(pipe, ctx->vs_layered);
369   pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
370   for (i = 0; i < 4; i++) {
371      if (ctx->velem_state_readbuf[i]) {
372         pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf[i]);
373      }
374   }
375
376   for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
377      if (ctx->fs_texfetch_col[i])
378         ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
379      if (ctx->fs_texfetch_depth[i])
380         ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
381      if (ctx->fs_texfetch_depthstencil[i])
382         ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]);
383      if (ctx->fs_texfetch_stencil[i])
384         ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]);
385
386      if (ctx->fs_texfetch_col_msaa[i])
387         ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[i]);
388      if (ctx->fs_texfetch_depth_msaa[i])
389         ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]);
390      if (ctx->fs_texfetch_depthstencil_msaa[i])
391         ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil_msaa[i]);
392      if (ctx->fs_texfetch_stencil_msaa[i])
393         ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]);
394
395      for (j = 0; j< Elements(ctx->fs_resolve[i]); j++)
396         for (f = 0; f < 2; f++)
397            if (ctx->fs_resolve[i][j][f])
398               ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]);
399
400      for (j = 0; j< Elements(ctx->fs_resolve_sint[i]); j++)
401         for (f = 0; f < 2; f++)
402            if (ctx->fs_resolve_sint[i][j][f])
403               ctx->delete_fs_state(pipe, ctx->fs_resolve_sint[i][j][f]);
404
405      for (j = 0; j< Elements(ctx->fs_resolve_uint[i]); j++)
406         for (f = 0; f < 2; f++)
407            if (ctx->fs_resolve_uint[i][j][f])
408               ctx->delete_fs_state(pipe, ctx->fs_resolve_uint[i][j][f]);
409   }
410
411   ctx->delete_fs_state(pipe, ctx->fs_empty);
412   ctx->delete_fs_state(pipe, ctx->fs_write_one_cbuf);
413   ctx->delete_fs_state(pipe, ctx->fs_write_all_cbufs);
414
415   pipe->delete_sampler_state(pipe, ctx->sampler_state_rect_linear);
416   pipe->delete_sampler_state(pipe, ctx->sampler_state_rect);
417   pipe->delete_sampler_state(pipe, ctx->sampler_state_linear);
418   pipe->delete_sampler_state(pipe, ctx->sampler_state);
419   u_upload_destroy(ctx->upload);
420   FREE(ctx);
421}
422
423void util_blitter_set_texture_multisample(struct blitter_context *blitter,
424                                          boolean supported)
425{
426   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
427
428   ctx->has_texture_multisample = supported;
429}
430
431static void blitter_set_running_flag(struct blitter_context_priv *ctx)
432{
433   if (ctx->base.running) {
434      _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
435                    __LINE__);
436   }
437   ctx->base.running = TRUE;
438}
439
440static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
441{
442   if (!ctx->base.running) {
443      _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
444                    __LINE__);
445   }
446   ctx->base.running = FALSE;
447}
448
449static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
450{
451   assert(ctx->base.saved_velem_state != INVALID_PTR);
452   assert(ctx->base.saved_vs != INVALID_PTR);
453   assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
454   assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0);
455   assert(ctx->base.saved_rs_state != INVALID_PTR);
456}
457
458static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
459{
460   struct pipe_context *pipe = ctx->base.pipe;
461   unsigned i;
462
463   /* Vertex buffer. */
464   pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
465                            &ctx->base.saved_vertex_buffer);
466   pipe_resource_reference(&ctx->base.saved_vertex_buffer.buffer, NULL);
467
468   /* Vertex elements. */
469   pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
470   ctx->base.saved_velem_state = INVALID_PTR;
471
472   /* Vertex shader. */
473   pipe->bind_vs_state(pipe, ctx->base.saved_vs);
474   ctx->base.saved_vs = INVALID_PTR;
475
476   /* Geometry shader. */
477   if (ctx->has_geometry_shader) {
478      pipe->bind_gs_state(pipe, ctx->base.saved_gs);
479      ctx->base.saved_gs = INVALID_PTR;
480   }
481
482   /* Stream outputs. */
483   if (ctx->has_stream_out) {
484      unsigned offsets[PIPE_MAX_SO_BUFFERS];
485      for (i = 0; i < ctx->base.saved_num_so_targets; i++)
486         offsets[i] = (unsigned)-1;
487      pipe->set_stream_output_targets(pipe,
488                                      ctx->base.saved_num_so_targets,
489                                      ctx->base.saved_so_targets, offsets);
490
491      for (i = 0; i < ctx->base.saved_num_so_targets; i++)
492         pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
493
494      ctx->base.saved_num_so_targets = ~0;
495   }
496
497   /* Rasterizer. */
498   pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
499   ctx->base.saved_rs_state = INVALID_PTR;
500}
501
502static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
503{
504   assert(ctx->base.saved_fs != INVALID_PTR);
505   assert(ctx->base.saved_dsa_state != INVALID_PTR);
506   assert(ctx->base.saved_blend_state != INVALID_PTR);
507}
508
509static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
510{
511   struct pipe_context *pipe = ctx->base.pipe;
512
513   /* Fragment shader. */
514   ctx->bind_fs_state(pipe, ctx->base.saved_fs);
515   ctx->base.saved_fs = INVALID_PTR;
516
517   /* Depth, stencil, alpha. */
518   pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
519   ctx->base.saved_dsa_state = INVALID_PTR;
520
521   /* Blend state. */
522   pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
523   ctx->base.saved_blend_state = INVALID_PTR;
524
525   /* Sample mask. */
526   if (ctx->base.is_sample_mask_saved) {
527      pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
528      ctx->base.is_sample_mask_saved = FALSE;
529   }
530
531   /* Miscellaneous states. */
532   /* XXX check whether these are saved and whether they need to be restored
533    * (depending on the operation) */
534   pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
535   pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
536}
537
538static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
539{
540   assert(ctx->base.saved_fb_state.nr_cbufs != ~0);
541}
542
543static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
544{
545   struct pipe_context *pipe = ctx->base.pipe;
546
547   if (ctx->base.saved_render_cond_query) {
548      pipe->render_condition(pipe, NULL, FALSE, 0);
549   }
550}
551
552static void blitter_restore_render_cond(struct blitter_context_priv *ctx)
553{
554   struct pipe_context *pipe = ctx->base.pipe;
555
556   if (ctx->base.saved_render_cond_query) {
557      pipe->render_condition(pipe, ctx->base.saved_render_cond_query,
558                             ctx->base.saved_render_cond_cond,
559                             ctx->base.saved_render_cond_mode);
560      ctx->base.saved_render_cond_query = NULL;
561   }
562}
563
564static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
565{
566   struct pipe_context *pipe = ctx->base.pipe;
567
568   pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
569   util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
570}
571
572static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
573{
574   assert(ctx->base.saved_num_sampler_states != ~0);
575   assert(ctx->base.saved_num_sampler_views != ~0);
576}
577
578static void blitter_restore_textures(struct blitter_context_priv *ctx)
579{
580   struct pipe_context *pipe = ctx->base.pipe;
581   unsigned i;
582
583   /* Fragment sampler states. */
584   pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
585                             ctx->base.saved_num_sampler_states,
586                             ctx->base.saved_sampler_states);
587
588   ctx->base.saved_num_sampler_states = ~0;
589
590   /* Fragment sampler views. */
591   pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
592                           ctx->base.saved_num_sampler_views,
593                           ctx->base.saved_sampler_views);
594
595   for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
596      pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
597
598   ctx->base.saved_num_sampler_views = ~0;
599}
600
601static void blitter_set_rectangle(struct blitter_context_priv *ctx,
602                                  int x1, int y1, int x2, int y2,
603                                  float depth)
604{
605   int i;
606
607   /* set vertex positions */
608   ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
609   ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
610
611   ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
612   ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
613
614   ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
615   ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
616
617   ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
618   ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
619
620   for (i = 0; i < 4; i++)
621      ctx->vertices[i][0][2] = depth; /*z*/
622
623   /* viewport */
624   ctx->viewport.scale[0] = 0.5f * ctx->dst_width;
625   ctx->viewport.scale[1] = 0.5f * ctx->dst_height;
626   ctx->viewport.scale[2] = 1.0f;
627   ctx->viewport.scale[3] = 1.0f;
628   ctx->viewport.translate[0] = 0.5f * ctx->dst_width;
629   ctx->viewport.translate[1] = 0.5f * ctx->dst_height;
630   ctx->viewport.translate[2] = 0.0f;
631   ctx->viewport.translate[3] = 0.0f;
632   ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &ctx->viewport);
633}
634
635static void blitter_set_clear_color(struct blitter_context_priv *ctx,
636                                    const union pipe_color_union *color)
637{
638   int i;
639
640   if (color) {
641      for (i = 0; i < 4; i++) {
642         uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1];
643         uiverts[0] = color->ui[0];
644         uiverts[1] = color->ui[1];
645         uiverts[2] = color->ui[2];
646         uiverts[3] = color->ui[3];
647      }
648   } else {
649      for (i = 0; i < 4; i++) {
650         ctx->vertices[i][1][0] = 0;
651         ctx->vertices[i][1][1] = 0;
652         ctx->vertices[i][1][2] = 0;
653         ctx->vertices[i][1][3] = 0;
654      }
655   }
656}
657
658static void get_texcoords(struct pipe_sampler_view *src,
659                          unsigned src_width0, unsigned src_height0,
660                          int x1, int y1, int x2, int y2,
661                          float out[4])
662{
663   struct pipe_resource *tex = src->texture;
664   unsigned level = src->u.tex.first_level;
665   boolean normalized = tex->target != PIPE_TEXTURE_RECT &&
666                        tex->nr_samples <= 1;
667
668   if (normalized) {
669      out[0] = x1 / (float)u_minify(src_width0,  level);
670      out[1] = y1 / (float)u_minify(src_height0, level);
671      out[2] = x2 / (float)u_minify(src_width0,  level);
672      out[3] = y2 / (float)u_minify(src_height0, level);
673   } else {
674      out[0] = (float) x1;
675      out[1] = (float) y1;
676      out[2] = (float) x2;
677      out[3] = (float) y2;
678   }
679}
680
681static void set_texcoords_in_vertices(const float coord[4],
682                                      float *out, unsigned stride)
683{
684   out[0] = coord[0]; /*t0.s*/
685   out[1] = coord[1]; /*t0.t*/
686   out += stride;
687   out[0] = coord[2]; /*t1.s*/
688   out[1] = coord[1]; /*t1.t*/
689   out += stride;
690   out[0] = coord[2]; /*t2.s*/
691   out[1] = coord[3]; /*t2.t*/
692   out += stride;
693   out[0] = coord[0]; /*t3.s*/
694   out[1] = coord[3]; /*t3.t*/
695}
696
697static void blitter_set_texcoords(struct blitter_context_priv *ctx,
698                                  struct pipe_sampler_view *src,
699                                  unsigned src_width0, unsigned src_height0,
700                                  float layer, unsigned sample,
701                                  int x1, int y1, int x2, int y2)
702{
703   unsigned i;
704   float coord[4];
705   float face_coord[4][2];
706
707   get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
708
709   if (src->texture->target == PIPE_TEXTURE_CUBE ||
710       src->texture->target == PIPE_TEXTURE_CUBE_ARRAY) {
711      set_texcoords_in_vertices(coord, &face_coord[0][0], 2);
712      util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
713                                        /* pointer, stride in floats */
714                                        &face_coord[0][0], 2,
715                                        &ctx->vertices[0][1][0], 8,
716                                        FALSE);
717   } else {
718      set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
719   }
720
721   /* Set the layer. */
722   switch (src->texture->target) {
723   case PIPE_TEXTURE_3D:
724      {
725         float r = layer / (float)u_minify(src->texture->depth0,
726                                           src->u.tex.first_level);
727         for (i = 0; i < 4; i++)
728            ctx->vertices[i][1][2] = r; /*r*/
729      }
730      break;
731
732   case PIPE_TEXTURE_1D_ARRAY:
733      for (i = 0; i < 4; i++)
734         ctx->vertices[i][1][1] = (float) layer; /*t*/
735      break;
736
737   case PIPE_TEXTURE_2D_ARRAY:
738      for (i = 0; i < 4; i++) {
739         ctx->vertices[i][1][2] = (float) layer;  /*r*/
740         ctx->vertices[i][1][3] = (float) sample; /*q*/
741      }
742      break;
743
744   case PIPE_TEXTURE_CUBE_ARRAY:
745      for (i = 0; i < 4; i++)
746         ctx->vertices[i][1][3] = (float) ((unsigned)layer / 6); /*w*/
747      break;
748
749   case PIPE_TEXTURE_2D:
750      for (i = 0; i < 4; i++) {
751         ctx->vertices[i][1][3] = (float) sample; /*r*/
752      }
753      break;
754
755   default:;
756   }
757}
758
759static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
760                                       unsigned width, unsigned height)
761{
762   ctx->dst_width = width;
763   ctx->dst_height = height;
764}
765
766static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
767                                         enum pipe_format format,
768                                         enum pipe_texture_target target,
769                                         unsigned src_nr_samples,
770                                         unsigned dst_nr_samples,
771                                         unsigned filter)
772{
773   struct pipe_context *pipe = ctx->base.pipe;
774   unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
775
776   assert(target < PIPE_MAX_TEXTURE_TYPES);
777
778   if (src_nr_samples > 1) {
779      void **shader;
780
781      if (dst_nr_samples <= 1) {
782         /* The destination has one sample, so we'll do color resolve. */
783         boolean is_uint, is_sint;
784         unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
785
786         is_uint = util_format_is_pure_uint(format);
787         is_sint = util_format_is_pure_sint(format);
788
789         assert(filter < 2);
790
791         if (is_uint)
792            shader = &ctx->fs_resolve_uint[target][index][filter];
793         else if (is_sint)
794            shader = &ctx->fs_resolve_sint[target][index][filter];
795         else
796            shader = &ctx->fs_resolve[target][index][filter];
797
798         if (!*shader) {
799            if (filter == PIPE_TEX_FILTER_LINEAR) {
800               *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
801                                                   src_nr_samples,
802                                                   is_uint, is_sint);
803            }
804            else {
805               *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
806                                                   src_nr_samples,
807                                                   is_uint, is_sint);
808            }
809         }
810      }
811      else {
812         /* The destination has multiple samples, we'll do
813          * an MSAA->MSAA copy.
814          */
815         shader = &ctx->fs_texfetch_col_msaa[target];
816
817         /* Create the fragment shader on-demand. */
818         if (!*shader) {
819            *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex);
820         }
821      }
822
823      return *shader;
824   } else {
825      void **shader = &ctx->fs_texfetch_col[target];
826
827      /* Create the fragment shader on-demand. */
828      if (!*shader) {
829         *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
830                                                 TGSI_INTERPOLATE_LINEAR);
831      }
832
833      return *shader;
834   }
835}
836
837static INLINE
838void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
839                                    enum pipe_texture_target target,
840                                    unsigned nr_samples)
841{
842   struct pipe_context *pipe = ctx->base.pipe;
843
844   assert(target < PIPE_MAX_TEXTURE_TYPES);
845
846   if (nr_samples > 1) {
847      void **shader = &ctx->fs_texfetch_depth_msaa[target];
848
849      /* Create the fragment shader on-demand. */
850      if (!*shader) {
851         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target,
852                                                       nr_samples);
853
854         *shader =
855            util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
856      }
857
858      return *shader;
859   } else {
860      void **shader = &ctx->fs_texfetch_depth[target];
861
862      /* Create the fragment shader on-demand. */
863      if (!*shader) {
864         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
865
866         *shader =
867            util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
868                                                     TGSI_INTERPOLATE_LINEAR);
869      }
870
871      return *shader;
872   }
873}
874
875static INLINE
876void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
877                                           enum pipe_texture_target target,
878                                           unsigned nr_samples)
879{
880   struct pipe_context *pipe = ctx->base.pipe;
881
882   assert(target < PIPE_MAX_TEXTURE_TYPES);
883
884   if (nr_samples > 1) {
885      void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
886
887      /* Create the fragment shader on-demand. */
888      if (!*shader) {
889         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target,
890                                                       nr_samples);
891
892         *shader =
893            util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
894      }
895
896      return *shader;
897   } else {
898      void **shader = &ctx->fs_texfetch_depthstencil[target];
899
900      /* Create the fragment shader on-demand. */
901      if (!*shader) {
902         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
903
904         *shader =
905            util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
906                                                     TGSI_INTERPOLATE_LINEAR);
907      }
908
909      return *shader;
910   }
911}
912
913static INLINE
914void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
915                                      enum pipe_texture_target target,
916                                      unsigned nr_samples)
917{
918   struct pipe_context *pipe = ctx->base.pipe;
919
920   assert(target < PIPE_MAX_TEXTURE_TYPES);
921
922   if (nr_samples > 1) {
923      void **shader = &ctx->fs_texfetch_stencil_msaa[target];
924
925      /* Create the fragment shader on-demand. */
926      if (!*shader) {
927         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target,
928                                                       nr_samples);
929
930         *shader =
931            util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
932      }
933
934      return *shader;
935   } else {
936      void **shader = &ctx->fs_texfetch_stencil[target];
937
938      /* Create the fragment shader on-demand. */
939      if (!*shader) {
940         unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
941
942         *shader =
943            util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
944                                                       TGSI_INTERPOLATE_LINEAR);
945      }
946
947      return *shader;
948   }
949}
950
951void util_blitter_cache_all_shaders(struct blitter_context *blitter)
952{
953   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
954   struct pipe_screen *screen = blitter->pipe->screen;
955   unsigned samples, j, f, target, max_samples;
956   boolean has_arraytex, has_cubearraytex;
957
958   max_samples = ctx->has_texture_multisample ? 2 : 1;
959   has_arraytex = screen->get_param(screen,
960                                    PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
961   has_cubearraytex = screen->get_param(screen,
962                                    PIPE_CAP_CUBE_MAP_ARRAY) != 0;
963
964   /* It only matters if i <= 1 or > 1. */
965   for (samples = 1; samples <= max_samples; samples++) {
966      for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
967         if (!has_arraytex &&
968             (target == PIPE_TEXTURE_1D_ARRAY ||
969              target == PIPE_TEXTURE_2D_ARRAY)) {
970            continue;
971         }
972         if (!has_cubearraytex &&
973             (target == PIPE_TEXTURE_CUBE_ARRAY))
974            continue;
975
976	 if (samples > 1 &&
977	     (target != PIPE_TEXTURE_2D &&
978	      target != PIPE_TEXTURE_2D_ARRAY))
979	    continue;
980
981         /* If samples == 1, the shaders read one texel. If samples >= 1,
982          * they read one sample.
983          */
984         blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
985                                     samples, samples, 0);
986         blitter_get_fs_texfetch_depth(ctx, target, samples);
987         if (ctx->has_stencil_export) {
988            blitter_get_fs_texfetch_depthstencil(ctx, target, samples);
989            blitter_get_fs_texfetch_stencil(ctx, target, samples);
990         }
991
992         if (samples == 1)
993            continue;
994
995         /* MSAA resolve shaders. */
996         for (j = 2; j < 32; j++) {
997            if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
998                                             target, j,
999                                             PIPE_BIND_SAMPLER_VIEW)) {
1000               continue;
1001            }
1002
1003            for (f = 0; f < 2; f++) {
1004               blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
1005                                           j, 1, f);
1006               blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, target,
1007                                           j, 1, f);
1008               blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, target,
1009                                           j, 1, f);
1010            }
1011         }
1012      }
1013   }
1014}
1015
1016static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
1017                                               boolean scissor,
1018                                               boolean vs_layered)
1019{
1020   struct pipe_context *pipe = ctx->base.pipe;
1021
1022   pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
1023                                             : ctx->rs_state);
1024   pipe->bind_vs_state(pipe, vs_layered ? ctx->vs_layered : ctx->vs);
1025   if (ctx->has_geometry_shader)
1026      pipe->bind_gs_state(pipe, NULL);
1027   if (ctx->has_stream_out)
1028      pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
1029}
1030
1031static void blitter_draw(struct blitter_context_priv *ctx,
1032                         int x1, int y1, int x2, int y2, float depth,
1033                         unsigned num_instances)
1034{
1035   struct pipe_context *pipe = ctx->base.pipe;
1036   struct pipe_vertex_buffer vb = {0};
1037
1038   blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
1039
1040   vb.stride = 8 * sizeof(float);
1041
1042   u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices,
1043                 &vb.buffer_offset, &vb.buffer);
1044   u_upload_unmap(ctx->upload);
1045
1046   pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
1047   util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
1048                              0, num_instances);
1049   pipe_resource_reference(&vb.buffer, NULL);
1050}
1051
1052void util_blitter_draw_rectangle(struct blitter_context *blitter,
1053                                 int x1, int y1, int x2, int y2, float depth,
1054                                 enum blitter_attrib_type type,
1055                                 const union pipe_color_union *attrib)
1056{
1057   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1058
1059   switch (type) {
1060      case UTIL_BLITTER_ATTRIB_COLOR:
1061         blitter_set_clear_color(ctx, attrib);
1062         break;
1063
1064      case UTIL_BLITTER_ATTRIB_TEXCOORD:
1065         set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
1066         break;
1067
1068      default:;
1069   }
1070
1071   blitter_draw(ctx, x1, y1, x2, y2, depth, 1);
1072}
1073
1074static void *get_clear_blend_state(struct blitter_context_priv *ctx,
1075                                   unsigned clear_buffers)
1076{
1077   struct pipe_context *pipe = ctx->base.pipe;
1078   int index;
1079
1080   clear_buffers &= PIPE_CLEAR_COLOR;
1081
1082   /* Return an existing blend state. */
1083   if (!clear_buffers)
1084      return ctx->blend[0];
1085
1086   index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
1087
1088   if (ctx->blend_clear[index])
1089      return ctx->blend_clear[index];
1090
1091   /* Create a new one. */
1092   {
1093      struct pipe_blend_state blend = {0};
1094      unsigned i;
1095
1096      blend.independent_blend_enable = 1;
1097
1098      for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
1099         if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
1100            blend.rt[i].colormask = PIPE_MASK_RGBA;
1101         }
1102      }
1103
1104      ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
1105   }
1106   return ctx->blend_clear[index];
1107}
1108
1109static void util_blitter_clear_custom(struct blitter_context *blitter,
1110                                      unsigned width, unsigned height,
1111                                      unsigned num_layers,
1112                                      unsigned clear_buffers,
1113                                      const union pipe_color_union *color,
1114                                      double depth, unsigned stencil,
1115                                      void *custom_blend, void *custom_dsa)
1116{
1117   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1118   struct pipe_context *pipe = ctx->base.pipe;
1119   struct pipe_stencil_ref sr = { { 0 } };
1120
1121   assert(ctx->vs_layered || num_layers <= 1);
1122
1123   blitter_set_running_flag(ctx);
1124   blitter_check_saved_vertex_states(ctx);
1125   blitter_check_saved_fragment_states(ctx);
1126   blitter_disable_render_cond(ctx);
1127
1128   /* bind states */
1129   if (custom_blend) {
1130      pipe->bind_blend_state(pipe, custom_blend);
1131   } else {
1132      pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
1133   }
1134
1135   if (custom_dsa) {
1136      pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
1137   } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1138      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1139   } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
1140      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1141   } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
1142      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1143   } else {
1144      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1145   }
1146
1147   sr.ref_value[0] = stencil & 0xff;
1148   pipe->set_stencil_ref(pipe, &sr);
1149
1150   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1151   ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs);
1152   pipe->set_sample_mask(pipe, ~0);
1153
1154   blitter_set_dst_dimensions(ctx, width, height);
1155
1156   if (num_layers > 1 && ctx->vs_layered) {
1157      blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
1158      blitter_set_clear_color(ctx, color);
1159      blitter_draw(ctx, 0, 0, width, height, depth, num_layers);
1160   }
1161   else {
1162      blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1163      blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
1164                              UTIL_BLITTER_ATTRIB_COLOR, color);
1165   }
1166
1167   blitter_restore_vertex_states(ctx);
1168   blitter_restore_fragment_states(ctx);
1169   blitter_restore_render_cond(ctx);
1170   blitter_unset_running_flag(ctx);
1171}
1172
1173void util_blitter_clear(struct blitter_context *blitter,
1174                        unsigned width, unsigned height, unsigned num_layers,
1175                        unsigned clear_buffers,
1176                        const union pipe_color_union *color,
1177                        double depth, unsigned stencil)
1178{
1179   util_blitter_clear_custom(blitter, width, height, num_layers,
1180                             clear_buffers, color, depth, stencil,
1181                             NULL, NULL);
1182}
1183
1184void util_blitter_custom_clear_depth(struct blitter_context *blitter,
1185                                     unsigned width, unsigned height,
1186                                     double depth, void *custom_dsa)
1187{
1188    static const union pipe_color_union color;
1189    util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
1190                              NULL, custom_dsa);
1191}
1192
1193void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
1194                                      struct pipe_resource *dst,
1195                                      unsigned dstlevel,
1196                                      unsigned dstz)
1197{
1198    memset(dst_templ, 0, sizeof(*dst_templ));
1199    dst_templ->format = util_format_linear(dst->format);
1200    dst_templ->u.tex.level = dstlevel;
1201    dst_templ->u.tex.first_layer = dstz;
1202    dst_templ->u.tex.last_layer = dstz;
1203}
1204
1205static struct pipe_surface *
1206util_blitter_get_next_surface_layer(struct pipe_context *pipe,
1207                                    struct pipe_surface *surf)
1208{
1209   struct pipe_surface dst_templ;
1210
1211   memset(&dst_templ, 0, sizeof(dst_templ));
1212   dst_templ.format = surf->format;
1213   dst_templ.u.tex.level = surf->u.tex.level;
1214   dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
1215   dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
1216
1217   return pipe->create_surface(pipe, surf->texture, &dst_templ);
1218}
1219
1220void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
1221                                      struct pipe_resource *src,
1222                                      unsigned srclevel)
1223{
1224    memset(src_templ, 0, sizeof(*src_templ));
1225    src_templ->format = util_format_linear(src->format);
1226    src_templ->u.tex.first_level = srclevel;
1227    src_templ->u.tex.last_level = srclevel;
1228    src_templ->u.tex.first_layer = 0;
1229    src_templ->u.tex.last_layer =
1230        src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1231                                       : src->array_size - 1;
1232    src_templ->swizzle_r = PIPE_SWIZZLE_RED;
1233    src_templ->swizzle_g = PIPE_SWIZZLE_GREEN;
1234    src_templ->swizzle_b = PIPE_SWIZZLE_BLUE;
1235    src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA;
1236}
1237
1238static boolean is_blit_generic_supported(struct blitter_context *blitter,
1239                                         const struct pipe_resource *dst,
1240                                         enum pipe_format dst_format,
1241                                         const struct pipe_resource *src,
1242                                         enum pipe_format src_format,
1243                                         unsigned mask)
1244{
1245   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1246   struct pipe_screen *screen = ctx->base.pipe->screen;
1247
1248   if (dst) {
1249      unsigned bind;
1250      const struct util_format_description *desc =
1251            util_format_description(dst_format);
1252      boolean dst_has_stencil = util_format_has_stencil(desc);
1253
1254      /* Stencil export must be supported for stencil copy. */
1255      if ((mask & PIPE_MASK_S) && dst_has_stencil &&
1256          !ctx->has_stencil_export) {
1257         return FALSE;
1258      }
1259
1260      if (dst_has_stencil || util_format_has_depth(desc))
1261         bind = PIPE_BIND_DEPTH_STENCIL;
1262      else
1263         bind = PIPE_BIND_RENDER_TARGET;
1264
1265      if (!screen->is_format_supported(screen, dst_format, dst->target,
1266                                       dst->nr_samples, bind)) {
1267         return FALSE;
1268      }
1269   }
1270
1271   if (src) {
1272      if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
1273         return FALSE;
1274      }
1275
1276      if (!screen->is_format_supported(screen, src_format, src->target,
1277                                 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1278         return FALSE;
1279      }
1280
1281      /* Check stencil sampler support for stencil copy. */
1282      if (mask & PIPE_MASK_S) {
1283         if (util_format_has_stencil(util_format_description(src_format))) {
1284            enum pipe_format stencil_format =
1285               util_format_stencil_only(src_format);
1286            assert(stencil_format != PIPE_FORMAT_NONE);
1287
1288            if (stencil_format != src_format &&
1289                !screen->is_format_supported(screen, stencil_format,
1290                                             src->target, src->nr_samples,
1291                                             PIPE_BIND_SAMPLER_VIEW)) {
1292               return FALSE;
1293            }
1294         }
1295      }
1296   }
1297
1298   return TRUE;
1299}
1300
1301boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
1302                                       const struct pipe_resource *dst,
1303                                       const struct pipe_resource *src)
1304{
1305   return is_blit_generic_supported(blitter, dst, dst->format,
1306                                    src, src->format, PIPE_MASK_RGBAZS);
1307}
1308
1309boolean util_blitter_is_blit_supported(struct blitter_context *blitter,
1310				       const struct pipe_blit_info *info)
1311{
1312   return is_blit_generic_supported(blitter,
1313                                    info->dst.resource, info->dst.format,
1314                                    info->src.resource, info->src.format,
1315                                    info->mask);
1316}
1317
1318void util_blitter_copy_texture(struct blitter_context *blitter,
1319                               struct pipe_resource *dst,
1320                               unsigned dst_level,
1321                               unsigned dstx, unsigned dsty, unsigned dstz,
1322                               struct pipe_resource *src,
1323                               unsigned src_level,
1324                               const struct pipe_box *srcbox)
1325{
1326   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1327   struct pipe_context *pipe = ctx->base.pipe;
1328   struct pipe_surface *dst_view, dst_templ;
1329   struct pipe_sampler_view src_templ, *src_view;
1330   struct pipe_box dstbox;
1331
1332   assert(dst && src);
1333   assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1334
1335   u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
1336            abs(srcbox->depth), &dstbox);
1337
1338   /* Initialize the surface. */
1339   util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
1340   dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1341
1342   /* Initialize the sampler view. */
1343   util_blitter_default_src_texture(&src_templ, src, src_level);
1344   src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1345
1346   /* Copy. */
1347   util_blitter_blit_generic(blitter, dst_view, &dstbox,
1348                             src_view, srcbox, src->width0, src->height0,
1349                             PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
1350
1351   pipe_surface_reference(&dst_view, NULL);
1352   pipe_sampler_view_reference(&src_view, NULL);
1353}
1354
1355void util_blitter_blit_generic(struct blitter_context *blitter,
1356                               struct pipe_surface *dst,
1357                               const struct pipe_box *dstbox,
1358                               struct pipe_sampler_view *src,
1359                               const struct pipe_box *srcbox,
1360                               unsigned src_width0, unsigned src_height0,
1361                               unsigned mask, unsigned filter,
1362                               const struct pipe_scissor_state *scissor)
1363{
1364   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1365   struct pipe_context *pipe = ctx->base.pipe;
1366   struct pipe_framebuffer_state fb_state;
1367   enum pipe_texture_target src_target = src->texture->target;
1368   unsigned src_samples = src->texture->nr_samples;
1369   unsigned dst_samples = dst->texture->nr_samples;
1370   boolean has_depth, has_stencil, has_color;
1371   boolean blit_stencil, blit_depth, blit_color;
1372   void *sampler_state;
1373   const struct util_format_description *src_desc =
1374         util_format_description(src->format);
1375   const struct util_format_description *dst_desc =
1376         util_format_description(dst->format);
1377
1378   has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
1379               dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1380   has_depth = util_format_has_depth(src_desc) &&
1381               util_format_has_depth(dst_desc);
1382   has_stencil = util_format_has_stencil(src_desc) &&
1383                 util_format_has_stencil(dst_desc);
1384
1385   blit_color = has_color && (mask & PIPE_MASK_RGBA);
1386   blit_depth = has_depth && (mask & PIPE_MASK_Z);
1387   blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
1388                  ctx->has_stencil_export;
1389
1390   if (!blit_stencil && !blit_depth && !blit_color) {
1391      return;
1392   }
1393
1394   if (blit_stencil ||
1395       (dstbox->width == abs(srcbox->width) &&
1396        dstbox->height == abs(srcbox->height))) {
1397      filter = PIPE_TEX_FILTER_NEAREST;
1398   }
1399
1400   /* Check whether the states are properly saved. */
1401   blitter_set_running_flag(ctx);
1402   blitter_check_saved_vertex_states(ctx);
1403   blitter_check_saved_fragment_states(ctx);
1404   blitter_check_saved_textures(ctx);
1405   blitter_check_saved_fb_state(ctx);
1406   blitter_disable_render_cond(ctx);
1407
1408   /* Initialize framebuffer state. */
1409   fb_state.width = dst->width;
1410   fb_state.height = dst->height;
1411   fb_state.nr_cbufs = blit_depth || blit_stencil ? 0 : 1;
1412   fb_state.cbufs[0] = NULL;
1413   fb_state.zsbuf = NULL;
1414
1415   if (blit_depth || blit_stencil) {
1416      pipe->bind_blend_state(pipe, ctx->blend[0]);
1417
1418      if (blit_depth && blit_stencil) {
1419         pipe->bind_depth_stencil_alpha_state(pipe,
1420                                              ctx->dsa_write_depth_stencil);
1421         ctx->bind_fs_state(pipe,
1422               blitter_get_fs_texfetch_depthstencil(ctx, src_target,
1423                                                    src_samples));
1424      } else if (blit_depth) {
1425         pipe->bind_depth_stencil_alpha_state(pipe,
1426                                              ctx->dsa_write_depth_keep_stencil);
1427         ctx->bind_fs_state(pipe,
1428               blitter_get_fs_texfetch_depth(ctx, src_target,
1429                                             src_samples));
1430      } else { /* is_stencil */
1431         pipe->bind_depth_stencil_alpha_state(pipe,
1432                                              ctx->dsa_keep_depth_write_stencil);
1433         ctx->bind_fs_state(pipe,
1434               blitter_get_fs_texfetch_stencil(ctx, src_target,
1435                                               src_samples));
1436      }
1437
1438   } else {
1439      pipe->bind_blend_state(pipe, ctx->blend[mask & PIPE_MASK_RGBA]);
1440      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1441      ctx->bind_fs_state(pipe,
1442            blitter_get_fs_texfetch_col(ctx, src->format, src_target,
1443                                        src_samples, dst_samples, filter));
1444   }
1445
1446   /* Set the linear filter only for scaled color non-MSAA blits. */
1447   if (filter == PIPE_TEX_FILTER_LINEAR) {
1448      if (src_target == PIPE_TEXTURE_RECT) {
1449         sampler_state = ctx->sampler_state_rect_linear;
1450      } else {
1451         sampler_state = ctx->sampler_state_linear;
1452      }
1453   } else {
1454      if (src_target == PIPE_TEXTURE_RECT) {
1455         sampler_state = ctx->sampler_state_rect;
1456      } else {
1457         sampler_state = ctx->sampler_state;
1458      }
1459   }
1460
1461   /* Set samplers. */
1462   if (blit_depth && blit_stencil) {
1463      /* Setup two samplers, one for depth and the other one for stencil. */
1464      struct pipe_sampler_view templ;
1465      struct pipe_sampler_view *views[2];
1466      void *samplers[2] = {sampler_state, sampler_state};
1467
1468      templ = *src;
1469      templ.format = util_format_stencil_only(templ.format);
1470      assert(templ.format != PIPE_FORMAT_NONE);
1471
1472      views[0] = src;
1473      views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
1474
1475      pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
1476      pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
1477
1478      pipe_sampler_view_reference(&views[1], NULL);
1479   } else if (blit_stencil) {
1480      /* Set a stencil-only sampler view for it not to sample depth instead. */
1481      struct pipe_sampler_view templ;
1482      struct pipe_sampler_view *view;
1483
1484      templ = *src;
1485      templ.format = util_format_stencil_only(templ.format);
1486      assert(templ.format != PIPE_FORMAT_NONE);
1487
1488      view = pipe->create_sampler_view(pipe, src->texture, &templ);
1489
1490      pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
1491      pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1492                                0, 1, &sampler_state);
1493
1494      pipe_sampler_view_reference(&view, NULL);
1495   } else {
1496      pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
1497      pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1498                                0, 1, &sampler_state);
1499   }
1500
1501   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1502   if (scissor) {
1503      pipe->set_scissor_states(pipe, 0, 1, scissor);
1504   }
1505
1506   blitter_set_common_draw_rect_state(ctx, scissor != NULL, FALSE);
1507   blitter_set_dst_dimensions(ctx, dst->width, dst->height);
1508
1509   if ((src_target == PIPE_TEXTURE_1D ||
1510        src_target == PIPE_TEXTURE_2D ||
1511        src_target == PIPE_TEXTURE_RECT) &&
1512       src_samples <= 1) {
1513      /* Draw the quad with the draw_rectangle callback. */
1514
1515      /* Set texture coordinates. - use a pipe color union
1516       * for interface purposes.
1517       * XXX pipe_color_union is a wrong name since we use that to set
1518       * texture coordinates too.
1519       */
1520      union pipe_color_union coord;
1521      get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1522                    srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f);
1523
1524      /* Set framebuffer state. */
1525      if (blit_depth || blit_stencil) {
1526         fb_state.zsbuf = dst;
1527      } else {
1528         fb_state.cbufs[0] = dst;
1529      }
1530      pipe->set_framebuffer_state(pipe, &fb_state);
1531
1532      /* Draw. */
1533      pipe->set_sample_mask(pipe, ~0);
1534      blitter->draw_rectangle(blitter, dstbox->x, dstbox->y,
1535                              dstbox->x + dstbox->width,
1536                              dstbox->y + dstbox->height, 0,
1537                              UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1538   } else {
1539      /* Draw the quad with the generic codepath. */
1540      int dst_z;
1541      for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
1542         struct pipe_surface *old;
1543         float dst2src_scale = srcbox->depth / (float)dstbox->depth;
1544
1545         /* Scale Z properly if the blit is scaled.
1546          *
1547          * When downscaling, we want the coordinates centered, so that
1548          * mipmapping works for 3D textures. For example, when generating
1549          * a 4x4x4 level, this wouldn't average the pixels:
1550          *
1551          *   src Z:  0 1 2 3 4 5 6 7
1552          *   dst Z:  0   1   2   3
1553          *
1554          * Because the pixels are not centered below the pixels of the higher
1555          * level. Therefore, we want this:
1556          *   src Z:  0 1 2 3 4 5 6 7
1557          *   dst Z:   0   1   2   3
1558          *
1559          * dst_offset defines the offset needed for centering the pixels and
1560          * it works with any scaling (not just 2x).
1561          */
1562         float dst_offset = ((srcbox->depth - 1) -
1563                             (dstbox->depth - 1) * dst2src_scale) * 0.5;
1564         float src_z = (dst_z + dst_offset) * dst2src_scale;
1565
1566         /* Set framebuffer state. */
1567         if (blit_depth || blit_stencil) {
1568            fb_state.zsbuf = dst;
1569         } else {
1570            fb_state.cbufs[0] = dst;
1571         }
1572         pipe->set_framebuffer_state(pipe, &fb_state);
1573
1574         /* See if we need to blit a multisample or singlesample buffer. */
1575         if (src_samples == dst_samples && dst_samples > 1) {
1576            /* MSAA copy. */
1577            unsigned i, max_sample = dst_samples - 1;
1578
1579            for (i = 0; i <= max_sample; i++) {
1580               pipe->set_sample_mask(pipe, 1 << i);
1581               blitter_set_texcoords(ctx, src, src_width0, src_height0,
1582                                     srcbox->z + src_z,
1583                                     i, srcbox->x, srcbox->y,
1584                                     srcbox->x + srcbox->width,
1585                                     srcbox->y + srcbox->height);
1586               blitter_draw(ctx, dstbox->x, dstbox->y,
1587                            dstbox->x + dstbox->width,
1588                            dstbox->y + dstbox->height, 0, 1);
1589            }
1590         } else {
1591            /* Normal copy, MSAA upsampling, or MSAA resolve. */
1592            pipe->set_sample_mask(pipe, ~0);
1593            blitter_set_texcoords(ctx, src, src_width0, src_height0,
1594                                  srcbox->z + src_z, 0,
1595                                  srcbox->x, srcbox->y,
1596                                  srcbox->x + srcbox->width,
1597                                  srcbox->y + srcbox->height);
1598            blitter_draw(ctx, dstbox->x, dstbox->y,
1599                         dstbox->x + dstbox->width,
1600                         dstbox->y + dstbox->height, 0, 1);
1601         }
1602
1603         /* Get the next surface or (if this is the last iteration)
1604          * just unreference the last one. */
1605         old = dst;
1606         if (dst_z < dstbox->depth-1) {
1607            dst = ctx->base.get_next_surface_layer(ctx->base.pipe, dst);
1608         }
1609         if (dst_z) {
1610            pipe_surface_reference(&old, NULL);
1611         }
1612      }
1613   }
1614
1615   blitter_restore_vertex_states(ctx);
1616   blitter_restore_fragment_states(ctx);
1617   blitter_restore_textures(ctx);
1618   blitter_restore_fb_state(ctx);
1619   if (scissor) {
1620      pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
1621   }
1622   blitter_restore_render_cond(ctx);
1623   blitter_unset_running_flag(ctx);
1624}
1625
1626void
1627util_blitter_blit(struct blitter_context *blitter,
1628		  const struct pipe_blit_info *info)
1629{
1630   struct pipe_resource *dst = info->dst.resource;
1631   struct pipe_resource *src = info->src.resource;
1632   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1633   struct pipe_context *pipe = ctx->base.pipe;
1634   struct pipe_surface *dst_view, dst_templ;
1635   struct pipe_sampler_view src_templ, *src_view;
1636
1637   /* Initialize the surface. */
1638   util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
1639                                    info->dst.box.z);
1640   dst_templ.format = info->dst.format;
1641   dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1642
1643   /* Initialize the sampler view. */
1644   util_blitter_default_src_texture(&src_templ, src, info->src.level);
1645   src_templ.format = info->src.format;
1646   src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1647
1648   /* Copy. */
1649   util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
1650                             src_view, &info->src.box, src->width0, src->height0,
1651                             info->mask, info->filter,
1652                             info->scissor_enable ? &info->scissor : NULL);
1653
1654   pipe_surface_reference(&dst_view, NULL);
1655   pipe_sampler_view_reference(&src_view, NULL);
1656}
1657
1658/* Clear a region of a color surface to a constant value. */
1659void util_blitter_clear_render_target(struct blitter_context *blitter,
1660                                      struct pipe_surface *dstsurf,
1661                                      const union pipe_color_union *color,
1662                                      unsigned dstx, unsigned dsty,
1663                                      unsigned width, unsigned height)
1664{
1665   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1666   struct pipe_context *pipe = ctx->base.pipe;
1667   struct pipe_framebuffer_state fb_state;
1668
1669   assert(dstsurf->texture);
1670   if (!dstsurf->texture)
1671      return;
1672
1673   /* check the saved state */
1674   blitter_set_running_flag(ctx);
1675   blitter_check_saved_vertex_states(ctx);
1676   blitter_check_saved_fragment_states(ctx);
1677   blitter_check_saved_fb_state(ctx);
1678   blitter_disable_render_cond(ctx);
1679
1680   /* bind states */
1681   pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA]);
1682   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1683   ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
1684   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1685
1686   /* set a framebuffer state */
1687   fb_state.width = dstsurf->width;
1688   fb_state.height = dstsurf->height;
1689   fb_state.nr_cbufs = 1;
1690   fb_state.cbufs[0] = dstsurf;
1691   fb_state.zsbuf = 0;
1692   pipe->set_framebuffer_state(pipe, &fb_state);
1693   pipe->set_sample_mask(pipe, ~0);
1694
1695   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1696   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1697   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1698                           UTIL_BLITTER_ATTRIB_COLOR, color);
1699
1700   blitter_restore_vertex_states(ctx);
1701   blitter_restore_fragment_states(ctx);
1702   blitter_restore_fb_state(ctx);
1703   blitter_restore_render_cond(ctx);
1704   blitter_unset_running_flag(ctx);
1705}
1706
1707/* Clear a region of a depth stencil surface. */
1708void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
1709                                      struct pipe_surface *dstsurf,
1710                                      unsigned clear_flags,
1711                                      double depth,
1712                                      unsigned stencil,
1713                                      unsigned dstx, unsigned dsty,
1714                                      unsigned width, unsigned height)
1715{
1716   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1717   struct pipe_context *pipe = ctx->base.pipe;
1718   struct pipe_framebuffer_state fb_state;
1719   struct pipe_stencil_ref sr = { { 0 } };
1720
1721   assert(dstsurf->texture);
1722   if (!dstsurf->texture)
1723      return;
1724
1725   /* check the saved state */
1726   blitter_set_running_flag(ctx);
1727   blitter_check_saved_vertex_states(ctx);
1728   blitter_check_saved_fragment_states(ctx);
1729   blitter_check_saved_fb_state(ctx);
1730   blitter_disable_render_cond(ctx);
1731
1732   /* bind states */
1733   pipe->bind_blend_state(pipe, ctx->blend[0]);
1734   if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1735      sr.ref_value[0] = stencil & 0xff;
1736      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1737      pipe->set_stencil_ref(pipe, &sr);
1738   }
1739   else if (clear_flags & PIPE_CLEAR_DEPTH) {
1740      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1741   }
1742   else if (clear_flags & PIPE_CLEAR_STENCIL) {
1743      sr.ref_value[0] = stencil & 0xff;
1744      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1745      pipe->set_stencil_ref(pipe, &sr);
1746   }
1747   else
1748      /* hmm that should be illegal probably, or make it a no-op somewhere */
1749      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1750
1751   ctx->bind_fs_state(pipe, ctx->fs_empty);
1752   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1753
1754   /* set a framebuffer state */
1755   fb_state.width = dstsurf->width;
1756   fb_state.height = dstsurf->height;
1757   fb_state.nr_cbufs = 0;
1758   fb_state.cbufs[0] = 0;
1759   fb_state.zsbuf = dstsurf;
1760   pipe->set_framebuffer_state(pipe, &fb_state);
1761   pipe->set_sample_mask(pipe, ~0);
1762
1763   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1764   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1765   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
1766                           (float) depth,
1767                           UTIL_BLITTER_ATTRIB_NONE, NULL);
1768
1769   blitter_restore_vertex_states(ctx);
1770   blitter_restore_fragment_states(ctx);
1771   blitter_restore_fb_state(ctx);
1772   blitter_restore_render_cond(ctx);
1773   blitter_unset_running_flag(ctx);
1774}
1775
1776/* draw a rectangle across a region using a custom dsa stage - for r600g */
1777void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
1778				       struct pipe_surface *zsurf,
1779				       struct pipe_surface *cbsurf,
1780				       unsigned sample_mask,
1781				       void *dsa_stage, float depth)
1782{
1783   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1784   struct pipe_context *pipe = ctx->base.pipe;
1785   struct pipe_framebuffer_state fb_state;
1786
1787   assert(zsurf->texture);
1788   if (!zsurf->texture)
1789      return;
1790
1791   /* check the saved state */
1792   blitter_set_running_flag(ctx);
1793   blitter_check_saved_vertex_states(ctx);
1794   blitter_check_saved_fragment_states(ctx);
1795   blitter_check_saved_fb_state(ctx);
1796   blitter_disable_render_cond(ctx);
1797
1798   /* bind states */
1799   pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA] :
1800                                         ctx->blend[0]);
1801   pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
1802   ctx->bind_fs_state(pipe, cbsurf ? ctx->fs_write_one_cbuf : ctx->fs_empty);
1803   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1804
1805   /* set a framebuffer state */
1806   fb_state.width = zsurf->width;
1807   fb_state.height = zsurf->height;
1808   fb_state.nr_cbufs = 1;
1809   if (cbsurf) {
1810	   fb_state.cbufs[0] = cbsurf;
1811	   fb_state.nr_cbufs = 1;
1812   } else {
1813	   fb_state.cbufs[0] = NULL;
1814	   fb_state.nr_cbufs = 0;
1815   }
1816   fb_state.zsbuf = zsurf;
1817   pipe->set_framebuffer_state(pipe, &fb_state);
1818   pipe->set_sample_mask(pipe, sample_mask);
1819
1820   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1821   blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
1822   blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
1823                           UTIL_BLITTER_ATTRIB_NONE, NULL);
1824
1825   blitter_restore_vertex_states(ctx);
1826   blitter_restore_fragment_states(ctx);
1827   blitter_restore_fb_state(ctx);
1828   blitter_restore_render_cond(ctx);
1829   blitter_unset_running_flag(ctx);
1830}
1831
1832void util_blitter_copy_buffer(struct blitter_context *blitter,
1833                              struct pipe_resource *dst,
1834                              unsigned dstx,
1835                              struct pipe_resource *src,
1836                              unsigned srcx,
1837                              unsigned size)
1838{
1839   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1840   struct pipe_context *pipe = ctx->base.pipe;
1841   struct pipe_vertex_buffer vb;
1842   struct pipe_stream_output_target *so_target;
1843   unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
1844
1845   if (srcx >= src->width0 ||
1846       dstx >= dst->width0) {
1847      return;
1848   }
1849   if (srcx + size > src->width0) {
1850      size = src->width0 - srcx;
1851   }
1852   if (dstx + size > dst->width0) {
1853      size = dst->width0 - dstx;
1854   }
1855
1856   /* Drivers not capable of Stream Out should not call this function
1857    * in the first place. */
1858   assert(ctx->has_stream_out);
1859
1860   /* Some alignment is required. */
1861   if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
1862       !ctx->has_stream_out) {
1863      struct pipe_box box;
1864      u_box_1d(srcx, size, &box);
1865      util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
1866      return;
1867   }
1868
1869   blitter_set_running_flag(ctx);
1870   blitter_check_saved_vertex_states(ctx);
1871   blitter_disable_render_cond(ctx);
1872
1873   vb.buffer = src;
1874   vb.buffer_offset = srcx;
1875   vb.stride = 4;
1876
1877   pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
1878   pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
1879   pipe->bind_vs_state(pipe, ctx->vs_pos_only);
1880   if (ctx->has_geometry_shader)
1881      pipe->bind_gs_state(pipe, NULL);
1882   pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
1883
1884   so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
1885   pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
1886
1887   util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
1888
1889   blitter_restore_vertex_states(ctx);
1890   blitter_restore_render_cond(ctx);
1891   blitter_unset_running_flag(ctx);
1892   pipe_so_target_reference(&so_target, NULL);
1893}
1894
1895void util_blitter_clear_buffer(struct blitter_context *blitter,
1896                               struct pipe_resource *dst,
1897                               unsigned offset, unsigned size,
1898                               unsigned num_channels,
1899                               const union pipe_color_union *clear_value)
1900{
1901   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1902   struct pipe_context *pipe = ctx->base.pipe;
1903   struct pipe_vertex_buffer vb = {0};
1904   struct pipe_stream_output_target *so_target;
1905   unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
1906
1907   assert(num_channels >= 1);
1908   assert(num_channels <= 4);
1909
1910   /* IMPORTANT:  DON'T DO ANY BOUNDS CHECKING HERE!
1911    *
1912    * R600 uses this to initialize texture resources, so width0 might not be
1913    * what you think it is.
1914    */
1915
1916   /* Streamout is required. */
1917   if (!ctx->has_stream_out) {
1918      assert(!"Streamout unsupported in util_blitter_clear_buffer()");
1919      return;
1920   }
1921
1922   /* Some alignment is required. */
1923   if (offset % 4 != 0 || size % 4 != 0) {
1924      assert(!"Bad alignment in util_blitter_clear_buffer()");
1925      return;
1926   }
1927
1928   u_upload_data(ctx->upload, 0, num_channels*4, clear_value,
1929                 &vb.buffer_offset, &vb.buffer);
1930   vb.stride = 0;
1931
1932   blitter_set_running_flag(ctx);
1933   blitter_check_saved_vertex_states(ctx);
1934   blitter_disable_render_cond(ctx);
1935
1936   pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
1937   pipe->bind_vertex_elements_state(pipe,
1938                                    ctx->velem_state_readbuf[num_channels-1]);
1939   pipe->bind_vs_state(pipe, ctx->vs_pos_only);
1940   if (ctx->has_geometry_shader)
1941      pipe->bind_gs_state(pipe, NULL);
1942   pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
1943
1944   so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
1945   pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
1946
1947   util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
1948
1949   blitter_restore_vertex_states(ctx);
1950   blitter_restore_render_cond(ctx);
1951   blitter_unset_running_flag(ctx);
1952   pipe_so_target_reference(&so_target, NULL);
1953   pipe_resource_reference(&vb.buffer, NULL);
1954}
1955
1956/* probably radeon specific */
1957void util_blitter_custom_resolve_color(struct blitter_context *blitter,
1958				       struct pipe_resource *dst,
1959				       unsigned dst_level,
1960				       unsigned dst_layer,
1961				       struct pipe_resource *src,
1962				       unsigned src_layer,
1963				       unsigned sample_mask,
1964				       void *custom_blend,
1965                                       enum pipe_format format)
1966{
1967   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1968   struct pipe_context *pipe = ctx->base.pipe;
1969   struct pipe_framebuffer_state fb_state;
1970   struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
1971
1972   blitter_set_running_flag(ctx);
1973   blitter_check_saved_vertex_states(ctx);
1974   blitter_check_saved_fragment_states(ctx);
1975   blitter_disable_render_cond(ctx);
1976
1977   /* bind states */
1978   pipe->bind_blend_state(pipe, custom_blend);
1979   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1980   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1981   ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
1982   pipe->set_sample_mask(pipe, sample_mask);
1983
1984   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1985   surf_tmpl.format = format;
1986   surf_tmpl.u.tex.level = dst_level;
1987   surf_tmpl.u.tex.first_layer = dst_layer;
1988   surf_tmpl.u.tex.last_layer = dst_layer;
1989
1990   dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
1991
1992   surf_tmpl.u.tex.level = 0;
1993   surf_tmpl.u.tex.first_layer = src_layer;
1994   surf_tmpl.u.tex.last_layer = src_layer;
1995
1996   srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
1997
1998   /* set a framebuffer state */
1999   fb_state.width = src->width0;
2000   fb_state.height = src->height0;
2001   fb_state.nr_cbufs = 2;
2002   fb_state.cbufs[0] = srcsurf;
2003   fb_state.cbufs[1] = dstsurf;
2004   fb_state.zsbuf = NULL;
2005   pipe->set_framebuffer_state(pipe, &fb_state);
2006
2007   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2008   blitter_set_dst_dimensions(ctx, src->width0, src->height0);
2009   blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
2010                           0, 0, NULL);
2011   blitter_restore_fb_state(ctx);
2012   blitter_restore_vertex_states(ctx);
2013   blitter_restore_fragment_states(ctx);
2014   blitter_restore_render_cond(ctx);
2015   blitter_unset_running_flag(ctx);
2016
2017   pipe_surface_reference(&srcsurf, NULL);
2018   pipe_surface_reference(&dstsurf, NULL);
2019}
2020
2021void util_blitter_custom_color(struct blitter_context *blitter,
2022                               struct pipe_surface *dstsurf,
2023                               void *custom_blend)
2024{
2025   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2026   struct pipe_context *pipe = ctx->base.pipe;
2027   struct pipe_framebuffer_state fb_state;
2028
2029   assert(dstsurf->texture);
2030   if (!dstsurf->texture)
2031      return;
2032
2033   /* check the saved state */
2034   blitter_set_running_flag(ctx);
2035   blitter_check_saved_vertex_states(ctx);
2036   blitter_check_saved_fragment_states(ctx);
2037   blitter_check_saved_fb_state(ctx);
2038   blitter_disable_render_cond(ctx);
2039
2040   /* bind states */
2041   pipe->bind_blend_state(pipe, custom_blend ? custom_blend
2042                                             : ctx->blend[PIPE_MASK_RGBA]);
2043   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2044   ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
2045   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2046   pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2047
2048   /* set a framebuffer state */
2049   fb_state.width = dstsurf->width;
2050   fb_state.height = dstsurf->height;
2051   fb_state.nr_cbufs = 1;
2052   fb_state.cbufs[0] = dstsurf;
2053   fb_state.zsbuf = 0;
2054   pipe->set_framebuffer_state(pipe, &fb_state);
2055   pipe->set_sample_mask(pipe, ~0);
2056
2057   blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2058   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2059   blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
2060                           0, 0, NULL);
2061
2062   blitter_restore_vertex_states(ctx);
2063   blitter_restore_fragment_states(ctx);
2064   blitter_restore_fb_state(ctx);
2065   blitter_restore_render_cond(ctx);
2066   blitter_unset_running_flag(ctx);
2067}
2068