u_blitter.c revision a730838a42c62b7729691b518a134017ff2d3b42
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**************************************************************************
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright 2009 Marek Olšák <maraeo@gmail.com>
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Permission is hereby granted, free of charge, to any person obtaining a
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * copy of this software and associated documentation files (the
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * "Software"), to deal in the Software without restriction, including
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * without limitation the rights to use, copy, modify, merge, publish,
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * distribute, sub license, and/or sell copies of the Software, and to
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch * permit persons to whom the Software is furnished to do so, subject to
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the following conditions:
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * The above copyright notice and this permission notice (including the
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * next paragraph) shall be included in all copies or substantial portions
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * of the Software.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) **************************************************************************/
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/**
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @file
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Blitter utility to facilitate acceleration of the clear, clear_render_target,
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * clear_depth_stencil, and resource_copy_region functions.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * @author Marek Olšák
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_context.h"
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_defines.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_inlines.h"
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_shader_tokens.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "pipe/p_state.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_format.h"
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_memory.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_math.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_blitter.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_draw_quad.h"
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_sampler.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_simple_shaders.h"
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_surface.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_texture.h"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "util/u_upload_mgr.h"
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define INVALID_PTR ((void*)~0)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct blitter_context_priv
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   struct blitter_context base;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   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_set_common_draw_rect_state(struct blitter_context_priv *ctx)
722{
723   struct pipe_context *pipe = ctx->base.pipe;
724
725   pipe->bind_rasterizer_state(pipe, ctx->rs_state);
726   pipe->bind_vs_state(pipe, ctx->vs);
727   if (ctx->has_geometry_shader)
728      pipe->bind_gs_state(pipe, NULL);
729   if (ctx->has_stream_out)
730      pipe->set_stream_output_targets(pipe, 0, NULL, 0);
731}
732
733static void blitter_draw(struct blitter_context_priv *ctx,
734                         unsigned x1, unsigned y1,
735                         unsigned x2, unsigned y2,
736                         float depth)
737{
738   struct pipe_resource *buf = NULL;
739   unsigned offset = 0;
740
741   blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
742
743   u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), ctx->vertices,
744                 &offset, &buf);
745   u_upload_unmap(ctx->upload);
746   util_draw_vertex_buffer(ctx->base.pipe, NULL, buf, offset,
747                           PIPE_PRIM_TRIANGLE_FAN, 4, 2);
748   pipe_resource_reference(&buf, NULL);
749}
750
751static void blitter_draw_rectangle(struct blitter_context *blitter,
752                                   unsigned x1, unsigned y1,
753                                   unsigned x2, unsigned y2,
754                                   float depth,
755                                   enum blitter_attrib_type type,
756                                   const union pipe_color_union *attrib)
757{
758   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
759
760   switch (type) {
761      case UTIL_BLITTER_ATTRIB_COLOR:
762         blitter_set_clear_color(ctx, attrib);
763         break;
764
765      case UTIL_BLITTER_ATTRIB_TEXCOORD:
766         set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
767         break;
768
769      default:;
770   }
771
772   blitter_draw(ctx, x1, y1, x2, y2, depth);
773}
774
775static void util_blitter_clear_custom(struct blitter_context *blitter,
776                                      unsigned width, unsigned height,
777                                      unsigned num_cbufs,
778                                      unsigned clear_buffers,
779                                      enum pipe_format cbuf_format,
780                                      const union pipe_color_union *color,
781                                      double depth, unsigned stencil,
782                                      void *custom_blend, void *custom_dsa)
783{
784   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
785   struct pipe_context *pipe = ctx->base.pipe;
786   struct pipe_stencil_ref sr = { { 0 } };
787   boolean int_format = util_format_is_pure_integer(cbuf_format);
788   assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
789
790   blitter_set_running_flag(ctx);
791   blitter_check_saved_vertex_states(ctx);
792   blitter_check_saved_fragment_states(ctx);
793
794   /* bind states */
795   if (custom_blend) {
796      pipe->bind_blend_state(pipe, custom_blend);
797   } else if (clear_buffers & PIPE_CLEAR_COLOR) {
798      pipe->bind_blend_state(pipe, ctx->blend_write_color);
799   } else {
800      pipe->bind_blend_state(pipe, ctx->blend_keep_color);
801   }
802
803   if (custom_dsa) {
804      pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
805   } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
806      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
807   } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
808      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
809   } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
810      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
811   } else {
812      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
813   }
814
815   sr.ref_value[0] = stencil & 0xff;
816   pipe->set_stencil_ref(pipe, &sr);
817
818   if (util_format_is_pure_sint(cbuf_format)) {
819      pipe->bind_vertex_elements_state(pipe, ctx->velem_sint_state);
820   } else if (util_format_is_pure_uint(cbuf_format)) {
821      pipe->bind_vertex_elements_state(pipe, ctx->velem_uint_state);
822   } else {
823      pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
824   }
825   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs, int_format));
826
827   blitter_set_common_draw_rect_state(ctx);
828   blitter_set_dst_dimensions(ctx, width, height);
829   blitter->draw_rectangle(blitter, 0, 0, width, height, depth,
830                           UTIL_BLITTER_ATTRIB_COLOR, color);
831
832   blitter_restore_vertex_states(ctx);
833   blitter_restore_fragment_states(ctx);
834   blitter_unset_running_flag(ctx);
835}
836
837void util_blitter_clear(struct blitter_context *blitter,
838                        unsigned width, unsigned height,
839                        unsigned num_cbufs,
840                        unsigned clear_buffers,
841                        enum pipe_format cbuf_format,
842                        const union pipe_color_union *color,
843                        double depth, unsigned stencil)
844{
845   util_blitter_clear_custom(blitter, width, height, num_cbufs,
846                             clear_buffers, cbuf_format, color, depth, stencil,
847                             NULL, NULL);
848}
849
850void util_blitter_clear_depth_custom(struct blitter_context *blitter,
851                                     unsigned width, unsigned height,
852                                     double depth, void *custom_dsa)
853{
854    static const union pipe_color_union color;
855    util_blitter_clear_custom(blitter, width, height, 0,
856                              0, PIPE_FORMAT_NONE, &color, depth, 0, NULL, custom_dsa);
857}
858
859static
860boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
861                   unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
862{
863   return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1;
864}
865
866void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
867                                      struct pipe_resource *dst,
868                                      unsigned dstlevel,
869                                      unsigned dstz,
870                                      const struct pipe_box *srcbox)
871{
872    memset(dst_templ, 0, sizeof(*dst_templ));
873    dst_templ->format = dst->format;
874    if (util_format_is_depth_or_stencil(dst->format)) {
875	dst_templ->usage = PIPE_BIND_DEPTH_STENCIL;
876    } else {
877	dst_templ->usage = PIPE_BIND_RENDER_TARGET;
878    }
879    dst_templ->format = util_format_linear(dst->format);
880    dst_templ->u.tex.level = dstlevel;
881    dst_templ->u.tex.first_layer = dstz;
882    dst_templ->u.tex.last_layer = dstz + srcbox->depth - 1;
883}
884
885void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
886                                      struct pipe_resource *src,
887                                      unsigned srclevel)
888{
889    memset(src_templ, 0, sizeof(*src_templ));
890    src_templ->format = util_format_linear(src->format);
891    src_templ->u.tex.first_level = srclevel;
892    src_templ->u.tex.last_level = srclevel;
893    src_templ->u.tex.first_layer = 0;
894    src_templ->u.tex.last_layer =
895        src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
896                                       : src->array_size - 1;
897    src_templ->swizzle_r = PIPE_SWIZZLE_RED;
898    src_templ->swizzle_g = PIPE_SWIZZLE_GREEN;
899    src_templ->swizzle_b = PIPE_SWIZZLE_BLUE;
900    src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA;
901}
902
903void util_blitter_copy_texture(struct blitter_context *blitter,
904                               struct pipe_resource *dst,
905                               unsigned dstlevel,
906                               unsigned dstx, unsigned dsty, unsigned dstz,
907                               struct pipe_resource *src,
908                               unsigned srclevel,
909                               const struct pipe_box *srcbox,
910                               boolean ignore_stencil)
911{
912   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
913   struct pipe_context *pipe = ctx->base.pipe;
914   struct pipe_screen *screen = pipe->screen;
915   struct pipe_surface *dst_view, dst_templ;
916   struct pipe_sampler_view src_templ, *src_view;
917   unsigned bind;
918   boolean is_stencil, is_depth;
919
920   /* Give up if textures are not set. */
921   assert(dst && src);
922   if (!dst || !src)
923      return;
924
925   assert(src->target < PIPE_MAX_TEXTURE_TYPES);
926
927   /* Is this a ZS format? */
928   is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
929   is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
930
931   if (is_depth || is_stencil)
932      bind = PIPE_BIND_DEPTH_STENCIL;
933   else
934      bind = PIPE_BIND_RENDER_TARGET;
935
936   /* Check if we can sample from and render to the surfaces. */
937   /* (assuming copying a stencil buffer is not possible) */
938   if ((!ignore_stencil && is_stencil) ||
939       !screen->is_format_supported(screen, dst->format, dst->target,
940                                    dst->nr_samples, bind) ||
941       !screen->is_format_supported(screen, src->format, src->target,
942                                    src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
943      blitter_set_running_flag(ctx);
944      util_resource_copy_region(pipe, dst, dstlevel, dstx, dsty, dstz,
945                                src, srclevel, srcbox);
946      blitter_unset_running_flag(ctx);
947      return;
948   }
949
950   /* Initialize the surface. */
951   util_blitter_default_dst_texture(&dst_templ, dst, dstlevel, dstz, srcbox);
952   dst_view = pipe->create_surface(pipe, dst, &dst_templ);
953
954   /* Initialize the sampler view. */
955   util_blitter_default_src_texture(&src_templ, src, srclevel);
956   src_view = pipe->create_sampler_view(pipe, src, &src_templ);
957
958   /* Copy. */
959   util_blitter_copy_texture_view(blitter, dst_view, dstx, dsty, src_view,
960                                  srcbox, src->width0, src->height0);
961
962   pipe_surface_reference(&dst_view, NULL);
963   pipe_sampler_view_reference(&src_view, NULL);
964}
965
966void util_blitter_copy_texture_view(struct blitter_context *blitter,
967                                    struct pipe_surface *dst,
968                                    unsigned dstx, unsigned dsty,
969                                    struct pipe_sampler_view *src,
970                                    const struct pipe_box *srcbox,
971                                    unsigned src_width0, unsigned src_height0)
972{
973   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
974   struct pipe_context *pipe = ctx->base.pipe;
975   struct pipe_framebuffer_state fb_state;
976   enum pipe_texture_target src_target = src->texture->target;
977   unsigned width = srcbox->width;
978   unsigned height = srcbox->height;
979
980   /* Sanity checks. */
981   if (dst->texture == src->texture &&
982       dst->u.tex.level == src->u.tex.first_level) {
983      assert(!is_overlap(srcbox->x, srcbox->x + width, srcbox->y, srcbox->y + height,
984                         dstx, dstx + width, dsty, dsty + height));
985   }
986   /* XXX should handle 3d regions */
987   assert(srcbox->depth == 1);
988
989   /* Check whether the states are properly saved. */
990   blitter_set_running_flag(ctx);
991   blitter_check_saved_vertex_states(ctx);
992   blitter_check_saved_fragment_states(ctx);
993   blitter_check_saved_textures(ctx);
994   blitter_check_saved_fb_state(ctx);
995
996   /* Initialize framebuffer state. */
997   fb_state.width = dst->width;
998   fb_state.height = dst->height;
999
1000   if (util_format_is_depth_or_stencil(dst->format)) {
1001      pipe->bind_blend_state(pipe, ctx->blend_keep_color);
1002      pipe->bind_depth_stencil_alpha_state(pipe,
1003                                           ctx->dsa_write_depth_keep_stencil);
1004      pipe->bind_fs_state(pipe,
1005            blitter_get_fs_texfetch_depth(ctx, src_target));
1006
1007      fb_state.nr_cbufs = 0;
1008      fb_state.zsbuf = dst;
1009   } else {
1010      pipe->bind_blend_state(pipe, ctx->blend_write_color);
1011      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1012      pipe->bind_fs_state(pipe,
1013            blitter_get_fs_texfetch_col(ctx, src_target));
1014
1015      fb_state.nr_cbufs = 1;
1016      fb_state.cbufs[0] = dst;
1017      fb_state.zsbuf = 0;
1018   }
1019
1020   pipe->bind_fragment_sampler_states(pipe, 1, &ctx->sampler_state);
1021   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1022   pipe->set_fragment_sampler_views(pipe, 1, &src);
1023   pipe->set_framebuffer_state(pipe, &fb_state);
1024
1025   blitter_set_common_draw_rect_state(ctx);
1026   blitter_set_dst_dimensions(ctx, dst->width, dst->height);
1027
1028   switch (src_target) {
1029      /* Draw the quad with the draw_rectangle callback. */
1030      case PIPE_TEXTURE_1D:
1031      case PIPE_TEXTURE_2D:
1032      case PIPE_TEXTURE_RECT:
1033         {
1034            /* Set texture coordinates. - use a pipe color union
1035             * for interface purposes.
1036             * XXX pipe_color_union is a wrong name since we use that to set
1037             * texture coordinates too.
1038             */
1039            union pipe_color_union coord;
1040            get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1041                          srcbox->x+width, srcbox->y+height, coord.f);
1042
1043            /* Draw. */
1044            blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1045                                    UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1046         }
1047         break;
1048
1049      /* Draw the quad with the generic codepath. */
1050      default:
1051         /* Set texture coordinates. */
1052         switch (src_target) {
1053         case PIPE_TEXTURE_1D_ARRAY:
1054         case PIPE_TEXTURE_2D_ARRAY:
1055         case PIPE_TEXTURE_3D:
1056         case PIPE_TEXTURE_CUBE:
1057            blitter_set_texcoords(ctx, src, src_width0, src_height0, srcbox->z,
1058                                  srcbox->y, srcbox->x,
1059                                  srcbox->x + width, srcbox->y + height);
1060            break;
1061
1062         default:
1063            assert(0);
1064         }
1065
1066         blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0);
1067         break;
1068   }
1069
1070   blitter_restore_vertex_states(ctx);
1071   blitter_restore_fragment_states(ctx);
1072   blitter_restore_textures(ctx);
1073   blitter_restore_fb_state(ctx);
1074   blitter_unset_running_flag(ctx);
1075}
1076
1077/* Clear a region of a color surface to a constant value. */
1078void util_blitter_clear_render_target(struct blitter_context *blitter,
1079                                      struct pipe_surface *dstsurf,
1080                                      const union pipe_color_union *color,
1081                                      unsigned dstx, unsigned dsty,
1082                                      unsigned width, unsigned height)
1083{
1084   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1085   struct pipe_context *pipe = ctx->base.pipe;
1086   struct pipe_framebuffer_state fb_state;
1087
1088   assert(dstsurf->texture);
1089   if (!dstsurf->texture)
1090      return;
1091
1092   /* check the saved state */
1093   blitter_set_running_flag(ctx);
1094   blitter_check_saved_vertex_states(ctx);
1095   blitter_check_saved_fragment_states(ctx);
1096   blitter_check_saved_fb_state(ctx);
1097
1098   /* bind states */
1099   pipe->bind_blend_state(pipe, ctx->blend_write_color);
1100   pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1101   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1, FALSE));
1102   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1103
1104   /* set a framebuffer state */
1105   fb_state.width = dstsurf->width;
1106   fb_state.height = dstsurf->height;
1107   fb_state.nr_cbufs = 1;
1108   fb_state.cbufs[0] = dstsurf;
1109   fb_state.zsbuf = 0;
1110   pipe->set_framebuffer_state(pipe, &fb_state);
1111
1112   blitter_set_common_draw_rect_state(ctx);
1113   blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1114   blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1115                           UTIL_BLITTER_ATTRIB_COLOR, color);
1116
1117   blitter_restore_vertex_states(ctx);
1118   blitter_restore_fragment_states(ctx);
1119   blitter_restore_fb_state(ctx);
1120   blitter_unset_running_flag(ctx);
1121}
1122
1123/* Clear a region of a depth stencil surface. */
1124void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
1125                                      struct pipe_surface *dstsurf,
1126                                      unsigned clear_flags,
1127                                      double depth,
1128                                      unsigned stencil,
1129                                      unsigned dstx, unsigned dsty,
1130                                      unsigned width, unsigned height)
1131{
1132   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1133   struct pipe_context *pipe = ctx->base.pipe;
1134   struct pipe_framebuffer_state fb_state;
1135   struct pipe_stencil_ref sr = { { 0 } };
1136
1137   assert(dstsurf->texture);
1138   if (!dstsurf->texture)
1139      return;
1140
1141   /* check the saved state */
1142   blitter_set_running_flag(ctx);
1143   blitter_check_saved_vertex_states(ctx);
1144   blitter_check_saved_fragment_states(ctx);
1145   blitter_check_saved_fb_state(ctx);
1146
1147   /* bind states */
1148   pipe->bind_blend_state(pipe, ctx->blend_keep_color);
1149   if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1150      sr.ref_value[0] = stencil & 0xff;
1151      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1152      pipe->set_stencil_ref(pipe, &sr);
1153   }
1154   else if (clear_flags & PIPE_CLEAR_DEPTH) {
1155      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1156   }
1157   else if (clear_flags & PIPE_CLEAR_STENCIL) {
1158      sr.ref_value[0] = stencil & 0xff;
1159      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1160      pipe->set_stencil_ref(pipe, &sr);
1161   }
1162   else
1163      /* hmm that should be illegal probably, or make it a no-op somewhere */
1164      pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1165
1166   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1167   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1168
1169   /* set a framebuffer state */
1170   fb_state.width = dstsurf->width;
1171   fb_state.height = dstsurf->height;
1172   fb_state.nr_cbufs = 0;
1173   fb_state.cbufs[0] = 0;
1174   fb_state.zsbuf = dstsurf;
1175   pipe->set_framebuffer_state(pipe, &fb_state);
1176
1177   blitter_set_common_draw_rect_state(ctx);
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   pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0, FALSE));
1212   pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1213
1214   /* set a framebuffer state */
1215   fb_state.width = zsurf->width;
1216   fb_state.height = zsurf->height;
1217   fb_state.nr_cbufs = 1;
1218   if (cbsurf) {
1219	   fb_state.cbufs[0] = cbsurf;
1220	   fb_state.nr_cbufs = 1;
1221   } else {
1222	   fb_state.cbufs[0] = NULL;
1223	   fb_state.nr_cbufs = 0;
1224   }
1225   fb_state.zsbuf = zsurf;
1226   pipe->set_framebuffer_state(pipe, &fb_state);
1227
1228   blitter_set_common_draw_rect_state(ctx);
1229   blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
1230   blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
1231                           UTIL_BLITTER_ATTRIB_NONE, NULL);
1232
1233   blitter_restore_vertex_states(ctx);
1234   blitter_restore_fragment_states(ctx);
1235   blitter_restore_fb_state(ctx);
1236   blitter_unset_running_flag(ctx);
1237}
1238
1239void util_blitter_copy_buffer(struct blitter_context *blitter,
1240                              struct pipe_resource *dst,
1241                              unsigned dstx,
1242                              struct pipe_resource *src,
1243                              unsigned srcx,
1244                              unsigned size)
1245{
1246   struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1247   struct pipe_context *pipe = ctx->base.pipe;
1248   struct pipe_vertex_buffer vb;
1249   struct pipe_stream_output_target *so_target;
1250
1251   if (srcx >= src->width0 ||
1252       dstx >= dst->width0) {
1253      return;
1254   }
1255   if (srcx + size > src->width0) {
1256      size = src->width0 - srcx;
1257   }
1258   if (dstx + size > dst->width0) {
1259      size = dst->width0 - dstx;
1260   }
1261
1262   /* Drivers not capable of Stream Out should not call this function
1263    * in the first place. */
1264   assert(ctx->has_stream_out);
1265
1266   /* Some alignment is required. */
1267   if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
1268       !ctx->has_stream_out) {
1269      struct pipe_box box;
1270      u_box_1d(srcx, size, &box);
1271      util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
1272      return;
1273   }
1274
1275   blitter_set_running_flag(ctx);
1276   blitter_check_saved_vertex_states(ctx);
1277
1278   vb.buffer = src;
1279   vb.buffer_offset = srcx;
1280   vb.stride = 4;
1281
1282   pipe->set_vertex_buffers(pipe, 1, &vb);
1283   pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf);
1284   pipe->bind_vs_state(pipe, ctx->vs_pos_only);
1285   if (ctx->has_geometry_shader)
1286      pipe->bind_gs_state(pipe, NULL);
1287   pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
1288
1289   so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
1290   pipe->set_stream_output_targets(pipe, 1, &so_target, 0);
1291
1292   util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
1293
1294   blitter_restore_vertex_states(ctx);
1295   blitter_unset_running_flag(ctx);
1296   pipe_so_target_reference(&so_target, NULL);
1297}
1298