Merge branch 'mesa_7_6_branch'
[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 boolean need_edgeflags;
46
47 struct translate_cache *cache;
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 {
63 struct draw_context *draw = fetch->draw;
64 unsigned nr_inputs;
65 unsigned i, nr = 0;
66 unsigned dst_offset = 0;
67 struct translate_key key;
68
69 fetch->vertex_size = vertex_size;
70
71 /* Always emit/leave space for a vertex header.
72 *
73 * It's worth considering whether the vertex headers should contain
74 * a pointer to the 'data', rather than having it inline.
75 * Something to look at after we've fully switched over to the pt
76 * paths.
77 */
78 {
79 /* Need to set header->vertex_id = 0xffff somehow.
80 */
81 key.element[nr].input_format = PIPE_FORMAT_R32_FLOAT;
82 key.element[nr].input_buffer = draw->pt.nr_vertex_buffers;
83 key.element[nr].input_offset = 0;
84 key.element[nr].output_format = PIPE_FORMAT_R32_FLOAT;
85 key.element[nr].output_offset = dst_offset;
86 dst_offset += 1 * sizeof(float);
87 nr++;
88
89
90 /* Just leave the clip[] array untouched.
91 */
92 dst_offset += 4 * sizeof(float);
93 }
94
95 assert( draw->pt.nr_vertex_elements >= vs_input_count );
96
97 nr_inputs = MIN2( vs_input_count, draw->pt.nr_vertex_elements );
98
99 for (i = 0; i < nr_inputs; i++) {
100 key.element[nr].input_format = draw->pt.vertex_element[i].src_format;
101 key.element[nr].input_buffer = draw->pt.vertex_element[i].vertex_buffer_index;
102 key.element[nr].input_offset = draw->pt.vertex_element[i].src_offset;
103 key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
104 key.element[nr].output_offset = dst_offset;
105
106 dst_offset += 4 * sizeof(float);
107 nr++;
108 }
109
110 assert(dst_offset <= vertex_size);
111
112 key.nr_elements = nr;
113 key.output_stride = vertex_size;
114
115
116 if (!fetch->translate ||
117 translate_key_compare(&fetch->translate->key, &key) != 0)
118 {
119 translate_key_sanitize(&key);
120 fetch->translate = translate_cache_find(fetch->cache, &key);
121
122 {
123 static struct vertex_header vh = { 0, 1, 0, UNDEFINED_VERTEX_ID, { .0f, .0f, .0f, .0f } };
124 fetch->translate->set_buffer(fetch->translate,
125 draw->pt.nr_vertex_buffers,
126 &vh,
127 0);
128 }
129 }
130
131 fetch->need_edgeflags = ((draw->rasterizer->fill_cw != PIPE_POLYGON_MODE_FILL ||
132 draw->rasterizer->fill_ccw != PIPE_POLYGON_MODE_FILL) &&
133 draw->pt.user.edgeflag);
134 }
135
136
137
138
139 void draw_pt_fetch_run( struct pt_fetch *fetch,
140 const unsigned *elts,
141 unsigned count,
142 char *verts )
143 {
144 struct draw_context *draw = fetch->draw;
145 struct translate *translate = fetch->translate;
146 unsigned i;
147
148 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
149 translate->set_buffer(translate,
150 i,
151 ((char *)draw->pt.user.vbuffer[i] +
152 draw->pt.vertex_buffer[i].buffer_offset),
153 draw->pt.vertex_buffer[i].stride );
154 }
155
156 translate->run_elts( translate,
157 elts,
158 count,
159 verts );
160
161 /* Edgeflags are hard to fit into a translate program, populate
162 * them separately if required. In the setup above they are
163 * defaulted to one, so only need this if there is reason to change
164 * that default:
165 */
166 if (fetch->need_edgeflags) {
167 for (i = 0; i < count; i++) {
168 struct vertex_header *vh = (struct vertex_header *)(verts + i * fetch->vertex_size);
169 vh->edgeflag = draw_pt_get_edgeflag( draw, elts[i] );
170 }
171 }
172 }
173
174
175 void draw_pt_fetch_run_linear( struct pt_fetch *fetch,
176 unsigned start,
177 unsigned count,
178 char *verts )
179 {
180 struct draw_context *draw = fetch->draw;
181 struct translate *translate = fetch->translate;
182 unsigned i;
183
184 for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
185 translate->set_buffer(translate,
186 i,
187 ((char *)draw->pt.user.vbuffer[i] +
188 draw->pt.vertex_buffer[i].buffer_offset),
189 draw->pt.vertex_buffer[i].stride );
190 }
191
192 translate->run( translate,
193 start,
194 count,
195 verts );
196
197 /* Edgeflags are hard to fit into a translate program, populate
198 * them separately if required. In the setup above they are
199 * defaulted to one, so only need this if there is reason to change
200 * that default:
201 */
202 if (fetch->need_edgeflags) {
203 for (i = 0; i < count; i++) {
204 struct vertex_header *vh = (struct vertex_header *)(verts + i * fetch->vertex_size);
205 vh->edgeflag = draw_pt_get_edgeflag( draw, start + i );
206 }
207 }
208 }
209
210
211 struct pt_fetch *draw_pt_fetch_create( struct draw_context *draw )
212 {
213 struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
214 if (!fetch)
215 return NULL;
216
217 fetch->draw = draw;
218 fetch->cache = translate_cache_create();
219 if (!fetch->cache) {
220 FREE(fetch);
221 return NULL;
222 }
223
224 return fetch;
225 }
226
227 void draw_pt_fetch_destroy( struct pt_fetch *fetch )
228 {
229 if (fetch->cache)
230 translate_cache_destroy(fetch->cache);
231
232 FREE(fetch);
233 }
234