anv: Add anv_render_pass_attachment::store_op
[mesa.git] / src / intel / vulkan / anv_meta.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_meta.h"
25
26 struct anv_render_pass anv_meta_dummy_renderpass = {0};
27
28 void
29 anv_meta_save(struct anv_meta_saved_state *state,
30 const struct anv_cmd_buffer *cmd_buffer,
31 uint32_t dynamic_mask)
32 {
33 state->old_pipeline = cmd_buffer->state.pipeline;
34 state->old_descriptor_set0 = cmd_buffer->state.descriptors[0];
35 memcpy(state->old_vertex_bindings, cmd_buffer->state.vertex_bindings,
36 sizeof(state->old_vertex_bindings));
37
38 state->dynamic_mask = dynamic_mask;
39 anv_dynamic_state_copy(&state->dynamic, &cmd_buffer->state.dynamic,
40 dynamic_mask);
41 }
42
43 void
44 anv_meta_restore(const struct anv_meta_saved_state *state,
45 struct anv_cmd_buffer *cmd_buffer)
46 {
47 cmd_buffer->state.pipeline = state->old_pipeline;
48 cmd_buffer->state.descriptors[0] = state->old_descriptor_set0;
49 memcpy(cmd_buffer->state.vertex_bindings, state->old_vertex_bindings,
50 sizeof(state->old_vertex_bindings));
51
52 cmd_buffer->state.vb_dirty |= (1 << ANV_META_VERTEX_BINDING_COUNT) - 1;
53 cmd_buffer->state.dirty |= ANV_CMD_DIRTY_PIPELINE;
54 cmd_buffer->state.descriptors_dirty |= VK_SHADER_STAGE_FRAGMENT_BIT;
55
56 anv_dynamic_state_copy(&cmd_buffer->state.dynamic, &state->dynamic,
57 state->dynamic_mask);
58 cmd_buffer->state.dirty |= state->dynamic_mask;
59
60 /* Since we've used the pipeline with the VS disabled, set
61 * need_query_wa. See CmdBeginQuery.
62 */
63 cmd_buffer->state.need_query_wa = true;
64 }
65
66 VkImageViewType
67 anv_meta_get_view_type(const struct anv_image *image)
68 {
69 switch (image->type) {
70 case VK_IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
71 case VK_IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
72 case VK_IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
73 default:
74 unreachable("bad VkImageViewType");
75 }
76 }
77
78 /**
79 * When creating a destination VkImageView, this function provides the needed
80 * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
81 */
82 uint32_t
83 anv_meta_get_iview_layer(const struct anv_image *dest_image,
84 const VkImageSubresourceLayers *dest_subresource,
85 const VkOffset3D *dest_offset)
86 {
87 switch (dest_image->type) {
88 case VK_IMAGE_TYPE_1D:
89 case VK_IMAGE_TYPE_2D:
90 return dest_subresource->baseArrayLayer;
91 case VK_IMAGE_TYPE_3D:
92 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
93 * but meta does it anyway. When doing so, we translate the
94 * destination's z offset into an array offset.
95 */
96 return dest_offset->z;
97 default:
98 assert(!"bad VkImageType");
99 return 0;
100 }
101 }
102
103 static void *
104 meta_alloc(void* _device, size_t size, size_t alignment,
105 VkSystemAllocationScope allocationScope)
106 {
107 struct anv_device *device = _device;
108 return device->alloc.pfnAllocation(device->alloc.pUserData, size, alignment,
109 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
110 }
111
112 static void *
113 meta_realloc(void* _device, void *original, size_t size, size_t alignment,
114 VkSystemAllocationScope allocationScope)
115 {
116 struct anv_device *device = _device;
117 return device->alloc.pfnReallocation(device->alloc.pUserData, original,
118 size, alignment,
119 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
120 }
121
122 static void
123 meta_free(void* _device, void *data)
124 {
125 struct anv_device *device = _device;
126 return device->alloc.pfnFree(device->alloc.pUserData, data);
127 }
128
129 VkResult
130 anv_device_init_meta(struct anv_device *device)
131 {
132 VkResult result;
133
134 device->meta_state.alloc = (VkAllocationCallbacks) {
135 .pUserData = device,
136 .pfnAllocation = meta_alloc,
137 .pfnReallocation = meta_realloc,
138 .pfnFree = meta_free,
139 };
140
141 result = anv_device_init_meta_clear_state(device);
142 if (result != VK_SUCCESS)
143 goto fail_clear;
144
145 result = anv_device_init_meta_resolve_state(device);
146 if (result != VK_SUCCESS)
147 goto fail_resolve;
148
149 result = anv_device_init_meta_blit_state(device);
150 if (result != VK_SUCCESS)
151 goto fail_blit;
152
153 result = anv_device_init_meta_blit2d_state(device);
154 if (result != VK_SUCCESS)
155 goto fail_blit2d;
156
157 return VK_SUCCESS;
158
159 fail_blit2d:
160 anv_device_finish_meta_blit_state(device);
161 fail_blit:
162 anv_device_finish_meta_resolve_state(device);
163 fail_resolve:
164 anv_device_finish_meta_clear_state(device);
165 fail_clear:
166 return result;
167 }
168
169 void
170 anv_device_finish_meta(struct anv_device *device)
171 {
172 anv_device_finish_meta_resolve_state(device);
173 anv_device_finish_meta_clear_state(device);
174 anv_device_finish_meta_blit_state(device);
175 anv_device_finish_meta_blit2d_state(device);
176 }