~mmach/netext73/mesa-ryzen

« back to all changes in this revision

Viewing changes to src/gallium/auxiliary/draw/draw_mesh_prim.c

  • Committer: mmach
  • Date: 2023-11-02 21:31:35 UTC
  • Revision ID: netbit73@gmail.com-20231102213135-18d4tzh7tj0uz752
2023-11-02 22:11:57

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
 *
 
3
 * Copyright 2023 Red Hat.
 
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 "Software"),
 
8
 * to deal in the Software without restriction, including without limitation
 
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
10
 * and/or sell copies of the Software, and to permit persons to whom the
 
11
 * Software is furnished to do so, subject to the following conditions:
 
12
 *
 
13
 * The above copyright notice and this permission notice shall be included
 
14
 * in all copies or substantial portions of the Software.
 
15
 *
 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
22
 * DEALINGS IN THE SOFTWARE.
 
23
 *
 
24
 **************************************************************************/
 
25
 
 
26
/*
 
27
 * Mesh primitive assembler
 
28
 *
 
29
 * This takes vertex and per-primitive data, and assembles a linear set
 
30
 * of draw compatible vertices.
 
31
 */
 
32
#include "draw_mesh_prim.h"
 
33
 
 
34
#include "draw/draw_context.h"
 
35
#include "draw/draw_private.h"
 
36
#include "util/u_debug.h"
 
37
#include "util/u_memory.h"
 
38
#include "util/u_prim.h"
 
39
 
 
40
#include "pipe/p_defines.h"
 
41
 
 
42
struct draw_mesh_prim
 
43
{
 
44
   struct draw_context *draw;
 
45
 
 
46
   struct draw_prim_info *output_prims;
 
47
   struct draw_vertex_info *output_verts;
 
48
 
 
49
   const struct draw_prim_info *input_prims;
 
50
   const struct draw_vertex_info *input_verts;
 
51
 
 
52
   unsigned num_prims;
 
53
   const char *per_prim;
 
54
   uint32_t num_per_prim;
 
55
   uint32_t added_prim_size;
 
56
   int cull_prim_idx;
 
57
};
 
58
 
 
59
static void
 
60
add_prim(struct draw_mesh_prim *asmblr, unsigned length)
 
61
{
 
62
   struct draw_prim_info *output_prims = asmblr->output_prims;
 
63
 
 
64
   output_prims->primitive_lengths = realloc(output_prims->primitive_lengths, sizeof(unsigned) * (output_prims->primitive_count + 1));
 
65
   output_prims->primitive_lengths[output_prims->primitive_count] = length;
 
66
   output_prims->primitive_count++;
 
67
}
 
68
 
 
69
 
 
70
/*
 
71
 * Copy the vertex header along with its data from the current
 
72
 * vertex buffer into a buffer holding vertices arranged
 
73
 * into decomposed primitives (i.e. buffer without the
 
74
 * adjacency vertices)
 
75
 */
 
76
static void
 
77
copy_verts(struct draw_mesh_prim *asmblr,
 
78
           unsigned *indices, unsigned num_indices)
 
79
{
 
80
   char *output = (char*)asmblr->output_verts->verts;
 
81
   const char *input = (const char*)asmblr->input_verts->verts;
 
82
 
 
83
   for (unsigned i = 0; i < num_indices; ++i) {
 
84
      unsigned idx = indices[i];
 
85
      unsigned output_offset =
 
86
         asmblr->output_verts->count * asmblr->output_verts->stride;
 
87
      unsigned input_offset = asmblr->input_verts->stride * idx;
 
88
      memcpy(output + output_offset, input + input_offset,
 
89
             asmblr->input_verts->vertex_size);
 
90
 
 
91
      memcpy(output + output_offset + asmblr->input_verts->vertex_size,
 
92
             asmblr->per_prim + (asmblr->num_prims * asmblr->added_prim_size * 8),
 
93
             asmblr->added_prim_size);
 
94
      asmblr->output_verts->count += 1;
 
95
   }
 
96
   ++asmblr->num_prims;
 
97
}
 
98
 
 
99
static bool
 
100
cull_prim(struct draw_mesh_prim *asmblr)
 
101
{
 
102
   if (asmblr->cull_prim_idx == -1)
 
103
      return false;
 
104
 
 
105
   const uint32_t *cull_prim_ptr = (uint32_t *)(asmblr->per_prim + (asmblr->num_prims * asmblr->added_prim_size * 8));
 
106
   cull_prim_ptr += (asmblr->cull_prim_idx * 4);
 
107
 
 
108
   return (*cull_prim_ptr) ? true : false;
 
109
}
 
110
 
 
111
 
 
112
static void
 
113
prim_point(struct draw_mesh_prim *asmblr,
 
114
           unsigned idx)
 
