draw_pt_fetch.c revision da8312a1cf73d0777d51c63148ee090a9acace8b
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
66   fetch->vertex_size = vertex_size;
67
68   memset(&key, 0, sizeof(key));
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
95   for (i = 0; i < draw->pt.nr_vertex_elements; i++) {
96      key.element[nr].input_format = draw->pt.vertex_element[i].src_format;
97      key.element[nr].input_buffer = draw->pt.vertex_element[i].vertex_buffer_index;
98      key.element[nr].input_offset = draw->pt.vertex_element[i].src_offset;
99      key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
100      key.element[nr].output_offset = dst_offset;
101
102      dst_offset += 4 * sizeof(float);
103      nr++;
104   }
105
106   assert(dst_offset <= vertex_size);
107
108   key.nr_elements = nr;
109   key.output_stride = vertex_size;
110
111
112   if (!fetch->translate ||
113       memcmp(&fetch->translate->key, &key, sizeof(key)) != 0)
114   {
115      fetch->translate = translate_cache_find(fetch->cache, &key);
116
117      {
118	 static struct vertex_header vh = { 0, 1, 0, 0xffff };
119	 fetch->translate->set_buffer(fetch->translate,
120				      draw->pt.nr_vertex_buffers,
121				      &vh,
122				      0);
123      }
124   }
125
126   fetch->need_edgeflags = ((draw->rasterizer->fill_cw != PIPE_POLYGON_MODE_FILL ||
127                             draw->rasterizer->fill_ccw != PIPE_POLYGON_MODE_FILL) &&
128                            draw->pt.user.edgeflag);
129}
130
131
132
133
134void draw_pt_fetch_run( struct pt_fetch *fetch,
135			const unsigned *elts,
136			unsigned count,
137			char *verts )
138{
139   struct draw_context *draw = fetch->draw;
140   struct translate *translate = fetch->translate;
141   unsigned i;
142
143   for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
144      translate->set_buffer(translate,
145			    i,
146			    ((char *)draw->pt.user.vbuffer[i] +
147			     draw->pt.vertex_buffer[i].buffer_offset),
148			    draw->pt.vertex_buffer[i].pitch );
149   }
150
151   translate->run_elts( translate,
152			elts,
153			count,
154			verts );
155
156   /* Edgeflags are hard to fit into a translate program, populate
157    * them separately if required.  In the setup above they are
158    * defaulted to one, so only need this if there is reason to change
159    * that default:
160    */
161   if (fetch->need_edgeflags) {
162      for (i = 0; i < count; i++) {
163         struct vertex_header *vh = (struct vertex_header *)(verts + i * fetch->vertex_size);
164         vh->edgeflag = draw_pt_get_edgeflag( draw, elts[i] );
165      }
166   }
167}
168
169
170struct pt_fetch *draw_pt_fetch_create( struct draw_context *draw )
171{
172   struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
173   if (!fetch)
174      return NULL;
175
176   fetch->draw = draw;
177   fetch->cache = translate_cache_create();
178   if (!fetch->cache) {
179      FREE(fetch);
180      return NULL;
181   }
182
183   return fetch;
184}
185
186void draw_pt_fetch_destroy( struct pt_fetch *fetch )
187{
188   translate_cache_destroy(fetch->cache);
189
190   FREE(fetch);
191}
192
193