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
;
90 void *dsa_flush_depth_stencil
;
94 /* Sampler state for clamping to a miplevel. */
95 void *sampler_state
[PIPE_MAX_TEXTURE_LEVELS
];
97 /* Rasterizer state. */
100 /* Viewport state. */
101 struct pipe_viewport_state viewport
;
104 struct pipe_clip_state clip
;
106 /* Destination surface dimensions. */
111 static void blitter_draw_rectangle(struct blitter_context
*blitter
,
112 unsigned x
, unsigned y
,
113 unsigned width
, unsigned height
,
115 enum blitter_attrib_type type
,
116 const float attrib
[4]);
119 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
121 struct blitter_context_priv
*ctx
;
122 struct pipe_blend_state blend
;
123 struct pipe_depth_stencil_alpha_state dsa
;
124 struct pipe_rasterizer_state rs_state
;
125 struct pipe_sampler_state
*sampler_state
;
126 struct pipe_vertex_element velem
[2];
129 ctx
= CALLOC_STRUCT(blitter_context_priv
);
133 ctx
->base
.pipe
= pipe
;
134 ctx
->base
.draw_rectangle
= blitter_draw_rectangle
;
136 /* init state objects for them to be considered invalid */
137 ctx
->base
.saved_blend_state
= INVALID_PTR
;
138 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
139 ctx
->base
.saved_rs_state
= INVALID_PTR
;
140 ctx
->base
.saved_fs
= INVALID_PTR
;
141 ctx
->base
.saved_vs
= INVALID_PTR
;
142 ctx
->base
.saved_velem_state
= INVALID_PTR
;
143 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
144 ctx
->base
.saved_num_sampler_views
= ~0;
145 ctx
->base
.saved_num_sampler_states
= ~0;
146 ctx
->base
.saved_num_vertex_buffers
= ~0;
148 /* blend state objects */
149 memset(&blend
, 0, sizeof(blend
));
150 ctx
->blend_keep_color
= pipe
->create_blend_state(pipe
, &blend
);
152 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
153 ctx
->blend_write_color
= pipe
->create_blend_state(pipe
, &blend
);
155 /* depth stencil alpha state objects */
156 memset(&dsa
, 0, sizeof(dsa
));
157 ctx
->dsa_keep_depth_stencil
=
158 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
160 dsa
.depth
.writemask
= 1;
161 ctx
->dsa_flush_depth_stencil
=
162 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
164 dsa
.depth
.enabled
= 1;
165 dsa
.depth
.writemask
= 1;
166 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
167 ctx
->dsa_write_depth_keep_stencil
=
168 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
170 dsa
.stencil
[0].enabled
= 1;
171 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
172 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
173 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
174 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
175 dsa
.stencil
[0].valuemask
= 0xff;
176 dsa
.stencil
[0].writemask
= 0xff;
177 ctx
->dsa_write_depth_stencil
=
178 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
181 dsa
.depth
.enabled
= 0;
182 dsa
.depth
.writemask
= 0;
183 ctx
->dsa_keep_depth_write_stencil
=
184 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
187 sampler_state
= &ctx
->template_sampler_state
;
188 sampler_state
->wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
189 sampler_state
->wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
190 sampler_state
->wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
191 sampler_state
->normalized_coords
= TRUE
;
192 /* The sampler state objects which sample from a specified mipmap level
193 * are created on-demand. */
195 /* rasterizer state */
196 memset(&rs_state
, 0, sizeof(rs_state
));
197 rs_state
.cull_face
= PIPE_FACE_NONE
;
198 rs_state
.gl_rasterization_rules
= 1;
199 rs_state
.flatshade
= 1;
200 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
202 /* vertex elements state */
203 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
204 for (i
= 0; i
< 2; i
++) {
205 velem
[i
].src_offset
= i
* 4 * sizeof(float);
206 velem
[i
].instance_divisor
= 0;
207 velem
[i
].vertex_buffer_index
= 0;
208 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
210 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
212 /* fragment shaders are created on-demand */
216 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
217 TGSI_SEMANTIC_COLOR
};
218 const uint semantic_indices
[] = { 0, 0 };
220 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
224 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
225 TGSI_SEMANTIC_GENERIC
};
226 const uint semantic_indices
[] = { 0, 0 };
228 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
232 /* set invariant vertex coordinates */
233 for (i
= 0; i
< 4; i
++)
234 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
236 /* create the vertex buffer */
237 ctx
->vbuf
= pipe_buffer_create(ctx
->base
.pipe
->screen
,
238 PIPE_BIND_VERTEX_BUFFER
,
239 sizeof(ctx
->vertices
));
244 void util_blitter_destroy(struct blitter_context
*blitter
)
246 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
247 struct pipe_context
*pipe
= blitter
->pipe
;
250 pipe
->delete_blend_state(pipe
, ctx
->blend_write_color
);
251 pipe
->delete_blend_state(pipe
, ctx
->blend_keep_color
);
252 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
253 pipe
->delete_depth_stencil_alpha_state(pipe
,
254 ctx
->dsa_write_depth_keep_stencil
);
255 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
256 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
258 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
259 pipe
->delete_vs_state(pipe
, ctx
->vs_col
);
260 pipe
->delete_vs_state(pipe
, ctx
->vs_tex
);
261 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
263 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
264 if (ctx
->fs_texfetch_col
[i
])
265 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
266 if (ctx
->fs_texfetch_depth
[i
])
267 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
270 for (i
= 0; i
<= PIPE_MAX_COLOR_BUFS
&& ctx
->fs_col
[i
]; i
++)
272 pipe
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
274 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
275 if (ctx
->sampler_state
[i
])
276 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
[i
]);
278 pipe_resource_reference(&ctx
->vbuf
, NULL
);
282 static void blitter_check_saved_CSOs(struct blitter_context_priv
*ctx
)
284 /* make sure these CSOs have been saved */
285 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
&&
286 ctx
->base
.saved_dsa_state
!= INVALID_PTR
&&
287 ctx
->base
.saved_rs_state
!= INVALID_PTR
&&
288 ctx
->base
.saved_fs
!= INVALID_PTR
&&
289 ctx
->base
.saved_vs
!= INVALID_PTR
&&
290 ctx
->base
.saved_velem_state
!= INVALID_PTR
);
293 static void blitter_restore_CSOs(struct blitter_context_priv
*ctx
)
295 struct pipe_context
*pipe
= ctx
->base
.pipe
;
298 /* restore the state objects which are always required to be saved */
299 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
300 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
301 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
302 pipe
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
303 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
304 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
306 ctx
->base
.saved_blend_state
= INVALID_PTR
;
307 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
308 ctx
->base
.saved_rs_state
= INVALID_PTR
;
309 ctx
->base
.saved_fs
= INVALID_PTR
;
310 ctx
->base
.saved_vs
= INVALID_PTR
;
311 ctx
->base
.saved_velem_state
= INVALID_PTR
;
313 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
315 pipe
->set_viewport_state(pipe
, &ctx
->base
.saved_viewport
);
316 pipe
->set_clip_state(pipe
, &ctx
->base
.saved_clip
);
318 /* restore the state objects which are required to be saved before copy/fill
320 if (ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0) {
321 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
322 util_assign_framebuffer_state(&ctx
->base
.saved_fb_state
, NULL
);
323 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
326 if (ctx
->base
.saved_num_sampler_states
!= ~0) {
327 pipe
->bind_fragment_sampler_states(pipe
,
328 ctx
->base
.saved_num_sampler_states
,
329 ctx
->base
.saved_sampler_states
);
330 ctx
->base
.saved_num_sampler_states
= ~0;
333 if (ctx
->base
.saved_num_sampler_views
!= ~0) {
334 pipe
->set_fragment_sampler_views(pipe
,
335 ctx
->base
.saved_num_sampler_views
,
336 ctx
->base
.saved_sampler_views
);
338 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
339 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
],
342 ctx
->base
.saved_num_sampler_views
= ~0;
345 if (ctx
->base
.saved_num_vertex_buffers
!= ~0) {
346 pipe
->set_vertex_buffers(pipe
,
347 ctx
->base
.saved_num_vertex_buffers
,
348 ctx
->base
.saved_vertex_buffers
);
350 for (i
= 0; i
< ctx
->base
.saved_num_vertex_buffers
; i
++) {
351 if (ctx
->base
.saved_vertex_buffers
[i
].buffer
) {
352 pipe_resource_reference(&ctx
->base
.saved_vertex_buffers
[i
].buffer
,
356 ctx
->base
.saved_num_vertex_buffers
= ~0;
360 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
361 unsigned x1
, unsigned y1
,
362 unsigned x2
, unsigned y2
,
367 /* set vertex positions */
368 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
369 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
371 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
372 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
374 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
375 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
377 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
378 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
380 for (i
= 0; i
< 4; i
++)
381 ctx
->vertices
[i
][0][2] = depth
; /*z*/
384 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
385 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
386 ctx
->viewport
.scale
[2] = 1.0f
;
387 ctx
->viewport
.scale
[3] = 1.0f
;
388 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
389 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
390 ctx
->viewport
.translate
[2] = 0.0f
;
391 ctx
->viewport
.translate
[3] = 0.0f
;
392 ctx
->base
.pipe
->set_viewport_state(ctx
->base
.pipe
, &ctx
->viewport
);
395 ctx
->base
.pipe
->set_clip_state(ctx
->base
.pipe
, &ctx
->clip
);
398 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
404 for (i
= 0; i
< 4; i
++) {
405 ctx
->vertices
[i
][1][0] = rgba
[0];
406 ctx
->vertices
[i
][1][1] = rgba
[1];
407 ctx
->vertices
[i
][1][2] = rgba
[2];
408 ctx
->vertices
[i
][1][3] = rgba
[3];
411 for (i
= 0; i
< 4; i
++) {
412 ctx
->vertices
[i
][1][0] = 0;
413 ctx
->vertices
[i
][1][1] = 0;
414 ctx
->vertices
[i
][1][2] = 0;
415 ctx
->vertices
[i
][1][3] = 0;
420 static void get_normalized_texcoords(struct pipe_resource
*src
,
421 struct pipe_subresource subsrc
,
422 unsigned x1
, unsigned y1
,
423 unsigned x2
, unsigned y2
,
426 out
[0] = x1
/ (float)u_minify(src
->width0
, subsrc
.level
);
427 out
[1] = y1
/ (float)u_minify(src
->height0
, subsrc
.level
);
428 out
[2] = x2
/ (float)u_minify(src
->width0
, subsrc
.level
);
429 out
[3] = y2
/ (float)u_minify(src
->height0
, subsrc
.level
);
432 static void set_texcoords_in_vertices(const float coord
[4],
433 float *out
, unsigned stride
)
435 out
[0] = coord
[0]; /*t0.s*/
436 out
[1] = coord
[1]; /*t0.t*/
438 out
[0] = coord
[2]; /*t1.s*/
439 out
[1] = coord
[1]; /*t1.t*/
441 out
[0] = coord
[2]; /*t2.s*/
442 out
[1] = coord
[3]; /*t2.t*/
444 out
[0] = coord
[0]; /*t3.s*/
445 out
[1] = coord
[3]; /*t3.t*/
448 static void blitter_set_texcoords_2d(struct blitter_context_priv
*ctx
,
449 struct pipe_resource
*src
,
450 struct pipe_subresource subsrc
,
451 unsigned x1
, unsigned y1
,
452 unsigned x2
, unsigned y2
)
457 get_normalized_texcoords(src
, subsrc
, x1
, y1
, x2
, y2
, coord
);
458 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
460 for (i
= 0; i
< 4; i
++) {
461 ctx
->vertices
[i
][1][2] = 0; /*r*/
462 ctx
->vertices
[i
][1][3] = 1; /*q*/
466 static void blitter_set_texcoords_3d(struct blitter_context_priv
*ctx
,
467 struct pipe_resource
*src
,
468 struct pipe_subresource subsrc
,
470 unsigned x1
, unsigned y1
,
471 unsigned x2
, unsigned y2
)
474 float r
= zslice
/ (float)u_minify(src
->depth0
, subsrc
.level
);
476 blitter_set_texcoords_2d(ctx
, src
, subsrc
, x1
, y1
, x2
, y2
);
478 for (i
= 0; i
< 4; i
++)
479 ctx
->vertices
[i
][1][2] = r
; /*r*/
482 static void blitter_set_texcoords_cube(struct blitter_context_priv
*ctx
,
483 struct pipe_resource
*src
,
484 struct pipe_subresource subsrc
,
485 unsigned x1
, unsigned y1
,
486 unsigned x2
, unsigned y2
)
492 get_normalized_texcoords(src
, subsrc
, x1
, y1
, x2
, y2
, coord
);
493 set_texcoords_in_vertices(coord
, &st
[0][0], 2);
495 util_map_texcoords2d_onto_cubemap(subsrc
.face
,
496 /* pointer, stride in floats */
498 &ctx
->vertices
[0][1][0], 8);
500 for (i
= 0; i
< 4; i
++)
501 ctx
->vertices
[i
][1][3] = 1; /*q*/
504 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
505 unsigned width
, unsigned height
)
507 ctx
->dst_width
= width
;
508 ctx
->dst_height
= height
;
511 static void blitter_draw_quad(struct blitter_context_priv
*ctx
)
513 struct pipe_context
*pipe
= ctx
->base
.pipe
;
515 /* write vertices and draw them */
516 pipe_buffer_write(pipe
, ctx
->vbuf
,
517 0, sizeof(ctx
->vertices
), ctx
->vertices
);
519 util_draw_vertex_buffer(pipe
, ctx
->vbuf
, 0, PIPE_PRIM_TRIANGLE_FAN
,
521 2); /* attribs/vert */
525 void **blitter_get_sampler_state(struct blitter_context_priv
*ctx
,
528 struct pipe_context
*pipe
= ctx
->base
.pipe
;
529 struct pipe_sampler_state
*sampler_state
= &ctx
->template_sampler_state
;
531 assert(miplevel
< PIPE_MAX_TEXTURE_LEVELS
);
533 /* Create the sampler state on-demand. */
534 if (!ctx
->sampler_state
[miplevel
]) {
535 sampler_state
->lod_bias
= miplevel
;
536 sampler_state
->min_lod
= miplevel
;
537 sampler_state
->max_lod
= miplevel
;
539 ctx
->sampler_state
[miplevel
] = pipe
->create_sampler_state(pipe
,
543 /* Return void** so that it can be passed to bind_fragment_sampler_states
545 return &ctx
->sampler_state
[miplevel
];
549 void *blitter_get_fs_col(struct blitter_context_priv
*ctx
, unsigned num_cbufs
)
551 struct pipe_context
*pipe
= ctx
->base
.pipe
;
553 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
555 if (!ctx
->fs_col
[num_cbufs
])
556 ctx
->fs_col
[num_cbufs
] =
557 util_make_fragment_clonecolor_shader(pipe
, num_cbufs
);
559 return ctx
->fs_col
[num_cbufs
];
562 /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
564 pipe_tex_to_tgsi_tex(enum pipe_texture_target pipe_tex_target
)
566 switch (pipe_tex_target
) {
567 case PIPE_TEXTURE_1D
:
568 return TGSI_TEXTURE_1D
;
569 case PIPE_TEXTURE_2D
:
570 return TGSI_TEXTURE_2D
;
571 case PIPE_TEXTURE_3D
:
572 return TGSI_TEXTURE_3D
;
573 case PIPE_TEXTURE_CUBE
:
574 return TGSI_TEXTURE_CUBE
;
576 assert(0 && "unexpected texture target");
577 return TGSI_TEXTURE_UNKNOWN
;
583 void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
586 struct pipe_context
*pipe
= ctx
->base
.pipe
;
588 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
590 /* Create the fragment shader on-demand. */
591 if (!ctx
->fs_texfetch_col
[tex_target
]) {
592 unsigned tgsi_tex
= pipe_tex_to_tgsi_tex(tex_target
);
594 ctx
->fs_texfetch_col
[tex_target
] =
595 util_make_fragment_tex_shader(pipe
, tgsi_tex
, TGSI_INTERPOLATE_LINEAR
);
598 return ctx
->fs_texfetch_col
[tex_target
];
602 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
605 struct pipe_context
*pipe
= ctx
->base
.pipe
;
607 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
609 /* Create the fragment shader on-demand. */
610 if (!ctx
->fs_texfetch_depth
[tex_target
]) {
611 unsigned tgsi_tex
= pipe_tex_to_tgsi_tex(tex_target
);
613 ctx
->fs_texfetch_depth
[tex_target
] =
614 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
615 TGSI_INTERPOLATE_LINEAR
);
618 return ctx
->fs_texfetch_depth
[tex_target
];
621 static void blitter_draw_rectangle(struct blitter_context
*blitter
,
622 unsigned x1
, unsigned y1
,
623 unsigned x2
, unsigned y2
,
625 enum blitter_attrib_type type
,
626 const float attrib
[4])
628 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
631 case UTIL_BLITTER_ATTRIB_COLOR
:
632 blitter_set_clear_color(ctx
, attrib
);
635 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
636 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
642 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
643 blitter_draw_quad(ctx
);
646 void util_blitter_clear(struct blitter_context
*blitter
,
647 unsigned width
, unsigned height
,
649 unsigned clear_buffers
,
651 double depth
, unsigned stencil
)
653 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
654 struct pipe_context
*pipe
= ctx
->base
.pipe
;
655 struct pipe_stencil_ref sr
= { { 0 } };
657 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
659 blitter_check_saved_CSOs(ctx
);
662 if (clear_buffers
& PIPE_CLEAR_COLOR
)
663 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
665 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
667 if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
668 sr
.ref_value
[0] = stencil
& 0xff;
669 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
670 pipe
->set_stencil_ref(pipe
, &sr
);
672 else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
673 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
675 else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
676 sr
.ref_value
[0] = stencil
& 0xff;
677 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
678 pipe
->set_stencil_ref(pipe
, &sr
);
681 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
683 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
684 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
685 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, num_cbufs
));
686 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
688 blitter_set_dst_dimensions(ctx
, width
, height
);
689 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, depth
,
690 UTIL_BLITTER_ATTRIB_COLOR
, rgba
);
691 blitter_restore_CSOs(ctx
);
695 boolean
is_overlap(unsigned sx1
, unsigned sx2
, unsigned sy1
, unsigned sy2
,
696 unsigned dx1
, unsigned dx2
, unsigned dy1
, unsigned dy2
)
698 return sx1
< dx2
&& sx2
> dx1
&& sy1
< dy2
&& sy2
> dy1
;
701 void util_blitter_copy_region(struct blitter_context
*blitter
,
702 struct pipe_resource
*dst
,
703 struct pipe_subresource subdst
,
704 unsigned dstx
, unsigned dsty
, unsigned dstz
,
705 struct pipe_resource
*src
,
706 struct pipe_subresource subsrc
,
707 unsigned srcx
, unsigned srcy
, unsigned srcz
,
708 unsigned width
, unsigned height
,
709 boolean ignore_stencil
)
711 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
712 struct pipe_context
*pipe
= ctx
->base
.pipe
;
713 struct pipe_screen
*screen
= pipe
->screen
;
714 struct pipe_surface
*dstsurf
;
715 struct pipe_framebuffer_state fb_state
;
716 struct pipe_sampler_view viewTempl
, *view
;
718 boolean is_stencil
, is_depth
;
720 /* Give up if textures are not set. */
727 assert(!is_overlap(srcx
, srcx
+ width
, srcy
, srcy
+ height
,
728 dstx
, dstx
+ width
, dsty
, dsty
+ height
));
730 assert(dst
->format
== src
->format
);
732 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
734 /* Is this a ZS format? */
735 is_depth
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0;
736 is_stencil
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) != 0;
738 if (is_depth
|| is_stencil
)
739 bind
= PIPE_BIND_DEPTH_STENCIL
;
741 bind
= PIPE_BIND_RENDER_TARGET
;
743 /* Check if we can sample from and render to the surfaces. */
744 /* (assuming copying a stencil buffer is not possible) */
745 if ((!ignore_stencil
&& is_stencil
) ||
746 !screen
->is_format_supported(screen
, dst
->format
, dst
->target
,
747 dst
->nr_samples
, bind
, 0) ||
748 !screen
->is_format_supported(screen
, src
->format
, src
->target
,
749 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
, 0)) {
750 util_resource_copy_region(pipe
, dst
, subdst
, dstx
, dsty
, dstz
,
751 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
);
756 dstsurf
= screen
->get_tex_surface(screen
, dst
,
757 subdst
.face
, subdst
.level
, dstz
,
760 /* Check whether the states are properly saved. */
761 blitter_check_saved_CSOs(ctx
);
762 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
763 assert(blitter
->saved_num_sampler_views
!= ~0);
764 assert(blitter
->saved_num_sampler_states
!= ~0);
766 /* Initialize framebuffer state. */
767 fb_state
.width
= dstsurf
->width
;
768 fb_state
.height
= dstsurf
->height
;
771 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
772 pipe
->bind_depth_stencil_alpha_state(pipe
,
773 ctx
->dsa_write_depth_keep_stencil
);
774 pipe
->bind_fs_state(pipe
,
775 blitter_get_fs_texfetch_depth(ctx
, src
->target
));
777 fb_state
.nr_cbufs
= 0;
778 fb_state
.zsbuf
= dstsurf
;
780 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
781 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
782 pipe
->bind_fs_state(pipe
,
783 blitter_get_fs_texfetch_col(ctx
, src
->target
));
785 fb_state
.nr_cbufs
= 1;
786 fb_state
.cbufs
[0] = dstsurf
;
790 /* Initialize sampler view. */
791 u_sampler_view_default_template(&viewTempl
, src
, src
->format
);
792 view
= pipe
->create_sampler_view(pipe
, src
, &viewTempl
);
794 /* Set rasterizer state, shaders, and textures. */
795 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
796 pipe
->bind_vs_state(pipe
, ctx
->vs_tex
);
797 pipe
->bind_fragment_sampler_states(pipe
, 1,
798 blitter_get_sampler_state(ctx
, subsrc
.level
));
799 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
800 pipe
->set_fragment_sampler_views(pipe
, 1, &view
);
801 pipe
->set_framebuffer_state(pipe
, &fb_state
);
803 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
805 switch (src
->target
) {
806 /* Draw the quad with the draw_rectangle callback. */
807 case PIPE_TEXTURE_1D
:
808 case PIPE_TEXTURE_2D
:
810 /* Set texture coordinates. */
812 get_normalized_texcoords(src
, subsrc
, srcx
, srcy
,
813 srcx
+width
, srcy
+height
, coord
);
816 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
817 UTIL_BLITTER_ATTRIB_TEXCOORD
, coord
);
821 /* Draw the quad with the generic codepath. */
822 case PIPE_TEXTURE_3D
:
823 case PIPE_TEXTURE_CUBE
:
824 /* Set texture coordinates. */
825 if (src
->target
== PIPE_TEXTURE_3D
)
826 blitter_set_texcoords_3d(ctx
, src
, subsrc
, srcz
,
827 srcx
, srcy
, srcx
+width
, srcy
+height
);
829 blitter_set_texcoords_cube(ctx
, src
, subsrc
,
830 srcx
, srcy
, srcx
+width
, srcy
+height
);
833 blitter_set_rectangle(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0);
834 blitter_draw_quad(ctx
);
842 blitter_restore_CSOs(ctx
);
844 pipe_surface_reference(&dstsurf
, NULL
);
845 pipe_sampler_view_reference(&view
, NULL
);
848 /* Clear a region of a color surface to a constant value. */
849 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
850 struct pipe_surface
*dstsurf
,
852 unsigned dstx
, unsigned dsty
,
853 unsigned width
, unsigned height
)
855 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
856 struct pipe_context
*pipe
= ctx
->base
.pipe
;
857 struct pipe_framebuffer_state fb_state
;
859 assert(dstsurf
->texture
);
860 if (!dstsurf
->texture
)
863 /* check the saved state */
864 blitter_check_saved_CSOs(ctx
);
865 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
868 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
869 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
870 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
871 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1));
872 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
873 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
875 /* set a framebuffer state */
876 fb_state
.width
= dstsurf
->width
;
877 fb_state
.height
= dstsurf
->height
;
878 fb_state
.nr_cbufs
= 1;
879 fb_state
.cbufs
[0] = dstsurf
;
881 pipe
->set_framebuffer_state(pipe
, &fb_state
);
883 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
884 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
885 UTIL_BLITTER_ATTRIB_COLOR
, rgba
);
886 blitter_restore_CSOs(ctx
);
889 /* Clear a region of a depth stencil surface. */
890 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
891 struct pipe_surface
*dstsurf
,
892 unsigned clear_flags
,
895 unsigned dstx
, unsigned dsty
,
896 unsigned width
, unsigned height
)
898 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
899 struct pipe_context
*pipe
= ctx
->base
.pipe
;
900 struct pipe_framebuffer_state fb_state
;
901 struct pipe_stencil_ref sr
= { { 0 } };
903 assert(dstsurf
->texture
);
904 if (!dstsurf
->texture
)
907 /* check the saved state */
908 blitter_check_saved_CSOs(ctx
);
909 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
912 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
913 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
914 sr
.ref_value
[0] = stencil
& 0xff;
915 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
916 pipe
->set_stencil_ref(pipe
, &sr
);
918 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
919 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
921 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
922 sr
.ref_value
[0] = stencil
& 0xff;
923 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
924 pipe
->set_stencil_ref(pipe
, &sr
);
927 /* hmm that should be illegal probably, or make it a no-op somewhere */
928 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
930 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
931 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0));
932 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
933 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
935 /* set a framebuffer state */
936 fb_state
.width
= dstsurf
->width
;
937 fb_state
.height
= dstsurf
->height
;
938 fb_state
.nr_cbufs
= 0;
939 fb_state
.cbufs
[0] = 0;
940 fb_state
.zsbuf
= dstsurf
;
941 pipe
->set_framebuffer_state(pipe
, &fb_state
);
943 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
944 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, depth
,
945 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
946 blitter_restore_CSOs(ctx
);
949 /* Clear a region of a depth stencil surface. */
950 void util_blitter_flush_depth_stencil(struct blitter_context
*blitter
,
951 struct pipe_surface
*dstsurf
)
953 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
954 struct pipe_context
*pipe
= ctx
->base
.pipe
;
955 struct pipe_framebuffer_state fb_state
;
957 assert(dstsurf
->texture
);
958 if (!dstsurf
->texture
)
961 /* check the saved state */
962 blitter_check_saved_CSOs(ctx
);
963 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
966 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
967 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_flush_depth_stencil
);
969 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
970 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0));
971 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
972 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
974 /* set a framebuffer state */
975 fb_state
.width
= dstsurf
->width
;
976 fb_state
.height
= dstsurf
->height
;
977 fb_state
.nr_cbufs
= 0;
978 fb_state
.cbufs
[0] = 0;
979 fb_state
.zsbuf
= dstsurf
;
980 pipe
->set_framebuffer_state(pipe
, &fb_state
);
982 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
983 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
, 0,
984 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
985 blitter_restore_CSOs(ctx
);