115
{
 
116
   unsigned indices[1];
 
117
 
 
118
   indices[0] = idx;
 
119
 
 
120
   if (cull_prim(asmblr)) {
 
121
      ++asmblr->num_prims;
 
122
      return;
 
123
   }
 
124
   add_prim(asmblr, 1);
 
125
   copy_verts(asmblr, indices, 1);
 
126
}
 
127
 
 
128
 
 
129
static void
 
130
prim_line(struct draw_mesh_prim *asmblr,
 
131
          unsigned i0, unsigned i1)
 
132
{
 
133
   unsigned indices[2];
 
134
 
 
135
   indices[0] = i0;
 
136
   indices[1] = i1;
 
137
 
 
138
   if (cull_prim(asmblr)) {
 
139
      ++asmblr->num_prims;
 
140
      return;
 
141
   }
 
142
   add_prim(asmblr, 2);
 
143
   copy_verts(asmblr, indices, 2);
 
144
}
 
145
 
 
146
 
 
147
static void
 
148
prim_tri(struct draw_mesh_prim *asmblr,
 
149
         unsigned i0, unsigned i1, unsigned i2)
 
150
{
 
151
   unsigned indices[3];
 
152
 
 
153
   indices[0] = i0;
 
154
   indices[1] = i1;
 
155
   indices[2] = i2;
 
156
 
 
157
   if (cull_prim(asmblr)) {
 
158
      ++asmblr->num_prims;
 
159
      return;
 
160
   }
 
161
   add_prim(asmblr, 3);
 
162
   copy_verts(asmblr, indices, 3);
 
163
}
 
164
 
 
165
 
 
166
#define FUNC assembler_run_linear
 
167
#define GET_ELT(idx) (start + (idx))
 
168
#include "draw_mesh_prim_tmp.h"
 
169
 
 
170
#define FUNC assembler_run_elts
 
171
#define LOCAL_VARS   const uint16_t *elts = input_prims->elts;
 
172
#define GET_ELT(idx) (elts[start + (idx)])
 
173
#include "draw_mesh_prim_tmp.h"
 
174
 
 
175
 
 
176
void
 
177
draw_mesh_prim_run(struct draw_context *draw,
 
178
                   unsigned num_per_prim_inputs,
 
179
                   void *per_prim_inputs,
 
180
                   int cull_prim_idx,
 
181
                   const struct draw_prim_info *input_prims,
 
182
                   const struct draw_vertex_info *input_verts,
 
183
                   struct draw_prim_info *output_prims,
 
184
                   struct draw_vertex_info *output_verts)
 
185
{
 
186
   struct draw_mesh_prim asmblr_mesh;
 
187
   struct draw_mesh_prim *asmblr = &asmblr_mesh;
 
188
   unsigned start, i;
 
189
   unsigned max_primitives = input_prims->primitive_count;
 
190
   unsigned max_verts = u_vertices_per_prim(input_prims->prim) * max_primitives;
 
191
 
 
192
   asmblr->output_prims = output_prims;
 
193
   asmblr->output_verts = output_verts;
 
194
   asmblr->input_prims = input_prims;
 
195
   asmblr->input_verts = input_verts;
 
196
   asmblr->num_prims = 0;
 
197
   asmblr->num_per_prim = num_per_prim_inputs;
 
198
   asmblr->per_prim = per_prim_inputs;
 
199
   asmblr->cull_prim_idx = cull_prim_idx;
 
200
 
 
201
   output_prims->linear = true;
 
202
   output_prims->elts = NULL;
 
203
   output_prims->start = 0;
 
204
   output_prims->prim = input_prims->prim;
 
205
   output_prims->flags = 0x0;
 
206
   output_prims->primitive_lengths = MALLOC(sizeof(unsigned));
 
207
   output_prims->primitive_lengths[0] = 0;
 
208
   output_prims->primitive_count = 0;
 
209
 
 
210
   asmblr->added_prim_size = asmblr->num_per_prim * (4 * sizeof(float));
 
211
   output_verts->vertex_size = input_verts->vertex_size + asmblr->added_prim_size;
 
212
   output_verts->stride = output_verts->vertex_size;
 
213
   output_verts->verts = (struct vertex_header*)MALLOC(
 
214
      output_verts->vertex_size * max_verts);
 
215
   output_verts->count = 0;
 
216
 
 
217
   for (start = i = 0; i < input_prims->primitive_count;
 
218
        start += input_prims->primitive_lengths[i], i++) {
 
219
      unsigned count = input_prims->primitive_lengths[i];
 
220
      if (input_prims->linear) {
 
221
         assembler_run_linear(asmblr, input_prims, input_verts,
 
222
                              start, count);
 
223
      } else {
 
224
         assembler_run_elts(asmblr, input_prims, input_verts,
 
225
                            start, count);
 
226
      }
 
227
   }
 
228
 
 
229
   output_prims->count = output_verts->count;
 
230
}