1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
30 * Copy/blit pixel rect between surfaces
36 #include "pipe/p_context.h"
37 #include "util/u_debug.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_inlines.h"
40 #include "pipe/p_shader_tokens.h"
41 #include "pipe/p_state.h"
43 #include "util/u_blit.h"
44 #include "util/u_draw_quad.h"
45 #include "util/u_math.h"
46 #include "util/u_memory.h"
47 #include "util/u_simple_shaders.h"
49 #include "cso_cache/cso_context.h"
54 struct pipe_context
*pipe
;
55 struct cso_context
*cso
;
57 struct pipe_blend_state blend
;
58 struct pipe_depth_stencil_alpha_state depthstencil
;
59 struct pipe_rasterizer_state rasterizer
;
60 struct pipe_sampler_state sampler
;
61 struct pipe_viewport_state viewport
;
63 struct pipe_shader_state vert_shader
;
64 struct pipe_shader_state frag_shader
;
68 struct pipe_buffer
*vbuf
; /**< quad vertices */
71 float vertices
[4][2][4]; /**< vertex/texcoords for quad */
76 * Create state object for blit.
77 * Intended to be created once and re-used for many blit() calls.
80 util_create_blit(struct pipe_context
*pipe
, struct cso_context
*cso
)
82 struct blit_state
*ctx
;
85 ctx
= CALLOC_STRUCT(blit_state
);
92 /* disabled blending/masking */
93 memset(&ctx
->blend
, 0, sizeof(ctx
->blend
));
94 ctx
->blend
.rgb_src_factor
= PIPE_BLENDFACTOR_ONE
;
95 ctx
->blend
.alpha_src_factor
= PIPE_BLENDFACTOR_ONE
;
96 ctx
->blend
.rgb_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
97 ctx
->blend
.alpha_dst_factor
= PIPE_BLENDFACTOR_ZERO
;
98 ctx
->blend
.colormask
= PIPE_MASK_RGBA
;
100 /* no-op depth/stencil/alpha */
101 memset(&ctx
->depthstencil
, 0, sizeof(ctx
->depthstencil
));
104 memset(&ctx
->rasterizer
, 0, sizeof(ctx
->rasterizer
));
105 ctx
->rasterizer
.front_winding
= PIPE_WINDING_CW
;
106 ctx
->rasterizer
.cull_mode
= PIPE_WINDING_NONE
;
107 ctx
->rasterizer
.bypass_clipping
= 1;
108 /*ctx->rasterizer.bypass_vs = 1;*/
109 ctx
->rasterizer
.gl_rasterization_rules
= 1;
112 memset(&ctx
->sampler
, 0, sizeof(ctx
->sampler
));
113 ctx
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
114 ctx
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
115 ctx
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
116 ctx
->sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
117 ctx
->sampler
.min_img_filter
= 0; /* set later */
118 ctx
->sampler
.mag_img_filter
= 0; /* set later */
119 ctx
->sampler
.normalized_coords
= 1;
121 /* viewport (identity, we setup vertices in wincoords) */
122 ctx
->viewport
.scale
[0] = 1.0;
123 ctx
->viewport
.scale
[1] = 1.0;
124 ctx
->viewport
.scale
[2] = 1.0;
125 ctx
->viewport
.scale
[3] = 1.0;
126 ctx
->viewport
.translate
[0] = 0.0;
127 ctx
->viewport
.translate
[1] = 0.0;
128 ctx
->viewport
.translate
[2] = 0.0;
129 ctx
->viewport
.translate
[3] = 0.0;
133 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
134 TGSI_SEMANTIC_GENERIC
};
135 const uint semantic_indexes
[] = { 0, 0 };
136 ctx
->vs
= util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
141 /* fragment shader */
142 ctx
->fs
= util_make_fragment_tex_shader(pipe
, &ctx
->frag_shader
);
145 /* init vertex data that doesn't change */
146 for (i
= 0; i
< 4; i
++) {
147 ctx
->vertices
[i
][0][3] = 1.0f
; /* w */
148 ctx
->vertices
[i
][1][2] = 0.0f
; /* r */
149 ctx
->vertices
[i
][1][3] = 1.0f
; /* q */
157 * Destroy a blit context
160 util_destroy_blit(struct blit_state
*ctx
)
162 struct pipe_context
*pipe
= ctx
->pipe
;
164 pipe
->delete_vs_state(pipe
, ctx
->vs
);
165 pipe
->delete_fs_state(pipe
, ctx
->fs
);
167 FREE((void*) ctx
->vert_shader
.tokens
);
168 FREE((void*) ctx
->frag_shader
.tokens
);
170 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
176 static unsigned get_next_slot( struct blit_state
*ctx
)
178 const unsigned max_slots
= 4096 / sizeof ctx
->vertices
;
180 if (ctx
->vbuf_slot
>= max_slots
)
181 util_blit_flush( ctx
);
184 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
186 PIPE_BUFFER_USAGE_VERTEX
,
187 max_slots
* sizeof ctx
->vertices
);
190 return ctx
->vbuf_slot
++ * sizeof ctx
->vertices
;
196 * Setup vertex data for the textured quad we'll draw.
200 setup_vertex_data(struct blit_state
*ctx
,
201 float x0
, float y0
, float x1
, float y1
, float z
)
205 ctx
->vertices
[0][0][0] = x0
;
206 ctx
->vertices
[0][0][1] = y0
;
207 ctx
->vertices
[0][0][2] = z
;
208 ctx
->vertices
[0][1][0] = 0.0f
; /*s*/
209 ctx
->vertices
[0][1][1] = 0.0f
; /*t*/
211 ctx
->vertices
[1][0][0] = x1
;
212 ctx
->vertices
[1][0][1] = y0
;
213 ctx
->vertices
[1][0][2] = z
;
214 ctx
->vertices
[1][1][0] = 1.0f
; /*s*/
215 ctx
->vertices
[1][1][1] = 0.0f
; /*t*/
217 ctx
->vertices
[2][0][0] = x1
;
218 ctx
->vertices
[2][0][1] = y1
;
219 ctx
->vertices
[2][0][2] = z
;
220 ctx
->vertices
[2][1][0] = 1.0f
;
221 ctx
->vertices
[2][1][1] = 1.0f
;
223 ctx
->vertices
[3][0][0] = x0
;
224 ctx
->vertices
[3][0][1] = y1
;
225 ctx
->vertices
[3][0][2] = z
;
226 ctx
->vertices
[3][1][0] = 0.0f
;
227 ctx
->vertices
[3][1][1] = 1.0f
;
229 offset
= get_next_slot( ctx
);
231 pipe_buffer_write(ctx
->pipe
->screen
, ctx
->vbuf
,
232 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
239 * Setup vertex data for the textured quad we'll draw.
243 setup_vertex_data_tex(struct blit_state
*ctx
,
244 float x0
, float y0
, float x1
, float y1
,
245 float s0
, float t0
, float s1
, float t1
,
250 ctx
->vertices
[0][0][0] = x0
;
251 ctx
->vertices
[0][0][1] = y0
;
252 ctx
->vertices
[0][0][2] = z
;
253 ctx
->vertices
[0][1][0] = s0
; /*s*/
254 ctx
->vertices
[0][1][1] = t0
; /*t*/
256 ctx
->vertices
[1][0][0] = x1
;
257 ctx
->vertices
[1][0][1] = y0
;
258 ctx
->vertices
[1][0][2] = z
;
259 ctx
->vertices
[1][1][0] = s1
; /*s*/
260 ctx
->vertices
[1][1][1] = t0
; /*t*/
262 ctx
->vertices
[2][0][0] = x1
;
263 ctx
->vertices
[2][0][1] = y1
;
264 ctx
->vertices
[2][0][2] = z
;
265 ctx
->vertices
[2][1][0] = s1
;
266 ctx
->vertices
[2][1][1] = t1
;
268 ctx
->vertices
[3][0][0] = x0
;
269 ctx
->vertices
[3][0][1] = y1
;
270 ctx
->vertices
[3][0][2] = z
;
271 ctx
->vertices
[3][1][0] = s0
;
272 ctx
->vertices
[3][1][1] = t1
;
274 offset
= get_next_slot( ctx
);
276 pipe_buffer_write(ctx
->pipe
->screen
, ctx
->vbuf
,
277 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
282 * Copy pixel block from src surface to dst surface.
283 * Overlapping regions are acceptable.
284 * XXX need some control over blitting Z and/or stencil.
287 util_blit_pixels(struct blit_state
*ctx
,
288 struct pipe_surface
*src
,
289 int srcX0
, int srcY0
,
290 int srcX1
, int srcY1
,
291 struct pipe_surface
*dst
,
292 int dstX0
, int dstY0
,
293 int dstX1
, int dstY1
,
294 float z
, uint filter
)
296 struct pipe_context
*pipe
= ctx
->pipe
;
297 struct pipe_screen
*screen
= pipe
->screen
;
298 struct pipe_texture texTemp
, *tex
;
299 struct pipe_surface
*texSurf
;
300 struct pipe_framebuffer_state fb
;
301 const int srcW
= abs(srcX1
- srcX0
);
302 const int srcH
= abs(srcY1
- srcY0
);
303 const int srcLeft
= MIN2(srcX0
, srcX1
);
304 const int srcTop
= MIN2(srcY0
, srcY1
);
307 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
308 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
310 if (srcLeft
!= srcX0
) {
311 /* left-right flip */
317 if (srcTop
!= srcY0
) {
324 assert(screen
->is_format_supported(screen
, src
->format
, PIPE_TEXTURE_2D
,
325 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
326 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
327 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
329 if(dst
->format
== src
->format
&& (dstX1
- dstX0
) == srcW
&& (dstY1
- dstY0
) == srcH
) {
330 /* FIXME: this will most surely fail for overlapping rectangles */
331 pipe
->surface_copy(pipe
,
332 dst
, dstX0
, dstY0
, /* dest */
333 src
, srcX0
, srcY0
, /* src */
334 srcW
, srcH
); /* size */
338 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
339 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
342 * XXX for now we're always creating a temporary texture.
343 * Strictly speaking that's not always needed.
346 /* create temp texture */
347 memset(&texTemp
, 0, sizeof(texTemp
));
348 texTemp
.target
= PIPE_TEXTURE_2D
;
349 texTemp
.format
= src
->format
;
350 texTemp
.last_level
= 0;
351 texTemp
.width
[0] = srcW
;
352 texTemp
.height
[0] = srcH
;
353 texTemp
.depth
[0] = 1;
354 texTemp
.compressed
= 0;
355 pf_get_block(src
->format
, &texTemp
.block
);
357 tex
= screen
->texture_create(screen
, &texTemp
);
361 texSurf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
362 PIPE_BUFFER_USAGE_GPU_WRITE
);
364 /* load temp texture */
365 pipe
->surface_copy(pipe
,
366 texSurf
, 0, 0, /* dest */
367 src
, srcLeft
, srcTop
, /* src */
368 srcW
, srcH
); /* size */
370 /* free the surface, update the texture if necessary.
372 pipe_surface_reference(&texSurf
, NULL
);
374 /* save state (restored below) */
375 cso_save_blend(ctx
->cso
);
376 cso_save_depth_stencil_alpha(ctx
->cso
);
377 cso_save_rasterizer(ctx
->cso
);
378 cso_save_samplers(ctx
->cso
);
379 cso_save_sampler_textures(ctx
->cso
);
380 cso_save_framebuffer(ctx
->cso
);
381 cso_save_fragment_shader(ctx
->cso
);
382 cso_save_vertex_shader(ctx
->cso
);
383 cso_save_viewport(ctx
->cso
);
385 /* set misc state we care about */
386 cso_set_blend(ctx
->cso
, &ctx
->blend
);
387 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
388 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
389 cso_set_viewport(ctx
->cso
, &ctx
->viewport
);
392 ctx
->sampler
.min_img_filter
= filter
;
393 ctx
->sampler
.mag_img_filter
= filter
;
394 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
395 cso_single_sampler_done(ctx
->cso
);
398 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
401 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
);
402 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
405 memset(&fb
, 0, sizeof(fb
));
406 fb
.width
= dst
->width
;
407 fb
.height
= dst
->height
;
410 cso_set_framebuffer(ctx
->cso
, &fb
);
413 offset
= setup_vertex_data(ctx
,
414 (float) dstX0
, (float) dstY0
,
415 (float) dstX1
, (float) dstY1
, z
);
417 util_draw_vertex_buffer(ctx
->pipe
, ctx
->vbuf
, offset
,
418 PIPE_PRIM_TRIANGLE_FAN
,
420 2); /* attribs/vert */
422 /* restore state we changed */
423 cso_restore_blend(ctx
->cso
);
424 cso_restore_depth_stencil_alpha(ctx
->cso
);
425 cso_restore_rasterizer(ctx
->cso
);
426 cso_restore_samplers(ctx
->cso
);
427 cso_restore_sampler_textures(ctx
->cso
);
428 cso_restore_framebuffer(ctx
->cso
);
429 cso_restore_fragment_shader(ctx
->cso
);
430 cso_restore_vertex_shader(ctx
->cso
);
431 cso_restore_viewport(ctx
->cso
);
433 pipe_texture_reference(&tex
, NULL
);
437 /* Release vertex buffer at end of frame to avoid synchronous
440 void util_blit_flush( struct blit_state
*ctx
)
442 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
449 * Copy pixel block from src texture to dst surface.
450 * Overlapping regions are acceptable.
452 * XXX Should support selection of level.
453 * XXX need some control over blitting Z and/or stencil.
456 util_blit_pixels_tex(struct blit_state
*ctx
,
457 struct pipe_texture
*tex
,
458 int srcX0
, int srcY0
,
459 int srcX1
, int srcY1
,
460 struct pipe_surface
*dst
,
461 int dstX0
, int dstY0
,
462 int dstX1
, int dstY1
,
463 float z
, uint filter
)
465 struct pipe_framebuffer_state fb
;
466 float s0
, t0
, s1
, t1
;
469 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
470 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
472 assert(tex
->width
[0] != 0);
473 assert(tex
->height
[0] != 0);
475 s0
= srcX0
/ (float)tex
->width
[0];
476 s1
= srcX1
/ (float)tex
->width
[0];
477 t0
= srcY0
/ (float)tex
->height
[0];
478 t1
= srcY1
/ (float)tex
->height
[0];
480 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
482 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
485 /* save state (restored below) */
486 cso_save_blend(ctx
->cso
);
487 cso_save_depth_stencil_alpha(ctx
->cso
);
488 cso_save_rasterizer(ctx
->cso
);
489 cso_save_samplers(ctx
->cso
);
490 cso_save_sampler_textures(ctx
->cso
);
491 cso_save_framebuffer(ctx
->cso
);
492 cso_save_fragment_shader(ctx
->cso
);
493 cso_save_vertex_shader(ctx
->cso
);
494 cso_save_viewport(ctx
->cso
);
496 /* set misc state we care about */
497 cso_set_blend(ctx
->cso
, &ctx
->blend
);
498 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
499 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
500 cso_set_viewport(ctx
->cso
, &ctx
->viewport
);
503 ctx
->sampler
.min_img_filter
= filter
;
504 ctx
->sampler
.mag_img_filter
= filter
;
505 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
506 cso_single_sampler_done(ctx
->cso
);
509 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
512 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
);
513 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
516 memset(&fb
, 0, sizeof(fb
));
517 fb
.width
= dst
->width
;
518 fb
.height
= dst
->height
;
521 cso_set_framebuffer(ctx
->cso
, &fb
);
524 offset
= setup_vertex_data_tex(ctx
,
525 (float) dstX0
, (float) dstY0
,
526 (float) dstX1
, (float) dstY1
,
530 util_draw_vertex_buffer(ctx
->pipe
,
532 PIPE_PRIM_TRIANGLE_FAN
,
534 2); /* attribs/vert */
536 /* restore state we changed */
537 cso_restore_blend(ctx
->cso
);
538 cso_restore_depth_stencil_alpha(ctx
->cso
);
539 cso_restore_rasterizer(ctx
->cso
);
540 cso_restore_samplers(ctx
->cso
);
541 cso_restore_sampler_textures(ctx
->cso
);
542 cso_restore_framebuffer(ctx
->cso
);
543 cso_restore_fragment_shader(ctx
->cso
);
544 cso_restore_vertex_shader(ctx
->cso
);
545 cso_restore_viewport(ctx
->cso
);