Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_blitter.c
4561 views
1
/**************************************************************************
2
*
3
* Copyright 2009 Marek Olšák <[email protected]>
4
*
5
* Permission is hereby granted, free of charge, to any person obtaining a
6
* copy of this software and associated documentation files (the
7
* "Software"), to deal in the Software without restriction, including
8
* without limitation the rights to use, copy, modify, merge, publish,
9
* distribute, sub license, and/or sell copies of the Software, and to
10
* permit persons to whom the Software is furnished to do so, subject to
11
* the following conditions:
12
*
13
* The above copyright notice and this permission notice (including the
14
* next paragraph) shall be included in all copies or substantial portions
15
* of the Software.
16
*
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
*
25
**************************************************************************/
26
27
/**
28
* @file
29
* Blitter utility to facilitate acceleration of the clear, clear_render_target,
30
* clear_depth_stencil, resource_copy_region, and blit functions.
31
*
32
* @author Marek Olšák
33
*/
34
35
#include "pipe/p_context.h"
36
#include "pipe/p_defines.h"
37
#include "util/u_inlines.h"
38
#include "pipe/p_shader_tokens.h"
39
#include "pipe/p_state.h"
40
41
#include "util/format/u_format.h"
42
#include "util/u_memory.h"
43
#include "util/u_math.h"
44
#include "util/u_blitter.h"
45
#include "util/u_draw_quad.h"
46
#include "util/u_sampler.h"
47
#include "util/u_simple_shaders.h"
48
#include "util/u_surface.h"
49
#include "util/u_texture.h"
50
#include "util/u_upload_mgr.h"
51
52
#define INVALID_PTR ((void*)~0)
53
54
#define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
55
((clear_buffers) / PIPE_CLEAR_COLOR0)
56
57
#define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
58
#define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
59
60
struct blitter_context_priv
61
{
62
struct blitter_context base;
63
64
float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
65
66
/* Templates for various state objects. */
67
68
/* Constant state objects. */
69
/* Vertex shaders. */
70
void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
71
void *vs_nogeneric;
72
void *vs_pos_only[4]; /**< Vertex shader which passes pos to the output
73
for clear_buffer/copy_buffer.*/
74
void *vs_layered; /**< Vertex shader which sets LAYER = INSTANCEID. */
75
76
/* Fragment shaders. */
77
void *fs_empty;
78
void *fs_write_one_cbuf;
79
void *fs_write_all_cbufs;
80
81
/* FS which outputs a color from a texture where
82
* the 1st index indicates the texture type / destination type,
83
* the 2nd index is the PIPE_TEXTURE_* to be sampled,
84
* the 3rd index is 0 = use TEX, 1 = use TXF.
85
*/
86
void *fs_texfetch_col[5][PIPE_MAX_TEXTURE_TYPES][2];
87
88
/* FS which outputs a depth from a texture, where
89
* the 1st index is the PIPE_TEXTURE_* to be sampled,
90
* the 2nd index is 0 = use TEX, 1 = use TXF.
91
*/
92
void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES][2];
93
void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES][2];
94
void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES][2];
95
96
/* FS which outputs one sample from a multisample texture. */
97
void *fs_texfetch_col_msaa[5][PIPE_MAX_TEXTURE_TYPES];
98
void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
99
void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
100
void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
101
102
/* FS which outputs an average of all samples. */
103
void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
104
105
/* FS which unpacks color to ZS or packs ZS to color, matching
106
* the ZS format. See util_blitter_get_color_format_for_zs().
107
*/
108
void *fs_pack_color_zs[TGSI_TEXTURE_COUNT][10];
109
110
/* FS which is meant for replicating indevidual stencil-buffer bits */
111
void *fs_stencil_blit_fallback[2];
112
113
/* Blend state. */
114
void *blend[PIPE_MASK_RGBA+1][2]; /**< blend state with writemask */
115
void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1];
116
117
/* Depth stencil alpha state. */
118
void *dsa_write_depth_stencil;
119
void *dsa_write_depth_keep_stencil;
120
void *dsa_keep_depth_stencil;
121
void *dsa_keep_depth_write_stencil;
122
void *dsa_replicate_stencil_bit[8];
123
124
/* Vertex elements states. */
125
void *velem_state;
126
void *velem_state_readbuf[4]; /**< X, XY, XYZ, XYZW */
127
128
/* Sampler state. */
129
void *sampler_state;
130
void *sampler_state_linear;
131
void *sampler_state_rect;
132
void *sampler_state_rect_linear;
133
134
/* Rasterizer state. */
135
void *rs_state[2][2]; /**< [scissor][msaa] */
136
void *rs_discard_state;
137
138
/* Destination surface dimensions. */
139
unsigned dst_width;
140
unsigned dst_height;
141
142
void *custom_vs;
143
144
bool has_geometry_shader;
145
bool has_tessellation;
146
bool has_layered;
147
bool has_stream_out;
148
bool has_stencil_export;
149
bool has_texture_multisample;
150
bool has_tex_lz;
151
bool has_txf;
152
bool cube_as_2darray;
153
bool cached_all_shaders;
154
155
/* The Draw module overrides these functions.
156
* Always create the blitter before Draw. */
157
void (*bind_fs_state)(struct pipe_context *, void *);
158
void (*delete_fs_state)(struct pipe_context *, void *);
159
};
160
161
struct blitter_context *util_blitter_create(struct pipe_context *pipe)
162
{
163
struct blitter_context_priv *ctx;
164
struct pipe_blend_state blend;
165
struct pipe_depth_stencil_alpha_state dsa;
166
struct pipe_rasterizer_state rs_state;
167
struct pipe_sampler_state sampler_state;
168
struct pipe_vertex_element velem[2];
169
unsigned i, j;
170
171
ctx = CALLOC_STRUCT(blitter_context_priv);
172
if (!ctx)
173
return NULL;
174
175
ctx->base.pipe = pipe;
176
ctx->base.draw_rectangle = util_blitter_draw_rectangle;
177
178
ctx->bind_fs_state = pipe->bind_fs_state;
179
ctx->delete_fs_state = pipe->delete_fs_state;
180
181
/* init state objects for them to be considered invalid */
182
ctx->base.saved_blend_state = INVALID_PTR;
183
ctx->base.saved_dsa_state = INVALID_PTR;
184
ctx->base.saved_rs_state = INVALID_PTR;
185
ctx->base.saved_fs = INVALID_PTR;
186
ctx->base.saved_vs = INVALID_PTR;
187
ctx->base.saved_gs = INVALID_PTR;
188
ctx->base.saved_velem_state = INVALID_PTR;
189
ctx->base.saved_fb_state.nr_cbufs = ~0;
190
ctx->base.saved_num_sampler_views = ~0;
191
ctx->base.saved_num_sampler_states = ~0;
192
ctx->base.saved_num_so_targets = ~0;
193
194
ctx->has_geometry_shader =
195
pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
196
PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
197
198
ctx->has_tessellation =
199
pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL,
200
PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
201
202
ctx->has_stream_out =
203
pipe->screen->get_param(pipe->screen,
204
PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
205
206
ctx->has_stencil_export =
207
pipe->screen->get_param(pipe->screen,
208
PIPE_CAP_SHADER_STENCIL_EXPORT);
209
210
ctx->has_texture_multisample =
211
pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE);
212
213
ctx->has_tex_lz = pipe->screen->get_param(pipe->screen,
214
PIPE_CAP_TGSI_TEX_TXF_LZ);
215
ctx->has_txf = pipe->screen->get_param(pipe->screen,
216
PIPE_CAP_GLSL_FEATURE_LEVEL) > 130;
217
ctx->cube_as_2darray = pipe->screen->get_param(pipe->screen,
218
PIPE_CAP_SAMPLER_VIEW_TARGET);
219
220
/* blend state objects */
221
memset(&blend, 0, sizeof(blend));
222
223
for (i = 0; i <= PIPE_MASK_RGBA; i++) {
224
for (j = 0; j < 2; j++) {
225
memset(&blend.rt[0], 0, sizeof(blend.rt[0]));
226
blend.rt[0].colormask = i;
227
if (j) {
228
blend.rt[0].blend_enable = 1;
229
blend.rt[0].rgb_func = PIPE_BLEND_ADD;
230
blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
231
blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
232
blend.rt[0].alpha_func = PIPE_BLEND_ADD;
233
blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
234
blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
235
}
236
ctx->blend[i][j] = pipe->create_blend_state(pipe, &blend);
237
}
238
}
239
240
/* depth stencil alpha state objects */
241
memset(&dsa, 0, sizeof(dsa));
242
ctx->dsa_keep_depth_stencil =
243
pipe->create_depth_stencil_alpha_state(pipe, &dsa);
244
245
dsa.depth_enabled = 1;
246
dsa.depth_writemask = 1;
247
dsa.depth_func = PIPE_FUNC_ALWAYS;
248
ctx->dsa_write_depth_keep_stencil =
249
pipe->create_depth_stencil_alpha_state(pipe, &dsa);
250
251
dsa.stencil[0].enabled = 1;
252
dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
253
dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
254
dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
255
dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
256
dsa.stencil[0].valuemask = 0xff;
257
dsa.stencil[0].writemask = 0xff;
258
ctx->dsa_write_depth_stencil =
259
pipe->create_depth_stencil_alpha_state(pipe, &dsa);
260
261
dsa.depth_enabled = 0;
262
dsa.depth_writemask = 0;
263
ctx->dsa_keep_depth_write_stencil =
264
pipe->create_depth_stencil_alpha_state(pipe, &dsa);
265
266
/* sampler state */
267
memset(&sampler_state, 0, sizeof(sampler_state));
268
sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
269
sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
270
sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
271
sampler_state.normalized_coords = 1;
272
ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
273
sampler_state.normalized_coords = 0;
274
ctx->sampler_state_rect = pipe->create_sampler_state(pipe, &sampler_state);
275
276
sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
277
sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
278
sampler_state.normalized_coords = 1;
279
ctx->sampler_state_linear = pipe->create_sampler_state(pipe, &sampler_state);
280
sampler_state.normalized_coords = 0;
281
ctx->sampler_state_rect_linear = pipe->create_sampler_state(pipe, &sampler_state);
282
283
/* rasterizer state */
284
memset(&rs_state, 0, sizeof(rs_state));
285
rs_state.cull_face = PIPE_FACE_NONE;
286
rs_state.half_pixel_center = 1;
287
rs_state.bottom_edge_rule = 1;
288
rs_state.flatshade = 1;
289
rs_state.depth_clip_near = 1;
290
rs_state.depth_clip_far = 1;
291
292
unsigned scissor, msaa;
293
for (scissor = 0; scissor < 2; scissor++) {
294
for (msaa = 0; msaa < 2; msaa++) {
295
rs_state.scissor = scissor;
296
rs_state.multisample = msaa;
297
ctx->rs_state[scissor][msaa] =
298
pipe->create_rasterizer_state(pipe, &rs_state);
299
}
300
}
301
302
if (ctx->has_stream_out) {
303
rs_state.scissor = rs_state.multisample = 0;
304
rs_state.rasterizer_discard = 1;
305
ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
306
}
307
308
ctx->base.cb_slot = 0; /* 0 for now */
309
ctx->base.vb_slot = 0; /* 0 for now */
310
311
/* vertex elements states */
312
memset(&velem[0], 0, sizeof(velem[0]) * 2);
313
for (i = 0; i < 2; i++) {
314
velem[i].src_offset = i * 4 * sizeof(float);
315
velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
316
velem[i].vertex_buffer_index = ctx->base.vb_slot;
317
}
318
ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
319
320
if (ctx->has_stream_out) {
321
static enum pipe_format formats[4] = {
322
PIPE_FORMAT_R32_UINT,
323
PIPE_FORMAT_R32G32_UINT,
324
PIPE_FORMAT_R32G32B32_UINT,
325
PIPE_FORMAT_R32G32B32A32_UINT
326
};
327
328
for (i = 0; i < 4; i++) {
329
velem[0].src_format = formats[i];
330
velem[0].vertex_buffer_index = ctx->base.vb_slot;
331
ctx->velem_state_readbuf[i] =
332
pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
333
}
334
}
335
336
ctx->has_layered =
337
pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) &&
338
pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT);
339
340
/* set invariant vertex coordinates */
341
for (i = 0; i < 4; i++) {
342
ctx->vertices[i][0][2] = 0; /*v.z*/
343
ctx->vertices[i][0][3] = 1; /*v.w*/
344
}
345
346
return &ctx->base;
347
}
348
349
void *util_blitter_get_noop_blend_state(struct blitter_context *blitter)
350
{
351
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
352
353
return ctx->blend[0][0];
354
}
355
356
void *util_blitter_get_noop_dsa_state(struct blitter_context *blitter)
357
{
358
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
359
360
return ctx->dsa_keep_depth_stencil;
361
}
362
363
void *util_blitter_get_discard_rasterizer_state(struct blitter_context *blitter)
364
{
365
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
366
367
return ctx->rs_discard_state;
368
}
369
370
static void bind_vs_pos_only(struct blitter_context_priv *ctx,
371
unsigned num_so_channels)
372
{
373
struct pipe_context *pipe = ctx->base.pipe;
374
int index = num_so_channels ? num_so_channels - 1 : 0;
375
376
if (!ctx->vs_pos_only[index]) {
377
struct pipe_stream_output_info so;
378
static const enum tgsi_semantic semantic_names[] =
379
{ TGSI_SEMANTIC_POSITION };
380
const uint semantic_indices[] = { 0 };
381
382
memset(&so, 0, sizeof(so));
383
so.num_outputs = 1;
384
so.output[0].num_components = num_so_channels;
385
so.stride[0] = num_so_channels;
386
387
ctx->vs_pos_only[index] =
388
util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
389
semantic_indices, false,
390
false, &so);
391
}
392
393
pipe->bind_vs_state(pipe, ctx->vs_pos_only[index]);
394
}
395
396
static void *get_vs_passthrough_pos_generic(struct blitter_context *blitter)
397
{
398
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
399
struct pipe_context *pipe = ctx->base.pipe;
400
401
if (!ctx->vs) {
402
static const enum tgsi_semantic semantic_names[] =
403
{ TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_GENERIC };
404
const uint semantic_indices[] = { 0, 0 };
405
ctx->vs =
406
util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
407
semantic_indices, false);
408
}
409
return ctx->vs;
410
}
411
412
static void *get_vs_passthrough_pos(struct blitter_context *blitter)
413
{
414
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
415
struct pipe_context *pipe = ctx->base.pipe;
416
417
if (!ctx->vs_nogeneric) {
418
static const enum tgsi_semantic semantic_names[] =
419
{ TGSI_SEMANTIC_POSITION };
420
const uint semantic_indices[] = { 0 };
421
422
ctx->vs_nogeneric =
423
util_make_vertex_passthrough_shader(pipe, 1,
424
semantic_names,
425
semantic_indices, false);
426
}
427
return ctx->vs_nogeneric;
428
}
429
430
static void *get_vs_layered(struct blitter_context *blitter)
431
{
432
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
433
struct pipe_context *pipe = ctx->base.pipe;
434
435
if (!ctx->vs_layered) {
436
ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe);
437
}
438
return ctx->vs_layered;
439
}
440
441
static void bind_fs_empty(struct blitter_context_priv *ctx)
442
{
443
struct pipe_context *pipe = ctx->base.pipe;
444
445
if (!ctx->fs_empty) {
446
assert(!ctx->cached_all_shaders);
447
ctx->fs_empty = util_make_empty_fragment_shader(pipe);
448
}
449
450
ctx->bind_fs_state(pipe, ctx->fs_empty);
451
}
452
453
static void bind_fs_write_one_cbuf(struct blitter_context_priv *ctx)
454
{
455
struct pipe_context *pipe = ctx->base.pipe;
456
457
if (!ctx->fs_write_one_cbuf) {
458
assert(!ctx->cached_all_shaders);
459
ctx->fs_write_one_cbuf =
460
util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
461
TGSI_INTERPOLATE_CONSTANT, false);
462
}
463
464
ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
465
}
466
467
static void bind_fs_write_all_cbufs(struct blitter_context_priv *ctx)
468
{
469
struct pipe_context *pipe = ctx->base.pipe;
470
471
if (!ctx->fs_write_all_cbufs) {
472
assert(!ctx->cached_all_shaders);
473
ctx->fs_write_all_cbufs =
474
util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
475
TGSI_INTERPOLATE_CONSTANT, true);
476
}
477
478
ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs);
479
}
480
481
void util_blitter_destroy(struct blitter_context *blitter)
482
{
483
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
484
struct pipe_context *pipe = blitter->pipe;
485
unsigned i, j, f;
486
487
for (i = 0; i <= PIPE_MASK_RGBA; i++)
488
for (j = 0; j < 2; j++)
489
pipe->delete_blend_state(pipe, ctx->blend[i][j]);
490
491
for (i = 0; i < ARRAY_SIZE(ctx->blend_clear); i++) {
492
if (ctx->blend_clear[i])
493
pipe->delete_blend_state(pipe, ctx->blend_clear[i]);
494
}
495
pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
496
pipe->delete_depth_stencil_alpha_state(pipe,
497
ctx->dsa_write_depth_keep_stencil);
498
pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
499
pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
500
501
for (i = 0; i < ARRAY_SIZE(ctx->dsa_replicate_stencil_bit); i++) {
502
if (ctx->dsa_replicate_stencil_bit[i])
503
pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_replicate_stencil_bit[i]);
504
}
505
506
unsigned scissor, msaa;
507
for (scissor = 0; scissor < 2; scissor++) {
508
for (msaa = 0; msaa < 2; msaa++) {
509
pipe->delete_rasterizer_state(pipe, ctx->rs_state[scissor][msaa]);
510
}
511
}
512
513
if (ctx->rs_discard_state)
514
pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
515
if (ctx->vs)
516
pipe->delete_vs_state(pipe, ctx->vs);
517
if (ctx->vs_nogeneric)
518
pipe->delete_vs_state(pipe, ctx->vs_nogeneric);
519
for (i = 0; i < 4; i++)
520
if (ctx->vs_pos_only[i])
521
pipe->delete_vs_state(pipe, ctx->vs_pos_only[i]);
522
if (ctx->vs_layered)
523
pipe->delete_vs_state(pipe, ctx->vs_layered);
524
pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
525
for (i = 0; i < 4; i++) {
526
if (ctx->velem_state_readbuf[i]) {
527
pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf[i]);
528
}
529
}
530
531
for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
532
for (unsigned type = 0; type < ARRAY_SIZE(ctx->fs_texfetch_col); ++type) {
533
for (unsigned inst = 0; inst < 2; inst++) {
534
if (ctx->fs_texfetch_col[type][i][inst])
535
ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[type][i][inst]);
536
}
537
if (ctx->fs_texfetch_col_msaa[type][i])
538
ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[type][i]);
539
}
540
541
for (unsigned inst = 0; inst < 2; inst++) {
542
if (ctx->fs_texfetch_depth[i][inst])
543
ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i][inst]);
544
if (ctx->fs_texfetch_depthstencil[i][inst])
545
ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i][inst]);
546
if (ctx->fs_texfetch_stencil[i][inst])
547
ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i][inst]);
548
}
549
550
if (ctx->fs_texfetch_depth_msaa[i])
551
ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]);
552
if (ctx->fs_texfetch_depthstencil_msaa[i])
553
ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil_msaa[i]);
554
if (ctx->fs_texfetch_stencil_msaa[i])
555
ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]);
556
557
for (j = 0; j< ARRAY_SIZE(ctx->fs_resolve[i]); j++)
558
for (f = 0; f < 2; f++)
559
if (ctx->fs_resolve[i][j][f])
560
ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]);
561
}
562
563
for (i = 0; i < ARRAY_SIZE(ctx->fs_pack_color_zs); i++) {
564
for (j = 0; j < ARRAY_SIZE(ctx->fs_pack_color_zs[0]); j++) {
565
if (ctx->fs_pack_color_zs[i][j])
566
ctx->delete_fs_state(pipe, ctx->fs_pack_color_zs[i][j]);
567
}
568
}
569
570
if (ctx->fs_empty)
571
ctx->delete_fs_state(pipe, ctx->fs_empty);
572
if (ctx->fs_write_one_cbuf)
573
ctx->delete_fs_state(pipe, ctx->fs_write_one_cbuf);
574
if (ctx->fs_write_all_cbufs)
575
ctx->delete_fs_state(pipe, ctx->fs_write_all_cbufs);
576
577
for (i = 0; i < ARRAY_SIZE(ctx->fs_stencil_blit_fallback); ++i)
578
if (ctx->fs_stencil_blit_fallback[i])
579
ctx->delete_fs_state(pipe, ctx->fs_stencil_blit_fallback[i]);
580
581
pipe->delete_sampler_state(pipe, ctx->sampler_state_rect_linear);
582
pipe->delete_sampler_state(pipe, ctx->sampler_state_rect);
583
pipe->delete_sampler_state(pipe, ctx->sampler_state_linear);
584
pipe->delete_sampler_state(pipe, ctx->sampler_state);
585
FREE(ctx);
586
}
587
588
void util_blitter_set_texture_multisample(struct blitter_context *blitter,
589
bool supported)
590
{
591
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
592
593
ctx->has_texture_multisample = supported;
594
}
595
596
void util_blitter_set_running_flag(struct blitter_context *blitter)
597
{
598
if (blitter->running) {
599
_debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
600
__LINE__);
601
}
602
blitter->running = true;
603
604
blitter->pipe->set_active_query_state(blitter->pipe, false);
605
}
606
607
void util_blitter_unset_running_flag(struct blitter_context *blitter)
608
{
609
if (!blitter->running) {
610
_debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
611
__LINE__);
612
}
613
blitter->running = false;
614
615
blitter->pipe->set_active_query_state(blitter->pipe, true);
616
}
617
618
static void blitter_check_saved_vertex_states(ASSERTED struct blitter_context_priv *ctx)
619
{
620
assert(ctx->base.saved_vs != INVALID_PTR);
621
assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
622
assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR);
623
assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR);
624
assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0u);
625
assert(ctx->base.saved_rs_state != INVALID_PTR);
626
}
627
628
void util_blitter_restore_vertex_states(struct blitter_context *blitter)
629
{
630
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
631
struct pipe_context *pipe = ctx->base.pipe;
632
unsigned i;
633
634
/* Vertex buffer. */
635
if (ctx->base.saved_vertex_buffer.buffer.resource) {
636
pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, 0, true,
637
&ctx->base.saved_vertex_buffer);
638
ctx->base.saved_vertex_buffer.buffer.resource = NULL;
639
}
640
641
/* Vertex elements. */
642
if (ctx->base.saved_velem_state != INVALID_PTR) {
643
pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
644
ctx->base.saved_velem_state = INVALID_PTR;
645
}
646
647
/* Vertex shader. */
648
pipe->bind_vs_state(pipe, ctx->base.saved_vs);
649
ctx->base.saved_vs = INVALID_PTR;
650
651
/* Geometry shader. */
652
if (ctx->has_geometry_shader) {
653
pipe->bind_gs_state(pipe, ctx->base.saved_gs);
654
ctx->base.saved_gs = INVALID_PTR;
655
}
656
657
if (ctx->has_tessellation) {
658
pipe->bind_tcs_state(pipe, ctx->base.saved_tcs);
659
pipe->bind_tes_state(pipe, ctx->base.saved_tes);
660
ctx->base.saved_tcs = INVALID_PTR;
661
ctx->base.saved_tes = INVALID_PTR;
662
}
663
664
/* Stream outputs. */
665
if (ctx->has_stream_out) {
666
unsigned offsets[PIPE_MAX_SO_BUFFERS];
667
for (i = 0; i < ctx->base.saved_num_so_targets; i++)
668
offsets[i] = (unsigned)-1;
669
pipe->set_stream_output_targets(pipe,
670
ctx->base.saved_num_so_targets,
671
ctx->base.saved_so_targets, offsets);
672
673
for (i = 0; i < ctx->base.saved_num_so_targets; i++)
674
pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
675
676
ctx->base.saved_num_so_targets = ~0;
677
}
678
679
/* Rasterizer. */
680
pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
681
ctx->base.saved_rs_state = INVALID_PTR;
682
}
683
684
static void blitter_check_saved_fragment_states(ASSERTED struct blitter_context_priv *ctx)
685
{
686
assert(ctx->base.saved_fs != INVALID_PTR);
687
assert(ctx->base.saved_dsa_state != INVALID_PTR);
688
assert(ctx->base.saved_blend_state != INVALID_PTR);
689
}
690
691
void util_blitter_restore_fragment_states(struct blitter_context *blitter)
692
{
693
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
694
struct pipe_context *pipe = ctx->base.pipe;
695
696
/* Fragment shader. */
697
ctx->bind_fs_state(pipe, ctx->base.saved_fs);
698
ctx->base.saved_fs = INVALID_PTR;
699
700
/* Depth, stencil, alpha. */
701
pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
702
ctx->base.saved_dsa_state = INVALID_PTR;
703
704
/* Blend state. */
705
pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
706
ctx->base.saved_blend_state = INVALID_PTR;
707
708
/* Sample mask. */
709
if (ctx->base.is_sample_mask_saved) {
710
pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
711
ctx->base.is_sample_mask_saved = false;
712
}
713
714
/* Miscellaneous states. */
715
/* XXX check whether these are saved and whether they need to be restored
716
* (depending on the operation) */
717
pipe->set_stencil_ref(pipe, ctx->base.saved_stencil_ref);
718
719
if (!blitter->skip_viewport_restore)
720
pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
721
722
if (blitter->saved_num_window_rectangles) {
723
pipe->set_window_rectangles(pipe,
724
blitter->saved_window_rectangles_include,
725
blitter->saved_num_window_rectangles,
726
blitter->saved_window_rectangles);
727
}
728
}
729
730
static void blitter_check_saved_fb_state(ASSERTED struct blitter_context_priv *ctx)
731
{
732
assert(ctx->base.saved_fb_state.nr_cbufs != (ubyte) ~0);
733
}
734
735
static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
736
{
737
struct pipe_context *pipe = ctx->base.pipe;
738
739
if (ctx->base.saved_render_cond_query) {
740
pipe->render_condition(pipe, NULL, false, 0);
741
}
742
}
743
744
void util_blitter_restore_render_cond(struct blitter_context *blitter)
745
{
746
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
747
struct pipe_context *pipe = ctx->base.pipe;
748
749
if (ctx->base.saved_render_cond_query) {
750
pipe->render_condition(pipe, ctx->base.saved_render_cond_query,
751
ctx->base.saved_render_cond_cond,
752
ctx->base.saved_render_cond_mode);
753
ctx->base.saved_render_cond_query = NULL;
754
}
755
}
756
757
void util_blitter_restore_fb_state(struct blitter_context *blitter)
758
{
759
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
760
struct pipe_context *pipe = ctx->base.pipe;
761
762
pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
763
util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
764
}
765
766
static void blitter_check_saved_textures(ASSERTED struct blitter_context_priv *ctx)
767
{
768
assert(ctx->base.saved_num_sampler_states != ~0u);
769
assert(ctx->base.saved_num_sampler_views != ~0u);
770
}
771
772
void util_blitter_restore_textures(struct blitter_context *blitter)
773
{
774
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
775
struct pipe_context *pipe = ctx->base.pipe;
776
unsigned i;
777
778
/* Fragment sampler states. */
779
pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
780
ctx->base.saved_num_sampler_states,
781
ctx->base.saved_sampler_states);
782
783
ctx->base.saved_num_sampler_states = ~0;
784
785
/* Fragment sampler views. */
786
pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
787
ctx->base.saved_num_sampler_views, 0,
788
ctx->base.saved_sampler_views);
789
790
for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
791
pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
792
793
ctx->base.saved_num_sampler_views = ~0;
794
}
795
796
void util_blitter_restore_constant_buffer_state(struct blitter_context *blitter)
797
{
798
struct pipe_context *pipe = blitter->pipe;
799
800
pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, blitter->cb_slot,
801
true, &blitter->saved_fs_constant_buffer);
802
blitter->saved_fs_constant_buffer.buffer = NULL;
803
}
804
805
static void blitter_set_rectangle(struct blitter_context_priv *ctx,
806
int x1, int y1, int x2, int y2,
807
float depth)
808
{
809
/* set vertex positions */
810
ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
811
ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
812
813
ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
814
ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
815
816
ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
817
ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
818
819
ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
820
ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
821
822
/* viewport */
823
struct pipe_viewport_state viewport;
824
viewport.scale[0] = 0.5f * ctx->dst_width;
825
viewport.scale[1] = 0.5f * ctx->dst_height;
826
viewport.scale[2] = 0.0f;
827
viewport.translate[0] = 0.5f * ctx->dst_width;
828
viewport.translate[1] = 0.5f * ctx->dst_height;
829
viewport.translate[2] = depth;
830
viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
831
viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
832
viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
833
viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
834
ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &viewport);
835
}
836
837
static void blitter_set_clear_color(struct blitter_context_priv *ctx,
838
const float color[4])
839
{
840
int i;
841
842
if (color) {
843
for (i = 0; i < 4; i++)
844
memcpy(&ctx->vertices[i][1][0], color, sizeof(uint32_t) * 4);
845
} else {
846
for (i = 0; i < 4; i++)
847
memset(&ctx->vertices[i][1][0], 0, sizeof(uint32_t) * 4);
848
}
849
}
850
851
static void get_texcoords(struct pipe_sampler_view *src,
852
unsigned src_width0, unsigned src_height0,
853
int x1, int y1, int x2, int y2,
854
float layer, unsigned sample,
855
bool uses_txf, union blitter_attrib *out)
856
{
857
unsigned level = src->u.tex.first_level;
858
bool normalized = !uses_txf &&
859
src->target != PIPE_TEXTURE_RECT &&
860
src->texture->nr_samples <= 1;
861
862
if (normalized) {
863
out->texcoord.x1 = x1 / (float)u_minify(src_width0, level);
864
out->texcoord.y1 = y1 / (float)u_minify(src_height0, level);
865
out->texcoord.x2 = x2 / (float)u_minify(src_width0, level);
866
out->texcoord.y2 = y2 / (float)u_minify(src_height0, level);
867
} else {
868
out->texcoord.x1 = x1;
869
out->texcoord.y1 = y1;
870
out->texcoord.x2 = x2;
871
out->texcoord.y2 = y2;
872
}
873
874
out->texcoord.z = 0;
875
out->texcoord.w = 0;
876
877
/* Set the layer. */
878
switch (src->target) {
879
case PIPE_TEXTURE_3D:
880
{
881
float r = layer;
882
883
if (!uses_txf)
884
r /= u_minify(src->texture->depth0, src->u.tex.first_level);
885
886
out->texcoord.z = r;
887
}
888
break;
889
890
case PIPE_TEXTURE_1D_ARRAY:
891
out->texcoord.y1 = out->texcoord.y2 = layer;
892
break;
893
894
case PIPE_TEXTURE_2D_ARRAY:
895
out->texcoord.z = layer;
896
out->texcoord.w = sample;
897
break;
898
899
case PIPE_TEXTURE_CUBE_ARRAY:
900
out->texcoord.w = (unsigned)layer / 6;
901
break;
902
903
case PIPE_TEXTURE_2D:
904
out->texcoord.w = sample;
905
break;
906
907
default:;
908
}
909
}
910
911
static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
912
unsigned width, unsigned height)
913
{
914
ctx->dst_width = width;
915
ctx->dst_height = height;
916
}
917
918
static void set_texcoords_in_vertices(const union blitter_attrib *attrib,
919
float *out, unsigned stride)
920
{
921
out[0] = attrib->texcoord.x1;
922
out[1] = attrib->texcoord.y1;
923
out += stride;
924
out[0] = attrib->texcoord.x2;
925
out[1] = attrib->texcoord.y1;
926
out += stride;
927
out[0] = attrib->texcoord.x2;
928
out[1] = attrib->texcoord.y2;
929
out += stride;
930
out[0] = attrib->texcoord.x1;
931
out[1] = attrib->texcoord.y2;
932
}
933
934
static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
935
enum pipe_format src_format,
936
enum pipe_format dst_format,
937
enum pipe_texture_target target,
938
unsigned src_nr_samples,
939
unsigned dst_nr_samples,
940
unsigned filter,
941
bool use_txf)
942
{
943
struct pipe_context *pipe = ctx->base.pipe;
944
enum tgsi_texture_type tgsi_tex =
945
util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
946
enum tgsi_return_type stype;
947
enum tgsi_return_type dtype;
948
unsigned type;
949
950
assert(target < PIPE_MAX_TEXTURE_TYPES);
951
952
if (util_format_is_pure_uint(src_format)) {
953
stype = TGSI_RETURN_TYPE_UINT;
954
if (util_format_is_pure_uint(dst_format)) {
955
dtype = TGSI_RETURN_TYPE_UINT;
956
type = 0;
957
} else {
958
assert(util_format_is_pure_sint(dst_format));
959
dtype = TGSI_RETURN_TYPE_SINT;
960
type = 1;
961
}
962
} else if (util_format_is_pure_sint(src_format)) {
963
stype = TGSI_RETURN_TYPE_SINT;
964
if (util_format_is_pure_sint(dst_format)) {
965
dtype = TGSI_RETURN_TYPE_SINT;
966
type = 2;
967
} else {
968
assert(util_format_is_pure_uint(dst_format));
969
dtype = TGSI_RETURN_TYPE_UINT;
970
type = 3;
971
}
972
} else {
973
assert(!util_format_is_pure_uint(dst_format) &&
974
!util_format_is_pure_sint(dst_format));
975
dtype = stype = TGSI_RETURN_TYPE_FLOAT;
976
type = 4;
977
}
978
979
if (src_nr_samples > 1) {
980
void **shader;
981
982
/* OpenGL requires that integer textures just copy 1 sample instead
983
* of averaging.
984
*/
985
if (dst_nr_samples <= 1 &&
986
stype != TGSI_RETURN_TYPE_UINT &&
987
stype != TGSI_RETURN_TYPE_SINT) {
988
/* The destination has one sample, so we'll do color resolve. */
989
unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
990
991
assert(filter < 2);
992
993
shader = &ctx->fs_resolve[target][index][filter];
994
995
if (!*shader) {
996
assert(!ctx->cached_all_shaders);
997
if (filter == PIPE_TEX_FILTER_LINEAR) {
998
*shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
999
src_nr_samples,
1000
stype);
1001
}
1002
else {
1003
*shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
1004
src_nr_samples,
1005
stype);
1006
}
1007
}
1008
}
1009
else {
1010
/* The destination has multiple samples, we'll do
1011
* an MSAA->MSAA copy.
1012
*/
1013
shader = &ctx->fs_texfetch_col_msaa[type][target];
1014
1015
/* Create the fragment shader on-demand. */
1016
if (!*shader) {
1017
assert(!ctx->cached_all_shaders);
1018
*shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype, dtype);
1019
}
1020
}
1021
1022
return *shader;
1023
} else {
1024
void **shader;
1025
1026
if (use_txf)
1027
shader = &ctx->fs_texfetch_col[type][target][1];
1028
else
1029
shader = &ctx->fs_texfetch_col[type][target][0];
1030
1031
/* Create the fragment shader on-demand. */
1032
if (!*shader) {
1033
assert(!ctx->cached_all_shaders);
1034
*shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
1035
TGSI_INTERPOLATE_LINEAR,
1036
stype, dtype,
1037
ctx->has_tex_lz, use_txf);
1038
}
1039
1040
return *shader;
1041
}
1042
}
1043
1044
static inline
1045
void *blitter_get_fs_pack_color_zs(struct blitter_context_priv *ctx,
1046
enum pipe_texture_target target,
1047
unsigned nr_samples,
1048
enum pipe_format zs_format,
1049
bool dst_is_color)
1050
{
1051
struct pipe_context *pipe = ctx->base.pipe;
1052
enum tgsi_texture_type tgsi_tex =
1053
util_pipe_tex_to_tgsi_tex(target, nr_samples);
1054
int format_index = zs_format == PIPE_FORMAT_Z24_UNORM_S8_UINT ? 0 :
1055
zs_format == PIPE_FORMAT_S8_UINT_Z24_UNORM ? 1 :
1056
zs_format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT ? 2 :
1057
zs_format == PIPE_FORMAT_Z24X8_UNORM ? 3 :
1058
zs_format == PIPE_FORMAT_X8Z24_UNORM ? 4 : -1;
1059
1060
if (format_index == -1) {
1061
assert(0);
1062
return NULL;
1063
}
1064
1065
/* The first 5 shaders pack ZS to color, the last 5 shaders unpack color
1066
* to ZS.
1067
*/
1068
if (dst_is_color)
1069
format_index += 5;
1070
1071
void **shader = &ctx->fs_pack_color_zs[tgsi_tex][format_index];
1072
1073
/* Create the fragment shader on-demand. */
1074
if (!*shader) {
1075
assert(!ctx->cached_all_shaders);
1076
*shader = util_make_fs_pack_color_zs(pipe, tgsi_tex, zs_format,
1077
dst_is_color);
1078
}
1079
return *shader;
1080
}
1081
1082
static inline
1083
void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
1084
enum pipe_texture_target target,
1085
unsigned nr_samples,
1086
bool use_txf)
1087
{
1088
struct pipe_context *pipe = ctx->base.pipe;
1089
1090
assert(target < PIPE_MAX_TEXTURE_TYPES);
1091
1092
if (nr_samples > 1) {
1093
void **shader = &ctx->fs_texfetch_depth_msaa[target];
1094
1095
/* Create the fragment shader on-demand. */
1096
if (!*shader) {
1097
enum tgsi_texture_type tgsi_tex;
1098
assert(!ctx->cached_all_shaders);
1099
tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1100
*shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
1101
}
1102
1103
return *shader;
1104
} else {
1105
void **shader;
1106
1107
if (use_txf)
1108
shader = &ctx->fs_texfetch_depth[target][1];
1109
else
1110
shader = &ctx->fs_texfetch_depth[target][0];
1111
1112
/* Create the fragment shader on-demand. */
1113
if (!*shader) {
1114
enum tgsi_texture_type tgsi_tex;
1115
assert(!ctx->cached_all_shaders);
1116
tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1117
*shader = util_make_fs_blit_zs(pipe, PIPE_MASK_Z, tgsi_tex,
1118
ctx->has_tex_lz, use_txf);
1119
}
1120
1121
return *shader;
1122
}
1123
}
1124
1125
static inline
1126
void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
1127
enum pipe_texture_target target,
1128
unsigned nr_samples,
1129
bool use_txf)
1130
{
1131
struct pipe_context *pipe = ctx->base.pipe;
1132
1133
assert(target < PIPE_MAX_TEXTURE_TYPES);
1134
1135
if (nr_samples > 1) {
1136
void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
1137
1138
/* Create the fragment shader on-demand. */
1139
if (!*shader) {
1140
enum tgsi_texture_type tgsi_tex;
1141
assert(!ctx->cached_all_shaders);
1142
tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1143
*shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
1144
}
1145
1146
return *shader;
1147
} else {
1148
void **shader;
1149
1150
if (use_txf)
1151
shader = &ctx->fs_texfetch_depthstencil[target][1];
1152
else
1153
shader = &ctx->fs_texfetch_depthstencil[target][0];
1154
1155
/* Create the fragment shader on-demand. */
1156
if (!*shader) {
1157
enum tgsi_texture_type tgsi_tex;
1158
assert(!ctx->cached_all_shaders);
1159
tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1160
*shader = util_make_fs_blit_zs(pipe, PIPE_MASK_ZS, tgsi_tex,
1161
ctx->has_tex_lz, use_txf);
1162
}
1163
1164
return *shader;
1165
}
1166
}
1167
1168
static inline
1169
void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
1170
enum pipe_texture_target target,
1171
unsigned nr_samples,
1172
bool use_txf)
1173
{
1174
struct pipe_context *pipe = ctx->base.pipe;
1175
1176
assert(target < PIPE_MAX_TEXTURE_TYPES);
1177
1178
if (nr_samples > 1) {
1179
void **shader = &ctx->fs_texfetch_stencil_msaa[target];
1180
1181
/* Create the fragment shader on-demand. */
1182
if (!*shader) {
1183
enum tgsi_texture_type tgsi_tex;
1184
assert(!ctx->cached_all_shaders);
1185
tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1186
*shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
1187
}
1188
1189
return *shader;
1190
} else {
1191
void **shader;
1192
1193
if (use_txf)
1194
shader = &ctx->fs_texfetch_stencil[target][1];
1195
else
1196
shader = &ctx->fs_texfetch_stencil[target][0];
1197
1198
/* Create the fragment shader on-demand. */
1199
if (!*shader) {
1200
enum tgsi_texture_type tgsi_tex;
1201
assert(!ctx->cached_all_shaders);
1202
tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1203
*shader = util_make_fs_blit_zs(pipe, PIPE_MASK_S, tgsi_tex,
1204
ctx->has_tex_lz, use_txf);
1205
}
1206
1207
return *shader;
1208
}
1209
}
1210
1211
1212
/**
1213
* Generate and save all fragment shaders that we will ever need for
1214
* blitting. Drivers which use the 'draw' fallbacks will typically use
1215
* this to make sure we generate/use shaders that don't go through the
1216
* draw module's wrapper functions.
1217
*/
1218
void util_blitter_cache_all_shaders(struct blitter_context *blitter)
1219
{
1220
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1221
struct pipe_context *pipe = blitter->pipe;
1222
struct pipe_screen *screen = pipe->screen;
1223
unsigned samples, j, f, target, max_samples, use_txf;
1224
bool has_arraytex, has_cubearraytex;
1225
1226
max_samples = ctx->has_texture_multisample ? 2 : 1;
1227
has_arraytex = screen->get_param(screen,
1228
PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
1229
has_cubearraytex = screen->get_param(screen,
1230
PIPE_CAP_CUBE_MAP_ARRAY) != 0;
1231
1232
/* It only matters if i <= 1 or > 1. */
1233
for (samples = 1; samples <= max_samples; samples++) {
1234
for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
1235
for (use_txf = 0; use_txf <= ctx->has_txf; use_txf++) {
1236
if (!has_arraytex &&
1237
(target == PIPE_TEXTURE_1D_ARRAY ||
1238
target == PIPE_TEXTURE_2D_ARRAY)) {
1239
continue;
1240
}
1241
if (!has_cubearraytex &&
1242
(target == PIPE_TEXTURE_CUBE_ARRAY))
1243
continue;
1244
1245
if (samples > 1 &&
1246
(target != PIPE_TEXTURE_2D &&
1247
target != PIPE_TEXTURE_2D_ARRAY))
1248
continue;
1249
1250
if (samples > 1 && use_txf)
1251
continue; /* TXF is the only option, use_txf has no effect */
1252
1253
/* If samples == 1, the shaders read one texel. If samples >= 1,
1254
* they read one sample.
1255
*/
1256
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
1257
PIPE_FORMAT_R32_FLOAT, target,
1258
samples, samples, 0, use_txf);
1259
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1260
PIPE_FORMAT_R32_UINT, target,
1261
samples, samples, 0, use_txf);
1262
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1263
PIPE_FORMAT_R32_SINT, target,
1264
samples, samples, 0, use_txf);
1265
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1266
PIPE_FORMAT_R32_SINT, target,
1267
samples, samples, 0, use_txf);
1268
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1269
PIPE_FORMAT_R32_UINT, target,
1270
samples, samples, 0, use_txf);
1271
blitter_get_fs_texfetch_depth(ctx, target, samples, use_txf);
1272
if (ctx->has_stencil_export) {
1273
blitter_get_fs_texfetch_depthstencil(ctx, target, samples, use_txf);
1274
blitter_get_fs_texfetch_stencil(ctx, target, samples, use_txf);
1275
}
1276
1277
if (samples == 1)
1278
continue;
1279
1280
/* MSAA resolve shaders. */
1281
for (j = 2; j < 32; j++) {
1282
if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
1283
target, j, j,
1284
PIPE_BIND_SAMPLER_VIEW)) {
1285
continue;
1286
}
1287
1288
for (f = 0; f < 2; f++) {
1289
if (f != PIPE_TEX_FILTER_NEAREST && use_txf)
1290
continue;
1291
1292
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
1293
PIPE_FORMAT_R32_FLOAT, target,
1294
j, 1, f, use_txf);
1295
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1296
PIPE_FORMAT_R32_UINT, target,
1297
j, 1, f, use_txf);
1298
blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1299
PIPE_FORMAT_R32_SINT, target,
1300
j, 1, f, use_txf);
1301
}
1302
}
1303
}
1304
}
1305
}
1306
1307
ctx->fs_empty = util_make_empty_fragment_shader(pipe);
1308
1309
ctx->fs_write_one_cbuf =
1310
util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1311
TGSI_INTERPOLATE_CONSTANT, false);
1312
1313
ctx->fs_write_all_cbufs =
1314
util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1315
TGSI_INTERPOLATE_CONSTANT, true);
1316
1317
ctx->cached_all_shaders = true;
1318
}
1319
1320
static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
1321
bool scissor, bool msaa)
1322
{
1323
struct pipe_context *pipe = ctx->base.pipe;
1324
1325
if (ctx->base.saved_num_window_rectangles)
1326
pipe->set_window_rectangles(pipe, false, 0, NULL);
1327
1328
pipe->bind_rasterizer_state(pipe, ctx->rs_state[scissor][msaa]);
1329
1330
if (ctx->has_geometry_shader)
1331
pipe->bind_gs_state(pipe, NULL);
1332
if (ctx->has_tessellation) {
1333
pipe->bind_tcs_state(pipe, NULL);
1334
pipe->bind_tes_state(pipe, NULL);
1335
}
1336
if (ctx->has_stream_out)
1337
pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
1338
}
1339
1340
static void blitter_draw(struct blitter_context_priv *ctx,
1341
void *vertex_elements_cso,
1342
blitter_get_vs_func get_vs,
1343
int x1, int y1, int x2, int y2, float depth,
1344
unsigned num_instances)
1345
{
1346
struct pipe_context *pipe = ctx->base.pipe;
1347
struct pipe_vertex_buffer vb = {0};
1348
1349
blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
1350
1351
vb.stride = 8 * sizeof(float);
1352
1353
u_upload_data(pipe->stream_uploader, 0, sizeof(ctx->vertices), 4, ctx->vertices,
1354
&vb.buffer_offset, &vb.buffer.resource);
1355
if (!vb.buffer.resource)
1356
return;
1357
u_upload_unmap(pipe->stream_uploader);
1358
1359
pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, 0, false, &vb);
1360
pipe->bind_vertex_elements_state(pipe, vertex_elements_cso);
1361
pipe->bind_vs_state(pipe, get_vs(&ctx->base));
1362
1363
if (ctx->base.use_index_buffer) {
1364
/* Note that for V3D,
1365
* dEQP-GLES3.functional.fbo.blit.rect.nearest_consistency_* require
1366
* that the last vert of the two tris be the same.
1367
*/
1368
static uint8_t indices[6] = { 0, 1, 2, 0, 3, 2 };
1369
util_draw_elements_instanced(pipe, indices, 1, 0,
1370
PIPE_PRIM_TRIANGLES, 0, 6,
1371
0, num_instances);
1372
} else {
1373
util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
1374
0, num_instances);
1375
}
1376
pipe_resource_reference(&vb.buffer.resource, NULL);
1377
}
1378
1379
void util_blitter_draw_rectangle(struct blitter_context *blitter,
1380
void *vertex_elements_cso,
1381
blitter_get_vs_func get_vs,
1382
int x1, int y1, int x2, int y2,
1383
float depth, unsigned num_instances,
1384
enum blitter_attrib_type type,
1385
const union blitter_attrib *attrib)
1386
{
1387
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1388
unsigned i;
1389
1390
switch (type) {
1391
case UTIL_BLITTER_ATTRIB_COLOR:
1392
blitter_set_clear_color(ctx, attrib->color);
1393
break;
1394
1395
case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
1396
for (i = 0; i < 4; i++) {
1397
ctx->vertices[i][1][2] = attrib->texcoord.z;
1398
ctx->vertices[i][1][3] = attrib->texcoord.w;
1399
}
1400
FALLTHROUGH;
1401
case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
1402
set_texcoords_in_vertices(attrib, &ctx->vertices[0][1][0], 8);
1403
break;
1404
1405
default:;
1406
}
1407
1408
blitter_draw(ctx, vertex_elements_cso, get_vs, x1, y1, x2, y2, depth,
1409
num_instances);
1410
}
1411
1412
static void *get_clear_blend_state(struct blitter_context_priv *ctx,
1413
unsigned clear_buffers)
1414
{
1415
struct pipe_context *pipe = ctx->base.pipe;
1416
int index;
1417
1418
clear_buffers &= PIPE_CLEAR_COLOR;
1419
1420
/* Return an existing blend state. */
1421
if (!clear_buffers)
1422
return ctx->blend[0][0];
1423
1424
index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
1425
1426
if (ctx->blend_clear[index])
1427
return ctx->blend_clear[index];
1428
1429
/* Create a new one. */
1430
{
1431
struct pipe_blend_state blend = {0};
1432
unsigned i;
1433
1434
blend.independent_blend_enable = 1;
1435
1436
for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
1437
if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
1438
blend.rt[i].colormask = PIPE_MASK_RGBA;
1439
blend.max_rt = i;
1440
}
1441
}
1442
1443
ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
1444
}
1445
return ctx->blend_clear[index];
1446
}
1447
1448
void util_blitter_common_clear_setup(struct blitter_context *blitter,
1449
unsigned width, unsigned height,
1450
unsigned clear_buffers,
1451
void *custom_blend, void *custom_dsa)
1452
{
1453
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1454
struct pipe_context *pipe = ctx->base.pipe;
1455
1456
util_blitter_set_running_flag(blitter);
1457
blitter_check_saved_vertex_states(ctx);
1458
blitter_check_saved_fragment_states(ctx);
1459
blitter_disable_render_cond(ctx);
1460
1461
/* bind states */
1462
if (custom_blend) {
1463
pipe->bind_blend_state(pipe, custom_blend);
1464
} else {
1465
pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
1466
}
1467
1468
if (custom_dsa) {
1469
pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
1470
} else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1471
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1472
} else if (clear_buffers & PIPE_CLEAR_DEPTH) {
1473
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1474
} else if (clear_buffers & PIPE_CLEAR_STENCIL) {
1475
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1476
} else {
1477
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1478
}
1479
1480
pipe->set_sample_mask(pipe, ~0);
1481
blitter_set_dst_dimensions(ctx, width, height);
1482
}
1483
1484
static void util_blitter_clear_custom(struct blitter_context *blitter,
1485
unsigned width, unsigned height,
1486
unsigned num_layers,
1487
unsigned clear_buffers,
1488
const union pipe_color_union *color,
1489
double depth, unsigned stencil,
1490
void *custom_blend, void *custom_dsa,
1491
bool msaa)
1492
{
1493
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1494
struct pipe_context *pipe = ctx->base.pipe;
1495
struct pipe_stencil_ref sr = { { 0 } };
1496
1497
assert(ctx->has_layered || num_layers <= 1);
1498
1499
util_blitter_common_clear_setup(blitter, width, height, clear_buffers,
1500
custom_blend, custom_dsa);
1501
1502
sr.ref_value[0] = stencil & 0xff;
1503
pipe->set_stencil_ref(pipe, sr);
1504
1505
union blitter_attrib attrib;
1506
memcpy(attrib.color, color->ui, sizeof(color->ui));
1507
1508
bool pass_generic = (clear_buffers & PIPE_CLEAR_COLOR) != 0;
1509
enum blitter_attrib_type type = pass_generic ? UTIL_BLITTER_ATTRIB_COLOR :
1510
UTIL_BLITTER_ATTRIB_NONE;
1511
1512
if (pass_generic)
1513
bind_fs_write_all_cbufs(ctx);
1514
else
1515
bind_fs_empty(ctx);
1516
1517
if (num_layers > 1 && ctx->has_layered) {
1518
blitter_get_vs_func get_vs = get_vs_layered;
1519
1520
blitter_set_common_draw_rect_state(ctx, false, msaa);
1521
blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
1522
0, 0, width, height,
1523
(float) depth, num_layers, type, &attrib);
1524
} else {
1525
blitter_get_vs_func get_vs;
1526
1527
if (pass_generic)
1528
get_vs = get_vs_passthrough_pos_generic;
1529
else
1530
get_vs = get_vs_passthrough_pos;
1531
1532
blitter_set_common_draw_rect_state(ctx, false, msaa);
1533
blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
1534
0, 0, width, height,
1535
(float) depth, 1, type, &attrib);
1536
}
1537
1538
util_blitter_restore_vertex_states(blitter);
1539
util_blitter_restore_fragment_states(blitter);
1540
util_blitter_restore_render_cond(blitter);
1541
util_blitter_unset_running_flag(blitter);
1542
}
1543
1544
void util_blitter_clear(struct blitter_context *blitter,
1545
unsigned width, unsigned height, unsigned num_layers,
1546
unsigned clear_buffers,
1547
const union pipe_color_union *color,
1548
double depth, unsigned stencil,
1549
bool msaa)
1550
{
1551
util_blitter_clear_custom(blitter, width, height, num_layers,
1552
clear_buffers, color, depth, stencil,
1553
NULL, NULL, msaa);
1554
}
1555
1556
void util_blitter_custom_clear_depth(struct blitter_context *blitter,
1557
unsigned width, unsigned height,
1558
double depth, void *custom_dsa)
1559
{
1560
static const union pipe_color_union color;
1561
util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
1562
NULL, custom_dsa, false);
1563
}
1564
1565
void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
1566
struct pipe_resource *dst,
1567
unsigned dstlevel,
1568
unsigned dstz)
1569
{
1570
memset(dst_templ, 0, sizeof(*dst_templ));
1571
dst_templ->format = util_format_linear(dst->format);
1572
dst_templ->u.tex.level = dstlevel;
1573
dst_templ->u.tex.first_layer = dstz;
1574
dst_templ->u.tex.last_layer = dstz;
1575
}
1576
1577
static struct pipe_surface *
1578
util_blitter_get_next_surface_layer(struct pipe_context *pipe,
1579
struct pipe_surface *surf)
1580
{
1581
struct pipe_surface dst_templ;
1582
1583
memset(&dst_templ, 0, sizeof(dst_templ));
1584
dst_templ.format = surf->format;
1585
dst_templ.u.tex.level = surf->u.tex.level;
1586
dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
1587
dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
1588
1589
return pipe->create_surface(pipe, surf->texture, &dst_templ);
1590
}
1591
1592
void util_blitter_default_src_texture(struct blitter_context *blitter,
1593
struct pipe_sampler_view *src_templ,
1594
struct pipe_resource *src,
1595
unsigned srclevel)
1596
{
1597
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1598
1599
memset(src_templ, 0, sizeof(*src_templ));
1600
1601
if (ctx->cube_as_2darray &&
1602
(src->target == PIPE_TEXTURE_CUBE ||
1603
src->target == PIPE_TEXTURE_CUBE_ARRAY))
1604
src_templ->target = PIPE_TEXTURE_2D_ARRAY;
1605
else
1606
src_templ->target = src->target;
1607
1608
src_templ->format = util_format_linear(src->format);
1609
src_templ->u.tex.first_level = srclevel;
1610
src_templ->u.tex.last_level = srclevel;
1611
src_templ->u.tex.first_layer = 0;
1612
src_templ->u.tex.last_layer =
1613
src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1614
: (unsigned)(src->array_size - 1);
1615
src_templ->swizzle_r = PIPE_SWIZZLE_X;
1616
src_templ->swizzle_g = PIPE_SWIZZLE_Y;
1617
src_templ->swizzle_b = PIPE_SWIZZLE_Z;
1618
src_templ->swizzle_a = PIPE_SWIZZLE_W;
1619
}
1620
1621
static bool is_blit_generic_supported(struct blitter_context *blitter,
1622
const struct pipe_resource *dst,
1623
enum pipe_format dst_format,
1624
const struct pipe_resource *src,
1625
enum pipe_format src_format,
1626
unsigned mask)
1627
{
1628
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1629
struct pipe_screen *screen = ctx->base.pipe->screen;
1630
1631
if (dst) {
1632
unsigned bind;
1633
const struct util_format_description *desc =
1634
util_format_description(dst_format);
1635
bool dst_has_stencil = util_format_has_stencil(desc);
1636
1637
/* Stencil export must be supported for stencil copy. */
1638
if ((mask & PIPE_MASK_S) && dst_has_stencil &&
1639
!ctx->has_stencil_export) {
1640
return false;
1641
}
1642
1643
if (dst_has_stencil || util_format_has_depth(desc))
1644
bind = PIPE_BIND_DEPTH_STENCIL;
1645
else
1646
bind = PIPE_BIND_RENDER_TARGET;
1647
1648
if (!screen->is_format_supported(screen, dst_format, dst->target,
1649
dst->nr_samples, dst->nr_storage_samples,
1650
bind)) {
1651
return false;
1652
}
1653
}
1654
1655
if (src) {
1656
if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
1657
return false;
1658
}
1659
1660
if (!screen->is_format_supported(screen, src_format, src->target,
1661
src->nr_samples, src->nr_storage_samples,
1662
PIPE_BIND_SAMPLER_VIEW)) {
1663
return false;
1664
}
1665
1666
/* Check stencil sampler support for stencil copy. */
1667
if (mask & PIPE_MASK_S) {
1668
if (util_format_has_stencil(util_format_description(src_format))) {
1669
enum pipe_format stencil_format =
1670
util_format_stencil_only(src_format);
1671
assert(stencil_format != PIPE_FORMAT_NONE);
1672
1673
if (stencil_format != src_format &&
1674
!screen->is_format_supported(screen, stencil_format,
1675
src->target, src->nr_samples,
1676
src->nr_storage_samples,
1677
PIPE_BIND_SAMPLER_VIEW)) {
1678
return false;
1679
}
1680
}
1681
}
1682
}
1683
1684
return true;
1685
}
1686
1687
bool util_blitter_is_copy_supported(struct blitter_context *blitter,
1688
const struct pipe_resource *dst,
1689
const struct pipe_resource *src)
1690
{
1691
return is_blit_generic_supported(blitter, dst, dst->format,
1692
src, src->format, PIPE_MASK_RGBAZS);
1693
}
1694
1695
bool util_blitter_is_blit_supported(struct blitter_context *blitter,
1696
const struct pipe_blit_info *info)
1697
{
1698
return is_blit_generic_supported(blitter,
1699
info->dst.resource, info->dst.format,
1700
info->src.resource, info->src.format,
1701
info->mask);
1702
}
1703
1704
void util_blitter_copy_texture(struct blitter_context *blitter,
1705
struct pipe_resource *dst,
1706
unsigned dst_level,
1707
unsigned dstx, unsigned dsty, unsigned dstz,
1708
struct pipe_resource *src,
1709
unsigned src_level,
1710
const struct pipe_box *srcbox)
1711
{
1712
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1713
struct pipe_context *pipe = ctx->base.pipe;
1714
struct pipe_surface *dst_view, dst_templ;
1715
struct pipe_sampler_view src_templ, *src_view;
1716
struct pipe_box dstbox;
1717
1718
assert(dst && src);
1719
assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1720
1721
u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
1722
abs(srcbox->depth), &dstbox);
1723
1724
/* Initialize the surface. */
1725
util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
1726
dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1727
1728
/* Initialize the sampler view. */
1729
util_blitter_default_src_texture(blitter, &src_templ, src, src_level);
1730
src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1731
1732
/* Copy. */
1733
util_blitter_blit_generic(blitter, dst_view, &dstbox,
1734
src_view, srcbox, src->width0, src->height0,
1735
PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
1736
false);
1737
1738
pipe_surface_reference(&dst_view, NULL);
1739
pipe_sampler_view_reference(&src_view, NULL);
1740
}
1741
1742
static void
1743
blitter_draw_tex(struct blitter_context_priv *ctx,
1744
int dst_x1, int dst_y1, int dst_x2, int dst_y2,
1745
struct pipe_sampler_view *src,
1746
unsigned src_width0, unsigned src_height0,
1747
int src_x1, int src_y1, int src_x2, int src_y2,
1748
float layer, unsigned sample,
1749
bool uses_txf, enum blitter_attrib_type type)
1750
{
1751
union blitter_attrib coord;
1752
blitter_get_vs_func get_vs = get_vs_passthrough_pos_generic;
1753
1754
get_texcoords(src, src_width0, src_height0,
1755
src_x1, src_y1, src_x2, src_y2, layer, sample,
1756
uses_txf, &coord);
1757
1758
if (src->target == PIPE_TEXTURE_CUBE ||
1759
src->target == PIPE_TEXTURE_CUBE_ARRAY) {
1760
float face_coord[4][2];
1761
1762
set_texcoords_in_vertices(&coord, &face_coord[0][0], 2);
1763
util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
1764
/* pointer, stride in floats */
1765
&face_coord[0][0], 2,
1766
&ctx->vertices[0][1][0], 8,
1767
false);
1768
for (unsigned i = 0; i < 4; i++)
1769
ctx->vertices[i][1][3] = coord.texcoord.w;
1770
1771
/* Cubemaps don't use draw_rectangle. */
1772
blitter_draw(ctx, ctx->velem_state, get_vs,
1773
dst_x1, dst_y1, dst_x2, dst_y2, 0, 1);
1774
} else {
1775
ctx->base.draw_rectangle(&ctx->base, ctx->velem_state, get_vs,
1776
dst_x1, dst_y1, dst_x2, dst_y2,
1777
0, 1, type, &coord);
1778
}
1779
}
1780
1781
static void do_blits(struct blitter_context_priv *ctx,
1782
struct pipe_surface *dst,
1783
const struct pipe_box *dstbox,
1784
struct pipe_sampler_view *src,
1785
unsigned src_width0,
1786
unsigned src_height0,
1787
const struct pipe_box *srcbox,
1788
bool is_zsbuf,
1789
bool uses_txf)
1790
{
1791
struct pipe_context *pipe = ctx->base.pipe;
1792
unsigned src_samples = src->texture->nr_samples;
1793
unsigned dst_samples = dst->texture->nr_samples;
1794
enum pipe_texture_target src_target = src->target;
1795
struct pipe_framebuffer_state fb_state = {0};
1796
1797
/* Initialize framebuffer state. */
1798
fb_state.width = dst->width;
1799
fb_state.height = dst->height;
1800
fb_state.nr_cbufs = is_zsbuf ? 0 : 1;
1801
1802
blitter_set_dst_dimensions(ctx, fb_state.width, fb_state.height);
1803
1804
if ((src_target == PIPE_TEXTURE_1D ||
1805
src_target == PIPE_TEXTURE_2D ||
1806
src_target == PIPE_TEXTURE_RECT) &&
1807
src_samples <= 1) {
1808
/* Set framebuffer state. */
1809
if (is_zsbuf) {
1810
fb_state.zsbuf = dst;
1811
} else {
1812
fb_state.cbufs[0] = dst;
1813
}
1814
pipe->set_framebuffer_state(pipe, &fb_state);
1815
1816
/* Draw. */
1817
pipe->set_sample_mask(pipe, ~0);
1818
blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1819
dstbox->x + dstbox->width,
1820
dstbox->y + dstbox->height,
1821
src, src_width0, src_height0, srcbox->x, srcbox->y,
1822
srcbox->x + srcbox->width, srcbox->y + srcbox->height,
1823
0, 0, uses_txf, UTIL_BLITTER_ATTRIB_TEXCOORD_XY);
1824
} else {
1825
/* Draw the quad with the generic codepath. */
1826
int dst_z;
1827
for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
1828
struct pipe_surface *old;
1829
bool flipped = (srcbox->depth < 0);
1830
float depth_center_offset = 0.0;
1831
int src_depth = abs(srcbox->depth);
1832
float src_z_step = src_depth / (float)dstbox->depth;
1833
1834
/* Scale Z properly if the blit is scaled.
1835
*
1836
* When downscaling, we want the coordinates centered, so that
1837
* mipmapping works for 3D textures. For example, when generating
1838
* a 4x4x4 level, this wouldn't average the pixels:
1839
*
1840
* src Z: 0 1 2 3 4 5 6 7
1841
* dst Z: 0 1 2 3
1842
*
1843
* Because the pixels are not centered below the pixels of the higher
1844
* level. Therefore, we want this:
1845
* src Z: 0 1 2 3 4 5 6 7
1846
* dst Z: 0 1 2 3
1847
*
1848
* This calculation is taken from the radv driver.
1849
*/
1850
if (src_target == PIPE_TEXTURE_3D)
1851
depth_center_offset = 0.5 / dstbox->depth * src_depth;
1852
1853
if (flipped) {
1854
src_z_step *= - 1;
1855
depth_center_offset *= -1;
1856
}
1857
1858
float src_z = dst_z * src_z_step + depth_center_offset;
1859
1860
/* Set framebuffer state. */
1861
if (is_zsbuf) {
1862
fb_state.zsbuf = dst;
1863
} else {
1864
fb_state.cbufs[0] = dst;
1865
}
1866
pipe->set_framebuffer_state(pipe, &fb_state);
1867
1868
/* See if we need to blit a multisample or singlesample buffer. */
1869
if (src_samples == dst_samples && dst_samples > 1) {
1870
/* MSAA copy. */
1871
unsigned i, max_sample = dst_samples - 1;
1872
1873
for (i = 0; i <= max_sample; i++) {
1874
pipe->set_sample_mask(pipe, 1 << i);
1875
blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1876
dstbox->x + dstbox->width,
1877
dstbox->y + dstbox->height,
1878
src, src_width0, src_height0,
1879
srcbox->x, srcbox->y,
1880
srcbox->x + srcbox->width,
1881
srcbox->y + srcbox->height,
1882
srcbox->z + src_z, i, uses_txf,
1883
UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
1884
}
1885
} else {
1886
/* Normal copy, MSAA upsampling, or MSAA resolve. */
1887
pipe->set_sample_mask(pipe, ~0);
1888
blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1889
dstbox->x + dstbox->width,
1890
dstbox->y + dstbox->height,
1891
src, src_width0, src_height0,
1892
srcbox->x, srcbox->y,
1893
srcbox->x + srcbox->width,
1894
srcbox->y + srcbox->height,
1895
srcbox->z + src_z, 0, uses_txf,
1896
UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
1897
}
1898
1899
/* Get the next surface or (if this is the last iteration)
1900
* just unreference the last one. */
1901
old = dst;
1902
if (dst_z < dstbox->depth-1) {
1903
dst = util_blitter_get_next_surface_layer(ctx->base.pipe, dst);
1904
}
1905
if (dst_z) {
1906
pipe_surface_reference(&old, NULL);
1907
}
1908
}
1909
}
1910
}
1911
1912
void util_blitter_blit_generic(struct blitter_context *blitter,
1913
struct pipe_surface *dst,
1914
const struct pipe_box *dstbox,
1915
struct pipe_sampler_view *src,
1916
const struct pipe_box *srcbox,
1917
unsigned src_width0, unsigned src_height0,
1918
unsigned mask, unsigned filter,
1919
const struct pipe_scissor_state *scissor,
1920
bool alpha_blend)
1921
{
1922
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1923
struct pipe_context *pipe = ctx->base.pipe;
1924
enum pipe_texture_target src_target = src->target;
1925
unsigned src_samples = src->texture->nr_samples;
1926
unsigned dst_samples = dst->texture->nr_samples;
1927
void *sampler_state;
1928
const struct util_format_description *src_desc =
1929
util_format_description(src->format);
1930
const struct util_format_description *dst_desc =
1931
util_format_description(dst->format);
1932
1933
bool src_has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1934
bool src_has_depth = util_format_has_depth(src_desc);
1935
bool src_has_stencil = util_format_has_stencil(src_desc);
1936
1937
bool dst_has_color = mask & PIPE_MASK_RGBA &&
1938
dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1939
bool dst_has_depth = mask & PIPE_MASK_Z &&
1940
util_format_has_depth(dst_desc);
1941
bool dst_has_stencil = ctx->has_stencil_export &&
1942
mask & PIPE_MASK_S &&
1943
util_format_has_stencil(dst_desc);
1944
1945
/* Return if there is nothing to do. */
1946
if (!dst_has_color && !dst_has_depth && !dst_has_stencil) {
1947
return;
1948
}
1949
1950
bool is_scaled = dstbox->width != abs(srcbox->width) ||
1951
dstbox->height != abs(srcbox->height);
1952
1953
if (src_has_stencil || !is_scaled)
1954
filter = PIPE_TEX_FILTER_NEAREST;
1955
1956
bool use_txf = false;
1957
1958
/* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1959
if (ctx->has_txf &&
1960
!is_scaled &&
1961
filter == PIPE_TEX_FILTER_NEAREST &&
1962
src->target != PIPE_TEXTURE_CUBE &&
1963
src->target != PIPE_TEXTURE_CUBE_ARRAY) {
1964
int src_width = u_minify(src_width0, src->u.tex.first_level);
1965
int src_height = u_minify(src_height0, src->u.tex.first_level);
1966
int src_depth = src->u.tex.last_layer + 1;
1967
struct pipe_box box = *srcbox;
1968
1969
/* Eliminate negative width/height/depth. */
1970
if (box.width < 0) {
1971
box.x += box.width;
1972
box.width *= -1;
1973
}
1974
if (box.height < 0) {
1975
box.y += box.height;
1976
box.height *= -1;
1977
}
1978
if (box.depth < 0) {
1979
box.z += box.depth;
1980
box.depth *= -1;
1981
}
1982
1983
/* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1984
use_txf =
1985
box.x >= 0 && box.x < src_width &&
1986
box.y >= 0 && box.y < src_height &&
1987
box.z >= 0 && box.z < src_depth &&
1988
box.x + box.width > 0 && box.x + box.width <= src_width &&
1989
box.y + box.height > 0 && box.y + box.height <= src_height &&
1990
box.z + box.depth > 0 && box.z + box.depth <= src_depth;
1991
}
1992
1993
/* Check whether the states are properly saved. */
1994
util_blitter_set_running_flag(blitter);
1995
blitter_check_saved_vertex_states(ctx);
1996
blitter_check_saved_fragment_states(ctx);
1997
blitter_check_saved_textures(ctx);
1998
blitter_check_saved_fb_state(ctx);
1999
blitter_disable_render_cond(ctx);
2000
2001
/* Blend, DSA, fragment shader. */
2002
if (dst_has_depth && dst_has_stencil) {
2003
pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2004
pipe->bind_depth_stencil_alpha_state(pipe,
2005
ctx->dsa_write_depth_stencil);
2006
if (src_has_color) {
2007
assert(use_txf);
2008
ctx->bind_fs_state(pipe,
2009
blitter_get_fs_pack_color_zs(ctx, src_target,
2010
src_samples, dst->format, false));
2011
} else {
2012
ctx->bind_fs_state(pipe,
2013
blitter_get_fs_texfetch_depthstencil(ctx, src_target,
2014
src_samples, use_txf));
2015
}
2016
} else if (dst_has_depth) {
2017
pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2018
pipe->bind_depth_stencil_alpha_state(pipe,
2019
ctx->dsa_write_depth_keep_stencil);
2020
if (src_has_color &&
2021
(src->format == PIPE_FORMAT_R32_UINT ||
2022
src->format == PIPE_FORMAT_R32G32_UINT)) {
2023
assert(use_txf);
2024
ctx->bind_fs_state(pipe,
2025
blitter_get_fs_pack_color_zs(ctx, src_target,
2026
src_samples, dst->format, false));
2027
} else {
2028
ctx->bind_fs_state(pipe,
2029
blitter_get_fs_texfetch_depth(ctx, src_target,
2030
src_samples, use_txf));
2031
}
2032
} else if (dst_has_stencil) {
2033
pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2034
pipe->bind_depth_stencil_alpha_state(pipe,
2035
ctx->dsa_keep_depth_write_stencil);
2036
2037
assert(src_has_stencil); /* unpacking from color is unsupported */
2038
ctx->bind_fs_state(pipe,
2039
blitter_get_fs_texfetch_stencil(ctx, src_target,
2040
src_samples, use_txf));
2041
} else {
2042
unsigned colormask = mask & PIPE_MASK_RGBA;
2043
2044
pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
2045
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2046
2047
if (src_has_depth &&
2048
(dst->format == PIPE_FORMAT_R32_UINT ||
2049
dst->format == PIPE_FORMAT_R32G32_UINT)) {
2050
assert(use_txf);
2051
ctx->bind_fs_state(pipe,
2052
blitter_get_fs_pack_color_zs(ctx, src_target,
2053
src_samples, src->format, true));
2054
} else {
2055
ctx->bind_fs_state(pipe,
2056
blitter_get_fs_texfetch_col(ctx, src->format, dst->format, src_target,
2057
src_samples, dst_samples, filter,
2058
use_txf));
2059
}
2060
}
2061
2062
/* Set the linear filter only for scaled color non-MSAA blits. */
2063
if (filter == PIPE_TEX_FILTER_LINEAR) {
2064
if (src_target == PIPE_TEXTURE_RECT) {
2065
sampler_state = ctx->sampler_state_rect_linear;
2066
} else {
2067
sampler_state = ctx->sampler_state_linear;
2068
}
2069
} else {
2070
if (src_target == PIPE_TEXTURE_RECT) {
2071
sampler_state = ctx->sampler_state_rect;
2072
} else {
2073
sampler_state = ctx->sampler_state;
2074
}
2075
}
2076
2077
/* Set samplers. */
2078
if (src_has_depth && src_has_stencil &&
2079
(dst_has_color || (dst_has_depth && dst_has_stencil))) {
2080
/* Setup two samplers, one for depth and the other one for stencil. */
2081
struct pipe_sampler_view templ;
2082
struct pipe_sampler_view *views[2];
2083
void *samplers[2] = {sampler_state, sampler_state};
2084
2085
templ = *src;
2086
templ.format = util_format_stencil_only(templ.format);
2087
assert(templ.format != PIPE_FORMAT_NONE);
2088
2089
views[0] = src;
2090
views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
2091
2092
pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, 0, views);
2093
pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
2094
2095
pipe_sampler_view_reference(&views[1], NULL);
2096
} else if (src_has_stencil && dst_has_stencil) {
2097
/* Set a stencil-only sampler view for it not to sample depth instead. */
2098
struct pipe_sampler_view templ;
2099
struct pipe_sampler_view *view;
2100
2101
templ = *src;
2102
templ.format = util_format_stencil_only(templ.format);
2103
assert(templ.format != PIPE_FORMAT_NONE);
2104
2105
view = pipe->create_sampler_view(pipe, src->texture, &templ);
2106
2107
pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, &view);
2108
pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2109
0, 1, &sampler_state);
2110
2111
pipe_sampler_view_reference(&view, NULL);
2112
} else {
2113
pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, &src);
2114
pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2115
0, 1, &sampler_state);
2116
}
2117
2118
if (scissor) {
2119
pipe->set_scissor_states(pipe, 0, 1, scissor);
2120
}
2121
2122
blitter_set_common_draw_rect_state(ctx, scissor != NULL, dst_samples > 1);
2123
2124
do_blits(ctx, dst, dstbox, src, src_width0, src_height0,
2125
srcbox, dst_has_depth || dst_has_stencil, use_txf);
2126
2127
util_blitter_restore_vertex_states(blitter);
2128
util_blitter_restore_fragment_states(blitter);
2129
util_blitter_restore_textures(blitter);
2130
util_blitter_restore_fb_state(blitter);
2131
if (scissor) {
2132
pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
2133
}
2134
util_blitter_restore_render_cond(blitter);
2135
util_blitter_unset_running_flag(blitter);
2136
}
2137
2138
void
2139
util_blitter_blit(struct blitter_context *blitter,
2140
const struct pipe_blit_info *info)
2141
{
2142
struct pipe_resource *dst = info->dst.resource;
2143
struct pipe_resource *src = info->src.resource;
2144
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2145
struct pipe_context *pipe = ctx->base.pipe;
2146
struct pipe_surface *dst_view, dst_templ;
2147
struct pipe_sampler_view src_templ, *src_view;
2148
2149
/* Initialize the surface. */
2150
util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
2151
info->dst.box.z);
2152
dst_templ.format = info->dst.format;
2153
dst_view = pipe->create_surface(pipe, dst, &dst_templ);
2154
2155
/* Initialize the sampler view. */
2156
util_blitter_default_src_texture(blitter, &src_templ, src, info->src.level);
2157
src_templ.format = info->src.format;
2158
src_view = pipe->create_sampler_view(pipe, src, &src_templ);
2159
2160
/* Copy. */
2161
util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
2162
src_view, &info->src.box, src->width0, src->height0,
2163
info->mask, info->filter,
2164
info->scissor_enable ? &info->scissor : NULL,
2165
info->alpha_blend);
2166
2167
pipe_surface_reference(&dst_view, NULL);
2168
pipe_sampler_view_reference(&src_view, NULL);
2169
}
2170
2171
void util_blitter_generate_mipmap(struct blitter_context *blitter,
2172
struct pipe_resource *tex,
2173
enum pipe_format format,
2174
unsigned base_level, unsigned last_level,
2175
unsigned first_layer, unsigned last_layer)
2176
{
2177
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2178
struct pipe_context *pipe = ctx->base.pipe;
2179
struct pipe_surface dst_templ, *dst_view;
2180
struct pipe_sampler_view src_templ, *src_view;
2181
bool is_depth;
2182
void *sampler_state;
2183
const struct util_format_description *desc =
2184
util_format_description(format);
2185
unsigned src_level;
2186
unsigned target = tex->target;
2187
2188
if (ctx->cube_as_2darray &&
2189
(target == PIPE_TEXTURE_CUBE || target == PIPE_TEXTURE_CUBE_ARRAY))
2190
target = PIPE_TEXTURE_2D_ARRAY;
2191
2192
assert(tex->nr_samples <= 1);
2193
/* Disallow stencil formats without depth. */
2194
assert(!util_format_has_stencil(desc) || util_format_has_depth(desc));
2195
2196
is_depth = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS;
2197
2198
/* Check whether the states are properly saved. */
2199
util_blitter_set_running_flag(blitter);
2200
blitter_check_saved_vertex_states(ctx);
2201
blitter_check_saved_fragment_states(ctx);
2202
blitter_check_saved_textures(ctx);
2203
blitter_check_saved_fb_state(ctx);
2204
blitter_disable_render_cond(ctx);
2205
2206
/* Set states. */
2207
if (is_depth) {
2208
pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2209
pipe->bind_depth_stencil_alpha_state(pipe,
2210
ctx->dsa_write_depth_keep_stencil);
2211
ctx->bind_fs_state(pipe,
2212
blitter_get_fs_texfetch_depth(ctx, target, 1, false));
2213
} else {
2214
pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2215
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2216
ctx->bind_fs_state(pipe,
2217
blitter_get_fs_texfetch_col(ctx, tex->format, tex->format, target,
2218
1, 1, PIPE_TEX_FILTER_LINEAR, false));
2219
}
2220
2221
if (target == PIPE_TEXTURE_RECT) {
2222
sampler_state = ctx->sampler_state_rect_linear;
2223
} else {
2224
sampler_state = ctx->sampler_state_linear;
2225
}
2226
pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2227
0, 1, &sampler_state);
2228
2229
blitter_set_common_draw_rect_state(ctx, false, false);
2230
2231
for (src_level = base_level; src_level < last_level; src_level++) {
2232
struct pipe_box dstbox = {0}, srcbox = {0};
2233
unsigned dst_level = src_level + 1;
2234
2235
dstbox.width = u_minify(tex->width0, dst_level);
2236
dstbox.height = u_minify(tex->height0, dst_level);
2237
2238
srcbox.width = u_minify(tex->width0, src_level);
2239
srcbox.height = u_minify(tex->height0, src_level);
2240
2241
if (target == PIPE_TEXTURE_3D) {
2242
dstbox.depth = util_num_layers(tex, dst_level);
2243
srcbox.depth = util_num_layers(tex, src_level);
2244
} else {
2245
dstbox.z = srcbox.z = first_layer;
2246
dstbox.depth = srcbox.depth = last_layer - first_layer + 1;
2247
}
2248
2249
/* Initialize the surface. */
2250
util_blitter_default_dst_texture(&dst_templ, tex, dst_level,
2251
first_layer);
2252
dst_templ.format = format;
2253
dst_view = pipe->create_surface(pipe, tex, &dst_templ);
2254
2255
/* Initialize the sampler view. */
2256
util_blitter_default_src_texture(blitter, &src_templ, tex, src_level);
2257
src_templ.format = format;
2258
src_view = pipe->create_sampler_view(pipe, tex, &src_templ);
2259
2260
pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, &src_view);
2261
2262
do_blits(ctx, dst_view, &dstbox, src_view, tex->width0, tex->height0,
2263
&srcbox, is_depth, false);
2264
2265
pipe_surface_reference(&dst_view, NULL);
2266
pipe_sampler_view_reference(&src_view, NULL);
2267
}
2268
2269
util_blitter_restore_vertex_states(blitter);
2270
util_blitter_restore_fragment_states(blitter);
2271
util_blitter_restore_textures(blitter);
2272
util_blitter_restore_fb_state(blitter);
2273
util_blitter_restore_render_cond(blitter);
2274
util_blitter_unset_running_flag(blitter);
2275
}
2276
2277
/* Clear a region of a color surface to a constant value. */
2278
void util_blitter_clear_render_target(struct blitter_context *blitter,
2279
struct pipe_surface *dstsurf,
2280
const union pipe_color_union *color,
2281
unsigned dstx, unsigned dsty,
2282
unsigned width, unsigned height)
2283
{
2284
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2285
struct pipe_context *pipe = ctx->base.pipe;
2286
struct pipe_framebuffer_state fb_state;
2287
bool msaa;
2288
unsigned num_layers;
2289
2290
assert(dstsurf->texture);
2291
if (!dstsurf->texture)
2292
return;
2293
2294
/* check the saved state */
2295
util_blitter_set_running_flag(blitter);
2296
blitter_check_saved_vertex_states(ctx);
2297
blitter_check_saved_fragment_states(ctx);
2298
blitter_check_saved_fb_state(ctx);
2299
blitter_disable_render_cond(ctx);
2300
2301
/* bind states */
2302
pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2303
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2304
bind_fs_write_one_cbuf(ctx);
2305
2306
/* set a framebuffer state */
2307
fb_state.width = dstsurf->width;
2308
fb_state.height = dstsurf->height;
2309
fb_state.nr_cbufs = 1;
2310
fb_state.cbufs[0] = dstsurf;
2311
fb_state.zsbuf = 0;
2312
pipe->set_framebuffer_state(pipe, &fb_state);
2313
pipe->set_sample_mask(pipe, ~0);
2314
msaa = util_framebuffer_get_num_samples(&fb_state) > 1;
2315
2316
blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2317
2318
union blitter_attrib attrib;
2319
memcpy(attrib.color, color->ui, sizeof(color->ui));
2320
2321
num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2322
if (num_layers > 1 && ctx->has_layered) {
2323
blitter_set_common_draw_rect_state(ctx, false, msaa);
2324
blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
2325
dstx, dsty, dstx+width, dsty+height, 0,
2326
num_layers, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
2327
} else {
2328
blitter_set_common_draw_rect_state(ctx, false, msaa);
2329
blitter->draw_rectangle(blitter, ctx->velem_state,
2330
get_vs_passthrough_pos_generic,
2331
dstx, dsty, dstx+width, dsty+height, 0,
2332
1, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
2333
}
2334
2335
util_blitter_restore_vertex_states(blitter);
2336
util_blitter_restore_fragment_states(blitter);
2337
util_blitter_restore_fb_state(blitter);
2338
util_blitter_restore_render_cond(blitter);
2339
util_blitter_unset_running_flag(blitter);
2340
}
2341
2342
/* Clear a region of a depth stencil surface. */
2343
void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
2344
struct pipe_surface *dstsurf,
2345
unsigned clear_flags,
2346
double depth,
2347
unsigned stencil,
2348
unsigned dstx, unsigned dsty,
2349
unsigned width, unsigned height)
2350
{
2351
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2352
struct pipe_context *pipe = ctx->base.pipe;
2353
struct pipe_framebuffer_state fb_state;
2354
struct pipe_stencil_ref sr = { { 0 } };
2355
unsigned num_layers;
2356
2357
assert(dstsurf->texture);
2358
if (!dstsurf->texture)
2359
return;
2360
2361
/* check the saved state */
2362
util_blitter_set_running_flag(blitter);
2363
blitter_check_saved_vertex_states(ctx);
2364
blitter_check_saved_fragment_states(ctx);
2365
blitter_check_saved_fb_state(ctx);
2366
blitter_disable_render_cond(ctx);
2367
2368
/* bind states */
2369
pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2370
if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
2371
sr.ref_value[0] = stencil & 0xff;
2372
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
2373
pipe->set_stencil_ref(pipe, sr);
2374
}
2375
else if (clear_flags & PIPE_CLEAR_DEPTH) {
2376
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
2377
}
2378
else if (clear_flags & PIPE_CLEAR_STENCIL) {
2379
sr.ref_value[0] = stencil & 0xff;
2380
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
2381
pipe->set_stencil_ref(pipe, sr);
2382
}
2383
else
2384
/* hmm that should be illegal probably, or make it a no-op somewhere */
2385
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2386
2387
bind_fs_empty(ctx);
2388
2389
/* set a framebuffer state */
2390
fb_state.width = dstsurf->width;
2391
fb_state.height = dstsurf->height;
2392
fb_state.nr_cbufs = 0;
2393
fb_state.cbufs[0] = 0;
2394
fb_state.zsbuf = dstsurf;
2395
pipe->set_framebuffer_state(pipe, &fb_state);
2396
pipe->set_sample_mask(pipe, ~0);
2397
2398
blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2399
2400
num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2401
if (num_layers > 1 && ctx->has_layered) {
2402
blitter_set_common_draw_rect_state(ctx, false, false);
2403
blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
2404
dstx, dsty, dstx+width, dsty+height, depth,
2405
num_layers, UTIL_BLITTER_ATTRIB_NONE, NULL);
2406
} else {
2407
blitter_set_common_draw_rect_state(ctx, false, false);
2408
blitter->draw_rectangle(blitter, ctx->velem_state,
2409
get_vs_passthrough_pos,
2410
dstx, dsty, dstx+width, dsty+height, depth, 1,
2411
UTIL_BLITTER_ATTRIB_NONE, NULL);
2412
}
2413
2414
util_blitter_restore_vertex_states(blitter);
2415
util_blitter_restore_fragment_states(blitter);
2416
util_blitter_restore_fb_state(blitter);
2417
util_blitter_restore_render_cond(blitter);
2418
util_blitter_unset_running_flag(blitter);
2419
}
2420
2421
/* draw a rectangle across a region using a custom dsa stage - for r600g */
2422
void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
2423
struct pipe_surface *zsurf,
2424
struct pipe_surface *cbsurf,
2425
unsigned sample_mask,
2426
void *dsa_stage, float depth)
2427
{
2428
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2429
struct pipe_context *pipe = ctx->base.pipe;
2430
struct pipe_framebuffer_state fb_state;
2431
2432
assert(zsurf->texture);
2433
if (!zsurf->texture)
2434
return;
2435
2436
/* check the saved state */
2437
util_blitter_set_running_flag(blitter);
2438
blitter_check_saved_vertex_states(ctx);
2439
blitter_check_saved_fragment_states(ctx);
2440
blitter_check_saved_fb_state(ctx);
2441
blitter_disable_render_cond(ctx);
2442
2443
/* bind states */
2444
pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
2445
ctx->blend[0][0]);
2446
pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
2447
if (cbsurf)
2448
bind_fs_write_one_cbuf(ctx);
2449
else
2450
bind_fs_empty(ctx);
2451
2452
/* set a framebuffer state */
2453
fb_state.width = zsurf->width;
2454
fb_state.height = zsurf->height;
2455
fb_state.nr_cbufs = 1;
2456
if (cbsurf) {
2457
fb_state.cbufs[0] = cbsurf;
2458
fb_state.nr_cbufs = 1;
2459
} else {
2460
fb_state.cbufs[0] = NULL;
2461
fb_state.nr_cbufs = 0;
2462
}
2463
fb_state.zsbuf = zsurf;
2464
pipe->set_framebuffer_state(pipe, &fb_state);
2465
pipe->set_sample_mask(pipe, sample_mask);
2466
2467
blitter_set_common_draw_rect_state(ctx, false,
2468
util_framebuffer_get_num_samples(&fb_state) > 1);
2469
blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
2470
blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2471
0, 0, zsurf->width, zsurf->height, depth,
2472
1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2473
2474
util_blitter_restore_vertex_states(blitter);
2475
util_blitter_restore_fragment_states(blitter);
2476
util_blitter_restore_fb_state(blitter);
2477
util_blitter_restore_render_cond(blitter);
2478
util_blitter_unset_running_flag(blitter);
2479
}
2480
2481
void util_blitter_copy_buffer(struct blitter_context *blitter,
2482
struct pipe_resource *dst,
2483
unsigned dstx,
2484
struct pipe_resource *src,
2485
unsigned srcx,
2486
unsigned size)
2487
{
2488
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2489
struct pipe_context *pipe = ctx->base.pipe;
2490
struct pipe_vertex_buffer vb;
2491
struct pipe_stream_output_target *so_target;
2492
unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2493
2494
if (srcx >= src->width0 ||
2495
dstx >= dst->width0) {
2496
return;
2497
}
2498
if (srcx + size > src->width0) {
2499
size = src->width0 - srcx;
2500
}
2501
if (dstx + size > dst->width0) {
2502
size = dst->width0 - dstx;
2503
}
2504
2505
/* Drivers not capable of Stream Out should not call this function
2506
* in the first place. */
2507
assert(ctx->has_stream_out);
2508
2509
/* Some alignment is required. */
2510
if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
2511
!ctx->has_stream_out) {
2512
struct pipe_box box;
2513
u_box_1d(srcx, size, &box);
2514
util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
2515
return;
2516
}
2517
2518
util_blitter_set_running_flag(blitter);
2519
blitter_check_saved_vertex_states(ctx);
2520
blitter_disable_render_cond(ctx);
2521
2522
vb.is_user_buffer = false;
2523
vb.buffer.resource = src;
2524
vb.buffer_offset = srcx;
2525
vb.stride = 4;
2526
2527
pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, 0, false, &vb);
2528
pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
2529
bind_vs_pos_only(ctx, 1);
2530
if (ctx->has_geometry_shader)
2531
pipe->bind_gs_state(pipe, NULL);
2532
if (ctx->has_tessellation) {
2533
pipe->bind_tcs_state(pipe, NULL);
2534
pipe->bind_tes_state(pipe, NULL);
2535
}
2536
pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2537
2538
so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
2539
pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2540
2541
util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2542
2543
util_blitter_restore_vertex_states(blitter);
2544
util_blitter_restore_render_cond(blitter);
2545
util_blitter_unset_running_flag(blitter);
2546
pipe_so_target_reference(&so_target, NULL);
2547
}
2548
2549
void util_blitter_clear_buffer(struct blitter_context *blitter,
2550
struct pipe_resource *dst,
2551
unsigned offset, unsigned size,
2552
unsigned num_channels,
2553
const union pipe_color_union *clear_value)
2554
{
2555
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2556
struct pipe_context *pipe = ctx->base.pipe;
2557
struct pipe_vertex_buffer vb = {0};
2558
struct pipe_stream_output_target *so_target = NULL;
2559
unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2560
2561
assert(num_channels >= 1);
2562
assert(num_channels <= 4);
2563
2564
/* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2565
*
2566
* R600 uses this to initialize texture resources, so width0 might not be
2567
* what you think it is.
2568
*/
2569
2570
/* Streamout is required. */
2571
if (!ctx->has_stream_out) {
2572
assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2573
return;
2574
}
2575
2576
/* Some alignment is required. */
2577
if (offset % 4 != 0 || size % 4 != 0) {
2578
assert(!"Bad alignment in util_blitter_clear_buffer()");
2579
return;
2580
}
2581
2582
u_upload_data(pipe->stream_uploader, 0, num_channels*4, 4, clear_value,
2583
&vb.buffer_offset, &vb.buffer.resource);
2584
if (!vb.buffer.resource)
2585
goto out;
2586
2587
vb.stride = 0;
2588
2589
util_blitter_set_running_flag(blitter);
2590
blitter_check_saved_vertex_states(ctx);
2591
blitter_disable_render_cond(ctx);
2592
2593
pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, 0, false, &vb);
2594
pipe->bind_vertex_elements_state(pipe,
2595
ctx->velem_state_readbuf[num_channels-1]);
2596
bind_vs_pos_only(ctx, num_channels);
2597
if (ctx->has_geometry_shader)
2598
pipe->bind_gs_state(pipe, NULL);
2599
if (ctx->has_tessellation) {
2600
pipe->bind_tcs_state(pipe, NULL);
2601
pipe->bind_tes_state(pipe, NULL);
2602
}
2603
pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2604
2605
so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
2606
pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2607
2608
util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2609
2610
out:
2611
util_blitter_restore_vertex_states(blitter);
2612
util_blitter_restore_render_cond(blitter);
2613
util_blitter_unset_running_flag(blitter);
2614
pipe_so_target_reference(&so_target, NULL);
2615
pipe_resource_reference(&vb.buffer.resource, NULL);
2616
}
2617
2618
/* probably radeon specific */
2619
void util_blitter_custom_resolve_color(struct blitter_context *blitter,
2620
struct pipe_resource *dst,
2621
unsigned dst_level,
2622
unsigned dst_layer,
2623
struct pipe_resource *src,
2624
unsigned src_layer,
2625
unsigned sample_mask,
2626
void *custom_blend,
2627
enum pipe_format format)
2628
{
2629
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2630
struct pipe_context *pipe = ctx->base.pipe;
2631
struct pipe_framebuffer_state fb_state;
2632
struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
2633
2634
util_blitter_set_running_flag(blitter);
2635
blitter_check_saved_vertex_states(ctx);
2636
blitter_check_saved_fragment_states(ctx);
2637
blitter_disable_render_cond(ctx);
2638
2639
/* bind states */
2640
pipe->bind_blend_state(pipe, custom_blend);
2641
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2642
bind_fs_write_one_cbuf(ctx);
2643
pipe->set_sample_mask(pipe, sample_mask);
2644
2645
memset(&surf_tmpl, 0, sizeof(surf_tmpl));
2646
surf_tmpl.format = format;
2647
surf_tmpl.u.tex.level = dst_level;
2648
surf_tmpl.u.tex.first_layer = dst_layer;
2649
surf_tmpl.u.tex.last_layer = dst_layer;
2650
2651
dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
2652
2653
surf_tmpl.u.tex.level = 0;
2654
surf_tmpl.u.tex.first_layer = src_layer;
2655
surf_tmpl.u.tex.last_layer = src_layer;
2656
2657
srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
2658
2659
/* set a framebuffer state */
2660
fb_state.width = src->width0;
2661
fb_state.height = src->height0;
2662
fb_state.nr_cbufs = 2;
2663
fb_state.cbufs[0] = srcsurf;
2664
fb_state.cbufs[1] = dstsurf;
2665
fb_state.zsbuf = NULL;
2666
pipe->set_framebuffer_state(pipe, &fb_state);
2667
2668
blitter_set_common_draw_rect_state(ctx, false,
2669
util_framebuffer_get_num_samples(&fb_state) > 1);
2670
blitter_set_dst_dimensions(ctx, src->width0, src->height0);
2671
blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2672
0, 0, src->width0, src->height0,
2673
0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2674
util_blitter_restore_fb_state(blitter);
2675
util_blitter_restore_vertex_states(blitter);
2676
util_blitter_restore_fragment_states(blitter);
2677
util_blitter_restore_render_cond(blitter);
2678
util_blitter_unset_running_flag(blitter);
2679
2680
pipe_surface_reference(&srcsurf, NULL);
2681
pipe_surface_reference(&dstsurf, NULL);
2682
}
2683
2684
void util_blitter_custom_color(struct blitter_context *blitter,
2685
struct pipe_surface *dstsurf,
2686
void *custom_blend)
2687
{
2688
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2689
struct pipe_context *pipe = ctx->base.pipe;
2690
struct pipe_framebuffer_state fb_state;
2691
2692
assert(dstsurf->texture);
2693
if (!dstsurf->texture)
2694
return;
2695
2696
/* check the saved state */
2697
util_blitter_set_running_flag(blitter);
2698
blitter_check_saved_vertex_states(ctx);
2699
blitter_check_saved_fragment_states(ctx);
2700
blitter_check_saved_fb_state(ctx);
2701
blitter_disable_render_cond(ctx);
2702
2703
/* bind states */
2704
pipe->bind_blend_state(pipe, custom_blend ? custom_blend
2705
: ctx->blend[PIPE_MASK_RGBA][0]);
2706
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2707
bind_fs_write_one_cbuf(ctx);
2708
2709
/* set a framebuffer state */
2710
fb_state.width = dstsurf->width;
2711
fb_state.height = dstsurf->height;
2712
fb_state.nr_cbufs = 1;
2713
fb_state.cbufs[0] = dstsurf;
2714
fb_state.zsbuf = 0;
2715
pipe->set_framebuffer_state(pipe, &fb_state);
2716
pipe->set_sample_mask(pipe, ~0);
2717
2718
blitter_set_common_draw_rect_state(ctx, false,
2719
util_framebuffer_get_num_samples(&fb_state) > 1);
2720
blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2721
blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2722
0, 0, dstsurf->width, dstsurf->height,
2723
0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2724
2725
util_blitter_restore_vertex_states(blitter);
2726
util_blitter_restore_fragment_states(blitter);
2727
util_blitter_restore_fb_state(blitter);
2728
util_blitter_restore_render_cond(blitter);
2729
util_blitter_unset_running_flag(blitter);
2730
}
2731
2732
static void *get_custom_vs(struct blitter_context *blitter)
2733
{
2734
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2735
2736
return ctx->custom_vs;
2737
}
2738
2739
/**
2740
* Performs a custom blit to the destination surface, using the VS and FS
2741
* provided.
2742
*
2743
* Used by vc4 for the 8-bit linear-to-tiled blit.
2744
*/
2745
void util_blitter_custom_shader(struct blitter_context *blitter,
2746
struct pipe_surface *dstsurf,
2747
void *custom_vs, void *custom_fs)
2748
{
2749
struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2750
struct pipe_context *pipe = ctx->base.pipe;
2751
struct pipe_framebuffer_state fb_state = { 0 };
2752
2753
ctx->custom_vs = custom_vs;
2754
2755
assert(dstsurf->texture);
2756
if (!dstsurf->texture)
2757
return;
2758
2759
/* check the saved state */
2760
util_blitter_set_running_flag(blitter);
2761
blitter_check_saved_vertex_states(ctx);
2762
blitter_check_saved_fragment_states(ctx);
2763
blitter_check_saved_fb_state(ctx);
2764
blitter_disable_render_cond(ctx);
2765
2766
/* bind states */
2767
pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2768
pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2769
pipe->bind_fs_state(pipe, custom_fs);
2770
2771
/* set a framebuffer state */
2772
fb_state.width = dstsurf->width;
2773
fb_state.height = dstsurf->height;
2774
fb_state.nr_cbufs = 1;
2775
fb_state.cbufs[0] = dstsurf;
2776
pipe->set_framebuffer_state(pipe, &fb_state);
2777
pipe->set_sample_mask(pipe, ~0);
2778
2779
blitter_set_common_draw_rect_state(ctx, false,
2780
util_framebuffer_get_num_samples(&fb_state) > 1);
2781
blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2782
blitter->draw_rectangle(blitter, ctx->velem_state, get_custom_vs,
2783
0, 0, dstsurf->width, dstsurf->height,
2784
0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2785
2786
util_blitter_restore_vertex_states(blitter);
2787
util_blitter_restore_fragment_states(blitter);
2788
util_blitter_restore_fb_state(blitter);
2789
util_blitter_restore_render_cond(blitter);
2790
util_blitter_unset_running_flag(blitter);
2791
}
2792
2793
static void *
2794
get_stencil_blit_fallback_fs(struct blitter_context_priv *ctx, bool msaa_src)
2795
{
2796
if (!ctx->fs_stencil_blit_fallback[msaa_src]) {
2797
ctx->fs_stencil_blit_fallback[msaa_src] =
2798
util_make_fs_stencil_blit(ctx->base.pipe, msaa_src);
2799
}
2800
2801
return ctx->fs_stencil_blit_fallback[msaa_src];
2802
}
2803
2804
static void *
2805
get_stencil_blit_fallback_dsa(struct blitter_context_priv *ctx, unsigned i)
2806
{
2807
assert(i < ARRAY_SIZE(ctx->dsa_replicate_stencil_bit));
2808
if (!ctx->dsa_replicate_stencil_bit[i]) {
2809
struct pipe_depth_stencil_alpha_state dsa = { 0 };
2810
dsa.depth_func = PIPE_FUNC_ALWAYS;
2811
dsa.stencil[0].enabled = 1;
2812
dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
2813
dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
2814
dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
2815
dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
2816
dsa.stencil[0].valuemask = 0xff;
2817
dsa.stencil[0].writemask = 1u << i;
2818
2819
ctx->dsa_replicate_stencil_bit[i] =
2820
ctx->base.pipe->create_depth_stencil_alpha_state(ctx->base.pipe, &dsa);
2821
}
2822
return ctx->dsa_replicate_stencil_bit[i];
2823
}
2824
2825
/**
2826
* Performs a series of draws to implement stencil blits texture without
2827
* requiring stencil writes, updating a single bit per pixel at the time.
2828
*/
2829
void
2830
util_blitter_stencil_fallback(struct blitter_context *blitter,
2831
struct pipe_resource *dst,
2832
unsigned dst_level,
2833
const struct pipe_box *dstbox,
2834
struct pipe_resource *src,
2835
unsigned src_level,
2836
const struct pipe_box *srcbox,
2837
const struct pipe_scissor_state *scissor)
2838
{
2839
struct blitter_context_priv *ctx = (struct blitter_context_priv *)blitter;
2840
struct pipe_context *pipe = ctx->base.pipe;
2841
2842
/* check the saved state */
2843
util_blitter_set_running_flag(blitter);
2844
blitter_check_saved_vertex_states(ctx);
2845
blitter_check_saved_fragment_states(ctx);
2846
blitter_check_saved_fb_state(ctx);
2847
blitter_disable_render_cond(ctx);
2848
2849
/* Initialize the surface. */
2850
struct pipe_surface *dst_view, dst_templ;
2851
util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstbox->z);
2852
dst_view = pipe->create_surface(pipe, dst, &dst_templ);
2853
2854
/* Initialize the sampler view. */
2855
struct pipe_sampler_view src_templ, *src_view;
2856
util_blitter_default_src_texture(blitter, &src_templ, src, src_level);
2857
src_templ.format = util_format_stencil_only(src_templ.format);
2858
src_view = pipe->create_sampler_view(pipe, src, &src_templ);
2859
2860
/* bind states */
2861
pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2862
pipe->bind_fs_state(pipe,
2863
get_stencil_blit_fallback_fs(ctx, src->nr_samples > 1));
2864
2865
/* set a framebuffer state */
2866
struct pipe_framebuffer_state fb_state = { 0 };
2867
fb_state.width = dstbox->width;
2868
fb_state.height = dstbox->height;
2869
fb_state.zsbuf = dst_view;
2870
pipe->set_framebuffer_state(pipe, &fb_state);
2871
pipe->set_sample_mask(pipe, ~0);
2872
2873
blitter_set_common_draw_rect_state(ctx, scissor != NULL,
2874
util_framebuffer_get_num_samples(&fb_state) > 1);
2875
blitter_set_dst_dimensions(ctx, dst_view->width, dst_view->height);
2876
2877
if (scissor) {
2878
pipe->clear_depth_stencil(pipe, dst_view, PIPE_CLEAR_STENCIL, 0.0, 0,
2879
MAX2(dstbox->x, scissor->minx),
2880
MAX2(dstbox->y, scissor->miny),
2881
MIN2(dstbox->x + dstbox->width, scissor->maxx) - dstbox->x,
2882
MIN2(dstbox->y + dstbox->height, scissor->maxy) - dstbox->y,
2883
true);
2884
pipe->set_scissor_states(pipe, 0, 1, scissor);
2885
} else {
2886
pipe->clear_depth_stencil(pipe, dst_view, PIPE_CLEAR_STENCIL, 0.0, 0,
2887
dstbox->x, dstbox->y,
2888
dstbox->width, dstbox->height,
2889
true);
2890
}
2891
2892
pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, &src_view);
2893
pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &ctx->sampler_state);
2894
2895
unsigned stencil_bits =
2896
util_format_get_component_bits(dst->format,
2897
UTIL_FORMAT_COLORSPACE_ZS, 1);
2898
2899
struct pipe_stencil_ref sr = { { (1u << stencil_bits) - 1 } };
2900
pipe->set_stencil_ref(pipe, sr);
2901
2902
union blitter_attrib coord;
2903
get_texcoords(src_view, src->width0, src->height0,
2904
srcbox->x, srcbox->y,
2905
srcbox->x + srcbox->width, srcbox->y + srcbox->height,
2906
srcbox->z, 0, true,
2907
&coord);
2908
2909
for (int i = 0; i < stencil_bits; ++i) {
2910
uint32_t mask = 1 << i;
2911
struct pipe_constant_buffer cb = {
2912
.user_buffer = &mask,
2913
.buffer_size = sizeof(mask),
2914
};
2915
pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, blitter->cb_slot,
2916
false, &cb);
2917
2918
pipe->bind_depth_stencil_alpha_state(pipe,
2919
get_stencil_blit_fallback_dsa(ctx, i));
2920
2921
blitter->draw_rectangle(blitter, ctx->velem_state,
2922
get_vs_passthrough_pos_generic,
2923
dstbox->x, dstbox->y,
2924
dstbox->x + dstbox->width,
2925
dstbox->y + dstbox->height,
2926
0, 1,
2927
UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW,
2928
&coord);
2929
}
2930
2931
if (scissor)
2932
pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
2933
2934
util_blitter_restore_vertex_states(blitter);
2935
util_blitter_restore_fragment_states(blitter);
2936
util_blitter_restore_textures(blitter);
2937
util_blitter_restore_fb_state(blitter);
2938
util_blitter_restore_render_cond(blitter);
2939
util_blitter_restore_constant_buffer_state(blitter);
2940
util_blitter_unset_running_flag(blitter);
2941
2942
pipe_surface_reference(&dst_view, NULL);
2943
pipe_sampler_view_reference(&src_view, NULL);
2944
}
2945
2946