xref: /aosp_15_r20/external/mesa3d/src/intel/vulkan/genX_cmd_draw_helpers.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2022 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 #ifndef GENX_CMD_DRAW_HELPERS_H
25 #define GENX_CMD_DRAW_HELPERS_H
26 
27 #include <assert.h>
28 #include <stdbool.h>
29 
30 #include "anv_private.h"
31 
32 #if GFX_VER < 11
33 static void
emit_vertex_bo(struct anv_cmd_buffer * cmd_buffer,struct anv_address addr,uint32_t size,uint32_t index)34 emit_vertex_bo(struct anv_cmd_buffer *cmd_buffer,
35                struct anv_address addr,
36                uint32_t size, uint32_t index)
37 {
38    uint32_t *p = anv_batch_emitn(&cmd_buffer->batch, 5,
39                                  GENX(3DSTATE_VERTEX_BUFFERS));
40 
41    GENX(VERTEX_BUFFER_STATE_pack)(&cmd_buffer->batch, p + 1,
42       &(struct GENX(VERTEX_BUFFER_STATE)) {
43          .VertexBufferIndex = index,
44          .AddressModifyEnable = true,
45          .BufferPitch = 0,
46          .MOCS = anv_mocs(cmd_buffer->device, addr.bo,
47                           ISL_SURF_USAGE_VERTEX_BUFFER_BIT),
48          .NullVertexBuffer = size == 0,
49          .BufferStartingAddress = addr,
50          .BufferSize = size
51       });
52 
53 #if GFX_VER == 9
54    genX(cmd_buffer_set_binding_for_gfx8_vb_flush)(cmd_buffer,
55                                                   index, addr, size);
56 #endif
57 }
58 
59 static void
emit_base_vertex_instance_bo(struct anv_cmd_buffer * cmd_buffer,struct anv_address addr)60 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
61                              struct anv_address addr)
62 {
63    emit_vertex_bo(cmd_buffer, addr, addr.bo ? 8 : 0, ANV_SVGS_VB_INDEX);
64 }
65 
66 static void
emit_base_vertex_instance(struct anv_cmd_buffer * cmd_buffer,uint32_t base_vertex,uint32_t base_instance)67 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
68                           uint32_t base_vertex, uint32_t base_instance)
69 {
70    if (base_vertex == 0 && base_instance == 0) {
71       emit_base_vertex_instance_bo(cmd_buffer, ANV_NULL_ADDRESS);
72       return;
73    }
74 
75    struct anv_state id_state =
76       anv_cmd_buffer_alloc_temporary_state(cmd_buffer, 8, 4);
77 
78    ((uint32_t *)id_state.map)[0] = base_vertex;
79    ((uint32_t *)id_state.map)[1] = base_instance;
80 
81    struct anv_address addr =
82       anv_cmd_buffer_temporary_state_address(cmd_buffer, id_state);
83 
84    emit_base_vertex_instance_bo(cmd_buffer, addr);
85 }
86 
87 static void
emit_draw_index(struct anv_cmd_buffer * cmd_buffer,uint32_t draw_index)88 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
89 {
90    struct anv_state state =
91       anv_cmd_buffer_alloc_temporary_state(cmd_buffer, 4, 4);
92 
93    ((uint32_t *)state.map)[0] = draw_index;
94 
95    struct anv_address addr =
96       anv_cmd_buffer_temporary_state_address(cmd_buffer, state);
97 
98    emit_vertex_bo(cmd_buffer, addr, 4, ANV_DRAWID_VB_INDEX);
99 }
100 #endif /* GFX_VER <= 11 */
101 
102 static void
update_dirty_vbs_for_gfx8_vb_flush(struct anv_cmd_buffer * cmd_buffer,uint32_t access_type)103 update_dirty_vbs_for_gfx8_vb_flush(struct anv_cmd_buffer *cmd_buffer,
104                                    uint32_t access_type)
105 {
106 #if GFX_VER == 9
107    const struct vk_dynamic_graphics_state *dyn =
108       &cmd_buffer->vk.dynamic_graphics_state;
109    struct anv_graphics_pipeline *pipeline =
110       anv_pipeline_to_graphics(cmd_buffer->state.gfx.base.pipeline);
111    const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
112 
113    uint64_t vb_used = dyn->vi->bindings_valid;
114    if (vs_prog_data->uses_firstvertex ||
115        vs_prog_data->uses_baseinstance)
116       vb_used |= 1ull << ANV_SVGS_VB_INDEX;
117    if (vs_prog_data->uses_drawid)
118       vb_used |= 1ull << ANV_DRAWID_VB_INDEX;
119 
120    genX(cmd_buffer_update_dirty_vbs_for_gfx8_vb_flush)(cmd_buffer,
121                                                        access_type,
122                                                        vb_used);
123 #endif
124 }
125 
126 #if GFX_VER < 11
127 ALWAYS_INLINE static void
cmd_buffer_emit_vertex_constants_and_flush(struct anv_cmd_buffer * cmd_buffer,const struct brw_vs_prog_data * vs_prog_data,uint32_t base_vertex,uint32_t base_instance,uint32_t draw_id,bool force_flush)128 cmd_buffer_emit_vertex_constants_and_flush(struct anv_cmd_buffer *cmd_buffer,
129                                            const struct brw_vs_prog_data *vs_prog_data,
130                                            uint32_t base_vertex,
131                                            uint32_t base_instance,
132                                            uint32_t draw_id,
133                                            bool force_flush)
134 {
135    bool emitted = false;
136    if (vs_prog_data->uses_firstvertex ||
137        vs_prog_data->uses_baseinstance) {
138       emit_base_vertex_instance(cmd_buffer, base_vertex, base_instance);
139       emitted = true;
140    }
141    if (vs_prog_data->uses_drawid) {
142       emit_draw_index(cmd_buffer, draw_id);
143       emitted = true;
144    }
145    /* Emitting draw index or vertex index BOs may result in needing
146     * additional VF cache flushes.
147     */
148    if (emitted || force_flush)
149       genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
150 }
151 #endif
152 
153 #endif /* GENX_CMD_DRAW_HELPERS_H */
154