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