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