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