• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2007 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  /*
29   * Authors:
30   *   Keith Whitwell <keith@tungstengraphics.com>
31   *   Brian Paul
32   */
33 
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 
37 #include "pipe/p_shader_tokens.h"
38 
39 #include "draw_private.h"
40 #include "draw_context.h"
41 #include "draw_vs.h"
42 
43 #include "translate/translate.h"
44 #include "translate/translate_cache.h"
45 
46 #include "tgsi/tgsi_dump.h"
47 #include "tgsi/tgsi_exec.h"
48 
49 DEBUG_GET_ONCE_BOOL_OPTION(gallium_dump_vs, "GALLIUM_DUMP_VS", FALSE)
50 
51 
52 /**
53  * Set a vertex shader constant buffer.
54  * \param slot  which constant buffer in [0, PIPE_MAX_CONSTANT_BUFFERS-1]
55  * \param constants  the mapped buffer
56  * \param size  size of buffer in bytes
57  */
58 void
draw_vs_set_constants(struct draw_context * draw,unsigned slot,const void * constants,unsigned size)59 draw_vs_set_constants(struct draw_context *draw,
60                       unsigned slot,
61                       const void *constants,
62                       unsigned size)
63 {
64    const int alignment = 16;
65 
66    /* check if buffer is 16-byte aligned */
67    if (((uintptr_t)constants) & (alignment - 1)) {
68       /* if not, copy the constants into a new, 16-byte aligned buffer */
69       if (size > draw->vs.const_storage_size[slot]) {
70          if (draw->vs.aligned_constant_storage[slot]) {
71             align_free((void *)draw->vs.aligned_constant_storage[slot]);
72             draw->vs.const_storage_size[slot] = 0;
73          }
74          draw->vs.aligned_constant_storage[slot] =
75             align_malloc(size, alignment);
76          if (draw->vs.aligned_constant_storage[slot]) {
77             draw->vs.const_storage_size[slot] = size;
78          }
79       }
80       assert(constants);
81       if (draw->vs.aligned_constant_storage[slot]) {
82          memcpy((void *)draw->vs.aligned_constant_storage[slot],
83                 constants,
84                 size);
85       }
86       constants = draw->vs.aligned_constant_storage[slot];
87    }
88 
89    draw->vs.aligned_constants[slot] = constants;
90 }
91 
92 
draw_vs_set_viewport(struct draw_context * draw,const struct pipe_viewport_state * viewport)93 void draw_vs_set_viewport( struct draw_context *draw,
94                            const struct pipe_viewport_state *viewport )
95 {
96 }
97 
98 
99 
100 struct draw_vertex_shader *
draw_create_vertex_shader(struct draw_context * draw,const struct pipe_shader_state * shader)101 draw_create_vertex_shader(struct draw_context *draw,
102                           const struct pipe_shader_state *shader)
103 {
104    struct draw_vertex_shader *vs = NULL;
105 
106    if (draw->dump_vs) {
107       tgsi_dump(shader->tokens, 0);
108    }
109 
110 #if HAVE_LLVM
111    if (draw->pt.middle.llvm) {
112       vs = draw_create_vs_llvm(draw, shader);
113    }
114 #endif
115 
116    if (!vs) {
117       vs = draw_create_vs_exec( draw, shader );
118    }
119 
120    if (vs)
121    {
122       uint i;
123       bool found_clipvertex = FALSE;
124       for (i = 0; i < vs->info.num_outputs; i++) {
125          if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_POSITION &&
126              vs->info.output_semantic_index[i] == 0)
127             vs->position_output = i;
128          else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_EDGEFLAG &&
129              vs->info.output_semantic_index[i] == 0)
130             vs->edgeflag_output = i;
131          else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPVERTEX &&
132                   vs->info.output_semantic_index[i] == 0) {
133             found_clipvertex = TRUE;
134             vs->clipvertex_output = i;
135          } else if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_CLIPDIST) {
136             if (vs->info.output_semantic_index[i] == 0)
137                vs->clipdistance_output[0] = i;
138             else
139                vs->clipdistance_output[1] = i;
140          }
141       }
142       if (!found_clipvertex)
143          vs->clipvertex_output = vs->position_output;
144    }
145 
146    assert(vs);
147    return vs;
148 }
149 
150 
151 void
draw_bind_vertex_shader(struct draw_context * draw,struct draw_vertex_shader * dvs)152 draw_bind_vertex_shader(struct draw_context *draw,
153                         struct draw_vertex_shader *dvs)
154 {
155    draw_do_flush( draw, DRAW_FLUSH_STATE_CHANGE );
156 
157    if (dvs)
158    {
159       draw->vs.vertex_shader = dvs;
160       draw->vs.num_vs_outputs = dvs->info.num_outputs;
161       draw->vs.position_output = dvs->position_output;
162       draw->vs.edgeflag_output = dvs->edgeflag_output;
163       draw->vs.clipvertex_output = dvs->clipvertex_output;
164       draw->vs.clipdistance_output[0] = dvs->clipdistance_output[0];
165       draw->vs.clipdistance_output[1] = dvs->clipdistance_output[1];
166       dvs->prepare( dvs, draw );
167    }
168    else {
169       draw->vs.vertex_shader = NULL;
170       draw->vs.num_vs_outputs = 0;
171    }
172 }
173 
174 
175 void
draw_delete_vertex_shader(struct draw_context * draw,struct draw_vertex_shader * dvs)176 draw_delete_vertex_shader(struct draw_context *draw,
177                           struct draw_vertex_shader *dvs)
178 {
179    unsigned i;
180 
181    for (i = 0; i < dvs->nr_variants; i++)
182       dvs->variant[i]->destroy( dvs->variant[i] );
183 
184    dvs->nr_variants = 0;
185 
186    dvs->delete( dvs );
187 }
188 
189 
190 
191 boolean
draw_vs_init(struct draw_context * draw)192 draw_vs_init( struct draw_context *draw )
193 {
194    draw->dump_vs = debug_get_option_gallium_dump_vs();
195 
196    draw->vs.tgsi.machine = tgsi_exec_machine_create();
197    if (!draw->vs.tgsi.machine)
198       return FALSE;
199 
200    draw->vs.emit_cache = translate_cache_create();
201    if (!draw->vs.emit_cache)
202       return FALSE;
203 
204    draw->vs.fetch_cache = translate_cache_create();
205    if (!draw->vs.fetch_cache)
206       return FALSE;
207 
208    return TRUE;
209 }
210 
211 void
draw_vs_destroy(struct draw_context * draw)212 draw_vs_destroy( struct draw_context *draw )
213 {
214    uint i;
215 
216    if (draw->vs.fetch_cache)
217       translate_cache_destroy(draw->vs.fetch_cache);
218 
219    if (draw->vs.emit_cache)
220       translate_cache_destroy(draw->vs.emit_cache);
221 
222    for (i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
223       if (draw->vs.aligned_constant_storage[i]) {
224          align_free((void *)draw->vs.aligned_constant_storage[i]);
225       }
226    }
227 
228    tgsi_exec_machine_destroy(draw->vs.tgsi.machine);
229 }
230 
231 
232 struct draw_vs_variant *
draw_vs_lookup_variant(struct draw_vertex_shader * vs,const struct draw_vs_variant_key * key)233 draw_vs_lookup_variant( struct draw_vertex_shader *vs,
234                         const struct draw_vs_variant_key *key )
235 {
236    struct draw_vs_variant *variant;
237    unsigned i;
238 
239    /* Lookup existing variant:
240     */
241    for (i = 0; i < vs->nr_variants; i++)
242       if (draw_vs_variant_key_compare(key, &vs->variant[i]->key) == 0)
243          return vs->variant[i];
244 
245    /* Else have to create a new one:
246     */
247    variant = vs->create_variant( vs, key );
248    if (variant == NULL)
249       return NULL;
250 
251    /* Add it to our list, could be smarter:
252     */
253    if (vs->nr_variants < Elements(vs->variant)) {
254       vs->variant[vs->nr_variants++] = variant;
255    }
256    else {
257       vs->last_variant++;
258       vs->last_variant %= Elements(vs->variant);
259       vs->variant[vs->last_variant]->destroy(vs->variant[vs->last_variant]);
260       vs->variant[vs->last_variant] = variant;
261    }
262 
263    /* Done
264     */
265    return variant;
266 }
267 
268 
269 struct translate *
draw_vs_get_fetch(struct draw_context * draw,struct translate_key * key)270 draw_vs_get_fetch( struct draw_context *draw,
271                    struct translate_key *key )
272 {
273    if (!draw->vs.fetch ||
274        translate_key_compare(&draw->vs.fetch->key, key) != 0)
275    {
276       translate_key_sanitize(key);
277       draw->vs.fetch = translate_cache_find(draw->vs.fetch_cache, key);
278    }
279 
280    return draw->vs.fetch;
281 }
282 
283 struct translate *
draw_vs_get_emit(struct draw_context * draw,struct translate_key * key)284 draw_vs_get_emit( struct draw_context *draw,
285                   struct translate_key *key )
286 {
287    if (!draw->vs.emit ||
288        translate_key_compare(&draw->vs.emit->key, key) != 0)
289    {
290       translate_key_sanitize(key);
291       draw->vs.emit = translate_cache_find(draw->vs.emit_cache, key);
292    }
293 
294    return draw->vs.emit;
295 }
296