1 /**************************************************************************
3 * Copyright 2003 VMware, Inc.
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 VMWARE 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 **************************************************************************/
28 #include "i915_surface.h"
29 #include "i915_resource.h"
30 #include "i915_state.h"
31 #include "i915_blit.h"
33 #include "i915_screen.h"
34 #include "pipe/p_defines.h"
35 #include "util/u_inlines.h"
36 #include "util/u_math.h"
37 #include "util/u_format.h"
38 #include "util/u_memory.h"
39 #include "util/u_pack_color.h"
40 #include "util/u_surface.h"
43 * surface functions using the render engine
47 i915_util_blitter_save_states(struct i915_context
*i915
)
49 util_blitter_save_blend(i915
->blitter
, (void *)i915
->blend
);
50 util_blitter_save_depth_stencil_alpha(i915
->blitter
, (void *)i915
->depth_stencil
);
51 util_blitter_save_stencil_ref(i915
->blitter
, &i915
->stencil_ref
);
52 util_blitter_save_rasterizer(i915
->blitter
, (void *)i915
->rasterizer
);
53 util_blitter_save_fragment_shader(i915
->blitter
, i915
->fs
);
54 util_blitter_save_vertex_shader(i915
->blitter
, i915
->vs
);
55 util_blitter_save_viewport(i915
->blitter
, &i915
->viewport
);
56 util_blitter_save_scissor(i915
->blitter
, &i915
->scissor
);
57 util_blitter_save_vertex_elements(i915
->blitter
, i915
->velems
);
58 util_blitter_save_vertex_buffer_slot(i915
->blitter
,
59 i915
->vertex_buffers
);
61 util_blitter_save_framebuffer(i915
->blitter
, &i915
->framebuffer
);
63 util_blitter_save_fragment_sampler_states(i915
->blitter
,
65 (void**)i915
->fragment_sampler
);
66 util_blitter_save_fragment_sampler_views(i915
->blitter
,
67 i915
->num_fragment_sampler_views
,
68 i915
->fragment_sampler_views
);
72 i915_surface_copy_render(struct pipe_context
*pipe
,
73 struct pipe_resource
*dst
, unsigned dst_level
,
74 unsigned dstx
, unsigned dsty
, unsigned dstz
,
75 struct pipe_resource
*src
, unsigned src_level
,
76 const struct pipe_box
*src_box
)
78 struct i915_context
*i915
= i915_context(pipe
);
80 /* Fallback for buffers. */
81 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
82 util_resource_copy_region(pipe
, dst
, dst_level
, dstx
, dsty
, dstz
,
83 src
, src_level
, src_box
);
87 if (!util_blitter_is_copy_supported(i915
->blitter
, dst
, src
)) {
88 util_resource_copy_region(pipe
, dst
, dst_level
, dstx
, dsty
, dstz
,
89 src
, src_level
, src_box
);
93 i915_util_blitter_save_states(i915
);
95 util_blitter_copy_texture(i915
->blitter
, dst
, dst_level
, dstx
, dsty
, dstz
,
96 src
, src_level
, src_box
);
100 i915_clear_render_target_render(struct pipe_context
*pipe
,
101 struct pipe_surface
*dst
,
102 const union pipe_color_union
*color
,
103 unsigned dstx
, unsigned dsty
,
104 unsigned width
, unsigned height
)
106 struct i915_context
*i915
= i915_context(pipe
);
107 struct pipe_framebuffer_state fb_state
;
109 util_blitter_save_framebuffer(i915
->blitter
, &i915
->framebuffer
);
111 fb_state
.width
= dst
->width
;
112 fb_state
.height
= dst
->height
;
113 fb_state
.nr_cbufs
= 1;
114 fb_state
.cbufs
[0] = dst
;
115 fb_state
.zsbuf
= NULL
;
116 pipe
->set_framebuffer_state(pipe
, &fb_state
);
119 i915_update_derived(i915
);
121 i915_clear_emit(pipe
, PIPE_CLEAR_COLOR
, color
, 0.0, 0x0,
122 dstx
, dsty
, width
, height
);
124 pipe
->set_framebuffer_state(pipe
, &i915
->blitter
->saved_fb_state
);
125 util_unreference_framebuffer_state(&i915
->blitter
->saved_fb_state
);
126 i915
->blitter
->saved_fb_state
.nr_cbufs
= ~0;
130 i915_clear_depth_stencil_render(struct pipe_context
*pipe
,
131 struct pipe_surface
*dst
,
132 unsigned clear_flags
,
135 unsigned dstx
, unsigned dsty
,
136 unsigned width
, unsigned height
)
138 struct i915_context
*i915
= i915_context(pipe
);
139 struct pipe_framebuffer_state fb_state
;
141 util_blitter_save_framebuffer(i915
->blitter
, &i915
->framebuffer
);
143 fb_state
.width
= dst
->width
;
144 fb_state
.height
= dst
->height
;
145 fb_state
.nr_cbufs
= 0;
146 fb_state
.zsbuf
= dst
;
147 pipe
->set_framebuffer_state(pipe
, &fb_state
);
150 i915_update_derived(i915
);
152 i915_clear_emit(pipe
, clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
,
153 NULL
, depth
, stencil
,
154 dstx
, dsty
, width
, height
);
156 pipe
->set_framebuffer_state(pipe
, &i915
->blitter
->saved_fb_state
);
157 util_unreference_framebuffer_state(&i915
->blitter
->saved_fb_state
);
158 i915
->blitter
->saved_fb_state
.nr_cbufs
= ~0;
162 * surface functions using the blitter
165 /* Assumes all values are within bounds -- no checking at this level -
166 * do it higher up if required.
169 i915_surface_copy_blitter(struct pipe_context
*pipe
,
170 struct pipe_resource
*dst
, unsigned dst_level
,
171 unsigned dstx
, unsigned dsty
, unsigned dstz
,
172 struct pipe_resource
*src
, unsigned src_level
,
173 const struct pipe_box
*src_box
)
175 struct i915_texture
*dst_tex
= i915_texture(dst
);
176 struct i915_texture
*src_tex
= i915_texture(src
);
177 struct pipe_resource
*dpt
= &dst_tex
->b
.b
;
178 struct pipe_resource
*spt
= &src_tex
->b
.b
;
179 unsigned dst_offset
, src_offset
; /* in bytes */
181 /* Fallback for buffers. */
182 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
183 util_resource_copy_region(pipe
, dst
, dst_level
, dstx
, dsty
, dstz
,
184 src
, src_level
, src_box
);
188 /* XXX cannot copy 3d regions at this time */
189 assert(src_box
->depth
== 1);
190 if (dst
->target
!= PIPE_TEXTURE_CUBE
&&
191 dst
->target
!= PIPE_TEXTURE_3D
)
193 dst_offset
= i915_texture_offset(dst_tex
, dst_level
, dstz
);
195 if (src
->target
!= PIPE_TEXTURE_CUBE
&&
196 src
->target
!= PIPE_TEXTURE_3D
)
197 assert(src_box
->z
== 0);
198 src_offset
= i915_texture_offset(src_tex
, src_level
, src_box
->z
);
200 assert( util_format_get_blocksize(dpt
->format
) == util_format_get_blocksize(spt
->format
) );
201 assert( util_format_get_blockwidth(dpt
->format
) == util_format_get_blockwidth(spt
->format
) );
202 assert( util_format_get_blockheight(dpt
->format
) == util_format_get_blockheight(spt
->format
) );
203 assert( util_format_get_blockwidth(dpt
->format
) == 1 );
204 assert( util_format_get_blockheight(dpt
->format
) == 1 );
206 i915_copy_blit( i915_context(pipe
),
207 util_format_get_blocksize(dpt
->format
),
208 (unsigned short) src_tex
->stride
, src_tex
->buffer
, src_offset
,
209 (unsigned short) dst_tex
->stride
, dst_tex
->buffer
, dst_offset
,
210 (short) src_box
->x
, (short) src_box
->y
, (short) dstx
, (short) dsty
,
211 (short) src_box
->width
, (short) src_box
->height
);
215 i915_blit(struct pipe_context
*pipe
, const struct pipe_blit_info
*blit_info
)
217 struct i915_context
*i915
= i915_context(pipe
);
218 struct pipe_blit_info info
= *blit_info
;
220 if (util_try_blit_via_copy_region(pipe
, &info
)) {
224 if (info
.mask
& PIPE_MASK_S
) {
225 debug_printf("i915: cannot blit stencil, skipping\n");
226 info
.mask
&= ~PIPE_MASK_S
;
229 if (!util_blitter_is_blit_supported(i915
->blitter
, &info
)) {
230 debug_printf("i915: blit unsupported %s -> %s\n",
231 util_format_short_name(info
.src
.resource
->format
),
232 util_format_short_name(info
.dst
.resource
->format
));
236 i915_util_blitter_save_states(i915
);
238 util_blitter_blit(i915
->blitter
, &info
);
242 i915_flush_resource(struct pipe_context
*ctx
, struct pipe_resource
*resource
)
247 i915_clear_render_target_blitter(struct pipe_context
*pipe
,
248 struct pipe_surface
*dst
,
249 const union pipe_color_union
*color
,
250 unsigned dstx
, unsigned dsty
,
251 unsigned width
, unsigned height
)
253 struct i915_texture
*tex
= i915_texture(dst
->texture
);
254 struct pipe_resource
*pt
= &tex
->b
.b
;
256 unsigned offset
= i915_texture_offset(tex
, dst
->u
.tex
.level
, dst
->u
.tex
.first_layer
);
258 assert(util_format_get_blockwidth(pt
->format
) == 1);
259 assert(util_format_get_blockheight(pt
->format
) == 1);
261 util_pack_color(color
->f
, dst
->format
, &uc
);
262 i915_fill_blit( i915_context(pipe
),
263 util_format_get_blocksize(pt
->format
),
264 XY_COLOR_BLT_WRITE_ALPHA
| XY_COLOR_BLT_WRITE_RGB
,
265 (unsigned short) tex
->stride
,
267 (short) dstx
, (short) dsty
,
268 (short) width
, (short) height
,
273 i915_clear_depth_stencil_blitter(struct pipe_context
*pipe
,
274 struct pipe_surface
*dst
,
275 unsigned clear_flags
,
278 unsigned dstx
, unsigned dsty
,
279 unsigned width
, unsigned height
)
281 struct i915_texture
*tex
= i915_texture(dst
->texture
);
282 struct pipe_resource
*pt
= &tex
->b
.b
;
285 unsigned offset
= i915_texture_offset(tex
, dst
->u
.tex
.level
, dst
->u
.tex
.first_layer
);
287 assert(util_format_get_blockwidth(pt
->format
) == 1);
288 assert(util_format_get_blockheight(pt
->format
) == 1);
290 packedds
= util_pack_z_stencil(dst
->format
, depth
, stencil
);
292 if (clear_flags
& PIPE_CLEAR_DEPTH
)
293 mask
|= XY_COLOR_BLT_WRITE_RGB
;
294 /* XXX presumably this does read-modify-write
295 (otherwise this won't work anyway). Hence will only want to
296 do it if really have stencil and it isn't cleared */
297 if ((clear_flags
& PIPE_CLEAR_STENCIL
) ||
298 (dst
->format
!= PIPE_FORMAT_Z24_UNORM_S8_UINT
))
299 mask
|= XY_COLOR_BLT_WRITE_ALPHA
;
301 i915_fill_blit( i915_context(pipe
),
302 util_format_get_blocksize(pt
->format
),
304 (unsigned short) tex
->stride
,
306 (short) dstx
, (short) dsty
,
307 (short) width
, (short) height
,
312 * Screen surface functions
316 static struct pipe_surface
*
317 i915_create_surface(struct pipe_context
*ctx
,
318 struct pipe_resource
*pt
,
319 const struct pipe_surface
*surf_tmpl
)
321 struct pipe_surface
*ps
;
323 assert(surf_tmpl
->u
.tex
.first_layer
== surf_tmpl
->u
.tex
.last_layer
);
324 if (pt
->target
!= PIPE_TEXTURE_CUBE
&&
325 pt
->target
!= PIPE_TEXTURE_3D
)
326 assert(surf_tmpl
->u
.tex
.first_layer
== 0);
328 ps
= CALLOC_STRUCT(pipe_surface
);
330 /* could subclass pipe_surface and store offset as it used to do */
331 pipe_reference_init(&ps
->reference
, 1);
332 pipe_resource_reference(&ps
->texture
, pt
);
333 ps
->format
= surf_tmpl
->format
;
334 ps
->width
= u_minify(pt
->width0
, surf_tmpl
->u
.tex
.level
);
335 ps
->height
= u_minify(pt
->height0
, surf_tmpl
->u
.tex
.level
);
336 ps
->u
.tex
.level
= surf_tmpl
->u
.tex
.level
;
337 ps
->u
.tex
.first_layer
= surf_tmpl
->u
.tex
.first_layer
;
338 ps
->u
.tex
.last_layer
= surf_tmpl
->u
.tex
.last_layer
;
345 i915_surface_destroy(struct pipe_context
*ctx
,
346 struct pipe_surface
*surf
)
348 pipe_resource_reference(&surf
->texture
, NULL
);
354 i915_init_surface_functions(struct i915_context
*i915
)
356 if (i915_screen(i915
->base
.screen
)->debug
.use_blitter
) {
357 i915
->base
.resource_copy_region
= i915_surface_copy_blitter
;
358 i915
->base
.clear_render_target
= i915_clear_render_target_blitter
;
359 i915
->base
.clear_depth_stencil
= i915_clear_depth_stencil_blitter
;
361 i915
->base
.resource_copy_region
= i915_surface_copy_render
;
362 i915
->base
.clear_render_target
= i915_clear_render_target_render
;
363 i915
->base
.clear_depth_stencil
= i915_clear_depth_stencil_render
;
365 i915
->base
.blit
= i915_blit
;
366 i915
->base
.flush_resource
= i915_flush_resource
;
367 i915
->base
.create_surface
= i915_create_surface
;
368 i915
->base
.surface_destroy
= i915_surface_destroy
;