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