1/**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 "util/format/u_format.h"
31#include "draw/draw_context.h"
32#include "draw/draw_private.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
45   struct translate_cache *cache;
46};
47
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 */
54void
55draw_pt_fetch_prepare(struct pt_fetch *fetch,
56                      unsigned vs_input_count,
57                      unsigned vertex_size,
58                      unsigned instance_id_index)
59{
60   struct draw_context *draw = fetch->draw;
61   unsigned nr_inputs;
62   unsigned i, nr = 0, ei = 0;
63   unsigned dst_offset = 0;
64   unsigned num_extra_inputs = 0;
65   struct translate_key key;
66
67   fetch->vertex_size = vertex_size;
68
69   /* Leave the clipmask/edgeflags/pad/vertex_id,
70    * clip[] and whatever else in the header untouched.
71    */
72   dst_offset = offsetof(struct vertex_header, data);
73
74   if (instance_id_index != ~0) {
75      num_extra_inputs++;
76   }
77
78   assert(draw->pt.nr_vertex_elements + num_extra_inputs >= vs_input_count);
79
80   nr_inputs = MIN2(vs_input_count, draw->pt.nr_vertex_elements + num_extra_inputs);
81
82   for (i = 0; i < nr_inputs; i++) {
83      if (i == instance_id_index) {
84         key.element[nr].type = TRANSLATE_ELEMENT_INSTANCE_ID;
85         key.element[nr].input_format = PIPE_FORMAT_R32_USCALED;
86         key.element[nr].output_format = PIPE_FORMAT_R32_USCALED;
87         key.element[nr].output_offset = dst_offset;
88
89         dst_offset += sizeof(uint);
90      } else if (util_format_is_pure_sint(draw->pt.vertex_element[i].src_format)) {
91         key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
92         key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;
93         key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;
94         key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;
95         key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;
96         key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_SINT;
97         key.element[nr].output_offset = dst_offset;
98
99         ei++;
100         dst_offset += 4 * sizeof(int);
101      } else if (util_format_is_pure_uint(draw->pt.vertex_element[i].src_format)) {
102         key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
103         key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;
104         key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;
105         key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;
106         key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;
107         key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_UINT;
108         key.element[nr].output_offset = dst_offset;
109
110         ei++;
111         dst_offset += 4 * sizeof(unsigned);
112      } else {
113         key.element[nr].type = TRANSLATE_ELEMENT_NORMAL;
114         key.element[nr].input_format = draw->pt.vertex_element[ei].src_format;
115         key.element[nr].input_buffer = draw->pt.vertex_element[ei].vertex_buffer_index;
116         key.element[nr].input_offset = draw->pt.vertex_element[ei].src_offset;
117         key.element[nr].instance_divisor = draw->pt.vertex_element[ei].instance_divisor;
118         key.element[nr].output_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
119         key.element[nr].output_offset = dst_offset;
120
121         ei++;
122         dst_offset += 4 * sizeof(float);
123      }
124
125      nr++;
126   }
127
128   assert(dst_offset <= vertex_size);
129
130   key.nr_elements = nr;
131   key.output_stride = vertex_size;
132
133   if (!fetch->translate ||
134       translate_key_compare(&fetch->translate->key, &key) != 0)
135   {
136      translate_key_sanitize(&key);
137      fetch->translate = translate_cache_find(fetch->cache, &key);
138   }
139}
140
141
142void
143draw_pt_fetch_run(struct pt_fetch *fetch,
144                  const unsigned *elts,
145                  unsigned count,
146                  char *verts)
147{
148   struct draw_context *draw = fetch->draw;
149   struct translate *translate = fetch->translate;
150   unsigned i;
151
152   for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
153      translate->set_buffer(translate,
154			    i,
155			    ((char *)draw->pt.user.vbuffer[i].map +
156			     draw->pt.vertex_buffer[i].buffer_offset),
157			    draw->pt.vertex_buffer[i].stride,
158			    draw->pt.max_index);
159   }
160
161   translate->run_elts( translate,
162			elts,
163			count,
164                        draw->start_instance,
165                        draw->instance_id,
166			verts );
167}
168
169
170void
171draw_pt_fetch_run_linear(struct pt_fetch *fetch,
172                         unsigned start,
173                         unsigned count,
174                         char *verts)
175{
176   struct draw_context *draw = fetch->draw;
177   struct translate *translate = fetch->translate;
178   unsigned i;
179
180   for (i = 0; i < draw->pt.nr_vertex_buffers; i++) {
181      translate->set_buffer(translate,
182			    i,
183			    ((char *)draw->pt.user.vbuffer[i].map +
184			     draw->pt.vertex_buffer[i].buffer_offset),
185			    draw->pt.vertex_buffer[i].stride,
186			    draw->pt.max_index);
187   }
188
189   translate->run( translate,
190                   start,
191                   count,
192                   draw->start_instance,
193                   draw->instance_id,
194                   verts );
195}
196
197
198struct pt_fetch *
199draw_pt_fetch_create(struct draw_context *draw)
200{
201   struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
202   if (!fetch)
203      return NULL;
204
205   fetch->draw = draw;
206   fetch->cache = translate_cache_create();
207   if (!fetch->cache) {
208      FREE(fetch);
209      return NULL;
210   }
211
212   return fetch;
213}
214
215
216void
217draw_pt_fetch_destroy(struct pt_fetch *fetch)
218{
219   if (fetch->cache)
220      translate_cache_destroy(fetch->cache);
221
222   FREE(fetch);
223}
224