• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2007 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  /*
29   * Authors:
30   *   Keith Whitwell <keithw@vmware.com>
31   *   Brian Paul
32   */
33 
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "pipe/p_shader_tokens.h"
37 
38 #include "draw_private.h"
39 #include "draw_context.h"
40 #include "draw_vs.h"
41 
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_scan.h"
44 #include "tgsi/tgsi_exec.h"
45 
46 
47 struct exec_vertex_shader {
48    struct draw_vertex_shader base;
49    struct tgsi_exec_machine *machine;
50 };
51 
52 
53 static struct exec_vertex_shader *
exec_vertex_shader(struct draw_vertex_shader * vs)54 exec_vertex_shader(struct draw_vertex_shader *vs)
55 {
56    return (struct exec_vertex_shader *)vs;
57 }
58 
59 
60 /* Not required for run_linear.
61  */
62 static void
vs_exec_prepare(struct draw_vertex_shader * shader,struct draw_context * draw)63 vs_exec_prepare(struct draw_vertex_shader *shader,
64                 struct draw_context *draw)
65 {
66    struct exec_vertex_shader *evs = exec_vertex_shader(shader);
67 
68    debug_assert(!draw->llvm);
69    /* Specify the vertex program to interpret/execute.
70     * Avoid rebinding when possible.
71     */
72    if (evs->machine->Tokens != shader->state.tokens) {
73       tgsi_exec_machine_bind_shader(evs->machine,
74                                     shader->state.tokens,
75                                     draw->vs.tgsi.sampler,
76                                     draw->vs.tgsi.image,
77                                     draw->vs.tgsi.buffer);
78    }
79 }
80 
81 
82 
83 /**
84  * Simplified vertex shader interface for the pt paths.  Given the
85  * complexity of code-generating all the above operations together,
86  * it's time to try doing all the other stuff separately.
87  */
88 static void
vs_exec_run_linear(struct draw_vertex_shader * shader,const float (* input)[4],float (* output)[4],const void * constants[PIPE_MAX_CONSTANT_BUFFERS],const unsigned const_size[PIPE_MAX_CONSTANT_BUFFERS],unsigned count,unsigned input_stride,unsigned output_stride)89 vs_exec_run_linear(struct draw_vertex_shader *shader,
90                    const float (*input)[4],
91                    float (*output)[4],
92                    const void *constants[PIPE_MAX_CONSTANT_BUFFERS],
93                     const unsigned const_size[PIPE_MAX_CONSTANT_BUFFERS],
94                    unsigned count,
95                    unsigned input_stride,
96                    unsigned output_stride)
97 {
98    struct exec_vertex_shader *evs = exec_vertex_shader(shader);
99    struct tgsi_exec_machine *machine = evs->machine;
100    unsigned int i, j;
101    unsigned slot;
102    boolean clamp_vertex_color = shader->draw->rasterizer->clamp_vertex_color;
103 
104    debug_assert(!shader->draw->llvm);
105    tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,
106                                   constants, const_size);
107 
108    if (shader->info.uses_instanceid) {
109       unsigned i = machine->SysSemanticToIndex[TGSI_SEMANTIC_INSTANCEID];
110       assert(i < ARRAY_SIZE(machine->SystemValue));
111       for (j = 0; j < TGSI_QUAD_SIZE; j++)
112          machine->SystemValue[i].xyzw[0].i[j] = shader->draw->instance_id;
113    }
114 
115    for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
116       unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
117 
118       /* Swizzle inputs.
119        */
120       for (j = 0; j < max_vertices; j++) {
121 #if 0
122          debug_printf("%d) Input vert:\n", i + j);
123          for (slot = 0; slot < shader->info.num_inputs; slot++) {
124             debug_printf("\t%d: %f %f %f %f\n", slot,
125                          input[slot][0],
126                          input[slot][1],
127                          input[slot][2],
128                          input[slot][3]);
129          }
130 #endif
131 
132          if (shader->info.uses_vertexid) {
133             unsigned vid = machine->SysSemanticToIndex[TGSI_SEMANTIC_VERTEXID];
134             assert(vid < ARRAY_SIZE(machine->SystemValue));
135             machine->SystemValue[vid].xyzw[0].i[j] = i + j;
136             /* XXX this should include base vertex. Where to get it??? */
137          }
138          if (shader->info.uses_basevertex) {
139             unsigned vid = machine->SysSemanticToIndex[TGSI_SEMANTIC_BASEVERTEX];
140             assert(vid < ARRAY_SIZE(machine->SystemValue));
141             machine->SystemValue[vid].xyzw[0].i[j] = 0;
142             /* XXX Where to get it??? */
143          }
144          if (shader->info.uses_vertexid_nobase) {
145             unsigned vid = machine->SysSemanticToIndex[TGSI_SEMANTIC_VERTEXID_NOBASE];
146             assert(vid < ARRAY_SIZE(machine->SystemValue));
147             machine->SystemValue[vid].xyzw[0].i[j] = i + j;
148          }
149 
150          for (slot = 0; slot < shader->info.num_inputs; slot++) {
151 #if 0
152             assert(!util_is_inf_or_nan(input[slot][0]));
153             assert(!util_is_inf_or_nan(input[slot][1]));
154             assert(!util_is_inf_or_nan(input[slot][2]));
155             assert(!util_is_inf_or_nan(input[slot][3]));
156 #endif
157             machine->Inputs[slot].xyzw[0].f[j] = input[slot][0];
158             machine->Inputs[slot].xyzw[1].f[j] = input[slot][1];
159             machine->Inputs[slot].xyzw[2].f[j] = input[slot][2];
160             machine->Inputs[slot].xyzw[3].f[j] = input[slot][3];
161          }
162 
163          input = (const float (*)[4])((const char *)input + input_stride);
164       }
165 
166       machine->NonHelperMask = (1 << max_vertices) - 1;
167       /* run interpreter */
168       tgsi_exec_machine_run(machine, 0);
169 
170       /* Unswizzle all output results.
171        */
172       for (j = 0; j < max_vertices; j++) {
173          for (slot = 0; slot < shader->info.num_outputs; slot++) {
174             enum tgsi_semantic name = shader->info.output_semantic_name[slot];
175             if (clamp_vertex_color &&
176                 (name == TGSI_SEMANTIC_COLOR || name == TGSI_SEMANTIC_BCOLOR)) {
177                output[slot][0] = CLAMP(machine->Outputs[slot].xyzw[0].f[j], 0.0f, 1.0f);
178                output[slot][1] = CLAMP(machine->Outputs[slot].xyzw[1].f[j], 0.0f, 1.0f);
179                output[slot][2] = CLAMP(machine->Outputs[slot].xyzw[2].f[j], 0.0f, 1.0f);
180                output[slot][3] = CLAMP(machine->Outputs[slot].xyzw[3].f[j], 0.0f, 1.0f);
181             } else {
182                output[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
183                output[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
184                output[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
185                output[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
186             }
187          }
188 
189 #if 0
190          debug_printf("%d) Post xform vert:\n", i + j);
191          for (slot = 0; slot < shader->info.num_outputs; slot++) {
192             debug_printf("\t%d: %f %f %f %f\n", slot,
193                          output[slot][0],
194                          output[slot][1],
195                          output[slot][2],
196                          output[slot][3]);
197             assert(!util_is_inf_or_nan(output[slot][0]));
198          }
199 #endif
200 
201          output = (float (*)[4])((char *)output + output_stride);
202       }
203    }
204 }
205 
206 
207 static void
vs_exec_delete(struct draw_vertex_shader * dvs)208 vs_exec_delete(struct draw_vertex_shader *dvs)
209 {
210    FREE((void*) dvs->state.tokens);
211    FREE(dvs);
212 }
213 
214 
215 struct draw_vertex_shader *
draw_create_vs_exec(struct draw_context * draw,const struct pipe_shader_state * state)216 draw_create_vs_exec(struct draw_context *draw,
217                     const struct pipe_shader_state *state)
218 {
219    struct exec_vertex_shader *vs = CALLOC_STRUCT(exec_vertex_shader);
220 
221    if (!vs)
222       return NULL;
223 
224    /* we make a private copy of the tokens */
225    vs->base.state.tokens = tgsi_dup_tokens(state->tokens);
226    if (!vs->base.state.tokens) {
227       FREE(vs);
228       return NULL;
229    }
230 
231    tgsi_scan_shader(state->tokens, &vs->base.info);
232 
233    vs->base.state.stream_output = state->stream_output;
234    vs->base.draw = draw;
235    vs->base.prepare = vs_exec_prepare;
236    vs->base.run_linear = vs_exec_run_linear;
237    vs->base.delete = vs_exec_delete;
238    vs->base.create_variant = draw_vs_create_variant_generic;
239    vs->machine = draw->vs.tgsi.machine;
240 
241    return &vs->base;
242 }
243