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 void
draw_pt_fetch_prepare(struct pt_fetch * fetch,unsigned vs_input_count,unsigned vertex_size,unsigned instance_id_index)55 draw_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 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 (unsigned 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(uint32_t);
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(int32_t);
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(uint32_t);
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 translate_key_sanitize(&key);
136 fetch->translate = translate_cache_find(fetch->cache, &key);
137 }
138 }
139
140 void
draw_pt_fetch_run(struct pt_fetch * fetch,const unsigned * elts,unsigned count,char * verts)141 draw_pt_fetch_run(struct pt_fetch *fetch,
142 const unsigned *elts,
143 unsigned count,
144 char *verts)
145 {
146 struct draw_context *draw = fetch->draw;
147 struct translate *translate = fetch->translate;
148
149 for (unsigned i = 0; i < draw->pt.nr_vertex_buffers; i++) {
150 translate->set_buffer(translate,
151 i,
152 ((char *)draw->pt.user.vbuffer[i].map +
153 draw->pt.vertex_buffer[i].buffer_offset),
154 draw->pt.vertex_strides[i],
155 draw->pt.max_index);
156 }
157
158 translate->run_elts(translate,
159 elts,
160 count,
161 draw->start_instance,
162 draw->instance_id,
163 verts);
164 }
165
166
167 void
draw_pt_fetch_run_linear(struct pt_fetch * fetch,unsigned start,unsigned count,char * verts)168 draw_pt_fetch_run_linear(struct pt_fetch *fetch,
169 unsigned start,
170 unsigned count,
171 char *verts)
172 {
173 struct draw_context *draw = fetch->draw;
174 struct translate *translate = fetch->translate;
175
176 for (unsigned i = 0; i < draw->pt.nr_vertex_buffers; i++) {
177 translate->set_buffer(translate,
178 i,
179 ((char *)draw->pt.user.vbuffer[i].map +
180 draw->pt.vertex_buffer[i].buffer_offset),
181 draw->pt.vertex_strides[i],
182 draw->pt.max_index);
183 }
184
185 translate->run(translate,
186 start,
187 count,
188 draw->start_instance,
189 draw->instance_id,
190 verts);
191 }
192
193
194 struct pt_fetch *
draw_pt_fetch_create(struct draw_context * draw)195 draw_pt_fetch_create(struct draw_context *draw)
196 {
197 struct pt_fetch *fetch = CALLOC_STRUCT(pt_fetch);
198 if (!fetch)
199 return NULL;
200
201 fetch->draw = draw;
202 fetch->cache = translate_cache_create();
203 if (!fetch->cache) {
204 FREE(fetch);
205 return NULL;
206 }
207
208 return fetch;
209 }
210
211
212 void
draw_pt_fetch_destroy(struct pt_fetch * fetch)213 draw_pt_fetch_destroy(struct pt_fetch *fetch)
214 {
215 if (fetch->cache)
216 translate_cache_destroy(fetch->cache);
217
218 FREE(fetch);
219 }
220