1 /**********************************************************
2 * Copyright 2008-2022 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29 #include "util/u_upload_mgr.h"
30
31 #include "svga_context.h"
32 #include "svga_state.h"
33 #include "svga_draw.h"
34 #include "svga_tgsi.h"
35 #include "svga_screen.h"
36 #include "svga_shader.h"
37 #include "svga_resource_buffer.h"
38 #include "svga_hw_reg.h"
39
40
41
42 static enum pipe_error
emit_hw_vs_vdecl(struct svga_context * svga,uint64_t dirty)43 emit_hw_vs_vdecl(struct svga_context *svga, uint64_t dirty)
44 {
45 const struct pipe_vertex_element *ve = svga->curr.velems->velem;
46 SVGA3dVertexDecl decls[SVGA3D_INPUTREG_MAX];
47 unsigned buffer_indexes[SVGA3D_INPUTREG_MAX];
48 unsigned i;
49 unsigned neg_bias = 0;
50
51 assert(svga->curr.velems->count >=
52 svga->curr.vs->base.info.num_inputs);
53
54 /**
55 * We can't set the VDECL offset to something negative, so we
56 * must calculate a common negative additional index bias, and modify
57 * the VDECL offsets accordingly so they *all* end up positive.
58 *
59 * Note that the exact value of the negative index bias is not that
60 * important, since we compensate for it when we calculate the vertex
61 * buffer offset below. The important thing is that all vertex buffer
62 * offsets remain positive.
63 *
64 * Note that we use a negative bias variable in order to make the
65 * rounding maths more easy to follow, and to avoid int / unsigned
66 * confusion.
67 */
68
69 for (i = 0; i < svga->curr.velems->count; i++) {
70 const struct pipe_vertex_buffer *vb =
71 &svga->curr.vb[ve[i].vertex_buffer_index];
72 struct svga_buffer *buffer;
73 unsigned int offset = vb->buffer_offset + ve[i].src_offset;
74 unsigned tmp_neg_bias = 0;
75
76 if (!vb->buffer.resource)
77 continue;
78
79 buffer = svga_buffer(vb->buffer.resource);
80 if (buffer->uploaded.start > offset) {
81 tmp_neg_bias = buffer->uploaded.start - offset;
82 if (vb->stride)
83 tmp_neg_bias = (tmp_neg_bias + vb->stride - 1) / vb->stride;
84 neg_bias = MAX2(neg_bias, tmp_neg_bias);
85 }
86 }
87
88 for (i = 0; i < svga->curr.velems->count; i++) {
89 const struct pipe_vertex_buffer *vb =
90 &svga->curr.vb[ve[i].vertex_buffer_index];
91 unsigned usage, index;
92 struct svga_buffer *buffer;
93
94 if (!vb->buffer.resource)
95 continue;
96
97 buffer = svga_buffer(vb->buffer.resource);
98 svga_generate_vdecl_semantics( i, &usage, &index );
99
100 /* SVGA_NEW_VELEMENT
101 */
102 decls[i].identity.type = svga->curr.velems->decl_type[i];
103 decls[i].identity.method = SVGA3D_DECLMETHOD_DEFAULT;
104 decls[i].identity.usage = usage;
105 decls[i].identity.usageIndex = index;
106 decls[i].array.stride = vb->stride;
107
108 /* Compensate for partially uploaded vbo, and
109 * for the negative index bias.
110 */
111 decls[i].array.offset = (vb->buffer_offset
112 + ve[i].src_offset
113 + neg_bias * vb->stride
114 - buffer->uploaded.start);
115
116 assert(decls[i].array.offset >= 0);
117
118 buffer_indexes[i] = ve[i].vertex_buffer_index;
119
120 assert(!buffer->uploaded.buffer);
121 }
122
123 svga_hwtnl_vertex_decls(svga->hwtnl,
124 svga->curr.velems->count,
125 decls,
126 buffer_indexes,
127 svga->curr.velems->id);
128
129 svga_hwtnl_vertex_buffers(svga->hwtnl,
130 svga->curr.num_vertex_buffers,
131 svga->curr.vb);
132
133 svga_hwtnl_set_index_bias( svga->hwtnl, -(int) neg_bias );
134 return PIPE_OK;
135 }
136
137
138 static enum pipe_error
emit_hw_vdecl(struct svga_context * svga,uint64_t dirty)139 emit_hw_vdecl(struct svga_context *svga, uint64_t dirty)
140 {
141 /* SVGA_NEW_NEED_SWTNL
142 */
143 if (svga->state.sw.need_swtnl)
144 return PIPE_OK; /* Do not emit during swtnl */
145
146 return emit_hw_vs_vdecl( svga, dirty );
147 }
148
149
150 struct svga_tracked_state svga_hw_vdecl =
151 {
152 "hw vertex decl state (hwtnl version)",
153 ( SVGA_NEW_NEED_SWTNL |
154 SVGA_NEW_VELEMENT |
155 SVGA_NEW_VBUFFER |
156 SVGA_NEW_RAST |
157 SVGA_NEW_FS |
158 SVGA_NEW_VS ),
159 emit_hw_vdecl
160 };
161