xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/zink/zink_context.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2018 Collabora Ltd.
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  * on the rights to use, copy, modify, merge, publish, distribute, sub
8  * license, and/or sell copies of the Software, and to permit persons to whom
9  * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef ZINK_CONTEXT_H
25 #define ZINK_CONTEXT_H
26 
27 #include "util/u_rect.h"
28 #include "zink_types.h"
29 #include "vk_enum_to_str.h"
30 
31 #define GFX_SHADER_BITS (VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | \
32                          VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT | \
33                          VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT | \
34                          VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT | \
35                          VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT)
36 
37 #define pipe_buffer_write "use tc_buffer_write to avoid breaking threaded context"
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 struct blitter_context;
44 struct list_head;
45 
46 struct zink_blend_state;
47 struct zink_depth_stencil_alpha_state;
48 struct zink_gfx_program;
49 struct zink_rasterizer_state;
50 struct zink_resource;
51 struct zink_vertex_elements_state;
52 
53 #define perf_debug(ctx, ...) do {                      \
54    util_debug_message(&ctx->dbg, PERF_INFO, __VA_ARGS__); \
55 } while(0)
56 
57 static inline struct zink_resource *
zink_descriptor_surface_resource(struct zink_descriptor_surface * ds)58 zink_descriptor_surface_resource(struct zink_descriptor_surface *ds)
59 {
60    return ds->is_buffer ?
61           zink_descriptor_mode == ZINK_DESCRIPTOR_MODE_DB ? zink_resource(ds->db.pres) : zink_resource(ds->bufferview->pres) :
62           (struct zink_resource*)ds->surface->base.texture;
63 }
64 
65 static inline bool
zink_fb_clear_enabled(const struct zink_context * ctx,unsigned idx)66 zink_fb_clear_enabled(const struct zink_context *ctx, unsigned idx)
67 {
68    if (idx == PIPE_MAX_COLOR_BUFS)
69       return ctx->clears_enabled & PIPE_CLEAR_DEPTHSTENCIL;
70    return ctx->clears_enabled & (PIPE_CLEAR_COLOR0 << idx);
71 }
72 
73 static inline uint32_t
zink_program_cache_stages(uint32_t stages_present)74 zink_program_cache_stages(uint32_t stages_present)
75 {
76    return (stages_present & ((1 << MESA_SHADER_TESS_CTRL) |
77                              (1 << MESA_SHADER_TESS_EVAL) |
78                              (1 << MESA_SHADER_GEOMETRY))) >> 1;
79 }
80 
81 static ALWAYS_INLINE bool
zink_is_zsbuf_used(const struct zink_context * ctx)82 zink_is_zsbuf_used(const struct zink_context *ctx)
83 {
84    return ctx->blitting || tc_renderpass_info_is_zsbuf_used(&ctx->dynamic_fb.tc_info);
85 }
86 
87 static ALWAYS_INLINE bool
zink_is_zsbuf_write(const struct zink_context * ctx)88 zink_is_zsbuf_write(const struct zink_context *ctx)
89 {
90    if (!zink_is_zsbuf_used(ctx))
91       return false;
92    return ctx->dynamic_fb.tc_info.zsbuf_write_fs || ctx->dynamic_fb.tc_info.zsbuf_write_dsa ||
93           ctx->dynamic_fb.tc_info.zsbuf_clear || ctx->dynamic_fb.tc_info.zsbuf_clear_partial;
94 }
95 
96 void
97 zink_fence_wait(struct pipe_context *ctx);
98 
99 void
100 zink_wait_on_batch(struct zink_context *ctx, uint64_t batch_id);
101 void
102 zink_reset_ds3_states(struct zink_context *ctx);
103 bool
104 zink_check_batch_completion(struct zink_context *ctx, uint64_t batch_id);
105 VkCommandBuffer
106 zink_get_cmdbuf(struct zink_context *ctx, struct zink_resource *src, struct zink_resource *dst);
107 unsigned
108 zink_update_rendering_info(struct zink_context *ctx);
109 void
110 zink_flush_queue(struct zink_context *ctx);
111 bool
112 zink_update_fbfetch(struct zink_context *ctx);
113 bool
114 zink_resource_access_is_write(VkAccessFlags flags);
115 
116 void
117 zink_resource_buffer_barrier(struct zink_context *ctx, struct zink_resource *res, VkAccessFlags flags, VkPipelineStageFlags pipeline);
118 void
119 zink_resource_buffer_barrier2(struct zink_context *ctx, struct zink_resource *res, VkAccessFlags flags, VkPipelineStageFlags pipeline);
120 void
121 zink_resource_image_barrier_init(VkImageMemoryBarrier *imb, struct zink_resource *res, VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
122 void
123 zink_resource_image_barrier2_init(VkImageMemoryBarrier2 *imb, struct zink_resource *res, VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
124 void
125 zink_resource_image_barrier(struct zink_context *ctx, struct zink_resource *res,
126                       VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
127 void
128 zink_resource_image_barrier2(struct zink_context *ctx, struct zink_resource *res, VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
129 bool
130 zink_check_unordered_transfer_access(struct zink_resource *res, unsigned level, const struct pipe_box *box);
131 bool
132 zink_check_valid_buffer_src_access(struct zink_context *ctx, struct zink_resource *res, unsigned offset, unsigned size);
133 void
134 zink_resource_image_transfer_dst_barrier(struct zink_context *ctx, struct zink_resource *res, unsigned level, const struct pipe_box *box, bool unsync);
135 bool
136 zink_resource_buffer_transfer_dst_barrier(struct zink_context *ctx, struct zink_resource *res, unsigned offset, unsigned size);
137 void
138 zink_synchronization_init(struct zink_screen *screen);
139 void
140 zink_update_descriptor_refs(struct zink_context *ctx, bool compute);
141 void
142 zink_init_vk_sample_locations(struct zink_context *ctx, VkSampleLocationsInfoEXT *loc);
143 
144 void
145 zink_batch_rp(struct zink_context *ctx);
146 
147 void
148 zink_batch_no_rp(struct zink_context *ctx);
149 void
150 zink_batch_no_rp_safe(struct zink_context *ctx);
151 
152 VkImageView
153 zink_prep_fb_attachment(struct zink_context *ctx, struct zink_surface *surf, unsigned i);
154 void
155 zink_update_vk_sample_locations(struct zink_context *ctx);
156 
157 static inline VkPipelineStageFlags
zink_pipeline_flags_from_pipe_stage(gl_shader_stage pstage)158 zink_pipeline_flags_from_pipe_stage(gl_shader_stage pstage)
159 {
160    switch (pstage) {
161    case MESA_SHADER_VERTEX:
162       return VK_PIPELINE_STAGE_VERTEX_SHADER_BIT;
163    case MESA_SHADER_FRAGMENT:
164       return VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
165    case MESA_SHADER_GEOMETRY:
166       return VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT;
167    case MESA_SHADER_TESS_CTRL:
168       return VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT;
169    case MESA_SHADER_TESS_EVAL:
170       return VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT;
171    case MESA_SHADER_COMPUTE:
172       return VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
173    default:
174       unreachable("unknown shader stage");
175    }
176 }
177 
178 void
179 zink_rebind_all_buffers(struct zink_context *ctx);
180 void
181 zink_rebind_all_images(struct zink_context *ctx);
182 
183 void
184 zink_parse_tc_info(struct zink_context *ctx);
185 void
186 zink_flush_memory_barrier(struct zink_context *ctx, bool is_compute);
187 void
188 zink_init_draw_functions(struct zink_context *ctx, struct zink_screen *screen);
189 void
190 zink_init_grid_functions(struct zink_context *ctx);
191 struct zink_context *
192 zink_tc_context_unwrap(struct pipe_context *pctx, bool threaded);
193 
194 void
195 zink_update_barriers(struct zink_context *ctx, bool is_compute,
196                      struct pipe_resource *index, struct pipe_resource *indirect, struct pipe_resource *indirect_draw_count);
197 
198 
199 bool
200 zink_cmd_debug_marker_begin(struct zink_context *ctx, VkCommandBuffer cmdbuf, const char *fmt, ...);
201 void
202 zink_cmd_debug_marker_end(struct zink_context *ctx, VkCommandBuffer cmdbuf,bool emitted);
203 void
204 zink_copy_buffer(struct zink_context *ctx, struct zink_resource *dst, struct zink_resource *src,
205                  unsigned dst_offset, unsigned src_offset, unsigned size);
206 #ifdef __cplusplus
207 }
208 #endif
209 
210 #ifndef __cplusplus
211 VkPipelineStageFlags
212 zink_pipeline_flags_from_stage(VkShaderStageFlagBits stage);
213 
214 struct pipe_context *
215 zink_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags);
216 
217 void
218 zink_context_query_init(struct pipe_context *ctx);
219 
220 void
221 zink_blit_begin(struct zink_context *ctx, enum zink_blit_flags flags);
222 void
223 zink_blit_barriers(struct zink_context *ctx, struct zink_resource *src, struct zink_resource *dst, bool whole_dst);
224 
225 void
226 zink_blit(struct pipe_context *pctx,
227           const struct pipe_blit_info *info);
228 
229 bool
230 zink_blit_region_fills(struct u_rect region, unsigned width, unsigned height);
231 
232 bool
233 zink_blit_region_covers(struct u_rect region, struct u_rect covers);
234 
235 void
236 zink_draw_rectangle(struct blitter_context *blitter, void *vertex_elements_cso,
237                     blitter_get_vs_func get_vs, int x1, int y1, int x2, int y2,
238                     float depth, unsigned num_instances, enum blitter_attrib_type type,
239                     const union blitter_attrib *attrib);
240 
241 static inline struct u_rect
zink_rect_from_box(const struct pipe_box * box)242 zink_rect_from_box(const struct pipe_box *box)
243 {
244    return (struct u_rect){box->x, box->x + box->width, box->y, box->y + box->height};
245 }
246 
247 static inline VkComponentSwizzle
zink_component_mapping(enum pipe_swizzle swizzle)248 zink_component_mapping(enum pipe_swizzle swizzle)
249 {
250    switch (swizzle) {
251    case PIPE_SWIZZLE_X: return VK_COMPONENT_SWIZZLE_R;
252    case PIPE_SWIZZLE_Y: return VK_COMPONENT_SWIZZLE_G;
253    case PIPE_SWIZZLE_Z: return VK_COMPONENT_SWIZZLE_B;
254    case PIPE_SWIZZLE_W: return VK_COMPONENT_SWIZZLE_A;
255    case PIPE_SWIZZLE_0: return VK_COMPONENT_SWIZZLE_ZERO;
256    case PIPE_SWIZZLE_1: return VK_COMPONENT_SWIZZLE_ONE;
257    default:
258       unreachable("unexpected swizzle");
259    }
260 }
261 
262 void
263 zink_update_shadow_samplerviews(struct zink_context *ctx, unsigned mask);
264 
265 enum pipe_swizzle
266 zink_clamp_void_swizzle(const struct util_format_description *desc, enum pipe_swizzle swizzle);
267 
268 bool
269 zink_resource_rebind(struct zink_context *ctx, struct zink_resource *res);
270 
271 void
272 zink_rebind_framebuffer(struct zink_context *ctx, struct zink_resource *res);
273 void
274 zink_set_null_fs(struct zink_context *ctx);
275 
276 void
277 zink_copy_image_buffer(struct zink_context *ctx, struct zink_resource *dst, struct zink_resource *src,
278                        unsigned dst_level, unsigned dstx, unsigned dsty, unsigned dstz,
279                        unsigned src_level, const struct pipe_box *src_box, enum pipe_map_flags map_flags);
280 
281 void
282 zink_destroy_buffer_view(struct zink_screen *screen, struct zink_buffer_view *buffer_view);
283 
284 struct pipe_surface *
285 zink_get_dummy_pipe_surface(struct zink_context *ctx, int samples_index);
286 struct zink_surface *
287 zink_get_dummy_surface(struct zink_context *ctx, int samples_index);
288 
289 void
290 debug_describe_zink_buffer_view(char *buf, const struct zink_buffer_view *ptr);
291 
292 static inline void
zink_buffer_view_reference(struct zink_screen * screen,struct zink_buffer_view ** dst,struct zink_buffer_view * src)293 zink_buffer_view_reference(struct zink_screen *screen,
294                            struct zink_buffer_view **dst,
295                            struct zink_buffer_view *src)
296 {
297    struct zink_buffer_view *old_dst = dst ? *dst : NULL;
298 
299    if (pipe_reference_described(old_dst ? &old_dst->reference : NULL, &src->reference,
300                                 (debug_reference_descriptor)debug_describe_zink_buffer_view))
301       zink_destroy_buffer_view(screen, old_dst);
302    if (dst) *dst = src;
303 }
304 #endif
305 
306 #endif
307