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 blitter
;
57 struct pipe_context
*pipe
; /**< pipe context */
58 struct pipe_resource
*vbuf
; /**< quad */
60 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
62 /* Templates for various state objects. */
63 struct pipe_sampler_state template_sampler_state
;
65 /* Constant state objects. */
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.*/
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];
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
];
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
];
83 void *blend_write_color
; /**< blend state with writemask of RGBA */
84 void *blend_keep_color
; /**< blend state with writemask of 0 */
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
;
94 /* Sampler state for clamping to a miplevel. */
95 void *sampler_state
[PIPE_MAX_TEXTURE_LEVELS
];
97 /* Rasterizer state. */
100 struct pipe_sampler_view
*sampler_view
;
102 /* Viewport state. */
103 struct pipe_viewport_state viewport
;
106 struct pipe_clip_state clip
;
109 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
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];
119 ctx
= CALLOC_STRUCT(blitter_context_priv
);
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;
137 /* blend state objects */
138 memset(&blend
, 0, sizeof(blend
));
139 ctx
->blend_keep_color
= pipe
->create_blend_state(pipe
, &blend
);
141 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
142 ctx
->blend_write_color
= pipe
->create_blend_state(pipe
, &blend
);
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
);
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
);
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
);
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
);
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. */
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
);
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
;
195 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
197 /* fragment shaders are created on-demand */
201 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
202 TGSI_SEMANTIC_COLOR
};
203 const uint semantic_indices
[] = { 0, 0 };
205 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
209 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
210 TGSI_SEMANTIC_GENERIC
};
211 const uint semantic_indices
[] = { 0, 0 };
213 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
217 /* set invariant vertex coordinates */
218 for (i
= 0; i
< 4; i
++)
219 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
221 /* create the vertex buffer */
222 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
223 PIPE_BIND_VERTEX_BUFFER
,
224 sizeof(ctx
->vertices
));
226 return &ctx
->blitter
;
229 void util_blitter_destroy(struct blitter_context
*blitter
)
231 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
232 struct pipe_context
*pipe
= ctx
->pipe
;
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
);
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
);
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
]);
255 for (i
= 0; i
<= PIPE_MAX_COLOR_BUFS
&& ctx
->fs_col
[i
]; i
++)
257 pipe
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
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
]);
263 if (ctx
->sampler_view
) {
264 pipe_sampler_view_reference(&ctx
->sampler_view
, NULL
);
267 pipe_resource_reference(&ctx
->vbuf
, NULL
);
271 static void blitter_check_saved_CSOs(struct blitter_context_priv
*ctx
)
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
);
282 static void blitter_restore_CSOs(struct blitter_context_priv
*ctx
)
284 struct pipe_context
*pipe
= ctx
->pipe
;
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
);
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
;
302 pipe
->set_stencil_ref(pipe
, &ctx
->blitter
.saved_stencil_ref
);
304 pipe
->set_viewport_state(pipe
, &ctx
->blitter
.saved_viewport
);
305 pipe
->set_clip_state(pipe
, &ctx
->blitter
.saved_clip
);
307 /* restore the state objects which are required to be saved before copy/fill
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;
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;
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;
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
);
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
,
339 ctx
->blitter
.saved_num_vertex_buffers
= ~0;
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
,
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*/
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*/
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*/
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*/
364 for (i
= 0; i
< 4; i
++)
365 ctx
->vertices
[i
][0][2] = depth
; /*z*/
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
);
379 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip
);
382 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
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];
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;
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
)
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
);
416 ctx
->vertices
[0][1][0] = s1
; /*t0.s*/
417 ctx
->vertices
[0][1][1] = t1
; /*t0.t*/
419 ctx
->vertices
[1][1][0] = s2
; /*t1.s*/
420 ctx
->vertices
[1][1][1] = t1
; /*t1.t*/
422 ctx
->vertices
[2][1][0] = s2
; /*t2.s*/
423 ctx
->vertices
[2][1][1] = t2
; /*t2.t*/
425 ctx
->vertices
[3][1][0] = s1
; /*t3.s*/
426 ctx
->vertices
[3][1][1] = t2
; /*t3.t*/
428 for (i
= 0; i
< 4; i
++) {
429 ctx
->vertices
[i
][1][2] = 0; /*r*/
430 ctx
->vertices
[i
][1][3] = 1; /*q*/
434 static void blitter_set_texcoords_3d(struct blitter_context_priv
*ctx
,
435 struct pipe_resource
*src
,
436 struct pipe_subresource subsrc
,
438 unsigned x1
, unsigned y1
,
439 unsigned x2
, unsigned y2
)
442 float r
= zslice
/ (float)u_minify(src
->depth0
, subsrc
.level
);
444 blitter_set_texcoords_2d(ctx
, src
, subsrc
, x1
, y1
, x2
, y2
);
446 for (i
= 0; i
< 4; i
++)
447 ctx
->vertices
[i
][1][2] = r
; /*r*/
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
)
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
);
472 util_map_texcoords2d_onto_cubemap(subsrc
.face
,
473 /* pointer, stride in floats */
475 &ctx
->vertices
[0][1][0], 8);
477 for (i
= 0; i
< 4; i
++)
478 ctx
->vertices
[i
][1][3] = 1; /*q*/
481 static void blitter_draw_quad(struct blitter_context_priv
*ctx
)
483 struct pipe_context
*pipe
= ctx
->pipe
;
485 /* write vertices and draw them */
486 pipe_buffer_write(pipe
, ctx
->vbuf
,
487 0, sizeof(ctx
->vertices
), ctx
->vertices
);
489 util_draw_vertex_buffer(pipe
, ctx
->vbuf
, 0, PIPE_PRIM_TRIANGLE_FAN
,
491 2); /* attribs/vert */
495 void **blitter_get_sampler_state(struct blitter_context_priv
*ctx
,
498 struct pipe_context
*pipe
= ctx
->pipe
;
499 struct pipe_sampler_state
*sampler_state
= &ctx
->template_sampler_state
;
501 assert(miplevel
< PIPE_MAX_TEXTURE_LEVELS
);
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
;
509 ctx
->sampler_state
[miplevel
] = pipe
->create_sampler_state(pipe
,
513 /* Return void** so that it can be passed to bind_fragment_sampler_states
515 return &ctx
->sampler_state
[miplevel
];
519 void *blitter_get_fs_col(struct blitter_context_priv
*ctx
, unsigned num_cbufs
)
521 struct pipe_context
*pipe
= ctx
->pipe
;
523 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
525 if (!ctx
->fs_col
[num_cbufs
])
526 ctx
->fs_col
[num_cbufs
] =
527 util_make_fragment_clonecolor_shader(pipe
, num_cbufs
);
529 return ctx
->fs_col
[num_cbufs
];
532 /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
534 pipe_tex_to_tgsi_tex(unsigned pipe_tex_target
)
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
;
546 assert(0 && "unexpected texture target");
547 return TGSI_TEXTURE_UNKNOWN
;
553 void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
556 struct pipe_context
*pipe
= ctx
->pipe
;
558 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
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
);
564 ctx
->fs_texfetch_col
[tex_target
] =
565 util_make_fragment_tex_shader(pipe
, tgsi_tex
, TGSI_INTERPOLATE_LINEAR
);
568 return ctx
->fs_texfetch_col
[tex_target
];
572 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
575 struct pipe_context
*pipe
= ctx
->pipe
;
577 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
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
);
583 ctx
->fs_texfetch_depth
[tex_target
] =
584 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
585 TGSI_INTERPOLATE_LINEAR
);
588 return ctx
->fs_texfetch_depth
[tex_target
];
591 void util_blitter_clear(struct blitter_context
*blitter
,
592 unsigned width
, unsigned height
,
594 unsigned clear_buffers
,
596 double depth
, unsigned stencil
)
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 } };
602 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
604 blitter_check_saved_CSOs(ctx
);
607 if (clear_buffers
& PIPE_CLEAR_COLOR
)
608 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
610 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
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
);
617 else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
618 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
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
);
626 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
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
);
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
);
640 boolean
is_overlap(unsigned sx1
, unsigned sx2
, unsigned sy1
, unsigned sy2
,
641 unsigned dx1
, unsigned dx2
, unsigned dy1
, unsigned dy2
)
643 return sx1
< dx2
&& sx2
> dx1
&& sy1
< dy2
&& sy2
> dy1
;
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
)
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
;
663 boolean is_stencil
, is_depth
;
665 /* Give up if textures are not set. */
672 assert(!is_overlap(srcx
, srcx
+ width
, srcy
, srcy
+ height
,
673 dstx
, dstx
+ width
, dsty
, dsty
+ height
));
675 assert(dst
->format
== src
->format
);
677 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
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;
683 if (is_depth
|| is_stencil
)
684 bind
= PIPE_BIND_DEPTH_STENCIL
;
686 bind
= PIPE_BIND_RENDER_TARGET
;
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
);
701 dstsurf
= screen
->get_tex_surface(screen
, dst
,
702 subdst
.face
, subdst
.level
, dstz
,
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);
711 /* Initialize framebuffer state. */
712 fb_state
.width
= dstsurf
->width
;
713 fb_state
.height
= dstsurf
->height
;
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
));
722 fb_state
.nr_cbufs
= 0;
723 fb_state
.zsbuf
= dstsurf
;
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
));
730 fb_state
.nr_cbufs
= 1;
731 fb_state
.cbufs
[0] = dstsurf
;
735 /* Initialize sampler view. */
736 u_sampler_view_default_template(&viewTempl
, src
, src
->format
);
737 view
= pipe
->create_sampler_view(pipe
, src
, &viewTempl
);
739 if (ctx
->sampler_view
) {
740 pipe_sampler_view_reference(&ctx
->sampler_view
, NULL
);
742 ctx
->sampler_view
= view
;
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
);
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
);
760 case PIPE_TEXTURE_3D
:
761 blitter_set_texcoords_3d(ctx
, src
, subsrc
, srcz
,
762 srcx
, srcy
, srcx
+width
, srcy
+height
);
764 case PIPE_TEXTURE_CUBE
:
765 blitter_set_texcoords_cube(ctx
, src
, subsrc
,
766 srcx
, srcy
, srcx
+width
, srcy
+height
);
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
);
778 pipe_surface_reference(&dstsurf
, NULL
);
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
,
785 unsigned dstx
, unsigned dsty
,
786 unsigned width
, unsigned height
)
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
;
792 assert(dstsurf
->texture
);
793 if (!dstsurf
->texture
)
796 /* check the saved state */
797 blitter_check_saved_CSOs(ctx
);
798 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
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
);
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
;
814 pipe
->set_framebuffer_state(pipe
, &fb_state
);
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
);
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
,
828 unsigned dstx
, unsigned dsty
,
829 unsigned width
, unsigned height
)
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 } };
836 assert(dstsurf
->texture
);
837 if (!dstsurf
->texture
)
840 /* check the saved state */
841 blitter_check_saved_CSOs(ctx
);
842 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
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
);
851 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
852 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
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
);
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
);
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
);
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
);
876 blitter_set_rectangle(ctx
, 0, 0, width
, height
, dstsurf
->width
, dstsurf
->height
, depth
);
877 blitter_draw_quad(ctx
);
878 blitter_restore_CSOs(ctx
);