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