Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_prim_assembler.c
4565 views
1
/**************************************************************************
2
*
3
* Copyright 2013 VMware, Inc.
4
* All Rights Reserved.
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a
7
* copy of this software and associated documentation files (the
8
* "Software"), to deal in the Software without restriction, including
9
* without limitation the rights to use, copy, modify, merge, publish,
10
* distribute, sub license, and/or sell copies of the Software, and to
11
* permit persons to whom the Software is furnished to do so, subject to
12
* the following conditions:
13
*
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
16
* of the Software.
17
*
18
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
*
26
**************************************************************************/
27
28
#include "draw_prim_assembler.h"
29
30
#include "draw_fs.h"
31
#include "draw_gs.h"
32
#include "draw_tess.h"
33
#include "util/u_debug.h"
34
#include "util/u_memory.h"
35
#include "util/u_prim.h"
36
37
#include "pipe/p_defines.h"
38
39
struct draw_assembler
40
{
41
struct draw_context *draw;
42
43
struct draw_prim_info *output_prims;
44
struct draw_vertex_info *output_verts;
45
46
const struct draw_prim_info *input_prims;
47
const struct draw_vertex_info *input_verts;
48
49
boolean needs_primid;
50
int primid_slot;
51
unsigned primid;
52
53
unsigned num_prims;
54
};
55
56
57
static boolean
58
needs_primid(const struct draw_context *draw)
59
{
60
const struct draw_fragment_shader *fs = draw->fs.fragment_shader;
61
const struct draw_geometry_shader *gs = draw->gs.geometry_shader;
62
const struct draw_tess_eval_shader *tes = draw->tes.tess_eval_shader;
63
if (fs && fs->info.uses_primid) {
64
if (gs)
65
return !gs->info.uses_primid;
66
else if (tes)
67
return !tes->info.uses_primid;
68
else
69
return TRUE;
70
}
71
return FALSE;
72
}
73
74
boolean
75
draw_prim_assembler_is_required(const struct draw_context *draw,
76
const struct draw_prim_info *prim_info,
77
const struct draw_vertex_info *vert_info)
78
{
79
/* viewport index requires primitive boundaries to get correct vertex */
80
if (draw_current_shader_uses_viewport_index(draw))
81
return TRUE;
82
switch (prim_info->prim) {
83
case PIPE_PRIM_LINES_ADJACENCY:
84
case PIPE_PRIM_LINE_STRIP_ADJACENCY:
85
case PIPE_PRIM_TRIANGLES_ADJACENCY:
86
case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY:
87
return TRUE;
88
default:
89
return needs_primid(draw);
90
}
91
}
92
93
static void
94
add_prim(struct draw_assembler *asmblr, unsigned length)
95
{
96
struct draw_prim_info *output_prims = asmblr->output_prims;
97
98
output_prims->primitive_lengths = realloc(output_prims->primitive_lengths, sizeof(unsigned) * (output_prims->primitive_count + 1));
99
output_prims->primitive_lengths[output_prims->primitive_count] = length;
100
output_prims->primitive_count++;
101
}
102
103
/*
104
* Copy the vertex header along with its data from the current
105
* vertex buffer into a buffer holding vertices arranged
106
* into decomposed primitives (i.e. buffer without the
107
* adjacency vertices)
108
*/
109
static void
110
copy_verts(struct draw_assembler *asmblr,
111
unsigned *indices, unsigned num_indices)
112
{
113
unsigned i;
114
115
char *output = (char*)asmblr->output_verts->verts;
116
const char *input = (const char*)asmblr->input_verts->verts;
117
118
for (i = 0; i < num_indices; ++i) {
119
unsigned idx = indices[i];
120
unsigned output_offset =
121
asmblr->output_verts->count * asmblr->output_verts->stride;
122
unsigned input_offset = asmblr->input_verts->stride * idx;
123
memcpy(output + output_offset, input + input_offset,
124
asmblr->input_verts->vertex_size);
125
asmblr->output_verts->count += 1;
126
}
127
++asmblr->num_prims;
128
}
129
130
131
static void
132
inject_primid(struct draw_assembler *asmblr,
133
unsigned idx,
134
unsigned primid)
135
{
136
int slot = asmblr->primid_slot;
137
char *input = (char*)asmblr->input_verts->verts;
138
unsigned input_offset = asmblr->input_verts->stride * idx;
139
struct vertex_header *v = (struct vertex_header*)(input + input_offset);
140
141
/* In case the backend doesn't care about it */
142
if (slot < 0) {
143
return;
144
}
145
146
memcpy(&v->data[slot][0], &primid, sizeof(primid));
147
memcpy(&v->data[slot][1], &primid, sizeof(primid));
148
memcpy(&v->data[slot][2], &primid, sizeof(primid));
149
memcpy(&v->data[slot][3], &primid, sizeof(primid));
150
}
151
152
153
static void
154
prim_point(struct draw_assembler *asmblr,
155
unsigned idx)
156
{
157
unsigned indices[1];
158
159
if (asmblr->needs_primid) {
160
inject_primid(asmblr, idx, asmblr->primid++);
161
}
162
indices[0] = idx;
163
164
add_prim(asmblr, 1);
165
copy_verts(asmblr, indices, 1);
166
}
167
168
static void
169
prim_line(struct draw_assembler *asmblr,
170
unsigned i0, unsigned i1)
171
{
172
unsigned indices[2];
173
174
if (asmblr->needs_primid) {
175
inject_primid(asmblr, i0, asmblr->primid);
176
inject_primid(asmblr, i1, asmblr->primid++);
177
}
178
indices[0] = i0;
179
indices[1] = i1;
180
181
add_prim(asmblr, 2);
182
copy_verts(asmblr, indices, 2);
183
}
184
185
static void
186
prim_tri(struct draw_assembler *asmblr,
187
unsigned i0, unsigned i1, unsigned i2)
188
{
189
unsigned indices[3];
190
191
if (asmblr->needs_primid) {
192
inject_primid(asmblr, i0, asmblr->primid);
193
inject_primid(asmblr, i1, asmblr->primid);
194
inject_primid(asmblr, i2, asmblr->primid++);
195
}
196
indices[0] = i0;
197
indices[1] = i1;
198
indices[2] = i2;
199
200
add_prim(asmblr, 3);
201
copy_verts(asmblr, indices, 3);
202
}
203
204
void
205
draw_prim_assembler_prepare_outputs(struct draw_assembler *ia)
206
{
207
struct draw_context *draw = ia->draw;
208
if (needs_primid(draw)) {
209
ia->primid_slot = draw_alloc_extra_vertex_attrib(
210
ia->draw, TGSI_SEMANTIC_PRIMID, 0);
211
} else {
212
ia->primid_slot = -1;
213
}
214
}
215
216
217
#define FUNC assembler_run_linear
218
#define GET_ELT(idx) (start + (idx))
219
#include "draw_prim_assembler_tmp.h"
220
221
#define FUNC assembler_run_elts
222
#define LOCAL_VARS const ushort *elts = input_prims->elts;
223
#define GET_ELT(idx) (elts[start + (idx)])
224
#include "draw_prim_assembler_tmp.h"
225
226
227
228
/*
229
* Primitive assembler breaks up adjacency primitives and assembles
230
* the base primitives they represent, e.g. vertices forming
231
* PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
232
* become vertices forming PIPE_PRIM_TRIANGLES
233
* This is needed because specification says that the adjacency
234
* primitives are only visible in the geometry shader so we need
235
* to get rid of them so that the rest of the pipeline can
236
* process the inputs.
237
*/
238
void
239
draw_prim_assembler_run(struct draw_context *draw,
240
const struct draw_prim_info *input_prims,
241
const struct draw_vertex_info *input_verts,
242
struct draw_prim_info *output_prims,
243
struct draw_vertex_info *output_verts)
244
{
245
struct draw_assembler *asmblr = draw->ia;
246
unsigned start, i;
247
unsigned assembled_prim = u_reduced_prim(input_prims->prim);
248
unsigned max_primitives = u_decomposed_prims_for_vertices(
249
input_prims->prim, input_prims->count);
250
unsigned max_verts = u_vertices_per_prim(assembled_prim) * max_primitives;
251
252
asmblr->output_prims = output_prims;
253
asmblr->output_verts = output_verts;
254
asmblr->input_prims = input_prims;
255
asmblr->input_verts = input_verts;
256
asmblr->needs_primid = needs_primid(asmblr->draw);
257
asmblr->num_prims = 0;
258
259
output_prims->linear = TRUE;
260
output_prims->elts = NULL;
261
output_prims->start = 0;
262
output_prims->prim = assembled_prim;
263
output_prims->flags = 0x0;
264
output_prims->primitive_lengths = MALLOC(sizeof(unsigned));
265
output_prims->primitive_lengths[0] = 0;
266
output_prims->primitive_count = 1;
267
268
output_verts->vertex_size = input_verts->vertex_size;
269
output_verts->stride = input_verts->stride;
270
output_verts->verts = (struct vertex_header*)MALLOC(
271
input_verts->vertex_size * max_verts + DRAW_EXTRA_VERTICES_PADDING);
272
output_verts->count = 0;
273
274
275
for (start = i = 0; i < input_prims->primitive_count;
276
start += input_prims->primitive_lengths[i], i++)
277
{
278
unsigned count = input_prims->primitive_lengths[i];
279
if (input_prims->linear) {
280
assembler_run_linear(asmblr, input_prims, input_verts,
281
start, count);
282
} else {
283
assembler_run_elts(asmblr, input_prims, input_verts,
284
start, count);
285
}
286
}
287
288
output_prims->count = output_verts->count;
289
}
290
291
struct draw_assembler *
292
draw_prim_assembler_create(struct draw_context *draw)
293
{
294
struct draw_assembler *ia = CALLOC_STRUCT( draw_assembler );
295
296
ia->draw = draw;
297
298
return ia;
299
}
300
301
void
302
draw_prim_assembler_destroy(struct draw_assembler *ia)
303
{
304
FREE(ia);
305
}
306
307
308
/*
309
* Called at the very begin of the draw call with a new instance
310
* Used to reset state that should persist between primitive restart.
311
*/
312
void
313
draw_prim_assembler_new_instance(struct draw_assembler *asmblr)
314
{
315
asmblr->primid = 0;
316
}
317
318