util: fix a memory leak in blitter
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target, clear_depth_stencil
30 * resource_copy_region functions.
31 *
32 * @author Marek Olšák
33 */
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_inlines.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "pipe/p_state.h"
40
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_blitter.h"
45 #include "util/u_draw_quad.h"
46 #include "util/u_sampler.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_surface.h"
49 #include "util/u_texture.h"
50
51 #define INVALID_PTR ((void*)~0)
52
53 struct blitter_context_priv
54 {
55 struct blitter_context blitter;
56
57 struct pipe_context *pipe; /**< pipe context */
58 struct pipe_resource *vbuf; /**< quad */
59
60 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
61
62 /* Templates for various state objects. */
63 struct pipe_sampler_state template_sampler_state;
64
65 /* Constant state objects. */
66 /* Vertex shaders. */
67 void *vs_col; /**< Vertex shader which passes {pos, color} to the output */
68 void *vs_tex; /**< Vertex shader which passes {pos, texcoord} to the output.*/
69
70 /* Fragment shaders. */
71 /* The shader at index i outputs color to color buffers 0,1,...,i-1. */
72 void *fs_col[PIPE_MAX_COLOR_BUFS+1];
73
74 /* FS which outputs a color from a texture,
75 where the index is PIPE_TEXTURE_* to be sampled. */
76 void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
77
78 /* FS which outputs a depth from a texture,
79 where the index is PIPE_TEXTURE_* to be sampled. */
80 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
81
82 /* Blend state. */
83 void *blend_write_color; /**< blend state with writemask of RGBA */
84 void *blend_keep_color; /**< blend state with writemask of 0 */
85
86 /* Depth stencil alpha state. */
87 void *dsa_write_depth_stencil;
88 void *dsa_write_depth_keep_stencil;
89 void *dsa_keep_depth_stencil;
90 void *dsa_keep_depth_write_stencil;
91
92 void *velem_state;
93
94 /* Sampler state for clamping to a miplevel. */
95 void *sampler_state[PIPE_MAX_TEXTURE_LEVELS];
96
97 /* Rasterizer state. */
98 void *rs_state;
99
100 /* Viewport state. */
101 struct pipe_viewport_state viewport;
102
103 /* Clip state. */
104 struct pipe_clip_state clip;
105 };
106
107 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
108 {
109 struct blitter_context_priv *ctx;
110 struct pipe_blend_state blend;
111 struct pipe_depth_stencil_alpha_state dsa;
112 struct pipe_rasterizer_state rs_state;
113 struct pipe_sampler_state *sampler_state;
114 struct pipe_vertex_element velem[2];
115 unsigned i;
116
117 ctx = CALLOC_STRUCT(blitter_context_priv);
118 if (!ctx)
119 return NULL;
120
121 ctx->pipe = pipe;
122
123 /* init state objects for them to be considered invalid */
124 ctx->blitter.saved_blend_state = INVALID_PTR;
125 ctx->blitter.saved_dsa_state = INVALID_PTR;
126 ctx->blitter.saved_rs_state = INVALID_PTR;
127 ctx->blitter.saved_fs = INVALID_PTR;
128 ctx->blitter.saved_vs = INVALID_PTR;
129 ctx->blitter.saved_velem_state = INVALID_PTR;
130 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
131 ctx->blitter.saved_num_sampler_views = ~0;
132 ctx->blitter.saved_num_sampler_states = ~0;
133 ctx->blitter.saved_num_vertex_buffers = ~0;
134
135 /* blend state objects */
136 memset(&blend, 0, sizeof(blend));
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 memset(&dsa, 0, sizeof(dsa));
144 ctx->dsa_keep_depth_stencil =
145 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
146
147 dsa.depth.enabled = 1;
148 dsa.depth.writemask = 1;
149 dsa.depth.func = PIPE_FUNC_ALWAYS;
150 ctx->dsa_write_depth_keep_stencil =
151 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
152
153 dsa.stencil[0].enabled = 1;
154 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
155 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
156 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
157 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
158 dsa.stencil[0].valuemask = 0xff;
159 dsa.stencil[0].writemask = 0xff;
160 ctx->dsa_write_depth_stencil =
161 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
162
163
164 dsa.depth.enabled = 0;
165 dsa.depth.writemask = 0;
166 ctx->dsa_keep_depth_write_stencil =
167 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
168
169 /* sampler state */
170 sampler_state = &ctx->template_sampler_state;
171 sampler_state->wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
172 sampler_state->wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
173 sampler_state->wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
174 sampler_state->normalized_coords = TRUE;
175 /* The sampler state objects which sample from a specified mipmap level
176 * are created on-demand. */
177
178 /* rasterizer state */
179 memset(&rs_state, 0, sizeof(rs_state));
180 rs_state.cull_face = PIPE_FACE_NONE;
181 rs_state.gl_rasterization_rules = 1;
182 rs_state.flatshade = 1;
183 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
184
185 /* vertex elements state */
186 memset(&velem[0], 0, sizeof(velem[0]) * 2);
187 for (i = 0; i < 2; i++) {
188 velem[i].src_offset = i * 4 * sizeof(float);
189 velem[i].instance_divisor = 0;
190 velem[i].vertex_buffer_index = 0;
191 velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
192 }
193 ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
194
195 /* fragment shaders are created on-demand */
196
197 /* vertex shaders */
198 {
199 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
200 TGSI_SEMANTIC_COLOR };
201 const uint semantic_indices[] = { 0, 0 };
202 ctx->vs_col =
203 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
204 semantic_indices);
205 }
206 {
207 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
208 TGSI_SEMANTIC_GENERIC };
209 const uint semantic_indices[] = { 0, 0 };
210 ctx->vs_tex =
211 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
212 semantic_indices);
213 }
214
215 /* set invariant vertex coordinates */
216 for (i = 0; i < 4; i++)
217 ctx->vertices[i][0][3] = 1; /*v.w*/
218
219 /* create the vertex buffer */
220 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
221 PIPE_BIND_VERTEX_BUFFER,
222 sizeof(ctx->vertices));
223
224 return &ctx->blitter;
225 }
226
227 void util_blitter_destroy(struct blitter_context *blitter)
228 {
229 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
230 struct pipe_context *pipe = ctx->pipe;
231 int i;
232
233 pipe->delete_blend_state(pipe, ctx->blend_write_color);
234 pipe->delete_blend_state(pipe, ctx->blend_keep_color);
235 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
236 pipe->delete_depth_stencil_alpha_state(pipe,
237 ctx->dsa_write_depth_keep_stencil);
238 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
239 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
240
241 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
242 pipe->delete_vs_state(pipe, ctx->vs_col);
243 pipe->delete_vs_state(pipe, ctx->vs_tex);
244 pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
245
246 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
247 if (ctx->fs_texfetch_col[i])
248 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
249 if (ctx->fs_texfetch_depth[i])
250 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
251 }
252
253 for (i = 0; i <= PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
254 if (ctx->fs_col[i])
255 pipe->delete_fs_state(pipe, ctx->fs_col[i]);
256
257 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
258 if (ctx->sampler_state[i])
259 pipe->delete_sampler_state(pipe, ctx->sampler_state[i]);
260
261 pipe_resource_reference(&ctx->vbuf, NULL);
262 FREE(ctx);
263 }
264
265 static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx)
266 {
267 /* make sure these CSOs have been saved */
268 assert(ctx->blitter.saved_blend_state != INVALID_PTR &&
269 ctx->blitter.saved_dsa_state != INVALID_PTR &&
270 ctx->blitter.saved_rs_state != INVALID_PTR &&
271 ctx->blitter.saved_fs != INVALID_PTR &&
272 ctx->blitter.saved_vs != INVALID_PTR &&
273 ctx->blitter.saved_velem_state != INVALID_PTR);
274 }
275
276 static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
277 {
278 struct pipe_context *pipe = ctx->pipe;
279 unsigned i;
280
281 /* restore the state objects which are always required to be saved */
282 pipe->bind_blend_state(pipe, ctx->blitter.saved_blend_state);
283 pipe->bind_depth_stencil_alpha_state(pipe, ctx->blitter.saved_dsa_state);
284 pipe->bind_rasterizer_state(pipe, ctx->blitter.saved_rs_state);
285 pipe->bind_fs_state(pipe, ctx->blitter.saved_fs);
286 pipe->bind_vs_state(pipe, ctx->blitter.saved_vs);
287 pipe->bind_vertex_elements_state(pipe, ctx->blitter.saved_velem_state);
288
289 ctx->blitter.saved_blend_state = INVALID_PTR;
290 ctx->blitter.saved_dsa_state = INVALID_PTR;
291 ctx->blitter.saved_rs_state = INVALID_PTR;
292 ctx->blitter.saved_fs = INVALID_PTR;
293 ctx->blitter.saved_vs = INVALID_PTR;
294 ctx->blitter.saved_velem_state = INVALID_PTR;
295
296 pipe->set_stencil_ref(pipe, &ctx->blitter.saved_stencil_ref);
297
298 pipe->set_viewport_state(pipe, &ctx->blitter.saved_viewport);
299 pipe->set_clip_state(pipe, &ctx->blitter.saved_clip);
300
301 /* restore the state objects which are required to be saved before copy/fill
302 */
303 if (ctx->blitter.saved_fb_state.nr_cbufs != ~0) {
304 pipe->set_framebuffer_state(pipe, &ctx->blitter.saved_fb_state);
305 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
306 }
307
308 if (ctx->blitter.saved_num_sampler_states != ~0) {
309 pipe->bind_fragment_sampler_states(pipe,
310 ctx->blitter.saved_num_sampler_states,
311 ctx->blitter.saved_sampler_states);
312 ctx->blitter.saved_num_sampler_states = ~0;
313 }
314
315 if (ctx->blitter.saved_num_sampler_views != ~0) {
316 pipe->set_fragment_sampler_views(pipe,
317 ctx->blitter.saved_num_sampler_views,
318 ctx->blitter.saved_sampler_views);
319 ctx->blitter.saved_num_sampler_views = ~0;
320 }
321
322 if (ctx->blitter.saved_num_vertex_buffers != ~0) {
323 pipe->set_vertex_buffers(pipe,
324 ctx->blitter.saved_num_vertex_buffers,
325 ctx->blitter.saved_vertex_buffers);
326
327 for (i = 0; i < ctx->blitter.saved_num_vertex_buffers; i++) {
328 if (ctx->blitter.saved_vertex_buffers[i].buffer) {
329 pipe_resource_reference(&ctx->blitter.saved_vertex_buffers[i].buffer,
330 NULL);
331 }
332 }
333 ctx->blitter.saved_num_vertex_buffers = ~0;
334 }
335 }
336
337 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
338 unsigned x1, unsigned y1,
339 unsigned x2, unsigned y2,
340 unsigned width, unsigned height,
341 float depth)
342 {
343 int i;
344
345 /* set vertex positions */
346 ctx->vertices[0][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v0.x*/
347 ctx->vertices[0][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v0.y*/
348
349 ctx->vertices[1][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v1.x*/
350 ctx->vertices[1][0][1] = (float)y1 / height * 2.0f - 1.0f; /*v1.y*/
351
352 ctx->vertices[2][0][0] = (float)x2 / width * 2.0f - 1.0f; /*v2.x*/
353 ctx->vertices[2][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v2.y*/
354
355 ctx->vertices[3][0][0] = (float)x1 / width * 2.0f - 1.0f; /*v3.x*/
356 ctx->vertices[3][0][1] = (float)y2 / height * 2.0f - 1.0f; /*v3.y*/
357
358 for (i = 0; i < 4; i++)
359 ctx->vertices[i][0][2] = depth; /*z*/
360
361 /* viewport */
362 ctx->viewport.scale[0] = 0.5f * width;
363 ctx->viewport.scale[1] = 0.5f * height;
364 ctx->viewport.scale[2] = 1.0f;
365 ctx->viewport.scale[3] = 1.0f;
366 ctx->viewport.translate[0] = 0.5f * width;
367 ctx->viewport.translate[1] = 0.5f * height;
368 ctx->viewport.translate[2] = 0.0f;
369 ctx->viewport.translate[3] = 0.0f;
370 ctx->pipe->set_viewport_state(ctx->pipe, &ctx->viewport);
371
372 /* clip */
373 ctx->pipe->set_clip_state(ctx->pipe, &ctx->clip);
374 }
375
376 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
377 const float *rgba)
378 {
379 int i;
380
381 if (rgba) {
382 for (i = 0; i < 4; i++) {
383 ctx->vertices[i][1][0] = rgba[0];
384 ctx->vertices[i][1][1] = rgba[1];
385 ctx->vertices[i][1][2] = rgba[2];
386 ctx->vertices[i][1][3] = rgba[3];
387 }
388 } else {
389 for (i = 0; i < 4; i++) {
390 ctx->vertices[i][1][0] = 0;
391 ctx->vertices[i][1][1] = 0;
392 ctx->vertices[i][1][2] = 0;
393 ctx->vertices[i][1][3] = 0;
394 }
395 }
396 }
397
398 static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
399 struct pipe_resource *src,
400 struct pipe_subresource subsrc,
401 unsigned x1, unsigned y1,
402 unsigned x2, unsigned y2)
403 {
404 int i;
405 float s1 = x1 / (float)u_minify(src->width0, subsrc.level);
406 float t1 = y1 / (float)u_minify(src->height0, subsrc.level);
407 float s2 = x2 / (float)u_minify(src->width0, subsrc.level);
408 float t2 = y2 / (float)u_minify(src->height0, subsrc.level);
409
410 ctx->vertices[0][1][0] = s1; /*t0.s*/
411 ctx->vertices[0][1][1] = t1; /*t0.t*/
412
413 ctx->vertices[1][1][0] = s2; /*t1.s*/
414 ctx->vertices[1][1][1] = t1; /*t1.t*/
415
416 ctx->vertices[2][1][0] = s2; /*t2.s*/
417 ctx->vertices[2][1][1] = t2; /*t2.t*/
418
419 ctx->vertices[3][1][0] = s1; /*t3.s*/
420 ctx->vertices[3][1][1] = t2; /*t3.t*/
421
422 for (i = 0; i < 4; i++) {
423 ctx->vertices[i][1][2] = 0; /*r*/
424 ctx->vertices[i][1][3] = 1; /*q*/
425 }
426 }
427
428 static void blitter_set_texcoords_3d(struct blitter_context_priv *ctx,
429 struct pipe_resource *src,
430 struct pipe_subresource subsrc,
431 unsigned zslice,
432 unsigned x1, unsigned y1,
433 unsigned x2, unsigned y2)
434 {
435 int i;
436 float r = zslice / (float)u_minify(src->depth0, subsrc.level);
437
438 blitter_set_texcoords_2d(ctx, src, subsrc, x1, y1, x2, y2);
439
440 for (i = 0; i < 4; i++)
441 ctx->vertices[i][1][2] = r; /*r*/
442 }
443
444 static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
445 struct pipe_resource *src,
446 struct pipe_subresource subsrc,
447 unsigned x1, unsigned y1,
448 unsigned x2, unsigned y2)
449 {
450 int i;
451 float s1 = x1 / (float)u_minify(src->width0, subsrc.level);
452 float t1 = y1 / (float)u_minify(src->height0, subsrc.level);
453 float s2 = x2 / (float)u_minify(src->width0, subsrc.level);
454 float t2 = y2 / (float)u_minify(src->height0, subsrc.level);
455 float st[4][2];
456
457 st[0][0] = s1;
458 st[0][1] = t1;
459 st[1][0] = s2;
460 st[1][1] = t1;
461 st[2][0] = s2;
462 st[2][1] = t2;
463 st[3][0] = s1;
464 st[3][1] = t2;
465
466 util_map_texcoords2d_onto_cubemap(subsrc.face,
467 /* pointer, stride in floats */
468 &st[0][0], 2,
469 &ctx->vertices[0][1][0], 8);
470
471 for (i = 0; i < 4; i++)
472 ctx->vertices[i][1][3] = 1; /*q*/
473 }
474
475 static void blitter_draw_quad(struct blitter_context_priv *ctx)
476 {
477 struct pipe_context *pipe = ctx->pipe;
478
479 /* write vertices and draw them */
480 pipe_buffer_write(pipe, ctx->vbuf,
481 0, sizeof(ctx->vertices), ctx->vertices);
482
483 util_draw_vertex_buffer(pipe, ctx->vbuf, 0, PIPE_PRIM_TRIANGLE_FAN,
484 4, /* verts */
485 2); /* attribs/vert */
486 }
487
488 static INLINE
489 void **blitter_get_sampler_state(struct blitter_context_priv *ctx,
490 int miplevel)
491 {
492 struct pipe_context *pipe = ctx->pipe;
493 struct pipe_sampler_state *sampler_state = &ctx->template_sampler_state;
494
495 assert(miplevel < PIPE_MAX_TEXTURE_LEVELS);
496
497 /* Create the sampler state on-demand. */
498 if (!ctx->sampler_state[miplevel]) {
499 sampler_state->lod_bias = miplevel;
500 sampler_state->min_lod = miplevel;
501 sampler_state->max_lod = miplevel;
502
503 ctx->sampler_state[miplevel] = pipe->create_sampler_state(pipe,
504 sampler_state);
505 }
506
507 /* Return void** so that it can be passed to bind_fragment_sampler_states
508 * directly. */
509 return &ctx->sampler_state[miplevel];
510 }
511
512 static INLINE
513 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs)
514 {
515 struct pipe_context *pipe = ctx->pipe;
516
517 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
518
519 if (!ctx->fs_col[num_cbufs])
520 ctx->fs_col[num_cbufs] =
521 util_make_fragment_clonecolor_shader(pipe, num_cbufs);
522
523 return ctx->fs_col[num_cbufs];
524 }
525
526 /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
527 static unsigned
528 pipe_tex_to_tgsi_tex(unsigned pipe_tex_target)
529 {
530 switch (pipe_tex_target) {
531 case PIPE_TEXTURE_1D:
532 return TGSI_TEXTURE_1D;
533 case PIPE_TEXTURE_2D:
534 return TGSI_TEXTURE_2D;
535 case PIPE_TEXTURE_3D:
536 return TGSI_TEXTURE_3D;
537 case PIPE_TEXTURE_CUBE:
538 return TGSI_TEXTURE_CUBE;
539 default:
540 assert(0 && "unexpected texture target");
541 return TGSI_TEXTURE_UNKNOWN;
542 }
543 }
544
545
546 static INLINE
547 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
548 unsigned tex_target)
549 {
550 struct pipe_context *pipe = ctx->pipe;
551
552 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
553
554 /* Create the fragment shader on-demand. */
555 if (!ctx->fs_texfetch_col[tex_target]) {
556 unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
557
558 ctx->fs_texfetch_col[tex_target] =
559 util_make_fragment_tex_shader(pipe, tgsi_tex, TGSI_INTERPOLATE_LINEAR);
560 }
561
562 return ctx->fs_texfetch_col[tex_target];
563 }
564
565 static INLINE
566 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
567 unsigned tex_target)
568 {
569 struct pipe_context *pipe = ctx->pipe;
570
571 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
572
573 /* Create the fragment shader on-demand. */
574 if (!ctx->fs_texfetch_depth[tex_target]) {
575 unsigned tgsi_tex = pipe_tex_to_tgsi_tex(tex_target);
576
577 ctx->fs_texfetch_depth[tex_target] =
578 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
579 TGSI_INTERPOLATE_LINEAR);
580 }
581
582 return ctx->fs_texfetch_depth[tex_target];
583 }
584
585 void util_blitter_clear(struct blitter_context *blitter,
586 unsigned width, unsigned height,
587 unsigned num_cbufs,
588 unsigned clear_buffers,
589 const float *rgba,
590 double depth, unsigned stencil)
591 {
592 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
593 struct pipe_context *pipe = ctx->pipe;
594 struct pipe_stencil_ref sr = { { 0 } };
595
596 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
597
598 blitter_check_saved_CSOs(ctx);
599
600 /* bind CSOs */
601 if (clear_buffers & PIPE_CLEAR_COLOR)
602 pipe->bind_blend_state(pipe, ctx->blend_write_color);
603 else
604 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
605
606 if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
607 sr.ref_value[0] = stencil & 0xff;
608 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
609 pipe->set_stencil_ref(pipe, &sr);
610 }
611 else if (clear_buffers & PIPE_CLEAR_DEPTH) {
612 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
613 }
614 else if (clear_buffers & PIPE_CLEAR_STENCIL) {
615 sr.ref_value[0] = stencil & 0xff;
616 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
617 pipe->set_stencil_ref(pipe, &sr);
618 }
619 else
620 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
621
622 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
623 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
624 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs));
625 pipe->bind_vs_state(pipe, ctx->vs_col);
626
627 blitter_set_clear_color(ctx, rgba);
628 blitter_set_rectangle(ctx, 0, 0, width, height, width, height, depth);
629 blitter_draw_quad(ctx);
630 blitter_restore_CSOs(ctx);
631 }
632
633 static
634 boolean is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
635 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
636 {
637 return sx1 < dx2 && sx2 > dx1 && sy1 < dy2 && sy2 > dy1;
638 }
639
640 void util_blitter_copy_region(struct blitter_context *blitter,
641 struct pipe_resource *dst,
642 struct pipe_subresource subdst,
643 unsigned dstx, unsigned dsty, unsigned dstz,
644 struct pipe_resource *src,
645 struct pipe_subresource subsrc,
646 unsigned srcx, unsigned srcy, unsigned srcz,
647 unsigned width, unsigned height,
648 boolean ignore_stencil)
649 {
650 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
651 struct pipe_context *pipe = ctx->pipe;
652 struct pipe_screen *screen = pipe->screen;
653 struct pipe_surface *dstsurf;
654 struct pipe_framebuffer_state fb_state;
655 struct pipe_sampler_view viewTempl, *view;
656 unsigned bind;
657 boolean is_stencil, is_depth;
658
659 /* Give up if textures are not set. */
660 assert(dst && src);
661 if (!dst || !src)
662 return;
663
664 /* Sanity checks. */
665 if (dst == src) {
666 assert(!is_overlap(srcx, srcx + width, srcy, srcy + height,
667 dstx, dstx + width, dsty, dsty + height));
668 } else {
669 assert(dst->format == src->format);
670 }
671 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
672
673 /* Is this a ZS format? */
674 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
675 is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
676
677 if (is_depth || is_stencil)
678 bind = PIPE_BIND_DEPTH_STENCIL;
679 else
680 bind = PIPE_BIND_RENDER_TARGET;
681
682 /* Check if we can sample from and render to the surfaces. */
683 /* (assuming copying a stencil buffer is not possible) */
684 if ((!ignore_stencil && is_stencil) ||
685 !screen->is_format_supported(screen, dst->format, dst->target,
686 dst->nr_samples, bind, 0) ||
687 !screen->is_format_supported(screen, src->format, src->target,
688 src->nr_samples, PIPE_BIND_SAMPLER_VIEW, 0)) {
689 util_resource_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
690 src, subsrc, srcx, srcy, srcz, width, height);
691 return;
692 }
693
694 /* Get surfaces. */
695 dstsurf = screen->get_tex_surface(screen, dst,
696 subdst.face, subdst.level, dstz,
697 bind);
698
699 /* Check whether the states are properly saved. */
700 blitter_check_saved_CSOs(ctx);
701 assert(blitter->saved_fb_state.nr_cbufs != ~0);
702 assert(blitter->saved_num_sampler_views != ~0);
703 assert(blitter->saved_num_sampler_states != ~0);
704
705 /* Initialize framebuffer state. */
706 fb_state.width = dstsurf->width;
707 fb_state.height = dstsurf->height;
708
709 if (is_depth) {
710 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
711 pipe->bind_depth_stencil_alpha_state(pipe,
712 ctx->dsa_write_depth_keep_stencil);
713 pipe->bind_fs_state(pipe,
714 blitter_get_fs_texfetch_depth(ctx, src->target));
715
716 fb_state.nr_cbufs = 0;
717 fb_state.zsbuf = dstsurf;
718 } else {
719 pipe->bind_blend_state(pipe, ctx->blend_write_color);
720 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
721 pipe->bind_fs_state(pipe,
722 blitter_get_fs_texfetch_col(ctx, src->target));
723
724 fb_state.nr_cbufs = 1;
725 fb_state.cbufs[0] = dstsurf;
726 fb_state.zsbuf = 0;
727 }
728
729 /* Initialize sampler view. */
730 u_sampler_view_default_template(&viewTempl, src, src->format);
731 view = pipe->create_sampler_view(pipe, src, &viewTempl);
732
733 /* Set rasterizer state, shaders, and textures. */
734 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
735 pipe->bind_vs_state(pipe, ctx->vs_tex);
736 pipe->bind_fragment_sampler_states(pipe, 1,
737 blitter_get_sampler_state(ctx, subsrc.level));
738 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
739 pipe->set_fragment_sampler_views(pipe, 1, &view);
740 pipe->set_framebuffer_state(pipe, &fb_state);
741
742 /* Set texture coordinates. */
743 switch (src->target) {
744 case PIPE_TEXTURE_1D:
745 case PIPE_TEXTURE_2D:
746 blitter_set_texcoords_2d(ctx, src, subsrc,
747 srcx, srcy, srcx+width, srcy+height);
748 break;
749 case PIPE_TEXTURE_3D:
750 blitter_set_texcoords_3d(ctx, src, subsrc, srcz,
751 srcx, srcy, srcx+width, srcy+height);
752 break;
753 case PIPE_TEXTURE_CUBE:
754 blitter_set_texcoords_cube(ctx, src, subsrc,
755 srcx, srcy, srcx+width, srcy+height);
756 break;
757 default:
758 assert(0);
759 return;
760 }
761
762 blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height,
763 dstsurf->width, dstsurf->height, 0);
764 blitter_draw_quad(ctx);
765 blitter_restore_CSOs(ctx);
766
767 pipe_surface_reference(&dstsurf, NULL);
768 pipe_sampler_view_reference(&view, NULL);
769 }
770
771 /* Clear a region of a color surface to a constant value. */
772 void util_blitter_clear_render_target(struct blitter_context *blitter,
773 struct pipe_surface *dstsurf,
774 const float *rgba,
775 unsigned dstx, unsigned dsty,
776 unsigned width, unsigned height)
777 {
778 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
779 struct pipe_context *pipe = ctx->pipe;
780 struct pipe_framebuffer_state fb_state;
781
782 assert(dstsurf->texture);
783 if (!dstsurf->texture)
784 return;
785
786 /* check the saved state */
787 blitter_check_saved_CSOs(ctx);
788 assert(blitter->saved_fb_state.nr_cbufs != ~0);
789
790 /* bind CSOs */
791 pipe->bind_blend_state(pipe, ctx->blend_write_color);
792 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
793 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
794 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
795 pipe->bind_vs_state(pipe, ctx->vs_col);
796 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
797
798 /* set a framebuffer state */
799 fb_state.width = dstsurf->width;
800 fb_state.height = dstsurf->height;
801 fb_state.nr_cbufs = 1;
802 fb_state.cbufs[0] = dstsurf;
803 fb_state.zsbuf = 0;
804 pipe->set_framebuffer_state(pipe, &fb_state);
805
806 blitter_set_clear_color(ctx, rgba);
807 blitter_set_rectangle(ctx, 0, 0, width, height, dstsurf->width, dstsurf->height, 0);
808 blitter_draw_quad(ctx);
809 blitter_restore_CSOs(ctx);
810 }
811
812 /* Clear a region of a depth stencil surface. */
813 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
814 struct pipe_surface *dstsurf,
815 unsigned clear_flags,
816 double depth,
817 unsigned stencil,
818 unsigned dstx, unsigned dsty,
819 unsigned width, unsigned height)
820 {
821 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
822 struct pipe_context *pipe = ctx->pipe;
823 struct pipe_framebuffer_state fb_state;
824 struct pipe_stencil_ref sr = { { 0 } };
825
826 assert(dstsurf->texture);
827 if (!dstsurf->texture)
828 return;
829
830 /* check the saved state */
831 blitter_check_saved_CSOs(ctx);
832 assert(blitter->saved_fb_state.nr_cbufs != ~0);
833
834 /* bind CSOs */
835 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
836 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
837 sr.ref_value[0] = stencil & 0xff;
838 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
839 pipe->set_stencil_ref(pipe, &sr);
840 }
841 else if (clear_flags & PIPE_CLEAR_DEPTH) {
842 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
843 }
844 else if (clear_flags & PIPE_CLEAR_STENCIL) {
845 sr.ref_value[0] = stencil & 0xff;
846 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
847 pipe->set_stencil_ref(pipe, &sr);
848 }
849 else
850 /* hmm that should be illegal probably, or make it a no-op somewhere */
851 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
852
853 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
854 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 0));
855 pipe->bind_vs_state(pipe, ctx->vs_col);
856 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
857
858 /* set a framebuffer state */
859 fb_state.width = dstsurf->width;
860 fb_state.height = dstsurf->height;
861 fb_state.nr_cbufs = 0;
862 fb_state.cbufs[0] = 0;
863 fb_state.zsbuf = dstsurf;
864 pipe->set_framebuffer_state(pipe, &fb_state);
865
866 blitter_set_rectangle(ctx, 0, 0, width, height, dstsurf->width, dstsurf->height, depth);
867 blitter_draw_quad(ctx);
868 blitter_restore_CSOs(ctx);
869 }