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