• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_dynamic_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_state_pool_state_address(&cmd_buffer->device->dynamic_state_pool,
83                                     id_state);
84 
85    emit_base_vertex_instance_bo(cmd_buffer, addr);
86 }
87 
88 static void
emit_draw_index(struct anv_cmd_buffer * cmd_buffer,uint32_t draw_index)89 emit_draw_index(struct anv_cmd_buffer *cmd_buffer, uint32_t draw_index)
90 {
91    struct anv_state state =
92       anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, 4, 4);
93 
94    ((uint32_t *)state.map)[0] = draw_index;
95 
96    struct anv_address addr =
97       anv_state_pool_state_address(&cmd_buffer->device->dynamic_state_pool,
98                                    state);
99 
100    emit_vertex_bo(cmd_buffer, addr, 4, ANV_DRAWID_VB_INDEX);
101 }
102 #endif /* GFX_VER <= 11 */
103 
104 static void
update_dirty_vbs_for_gfx8_vb_flush(struct anv_cmd_buffer * cmd_buffer,uint32_t access_type)105 update_dirty_vbs_for_gfx8_vb_flush(struct anv_cmd_buffer *cmd_buffer,
106                                    uint32_t access_type)
107 {
108 #if GFX_VER == 9
109    const struct vk_dynamic_graphics_state *dyn =
110       &cmd_buffer->vk.dynamic_graphics_state;
111    struct anv_graphics_pipeline *pipeline =
112       anv_pipeline_to_graphics(cmd_buffer->state.gfx.base.pipeline);
113    const struct brw_vs_prog_data *vs_prog_data = get_vs_prog_data(pipeline);
114 
115    uint64_t vb_used = dyn->vi->bindings_valid;
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,
124                                                        vb_used);
125 #endif
126 }
127 
128 #if GFX_VER < 11
129 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)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 */
156