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