draw: don't translate non-floats to float.
[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_pt.h"
33 #include "translate/translate.h"
34 #include "translate/translate_cache.h"
35
36
37 struct pt_fetch {
38 struct draw_context *draw;
39
40 struct translate *translate;
41
42 unsigned vertex_size;
43
44 struct translate_cache *cache;
45 };
46
47
48 /* Perform the fetch from API vertex elements & vertex buffers, to a
49 * contiguous set of float[4] attributes as required for the
50 * vertex_shader->run_linear() method.
51 *
52 * This is used in all cases except pure passthrough
53 * (draw_pt_fetch_emit.c) which has its own version to translate
54 * directly to hw vertices.
55 *
56 */
57 void draw_pt_fetch_prepare( struct pt_fetch *fetch,
58 unsigned vs_input_count,
59 unsigned vertex_size,
60 unsigned instance_id_index )
61 {
62 struct draw_context *draw = fetch->draw;
63 unsigned nr_inputs;
64 unsigned i, nr = 0, ei = 0;
65 unsigned dst_offset = 0;
66 unsigned num_extra_inputs = 0;
67 struct translate_key key;
68
69 fetch->vertex_size = vertex_size;
70
71 /* Leave the clipmask/edgeflags/pad/vertex_id untouched
72 */
73 dst_offset += 1 * sizeof(float);
74 /* Just leave the clip[] array untouched.
75 */
76 dst_offset += 4 * sizeof(float);
77
78 if (instance_id_index != ~0) {
79 num_extra_inputs++;
80 }
81
82 assert(draw->pt.nr_vertex_elements + num_extra_inputs >= vs_input_count);
83
84 nr_inputs = MIN2(vs_input_count, draw->pt.nr_vertex_elements + num_extra_inputs);
85
86 for (i = 0; i < nr_inputs; i++) {
87 if (i == instance_id_index) {
88 key.element[nr].type = TRANSLATE_ELEMENT_INSTANCE_ID;
89 key.element[nr].input_format = PIPE_FORMAT_R32_USCALED;
90 key.element[nr].output_format = PIPE_FORMAT_R32_USCALED;
91 key.element[nr].output_offset = dst_offset;
92
93 dst_offset += sizeof(uint);
94 } else if (util_format_is_pure_sint(draw->pt.vertex_element[i].src_format)) {
95 key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
96 key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;
97 key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;
98 key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;
99 key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;
100 key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_SINT;
101 key.element[nr].output_offset = dst_offset;
102
103 ei++;
104 dst_offset += 4 * sizeof(int);
105 } else if (util_format_is_pure_uint(draw->pt.vertex_element[i].src_format)) {
106 key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
107 key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;
108 key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;
109 key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;
110 key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;
111 key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_UINT;
112 key.element[nr].output_offset = dst_offset;
113
114 ei++;
115 dst_offset += 4 * sizeof(unsigned);
116 } else {
117 key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
118 key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;
119 key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;
120 key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;
121 key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;
122 key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
123 key.element[nr].output_offset = dst_offset;
124
125 ei++;
126 dst_offset += 4 * sizeof(float);
127 }
128
129 nr++;
130 }
131
132 assert(dst_offset <= vertex_size);
133
134 key.nr_elements = nr;
135 key.output_stride = vertex_size;
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 }
145
146
147
148
149 void draw_pt_fetch_run( struct pt_fetch *fetch,
150 const unsigned *elts,
151 unsigned count,
152 char *verts )
153 {
154 struct draw_context *draw = fetch->draw;
155 struct translate *translate = fetch->translate;
156 unsigned i;
157
158 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
159 translate->set_buffer(translate,
160 i,
161 ((char *)draw->pt.user.vbuffer[i] +
162 draw->pt.vertex_buffer[i].buffer_offset),
163 draw->pt.vertex_buffer[i].stride,
164 draw->pt.max_index);
165 }
166
167 translate->run_elts( translate,
168 elts,
169 count,
170 draw->instance_id,
171 verts );
172
173 }
174
175
176 void draw_pt_fetch_run_linear( struct pt_fetch *fetch,
177 unsigned start,
178 unsigned count,
179 char *verts )
180 {
181 struct draw_context *draw = fetch->draw;
182 struct translate *translate = fetch->translate;
183 unsigned i;
184
185 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
186 translate->set_buffer(translate,
187 i,
188 ((char *)draw->pt.user.vbuffer[i] +
189 draw->pt.vertex_buffer[i].buffer_offset),
190 draw->pt.vertex_buffer[i].stride,
191 draw->pt.user.max_index + draw->pt.user.eltBias);
192 }
193
194 translate->run( translate,
195 start,
196 count,
197 draw->instance_id,
198 verts );
199 }
200
201
202 struct pt_fetch *draw_pt_fetch_create( struct draw_context *draw )
203 {
204 struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
205 if (!fetch)
206 return NULL;
207
208 fetch->draw = draw;
209 fetch->cache = translate_cache_create();
210 if (!fetch->cache) {
211 FREE(fetch);
212 return NULL;
213 }
214
215 return fetch;
216 }
217
218 void draw_pt_fetch_destroy( struct pt_fetch *fetch )
219 {
220 if (fetch->cache)
221 translate_cache_destroy(fetch->cache);
222
223 FREE(fetch);
224 }
225