• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
58 static inline struct zink_resource *
zink_descriptor_surface_resource(struct zink_descriptor_surface * ds)59 zink_descriptor_surface_resource(struct zink_descriptor_surface *ds)
60 {
61    return ds->is_buffer ?
62           zink_descriptor_mode == ZINK_DESCRIPTOR_MODE_DB ? zink_resource(ds->db.pres) : zink_resource(ds->bufferview->pres) :
63           (struct zink_resource*)ds->surface->base.texture;
64 }
65 
66 static inline bool
zink_fb_clear_enabled(const struct zink_context * ctx,unsigned idx)67 zink_fb_clear_enabled(const struct zink_context *ctx, unsigned idx)
68 {
69    if (idx == PIPE_MAX_COLOR_BUFS)
70       return ctx->clears_enabled & PIPE_CLEAR_DEPTHSTENCIL;
71    return ctx->clears_enabled & (PIPE_CLEAR_COLOR0 << idx);
72 }
73 
74 static inline uint32_t
zink_program_cache_stages(uint32_t stages_present)75 zink_program_cache_stages(uint32_t stages_present)
76 {
77    return (stages_present & ((1 << MESA_SHADER_TESS_CTRL) |
78                              (1 << MESA_SHADER_TESS_EVAL) |
79                              (1 << MESA_SHADER_GEOMETRY))) >> 1;
80 }
81 
82 static ALWAYS_INLINE bool
zink_is_zsbuf_used(const struct zink_context * ctx)83 zink_is_zsbuf_used(const struct zink_context *ctx)
84 {
85    return ctx->blitting || tc_renderpass_info_is_zsbuf_used(&ctx->dynamic_fb.tc_info);
86 }
87 
88 static ALWAYS_INLINE bool
zink_is_zsbuf_write(const struct zink_context * ctx)89 zink_is_zsbuf_write(const struct zink_context *ctx)
90 {
91    if (!zink_is_zsbuf_used(ctx))
92       return false;
93    return ctx->dynamic_fb.tc_info.zsbuf_write_fs || ctx->dynamic_fb.tc_info.zsbuf_write_dsa ||
94           ctx->dynamic_fb.tc_info.zsbuf_clear || ctx->dynamic_fb.tc_info.zsbuf_clear_partial;
95 }
96 
97 void
98 zink_fence_wait(struct pipe_context *ctx);
99 
100 void
101 zink_wait_on_batch(struct zink_context *ctx, uint64_t batch_id);
102 void
103 zink_reset_ds3_states(struct zink_context *ctx);
104 bool
105 zink_check_batch_completion(struct zink_context *ctx, uint64_t batch_id);
106 VkCommandBuffer
107 zink_get_cmdbuf(struct zink_context *ctx, struct zink_resource *src, struct zink_resource *dst);
108 unsigned
109 zink_update_rendering_info(struct zink_context *ctx);
110 void
111 zink_flush_queue(struct zink_context *ctx);
112 bool
113 zink_update_fbfetch(struct zink_context *ctx);
114 bool
115 zink_resource_access_is_write(VkAccessFlags flags);
116 
117 void
118 zink_resource_buffer_barrier(struct zink_context *ctx, struct zink_resource *res, VkAccessFlags flags, VkPipelineStageFlags pipeline);
119 void
120 zink_resource_buffer_barrier2(struct zink_context *ctx, struct zink_resource *res, VkAccessFlags flags, VkPipelineStageFlags pipeline);
121 void
122 zink_resource_image_barrier_init(VkImageMemoryBarrier *imb, struct zink_resource *res, VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
123 void
124 zink_resource_image_barrier2_init(VkImageMemoryBarrier2 *imb, struct zink_resource *res, VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
125 void
126 zink_resource_image_barrier(struct zink_context *ctx, struct zink_resource *res,
127                       VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
128 void
129 zink_resource_image_barrier2(struct zink_context *ctx, struct zink_resource *res, VkImageLayout new_layout, VkAccessFlags flags, VkPipelineStageFlags pipeline);
130 bool
131 zink_check_unordered_transfer_access(struct zink_resource *res, unsigned level, const struct pipe_box *box);
132 bool
133 zink_check_valid_buffer_src_access(struct zink_context *ctx, struct zink_resource *res, unsigned offset, unsigned size);
134 void
135 zink_resource_image_transfer_dst_barrier(struct zink_context *ctx, struct zink_resource *res, unsigned level, const struct pipe_box *box, bool unsync);
136 bool
137 zink_resource_buffer_transfer_dst_barrier(struct zink_context *ctx, struct zink_resource *res, unsigned offset, unsigned size);
138 void
139 zink_synchronization_init(struct zink_screen *screen);
140 void
141 zink_update_descriptor_refs(struct zink_context *ctx, bool compute);
142 void
143 zink_init_vk_sample_locations(struct zink_context *ctx, VkSampleLocationsInfoEXT *loc);
144 
145 void
146 zink_batch_rp(struct zink_context *ctx);
147 
148 void
149 zink_batch_no_rp(struct zink_context *ctx);
150 void
151 zink_batch_no_rp_safe(struct zink_context *ctx);
152 
153 VkImageView
154 zink_prep_fb_attachment(struct zink_context *ctx, struct zink_surface *surf, unsigned i);
155 void
156 zink_update_vk_sample_locations(struct zink_context *ctx);
157 
158 static inline VkPipelineStageFlags
zink_pipeline_flags_from_pipe_stage(gl_shader_stage pstage)159 zink_pipeline_flags_from_pipe_stage(gl_shader_stage pstage)
160 {
161    switch (pstage) {
162    case MESA_SHADER_VERTEX:
163       return VK_PIPELINE_STAGE_VERTEX_SHADER_BIT;
164    case MESA_SHADER_FRAGMENT:
165       return VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
166    case MESA_SHADER_GEOMETRY:
167       return VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT;
168    case MESA_SHADER_TESS_CTRL:
169       return VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT;
170    case MESA_SHADER_TESS_EVAL:
171       return VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT;
172    case MESA_SHADER_COMPUTE:
173       return VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
174    default:
175       unreachable("unknown shader stage");
176    }
177 }
178 
179 void
180 zink_rebind_all_buffers(struct zink_context *ctx);
181 void
182 zink_rebind_all_images(struct zink_context *ctx);
183 
184 void
185 zink_parse_tc_info(struct zink_context *ctx);
186 void
187 zink_flush_memory_barrier(struct zink_context *ctx, bool is_compute);
188 void
189 zink_init_draw_functions(struct zink_context *ctx, struct zink_screen *screen);
190 void
191 zink_init_grid_functions(struct zink_context *ctx);
192 struct zink_context *
193 zink_tc_context_unwrap(struct pipe_context *pctx, bool threaded);
194 
195 void
196 zink_update_barriers(struct zink_context *ctx, bool is_compute,
197                      struct pipe_resource *index, struct pipe_resource *indirect, struct pipe_resource *indirect_draw_count);
198 
199 
200 bool
201 zink_cmd_debug_marker_begin(struct zink_context *ctx, VkCommandBuffer cmdbuf, const char *fmt, ...);
202 void
203 zink_cmd_debug_marker_end(struct zink_context *ctx, VkCommandBuffer cmdbuf,bool emitted);
204 void
205 zink_copy_buffer(struct zink_context *ctx, struct zink_resource *dst, struct zink_resource *src,
206                  unsigned dst_offset, unsigned src_offset, unsigned size);
207 
208 VkIndirectCommandsLayoutTokenNV *
209 zink_dgc_add_token(struct zink_context *ctx, VkIndirectCommandsTokenTypeNV type, void **mem);
210 void
211 zink_flush_dgc(struct zink_context *ctx);
212 
213 static ALWAYS_INLINE void
zink_flush_dgc_if_enabled(struct zink_context * ctx)214 zink_flush_dgc_if_enabled(struct zink_context *ctx)
215 {
216    if (unlikely(zink_debug & ZINK_DEBUG_DGC))
217       zink_flush_dgc(ctx);
218 }
219 
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 #ifndef __cplusplus
225 VkPipelineStageFlags
226 zink_pipeline_flags_from_stage(VkShaderStageFlagBits stage);
227 
228 struct pipe_context *
229 zink_context_create(struct pipe_screen *pscreen, void *priv, unsigned flags);
230 
231 void
232 zink_context_query_init(struct pipe_context *ctx);
233 
234 void
235 zink_blit_begin(struct zink_context *ctx, enum zink_blit_flags flags);
236 void
237 zink_blit_barriers(struct zink_context *ctx, struct zink_resource *src, struct zink_resource *dst, bool whole_dst);
238 
239 void
240 zink_blit(struct pipe_context *pctx,
241           const struct pipe_blit_info *info);
242 
243 bool
244 zink_blit_region_fills(struct u_rect region, unsigned width, unsigned height);
245 
246 bool
247 zink_blit_region_covers(struct u_rect region, struct u_rect covers);
248 
249 static inline struct u_rect
zink_rect_from_box(const struct pipe_box * box)250 zink_rect_from_box(const struct pipe_box *box)
251 {
252    return (struct u_rect){box->x, box->x + box->width, box->y, box->y + box->height};
253 }
254 
255 static inline VkComponentSwizzle
zink_component_mapping(enum pipe_swizzle swizzle)256 zink_component_mapping(enum pipe_swizzle swizzle)
257 {
258    switch (swizzle) {
259    case PIPE_SWIZZLE_X: return VK_COMPONENT_SWIZZLE_R;
260    case PIPE_SWIZZLE_Y: return VK_COMPONENT_SWIZZLE_G;
261    case PIPE_SWIZZLE_Z: return VK_COMPONENT_SWIZZLE_B;
262    case PIPE_SWIZZLE_W: return VK_COMPONENT_SWIZZLE_A;
263    case PIPE_SWIZZLE_0: return VK_COMPONENT_SWIZZLE_ZERO;
264    case PIPE_SWIZZLE_1: return VK_COMPONENT_SWIZZLE_ONE;
265    default:
266       unreachable("unexpected swizzle");
267    }
268 }
269 
270 void
271 zink_update_shadow_samplerviews(struct zink_context *ctx, unsigned mask);
272 
273 enum pipe_swizzle
274 zink_clamp_void_swizzle(const struct util_format_description *desc, enum pipe_swizzle swizzle);
275 
276 bool
277 zink_resource_rebind(struct zink_context *ctx, struct zink_resource *res);
278 
279 void
280 zink_rebind_framebuffer(struct zink_context *ctx, struct zink_resource *res);
281 void
282 zink_set_null_fs(struct zink_context *ctx);
283 
284 void
285 zink_copy_image_buffer(struct zink_context *ctx, struct zink_resource *dst, struct zink_resource *src,
286                        unsigned dst_level, unsigned dstx, unsigned dsty, unsigned dstz,
287                        unsigned src_level, const struct pipe_box *src_box, enum pipe_map_flags map_flags);
288 
289 void
290 zink_destroy_buffer_view(struct zink_screen *screen, struct zink_buffer_view *buffer_view);
291 
292 struct pipe_surface *
293 zink_get_dummy_pipe_surface(struct zink_context *ctx, int samples_index);
294 struct zink_surface *
295 zink_get_dummy_surface(struct zink_context *ctx, int samples_index);
296 
297 void
298 debug_describe_zink_buffer_view(char *buf, const struct zink_buffer_view *ptr);
299 
300 static inline void
zink_buffer_view_reference(struct zink_screen * screen,struct zink_buffer_view ** dst,struct zink_buffer_view * src)301 zink_buffer_view_reference(struct zink_screen *screen,
302                            struct zink_buffer_view **dst,
303                            struct zink_buffer_view *src)
304 {
305    struct zink_buffer_view *old_dst = dst ? *dst : NULL;
306 
307    if (pipe_reference_described(old_dst ? &old_dst->reference : NULL, &src->reference,
308                                 (debug_reference_descriptor)debug_describe_zink_buffer_view))
309       zink_destroy_buffer_view(screen, old_dst);
310    if (dst) *dst = src;
311 }
312 #endif
313 
314 #endif
315