anv: Move anv_device_check_status() code to i915/anv_device.c
[platform/upstream/mesa.git] / src / intel / vulkan / genX_cmd_draw_helpers.h
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
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 #if GFX_VER >= 12
50          .L3BypassDisable = true,
51 #endif
52          .BufferStartingAddress = addr,
53          .BufferSize = size
54       });
55
56 #if GFX_VER == 9
57    genX(cmd_buffer_set_binding_for_gfx8_vb_flush)(cmd_buffer,
58                                                   index, addr, size);
59 #endif
60 }
61
62 static void
63 emit_base_vertex_instance_bo(struct anv_cmd_buffer *cmd_buffer,
64                              struct anv_address addr)
65 {
66    emit_vertex_bo(cmd_buffer, addr, addr.bo ? 8 : 0, ANV_SVGS_VB_INDEX);
67 }
68
69 static void
70 emit_base_vertex_instance(struct anv_cmd_buffer *cmd_buffer,
71                           uint32_t base_vertex, uint32_t base_instance)
72 {
73    if (base_vertex == 0 && base_instance == 0) {
74       emit_base_vertex_instance_bo(cmd_buffer, ANV_NULL_ADDRESS);
75       return;
76    }
77
78    struct anv_state id_state =
79       anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 8, 4);
80
81    ((uint32_t *)id_state.map)[0] = base_vertex;
82    ((uint32_t *)id_state.map)[1] = base_instance;
83
84    struct anv_address addr =
85       anv_state_pool_state_address(&cmd_buffer->device->dynamic_state_pool,
86                                     id_state);
87
88    emit_base_vertex_instance_bo(cmd_buffer, addr);
89 }
90
91 static void
92 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
93 {
94    struct anv_state state =
95       anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
96
97    ((uint32_t *)state.map)[0] = draw_index;
98
99    struct anv_address addr =
100       anv_state_pool_state_address(&cmd_buffer->device->dynamic_state_pool,
101                                    state);
102
103    emit_vertex_bo(cmd_buffer, addr, 4, ANV_DRAWID_VB_INDEX);
104 }
105 #endif /* GFX_VER <= 11 */
106
107 static void
108 update_dirty_vbs_for_gfx8_vb_flush(struct anv_cmd_buffer *cmd_buffer,
109                                    uint32_t access_type)
110 {
111 #if GFX_VER == 9
112    struct anv_graphics_pipeline *pipeline = cmd_buffer->state.gfx.pipeline;
113    const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
114
115    uint64_t vb_used = pipeline->vb_used;
116    if (vs_prog_data->uses_firstvertex ||
117        vs_prog_data->uses_baseinstance)
118       vb_used |= 1ull << ANV_SVGS_VB_INDEX;
119    if (vs_prog_data->uses_drawid)
120       vb_used |= 1ull << ANV_DRAWID_VB_INDEX;
121
122    genX(cmd_buffer_update_dirty_vbs_for_gfx8_vb_flush)(cmd_buffer,
123                                                        access_type == RANDOM,
124                                                        vb_used);
125 #endif
126 }
127
128 #if GFX_VER < 11
129 ALWAYS_INLINE static void
130 cmd_buffer_emit_vertex_constants_and_flush(struct anv_cmd_buffer *cmd_buffer,
131                                            const struct brw_vs_prog_data *vs_prog_data,
132                                            uint32_t base_vertex,
133                                            uint32_t base_instance,
134                                            uint32_t draw_id,
135                                            bool force_flush)
136 {
137    bool emitted = false;
138    if (vs_prog_data->uses_firstvertex ||
139        vs_prog_data->uses_baseinstance) {
140       emit_base_vertex_instance(cmd_buffer, base_vertex, base_instance);
141       emitted = true;
142    }
143    if (vs_prog_data->uses_drawid) {
144       emit_draw_index(cmd_buffer, draw_id);
145       emitted = true;
146    }
147    /* Emitting draw index or vertex index BOs may result in needing
148     * additional VF cache flushes.
149     */
150    if (emitted || force_flush)
151       genX(cmd_buffer_apply_pipe_flushes)(cmd_buffer);
152 }
153 #endif
154
155 #endif /* GENX_CMD_DRAW_HELPERS_H */