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