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 "pipe/p_defines.h"
30 #include "util/format/u_format.h"
31 #include "util/u_inlines.h"
32 #include "util/u_math.h"
33 #include "util/u_memory.h"
34 #include "util/u_pack_color.h"
35 #include "util/u_surface.h"
36 #include "i915_blit.h"
37 #include "i915_reg.h"
38 #include "i915_resource.h"
39 #include "i915_screen.h"
40 #include "i915_state.h"
41
42 static struct pipe_surface *
43 i915_create_surface_custom(struct pipe_context *ctx, struct pipe_resource *pt,
44 const struct pipe_surface *surf_tmpl,
45 unsigned width0, unsigned height0);
46 /*
47 * surface functions using the render engine
48 */
49
50 static void
i915_util_blitter_save_states(struct i915_context * i915)51 i915_util_blitter_save_states(struct i915_context *i915)
52 {
53 util_blitter_save_blend(i915->blitter, (void *)i915->blend);
54 util_blitter_save_depth_stencil_alpha(i915->blitter,
55 (void *)i915->depth_stencil);
56 util_blitter_save_stencil_ref(i915->blitter, &i915->stencil_ref);
57 util_blitter_save_rasterizer(i915->blitter, (void *)i915->rasterizer);
58 util_blitter_save_fragment_shader(i915->blitter, i915->fs);
59 util_blitter_save_vertex_shader(i915->blitter, i915->vs);
60 util_blitter_save_viewport(i915->blitter, &i915->viewport);
61 util_blitter_save_scissor(i915->blitter, &i915->scissor);
62 util_blitter_save_vertex_elements(i915->blitter, i915->velems);
63 util_blitter_save_vertex_buffers(i915->blitter, i915->vertex_buffers,
64 i915->nr_vertex_buffers);
65
66 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
67
68 util_blitter_save_fragment_sampler_states(i915->blitter, i915->num_samplers,
69 (void **)i915->fragment_sampler);
70 util_blitter_save_fragment_sampler_views(i915->blitter,
71 i915->num_fragment_sampler_views,
72 i915->fragment_sampler_views);
73 }
74
75 static void
i915_surface_copy_render(struct pipe_context * pipe,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)76 i915_surface_copy_render(struct pipe_context *pipe, struct pipe_resource *dst,
77 unsigned dst_level, unsigned dstx, unsigned dsty,
78 unsigned dstz, struct pipe_resource *src,
79 unsigned src_level, const struct pipe_box *src_box)
80 {
81 struct i915_context *i915 = i915_context(pipe);
82 unsigned src_width0 = src->width0;
83 unsigned src_height0 = src->height0;
84 unsigned dst_width0 = dst->width0;
85 unsigned dst_height0 = dst->height0;
86 struct pipe_box dstbox;
87 struct pipe_sampler_view src_templ, *src_view;
88 struct pipe_surface dst_templ, *dst_view;
89 const struct util_format_description *desc;
90
91 /* Fallback for buffers. */
92 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER)
93 goto fallback;
94
95 /* Fallback for depth&stencil. XXX: see if we can use a proxy format */
96 desc = util_format_description(src->format);
97 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
98 goto fallback;
99
100 desc = util_format_description(dst->format);
101 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
102 goto fallback;
103
104 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
105 util_blitter_default_src_texture(i915->blitter, &src_templ, src, src_level);
106
107 if (!util_blitter_is_copy_supported(i915->blitter, dst, src))
108 goto fallback;
109
110 i915_util_blitter_save_states(i915);
111
112 dst_view = i915_create_surface_custom(pipe, dst, &dst_templ, dst_width0,
113 dst_height0);
114 src_view = i915_create_sampler_view_custom(pipe, src, &src_templ, src_width0,
115 src_height0);
116
117 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
118 abs(src_box->depth), &dstbox);
119
120 util_blitter_blit_generic(i915->blitter, dst_view, &dstbox, src_view,
121 src_box, src_width0, src_height0, PIPE_MASK_RGBAZS,
122 PIPE_TEX_FILTER_NEAREST, NULL, false, false, 0);
123 return;
124
125 fallback:
126 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz, src,
127 src_level, src_box);
128 }
129
130 static void
i915_clear_render_target_render(struct pipe_context * pipe,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)131 i915_clear_render_target_render(struct pipe_context *pipe,
132 struct pipe_surface *dst,
133 const union pipe_color_union *color,
134 unsigned dstx, unsigned dsty, unsigned width,
135 unsigned height, bool render_condition_enabled)
136 {
137 struct i915_context *i915 = i915_context(pipe);
138 struct pipe_framebuffer_state fb_state;
139
140 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
141
142 fb_state.width = dst->width;
143 fb_state.height = dst->height;
144 fb_state.nr_cbufs = 1;
145 fb_state.cbufs[0] = dst;
146 fb_state.zsbuf = NULL;
147 pipe->set_framebuffer_state(pipe, &fb_state);
148
149 if (i915->dirty)
150 i915_update_derived(i915);
151
152 i915_clear_emit(pipe, PIPE_CLEAR_COLOR, color, 0.0, 0x0, dstx, dsty, width,
153 height);
154
155 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
156 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
157 i915->blitter->saved_fb_state.nr_cbufs = ~0;
158 }
159
160 static void
i915_clear_depth_stencil_render(struct pipe_context * pipe,struct pipe_surface * dst,unsigned clear_flags,double depth,unsigned stencil,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)161 i915_clear_depth_stencil_render(struct pipe_context *pipe,
162 struct pipe_surface *dst, unsigned clear_flags,
163 double depth, unsigned stencil, unsigned dstx,
164 unsigned dsty, unsigned width, unsigned height,
165 bool render_condition_enabled)
166 {
167 struct i915_context *i915 = i915_context(pipe);
168 struct pipe_framebuffer_state fb_state;
169
170 util_blitter_save_framebuffer(i915->blitter, &i915->framebuffer);
171
172 fb_state.width = dst->width;
173 fb_state.height = dst->height;
174 fb_state.nr_cbufs = 0;
175 fb_state.zsbuf = dst;
176 pipe->set_framebuffer_state(pipe, &fb_state);
177
178 if (i915->dirty)
179 i915_update_derived(i915);
180
181 i915_clear_emit(pipe, clear_flags & PIPE_CLEAR_DEPTHSTENCIL, NULL, depth,
182 stencil, dstx, dsty, width, height);
183
184 pipe->set_framebuffer_state(pipe, &i915->blitter->saved_fb_state);
185 util_unreference_framebuffer_state(&i915->blitter->saved_fb_state);
186 i915->blitter->saved_fb_state.nr_cbufs = ~0;
187 }
188
189 /*
190 * surface functions using the blitter
191 */
192
193 /* Assumes all values are within bounds -- no checking at this level -
194 * do it higher up if required.
195 */
196 static void
i915_surface_copy_blitter(struct pipe_context * pipe,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)197 i915_surface_copy_blitter(struct pipe_context *pipe, struct pipe_resource *dst,
198 unsigned dst_level, unsigned dstx, unsigned dsty,
199 unsigned dstz, struct pipe_resource *src,
200 unsigned src_level, const struct pipe_box *src_box)
201 {
202 /* Fallback for buffers. */
203 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
204 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz, src,
205 src_level, src_box);
206 return;
207 }
208
209 struct i915_texture *dst_tex = i915_texture(dst);
210 struct i915_texture *src_tex = i915_texture(src);
211 struct pipe_resource *dpt = &dst_tex->b;
212 ASSERTED struct pipe_resource *spt = &src_tex->b;
213 unsigned dst_offset, src_offset; /* in bytes */
214
215 /* XXX cannot copy 3d regions at this time */
216 assert(src_box->depth == 1);
217 if (dst->target != PIPE_TEXTURE_CUBE && dst->target != PIPE_TEXTURE_3D)
218 assert(dstz == 0);
219 dst_offset = i915_texture_offset(dst_tex, dst_level, dstz);
220
221 if (src->target != PIPE_TEXTURE_CUBE && src->target != PIPE_TEXTURE_3D)
222 assert(src_box->z == 0);
223 src_offset = i915_texture_offset(src_tex, src_level, src_box->z);
224
225 int block_width = util_format_get_blockwidth(dpt->format);
226 int block_height = util_format_get_blockheight(dpt->format);
227 int block_size = util_format_get_blocksize(dpt->format);
228 assert(util_format_get_blocksize(spt->format) == block_size);
229 assert(util_format_get_blockwidth(spt->format) == block_width);
230 assert(util_format_get_blockheight(spt->format) == block_height);
231
232 dstx /= block_width;
233 dsty /= block_height;
234 int srcx = src_box->x / block_width;
235 int srcy = src_box->y / block_height;
236 int width = DIV_ROUND_UP(src_box->width, block_width);
237 int height = DIV_ROUND_UP(src_box->height, block_height);
238
239 if (block_size > 4) {
240 srcx *= (block_size / 4);
241 dstx *= (block_size / 4);
242 width *= (block_size / 4);
243 block_size = 4;
244 }
245
246 i915_copy_blit(i915_context(pipe), block_size,
247 (unsigned short)src_tex->stride, src_tex->buffer, src_offset,
248 (unsigned short)dst_tex->stride, dst_tex->buffer, dst_offset,
249 (short)srcx, (short)srcy, (short)dstx, (short)dsty,
250 (short)width, (short)height);
251 }
252
253 static void
i915_blit(struct pipe_context * pipe,const struct pipe_blit_info * blit_info)254 i915_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)
255 {
256 struct i915_context *i915 = i915_context(pipe);
257 struct pipe_blit_info info = *blit_info;
258
259 if (util_try_blit_via_copy_region(pipe, &info, false)) {
260 return; /* done */
261 }
262
263 if (info.mask & PIPE_MASK_S) {
264 debug_printf("i915: cannot blit stencil, skipping\n");
265 info.mask &= ~PIPE_MASK_S;
266 }
267
268 if (!util_blitter_is_blit_supported(i915->blitter, &info)) {
269 debug_printf("i915: blit unsupported %s -> %s\n",
270 util_format_short_name(info.src.resource->format),
271 util_format_short_name(info.dst.resource->format));
272 return;
273 }
274
275 i915_util_blitter_save_states(i915);
276
277 util_blitter_blit(i915->blitter, &info);
278 }
279
280 static void
i915_flush_resource(struct pipe_context * ctx,struct pipe_resource * resource)281 i915_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
282 {
283 }
284
285 static void
i915_clear_render_target_blitter(struct pipe_context * pipe,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)286 i915_clear_render_target_blitter(struct pipe_context *pipe,
287 struct pipe_surface *dst,
288 const union pipe_color_union *color,
289 unsigned dstx, unsigned dsty, unsigned width,
290 unsigned height, bool render_condition_enabled)
291 {
292 struct i915_texture *tex = i915_texture(dst->texture);
293 struct pipe_resource *pt = &tex->b;
294 union util_color uc;
295 unsigned offset =
296 i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
297
298 assert(util_format_get_blockwidth(pt->format) == 1);
299 assert(util_format_get_blockheight(pt->format) == 1);
300
301 util_pack_color(color->f, dst->format, &uc);
302 i915_fill_blit(i915_context(pipe), util_format_get_blocksize(pt->format),
303 XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
304 (unsigned short)tex->stride, tex->buffer, offset, (short)dstx,
305 (short)dsty, (short)width, (short)height, uc.ui[0]);
306 }
307
308 static void
i915_clear_depth_stencil_blitter(struct pipe_context * pipe,struct pipe_surface * dst,unsigned clear_flags,double depth,unsigned stencil,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)309 i915_clear_depth_stencil_blitter(struct pipe_context *pipe,
310 struct pipe_surface *dst, unsigned clear_flags,
311 double depth, unsigned stencil, unsigned dstx,
312 unsigned dsty, unsigned width, unsigned height,
313 bool render_condition_enabled)
314 {
315 struct i915_texture *tex = i915_texture(dst->texture);
316 struct pipe_resource *pt = &tex->b;
317 unsigned packedds;
318 unsigned mask = 0;
319 unsigned offset =
320 i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
321
322 assert(util_format_get_blockwidth(pt->format) == 1);
323 assert(util_format_get_blockheight(pt->format) == 1);
324
325 packedds = util_pack_z_stencil(dst->format, depth, stencil);
326
327 if (clear_flags & PIPE_CLEAR_DEPTH)
328 mask |= XY_COLOR_BLT_WRITE_RGB;
329 /* XXX presumably this does read-modify-write
330 (otherwise this won't work anyway). Hence will only want to
331 do it if really have stencil and it isn't cleared */
332 if ((clear_flags & PIPE_CLEAR_STENCIL) ||
333 (dst->format != PIPE_FORMAT_Z24_UNORM_S8_UINT))
334 mask |= XY_COLOR_BLT_WRITE_ALPHA;
335
336 i915_fill_blit(i915_context(pipe), util_format_get_blocksize(pt->format),
337 mask, (unsigned short)tex->stride, tex->buffer, offset,
338 (short)dstx, (short)dsty, (short)width, (short)height,
339 packedds);
340 }
341
342 /*
343 * Screen surface functions
344 */
345
346 static void
i915_set_color_surface_swizzle(struct i915_surface * surf)347 i915_set_color_surface_swizzle(struct i915_surface *surf)
348 {
349 enum pipe_format format = surf->templ.format;
350
351 const struct {
352 enum pipe_format format;
353 uint8_t color_swizzle[4];
354 uint32_t oc_swizzle;
355 } fixup_formats[] = {
356 {PIPE_FORMAT_R8G8B8A8_UNORM, {2, 1, 0, 3}, 0x21030000 /* BGRA */},
357 {PIPE_FORMAT_R8G8B8X8_UNORM, {2, 1, 0, 3}, 0x21030000 /* BGRX */},
358
359 /* These are rendered to using COLORBUF_8BIT, where the G channel written
360 * by shader (and output by blending) is used.
361 */
362 {PIPE_FORMAT_L8_UNORM, {0, 0, 0, 0}, 0x00030000 /* RRRA */},
363 {PIPE_FORMAT_I8_UNORM, {0, 0, 0, 0}, 0x00030000 /* RRRA */},
364 {PIPE_FORMAT_A8_UNORM, {3, 3, 3, 3}, 0x33330000 /* AAAA */},
365 };
366
367 if (format == PIPE_FORMAT_A8_UNORM)
368 surf->alpha_in_g = true;
369 else if (util_format_is_rgbx_or_bgrx(format))
370 surf->alpha_is_x = true;
371
372 for (int i = 0; i < ARRAY_SIZE(fixup_formats); i++) {
373 if (fixup_formats[i].format == format) {
374 memcpy(surf->color_swizzle, fixup_formats[i].color_swizzle,
375 sizeof(surf->color_swizzle));
376 surf->oc_swizzle = fixup_formats[i].oc_swizzle;
377 return;
378 }
379 }
380
381 for (int i = 0; i < 4; i++)
382 surf->color_swizzle[i] = i;
383 }
384
385 static struct pipe_surface *
i915_create_surface_custom(struct pipe_context * ctx,struct pipe_resource * pt,const struct pipe_surface * surf_tmpl,unsigned width0,unsigned height0)386 i915_create_surface_custom(struct pipe_context *ctx, struct pipe_resource *pt,
387 const struct pipe_surface *surf_tmpl,
388 unsigned width0, unsigned height0)
389 {
390 struct i915_texture *tex = i915_texture(pt);
391 struct i915_surface *surf;
392
393 assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
394 if (pt->target != PIPE_TEXTURE_CUBE && pt->target != PIPE_TEXTURE_3D)
395 assert(surf_tmpl->u.tex.first_layer == 0);
396
397 surf = CALLOC_STRUCT(i915_surface);
398 if (!surf)
399 return NULL;
400
401 struct pipe_surface *ps = &surf->templ;
402
403 pipe_reference_init(&ps->reference, 1);
404 pipe_resource_reference(&ps->texture, pt);
405 ps->format = surf_tmpl->format;
406 ps->width = u_minify(width0, surf_tmpl->u.tex.level);
407 ps->height = u_minify(height0, surf_tmpl->u.tex.level);
408 ps->u.tex.level = surf_tmpl->u.tex.level;
409 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
410 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
411 ps->context = ctx;
412
413 if (util_format_is_depth_or_stencil(ps->format)) {
414 surf->buf_info = BUF_3D_ID_DEPTH;
415 } else {
416 surf->buf_info = BUF_3D_ID_COLOR_BACK;
417
418 i915_set_color_surface_swizzle(surf);
419 }
420
421 surf->buf_info |= BUF_3D_PITCH(tex->stride); /* pitch in bytes */
422
423 switch (tex->tiling) {
424 case I915_TILE_Y:
425 surf->buf_info |= BUF_3D_TILED_SURFACE | BUF_3D_TILE_WALK_Y;
426 break;
427 case I915_TILE_X:
428 surf->buf_info |= BUF_3D_TILED_SURFACE;
429 break;
430 case I915_TILE_NONE:
431 break;
432 }
433
434 return ps;
435 }
436
437 static struct pipe_surface *
i915_create_surface(struct pipe_context * ctx,struct pipe_resource * pt,const struct pipe_surface * surf_tmpl)438 i915_create_surface(struct pipe_context *ctx, struct pipe_resource *pt,
439 const struct pipe_surface *surf_tmpl)
440 {
441 return i915_create_surface_custom(ctx, pt, surf_tmpl, pt->width0,
442 pt->height0);
443 }
444
445 static void
i915_surface_destroy(struct pipe_context * ctx,struct pipe_surface * surf)446 i915_surface_destroy(struct pipe_context *ctx, struct pipe_surface *surf)
447 {
448 pipe_resource_reference(&surf->texture, NULL);
449 FREE(surf);
450 }
451
452 void
i915_init_surface_functions(struct i915_context * i915)453 i915_init_surface_functions(struct i915_context *i915)
454 {
455 if (i915_screen(i915->base.screen)->debug.use_blitter) {
456 i915->base.resource_copy_region = i915_surface_copy_blitter;
457 i915->base.clear_render_target = i915_clear_render_target_blitter;
458 i915->base.clear_depth_stencil = i915_clear_depth_stencil_blitter;
459 } else {
460 i915->base.resource_copy_region = i915_surface_copy_render;
461 i915->base.clear_render_target = i915_clear_render_target_render;
462 i915->base.clear_depth_stencil = i915_clear_depth_stencil_render;
463 }
464 i915->base.blit = i915_blit;
465 i915->base.flush_resource = i915_flush_resource;
466 i915->base.create_surface = i915_create_surface;
467 i915->base.surface_destroy = i915_surface_destroy;
468 }
469