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