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 /* Viewport state. */
101 struct pipe_viewport_state viewport
;
104 struct pipe_clip_state clip
;
107 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
109 struct blitter_context_priv
*ctx
;
110 struct pipe_blend_state blend
;
111 struct pipe_depth_stencil_alpha_state dsa
;
112 struct pipe_rasterizer_state rs_state
;
113 struct pipe_sampler_state
*sampler_state
;
114 struct pipe_vertex_element velem
[2];
117 ctx
= CALLOC_STRUCT(blitter_context_priv
);
123 /* init state objects for them to be considered invalid */
124 ctx
->blitter
.saved_blend_state
= INVALID_PTR
;
125 ctx
->blitter
.saved_dsa_state
= INVALID_PTR
;
126 ctx
->blitter
.saved_rs_state
= INVALID_PTR
;
127 ctx
->blitter
.saved_fs
= INVALID_PTR
;
128 ctx
->blitter
.saved_vs
= INVALID_PTR
;
129 ctx
->blitter
.saved_velem_state
= INVALID_PTR
;
130 ctx
->blitter
.saved_fb_state
.nr_cbufs
= ~0;
131 ctx
->blitter
.saved_num_sampler_views
= ~0;
132 ctx
->blitter
.saved_num_sampler_states
= ~0;
133 ctx
->blitter
.saved_num_vertex_buffers
= ~0;
135 /* blend state objects */
136 memset(&blend
, 0, sizeof(blend
));
137 ctx
->blend_keep_color
= pipe
->create_blend_state(pipe
, &blend
);
139 blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
140 ctx
->blend_write_color
= pipe
->create_blend_state(pipe
, &blend
);
142 /* depth stencil alpha state objects */
143 memset(&dsa
, 0, sizeof(dsa
));
144 ctx
->dsa_keep_depth_stencil
=
145 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
147 dsa
.depth
.enabled
= 1;
148 dsa
.depth
.writemask
= 1;
149 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
150 ctx
->dsa_write_depth_keep_stencil
=
151 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
153 dsa
.stencil
[0].enabled
= 1;
154 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
155 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
156 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
157 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
158 dsa
.stencil
[0].valuemask
= 0xff;
159 dsa
.stencil
[0].writemask
= 0xff;
160 ctx
->dsa_write_depth_stencil
=
161 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
164 dsa
.depth
.enabled
= 0;
165 dsa
.depth
.writemask
= 0;
166 ctx
->dsa_keep_depth_write_stencil
=
167 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
170 sampler_state
= &ctx
->template_sampler_state
;
171 sampler_state
->wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
172 sampler_state
->wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
173 sampler_state
->wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
174 sampler_state
->normalized_coords
= TRUE
;
175 /* The sampler state objects which sample from a specified mipmap level
176 * are created on-demand. */
178 /* rasterizer state */
179 memset(&rs_state
, 0, sizeof(rs_state
));
180 rs_state
.cull_face
= PIPE_FACE_NONE
;
181 rs_state
.gl_rasterization_rules
= 1;
182 rs_state
.flatshade
= 1;
183 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
185 /* vertex elements state */
186 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
187 for (i
= 0; i
< 2; i
++) {
188 velem
[i
].src_offset
= i
* 4 * sizeof(float);
189 velem
[i
].instance_divisor
= 0;
190 velem
[i
].vertex_buffer_index
= 0;
191 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
193 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
195 /* fragment shaders are created on-demand */
199 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
200 TGSI_SEMANTIC_COLOR
};
201 const uint semantic_indices
[] = { 0, 0 };
203 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
207 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
208 TGSI_SEMANTIC_GENERIC
};
209 const uint semantic_indices
[] = { 0, 0 };
211 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
215 /* set invariant vertex coordinates */
216 for (i
= 0; i
< 4; i
++)
217 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
219 /* create the vertex buffer */
220 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
221 PIPE_BIND_VERTEX_BUFFER
,
222 sizeof(ctx
->vertices
));
224 return &ctx
->blitter
;
227 void util_blitter_destroy(struct blitter_context
*blitter
)
229 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
230 struct pipe_context
*pipe
= ctx
->pipe
;
233 pipe
->delete_blend_state(pipe
, ctx
->blend_write_color
);
234 pipe
->delete_blend_state(pipe
, ctx
->blend_keep_color
);
235 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
236 pipe
->delete_depth_stencil_alpha_state(pipe
,
237 ctx
->dsa_write_depth_keep_stencil
);
238 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
239 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
241 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
242 pipe
->delete_vs_state(pipe
, ctx
->vs_col
);
243 pipe
->delete_vs_state(pipe
, ctx
->vs_tex
);
244 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
246 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
247 if (ctx
->fs_texfetch_col
[i
])
248 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
249 if (ctx
->fs_texfetch_depth
[i
])
250 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
253 for (i
= 0; i
<= PIPE_MAX_COLOR_BUFS
&& ctx
->fs_col
[i
]; i
++)
255 pipe
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
257 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
258 if (ctx
->sampler_state
[i
])
259 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
[i
]);
261 pipe_resource_reference(&ctx
->vbuf
, NULL
);
265 static void blitter_check_saved_CSOs(struct blitter_context_priv
*ctx
)
267 /* make sure these CSOs have been saved */
268 assert(ctx
->blitter
.saved_blend_state
!= INVALID_PTR
&&
269 ctx
->blitter
.saved_dsa_state
!= INVALID_PTR
&&
270 ctx
->blitter
.saved_rs_state
!= INVALID_PTR
&&
271 ctx
->blitter
.saved_fs
!= INVALID_PTR
&&
272 ctx
->blitter
.saved_vs
!= INVALID_PTR
&&
273 ctx
->blitter
.saved_velem_state
!= INVALID_PTR
);
276 static void blitter_restore_CSOs(struct blitter_context_priv
*ctx
)
278 struct pipe_context
*pipe
= ctx
->pipe
;
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
);
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
;
296 pipe
->set_stencil_ref(pipe
, &ctx
->blitter
.saved_stencil_ref
);
298 pipe
->set_viewport_state(pipe
, &ctx
->blitter
.saved_viewport
);
299 pipe
->set_clip_state(pipe
, &ctx
->blitter
.saved_clip
);
301 /* restore the state objects which are required to be saved before copy/fill
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;
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;
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;
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
);
327 for (i
= 0; i
< ctx
->blitter
.saved_num_vertex_buffers
; i
++) {
328 if (ctx
->blitter
.saved_vertex_buffers
[i
].buffer
) {
329 pipe_resource_reference(&ctx
->blitter
.saved_vertex_buffers
[i
].buffer
,
333 ctx
->blitter
.saved_num_vertex_buffers
= ~0;
337 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
338 unsigned x1
, unsigned y1
,
339 unsigned x2
, unsigned y2
,
340 unsigned width
, unsigned height
,
345 /* set vertex positions */
346 ctx
->vertices
[0][0][0] = (float)x1
/ width
* 2.0f
- 1.0f
; /*v0.x*/
347 ctx
->vertices
[0][0][1] = (float)y1
/ height
* 2.0f
- 1.0f
; /*v0.y*/
349 ctx
->vertices
[1][0][0] = (float)x2
/ width
* 2.0f
- 1.0f
; /*v1.x*/
350 ctx
->vertices
[1][0][1] = (float)y1
/ height
* 2.0f
- 1.0f
; /*v1.y*/
352 ctx
->vertices
[2][0][0] = (float)x2
/ width
* 2.0f
- 1.0f
; /*v2.x*/
353 ctx
->vertices
[2][0][1] = (float)y2
/ height
* 2.0f
- 1.0f
; /*v2.y*/
355 ctx
->vertices
[3][0][0] = (float)x1
/ width
* 2.0f
- 1.0f
; /*v3.x*/
356 ctx
->vertices
[3][0][1] = (float)y2
/ height
* 2.0f
- 1.0f
; /*v3.y*/
358 for (i
= 0; i
< 4; i
++)
359 ctx
->vertices
[i
][0][2] = depth
; /*z*/
362 ctx
->viewport
.scale
[0] = 0.5f
* width
;
363 ctx
->viewport
.scale
[1] = 0.5f
* height
;
364 ctx
->viewport
.scale
[2] = 1.0f
;
365 ctx
->viewport
.scale
[3] = 1.0f
;
366 ctx
->viewport
.translate
[0] = 0.5f
* width
;
367 ctx
->viewport
.translate
[1] = 0.5f
* height
;
368 ctx
->viewport
.translate
[2] = 0.0f
;
369 ctx
->viewport
.translate
[3] = 0.0f
;
370 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->viewport
);
373 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip
);
376 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
382 for (i
= 0; i
< 4; i
++) {
383 ctx
->vertices
[i
][1][0] = rgba
[0];
384 ctx
->vertices
[i
][1][1] = rgba
[1];
385 ctx
->vertices
[i
][1][2] = rgba
[2];
386 ctx
->vertices
[i
][1][3] = rgba
[3];
389 for (i
= 0; i
< 4; i
++) {
390 ctx
->vertices
[i
][1][0] = 0;
391 ctx
->vertices
[i
][1][1] = 0;
392 ctx
->vertices
[i
][1][2] = 0;
393 ctx
->vertices
[i
][1][3] = 0;
398 static void blitter_set_texcoords_2d(struct blitter_context_priv
*ctx
,
399 struct pipe_resource
*src
,
400 struct pipe_subresource subsrc
,
401 unsigned x1
, unsigned y1
,
402 unsigned x2
, unsigned y2
)
405 float s1
= x1
/ (float)u_minify(src
->width0
, subsrc
.level
);
406 float t1
= y1
/ (float)u_minify(src
->height0
, subsrc
.level
);
407 float s2
= x2
/ (float)u_minify(src
->width0
, subsrc
.level
);
408 float t2
= y2
/ (float)u_minify(src
->height0
, subsrc
.level
);
410 ctx
->vertices
[0][1][0] = s1
; /*t0.s*/
411 ctx
->vertices
[0][1][1] = t1
; /*t0.t*/
413 ctx
->vertices
[1][1][0] = s2
; /*t1.s*/
414 ctx
->vertices
[1][1][1] = t1
; /*t1.t*/
416 ctx
->vertices
[2][1][0] = s2
; /*t2.s*/
417 ctx
->vertices
[2][1][1] = t2
; /*t2.t*/
419 ctx
->vertices
[3][1][0] = s1
; /*t3.s*/
420 ctx
->vertices
[3][1][1] = t2
; /*t3.t*/
422 for (i
= 0; i
< 4; i
++) {
423 ctx
->vertices
[i
][1][2] = 0; /*r*/
424 ctx
->vertices
[i
][1][3] = 1; /*q*/
428 static void blitter_set_texcoords_3d(struct blitter_context_priv
*ctx
,
429 struct pipe_resource
*src
,
430 struct pipe_subresource subsrc
,
432 unsigned x1
, unsigned y1
,
433 unsigned x2
, unsigned y2
)
436 float r
= zslice
/ (float)u_minify(src
->depth0
, subsrc
.level
);
438 blitter_set_texcoords_2d(ctx
, src
, subsrc
, x1
, y1
, x2
, y2
);
440 for (i
= 0; i
< 4; i
++)
441 ctx
->vertices
[i
][1][2] = r
; /*r*/
444 static void blitter_set_texcoords_cube(struct blitter_context_priv
*ctx
,
445 struct pipe_resource
*src
,
446 struct pipe_subresource subsrc
,
447 unsigned x1
, unsigned y1
,
448 unsigned x2
, unsigned y2
)
451 float s1
= x1
/ (float)u_minify(src
->width0
, subsrc
.level
);
452 float t1
= y1
/ (float)u_minify(src
->height0
, subsrc
.level
);
453 float s2
= x2
/ (float)u_minify(src
->width0
, subsrc
.level
);
454 float t2
= y2
/ (float)u_minify(src
->height0
, subsrc
.level
);
466 util_map_texcoords2d_onto_cubemap(subsrc
.face
,
467 /* pointer, stride in floats */
469 &ctx
->vertices
[0][1][0], 8);
471 for (i
= 0; i
< 4; i
++)
472 ctx
->vertices
[i
][1][3] = 1; /*q*/
475 static void blitter_draw_quad(struct blitter_context_priv
*ctx
)
477 struct pipe_context
*pipe
= ctx
->pipe
;
479 /* write vertices and draw them */
480 pipe_buffer_write(pipe
, ctx
->vbuf
,
481 0, sizeof(ctx
->vertices
), ctx
->vertices
);
483 util_draw_vertex_buffer(pipe
, ctx
->vbuf
, 0, PIPE_PRIM_TRIANGLE_FAN
,
485 2); /* attribs/vert */
489 void **blitter_get_sampler_state(struct blitter_context_priv
*ctx
,
492 struct pipe_context
*pipe
= ctx
->pipe
;
493 struct pipe_sampler_state
*sampler_state
= &ctx
->template_sampler_state
;
495 assert(miplevel
< PIPE_MAX_TEXTURE_LEVELS
);
497 /* Create the sampler state on-demand. */
498 if (!ctx
->sampler_state
[miplevel
]) {
499 sampler_state
->lod_bias
= miplevel
;
500 sampler_state
->min_lod
= miplevel
;
501 sampler_state
->max_lod
= miplevel
;
503 ctx
->sampler_state
[miplevel
] = pipe
->create_sampler_state(pipe
,
507 /* Return void** so that it can be passed to bind_fragment_sampler_states
509 return &ctx
->sampler_state
[miplevel
];
513 void *blitter_get_fs_col(struct blitter_context_priv
*ctx
, unsigned num_cbufs
)
515 struct pipe_context
*pipe
= ctx
->pipe
;
517 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
519 if (!ctx
->fs_col
[num_cbufs
])
520 ctx
->fs_col
[num_cbufs
] =
521 util_make_fragment_clonecolor_shader(pipe
, num_cbufs
);
523 return ctx
->fs_col
[num_cbufs
];
526 /** Convert PIPE_TEXTURE_x to TGSI_TEXTURE_x */
528 pipe_tex_to_tgsi_tex(unsigned pipe_tex_target
)
530 switch (pipe_tex_target
) {
531 case PIPE_TEXTURE_1D
:
532 return TGSI_TEXTURE_1D
;
533 case PIPE_TEXTURE_2D
:
534 return TGSI_TEXTURE_2D
;
535 case PIPE_TEXTURE_3D
:
536 return TGSI_TEXTURE_3D
;
537 case PIPE_TEXTURE_CUBE
:
538 return TGSI_TEXTURE_CUBE
;
540 assert(0 && "unexpected texture target");
541 return TGSI_TEXTURE_UNKNOWN
;
547 void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
550 struct pipe_context
*pipe
= ctx
->pipe
;
552 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
554 /* Create the fragment shader on-demand. */
555 if (!ctx
->fs_texfetch_col
[tex_target
]) {
556 unsigned tgsi_tex
= pipe_tex_to_tgsi_tex(tex_target
);
558 ctx
->fs_texfetch_col
[tex_target
] =
559 util_make_fragment_tex_shader(pipe
, tgsi_tex
, TGSI_INTERPOLATE_LINEAR
);
562 return ctx
->fs_texfetch_col
[tex_target
];
566 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
569 struct pipe_context
*pipe
= ctx
->pipe
;
571 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
573 /* Create the fragment shader on-demand. */
574 if (!ctx
->fs_texfetch_depth
[tex_target
]) {
575 unsigned tgsi_tex
= pipe_tex_to_tgsi_tex(tex_target
);
577 ctx
->fs_texfetch_depth
[tex_target
] =
578 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
579 TGSI_INTERPOLATE_LINEAR
);
582 return ctx
->fs_texfetch_depth
[tex_target
];
585 void util_blitter_clear(struct blitter_context
*blitter
,
586 unsigned width
, unsigned height
,
588 unsigned clear_buffers
,
590 double depth
, unsigned stencil
)
592 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
593 struct pipe_context
*pipe
= ctx
->pipe
;
594 struct pipe_stencil_ref sr
= { { 0 } };
596 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
598 blitter_check_saved_CSOs(ctx
);
601 if (clear_buffers
& PIPE_CLEAR_COLOR
)
602 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
604 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
606 if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
607 sr
.ref_value
[0] = stencil
& 0xff;
608 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
609 pipe
->set_stencil_ref(pipe
, &sr
);
611 else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
612 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
614 else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
615 sr
.ref_value
[0] = stencil
& 0xff;
616 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
617 pipe
->set_stencil_ref(pipe
, &sr
);
620 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
622 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
623 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
624 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, num_cbufs
));
625 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
627 blitter_set_clear_color(ctx
, rgba
);
628 blitter_set_rectangle(ctx
, 0, 0, width
, height
, width
, height
, depth
);
629 blitter_draw_quad(ctx
);
630 blitter_restore_CSOs(ctx
);
634 boolean
is_overlap(unsigned sx1
, unsigned sx2
, unsigned sy1
, unsigned sy2
,
635 unsigned dx1
, unsigned dx2
, unsigned dy1
, unsigned dy2
)
637 return sx1
< dx2
&& sx2
> dx1
&& sy1
< dy2
&& sy2
> dy1
;
640 void util_blitter_copy_region(struct blitter_context
*blitter
,
641 struct pipe_resource
*dst
,
642 struct pipe_subresource subdst
,
643 unsigned dstx
, unsigned dsty
, unsigned dstz
,
644 struct pipe_resource
*src
,
645 struct pipe_subresource subsrc
,
646 unsigned srcx
, unsigned srcy
, unsigned srcz
,
647 unsigned width
, unsigned height
,
648 boolean ignore_stencil
)
650 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
651 struct pipe_context
*pipe
= ctx
->pipe
;
652 struct pipe_screen
*screen
= pipe
->screen
;
653 struct pipe_surface
*dstsurf
;
654 struct pipe_framebuffer_state fb_state
;
655 struct pipe_sampler_view viewTempl
, *view
;
657 boolean is_stencil
, is_depth
;
659 /* Give up if textures are not set. */
666 assert(!is_overlap(srcx
, srcx
+ width
, srcy
, srcy
+ height
,
667 dstx
, dstx
+ width
, dsty
, dsty
+ height
));
669 assert(dst
->format
== src
->format
);
671 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
673 /* Is this a ZS format? */
674 is_depth
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0;
675 is_stencil
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) != 0;
677 if (is_depth
|| is_stencil
)
678 bind
= PIPE_BIND_DEPTH_STENCIL
;
680 bind
= PIPE_BIND_RENDER_TARGET
;
682 /* Check if we can sample from and render to the surfaces. */
683 /* (assuming copying a stencil buffer is not possible) */
684 if ((!ignore_stencil
&& is_stencil
) ||
685 !screen
->is_format_supported(screen
, dst
->format
, dst
->target
,
686 dst
->nr_samples
, bind
, 0) ||
687 !screen
->is_format_supported(screen
, src
->format
, src
->target
,
688 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
, 0)) {
689 util_resource_copy_region(pipe
, dst
, subdst
, dstx
, dsty
, dstz
,
690 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
);
695 dstsurf
= screen
->get_tex_surface(screen
, dst
,
696 subdst
.face
, subdst
.level
, dstz
,
699 /* Check whether the states are properly saved. */
700 blitter_check_saved_CSOs(ctx
);
701 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
702 assert(blitter
->saved_num_sampler_views
!= ~0);
703 assert(blitter
->saved_num_sampler_states
!= ~0);
705 /* Initialize framebuffer state. */
706 fb_state
.width
= dstsurf
->width
;
707 fb_state
.height
= dstsurf
->height
;
710 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
711 pipe
->bind_depth_stencil_alpha_state(pipe
,
712 ctx
->dsa_write_depth_keep_stencil
);
713 pipe
->bind_fs_state(pipe
,
714 blitter_get_fs_texfetch_depth(ctx
, src
->target
));
716 fb_state
.nr_cbufs
= 0;
717 fb_state
.zsbuf
= dstsurf
;
719 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
720 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
721 pipe
->bind_fs_state(pipe
,
722 blitter_get_fs_texfetch_col(ctx
, src
->target
));
724 fb_state
.nr_cbufs
= 1;
725 fb_state
.cbufs
[0] = dstsurf
;
729 /* Initialize sampler view. */
730 u_sampler_view_default_template(&viewTempl
, src
, src
->format
);
731 view
= pipe
->create_sampler_view(pipe
, src
, &viewTempl
);
733 /* Set rasterizer state, shaders, and textures. */
734 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
735 pipe
->bind_vs_state(pipe
, ctx
->vs_tex
);
736 pipe
->bind_fragment_sampler_states(pipe
, 1,
737 blitter_get_sampler_state(ctx
, subsrc
.level
));
738 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
739 pipe
->set_fragment_sampler_views(pipe
, 1, &view
);
740 pipe
->set_framebuffer_state(pipe
, &fb_state
);
742 /* Set texture coordinates. */
743 switch (src
->target
) {
744 case PIPE_TEXTURE_1D
:
745 case PIPE_TEXTURE_2D
:
746 blitter_set_texcoords_2d(ctx
, src
, subsrc
,
747 srcx
, srcy
, srcx
+width
, srcy
+height
);
749 case PIPE_TEXTURE_3D
:
750 blitter_set_texcoords_3d(ctx
, src
, subsrc
, srcz
,
751 srcx
, srcy
, srcx
+width
, srcy
+height
);
753 case PIPE_TEXTURE_CUBE
:
754 blitter_set_texcoords_cube(ctx
, src
, subsrc
,
755 srcx
, srcy
, srcx
+width
, srcy
+height
);
762 blitter_set_rectangle(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
,
763 dstsurf
->width
, dstsurf
->height
, 0);
764 blitter_draw_quad(ctx
);
765 blitter_restore_CSOs(ctx
);
767 pipe_surface_reference(&dstsurf
, NULL
);
768 pipe_sampler_view_reference(&view
, NULL
);
771 /* Clear a region of a color surface to a constant value. */
772 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
773 struct pipe_surface
*dstsurf
,
775 unsigned dstx
, unsigned dsty
,
776 unsigned width
, unsigned height
)
778 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
779 struct pipe_context
*pipe
= ctx
->pipe
;
780 struct pipe_framebuffer_state fb_state
;
782 assert(dstsurf
->texture
);
783 if (!dstsurf
->texture
)
786 /* check the saved state */
787 blitter_check_saved_CSOs(ctx
);
788 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
791 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
792 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
793 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
794 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1));
795 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
796 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
798 /* set a framebuffer state */
799 fb_state
.width
= dstsurf
->width
;
800 fb_state
.height
= dstsurf
->height
;
801 fb_state
.nr_cbufs
= 1;
802 fb_state
.cbufs
[0] = dstsurf
;
804 pipe
->set_framebuffer_state(pipe
, &fb_state
);
806 blitter_set_clear_color(ctx
, rgba
);
807 blitter_set_rectangle(ctx
, 0, 0, width
, height
, dstsurf
->width
, dstsurf
->height
, 0);
808 blitter_draw_quad(ctx
);
809 blitter_restore_CSOs(ctx
);
812 /* Clear a region of a depth stencil surface. */
813 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
814 struct pipe_surface
*dstsurf
,
815 unsigned clear_flags
,
818 unsigned dstx
, unsigned dsty
,
819 unsigned width
, unsigned height
)
821 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
822 struct pipe_context
*pipe
= ctx
->pipe
;
823 struct pipe_framebuffer_state fb_state
;
824 struct pipe_stencil_ref sr
= { { 0 } };
826 assert(dstsurf
->texture
);
827 if (!dstsurf
->texture
)
830 /* check the saved state */
831 blitter_check_saved_CSOs(ctx
);
832 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
835 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
836 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
837 sr
.ref_value
[0] = stencil
& 0xff;
838 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
839 pipe
->set_stencil_ref(pipe
, &sr
);
841 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
842 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
844 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
845 sr
.ref_value
[0] = stencil
& 0xff;
846 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
847 pipe
->set_stencil_ref(pipe
, &sr
);
850 /* hmm that should be illegal probably, or make it a no-op somewhere */
851 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
853 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
854 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0));
855 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
856 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
858 /* set a framebuffer state */
859 fb_state
.width
= dstsurf
->width
;
860 fb_state
.height
= dstsurf
->height
;
861 fb_state
.nr_cbufs
= 0;
862 fb_state
.cbufs
[0] = 0;
863 fb_state
.zsbuf
= dstsurf
;
864 pipe
->set_framebuffer_state(pipe
, &fb_state
);
866 blitter_set_rectangle(ctx
, 0, 0, width
, height
, dstsurf
->width
, dstsurf
->height
, depth
);
867 blitter_draw_quad(ctx
);
868 blitter_restore_CSOs(ctx
);