Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/amd/vulkan/radv_meta.c
7202 views
1
/*
2
* Copyright © 2016 Red Hat
3
* based on intel anv code:
4
* Copyright © 2015 Intel Corporation
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the "Software"),
8
* to deal in the Software without restriction, including without limitation
9
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
* and/or sell copies of the Software, and to permit persons to whom the
11
* Software is furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice (including the next
14
* paragraph) shall be included in all copies or substantial portions of the
15
* Software.
16
*
17
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
* IN THE SOFTWARE.
24
*/
25
26
#include "radv_meta.h"
27
28
#include "vk_util.h"
29
30
#include <fcntl.h>
31
#include <limits.h>
32
#ifndef _WIN32
33
#include <pwd.h>
34
#endif
35
#include <sys/stat.h>
36
37
void
38
radv_meta_save(struct radv_meta_saved_state *state, struct radv_cmd_buffer *cmd_buffer,
39
uint32_t flags)
40
{
41
VkPipelineBindPoint bind_point = flags & RADV_META_SAVE_GRAPHICS_PIPELINE
42
? VK_PIPELINE_BIND_POINT_GRAPHICS
43
: VK_PIPELINE_BIND_POINT_COMPUTE;
44
struct radv_descriptor_state *descriptors_state =
45
radv_get_descriptors_state(cmd_buffer, bind_point);
46
47
assert(flags & (RADV_META_SAVE_GRAPHICS_PIPELINE | RADV_META_SAVE_COMPUTE_PIPELINE));
48
49
state->flags = flags;
50
51
if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE) {
52
assert(!(state->flags & RADV_META_SAVE_COMPUTE_PIPELINE));
53
54
state->old_pipeline = cmd_buffer->state.pipeline;
55
56
/* Save all viewports. */
57
state->viewport.count = cmd_buffer->state.dynamic.viewport.count;
58
typed_memcpy(state->viewport.viewports, cmd_buffer->state.dynamic.viewport.viewports,
59
MAX_VIEWPORTS);
60
61
/* Save all scissors. */
62
state->scissor.count = cmd_buffer->state.dynamic.scissor.count;
63
typed_memcpy(state->scissor.scissors, cmd_buffer->state.dynamic.scissor.scissors,
64
MAX_SCISSORS);
65
66
state->cull_mode = cmd_buffer->state.dynamic.cull_mode;
67
state->front_face = cmd_buffer->state.dynamic.front_face;
68
69
state->primitive_topology = cmd_buffer->state.dynamic.primitive_topology;
70
71
state->depth_test_enable = cmd_buffer->state.dynamic.depth_test_enable;
72
state->depth_write_enable = cmd_buffer->state.dynamic.depth_write_enable;
73
state->depth_compare_op = cmd_buffer->state.dynamic.depth_compare_op;
74
state->depth_bounds_test_enable = cmd_buffer->state.dynamic.depth_bounds_test_enable;
75
state->stencil_test_enable = cmd_buffer->state.dynamic.stencil_test_enable;
76
77
state->stencil_op.front.compare_op = cmd_buffer->state.dynamic.stencil_op.front.compare_op;
78
state->stencil_op.front.fail_op = cmd_buffer->state.dynamic.stencil_op.front.fail_op;
79
state->stencil_op.front.pass_op = cmd_buffer->state.dynamic.stencil_op.front.pass_op;
80
state->stencil_op.front.depth_fail_op =
81
cmd_buffer->state.dynamic.stencil_op.front.depth_fail_op;
82
83
state->stencil_op.back.compare_op = cmd_buffer->state.dynamic.stencil_op.back.compare_op;
84
state->stencil_op.back.fail_op = cmd_buffer->state.dynamic.stencil_op.back.fail_op;
85
state->stencil_op.back.pass_op = cmd_buffer->state.dynamic.stencil_op.back.pass_op;
86
state->stencil_op.back.depth_fail_op =
87
cmd_buffer->state.dynamic.stencil_op.back.depth_fail_op;
88
89
state->fragment_shading_rate.size = cmd_buffer->state.dynamic.fragment_shading_rate.size;
90
state->fragment_shading_rate.combiner_ops[0] =
91
cmd_buffer->state.dynamic.fragment_shading_rate.combiner_ops[0];
92
state->fragment_shading_rate.combiner_ops[1] =
93
cmd_buffer->state.dynamic.fragment_shading_rate.combiner_ops[1];
94
95
state->depth_bias_enable = cmd_buffer->state.dynamic.depth_bias_enable;
96
97
state->primitive_restart_enable = cmd_buffer->state.dynamic.primitive_restart_enable;
98
99
state->rasterizer_discard_enable = cmd_buffer->state.dynamic.rasterizer_discard_enable;
100
101
state->logic_op = cmd_buffer->state.dynamic.logic_op;
102
103
state->color_write_enable = cmd_buffer->state.dynamic.color_write_enable;
104
}
105
106
if (state->flags & RADV_META_SAVE_SAMPLE_LOCATIONS) {
107
typed_memcpy(&state->sample_location, &cmd_buffer->state.dynamic.sample_location, 1);
108
}
109
110
if (state->flags & RADV_META_SAVE_COMPUTE_PIPELINE) {
111
assert(!(state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE));
112
113
state->old_pipeline = cmd_buffer->state.compute_pipeline;
114
}
115
116
if (state->flags & RADV_META_SAVE_DESCRIPTORS) {
117
state->old_descriptor_set0 = descriptors_state->sets[0];
118
if (!(descriptors_state->valid & 1) || !state->old_descriptor_set0)
119
state->flags &= ~RADV_META_SAVE_DESCRIPTORS;
120
}
121
122
if (state->flags & RADV_META_SAVE_CONSTANTS) {
123
memcpy(state->push_constants, cmd_buffer->push_constants, MAX_PUSH_CONSTANTS_SIZE);
124
}
125
126
if (state->flags & RADV_META_SAVE_PASS) {
127
state->pass = cmd_buffer->state.pass;
128
state->subpass = cmd_buffer->state.subpass;
129
state->framebuffer = cmd_buffer->state.framebuffer;
130
state->attachments = cmd_buffer->state.attachments;
131
state->render_area = cmd_buffer->state.render_area;
132
}
133
}
134
135
void
136
radv_meta_restore(const struct radv_meta_saved_state *state, struct radv_cmd_buffer *cmd_buffer)
137
{
138
VkPipelineBindPoint bind_point = state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE
139
? VK_PIPELINE_BIND_POINT_GRAPHICS
140
: VK_PIPELINE_BIND_POINT_COMPUTE;
141
142
if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE) {
143
radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_GRAPHICS,
144
radv_pipeline_to_handle(state->old_pipeline));
145
146
cmd_buffer->state.dirty |= RADV_CMD_DIRTY_PIPELINE;
147
148
/* Restore all viewports. */
149
cmd_buffer->state.dynamic.viewport.count = state->viewport.count;
150
typed_memcpy(cmd_buffer->state.dynamic.viewport.viewports, state->viewport.viewports,
151
MAX_VIEWPORTS);
152
153
/* Restore all scissors. */
154
cmd_buffer->state.dynamic.scissor.count = state->scissor.count;
155
typed_memcpy(cmd_buffer->state.dynamic.scissor.scissors, state->scissor.scissors,
156
MAX_SCISSORS);
157
158
cmd_buffer->state.dynamic.cull_mode = state->cull_mode;
159
cmd_buffer->state.dynamic.front_face = state->front_face;
160
161
cmd_buffer->state.dynamic.primitive_topology = state->primitive_topology;
162
163
cmd_buffer->state.dynamic.depth_test_enable = state->depth_test_enable;
164
cmd_buffer->state.dynamic.depth_write_enable = state->depth_write_enable;
165
cmd_buffer->state.dynamic.depth_compare_op = state->depth_compare_op;
166
cmd_buffer->state.dynamic.depth_bounds_test_enable = state->depth_bounds_test_enable;
167
cmd_buffer->state.dynamic.stencil_test_enable = state->stencil_test_enable;
168
169
cmd_buffer->state.dynamic.stencil_op.front.compare_op = state->stencil_op.front.compare_op;
170
cmd_buffer->state.dynamic.stencil_op.front.fail_op = state->stencil_op.front.fail_op;
171
cmd_buffer->state.dynamic.stencil_op.front.pass_op = state->stencil_op.front.pass_op;
172
cmd_buffer->state.dynamic.stencil_op.front.depth_fail_op =
173
state->stencil_op.front.depth_fail_op;
174
175
cmd_buffer->state.dynamic.stencil_op.back.compare_op = state->stencil_op.back.compare_op;
176
cmd_buffer->state.dynamic.stencil_op.back.fail_op = state->stencil_op.back.fail_op;
177
cmd_buffer->state.dynamic.stencil_op.back.pass_op = state->stencil_op.back.pass_op;
178
cmd_buffer->state.dynamic.stencil_op.back.depth_fail_op =
179
state->stencil_op.back.depth_fail_op;
180
181
cmd_buffer->state.dynamic.fragment_shading_rate.size = state->fragment_shading_rate.size;
182
cmd_buffer->state.dynamic.fragment_shading_rate.combiner_ops[0] =
183
state->fragment_shading_rate.combiner_ops[0];
184
cmd_buffer->state.dynamic.fragment_shading_rate.combiner_ops[1] =
185
state->fragment_shading_rate.combiner_ops[1];
186
187
cmd_buffer->state.dynamic.depth_bias_enable = state->depth_bias_enable;
188
189
cmd_buffer->state.dynamic.primitive_restart_enable = state->primitive_restart_enable;
190
191
cmd_buffer->state.dynamic.rasterizer_discard_enable = state->rasterizer_discard_enable;
192
193
cmd_buffer->state.dynamic.logic_op = state->logic_op;
194
195
cmd_buffer->state.dynamic.color_write_enable = state->color_write_enable;
196
197
cmd_buffer->state.dirty |=
198
RADV_CMD_DIRTY_DYNAMIC_VIEWPORT | RADV_CMD_DIRTY_DYNAMIC_SCISSOR |
199
RADV_CMD_DIRTY_DYNAMIC_CULL_MODE | RADV_CMD_DIRTY_DYNAMIC_FRONT_FACE |
200
RADV_CMD_DIRTY_DYNAMIC_PRIMITIVE_TOPOLOGY | RADV_CMD_DIRTY_DYNAMIC_DEPTH_TEST_ENABLE |
201
RADV_CMD_DIRTY_DYNAMIC_DEPTH_WRITE_ENABLE | RADV_CMD_DIRTY_DYNAMIC_DEPTH_COMPARE_OP |
202
RADV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS_TEST_ENABLE |
203
RADV_CMD_DIRTY_DYNAMIC_STENCIL_TEST_ENABLE | RADV_CMD_DIRTY_DYNAMIC_STENCIL_OP |
204
RADV_CMD_DIRTY_DYNAMIC_FRAGMENT_SHADING_RATE | RADV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS_ENABLE |
205
RADV_CMD_DIRTY_DYNAMIC_PRIMITIVE_RESTART_ENABLE |
206
RADV_CMD_DIRTY_DYNAMIC_RASTERIZER_DISCARD_ENABLE | RADV_CMD_DIRTY_DYNAMIC_LOGIC_OP |
207
RADV_CMD_DIRTY_DYNAMIC_COLOR_WRITE_ENABLE;
208
}
209
210
if (state->flags & RADV_META_SAVE_SAMPLE_LOCATIONS) {
211
typed_memcpy(&cmd_buffer->state.dynamic.sample_location.locations,
212
&state->sample_location.locations, 1);
213
214
cmd_buffer->state.dirty |= RADV_CMD_DIRTY_DYNAMIC_SAMPLE_LOCATIONS;
215
}
216
217
if (state->flags & RADV_META_SAVE_COMPUTE_PIPELINE) {
218
radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE,
219
radv_pipeline_to_handle(state->old_pipeline));
220
}
221
222
if (state->flags & RADV_META_SAVE_DESCRIPTORS) {
223
radv_set_descriptor_set(cmd_buffer, bind_point, state->old_descriptor_set0, 0);
224
}
225
226
if (state->flags & RADV_META_SAVE_CONSTANTS) {
227
VkShaderStageFlags stages = VK_SHADER_STAGE_COMPUTE_BIT;
228
229
if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE)
230
stages |= VK_SHADER_STAGE_ALL_GRAPHICS;
231
232
radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer), VK_NULL_HANDLE, stages, 0,
233
MAX_PUSH_CONSTANTS_SIZE, state->push_constants);
234
}
235
236
if (state->flags & RADV_META_SAVE_PASS) {
237
cmd_buffer->state.pass = state->pass;
238
cmd_buffer->state.subpass = state->subpass;
239
cmd_buffer->state.framebuffer = state->framebuffer;
240
cmd_buffer->state.attachments = state->attachments;
241
cmd_buffer->state.render_area = state->render_area;
242
if (state->subpass)
243
cmd_buffer->state.dirty |= RADV_CMD_DIRTY_FRAMEBUFFER;
244
}
245
}
246
247
VkImageViewType
248
radv_meta_get_view_type(const struct radv_image *image)
249
{
250
switch (image->type) {
251
case VK_IMAGE_TYPE_1D:
252
return VK_IMAGE_VIEW_TYPE_1D;
253
case VK_IMAGE_TYPE_2D:
254
return VK_IMAGE_VIEW_TYPE_2D;
255
case VK_IMAGE_TYPE_3D:
256
return VK_IMAGE_VIEW_TYPE_3D;
257
default:
258
unreachable("bad VkImageViewType");
259
}
260
}
261
262
/**
263
* When creating a destination VkImageView, this function provides the needed
264
* VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
265
*/
266
uint32_t
267
radv_meta_get_iview_layer(const struct radv_image *dest_image,
268
const VkImageSubresourceLayers *dest_subresource,
269
const VkOffset3D *dest_offset)
270
{
271
switch (dest_image->type) {
272
case VK_IMAGE_TYPE_1D:
273
case VK_IMAGE_TYPE_2D:
274
return dest_subresource->baseArrayLayer;
275
case VK_IMAGE_TYPE_3D:
276
/* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
277
* but meta does it anyway. When doing so, we translate the
278
* destination's z offset into an array offset.
279
*/
280
return dest_offset->z;
281
default:
282
assert(!"bad VkImageType");
283
return 0;
284
}
285
}
286
287
static void *
288
meta_alloc(void *_device, size_t size, size_t alignment, VkSystemAllocationScope allocationScope)
289
{
290
struct radv_device *device = _device;
291
return device->vk.alloc.pfnAllocation(device->vk.alloc.pUserData, size, alignment,
292
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
293
}
294
295
static void *
296
meta_realloc(void *_device, void *original, size_t size, size_t alignment,
297
VkSystemAllocationScope allocationScope)
298
{
299
struct radv_device *device = _device;
300
return device->vk.alloc.pfnReallocation(device->vk.alloc.pUserData, original, size, alignment,
301
VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
302
}
303
304
static void
305
meta_free(void *_device, void *data)
306
{
307
struct radv_device *device = _device;
308
device->vk.alloc.pfnFree(device->vk.alloc.pUserData, data);
309
}
310
311
#ifndef _WIN32
312
static bool
313
radv_builtin_cache_path(char *path)
314
{
315
char *xdg_cache_home = getenv("XDG_CACHE_HOME");
316
const char *suffix = "/radv_builtin_shaders";
317
const char *suffix2 = "/.cache/radv_builtin_shaders";
318
struct passwd pwd, *result;
319
char path2[PATH_MAX + 1]; /* PATH_MAX is not a real max,but suffices here. */
320
int ret;
321
322
if (xdg_cache_home) {
323
ret = snprintf(path, PATH_MAX + 1, "%s%s%zd", xdg_cache_home, suffix, sizeof(void *) * 8);
324
return ret > 0 && ret < PATH_MAX + 1;
325
}
326
327
getpwuid_r(getuid(), &pwd, path2, PATH_MAX - strlen(suffix2), &result);
328
if (!result)
329
return false;
330
331
strcpy(path, pwd.pw_dir);
332
strcat(path, "/.cache");
333
if (mkdir(path, 0755) && errno != EEXIST)
334
return false;
335
336
ret = snprintf(path, PATH_MAX + 1, "%s%s%zd", pwd.pw_dir, suffix2, sizeof(void *) * 8);
337
return ret > 0 && ret < PATH_MAX + 1;
338
}
339
#endif
340
341
static bool
342
radv_load_meta_pipeline(struct radv_device *device)
343
{
344
#ifdef _WIN32
345
return false;
346
#else
347
char path[PATH_MAX + 1];
348
struct stat st;
349
void *data = NULL;
350
bool ret = false;
351
352
if (!radv_builtin_cache_path(path))
353
return false;
354
355
int fd = open(path, O_RDONLY);
356
if (fd < 0)
357
return false;
358
if (fstat(fd, &st))
359
goto fail;
360
data = malloc(st.st_size);
361
if (!data)
362
goto fail;
363
if (read(fd, data, st.st_size) == -1)
364
goto fail;
365
366
ret = radv_pipeline_cache_load(&device->meta_state.cache, data, st.st_size);
367
fail:
368
free(data);
369
close(fd);
370
return ret;
371
#endif
372
}
373
374
static void
375
radv_store_meta_pipeline(struct radv_device *device)
376
{
377
#ifndef _WIN32
378
char path[PATH_MAX + 1], path2[PATH_MAX + 7];
379
size_t size;
380
void *data = NULL;
381
382
if (!device->meta_state.cache.modified)
383
return;
384
385
if (radv_GetPipelineCacheData(radv_device_to_handle(device),
386
radv_pipeline_cache_to_handle(&device->meta_state.cache), &size,
387
NULL))
388
return;
389
390
if (!radv_builtin_cache_path(path))
391
return;
392
393
strcpy(path2, path);
394
strcat(path2, "XXXXXX");
395
int fd = mkstemp(path2); // open(path, O_WRONLY | O_CREAT, 0600);
396
if (fd < 0)
397
return;
398
data = malloc(size);
399
if (!data)
400
goto fail;
401
402
if (radv_GetPipelineCacheData(radv_device_to_handle(device),
403
radv_pipeline_cache_to_handle(&device->meta_state.cache), &size,
404
data))
405
goto fail;
406
if (write(fd, data, size) == -1)
407
goto fail;
408
409
rename(path2, path);
410
fail:
411
free(data);
412
close(fd);
413
unlink(path2);
414
#endif
415
}
416
417
VkResult
418
radv_device_init_meta(struct radv_device *device)
419
{
420
VkResult result;
421
422
memset(&device->meta_state, 0, sizeof(device->meta_state));
423
424
device->meta_state.alloc = (VkAllocationCallbacks){
425
.pUserData = device,
426
.pfnAllocation = meta_alloc,
427
.pfnReallocation = meta_realloc,
428
.pfnFree = meta_free,
429
};
430
431
device->meta_state.cache.alloc = device->meta_state.alloc;
432
radv_pipeline_cache_init(&device->meta_state.cache, device);
433
bool loaded_cache = radv_load_meta_pipeline(device);
434
bool on_demand = !loaded_cache;
435
436
mtx_init(&device->meta_state.mtx, mtx_plain);
437
438
result = radv_device_init_meta_clear_state(device, on_demand);
439
if (result != VK_SUCCESS)
440
goto fail_clear;
441
442
result = radv_device_init_meta_resolve_state(device, on_demand);
443
if (result != VK_SUCCESS)
444
goto fail_resolve;
445
446
result = radv_device_init_meta_blit_state(device, on_demand);
447
if (result != VK_SUCCESS)
448
goto fail_blit;
449
450
result = radv_device_init_meta_blit2d_state(device, on_demand);
451
if (result != VK_SUCCESS)
452
goto fail_blit2d;
453
454
result = radv_device_init_meta_bufimage_state(device);
455
if (result != VK_SUCCESS)
456
goto fail_bufimage;
457
458
result = radv_device_init_meta_depth_decomp_state(device, on_demand);
459
if (result != VK_SUCCESS)
460
goto fail_depth_decomp;
461
462
result = radv_device_init_meta_buffer_state(device);
463
if (result != VK_SUCCESS)
464
goto fail_buffer;
465
466
result = radv_device_init_meta_query_state(device, on_demand);
467
if (result != VK_SUCCESS)
468
goto fail_query;
469
470
result = radv_device_init_meta_fast_clear_flush_state(device, on_demand);
471
if (result != VK_SUCCESS)
472
goto fail_fast_clear;
473
474
result = radv_device_init_meta_resolve_compute_state(device, on_demand);
475
if (result != VK_SUCCESS)
476
goto fail_resolve_compute;
477
478
result = radv_device_init_meta_resolve_fragment_state(device, on_demand);
479
if (result != VK_SUCCESS)
480
goto fail_resolve_fragment;
481
482
result = radv_device_init_meta_fmask_expand_state(device);
483
if (result != VK_SUCCESS)
484
goto fail_fmask_expand;
485
486
result = radv_device_init_accel_struct_build_state(device);
487
if (result != VK_SUCCESS)
488
goto fail_accel_struct_build;
489
490
return VK_SUCCESS;
491
492
fail_accel_struct_build:
493
radv_device_finish_meta_fmask_expand_state(device);
494
fail_fmask_expand:
495
radv_device_finish_meta_resolve_fragment_state(device);
496
fail_resolve_fragment:
497
radv_device_finish_meta_resolve_compute_state(device);
498
fail_resolve_compute:
499
radv_device_finish_meta_fast_clear_flush_state(device);
500
fail_fast_clear:
501
radv_device_finish_meta_query_state(device);
502
fail_query:
503
radv_device_finish_meta_buffer_state(device);
504
fail_buffer:
505
radv_device_finish_meta_depth_decomp_state(device);
506
fail_depth_decomp:
507
radv_device_finish_meta_bufimage_state(device);
508
fail_bufimage:
509
radv_device_finish_meta_blit2d_state(device);
510
fail_blit2d:
511
radv_device_finish_meta_blit_state(device);
512
fail_blit:
513
radv_device_finish_meta_resolve_state(device);
514
fail_resolve:
515
radv_device_finish_meta_clear_state(device);
516
fail_clear:
517
mtx_destroy(&device->meta_state.mtx);
518
radv_pipeline_cache_finish(&device->meta_state.cache);
519
return result;
520
}
521
522
void
523
radv_device_finish_meta(struct radv_device *device)
524
{
525
radv_device_finish_accel_struct_build_state(device);
526
radv_device_finish_meta_clear_state(device);
527
radv_device_finish_meta_resolve_state(device);
528
radv_device_finish_meta_blit_state(device);
529
radv_device_finish_meta_blit2d_state(device);
530
radv_device_finish_meta_bufimage_state(device);
531
radv_device_finish_meta_depth_decomp_state(device);
532
radv_device_finish_meta_query_state(device);
533
radv_device_finish_meta_buffer_state(device);
534
radv_device_finish_meta_fast_clear_flush_state(device);
535
radv_device_finish_meta_resolve_compute_state(device);
536
radv_device_finish_meta_resolve_fragment_state(device);
537
radv_device_finish_meta_fmask_expand_state(device);
538
radv_device_finish_meta_dcc_retile_state(device);
539
radv_device_finish_meta_copy_vrs_htile_state(device);
540
541
radv_store_meta_pipeline(device);
542
radv_pipeline_cache_finish(&device->meta_state.cache);
543
mtx_destroy(&device->meta_state.mtx);
544
}
545
546
nir_ssa_def *
547
radv_meta_gen_rect_vertices_comp2(nir_builder *vs_b, nir_ssa_def *comp2)
548
{
549
550
nir_ssa_def *vertex_id = nir_load_vertex_id_zero_base(vs_b);
551
552
/* vertex 0 - -1.0, -1.0 */
553
/* vertex 1 - -1.0, 1.0 */
554
/* vertex 2 - 1.0, -1.0 */
555
/* so channel 0 is vertex_id != 2 ? -1.0 : 1.0
556
channel 1 is vertex id != 1 ? -1.0 : 1.0 */
557
558
nir_ssa_def *c0cmp = nir_ine(vs_b, vertex_id, nir_imm_int(vs_b, 2));
559
nir_ssa_def *c1cmp = nir_ine(vs_b, vertex_id, nir_imm_int(vs_b, 1));
560
561
nir_ssa_def *comp[4];
562
comp[0] = nir_bcsel(vs_b, c0cmp, nir_imm_float(vs_b, -1.0), nir_imm_float(vs_b, 1.0));
563
564
comp[1] = nir_bcsel(vs_b, c1cmp, nir_imm_float(vs_b, -1.0), nir_imm_float(vs_b, 1.0));
565
comp[2] = comp2;
566
comp[3] = nir_imm_float(vs_b, 1.0);
567
nir_ssa_def *outvec = nir_vec(vs_b, comp, 4);
568
569
return outvec;
570
}
571
572
nir_ssa_def *
573
radv_meta_gen_rect_vertices(nir_builder *vs_b)
574
{
575
return radv_meta_gen_rect_vertices_comp2(vs_b, nir_imm_float(vs_b, 0.0));
576
}
577
578
/* vertex shader that generates vertices */
579
nir_shader *
580
radv_meta_build_nir_vs_generate_vertices(void)
581
{
582
const struct glsl_type *vec4 = glsl_vec4_type();
583
584
nir_variable *v_position;
585
586
nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_VERTEX, NULL, "meta_vs_gen_verts");
587
588
nir_ssa_def *outvec = radv_meta_gen_rect_vertices(&b);
589
590
v_position = nir_variable_create(b.shader, nir_var_shader_out, vec4, "gl_Position");
591
v_position->data.location = VARYING_SLOT_POS;
592
593
nir_store_var(&b, v_position, outvec, 0xf);
594
595
return b.shader;
596
}
597
598
nir_shader *
599
radv_meta_build_nir_fs_noop(void)
600
{
601
nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, NULL, "meta_noop_fs");
602
603
return b.shader;
604
}
605
606
void
607
radv_meta_build_resolve_shader_core(nir_builder *b, bool is_integer, int samples,
608
nir_variable *input_img, nir_variable *color,
609
nir_ssa_def *img_coord)
610
{
611
/* do a txf_ms on each sample */
612
nir_ssa_def *tmp;
613
bool inserted_if = false;
614
615
nir_ssa_def *input_img_deref = &nir_build_deref_var(b, input_img)->dest.ssa;
616
617
nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3);
618
tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
619
tex->op = nir_texop_txf_ms;
620
tex->src[0].src_type = nir_tex_src_coord;
621
tex->src[0].src = nir_src_for_ssa(img_coord);
622
tex->src[1].src_type = nir_tex_src_ms_index;
623
tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
624
tex->src[2].src_type = nir_tex_src_texture_deref;
625
tex->src[2].src = nir_src_for_ssa(input_img_deref);
626
tex->dest_type = nir_type_float32;
627
tex->is_array = false;
628
tex->coord_components = 2;
629
630
nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
631
nir_builder_instr_insert(b, &tex->instr);
632
633
tmp = &tex->dest.ssa;
634
635
if (!is_integer && samples > 1) {
636
nir_tex_instr *tex_all_same = nir_tex_instr_create(b->shader, 2);
637
tex_all_same->sampler_dim = GLSL_SAMPLER_DIM_MS;
638
tex_all_same->op = nir_texop_samples_identical;
639
tex_all_same->src[0].src_type = nir_tex_src_coord;
640
tex_all_same->src[0].src = nir_src_for_ssa(img_coord);
641
tex_all_same->src[1].src_type = nir_tex_src_texture_deref;
642
tex_all_same->src[1].src = nir_src_for_ssa(input_img_deref);
643
tex_all_same->dest_type = nir_type_bool1;
644
tex_all_same->is_array = false;
645
tex_all_same->coord_components = 2;
646
647
nir_ssa_dest_init(&tex_all_same->instr, &tex_all_same->dest, 1, 1, "tex");
648
nir_builder_instr_insert(b, &tex_all_same->instr);
649
650
nir_ssa_def *all_same = nir_ieq(b, &tex_all_same->dest.ssa, nir_imm_bool(b, false));
651
nir_push_if(b, all_same);
652
for (int i = 1; i < samples; i++) {
653
nir_tex_instr *tex_add = nir_tex_instr_create(b->shader, 3);
654
tex_add->sampler_dim = GLSL_SAMPLER_DIM_MS;
655
tex_add->op = nir_texop_txf_ms;
656
tex_add->src[0].src_type = nir_tex_src_coord;
657
tex_add->src[0].src = nir_src_for_ssa(img_coord);
658
tex_add->src[1].src_type = nir_tex_src_ms_index;
659
tex_add->src[1].src = nir_src_for_ssa(nir_imm_int(b, i));
660
tex_add->src[2].src_type = nir_tex_src_texture_deref;
661
tex_add->src[2].src = nir_src_for_ssa(input_img_deref);
662
tex_add->dest_type = nir_type_float32;
663
tex_add->is_array = false;
664
tex_add->coord_components = 2;
665
666
nir_ssa_dest_init(&tex_add->instr, &tex_add->dest, 4, 32, "tex");
667
nir_builder_instr_insert(b, &tex_add->instr);
668
669
tmp = nir_fadd(b, tmp, &tex_add->dest.ssa);
670
}
671
672
tmp = nir_fdiv(b, tmp, nir_imm_float(b, samples));
673
nir_store_var(b, color, tmp, 0xf);
674
nir_push_else(b, NULL);
675
inserted_if = true;
676
}
677
nir_store_var(b, color, &tex->dest.ssa, 0xf);
678
679
if (inserted_if)
680
nir_pop_if(b, NULL);
681
}
682
683
nir_ssa_def *
684
radv_meta_load_descriptor(nir_builder *b, unsigned desc_set, unsigned binding)
685
{
686
nir_ssa_def *rsrc = nir_vulkan_resource_index(b, 3, 32, nir_imm_int(b, 0), .desc_set = desc_set,
687
.binding = binding);
688
return nir_channels(b, rsrc, 0x3);
689
}
690
691