draw_pt_fetch.c revision cec016271ccf38d2f32e426f96e7d5d1fdf962f7
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
38struct 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 */
58void 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   unsigned keysize;
66
67   fetch->vertex_size = vertex_size;
68   keysize = (2*4 +
69              (draw->pt.nr_vertex_elements + 1) * sizeof(key.element[0]));
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
96   for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
97      key.element[nr].input_format = draw->pt.vertex_element[i].src_format;
98      key.element[nr].input_buffer = draw->pt.vertex_element[i].vertex_buffer_index;
99      key.element[nr].input_offset = draw->pt.vertex_element[i].src_offset;
100      key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
101      key.element[nr].output_offset = dst_offset;
102
103      dst_offset += 4 * sizeof(float);
104      nr++;
105   }
106
107   assert(dst_offset <= vertex_size);
108
109   key.nr_elements = nr;
110   key.output_stride = vertex_size;
111
112
113   if (!fetch->translate ||
114       memcmp(&fetch->translate->key, &key, keysize) != 0)
115   {
116      memset((char *)&key + keysize, 0, sizeof(key) - keysize);
117      fetch->translate = translate_cache_find(fetch->cache, &key);
118
119      {
120	 static struct vertex_header vh = { 0, 1, 0, 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
136void 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
172void 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
208struct 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
224void draw_pt_fetch_destroy( struct pt_fetch *fetch )
225{
226   translate_cache_destroy(fetch->cache);
227
228   FREE(fetch);
229}
230
231