8afe8d07867f5042e75dff5ca1286f9ac2899429
[mesa.git] / src / gallium / drivers / i915 / i915_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2003 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "i915_surface.h"
29 #include "i915_resource.h"
30 #include "i915_state.h"
31 #include "i915_blit.h"
32 #include "i915_reg.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"
41
42 static struct pipe_surface *
43 i915_create_surface_custom(struct pipe_context *ctx,
44 struct pipe_resource *pt,
45 const struct pipe_surface *surf_tmpl,
46 unsigned width0,
47 unsigned height0);
48 /*
49 * surface functions using the render engine
50 */
51
52 static void
53 i915_util_blitter_save_states(struct i915_context *i915)
54 {
55 util_blitter_save_blend(i915->blitter, (void *)i915->blend);
56 util_blitter_save_depth_stencil_alpha(i915->blitter, (void *)i915->depth_stencil);
57 util_blitter_save_stencil_ref(i915->blitter, &i915->stencil_ref);
58 util_blitter_save_rasterizer(i915->blitter, (void *)i915->rasterizer);
59 util_blitter_save_fragment_shader(i915->blitter, i915->fs);
60 util_blitter_save_vertex_shader(i915->blitter, i915->vs);
61 util_blitter_save_viewport(i915->blitter, &i915->viewport);
62 util_blitter_save_scissor(i915->blitter, &i915->scissor);
63 util_blitter_save_vertex_elements(i915->blitter, i915->velems);
64 util_blitter_save_vertex_buffer_slot(i915->blitter,
65 i915->vertex_buffers);
66
67 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
68
69 util_blitter_save_fragment_sampler_states(i915->blitter,
70 i915->num_samplers,
71 (void**)i915->fragment_sampler);
72 util_blitter_save_fragment_sampler_views(i915->blitter,
73 i915->num_fragment_sampler_views,
74 i915->fragment_sampler_views);
75 }
76
77 static void
78 i915_surface_copy_render(struct pipe_context *pipe,
79 struct pipe_resource *dst, unsigned dst_level,
80 unsigned dstx, unsigned dsty, unsigned dstz,
81 struct pipe_resource *src, unsigned src_level,
82 const struct pipe_box *src_box)
83 {
84 struct pipe_screen *screen = pipe->screen;
85 struct i915_context *i915 = i915_context(pipe);
86 struct i915_texture *dst_tex = i915_texture(dst);
87 struct i915_texture *src_tex = i915_texture(src);
88 unsigned src_width0 = src->width0;
89 unsigned src_height0 = src->height0;
90 unsigned dst_width0 = dst->width0;
91 unsigned dst_height0 = dst->height0;
92 unsigned layout;
93 struct pipe_box dstbox;
94 struct pipe_sampler_view src_templ, *src_view;
95 struct pipe_surface dst_templ, *dst_view;
96
97 /* Fallback for buffers and npot. */
98 if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) ||
99 !util_is_power_of_two(src_width0) || !util_is_power_of_two(src_height0)) {
100 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
101 src, src_level, src_box);
102 return;
103 }
104
105 layout = util_format_description(dst_templ.format)->layout;
106
107 if (!util_blitter_is_copy_supported(i915->blitter, dst, src)) {
108 switch (util_format_get_blocksize(dst_templ.format)) {
109 case 1:
110 dst_templ.format = PIPE_FORMAT_I8_UNORM;
111 break;
112 case 2:
113 dst_templ.format = PIPE_FORMAT_B5G6R5_UNORM;
114 break;
115 case 4:
116 dst_templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
117 break;
118 default:
119 debug_printf("i915: copy_region: Unhandled format: %s. Falling back to software.\n"
120 "i915: copy_region: Software fallback doesn't work for tiled textures.\n",
121 util_format_short_name(dst_templ.format));
122 }
123 src_templ.format = dst_templ.format;
124 }
125
126 i915_util_blitter_save_states(i915);
127
128 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
129 util_blitter_default_src_texture(&src_templ, src, src_level);
130
131 dst_view = i915_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);
132 src_view = i915_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);
133
134 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
135 abs(src_box->depth), &dstbox);
136
137 util_blitter_blit_generic(i915->blitter, dst_view, &dstbox,
138 src_view, src_box, src_width0, src_height0,
139 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL);
140 }
141
142 static void
143 i915_clear_render_target_render(struct pipe_context *pipe,
144 struct pipe_surface *dst,
145 const union pipe_color_union *color,
146 unsigned dstx, unsigned dsty,
147 unsigned width, unsigned height)
148 {
149 struct i915_context *i915 = i915_context(pipe);
150 struct pipe_framebuffer_state fb_state;
151
152 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
153
154 fb_state.width = dst->width;
155 fb_state.height = dst->height;
156 fb_state.nr_cbufs = 1;
157 fb_state.cbufs[0] = dst;
158 fb_state.zsbuf = NULL;
159 pipe->set_framebuffer_state(pipe, &fb_state);
160
161 if (i915->dirty)
162 i915_update_derived(i915);
163
164 i915_clear_emit(pipe, PIPE_CLEAR_COLOR, color, 0.0, 0x0,
165 dstx, dsty, width, height);
166
167 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
168 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
169 i915->blitter->saved_fb_state.nr_cbufs = ~0;
170 }
171
172 static void
173 i915_clear_depth_stencil_render(struct pipe_context *pipe,
174 struct pipe_surface *dst,
175 unsigned clear_flags,
176 double depth,
177 unsigned stencil,
178 unsigned dstx, unsigned dsty,
179 unsigned width, unsigned height)
180 {
181 struct i915_context *i915 = i915_context(pipe);
182 struct pipe_framebuffer_state fb_state;
183
184 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
185
186 fb_state.width = dst->width;
187 fb_state.height = dst->height;
188 fb_state.nr_cbufs = 0;
189 fb_state.zsbuf = dst;
190 pipe->set_framebuffer_state(pipe, &fb_state);
191
192 if (i915->dirty)
193 i915_update_derived(i915);
194
195 i915_clear_emit(pipe, clear_flags & PIPE_CLEAR_DEPTHSTENCIL,
196 NULL, depth, stencil,
197 dstx, dsty, width, height);
198
199 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
200 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
201 i915->blitter->saved_fb_state.nr_cbufs = ~0;
202 }
203
204 /*
205 * surface functions using the blitter
206 */
207
208 /* Assumes all values are within bounds -- no checking at this level -
209 * do it higher up if required.
210 */
211 static void
212 i915_surface_copy_blitter(struct pipe_context *pipe,
213 struct pipe_resource *dst, unsigned dst_level,
214 unsigned dstx, unsigned dsty, unsigned dstz,
215 struct pipe_resource *src, unsigned src_level,
216 const struct pipe_box *src_box)
217 {
218 struct i915_texture *dst_tex = i915_texture(dst);
219 struct i915_texture *src_tex = i915_texture(src);
220 struct pipe_resource *dpt = &dst_tex->b.b;
221 struct pipe_resource *spt = &src_tex->b.b;
222 unsigned dst_offset, src_offset; /* in bytes */
223
224 /* Fallback for buffers. */
225 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
226 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
227 src, src_level, src_box);
228 return;
229 }
230
231 /* XXX cannot copy 3d regions at this time */
232 assert(src_box->depth == 1);
233 if (dst->target != PIPE_TEXTURE_CUBE &&
234 dst->target != PIPE_TEXTURE_3D)
235 assert(dstz == 0);
236 dst_offset = i915_texture_offset(dst_tex, dst_level, dstz);
237
238 if (src->target != PIPE_TEXTURE_CUBE &&
239 src->target != PIPE_TEXTURE_3D)
240 assert(src_box->z == 0);
241 src_offset = i915_texture_offset(src_tex, src_level, src_box->z);
242
243 assert( util_format_get_blocksize(dpt->format) == util_format_get_blocksize(spt->format) );
244 assert( util_format_get_blockwidth(dpt->format) == util_format_get_blockwidth(spt->format) );
245 assert( util_format_get_blockheight(dpt->format) == util_format_get_blockheight(spt->format) );
246 assert( util_format_get_blockwidth(dpt->format) == 1 );
247 assert( util_format_get_blockheight(dpt->format) == 1 );
248
249 i915_copy_blit( i915_context(pipe),
250 util_format_get_blocksize(dpt->format),
251 (unsigned short) src_tex->stride, src_tex->buffer, src_offset,
252 (unsigned short) dst_tex->stride, dst_tex->buffer, dst_offset,
253 (short) src_box->x, (short) src_box->y, (short) dstx, (short) dsty,
254 (short) src_box->width, (short) src_box->height );
255 }
256
257 static void
258 i915_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)
259 {
260 struct i915_context *i915 = i915_context(pipe);
261 struct pipe_blit_info info = *blit_info;
262
263 if (util_try_blit_via_copy_region(pipe, &info)) {
264 return; /* done */
265 }
266
267 if (info.mask & PIPE_MASK_S) {
268 debug_printf("i915: cannot blit stencil, skipping\n");
269 info.mask &= ~PIPE_MASK_S;
270 }
271
272 if (!util_blitter_is_blit_supported(i915->blitter, &info)) {
273 debug_printf("i915: blit unsupported %s -> %s\n",
274 util_format_short_name(info.src.resource->format),
275 util_format_short_name(info.dst.resource->format));
276 return;
277 }
278
279 i915_util_blitter_save_states(i915);
280
281 util_blitter_blit(i915->blitter, &info);
282 }
283
284 static void
285 i915_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
286 {
287 }
288
289 static void
290 i915_clear_render_target_blitter(struct pipe_context *pipe,
291 struct pipe_surface *dst,
292 const union pipe_color_union *color,
293 unsigned dstx, unsigned dsty,
294 unsigned width, unsigned height)
295 {
296 struct i915_texture *tex = i915_texture(dst->texture);
297 struct pipe_resource *pt = &tex->b.b;
298 union util_color uc;
299 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
300
301 assert(util_format_get_blockwidth(pt->format) == 1);
302 assert(util_format_get_blockheight(pt->format) == 1);
303
304 util_pack_color(color->f, dst->format, &uc);
305 i915_fill_blit( i915_context(pipe),
306 util_format_get_blocksize(pt->format),
307 XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
308 (unsigned short) tex->stride,
309 tex->buffer, offset,
310 (short) dstx, (short) dsty,
311 (short) width, (short) height,
312 uc.ui[0] );
313 }
314
315 static void
316 i915_clear_depth_stencil_blitter(struct pipe_context *pipe,
317 struct pipe_surface *dst,
318 unsigned clear_flags,
319 double depth,
320 unsigned stencil,
321 unsigned dstx, unsigned dsty,
322 unsigned width, unsigned height)
323 {
324 struct i915_texture *tex = i915_texture(dst->texture);
325 struct pipe_resource *pt = &tex->b.b;
326 unsigned packedds;
327 unsigned mask = 0;
328 unsigned offset = i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
329
330 assert(util_format_get_blockwidth(pt->format) == 1);
331 assert(util_format_get_blockheight(pt->format) == 1);
332
333 packedds = util_pack_z_stencil(dst->format, depth, stencil);
334
335 if (clear_flags & PIPE_CLEAR_DEPTH)
336 mask |= XY_COLOR_BLT_WRITE_RGB;
337 /* XXX presumably this does read-modify-write
338 (otherwise this won't work anyway). Hence will only want to
339 do it if really have stencil and it isn't cleared */
340 if ((clear_flags & PIPE_CLEAR_STENCIL) ||
341 (dst->format != PIPE_FORMAT_Z24_UNORM_S8_UINT))
342 mask |= XY_COLOR_BLT_WRITE_ALPHA;
343
344 i915_fill_blit( i915_context(pipe),
345 util_format_get_blocksize(pt->format),
346 mask,
347 (unsigned short) tex->stride,
348 tex->buffer, offset,
349 (short) dstx, (short) dsty,
350 (short) width, (short) height,
351 packedds );
352 }
353
354 /*
355 * Screen surface functions
356 */
357
358
359 static struct pipe_surface *
360 i915_create_surface_custom(struct pipe_context *ctx,
361 struct pipe_resource *pt,
362 const struct pipe_surface *surf_tmpl,
363 unsigned width0,
364 unsigned height0)
365 {
366 struct pipe_surface *ps;
367
368 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
369 if (pt->target != PIPE_TEXTURE_CUBE &&
370 pt->target != PIPE_TEXTURE_3D)
371 assert(surf_tmpl->u.tex.first_layer == 0);
372
373 ps = CALLOC_STRUCT(pipe_surface);
374 if (ps) {
375 /* could subclass pipe_surface and store offset as it used to do */
376 pipe_reference_init(&ps->reference, 1);
377 pipe_resource_reference(&ps->texture, pt);
378 ps->format = surf_tmpl->format;
379 ps->width = u_minify(width0, surf_tmpl->u.tex.level);
380 ps->height = u_minify(height0, surf_tmpl->u.tex.level);
381 ps->u.tex.level = surf_tmpl->u.tex.level;
382 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
383 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
384 ps->context = ctx;
385 }
386 return ps;
387 }
388
389 static struct pipe_surface *
390 i915_create_surface(struct pipe_context *ctx,
391 struct pipe_resource *pt,
392 const struct pipe_surface *surf_tmpl)
393 {
394 return i915_create_surface_custom(ctx, pt, surf_tmpl,
395 pt->width0, pt->height0);
396 }
397
398 static void
399 i915_surface_destroy(struct pipe_context *ctx,
400 struct pipe_surface *surf)
401 {
402 pipe_resource_reference(&surf->texture, NULL);
403 FREE(surf);
404 }
405
406
407 void
408 i915_init_surface_functions(struct i915_context *i915)
409 {
410 if (i915_screen(i915->base.screen)->debug.use_blitter) {
411 i915->base.resource_copy_region = i915_surface_copy_blitter;
412 i915->base.clear_render_target = i915_clear_render_target_blitter;
413 i915->base.clear_depth_stencil = i915_clear_depth_stencil_blitter;
414 } else {
415 i915->base.resource_copy_region = i915_surface_copy_render;
416 i915->base.clear_render_target = i915_clear_render_target_render;
417 i915->base.clear_depth_stencil = i915_clear_depth_stencil_render;
418 }
419 i915->base.blit = i915_blit;
420 i915->base.flush_resource = i915_flush_resource;
421 i915->base.create_surface = i915_create_surface;
422 i915->base.surface_destroy = i915_surface_destroy;
423 }