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