Merge branch '7.8'
[mesa.git] / src / gallium / auxiliary / draw / draw_pt_fetch.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 "util/u_memory.h"
29 #include "util/u_math.h"
30 #include "draw/draw_context.h"
31 #include "draw/draw_private.h"
32 #include "draw/draw_vbuf.h"
33 #include "draw/draw_pt.h"
34 #include "translate/translate.h"
35 #include "translate/translate_cache.h"
36
37
38 struct pt_fetch {
39 struct draw_context *draw;
40
41 struct translate *translate;
42
43 unsigned vertex_size;
44
45 struct translate_cache *cache;
46 };
47
48
49 /* Perform the fetch from API vertex elements & vertex buffers, to a
50 * contiguous set of float[4] attributes as required for the
51 * vertex_shader->run_linear() method.
52 *
53 * This is used in all cases except pure passthrough
54 * (draw_pt_fetch_emit.c) which has its own version to translate
55 * directly to hw vertices.
56 *
57 */
58 void draw_pt_fetch_prepare( struct pt_fetch *fetch,
59 unsigned vs_input_count,
60 unsigned vertex_size,
61 unsigned instance_id_index )
62 {
63 struct draw_context *draw = fetch->draw;
64 unsigned nr_inputs;
65 unsigned i, nr = 0, ei = 0;
66 unsigned dst_offset = 0;
67 unsigned num_extra_inputs = 0;
68 struct translate_key key;
69
70 fetch->vertex_size = vertex_size;
71
72 /* Always emit/leave space for a vertex header.
73 *
74 * It's worth considering whether the vertex headers should contain
75 * a pointer to the 'data', rather than having it inline.
76 * Something to look at after we've fully switched over to the pt
77 * paths.
78 */
79 {
80 /* Need to set header->vertex_id = 0xffff somehow.
81 */
82 key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
83 key.element[nr].input_format = PIPE_FORMAT_R32_FLOAT;
84 key.element[nr].input_buffer = draw->pt.nr_vertex_buffers;
85 key.element[nr].input_offset = 0;
86 key.element[nr].instance_divisor = 0;
87 key.element[nr].output_format = PIPE_FORMAT_R32_FLOAT;
88 key.element[nr].output_offset = dst_offset;
89 dst_offset += 1 * sizeof(float);
90 nr++;
91
92
93 /* Just leave the clip[] array untouched.
94 */
95 dst_offset += 4 * sizeof(float);
96 }
97
98 if (instance_id_index != ~0) {
99 num_extra_inputs++;
100 }
101
102 assert(draw->pt.nr_vertex_elements + num_extra_inputs >= vs_input_count);
103
104 nr_inputs = MIN2(vs_input_count, draw->pt.nr_vertex_elements + num_extra_inputs);
105
106 for (i = 0; i < nr_inputs; i++) {
107 if (i == instance_id_index) {
108 key.element[nr].type = TRANSLATE_ELEMENT_INSTANCE_ID;
109 key.element[nr].input_format = PIPE_FORMAT_R32_USCALED;
110 key.element[nr].output_format = PIPE_FORMAT_R32_USCALED;
111 key.element[nr].output_offset = dst_offset;
112
113 dst_offset += sizeof(uint);
114 } else {
115 key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
116 key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;
117 key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;
118 key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;
119 key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;
120 key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
121 key.element[nr].output_offset = dst_offset;
122
123 ei++;
124 dst_offset += 4 * sizeof(float);
125 }
126
127 nr++;
128 }
129
130 assert(dst_offset <= vertex_size);
131
132 key.nr_elements = nr;
133 key.output_stride = vertex_size;
134
135
136 if (!fetch->translate ||
137 translate_key_compare(&fetch->translate->key, &key) != 0)
138 {
139 translate_key_sanitize(&key);
140 fetch->translate = translate_cache_find(fetch->cache, &key);
141
142 {
143 static struct vertex_header vh = { 0,
144 1,
145 0,
146 UNDEFINED_VERTEX_ID,
147 { .0f, .0f, .0f, .0f } };
148
149 fetch->translate->set_buffer(fetch->translate,
150 draw->pt.nr_vertex_buffers,
151 &vh,
152 0,
153 ~0);
154 }
155 }
156
157 }
158
159
160
161
162 void draw_pt_fetch_run( struct pt_fetch *fetch,
163 const unsigned *elts,
164 unsigned count,
165 char *verts )
166 {
167 struct draw_context *draw = fetch->draw;
168 struct translate *translate = fetch->translate;
169 unsigned i;
170
171 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
172 translate->set_buffer(translate,
173 i,
174 ((char *)draw->pt.user.vbuffer[i] +
175 draw->pt.vertex_buffer[i].buffer_offset),
176 draw->pt.vertex_buffer[i].stride,
177 draw->pt.vertex_buffer[i].max_index);
178 }
179
180 translate->run_elts( translate,
181 elts,
182 count,
183 draw->instance_id,
184 verts );
185
186 }
187
188
189 void draw_pt_fetch_run_linear( struct pt_fetch *fetch,
190 unsigned start,
191 unsigned count,
192 char *verts )
193 {
194 struct draw_context *draw = fetch->draw;
195 struct translate *translate = fetch->translate;
196 unsigned i;
197
198 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
199 translate->set_buffer(translate,
200 i,
201 ((char *)draw->pt.user.vbuffer[i] +
202 draw->pt.vertex_buffer[i].buffer_offset),
203 draw->pt.vertex_buffer[i].stride,
204 draw->pt.vertex_buffer[i].max_index);
205 }
206
207 translate->run( translate,
208 start,
209 count,
210 draw->instance_id,
211 verts );
212 }
213
214
215 struct pt_fetch *draw_pt_fetch_create( struct draw_context *draw )
216 {
217 struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
218 if (!fetch)
219 return NULL;
220
221 fetch->draw = draw;
222 fetch->cache = translate_cache_create();
223 if (!fetch->cache) {
224 FREE(fetch);
225 return NULL;
226 }
227
228 return fetch;
229 }
230
231 void draw_pt_fetch_destroy( struct pt_fetch *fetch )
232 {
233 if (fetch->cache)
234 translate_cache_destroy(fetch->cache);
235
236 FREE(fetch);
237 }
238