Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/drivers/i915/i915_surface.c
4570 views
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
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
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);
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
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
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
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
assert(util_format_get_blocksize(dpt->format) ==
225
util_format_get_blocksize(spt->format));
226
assert(util_format_get_blockwidth(dpt->format) ==
227
util_format_get_blockwidth(spt->format));
228
assert(util_format_get_blockheight(dpt->format) ==
229
util_format_get_blockheight(spt->format));
230
assert(util_format_get_blockwidth(dpt->format) == 1);
231
assert(util_format_get_blockheight(dpt->format) == 1);
232
233
i915_copy_blit(i915_context(pipe), util_format_get_blocksize(dpt->format),
234
(unsigned short)src_tex->stride, src_tex->buffer, src_offset,
235
(unsigned short)dst_tex->stride, dst_tex->buffer, dst_offset,
236
(short)src_box->x, (short)src_box->y, (short)dstx,
237
(short)dsty, (short)src_box->width, (short)src_box->height);
238
}
239
240
static void
241
i915_blit(struct pipe_context *pipe, const struct pipe_blit_info *blit_info)
242
{
243
struct i915_context *i915 = i915_context(pipe);
244
struct pipe_blit_info info = *blit_info;
245
246
if (util_try_blit_via_copy_region(pipe, &info)) {
247
return; /* done */
248
}
249
250
if (info.mask & PIPE_MASK_S) {
251
debug_printf("i915: cannot blit stencil, skipping\n");
252
info.mask &= ~PIPE_MASK_S;
253
}
254
255
if (!util_blitter_is_blit_supported(i915->blitter, &info)) {
256
debug_printf("i915: blit unsupported %s -> %s\n",
257
util_format_short_name(info.src.resource->format),
258
util_format_short_name(info.dst.resource->format));
259
return;
260
}
261
262
i915_util_blitter_save_states(i915);
263
264
util_blitter_blit(i915->blitter, &info);
265
}
266
267
static void
268
i915_flush_resource(struct pipe_context *ctx, struct pipe_resource *resource)
269
{
270
}
271
272
static void
273
i915_clear_render_target_blitter(struct pipe_context *pipe,
274
struct pipe_surface *dst,
275
const union pipe_color_union *color,
276
unsigned dstx, unsigned dsty, unsigned width,
277
unsigned height, bool render_condition_enabled)
278
{
279
struct i915_texture *tex = i915_texture(dst->texture);
280
struct pipe_resource *pt = &tex->b;
281
union util_color uc;
282
unsigned offset =
283
i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
284
285
assert(util_format_get_blockwidth(pt->format) == 1);
286
assert(util_format_get_blockheight(pt->format) == 1);
287
288
util_pack_color(color->f, dst->format, &uc);
289
i915_fill_blit(i915_context(pipe), util_format_get_blocksize(pt->format),
290
XY_COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB,
291
(unsigned short)tex->stride, tex->buffer, offset, (short)dstx,
292
(short)dsty, (short)width, (short)height, uc.ui[0]);
293
}
294
295
static void
296
i915_clear_depth_stencil_blitter(struct pipe_context *pipe,
297
struct pipe_surface *dst, unsigned clear_flags,
298
double depth, unsigned stencil, unsigned dstx,
299
unsigned dsty, unsigned width, unsigned height,
300
bool render_condition_enabled)
301
{
302
struct i915_texture *tex = i915_texture(dst->texture);
303
struct pipe_resource *pt = &tex->b;
304
unsigned packedds;
305
unsigned mask = 0;
306
unsigned offset =
307
i915_texture_offset(tex, dst->u.tex.level, dst->u.tex.first_layer);
308
309
assert(util_format_get_blockwidth(pt->format) == 1);
310
assert(util_format_get_blockheight(pt->format) == 1);
311
312
packedds = util_pack_z_stencil(dst->format, depth, stencil);
313
314
if (clear_flags & PIPE_CLEAR_DEPTH)
315
mask |= XY_COLOR_BLT_WRITE_RGB;
316
/* XXX presumably this does read-modify-write
317
(otherwise this won't work anyway). Hence will only want to
318
do it if really have stencil and it isn't cleared */
319
if ((clear_flags & PIPE_CLEAR_STENCIL) ||
320
(dst->format != PIPE_FORMAT_Z24_UNORM_S8_UINT))
321
mask |= XY_COLOR_BLT_WRITE_ALPHA;
322
323
i915_fill_blit(i915_context(pipe), util_format_get_blocksize(pt->format),
324
mask, (unsigned short)tex->stride, tex->buffer, offset,
325
(short)dstx, (short)dsty, (short)width, (short)height,
326
packedds);
327
}
328
329
/*
330
* Screen surface functions
331
*/
332
333
static void
334
i915_set_color_surface_swizzle(struct i915_surface *surf)
335
{
336
enum pipe_format format = surf->templ.format;
337
338
const struct {
339
enum pipe_format format;
340
uint8_t color_swizzle[4];
341
uint32_t oc_swizzle;
342
} fixup_formats[] = {
343
{PIPE_FORMAT_R8G8B8A8_UNORM, {2, 1, 0, 3}, 0x21030000 /* BGRA */},
344
{PIPE_FORMAT_R8G8B8X8_UNORM, {2, 1, 0, 3}, 0x21030000 /* BGRX */},
345
346
/* These are rendered to using COLORBUF_8BIT, where the G channel written
347
* by shader (and output by blending) is used.
348
*/
349
{PIPE_FORMAT_L8_UNORM, {0, 0, 0, 0}, 0x00030000 /* RRRA */},
350
{PIPE_FORMAT_I8_UNORM, {0, 0, 0, 0}, 0x00030000 /* RRRA */},
351
{PIPE_FORMAT_A8_UNORM, {3, 3, 3, 3}, 0x33330000 /* AAAA */},
352
};
353
354
if (format == PIPE_FORMAT_A8_UNORM)
355
surf->alpha_in_g = true;
356
else if (util_format_is_rgbx_or_bgrx(format))
357
surf->alpha_is_x = true;
358
359
for (int i = 0; i < ARRAY_SIZE(fixup_formats); i++) {
360
if (fixup_formats[i].format == format) {
361
memcpy(surf->color_swizzle, fixup_formats[i].color_swizzle,
362
sizeof(surf->color_swizzle));
363
surf->oc_swizzle = fixup_formats[i].oc_swizzle;
364
return;
365
}
366
}
367
368
for (int i = 0; i < 4; i++)
369
surf->color_swizzle[i] = i;
370
}
371
372
static struct pipe_surface *
373
i915_create_surface_custom(struct pipe_context *ctx, struct pipe_resource *pt,
374
const struct pipe_surface *surf_tmpl,
375
unsigned width0, unsigned height0)
376
{
377
struct i915_texture *tex = i915_texture(pt);
378
struct i915_surface *surf;
379
380
assert(surf_tmpl->u.tex.first_layer == surf_tmpl->u.tex.last_layer);
381
if (pt->target != PIPE_TEXTURE_CUBE && pt->target != PIPE_TEXTURE_3D)
382
assert(surf_tmpl->u.tex.first_layer == 0);
383
384
surf = CALLOC_STRUCT(i915_surface);
385
if (!surf)
386
return NULL;
387
388
struct pipe_surface *ps = &surf->templ;
389
390
pipe_reference_init(&ps->reference, 1);
391
pipe_resource_reference(&ps->texture, pt);
392
ps->format = surf_tmpl->format;
393
ps->width = u_minify(width0, surf_tmpl->u.tex.level);
394
ps->height = u_minify(height0, surf_tmpl->u.tex.level);
395
ps->u.tex.level = surf_tmpl->u.tex.level;
396
ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
397
ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
398
ps->context = ctx;
399
400
if (util_format_is_depth_or_stencil(ps->format)) {
401
surf->buf_info = BUF_3D_ID_DEPTH;
402
} else {
403
surf->buf_info = BUF_3D_ID_COLOR_BACK;
404
405
i915_set_color_surface_swizzle(surf);
406
}
407
408
surf->buf_info |= BUF_3D_PITCH(tex->stride); /* pitch in bytes */
409
410
switch (tex->tiling) {
411
case I915_TILE_Y:
412
surf->buf_info |= BUF_3D_TILED_SURFACE | BUF_3D_TILE_WALK_Y;
413
break;
414
case I915_TILE_X:
415
surf->buf_info |= BUF_3D_TILED_SURFACE;
416
break;
417
case I915_TILE_NONE:
418
break;
419
}
420
421
return ps;
422
}
423
424
static struct pipe_surface *
425
i915_create_surface(struct pipe_context *ctx, struct pipe_resource *pt,
426
const struct pipe_surface *surf_tmpl)
427
{
428
return i915_create_surface_custom(ctx, pt, surf_tmpl, pt->width0,
429
pt->height0);
430
}
431
432
static void
433
i915_surface_destroy(struct pipe_context *ctx, struct pipe_surface *surf)
434
{
435
pipe_resource_reference(&surf->texture, NULL);
436
FREE(surf);
437
}
438
439
void
440
i915_init_surface_functions(struct i915_context *i915)
441
{
442
if (i915_screen(i915->base.screen)->debug.use_blitter) {
443
i915->base.resource_copy_region = i915_surface_copy_blitter;
444
i915->base.clear_render_target = i915_clear_render_target_blitter;
445
i915->base.clear_depth_stencil = i915_clear_depth_stencil_blitter;
446
} else {
447
i915->base.resource_copy_region = i915_surface_copy_render;
448
i915->base.clear_render_target = i915_clear_render_target_render;
449
i915->base.clear_depth_stencil = i915_clear_depth_stencil_render;
450
}
451
i915->base.blit = i915_blit;
452
i915->base.flush_resource = i915_flush_resource;
453
i915->base.create_surface = i915_create_surface;
454
i915->base.surface_destroy = i915_surface_destroy;
455
}
456
457