1 /*
2 * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
3 * The term “Broadcom” refers to Broadcom Inc.
4 * and/or its subsidiaries.
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "draw/draw_context.h"
9 #include "draw/draw_vbuf.h"
10 #include "util/u_inlines.h"
11 #include "pipe/p_state.h"
12
13 #include "svga_context.h"
14 #include "svga_screen.h"
15 #include "svga_swtnl.h"
16 #include "svga_state.h"
17 #include "svga_swtnl_private.h"
18
19
20
21 enum pipe_error
svga_swtnl_draw_vbo(struct svga_context * svga,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draw_one)22 svga_swtnl_draw_vbo(struct svga_context *svga,
23 const struct pipe_draw_info *info,
24 unsigned drawid_offset,
25 const struct pipe_draw_indirect_info *indirect,
26 const struct pipe_draw_start_count_bias *draw_one)
27 {
28 struct pipe_transfer *vb_transfer[PIPE_MAX_ATTRIBS] = { 0 };
29 struct pipe_transfer *ib_transfer = NULL;
30 struct pipe_transfer *cb_transfer[SVGA_MAX_CONST_BUFS] = { 0 };
31 struct draw_context *draw = svga->swtnl.draw;
32 ASSERTED unsigned old_num_vertex_buffers;
33 unsigned i;
34 const void *map;
35 bool retried;
36
37 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SWTNLDRAWVBO);
38
39 assert(!svga->dirty);
40 assert(svga->state.sw.need_swtnl);
41 assert(draw);
42
43 /* Make sure that the need_swtnl flag does not go away */
44 svga->state.sw.in_swtnl_draw = true;
45
46 SVGA_RETRY_CHECK(svga, svga_update_state(svga, SVGA_STATE_SWTNL_DRAW), retried);
47 if (retried) {
48 svga->swtnl.new_vbuf = true;
49 }
50
51 /*
52 * Map vertex buffers
53 */
54 for (i = 0; i < svga->curr.num_vertex_buffers; i++) {
55 if (svga->curr.vb[i].buffer.resource) {
56 map = pipe_buffer_map(&svga->pipe,
57 svga->curr.vb[i].buffer.resource,
58 PIPE_MAP_READ |
59 PIPE_MAP_UNSYNCHRONIZED,
60 &vb_transfer[i]);
61
62 draw_set_mapped_vertex_buffer(draw, i, map, ~0);
63 }
64 }
65 old_num_vertex_buffers = svga->curr.num_vertex_buffers;
66
67 /* Map index buffer, if present */
68 map = NULL;
69 if (info->index_size) {
70 if (info->has_user_indices) {
71 map = (uint8_t *) info->index.user;
72 } else {
73 map = pipe_buffer_map(&svga->pipe, info->index.resource,
74 PIPE_MAP_READ |
75 PIPE_MAP_UNSYNCHRONIZED, &ib_transfer);
76 }
77 draw_set_indexes(draw,
78 (const uint8_t *) map,
79 info->index_size, ~0);
80 }
81
82 /* Map constant buffers */
83 for (i = 0; i < ARRAY_SIZE(svga->curr.constbufs[PIPE_SHADER_VERTEX]); ++i) {
84 if (svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer == NULL) {
85 continue;
86 }
87
88 map = pipe_buffer_map(&svga->pipe,
89 svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer,
90 PIPE_MAP_READ |
91 PIPE_MAP_UNSYNCHRONIZED,
92 &cb_transfer[i]);
93 assert(map);
94 draw_set_mapped_constant_buffer(
95 draw, PIPE_SHADER_VERTEX, i,
96 map,
97 svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer->width0);
98 }
99
100 draw_vbo(draw, info, drawid_offset, indirect, draw_one, 1,
101 svga->patch_vertices);
102
103 draw_flush(svga->swtnl.draw);
104
105 /* Ensure the draw module didn't touch this */
106 assert(old_num_vertex_buffers == svga->curr.num_vertex_buffers);
107
108 /*
109 * unmap vertex/index buffers
110 */
111 for (i = 0; i < svga->curr.num_vertex_buffers; i++) {
112 if (svga->curr.vb[i].buffer.resource) {
113 pipe_buffer_unmap(&svga->pipe, vb_transfer[i]);
114 draw_set_mapped_vertex_buffer(draw, i, NULL, 0);
115 }
116 }
117
118 if (ib_transfer) {
119 pipe_buffer_unmap(&svga->pipe, ib_transfer);
120 draw_set_indexes(draw, NULL, 0, 0);
121 }
122
123 for (i = 0; i < ARRAY_SIZE(svga->curr.constbufs[PIPE_SHADER_VERTEX]); ++i) {
124 if (svga->curr.constbufs[PIPE_SHADER_VERTEX][i].buffer) {
125 pipe_buffer_unmap(&svga->pipe, cb_transfer[i]);
126 }
127 }
128
129 /* Now safe to remove the need_swtnl flag in any update_state call */
130 svga->state.sw.in_swtnl_draw = false;
131 svga->dirty |= SVGA_NEW_NEED_PIPELINE | SVGA_NEW_NEED_SWVFETCH;
132
133 SVGA_STATS_TIME_POP(svga_sws(svga));
134 return PIPE_OK;
135 }
136
137
138 bool
svga_init_swtnl(struct svga_context * svga)139 svga_init_swtnl(struct svga_context *svga)
140 {
141 struct svga_screen *screen = svga_screen(svga->pipe.screen);
142
143 svga->swtnl.backend = svga_vbuf_render_create(svga);
144 if (!svga->swtnl.backend)
145 goto fail;
146
147 /*
148 * Create drawing context and plug our rendering stage into it.
149 */
150 svga->swtnl.draw = draw_create(&svga->pipe);
151 if (svga->swtnl.draw == NULL)
152 goto fail;
153
154
155 draw_set_rasterize_stage(svga->swtnl.draw,
156 draw_vbuf_stage(svga->swtnl.draw, svga->swtnl.backend));
157
158 draw_set_render(svga->swtnl.draw, svga->swtnl.backend);
159
160 svga->blitter = util_blitter_create(&svga->pipe);
161 if (!svga->blitter)
162 goto fail;
163
164 /* must be done before installing Draw stages */
165 util_blitter_cache_all_shaders(svga->blitter);
166
167 const nir_alu_type bool_type =
168 screen->screen.get_shader_param(&screen->screen, PIPE_SHADER_FRAGMENT,
169 PIPE_SHADER_CAP_INTEGERS) ?
170 nir_type_bool32 : nir_type_float32;
171
172 if (!screen->haveLineSmooth)
173 draw_install_aaline_stage(svga->swtnl.draw, &svga->pipe);
174
175 /* enable/disable line stipple stage depending on device caps */
176 draw_enable_line_stipple(svga->swtnl.draw, !screen->haveLineStipple);
177
178 /* always install AA point stage */
179 draw_install_aapoint_stage(svga->swtnl.draw, &svga->pipe, bool_type);
180
181 /* Set wide line threshold above device limit (so we'll never really use it)
182 */
183 draw_wide_line_threshold(svga->swtnl.draw,
184 MAX2(screen->maxLineWidth,
185 screen->maxLineWidthAA));
186
187 if (debug_get_bool_option("SVGA_SWTNL_FSE", false))
188 draw_set_driver_clipping(svga->swtnl.draw, true, true, true, false);
189
190 return true;
191
192 fail:
193 if (svga->blitter)
194 util_blitter_destroy(svga->blitter);
195
196 if (svga->swtnl.backend)
197 svga->swtnl.backend->destroy(svga->swtnl.backend);
198
199 if (svga->swtnl.draw)
200 draw_destroy(svga->swtnl.draw);
201
202 return false;
203 }
204
205
206 void
svga_destroy_swtnl(struct svga_context * svga)207 svga_destroy_swtnl(struct svga_context *svga)
208 {
209 draw_destroy(svga->swtnl.draw);
210 }
211