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