1 /*
2 * Copyright © 2021 Google
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include <assert.h>
8 #include <stdbool.h>
9
10 #include "nir/nir_builder.h"
11 #include "radv_meta.h"
12 #include "sid.h"
13 #include "vk_common_entrypoints.h"
14 #include "vk_format.h"
15
16 VkResult
radv_device_init_meta_etc_decode_state(struct radv_device * device,bool on_demand)17 radv_device_init_meta_etc_decode_state(struct radv_device *device, bool on_demand)
18 {
19 const struct radv_physical_device *pdev = radv_device_physical(device);
20 struct radv_meta_state *state = &device->meta_state;
21
22 if (!pdev->emulate_etc2)
23 return VK_SUCCESS;
24
25 state->etc_decode.allocator = &state->alloc;
26 state->etc_decode.nir_options = &pdev->nir_options[MESA_SHADER_COMPUTE];
27 state->etc_decode.pipeline_cache = state->cache;
28 vk_texcompress_etc2_init(&device->vk, &state->etc_decode);
29
30 if (on_demand)
31 return VK_SUCCESS;
32
33 return vk_texcompress_etc2_late_init(&device->vk, &state->etc_decode);
34 }
35
36 void
radv_device_finish_meta_etc_decode_state(struct radv_device * device)37 radv_device_finish_meta_etc_decode_state(struct radv_device *device)
38 {
39 struct radv_meta_state *state = &device->meta_state;
40 vk_texcompress_etc2_finish(&device->vk, &state->etc_decode);
41 }
42
43 static VkPipeline
radv_get_etc_decode_pipeline(struct radv_cmd_buffer * cmd_buffer)44 radv_get_etc_decode_pipeline(struct radv_cmd_buffer *cmd_buffer)
45 {
46 struct radv_device *device = radv_cmd_buffer_device(cmd_buffer);
47 struct radv_meta_state *state = &device->meta_state;
48 VkResult ret;
49
50 ret = vk_texcompress_etc2_late_init(&device->vk, &state->etc_decode);
51 if (ret != VK_SUCCESS) {
52 vk_command_buffer_set_error(&cmd_buffer->vk, ret);
53 return VK_NULL_HANDLE;
54 }
55
56 return state->etc_decode.pipeline;
57 }
58
59 static void
decode_etc(struct radv_cmd_buffer * cmd_buffer,struct radv_image_view * src_iview,struct radv_image_view * dst_iview,const VkOffset3D * offset,const VkExtent3D * extent)60 decode_etc(struct radv_cmd_buffer *cmd_buffer, struct radv_image_view *src_iview, struct radv_image_view *dst_iview,
61 const VkOffset3D *offset, const VkExtent3D *extent)
62 {
63 struct radv_device *device = radv_cmd_buffer_device(cmd_buffer);
64 VkPipeline pipeline = radv_get_etc_decode_pipeline(cmd_buffer);
65
66 radv_meta_push_descriptor_set(cmd_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
67 device->meta_state.etc_decode.pipeline_layout, 0, 2,
68 (VkWriteDescriptorSet[]){{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
69 .dstBinding = 0,
70 .dstArrayElement = 0,
71 .descriptorCount = 1,
72 .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
73 .pImageInfo =
74 (VkDescriptorImageInfo[]){
75 {.sampler = VK_NULL_HANDLE,
76 .imageView = radv_image_view_to_handle(src_iview),
77 .imageLayout = VK_IMAGE_LAYOUT_GENERAL},
78 }},
79 {.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
80 .dstBinding = 1,
81 .dstArrayElement = 0,
82 .descriptorCount = 1,
83 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
84 .pImageInfo = (VkDescriptorImageInfo[]){
85 {
86 .sampler = VK_NULL_HANDLE,
87 .imageView = radv_image_view_to_handle(dst_iview),
88 .imageLayout = VK_IMAGE_LAYOUT_GENERAL,
89 },
90 }}});
91
92 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer), VK_PIPELINE_BIND_POINT_COMPUTE, pipeline);
93
94 unsigned push_constants[5] = {
95 offset->x, offset->y, offset->z, src_iview->image->vk.format, src_iview->image->vk.image_type,
96 };
97
98 vk_common_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer), device->meta_state.etc_decode.pipeline_layout,
99 VK_SHADER_STAGE_COMPUTE_BIT, 0, 20, push_constants);
100 radv_unaligned_dispatch(cmd_buffer, extent->width, extent->height, extent->depth);
101 }
102
103 void
radv_meta_decode_etc(struct radv_cmd_buffer * cmd_buffer,struct radv_image * image,VkImageLayout layout,const VkImageSubresourceLayers * subresource,VkOffset3D offset,VkExtent3D extent)104 radv_meta_decode_etc(struct radv_cmd_buffer *cmd_buffer, struct radv_image *image, VkImageLayout layout,
105 const VkImageSubresourceLayers *subresource, VkOffset3D offset, VkExtent3D extent)
106 {
107 struct radv_device *device = radv_cmd_buffer_device(cmd_buffer);
108 struct radv_meta_saved_state saved_state;
109 radv_meta_save(&saved_state, cmd_buffer,
110 RADV_META_SAVE_COMPUTE_PIPELINE | RADV_META_SAVE_CONSTANTS | RADV_META_SAVE_DESCRIPTORS |
111 RADV_META_SUSPEND_PREDICATING);
112
113 uint32_t base_slice = radv_meta_get_iview_layer(image, subresource, &offset);
114 uint32_t slice_count = image->vk.image_type == VK_IMAGE_TYPE_3D
115 ? extent.depth
116 : vk_image_subresource_layer_count(&image->vk, subresource);
117
118 extent = vk_image_sanitize_extent(&image->vk, extent);
119 offset = vk_image_sanitize_offset(&image->vk, offset);
120
121 VkFormat load_format = vk_texcompress_etc2_load_format(image->vk.format);
122 struct radv_image_view src_iview;
123 radv_image_view_init(
124 &src_iview, device,
125 &(VkImageViewCreateInfo){
126 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
127 .image = radv_image_to_handle(image),
128 .viewType = vk_texcompress_etc2_image_view_type(image->vk.image_type),
129 .format = load_format,
130 .subresourceRange =
131 {
132 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
133 .baseMipLevel = subresource->mipLevel,
134 .levelCount = 1,
135 .baseArrayLayer = 0,
136 .layerCount = subresource->baseArrayLayer + vk_image_subresource_layer_count(&image->vk, subresource),
137 },
138 },
139 0, NULL);
140
141 VkFormat store_format = vk_texcompress_etc2_store_format(image->vk.format);
142 struct radv_image_view dst_iview;
143 radv_image_view_init(
144 &dst_iview, device,
145 &(VkImageViewCreateInfo){
146 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
147 .image = radv_image_to_handle(image),
148 .viewType = vk_texcompress_etc2_image_view_type(image->vk.image_type),
149 .format = store_format,
150 .subresourceRange =
151 {
152 .aspectMask = VK_IMAGE_ASPECT_PLANE_1_BIT,
153 .baseMipLevel = subresource->mipLevel,
154 .levelCount = 1,
155 .baseArrayLayer = 0,
156 .layerCount = subresource->baseArrayLayer + vk_image_subresource_layer_count(&image->vk, subresource),
157 },
158 },
159 0, NULL);
160
161 decode_etc(cmd_buffer, &src_iview, &dst_iview, &(VkOffset3D){offset.x, offset.y, base_slice},
162 &(VkExtent3D){extent.width, extent.height, slice_count});
163
164 radv_image_view_finish(&src_iview);
165 radv_image_view_finish(&dst_iview);
166
167 radv_meta_restore(&saved_state, cmd_buffer);
168 }
169