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