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