summaryrefslogtreecommitdiff
path: root/src/freedreno/vulkan/tu_meta_clear.c
blob: 4b7e116949b9194f5226707dfd1d7349c3999b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright © 2015 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "tu_private.h"
#include "tu_blit.h"
#include "tu_cs.h"

static void
clear_image(struct tu_cmd_buffer *cmdbuf,
            struct tu_image *image,
            uint32_t clear_value[4],
            const VkImageSubresourceRange *range)
{
   uint32_t level_count = tu_get_levelCount(image, range);
   uint32_t layer_count = tu_get_layerCount(image, range);

   if (image->type == VK_IMAGE_TYPE_3D) {
      assert(layer_count == 1);
      assert(range->baseArrayLayer == 0);
   }

   for (unsigned j = 0; j < level_count; j++) {
      if (image->type == VK_IMAGE_TYPE_3D)
         layer_count = u_minify(image->extent.depth, range->baseMipLevel + j);

      tu_blit(cmdbuf, &(struct tu_blit) {
         .dst = tu_blit_surf_whole(image, range->baseMipLevel + j, range->baseArrayLayer),
         .layers = layer_count,
         .clear_value = {clear_value[0], clear_value[1], clear_value[2], clear_value[3]},
         .type = TU_BLIT_CLEAR,
      });
   }
}

void
tu_CmdClearColorImage(VkCommandBuffer commandBuffer,
                      VkImage image_h,
                      VkImageLayout imageLayout,
                      const VkClearColorValue *pColor,
                      uint32_t rangeCount,
                      const VkImageSubresourceRange *pRanges)
{
   TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, commandBuffer);
   TU_FROM_HANDLE(tu_image, image, image_h);
   uint32_t clear_value[4] = {};

   tu_2d_clear_color(pColor, image->vk_format, clear_value);

   tu_bo_list_add(&cmdbuf->bo_list, image->bo, MSM_SUBMIT_BO_WRITE);

   for (unsigned i = 0; i < rangeCount; i++)
      clear_image(cmdbuf, image, clear_value, pRanges + i);
}

void
tu_CmdClearDepthStencilImage(VkCommandBuffer commandBuffer,
                             VkImage image_h,
                             VkImageLayout imageLayout,
                             const VkClearDepthStencilValue *pDepthStencil,
                             uint32_t rangeCount,
                             const VkImageSubresourceRange *pRanges)
{
   TU_FROM_HANDLE(tu_cmd_buffer, cmdbuf, commandBuffer);
   TU_FROM_HANDLE(tu_image, image, image_h);
   uint32_t clear_value[4] = {};

   tu_2d_clear_zs(pDepthStencil, image->vk_format, clear_value);

   tu_bo_list_add(&cmdbuf->bo_list, image->bo, MSM_SUBMIT_BO_WRITE);

   for (unsigned i = 0; i < rangeCount; i++)
      clear_image(cmdbuf, image, clear_value, pRanges + i);
}

void
tu_CmdClearAttachments(VkCommandBuffer commandBuffer,
                       uint32_t attachmentCount,
                       const VkClearAttachment *pAttachments,
                       uint32_t rectCount,
                       const VkClearRect *pRects)
{
   TU_FROM_HANDLE(tu_cmd_buffer, cmd, commandBuffer);
   const struct tu_subpass *subpass = cmd->state.subpass;
   struct tu_cs *cs = &cmd->draw_cs;

   VkResult result = tu_cs_reserve_space(cmd->device, cs,
                                         rectCount * (3 + 15 * attachmentCount));
   if (result != VK_SUCCESS) {
      cmd->record_result = result;
      return;
   }

   /* TODO: deal with layered rendering (when layered rendering is implemented)
    * TODO: disable bypass rendering for subpass (when bypass is implemented)
    */

   for (unsigned i = 0; i < rectCount; i++) {
      unsigned x1 = pRects[i].rect.offset.x;
      unsigned y1 = pRects[i].rect.offset.y;
      unsigned x2 = x1 + pRects[i].rect.extent.width - 1;
      unsigned y2 = y1 + pRects[i].rect.extent.height - 1;

      tu_cs_emit_pkt4(cs, REG_A6XX_RB_BLIT_SCISSOR_TL, 2);
      tu_cs_emit(cs, A6XX_RB_BLIT_SCISSOR_TL_X(x1) | A6XX_RB_BLIT_SCISSOR_TL_Y(y1));
      tu_cs_emit(cs, A6XX_RB_BLIT_SCISSOR_BR_X(x2) | A6XX_RB_BLIT_SCISSOR_BR_Y(y2));

      for (unsigned j = 0; j < attachmentCount; j++) {
         uint32_t a;
         unsigned clear_mask = 0;
         if (pAttachments[j].aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
            clear_mask = 0xf;
            a = subpass->color_attachments[pAttachments[j].colorAttachment].attachment;
         } else {
            a = subpass->depth_stencil_attachment.attachment;
            if (pAttachments[j].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT)
               clear_mask |= 1;
            if (pAttachments[j].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)
               clear_mask |= 2;
         }

         if (a == VK_ATTACHMENT_UNUSED)
               continue;

         VkFormat fmt = cmd->state.pass->attachments[a].format;
         const struct tu_native_format *format = tu6_get_native_format(fmt);
         assert(format && format->rb >= 0);

         tu_cs_emit_pkt4(cs, REG_A6XX_RB_BLIT_DST_INFO, 1);
         tu_cs_emit(cs, A6XX_RB_BLIT_DST_INFO_COLOR_FORMAT(format->rb));

         tu_cs_emit_pkt4(cs, REG_A6XX_RB_BLIT_INFO, 1);
         tu_cs_emit(cs, A6XX_RB_BLIT_INFO_GMEM | A6XX_RB_BLIT_INFO_CLEAR_MASK(clear_mask));

         tu_cs_emit_pkt4(cs, REG_A6XX_RB_BLIT_BASE_GMEM, 1);
         tu_cs_emit(cs, cmd->state.pass->attachments[a].gmem_offset);

         tu_cs_emit_pkt4(cs, REG_A6XX_RB_UNKNOWN_88D0, 1);
         tu_cs_emit(cs, 0);

         uint32_t clear_vals[4] = { 0 };
         tu_pack_clear_value(&pAttachments[j].clearValue, fmt, clear_vals);

         tu_cs_emit_pkt4(cs, REG_A6XX_RB_BLIT_CLEAR_COLOR_DW0, 4);
         tu_cs_emit(cs, clear_vals[0]);
         tu_cs_emit(cs, clear_vals[1]);
         tu_cs_emit(cs, clear_vals[2]);
         tu_cs_emit(cs, clear_vals[3]);

         tu6_emit_event_write(cmd, cs, BLIT, false);
      }
   }
}