Merge branch 'gallium-newclear'
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target, clear_depth_stencil
30 * resource_copy_region functions.
31 *
32 * @author Marek Olšák
33 */
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_inlines.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "pipe/p_state.h"
40
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_blitter.h"
45 #include "util/u_draw_quad.h"
46 #include "util/u_pack_color.h"
47 #include "util/u_sampler.h"
48 #include "util/u_simple_shaders.h"
49 #include "util/u_surface.h"
50 #include "util/u_texture.h"
51
52 #define INVALID_PTR ((void*)~0)
53
54 struct blitter_context_priv
55 {
56 struct blitter_context blitter;
57
58 struct pipe_context *pipe; /**< pipe context */
59 struct pipe_resource *vbuf; /**< quad */
60
61 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
62
63 /* Templates for various state objects. */
64 struct pipe_sampler_state template_sampler_state;
65
66 /* Constant state objects. */
67 /* Vertex shaders. */
68 void *vs_col; /**< Vertex shader which passes {pos, color} to the output */
69 void *vs_tex; /**< Vertex shader which passes {pos, texcoord} to the output.*/
70
71 /* Fragment shaders. */
72 /* The shader at index i outputs color to color buffers 0,1,...,i-1. */
73 void *fs_col[PIPE_MAX_COLOR_BUFS+1];
74
75 /* FS which outputs a color from a texture,
76 where the index is PIPE_TEXTURE_* to be sampled. */
77 void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
78
79 /* FS which outputs a depth from a texture,
80 where the index is PIPE_TEXTURE_* to be sampled. */
81 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
82
83 /* Blend state. */
84 void *blend_write_color; /**< blend state with writemask of RGBA */
85 void *blend_keep_color; /**< blend state with writemask of 0 */
86
87 /* Depth stencil alpha state. */
88 void *dsa_write_depth_stencil;
89 void *dsa_write_depth_keep_stencil;
90 void *dsa_keep_depth_stencil;
91 void *dsa_keep_depth_write_stencil;
92
93 void *velem_state;
94
95 /* Sampler state for clamping to a miplevel. */
96 void *sampler_state[PIPE_MAX_TEXTURE_LEVELS];
97
98 /* Rasterizer state. */
99 void *rs_state;
100
101 struct pipe_sampler_view *sampler_view;
102
103 /* Viewport state. */
104 struct pipe_viewport_state viewport;
105
106 /* Clip state. */
107 struct pipe_clip_state clip;
108 };
109
110 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
111 {
112 struct blitter_context_priv *ctx;
113 struct pipe_blend_state blend;
114 struct pipe_depth_stencil_alpha_state dsa;
115 struct pipe_rasterizer_state rs_state;
116 struct pipe_sampler_state *sampler_state;
117 struct pipe_vertex_element velem[2];
118 unsigned i;
119
120 ctx = CALLOC_STRUCT(blitter_context_priv);
121 if (!ctx)
122 return NULL;
123
124 ctx->pipe = pipe;
125
126 /* init state objects for them to be considered invalid */
127 ctx->blitter.saved_blend_state = INVALID_PTR;
128 ctx->blitter.saved_dsa_state = INVALID_PTR;
129 ctx->blitter.saved_rs_state = INVALID_PTR;
130 ctx->blitter.saved_fs = INVALID_PTR;
131 ctx->blitter.saved_vs = INVALID_PTR;
132 ctx->blitter.saved_velem_state = INVALID_PTR;
133 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
134 ctx->blitter.saved_num_sampler_views = ~0;
135 ctx->blitter.saved_num_sampler_states = ~0;
136 ctx->blitter.saved_num_vertex_buffers = ~0;
137
138 /* blend state objects */
139 memset(&blend, 0, sizeof(blend));
140 ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
141
142 blend.rt[0].colormask = PIPE_MASK_RGBA;
143 ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
144
145 /* depth stencil alpha state objects */
146 memset(&dsa, 0, sizeof(dsa));
147 ctx->dsa_keep_depth_stencil =
148 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
149
150 dsa.depth.enabled = 1;
151 dsa.depth.writemask = 1;
152 dsa.depth.func = PIPE_FUNC_ALWAYS;
153 ctx->dsa_write_depth_keep_stencil =
154 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
155
156 dsa.stencil[0].enabled = 1;
157 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
158 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
159 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
160 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
161 dsa.stencil[0].valuemask = 0xff;
162 dsa.stencil[0].writemask = 0xff;
163 ctx->dsa_write_depth_stencil =
164 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
165
166
167 dsa.depth.enabled = 0;
168 dsa.depth.writemask = 0;
169 ctx->dsa_keep_depth_write_stencil =
170 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
171
172 /* sampler state */
173 sampler_state = &ctx->template_sampler_state;
174 sampler_state->wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
175 sampler_state->wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
176 sampler_state->wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
177 sampler_state->normalized_coords = TRUE;
178 /* The sampler state objects which sample from a specified mipmap level
179 * are created on-demand. */
180
181 /* rasterizer state */
182 memset(&rs_state, 0, sizeof(rs_state));
183 rs_state.cull_face = PIPE_FACE_NONE;
184 rs_state.gl_rasterization_rules = 1;
185 rs_state.flatshade = 1;
186 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
187
188 /* vertex elements state */
189 memset(&velem[0], 0, sizeof(velem[0]) * 2);
190 for (i = 0; i < 2; i++) {
191 velem[i].src_offset = i * 4 * sizeof(float);
192 velem[i].instance_divisor = 0;
193 velem[i].vertex_buffer_index = 0;
194 velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
195 }
196 ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
197
198 /* fragment shaders are created on-demand */
199
200 /* vertex shaders */
201 {
202 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
203 TGSI_SEMANTIC_COLOR };
204 const uint semantic_indices[] = { 0, 0 };
205 ctx->vs_col =
206 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
207 semantic_indices);
208 }
209 {
210 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
211 TGSI_SEMANTIC_GENERIC };
212 const uint semantic_indices[] = { 0, 0 };
213 ctx->vs_tex =
214 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
215 semantic_indices);
216 }
217
218 /* set invariant vertex coordinates */
219 for (i = 0; i < 4; i++)
220 ctx->vertices[i][0][3] = 1; /*v.w*/
221
222 /* create the vertex buffer */
223 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
224 PIPE_BIND_VERTEX_BUFFER,
225 sizeof(ctx->vertices));
226
227 return &ctx->blitter;
228 }
229
230 void util_blitter_destroy(struct blitter_context *blitter)
231 {
232 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
233 struct pipe_context *pipe = ctx->pipe;
234 int i;
235
236 pipe->delete_blend_state(pipe, ctx->blend_write_color);
237 pipe->delete_blend_state(pipe, ctx->blend_keep_color);
238 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
239 pipe->delete_depth_stencil_alpha_state(pipe,
240 ctx->dsa_write_depth_keep_stencil);
241 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
242 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
243
244 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
245 pipe->delete_vs_state(pipe, ctx->vs_col);
246 pipe->delete_vs_state(pipe, ctx->vs_tex);
247 pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
248
249 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
250 if (ctx->fs_texfetch_col[i])
251 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
252 if (ctx->fs_texfetch_depth[i])
253 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
254 }
255
256 for (i = 0; i <= PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
257 if (ctx->fs_col[i])
258 pipe->delete_fs_state(pipe, ctx->fs_col[i]);
259
260 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
261 if (ctx->sampler_state[i])
262 pipe->delete_sampler_state(pipe, ctx->sampler_state[i]);
263
264 if (ctx->sampler_view) {
265 pipe_sampler_view_reference(&ctx->sampler_view, NULL);
266 }
267
268 pipe_resource_reference(&ctx->vbuf, NULL);
269 FREE(ctx);
270 }
271
272 static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx)
273 {
274 /* make sure these CSOs have been saved */
275 assert(ctx->blitter.saved_blend_state != INVALID_PTR &&
276 ctx->blitter.saved_dsa_state != INVALID_PTR &&
277 ctx->blitter.saved_rs_state != INVALID_PTR &&
278 ctx->blitter.saved_fs != INVALID_PTR &&
279 ctx->blitter.saved_vs != INVALID_PTR &&
280 ctx->blitter.saved_velem_state != INVALID_PTR);
281 }
282
283 static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
284 {
285 struct pipe_context *pipe = ctx->pipe;
286
287 /* restore the state objects which are always required to be saved */
288 pipe->bind_blend_state(pipe, ctx->blitter.saved_blend_state);
289 pipe->bind_depth_stencil_alpha_state(pipe, ctx->blitter.saved_dsa_state);
290 pipe->bind_rasterizer_state(pipe, ctx->blitter.saved_rs_state);
291 pipe->bind_fs_state(pipe, ctx->blitter.saved_fs);
292 pipe->bind_vs_state(pipe, ctx->blitter.saved_vs);
293 pipe->bind_vertex_elements_state(pipe, ctx->blitter.saved_velem_state);
294
295 ctx->blitter.saved_blend_state = INVALID_PTR;
296 ctx->blitter.saved_dsa_state = INVALID_PTR;
297 ctx->blitter.saved_rs_state = INVALID_PTR;
298 ctx->blitter.saved_fs = INVALID_PTR;
299 ctx->blitter.saved_vs = INVALID_PTR;
300 ctx->blitter.saved_velem_state = INVALID_PTR;
301
302 pipe->set_stencil_ref(pipe, &ctx->blitter.saved_stencil_ref);
303
304 pipe->set_viewport_state(pipe, &ctx->blitter.saved_viewport);
305 pipe->set_clip_state(pipe, &ctx->blitter.saved_clip);
306
307 /* restore the state objects which are required to be saved before copy/fill
308 */
309 if (ctx->blitter.saved_fb_state.nr_cbufs != ~0) {
310 pipe->set_framebuffer_state(pipe, &ctx->blitter.saved_fb_state);
311 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
312 }
313
314 if (ctx->blitter.saved_num_sampler_states != ~0) {
315 pipe->bind_fragment_sampler_states(pipe,
316 ctx->blitter.saved_num_sampler_states,
317 ctx->blitter.saved_sampler_states);
318 ctx->blitter.saved_num_sampler_states = ~0;
319 }
320
321 if (ctx->blitter.saved_num_sampler_views != ~0) {
322 pipe->set_fragment_sampler_views(pipe,
323 ctx->blitter.saved_num_sampler_views,
324 ctx->blitter.saved_sampler_views);
325 ctx->blitter.saved_num_sampler_views = ~0;
326 }
327
328 if (ctx->blitter.saved_num_vertex_buffers != ~0) {
329 pipe->set_vertex_buffers(pipe,
330 ctx->blitter.saved_num_vertex_buffers,
331 ctx->blitter.saved_vertex_buffers);
332 ctx->blitter.saved_num_vertex_buffers = ~0;
333 }
334 }
335
336 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
337 unsigned x1, unsigned y1,
338 unsigned x2, unsigned y2,
339 unsigned width, unsigned height,
340 float depth)
341 {
342 int i;
343
344 /* set vertex positions */
345 ctx->vertices[0][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v0.x*/
346 ctx->vertices[0][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v0.y*/
347
348 ctx->vertices[1][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v1.x*/
349 ctx->vertices[1][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v1.y*/
350
351 ctx->vertices[2][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v2.x*/
352 ctx->vertices[2][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v2.y*/
353
354 ctx->vertices[3][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v3.x*/
355 ctx->vertices[3][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v3.y*/
356
357 for (i = 0; i < 4; i++)
358 ctx->vertices[i][0][2] = depth; /*z*/
359
360 /* viewport */
361 ctx->viewport.scale[0] = 0.5f * width;
362 ctx->viewport.scale[1] = 0.5f * height;
363 ctx->viewport.scale[2] = 1.0f;
364 ctx->viewport.scale[3] = 1.0f;
365 ctx->viewport.translate[0] = 0.5f * width;
366 ctx->viewport.translate[1] = 0.5f * height;
367 ctx->viewport.translate[2] = 0.0f;
368 ctx->viewport.translate[3] = 0.0f;
369 ctx->pipe->set_viewport_state(ctx->pipe, &ctx->viewport);
370
371 /* clip */
372 ctx->pipe->set_clip_state(ctx->pipe, &ctx->clip);
373 }
374
375 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
376 const float *rgba)
377 {
378 int i;
379
380 if (rgba) {
381 for (i = 0; i < 4; i++) {
382 ctx->vertices[i][1][0] = rgba[0];
383 ctx->vertices[i][1][1] = rgba[1];
384 ctx->vertices[i][1][2] = rgba[2];
385 ctx->vertices[i][1][3] = rgba[3];
386 }
387 } else {
388 for (i = 0; i < 4; i++) {
389 ctx->vertices[i][1][0] = 0;
390 ctx->vertices[i][1][1] = 0;
391 ctx->vertices[i][1][2] = 0;
392 ctx->vertices[i][1][3] = 0;
393 }
394 }
395 }
396
397 static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
398 struct pipe_resource *src,
399 struct pipe_subresource subsrc,
400 unsigned x1, unsigned y1,
401 unsigned x2, unsigned y2)
402 {
403 int i;
404 float s1 = x1 / (float)u_minify(src->width0, subsrc.level);
405 float t1 = y1 / (float)u_minify(src->height0, subsrc.level);
406 float s2 = x2 / (float)u_minify(src->width0, subsrc.level);
407 float t2 = y2 / (float)u_minify(src->height0, subsrc.level);
408
409 ctx->vertices[0][1][0] = s1; /*t0.s*/
410 ctx->vertices[0][1][1] = t1; /*t0.t*/
411
412 ctx->vertices[1][1][0] = s2; /*t1.s*/
413 ctx->vertices[1][1][1] = t1; /*t1.t*/
414
415 ctx->vertices[2][1][0] = s2; /*t2.s*/
416 ctx->vertices[2][1][1] = t2; /*t2.t*/
417
418 ctx->vertices[3][1][0] = s1; /*t3.s*/
419 ctx->vertices[3][1][1] = t2; /*t3.t*/
420
421 for (i = 0; i < 4; i++) {
422 ctx->vertices[i][1][2] = 0; /*r*/
423 ctx->vertices[i][1][3] = 1; /*q*/
424 }
425 }
426
427 static void blitter_set_texcoords_3d(struct blitter_context_priv *ctx,
428 struct pipe_resource *src,
429 struct pipe_subresource subsrc,
430 unsigned zslice,
431 unsigned x1, unsigned y1,
432 unsigned x2, unsigned y2)
433 {
434 int i;
435 float r = zslice / (float)u_minify(src->depth0, subsrc.level);
436
437 blitter_set_texcoords_2d(ctx, src, subsrc, x1, y1, x2, y2);
438
439 for (i = 0; i < 4; i++)
440 ctx->vertices[i][1][2] = r; /*r*/
441 }
442
443 static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
444 struct pipe_resource *src,
445 struct pipe_subresource subsrc,
446 unsigned x1, unsigned y1,
447 unsigned x2, unsigned y2)
448 {
449 int i;
450 float s1 = x1 / (float)u_minify(src->width0, subsrc.level);
451 float t1 = y1 / (float)u_minify(src->height0, subsrc.level);
452 float s2 = x2 / (float)u_minify(src->width0, subsrc.level);
453 float t2 = y2 / (float)u_minify(src->height0, subsrc.level);
454 float st[4][2];
455
456 st[0][0] = s1;
457 st[0][1] = t1;
458 st[1][0] = s2;
459 st[1][1] = t1;
460 st[2][0] = s2;
461 st[2][1] = t2;
462 st[3][0] = s1;
463 st[3][1] = t2;
464
465 util_map_texcoords2d_onto_cubemap(subsrc.face,
466 /* pointer, stride in floats */
467 &st[0][0], 2,
468 &ctx->vertices[0][1][0], 8);
469
470 for (i = 0; i < 4; i++)
471 ctx->vertices[i][1][3] = 1; /*q*/
472 }
473
474 static void blitter_draw_quad(struct blitter_context_priv *ctx)
475 {
476 struct pipe_context *pipe = ctx->pipe;
477
478 /* write vertices and draw them */
479 pipe_buffer_write(pipe, ctx->vbuf,
480 0, sizeof(ctx->vertices), ctx->vertices);
481
482 util_draw_vertex_buffer(pipe, ctx->vbuf, 0, PIPE_PRIM_TRIANGLE_FAN,
483 4, /* verts */
484 2); /* attribs/vert */
485 }
486
487 static INLINE
488 void **blitter_get_sampler_state(struct blitter_context_priv *ctx,
489 int miplevel)
490 {
491 struct pipe_context *pipe = ctx->pipe;
492 struct pipe_sampler_state *sampler_state = &ctx->template_sampler_state;
493
494 assert(miplevel < PIPE_MAX_TEXTURE_LEVELS);
495
496 /* Create the sampler state on-demand. */
497 if (!ctx->sampler_state[miplevel]) {
498 sampler_state->lod_bias = miplevel;
499 sampler_state->min_lod = miplevel;
500 sampler_state->max_lod = miplevel;
501
502 ctx->sampler_state[miplevel] = pipe->create_sampler_state(pipe,
503 sampler_state);
504 }
505
506 /* Return void** so that it can be passed to bind_fragment_sampler_states
507 * directly. */
508 return &ctx->sampler_state[miplevel];
509 }
510
511 static INLINE
512 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs)
513 {
514 struct pipe_context *pipe = ctx->pipe;
515
516 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
517
518 if (!ctx->fs_col[num_cbufs])
519 ctx->fs_col[num_cbufs] =
520 util_make_fragment_clonecolor_shader(pipe, num_cbufs);
521
522 return ctx->fs_col[num_cbufs];
523 }
524
525 /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
526 static unsigned
527 pipe_tex_to_tgsi_tex(unsigned pipe_tex_target)
528 {
529 switch (pipe_tex_target) {
530 case PIPE_TEXTURE_1D:
531 return TGSI_TEXTURE_1D;
532 case PIPE_TEXTURE_2D:
533 return TGSI_TEXTURE_2D;
534 case PIPE_TEXTURE_3D:
535 return TGSI_TEXTURE_3D;
536 case PIPE_TEXTURE_CUBE:
537 return TGSI_TEXTURE_CUBE;
538 default:
539 assert(0 && "unexpected texture target");
540 return TGSI_TEXTURE_UNKNOWN;
541 }
542 }
543
544
545 static INLINE
546 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
547 unsigned tex_target)
548 {
549 struct pipe_context *pipe = ctx->pipe;
550
551 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
552
553 /* Create the fragment shader on-demand. */
554 if (!ctx->fs_texfetch_col[tex_target]) {
555 unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
556
557 ctx->fs_texfetch_col[tex_target] =
558 util_make_fragment_tex_shader(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR);
559 }
560
561 return ctx->fs_texfetch_col[tex_target];
562 }
563
564 static INLINE
565 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
566 unsigned tex_target)
567 {
568 struct pipe_context *pipe = ctx->pipe;
569
570 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
571
572 /* Create the fragment shader on-demand. */
573 if (!ctx->fs_texfetch_depth[tex_target]) {
574 unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
575
576 ctx->fs_texfetch_depth[tex_target] =
577 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
578 TGSI_INTERPOLATE_LINEAR);
579 }
580
581 return ctx->fs_texfetch_depth[tex_target];
582 }
583
584 void util_blitter_clear(struct blitter_context *blitter,
585 unsigned width, unsigned height,
586 unsigned num_cbufs,
587 unsigned clear_buffers,
588 const float *rgba,
589 double depth, unsigned stencil)
590 {
591 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
592 struct pipe_context *pipe = ctx->pipe;
593 struct pipe_stencil_ref sr = { { 0 } };
594
595 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
596
597 blitter_check_saved_CSOs(ctx);
598
599 /* bind CSOs */
600 if (clear_buffers & PIPE_CLEAR_COLOR)
601 pipe->bind_blend_state(pipe, ctx->blend_write_color);
602 else
603 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
604
605 if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
606 sr.ref_value[0] = stencil & 0xff;
607 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
608 pipe->set_stencil_ref(pipe, &sr);
609 }
610 else if (clear_buffers & PIPE_CLEAR_DEPTH) {
611 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
612 }
613 else if (clear_buffers & PIPE_CLEAR_STENCIL) {
614 sr.ref_value[0] = stencil & 0xff;
615 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
616 pipe->set_stencil_ref(pipe, &sr);
617 }
618 else
619 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
620
621 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
622 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
623 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs));
624 pipe->bind_vs_state(pipe, ctx->vs_col);
625
626 blitter_set_clear_color(ctx, rgba);
627 blitter_set_rectangle(ctx, 0, 0, width, height, width, height, depth);
628 blitter_draw_quad(ctx);
629 blitter_restore_CSOs(ctx);
630 }
631
632 static
633 boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
634 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
635 {
636 return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1;
637 }
638
639 void util_blitter_copy_region(struct blitter_context *blitter,
640 struct pipe_resource *dst,
641 struct pipe_subresource subdst,
642 unsigned dstx, unsigned dsty, unsigned dstz,
643 struct pipe_resource *src,
644 struct pipe_subresource subsrc,
645 unsigned srcx, unsigned srcy, unsigned srcz,
646 unsigned width, unsigned height,
647 boolean ignore_stencil)
648 {
649 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
650 struct pipe_context *pipe = ctx->pipe;
651 struct pipe_screen *screen = pipe->screen;
652 struct pipe_surface *dstsurf;
653 struct pipe_framebuffer_state fb_state;
654 struct pipe_sampler_view viewTempl, *view;
655 unsigned bind;
656 boolean is_stencil, is_depth;
657
658 /* Give up if textures are not set. */
659 assert(dst && src);
660 if (!dst || !src)
661 return;
662
663 /* Sanity checks. */
664 if (dst == src) {
665 assert(!is_overlap(srcx, srcx + width, srcy, srcy + height,
666 dstx, dstx + width, dsty, dsty + height));
667 } else {
668 assert(dst->format == src->format);
669 }
670 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
671
672 /* Is this a ZS format? */
673 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
674 is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
675
676 if (is_depth || is_stencil)
677 bind = PIPE_BIND_DEPTH_STENCIL;
678 else
679 bind = PIPE_BIND_RENDER_TARGET;
680
681 /* Check if we can sample from and render to the surfaces. */
682 /* (assuming copying a stencil buffer is not possible) */
683 if ((!ignore_stencil && is_stencil) ||
684 !screen->is_format_supported(screen, dst->format, dst->target,
685 dst->nr_samples, bind, 0) ||
686 !screen->is_format_supported(screen, src->format, src->target,
687 src->nr_samples, PIPE_BIND_SAMPLER_VIEW, 0)) {
688 util_resource_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
689 src, subsrc, srcx, srcy, srcz, width, height);
690 return;
691 }
692
693 /* Get surfaces. */
694 dstsurf = screen->get_tex_surface(screen, dst,
695 subdst.face, subdst.level, dstz,
696 bind);
697
698 /* Check whether the states are properly saved. */
699 blitter_check_saved_CSOs(ctx);
700 assert(blitter->saved_fb_state.nr_cbufs != ~0);
701 assert(blitter->saved_num_sampler_views != ~0);
702 assert(blitter->saved_num_sampler_states != ~0);
703
704 /* Initialize framebuffer state. */
705 fb_state.width = dstsurf->width;
706 fb_state.height = dstsurf->height;
707
708 if (is_depth) {
709 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
710 pipe->bind_depth_stencil_alpha_state(pipe,
711 ctx->dsa_write_depth_keep_stencil);
712 pipe->bind_fs_state(pipe,
713 blitter_get_fs_texfetch_depth(ctx, src->target));
714
715 fb_state.nr_cbufs = 0;
716 fb_state.zsbuf = dstsurf;
717 } else {
718 pipe->bind_blend_state(pipe, ctx->blend_write_color);
719 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
720 pipe->bind_fs_state(pipe,
721 blitter_get_fs_texfetch_col(ctx, src->target));
722
723 fb_state.nr_cbufs = 1;
724 fb_state.cbufs[0] = dstsurf;
725 fb_state.zsbuf = 0;
726 }
727
728 /* Initialize sampler view. */
729 u_sampler_view_default_template(&viewTempl, src, src->format);
730 view = pipe->create_sampler_view(pipe, src, &viewTempl);
731
732 if (ctx->sampler_view) {
733 pipe_sampler_view_reference(&ctx->sampler_view, NULL);
734 }
735 ctx->sampler_view = view;
736
737 /* Set rasterizer state, shaders, and textures. */
738 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
739 pipe->bind_vs_state(pipe, ctx->vs_tex);
740 pipe->bind_fragment_sampler_states(pipe, 1,
741 blitter_get_sampler_state(ctx, subsrc.level));
742 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
743 pipe->set_fragment_sampler_views(pipe, 1, &view);
744 pipe->set_framebuffer_state(pipe, &fb_state);
745
746 /* Set texture coordinates. */
747 switch (src->target) {
748 case PIPE_TEXTURE_1D:
749 case PIPE_TEXTURE_2D:
750 blitter_set_texcoords_2d(ctx, src, subsrc,
751 srcx, srcy, srcx+width, srcy+height);
752 break;
753 case PIPE_TEXTURE_3D:
754 blitter_set_texcoords_3d(ctx, src, subsrc, srcz,
755 srcx, srcy, srcx+width, srcy+height);
756 break;
757 case PIPE_TEXTURE_CUBE:
758 blitter_set_texcoords_cube(ctx, src, subsrc,
759 srcx, srcy, srcx+width, srcy+height);
760 break;
761 default:
762 assert(0);
763 return;
764 }
765
766 blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height,
767 dstsurf->width, dstsurf->height, 0);
768 blitter_draw_quad(ctx);
769 blitter_restore_CSOs(ctx);
770
771 pipe_surface_reference(&dstsurf, NULL);
772 }
773
774 /* Clear a region of a color surface to a constant value. */
775 void util_blitter_clear_render_target(struct blitter_context *blitter,
776 struct pipe_surface *dstsurf,
777 const float *rgba,
778 unsigned dstx, unsigned dsty,
779 unsigned width, unsigned height)
780 {
781 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
782 struct pipe_context *pipe = ctx->pipe;
783 struct pipe_framebuffer_state fb_state;
784
785 assert(dstsurf->texture);
786 if (!dstsurf->texture)
787 return;
788
789 /* check the saved state */
790 blitter_check_saved_CSOs(ctx);
791 assert(blitter->saved_fb_state.nr_cbufs != ~0);
792
793 /* bind CSOs */
794 pipe->bind_blend_state(pipe, ctx->blend_write_color);
795 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
796 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
797 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
798 pipe->bind_vs_state(pipe, ctx->vs_col);
799 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
800
801 /* set a framebuffer state */
802 fb_state.width = dstsurf->width;
803 fb_state.height = dstsurf->height;
804 fb_state.nr_cbufs = 1;
805 fb_state.cbufs[0] = dstsurf;
806 fb_state.zsbuf = 0;
807 pipe->set_framebuffer_state(pipe, &fb_state);
808
809 blitter_set_clear_color(ctx, rgba);
810 blitter_set_rectangle(ctx, 0, 0, width, height, dstsurf->width, dstsurf->height, 0);
811 blitter_draw_quad(ctx);
812 blitter_restore_CSOs(ctx);
813 }
814
815 /* Clear a region of a depth stencil surface. */
816 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
817 struct pipe_surface *dstsurf,
818 unsigned clear_flags,
819 double depth,
820 unsigned stencil,
821 unsigned dstx, unsigned dsty,
822 unsigned width, unsigned height)
823 {
824 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
825 struct pipe_context *pipe = ctx->pipe;
826 struct pipe_framebuffer_state fb_state;
827 struct pipe_stencil_ref sr = { { 0 } };
828
829 assert(dstsurf->texture);
830 if (!dstsurf->texture)
831 return;
832
833 /* check the saved state */
834 blitter_check_saved_CSOs(ctx);
835 assert(blitter->saved_fb_state.nr_cbufs != ~0);
836
837 /* bind CSOs */
838 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
839 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
840 sr.ref_value[0] = stencil & 0xff;
841 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
842 pipe->set_stencil_ref(pipe, &sr);
843 }
844 else if (clear_flags & PIPE_CLEAR_DEPTH) {
845 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
846 }
847 else if (clear_flags & PIPE_CLEAR_STENCIL) {
848 sr.ref_value[0] = stencil & 0xff;
849 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
850 pipe->set_stencil_ref(pipe, &sr);
851 }
852 else
853 /* hmm that should be illegal probably, or make it a no-op somewhere */
854 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
855
856 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
857 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0));
858 pipe->bind_vs_state(pipe, ctx->vs_col);
859 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
860
861 /* set a framebuffer state */
862 fb_state.width = dstsurf->width;
863 fb_state.height = dstsurf->height;
864 fb_state.nr_cbufs = 0;
865 fb_state.cbufs[0] = 0;
866 fb_state.zsbuf = dstsurf;
867 pipe->set_framebuffer_state(pipe, &fb_state);
868
869 blitter_set_rectangle(ctx, 0, 0, width, height, dstsurf->width, dstsurf->height, depth);
870 blitter_draw_quad(ctx);
871 blitter_restore_CSOs(ctx);
872 }