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