1 /**************************************************************************
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target, clear_depth_stencil
30 * resource_copy_region functions.
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"
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"
51 #define INVALID_PTR ((void*)~0)
53 struct blitter_context_priv
55 struct blitter_context base
;
57 struct pipe_resource
*vbuf
; /**< quad */
59 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
61 /* Templates for various state objects. */
62 struct pipe_sampler_state template_sampler_state
;
64 /* Constant state objects. */
66 void *vs_col
; /**< Vertex shader which passes {pos, color} to the output */
67 void *vs_tex
; /**< Vertex shader which passes {pos, texcoord} to the output.*/
69 /* Fragment shaders. */
70 /* The shader at index i outputs color to color buffers 0,1,...,i-1. */
71 void *fs_col
[PIPE_MAX_COLOR_BUFS
+1];
73 /* FS which outputs a color from a texture,
74 where the index is PIPE_TEXTURE_* to be sampled. */
75 void *fs_texfetch_col
[PIPE_MAX_TEXTURE_TYPES
];
77 /* FS which outputs a depth from a texture,
78 where the index is PIPE_TEXTURE_* to be sampled. */
79 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
82 void *blend_write_color
; /**< blend state with writemask of RGBA */
83 void *blend_keep_color
; /**< blend state with writemask of 0 */
85 /* Depth stencil alpha state. */
86 void *dsa_write_depth_stencil
;
87 void *dsa_write_depth_keep_stencil
;
88 void *dsa_keep_depth_stencil
;
89 void *dsa_keep_depth_write_stencil
;
93 /* Sampler state for clamping to a miplevel. */
94 void *sampler_state
[PIPE_MAX_TEXTURE_LEVELS
];
96 /* Rasterizer state. */
100 struct pipe_viewport_state viewport
;
103 struct pipe_clip_state clip
;
106 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
108 struct blitter_context_priv
*ctx
;
109 struct pipe_blend_state blend
;
110 struct pipe_depth_stencil_alpha_state dsa
;
111 struct pipe_rasterizer_state rs_state
;
112 struct pipe_sampler_state
*sampler_state
;
113 struct pipe_vertex_element velem
[2];
116 ctx
= CALLOC_STRUCT(blitter_context_priv
);
120 ctx
->base
.pipe
= pipe
;
122 /* init state objects for them to be considered invalid */
123 ctx
->base
.saved_blend_state
= INVALID_PTR
;
124 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
125 ctx
->base
.saved_rs_state
= INVALID_PTR
;
126 ctx
->base
.saved_fs
= INVALID_PTR
;
127 ctx
->base
.saved_vs
= INVALID_PTR
;
128 ctx
->base
.saved_velem_state
= INVALID_PTR
;
129 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
130 ctx
->base
.saved_num_sampler_views
= ~0;
131 ctx
->base
.saved_num_sampler_states
= ~0;
132 ctx
->base
.saved_num_vertex_buffers
= ~0;
134 /* blend state objects */
135 memset(&blend
, 0, sizeof(blend
));
136 ctx
->blend_keep_color
= pipe
->create_blend_state(pipe
, &blend
);
138 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
139 ctx
->blend_write_color
= pipe
->create_blend_state(pipe
, &blend
);
141 /* depth stencil alpha state objects */
142 memset(&dsa
, 0, sizeof(dsa
));
143 ctx
->dsa_keep_depth_stencil
=
144 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
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
);
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
);
163 dsa
.depth
.enabled
= 0;
164 dsa
.depth
.writemask
= 0;
165 ctx
->dsa_keep_depth_write_stencil
=
166 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
169 sampler_state
= &ctx
->template_sampler_state
;
170 sampler_state
->wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
171 sampler_state
->wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
172 sampler_state
->wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
173 sampler_state
->normalized_coords
= TRUE
;
174 /* The sampler state objects which sample from a specified mipmap level
175 * are created on-demand. */
177 /* rasterizer state */
178 memset(&rs_state
, 0, sizeof(rs_state
));
179 rs_state
.cull_face
= PIPE_FACE_NONE
;
180 rs_state
.gl_rasterization_rules
= 1;
181 rs_state
.flatshade
= 1;
182 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
184 /* vertex elements state */
185 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
186 for (i
= 0; i
< 2; i
++) {
187 velem
[i
].src_offset
= i
* 4 * sizeof(float);
188 velem
[i
].instance_divisor
= 0;
189 velem
[i
].vertex_buffer_index
= 0;
190 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
192 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
194 /* fragment shaders are created on-demand */
198 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
199 TGSI_SEMANTIC_COLOR
};
200 const uint semantic_indices
[] = { 0, 0 };
202 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
206 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
207 TGSI_SEMANTIC_GENERIC
};
208 const uint semantic_indices
[] = { 0, 0 };
210 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
214 /* set invariant vertex coordinates */
215 for (i
= 0; i
< 4; i
++)
216 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
218 /* create the vertex buffer */
219 ctx
->vbuf
= pipe_buffer_create(ctx
->base
.pipe
->screen
,
220 PIPE_BIND_VERTEX_BUFFER
,
221 sizeof(ctx
->vertices
));
226 void util_blitter_destroy(struct blitter_context
*blitter
)
228 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
229 struct pipe_context
*pipe
= blitter
->pipe
;
232 pipe
->delete_blend_state(pipe
, ctx
->blend_write_color
);
233 pipe
->delete_blend_state(pipe
, ctx
->blend_keep_color
);
234 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
235 pipe
->delete_depth_stencil_alpha_state(pipe
,
236 ctx
->dsa_write_depth_keep_stencil
);
237 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
238 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
240 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
241 pipe
->delete_vs_state(pipe
, ctx
->vs_col
);
242 pipe
->delete_vs_state(pipe
, ctx
->vs_tex
);
243 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
245 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
246 if (ctx
->fs_texfetch_col
[i
])
247 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
248 if (ctx
->fs_texfetch_depth
[i
])
249 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
252 for (i
= 0; i
<= PIPE_MAX_COLOR_BUFS
&& ctx
->fs_col
[i
]; i
++)
254 pipe
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
256 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
257 if (ctx
->sampler_state
[i
])
258 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
[i
]);
260 pipe_resource_reference(&ctx
->vbuf
, NULL
);
264 static void blitter_check_saved_CSOs(struct blitter_context_priv
*ctx
)
266 /* make sure these CSOs have been saved */
267 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
&&
268 ctx
->base
.saved_dsa_state
!= INVALID_PTR
&&
269 ctx
->base
.saved_rs_state
!= INVALID_PTR
&&
270 ctx
->base
.saved_fs
!= INVALID_PTR
&&
271 ctx
->base
.saved_vs
!= INVALID_PTR
&&
272 ctx
->base
.saved_velem_state
!= INVALID_PTR
);
275 static void blitter_restore_CSOs(struct blitter_context_priv
*ctx
)
277 struct pipe_context
*pipe
= ctx
->base
.pipe
;
280 /* restore the state objects which are always required to be saved */
281 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
282 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
283 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
284 pipe
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
285 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
286 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
288 ctx
->base
.saved_blend_state
= INVALID_PTR
;
289 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
290 ctx
->base
.saved_rs_state
= INVALID_PTR
;
291 ctx
->base
.saved_fs
= INVALID_PTR
;
292 ctx
->base
.saved_vs
= INVALID_PTR
;
293 ctx
->base
.saved_velem_state
= INVALID_PTR
;
295 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
297 pipe
->set_viewport_state(pipe
, &ctx
->base
.saved_viewport
);
298 pipe
->set_clip_state(pipe
, &ctx
->base
.saved_clip
);
300 /* restore the state objects which are required to be saved before copy/fill
302 if (ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0) {
303 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
304 util_assign_framebuffer_state(&ctx
->base
.saved_fb_state
, NULL
);
305 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
308 if (ctx
->base
.saved_num_sampler_states
!= ~0) {
309 pipe
->bind_fragment_sampler_states(pipe
,
310 ctx
->base
.saved_num_sampler_states
,
311 ctx
->base
.saved_sampler_states
);
312 ctx
->base
.saved_num_sampler_states
= ~0;
315 if (ctx
->base
.saved_num_sampler_views
!= ~0) {
316 pipe
->set_fragment_sampler_views(pipe
,
317 ctx
->base
.saved_num_sampler_views
,
318 ctx
->base
.saved_sampler_views
);
320 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
321 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
],
324 ctx
->base
.saved_num_sampler_views
= ~0;
327 if (ctx
->base
.saved_num_vertex_buffers
!= ~0) {
328 pipe
->set_vertex_buffers(pipe
,
329 ctx
->base
.saved_num_vertex_buffers
,
330 ctx
->base
.saved_vertex_buffers
);
332 for (i
= 0; i
< ctx
->base
.saved_num_vertex_buffers
; i
++) {
333 if (ctx
->base
.saved_vertex_buffers
[i
].buffer
) {
334 pipe_resource_reference(&ctx
->base
.saved_vertex_buffers
[i
].buffer
,
338 ctx
->base
.saved_num_vertex_buffers
= ~0;
342 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
343 unsigned x1
, unsigned y1
,
344 unsigned x2
, unsigned y2
,
345 unsigned width
, unsigned height
,
350 /* set vertex positions */
351 ctx
->vertices
[0][0][0] = (float)x1
/ width
* 2.0f
- 1.0f
; /*v0.x*/
352 ctx
->vertices
[0][0][1] = (float)y1
/ height
* 2.0f
- 1.0f
; /*v0.y*/
354 ctx
->vertices
[1][0][0] = (float)x2
/ width
* 2.0f
- 1.0f
; /*v1.x*/
355 ctx
->vertices
[1][0][1] = (float)y1
/ height
* 2.0f
- 1.0f
; /*v1.y*/
357 ctx
->vertices
[2][0][0] = (float)x2
/ width
* 2.0f
- 1.0f
; /*v2.x*/
358 ctx
->vertices
[2][0][1] = (float)y2
/ height
* 2.0f
- 1.0f
; /*v2.y*/
360 ctx
->vertices
[3][0][0] = (float)x1
/ width
* 2.0f
- 1.0f
; /*v3.x*/
361 ctx
->vertices
[3][0][1] = (float)y2
/ height
* 2.0f
- 1.0f
; /*v3.y*/
363 for (i
= 0; i
< 4; i
++)
364 ctx
->vertices
[i
][0][2] = depth
; /*z*/
367 ctx
->viewport
.scale
[0] = 0.5f
* width
;
368 ctx
->viewport
.scale
[1] = 0.5f
* height
;
369 ctx
->viewport
.scale
[2] = 1.0f
;
370 ctx
->viewport
.scale
[3] = 1.0f
;
371 ctx
->viewport
.translate
[0] = 0.5f
* width
;
372 ctx
->viewport
.translate
[1] = 0.5f
* height
;
373 ctx
->viewport
.translate
[2] = 0.0f
;
374 ctx
->viewport
.translate
[3] = 0.0f
;
375 ctx
->base
.pipe
->set_viewport_state(ctx
->base
.pipe
, &ctx
->viewport
);
378 ctx
->base
.pipe
->set_clip_state(ctx
->base
.pipe
, &ctx
->clip
);
381 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
387 for (i
= 0; i
< 4; i
++) {
388 ctx
->vertices
[i
][1][0] = rgba
[0];
389 ctx
->vertices
[i
][1][1] = rgba
[1];
390 ctx
->vertices
[i
][1][2] = rgba
[2];
391 ctx
->vertices
[i
][1][3] = rgba
[3];
394 for (i
= 0; i
< 4; i
++) {
395 ctx
->vertices
[i
][1][0] = 0;
396 ctx
->vertices
[i
][1][1] = 0;
397 ctx
->vertices
[i
][1][2] = 0;
398 ctx
->vertices
[i
][1][3] = 0;
403 static void blitter_set_texcoords_2d(struct blitter_context_priv
*ctx
,
404 struct pipe_resource
*src
,
405 struct pipe_subresource subsrc
,
406 unsigned x1
, unsigned y1
,
407 unsigned x2
, unsigned y2
)
410 float s1
= x1
/ (float)u_minify(src
->width0
, subsrc
.level
);
411 float t1
= y1
/ (float)u_minify(src
->height0
, subsrc
.level
);
412 float s2
= x2
/ (float)u_minify(src
->width0
, subsrc
.level
);
413 float t2
= y2
/ (float)u_minify(src
->height0
, subsrc
.level
);
415 ctx
->vertices
[0][1][0] = s1
; /*t0.s*/
416 ctx
->vertices
[0][1][1] = t1
; /*t0.t*/
418 ctx
->vertices
[1][1][0] = s2
; /*t1.s*/
419 ctx
->vertices
[1][1][1] = t1
; /*t1.t*/
421 ctx
->vertices
[2][1][0] = s2
; /*t2.s*/
422 ctx
->vertices
[2][1][1] = t2
; /*t2.t*/
424 ctx
->vertices
[3][1][0] = s1
; /*t3.s*/
425 ctx
->vertices
[3][1][1] = t2
; /*t3.t*/
427 for (i
= 0; i
< 4; i
++) {
428 ctx
->vertices
[i
][1][2] = 0; /*r*/
429 ctx
->vertices
[i
][1][3] = 1; /*q*/
433 static void blitter_set_texcoords_3d(struct blitter_context_priv
*ctx
,
434 struct pipe_resource
*src
,
435 struct pipe_subresource subsrc
,
437 unsigned x1
, unsigned y1
,
438 unsigned x2
, unsigned y2
)
441 float r
= zslice
/ (float)u_minify(src
->depth0
, subsrc
.level
);
443 blitter_set_texcoords_2d(ctx
, src
, subsrc
, x1
, y1
, x2
, y2
);
445 for (i
= 0; i
< 4; i
++)
446 ctx
->vertices
[i
][1][2] = r
; /*r*/
449 static void blitter_set_texcoords_cube(struct blitter_context_priv
*ctx
,
450 struct pipe_resource
*src
,
451 struct pipe_subresource subsrc
,
452 unsigned x1
, unsigned y1
,
453 unsigned x2
, unsigned y2
)
456 float s1
= x1
/ (float)u_minify(src
->width0
, subsrc
.level
);
457 float t1
= y1
/ (float)u_minify(src
->height0
, subsrc
.level
);
458 float s2
= x2
/ (float)u_minify(src
->width0
, subsrc
.level
);
459 float t2
= y2
/ (float)u_minify(src
->height0
, subsrc
.level
);
471 util_map_texcoords2d_onto_cubemap(subsrc
.face
,
472 /* pointer, stride in floats */
474 &ctx
->vertices
[0][1][0], 8);
476 for (i
= 0; i
< 4; i
++)
477 ctx
->vertices
[i
][1][3] = 1; /*q*/
480 static void blitter_draw_quad(struct blitter_context_priv
*ctx
)
482 struct pipe_context
*pipe
= ctx
->base
.pipe
;
484 /* write vertices and draw them */
485 pipe_buffer_write(pipe
, ctx
->vbuf
,
486 0, sizeof(ctx
->vertices
), ctx
->vertices
);
488 util_draw_vertex_buffer(pipe
, ctx
->vbuf
, 0, PIPE_PRIM_TRIANGLE_FAN
,
490 2); /* attribs/vert */
494 void **blitter_get_sampler_state(struct blitter_context_priv
*ctx
,
497 struct pipe_context
*pipe
= ctx
->base
.pipe
;
498 struct pipe_sampler_state
*sampler_state
= &ctx
->template_sampler_state
;
500 assert(miplevel
< PIPE_MAX_TEXTURE_LEVELS
);
502 /* Create the sampler state on-demand. */
503 if (!ctx
->sampler_state
[miplevel
]) {
504 sampler_state
->lod_bias
= miplevel
;
505 sampler_state
->min_lod
= miplevel
;
506 sampler_state
->max_lod
= miplevel
;
508 ctx
->sampler_state
[miplevel
] = pipe
->create_sampler_state(pipe
,
512 /* Return void** so that it can be passed to bind_fragment_sampler_states
514 return &ctx
->sampler_state
[miplevel
];
518 void *blitter_get_fs_col(struct blitter_context_priv
*ctx
, unsigned num_cbufs
)
520 struct pipe_context
*pipe
= ctx
->base
.pipe
;
522 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
524 if (!ctx
->fs_col
[num_cbufs
])
525 ctx
->fs_col
[num_cbufs
] =
526 util_make_fragment_clonecolor_shader(pipe
, num_cbufs
);
528 return ctx
->fs_col
[num_cbufs
];
531 /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
533 pipe_tex_to_tgsi_tex(unsigned pipe_tex_target
)
535 switch (pipe_tex_target
) {
536 case PIPE_TEXTURE_1D
:
537 return TGSI_TEXTURE_1D
;
538 case PIPE_TEXTURE_2D
:
539 return TGSI_TEXTURE_2D
;
540 case PIPE_TEXTURE_3D
:
541 return TGSI_TEXTURE_3D
;
542 case PIPE_TEXTURE_CUBE
:
543 return TGSI_TEXTURE_CUBE
;
545 assert(0 && "unexpected texture target");
546 return TGSI_TEXTURE_UNKNOWN
;
552 void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
555 struct pipe_context
*pipe
= ctx
->base
.pipe
;
557 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
559 /* Create the fragment shader on-demand. */
560 if (!ctx
->fs_texfetch_col
[tex_target
]) {
561 unsigned tgsi_tex
= pipe_tex_to_tgsi_tex(tex_target
);
563 ctx
->fs_texfetch_col
[tex_target
] =
564 util_make_fragment_tex_shader(pipe
, tgsi_tex
, TGSI_INTERPOLATE_LINEAR
);
567 return ctx
->fs_texfetch_col
[tex_target
];
571 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
574 struct pipe_context
*pipe
= ctx
->base
.pipe
;
576 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
578 /* Create the fragment shader on-demand. */
579 if (!ctx
->fs_texfetch_depth
[tex_target
]) {
580 unsigned tgsi_tex
= pipe_tex_to_tgsi_tex(tex_target
);
582 ctx
->fs_texfetch_depth
[tex_target
] =
583 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
584 TGSI_INTERPOLATE_LINEAR
);
587 return ctx
->fs_texfetch_depth
[tex_target
];
590 void util_blitter_clear(struct blitter_context
*blitter
,
591 unsigned width
, unsigned height
,
593 unsigned clear_buffers
,
595 double depth
, unsigned stencil
)
597 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
598 struct pipe_context
*pipe
= ctx
->base
.pipe
;
599 struct pipe_stencil_ref sr
= { { 0 } };
601 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
603 blitter_check_saved_CSOs(ctx
);
606 if (clear_buffers
& PIPE_CLEAR_COLOR
)
607 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
609 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
611 if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
612 sr
.ref_value
[0] = stencil
& 0xff;
613 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
614 pipe
->set_stencil_ref(pipe
, &sr
);
616 else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
617 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
619 else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
620 sr
.ref_value
[0] = stencil
& 0xff;
621 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
622 pipe
->set_stencil_ref(pipe
, &sr
);
625 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
627 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
628 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
629 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, num_cbufs
));
630 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
632 blitter_set_clear_color(ctx
, rgba
);
633 blitter_set_rectangle(ctx
, 0, 0, width
, height
, width
, height
, depth
);
634 blitter_draw_quad(ctx
);
635 blitter_restore_CSOs(ctx
);
639 boolean
is_overlap(unsigned sx1
, unsigned sx2
, unsigned sy1
, unsigned sy2
,
640 unsigned dx1
, unsigned dx2
, unsigned dy1
, unsigned dy2
)
642 return sx1
< dx2
&& sx2
> dx1
&& sy1
< dy2
&& sy2
> dy1
;
645 void util_blitter_copy_region(struct blitter_context
*blitter
,
646 struct pipe_resource
*dst
,
647 struct pipe_subresource subdst
,
648 unsigned dstx
, unsigned dsty
, unsigned dstz
,
649 struct pipe_resource
*src
,
650 struct pipe_subresource subsrc
,
651 unsigned srcx
, unsigned srcy
, unsigned srcz
,
652 unsigned width
, unsigned height
,
653 boolean ignore_stencil
)
655 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
656 struct pipe_context
*pipe
= ctx
->base
.pipe
;
657 struct pipe_screen
*screen
= pipe
->screen
;
658 struct pipe_surface
*dstsurf
;
659 struct pipe_framebuffer_state fb_state
;
660 struct pipe_sampler_view viewTempl
, *view
;
662 boolean is_stencil
, is_depth
;
664 /* Give up if textures are not set. */
671 assert(!is_overlap(srcx
, srcx
+ width
, srcy
, srcy
+ height
,
672 dstx
, dstx
+ width
, dsty
, dsty
+ height
));
674 assert(dst
->format
== src
->format
);
676 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
678 /* Is this a ZS format? */
679 is_depth
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0;
680 is_stencil
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) != 0;
682 if (is_depth
|| is_stencil
)
683 bind
= PIPE_BIND_DEPTH_STENCIL
;
685 bind
= PIPE_BIND_RENDER_TARGET
;
687 /* Check if we can sample from and render to the surfaces. */
688 /* (assuming copying a stencil buffer is not possible) */
689 if ((!ignore_stencil
&& is_stencil
) ||
690 !screen
->is_format_supported(screen
, dst
->format
, dst
->target
,
691 dst
->nr_samples
, bind
, 0) ||
692 !screen
->is_format_supported(screen
, src
->format
, src
->target
,
693 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
, 0)) {
694 util_resource_copy_region(pipe
, dst
, subdst
, dstx
, dsty
, dstz
,
695 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
);
700 dstsurf
= screen
->get_tex_surface(screen
, dst
,
701 subdst
.face
, subdst
.level
, dstz
,
704 /* Check whether the states are properly saved. */
705 blitter_check_saved_CSOs(ctx
);
706 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
707 assert(blitter
->saved_num_sampler_views
!= ~0);
708 assert(blitter
->saved_num_sampler_states
!= ~0);
710 /* Initialize framebuffer state. */
711 fb_state
.width
= dstsurf
->width
;
712 fb_state
.height
= dstsurf
->height
;
715 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
716 pipe
->bind_depth_stencil_alpha_state(pipe
,
717 ctx
->dsa_write_depth_keep_stencil
);
718 pipe
->bind_fs_state(pipe
,
719 blitter_get_fs_texfetch_depth(ctx
, src
->target
));
721 fb_state
.nr_cbufs
= 0;
722 fb_state
.zsbuf
= dstsurf
;
724 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
725 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
726 pipe
->bind_fs_state(pipe
,
727 blitter_get_fs_texfetch_col(ctx
, src
->target
));
729 fb_state
.nr_cbufs
= 1;
730 fb_state
.cbufs
[0] = dstsurf
;
734 /* Initialize sampler view. */
735 u_sampler_view_default_template(&viewTempl
, src
, src
->format
);
736 view
= pipe
->create_sampler_view(pipe
, src
, &viewTempl
);
738 /* Set rasterizer state, shaders, and textures. */
739 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
740 pipe
->bind_vs_state(pipe
, ctx
->vs_tex
);
741 pipe
->bind_fragment_sampler_states(pipe
, 1,
742 blitter_get_sampler_state(ctx
, subsrc
.level
));
743 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
744 pipe
->set_fragment_sampler_views(pipe
, 1, &view
);
745 pipe
->set_framebuffer_state(pipe
, &fb_state
);
747 /* Set texture coordinates. */
748 switch (src
->target
) {
749 case PIPE_TEXTURE_1D
:
750 case PIPE_TEXTURE_2D
:
751 blitter_set_texcoords_2d(ctx
, src
, subsrc
,
752 srcx
, srcy
, srcx
+width
, srcy
+height
);
754 case PIPE_TEXTURE_3D
:
755 blitter_set_texcoords_3d(ctx
, src
, subsrc
, srcz
,
756 srcx
, srcy
, srcx
+width
, srcy
+height
);
758 case PIPE_TEXTURE_CUBE
:
759 blitter_set_texcoords_cube(ctx
, src
, subsrc
,
760 srcx
, srcy
, srcx
+width
, srcy
+height
);
767 blitter_set_rectangle(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
,
768 dstsurf
->width
, dstsurf
->height
, 0);
769 blitter_draw_quad(ctx
);
770 blitter_restore_CSOs(ctx
);
772 pipe_surface_reference(&dstsurf
, NULL
);
773 pipe_sampler_view_reference(&view
, NULL
);
776 /* Clear a region of a color surface to a constant value. */
777 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
778 struct pipe_surface
*dstsurf
,
780 unsigned dstx
, unsigned dsty
,
781 unsigned width
, unsigned height
)
783 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
784 struct pipe_context
*pipe
= ctx
->base
.pipe
;
785 struct pipe_framebuffer_state fb_state
;
787 assert(dstsurf
->texture
);
788 if (!dstsurf
->texture
)
791 /* check the saved state */
792 blitter_check_saved_CSOs(ctx
);
793 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
796 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
797 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
798 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
799 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1));
800 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
801 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
803 /* set a framebuffer state */
804 fb_state
.width
= dstsurf
->width
;
805 fb_state
.height
= dstsurf
->height
;
806 fb_state
.nr_cbufs
= 1;
807 fb_state
.cbufs
[0] = dstsurf
;
809 pipe
->set_framebuffer_state(pipe
, &fb_state
);
811 blitter_set_clear_color(ctx
, rgba
);
812 blitter_set_rectangle(ctx
, 0, 0, width
, height
, dstsurf
->width
, dstsurf
->height
, 0);
813 blitter_draw_quad(ctx
);
814 blitter_restore_CSOs(ctx
);
817 /* Clear a region of a depth stencil surface. */
818 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
819 struct pipe_surface
*dstsurf
,
820 unsigned clear_flags
,
823 unsigned dstx
, unsigned dsty
,
824 unsigned width
, unsigned height
)
826 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
827 struct pipe_context
*pipe
= ctx
->base
.pipe
;
828 struct pipe_framebuffer_state fb_state
;
829 struct pipe_stencil_ref sr
= { { 0 } };
831 assert(dstsurf
->texture
);
832 if (!dstsurf
->texture
)
835 /* check the saved state */
836 blitter_check_saved_CSOs(ctx
);
837 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
840 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
841 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
842 sr
.ref_value
[0] = stencil
& 0xff;
843 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
844 pipe
->set_stencil_ref(pipe
, &sr
);
846 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
847 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
849 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
850 sr
.ref_value
[0] = stencil
& 0xff;
851 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
852 pipe
->set_stencil_ref(pipe
, &sr
);
855 /* hmm that should be illegal probably, or make it a no-op somewhere */
856 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
858 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
859 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0));
860 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
861 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
863 /* set a framebuffer state */
864 fb_state
.width
= dstsurf
->width
;
865 fb_state
.height
= dstsurf
->height
;
866 fb_state
.nr_cbufs
= 0;
867 fb_state
.cbufs
[0] = 0;
868 fb_state
.zsbuf
= dstsurf
;
869 pipe
->set_framebuffer_state(pipe
, &fb_state
);
871 blitter_set_rectangle(ctx
, 0, 0, width
, height
, dstsurf
->width
, dstsurf
->height
, depth
);
872 blitter_draw_quad(ctx
);
873 blitter_restore_CSOs(ctx
);