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